aboutsummaryrefslogtreecommitdiff
path: root/repo.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-09 16:21:20 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-09 16:21:20 +0200
commit3e866db2accc6ed5aa65befdf21474dbc7b1ec18 (patch)
tree62670e092cbc41817edc76317a0ac2482b9480af /repo.go
parent11404879f2e7ee02118852b6b42e19f99cb2edd9 (diff)
downloaddna-backup-3e866db2accc6ed5aa65befdf21474dbc7b1ec18.tar.gz
dna-backup-3e866db2accc6ed5aa65befdf21474dbc7b1ec18.zip
first add of restore function to Repo
Diffstat (limited to 'repo.go')
-rw-r--r--repo.go42
1 files changed, 37 insertions, 5 deletions
diff --git a/repo.go b/repo.go
index 467567d..585e505 100644
--- a/repo.go
+++ b/repo.go
@@ -101,13 +101,13 @@ func (r *Repo) Patcher() Patcher {
func (r *Repo) Commit(source string) {
versions := r.loadVersions()
- newVersion := len(versions)
+ newVersion := len(versions) // TODO: add newVersion functino
newPath := path.Join(r.path, fmt.Sprintf(versionFmt, newVersion))
newChunkPath := path.Join(newPath, chunksName)
newFilesPath := path.Join(newPath, filesName)
newRecipePath := path.Join(newPath, recipeName)
- os.Mkdir(newPath, 0775)
- os.Mkdir(newChunkPath, 0775)
+ os.Mkdir(newPath, 0775) // TODO: handle errors
+ os.Mkdir(newChunkPath, 0775) // TODO: handle errors
reader, writer := io.Pipe()
oldChunks := make(chan IdentifiedChunk, 16)
files := listFiles(source)
@@ -120,6 +120,28 @@ func (r *Repo) Commit(source string) {
fmt.Println(files)
}
+func (r *Repo) Restore(destination string) {
+ versions := r.loadVersions()
+ latest := versions[len(versions)-1]
+ latestFilesPath := path.Join(latest, filesName)
+ latestRecipePath := path.Join(latest, recipeName)
+ files := loadFileList(latestFilesPath)
+ recipe := loadRecipe(latestRecipePath)
+ reader, writer := io.Pipe()
+ go r.restoreStream(writer, recipe)
+ bufReader := bufio.NewReaderSize(reader, r.chunkSize*2)
+ for _, file := range files {
+ filePath := path.Join(destination, file.Path)
+ dir := filepath.Dir(filePath)
+ os.MkdirAll(dir, 0775) // TODO: handle errors
+ f, _ := os.Create(filePath) // TODO: handle errors
+ n, err := io.CopyN(f, bufReader, file.Size)
+ if err != nil {
+ log.Printf("Error storing file content for '%s', written %d/%d bytes: %s\n", filePath, n, file.Size, err)
+ }
+ }
+}
+
func (r *Repo) loadVersions() []string {
var versions []string
files, err := os.ReadDir(r.path)
@@ -468,8 +490,19 @@ func (r *Repo) matchStream(stream io.Reader, version int) []Chunk {
return chunks
}
+func (r *Repo) restoreStream(stream io.WriteCloser, recipe []Chunk) {
+ for _, c := range recipe {
+ if rc, isRepo := c.(RepoChunk); isRepo {
+ rc.SetRepo(r)
+ }
+ if n, err := io.Copy(stream, c.Reader()); err != nil {
+ log.Printf("Error copying to stream, read %d bytes from chunk: %s\n", n, err)
+ }
+ }
+ stream.Close()
+}
+
func storeRecipe(dest string, recipe []Chunk) {
- gob.Register(&LoadedChunk{})
gob.Register(&StoredChunk{})
gob.Register(&TempChunk{})
gob.Register(&DeltaChunk{})
@@ -492,7 +525,6 @@ func storeRecipe(dest string, recipe []Chunk) {
func loadRecipe(path string) []Chunk {
var recipe []Chunk
- gob.Register(&LoadedChunk{})
gob.Register(&StoredChunk{})
gob.Register(&TempChunk{})
gob.Register(&DeltaChunk{})