Skip to content

Commit

Permalink
Add sanity check to fsnotify paths (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiat authored Oct 13, 2023
1 parent 17a593d commit 2cb8794
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/pipeline/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Watch(

proc := &watchProcess{
verbose: verbose,
rootDirs: dirs,
watcher: watcher,
watchFilter: watchFilter,
trackedPaths: ds.NewSet[string](1024),
Expand Down Expand Up @@ -68,6 +69,7 @@ func Watch(

type watchProcess struct {
verbose bool
rootDirs []string
watcher *fsnotify.Watcher
watchFilter *filesystem.FilterTree

Expand Down Expand Up @@ -109,6 +111,10 @@ func (proc *watchProcess) handleEvent(event fsnotify.Event) *ds.Set[string] {
}

func (proc *watchProcess) startWatching(root string) *ds.Set[string] {
if !proc.isSanePath(root) {
return nil
}

result := ds.NewSet[string](1)

filesystem.Traverse(root, func(p string, isDir bool, err error) error {
Expand Down Expand Up @@ -150,6 +156,10 @@ func (proc *watchProcess) startWatching(root string) *ds.Set[string] {
}

func (proc *watchProcess) stopWatching(root string) *ds.Set[string] {
if !proc.isSanePath(root) {
return nil
}

result := ds.NewSet[string](1)

for p := range proc.trackedPaths.Unbox() {
Expand Down Expand Up @@ -186,6 +196,18 @@ func (proc *watchProcess) isTracked(path string) bool {
return proc.trackedPaths.Contains(path)
}

// isSanePath returns whether the path appears to be legit, since fsnotify
// has issues on some platforms where after a Remove it starts emitting
// empty paths or ones relative to root.
func (proc *watchProcess) isSanePath(path string) bool {
for _, root := range proc.rootDirs {
if strings.HasPrefix(path, root) {
return true
}
}
return false
}

func (proc *watchProcess) logFSWatchEvent(event fsnotify.Event) {
if proc.verbose {
log.Printf("Filesystem watch event: %s", event)
Expand Down

0 comments on commit 2cb8794

Please sign in to comment.