Skip to content

Commit

Permalink
Watch the *directory* for file deletions instead of the file itself.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davidsansome committed Sep 29, 2015
1 parent 760f3e6 commit e681532
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions watch/inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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():
Expand Down

0 comments on commit e681532

Please sign in to comment.