Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature to return the directories and files that are being monitored #374

Merged
merged 4 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions inotify.go
Expand Up @@ -163,6 +163,19 @@ func (w *Watcher) Remove(name string) error {
return nil
}

// WatchList returns the directories and files that are being monitered.
func (w *Watcher) WatchList() []string {
NitroCao marked this conversation as resolved.
Show resolved Hide resolved
w.mu.Lock()
defer w.mu.Unlock()

entries := make([]string, 0, len(w.watches))
for pathname := range w.watches {
entries = append(entries, pathname)
}

return entries
}

type watch struct {
wd uint32 // Watch descriptor (as returned by the inotify_add_watch() syscall)
flags uint32 // inotify flags of this watch (see inotify(7) for the list of valid flags)
Expand Down
37 changes: 37 additions & 0 deletions inotify_test.go
Expand Up @@ -459,3 +459,40 @@ func TestInotifyOverflow(t *testing.T) {
numDirs*numFiles, creates)
}
}

func TestInotifyWatchList(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testFile := filepath.Join(testDir, "testfile")

handle, err := os.Create(testFile)
if err != nil {
t.Fatalf("Create failed: %v", err)
}
handle.Close()

w, err := NewWatcher()
if err != nil {
t.Fatalf("Failed to create watcher: %v", err)
}
defer w.Close()

err = w.Add(testFile)
if err != nil {
t.Fatalf("Failed to add testFile: %v", err)
}
err = w.Add(testDir)
if err != nil {
t.Fatalf("Failed to add testDir: %v", err)
}

value := w.WatchList()

w.mu.Lock()
defer w.mu.Unlock()
for _, entry := range value {
if _, ok := w.watches[entry]; !ok {
t.Fatal("return value of WatchList is not same as the expected")
}
}
}
13 changes: 13 additions & 0 deletions kqueue.go
Expand Up @@ -148,6 +148,19 @@ func (w *Watcher) Remove(name string) error {
return nil
}

// WatchList returns the directories and files that are being monitered.
func (w *Watcher) WatchList() []string {
w.mu.Lock()
defer w.mu.Unlock()

entries := make([]string, 0, len(w.watches))
for pathname := range w.watches {
entries = append(entries, pathname)
}

return entries
}

// Watch all events (except NOTE_EXTEND, NOTE_LINK, NOTE_REVOKE)
const noteAllEvents = unix.NOTE_DELETE | unix.NOTE_WRITE | unix.NOTE_ATTRIB | unix.NOTE_RENAME

Expand Down
15 changes: 15 additions & 0 deletions windows.go
Expand Up @@ -97,6 +97,21 @@ func (w *Watcher) Remove(name string) error {
return <-in.reply
}

// WatchList returns the directories and files that are being monitered.
func (w *Watcher) WatchList() []string {
w.mu.Lock()
w.mu.Unlock()

entries := make([]string, 0, len(w.watches))
for _, entry := range w.watches {
for _, watchEntry := range entry {
entries = append(entries, watchEntry.path)
}
}

return entries
}

const (
// Options for AddWatch
sysFSONESHOT = 0x80000000
Expand Down