-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdups.go
65 lines (53 loc) · 1.03 KB
/
dups.go
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
package main
import (
"fmt"
"github.com/github.com/vblz/FilesTree/store"
"os"
"strings"
)
type dupsCommand struct {
commonOptions
}
func (d *dupsCommand) Execute(args []string) error {
if len(args) > 0 {
flagParser.WriteHelp(os.Stdout)
os.Exit(1)
}
files, err := store.Read(d.DatabasePath)
if err != nil {
return fmt.Errorf("database open error: %w", err)
}
res := findDuplicates(files)
if len(res) == 0 {
fmt.Printf("no duplicates find")
return nil
}
for _, dups := range res {
for _, filePath := range dups {
fmt.Println(filePath)
}
fmt.Println(strings.Repeat("-", 32))
}
return nil
}
func findDuplicates(files map[string]os.FileInfo) [][]string {
sizes := make(map[int64][]string)
for k, v := range files {
size := v.Size()
if !v.IsDir() && size != 0 {
l, ok := sizes[size]
if !ok {
l = make([]string, 0, 1)
}
l = append(l, k)
sizes[size] = l
}
}
result := make([][]string, 0)
for _, v := range sizes {
if len(v) > 1 {
result = append(result, v)
}
}
return result
}