1
0
Fork 0
go-asink/client/asink.go

201 lines
4.4 KiB
Go
Raw Normal View History

2013-02-10 23:39:23 -05:00
package main
import (
"asink"
"asink/util"
2013-02-11 23:17:12 -05:00
"code.google.com/p/goconf/conf"
2013-02-10 23:39:23 -05:00
"flag"
2013-02-11 23:17:12 -05:00
"fmt"
"io/ioutil"
"os"
2013-02-10 23:39:23 -05:00
"os/user"
2013-02-11 23:17:12 -05:00
"path"
2013-02-10 23:39:23 -05:00
)
type AsinkGlobals struct {
configFileName string
syncDir string
cacheDir string
tmpDir string
db *AsinkDB
storage Storage
server string
port int
}
var globals AsinkGlobals
2013-02-10 23:39:23 -05:00
func init() {
const config_usage = "Config File to use"
userHomeDir := "~"
u, err := user.Current()
if err == nil {
userHomeDir = u.HomeDir
2013-02-11 23:17:12 -05:00
}
2013-02-10 23:39:23 -05:00
flag.StringVar(&globals.configFileName, "config", path.Join(userHomeDir, ".asink", "config"), config_usage)
flag.StringVar(&globals.configFileName, "c", path.Join(userHomeDir, ".asink", "config"), config_usage+" (shorthand)")
2013-02-10 23:39:23 -05:00
}
func main() {
flag.Parse()
config, err := conf.ReadConfigFile(globals.configFileName)
2013-02-10 23:39:23 -05:00
if err != nil {
fmt.Println(err)
fmt.Println("Error reading config file at ", globals.configFileName, ". Does it exist?")
2013-02-10 23:39:23 -05:00
return
}
globals.storage, err = GetStorage(config)
2013-02-10 23:39:23 -05:00
if err != nil {
fmt.Println(err)
return
}
globals.syncDir, err = config.GetString("local", "syncdir")
globals.cacheDir, err = config.GetString("local", "cachedir")
globals.tmpDir, err = config.GetString("local", "tmpdir")
//make sure all the necessary directories exist
err = util.EnsureDirExists(globals.syncDir)
if err != nil {
panic(err)
}
err = util.EnsureDirExists(globals.cacheDir)
if err != nil {
panic(err)
}
err = util.EnsureDirExists(globals.tmpDir)
if err != nil {
panic(err)
}
2013-02-10 23:39:23 -05:00
globals.server, err = config.GetString("server", "host")
globals.port, err = config.GetInt("server", "port")
globals.db, err = GetAndInitDB(config)
if err != nil {
panic(err)
}
2013-02-10 23:39:23 -05:00
2013-03-17 23:02:51 -04:00
//spawn goroutines to handle local events
localFileUpdates := make(chan *asink.Event)
go StartWatching(globals.syncDir, localFileUpdates)
//spawn goroutines to receive remote events
remoteFileUpdates := make(chan *asink.Event)
go GetEvents(globals, remoteFileUpdates)
go ProcessRemoteEvents(globals, remoteFileUpdates)
2013-02-11 23:16:19 -05:00
for {
2013-03-17 23:02:51 -04:00
event := <-localFileUpdates
go ProcessLocalEvent(globals, event)
2013-02-10 23:39:23 -05:00
}
2013-02-11 23:16:19 -05:00
}
2013-02-10 23:39:23 -05:00
2013-03-17 23:02:51 -04:00
func ProcessLocalEvent(globals AsinkGlobals, event *asink.Event) {
//add to database
err := globals.db.DatabaseAddEvent(event)
if err != nil {
panic(err)
}
2013-02-10 23:39:23 -05:00
2013-02-11 23:16:19 -05:00
if event.IsUpdate() {
//copy to tmp
tmpfilename, err := util.CopyToTmp(event.Path, globals.tmpDir)
if err != nil {
panic(err)
}
event.Status |= asink.COPIED_TO_TMP
//get the file's hash
hash, err := HashFile(tmpfilename)
event.Hash = hash
if err != nil {
panic(err)
}
event.Status |= asink.HASHED
//rename to local cache w/ filename=hash
err = os.Rename(tmpfilename, path.Join(globals.cacheDir, event.Hash))
if err != nil {
err := os.Remove(tmpfilename)
if err != nil {
panic(err)
}
2013-03-17 23:02:51 -04:00
panic(err)
}
event.Status |= asink.CACHED
//update database
err = globals.db.DatabaseUpdateEvent(event)
2013-02-11 23:17:12 -05:00
if err != nil {
panic(err)
}
//upload file to remote storage
err = globals.storage.Put(event.Path, event.Hash)
if err != nil {
panic(err)
}
event.Status |= asink.UPLOADED
//update database again
err = globals.db.DatabaseUpdateEvent(event)
if err != nil {
panic(err)
}
2013-02-10 23:39:23 -05:00
}
//finally, send it off to the server
2013-02-22 00:06:10 -05:00
err = SendEvent(globals, event)
if err != nil {
panic(err) //TODO handle sensibly
}
event.Status |= asink.ON_SERVER
err = globals.db.DatabaseUpdateEvent(event)
2013-02-22 00:06:10 -05:00
if err != nil {
2013-03-09 22:47:49 -05:00
panic(err) //TODO probably, definitely, none of these should panic
2013-02-22 00:06:10 -05:00
}
}
func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) {
//Download event
if event.IsUpdate() {
outfile, err := ioutil.TempFile(globals.tmpDir, "asink")
if err != nil {
panic(err) //TODO handle sensibly
}
tmpfilename := outfile.Name()
outfile.Close()
err = globals.storage.Get(tmpfilename, event.Hash)
if err != nil {
panic(err) //TODO handle sensibly
}
//rename to local hashed filename
err = os.Rename(tmpfilename, path.Join(globals.cacheDir, event.Hash))
if err != nil {
err := os.Remove(tmpfilename)
if err != nil {
panic(err)
}
panic(err)
}
}
fmt.Println(event)
//TODO make sure file being overwritten is either unchanged or already copied off and hashed
//TODO add event to the local database, and populate the local directory
}
2013-03-17 23:02:51 -04:00
func ProcessRemoteEvents(globals AsinkGlobals, eventChan chan *asink.Event) {
for event := range eventChan {
ProcessRemoteEvent(globals, event)
}
2013-02-10 23:39:23 -05:00
}