-
Notifications
You must be signed in to change notification settings - Fork 0
/
summer.go
282 lines (232 loc) · 6.21 KB
/
summer.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
package main
import (
"flag"
"fmt"
"hash/crc32"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"runtime"
"golang.org/x/term"
)
const usage = `# summer 🌞 🏖
Utility to detect accidental data corruption (e.g. bitrot, storage media
problems). Not intended to detect malicious modification.
Checksums are written to/read from each file's extended attributes.
Paths given can be files or directories. If a directory is given, it is
processed recursively.
Usage:
summer [flags] update <paths>
Verify checksums in the given paths, and update them for new or changed
files.
summer [flags] verify <paths>
Verify checksums in the given paths.
summer [flags] generate <paths>
Write checksums for the given paths. Files with pre-existing checksums
are left untouched, and checksums are not verified.
Useful when generating checksums for a lot of files for the first time,
as is faster to resume work if interrupted.
summer [flags] version
Print software version information.
Flags:
`
// Flags.
var (
oneFilesystem = flag.Bool("x", false, "don't cross filesystem boundaries")
forceTTY = flag.Bool("forcetty", false, "force TTY output")
exclude = &RepeatedStringFlag{}
excludeRe = &RepeatedStringFlag{}
parallel = flag.Int("parallel", 0,
"number of files to process in parallel (0 = number of CPUs)")
)
var options = struct {
// Database to use.
db DB
// Do not cross filesystem boundaries.
oneFilesystem bool
// Whether output is a TTY.
isTTY bool
// Paths to exclude.
exclude map[string]bool
// Regexp patterns to exclude.
excludeRe []*regexp.Regexp
// How many files to process in parallel.
parallel int
}{}
func Usage() {
fmt.Fprintf(flag.CommandLine.Output(), usage)
flag.PrintDefaults()
}
func main() {
var err error
flag.Var(exclude, "exclude",
"exclude these paths (can be repeated)")
flag.Var(excludeRe, "excludere",
"exclude paths matching this regexp (can be repeated)")
flag.Usage = Usage
flag.Parse()
options.oneFilesystem = *oneFilesystem
options.isTTY = *forceTTY || term.IsTerminal(int(os.Stdout.Fd()))
options.exclude = map[string]bool{}
for _, s := range *exclude {
options.exclude[filepath.Clean(s)] = true
}
for _, s := range *excludeRe {
options.excludeRe = append(options.excludeRe, regexp.MustCompile(s))
}
options.parallel = *parallel
if options.parallel == 0 {
options.parallel = runtime.NumCPU()
}
op := flag.Arg(0)
roots := []string{}
if flag.NArg() > 1 {
roots = flag.Args()[1:]
}
if op != "version" && len(roots) == 0 {
Usage()
os.Exit(1)
}
options.db = XattrDB{}
defer options.db.Close()
switch op {
case "generate":
err = walk(roots, generate)
case "verify":
err = walk(roots, verify)
case "update":
err = walk(roots, update)
case "version":
PrintVersion()
default:
Fatalf("unknown command %q", op)
}
if err != nil {
Fatalf("%v", err)
}
}
func isExcluded(path string) bool {
if options.exclude[path] {
return true
}
for _, re := range options.excludeRe {
if re.MatchString(path) {
return true
}
}
return false
}
var crc32c = crc32.MakeTable(crc32.Castagnoli)
type ChecksumV1 struct {
// CRC32C of the file contents.
CRC32C uint32
// Modification time of the file when the checksum was computed.
// In Unix microseconds.
//
// Because we do not lock the file, it could be modified as we read it,
// and then depending on the order of operations, the checksum could be
// wrong and cause a false positive.
// To avoid this, we always read the modification time prior to reading
// the file contents. That way, if the file changes while we are reading
// it, it should be detected later as modified instead of corrupted.
//
// This relies on mtime having enough resolution to detect the change. On
// some filesystems that may not be the case, and a file modified very
// quickly may not be detected as modified. This is a limitation of the
// filesystem, and there is nothing we can do about it.
ModTimeUsec int64
}
func generate(fd *os.File, info fs.FileInfo, p *Progress) error {
hasAttr, err := options.db.Has(fd)
if err != nil {
return err
}
if hasAttr {
// Skip files that already have a checksum.
return nil
}
h := crc32.New(crc32c)
_, err = io.Copy(h, fd)
if err != nil {
return err
}
csum := ChecksumV1{
CRC32C: h.Sum32(),
ModTimeUsec: info.ModTime().UnixMicro(),
}
err = options.db.Write(fd, csum)
if err != nil {
return err
}
p.PrintNew(fd.Name(), csum)
return nil
}
func verify(fd *os.File, info fs.FileInfo, p *Progress) error {
hasAttr, err := options.db.Has(fd)
if err != nil {
return err
}
if !hasAttr {
p.PrintMissing(fd.Name(), nil)
return nil
}
csumFromFile, err := options.db.Read(fd)
if err != nil {
return err
}
h := crc32.New(crc32c)
_, err = io.Copy(h, fd)
if err != nil {
return err
}
csumComputed := ChecksumV1{
CRC32C: h.Sum32(),
ModTimeUsec: info.ModTime().UnixMicro(),
}
if csumFromFile.ModTimeUsec != csumComputed.ModTimeUsec {
p.PrintModified(fd.Name(), csumFromFile, csumComputed)
} else if csumFromFile.CRC32C != csumComputed.CRC32C {
p.PrintCorrupted(fd.Name(), csumFromFile, csumComputed)
} else {
p.PrintMatched(fd.Name(), csumComputed)
}
return nil
}
func update(fd *os.File, info fs.FileInfo, p *Progress) error {
// Compute checksum from the current state.
h := crc32.New(crc32c)
_, err := io.Copy(h, fd)
if err != nil {
return err
}
csumComputed := ChecksumV1{
CRC32C: h.Sum32(),
ModTimeUsec: info.ModTime().UnixMicro(),
}
// Read the saved checksum (if any).
hasAttr, err := options.db.Has(fd)
if err != nil {
return err
}
if !hasAttr {
// Attribute is missing. Expected for newly created files.
p.PrintMissing(fd.Name(), &csumComputed)
return options.db.Write(fd, csumComputed)
}
csumFromFile, err := options.db.Read(fd)
if err != nil {
return err
}
if csumFromFile.ModTimeUsec != csumComputed.ModTimeUsec {
// File modified. Expected for updated files.
p.PrintModified(fd.Name(), csumFromFile, csumComputed)
return options.db.Write(fd, csumComputed)
} else if csumFromFile.CRC32C != csumComputed.CRC32C {
p.PrintCorrupted(fd.Name(), csumFromFile, csumComputed)
} else {
p.PrintMatched(fd.Name(), csumComputed)
}
return nil
}