aboutsummaryrefslogtreecommitdiff
path: root/repo.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-08-20 18:18:36 +0200
committern-peugnet <n.peugnet@free.fr>2021-08-20 18:18:36 +0200
commit9a1aa7fe443438fdfdab89d77aa07c51b633644c (patch)
tree1d90ea6007fc59c3012efb17d14370c890b2805e /repo.go
parent9f98d6ef4931f04b9023264f0e3408e4529c977d (diff)
downloaddna-backup-9a1aa7fe443438fdfdab89d77aa07c51b633644c.tar.gz
dna-backup-9a1aa7fe443438fdfdab89d77aa07c51b633644c.zip
initial repo tree
Diffstat (limited to 'repo.go')
-rw-r--r--repo.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/repo.go b/repo.go
index 17ad8f3..22c323f 100644
--- a/repo.go
+++ b/repo.go
@@ -1,13 +1,39 @@
+/*
+Manage a deduplicated versionned backups repository.
+
+Sample repository:
+
+```
+repo/
+├── 00000/
+│ ├── chunks/
+│ │ ├── 000000000000000
+│ │ ├── 000000000000001
+│ │ ├── 000000000000002
+│ │ ├── 000000000000003
+│ ├── dentries
+│ └── recipe
+└── 00001/
+ ├── chunks/
+ │ ├── 000000000000000
+ │ ├── 000000000000001
+ ├── dentries
+ └── recipe
+```
+*/
+
package main
import (
"fmt"
"io"
"io/fs"
+ "io/ioutil"
"log"
"os"
"path"
"path/filepath"
+ "strconv"
)
const (
@@ -19,6 +45,44 @@ type File struct {
Size int64
}
+func Commit(source string, repo string) {
+ latest := GetLastVersion(repo)
+ new := latest + 1
+ newPath := path.Join(repo, fmt.Sprintf("%05d", new))
+ newChunkPath := path.Join(newPath, "chunks")
+ os.Mkdir(newPath, 0775)
+ os.Mkdir(newChunkPath, 0775)
+ files := make(chan File)
+ newChunks := make(chan []byte)
+ oldChunks := make(chan []byte)
+ go LoadChunks(repo, oldChunks)
+ go ListFiles(source, files)
+ go ReadFiles(files, newChunks)
+ StoreChunks(newChunkPath, newChunks)
+}
+
+func GetLastVersion(repo string) int {
+ v := -1
+ files, err := ioutil.ReadDir(repo)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ for _, f := range files {
+ if !f.IsDir() {
+ continue
+ }
+ num, err := strconv.Atoi(f.Name())
+ if err != nil {
+ log.Println(err)
+ continue
+ }
+ if num > v {
+ v = num
+ }
+ }
+ return v
+}
+
func ListFiles(path string, files chan<- File) {
err := filepath.Walk(path,
func(p string, i fs.FileInfo, err error) error {