backend/server: remove id and version from backend

This commit is contained in:
Alexander Neumann 2015-05-03 16:43:27 +02:00
parent 1cedff0e9e
commit 2fb1783885
6 changed files with 28 additions and 246 deletions

View File

@ -11,10 +11,7 @@ const (
Lock = "lock"
Snapshot = "snapshot"
Index = "index"
)
const (
Version = 1
Config = "config"
)
// A Backend manages data stored somewhere.
@ -43,17 +40,9 @@ type Backend interface {
// Close the backend
Close() error
Identifier
Lister
}
type Identifier interface {
// ID returns a unique ID for a specific repository. This means restic can
// recognize repositories accessed via different methods (e.g. local file
// access and sftp).
ID() string
}
type Lister interface {
// List returns a channel that yields all names of blobs of type t in
// lexicographic order. A goroutine is started for this. If the channel

View File

@ -1,9 +1,6 @@
package local
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
@ -11,7 +8,6 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/restic/restic/backend"
)
@ -20,8 +16,6 @@ var ErrWrongData = errors.New("wrong data returned by backend, checksum does not
type Local struct {
p string
ver uint
id string
}
// Open opens the local backend at dir.
@ -36,68 +30,19 @@ func Open(dir string) (*Local, error) {
filepath.Join(dir, backend.Paths.Temp),
}
// test if all necessary dirs and files are there
// test if all necessary dirs are there
for _, d := range items {
if _, err := os.Stat(d); err != nil {
return nil, fmt.Errorf("%s does not exist", d)
}
}
// read version file
f, err := os.Open(filepath.Join(dir, backend.Paths.Version))
if err != nil {
return nil, fmt.Errorf("unable to read version file: %v\n", err)
}
var version uint
n, err := fmt.Fscanf(f, "%d", &version)
if err != nil {
return nil, err
}
if n != 1 {
return nil, errors.New("could not read version from file")
}
err = f.Close()
if err != nil {
return nil, err
}
// check version
if version != backend.Version {
return nil, fmt.Errorf("wrong version %d", version)
}
// read ID
f, err = os.Open(filepath.Join(dir, backend.Paths.ID))
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
id := strings.TrimSpace(string(buf))
if err != nil {
return nil, err
}
return &Local{p: dir, ver: version, id: id}, nil
return &Local{p: dir}, nil
}
// Create creates all the necessary files and directories for a new local
// backend at dir.
// backend at dir. Afterwards a new config blob should must created.
func Create(dir string) (*Local, error) {
versionFile := filepath.Join(dir, backend.Paths.Version)
idFile := filepath.Join(dir, backend.Paths.ID)
dirs := []string{
dir,
filepath.Join(dir, backend.Paths.Data),
@ -108,15 +53,10 @@ func Create(dir string) (*Local, error) {
filepath.Join(dir, backend.Paths.Temp),
}
// test if files already exist
_, err := os.Lstat(versionFile)
// test if config file already exist
_, err := os.Lstat(backend.Paths.Config)
if err == nil {
return nil, errors.New("version file already exists")
}
_, err = os.Lstat(idFile)
if err == nil {
return nil, errors.New("id file already exists")
return nil, errors.New("config file already exists")
}
// test if directories already exist
@ -134,44 +74,6 @@ func Create(dir string) (*Local, error) {
}
}
// create version file
f, err := os.Create(versionFile)
if err != nil {
return nil, err
}
_, err = fmt.Fprintf(f, "%d\n", backend.Version)
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
// create ID file
id := make([]byte, sha256.Size)
_, err = rand.Read(id)
if err != nil {
return nil, err
}
f, err = os.Create(idFile)
if err != nil {
return nil, err
}
_, err = fmt.Fprintln(f, hex.EncodeToString(id))
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
// open backend
return Open(dir)
}
@ -265,6 +167,10 @@ func (b *Local) Create() (backend.Blob, error) {
// Construct path for given Type and name.
func filename(base string, t backend.Type, name string) string {
if t == backend.Config {
return filepath.Join(base, "config")
}
return filepath.Join(dirname(base, t, name), name)
}
@ -376,16 +282,6 @@ func (b *Local) List(t backend.Type, done <-chan struct{}) <-chan string {
return ch
}
// Version returns the version of this local backend.
func (b *Local) Version() uint {
return b.ver
}
// ID returns the ID of this local backend.
func (b *Local) ID() string {
return b.id
}
// Delete removes the repository and all files.
func (b *Local) Delete() error { return os.RemoveAll(b.p) }

View File

@ -10,8 +10,7 @@ var Paths = struct {
Locks string
Keys string
Temp string
Version string
ID string
Config string
}{
"data",
"snapshots",
@ -19,8 +18,7 @@ var Paths = struct {
"locks",
"keys",
"tmp",
"version",
"id",
"config",
}
// Default modes for file-based backends

View File

@ -2,18 +2,15 @@ package sftp
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"github.com/pkg/sftp"
"github.com/restic/restic/backend"
@ -26,8 +23,6 @@ const (
type SFTP struct {
c *sftp.Client
p string
ver uint
id string
cmd *exec.Cmd
}
@ -81,8 +76,7 @@ func Open(dir string, program string, args ...string) (*SFTP, error) {
filepath.Join(dir, backend.Paths.Index),
filepath.Join(dir, backend.Paths.Locks),
filepath.Join(dir, backend.Paths.Keys),
filepath.Join(dir, backend.Paths.Version),
filepath.Join(dir, backend.Paths.ID),
filepath.Join(dir, backend.Paths.Temp),
}
for _, d := range items {
if _, err := sftp.c.Lstat(d); err != nil {
@ -90,64 +84,18 @@ func Open(dir string, program string, args ...string) (*SFTP, error) {
}
}
// read version file
f, err := sftp.c.Open(filepath.Join(dir, backend.Paths.Version))
if err != nil {
return nil, fmt.Errorf("unable to read version file: %v\n", err)
}
var version uint
n, err := fmt.Fscanf(f, "%d", &version)
if err != nil {
return nil, err
}
if n != 1 {
return nil, errors.New("could not read version from file")
}
err = f.Close()
if err != nil {
return nil, err
}
// check version
if version != backend.Version {
return nil, fmt.Errorf("wrong version %d", version)
}
// read ID
f, err = sftp.c.Open(filepath.Join(dir, backend.Paths.ID))
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
sftp.id = strings.TrimSpace(string(buf))
sftp.p = dir
return sftp, nil
}
// Create creates all the necessary files and directories for a new sftp
// backend at dir.
// backend at dir. Afterwards a new config blob should must created.
func Create(dir string, program string, args ...string) (*SFTP, error) {
sftp, err := startClient(program, args...)
if err != nil {
return nil, err
}
versionFile := filepath.Join(dir, backend.Paths.Version)
idFile := filepath.Join(dir, backend.Paths.ID)
dirs := []string{
dir,
filepath.Join(dir, backend.Paths.Data),
@ -158,15 +106,10 @@ func Create(dir string, program string, args ...string) (*SFTP, error) {
filepath.Join(dir, backend.Paths.Temp),
}
// test if files already exist
_, err = sftp.c.Lstat(versionFile)
// test if config file already exist
_, err = sftp.c.Lstat(backend.Paths.Config)
if err == nil {
return nil, errors.New("version file already exists")
}
_, err = sftp.c.Lstat(idFile)
if err == nil {
return nil, errors.New("id file already exists")
return nil, errors.New("config file already exists")
}
// test if directories already exist
@ -184,44 +127,6 @@ func Create(dir string, program string, args ...string) (*SFTP, error) {
}
}
// create version file
f, err := sftp.c.Create(versionFile)
if err != nil {
return nil, err
}
_, err = fmt.Fprintf(f, "%d\n", backend.Version)
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
// create ID file
id := make([]byte, sha256.Size)
_, err = rand.Read(id)
if err != nil {
return nil, err
}
f, err = sftp.c.Create(idFile)
if err != nil {
return nil, err
}
_, err = fmt.Fprintln(f, hex.EncodeToString(id))
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
err = sftp.c.Close()
if err != nil {
return nil, err
@ -389,6 +294,10 @@ func (r *SFTP) Create() (backend.Blob, error) {
// Construct path for given backend.Type and name.
func (r *SFTP) filename(t backend.Type, name string) string {
if t == backend.Config {
return filepath.Join(r.p, "config")
}
return filepath.Join(r.dirname(t, name), name)
}
@ -542,16 +451,6 @@ func (r *SFTP) List(t backend.Type, done <-chan struct{}) <-chan string {
}
// Version returns the version of this local backend.
func (r *SFTP) Version() uint {
return r.ver
}
// ID returns the ID of this local backend.
func (r *SFTP) ID() string {
return r.id
}
// Close closes the sftp connection and terminates the underlying command.
func (s *SFTP) Close() error {
if s == nil {

View File

@ -18,13 +18,13 @@ type Cache struct {
base string
}
func NewCache(be backend.Identifier) (*Cache, error) {
func NewCache(s *server.Server) (*Cache, error) {
cacheDir, err := getCacheDir()
if err != nil {
return nil, err
}
basedir := filepath.Join(cacheDir, be.ID())
basedir := filepath.Join(cacheDir, s.ID())
debug.Log("Cache.New", "opened cache at %v", basedir)
return &Cache{base: basedir}, nil

View File

@ -601,7 +601,7 @@ func (s *Server) Delete() error {
}
func (s *Server) ID() string {
return s.be.ID()
return "empty"
}
func (s *Server) Location() string {