From 9a1aa7fe443438fdfdab89d77aa07c51b633644c Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Fri, 20 Aug 2021 18:18:36 +0200 Subject: initial repo tree --- repo.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'repo.go') 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 { -- cgit v1.2.3