Skip to content

Commit ceba4ef

Browse files
authored
Add a feature to return the directories and files that are being monitored (#374)
* Add a feature to return the directories and files that are being monitored * add WatchList() method for bsd and windows platforms * preallocate space for the array to be returned
1 parent 712fe1d commit ceba4ef

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

inotify.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ func (w *Watcher) Remove(name string) error {
163163
return nil
164164
}
165165

166+
// WatchList returns the directories and files that are being monitered.
167+
func (w *Watcher) WatchList() []string {
168+
w.mu.Lock()
169+
defer w.mu.Unlock()
170+
171+
entries := make([]string, 0, len(w.watches))
172+
for pathname := range w.watches {
173+
entries = append(entries, pathname)
174+
}
175+
176+
return entries
177+
}
178+
166179
type watch struct {
167180
wd uint32 // Watch descriptor (as returned by the inotify_add_watch() syscall)
168181
flags uint32 // inotify flags of this watch (see inotify(7) for the list of valid flags)

inotify_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,40 @@ func TestInotifyOverflow(t *testing.T) {
459459
numDirs*numFiles, creates)
460460
}
461461
}
462+
463+
func TestInotifyWatchList(t *testing.T) {
464+
testDir := tempMkdir(t)
465+
defer os.RemoveAll(testDir)
466+
testFile := filepath.Join(testDir, "testfile")
467+
468+
handle, err := os.Create(testFile)
469+
if err != nil {
470+
t.Fatalf("Create failed: %v", err)
471+
}
472+
handle.Close()
473+
474+
w, err := NewWatcher()
475+
if err != nil {
476+
t.Fatalf("Failed to create watcher: %v", err)
477+
}
478+
defer w.Close()
479+
480+
err = w.Add(testFile)
481+
if err != nil {
482+
t.Fatalf("Failed to add testFile: %v", err)
483+
}
484+
err = w.Add(testDir)
485+
if err != nil {
486+
t.Fatalf("Failed to add testDir: %v", err)
487+
}
488+
489+
value := w.WatchList()
490+
491+
w.mu.Lock()
492+
defer w.mu.Unlock()
493+
for _, entry := range value {
494+
if _, ok := w.watches[entry]; !ok {
495+
t.Fatal("return value of WatchList is not same as the expected")
496+
}
497+
}
498+
}

kqueue.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ func (w *Watcher) Remove(name string) error {
148148
return nil
149149
}
150150

151+
// WatchList returns the directories and files that are being monitered.
152+
func (w *Watcher) WatchList() []string {
153+
w.mu.Lock()
154+
defer w.mu.Unlock()
155+
156+
entries := make([]string, 0, len(w.watches))
157+
for pathname := range w.watches {
158+
entries = append(entries, pathname)
159+
}
160+
161+
return entries
162+
}
163+
151164
// Watch all events (except NOTE_EXTEND, NOTE_LINK, NOTE_REVOKE)
152165
const noteAllEvents = unix.NOTE_DELETE | unix.NOTE_WRITE | unix.NOTE_ATTRIB | unix.NOTE_RENAME
153166

windows.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ func (w *Watcher) Remove(name string) error {
9797
return <-in.reply
9898
}
9999

100+
// WatchList returns the directories and files that are being monitered.
101+
func (w *Watcher) WatchList() []string {
102+
w.mu.Lock()
103+
w.mu.Unlock()
104+
105+
entries := make([]string, 0, len(w.watches))
106+
for _, entry := range w.watches {
107+
for _, watchEntry := range entry {
108+
entries = append(entries, watchEntry.path)
109+
}
110+
}
111+
112+
return entries
113+
}
114+
100115
const (
101116
// Options for AddWatch
102117
sysFSONESHOT = 0x80000000

0 commit comments

Comments
 (0)