forked from oniony/TMSU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fingerprinter.go
264 lines (218 loc) · 6.28 KB
/
fingerprinter.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2011-2018 Paul Ruane.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package fingerprint
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"os"
"path/filepath"
"strconv"
"strings"
)
const sparseFingerprintThreshold = 5 * 1024 * 1024
const sparseFingerprintSize = 512 * 1024
func Create(path, fileAlgorithm, directoryAlgorithm, symlinkAlgorithm string) (Fingerprint, error) {
stat, err := os.Lstat(path)
if err != nil {
return Empty, err
}
if stat.Mode()&os.ModeSymlink != 0 {
if symlinkAlgorithm == "follow" {
stat, err = os.Stat(path)
if err != nil {
return Empty, err
}
} else {
return createSymlinkFingerprint(path, symlinkAlgorithm)
}
}
switch {
case stat.Mode().IsDir():
return createDirectoryFingerprint(path, directoryAlgorithm)
case stat.Mode().IsRegular():
return createFileFingerprint(path, fileAlgorithm, stat)
default:
return Empty, fmt.Errorf("unsupported file mode '%v'", stat.Mode())
}
}
// unexported
func createFileFingerprint(path, algorithm string, stat os.FileInfo) (Fingerprint, error) {
switch algorithm {
case "dynamic:SHA256", "":
return dynamicFingerprint(path, sha256.New(), stat.Size())
case "dynamic:SHA1":
return dynamicFingerprint(path, sha1.New(), stat.Size())
case "dynamic:MD5":
return dynamicFingerprint(path, md5.New(), stat.Size())
case "SHA256":
return regularFingerprint(path, sha256.New())
case "SHA1":
return regularFingerprint(path, sha1.New())
case "MD5":
return regularFingerprint(path, md5.New())
case "none":
return Empty, nil
default:
return "", fmt.Errorf("unsupported file fingerprint algorithm '%v'", algorithm)
}
}
func createDirectoryFingerprint(path, algorithm string) (Fingerprint, error) {
switch algorithm {
case "sumSizes":
return sumSizesFingerprint(path, 0)
case "dynamic:sumSizes", "":
return sumSizesFingerprint(path, 500)
case "none":
return Empty, nil
default:
return "", fmt.Errorf("unsupported directory fingerprint algorithm '%v'", algorithm)
}
}
func createSymlinkFingerprint(path, algorithm string) (Fingerprint, error) {
switch algorithm {
case "targetName":
return symlinkTargetNameFingerprint(path, true)
case "targetNameNoExt":
return symlinkTargetNameFingerprint(path, false)
case "none":
return Empty, nil
default:
return Empty, fmt.Errorf("unsupported symbolic link fingerprint algorithm '%v'", algorithm)
}
}
func regularFingerprint(path string, h hash.Hash) (Fingerprint, error) {
return calculateRegularFingerprint(path, h)
}
func dynamicFingerprint(path string, h hash.Hash, fileSize int64) (Fingerprint, error) {
if fileSize > sparseFingerprintThreshold {
return calculateSparseFingerprint(path, fileSize, h)
}
return calculateRegularFingerprint(path, h)
}
// Uses the symbolic target's filename as the fingerprint
func symlinkTargetNameFingerprint(path string, includeExtension bool) (Fingerprint, error) {
target, err := os.Readlink(path)
if err != nil {
if os.IsNotExist(err) {
return Empty, nil
}
return Empty, fmt.Errorf("'%v': could not determine target of symbolic link: %v", path, err)
}
fingerprint := filepath.Base(target)
if !includeExtension {
pos := strings.Index(fingerprint, ".")
if pos > -1 {
fingerprint = fingerprint[0:pos]
}
}
return Fingerprint(fingerprint), nil
}
// Creates a crude directory fingerprint by add the size of the contained files
func sumSizesFingerprint(path string, maxFiles uint) (Fingerprint, error) {
paths := []string{path}
var fileCount uint
var totalSize int64
out:
for index := 0; index < len(paths); index++ {
path := paths[index]
stats := stats(path)
for _, stat := range stats {
if stat.IsDir() {
childPath := filepath.Join(path, stat.Name())
paths = append(paths, childPath)
} else {
totalSize += stat.Size()
if fileCount++; maxFiles != 0 && fileCount >= maxFiles {
break out
}
}
}
}
return Fingerprint(strconv.FormatInt(totalSize, 16)), nil
}
func stats(path string) []os.FileInfo {
file, err := os.Open(path)
if err != nil {
return []os.FileInfo{} // ignore the error
}
defer file.Close()
stats, err := file.Readdir(0)
if err != nil {
return []os.FileInfo{} // ignore the error
}
return stats
}
func calculateSparseFingerprint(path string, fileSize int64, h hash.Hash) (Fingerprint, error) {
buffer := make([]byte, sparseFingerprintSize)
file, err := os.Open(path)
if err != nil {
return Empty, err
}
defer file.Close()
// start
count, err := file.Read(buffer)
if err != nil {
return Empty, err
}
h.Write(buffer[:count])
// middle
_, err = file.Seek((fileSize-sparseFingerprintSize)/2, 0)
if err != nil {
return Empty, err
}
count, err = file.Read(buffer)
if err != nil {
return Empty, err
}
h.Write(buffer[:count])
// end
_, err = file.Seek(-sparseFingerprintSize, 2)
if err != nil {
return Empty, err
}
count, err = file.Read(buffer)
if err != nil {
return Empty, err
}
h.Write(buffer[:count])
sum := h.Sum(make([]byte, 0, 64))
fingerprint := hex.EncodeToString(sum)
return Fingerprint(fingerprint), nil
}
func calculateRegularFingerprint(path string, h hash.Hash) (Fingerprint, error) {
file, err := os.Open(path)
if err != nil {
return Empty, err
}
defer file.Close()
buffer := make([]byte, 1024)
for count := 0; err == nil; count, err = file.Read(buffer) {
h.Write(buffer[:count])
}
sum := h.Sum(make([]byte, 0, 64))
fingerprint := hex.EncodeToString(sum)
return Fingerprint(fingerprint), nil
}
type FileInfoSlice []os.FileInfo
func (infos FileInfoSlice) Len() int {
return len(infos)
}
func (infos FileInfoSlice) Less(i, j int) bool {
return infos[i].Name() < infos[j].Name()
}
func (infos FileInfoSlice) Swap(i, j int) {
infos[j], infos[i] = infos[i], infos[j]
}