Skip to content

Commit c5bce8b

Browse files
nicksndeloof
authored andcommitted
watch: fix a spurious error (docker#344)
1 parent c8a358a commit c5bce8b

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

pkg/watch/notify_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,32 @@ func TestWriteGoodLink(t *testing.T) {
277277
f.assertEvents(goodFile, link)
278278
}
279279

280+
func TestWatchBrokenLink(t *testing.T) {
281+
f := newNotifyFixture(t)
282+
defer f.tearDown()
283+
284+
newRoot, err := NewDir(t.Name())
285+
if err != nil {
286+
t.Fatal(err)
287+
}
288+
defer newRoot.TearDown()
289+
290+
link := filepath.Join(newRoot.Path(), "brokenLink")
291+
missingFile := filepath.Join(newRoot.Path(), "missingFile")
292+
err = os.Symlink(missingFile, link)
293+
if err != nil {
294+
t.Fatal(err)
295+
}
296+
297+
err = f.notify.Add(newRoot.Path())
298+
if err != nil {
299+
t.Fatal(err)
300+
}
301+
302+
os.Remove(link)
303+
f.assertEvents(link)
304+
}
305+
280306
type notifyFixture struct {
281307
t *testing.T
282308
root *TempDir

pkg/watch/watcher_linux.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package watch
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"log"
67
"os"
@@ -28,26 +29,27 @@ type linuxNotify struct {
2829
func (d *linuxNotify) Add(name string) error {
2930
fi, err := os.Stat(name)
3031
if err != nil && !os.IsNotExist(err) {
31-
return err
32+
return fmt.Errorf("notify.Add(%q): %v", name, err)
3233
}
34+
3335
// if it's a file that doesn't exist watch it's parent
3436
if os.IsNotExist(err) {
3537
parent := filepath.Join(name, "..")
3638
err = d.watcher.Add(parent)
3739
if err != nil {
38-
return err
40+
return fmt.Errorf("notify.Add(%q): %v", name, err)
3941
}
4042
d.watchList[parent] = true
4143
} else if fi.IsDir() {
4244
err = d.watchRecursively(name)
4345
if err != nil {
44-
return err
46+
return fmt.Errorf("notify.Add(%q): %v", name, err)
4547
}
4648
d.watchList[name] = true
4749
} else {
4850
err = d.watcher.Add(name)
4951
if err != nil {
50-
return err
52+
return fmt.Errorf("notify.Add(%q): %v", name, err)
5153
}
5254
d.watchList[name] = true
5355
}
@@ -61,7 +63,14 @@ func (d *linuxNotify) watchRecursively(dir string) error {
6163
return err
6264
}
6365

64-
return d.watcher.Add(path)
66+
err = d.watcher.Add(path)
67+
if err != nil {
68+
if os.IsNotExist(err) {
69+
return nil
70+
}
71+
return fmt.Errorf("watcher.Add(%q): %v", path, err)
72+
}
73+
return nil
6574
})
6675
}
6776

0 commit comments

Comments
 (0)