2013-02-10 23:39:23 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-02-11 23:17:12 -05:00
|
|
|
"code.google.com/p/goconf/conf"
|
|
|
|
"errors"
|
2013-02-10 23:39:23 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-02-11 23:17:12 -05:00
|
|
|
"os"
|
2013-02-10 23:39:23 -05:00
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalStorage struct {
|
|
|
|
storageDir string
|
2013-02-11 23:17:12 -05:00
|
|
|
tmpSubdir string
|
2013-02-10 23:39:23 -05:00
|
|
|
}
|
|
|
|
|
2013-02-11 07:25:29 -05:00
|
|
|
func ensureDirExists(dir string) error {
|
|
|
|
_, err := os.Lstat(dir)
|
|
|
|
if err != nil {
|
|
|
|
fi, err := os.Lstat(path.Dir(dir))
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-02-11 07:25:29 -05:00
|
|
|
err = os.Mkdir(dir, fi.Mode().Perm())
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-02-11 07:25:29 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-10 23:39:23 -05:00
|
|
|
func NewLocalStorage(config *conf.ConfigFile) (*LocalStorage, error) {
|
|
|
|
storageDir, err := config.GetString("storage", "dir")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("Error: LocalStorage indicated in config file, but lacking local storage directory ('dir = some/dir').")
|
|
|
|
}
|
|
|
|
|
|
|
|
ls := new(LocalStorage)
|
|
|
|
ls.storageDir = storageDir
|
|
|
|
ls.tmpSubdir = path.Join(storageDir, ".asink-tmpdir")
|
2013-02-11 07:25:29 -05:00
|
|
|
|
|
|
|
//make sure the base directory and tmp subdir exist
|
|
|
|
err = ensureDirExists(ls.storageDir)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-02-11 07:25:29 -05:00
|
|
|
err = ensureDirExists(ls.tmpSubdir)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-02-11 07:25:29 -05:00
|
|
|
|
2013-02-10 23:39:23 -05:00
|
|
|
return ls, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LocalStorage) copyToTmp(src string) (string, error) {
|
|
|
|
infile, err := os.Open(src)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
defer infile.Close()
|
2013-02-11 23:17:12 -05:00
|
|
|
|
2013-02-10 23:39:23 -05:00
|
|
|
outfile, err := ioutil.TempFile(ls.tmpSubdir, "asink")
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
defer outfile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(outfile, infile)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
|
|
|
return outfile.Name(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LocalStorage) Put(filename string, hash string) (e error) {
|
|
|
|
tmpfile, err := ls.copyToTmp(filename)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
|
|
|
err = os.Rename(tmpfile, path.Join(ls.storageDir, hash))
|
2013-02-11 07:37:07 -05:00
|
|
|
if err != nil {
|
|
|
|
err := os.Remove(tmpfile)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-02-11 07:37:07 -05:00
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LocalStorage) Get(filename string, hash string) error {
|
|
|
|
infile, err := os.Open(path.Join(ls.storageDir, hash))
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
defer infile.Close()
|
|
|
|
|
|
|
|
outfile, err := os.Open(filename)
|
2013-02-11 23:17:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-02-10 23:39:23 -05:00
|
|
|
defer outfile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(outfile, infile)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|