forked from markfasheh/duperemove
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduperemove.c
More file actions
463 lines (401 loc) · 9.73 KB
/
duperemove.c
File metadata and controls
463 lines (401 loc) · 9.73 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* duperemove.c
*
* Copyright (C) 2013 SUSE. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* 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.
*
* Authors: Mark Fasheh <mfasheh@suse.de>
*/
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <inttypes.h>
#include <unistd.h>
#include <glib.h>
#include "list.h"
#include "csum.h"
#include "filerec.h"
#include "hash-tree.h"
#include "results-tree.h"
#include "dedupe.h"
#include "util.h"
#include "btrfs-util.h"
#include "dbfile.h"
#include "memstats.h"
#include "debug.h"
#include "file_scan.h"
#include "find_dupes.h"
#include "run_dedupe.h"
/* exported via debug.h */
int verbose = 0, debug = 0;
#define MIN_BLOCKSIZE (4*1024)
/* max blocksize is somewhat arbitrary. */
#define MAX_BLOCKSIZE (1024*1024)
#define DEFAULT_BLOCKSIZE (128*1024)
unsigned int blocksize = DEFAULT_BLOCKSIZE;
int run_dedupe = 0;
int recurse_dirs = 0;
int one_file_system = 0;
int target_rw = 1;
static int version_only = 0;
static int fdupes_mode = 0;
static enum {
H_NONE = 0,
H_READ,
H_WRITE,
H_UPDATE,
} use_hashfile = H_NONE;
static char *serialize_fname = NULL;
unsigned int io_threads;
int do_lookup_extents = 0;
int stdout_is_tty = 0;
static char *user_hash = DEFAULT_HASH_STR;
static void usage(const char *prog)
{
printf("duperemove %s\n", VERSTRING);
if (version_only)
return;
printf("Find duplicate extents and print them to stdout\n\n");
printf("Usage: %s [-r] [-d] [-h] [--debug] [--hashfile=hashfile]"
" OBJECTS\n", prog);
printf("\n\"OBJECTS\" is a list of files (or directories) which we\n");
printf("want to find duplicate extents in. If a directory is \n");
printf("specified, all regular files inside of it will be scanned.\n");
printf("\n\t<switches>\n");
printf("\t-r\t\tEnable recursive dir traversal.\n");
printf("\t-d\t\tDe-dupe the results - only works on btrfs.\n");
printf("\t-h\t\tPrint numbers in human-readable format.\n");
printf("\t--hashfile=FILE\tUse a file instead of memory for storing hashes.\n");
printf("\t--help\t\tPrints this help text.\n");
printf("\nPlease see the duperemove(8) manpage for more options.\n");
}
static int parse_yesno_option(char *arg, int default_val)
{
if (strncmp(arg, "yes", 3) == 0)
return 1;
else if (strncmp(arg, "no", 2) == 0)
return 0;
return default_val;
}
enum {
DEBUG_OPTION = CHAR_MAX + 1,
HELP_OPTION,
VERSION_OPTION,
WRITE_HASHES_OPTION,
READ_HASHES_OPTION,
HASHFILE_OPTION,
IO_THREADS_OPTION,
LOOKUP_EXTENTS_OPTION,
ONE_FILESYSTEM_OPTION,
HASH_OPTION,
FDUPES_OPTION,
};
static int files_from_stdin(int fdupes)
{
int ret = 0;
char *path = NULL;
size_t pathlen = 0;
ssize_t readlen;
while ((readlen = getline(&path, &pathlen, stdin)) != -1) {
if (readlen == 0)
continue;
if (fdupes && readlen == 1 && path[0] == '\n') {
ret = fdupes_dedupe();
if (ret)
return ret;
continue;
}
if (readlen > 0 && path[readlen - 1] == '\n') {
path[--readlen] = '\0';
}
if (readlen > PATH_MAX - 1) {
fprintf(stderr, "Path max exceeded: %s\n", path);
continue;
}
if (add_file(path, AT_FDCWD))
return 1;
/* Give the user a chance to see some output from add_file(). */
if (!fdupes)
fflush(stdout);
}
if (path != NULL)
free(path);
return 0;
}
/*
* Ok this is doing more than just parsing options.
*/
static int parse_options(int argc, char **argv)
{
int i, c, numfiles;
int read_hashes = 0;
int write_hashes = 0;
int update_hashes = 0;
static struct option long_ops[] = {
{ "debug", 0, NULL, DEBUG_OPTION },
{ "help", 0, NULL, HELP_OPTION },
{ "version", 0, NULL, VERSION_OPTION },
{ "write-hashes", 1, NULL, WRITE_HASHES_OPTION },
{ "read-hashes", 1, NULL, READ_HASHES_OPTION },
{ "hashfile", 1, NULL, HASHFILE_OPTION },
{ "io-threads", 1, NULL, IO_THREADS_OPTION },
{ "hash-threads", 1, NULL, IO_THREADS_OPTION },
{ "lookup-extents", 1, NULL, LOOKUP_EXTENTS_OPTION },
{ "one-file-system", 0, NULL, ONE_FILESYSTEM_OPTION },
{ "hash", 1, NULL, HASH_OPTION },
{ "fdupes", 0, NULL, FDUPES_OPTION },
{ NULL, 0, NULL, 0}
};
if (argc < 2)
return 1;
while ((c = getopt_long(argc, argv, "Ab:vdDrh?x", long_ops, NULL))
!= -1) {
switch (c) {
case 'A':
target_rw = 0;
break;
case 'b':
blocksize = parse_size(optarg);
if (blocksize < MIN_BLOCKSIZE ||
blocksize > MAX_BLOCKSIZE)
return EINVAL;
break;
case 'd':
case 'D':
run_dedupe = 1;
break;
case 'r':
recurse_dirs = 1;
break;
case VERSION_OPTION:
version_only = 1;
break;
case DEBUG_OPTION:
debug = 1;
/* Fall through */
case 'v':
verbose = 1;
break;
case 'h':
human_readable = 1;
break;
case WRITE_HASHES_OPTION:
write_hashes = 1;
serialize_fname = strdup(optarg);
break;
case READ_HASHES_OPTION:
read_hashes = 1;
serialize_fname = strdup(optarg);
break;
case HASHFILE_OPTION:
update_hashes = 1;
serialize_fname = strdup(optarg);
break;
case IO_THREADS_OPTION:
io_threads = strtoul(optarg, NULL, 10);
if (!io_threads)
return EINVAL;
break;
case LOOKUP_EXTENTS_OPTION:
do_lookup_extents = parse_yesno_option(optarg, 0);
break;
case ONE_FILESYSTEM_OPTION:
case 'x':
one_file_system = 1;
break;
case HASH_OPTION:
user_hash = optarg;
break;
case FDUPES_OPTION:
fdupes_mode = 1;
break;
case HELP_OPTION:
case '?':
default:
version_only = 0;
return 1;
}
}
numfiles = argc - optind;
/* Filter out option combinations that don't make sense. */
if ((write_hashes + read_hashes + update_hashes) > 1) {
fprintf(stderr, "Error: Specify only one hashfile option.\n");
return 1;
}
if (read_hashes)
use_hashfile = H_READ;
else if (write_hashes)
use_hashfile = H_WRITE;
else if (update_hashes)
use_hashfile = H_UPDATE;
if (read_hashes) {
if (numfiles) {
fprintf(stderr,
"Error: --read-hashes option does not take a "
"file list argument\n");
return 1;
}
if (fdupes_mode) {
fprintf(stderr,
"Error: cannot mix hashfile option with "
"--fdupes option\n");
return 1;
}
goto out_nofiles;
}
if (fdupes_mode) {
if (numfiles) {
fprintf(stderr,
"Error: fdupes option does not take a file "
"list argument\n");
return 1;
}
/* rest of fdupes mode is implemented in main() */
return 0;
}
if (numfiles == 1 && strcmp(argv[optind], "-") == 0) {
if (files_from_stdin(0))
return 1;
} else {
for (i = 0; i < numfiles; i++) {
const char *name = argv[i + optind];
if (add_file(name, AT_FDCWD))
return 1;
}
}
/* This can happen if for example, all files passed in on
* command line are bad. */
if (list_empty(&filerec_list))
return EINVAL;
out_nofiles:
return 0;
}
int main(int argc, char **argv)
{
int ret;
struct results_tree res;
struct filerec *file;
struct hash_tree scan_tree;
init_filerec();
init_results_tree(&res);
init_hash_tree(&scan_tree);
/* Parse options might change this so set a default here */
#if GLIB_CHECK_VERSION(2,36,0)
io_threads = g_get_num_processors();
#else
io_threads = sysconf(_SC_NPROCESSORS_ONLN);
#endif
if (parse_options(argc, argv)) {
usage(argv[0]);
return EINVAL;
}
if (fdupes_mode)
return files_from_stdin(1);
ret = init_csum_module(user_hash);
if (ret) {
if (ret == EINVAL)
fprintf(stderr,
"Could not initialize hash module \"%s\"\n",
user_hash);
return ret;
}
if (isatty(STDOUT_FILENO))
stdout_is_tty = 1;
printf("Using %uK blocks\n", blocksize / 1024);
printf("Using hash: %s\n", csum_mod->name);
switch (use_hashfile) {
case H_UPDATE:
case H_WRITE:
ret = dbfile_create(serialize_fname);
if (ret)
break;
ret = populate_tree_swap();
break;
case H_READ:
ret = dbfile_open(serialize_fname);
if (ret)
break;
/*
* Skips the file scan, used to isolate the
* extent-find and dedupe stages
*/
ret = dbfile_get_config(&blocksize, NULL, NULL, NULL, NULL);
break;
case H_NONE:
ret = populate_tree_aim(&scan_tree);
break;
default:
abort_lineno();
break;
}
if (ret) {
fprintf(stderr, "Error while populating extent tree!\n");
goto out;
}
/*
* File scan from above can cause quite a bit of output, flush
* here in case of logfile.
*/
if (stdout_is_tty)
fflush(stdout);
if (use_hashfile == H_WRITE || use_hashfile == H_UPDATE) {
ret = dbfile_sync_config(blocksize);
if (ret)
goto out;
if (use_hashfile == H_WRITE) {
/*
* This option is for isolating the file scan
* stage. Exit the program now.
*/
printf("Hashfile \"%s\" written, exiting.\n",
serialize_fname);
goto out;
}
}
if (use_hashfile == H_NONE) {
ret = find_all_dupes(&scan_tree, &res);
} else {
/* We will now reread the serialized file, and create a new
* shiny tree with only duplicate hashes
*/
struct hash_tree dups_tree;
printf("Loading only duplicated hashes from hashfile.\n");
init_hash_tree(&dups_tree);
ret = dbfile_load_hashes(&dups_tree);
if (ret)
goto out;
ret = find_all_dupes(&dups_tree, &res);
}
if (debug) {
print_dupes_table(&res);
printf("\n\nRemoving overlapping extents\n\n");
}
list_for_each_entry(file, &filerec_list, rec_list) {
remove_overlapping_extents(&res, file);
}
if (run_dedupe)
dedupe_results(&res);
else
print_dupes_table(&res);
free_all_filerecs();
out:
dbfile_close();
if (ret == ENOMEM || debug)
print_mem_stats();
return ret;
}