基本信息
源码名称:文件变动监控(监控某文件夹的文件变化)
源码大小:0.81M
文件格式:.zip
开发语言:Go
更新时间:2019-04-01
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
可以使用创建一个watcher来对某个文件夹进行监控
// FileLock project main.go package main import ( "fmt" "log" "os" "os/exec" "path/filepath" "strings" "time" "github.com/fsnotify/fsnotify" ) var fileLog *os.File var debuglog *log.Logger func CheckErr(err error) { if fileLog == nil { logfilename, _ := os.Getwd() logfilename = "\\op.log" log.Println(logfilename) if _, err := os.Stat(logfilename); err == nil { err = os.Rename(logfilename, logfilename "bak") if err != nil { err = os.Remove(logfilename) if err != nil { log.Println("删除成功") } } } fileLog, err = os.Create(logfilename) if err != nil { log.Println("日志创建出错:", err) } debuglog = log.New(fileLog, "", log.LstdFlags) } if nil != err { debuglog.Println("日志:", err) } } type Commfun struct { nowfile string } func NewCommfun() (*Commfun, error) { cf := &Commfun{} return cf, nil } func (g *Commfun) opFiles(ev fsnotify.Event) error { if g.nowfile == ev.Name { return nil } g.nowfile = ev.Name if ev.Op&fsnotify.Write == fsnotify.Write { debuglog.Println("旧文件内容变动事件:", ev.Name) err := os.Remove(strings.Replace(ev.Name, "\\", "\\\\", -1)) log.Println(err) if err != nil { debuglog.Println("旧文件被篡改异动监控【----------------失败-----------------】:", err, ev.Name) } else { debuglog.Println("旧文件被篡改异动监控【处理动作:隔离】:", ev.Name) } } else if ev.Op&fsnotify.Create == fsnotify.Create { debuglog.Println("创建新文件事件:", ev.Name) fi, err3 := os.Stat(ev.Name) if err3 != nil { debuglog.Println("新增文件夹【-----------------------监控失败----------------】:", err3, ev.Name) } if fi.IsDir() { g.Guard(ev.Name) } else { err := os.Remove(strings.Replace(ev.Name, "\\", "\\\\", -1)) if err != nil { debuglog.Println("新文件写入删除【----------------失败-----------------】:", err, ev.Name) } else { debuglog.Println("新文件写入异动监控【处理动作:隔离】:", ev.Name) } } } else { debuglog.Println("默认事件记录:s%", ev.Name, ev.Op) } return nil } func (g *Commfun) Guard(fullPath string) { start := time.Now() watcher, err := fsnotify.NewWatcher() if err != nil { debuglog.Fatal(err) } defer watcher.Close() done := make(chan bool) go func() { count := 0 for { select { case event := <-watcher.Events: err = g.opFiles(event) case err := <-watcher.Errors: count if count > 100 { debuglog.Println("计数器显示:文件监错误太多") break } debuglog.Println("监听文件出错:", err) } } }() fullPath, _ = filepath.Abs(fullPath) err = watcher.Add(fullPath) filepath.Walk(fullPath, func(path string, fi os.FileInfo, err error) error { if nil == fi { CheckErr(err) return err } if fi.IsDir() { err = watcher.Add(strings.Replace(path, "\\", "\\\\", -1)) if err != nil { debuglog.Println(err) } return err } return nil }) fmt.Println("监控文件处理监听处理时间约%f秒", time.Now().Sub(start).Seconds()) if err != nil { debuglog.Println(err) } <-done } func main() { done := make(chan bool) cn, err := NewCommfun() CheckErr(err) go cn.Guard("E:\\") //go cn.Guard(os.Args[1]) <-done ExitFunc() } func ExitFunc() { fmt.Println("i am exiting!") cmd := exec.Command("D:\\开发\\golang\\work\\src\\FileLock\\FileLock.exe", " E:\360Downloads", " ") err := cmd.Run() fmt.Println(err) }