playing with an efficient file sync tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

58 lines
1.4 KiB

package main
import (
"log"
"github.com/fsnotify/fsnotify"
)
// WatchPath establishes a watch on the provided path
func WatchPath(path string) {
/*
29044 2016-09-21 12:40:21 cd x
29045 2016-09-21 12:40:25 touch file
29046 2016-09-21 12:40:39 rm file
29047 2016-09-21 12:40:42 touch file
29048 2016-09-21 12:40:50 echo farts > file
29049 2016-09-21 12:40:57 rm file
produces:
$ ./sync_pass x
2016/09/21 12:40:25 event: "x/file": CREATE
2016/09/21 12:40:25 event: "x/file": CHMOD
2016/09/21 12:40:39 event: "x/file": REMOVE
2016/09/21 12:40:42 event: "x/file": CREATE
2016/09/21 12:40:42 event: "x/file": CHMOD
2016/09/21 12:40:50 event: "x/file": WRITE
2016/09/21 12:40:50 modified file: x/file
2016/09/21 12:40:50 event: "x/file": WRITE
2016/09/21 12:40:50 modified file: x/file
2016/09/21 12:40:57 event: "x/file": REMOVE
*/
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
err = watcher.Add(path)
if err != nil {
log.Fatal(err)
}
<-done
}