forked from h2oai/jdupes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.c
More file actions
429 lines (371 loc) · 13.8 KB
/
match.c
File metadata and controls
429 lines (371 loc) · 13.8 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* jdupes file matching functions
* This file is part of jdupes; see jdupes.c for license information */
#ifdef __linux__
#include <fcntl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libjodycode.h>
#include "jdupes.h"
#include "likely_unlikely.h"
#include "checks.h"
#include "filehash.h"
#ifndef NO_HASHDB
#include "hashdb.h"
#endif
#include "interrupt.h"
#include "match.h"
#include "progress.h"
#ifndef NO_HARDLINKS
/* Copy any hashes between entries for detected hard-linked files */
static void cross_copy_hashes(file_t *file1, file_t *file2)
{
#ifndef NO_HASHDB
int dirty1 = 0, dirty2 = 0;
#endif
if (file1 == NULL || file2 == NULL) jc_nullptr("cross_copy_hashes()");
if (ISFLAG(file1->flags, FF_HASH_FULL)) {
if (ISFLAG(file2->flags, FF_HASH_FULL)) return;
file2->filehash_partial = file1->filehash_partial;
file2->filehash = file1->filehash;
SETFLAG(file2->flags, FF_HASH_PARTIAL | FF_HASH_FULL);
#ifndef NO_HASHDB
dirty2 = 1;
#endif
} else if (ISFLAG(file2->flags, FF_HASH_FULL)) {
if (ISFLAG(file1->flags, FF_HASH_FULL)) return;
file1->filehash_partial = file2->filehash_partial;
file1->filehash = file2->filehash;
SETFLAG(file1->flags, FF_HASH_PARTIAL | FF_HASH_FULL);
#ifndef NO_HASHDB
dirty1 = 1;
#endif
} else if (ISFLAG(file1->flags, FF_HASH_PARTIAL)) {
if (ISFLAG(file2->flags, FF_HASH_PARTIAL)) return;
file2->filehash_partial = file1->filehash_partial;
SETFLAG(file2->flags, FF_HASH_PARTIAL);
#ifndef NO_HASHDB
dirty2 = 1;
#endif
} else if (ISFLAG(file2->flags, FF_HASH_PARTIAL)) {
if (ISFLAG(file1->flags, FF_HASH_PARTIAL)) return;
file1->filehash_partial = file2->filehash_partial;
SETFLAG(file1->flags, FF_HASH_PARTIAL);
#ifndef NO_HASHDB
dirty1 = 1;
#endif
}
/* Add to hash database */
#ifndef NO_HASHDB
if (ISFLAG(flags, F_HASHDB)) {
if (dirty1 == 1) add_hashdb_entry(NULL, 0, file1);
if (dirty2 == 1) add_hashdb_entry(NULL, 0, file2);
}
#endif
return;
}
#endif /* NO_HARDLINKS */
void registerpair(file_t **matchlist, file_t *newmatch, int (*comparef)(file_t *f1, file_t *f2))
{
file_t *traverse;
file_t *back;
/* NULL pointer sanity checks */
if (unlikely(matchlist == NULL || newmatch == NULL || comparef == NULL)) jc_nullptr("registerpair()");
LOUD(fprintf(stderr, "registerpair: '%s', '%s'\n", (*matchlist)->d_name, newmatch->d_name);)
#ifndef NO_ERRORONDUPE
if (ISFLAG(a_flags, FA_ERRORONDUPE)) {
if (!ISFLAG(flags, F_HIDEPROGRESS)) fprintf(stderr, "\r");
fprintf(stderr, "Exiting based on user request (-e); duplicates found:\n");
printf("%s\n%s\n", (*matchlist)->d_name, newmatch->d_name);
exit(255);
}
#endif
SETFLAG((*matchlist)->flags, FF_HAS_DUPES);
back = NULL;
traverse = *matchlist;
/* FIXME: This needs to be changed! As it currently stands, the compare
* function only runs on a pair as it is registered and future pairs can
* mess up the sort order. A separate sorting function should happen before
* the dupe chain is acted upon rather than while pairs are registered. */
while (traverse) {
if (comparef(newmatch, traverse) <= 0) {
newmatch->duplicates = traverse;
if (!back) {
*matchlist = newmatch; /* update pointer to head of list */
SETFLAG(newmatch->flags, FF_HAS_DUPES);
CLEARFLAG(traverse->flags, FF_HAS_DUPES); /* flag is only for first file in dupe chain */
} else back->duplicates = newmatch;
break;
} else {
if (traverse->duplicates == 0) {
traverse->duplicates = newmatch;
if (!back) SETFLAG(traverse->flags, FF_HAS_DUPES);
break;
}
}
back = traverse;
traverse = traverse->duplicates;
}
return;
}
void registerfile(filetree_t * restrict * const restrict nodeptr, const enum tree_direction d, file_t * const restrict file)
{
filetree_t * restrict branch;
if (unlikely(nodeptr == NULL || file == NULL || (d != NONE && *nodeptr == NULL))) jc_nullptr("registerfile()");
LOUD(fprintf(stderr, "registerfile(direction %d)\n", d));
/* Allocate and initialize a new node for the file */
branch = (filetree_t *)malloc(sizeof(filetree_t));
if (unlikely(branch == NULL)) jc_oom("registerfile() branch");
branch->file = file;
branch->left = NULL;
branch->right = NULL;
/* Attach the new node to the requested branch */
switch (d) {
case LEFT:
(*nodeptr)->left = branch;
break;
case RIGHT:
(*nodeptr)->right = branch;
break;
case NONE:
/* For the root of the tree only */
*nodeptr = branch;
break;
default:
/* This should never ever happen */
fprintf(stderr, "\ninternal error: invalid direction for registerfile(), report this\n");
exit(EXIT_FAILURE);
break;
}
return;
}
/* Check two files for a match */
file_t **checkmatch(filetree_t * restrict tree, file_t * const restrict file)
{
int cmpresult = 0;
int cantmatch = 0;
const uint64_t * restrict filehash;
#ifndef NO_HASHDB
int dirtyfile = 0, dirtytree = 0;
#endif
if (unlikely(tree == NULL || file == NULL || tree->file == NULL || tree->file->d_name == NULL || file->d_name == NULL)) jc_nullptr("checkmatch()");
LOUD(fprintf(stderr, "checkmatch ('%s', '%s')\n", tree->file->d_name, file->d_name));
/* If device and inode fields are equal one of the files is a
* hard link to the other or the files have been listed twice
* unintentionally. We don't want to flag these files as
* duplicates unless the user specifies otherwise. */
/* Count the total number of comparisons requested */
DBG(comparisons++;)
/* If considering hard linked files as duplicates, they are
* automatically duplicates without being read further since
* they point to the exact same inode. If we aren't considering
* hard links as duplicates, we just return NULL. */
cmpresult = check_conditions(tree->file, file);
switch (cmpresult) {
#ifndef NO_HARDLINKS
case 2:
cross_copy_hashes(tree->file, file);
return &tree->file; /* linked files + -H switch */
case -2: return NULL; /* linked files, no -H switch */
#endif
case -3: /* user order */
case -4: /* one filesystem */
case -5: /* permissions */
cantmatch = 1;
cmpresult = 0;
break;
default: break;
}
/* If preliminary matching succeeded, do main file data checks */
if (cmpresult == 0) {
/* Print pre-check (early) match candidates if requested */
if (ISFLAG(p_flags, PF_EARLYMATCH)) printf("Early match check passed:\n %s\n %s\n\n", file->d_name, tree->file->d_name);
LOUD(fprintf(stderr, "checkmatch: starting file data comparisons\n"));
/* Attempt to exclude files quickly with partial file hashing */
if (!ISFLAG(tree->file->flags, FF_HASH_PARTIAL)) {
filehash = get_filehash(tree->file, PARTIAL_HASH_SIZE, hash_algo);
if (filehash == NULL) return NULL;
tree->file->filehash_partial = *filehash;
SETFLAG(tree->file->flags, FF_HASH_PARTIAL);
#ifndef NO_HASHDB
dirtytree = 1;
#endif
}
if (!ISFLAG(file->flags, FF_HASH_PARTIAL)) {
filehash = get_filehash(file, PARTIAL_HASH_SIZE, hash_algo);
if (filehash == NULL) return NULL;
file->filehash_partial = *filehash;
SETFLAG(file->flags, FF_HASH_PARTIAL);
#ifndef NO_HASHDB
dirtyfile = 1;
#endif
}
cmpresult = HASH_COMPARE(file->filehash_partial, tree->file->filehash_partial);
LOUD(if (!cmpresult) fprintf(stderr, "checkmatch: partial hashes match\n"));
LOUD(if (cmpresult) fprintf(stderr, "checkmatch: partial hashes do not match\n"));
DBG(partial_hash++;)
/* Print partial hash matching pairs if requested */
if (cmpresult == 0 && ISFLAG(p_flags, PF_PARTIAL))
printf("\nPartial hashes match:\n %s\n %s\n\n", file->d_name, tree->file->d_name);
if (file->size <= PARTIAL_HASH_SIZE || ISFLAG(flags, F_PARTIALONLY)) {
if (ISFLAG(flags, F_PARTIALONLY)) { LOUD(fprintf(stderr, "checkmatch: partial only mode: treating partial hash as full hash\n")); }
else { LOUD(fprintf(stderr, "checkmatch: small file: copying partial hash to full hash\n")); }
/* filehash_partial = filehash if file is small enough */
if (!ISFLAG(file->flags, FF_HASH_FULL)) {
file->filehash = file->filehash_partial;
SETFLAG(file->flags, FF_HASH_FULL);
#ifndef NO_HASHDB
dirtyfile = 1;
#endif
DBG(small_file++;)
}
if (!ISFLAG(tree->file->flags, FF_HASH_FULL)) {
tree->file->filehash = tree->file->filehash_partial;
SETFLAG(tree->file->flags, FF_HASH_FULL);
#ifndef NO_HASHDB
dirtytree = 1;
#endif
DBG(small_file++;)
}
} else if (cmpresult == 0) {
// if (ISFLAG(flags, F_SKIPHASH)) {
// LOUD(fprintf(stderr, "checkmatch: skipping full file hashes (F_SKIPMATCH)\n"));
// } else {
/* If partial match was correct, perform a full file hash match */
if (!ISFLAG(tree->file->flags, FF_HASH_FULL)) {
filehash = get_filehash(tree->file, 0, hash_algo);
if (filehash == NULL) return NULL;
tree->file->filehash = *filehash;
SETFLAG(tree->file->flags, FF_HASH_FULL);
#ifndef NO_HASHDB
dirtytree = 1;
#endif
}
if (!ISFLAG(file->flags, FF_HASH_FULL)) {
filehash = get_filehash(file, 0, hash_algo);
if (filehash == NULL) return NULL;
file->filehash = *filehash;
SETFLAG(file->flags, FF_HASH_FULL);
#ifndef NO_HASHDB
dirtyfile = 1;
#endif
}
/* Full file hash comparison */
cmpresult = HASH_COMPARE(file->filehash, tree->file->filehash);
LOUD(if (!cmpresult) fprintf(stderr, "checkmatch: full hashes match\n"));
LOUD(if (cmpresult) fprintf(stderr, "checkmatch: full hashes do not match\n"));
DBG(full_hash++);
// }
} else {
DBG(partial_elim++);
}
} /* if (cmpresult == 0) */
/* Add to hash database */
#ifndef NO_HASHDB
if (ISFLAG(flags, F_HASHDB)) {
if (dirtyfile == 1) add_hashdb_entry(NULL, 0, file);
if (dirtytree == 1) add_hashdb_entry(NULL, 0, tree->file);
}
#endif
if ((cantmatch != 0) && (cmpresult == 0)) {
LOUD(fprintf(stderr, "checkmatch: rejecting because match not allowed (cantmatch = 1)\n"));
cmpresult = -1;
}
/* How the file tree works
*
* The tree is sorted by size as files arrive. If the files are the same
* size, they are possible duplicates and are checked for duplication.
* If they are not a match, the hashes are used to decide whether to
* continue with the file to the left or the right in the file tree.
* If the direction decision points to a leaf node, the duplicate scan
* continues down that path; if it points to an empty node, the current
* file is attached to the file tree at that point.
*
* This allows for quickly finding files of the same size by avoiding
* tree branches with differing size groups.
*/
if (cmpresult < 0) {
if (tree->left != NULL) {
LOUD(fprintf(stderr, "checkmatch: recursing tree: left\n"));
return checkmatch(tree->left, file);
} else {
LOUD(fprintf(stderr, "checkmatch: registering file: left\n"));
registerfile(&tree, LEFT, file);
return NULL;
}
} else if (cmpresult > 0) {
if (tree->right != NULL) {
LOUD(fprintf(stderr, "checkmatch: recursing tree: right\n"));
return checkmatch(tree->right, file);
} else {
LOUD(fprintf(stderr, "checkmatch: registering file: right\n"));
registerfile(&tree, RIGHT, file);
return NULL;
}
} else {
/* All compares matched */
DBG(partial_to_full++;)
LOUD(fprintf(stderr, "checkmatch: files appear to match based on hashes\n"));
if (ISFLAG(p_flags, PF_FULLHASH)) printf("Full hashes match:\n %s\n %s\n\n", file->d_name, tree->file->d_name);
return &tree->file;
}
}
/* Do a byte-by-byte comparison in case two different files produce the
same signature. Unlikely, but better safe than sorry. */
int confirmmatch(const char * const restrict file1, const char * const restrict file2, const off_t size)
{
static char *c1 = NULL, *c2 = NULL;
FILE *fp1, *fp2;
size_t r1, r2;
off_t bytes = 0;
int retval = 0;
if (unlikely(file1 == NULL || file2 == NULL)) jc_nullptr("confirmmatch()");
LOUD(fprintf(stderr, "confirmmatch running\n"));
if (unlikely(c1 == NULL || c2 == NULL)) {
c1 = (char *)malloc(auto_chunk_size);
c2 = (char *)malloc(auto_chunk_size);
}
if (unlikely(c1 == NULL || c2 == NULL)) jc_oom("confirmmatch() buffers");
fp1 = jc_fopen(file1, JC_FILE_MODE_RDONLY_SEQ);
fp2 = jc_fopen(file2, JC_FILE_MODE_RDONLY_SEQ);
if (fp1 == NULL) {
if (fp2 != NULL) fclose(fp2);
LOUD(fprintf(stderr, "confirmmatch: warning: file open failed ('%s')\n", file1);)
goto different;
}
if (fp2 == NULL) {
if (fp1 != NULL) fclose(fp1);
LOUD(fprintf(stderr, "confirmmatch: warning: file open failed ('%s')\n", file2);)
goto different;
}
fseek(fp1, 0, SEEK_SET);
fseek(fp2, 0, SEEK_SET);
#ifdef __linux__
/* Tell Linux we will accees sequentially and soon */
posix_fadvise(fileno(fp1), 0, size, POSIX_FADV_SEQUENTIAL);
posix_fadvise(fileno(fp1), 0, size, POSIX_FADV_WILLNEED);
posix_fadvise(fileno(fp2), 0, size, POSIX_FADV_SEQUENTIAL);
posix_fadvise(fileno(fp2), 0, size, POSIX_FADV_WILLNEED);
#endif /* __linux__ */
do {
if (interrupt) goto different;
r1 = fread(c1, sizeof(char), auto_chunk_size, fp1);
r2 = fread(c2, sizeof(char), auto_chunk_size, fp2);
if (r1 != r2) goto different; /* file lengths are different */
if (memcmp (c1, c2, r1)) goto different; /* file contents are different */
bytes += (off_t)r1;
if (jc_alarm_ring != 0) {
jc_alarm_ring = 0;
update_phase2_progress("confirm", (int)((bytes * 100) / size));
}
} while (r2);
/* Success: return 0 */
goto finish_confirm;
different:
retval = 1;
finish_confirm:
// free(c1); free(c2);
fclose(fp1); fclose(fp2);
return retval;
}