-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicates.go
More file actions
90 lines (85 loc) · 2.32 KB
/
Copy pathduplicates.go
File metadata and controls
90 lines (85 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"sort"
)
type DuplicateResult struct {
Directory string `json:"directory"`
Groups [][]File `json:"groups"`
ReclaimableBytes int64 `json:"reclaimable_bytes"`
Skipped []string `json:"skipped,omitempty"`
}
func findDuplicates(directory string, format outputFormat) error {
result, err := duplicateResult(directory)
if err != nil {
return err
}
if format == jsonFormat {
return writeJSON(result)
}
if len(result.Groups) == 0 {
fmt.Println("No exact duplicate files found.")
return nil
}
fmt.Println("Duplicate files:")
for i, files := range result.Groups {
fmt.Printf("\nGroup %d - %.2f MB each\n", i+1, float64(files[0].Size)/1024/1024)
for _, file := range files {
fmt.Printf(" %s\n", file.Name)
}
}
fmt.Printf("\nFound %d duplicate groups. Reclaimable space: %.2f MB\n", len(result.Groups), float64(result.ReclaimableBytes)/1024/1024)
return nil
}
func duplicateResult(directory string) (DuplicateResult, error) {
entries, err := os.ReadDir(directory)
if err != nil {
return DuplicateResult{}, err
}
byHash := map[string][]File{}
result := DuplicateResult{Directory: directory}
for _, entry := range entries {
if entry.IsDir() {
continue
}
path := filepath.Join(directory, entry.Name())
info, err := entry.Info()
if err != nil {
result.Skipped = append(result.Skipped, entry.Name())
continue
}
hash, err := hashFile(path)
if err != nil {
result.Skipped = append(result.Skipped, entry.Name())
continue
}
byHash[hash] = append(byHash[hash], File{Name: entry.Name(), Size: info.Size()})
}
for _, files := range byHash {
if len(files) < 2 {
continue
}
sort.Slice(files, func(i, j int) bool { return files[i].Name < files[j].Name })
result.Groups = append(result.Groups, files)
result.ReclaimableBytes += files[0].Size * int64(len(files)-1)
}
sort.Slice(result.Groups, func(i, j int) bool { return result.Groups[i][0].Name < result.Groups[j][0].Name })
sort.Strings(result.Skipped)
return result, nil
}
func hashFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}