Skip to content

Instantly share code, notes, and snippets.

@mengzhuo
Created March 22, 2018 11:26
Show Gist options
  • Save mengzhuo/c517ef4dab49dededec912eab05301cc to your computer and use it in GitHub Desktop.
Save mengzhuo/c517ef4dab49dededec912eab05301cc to your computer and use it in GitHub Desktop.
Simple parallel md5sum compatible to GNU md5sum
package main
// go build md5sum.go
// ./md5sum ~/*.log
// # or
// ./md5sum << EOF
import (
"crypto/md5"
"fmt"
"io"
"os"
"sync"
)
func main() {
tarList := make([]*os.File, notLessThan(len(os.Args)-1, 1))
if len(os.Args) == 1 {
tarList[0] = os.Stdin
} else {
for i, fN := range os.Args[1:] {
fi, err := os.Lstat(fN)
if err != nil {
fmt.Printf("md5sum: %s\n", err)
continue
}
if fi.Mode().IsDir() {
fmt.Printf("md5sum: %s is a directory\n", fN)
continue
}
f, err := os.Open(fN)
if err != nil {
fmt.Printf("md5sum: %s\n", err)
continue
}
}
tarList[i] = f
}
}
wg := &sync.WaitGroup{}
for _, f := range tarList {
if f == nil {
continue
}
wg.Add(1)
go doSum(f, wg)
}
wg.Wait()
}
func doSum(f *os.File, wg *sync.WaitGroup) {
defer wg.Done()
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
fmt.Printf("md5sum:%s\n", err)
return
}
ret := h.Sum(nil)
fmt.Printf("%x %s\n", ret, f.Name())
return
}
func notLessThan(a, b int) int {
if a < b {
return b
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment