From 451fb35f93a5195d77c32124e80ab9f19a24402e Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 26 Sep 2015 03:28:05 -0400 Subject: [PATCH] Add missing watch event for files If a file was supposed to exist in a directory, and it didn't exist yet, when it gets created, we should notice, and cause an event so that we wake up and actually see about then creating that file! --- file.go | 4 ++++ misc.go | 11 +++++++++++ misc_test.go | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/file.go b/file.go index 677b6178..3c8fc9fc 100644 --- a/file.go +++ b/file.go @@ -176,6 +176,10 @@ func (obj FileType) Watch(v *Vertex) { } if delta_depth < 0 { + log.Println("Parent!") + if PathPrefixDelta(safename, event.Name) == 1 { // we're the parent dir + send = true + } watcher.Remove(current) index++ } diff --git a/misc.go b/misc.go index e7c79a1c..eff0355b 100644 --- a/misc.go +++ b/misc.go @@ -52,6 +52,17 @@ func HasPathPrefix(p, prefix string) bool { return true } +// Delta of path prefix, tells you how many path tokens different the prefix is +func PathPrefixDelta(p, prefix string) int { + + if !HasPathPrefix(p, prefix) { + return -1 + } + patharray := PathSplit(p) + prefixarray := PathSplit(prefix) + return len(patharray) - len(prefixarray) +} + func PathIsDir(p string) bool { return p[len(p)-1:] == "/" // a dir has a trailing slash in this context } diff --git a/misc_test.go b/misc_test.go index 634f5c52..4cac564d 100644 --- a/misc_test.go +++ b/misc_test.go @@ -82,6 +82,41 @@ func TestMiscT3(t *testing.T) { func TestMiscT4(t *testing.T) { + if PathPrefixDelta("/foo/bar/baz", "/foo/ba") != -1 { + t.Errorf("Result should be -1.") + } + + if PathPrefixDelta("/foo/bar/baz", "/foo/bar") != 1 { + t.Errorf("Result should be 1.") + } + + if PathPrefixDelta("/foo/bar/baz", "/foo/bar/") != 1 { + t.Errorf("Result should be 1.") + } + + if PathPrefixDelta("/foo/bar/baz/", "/foo/bar") != 1 { + t.Errorf("Result should be 1.") + } + + if PathPrefixDelta("/foo/bar/baz/", "/foo/bar/") != 1 { + t.Errorf("Result should be 1.") + } + + if PathPrefixDelta("/foo/bar/baz/", "/foo/bar/baz/dude") != -1 { + t.Errorf("Result should be -1.") + } + + if PathPrefixDelta("/foo/bar/baz/a/b/c/", "/foo/bar/baz") != 3 { + t.Errorf("Result should be 3.") + } + + if PathPrefixDelta("/foo/bar/baz/", "/foo/bar/baz") != 0 { + t.Errorf("Result should be 0.") + } +} + +func TestMiscT5(t *testing.T) { + if PathIsDir("/foo/bar/baz/") != true { t.Errorf("Result should be false.") } @@ -97,5 +132,4 @@ func TestMiscT4(t *testing.T) { if PathIsDir("/") != true { t.Errorf("Result should be true.") } - }