-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
209 lines (173 loc) · 4.4 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"sync/atomic"
"time"
"github.com/fatih/color"
"golang.org/x/sync/errgroup"
"gopkg.in/cheggaaa/pb.v1"
database "github.com/teran/checksum/database/legacy"
)
var (
appVersion = "No version specified(probably trunk build)"
buildTimestamp = "0000-00-00T00:00:00Z"
db *database.Database
filePattern *regexp.Regexp
cntAdded uint64
cntDeleted uint64
cntFailed uint64
cntMissed uint64
cntPassed uint64
)
func main() {
cfg := newConfig()
if cfg.Complete == true {
completeArgs(flag.Arg(1))
return
}
if cfg.Progressbar == true {
cfg.SkipOk = true
}
var err error
db, err = database.NewDatabase(cfg.DbPath)
if err != nil {
log.Fatalf("error opening database: %s", err)
}
filePattern, err = regexp.Compile(cfg.Pattern)
if err != nil {
log.Fatalf("Error compiling pattern: %s", err)
}
if !cfg.GenerateChecksumOnly {
var bar *pb.ProgressBar
if cfg.Progressbar {
bar = pb.New(db.Count())
bar.ShowCounters = true
bar.SetRefreshRate(time.Second)
bar.ShowPercent = true
bar.ShowBar = true
bar.ShowTimeLeft = true
bar.ShowSpeed = true
bar.Start()
}
objects := db.MapObjects()
keys := make([]string, 0, len(objects))
for k := range objects {
keys = append(keys, k)
}
sort.Strings(keys)
wg := &errgroup.Group{}
wg.SetLimit(cfg.Concurrency)
for _, key := range keys {
wg.Go(func(file string, obj *database.DataObject) func() error {
return func() error {
if cfg.Progressbar {
defer func() {
bar.Increment()
}()
}
if _, err := os.Stat(file); os.IsNotExist(err) {
if !cfg.SkipMissed {
fmt.Printf("%s %s\n", color.RedString("[MISS]"), file)
}
if cfg.DeleteMissed {
fmt.Printf("%s DeleteMissed requested: deleting file `%s` from database\n", color.BlueString("[NOTE]"), file)
db.DeleteOne(file)
atomic.AddUint64(&cntDeleted, 1)
}
atomic.AddUint64(&cntMissed, 1)
return nil
}
isChanged := false
if obj.Length == 0 {
obj.Length = flength(file)
isChanged = true
}
if obj.SHA1 == "" || obj.SHA256 == "" {
sha1, sha256, err := generateActualChecksum(file)
if err != nil {
return err
}
obj.SHA1 = sha1
obj.SHA256 = sha256
isChanged = true
}
res := verify(file, obj.Length, obj.SHA1, obj.SHA256)
if isChanged {
db.WriteOne(file, &database.DataObject{
Length: obj.Length,
SHA1: obj.SHA1,
SHA256: obj.SHA256,
Modified: time.Now().UTC(),
})
}
if res {
if !cfg.SkipOk {
fmt.Printf("%s %s\n", color.GreenString("[ OK ]"), file)
}
atomic.AddUint64(&cntPassed, 1)
return nil
}
if !cfg.SkipFailed {
fmt.Printf("%s %s\n", color.RedString("[FAIL]"), file)
}
atomic.AddUint64(&cntFailed, 1)
return nil
}
}(key, objects[key]))
}
err = wg.Wait()
if err != nil {
log.Fatalf("error handling threads")
}
if cfg.Progressbar {
bar.Finish()
}
fmt.Printf("%s Verification step complete\n", color.CyanString("[INFO]"))
}
if cfg.DataDir != "" {
fmt.Printf("%s Checking for new files on %s\n", color.CyanString("[INFO]"), cfg.DataDir)
// TODO: check data dir for existence
err = filepath.Walk(cfg.DataDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if isApplicable(path) {
sha1, sha256, err := generateActualChecksum(path)
if err != nil {
return err
}
db.WriteOne(path, &database.DataObject{
Length: flength(path),
SHA1: sha1,
SHA256: sha256,
Modified: time.Now().UTC(),
})
fmt.Printf("%s %s\n", color.YellowString("[CALCULATED]"), path)
atomic.AddUint64(&cntAdded, 1)
}
return nil
})
if err != nil {
panic(fmt.Sprintf("Error walking through files: %s", err))
}
} else {
fmt.Printf("%s data directory is not specified. Skipping new files check\n", color.CyanString("[INFO]"))
}
err = db.Commit()
if err != nil {
panic(fmt.Sprintf("Error commiting the data: %s", err))
}
fmt.Printf("------------\n")
fmt.Printf("Job is done:\n")
fmt.Printf(" Added: %d\n", cntAdded)
fmt.Printf(" Deleted: %d\n", cntDeleted)
fmt.Printf(" Missed: %d\n", cntMissed)
fmt.Printf(" Failed: %d\n", cntFailed)
fmt.Printf(" Passed: %d\n", cntPassed)
}