Added verbose mode

This commit is contained in:
Stephen McQuay 2017-10-18 17:58:11 -07:00
parent 98d5dd7033
commit 9017579805
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
3 changed files with 14 additions and 8 deletions

View File

@ -31,7 +31,7 @@ type checksum struct {
}
// check is the entry point for -c operation.
func check(args []string) chan error {
func check(args []string, verbose bool) chan error {
jobs := make(chan checksum)
go func() {
@ -55,7 +55,7 @@ func check(args []string) chan error {
results := []<-chan error{}
for w := 0; w < *ngo; w++ {
results = append(results, verify(jobs))
results = append(results, verify(jobs, verbose))
}
return merge(results)
@ -105,7 +105,7 @@ func parseCS(line string) checksum {
}
// verify does grunt work of verifying a stream of jobs (filenames).
func verify(jobs chan checksum) chan error {
func verify(jobs chan checksum, verbose bool) chan error {
r := make(chan error)
go func() {
for job := range jobs {
@ -125,6 +125,8 @@ func verify(jobs chan checksum) chan error {
f.Close()
if fmt.Sprintf("%x", job.hash.Sum(nil)) != job.checksum {
r <- fmt.Errorf("%s: bad", job.filename)
} else if verbose {
fmt.Fprintf(os.Stderr, "ok: %v\n", job.filename)
}
}
close(r)

View File

@ -32,7 +32,7 @@ func (r results) Less(i, j int) bool { return r[i].f < r[j].f }
type hashr func() hash.Hash
// hsh figures out which hash algo to use, and distributes the work of hashing
func hsh(files []string) chan result {
func hsh(files []string, verbose bool) chan result {
var h hashr
switch *algo {
case "sha1", "1":
@ -77,7 +77,7 @@ func hsh(files []string) chan result {
res := []<-chan result{}
for w := 0; w < *ngo; w++ {
res = append(res, compute(h, jobs))
res = append(res, compute(h, jobs, verbose))
}
o := make(chan result)
@ -96,7 +96,7 @@ func hsh(files []string) chan result {
}
// compute is the checksumming workhorse
func compute(h hashr, jobs chan checksum) chan result {
func compute(h hashr, jobs chan checksum, verbose bool) chan result {
hsh := h()
r := make(chan result)
go func() {
@ -113,6 +113,9 @@ func compute(h hashr, jobs chan checksum) chan result {
r <- result{err: err}
continue
}
if verbose {
fmt.Fprintf(os.Stderr, "%v\n", job.filename)
}
r <- result{f: job.filename, cs: fmt.Sprintf("%x", hsh.Sum(nil))}
}
close(r)

View File

@ -10,6 +10,7 @@ import (
var algo = flag.String("a", "sha256", "algorithm to use")
var mode = flag.Bool("c", false, "check")
var ngo = flag.Int("n", runtime.NumCPU(), "number of goroutines")
var verbose = flag.Bool("v", false, "vebose")
func main() {
flag.Parse()
@ -17,7 +18,7 @@ func main() {
switch *mode {
case true:
ec := 0
for err := range check(files) {
for err := range check(files, *verbose) {
ec++
fmt.Fprintf(os.Stderr, "%v\n", err)
}
@ -26,7 +27,7 @@ func main() {
}
case false:
ec := 0
for res := range hsh(files) {
for res := range hsh(files, *verbose) {
if res.err != nil {
ec++
fmt.Fprintf(os.Stderr, "%v\n", res.err)