-
Notifications
You must be signed in to change notification settings - Fork 0
/
listdir.go
102 lines (81 loc) · 1.49 KB
/
listdir.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
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
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
)
func compare(paths []string) {
var readers []*bufio.Reader
for _, p := range paths {
f, err := os.Open(p)
if err != nil {
return
}
defer f.Close()
readers = append(readers, bufio.NewReader(f))
}
eof := false
offset := 0
for done := false; !done; {
unique := make(map[byte]struct{}, 0)
for _, reader := range readers {
c, err := reader.ReadByte()
if err == io.EOF {
eof = true
} else if err != nil {
fmt.Println("err:", err)
continue
}
unique[c] = struct{}{}
}
if eof || (len(unique) != 1) {
done = true
continue
}
offset = offset + 1
}
if eof {
fmt.Println("end of file reached at:", offset)
} else {
fmt.Println("differs at:", offset)
}
}
func main() {
var dirs []string
if len(os.Args) > 1 {
dirs = os.Args[1:]
} else {
dirs = append(dirs, ".")
}
f := make(map[int64][]string)
for _, root := range dirs {
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
return nil
}
relative, _ := filepath.Rel(root, path)
f[info.Size()] = append(f[info.Size()], relative)
fmt.Println(relative)
return nil
})
fmt.Println()
for k, v := range f {
if len(v) > 1 {
fmt.Println(k)
var paths []string
for _, n := range v {
fmt.Println(n)
paths = append(paths, n)
}
fmt.Println()
compare(paths)
fmt.Println()
}
}
}
}