Separate watcher, create event object

This commit is contained in:
2013-02-11 23:16:19 -05:00
parent 4651d05c34
commit 54a0359897
4 changed files with 83 additions and 32 deletions

46
watcher.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"github.com/howeyc/fsnotify"
)
func StartWatching(watchDir string, fileUpdates chan *Event) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic("Failed to create fsnotify watcher")
}
err = watcher.Watch(watchDir)
if err != nil {
panic("Failed to watch " + watchDir)
}
for {
select {
case ev := <-watcher.Event:
event := new(Event)
if ev.IsCreate() || ev.IsModify() {
event.Type = UPDATE
} else if ev.IsDelete() || ev.IsRename() {
event.Type = DELETE
} else {
panic("Unknown fsnotify event type")
}
event.Path = ev.Name
if event.IsUpdate() {
event.Hash, err = HashFile(ev.Name)
if err != nil { continue }
} else {
event.Hash = ""
}
fileUpdates <- event
//TODO if creating a directory, start watching it (and then initiate a full scan of it so we're sure nothing slipped through the cracks)
case err := <-watcher.Error:
panic(err)
}
}
}