refactoring SHA directory, goroutines pool

This commit is contained in:
Meutel 2017-07-08 16:36:23 +02:00
parent 8cd3a4517f
commit db3d0426ee

View File

@ -8,63 +8,55 @@ import "os"
import "path/filepath" import "path/filepath"
import "sync" import "sync"
var chan_hash chan string func sha256File(path string) (string, error) {
var wg sync.WaitGroup
func walk(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatalln(err)
return err
}
if info.IsDir() {
return nil
} else {
wg.Add(1)
go hash(info.Name(), path)
}
return nil
}
func hash(name string, path string) {
h, err := sha256File(path)
if err != nil {
log.Fatalln(err)
}
chan_hash <- fmt.Sprintf("SHA256 (%s) = %x", name, h)
}
func sha256File(path string) ([]byte, error) {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
log.Fatalln(err) return "", err
return nil, err
} }
defer f.Close() defer f.Close()
h := sha256.New() h := sha256.New()
io.Copy(h, f) io.Copy(h, f)
return h.Sum(nil), nil return fmt.Sprintf("SHA256 (%s) = %x", path, 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)
go printer() var wg sync.WaitGroup
if err := filepath.Walk(os.Args[1], walk); err != nil { chan_hash, chan_path := make(chan string), make(chan string)
panic(err)
for i := 0; i < 100; i++ {
go func() {
for {
path := <-chan_path
if h, err := sha256File(path); err != nil {
wg.Done()
log.Print("Error "+path, err)
} else {
chan_hash <- h
}
}
}()
}
go func() {
for {
fmt.Println(<-chan_hash)
wg.Done()
}
}()
err := filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error {
if err != nil && !info.IsDir() {
wg.Add(1)
chan_path <- path
}
return err
})
if err != nil {
log.Fatalln(err)
} }
wg.Wait() wg.Wait()
} }