From 964198cb7a133ecfe1a489e331e76ee92feba3d0 Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Fri, 30 Dec 2011 22:45:46 -0600 Subject: [PATCH] Updated to latest go weekly code --- fsnotify_bsd.go | 61 ++++++++++++++++++++++++----------------------- fsnotify_linux.go | 23 +++++++++--------- fsnotify_test.go | 4 ++-- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/fsnotify_bsd.go b/fsnotify_bsd.go index c018723..83256d7 100644 --- a/fsnotify_bsd.go +++ b/fsnotify_bsd.go @@ -27,11 +27,12 @@ Example: package fsnotify import ( + "errors" "fmt" - "os" - "syscall" "io/ioutil" + "os" "path" + "syscall" ) type FileEvent struct { @@ -56,19 +57,19 @@ func (e *FileEvent) IsAttribute() bool { return (e.mask & NOTE_ATTRIB) == NOTE_A func (e *FileEvent) IsRename() bool { return (e.mask & NOTE_RENAME) == NOTE_RENAME } type Watcher struct { - kq int // File descriptor (as returned by the kqueue() syscall) - watches map[string]int // Map of watched file diescriptors (key: path) - paths map[int]string // Map of watched paths (key: watch descriptor) - finfo map[int]*os.FileInfo // Map of file information (isDir, isReg; key: watch descriptor) - Error chan os.Error // Errors are sent on this channel - Event chan *FileEvent // Events are returned on this channel - done chan bool // Channel for sending a "quit message" to the reader goroutine - isClosed bool // Set to true when Close() is first called - kbuf [1]syscall.Kevent_t // An event buffer for Add/Remove watch + kq int // File descriptor (as returned by the kqueue() syscall) + watches map[string]int // Map of watched file diescriptors (key: path) + paths map[int]string // Map of watched paths (key: watch descriptor) + finfo map[int]os.FileInfo // Map of file information (isDir, isReg; key: watch descriptor) + Error chan error // Errors are sent on this channel + Event chan *FileEvent // Events are returned on this channel + done chan bool // Channel for sending a "quit message" to the reader goroutine + isClosed bool // Set to true when Close() is first called + kbuf [1]syscall.Kevent_t // An event buffer for Add/Remove watch } // NewWatcher creates and returns a new kevent instance using kqueue(2) -func NewWatcher() (*Watcher, os.Error) { +func NewWatcher() (*Watcher, error) { fd, errno := syscall.Kqueue() if fd == -1 { return nil, os.NewSyscallError("kqueue", errno) @@ -77,9 +78,9 @@ func NewWatcher() (*Watcher, os.Error) { kq: fd, watches: make(map[string]int), paths: make(map[int]string), - finfo: make(map[int]*os.FileInfo), + finfo: make(map[int]os.FileInfo), Event: make(chan *FileEvent), - Error: make(chan os.Error), + Error: make(chan error), done: make(chan bool, 1), } @@ -90,7 +91,7 @@ func NewWatcher() (*Watcher, os.Error) { // Close closes a kevent watcher instance // It sends a message to the reader goroutine to quit and removes all watches // associated with the kevent instance -func (w *Watcher) Close() os.Error { +func (w *Watcher) Close() error { if w.isClosed { return nil } @@ -107,9 +108,9 @@ func (w *Watcher) Close() os.Error { // AddWatch adds path to the watched file set. // The flags are interpreted as described in kevent(2). -func (w *Watcher) addWatch(path string, flags uint32) os.Error { +func (w *Watcher) addWatch(path string, flags uint32) error { if w.isClosed { - return os.NewError("kevent instance already closed") + return errors.New("kevent instance already closed") } watchEntry := &w.kbuf[0] @@ -119,7 +120,7 @@ func (w *Watcher) addWatch(path string, flags uint32) os.Error { if !found { fd, errno := syscall.Open(path, syscall.O_NONBLOCK|syscall.O_RDONLY, 0700) if fd == -1 { - return &os.PathError{"kevent_add_watch", path, os.Errno(errno)} + return errno } watchfd = fd @@ -133,24 +134,24 @@ func (w *Watcher) addWatch(path string, flags uint32) os.Error { wd, errno := syscall.Kevent(w.kq, w.kbuf[:], nil, nil) if wd == -1 { - return &os.PathError{"kevent_add_watch", path, os.Errno(errno)} + return errno } else if (watchEntry.Flags & syscall.EV_ERROR) == syscall.EV_ERROR { - return &os.PathError{"kevent_add_watch", path, os.Errno(int(watchEntry.Data))} + return errors.New("kevent add error") } return nil } // Watch adds path to the watched file set, watching all events. -func (w *Watcher) Watch(path string) os.Error { +func (w *Watcher) Watch(path string) error { return w.addWatch(path, NOTE_ALLEVENTS) } // RemoveWatch removes path from the watched file set. -func (w *Watcher) RemoveWatch(path string) os.Error { +func (w *Watcher) RemoveWatch(path string) error { watchfd, ok := w.watches[path] if !ok { - return os.NewError(fmt.Sprintf("can't remove non-existent kevent watch for: %s", path)) + return errors.New(fmt.Sprintf("can't remove non-existent kevent watch for: %s", path)) } syscall.Close(watchfd) watchEntry := &w.kbuf[0] @@ -159,9 +160,9 @@ func (w *Watcher) RemoveWatch(path string) os.Error { if success == -1 { return os.NewSyscallError("kevent_rm_watch", errno) } else if (watchEntry.Flags & syscall.EV_ERROR) == syscall.EV_ERROR { - return os.NewSyscallError("kevent_rm_watch", int(watchEntry.Data)) + return errors.New("kevent rm error") } - w.watches[path] = 0, false + delete(w.watches, path) return nil } @@ -173,7 +174,7 @@ func (w *Watcher) readEvents() { events []syscall.Kevent_t // Received events twait *syscall.Timespec // Time to block waiting for events n int // Number of events returned from kevent - errno int // Syscall errno + errno error // Syscall errno ) events = eventbuf[0:0] twait = new(syscall.Timespec) @@ -194,7 +195,7 @@ func (w *Watcher) readEvents() { // If "done" message is received if done { errno := syscall.Close(w.kq) - if errno == -1 { + if errno != nil { w.Error <- os.NewSyscallError("close", errno) } close(w.Event) @@ -219,7 +220,7 @@ func (w *Watcher) readEvents() { fileEvent.Name = w.paths[int(watchEvent.Ident)] fileInfo := w.finfo[int(watchEvent.Ident)] - if fileInfo.IsDirectory() && fileEvent.IsModify() { + if fileInfo.IsDir() && fileEvent.IsModify() { w.sendDirectoryChangeEvents(fileEvent.Name) } else { // Send the event on the events channel @@ -245,8 +246,8 @@ func (w *Watcher) sendDirectoryChangeEvents(dirPath string) { // Search for new files for _, fileInfo := range files { - if fileInfo.IsRegular() == true { - filePath := path.Join(dirPath, fileInfo.Name) + if fileInfo.IsDir() == false { + filePath := path.Join(dirPath, fileInfo.Name()) if w.watches[filePath] == 0 { // Watch file to mimic linux fsnotify e := w.addWatch(filePath, NOTE_DELETE|NOTE_WRITE|NOTE_RENAME) diff --git a/fsnotify_linux.go b/fsnotify_linux.go index d2b17c3..28e29cf 100644 --- a/fsnotify_linux.go +++ b/fsnotify_linux.go @@ -27,6 +27,7 @@ Example: package fsnotify import ( + "errors" "fmt" "os" "strings" @@ -66,14 +67,14 @@ type Watcher struct { fd int // File descriptor (as returned by the inotify_init() syscall) watches map[string]*watch // Map of inotify watches (key: path) paths map[int]string // Map of watched paths (key: watch descriptor) - Error chan os.Error // Errors are sent on this channel + Error chan error // Errors are sent on this channel Event chan *FileEvent // Events are returned on this channel done chan bool // Channel for sending a "quit message" to the reader goroutine isClosed bool // Set to true when Close() is first called } // NewWatcher creates and returns a new inotify instance using inotify_init(2) -func NewWatcher() (*Watcher, os.Error) { +func NewWatcher() (*Watcher, error) { fd, errno := syscall.InotifyInit() if fd == -1 { return nil, os.NewSyscallError("inotify_init", errno) @@ -83,7 +84,7 @@ func NewWatcher() (*Watcher, os.Error) { watches: make(map[string]*watch), paths: make(map[int]string), Event: make(chan *FileEvent), - Error: make(chan os.Error), + Error: make(chan error), done: make(chan bool, 1), } @@ -94,7 +95,7 @@ func NewWatcher() (*Watcher, os.Error) { // Close closes an inotify watcher instance // It sends a message to the reader goroutine to quit and removes all watches // associated with the inotify instance -func (w *Watcher) Close() os.Error { +func (w *Watcher) Close() error { if w.isClosed { return nil } @@ -111,9 +112,9 @@ func (w *Watcher) Close() os.Error { // AddWatch adds path to the watched file set. // The flags are interpreted as described in inotify_add_watch(2). -func (w *Watcher) addWatch(path string, flags uint32) os.Error { +func (w *Watcher) addWatch(path string, flags uint32) error { if w.isClosed { - return os.NewError("inotify instance already closed") + return errors.New("inotify instance already closed") } watchEntry, found := w.watches[path] @@ -134,21 +135,21 @@ func (w *Watcher) addWatch(path string, flags uint32) os.Error { } // Watch adds path to the watched file set, watching all events. -func (w *Watcher) Watch(path string) os.Error { +func (w *Watcher) Watch(path string) error { return w.addWatch(path, OS_AGNOSTIC_EVENTS) } // RemoveWatch removes path from the watched file set. -func (w *Watcher) RemoveWatch(path string) os.Error { +func (w *Watcher) RemoveWatch(path string) error { watch, ok := w.watches[path] if !ok { - return os.NewError(fmt.Sprintf("can't remove non-existent inotify watch for: %s", path)) + return errors.New(fmt.Sprintf("can't remove non-existent inotify watch for: %s", path)) } success, errno := syscall.InotifyRmWatch(w.fd, watch.wd) if success == -1 { return os.NewSyscallError("inotify_rm_watch", errno) } - w.watches[path] = nil, false + delete(w.watches, path) return nil } @@ -185,7 +186,7 @@ func (w *Watcher) readEvents() { continue } if n < syscall.SizeofInotifyEvent { - w.Error <- os.NewError("inotify: short read in readEvents()") + w.Error <- errors.New("inotify: short read in readEvents()") continue } diff --git a/fsnotify_test.go b/fsnotify_test.go index c156219..13fb762 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -6,9 +6,9 @@ package fsnotify import ( "os" - "time" + "os/exec" "testing" - "exec" + "time" ) func TestFsnotifyDirOnly(t *testing.T) { -- 2.50.1