2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-24 21:57:41 +00:00

Refactor, add Object and Snapshot

This commit is contained in:
Alexander Neumann 2014-08-04 20:47:04 +02:00
parent fbd33636f0
commit b3c2d82331
10 changed files with 281 additions and 29 deletions

View File

@ -24,8 +24,9 @@ func hash(filename string) (khepri.ID, error) {
return h.Sum([]byte{}), nil return h.Sum([]byte{}), nil
} }
func archive_dir(repo *khepri.DirRepository, path string) (khepri.ID, error) { func archive_dir(repo *khepri.Repository, path string) (khepri.ID, error) {
log.Printf("archiving dir %q", path) log.Printf("archiving dir %q", path)
dir, err := os.Open(path) dir, err := os.Open(path)
if err != nil { if err != nil {
log.Printf("open(%q): %v\n", path, err) log.Printf("open(%q): %v\n", path, err)
@ -81,7 +82,7 @@ func archive_dir(repo *khepri.DirRepository, path string) (khepri.ID, error) {
return id, nil return id, nil
} }
func commandBackup(repo *khepri.DirRepository, args []string) error { func commandBackup(repo *khepri.Repository, args []string) error {
if len(args) != 1 { if len(args) != 1 {
return errors.New("usage: backup dir") return errors.New("usage: backup dir")
} }
@ -93,7 +94,11 @@ func commandBackup(repo *khepri.DirRepository, args []string) error {
return err return err
} }
fmt.Printf("%q archived as %v\n", target, id) sn := repo.NewSnapshot(target)
sn.Tree = id
sn.Save()
fmt.Printf("%q archived as %v\n", target, sn.ID())
return nil return nil
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/fd0/khepri" "github.com/fd0/khepri"
) )
func commandList(repo *khepri.DirRepository, args []string) error { func commandList(repo *khepri.Repository, args []string) error {
if len(args) != 1 { if len(args) != 1 {
return errors.New("usage: list [blob|ref]") return errors.New("usage: list [blob|ref]")
} }

View File

@ -10,7 +10,7 @@ import (
"github.com/fd0/khepri" "github.com/fd0/khepri"
) )
func restore_file(repo *khepri.DirRepository, node khepri.Node, target string) error { func restore_file(repo *khepri.Repository, node khepri.Node, target string) error {
fmt.Printf(" restore file %q\n", target) fmt.Printf(" restore file %q\n", target)
rd, err := repo.Get(khepri.TYPE_BLOB, node.Content) rd, err := repo.Get(khepri.TYPE_BLOB, node.Content)
@ -47,7 +47,7 @@ func restore_file(repo *khepri.DirRepository, node khepri.Node, target string) e
return nil return nil
} }
func restore_dir(repo *khepri.DirRepository, id khepri.ID, target string) error { func restore_dir(repo *khepri.Repository, id khepri.ID, target string) error {
fmt.Printf(" restore dir %q\n", target) fmt.Printf(" restore dir %q\n", target)
rd, err := repo.Get(khepri.TYPE_REF, id) rd, err := repo.Get(khepri.TYPE_REF, id)
if err != nil { if err != nil {
@ -104,7 +104,7 @@ func restore_dir(repo *khepri.DirRepository, id khepri.ID, target string) error
return nil return nil
} }
func commandRestore(repo *khepri.DirRepository, args []string) error { func commandRestore(repo *khepri.Repository, args []string) error {
if len(args) != 2 { if len(args) != 2 {
return errors.New("usage: restore ID dir") return errors.New("usage: restore ID dir")
} }

View File

@ -22,7 +22,7 @@ func errx(code int, format string, data ...interface{}) {
os.Exit(code) os.Exit(code)
} }
type commandFunc func(*khepri.DirRepository, []string) error type commandFunc func(*khepri.Repository, []string) error
var commands map[string]commandFunc var commands map[string]commandFunc
@ -61,7 +61,7 @@ func main() {
errx(1, "unknown command: %q\n", cmd) errx(1, "unknown command: %q\n", cmd)
} }
repo, err := khepri.NewDirRepository(Opts.Repo) repo, err := khepri.NewRepository(Opts.Repo)
if err != nil { if err != nil {
errx(1, "unable to create/open repo: %v", err) errx(1, "unable to create/open repo: %v", err)

119
object.go Normal file
View File

@ -0,0 +1,119 @@
package khepri
import "os"
type Object struct {
repo *Repository
id ID
tpe Type
hw HashingWriter
file *os.File
}
func (repo *Repository) NewObject(t Type) (*Object, error) {
obj := &Object{
repo: repo,
tpe: t,
}
return obj, obj.open()
}
func (obj *Object) open() error {
if obj.isFinal() {
panic("object is finalized")
}
if obj.isOpen() {
panic("object already open")
}
// create tempfile in repository
if obj.hw == nil {
// save contents to tempfile, hash while writing
var err error
obj.file, err = obj.repo.tempFile()
if err != nil {
return err
}
// create hashing writer
obj.hw = NewHashingWriter(obj.file, obj.repo.hash)
}
return nil
}
func (obj *Object) isOpen() bool {
return obj.file != nil && obj.hw != nil
}
func (obj *Object) isFinal() bool {
return obj.id != nil
}
func (obj *Object) Write(data []byte) (int, error) {
if !obj.isOpen() {
panic("object not open")
}
return obj.hw.Write(data)
}
func (obj *Object) Close() error {
if obj.file == nil || obj.hw == nil {
panic("object is not open")
}
obj.file.Close()
hash := obj.hw.Hash()
// move file to final name using hash of contents
id := ID(hash)
err := obj.repo.renameFile(obj.file, obj.tpe, id)
if err != nil {
return err
}
obj.hw = nil
obj.file = nil
obj.id = id
return nil
}
func (obj *Object) ID() ID {
if !obj.isFinal() {
panic("object not finalized")
}
return obj.id
}
func (obj *Object) Type() Type {
return obj.tpe
}
func (obj *Object) Remove() error {
if obj.id != nil {
return obj.repo.Remove(obj.tpe, obj.id)
}
if obj.file != nil {
file := obj.file
obj.hw = nil
obj.file = nil
err := file.Close()
if err != nil {
return err
}
return os.Remove(file.Name())
}
return nil
}

31
object_test.go Normal file
View File

@ -0,0 +1,31 @@
package khepri_test
import (
"testing"
"github.com/fd0/khepri"
)
func TestObjects(t *testing.T) {
repo, err := setupRepo()
ok(t, err)
defer func() {
err = teardownRepo(repo)
ok(t, err)
}()
for _, test := range TestStrings {
obj, err := repo.NewObject(khepri.TYPE_BLOB)
ok(t, err)
_, err = obj.Write([]byte(test.data))
ok(t, err)
obj.Close()
id, err := khepri.ParseID(test.id)
ok(t, err)
equals(t, id, obj.ID())
}
}

View File

@ -31,7 +31,7 @@ func (n Name) Encode() string {
return url.QueryEscape(string(n)) return url.QueryEscape(string(n))
} }
type DirRepository struct { type Repository struct {
path string path string
hash func() hash.Hash hash func() hash.Hash
} }
@ -66,8 +66,8 @@ func (t Type) String() string {
} }
// NewDirRepository creates a new dir-baked repository at the given path. // NewDirRepository creates a new dir-baked repository at the given path.
func NewDirRepository(path string) (*DirRepository, error) { func NewRepository(path string) (*Repository, error) {
d := &DirRepository{ d := &Repository{
path: path, path: path,
hash: sha256.New, hash: sha256.New,
} }
@ -81,7 +81,7 @@ func NewDirRepository(path string) (*DirRepository, error) {
return d, nil return d, nil
} }
func (r *DirRepository) create() error { func (r *Repository) create() error {
dirs := []string{ dirs := []string{
r.path, r.path,
path.Join(r.path, blobPath), path.Join(r.path, blobPath),
@ -100,28 +100,28 @@ func (r *DirRepository) create() error {
} }
// SetHash changes the hash function used for deriving IDs. Default is SHA256. // SetHash changes the hash function used for deriving IDs. Default is SHA256.
func (r *DirRepository) SetHash(h func() hash.Hash) { func (r *Repository) SetHash(h func() hash.Hash) {
r.hash = h r.hash = h
} }
// Path returns the directory used for this repository. // Path returns the directory used for this repository.
func (r *DirRepository) Path() string { func (r *Repository) Path() string {
return r.path return r.path
} }
// Return temp directory in correct directory for this repository. // Return temp directory in correct directory for this repository.
func (r *DirRepository) tempFile() (*os.File, error) { func (r *Repository) tempFile() (*os.File, error) {
return ioutil.TempFile(path.Join(r.path, tempPath), "temp-") return ioutil.TempFile(path.Join(r.path, tempPath), "temp-")
} }
// Rename temp file to final name according to type and ID. // Rename temp file to final name according to type and ID.
func (r *DirRepository) renameFile(file *os.File, t Type, id ID) error { func (r *Repository) renameFile(file *os.File, t Type, id ID) error {
filename := path.Join(r.dir(t), id.String()) filename := path.Join(r.dir(t), id.String())
return os.Rename(file.Name(), filename) return os.Rename(file.Name(), filename)
} }
// Put saves content and returns the ID. // Put saves content and returns the ID.
func (r *DirRepository) Put(t Type, reader io.Reader) (ID, error) { func (r *Repository) Put(t Type, reader io.Reader) (ID, error) {
// save contents to tempfile, hash while writing // save contents to tempfile, hash while writing
file, err := r.tempFile() file, err := r.tempFile()
if err != nil { if err != nil {
@ -150,7 +150,7 @@ func (r *DirRepository) Put(t Type, reader io.Reader) (ID, error) {
} }
// Construct directory for given Type. // Construct directory for given Type.
func (r *DirRepository) dir(t Type) string { func (r *Repository) dir(t Type) string {
switch t { switch t {
case TYPE_BLOB: case TYPE_BLOB:
return path.Join(r.path, blobPath) return path.Join(r.path, blobPath)
@ -162,12 +162,12 @@ func (r *DirRepository) dir(t Type) string {
} }
// Construct path for given Type and ID. // Construct path for given Type and ID.
func (r *DirRepository) filename(t Type, id ID) string { func (r *Repository) filename(t Type, id ID) string {
return path.Join(r.dir(t), id.String()) return path.Join(r.dir(t), id.String())
} }
// PutFile saves a file's content to the repository and returns the ID. // PutFile saves a file's content to the repository and returns the ID.
func (r *DirRepository) PutFile(path string) (ID, error) { func (r *Repository) PutFile(path string) (ID, error) {
f, err := os.Open(path) f, err := os.Open(path)
defer f.Close() defer f.Close()
if err != nil { if err != nil {
@ -178,7 +178,7 @@ func (r *DirRepository) PutFile(path string) (ID, error) {
} }
// PutRaw saves a []byte's content to the repository and returns the ID. // PutRaw saves a []byte's content to the repository and returns the ID.
func (r *DirRepository) PutRaw(t Type, buf []byte) (ID, error) { func (r *Repository) PutRaw(t Type, buf []byte) (ID, error) {
// save contents to tempfile, hash while writing // save contents to tempfile, hash while writing
file, err := r.tempFile() file, err := r.tempFile()
if err != nil { if err != nil {
@ -206,7 +206,7 @@ func (r *DirRepository) PutRaw(t Type, buf []byte) (ID, error) {
} }
// Test returns true if the given ID exists in the repository. // Test returns true if the given ID exists in the repository.
func (r *DirRepository) Test(t Type, id ID) (bool, error) { func (r *Repository) Test(t Type, id ID) (bool, error) {
// try to open file // try to open file
file, err := os.Open(r.filename(t, id)) file, err := os.Open(r.filename(t, id))
defer func() { defer func() {
@ -224,7 +224,7 @@ func (r *DirRepository) Test(t Type, id ID) (bool, error) {
} }
// Get returns a reader for the content stored under the given ID. // Get returns a reader for the content stored under the given ID.
func (r *DirRepository) Get(t Type, id ID) (io.Reader, error) { func (r *Repository) Get(t Type, id ID) (io.Reader, error) {
// try to open file // try to open file
file, err := os.Open(r.filename(t, id)) file, err := os.Open(r.filename(t, id))
if err != nil { if err != nil {
@ -235,14 +235,14 @@ func (r *DirRepository) Get(t Type, id ID) (io.Reader, error) {
} }
// Remove removes the content stored at ID. // Remove removes the content stored at ID.
func (r *DirRepository) Remove(t Type, id ID) error { func (r *Repository) Remove(t Type, id ID) error {
return os.Remove(r.filename(t, id)) return os.Remove(r.filename(t, id))
} }
type IDs []ID type IDs []ID
// Lists all objects of a given type. // Lists all objects of a given type.
func (r *DirRepository) ListIDs(t Type) (IDs, error) { func (r *Repository) ListIDs(t Type) (IDs, error) {
// TODO: use os.Open() and d.Readdirnames() instead of Glob() // TODO: use os.Open() and d.Readdirnames() instead of Glob()
pattern := path.Join(r.dir(t), "*") pattern := path.Join(r.dir(t), "*")

View File

@ -25,13 +25,13 @@ var TestStrings = []struct {
{"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", khepri.TYPE_BLOB, "foo/../../baz"}, {"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", khepri.TYPE_BLOB, "foo/../../baz"},
} }
func setupRepo() (*khepri.DirRepository, error) { func setupRepo() (*khepri.Repository, error) {
tempdir, err := ioutil.TempDir("", "khepri-test-") tempdir, err := ioutil.TempDir("", "khepri-test-")
if err != nil { if err != nil {
return nil, err return nil, err
} }
repo, err := khepri.NewDirRepository(tempdir) repo, err := khepri.NewRepository(tempdir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -39,7 +39,7 @@ func setupRepo() (*khepri.DirRepository, error) {
return repo, nil return repo, nil
} }
func teardownRepo(repo *khepri.DirRepository) error { func teardownRepo(repo *khepri.Repository) error {
if !*testCleanup { if !*testCleanup {
fmt.Fprintf(os.Stderr, "leaving repository at %s\n", repo.Path()) fmt.Fprintf(os.Stderr, "leaving repository at %s\n", repo.Path())
return nil return nil

71
snapshot.go Normal file
View File

@ -0,0 +1,71 @@
package khepri
import (
"encoding/json"
"os"
"os/user"
"time"
)
type Snapshot struct {
Time time.Time `json:"time"`
Tree ID `json:"tree"`
Dir string `json:"dir"`
Hostname string `json:"hostname,omitempty"`
Username string `json:"username,omitempty"`
UID string `json:"uid,omitempty"`
GID string `json:"gid,omitempty"`
id ID
repo *Repository
}
func (repo *Repository) NewSnapshot(dir string) *Snapshot {
sn := &Snapshot{
Dir: dir,
repo: repo,
Time: time.Now(),
}
hn, err := os.Hostname()
if err == nil {
sn.Hostname = hn
}
usr, err := user.Current()
if err == nil {
sn.Username = usr.Username
sn.UID = usr.Uid
sn.GID = usr.Gid
}
return sn
}
func (sn *Snapshot) Save() error {
if sn.Tree == nil {
panic("Snapshot.Save() called with nil tree id")
}
obj, err := sn.repo.NewObject(TYPE_REF)
if err != nil {
return err
}
enc := json.NewEncoder(obj)
err = enc.Encode(sn)
if err != nil {
return err
}
err = obj.Close()
if err != nil {
return err
}
sn.id = obj.ID()
return nil
}
func (sn *Snapshot) ID() ID {
return sn.id
}

26
snapshot_test.go Normal file
View File

@ -0,0 +1,26 @@
package khepri_test
import (
"testing"
"time"
"github.com/fd0/khepri"
)
func TestSnapshot(t *testing.T) {
repo, err := setupRepo()
ok(t, err)
defer func() {
err = teardownRepo(repo)
ok(t, err)
}()
sn := repo.NewSnapshot("/home/foobar")
sn.Tree, err = khepri.ParseID("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")
ok(t, err)
sn.Time, err = time.Parse(time.RFC3339Nano, "2014-08-03T17:49:05.378595539+02:00")
ok(t, err)
ok(t, sn.Save())
}