Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #86 from gophertown/cl58500043
Browse files Browse the repository at this point in the history
test helpers and cleanup
  • Loading branch information
howeyc committed Feb 9, 2014
2 parents b4af298 + 0713b5a commit 9c5eb19
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 270 deletions.
19 changes: 9 additions & 10 deletions fsnotify_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,21 @@ type FileEvent struct {
create bool // set by fsnotify package if found new file
}

// IsCreate reports whether the FileEvent was triggerd by a creation
// IsCreate reports whether the FileEvent was triggered by a creation
func (e *FileEvent) IsCreate() bool { return e.create }

// IsDelete reports whether the FileEvent was triggerd by a delete
// IsDelete reports whether the FileEvent was triggered by a delete
func (e *FileEvent) IsDelete() bool { return (e.mask & sys_NOTE_DELETE) == sys_NOTE_DELETE }

// IsModify reports whether the FileEvent was triggerd by a file modification
// IsModify reports whether the FileEvent was triggered by a file modification
func (e *FileEvent) IsModify() bool {
return ((e.mask&sys_NOTE_WRITE) == sys_NOTE_WRITE || (e.mask&sys_NOTE_ATTRIB) == sys_NOTE_ATTRIB)
}

// IsRename reports whether the FileEvent was triggerd by a change name
// IsRename reports whether the FileEvent was triggered by a change name
func (e *FileEvent) IsRename() bool { return (e.mask & sys_NOTE_RENAME) == sys_NOTE_RENAME }

// IsAttrib reports whether the FileEvent was triggered by a change in the file metadata (eg.
// atime, mtime etc.)
// IsAttrib reports whether the FileEvent was triggered by a change in the file metadata.
func (e *FileEvent) IsAttrib() bool {
return (e.mask & sys_NOTE_ATTRIB) == sys_NOTE_ATTRIB
}
Expand Down Expand Up @@ -272,24 +271,24 @@ func (w *Watcher) removeWatch(path string) error {

// Find all watched paths that are in this directory that are not external.
if fInfo.IsDir() {
pathsToRemove := make([]string, 0)
var pathsToRemove []string
w.pmut.Lock()
for _, wpath := range w.paths {
wdir, _ := filepath.Split(wpath)
if filepath.Clean(wdir) == filepath.Clean(path) {
w.ewmut.Lock()
if _, extern := w.externalWatches[wpath]; !extern {
if !w.externalWatches[wpath] {
pathsToRemove = append(pathsToRemove, wpath)
}
w.ewmut.Unlock()
}
}
w.pmut.Unlock()
for idx := 0; idx < len(pathsToRemove); idx++ {
for _, p := range pathsToRemove {
// Since these are internal, not much sense in propagating error
// to the user, as that will just confuse them with an error about
// a path they did not explicitly watch themselves.
w.removeWatch(pathsToRemove[idx])
w.removeWatch(p)
}
}

Expand Down
8 changes: 2 additions & 6 deletions fsnotify_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ const (
sys_IN_IGNORED uint32 = syscall.IN_IGNORED
sys_IN_Q_OVERFLOW uint32 = syscall.IN_Q_OVERFLOW
sys_IN_UNMOUNT uint32 = syscall.IN_UNMOUNT

// Block for 100ms on each call to Select
selectWaitTime = 100e6
)

type FileEvent struct {
Expand Down Expand Up @@ -85,8 +82,7 @@ func (e *FileEvent) IsRename() bool {
return ((e.mask&sys_IN_MOVE_SELF) == sys_IN_MOVE_SELF || (e.mask&sys_IN_MOVED_FROM) == sys_IN_MOVED_FROM)
}

// IsAttrib reports whether the FileEvent was triggered by a change in the file metadata (eg.
// atime, mtime etc.)
// IsAttrib reports whether the FileEvent was triggered by a change in the file metadata.
func (e *FileEvent) IsAttrib() bool {
return (e.mask & sys_IN_ATTRIB) == sys_IN_ATTRIB
}
Expand Down Expand Up @@ -220,7 +216,7 @@ func (w *Watcher) readEvents() {
default:
}

n, errno = syscall.Read(w.fd, buf[0:])
n, errno = syscall.Read(w.fd, buf[:])

// If EOF is received
if n == 0 {
Expand Down
23 changes: 5 additions & 18 deletions fsnotify_symlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@ import (
)

func TestFsnotifyFakeSymlink(t *testing.T) {
// Create an fsnotify watcher instance and initialize it
watcher, err := NewWatcher()
if err != nil {
t.Fatalf("NewWatcher() failed: %s", err)
}

var testDir string = testTempDir()
watcher := newWatcher(t)

// Create directory to watch
if err := os.Mkdir(testDir, 0777); err != nil {
t.Fatalf("Failed to create test directory: %s", err)
}
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)

var errorsReceived counter
Expand All @@ -38,8 +30,7 @@ func TestFsnotifyFakeSymlink(t *testing.T) {
}()

// Count the CREATE events received
var createEventsReceived counter
var otherEventsReceived counter
var createEventsReceived, otherEventsReceived counter
go func() {
for ev := range watcher.Event {
t.Logf("event received: %s", ev)
Expand All @@ -51,13 +42,9 @@ func TestFsnotifyFakeSymlink(t *testing.T) {
}
}()

// Add a watch for testDir
err = watcher.Watch(testDir)
if err != nil {
t.Fatalf("Watcher.Watch() failed: %s", err)
}
addWatch(t, watcher, testDir)

if os.Symlink(filepath.Join(testDir, "zzz"), filepath.Join(testDir, "zzznew")) != nil {
if err := os.Symlink(filepath.Join(testDir, "zzz"), filepath.Join(testDir, "zzznew")); err != nil {
t.Fatalf("Failed to create bogus symlink: %s", err)
}
t.Logf("Created bogus symlink")
Expand Down
Loading

0 comments on commit 9c5eb19

Please sign in to comment.