本文整理汇总了Golang中golang.org/x/exp/inotify.Watcher.AddWatch方法的典型用法代码示例。如果您正苦于以下问题:Golang Watcher.AddWatch方法的具体用法?Golang Watcher.AddWatch怎么用?Golang Watcher.AddWatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/exp/inotify.Watcher
的用法示例。
在下文中一共展示了Watcher.AddWatch方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addWatchToClosestPath
func addWatchToClosestPath(watcher *inotify.Watcher, path string) {
err := watcher.AddWatch(path, InotifyFlags)
for err != nil && err.(*os.PathError).Err == syscall.ENOENT && path != "/" {
path = filepath.Dir(path)
if path != "/" {
err = watcher.AddWatch(path, InotifyDirFlags)
}
}
}
示例2: addWatch
func addWatch(from string, rec bool, hidden bool, watcher *inotify.Watcher) error {
mask := inotify.IN_MODIFY | inotify.IN_CLOSE_WRITE | inotify.IN_MOVE |
inotify.IN_CREATE
walkFn := func(path string, _ os.FileInfo, err error) error {
if !hidden && path != "." && strings.HasPrefix(path, ".") {
return nil
}
return watcher.AddWatch(path, mask)
}
if rec {
return filepath.Walk(from, walkFn)
}
return watcher.AddWatch(from, mask)
}
示例3: watch
func watch(watcher *inotify.Watcher, paths []string, flag uint32, recursive, must bool) {
log.Debugln("Watching: ", paths)
for _, p := range paths {
fi, err := os.Stat(p)
if err != nil {
if must {
log.Fatal(err)
}
return
}
if err := watcher.AddWatch(p, flag); err != nil {
if must {
log.Fatal(err)
}
return
}
if !recursive || !fi.IsDir() {
return
}
entries, err := ioutil.ReadDir(p)
if err != nil {
if must {
log.Fatal(err)
}
return
}
var ps []string
for _, en := range entries {
if en.IsDir() {
ps = append(ps, path.Join(p, en.Name()))
}
}
if len(ps) == 0 {
return
}
watch(watcher, ps, flag, true, false)
}
}
示例4: runWatcher
// TODO: Resolve some corner cases like watches not set after create, delete, create, move
func runWatcher(watcher *inotify.Watcher) {
LogWriter.Info("Starting paxrat watcher")
for {
select {
case ev := <-watcher.Event:
if ev.Mask == inotify.IN_CREATE {
if _, ok := (*Conf).Settings[ev.Name]; ok {
watcher.AddWatch(ev.Name, InotifyFlags)
msg := fmt.Sprintf("File created: %s\n", ev.Name)
LogWriter.Info(msg)
}
// Catch directory creation events for non-existent directories in executable path
} else if ev.Mask == (inotify.IN_CREATE | inotify.IN_ISDIR) {
for path, _ := range (*Conf).Settings {
if strings.HasPrefix(path, ev.Name) {
addWatchToClosestPath(watcher, path)
}
}
} else if ev.Mask == inotify.IN_DELETE_SELF || ev.Mask == inotify.IN_MOVE_SELF {
if _, ok := (*Conf).Settings[ev.Name]; ok {
msg := fmt.Sprintf("File deleted: %s\n", ev.Name)
LogWriter.Info(msg)
parent := filepath.Dir(ev.Name)
watcher.AddWatch(parent, InotifyDirFlags)
continue
}
} else if ev.Mask == inotify.IN_ATTRIB {
if _, ok := (*Conf).Settings[ev.Name]; ok {
exists := pathExists(ev.Name)
if !exists {
msg := fmt.Sprintf("File deleted: %s\n", ev.Name)
LogWriter.Info(msg)
parent := filepath.Dir(ev.Name)
watcher.AddWatch(parent, InotifyDirFlags)
continue
} else {
msg := fmt.Sprintf("File attributes changed: %s", ev.Name)
LogWriter.Info(msg)
}
}
}
if settings, ok := (*Conf).Settings[ev.Name]; ok {
if ev.Mask != inotify.IN_IGNORED {
err := setFlagsWatchMode(watcher, ev.Name, settings.Flags, settings.Nonroot)
if err != nil {
msg := fmt.Sprintf("watch mode setFlags error: %s", err)
LogWriter.Err(msg)
}
}
}
case err := <-watcher.Error:
msg := fmt.Sprintf("watch mode watcher error: %s", err)
LogWriter.Err(msg)
}
}
return
}
示例5: setWatch
func setWatch(watch *inotify.Watcher, path string) {
if err := watch.AddWatch(path, inotify.IN_MODIFY); err != nil {
log.Println(fmt.Sprintf("Error %s setting a watcher on the config file. Changes will need a restart to become effective", err))
}
}