• [Go] 跨平台文件系统监控工具 fsnotify 应用举例


    项目地址:https://github.com/fsnotify/fsnotify

    fsnotify 能监控指定文件夹内 文件的修改情况,如 文件的 增加、删除、修改、重命名等操作。

    官方给出了以下注意事项:

    When a file is moved to another directory is it still being watched?

    No (it shouldn't be, unless you are watching where it was moved to).

    When I watch a directory, are all subdirectories watched as well?

    No, you must add watches for any directory you want to watch (a recursive watcher is on the roadmap #18).

    Do I have to watch the Error and Event channels in a separate goroutine?

    As of now, yes. Looking into making this single-thread friendly (see howeyc #7)

    Why am I receiving multiple events for the same file on OS X?

    Spotlight indexing on OS X can result in multiple events (see howeyc #62). A temporary workaround is to add your folder(s) to the Spotlight Privacy settings until we have a native FSEvents implementation (see #11).

    How many files can be watched at once?

    There are OS-specific limits as to how many watches can be created:

    • Linux: /proc/sys/fs/inotify/max_user_watches contains the limit, reaching this limit results in a "no space left on device" error.
    • BSD / OSX: sysctl variables "kern.maxfiles" and "kern.maxfilesperproc", reaching these limits results in a "too many open files" error.

    最为注意的一点是:

    文件夹中的子文件夹,还需自己去添加监控,fsnotify 本身不提供递归循环监控功能!

    应用举例

    package main
    
    import (
    	"github.com/fsnotify/fsnotify"
    	"log"
    	"runtime"
    )
    
    func main() {
    	// 监控路径列表
    	paths := []string{
    		"/Users/jianbao/GoglandProjects/fiisoo/src/test/ch0",
    		"/Users/jianbao/GoglandProjects/fiisoo/src/test/ch1",
    	}
    
    	watcher, err := fsnotify.NewWatcher()
    	if err != nil {
    		log.Fatalf("Failed to create watcher: %s", err)
    	}
    	defer watcher.Close()
    
    	exit := make(chan bool)
    
    	go func() {
    		for {
    			select {
    			case e := <-watcher.Events:
    				log.Println("修改文件:" + e.Name)
    				log.Println("修改类型:" + e.Op.String())
    			case err := <-watcher.Errors:
    				log.Printf("Watcher error: %s
    ", err.Error()) // No need to exit here
    			}
    		}
    	}()
    
    	log.Println("Initializing watcher...")
    	for _, path := range paths {
    		log.Printf("Watching: %s
    ", path)
    		err = watcher.Add(path)
    		if err != nil {
    			log.Fatalf("Failed to watch directory: %s", err)
    		}
    	}
    
    	<-exit // 用来 阻塞应用不退出,只能通过“杀死进程”的方式退出,如 按住 Ctrl + C 快捷键强制推出
    	runtime.Goexit()
    }
    
    
  • 相关阅读:
    Gson简要使用笔记
    android入门到熟练(五)---广播
    95&&96.Unique Binary Search Trees I&&II
    day 08 文件操作
    07 深浅拷贝
    06 day小数据池
    05,.字典,集合
    列表和元组
    字符串
    while 循环,格式化输出和运算编码
  • 原文地址:https://www.cnblogs.com/52php/p/6794962.html
Copyright © 2020-2023  润新知