2
2
mirror of https://github.com/octoleo/restic.git synced 2024-10-31 19:02:32 +00:00

Add 'version' file to local backend

This commit is contained in:
Alexander Neumann 2014-10-04 16:49:39 +02:00
parent 30ab03b7b7
commit bfd99a9be6
2 changed files with 79 additions and 9 deletions

View File

@ -10,12 +10,17 @@ const (
Tree = "tree" Tree = "tree"
) )
const (
BackendVersion = 1
)
type Server interface { type Server interface {
Create(Type, []byte) (ID, error) Create(Type, []byte) (ID, error)
Get(Type, ID) ([]byte, error) Get(Type, ID) ([]byte, error)
List(Type) (IDs, error) List(Type) (IDs, error)
Test(Type, ID) (bool, error) Test(Type, ID) (bool, error)
Remove(Type, ID) error Remove(Type, ID) error
Version() uint
Location() string Location() string
} }

View File

@ -1,10 +1,13 @@
package backend package backend
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings"
) )
const ( const (
@ -15,10 +18,12 @@ const (
lockPath = "locks" lockPath = "locks"
keyPath = "keys" keyPath = "keys"
tempPath = "tmp" tempPath = "tmp"
versionFileName = "version"
) )
type Local struct { type Local struct {
p string p string
ver uint
} }
// OpenLocal opens the local backend at dir. // OpenLocal opens the local backend at dir.
@ -40,12 +45,45 @@ func OpenLocal(dir string) (*Local, error) {
} }
} }
return &Local{p: dir}, nil // 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)
}
if version != BackendVersion {
return nil, fmt.Errorf("wrong version %d", version)
}
// check version
if version != BackendVersion {
return nil, fmt.Errorf("wrong version %d", version)
}
return &Local{p: dir, ver: uint(version)}, nil
} }
// CreateLocal creates all the necessary files and directories for a new local // CreateLocal creates all the necessary files and directories for a new local
// backend at dir. // backend at dir.
func CreateLocal(dir string) (*Local, error) { func CreateLocal(dir string) (*Local, error) {
versionFile := filepath.Join(dir, versionFileName)
dirs := []string{ dirs := []string{
dir, dir,
filepath.Join(dir, blobPath), filepath.Join(dir, blobPath),
@ -56,6 +94,12 @@ func CreateLocal(dir string) (*Local, error) {
filepath.Join(dir, tempPath), filepath.Join(dir, tempPath),
} }
// test if version file already exists
_, err := os.Lstat(versionFile)
if err == nil {
return nil, errors.New("version file already exists")
}
// test if directories already exist // test if directories already exist
for _, d := range dirs[1:] { for _, d := range dirs[1:] {
if _, err := os.Stat(d); err == nil { if _, err := os.Stat(d); err == nil {
@ -71,6 +115,22 @@ func CreateLocal(dir string) (*Local, error) {
} }
} }
// create version file
f, err := os.Create(versionFile)
if err != nil {
return nil, err
}
_, err = f.Write([]byte(strconv.Itoa(BackendVersion)))
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
// open repository // open repository
return OpenLocal(dir) return OpenLocal(dir)
} }
@ -211,3 +271,8 @@ func (b *Local) List(t Type) (IDs, error) {
return ids, nil return ids, nil
} }
// Version returns the version of this local backend.
func (b *Local) Version() uint {
return b.ver
}