SHA directory wait group

This commit is contained in:
Meutel 2017-07-08 15:32:40 +02:00
parent 865b0a1c88
commit 01518f6f9f

View File

@ -6,9 +6,10 @@ import "io"
import "log" import "log"
import "os" import "os"
import "path/filepath" import "path/filepath"
import "time" import "sync"
var chan_hash chan string var chan_hash chan string
var wg sync.WaitGroup
func walk(path string, info os.FileInfo, err error) error { func walk(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@ -19,6 +20,7 @@ func walk(path string, info os.FileInfo, err error) error {
if info.IsDir() { if info.IsDir() {
return nil return nil
} else { } else {
wg.Add(1)
go hash(info.Name(), path) go hash(info.Name(), path)
} }
return nil return nil
@ -44,21 +46,25 @@ func sha256File(path string) ([]byte, error) {
return h.Sum(nil), nil return h.Sum(nil), nil
} }
func printer() {
for {
select {
case str := <-chan_hash:
fmt.Println(str)
wg.Done()
}
}
}
func main() { func main() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
log.Fatalln("Usage shadir file") log.Fatalln("Usage shadir file")
} }
chan_hash = make(chan string) chan_hash = make(chan string)
go printer()
if err := filepath.Walk(os.Args[1], walk); err != nil { if err := filepath.Walk(os.Args[1], walk); err != nil {
panic(err) panic(err)
} }
wg.Wait()
for {
select {
case str := <-chan_hash:
fmt.Println(str)
case <-time.After(10 * time.Millisecond):
return
}
}
} }