Skip to content

Instantly share code, notes, and snippets.

@bheeshmar
Last active August 29, 2015 14:19
Show Gist options
  • Save bheeshmar/94e0d75f6aa48022750e to your computer and use it in GitHub Desktop.
Save bheeshmar/94e0d75f6aa48022750e to your computer and use it in GitHub Desktop.
Print filetree of current directory with md5sums of files
package main
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func md5Sum(fullpath string) []uint8 {
hasher := md5.New()
file, err := os.Open(fullpath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
if _, err := io.Copy(hasher, file); err != nil {
// It's a directory
return []uint8{0}
}
return hasher.Sum(nil)
}
func printDir(fullpath string, entry os.FileInfo, indent int) {
fullpath = filepath.Join(fullpath, entry.Name())
digest := md5Sum(fullpath)
fmt.Printf("%s %x %s\n", strings.Repeat("-", indent), digest, entry.Name())
if entry.IsDir() {
printDirs(fullpath, indent+1)
}
}
func printDirs(fullpath string, indent int) {
dirs, _ := ioutil.ReadDir(fullpath)
for _, entry := range dirs {
printDir(fullpath, entry, indent)
}
}
func main() {
printDirs(".", 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment