Skip to content

Commit

Permalink
Add a feature to return the directories and files that are being moni…
Browse files Browse the repository at this point in the history
…tored
  • Loading branch information
NitroCao committed Jul 28, 2021
1 parent 7f4cf4d commit e7e68f8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ func (w *Watcher) Remove(name string) error {
return nil
}

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

w.mu.Lock()
defer w.mu.Unlock()

for key := range w.watches {
dirs = append(dirs, key)
}

return dirs
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,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")
}
}
}

0 comments on commit e7e68f8

Please sign in to comment.