1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
Manage a deduplicated versionned backups repository.
Sample repository:
```
repo/
├── 00000/
│ ├── chunks/
│ │ ├── 000000000000000
│ │ ├── 000000000000001
│ │ ├── 000000000000002
│ │ ├── 000000000000003
│ ├── files
│ └── recipe
└── 00001/
├── chunks/
│ ├── 000000000000000
│ ├── 000000000000001
├── files
└── recipe
```
*/
package main
import (
"encoding/gob"
"fmt"
"hash"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strconv"
"github.com/chmduquesne/rollinghash/rabinkarp64"
)
var chunkSize = 8 << 10
type File struct {
Path string
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")
// newFilesPath := path.Join(newPath, "files")
os.Mkdir(newPath, 0775)
os.Mkdir(newChunkPath, 0775)
newChunks := make(chan []byte, 16)
oldChunks := make(chan []byte, 16)
files := ListFiles(source)
go LoadChunks(repo, oldChunks)
go ReadFiles(files, newChunks)
hashes := HashChunks(oldChunks)
MatchChunks(newChunks, hashes)
// StoreChunks(newChunkPath, newChunks)
// StoreFiles(newFilesPath, files)
fmt.Println(files)
}
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) []File {
var files []File
err := filepath.Walk(path,
func(p string, i fs.FileInfo, err error) error {
if err != nil {
log.Println(err)
return err
}
if i.IsDir() {
return nil
}
files = append(files, File{p, i.Size()})
return nil
})
if err != nil {
log.Println(err)
}
return files
}
func ReadFiles(files []File, chunks chan<- []byte) {
var buff []byte
var prev, read = chunkSize, 0
for _, f := range files {
file, err := os.Open(f.Path)
if err != nil {
log.Println(err)
continue
}
for err != io.EOF {
if prev == chunkSize {
buff = make([]byte, chunkSize)
prev, err = file.Read(buff)
} else {
read, err = file.Read(buff[prev:])
prev += read
}
if err != nil && err != io.EOF {
log.Println(err)
}
if prev == chunkSize {
chunks <- buff
}
}
}
chunks <- buff
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)
}
}
func StoreChunks(dest string, chunks <-chan []byte) {
i := 0
for c := range chunks {
path := path.Join(dest, fmt.Sprintf("%015d", i))
os.WriteFile(path, c, 0664)
i++
}
}
func LoadChunks(repo string, chunks chan<- []byte) {
err := filepath.WalkDir(repo,
func(p string, e fs.DirEntry, err error) error {
if err != nil {
log.Println(err)
return err
}
if e.IsDir() {
return nil
}
buff, err := os.ReadFile(p)
chunks <- buff
return nil
})
if err != nil {
log.Println(err)
}
close(chunks)
}
func HashChunks(chunks <-chan []byte) map[uint64]uint64 {
hashes := make(map[uint64]uint64)
hasher := hash.Hash64(rabinkarp64.New())
var i uint64 = 0
for c := range chunks {
hasher.Reset()
hasher.Write(c)
h := hasher.Sum64()
hashes[h] = i
i++
}
return hashes
}
func MatchChunks(chunks <-chan []byte, hashes map[uint64]uint64) {
hasher := rabinkarp64.New()
hasher.Write(<-chunks)
var i uint64 = 0
for c := range chunks {
for offset, b := range c {
h := hasher.Sum64()
chunk, exists := hashes[h]
if exists {
fmt.Printf("Found existing chunk. New{id:%d, offset:%d}, Old: %d\n", i, offset, chunk)
}
// Roll the incoming byte in rolling
hasher.Roll(b)
}
i++
}
}
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
}
|