summaryrefslogtreecommitdiff
path: root/internal/file/file.go
blob: 364ee330e5be26b9a1a3a1f515c97380d79a675d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package file

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type File interface {
	String() string
	Name() string
	Flags() string
}

type fdFile struct {
	fd    int32
	name  string
	flags int32
}

func NewFd(fd int32, name []byte, flags int32) fdFile {
	return fdFile{fd, stringValue(name), flags}
}

func NewFdWithPid(fd int32, pid uint32) fdFile {
	linkName, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%d", pid, fd))
	if err != nil {
		return fdFile{fd, "?", -1}
	}
	flags, _ := readFlagsFromFdInfo(fd, pid)
	return fdFile{fd, linkName, flags}
}

func readFlagsFromFdInfo(fd int32, pid uint32) (int32, error) {
	data, err := os.ReadFile(fmt.Sprintf("/proc/%d/fdinfo/%d", pid, fd))
	if err != nil {
		return -1, err
	}
	scanner := bufio.NewScanner(bytes.NewReader(data))
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "flags:") {
			flagsStr := strings.Fields(line)[1]
			flags, err := strconv.ParseUint(flagsStr, 8, 32)
			return int32(flags), err
		}
	}
	return -1, scanner.Err()
}

func (f fdFile) Name() string {
	return f.name
}

func (f fdFile) Flags() string {
	return flagsToStr(f.flags)
}

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(",")
		sb.WriteString(f.Flags())
		sb.WriteString(")")
	}

	return sb.String()
}

type oldnameNewnameFile struct {
	Oldname, Newname string
}

func NewOldnameNewname(oldname, newname []byte) oldnameNewnameFile {
	return oldnameNewnameFile{stringValue(oldname), stringValue(newname)}
}

func (f oldnameNewnameFile) Name() string {
	return f.Newname
}

func (f oldnameNewnameFile) Flags() string {
	return ""
}

func (f oldnameNewnameFile) String() string {
	var sb strings.Builder

	sb.WriteString("old:")
	sb.WriteString(f.Oldname)
	sb.WriteString(" ->new:")
	sb.WriteString(f.Newname)
	sb.WriteString(" (")
	sb.WriteString(f.Flags())
	sb.WriteString(")")

	return sb.String()
}

type pathnameFile struct {
	Pathname string
}

func NewPathname(pathname []byte) pathnameFile {
	return pathnameFile{stringValue(pathname)}
}

func (f pathnameFile) Name() string {
	return f.Pathname
}

func (f pathnameFile) Flags() string {
	return ""
}

func (f pathnameFile) String() string {
	var sb strings.Builder

	sb.WriteString("pathname:")
	sb.WriteString(f.Pathname)
	sb.WriteString(" (")
	sb.WriteString(f.Flags())
	sb.WriteString(")")

	return sb.String()
}

// As data comes in from arrays, converted to slices, there will be null-bytes at the end..
func stringValue(byteStr []byte) string {
	// TODO: Hopefully, this won't cause a panic when the filename is as long as the array itself. Unit test this!
	return string(byteStr[:bytes.IndexByte(byteStr, 0)])
}