-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrive.go
50 lines (42 loc) · 1.17 KB
/
drive.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
// Package suredrive implements higher-level wrappers around the various
// gosure libraries, making it easier both for a command-line tool, as
// well as other tools to perform the integrity scans.
package gosure // import "davidb.org/x/gosure"
import (
"log"
"time"
"davidb.org/x/gosure/status"
"davidb.org/x/gosure/store"
"davidb.org/x/gosure/sure"
)
// Scan performs a scan or an update.
func Scan(st *store.Store, dir string, mgr *status.Manager) error {
oldTree, err := st.ReadDat()
if err != nil {
log.Printf("no prior scan, doing initial scan\n")
}
meter := mgr.Meter(250 * time.Millisecond)
newTree, err := sure.ScanFs(dir, meter)
meter.Close()
if err != nil {
return err
}
if oldTree != nil {
sure.MigrateHashes(oldTree, newTree)
}
HashUpdate(newTree, dir, mgr)
err = st.Write(newTree)
if err != nil {
return err
}
return nil
}
// HashUpdate updates the hashes of any files that are needed.
func HashUpdate(tree *sure.Tree, dir string, mgr *status.Manager) {
est := tree.EstimateHashes()
meter := mgr.Meter(250 * time.Millisecond)
prog := sure.NewProgress(est.Files, est.Bytes, meter)
prog.Flush()
tree.ComputeHashes(&prog, dir)
meter.Close()
}