author | Mikael Berthe <mikael@lilotux.net> |
Sun, 14 Oct 2018 13:20:09 +0200 | |
changeset 36 | e918b7e63748 |
parent 35 | 730377b4449f |
child 37 | a7662fbfbe02 |
permissions | -rw-r--r-- |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
/* |
34
b70346ff153d
Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
2 |
* Copyright (C) 2014-2018 Mikael Berthe <mikael@lilotux.net> |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
3 |
* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
4 |
* This program is free software; you can redistribute it and/or modify |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
5 |
* it under the terms of the GNU General Public License as published by |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
6 |
* the Free Software Foundation; either version 2 of the License, or (at |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
7 |
* your option) any later version. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
8 |
* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, but |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
* General Public License for more details. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
* USA |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
*/ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
19 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
// This program (Goduf) is a fast duplicate file finder. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
// Use goduf --help to get the list of available options. |
25 | 22 |
// |
24
40074e33c579
Add installation command line example
Mikael Berthe <mikael@lilotux.net>
parents:
23
diff
changeset
|
23 |
// Installation: |
40074e33c579
Add installation command line example
Mikael Berthe <mikael@lilotux.net>
parents:
23
diff
changeset
|
24 |
// |
40074e33c579
Add installation command line example
Mikael Berthe <mikael@lilotux.net>
parents:
23
diff
changeset
|
25 |
// % go get hg.lilotux.net/golang/mikael/goduf |
35
730377b4449f
Mention github mirror in the comments
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
26 |
// or |
730377b4449f
Mention github mirror in the comments
Mikael Berthe <mikael@lilotux.net>
parents:
34
diff
changeset
|
27 |
// % go get github.com/McKael/goduf |
24
40074e33c579
Add installation command line example
Mikael Berthe <mikael@lilotux.net>
parents:
23
diff
changeset
|
28 |
|
25 | 29 |
package main |
30 |
||
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
import ( |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
"crypto/sha1" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 |
"encoding/hex" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 |
"errors" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 |
"flag" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
"fmt" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
"io" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
"os" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
"path/filepath" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
"sort" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 |
const medsumBytes = 128 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 |
const minSizePartialChecksum = 49152 // Should be > 3*medsumBytes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
type sumType int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 |
const ( |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
49 |
noChecksum sumType = iota |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
50 |
fullChecksum |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 |
partialChecksum |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 |
) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 |
|
36 | 54 |
// Results contains the results of the duplicates search |
55 |
type Results struct { |
|
56 |
Groups []ResultSet `json:"groups"` |
|
57 |
Duplicates uint `json:"duplicates"` |
|
58 |
NumberOfSets uint `json:"number_of_sets"` |
|
59 |
RedundantDataSize uint64 `json:"redundant_data_size"` |
|
60 |
RedundantDataSizeH string `json:"redundant_data_size_h"` |
|
61 |
TotalFileCount uint `json:"total_file_count"` |
|
62 |
} |
|
63 |
||
64 |
// ResultSet contains a group of identical duplicate files |
|
65 |
type ResultSet struct { |
|
66 |
Size uint64 `json:"size"` // Size of each item |
|
67 |
Paths []string `json:"paths"` // List of file paths |
|
68 |
} |
|
69 |
||
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
type fileObj struct { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
//Unique bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
FilePath string |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
os.FileInfo |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 |
PartialHash []byte |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 |
Hash []byte |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
76 |
needHash sumType |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
79 |
// FileObjList is only exported so that we can have a sort interface on inodes. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 |
type FileObjList []*fileObj |
16 | 81 |
type foListList []FileObjList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
82 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 |
type dataT struct { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 |
totalSize uint64 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 |
cmpt uint |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
86 |
sizeGroups map[int64]*FileObjList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 |
emptyFiles FileObjList |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
ignoreCount int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 |
var data dataT |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
|
20 | 93 |
// Implement my own logger |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 |
var myLog myLogT |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 |
|
20 | 96 |
// visit is called for every file and directory. |
97 |
// We check the file object is correct (regular, readable...) and add |
|
98 |
// it to the data.sizeGroups hash. |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 |
func visit(path string, f os.FileInfo, err error) error { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 |
if f == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
104 |
if f.IsDir() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
105 |
myLog.Println(-1, "Warning: cannot process directory:", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 |
path) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 |
return filepath.SkipDir |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 |
myLog.Println(-1, "Ignoring ", path, " - ", err) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 |
data.ignoreCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 |
if f.IsDir() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 |
if mode := f.Mode(); mode&os.ModeType != 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 |
if mode&os.ModeSymlink != 0 { |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
120 |
myLog.Println(6, "Ignoring symbolic link", path) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
121 |
} else { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 |
myLog.Println(0, "Ignoring special file", path) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
124 |
data.ignoreCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
125 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
128 |
data.cmpt++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 |
data.totalSize += uint64(f.Size()) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 |
fo := &fileObj{FilePath: path, FileInfo: f} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
if _, ok := data.sizeGroups[f.Size()]; !ok { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
132 |
data.sizeGroups[f.Size()] = new(FileObjList) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
133 |
} |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
134 |
*data.sizeGroups[f.Size()] = append(*data.sizeGroups[f.Size()], fo) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
135 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
136 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 |
|
20 | 138 |
// Checksum computes the file's complete SHA1 hash. |
139 |
func (fo *fileObj) Checksum() error { |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 |
file, err := os.Open(fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
141 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
142 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
144 |
defer file.Close() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
145 |
hash := sha1.New() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
146 |
if size, err := io.Copy(hash, file); size != fo.Size() || err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
147 |
if err == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
148 |
return errors.New("failed to read the whole file: " + |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
149 |
fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
150 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
151 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
152 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
153 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
154 |
fo.Hash = hash.Sum(nil) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
155 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
156 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
158 |
|
20 | 159 |
// partialChecksum computes the file's partial SHA1 hash (first and last bytes). |
160 |
func (fo *fileObj) partialChecksum() error { |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
161 |
file, err := os.Open(fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
162 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
163 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
164 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
165 |
defer file.Close() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
166 |
hash := sha1.New() |
19 | 167 |
|
168 |
// Read first bytes and last bytes from file |
|
169 |
for i := 0; i < 2; i++ { |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
170 |
if _, err := io.CopyN(hash, file, medsumBytes); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
171 |
if err == nil { |
19 | 172 |
const errmsg = "failed to read bytes from file: " |
173 |
return errors.New(errmsg + fo.FilePath) |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
174 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
175 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
176 |
} |
19 | 177 |
if i == 0 { // Seek to end of file |
178 |
file.Seek(0-medsumBytes, 2) |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
179 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
180 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
181 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
182 |
fo.PartialHash = hash.Sum(nil) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
183 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
184 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
185 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
186 |
|
20 | 187 |
// Sum computes the file's SHA1 hash, partial or full according to sType. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
188 |
func (fo *fileObj) Sum(sType sumType) error { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
189 |
if sType == partialChecksum { |
20 | 190 |
return fo.partialChecksum() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
191 |
} else if sType == fullChecksum { |
20 | 192 |
return fo.Checksum() |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
193 |
} else if sType == noChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
194 |
return nil |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
195 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
196 |
panic("Internal error: Invalid sType") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
197 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
198 |
|
20 | 199 |
// dispCount display statistics to the user. |
200 |
func (data *dataT) dispCount() { // It this still useful? |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
201 |
if myLog.verbosity < 4 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
202 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
203 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
204 |
var c1, c1b, c2 int |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
205 |
var s1 string |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
206 |
for _, scListP := range data.sizeGroups { |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
207 |
c1 += len(*scListP) |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
208 |
c2++ |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
209 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
210 |
c1b = len(data.emptyFiles) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
211 |
if c1b > 0 { |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
212 |
s1 = fmt.Sprintf("+%d", c1b) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
213 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
214 |
myLog.Printf(4, " Current countdown: %d [%d%s/%d]\n", |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
215 |
c1+c1b, c1, s1, c2) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
216 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
217 |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
218 |
// checksum returns the requested checksum as a string. |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
219 |
// If the checksum has not been pre-computed, it is calculated now. |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
220 |
func (fo fileObj) checksum(sType sumType) (string, error) { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
221 |
var hbytes []byte |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
222 |
if sType == partialChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
223 |
hbytes = fo.PartialHash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
224 |
} else if sType == fullChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
225 |
hbytes = fo.Hash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
226 |
} else { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
227 |
panic("Internal error: Invalid sType") |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
228 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
229 |
if hbytes == nil { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
230 |
if err := fo.Sum(sType); err != nil { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
231 |
return "", err |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
232 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
233 |
if sType == partialChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
234 |
hbytes = fo.PartialHash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
235 |
} else if sType == fullChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
236 |
hbytes = fo.Hash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
237 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
238 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
239 |
return hex.EncodeToString(hbytes), nil |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
240 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
241 |
|
20 | 242 |
// computeSheduledChecksums calculates the checksums for all the files |
243 |
// from the fileLists slice items (the kind of hash is taken from the |
|
244 |
// needHash field). |
|
16 | 245 |
func computeSheduledChecksums(fileLists ...foListList) { |
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
246 |
var bigFileList FileObjList |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
247 |
// Merge the lists of FileObjList lists and create a unique list |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
248 |
// of file objects. |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
249 |
for _, foll := range fileLists { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
250 |
for _, fol := range foll { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
251 |
bigFileList = append(bigFileList, fol...) |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
252 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
253 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
254 |
|
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
255 |
// Sort the list for better efficiency |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
256 |
sort.Sort(ByInode(bigFileList)) |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
257 |
|
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
258 |
// Compute checksums |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
259 |
for _, fo := range bigFileList { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
260 |
if err := fo.Sum(fo.needHash); err != nil { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
261 |
myLog.Println(0, "Error:", err) |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
262 |
} |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
263 |
fo.needHash = noChecksum |
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
264 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
265 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
266 |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
267 |
func (fileList FileObjList) scheduleChecksum(sType sumType) { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
268 |
for _, fo := range fileList { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
269 |
fo.needHash = sType |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
270 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
271 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
272 |
|
20 | 273 |
// findDupesChecksums splits the fileObj list into several lists with the |
274 |
// same sType hash. |
|
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
275 |
func (fileList FileObjList) findDupesChecksums(sType sumType, dryRun bool) foListList { |
16 | 276 |
var dupeList foListList |
277 |
var scheduleFull foListList |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
278 |
hashes := make(map[string]FileObjList) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
279 |
|
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
280 |
// Sort the list for better efficiency |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
281 |
sort.Sort(ByInode(fileList)) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
282 |
|
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
283 |
if sType == fullChecksum && dryRun { |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
284 |
fileList.scheduleChecksum(fullChecksum) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
285 |
return append(dupeList, fileList) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
286 |
} |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
287 |
// Compute checksums |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
288 |
for _, fo := range fileList { |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
289 |
hash, err := fo.checksum(sType) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
290 |
if err != nil { |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
291 |
myLog.Println(0, "Error:", err) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
292 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
293 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
294 |
hashes[hash] = append(hashes[hash], fo) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
295 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
296 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
297 |
// Let's de-dupe now... |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
298 |
for _, l := range hashes { |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
299 |
if len(l) < 2 { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
300 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
301 |
} |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
302 |
if sType == partialChecksum { |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
303 |
scheduleFull = append(scheduleFull, l) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
304 |
} else { // full checksums -> we're done |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
305 |
dupeList = append(dupeList, l) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
306 |
myLog.Printf(5, " . found %d new duplicates\n", len(l)) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
307 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
308 |
} |
9 | 309 |
if sType == partialChecksum && len(scheduleFull) > 0 { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
310 |
//computeSheduledChecksums(scheduleFull) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
311 |
for _, l := range scheduleFull { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
312 |
r := l.findDupesChecksums(fullChecksum, dryRun) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
313 |
dupeList = append(dupeList, r...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
314 |
} |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
315 |
if dryRun { |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
316 |
return scheduleFull |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
317 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
318 |
} |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
319 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
320 |
return dupeList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
321 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
322 |
|
6 | 323 |
// findDupes() uses checksums to find file duplicates |
16 | 324 |
func (data *dataT) findDupes(skipPartial bool) foListList { |
325 |
var dupeList foListList |
|
326 |
var schedulePartial foListList |
|
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
327 |
var schedulePartial2 foListList |
16 | 328 |
var scheduleFull foListList |
6 | 329 |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
330 |
for size, sgListP := range data.sizeGroups { |
6 | 331 |
// We skip partial checksums for small files or if requested |
332 |
if size > minSizePartialChecksum && !skipPartial { |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
333 |
sgListP.scheduleChecksum(partialChecksum) |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
334 |
schedulePartial = append(schedulePartial, *sgListP) |
6 | 335 |
} else { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
336 |
sgListP.scheduleChecksum(fullChecksum) |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
337 |
scheduleFull = append(scheduleFull, *sgListP) |
6 | 338 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
339 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
340 |
|
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
341 |
computeSheduledChecksums(schedulePartial, scheduleFull) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
342 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
343 |
for _, l := range schedulePartial { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
344 |
r := l.findDupesChecksums(partialChecksum, true) // dry-run |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
345 |
schedulePartial2 = append(schedulePartial2, r...) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
346 |
} |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
347 |
computeSheduledChecksums(schedulePartial2) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
348 |
for _, l := range schedulePartial { |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
349 |
r := l.findDupesChecksums(partialChecksum, false) |
6 | 350 |
dupeList = append(dupeList, r...) |
351 |
} |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
352 |
for _, l := range scheduleFull { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
353 |
r := l.findDupesChecksums(fullChecksum, false) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
354 |
dupeList = append(dupeList, r...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
355 |
} |
6 | 356 |
return dupeList |
357 |
} |
|
358 |
||
20 | 359 |
// dropEmptyFiles removes the empty files from the main map, since we don't |
360 |
// have to do any processing about them. |
|
361 |
// If ignoreEmpty is false, the empty file list is saved in data.emptyFiles. |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
362 |
func (data *dataT) dropEmptyFiles(ignoreEmpty bool) (emptyCount int) { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
363 |
sgListP, ok := data.sizeGroups[0] |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
364 |
if ok == false { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
365 |
return // no empty files |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
366 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
367 |
if !ignoreEmpty { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
368 |
if len(*sgListP) > 1 { |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
369 |
data.emptyFiles = *sgListP |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
370 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
371 |
delete(data.sizeGroups, 0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
372 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
373 |
} |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
374 |
emptyCount = len(*sgListP) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
375 |
delete(data.sizeGroups, 0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
376 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
377 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
378 |
|
2
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
379 |
// initialCleanup() removes files with unique size as well as hard links |
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
380 |
func (data *dataT) initialCleanup() (hardLinkCount, uniqueSizeCount int) { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
381 |
for s, sgListP := range data.sizeGroups { |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
382 |
if len(*sgListP) < 2 { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
383 |
delete(data.sizeGroups, s) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
384 |
uniqueSizeCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
385 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
386 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
387 |
|
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
388 |
// We can't look for hard links if the O.S. does not support |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
389 |
// them... |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
390 |
if !OSHasInodes() { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
391 |
continue |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
392 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
393 |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
394 |
var hardlinksFound bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
395 |
|
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
396 |
// Check for hard links |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
397 |
// Remove unique dev/inodes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
398 |
// Instead of this loop, another way would be to use the field |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
399 |
// "Unique" of the fileObj to mark them to be discarded |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
400 |
// and remove them all at the end. |
20 | 401 |
// TODO: Should we also check for duplicate paths? |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
402 |
for { |
15
4e3a67dc70a0
Cosmetics (go fmt and comments...)
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
403 |
type devinode struct{ dev, ino uint64 } |
12 | 404 |
devinodes := make(map[devinode]bool) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
405 |
var hardLinkIndex int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
406 |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
407 |
for i, fo := range *sgListP { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
408 |
dev, ino := GetDevIno(fo) |
15
4e3a67dc70a0
Cosmetics (go fmt and comments...)
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
409 |
di := devinode{dev, ino} |
12 | 410 |
if _, hlink := devinodes[di]; hlink { |
411 |
hardLinkIndex = i |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
412 |
hardLinkCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
413 |
hardlinksFound = true |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
414 |
break |
12 | 415 |
} else { |
416 |
devinodes[di] = true |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
417 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
418 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
419 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
420 |
if hardLinkIndex == 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
421 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
422 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
423 |
i := hardLinkIndex |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
424 |
// Remove hardink |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
425 |
copy((*sgListP)[i:], (*sgListP)[i+1:]) |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
426 |
(*sgListP)[len(*sgListP)-1] = nil |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
427 |
*sgListP = (*sgListP)[:len(*sgListP)-1] |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
428 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
429 |
// We have found hard links in this size group, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
430 |
// maybe we can remove it |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
431 |
if hardlinksFound { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
432 |
if len(*sgListP) < 2 { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
433 |
delete(data.sizeGroups, s) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
434 |
uniqueSizeCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
435 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
436 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
437 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
438 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
439 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
440 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
441 |
|
20 | 442 |
// formatSize returns the size in a string with a human-readable format. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
443 |
func formatSize(sizeBytes uint64, short bool) string { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
444 |
var units = map[int]string{ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
445 |
0: "B", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
446 |
1: "KiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
447 |
2: "MiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
448 |
3: "GiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
449 |
4: "TiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
450 |
5: "PiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
451 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
452 |
humanSize := sizeBytes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
453 |
var n int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
454 |
for n < len(units)-1 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
455 |
if humanSize < 10000 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
456 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
457 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
458 |
humanSize /= 1024 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
459 |
n++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
460 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
461 |
if n < 1 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
462 |
return fmt.Sprintf("%d bytes", sizeBytes) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
463 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
464 |
if short { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
465 |
return fmt.Sprintf("%d %s", humanSize, units[n]) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
466 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
467 |
return fmt.Sprintf("%d bytes (%d %s)", sizeBytes, humanSize, units[n]) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
468 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
469 |
|
20 | 470 |
// It all starts here. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
471 |
func main() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
472 |
var verbose bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
473 |
var summary bool |
36 | 474 |
var outToJSON bool |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
475 |
var skipPartial bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
476 |
var ignoreEmpty bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
477 |
|
23
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
478 |
// Assertion on constant values |
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
479 |
if minSizePartialChecksum <= 2*medsumBytes { |
34
b70346ff153d
Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
480 |
myLog.Fatal("Internal error: assert minSizePartialChecksum > 2*medsumBytes") |
23
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
481 |
} |
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
482 |
|
20 | 483 |
// Command line parameters parsingg |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
484 |
flag.BoolVar(&verbose, "verbose", false, "Be verbose (verbosity=1)") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
485 |
flag.BoolVar(&verbose, "v", false, "See --verbose") |
36 | 486 |
flag.BoolVar(&outToJSON, "json", false, "Use JSON format for output") |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
487 |
flag.BoolVar(&summary, "summary", false, "Do not display the duplicate list") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
488 |
flag.BoolVar(&summary, "s", false, "See --summary") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
489 |
flag.BoolVar(&skipPartial, "skip-partial", false, "Skip partial checksums") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
490 |
flag.IntVar(&myLog.verbosity, "verbosity", 0, |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
491 |
"Set verbosity level (1-6)") |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
492 |
flag.IntVar(&myLog.verbosity, "vl", 0, "See verbosity") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
493 |
timings := flag.Bool("timings", false, "Set detailed log timings") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
494 |
flag.BoolVar(&ignoreEmpty, "no-empty", false, "Ignore empty files") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
495 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
496 |
flag.Parse() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
497 |
|
20 | 498 |
// Set verbosity: --verbose=true == --verbosity=1 |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
499 |
if myLog.verbosity > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
500 |
verbose = true |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
501 |
} else if verbose == true { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
502 |
myLog.verbosity = 1 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
503 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
504 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
505 |
if len(flag.Args()) == 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
506 |
// TODO: more helpful usage statement |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
507 |
myLog.Println(-1, "Usage:", os.Args[0], |
20 | 508 |
"[options] base_directory|file...") |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
509 |
os.Exit(0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
510 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
511 |
|
20 | 512 |
// Change log format for benchmarking |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
513 |
if *timings { |
34
b70346ff153d
Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
514 |
myLog.SetBenchFlags() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
515 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
516 |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
517 |
data.sizeGroups = make(map[int64]*FileObjList) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
518 |
myLog.Println(1, "* Reading file metadata") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
519 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
520 |
for _, root := range flag.Args() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
521 |
if err := filepath.Walk(root, visit); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
522 |
myLog.Printf(-1, "* Error: could not read file tree:\n") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
523 |
myLog.Printf(-1, "> %v\n", err) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
524 |
os.Exit(1) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
525 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
526 |
} |
20 | 527 |
|
528 |
// Count empty files and drop them if they should be ignored |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
529 |
emptyCount := data.dropEmptyFiles(ignoreEmpty) |
20 | 530 |
|
531 |
// Display a small report |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
532 |
if verbose { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
533 |
if data.ignoreCount > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
534 |
myLog.Printf(1, " %d special files were ignored\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
535 |
data.ignoreCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
536 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
537 |
myLog.Println(2, " Initial counter:", data.cmpt, "files") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
538 |
myLog.Println(2, " Total size:", formatSize(data.totalSize, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
539 |
false)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
540 |
if emptyCount > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
541 |
myLog.Printf(1, " %d empty files were ignored\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
542 |
emptyCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
543 |
} |
20 | 544 |
data.dispCount() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
545 |
myLog.Println(3, "* Number of size groups:", len(data.sizeGroups)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
546 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
547 |
|
20 | 548 |
// Remove unique sizes and hard links |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
549 |
myLog.Println(1, "* Removing files with unique size and hard links...") |
2
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
550 |
hardLinkCount, uniqueSizeCount := data.initialCleanup() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
551 |
if verbose { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
552 |
myLog.Printf(2, " Dropped %d files with unique size\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
553 |
uniqueSizeCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
554 |
myLog.Printf(2, " Dropped %d hard links\n", hardLinkCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
555 |
myLog.Println(3, "* Number of size groups:", len(data.sizeGroups)) |
20 | 556 |
data.dispCount() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
557 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
558 |
|
20 | 559 |
// Get the final list of dupes, using checksums |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
560 |
myLog.Println(1, "* Computing checksums...") |
16 | 561 |
var result foListList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
562 |
if len(data.emptyFiles) > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
563 |
result = append(result, data.emptyFiles) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
564 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
565 |
result = append(result, data.findDupes(skipPartial)...) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
566 |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
567 |
myLog.Println(3, "* Number of match groups:", len(result)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
568 |
|
36 | 569 |
// Done! Prepare results data |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
570 |
if len(result) > 0 && !summary { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
571 |
myLog.Println(1, "* Dupes:") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
572 |
} |
21 | 573 |
|
574 |
// Sort files by path inside each group |
|
575 |
for _, l := range result { |
|
576 |
sort.Sort(byFilePathName(l)) |
|
577 |
} |
|
578 |
// Sort groups by increasing size (of the duplicated files) |
|
16 | 579 |
sort.Sort(byGroupFileSize(result)) |
580 |
||
36 | 581 |
var results Results |
582 |
||
583 |
for _, l := range result { |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
584 |
size := uint64(l[0].Size()) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
585 |
// We do not count the size of the 1st item |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
586 |
// so we get only duplicate size. |
36 | 587 |
results.RedundantDataSize += size * uint64(len(l)-1) |
588 |
newSet := ResultSet{Size: size} |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
589 |
for _, f := range l { |
36 | 590 |
newSet.Paths = append(newSet.Paths, f.FilePath) |
591 |
results.Duplicates++ |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
592 |
} |
36 | 593 |
results.Groups = append(results.Groups, newSet) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
594 |
} |
36 | 595 |
results.RedundantDataSizeH = formatSize(results.RedundantDataSize, true) |
596 |
results.TotalFileCount = data.cmpt |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
597 |
|
36 | 598 |
// Output the results |
599 |
displayResults(results, outToJSON, summary) |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
600 |
} |