summaryrefslogtreecommitdiff
path: root/internal/file
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-03-11 09:52:30 +0200
committerPaul Buetow <paul@buetow.org>2025-03-11 09:52:30 +0200
commitf9d8d2d9a107204fddf13b8f60845e817076d1f8 (patch)
treeb38437d816f589dc18246290c95124fe25962e5e /internal/file
parenta7f6f047de1e0ae56a0ef3a4c74e86f4f8f6eeb7 (diff)
refactor and initial tree
Diffstat (limited to 'internal/file')
-rw-r--r--internal/file/file.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/internal/file/file.go b/internal/file/file.go
new file mode 100644
index 0000000..4d9afab
--- /dev/null
+++ b/internal/file/file.go
@@ -0,0 +1,84 @@
+package file
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+ "strings"
+)
+
+type File interface {
+ String() string
+ Name() string
+}
+
+type FdFile struct {
+ fd int32
+ name string
+}
+
+func NewFd(fd int32, name string) FdFile {
+ return FdFile{fd, name}
+}
+
+func NewFdWithPid(fd int32, pid uint32) FdFile {
+ if linkName, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%d", pid, fd)); err == nil {
+ return FdFile{fd, linkName}
+ }
+ return FdFile{fd, "?"}
+}
+
+func (f FdFile) Name() string {
+ return f.name
+}
+
+func (f FdFile) String() string {
+ var sb strings.Builder
+
+ if len(f.name) == 0 {
+ sb.WriteString("?")
+ } else {
+ sb.WriteString(f.name)
+ sb.WriteString(" (")
+ sb.WriteString(strconv.FormatInt(int64(f.fd), 10))
+ sb.WriteString(")")
+ }
+
+ return sb.String()
+}
+
+type OldnameNewnameFile struct {
+ Oldname, Newname string
+}
+
+func (f OldnameNewnameFile) Name() string {
+ return f.Newname
+}
+
+func (f OldnameNewnameFile) String() string {
+ var sb strings.Builder
+
+ sb.WriteString("old:")
+ sb.WriteString(f.Oldname)
+ sb.WriteString(" ->new:")
+ sb.WriteString(f.Newname)
+
+ return sb.String()
+}
+
+type PathnameFile struct {
+ Pathname string
+}
+
+func (f PathnameFile) Name() string {
+ return f.Pathname
+}
+
+func (f PathnameFile) String() string {
+ var sb strings.Builder
+
+ sb.WriteString("pathname:")
+ sb.WriteString(f.Pathname)
+
+ return sb.String()
+}