Skip to content

Commit d8c9419

Browse files
committed
Update package documentation; fix links
1 parent e1329dc commit d8c9419

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

README.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@ fanotify has features spanning different kernel versions -
1818
package main
1919
2020
import (
21-
"flag"
22-
"fmt"
23-
"os"
21+
"flag"
22+
"fmt"
23+
"os"
2424
25-
"github.com/opcoder0/fanotify"
25+
"github.com/opcoder0/fanotify"
2626
)
2727
2828
func main() {
29-
var listenPath string
30-
31-
flag.StringVar(&listenPath, "listen-path", "", "path to watch events")
32-
flag.Parse()
33-
if listenPath == "" {
34-
fmt.Println("missing listen path")
35-
os.Exit(1)
36-
}
37-
mountPoint := "/"
38-
listener, err := fanotify.NewListener(mountPoint, false)
39-
if err != nil {
40-
fmt.Println(err)
41-
os.Exit(1)
42-
}
43-
fmt.Println("Listening to events for:", listenPath)
29+
var listenPath string
30+
31+
flag.StringVar(&listenPath, "listen-path", "", "path to watch events")
32+
flag.Parse()
33+
if listenPath == "" {
34+
fmt.Println("missing listen path")
35+
os.Exit(1)
36+
}
37+
mountPoint := "/"
38+
listener, err := fanotify.NewListener(mountPoint, false)
39+
if err != nil {
40+
fmt.Println(err)
41+
os.Exit(1)
42+
}
43+
fmt.Println("Listening to events for:", listenPath)
4444
var eventTypes EventType
4545
eventTypes =
4646
fanotify.FileAccessed |
@@ -61,18 +61,18 @@ func main() {
6161
fanotify.FileOrDirectoryMovedTo |
6262
fanotify.WatchedFileMoved |
6363
fanotify.WatchedFileOrDirectoryMoved
64-
listener.AddWatch(listenPath, eventTypes)
65-
go listener.Start()
66-
i := 1
67-
for event := range listener.Events {
68-
fmt.Println(event)
69-
if i == 5 {
70-
fmt.Println("Enough events. Stopping...")
71-
listener.Stop()
72-
break
73-
}
74-
i++
75-
}
64+
listener.AddWatch(listenPath, eventTypes)
65+
go listener.Start()
66+
i := 1
67+
for event := range listener.Events {
68+
fmt.Println(event)
69+
if i == 5 {
70+
fmt.Println("Enough events. Stopping...")
71+
listener.Stop()
72+
break
73+
}
74+
i++
75+
}
7676
}
7777
```
7878

fanotify_api.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616
// ErrCapSysAdmin indicates caller is missing CAP_SYS_ADMIN permissions
1717
ErrCapSysAdmin = errors.New("require CAP_SYS_ADMIN capability")
1818
// ErrInvalidFlagCombination indicates the bit/combination of flags are invalid
19-
ErrInvalidFlagCombination = errors.New("invalid flag bits")
19+
ErrInvalidFlagCombination = errors.New("invalid flag bitmask")
2020
// ErrNilListener indicates the listener is nil
2121
ErrNilListener = errors.New("nil listener")
2222
// ErrUnsupportedOnKernelVersion indicates the feature/flag is unavailable for the current kernel version
@@ -36,8 +36,8 @@ type Event struct {
3636
// Path holds the name of the parent directory
3737
Path string
3838
// FileName holds the name of the file under the watched parent. The value is only available
39-
// when NewListener is created by passing `true` with `withName` argument. The feature is available
40-
// only with kernels 5.9 or higher.
39+
// on kernels 5.1 or greater (that support the receipt of events which contain additional information
40+
// about the underlying filesystem object correlated to an event).
4141
FileName string
4242
// EventTypes holds bit mask representing the operations
4343
EventTypes EventType
@@ -46,7 +46,7 @@ type Event struct {
4646
}
4747

4848
// Listener represents a fanotify notification group that holds a list of files,
49-
// directories and filesystems under a given mountpoint for which events shall be created.
49+
// directories or a mountpoint for which events shall be created.
5050
type Listener struct {
5151
// fd returned by fanotify_init
5252
fd int
@@ -73,13 +73,13 @@ type Listener struct {
7373
// For cases where multiple mountpoints need to be monitored
7474
// multiple listener instances need to be used.
7575
//
76-
// mountpoint can be any file/directory under the mount point being watched.
77-
// Passing "true" to the entireMount flag monitors the entire mount point for marked
76+
// mountPoint can be any file/directory under the mount point being watched.
77+
// Passing "true" to the entireMount parameter monitors the entire mount point for marked
7878
// events. Passing "false" allows specifying multiple paths (files/directories)
7979
// under this mount point for monitoring filesystem events.
8080
//
8181
// The function returns a new instance of the listener. The fanotify flags are set
82-
// based on the running kernel version. ErrCapSysAdmin is returned if the process does not
82+
// based on the running kernel version. [ErrCapSysAdmin] is returned if the process does not
8383
// have CAP_SYS_ADM capability.
8484
//
8585
// - For Linux kernel version 5.0 and earlier no additional information about the underlying filesystem object is available.
@@ -146,13 +146,12 @@ func (l *Listener) Stop() {
146146
close(l.Events)
147147
}
148148

149-
// MarkMount adds, modifies or removes the fanotify mark (passed in as eventTypes) for the entire
150-
// mountpoint. Passing true to remove, removes the mark from the mountpoint.
149+
// MarkMount adds, modifies or removes the fanotify mark (eventTypes) for the entire
150+
// mount point. Passing true to remove, removes the mark from the mount point.
151151
// This method returns an [ErrWatchPath] if the listener was not initialized to monitor
152-
// the entire mountpoint. To mark specific files or directories use [AddWatch] method.
152+
// the entire mount point. To mark specific files or directories use [AddWatch] method.
153153
// The entire mount cannot be monitored for the following events:
154-
// [FileCreated], [FileAttribChanged], [FileMovedFrom],
155-
// [FileMovedTo], [WatchedFileDeleted]
154+
// [FileCreated], [FileAttribChanged], [FileMovedFrom], [FileMovedTo], [WatchedFileDeleted]
156155
// Passing any of these flags in eventTypes will return [ErrInvalidFlagCombination] error
157156
func (l *Listener) MarkMount(eventTypes EventType, remove bool) error {
158157
if l.entireMount == false {
@@ -174,9 +173,9 @@ func (l *Listener) MarkMount(eventTypes EventType, remove bool) error {
174173
// AddWatch adds or modifies the fanotify mark for the specified path.
175174
// The events are only raised for the specified directory and does raise events
176175
// for subdirectories. Calling AddWatch to mark the entire mountpoint results in
177-
// [os.ErrInvalid]. To mark the entire mountpoint use [MarkMount] method.
176+
// [os.ErrInvalid]. To mark the entire mount point use [MarkMount] method.
178177
// Certain flag combinations are known to cause issues.
179-
// - [FileCreated] cannot be or-ed / combined with FileClosed. The fanotify system does not generate any event for this combination.
178+
// - [FileCreated] cannot be or-ed / combined with [FileClosed]. The fanotify system does not generate any event for this combination.
180179
// - [FileOpened] with any of the event types containing OrDirectory causes an event flood for the directory and then stopping raising any events at all.
181180
// - [FileOrDirectoryOpened] with any of the other event types causes an event flood for the directory and then stopping raising any events at all.
182181
func (l *Listener) AddWatch(path string, eventTypes EventType) error {
@@ -187,8 +186,8 @@ func (l *Listener) AddWatch(path string, eventTypes EventType) error {
187186
}
188187

189188
// DeleteWatch removes or modifies the fanotify mark for the specified path.
190-
// Calling DeleteWatch on the listener initialized to monitor the entire mountpoint
191-
// results in [os.ErrInvalid]. To modify the mark for the entire mountpoint use [MarkMount] method.
189+
// Calling DeleteWatch on the listener initialized to monitor the entire mount point
190+
// results in [os.ErrInvalid]. To modify the mark for the entire mount point use [MarkMount] method.
192191
func (l *Listener) DeleteWatch(parentDir string, eventTypes EventType) error {
193192
if l.entireMount {
194193
return os.ErrInvalid
@@ -208,18 +207,18 @@ func (l *Listener) ClearWatch() error {
208207
return nil
209208
}
210209

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
210+
// Has returns true if event types (e) contains the passed in event type (et).
211+
func (e EventType) Has(et EventType) bool {
212+
return e&et == et
214213
}
215214

216215
// 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
216+
func (e EventType) Or(et EventType) EventType {
217+
return e | et
219218
}
220219

221220
// String prints event types
222-
func (a EventType) String() string {
221+
func (e EventType) String() string {
223222
var eventTypes = map[EventType]string{
224223
unix.FAN_ACCESS: "Access",
225224
unix.FAN_MODIFY: "Modify",
@@ -237,7 +236,7 @@ func (a EventType) String() string {
237236
}
238237
var eventTypeList []string
239238
for k, v := range eventTypes {
240-
if a.Has(k) {
239+
if e.Has(k) {
241240
eventTypeList = append(eventTypeList, v)
242241
}
243242
}

0 commit comments

Comments
 (0)