/
check.go
98 lines (79 loc) · 2.04 KB
/
check.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"time"
base58 "github.com/jbenet/go-base58"
attrs "github.com/mildred/doc/attrs"
repo "github.com/mildred/doc/repo"
)
const checkUsage string = `doc check [OPTIONS...] [DIR]
Scan DIR or the current directory and check for non modified files their content
compared to the stored checksum. If -a is specified, modified files are also
shown.
Symbols show the file status:
+ Modified file (mtime changed since lash hash)
= Modified mtime (identical content but mtime updated)
! Corrupt file (mtime identical but content changed)
Options:
`
func mainCheck(args []string) int {
f := flag.NewFlagSet("status", flag.ExitOnError)
opt_all := f.Bool("a", false, "Check all files, including modified")
f.Usage = func() {
fmt.Print(checkUsage)
f.PrintDefaults()
}
f.Parse(args)
dir := f.Arg(0)
if dir == "" {
dir = "."
}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip .dirstore/ at root
if filepath.Base(path) == attrs.DirStoreName && filepath.Dir(path) == dir && info.IsDir() {
return filepath.SkipDir
} else if info.IsDir() {
return nil
}
hashTimeStr, err := attrs.Get(path, repo.XattrHashTime)
if err != nil {
return nil
}
hashTime, err := time.Parse(time.RFC3339Nano, string(hashTimeStr))
if err != nil {
return err
}
timeEqual := hashTime == info.ModTime()
if *opt_all || timeEqual {
hash, err := attrs.Get(path, repo.XattrHash)
if err != nil {
return err
}
digest, err := repo.HashFile(path, info)
if err != nil {
return err
}
hashEqual := bytes.Equal(hash, digest)
if !timeEqual && !hashEqual {
fmt.Printf("+\t%s\t%s\n", base58.Encode(digest), path)
} else if !hashEqual {
fmt.Printf("!\t%s\t%s\n", base58.Encode(digest), path)
} else if !timeEqual {
fmt.Printf("=\t%s\t%s", base58.Encode(digest), path)
}
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
return 1
}
return 0
}