/
data_manifest.go
535 lines (423 loc) · 11.1 KB
/
data_manifest.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package data
import (
"bytes"
"fmt"
"github.com/gonuts/flag"
"github.com/jbenet/commander"
"os"
"path/filepath"
"strings"
)
const ManifestFileName = ".data/Manifest"
const noHash = "<to be hashed>"
var cmd_data_manifest = &commander.Command{
UsageLine: "manifest [[ add | remove | hash | check ] <path>]",
Short: "Generate and manipulate dataset manifest.",
Long: `data manifest - Generate and manipulate dataset manifest.
Generates and manipulates this dataset's manifest. The manifest
is a mapping of { <path>: <checksum>}, and describes all files
that compose a dataset. This mapping is generated by adding and
hashing (checksum) files.
Running data-manifest without arguments will generate (or patch)
the manifest. Note that already hashed files will not be re-hashed
unless forced to. Some files may be massive, and hashing every run
would be prohibitively expensive.
Commands:
add <file> Adds <file> to manifest (does not hash).
rm <file> Removes <file> from manifest.
hash <file> Hashes <file> and adds checksum to manifest.
check <file> Verifies <file> checksum matches manifest.
(use the --all flag to do it to all available files)
Loosely, data-manifest's process is:
- List all files in the working directory.
- Add files to the manifest (effectively tracking them).
- Hash tracked files, adding checksums to the manifest.
`,
Run: manifestCmd,
Subcommands: []*commander.Command{
cmd_data_manifest_add,
cmd_data_manifest_rm,
cmd_data_manifest_hash,
cmd_data_manifest_check,
},
}
var cmd_data_manifest_add = &commander.Command{
UsageLine: "add <file>",
Short: "Adds <file> to manifest (does not hash).",
Long: `data manifest add - Adds <file> to manifest (does not hash).
Adding files to the manifest ensures they are tracked. This command
adds the given <file> to the manifest, saves it, and exits. It does
not automatically hash the file (run 'data manifest hash').
See 'data manifest'.
Arguments:
<file> path of the file to add.
`,
Run: manifestAddCmd,
Flag: *flag.NewFlagSet("data-manifest-add", flag.ExitOnError),
}
var cmd_data_manifest_rm = &commander.Command{
UsageLine: "rm <file>",
Short: "Removes <file> from manifest.",
Long: `data manifest rm - Removes <file> from manifest.
Removing files from the manifest stops tracking them. This command
removes the given <file> (and hash) from the manifest, and exits.
See 'data manifest'.
Arguments:
<file> path of the file to remove.
`,
Run: manifestRmCmd,
Flag: *flag.NewFlagSet("data-manifest-rm", flag.ExitOnError),
}
var cmd_data_manifest_hash = &commander.Command{
UsageLine: "hash <file>",
Short: "Hashes <file> and adds checksum to manifest.",
Long: `data manifest hash - Hashes <file> and adds checksum to manifest.
Hashing files in the manifest calculates the file checksums. This command
hashes the given <file>, adds it to the manifest, and exits.
See 'data manifest'.
Arguments:
<file> path of the file to hash.
`,
Run: manifestHashCmd,
Flag: *flag.NewFlagSet("data-manifest-hash", flag.ExitOnError),
}
var cmd_data_manifest_check = &commander.Command{
UsageLine: "check <file>",
Short: "Verifies <file> checksum matches manifest.",
Long: `data manifest check - Verifies <file> checksum matches manifest.
The manifest lists the files and their checksums. This command
hashes the given <file>, and prints whether its checksum matches the
stored checksum.
See 'data manifest'.
Arguments:
<file> path of the file to check.
`,
Run: manifestCheckCmd,
Flag: *flag.NewFlagSet("data-manifest-check", flag.ExitOnError),
}
func init() {
cmd_data_manifest_add.Flag.Bool("all", false, "add all available files")
cmd_data_manifest_rm.Flag.Bool("all", false, "remove all tracked files")
cmd_data_manifest_hash.Flag.Bool("all", false, "hash all tracked files")
cmd_data_manifest_check.Flag.Bool("all", false, "check all tracked files")
}
func manifestCmd(c *commander.Command, args []string) error {
mf := NewDefaultManifest()
return mf.Generate()
}
func manifestCmdPaths(c *commander.Command, args []string) ([]string, error) {
mf := NewDefaultManifest()
paths := args
// Use all files available if --all is passed in.
all := c.Flag.Lookup("all").Value.Get().(bool)
if all {
paths = []string{}
for path, _ := range mf.Files {
paths = append(paths, path)
}
}
if len(paths) < 1 {
return nil, fmt.Errorf("%v: no files specified.", c.FullName())
}
return paths, nil
}
func manifestAddCmd(c *commander.Command, args []string) error {
mf := NewDefaultManifest()
paths := args
// Use all files available if --all is passed in.
all := c.Flag.Lookup("all").Value.Get().(bool)
if all {
paths = listAllFiles(".")
}
if len(paths) < 1 {
return fmt.Errorf("%v: no files specified.", c.FullName())
}
// add files to manifest file
for _, f := range paths {
err := mf.Add(f)
if err != nil {
return err
}
}
return nil
}
func manifestRmCmd(c *commander.Command, args []string) error {
mf := NewDefaultManifest()
paths, err := manifestCmdPaths(c, args)
if err != nil {
return err
}
// remove files from manifest file
for _, f := range paths {
err := mf.Remove(f)
if err != nil {
return err
}
}
return nil
}
func manifestHashCmd(c *commander.Command, args []string) error {
mf := NewDefaultManifest()
paths, err := manifestCmdPaths(c, args)
if err != nil {
return err
}
// hash files in manifest file
for _, f := range paths {
err := mf.Hash(f)
if err != nil {
return err
}
}
return nil
}
func manifestCheckCmd(c *commander.Command, args []string) error {
mf := NewDefaultManifest()
paths, err := manifestCmdPaths(c, args)
if err != nil {
return err
}
// hash files in manifest file
failed := 0
for _, f := range paths {
pass, err := mf.Check(f)
if err != nil {
// return err
}
if !pass {
failed++
}
}
if failed > 0 {
return fmt.Errorf("data manifest check: %d/%d checksums failed.",
failed, len(paths))
}
return nil
}
type Manifest struct {
SerializedFile "-"
Files blobPaths ""
}
func NewManifest(path string) *Manifest {
mf := &Manifest{SerializedFile: SerializedFile{Path: path}}
// initialize map
mf.Files = blobPaths{}
mf.SerializedFile.Format = mf.Files
// attempt to load
if len(path) > 0 {
mf.ReadFile()
}
return mf
}
func NewDefaultManifest() *Manifest {
return NewManifest(ManifestFileName)
}
func NewManifestWithRef(ref string) (*Manifest, error) {
f := NewManifest("")
err := f.ReadBlob(ref)
if err != nil {
return nil, err
}
return f, nil
}
func (mf *Manifest) Generate() error {
pErr("Generating Manifest file...\n")
// add new files to manifest file
// (for now add everything. `data manifest {add,rm}` in future)
for _, f := range listAllFiles(".") {
err := mf.Add(f)
if err != nil {
return err
}
}
// warn about manifest-listed files missing from directory
// (basically, missing things. User removes individually, or `rm --missing`)
// Once all files are listed, hash all the files, storing the hashes.
for f, h := range mf.Files {
if IsHash(h) && h != noHash {
continue
}
err := mf.Hash(f)
if err != nil {
return err
}
}
if len(mf.Files) == 0 {
err := mf.WriteFile()
if err != nil {
return nil
}
pErr("Warning: no files in directory. Manifest is empty.\n")
} else {
pErr("%d files in Manifest.\n", len(mf.Files))
}
return nil
}
func (mf *Manifest) Clear() error {
for f, _ := range mf.Files {
delete(mf.Files, f)
}
return mf.WriteFile()
}
func (mf *Manifest) Add(path string) error {
// check, dont override (could have hash value)
_, exists := (mf.Files)[path]
if exists {
return nil
}
(mf.Files)[path] = noHash
// Write out file (store incrementally)
err := mf.WriteFile()
if err != nil {
return err
}
pErr("data manifest: added %s\n", path)
return nil
}
func (mf *Manifest) Remove(path string) error {
// check, dont remove nonexistent path
_, exists := (mf.Files)[path]
if !exists {
return nil
}
delete(mf.Files, path)
// Write out file (store incrementally)
err := mf.WriteFile()
if err != nil {
return err
}
pErr("data manifest: removed %s\n", path)
return nil
}
func (mf *Manifest) Hash(path string) error {
h, err := hashFile(path)
if err != nil {
return err
}
(mf.Files)[path] = h
// Write out file (store incrementally)
err = mf.WriteFile()
if err != nil {
return err
}
pErr("data manifest: hashed %.7s %s\n", h, path)
return nil
}
func (mf *Manifest) Check(path string) (bool, error) {
oldHash, found := (mf.Files)[path]
if !found {
return false, fmt.Errorf("data manifest: file not in manifest %s", path)
}
mfmt := "data manifest: check %.7s %s %s"
newHash, err := hashFile(path)
if err != nil {
switch err.(type) {
case *os.PathError:
// non existent files count as not hashing correctly.
pErr(mfmt, oldHash, path, "FAIL - not found\n")
return false, nil
default:
return false, err
}
}
if newHash != oldHash {
pErr(mfmt, oldHash, path, "FAIL\n")
return false, nil
}
dOut(mfmt, oldHash, path, "PASS\n")
return true, nil
}
func (mf *Manifest) PathsForHash(hash string) []string {
l := []string{}
for path, h := range mf.Files {
if h == hash {
l = append(l, path)
}
}
return l
}
func (mf *Manifest) HashForPath(path string) string {
hash, exists := (mf.Files)[path]
if exists {
return hash
}
return ""
}
func (mf *Manifest) HashForPathCaseInsensitive(path string) string {
path = strings.ToLower(path)
for opath, h := range mf.Files {
opath = strings.ToLower(opath)
if opath == path {
return h
}
}
return ""
}
func (mf *Manifest) AllPaths() []string {
l := []string{}
for p, _ := range mf.Files {
l = append(l, p)
}
return l
}
func (mf *Manifest) AllHashes() []string {
l := []string{}
for _, h := range mf.Files {
l = append(l, h)
}
return l
}
func (mf *Manifest) Complete() bool {
// must have at least one file (Datafile)
if len(mf.Files) < 1 {
return false
}
// all hashes must be computed
for _, h := range mf.Files {
if !IsHash(h) || h == noHash {
return false
}
}
return true
}
func listAllFiles(path string) []string {
files := []string{}
walkFn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
// entirely skip hidden dirs
if len(info.Name()) > 1 && strings.HasPrefix(info.Name(), ".") {
dOut("data manifest: skipping %s/\n", info.Name())
return filepath.SkipDir
}
// skip datasets/
if path == DatasetDir {
dOut("data manifest: skipping %s/\n", info.Name())
return filepath.SkipDir
}
// dont store dirs
return nil
}
// skip manifest file
if path == ManifestFileName {
dOut("data manifest: skipping %s\n", info.Name())
return nil
}
// skip hidden files
if strings.HasPrefix(info.Name(), ".") {
dOut("data manifest: skipping %s\n", info.Name())
return nil
}
files = append(files, path)
return nil
}
filepath.Walk(path, walkFn)
return files
}
func (mf *Manifest) ManifestHash() (string, error) {
buf, err := mf.Marshal()
if err != nil {
return "", err
}
r := bytes.NewReader(buf)
return readerHash(r)
}