Exemple #1
0
func watch(w *fsnotify.Watcher, p string) {
	debugPrint("Watching %s", p)

	switch err := w.Add(p); {
	case os.IsNotExist(err):
		debugPrint("%s no longer exists", p)

	case err != nil:
		log.Printf("Failed to watch %s: %s", p, err)
	}
}
Exemple #2
0
func addWatcher(watcher *fsnotify.Watcher, dir string) {
	watcher.Add(dir)

	dirs, err := ioutil.ReadDir(dir)
	if err != nil {
		return
	}

	for _, d := range dirs {
		if d.IsDir() {
			addWatcher(watcher, d.Name())
		}
	}
}
Exemple #3
0
func waitWatchEvent(fpath string, op fsnotify.Op) (event fsnotify.Event, err error) {
	var (
		fdir    string
		watcher *fsnotify.Watcher
		ok      bool
	)

	if fpath, err = filepath.EvalSymlinks(fpath); err != nil {
		log.Errorf("Get symlinks failed: %q\n%v", fpath, err)
		return
	}

	fdir = filepath.Dir(fpath)

	if watcher, ok = mapWatcher[fdir]; !ok {
		log.Debugf("create new watcher for %q", fdir)
		if watcher, err = fsnotify.NewWatcher(); err != nil {
			log.Errorf("create new watcher failed: %q\n%s", fdir, err)
			return
		}
		mapWatcher[fdir] = watcher
		if err = watcher.Add(fdir); err != nil {
			log.Errorf("add new watch path failed: %q\n%s", fdir, err)
			return
		}
	}

	for {
		select {
		case event = <-watcher.Events:
			if event.Name == fpath {
				if op > 0 {
					if event.Op&op > 0 {
						return
					}
				} else {
					return
				}
			}
		case err = <-watcher.Errors:
			log.Errorf("watcher error: %s", err)
			return
		}
	}

	return
}