restic/backend/local.go

336 lines
6.8 KiB
Go
Raw Normal View History

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
dataPath = "data"
2014-10-04 14:49:39 +00:00
snapshotPath = "snapshots"
treePath = "trees"
2014-11-23 21:26:01 +00:00
mapPath = "maps"
2014-10-04 14:49:39 +00:00
lockPath = "locks"
keyPath = "keys"
tempPath = "tmp"
versionFileName = "version"
2014-09-23 20:39:12 +00:00
)
2014-11-24 21:11:09 +00:00
var ErrWrongData = errors.New("wrong data returned by backend, checksum does not match")
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, dataPath),
2014-09-23 20:39:12 +00:00
filepath.Join(dir, snapshotPath),
filepath.Join(dir, treePath),
filepath.Join(dir, mapPath),
2014-09-23 20:39:12 +00:00
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, dataPath),
2014-09-23 20:39:12 +00:00
filepath.Join(dir, snapshotPath),
filepath.Join(dir, treePath),
2014-11-23 21:26:01 +00:00
filepath.Join(dir, mapPath),
2014-09-23 20:39:12 +00:00
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 data, refs and temp
2014-09-23 20:39:12 +00:00
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 {
newname := b.filename(t, id)
oldname := file.Name()
if t == Data || t == Tree {
// create directories if necessary, ignore errors
os.MkdirAll(filepath.Dir(newname), dirMode)
}
return os.Rename(oldname, newname)
2014-09-23 20:39:12 +00:00
}
// Construct directory for given Type.
func (b *Local) dirname(t Type, id ID) string {
2014-09-23 20:39:12 +00:00
var n string
switch t {
case Data:
n = dataPath
if id != nil {
n = filepath.Join(dataPath, fmt.Sprintf("%02x", id[0]))
}
2014-09-23 20:39:12 +00:00
case Snapshot:
n = snapshotPath
case Tree:
n = treePath
if id != nil {
n = filepath.Join(treePath, fmt.Sprintf("%02x", id[0]))
}
2014-11-23 21:26:01 +00:00
case Map:
n = mapPath
2014-09-23 20:39:12 +00:00
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.dirname(t, id), id.String())
2014-09-23 20:39:12 +00:00
}
2014-11-24 21:11:09 +00:00
// Get returns the content stored under the given ID. If the data doesn't match
// the requested ID, ErrWrongData is returned.
2014-09-23 20:39:12 +00:00
func (b *Local) Get(t Type, id ID) ([]byte, error) {
2014-11-30 21:34:21 +00:00
if id == nil {
return nil, errors.New("unable to load nil ID")
}
2014-09-23 20:39:12 +00:00
// 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
}
2014-11-24 21:11:09 +00:00
// check id
if !Hash(buf).Equal(id) {
return nil, ErrWrongData
}
2014-09-23 20:39:12 +00:00
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()
var pattern string
if t == Data || t == Tree {
pattern = filepath.Join(b.dirname(t, nil), "*", "*")
} else {
pattern = filepath.Join(b.dirname(t, nil), "*")
}
2014-09-23 20:39:12 +00:00
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
}
2014-12-21 14:57:41 +00:00
// Delete removes the repository and all files.
func (b *Local) Delete() error {
return os.RemoveAll(b.p)
}