From 2fb17838852cd58ebd41a8fdd6f4b651b25f1f23 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 3 May 2015 16:43:27 +0200 Subject: [PATCH] backend/server: remove id and version from backend --- backend/interface.go | 13 +---- backend/local/local.go | 126 ++++------------------------------------- backend/paths.go | 6 +- backend/sftp/sftp.go | 123 ++++------------------------------------ cache.go | 4 +- server/server.go | 2 +- 6 files changed, 28 insertions(+), 246 deletions(-) diff --git a/backend/interface.go b/backend/interface.go index a20b0e751..63a3a95f8 100644 --- a/backend/interface.go +++ b/backend/interface.go @@ -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 diff --git a/backend/local/local.go b/backend/local/local.go index a50d2ef31..753e905fa 100644 --- a/backend/local/local.go +++ b/backend/local/local.go @@ -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" ) @@ -19,9 +15,7 @@ import ( var ErrWrongData = errors.New("wrong data returned by backend, checksum does not match") type Local struct { - p string - ver uint - id string + p 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) } diff --git a/backend/paths.go b/backend/paths.go index d697a6cc6..8e29e6950 100644 --- a/backend/paths.go +++ b/backend/paths.go @@ -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 diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index 5c850ce77..2207d897a 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -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" @@ -24,10 +21,8 @@ const ( ) type SFTP struct { - c *sftp.Client - p string - ver uint - id string + c *sftp.Client + p 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 { diff --git a/cache.go b/cache.go index 24f7a17fd..56fe3e321 100644 --- a/cache.go +++ b/cache.go @@ -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 diff --git a/server/server.go b/server/server.go index accc1c40c..c616fbb0d 100644 --- a/server/server.go +++ b/server/server.go @@ -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 {