This commit is contained in:
Aaron Lindsay 2013-02-11 23:17:12 -05:00
parent 54a0359897
commit 61d5532e33
6 changed files with 69 additions and 39 deletions

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"fmt"
"flag"
"path"
"os/user"
"code.google.com/p/goconf/conf" "code.google.com/p/goconf/conf"
"flag"
"fmt"
"os/user"
"path"
) )
var configFileName string var configFileName string
@ -17,7 +17,7 @@ func init() {
u, err := user.Current() u, err := user.Current()
if err == nil { if err == nil {
userHomeDir = u.HomeDir userHomeDir = u.HomeDir
} }
flag.StringVar(&configFileName, "config", path.Join(userHomeDir, ".asink", "config"), config_usage) flag.StringVar(&configFileName, "config", path.Join(userHomeDir, ".asink", "config"), config_usage)
flag.StringVar(&configFileName, "c", path.Join(userHomeDir, ".asink", "config"), config_usage+" (shorthand)") flag.StringVar(&configFileName, "c", path.Join(userHomeDir, ".asink", "config"), config_usage+" (shorthand)")
@ -51,7 +51,7 @@ func main() {
go StartWatching(syncdir, fileUpdates) go StartWatching(syncdir, fileUpdates)
for { for {
event := <- fileUpdates event := <-fileUpdates
ProcessEvent(storage, event) ProcessEvent(storage, event)
} }
} }
@ -61,6 +61,8 @@ func ProcessEvent(storage Storage, event *Event) {
if event.IsUpdate() { if event.IsUpdate() {
err := storage.Put(event.Path, event.Hash) err := storage.Put(event.Path, event.Hash)
if err != nil { panic(err) } if err != nil {
panic(err)
}
} }
} }

View File

@ -3,7 +3,7 @@ package main
type EventType uint32 type EventType uint32
const ( const (
UPDATE = 1 << iota UPDATE = 1 << iota
DELETE DELETE
) )
@ -14,9 +14,9 @@ type Event struct {
} }
func (e Event) IsUpdate() bool { func (e Event) IsUpdate() bool {
return e.Type & UPDATE == UPDATE return e.Type&UPDATE == UPDATE
} }
func (e Event) IsDelete() bool { func (e Event) IsDelete() bool {
return e.Type & DELETE == DELETE return e.Type&DELETE == DELETE
} }

12
hash.go
View File

@ -1,21 +1,25 @@
package main package main
import ( import (
"crypto/sha256"
"fmt"
"io" "io"
"os" "os"
"fmt"
"crypto/sha256"
) )
func HashFile(filename string) (string, error) { func HashFile(filename string) (string, error) {
hashfn := sha256.New() hashfn := sha256.New()
infile, err := os.Open(filename) infile, err := os.Open(filename)
if err != nil { return "", err } if err != nil {
return "", err
}
defer infile.Close() defer infile.Close()
_, err = io.Copy(hashfn, infile) _, err = io.Copy(hashfn, infile)
if err != nil { return "", err } if err != nil {
return "", err
}
return fmt.Sprintf("%x", hashfn.Sum(nil)), nil return fmt.Sprintf("%x", hashfn.Sum(nil)), nil
} }

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"errors"
"code.google.com/p/goconf/conf" "code.google.com/p/goconf/conf"
"errors"
) )
type Storage interface { type Storage interface {
@ -19,13 +19,13 @@ func GetStorage(config *conf.ConfigFile) (Storage, error) {
var storage Storage var storage Storage
switch storageMethod { switch storageMethod {
case "local": case "local":
storage, err = NewLocalStorage(config) storage, err = NewLocalStorage(config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
default: default:
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

View File

@ -1,26 +1,30 @@
package main package main
import ( import (
"os" "code.google.com/p/goconf/conf"
"errors"
"io" "io"
"io/ioutil" "io/ioutil"
"os"
"path" "path"
"errors"
"code.google.com/p/goconf/conf"
) )
type LocalStorage struct { type LocalStorage struct {
storageDir string storageDir string
tmpSubdir string tmpSubdir string
} }
func ensureDirExists(dir string) error { func ensureDirExists(dir string) error {
_, err := os.Lstat(dir) _, err := os.Lstat(dir)
if err != nil { if err != nil {
fi, err := os.Lstat(path.Dir(dir)) fi, err := os.Lstat(path.Dir(dir))
if err != nil { return err } if err != nil {
return err
}
err = os.Mkdir(dir, fi.Mode().Perm()) err = os.Mkdir(dir, fi.Mode().Perm())
if err != nil { return err } if err != nil {
return err
}
} }
return nil return nil
} }
@ -37,36 +41,50 @@ func NewLocalStorage(config *conf.ConfigFile) (*LocalStorage, error) {
//make sure the base directory and tmp subdir exist //make sure the base directory and tmp subdir exist
err = ensureDirExists(ls.storageDir) err = ensureDirExists(ls.storageDir)
if err != nil { return nil, err} if err != nil {
return nil, err
}
err = ensureDirExists(ls.tmpSubdir) err = ensureDirExists(ls.tmpSubdir)
if err != nil { return nil, err} if err != nil {
return nil, err
}
return ls, nil return ls, nil
} }
func (ls *LocalStorage) copyToTmp(src string) (string, error) { func (ls *LocalStorage) copyToTmp(src string) (string, error) {
infile, err := os.Open(src) infile, err := os.Open(src)
if err != nil { return "", err } if err != nil {
return "", err
}
defer infile.Close() defer infile.Close()
outfile, err := ioutil.TempFile(ls.tmpSubdir, "asink") outfile, err := ioutil.TempFile(ls.tmpSubdir, "asink")
if err != nil { return "", err } if err != nil {
return "", err
}
defer outfile.Close() defer outfile.Close()
_, err = io.Copy(outfile, infile) _, err = io.Copy(outfile, infile)
if err != nil { return "", err } if err != nil {
return "", err
}
return outfile.Name(), nil return outfile.Name(), nil
} }
func (ls *LocalStorage) Put(filename string, hash string) (e error) { func (ls *LocalStorage) Put(filename string, hash string) (e error) {
tmpfile, err := ls.copyToTmp(filename) tmpfile, err := ls.copyToTmp(filename)
if err != nil { return err } if err != nil {
return err
}
err = os.Rename(tmpfile, path.Join(ls.storageDir, hash)) err = os.Rename(tmpfile, path.Join(ls.storageDir, hash))
if err != nil { if err != nil {
err := os.Remove(tmpfile) err := os.Remove(tmpfile)
if err != nil { return err } if err != nil {
return err
}
} }
return nil return nil
@ -74,11 +92,15 @@ func (ls *LocalStorage) Put(filename string, hash string) (e error) {
func (ls *LocalStorage) Get(filename string, hash string) error { func (ls *LocalStorage) Get(filename string, hash string) error {
infile, err := os.Open(path.Join(ls.storageDir, hash)) infile, err := os.Open(path.Join(ls.storageDir, hash))
if err != nil { return err } if err != nil {
return err
}
defer infile.Close() defer infile.Close()
outfile, err := os.Open(filename) outfile, err := os.Open(filename)
if err != nil { return err } if err != nil {
return err
}
defer outfile.Close() defer outfile.Close()
_, err = io.Copy(outfile, infile) _, err = io.Copy(outfile, infile)

View File

@ -30,7 +30,9 @@ func StartWatching(watchDir string, fileUpdates chan *Event) {
event.Path = ev.Name event.Path = ev.Name
if event.IsUpdate() { if event.IsUpdate() {
event.Hash, err = HashFile(ev.Name) event.Hash, err = HashFile(ev.Name)
if err != nil { continue } if err != nil {
continue
}
} else { } else {
event.Hash = "" event.Hash = ""
} }