-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
213 lines (197 loc) · 4.93 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2020 Tobias Klausmann
// License: Apache 2.0, see LICENSE for details
package main
import (
"flag"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"time"
"github.com/dustin/go-humanize"
"github.com/schollz/progressbar/v3"
"golang.org/x/crypto/blake2b"
)
var (
dryrun = flag.Bool("dryrun", false, "Do not do anything, just print what would be done")
jobs = flag.Int("jobs", runtime.NumCPU(), "Number of parallel jobs to use when checksumming")
pathlist []string
)
func main() {
flag.Parse()
logger := logSetup(os.Stderr, slog.LevelInfo, "20060102-15:04:05.000", true)
var root string
args := flag.Args()
if len(args) == 0 {
root = "."
} else {
root = args[0]
}
os.Exit(doD2hl(root, logger))
}
func doD2hl(root string, logger *slog.Logger) int {
ti := newTI()
ti.l = logger
start := time.Now()
err := filepath.Walk(root, ti.process)
if err != nil {
logger.Error("Walking tree failed", "error", err)
return -1
}
ela := time.Since(start)
logger.Info("Files enumerated", "total", ti.FileCount, "tocheck", len(pathlist),
"time", ela, "per_sec", float64(ti.FileCount)/ela.Seconds())
ti.pb = progressbar.Default(int64(len(pathlist)), "Checksum")
start = time.Now()
c := make(chan string)
var wg sync.WaitGroup
for i := 0; i < *jobs; i++ {
go ti.checksum(i, c, &wg)
wg.Add(1)
}
for _, path := range pathlist {
c <- path
}
close(c)
wg.Wait()
ela = time.Since(start)
logger.Info("Files checksummed", "total", len(pathlist), "time", ela,
"per_sec", float64(len(pathlist))/ela.Seconds())
start = time.Now()
s := dedupe(&ti)
ela = time.Since(start)
logger.Info("Deduplication complete", "freedspace", humanize.Bytes(uint64(s)),
"dedupes", ti.DupeCount, "time", ela, "per_sec", float64(ti.DupeCount)/ela.Seconds())
return 0
}
type treeinfo struct {
RWLock *sync.RWMutex
Sums map[string][]string
Inodes map[uint64]bool
DupeCount int
FileCount int
pb *progressbar.ProgressBar
l *slog.Logger
}
func newTI() treeinfo {
var ti treeinfo
var newmtx sync.RWMutex
ti.Sums = make(map[string][]string)
ti.Inodes = make(map[uint64]bool)
ti.RWLock = &newmtx
return ti
}
func (ti *treeinfo) process(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
if strings.HasSuffix(path, ".tmpdedupe") {
ti.l.Error("Leftover file from previous run, please investigate", "path", path)
os.Exit(-1)
}
ti.FileCount++
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
ti.l.Error("We somehow got a file without an inode number", "path", path)
os.Exit(-1)
}
if ok = ti.Inodes[stat.Ino]; ok {
ti.l.Debug("We have lready seen this i-node", "inodenum", stat.Ino)
return nil
}
ti.Inodes[stat.Ino] = true
pathlist = append(pathlist, path)
return nil
}
func (ti treeinfo) String() string {
r := make([]string, 0, len(ti.Sums))
for sum, paths := range ti.Sums {
r = append(r, fmt.Sprintf("%s: %s", sum, strings.Join(paths, " ")))
}
return strings.Join(r, "\n")
}
func (ti *treeinfo) checksum(id int, p chan string, wg *sync.WaitGroup) {
wlog := ti.l.With("workerid", id)
wlog.Debug("Worker starting")
defer wg.Done()
for path := range p {
f, err := os.Open(path)
if err != nil {
continue
}
h, err := blake2b.New256(nil)
if err != nil {
wlog.Error("Could not create new hash: %w", err)
panic("Exiting")
}
if _, err := io.Copy(h, f); err != nil {
f.Close()
continue
}
f.Close()
s := fmt.Sprintf("%x", h.Sum(nil))
wlog.Debug("Checksum", "path", path, "sum", s)
ti.RWLock.Lock()
ti.Sums[s] = append(ti.Sums[s], path)
ti.RWLock.Unlock()
err = ti.pb.Add(1)
if err != nil {
panic(err)
}
}
wlog.Debug("Worker exiting")
}
func dedupe(ti *treeinfo) int64 {
var savings int64
ti.pb = progressbar.Default(int64(len(pathlist)), "Cmp/Link")
for _, names := range ti.Sums {
err := ti.pb.Add(1)
if err != nil {
panic(err)
}
if len(names) <= 1 {
continue
}
first := names[0]
fi, err := os.Stat(first)
if err != nil {
ti.l.Error("Could not stat destination file for dedupe", "path", first, "error", err)
os.Exit(-1)
}
size := fi.Size()
for _, name := range names[1:] {
if *dryrun {
ti.l.Warn("Would deduplicate", "src", name, "dest", first)
} else {
ti.l.Info("Deduping", "src", name, "dest", first)
tmpname := fmt.Sprintf("%s.tmpdedupe", name)
err := os.Rename(name, tmpname)
if err != nil {
ti.l.Error("Could not rename source file for dedupe", "path", name, "tmpname", tmpname, "error", err)
os.Exit(-1)
}
err = os.Link(first, name)
if err != nil {
ti.l.Error("Could not link src file to dest file", "src", name, "dest", first, "error", err)
os.Exit(-1)
}
err = os.Remove(tmpname)
if err != nil {
ti.l.Error("Could not delete temp file", "tmpname", tmpname, "error", err)
os.Exit(-1)
}
}
savings += size
ti.DupeCount++
}
}
return savings
}