]> go.fuhry.dev Git - fsnotify.git/commitdiff
Golang exp/inotify fixes
authorChris Howey <chris@howey.me>
Wed, 10 Oct 2012 00:23:22 +0000 (19:23 -0500)
committerChris Howey <chris@howey.me>
Wed, 10 Oct 2012 00:23:22 +0000 (19:23 -0500)
http://code.google.com/p/go/issues/detail?id=3713
http://codereview.appspot.com/5418045/

fsnotify_linux.go

index 73c9a3c80720e57a12488983ef01756d2832a888..07a047d7be50f49f27e2d5e1a99022ed7bbb0f8a 100644 (file)
@@ -12,6 +12,7 @@ import (
        "fmt"
        "os"
        "strings"
+       "sync"
        "syscall"
        "unsafe"
 )
@@ -46,6 +47,7 @@ type watch struct {
 }
 
 type Watcher struct {
+       mu            sync.Mutex        // Map access
        fd            int               // File descriptor (as returned by the inotify_init() syscall)
        watches       map[string]*watch // Map of inotify watches (key: path)
        fsnFlags      map[string]uint32 // Map of watched files to flags used for filter
@@ -116,10 +118,11 @@ func (w *Watcher) addWatch(path string, flags uint32) error {
                return errno
        }
 
-       if !found {
-               w.watches[path] = &watch{wd: uint32(wd), flags: flags}
-               w.paths[wd] = path
-       }
+       w.mu.Lock()
+       w.watches[path] = &watch{wd: uint32(wd), flags: flags}
+       w.paths[wd] = path
+       w.mu.Unlock()
+
        return nil
 }
 
@@ -191,7 +194,9 @@ func (w *Watcher) readEvents() {
                        // doesn't append the filename to the event, but we would like to always fill the
                        // the "Name" field with a valid filename. We retrieve the path of the watch from
                        // the "paths" map.
+                       w.mu.Lock()
                        event.Name = w.paths[int(raw.Wd)]
+                       w.mu.Unlock()
                        watchedName := event.Name
                        if nameLen > 0 {
                                // Point "bytes" at the first byte of the filename