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:
}
}
+ err = watcher.Watch("/tmp")
+ if err != nil {
+ log.Fatal(err)
+ }
+
Notifications:
IsCreate()
IsDelete()
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
w.watches[path] = watchfd
w.paths[watchfd] = path
- fi, _ := os.Stat(path)
w.finfo[watchfd] = fi
if fi.IsDir() {
w.watchDirectoryFiles(path)
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()