From b63a275e7a4ba4770de2584c035f2d9166e74102 Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Tue, 9 Oct 2012 19:23:22 -0500 Subject: [PATCH] Golang exp/inotify fixes http://code.google.com/p/go/issues/detail?id=3713 http://codereview.appspot.com/5418045/ --- fsnotify_linux.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/fsnotify_linux.go b/fsnotify_linux.go index 73c9a3c..07a047d 100644 --- a/fsnotify_linux.go +++ b/fsnotify_linux.go @@ -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 -- 2.50.1