2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-28 04:56:04 +00:00

Add ID to repository

This allows identifying a repository regardless if it's accessed over
SFTP or locally. Introduced for having a per-repository cache.
This commit is contained in:
Alexander Neumann 2015-03-14 11:56:45 +01:00
parent 8156d4481d
commit f69a39cff5
4 changed files with 146 additions and 26 deletions

View File

@ -62,6 +62,10 @@ type Locationer interface {
Location() string Location() string
} }
type IDer interface {
ID() ID
}
type Backend interface { type Backend interface {
Lister Lister
Getter Getter
@ -69,4 +73,5 @@ type Backend interface {
Tester Tester
Remover Remover
Closer Closer
IDer
} }

View File

@ -1,13 +1,14 @@
package backend package backend
import ( import (
"crypto/rand"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
) )
@ -20,6 +21,7 @@ const (
keyPath = "keys" keyPath = "keys"
tempPath = "tmp" tempPath = "tmp"
versionFileName = "version" versionFileName = "version"
idFileName = "id"
) )
var ErrWrongData = errors.New("wrong data returned by backend, checksum does not match") var ErrWrongData = errors.New("wrong data returned by backend, checksum does not match")
@ -27,6 +29,7 @@ var ErrWrongData = errors.New("wrong data returned by backend, checksum does not
type Local struct { type Local struct {
p string p string
ver uint ver uint
id ID
} }
// OpenLocal opens the local backend at dir. // OpenLocal opens the local backend at dir.
@ -54,8 +57,33 @@ func OpenLocal(dir string) (*Local, error) {
return nil, fmt.Errorf("unable to read version file: %v\n", err) return nil, fmt.Errorf("unable to read version file: %v\n", err)
} }
buf := make([]byte, 100) var version uint
n, err := f.Read(buf) 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 != BackendVersion {
return nil, fmt.Errorf("wrong version %d", version)
}
// read ID
f, err = os.Open(filepath.Join(dir, idFileName))
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -65,23 +93,19 @@ func OpenLocal(dir string) (*Local, error) {
return nil, err return nil, err
} }
version, err := strconv.Atoi(strings.TrimSpace(string(buf[:n]))) id, err := ParseID(strings.TrimSpace(string(buf)))
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to convert version to integer: %v\n", err) return nil, err
} }
// check version return &Local{p: dir, ver: version, id: id}, nil
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) versionFile := filepath.Join(dir, versionFileName)
idFile := filepath.Join(dir, idFileName)
dirs := []string{ dirs := []string{
dir, dir,
filepath.Join(dir, dataPath), filepath.Join(dir, dataPath),
@ -92,12 +116,17 @@ func CreateLocal(dir string) (*Local, error) {
filepath.Join(dir, tempPath), filepath.Join(dir, tempPath),
} }
// test if version file already exists // test if files already exist
_, err := os.Lstat(versionFile) _, err := os.Lstat(versionFile)
if err == nil { if err == nil {
return nil, errors.New("version file already exists") return nil, errors.New("version file already exists")
} }
_, err = os.Lstat(idFile)
if err == nil {
return nil, errors.New("id 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 {
@ -119,7 +148,29 @@ func CreateLocal(dir string) (*Local, error) {
return nil, err return nil, err
} }
_, err = f.Write([]byte(fmt.Sprintf("%d\n", BackendVersion))) _, err = fmt.Fprintf(f, "%d\n", BackendVersion)
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.Fprintf(f, "%s\n", ID(id).String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -378,6 +429,11 @@ func (b *Local) Version() uint {
return b.ver return b.ver
} }
// ID returns the ID of this local backend.
func (b *Local) ID() ID {
return b.id
}
// Close closes the backend // Close closes the backend
func (b *Local) Close() error { func (b *Local) Close() error {
return nil return nil

View File

@ -2,6 +2,7 @@ package backend
import ( import (
"crypto/rand" "crypto/rand"
"crypto/sha256"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
@ -11,7 +12,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"github.com/pkg/sftp" "github.com/pkg/sftp"
@ -25,6 +25,7 @@ type SFTP struct {
c *sftp.Client c *sftp.Client
p string p string
ver uint ver uint
id ID
cmd *exec.Cmd cmd *exec.Cmd
} }
@ -92,9 +93,34 @@ func OpenSFTP(dir string, program string, args ...string) (*SFTP, error) {
return nil, fmt.Errorf("unable to read version file: %v\n", err) return nil, fmt.Errorf("unable to read version file: %v\n", err)
} }
buf := make([]byte, 100) var version uint
n, err := f.Read(buf) n, err := fmt.Fscanf(f, "%d", &version)
if err != nil && err != io.EOF { 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 != BackendVersion {
return nil, fmt.Errorf("wrong version %d", version)
}
// read ID
f, err = sftp.c.Open(filepath.Join(dir, idFileName))
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, err return nil, err
} }
@ -103,16 +129,12 @@ func OpenSFTP(dir string, program string, args ...string) (*SFTP, error) {
return nil, err return nil, err
} }
version, err := strconv.Atoi(strings.TrimSpace(string(buf[:n]))) id, err := ParseID(strings.TrimSpace(string(buf)))
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to convert version to integer: %v\n", err) return nil, err
}
// check version
if version != BackendVersion {
return nil, fmt.Errorf("wrong version %d", version)
} }
sftp.id = id
sftp.p = dir sftp.p = dir
return sftp, nil return sftp, nil
@ -127,6 +149,7 @@ func CreateSFTP(dir string, program string, args ...string) (*SFTP, error) {
} }
versionFile := filepath.Join(dir, versionFileName) versionFile := filepath.Join(dir, versionFileName)
idFile := filepath.Join(dir, idFileName)
dirs := []string{ dirs := []string{
dir, dir,
filepath.Join(dir, dataPath), filepath.Join(dir, dataPath),
@ -137,12 +160,17 @@ func CreateSFTP(dir string, program string, args ...string) (*SFTP, error) {
filepath.Join(dir, tempPath), filepath.Join(dir, tempPath),
} }
// test if version file already exists // test if files already exist
_, err = sftp.c.Lstat(versionFile) _, err = sftp.c.Lstat(versionFile)
if err == nil { if err == nil {
return nil, errors.New("version file already exists") return nil, errors.New("version file already exists")
} }
_, err = sftp.c.Lstat(idFile)
if err == nil {
return nil, errors.New("id 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 := sftp.c.Lstat(d); err == nil { if _, err := sftp.c.Lstat(d); err == nil {
@ -164,7 +192,29 @@ func CreateSFTP(dir string, program string, args ...string) (*SFTP, error) {
return nil, err return nil, err
} }
_, err = f.Write([]byte(strconv.Itoa(BackendVersion))) _, err = fmt.Fprintf(f, "%d\n", BackendVersion)
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.Fprintf(f, "%s\n", ID(id).String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -504,6 +554,11 @@ func (r *SFTP) Version() uint {
return r.ver return r.ver
} }
// ID returns the ID of this local backend.
func (r *SFTP) ID() ID {
return r.id
}
// Close closes the sftp connection and terminates the underlying command. // Close closes the sftp connection and terminates the underlying command.
func (s *SFTP) Close() error { func (s *SFTP) Close() error {
s.c.Close() s.c.Close()

View File

@ -414,3 +414,7 @@ func (s Server) Delete() error {
return errors.New("Delete() called for backend that does not implement this method") return errors.New("Delete() called for backend that does not implement this method")
} }
func (s Server) ID() backend.ID {
return s.be.ID()
}