-
Notifications
You must be signed in to change notification settings - Fork 3
/
iostore.go
438 lines (373 loc) · 9.43 KB
/
iostore.go
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package main
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"time"
roc "github.com/james-antill/rename-on-close"
)
func storeNodeType(r *MTnode) byte {
if r.IsSymlink() {
return 'l'
} else if r.IsDir() {
return 'd'
} else {
return 'f'
}
}
func storeWriteFileNode(iow io.Writer, r *MTnode) {
fn := r.Path()
fmt.Fprintf(iow, "P: %c %d %s\n", storeNodeType(r), len(fn), fn)
// Double check all the data is there...
for _, csum := range calcChecksumKinds {
r.Checksum(csum)
}
// Write out everything we have, so we get old checksums too etc.
for _, csum := range r.csums {
fmt.Fprintf(iow, "C-%s: %s\n", csum.Kind, b2s(csum.Data))
}
fmt.Fprintf(iow, "%s %d\n", "S:", r.Size())
timeFmt := ".000000000"
tm := time.Unix(0, r.mtimeNsecs)
if tm.Nanosecond() == 0 {
fmt.Fprintf(iow, "%s %d\n", "MT:", tm.Unix())
} else {
fmt.Fprintf(iow, "%s %d%s\n", "MT:", tm.Unix(), tm.Format(timeFmt))
}
}
func storeWriteFileDir(iow io.Writer, r *MTnode) {
leafOnly := false
if !leafOnly || !r.IsDir() || len(r.children) == 0 {
storeWriteFileNode(iow, r)
}
if !r.IsDir() {
return
}
for _, c := range r.Children() {
storeWriteFileDir(iow, c)
}
}
func storeWriteFile(iow io.Writer, r *MTnode) {
fmt.Fprintf(iow, "mtree-file-0.2\n")
storeWriteFileDir(iow, r)
}
func atoi(s string) (int64, error) {
i64, err := strconv.ParseInt(s, 10, 64)
return i64, err
}
func exists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
// MtreeFile loads an mtree from a file.
func MtreeFile(mfname string, progress bool) (*MTnode, error) {
file, err := os.Open(mfname)
if err != nil {
if !errors.Is(err, os.ErrNotExist) { // Check ends with .mtree?
return nil, err
}
switch {
case exists(mfname + ".gz"):
mfname += ".gz"
case exists(mfname + ".bz2"):
mfname += ".bz2"
case exists(mfname + ".xz"):
mfname += ".xz"
default:
return nil, err
}
file, err = os.Open(mfname)
}
if err != nil {
return nil, err
}
defer file.Close()
npath, err := normPath(mfname) // ????
if err != nil {
return nil, err
}
zr, err := autounzip(file, npath)
if err != nil {
return nil, err
}
defer zr.Close()
scanner := bufio.NewScanner(zr)
if !scanner.Scan() {
return nil, fmt.Errorf("Invalid mtree file: %s", mfname)
}
switch scanner.Text() {
case "mtree-file-0.1":
fallthrough
case "mtree-file-0.2":
break
default:
return nil, fmt.Errorf("Invalid mtree file: %s", mfname)
}
root := rootRes()
var hasRoot = false
pparent := ""
ppent := root
var cur *MTnode
for scanner.Scan() {
txt := scanner.Text()
switch {
case strings.HasPrefix(txt, "P: "): // Path
tsp := txt[3:]
tsps := strings.SplitN(tsp, " ", 3)
if len(tsps) != 3 {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
ftype, sizeStr, wpath := tsps[0], tsps[1], tsps[2]
var mode os.FileMode
switch ftype {
case "d":
mode = os.ModeDir
case "f": // zero case is regular file.
case "l":
mode = os.ModeSymlink
default:
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
size, err := atoi(sizeStr)
if err != nil {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
for size > int64(len(wpath)) {
if !scanner.Scan() {
return nil, fmt.Errorf("Corrupt mtree file: %s near %s",
mfname, txt)
}
wpath += "\n" + scanner.Text()
}
if wpath[0] == '/' {
hasRoot = true
} else { // Shouldn't be a mix in a single file.
wpath = "/" + wpath
}
ppent, pparent = ensureParentDir(root, wpath, pparent, ppent)
name := path.Base(wpath)
cur = newRes(ppent, name, mode)
case false && strings.HasPrefix(txt, "D: "): // Data
tsp := txt[3:]
tsps := strings.SplitN(tsp, " ", 2)
if len(tsps) != 2 {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
sizeStr, wData := tsps[0], tsps[1]
size, err := atoi(sizeStr)
if err != nil {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
for size > int64(len(wData)) {
if !scanner.Scan() {
return nil, fmt.Errorf("Corrupt mtree file: %s near %s",
mfname, txt)
}
wData += "\n" + scanner.Text()
}
// cur.data = wData
case strings.HasPrefix(txt, "MT: "): // Modified Time
modtimeLine := txt[4:]
sns := strings.SplitN(modtimeLine, ".", 2)
secs, err := atoi(sns[0])
if err != nil {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
var nsecs int64
if len(sns) == 2 {
nsecs, err = atoi(sns[1])
if err != nil {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
}
nsecs += secs * 1000000000
cur.mtimeNsecs = nsecs
case strings.HasPrefix(txt, "S: "): // Size
sizeStr := txt[3:]
size, err := atoi(sizeStr)
if err != nil {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
if cur.IsDir() {
continue // FIXME: Have the directory size re-resolve.
}
cur.size = size
case strings.HasPrefix(txt, "C-"): // Checksums
ckd := txt[2:]
ckds := strings.SplitN(ckd, ": ", 2)
if len(ckds) != 2 {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
if cur.IsDir() {
continue // FIXME: Have the directory checksums re-resolve.
}
chkKind, chkDataStr := ckds[0], ckds[1]
chkData, err := hex.DecodeString(chkDataStr)
if err != nil {
return nil, fmt.Errorf("Corrupt mtree file: %s near: %s",
mfname, txt)
}
csum := Checksum{Kind: chkKind, Data: chkData}
cur.csums = append(cur.csums, csum)
// case strings.HasPrefix(txt, "AT: "): // access time
// case strings.HasPrefix(txt, "CT: "): // ctime
// case strings.HasPrefix(txt, "MO: "): /// Mode
// case strings.HasPrefix(txt, "Num: "):
// case strings.HasPrefix(txt, "U: "): // Uid
// case strings.HasPrefix(txt, "G: "): // Gid
// case strings.HasPrefix(txt, "D: "): // Device
// case strings.HasPrefix(txt, "I: "): // Inode
// case strings.HasPrefix(txt, "L: "): // Links (number of)
// default: warn?
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
if !hasRoot {
if len(root.children) != 1 {
return nil, fmt.Errorf("Bad .mtree file: %s: No data found",
mfname)
}
root.children[0].parent = nil
root = root.children[0]
}
// Regenerate directory checksums from the files...
for _, csum := range validChecksumKinds {
root.Checksum(csum)
}
return root, nil
}
func tmBaseName(tm time.Time) string {
// In old speak: "%Y-%m-%d--%H%MZ
return tm.Format("2006-01-02--1504Z")
}
func hasSuffixMtree(name string) bool {
switch {
case strings.HasSuffix(name, ".mtree.bz2"):
fallthrough
case strings.HasSuffix(name, ".mtree.gz"):
fallthrough
case strings.HasSuffix(name, ".mtree.xz"):
fallthrough
case strings.HasSuffix(name, ".mtree"):
return true
}
return false
}
func latestSnapshot(dmt string, flagProgress bool) (string, error) {
// Find the latest snapshot file from a given .mtree dir...
mh, err := MtreeFile(dmt+"/HEAD", flagProgress)
if err != nil {
// FIXME: Error.Wrap
return "", fmt.Errorf("Can't load .mtree/HEAD from: %s (%v)", dmt, err)
}
mh, _ = mtreeChdir(mh, "local")
mh.parent = nil // Kind of hacky atm. ... for Path()
var mhl *MTnode
for _, n := range mh.Children() {
if hasSuffixMtree(n.Name()) {
mhl = n
break
}
}
if mhl == nil {
return "", fmt.Errorf("Can't load .mtree/HEAD from: %s", dmt)
}
return mhl.Name(), nil
}
func latestCache(dmt string) (string, error) {
dmtc := dmt + "/cache/"
fname, err := latestMtree(dmtc)
if err != nil {
return "", err
}
return fname, nil
}
func storeWriteDataSymlink(dmt, ndmt, ndfn string, r *MTnode) error {
if err := os.MkdirAll(dmt, 0770); err != nil {
return err
}
fo, err := roc.Create(ndmt)
if err != nil {
return err
}
defer fo.Close()
// FIXME: Race with MtreePath() and not efficient
data, err := os.Readlink(ndfn)
if err != nil { // Ignore read errors
fmt.Fprintln(os.Stderr, "data:", ndfn, err)
return nil
}
if _, err := fo.WriteString(data); err != nil {
return err
}
if err := fo.CloseRename(); err != nil {
return err
}
return nil
}
// storeWriteData writes the data for the non-regular files to the .mtree/data
// dmt: path to the .mtree/data/<foo>
// dfn: path to the FS (parent of mtree)
func storeWriteData(dmt, dfn string, r *MTnode) error {
ndmt := dmt + "/" + r.name
ndfn := dfn + "/" + r.name
if r.IsSymlink() { // Atm. just symlink data
return storeWriteDataSymlink(dmt, ndmt, ndfn, r)
}
if !r.IsDir() {
return nil
}
for _, c := range r.Children() {
if err := storeWriteData(ndmt, ndfn, c); err != nil {
return err
}
}
return nil
}
func storeWriteDotMtree(dmt, prefix string, saveData bool,
mtree *MTnode) (string, error) {
bfn := tmBaseName(time.Now().UTC())
if saveData {
sdfn := dmt + "/data/" + bfn
dtree := path.Dir(path.Dir(dmt))
if err := storeWriteData(sdfn, dtree, mtree); err != nil {
fmt.Println("JDBG:", err)
return "", err
}
}
nfn := dmt + prefix + bfn + ".mtree"
fo, err := roc.Create(nfn)
if err != nil {
fmt.Println("JDBG:", err)
return "", err
}
defer fo.Close()
iow := bufio.NewWriter(fo)
storeWriteFile(iow, mtree)
if err := iow.Flush(); err != nil {
fmt.Println("JDBG:", err)
return "", err
}
if err := fo.CloseRename(); err != nil {
fmt.Println("JDBG:", err)
return "", err
}
return bfn, nil
}