| 991 | } |
| 992 | |
| 993 | func HashFile(path string, paths ...string) (string, error) { |
| 994 | md5Hash := md5.New() |
| 995 | |
| 996 | allPaths := append(paths, path) |
| 997 | for _, path := range allPaths { |
| 998 | f, err := Open(path) |
| 999 | if err != nil { |
| 1000 | return "", err |
| 1001 | } |
| 1002 | |
| 1003 | // Skip directories |
| 1004 | fileInfo, err := f.Stat() |
| 1005 | if err != nil { |
| 1006 | f.Close() |
| 1007 | return "", errors.WithStack(err) |
| 1008 | } |
| 1009 | if fileInfo.IsDir() { |
| 1010 | f.Close() |
| 1011 | continue |
| 1012 | } |
| 1013 | |
| 1014 | if _, err := io.Copy(md5Hash, f); err != nil { |
| 1015 | f.Close() |
| 1016 | return "", errors.Wrap(err, path) |
| 1017 | } |
| 1018 | |
| 1019 | io.WriteString(md5Hash, path) |
| 1020 | f.Close() |
| 1021 | } |
| 1022 | |
| 1023 | return hex.EncodeToString(md5Hash.Sum(nil)), nil |
| 1024 | } |
| 1025 | |
| 1026 | func HashDirectory(dir string, ignoreFns ...IgnoreFn) (string, error) { |
| 1027 | md5Hash := md5.New() |