Directory creates the hash value of a directory
(path string)
| 43 | |
| 44 | // Directory creates the hash value of a directory |
| 45 | func Directory(path string) (string, error) { |
| 46 | hash := sha256.New() |
| 47 | |
| 48 | // Stat dir / file |
| 49 | fileInfo, err := os.Stat(path) |
| 50 | if err != nil { |
| 51 | return "", err |
| 52 | } |
| 53 | |
| 54 | // Hash file |
| 55 | if !fileInfo.IsDir() { |
| 56 | size := strconv.FormatInt(fileInfo.Size(), 10) |
| 57 | mTime := strconv.FormatInt(fileInfo.ModTime().UnixNano(), 10) |
| 58 | _, _ = io.WriteString(hash, path+";"+size+";"+mTime) |
| 59 | |
| 60 | return fmt.Sprintf("%x", hash.Sum(nil)), nil |
| 61 | } |
| 62 | |
| 63 | // Hash directory |
| 64 | err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error { |
| 65 | if err != nil { |
| 66 | // We ignore errors |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | size := strconv.FormatInt(info.Size(), 10) |
| 71 | mTime := strconv.FormatInt(info.ModTime().UnixNano(), 10) |
| 72 | _, _ = io.WriteString(hash, path+";"+size+";"+mTime) |
| 73 | |
| 74 | return nil |
| 75 | }) |
| 76 | if err != nil { |
| 77 | return "", err |
| 78 | } |
| 79 | |
| 80 | return fmt.Sprintf("%x", hash.Sum(nil)), nil |
| 81 | } |
| 82 | |
| 83 | // DirectoryExcludes calculates a hash for a directory and excludes the submitted patterns |
| 84 | func DirectoryExcludes(srcPath string, excludePatterns []string, fast bool) (string, error) { |