diff --git a/asink.go b/asink.go index 8a0f6ab..3e7b3e4 100644 --- a/asink.go +++ b/asink.go @@ -5,7 +5,6 @@ import ( "flag" "path" "os/user" - "github.com/howeyc/fsnotify" "code.google.com/p/goconf/conf" ) @@ -48,36 +47,20 @@ func main() { fmt.Println(cachedir) fmt.Println(storage) - setup_watchers() -} - -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 - } + fileUpdates := make(chan *Event) + go StartWatching(syncdir, fileUpdates) for { - select { - case ev := <-watcher.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 { - fmt.Println(err) - } else { - fmt.Println(hash) - } - case err := <-watcher.Error: - fmt.Println("error:", err) - } + event := <- fileUpdates + ProcessEvent(storage, event) + } +} + +func ProcessEvent(storage Storage, event *Event) { + fmt.Println(event) + + if event.IsUpdate() { + err := storage.Put(event.Path, event.Hash) + if err != nil { panic(err) } } } diff --git a/events.go b/events.go new file mode 100644 index 0000000..72b35a7 --- /dev/null +++ b/events.go @@ -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 +} diff --git a/storage.go b/storage.go index ff0aab4..1cc0d26 100644 --- a/storage.go +++ b/storage.go @@ -10,7 +10,7 @@ type Storage interface { 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") if err != nil { 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 &storage, nil + return storage, nil } diff --git a/watcher.go b/watcher.go new file mode 100644 index 0000000..488bc77 --- /dev/null +++ b/watcher.go @@ -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) + } + } +}