func watch(root string, watcher *fsnotify.Watcher, names chan<- string, done chan<- error) { if err := filepath.Walk(root, walker(watcher)); err != nil { // TODO: handle this somehow? infoPrintf(-1, "Error while walking path %s: %s\n", root, err) } for { select { case e := <-watcher.Event: path := strings.TrimPrefix(e.Name, "./") names <- path if e.IsCreate() { if err := filepath.Walk(path, walker(watcher)); err != nil { // TODO: handle this somehow? infoPrintf(-1, "Error while walking path %s: %s\n", path, err) } } if e.IsDelete() { watcher.RemoveWatch(path) } case err := <-watcher.Error: done <- err return } } }
func processSubdir(watcher *fsnotify.Watcher, ev *fsnotify.FileEvent) { if ev.IsModify() { return } if ev.IsDelete() { log.Println("remove watch", ev.Name) // FIXME: what to do with err? watcher.RemoveWatch(ev.Name) return } // FIXME: Lstat or Stat? // TODO: what to do with err? can we safely ignore? mode, err := os.Lstat(ev.Name) if err != nil { log.Println("error processing subdir:", err.Error()) return } if !mode.IsDir() { return } // FIXME: handle renames if ev.IsCreate() { log.Println("add watch", ev.Name) // FIXME: what to do with err? watcher.Watch(ev.Name) } }
func (self *Launcher) eventHandler(watcher *fsnotify.Watcher) { var info = map[string]ItemChangedStatus{} for { select { case ev := <-watcher.Event: name := path.Clean(ev.Name) basename := path.Base(name) if basename == "kde4" { if ev.IsCreate() { watcher.Watch(name) } else if ev.IsDelete() { watcher.RemoveWatch(name) } } matched, _ := path.Match(`[^#.]*.desktop`, basename) if matched { go self.itemChangedHandler(ev, name, info) } case <-watcher.Error: } } }