-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha1walk.go
169 lines (144 loc) · 3.94 KB
/
sha1walk.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
/**
* This file is part of drs.
*
* drs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* drs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rmlint. If not, see <http://www.gnu.org/licenses/>.
*
** Copyright 2016 the drs Authors:
*
* - Daniel <SeeSpotRun> T. 2016-2016 (https://github.com/SeeSpotRun)
*
** Hosted on https://github.com/SeeSpotRun/go-disk-utils
*
**/
// sha1walk is a demo main for the drs package.
package main
/*
* TODO:
* [ ] write sha1_test
* [ ] benchmarking & profiling
*/
import (
"crypto/sha1"
"fmt"
"github.com/SeeSpotRun/drs/drs"
"github.com/docopt/docopt-go"
"log"
"os"
"sort"
)
//////////////////////////////////////////////////////////////////
// sortRes defines whether to sort results by alphabetical order of path
var sortRes bool
// job implements the drs.Job interface
type job struct {
path string
offset uint64
hash string
}
// Go hashes the file contents and adds the job to results[]
func (j *job) Go(f *drs.File, err error) {
if err != nil {
log.Println(err)
return
}
h := sha1.New()
_, err = drs.Copy(h, f)
// Note: Copy closes f TODO: rename to CopyClose?
if err != nil {
log.Printf("Failed hashing %s: %s", j.path, err)
} else {
j.hash = fmt.Sprintf("%x", h.Sum(nil))
if sortRes {
res = append(res, j)
} else {
fmt.Printf("%s: %s\n", j.hash, j.path)
}
}
}
type results []*job
var res results
// implements sort.Interface for sorting by increasing path.
func (r results) Len() int { return len(r) }
func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r results) Less(i, j int) bool { return r[i].path < r[j].path }
func main() {
// parse args:
usage := `Usage:
sha1walk -h | --help
sha1walk [options] [--ssd=path]... <paths>...
Options:
-h --help Show this screen
--version Show version
-r --recurse Recurse paths if they are folders
--walkfirst Finish walk before starting hashing
--hashfirst Hash files as soon as they are found
--aggressive Use more aggressive HDD scheduler settings
--ssd=path Identify path as belonging to an SSD
--sort Sort results before printing
`
args, _ := docopt.Parse(usage, os.Args[1:], true, "sums 0.1", false, true)
fmt.Println(args)
// sort results before printing?
sortRes := args["--sort"] == true
// use more aggressive scheduler for HDD's ?
if args["--aggressive"] == true {
drs.HDD = drs.Aggressive
}
// do hashing ASAP, during walking or after walking?
hashPriority := drs.Normal
if args["--hashfirst"] == true {
hashPriority = drs.High
} else if args["--walkfirst"] == true {
hashPriority = drs.Low
}
// configure walk options
walkopts := &drs.WalkOptions{
Priority: drs.Normal,
Errs: make(chan error),
NoRecurse: args["--recurse"] != true,
}
// error reporting during walk:
go func() {
for err := range walkopts.Errs {
log.Printf("Walk error: %s\n", err)
}
}()
// register user-identified ssd's:
if ssds, ok := args["--ssd"].([]string); ok {
err := drs.RegisterSSDs(ssds)
if err != nil {
fmt.Println(err)
}
}
// walk paths:
paths, ok := args["<paths>"].([]string)
if !ok {
fmt.Println("Error: no paths to hash")
return
}
for p := range drs.Walk(paths, walkopts) {
j := &job{path: p.Name, offset: p.Offset}
// shedule for hashing
p.Disk.Schedule(j, j.path, j.offset, hashPriority)
}
// wait for all jobs to finish
drs.WaitDisks()
// print results
if sortRes {
sort.Sort(results(res))
for _, j := range res {
fmt.Printf("%s: %s\n", j.hash, j.path)
}
}
}