From e6815324582c19ed8d618808a330333805f9d54f Mon Sep 17 00:00:00 2001 From: David Sansome Date: Tue, 29 Sep 2015 18:04:25 +1000 Subject: [PATCH] Watch the *directory* for file deletions instead of the file itself. inotify works on the inode, not the path. Deleting the file doesn't touch the inode, so doesn't generate an event on the file. Fixes #57 --- watch/inotify.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/watch/inotify.go b/watch/inotify.go index 55c8fab..dde26b7 100644 --- a/watch/inotify.go +++ b/watch/inotify.go @@ -59,15 +59,22 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error { func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileChanges { changes := NewFileChanges() - err := fw.w.Watch(fw.Filename) - if err != nil { + if err := fw.w.Watch(fw.Filename); err != nil { util.Fatal("Error watching %v: %v", fw.Filename, err) } + // Watch the directory to be notified when the file is deleted since the file + // watch is on the inode, not the path. + dirname := filepath.Dir(fw.Filename) + if err := fw.w.WatchFlags(dirname, fsnotify.FSN_DELETE); err != nil { + util.Fatal("Error watching %v: %v", dirname, err) + } + fw.Size = fi.Size() go func() { defer fw.w.RemoveWatch(fw.Filename) + defer fw.w.RemoveWatch(dirname) defer changes.Close() for { @@ -87,6 +94,9 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh switch { case evt.IsDelete(): + if filepath.Base(evt.Name) != filepath.Base(fw.Filename) { + continue + } fallthrough case evt.IsRename():