aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-08-22 19:43:53 +0200
committern-peugnet <n.peugnet@free.fr>2021-08-22 19:43:53 +0200
commitf63d18f08e86b4135e466d5e88e03dda65466e6b (patch)
treec3455ed461a33f1d92038ea9da0b904998cb23b7
parent4df6871c2642dded074db47a39c75722896f8f1e (diff)
downloaddna-backup-f63d18f08e86b4135e466d5e88e03dda65466e6b.tar.gz
dna-backup-f63d18f08e86b4135e466d5e88e03dda65466e6b.zip
add store and load files list
-rw-r--r--repo.go43
-rw-r--r--repo_test.go24
2 files changed, 61 insertions, 6 deletions
diff --git a/repo.go b/repo.go
index 9cde6fa..3a91407 100644
--- a/repo.go
+++ b/repo.go
@@ -11,13 +11,13 @@ repo/
│ │ ├── 000000000000001
│ │ ├── 000000000000002
│ │ ├── 000000000000003
-│ ├── dentries
+│ ├── files
│ └── recipe
└── 00001/
├── chunks/
│ ├── 000000000000000
│ ├── 000000000000001
- ├── dentries
+ ├── files
└── recipe
```
*/
@@ -25,6 +25,7 @@ repo/
package main
import (
+ "encoding/gob"
"fmt"
"io"
"io/fs"
@@ -48,6 +49,7 @@ func Commit(source string, repo string) {
new := latest + 1
newPath := path.Join(repo, fmt.Sprintf("%05d", new))
newChunkPath := path.Join(newPath, "chunks")
+ newFilesPath := path.Join(newPath, "files")
os.Mkdir(newPath, 0775)
os.Mkdir(newChunkPath, 0775)
newChunks := make(chan []byte, 16)
@@ -56,6 +58,7 @@ func Commit(source string, repo string) {
go LoadChunks(repo, oldChunks)
go ReadFiles(files, newChunks)
StoreChunks(newChunkPath, newChunks)
+ StoreFiles(newFilesPath, files)
fmt.Println(files)
}
@@ -131,6 +134,22 @@ func ReadFiles(files []File, chunks chan<- []byte) {
close(chunks)
}
+func StoreFiles(dest string, files []File) {
+ err := writeFile(dest, files)
+ if err != nil {
+ log.Println(err)
+ }
+}
+
+func LoadFiles(repo string) []File {
+ files := make([]File, 0)
+ err := readFile(repo, &files)
+ if err != nil {
+ log.Println(err)
+ }
+ return files
+}
+
func PrintChunks(chunks <-chan []byte) {
for c := range chunks {
fmt.Println(c)
@@ -165,3 +184,23 @@ func LoadChunks(repo string, chunks chan<- []byte) {
}
close(chunks)
}
+
+func writeFile(filePath string, object interface{}) error {
+ file, err := os.Create(filePath)
+ if err == nil {
+ encoder := gob.NewEncoder(file)
+ encoder.Encode(object)
+ }
+ file.Close()
+ return err
+}
+
+func readFile(filePath string, object interface{}) error {
+ file, err := os.Open(filePath)
+ if err == nil {
+ decoder := gob.NewDecoder(file)
+ err = decoder.Decode(object)
+ }
+ file.Close()
+ return err
+}
diff --git a/repo_test.go b/repo_test.go
index 7c6316e..3be4835 100644
--- a/repo_test.go
+++ b/repo_test.go
@@ -40,8 +40,8 @@ func chunkCompare(t *testing.T, dataDir string, testFiles []string, chunkCount i
}
if bytes.Compare(c, content) != 0 {
t.Errorf("Chunk %d does not match file content", i)
- t.Log(c)
- t.Log(content)
+ t.Log("Expected: ", c)
+ t.Log("Result:", content)
}
i++
}
@@ -94,9 +94,25 @@ func TestLoadChunks(t *testing.T) {
c3 := <-chunks3
if bytes.Compare(c2, c3) != 0 {
t.Errorf("Chunk %d does not match file content", i)
- t.Log(c2)
- t.Log(c3)
+ t.Log("Expected: ", c2)
+ t.Log("Result:", c3)
}
i++
}
}
+
+func TestStoreLoadFiles(t *testing.T) {
+ prepareResult()
+ dataDir := path.Join("test", "data")
+ resultFiles := path.Join("test", "result", "files")
+ files1 := ListFiles(dataDir)
+ StoreFiles(resultFiles, files1)
+ files2 := LoadFiles(resultFiles)
+ for i, f := range files1 {
+ if f != files2[i] {
+ t.Errorf("Loaded file data %d does not match stored one", i)
+ t.Log("Expected: ", f)
+ t.Log("Result: ", files2[i])
+ }
+ }
+}