Separate watcher, create event object
This commit is contained in:
		
							
								
								
									
										43
									
								
								asink.go
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								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) }
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										22
									
								
								events.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								events.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										46
									
								
								watcher.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								watcher.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user