Separate watcher, create event object

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

View File

@ -5,7 +5,6 @@ import (
"flag" "flag"
"path" "path"
"os/user" "os/user"
"github.com/howeyc/fsnotify"
"code.google.com/p/goconf/conf" "code.google.com/p/goconf/conf"
) )
@ -48,36 +47,20 @@ func main() {
fmt.Println(cachedir) fmt.Println(cachedir)
fmt.Println(storage) fmt.Println(storage)
setup_watchers() fileUpdates := make(chan *Event)
} go StartWatching(syncdir, fileUpdates)
func setup_watchers() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("Failed to create fsnotify watcher")
return
}
fmt.Println("Created new fsnotify watcher!")
err = watcher.Watch("/home/aclindsa/.asink")
if err != nil {
fmt.Println("Failed to watch /home/aclindsa/.asink")
return
}
for { for {
select { event := <- fileUpdates
case ev := <-watcher.Event: ProcessEvent(storage, event)
fmt.Println("event:", ev) }
hash, err := HashFile(ev.Name) }
//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)
if err != nil { func ProcessEvent(storage Storage, event *Event) {
fmt.Println(err) fmt.Println(event)
} else {
fmt.Println(hash) if event.IsUpdate() {
} err := storage.Put(event.Path, event.Hash)
case err := <-watcher.Error: if err != nil { panic(err) }
fmt.Println("error:", err)
}
} }
} }

22
events.go Normal file
View File

@ -0,0 +1,22 @@
package main
type EventType uint32
const (
UPDATE = 1 << iota
DELETE
)
type Event struct {
Type EventType
Path string
Hash string
}
func (e Event) IsUpdate() bool {
return e.Type & UPDATE == UPDATE
}
func (e Event) IsDelete() bool {
return e.Type & DELETE == DELETE
}

View File

@ -10,7 +10,7 @@ type Storage interface {
Get(filename string, hash string) error Get(filename string, hash string) error
} }
func GetStorage(config *conf.ConfigFile) (*Storage, error) { func GetStorage(config *conf.ConfigFile) (Storage, error) {
storageMethod, err := config.GetString("storage", "method") storageMethod, err := config.GetString("storage", "method")
if err != nil { if err != nil {
return nil, errors.New("Error: storage method not specified in config file.") return nil, errors.New("Error: storage method not specified in config file.")
@ -28,5 +28,5 @@ func GetStorage(config *conf.ConfigFile) (*Storage, error) {
return nil, errors.New("Error: storage method '" + storageMethod + "' not implemented.") return nil, errors.New("Error: storage method '" + storageMethod + "' not implemented.")
} }
return &storage, nil return storage, nil
} }

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)
}
}
}