2014-09-23 20:39:12 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2014-10-04 14:49:39 +00:00
|
|
|
"errors"
|
2014-09-23 20:39:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-10-04 14:49:39 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-07 21:19:26 +00:00
|
|
|
|
|
|
|
"github.com/juju/arrar"
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-10-04 14:49:39 +00:00
|
|
|
dirMode = 0700
|
|
|
|
blobPath = "blobs"
|
|
|
|
snapshotPath = "snapshots"
|
|
|
|
treePath = "trees"
|
|
|
|
lockPath = "locks"
|
|
|
|
keyPath = "keys"
|
|
|
|
tempPath = "tmp"
|
|
|
|
versionFileName = "version"
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Local struct {
|
2014-10-04 14:49:39 +00:00
|
|
|
p string
|
|
|
|
ver uint
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenLocal opens the local backend at dir.
|
|
|
|
func OpenLocal(dir string) (*Local, error) {
|
|
|
|
items := []string{
|
|
|
|
dir,
|
|
|
|
filepath.Join(dir, blobPath),
|
|
|
|
filepath.Join(dir, snapshotPath),
|
|
|
|
filepath.Join(dir, treePath),
|
|
|
|
filepath.Join(dir, lockPath),
|
|
|
|
filepath.Join(dir, keyPath),
|
|
|
|
filepath.Join(dir, tempPath),
|
|
|
|
}
|
|
|
|
|
|
|
|
// test if all necessary dirs and files are there
|
|
|
|
for _, d := range items {
|
|
|
|
if _, err := os.Stat(d); err != nil {
|
|
|
|
return nil, fmt.Errorf("%s does not exist", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-04 14:49:39 +00:00
|
|
|
// read version file
|
|
|
|
f, err := os.Open(filepath.Join(dir, versionFileName))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read version file: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
n, err := f.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
version, err := strconv.Atoi(strings.TrimSpace(string(buf[:n])))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to convert version to integer: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check version
|
|
|
|
if version != BackendVersion {
|
|
|
|
return nil, fmt.Errorf("wrong version %d", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Local{p: dir, ver: uint(version)}, nil
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLocal creates all the necessary files and directories for a new local
|
|
|
|
// backend at dir.
|
|
|
|
func CreateLocal(dir string) (*Local, error) {
|
2014-10-04 14:49:39 +00:00
|
|
|
versionFile := filepath.Join(dir, versionFileName)
|
2014-09-23 20:39:12 +00:00
|
|
|
dirs := []string{
|
|
|
|
dir,
|
|
|
|
filepath.Join(dir, blobPath),
|
|
|
|
filepath.Join(dir, snapshotPath),
|
|
|
|
filepath.Join(dir, treePath),
|
|
|
|
filepath.Join(dir, lockPath),
|
|
|
|
filepath.Join(dir, keyPath),
|
|
|
|
filepath.Join(dir, tempPath),
|
|
|
|
}
|
|
|
|
|
2014-10-04 14:49:39 +00:00
|
|
|
// test if version file already exists
|
|
|
|
_, err := os.Lstat(versionFile)
|
|
|
|
if err == nil {
|
|
|
|
return nil, errors.New("version file already exists")
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
// test if directories already exist
|
|
|
|
for _, d := range dirs[1:] {
|
|
|
|
if _, err := os.Stat(d); err == nil {
|
|
|
|
return nil, fmt.Errorf("dir %s already exists", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create paths for blobs, refs and temp
|
|
|
|
for _, d := range dirs {
|
|
|
|
err := os.MkdirAll(d, dirMode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-04 14:49:39 +00:00
|
|
|
// create version file
|
|
|
|
f, err := os.Create(versionFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-15 14:30:54 +00:00
|
|
|
_, err = f.Write([]byte(fmt.Sprintf("%d\n", BackendVersion)))
|
2014-10-04 14:49:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:19:26 +00:00
|
|
|
// open backend
|
2014-09-23 20:39:12 +00:00
|
|
|
return OpenLocal(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Location returns this backend's location (the directory name).
|
|
|
|
func (b *Local) Location() string {
|
|
|
|
return b.p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return temp directory in correct directory for this backend.
|
|
|
|
func (b *Local) tempFile() (*os.File, error) {
|
|
|
|
return ioutil.TempFile(filepath.Join(b.p, tempPath), "temp-")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename temp file to final name according to type and ID.
|
|
|
|
func (b *Local) renameFile(file *os.File, t Type, id ID) error {
|
|
|
|
filename := filepath.Join(b.dir(t), id.String())
|
|
|
|
return os.Rename(file.Name(), filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct directory for given Type.
|
|
|
|
func (b *Local) dir(t Type) string {
|
|
|
|
var n string
|
|
|
|
switch t {
|
|
|
|
case Blob:
|
|
|
|
n = blobPath
|
|
|
|
case Snapshot:
|
|
|
|
n = snapshotPath
|
|
|
|
case Tree:
|
|
|
|
n = treePath
|
|
|
|
case Lock:
|
|
|
|
n = lockPath
|
|
|
|
case Key:
|
|
|
|
n = keyPath
|
|
|
|
}
|
|
|
|
return filepath.Join(b.p, n)
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:19:26 +00:00
|
|
|
// Create stores new content of type t and data and returns the ID. If the blob
|
|
|
|
// is already present, returns ErrAlreadyPresent and the blob's ID.
|
2014-09-23 20:39:12 +00:00
|
|
|
func (b *Local) Create(t Type, data []byte) (ID, error) {
|
|
|
|
// TODO: make sure that tempfile is removed upon error
|
|
|
|
|
2014-10-07 21:19:26 +00:00
|
|
|
// check if blob is already present in backend
|
|
|
|
id := IDFromData(data)
|
|
|
|
res, err := b.Test(t, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, arrar.Annotate(err, "test for presence")
|
|
|
|
}
|
|
|
|
|
|
|
|
if res {
|
|
|
|
return id, ErrAlreadyPresent
|
|
|
|
}
|
|
|
|
|
|
|
|
// create tempfile in backend
|
2014-09-23 20:39:12 +00:00
|
|
|
file, err := b.tempFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write data to tempfile
|
|
|
|
_, err = file.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-04 17:20:15 +00:00
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// return id
|
2014-09-23 20:39:12 +00:00
|
|
|
err = b.renameFile(file, t, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct path for given Type and ID.
|
|
|
|
func (b *Local) filename(t Type, id ID) string {
|
|
|
|
return filepath.Join(b.dir(t), id.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the content stored under the given ID.
|
|
|
|
func (b *Local) Get(t Type, id ID) ([]byte, error) {
|
|
|
|
// try to open file
|
|
|
|
file, err := os.Open(b.filename(t, id))
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// read all
|
|
|
|
buf, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test returns true if a blob of the given type and ID exists in the backend.
|
|
|
|
func (b *Local) Test(t Type, id ID) (bool, error) {
|
|
|
|
// try to open file
|
|
|
|
file, err := os.Open(b.filename(t, id))
|
|
|
|
defer func() {
|
|
|
|
file.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the content stored at ID.
|
|
|
|
func (b *Local) Remove(t Type, id ID) error {
|
|
|
|
return os.Remove(b.filename(t, id))
|
|
|
|
}
|
|
|
|
|
|
|
|
// List lists all objects of a given type.
|
|
|
|
func (b *Local) List(t Type) (IDs, error) {
|
|
|
|
// TODO: use os.Open() and d.Readdirnames() instead of Glob()
|
|
|
|
pattern := filepath.Join(b.dir(t), "*")
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make(IDs, 0, len(matches))
|
|
|
|
|
|
|
|
for _, m := range matches {
|
|
|
|
base := filepath.Base(m)
|
|
|
|
|
|
|
|
if base == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
id, err := ParseID(base)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids, nil
|
|
|
|
}
|
2014-10-04 14:49:39 +00:00
|
|
|
|
|
|
|
// Version returns the version of this local backend.
|
|
|
|
func (b *Local) Version() uint {
|
|
|
|
return b.ver
|
|
|
|
}
|
2014-10-04 17:20:15 +00:00
|
|
|
|
2014-10-07 21:19:26 +00:00
|
|
|
// Close closes the backend
|
2014-10-04 17:20:15 +00:00
|
|
|
func (b *Local) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|