From 9aa2e1143c55cfa6a0b8651067b34c61da8d5646 Mon Sep 17 00:00:00 2001 From: Sameer Ajmani Date: Wed, 12 Mar 2014 23:43:55 -0400 Subject: go.blog: pipelines and cancellation. R=adg, r, rsc, ken, bcmills CC=adonovan, campoy, david.crawshaw, gmlewis, golang-codereviews https://golang.org/cl/71070047 --- content/pipelines/serial.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 content/pipelines/serial.go (limited to 'content/pipelines/serial.go') diff --git a/content/pipelines/serial.go b/content/pipelines/serial.go new file mode 100644 index 0000000..7a44ca1 --- /dev/null +++ b/content/pipelines/serial.go @@ -0,0 +1,53 @@ +package main + +import ( + "crypto/md5" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "sort" +) + +// MD5All reads all the files in the file tree rooted at root and returns a map +// from file path to the MD5 sum of the file's contents. If the directory walk +// fails or any read operation fails, MD5All returns an error. +func MD5All(root string) (map[string][md5.Size]byte, error) { + m := make(map[string][md5.Size]byte) + err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { // HL + if err != nil { + return err + } + if info.IsDir() { + return nil + } + data, err := ioutil.ReadFile(path) // HL + if err != nil { + return err + } + m[path] = md5.Sum(data) // HL + return nil + }) + if err != nil { + return nil, err + } + return m, nil +} + +func main() { + // Calculate the MD5 sum of all files under the specified directory, + // then print the results sorted by path name. + m, err := MD5All(os.Args[1]) // HL + if err != nil { + fmt.Println(err) + return + } + var paths []string + for path := range m { + paths = append(paths, path) + } + sort.Strings(paths) // HL + for _, path := range paths { + fmt.Printf("%x %s\n", m[path], path) + } +} -- cgit v1.2.3