mirror of
https://github.com/docker/compose.git
synced 2025-07-29 08:34:15 +02:00
watch: add watch function that traverses up directory structure recursively (#1013)
This commit is contained in:
parent
e8a34c8d1e
commit
0482f9276a
@ -5,6 +5,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -309,6 +310,29 @@ func TestWatchBothDirAndFile(t *testing.T) {
|
|||||||
f.assertEvents(fileB)
|
f.assertEvents(fileB)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWatchNonexistentDirectory(t *testing.T) {
|
||||||
|
f := newNotifyFixture(t)
|
||||||
|
defer f.tearDown()
|
||||||
|
|
||||||
|
root := f.JoinPath("root")
|
||||||
|
err := os.Mkdir(root, 0777)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
parent := f.JoinPath("root", "parent")
|
||||||
|
file := f.JoinPath("root", "parent", "a")
|
||||||
|
|
||||||
|
f.watch(file)
|
||||||
|
f.fsync()
|
||||||
|
f.events = nil
|
||||||
|
f.WriteFile(file, "hello")
|
||||||
|
if runtime.GOOS == "darwin" {
|
||||||
|
f.assertEvents(file)
|
||||||
|
} else {
|
||||||
|
f.assertEvents(parent, file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type notifyFixture struct {
|
type notifyFixture struct {
|
||||||
*tempdir.TempDirFixture
|
*tempdir.TempDirFixture
|
||||||
notify Notify
|
notify Notify
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
package watch
|
package watch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -31,12 +32,11 @@ func (d *naiveNotify) Add(name string) error {
|
|||||||
|
|
||||||
// if it's a file that doesn't exist, watch its parent
|
// if it's a file that doesn't exist, watch its parent
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
parent := filepath.Join(name, "..")
|
err, fileWatched := d.watchUpRecursively(name)
|
||||||
err = d.watcher.Add(parent)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "notify.Add(%q)", name)
|
return errors.Wrapf(err, "watchUpRecursively(%q)", name)
|
||||||
}
|
}
|
||||||
d.watchList[parent] = true
|
d.watchList[fileWatched] = true
|
||||||
} else if fi.IsDir() {
|
} else if fi.IsDir() {
|
||||||
err = d.watchRecursively(name)
|
err = d.watchRecursively(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,6 +71,24 @@ func (d *naiveNotify) watchRecursively(dir string) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *naiveNotify) watchUpRecursively(path string) (error, string) {
|
||||||
|
if path == string(filepath.Separator) {
|
||||||
|
return fmt.Errorf("cannot watch root directory"), ""
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return errors.Wrapf(err, "os.Stat(%q)", path), ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
parent := filepath.Dir(path)
|
||||||
|
return d.watchUpRecursively(parent)
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.watcher.Add(path), path
|
||||||
|
}
|
||||||
|
|
||||||
func (d *naiveNotify) Close() error {
|
func (d *naiveNotify) Close() error {
|
||||||
return d.watcher.Close()
|
return d.watcher.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user