Skip to content

Commit

Permalink
Don't fail when listing dir content that is ignored (syncthing#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
imsodin authored Apr 20, 2018
1 parent f067ae0 commit b9ceffc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
34 changes: 24 additions & 10 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package notify

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -60,7 +59,7 @@ func (nd node) Add(name string) node {
return nd.addchild(name, name[i:])
}

func (nd node) AddDir(fn walkFunc) error {
func (nd node) AddDir(fn walkFunc, doNotWatch DoNotWatchFn) error {
stack := []node{nd}
Traverse:
for n := len(stack); n != 0; n = len(stack) {
Expand All @@ -78,13 +77,25 @@ Traverse:
}
// TODO(rjeczalik): tolerate open failures - add failed names to
// AddDirError and notify users which names are not added to the tree.
fi, err := ioutil.ReadDir(nd.Name)
f, err := os.Open(nd.Name)
if err != nil {
return err
}
for _, fi := range fi {
names, err := f.Readdirnames(-1)
f.Close()
if err != nil {
return err
}
for _, name := range names {
name = filepath.Join(nd.Name, name)
if doNotWatch != nil && doNotWatch(name) {
continue
}
fi, err := os.Lstat(name)
if err != nil {
return err
}
if fi.Mode()&(os.ModeSymlink|os.ModeDir) == os.ModeDir {
name := filepath.Join(nd.Name, fi.Name())
stack = append(stack, nd.addchild(name, name[len(nd.Name)+1:]))
}
}
Expand Down Expand Up @@ -141,7 +152,7 @@ func (nd node) Del(name string) error {
return nil
}

func (nd node) Walk(fn walkFunc) error {
func (nd node) Walk(fn walkFunc, doNotWatch DoNotWatchFn) error {
stack := []node{nd}
Traverse:
for n := len(stack); n != 0; n = len(stack) {
Expand All @@ -160,6 +171,9 @@ Traverse:
// never has a parent node.
continue
}
if doNotWatch != nil && doNotWatch(nd.Name) {
continue
}
stack = append(stack, nd)
}
}
Expand Down Expand Up @@ -233,8 +247,8 @@ func (r root) Add(name string) node {
return r.addroot(name).Add(name)
}

func (r root) AddDir(dir string, fn walkFunc) error {
return r.Add(dir).AddDir(fn)
func (r root) AddDir(dir string, fn walkFunc, doNotWatch DoNotWatchFn) error {
return r.Add(dir).AddDir(fn, doNotWatch)
}

func (r root) Del(name string) error {
Expand All @@ -258,12 +272,12 @@ func (r root) Get(name string) (node, error) {
return nd, nil
}

func (r root) Walk(name string, fn walkFunc) error {
func (r root) Walk(name string, fn walkFunc, doNotWatch DoNotWatchFn) error {
nd, err := r.Get(name)
if err != nil {
return err
}
return nd.Walk(fn)
return nd.Walk(fn, doNotWatch)
}

func (r root) WalkPath(name string, fn walkPathFunc) error {
Expand Down
4 changes: 3 additions & 1 deletion notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import "fmt"

var defaultTree tree // lazy init

type DoNotWatchFn func(string) bool

func lazyInitDefaultTree() (err error) {
if defaultTree != nil {
// already initialized
Expand Down Expand Up @@ -96,7 +98,7 @@ func Watch(path string, c chan<- EventInfo, events ...Event) error {
// doNotWatch. Given a path as argument doNotWatch should return true if the
// file or directory should not be watched.
func WatchWithFilter(path string, c chan<- EventInfo,
doNotWatch func(string) bool, events ...Event) error {
doNotWatch DoNotWatchFn, events ...Event) error {
if err := lazyInitDefaultTree(); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,11 +946,11 @@ func (n *N) ExpectNotifyEvents(cases []NCase, all Chans) {
func (n *N) Walk(fn walkFunc) {
switch t := n.tree.(type) {
case *recursiveTree:
if err := t.root.Walk("", fn); err != nil {
if err := t.root.Walk("", fn, nil); err != nil {
n.w.Fatal(err)
}
case *nonrecursiveTree:
if err := t.root.Walk("", fn); err != nil {
if err := t.root.Walk("", fn, nil); err != nil {
n.w.Fatal(err)
}
default:
Expand Down
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package notify
const buffer = 128

type tree interface {
Watch(string, chan<- EventInfo, func(string) bool, ...Event) error
Watch(string, chan<- EventInfo, DoNotWatchFn, ...Event) error
Stop(chan<- EventInfo)
Close() error
}
Expand Down
23 changes: 7 additions & 16 deletions tree_nonrecursive.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (t *nonrecursiveTree) internal(rec <-chan EventInfo) {
t.rw.Unlock()
continue
}
err := nd.Add(ei.Path()).AddDir(t.recFunc(eset, nil))
err := nd.Add(ei.Path()).AddDir(t.recFunc(eset), nil)
t.rw.Unlock()
if err != nil {
dbgprintf("internal(%p) error: %v", rec, err)
Expand Down Expand Up @@ -146,7 +146,7 @@ func (t *nonrecursiveTree) watchDel(nd node, c chan<- EventInfo, e Event) eventD

// Watch TODO(rjeczalik)
func (t *nonrecursiveTree) Watch(path string, c chan<- EventInfo,
doNotWatch func(string) bool, events ...Event) error {
doNotWatch DoNotWatchFn, events ...Event) error {
if c == nil {
panic("notify: Watch using nil channel")
}
Expand Down Expand Up @@ -188,8 +188,8 @@ func (t *nonrecursiveTree) watch(nd node, c chan<- EventInfo, e Event) (err erro
return nil
}

func (t *nonrecursiveTree) recFunc(e Event, doNotWatch func(string) bool) walkFunc {
addWatch := func(nd node) (err error) {
func (t *nonrecursiveTree) recFunc(e Event) walkFunc {
return func(nd node) (err error) {
switch diff := nd.Watch.Add(t.rec, e|omit|Create); {
case diff == none:
case diff[1] == 0:
Expand All @@ -202,20 +202,11 @@ func (t *nonrecursiveTree) recFunc(e Event, doNotWatch func(string) bool) walkFu
}
return
}
if doNotWatch != nil {
return func(nd node) (err error) {
if doNotWatch(nd.Name) {
return errSkip
}
return addWatch(nd)
}
}
return addWatch
}

func (t *nonrecursiveTree) watchrec(nd node, c chan<- EventInfo, e Event,
doNotWatch func(string) bool) error {
var traverse func(walkFunc) error
doNotWatch DoNotWatchFn) error {
var traverse func(walkFunc, DoNotWatchFn) error
// Non-recursive tree listens on Create event for every recursive
// watchpoint in order to automagically set a watch for every
// created directory.
Expand All @@ -236,7 +227,7 @@ func (t *nonrecursiveTree) watchrec(nd node, c chan<- EventInfo, e Event,
}
// TODO(rjeczalik): account every path that failed to be (re)watched
// and retry.
if err := traverse(t.recFunc(e, doNotWatch)); err != nil {
if err := traverse(t.recFunc(e), doNotWatch); err != nil {
return err
}
t.watchAdd(nd, c, e)
Expand Down
8 changes: 4 additions & 4 deletions tree_recursive.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (t *recursiveTree) dispatch() {

// Watch TODO(rjeczalik)
func (t *recursiveTree) Watch(path string, c chan<- EventInfo,
doNotWatch func(string) bool, events ...Event) error {
_ DoNotWatchFn, events ...Event) error {
if c == nil {
panic("notify: Watch using nil channel")
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (t *recursiveTree) Watch(path string, c chan<- EventInfo,
children = append(children, nd)
return errSkip
}
switch must(cur.Walk(fn)); len(children) {
switch must(cur.Walk(fn, nil)); len(children) {
case 0:
// no child watches, cur holds a new watch
case 1:
Expand Down Expand Up @@ -332,14 +332,14 @@ func (t *recursiveTree) Stop(c chan<- EventInfo) {
watchDel(nd, c, all)
return nil
}
err = nonil(err, e, nd.Walk(fn))
err = nonil(err, e, nd.Walk(fn, nil))
// TODO(rjeczalik): if e != nil store dummy chan in nd.Watch just to
// retry un/rewatching next time and/or let the user handle the failure
// vie Error event?
return errSkip
}
t.rw.Lock()
e := t.root.Walk("", fn) // TODO(rjeczalik): use max root per c
e := t.root.Walk("", fn, nil) // TODO(rjeczalik): use max root per c
t.rw.Unlock()
if e != nil {
err = nonil(err, e)
Expand Down

0 comments on commit b9ceffc

Please sign in to comment.