From 37111d473ee6e2219c296e420402d73ac066ddf6 Mon Sep 17 00:00:00 2001 From: llescouzeres Date: Sun, 8 Jul 2018 15:41:30 +0200 Subject: [PATCH 1/3] Change event to access old and new path on events --- watcher.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/watcher.go b/watcher.go index c2ea19f..57969f2 100644 --- a/watcher.go +++ b/watcher.go @@ -62,7 +62,8 @@ func (e Op) String() string { // directory and the type of event that's occurred and the full path of the file. type Event struct { Op - Path string + Path string + OldPath string os.FileInfo } @@ -547,14 +548,14 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, select { case <-cancel: return - case evt <- Event{Write, path, info}: + case evt <- Event{Write, path, path, info}: } } if oldInfo.Mode() != info.Mode() { select { case <-cancel: return - case evt <- Event{Chmod, path, info}: + case evt <- Event{Chmod, path, path, info}: } } } @@ -565,7 +566,8 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, if sameFile(info1, info2) { e := Event{ Op: Move, - Path: fmt.Sprintf("%s -> %s", path1, path2), + Path: path2, + OldPath: path1, FileInfo: info1, } // If they are from the same directory, it's a rename @@ -591,14 +593,14 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, select { case <-cancel: return - case evt <- Event{Create, path, info}: + case evt <- Event{Create, path, "", info}: } } for path, info := range removes { select { case <-cancel: return - case evt <- Event{Remove, path, info}: + case evt <- Event{Remove, "", path, info}: } } } From c23a36d070398e155e4ec693d3a72869f3f9531b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lescouz=C3=A8res?= Date: Fri, 14 Dec 2018 13:53:27 +0100 Subject: [PATCH 2/3] Add test to check for Path and OldPath values --- watcher_test.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/watcher_test.go b/watcher_test.go index 3fb831c..59894a6 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -571,6 +571,15 @@ func TestEventAddFile(t *testing.T) { files[event.Name()] = true events++ + // Check Path and OldPath content + newFile := filepath.Join(testDir, event.Name()) + if event.Path != newFile { + t.Errorf("Event.Path should be %s but got %s", newFile, event.Path) + } + if event.OldPath != "" { + t.Errorf("Event.OldPath should be empty on create, but got %s", event.OldPath) + } + if events == len(files) { return } @@ -668,6 +677,9 @@ func TestEventRenameFile(t *testing.T) { testDir, teardown := setup(t) defer teardown() + srcFilename := "file.txt" + dstFilename := "file1.txt" + w := New() w.FilterOps(Rename) @@ -678,8 +690,8 @@ func TestEventRenameFile(t *testing.T) { // Rename a file. if err := os.Rename( - filepath.Join(testDir, "file.txt"), - filepath.Join(testDir, "file1.txt"), + filepath.Join(testDir, srcFilename), + filepath.Join(testDir, dstFilename), ); err != nil { t.Error(err) } @@ -695,6 +707,17 @@ func TestEventRenameFile(t *testing.T) { if event.Op != Rename { t.Errorf("expected event to be Rename, got %s", event.Op) } + + // Check Path and OldPath content + oldFile := filepath.Join(testDir, srcFilename) + newFile := filepath.Join(testDir, dstFilename) + if event.Path != newFile { + t.Errorf("Event.Path should be %s but got %s", newFile, event.Path) + } + if event.OldPath != oldFile { + t.Errorf("Event.OldPath should %s but got %s", oldFile, event.OldPath) + } + case <-time.After(time.Millisecond * 250): t.Fatal("received no rename event") } From 7f55b912d65cbffcd0bd54c0964f896d49c95f77 Mon Sep 17 00:00:00 2001 From: Benjamin Radovsky Date: Sat, 17 Aug 2019 10:37:52 +1000 Subject: [PATCH 3/3] Keep path as well as old path for remove event. --- watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/watcher.go b/watcher.go index 57969f2..7aeae50 100644 --- a/watcher.go +++ b/watcher.go @@ -600,7 +600,7 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, select { case <-cancel: return - case evt <- Event{Remove, "", path, info}: + case evt <- Event{Remove, path, path, info}: } } }