Fix backup command

This commit is contained in:
Alexander Neumann 2014-08-04 21:53:25 +02:00
parent 3ac797a8fa
commit e525655dcb

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"fmt" "fmt"
@ -24,6 +23,30 @@ func hash(filename string) (khepri.ID, error) {
return h.Sum([]byte{}), nil return h.Sum([]byte{}), nil
} }
func store_file(repo *khepri.Repository, path string) (khepri.ID, error) {
obj, err := repo.NewObject(khepri.TYPE_BLOB)
if err != nil {
return nil, err
}
file, err := os.Open(path)
defer func() {
file.Close()
}()
_, err = io.Copy(obj, file)
if err != nil {
return nil, err
}
err = obj.Close()
if err != nil {
return nil, err
}
return obj.ID(), nil
}
func archive_dir(repo *khepri.Repository, 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)
@ -54,7 +77,7 @@ func archive_dir(repo *khepri.Repository, path string) (khepri.ID, error) {
if e.IsDir() { if e.IsDir() {
id, err = archive_dir(repo, filepath.Join(path, e.Name())) id, err = archive_dir(repo, filepath.Join(path, e.Name()))
} else { } else {
id, err = repo.PutFile(filepath.Join(path, e.Name())) id, err = store_file(repo, filepath.Join(path, e.Name()))
} }
node.Content = id node.Content = id
@ -69,14 +92,21 @@ func archive_dir(repo *khepri.Repository, path string) (khepri.ID, error) {
log.Printf(" dir %q: %v entries", path, len(t.Nodes)) log.Printf(" dir %q: %v entries", path, len(t.Nodes))
var buf bytes.Buffer obj, err := repo.NewObject(khepri.TYPE_BLOB)
t.Save(&buf)
id, err := repo.PutRaw(khepri.TYPE_BLOB, buf.Bytes())
if err != nil {
log.Printf("error creating object for tree: %v", err)
return nil, err
}
err = t.Save(obj)
if err != nil { if err != nil {
log.Printf("error saving tree to repo: %v", err) log.Printf("error saving tree to repo: %v", err)
} }
obj.Close()
id := obj.ID()
log.Printf("tree for %q saved at %s", path, id) log.Printf("tree for %q saved at %s", path, id)
return id, nil return id, nil