summaryrefslogtreecommitdiff
path: root/internal/file/file.go
blob: ab67fe296f428d717c8295f3ec11fa64f69fdb42 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package file

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

	"ior/internal/types"
)

// File is the common interface for file-like syscall payload representations.
//
// Implementations may represent either a live file descriptor-backed handle
// (FdFile) or partial path metadata for syscalls that do not resolve to a
// stable descriptor (for example rename-like or pathname-only events).
//
// Semantics:
//   - Name returns the best single-path identifier for the event. For
//     rename-like events this is the "new" path; for pathname-only events it is
//     the observed pathname.
//   - Flags returns open flags when known, otherwise unknownFlag.
//   - FD returns the tracked file descriptor when one exists, otherwise -1.
//   - String returns a human-readable representation suitable for TUI/CSV use
//     and should remain informative even when Name or FD are unavailable.
type File interface {
	String() string
	Name() string
	Flags() Flags
	FD() int32
}

// FdFile represents a file descriptor-backed file reference.
type FdFile struct {
	fd              int32
	name            string
	flags           Flags
	flagsFromProcFS bool
}

// NewFd constructs an FdFile from explicit descriptor metadata.
func NewFd(fd int32, name string, flags int32) *FdFile {
	f := &FdFile{
		fd:    fd,
		name:  name,
		flags: Flags(flags),
	}
	if f.flags == -1 {
		f.flags = unknownFlag
	}
	return f
}

// NewFdWithPid resolves descriptor metadata from /proc/<pid>/fd.
func NewFdWithPid(fd int32, pid uint32) *FdFile {
	f := &FdFile{
		fd: fd,
	}
	var err error

	procPath := fmt.Sprintf("/proc/%d/fd/%d", pid, fd)
	f.name, err = os.Readlink(procPath)
	if err != nil {
		f.name = ""
		f.flags = unknownFlag
		f.flagsFromProcFS = true
		return f
	}

	flags, err := readFlagsFromFdInfo(fd, pid)
	if err != nil {
		f.flags = unknownFlag
	} else {
		f.flags = flags
	}
	f.flagsFromProcFS = true

	return f
}

func (f *FdFile) Dup(fd int32) *FdFile {
	dupFd := *f
	dupFd.fd = fd
	return &dupFd
}

func readFlagsFromFdInfo(fd int32, pid uint32) (Flags, error) {
	data, err := os.ReadFile(fmt.Sprintf("/proc/%d/fdinfo/%d", pid, fd))
	if err != nil {
		return unknownFlag, err
	}
	return parseFlagsFromFdInfo(data)
}

func parseFlagsFromFdInfo(data []byte) (Flags, error) {
	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 Flags(flags), err
		}
	}
	if err := scanner.Err(); err != nil {
		return unknownFlag, err
	}
	return unknownFlag, fmt.Errorf("flags field not found in fdinfo")
}

func (f *FdFile) Name() string {
	return f.name
}

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

	if len(f.name) == 0 {
		sb.WriteString("E:name") // Emtpy name string
	} else {
		sb.WriteString(f.name)
	}
	sb.WriteString("%(")
	sb.WriteString(strconv.FormatInt(int64(f.fd), 10))
	sb.WriteString(",")
	sb.WriteString(f.Flags().String())
	sb.WriteString(")")

	return sb.String()
}

func (f *FdFile) Flags() Flags {
	return f.flags
}

func (f *FdFile) FD() int32 {
	return f.fd
}

func (f *FdFile) SetFlags(flags int32) {
	f.flags = Flags(flags)
}

func (f *FdFile) AddFlags(flags int32) {
	f.flags = Flags(int32(f.flags) | flags)
}

type oldnameNewnameFile struct {
	Oldname, Newname string
}

// NewOldnameNewname creates a file representation for rename-like syscalls.
func NewOldnameNewname(oldname, newname []byte) oldnameNewnameFile {
	return oldnameNewnameFile{types.StringValue(oldname), types.StringValue(newname)}
}

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

func (f oldnameNewnameFile) Flags() Flags {
	return unknownFlag
}

func (f oldnameNewnameFile) FD() int32 {
	return -1
}

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().String())
	sb.WriteString(")")

	return sb.String()
}

type pathnameFile struct {
	Pathname string
}

// NewPathname creates a path-only file representation.
func NewPathname(pathname []byte) pathnameFile {
	return pathnameFile{types.StringValue(pathname)}
}

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

func (f pathnameFile) Flags() Flags {
	return unknownFlag
}

func (f pathnameFile) FD() int32 {
	return -1
}

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

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

	return sb.String()
}