]> go.fuhry.dev Git - fsnotify.git/commitdiff
BSD - Added fake symlink test
authorChris Howey <chris@howey.me>
Sat, 28 Apr 2012 15:51:46 +0000 (10:51 -0500)
committerChris Howey <chris@howey.me>
Sat, 28 Apr 2012 15:51:46 +0000 (10:51 -0500)
README
example_test.go
fsnotify_bsd.go
fsnotify_test.go

diff --git a/README b/README
index 57128bf2debbbe918c381d7be5b2bb4f76d8fb14..afe1e82b38da4a9d23e16cba431b3afae298f936 100644 (file)
--- a/README
+++ b/README
@@ -7,14 +7,12 @@ BSD
 OSX
 
 Example:
+// Note that it is possible for Watch() to send errors over the Error channel.
     watcher, err := fsnotify.NewWatcher()
     if err != nil {
         log.Fatal(err)
     }
-    err = watcher.Watch("/tmp")
-    if err != nil {
-        log.Fatal(err)
-    }
+
     for {
         select {
         case ev := <-watcher.Event:
@@ -24,6 +22,11 @@ Example:
         }
     }
 
+    err = watcher.Watch("/tmp")
+    if err != nil {
+        log.Fatal(err)
+    }
+
 Notifications:
 IsCreate()
 IsDelete()
index 74dd759dc495e3cfb4aa0df80dcc62af8c9f0870..95ba62aaa81558e4565d0afb6ecc64fa8f176c29 100644 (file)
@@ -10,10 +10,7 @@ func ExampleNewWatcher() {
        if err != nil {
                log.Fatal(err)
        }
-       err = watcher.Watch("/tmp")
-       if err != nil {
-               log.Fatal(err)
-       }
+
        for {
                select {
                case ev := <-watcher.Event:
@@ -22,4 +19,9 @@ func ExampleNewWatcher() {
                        log.Println("error:", err)
                }
        }
+
+       err = watcher.Watch("/tmp")
+       if err != nil {
+               log.Fatal(err)
+       }
 }
index 320323cb6539a04afb4c61d5549298cf9d8276da..e06dfc11e6fd64943f8d535cba940414881ca9a1 100644 (file)
@@ -98,6 +98,24 @@ func (w *Watcher) addWatch(path string, flags uint32) error {
 
        watchfd, found := w.watches[path]
        if !found {
+               fi, errstat := os.Lstat(path)
+               if errstat != nil {
+                       return errstat
+               }
+
+               // Follow Symlinks
+               for fi.Mode()&os.ModeSymlink == os.ModeSymlink {
+                       path, errstat = os.Readlink(path)
+                       if errstat != nil {
+                               return errstat
+                       }
+
+                       fi, errstat = os.Lstat(path)
+                       if errstat != nil {
+                               return errstat
+                       }
+               }
+
                fd, errno := syscall.Open(path, syscall.O_NONBLOCK|syscall.O_RDONLY, 0700)
                if fd == -1 {
                        return errno
@@ -107,7 +125,6 @@ func (w *Watcher) addWatch(path string, flags uint32) error {
                w.watches[path] = watchfd
                w.paths[watchfd] = path
 
-               fi, _ := os.Stat(path)
                w.finfo[watchfd] = fi
                if fi.IsDir() {
                        w.watchDirectoryFiles(path)
index 3561599aa198d86c04b96402a9493b33106358ca..55220baedb59172c2e0b0ba69952d83809f40a7b 100644 (file)
@@ -217,6 +217,51 @@ func TestFsnotifyRename(t *testing.T) {
        os.Remove(testFileRenamed)
 }
 
+func TestFsnotifyFakeSymlink(t *testing.T) {
+       // Create an fsnotify watcher instance and initialize it
+       watcher, err := NewWatcher()
+       if err != nil {
+               t.Fatalf("NewWatcher() failed: %s", err)
+       }
+
+       const testDir string = "_test"
+
+       // Create directory to watch
+       if os.Mkdir(testDir, 0777) != nil {
+               t.Fatalf("Failed to create test directory: %s", err)
+       }
+       defer os.RemoveAll(testDir)
+
+       if os.Symlink("_test/zzz", "_test/zzznew") != nil {
+               t.Fatalf("Failed to create bogus symlink: %s", err)
+       }
+       t.Logf("Created bogus symlink")
+
+       var errorsReceived = 0
+       // Receive errors on the error channel on a separate goroutine
+       go func() {
+               for errors := range watcher.Error {
+                       t.Logf("Received error: %s", errors)
+                       errorsReceived++
+               }
+       }()
+
+       // Add a watch for testDir
+       err = watcher.Watch(testDir)
+       if err != nil {
+               t.Fatalf("Watcher.Watch() failed: %s", err)
+       }
+
+       // We expect this event to be received immediately, as they happen during Watch() call
+       if errorsReceived == 0 {
+               t.Fatal("fsnotify errors have not been received.")
+       }
+
+       // Try closing the fsnotify instance
+       t.Log("calling Close()")
+       watcher.Close()
+}
+
 func TestFsnotifyAttrib(t *testing.T) {
        // Create an fsnotify watcher instance and initialize it
        watcher, err := NewWatcher()