Skip to content

Commit

Permalink
Fix handling of moved / renamed files in watched directories
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-ward committed Dec 28, 2021
1 parent b3c552d commit 01830c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.8.1

### Bug fixes

- fixed handling of moved / renamed files within watched directories when
using the `--enable-fs-watch` option.

## 0.8

### General changes
Expand Down
24 changes: 22 additions & 2 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
return
}

if !((event.Op&fsnotify.Write == fsnotify.Write) ||
if !((event.Op&fsnotify.Create == fsnotify.Create) ||
(event.Op&fsnotify.Write == fsnotify.Write) ||
(event.Op&fsnotify.Remove == fsnotify.Remove) ||
(event.Op&fsnotify.Rename == fsnotify.Rename)) {
continue
Expand All @@ -156,13 +157,24 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
continue
}

if event.Op&fsnotify.Write == fsnotify.Write {
if (event.Op&fsnotify.Create == fsnotify.Create) ||
(event.Op&fsnotify.Write == fsnotify.Write) {
// This event may get called multiple times while a file is being copied into a watched directory,
// so we debounce this instead.
c <- path
continue
}

if (event.Op&fsnotify.Remove == fsnotify.Remove) || (event.Op&fsnotify.Rename == fsnotify.Rename) {
// some file move events trigger remove / rename, so if the file still exists, assume it is
// one of these
_, err := os.Stat(path)
if err == nil {
// debounce to give it a little more time to update, if needed
c <- path
continue
}

// remove tileset immediately so that there are not other errors in request handlers
id, err := w.generateID(path, baseDir)
if err != nil {
Expand Down Expand Up @@ -202,3 +214,11 @@ func (w *FSWatcher) WatchDir(baseDir string) error {

return nil
}

func exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return false
}
return true
}

0 comments on commit 01830c1

Please sign in to comment.