]> go.fuhry.dev Git - fsnotify.git/commitdiff
Add a feature to return the directories and files that are being monitored (#374)
authorNitroCao <17915615+NitroCao@users.noreply.github.com>
Thu, 21 Apr 2022 06:46:51 +0000 (14:46 +0800)
committerGitHub <noreply@github.com>
Thu, 21 Apr 2022 06:46:51 +0000 (15:46 +0900)
* 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

inotify.go
inotify_test.go
kqueue.go
windows.go

index eb87699b5b4c15494d394afbd9dd689341a38e9b..a6d0e0ec8c10e1720ab8cf1ef5bee16b6efef4ee 100644 (file)
@@ -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 {
+       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)
index f121c0144699922f7cadfcd98325db626d0859d9..bb6db097234fc4f361a72f5f29233d5e7c6ef953 100644 (file)
@@ -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")
+               }
+       }
+}
index 368f5b790d4de625f4e953dfb1001f60590d27f3..6fb8d8532e7f365dfcebf460fbe5e96367a14140 100644 (file)
--- a/kqueue.go
+++ b/kqueue.go
@@ -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
 
index b0105ae4cb0148ecba7f5383c6757f6b877f3b67..ddc69ef879ba2aef78c642a879721d54cad22f4c 100644 (file)
@@ -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