github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/blog/content/pipelines/serial.go (about) 1 // +build OMIT 2 3 package main 4 5 import ( 6 "crypto/md5" 7 "fmt" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "sort" 12 ) 13 14 // MD5All reads all the files in the file tree rooted at root and returns a map 15 // from file path to the MD5 sum of the file's contents. If the directory walk 16 // fails or any read operation fails, MD5All returns an error. 17 func MD5All(root string) (map[string][md5.Size]byte, error) { 18 m := make(map[string][md5.Size]byte) 19 err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { // HL 20 if err != nil { 21 return err 22 } 23 if !info.Mode().IsRegular() { 24 return nil 25 } 26 data, err := ioutil.ReadFile(path) // HL 27 if err != nil { 28 return err 29 } 30 m[path] = md5.Sum(data) // HL 31 return nil 32 }) 33 if err != nil { 34 return nil, err 35 } 36 return m, nil 37 } 38 39 func main() { 40 // Calculate the MD5 sum of all files under the specified directory, 41 // then print the results sorted by path name. 42 m, err := MD5All(os.Args[1]) // HL 43 if err != nil { 44 fmt.Println(err) 45 return 46 } 47 var paths []string 48 for path := range m { 49 paths = append(paths, path) 50 } 51 sort.Strings(paths) // HL 52 for _, path := range paths { 53 fmt.Printf("%x %s\n", m[path], path) 54 } 55 }