Skip to content

Commit e1329dc

Browse files
committed
Refactor: rename Action -> EventType
1 parent 1fcf735 commit e1329dc

File tree

6 files changed

+128
-124
lines changed

6 files changed

+128
-124
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func main() {
4141
os.Exit(1)
4242
}
4343
fmt.Println("Listening to events for:", listenPath)
44-
var actions Action
45-
actions =
44+
var eventTypes EventType
45+
eventTypes =
4646
fanotify.FileAccessed |
4747
fanotify.FileOrDirectoryAccessed |
4848
fanotify.FileModified |
@@ -61,7 +61,7 @@ func main() {
6161
fanotify.FileOrDirectoryMovedTo |
6262
fanotify.WatchedFileMoved |
6363
fanotify.WatchedFileOrDirectoryMoved
64-
listener.AddWatch(listenPath, actions)
64+
listener.AddWatch(listenPath, eventTypes)
6565
go listener.Start()
6666
i := 1
6767
for event := range listener.Events {
@@ -78,13 +78,13 @@ func main() {
7878

7979
## Known Issues
8080

81-
Certain flag combinations / actions cause issues with event reporting.
81+
Certain flag combinations / event types cause issues with event reporting.
8282

8383
- `fanotify.FileCreated` (`unix.FAN_CREATE`) cannot be or-ed / combined with `fanotify.FileClosed` (`unix.FAN_CLOSE_WRITE` or `unix.FAN_CLOSE_NOWRITE`). The `fanotify` event notification group does not generate any event for this combination.
8484

85-
- Using `fanotify.FileOpened` with any of the actions containing `OrDirectory` (`unix.FAN_ONDIR`) causes an event flood for the directory and then stopping raising any events at all.
85+
- Using `fanotify.FileOpened` with any of the event types containing `OrDirectory` (`unix.FAN_ONDIR`) causes an event flood for the directory and then stopping raising any events at all.
8686

87-
- `fanotifyFileOrDirectoryOpened` with any of the other actions causes an event flood for the directory and then stopping raising any events at all.
87+
- `fanotifyFileOrDirectoryOpened` with any of the other event types causes an event flood for the directory and then stopping raising any events at all.
8888

8989
## Tests
9090

doc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func ExampleListener_AddWatch() {
2323

2424
func ExampleListener_AddWatch_all() {
2525
var listener *fanotify.Listener
26-
var actions fanotify.Action
26+
var eventTypes fanotify.EventType
2727

2828
listener, err := fanotify.NewListener("/", false)
2929
if err != nil {
3030
log.Fatal("Cannot create listener for path /", err)
3131
}
32-
actions = fanotify.FileAccessed |
32+
eventTypes = fanotify.FileAccessed |
3333
fanotify.FileOrDirectoryAccessed |
3434
fanotify.FileModified |
3535
fanotify.FileOpenedForExec |
@@ -47,5 +47,5 @@ func ExampleListener_AddWatch_all() {
4747
fanotify.FileOrDirectoryMovedTo |
4848
fanotify.WatchedFileMoved |
4949
fanotify.WatchedFileOrDirectoryMoved
50-
listener.AddWatch("/home/user", actions)
50+
listener.AddWatch("/home/user", eventTypes)
5151
}

fanotify_api.go

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ var (
2525
ErrWatchPath = errors.New("missing watch path")
2626
)
2727

28-
// Action represents an event / operation on a particular file/directory
29-
type Action uint64
28+
// EventType represents an event / operation on a particular file/directory
29+
type EventType uint64
3030

3131
// Event represents a notification from the kernel for the file, directory
3232
// or a filesystem marked for watching.
@@ -39,8 +39,8 @@ type Event struct {
3939
// when NewListener is created by passing `true` with `withName` argument. The feature is available
4040
// only with kernels 5.9 or higher.
4141
FileName string
42-
// Actions holds bit mask representing the operations
43-
Actions Action
42+
// EventTypes holds bit mask representing the operations
43+
EventTypes EventType
4444
// Pid Process ID of the process that caused the event
4545
Pid int
4646
}
@@ -146,25 +146,29 @@ func (l *Listener) Stop() {
146146
close(l.Events)
147147
}
148148

149-
// MarkMount adds, modifies or removes the fanotify mark (passed in as action) for the entire
149+
// MarkMount adds, modifies or removes the fanotify mark (passed in as eventTypes) for the entire
150150
// mountpoint. Passing true to remove, removes the mark from the mountpoint.
151151
// This method returns an [ErrWatchPath] if the listener was not initialized to monitor
152152
// the entire mountpoint. To mark specific files or directories use [AddWatch] method.
153153
// The entire mount cannot be monitored for the following events:
154154
// [FileCreated], [FileAttribChanged], [FileMovedFrom],
155155
// [FileMovedTo], [WatchedFileDeleted]
156-
// Passing any of these flags in action will return [ErrInvalidFlagCombination] error
157-
func (l *Listener) MarkMount(action Action, remove bool) error {
156+
// Passing any of these flags in eventTypes will return [ErrInvalidFlagCombination] error
157+
func (l *Listener) MarkMount(eventTypes EventType, remove bool) error {
158158
if l.entireMount == false {
159159
return ErrWatchPath
160160
}
161-
if action.Has(FileCreated) || action.Has(FileAttribChanged) || action.Has(FileMovedFrom) || action.Has(FileMovedTo) || action.Has(WatchedFileDeleted) {
161+
if eventTypes.Has(FileCreated) ||
162+
eventTypes.Has(FileAttribChanged) ||
163+
eventTypes.Has(FileMovedFrom) ||
164+
eventTypes.Has(FileMovedTo) ||
165+
eventTypes.Has(WatchedFileDeleted) {
162166
return ErrInvalidFlagCombination
163167
}
164168
if remove {
165-
return l.fanotifyMark(l.mountpoint.Name(), unix.FAN_MARK_REMOVE|unix.FAN_MARK_MOUNT, uint64(action), false)
169+
return l.fanotifyMark(l.mountpoint.Name(), unix.FAN_MARK_REMOVE|unix.FAN_MARK_MOUNT, uint64(eventTypes), false)
166170
}
167-
return l.fanotifyMark(l.mountpoint.Name(), unix.FAN_MARK_ADD|unix.FAN_MARK_MOUNT, uint64(action), false)
171+
return l.fanotifyMark(l.mountpoint.Name(), unix.FAN_MARK_ADD|unix.FAN_MARK_MOUNT, uint64(eventTypes), false)
168172
}
169173

170174
// AddWatch adds or modifies the fanotify mark for the specified path.
@@ -173,26 +177,26 @@ func (l *Listener) MarkMount(action Action, remove bool) error {
173177
// [os.ErrInvalid]. To mark the entire mountpoint use [MarkMount] method.
174178
// Certain flag combinations are known to cause issues.
175179
// - [FileCreated] cannot be or-ed / combined with FileClosed. The fanotify system does not generate any event for this combination.
176-
// - [FileOpened] with any of the actions containing OrDirectory causes an event flood for the directory and then stopping raising any events at all.
177-
// - [FileOrDirectoryOpened] with any of the other actions causes an event flood for the directory and then stopping raising any events at all.
178-
func (l *Listener) AddWatch(path string, action Action) error {
180+
// - [FileOpened] with any of the event types containing OrDirectory causes an event flood for the directory and then stopping raising any events at all.
181+
// - [FileOrDirectoryOpened] with any of the other event types causes an event flood for the directory and then stopping raising any events at all.
182+
func (l *Listener) AddWatch(path string, eventTypes EventType) error {
179183
if l.entireMount {
180184
return os.ErrInvalid
181185
}
182-
return l.fanotifyMark(path, unix.FAN_MARK_ADD, uint64(action|unix.FAN_EVENT_ON_CHILD), false)
186+
return l.fanotifyMark(path, unix.FAN_MARK_ADD, uint64(eventTypes|unix.FAN_EVENT_ON_CHILD), false)
183187
}
184188

185189
// DeleteWatch removes or modifies the fanotify mark for the specified path.
186190
// Calling DeleteWatch on the listener initialized to monitor the entire mountpoint
187191
// results in [os.ErrInvalid]. To modify the mark for the entire mountpoint use [MarkMount] method.
188-
func (l *Listener) DeleteWatch(parentDir string, action Action) error {
192+
func (l *Listener) DeleteWatch(parentDir string, eventTypes EventType) error {
189193
if l.entireMount {
190194
return os.ErrInvalid
191195
}
192-
return l.fanotifyMark(parentDir, unix.FAN_MARK_REMOVE, uint64(action|unix.FAN_EVENT_ON_CHILD), false)
196+
return l.fanotifyMark(parentDir, unix.FAN_MARK_REMOVE, uint64(eventTypes|unix.FAN_EVENT_ON_CHILD), false)
193197
}
194198

195-
// ClearWatch stops watching for all actions
199+
// ClearWatch stops watching for all event types
196200
func (l *Listener) ClearWatch() error {
197201
if l == nil {
198202
return ErrNilListener
@@ -204,19 +208,19 @@ func (l *Listener) ClearWatch() error {
204208
return nil
205209
}
206210

207-
// Has returns true if actions contains the passed in action (a).
208-
func (actions Action) Has(a Action) bool {
209-
return actions&a == a
211+
// Has returns true if event types contains the passed in event type (et).
212+
func (eventTypes EventType) Has(et EventType) bool {
213+
return eventTypes&et == et
210214
}
211215

212-
// Or appends the specified action to the set of actions to watch for
213-
func (actions Action) Or(a Action) Action {
214-
return actions | a
216+
// Or appends the specified event types to the set of event types to watch for
217+
func (eventTypes EventType) Or(et EventType) EventType {
218+
return eventTypes | et
215219
}
216220

217-
// String prints action
218-
func (a Action) String() string {
219-
var actions = map[Action]string{
221+
// String prints event types
222+
func (a EventType) String() string {
223+
var eventTypes = map[EventType]string{
220224
unix.FAN_ACCESS: "Access",
221225
unix.FAN_MODIFY: "Modify",
222226
unix.FAN_CLOSE_WRITE: "CloseWrite",
@@ -231,15 +235,15 @@ func (a Action) String() string {
231235
unix.FAN_MOVED_TO: "MovedTo",
232236
unix.FAN_MOVE_SELF: "SelfMove",
233237
}
234-
var actionList []string
235-
for k, v := range actions {
238+
var eventTypeList []string
239+
for k, v := range eventTypes {
236240
if a.Has(k) {
237-
actionList = append(actionList, v)
241+
eventTypeList = append(eventTypeList, v)
238242
}
239243
}
240-
return strings.Join(actionList, ",")
244+
return strings.Join(eventTypeList, ",")
241245
}
242246

243247
func (e Event) String() string {
244-
return fmt.Sprintf("Fd:(%d), Pid:(%d), Action:(%s), Path:(%s), Filename:(%s)", e.Fd, e.Pid, e.Actions, e.Path, e.FileName)
248+
return fmt.Sprintf("Fd:(%d), Pid:(%d), EventType:(%s), Path:(%s), Filename:(%s)", e.Fd, e.Pid, e.EventTypes, e.Path, e.FileName)
245249
}

fanotify_event.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func fanotifyMarkMaskValid(mask uint64) error {
9898
return n&k == k
9999
}
100100
if isSet(mask, unix.FAN_MARK_MOUNT) && (isSet(mask, unix.FAN_CREATE) || isSet(mask, unix.FAN_ATTRIB) || isSet(mask, unix.FAN_MOVE) || isSet(mask, unix.FAN_DELETE_SELF)) {
101-
return errors.New("mountpoint cannot be watched for create, attrib, move or delete self actions")
101+
return errors.New("mountpoint cannot be watched for create, attrib, move or delete self event types")
102102
}
103103
return nil
104104
}
@@ -379,10 +379,10 @@ func (l *Listener) readEvents() error {
379379
mask = mask ^ unix.FAN_ONDIR
380380
}
381381
event := Event{
382-
Fd: int(metadata.Fd),
383-
Path: string(name[:n1]),
384-
Actions: Action(mask),
385-
Pid: int(metadata.Pid),
382+
Fd: int(metadata.Fd),
383+
Path: string(name[:n1]),
384+
EventTypes: EventType(mask),
385+
Pid: int(metadata.Pid),
386386
}
387387
l.Events <- event
388388
} else {
@@ -424,11 +424,11 @@ func (l *Listener) readEvents() error {
424424
mask = mask ^ unix.FAN_ONDIR
425425
}
426426
event := Event{
427-
Fd: fd,
428-
Path: pathName,
429-
FileName: fileName,
430-
Actions: Action(mask),
431-
Pid: int(metadata.Pid),
427+
Fd: fd,
428+
Path: pathName,
429+
FileName: fileName,
430+
EventTypes: EventType(mask),
431+
Pid: int(metadata.Pid),
432432
}
433433
l.Events <- event
434434
i += int(metadata.Event_len)

fanotify_event_types.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,93 @@ import "golang.org/x/sys/unix"
44

55
const (
66
// FileAccessed event when a file is accessed
7-
FileAccessed Action = unix.FAN_ACCESS
7+
FileAccessed EventType = unix.FAN_ACCESS
88

99
// FileOrDirectoryAccessed event when a file or directory is accessed
10-
FileOrDirectoryAccessed Action = unix.FAN_ACCESS | unix.FAN_ONDIR
10+
FileOrDirectoryAccessed EventType = unix.FAN_ACCESS | unix.FAN_ONDIR
1111

1212
// FileModified event when a file is modified
13-
FileModified Action = unix.FAN_MODIFY
13+
FileModified EventType = unix.FAN_MODIFY
1414

1515
// FileClosedAfterWrite event when a file is closed
16-
FileClosedAfterWrite Action = unix.FAN_CLOSE_WRITE
16+
FileClosedAfterWrite EventType = unix.FAN_CLOSE_WRITE
1717

1818
// FileClosedWithNoWrite event when a file is closed without writing
19-
FileClosedWithNoWrite Action = unix.FAN_CLOSE_NOWRITE
19+
FileClosedWithNoWrite EventType = unix.FAN_CLOSE_NOWRITE
2020

2121
// FileClosed event when a file is closed after write or no write
22-
FileClosed Action = unix.FAN_CLOSE_WRITE | unix.FAN_CLOSE_NOWRITE
22+
FileClosed EventType = unix.FAN_CLOSE_WRITE | unix.FAN_CLOSE_NOWRITE
2323

2424
// FileOpened event when a file is opened
25-
// BUG Using FileOpened flag with any OrDirectory actions
25+
// BUG Using FileOpened flag with any OrDirectory event types
2626
// causes an event flood and complete stoppage of events. The flag
2727
// can be used with other file only flags or by itself
2828
// without any errors/issues.
29-
FileOpened Action = unix.FAN_OPEN
29+
FileOpened EventType = unix.FAN_OPEN
3030

3131
// FileOrDirectoryOpened event when a file or directory is opened
3232
// BUG Using FileOrDirectoryOpened causes an event flood and complete
3333
// stoppage of events. The flag by itself without any errors/issues.
34-
FileOrDirectoryOpened Action = unix.FAN_OPEN | unix.FAN_ONDIR
34+
FileOrDirectoryOpened EventType = unix.FAN_OPEN | unix.FAN_ONDIR
3535

3636
// FileOpenedForExec event when a file is opened with the intent to be executed.
3737
// Requires Linux kernel 5.0 or later
38-
FileOpenedForExec Action = unix.FAN_OPEN_EXEC
38+
FileOpenedForExec EventType = unix.FAN_OPEN_EXEC
3939

4040
// FileAttribChanged event when a file attribute has changed
4141
// Requires Linux kernel 5.1 or later (requires FID)
42-
FileAttribChanged Action = unix.FAN_ATTRIB
42+
FileAttribChanged EventType = unix.FAN_ATTRIB
4343

4444
// FileOrDirectoryAttribChanged event when a file or directory attribute has changed
4545
// Requires Linux kernel 5.1 or later (requires FID)
46-
FileOrDirectoryAttribChanged Action = unix.FAN_ATTRIB | unix.FAN_ONDIR
46+
FileOrDirectoryAttribChanged EventType = unix.FAN_ATTRIB | unix.FAN_ONDIR
4747

4848
// FileCreated event when file a has been created
4949
// Requires Linux kernel 5.1 or later (requires FID)
5050
// BUG FileCreated does not work with FileClosed, FileClosedAfterWrite or FileClosedWithNoWrite
51-
FileCreated Action = unix.FAN_CREATE
51+
FileCreated EventType = unix.FAN_CREATE
5252

5353
// FileOrDirectoryCreated event when a file or directory has been created
5454
// Requires Linux kernel 5.1 or later (requires FID)
55-
FileOrDirectoryCreated Action = unix.FAN_CREATE | unix.FAN_ONDIR
55+
FileOrDirectoryCreated EventType = unix.FAN_CREATE | unix.FAN_ONDIR
5656

5757
// FileDeleted event when file a has been deleted
5858
// Requires Linux kernel 5.1 or later (requires FID)
59-
FileDeleted Action = unix.FAN_DELETE
59+
FileDeleted EventType = unix.FAN_DELETE
6060

6161
// FileOrDirectoryDeleted event when a file or directory has been deleted
6262
// Requires Linux kernel 5.1 or later (requires FID)
63-
FileOrDirectoryDeleted Action = unix.FAN_DELETE | unix.FAN_ONDIR
63+
FileOrDirectoryDeleted EventType = unix.FAN_DELETE | unix.FAN_ONDIR
6464

6565
// WatchedFileDeleted event when a watched file has been deleted
6666
// Requires Linux kernel 5.1 or later (requires FID)
67-
WatchedFileDeleted Action = unix.FAN_DELETE_SELF
67+
WatchedFileDeleted EventType = unix.FAN_DELETE_SELF
6868

6969
// WatchedFileOrDirectoryDeleted event when a watched file or directory has been deleted
7070
// Requires Linux kernel 5.1 or later (requires FID)
71-
WatchedFileOrDirectoryDeleted Action = unix.FAN_DELETE_SELF | unix.FAN_ONDIR
71+
WatchedFileOrDirectoryDeleted EventType = unix.FAN_DELETE_SELF | unix.FAN_ONDIR
7272

7373
// FileMovedFrom event when a file has been moved from the watched directory
7474
// Requires Linux kernel 5.1 or later (requires FID)
75-
FileMovedFrom Action = unix.FAN_MOVED_FROM
75+
FileMovedFrom EventType = unix.FAN_MOVED_FROM
7676

7777
// FileOrDirectoryMovedFrom event when a file or directory has been moved from the watched directory
7878
// Requires Linux kernel 5.1 or later (requires FID)
79-
FileOrDirectoryMovedFrom Action = unix.FAN_MOVED_FROM | unix.FAN_ONDIR
79+
FileOrDirectoryMovedFrom EventType = unix.FAN_MOVED_FROM | unix.FAN_ONDIR
8080

8181
// FileMovedTo event when a file has been moved to the watched directory
8282
// Requires Linux kernel 5.1 or later (requires FID)
83-
FileMovedTo Action = unix.FAN_MOVED_TO
83+
FileMovedTo EventType = unix.FAN_MOVED_TO
8484

8585
// FileOrDirectoryMovedTo event when a file or directory has been moved to the watched directory
8686
// Requires Linux kernel 5.1 or later (requires FID)
87-
FileOrDirectoryMovedTo Action = unix.FAN_MOVED_TO | unix.FAN_ONDIR
87+
FileOrDirectoryMovedTo EventType = unix.FAN_MOVED_TO | unix.FAN_ONDIR
8888

8989
// WatchedFileMoved event when a watched file has moved
9090
// Requires Linux kernel 5.1 or later (requires FID)
91-
WatchedFileMoved Action = unix.FAN_MOVE_SELF
91+
WatchedFileMoved EventType = unix.FAN_MOVE_SELF
9292

9393
// WatchedFileOrDirectoryMoved event when a watched file or directory has moved
9494
// Requires Linux kernel 5.1 or later (requires FID)
95-
WatchedFileOrDirectoryMoved Action = unix.FAN_MOVE_SELF | unix.FAN_ONDIR
95+
WatchedFileOrDirectoryMoved EventType = unix.FAN_MOVE_SELF | unix.FAN_ONDIR
9696
)

0 commit comments

Comments
 (0)