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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
|
/*
* 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 <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 "progress.h"
#include "file_scan.h"
#include "find_dupes.h"
#include "run_dedupe.h"
#include "opt.h"
unsigned int blocksize = DEFAULT_BLOCKSIZE;
static int stdin_filelist = 0;
static unsigned int list_only_opt = 0;
static unsigned int rm_only_opt = 0;
struct dbfile_config dbfile_cfg;
static enum {
H_READ,
H_WRITE,
H_UPDATE,
} use_hashfile = H_UPDATE;
static void print_file(char *filename, char *ino, char *subvol)
{
if (verbose)
printf("%s\t%s\t%s\n", filename, ino, subvol);
else
printf("%s\n", filename);
}
static int list_db_files(char *filename)
{
int ret;
_cleanup_(sqlite3_close_cleanup) struct dbhandle *db = dbfile_open_handle(filename);
if (!db) {
eprintf("Error: Could not open \"%s\"\n", filename);
return -1;
}
ret = dbfile_iter_files(db, &print_file);
return ret;
}
static void rm_db_files_from_stdin(struct dbhandle *db)
{
_cleanup_(freep) char *path = NULL;
size_t pathlen = 0;
ssize_t readlen;
while ((readlen = getline(&path, &pathlen, stdin)) != -1) {
if (readlen == 0)
continue;
if (readlen > 0 && path[readlen - 1] == '\n') {
path[--readlen] = '\0';
}
if (readlen > PATH_MAX - 1) {
eprintf("Path max exceeded: %s\n", path);
continue;
}
dbfile_remove_file(db, path);
}
}
static int rm_db_files(int numfiles, char **files)
{
int i, ret;
_cleanup_(sqlite3_close_cleanup) struct dbhandle *db = dbfile_open_handle(options.hashfile);
if (!db) {
eprintf("Error: Could not open \"%s\"\n", options.hashfile);
return -1;
}
for (i = 0; i < numfiles; i++) {
const char *name = files[i];
if (strlen(name) == 1 && name[0] == '-')
rm_db_files_from_stdin(db);
ret = dbfile_remove_file(db, name);
if (ret == 0)
vprintf("Removed \"%s\" from hashfile.\n", name);
if (ret)
printf("ret ?\n");
}
return 0;
}
static void print_version(void)
{
char *s = NULL;
#ifdef DEBUG_BUILD
s = " (debug build)";
#endif
printf("duperemove %s%s\n", VERSTRING, s ? s : "");
}
/* adapted from ocfs2-tools */
static int parse_dedupe_opts(const char *raw_opts)
{
_cleanup_(freep) char *opts;
char *token, *next, *p, *arg;
int print_usage = false;
int invert, ret = 0;
opts = strdup(raw_opts);
for (token = opts; token && *token; token = next) {
p = strchr(token, ',');
next = NULL;
invert = 0;
if (p) {
*p = '\0';
next = p + 1;
}
arg = strstr(token, "no");
if (arg == token) {
invert = 1;
token += strlen("no");
}
if (strcmp(token, "same") == 0) {
options.dedupe_same_file = !invert;
} else if (strcmp(token, "partial") == 0) {
options.do_block_hash = !invert;
} else if (strcmp(token, "only_whole_files") == 0) {
options.only_whole_files = !invert;
} else {
print_usage = true;
break;
}
}
if (print_usage) {
eprintf("Bad dedupe options specified. Valid dedupe "
"options are:\n"
"\t[no]same\n"
"\t[no]only_whole_files\n"
"\t[no]partial\n");
ret = EINVAL;
}
return ret;
}
enum {
DEBUG_OPTION = CHAR_MAX + 1,
HELP_OPTION,
VERSION_OPTION,
WRITE_HASHES_OPTION,
READ_HASHES_OPTION,
HASHFILE_OPTION,
IO_THREADS_OPTION,
CPU_THREADS_OPTION,
SKIP_ZEROES_OPTION,
FDUPES_OPTION,
DEDUPE_OPTS_OPTION,
QUIET_OPTION,
EXCLUDE_OPTION,
BATCH_SIZE_OPTION,
};
static int process_fdupes(void)
{
int ret = 0;
_cleanup_(freep) char *path = NULL;
size_t pathlen = 0;
ssize_t readlen;
while ((readlen = getline(&path, &pathlen, stdin)) != -1) {
if (readlen == 0)
continue;
if (readlen == 1 && path[0] == '\n') {
ret = fdupes_dedupe();
if (ret)
return ret;
free_all_filerecs();
continue;
}
if (readlen > 0 && path[readlen - 1] == '\n') {
path[--readlen] = '\0';
}
if (readlen > PATH_MAX - 1) {
eprintf("Path max exceeded: %s\n", path);
continue;
}
add_file_fdupes(path);
}
return 0;
}
static int add_files_from_stdin(struct dbhandle *db)
{
_cleanup_(freep) char *path = NULL;
size_t pathlen = 0;
ssize_t readlen;
while ((readlen = getline(&path, &pathlen, stdin)) != -1) {
if (readlen == 0)
continue;
if (readlen > 0 && path[readlen - 1] == '\n') {
path[--readlen] = '\0';
}
if (readlen > PATH_MAX - 1) {
eprintf("Path max exceeded: %s\n", path);
continue;
}
if (scan_file(path, db)) {
eprintf("Error: cannot add %s into the lookup list\n",
path);
return 1;
}
}
return 0;
}
static int scan_files_from_cmdline(int numfiles, char **files, struct dbhandle *db)
{
int i;
for (i = 0; i < numfiles; i++) {
char *name = files[i];
if (scan_file(name, db)) {
eprintf("Error: cannot scan %s\n", name);
return 1;
}
}
return 0;
}
static void help(void)
{
execlp("man", "man", "8", "duperemove", NULL);
}
/*
* Ok this is doing more than just parsing options.
*/
static int parse_options(int argc, char **argv, int *filelist_idx)
{
int c, numfiles;
bool read_hashes = false;
bool write_hashes = false;
bool update_hashes = false;
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 },
{ "cpu-threads", 1, NULL, CPU_THREADS_OPTION },
{ "skip-zeroes", 0, NULL, SKIP_ZEROES_OPTION },
{ "fdupes", 0, NULL, FDUPES_OPTION },
{ "dedupe-options=", 1, NULL, DEDUPE_OPTS_OPTION },
{ "quiet", 0, NULL, QUIET_OPTION },
{ "exclude", 1, NULL, EXCLUDE_OPTION },
{ "batchsize", 1, NULL, BATCH_SIZE_OPTION },
{ NULL, 0, NULL, 0}
};
if (argc < 2) {
help(); /* Never returns */
}
while ((c = getopt_long(argc, argv, "b:vdDrh?LRqB:", long_ops, NULL))
!= -1) {
switch (c) {
case 'b':
blocksize = parse_size(optarg);
if (blocksize < MIN_BLOCKSIZE ||
blocksize > MAX_BLOCKSIZE){
eprintf("Error: Blocksize is bounded by %u and %u, %u found\n",
MIN_BLOCKSIZE, MAX_BLOCKSIZE, blocksize);
return EINVAL;
}
break;
case 'd':
case 'D':
options.run_dedupe += 1;
break;
case 'r':
options.recurse_dirs = true;
break;
case VERSION_OPTION:
print_version();
exit(0);
case DEBUG_OPTION:
debug = 1;
verbose = 1;
break;
case 'v':
verbose = 1;
break;
case 'h':
human_readable = 1;
break;
case WRITE_HASHES_OPTION:
write_hashes = true;
options.hashfile = strdup(optarg);
break;
case READ_HASHES_OPTION:
read_hashes = true;
options.hashfile = strdup(optarg);
break;
case HASHFILE_OPTION:
update_hashes = true;
options.hashfile = strdup(optarg);
break;
case IO_THREADS_OPTION:
options.io_threads = strtoul(optarg, NULL, 10);
if (!options.io_threads){
eprintf("Error: --io-threads must be "
"an integer, %s found\n", optarg);
return EINVAL;
}
break;
case CPU_THREADS_OPTION:
options.cpu_threads = strtoul(optarg, NULL, 10);
if (!options.cpu_threads){
eprintf("Error: --cpu-threads must be "
"an integer, %s found\n", optarg);
return EINVAL;
}
break;
case SKIP_ZEROES_OPTION:
options.skip_zeroes = true;
break;
case FDUPES_OPTION:
options.fdupes_mode = 1;
break;
case DEDUPE_OPTS_OPTION:
if (parse_dedupe_opts(optarg))
return EINVAL;
break;
case 'L':
list_only_opt = 1;
break;
case 'R':
rm_only_opt = 1;
break;
case QUIET_OPTION:
case 'q':
quiet = 1;
break;
case EXCLUDE_OPTION:
if (add_exclude_pattern(optarg))
eprintf("Error: cannot exclude %s\n", optarg);
break;
case BATCH_SIZE_OPTION:
case 'B':
options.batch_size = parse_size(optarg);
break;
case HELP_OPTION:
help();
break;
case '?':
default:
return 1;
}
}
numfiles = argc - optind;
if (options.only_whole_files && options.do_block_hash) {
eprintf("Error: using both only_whole_files and partial "
"options have no meaning\n");
return 1;
}
/* Filter out option combinations that don't make sense. */
if ((write_hashes + read_hashes + update_hashes) > 1) {
eprintf("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;
/*
* Always add the hashfile and its wal etc to the exclude list
* A wildcard would be easier but may exclude extra files silently,
* this would be confusing for the user.
*/
if (options.hashfile != NULL) {
char tmp[PATH_MAX + 10 ] = {0,};
add_exclude_pattern(options.hashfile);
snprintf(tmp, PATH_MAX + 9, "%s-wal", options.hashfile);
add_exclude_pattern(tmp);
snprintf(tmp, PATH_MAX + 9, "%s-shm", options.hashfile);
add_exclude_pattern(tmp);
}
if (read_hashes) {
if (numfiles) {
eprintf("Error: --read-hashes option does not take a "
"file list argument\n");
return 1;
}
goto out_nofiles;
}
if (options.fdupes_mode) {
if (read_hashes || write_hashes || update_hashes) {
eprintf("Error: cannot mix hashfile option with "
"--fdupes option\n");
return 1;
}
if (numfiles) {
eprintf("Error: fdupes option does not take a file "
"list argument\n");
return 1;
}
/* rest of fdupes mode is implemented in main() */
return 0;
}
*filelist_idx = optind;
if (numfiles == 1 && strcmp(argv[optind], "-") == 0)
stdin_filelist = 1;
if (list_only_opt && rm_only_opt) {
eprintf("Error: Can not mix '-L' and '-R' options.\n");
return 1;
}
if (list_only_opt || rm_only_opt) {
if (!options.hashfile || use_hashfile == H_WRITE) {
eprintf("Error: --hashfile= option is required "
"with '-L' or -R.\n");
return 1;
}
if (list_only_opt && numfiles) {
eprintf("Error: -L option do not take "
"a file list argument\n");
return 1;
}
}
if (!(options.fdupes_mode || list_only_opt)
&& numfiles == 0) {
eprintf("Error: a file list argument is required.\n");
return 1;
}
out_nofiles:
return 0;
}
static void print_header(void)
{
vprintf("Using %uK blocks\n", blocksize / 1024);
vprintf("Using %s hashing\n", options.do_block_hash ? "block+extent" : "extent");
#ifdef DEBUG_BUILD
printf("Debug build, performance may be impacted.\n");
#endif
qprintf("Gathering file list...\n");
}
static void __process_duplicates(struct dbhandle *db, unsigned int seq)
{
int ret;
struct results_tree res;
struct hash_tree dups_tree;
init_results_tree(&res);
init_hash_tree(&dups_tree);
qprintf("Loading only identical files from hashfile.\n");
ret = dbfile_load_same_files(db, &res, seq + 1);
if (ret)
goto out;
if (options.run_dedupe)
dedupe_results(&res, true);
else
print_dupes_table(&res, true);
/* Reset the results_tree before loading extents or blocks */
free_results_tree(&res);
if (!options.only_whole_files) {
init_results_tree(&res);
qprintf("Loading only duplicated hashes from hashfile.\n");
ret = dbfile_load_extent_hashes(db, &res, seq + 1);
if (ret)
goto out;
printf("Found %llu identical extents.\n", res.num_extents);
if (options.do_block_hash) {
ret = dbfile_load_block_hashes(db, &dups_tree, seq + 1);
if (ret)
goto out;
ret = find_additional_dedupe(&res);
if (ret)
goto out;
}
if (options.run_dedupe)
dedupe_results(&res, false);
else
print_dupes_table(&res, false);
}
out:
free_results_tree(&res);
free_hash_tree(&dups_tree);
}
static void process_duplicates(struct dbhandle *db)
{
unsigned int max = get_max_dedupe_seq(db);
/* Spawn a dedicated thread pool to block-based lookup */
if (options.do_block_hash)
extents_search_init();
for (unsigned int i = dedupe_seq; i < max; i++) {
/* Drop all filerecs from the previous iteration. Needed filerecs will be
* recreated by __process_duplicates()
*/
free_all_filerecs();
__process_duplicates(db, i);
if (options.run_dedupe) {
/*
* Bump dedupe_seq, this effectively marks the files
* in our hashfile as having been through dedupe.
*/
dedupe_seq++;
dbfile_cfg.dedupe_seq = dedupe_seq;
dbfile_cfg.blocksize = blocksize;
dbfile_sync_config(db, &dbfile_cfg);
}
}
if (options.do_block_hash)
extents_search_free();
}
static int scan_files(int argc, char **argv, int filelist_idx, struct dbhandle *db)
{
int ret;
filescan_init();
if (!quiet)
pscan_run();
if (stdin_filelist)
ret = add_files_from_stdin(db);
else
ret = scan_files_from_cmdline(argc - filelist_idx,
&argv[filelist_idx], db);
pscan_finish_listing();
filescan_free();
if (!quiet)
pscan_join();
if (ret)
return ret;
/*
* Sync the locked filesystem informations in the hashfile
*/
fs_get_locked_uuid(&(dbfile_cfg.fs_uuid));
ret = dbfile_sync_config(db, &dbfile_cfg);
if (ret)
return ret;
return 0;
}
int main(int argc, char **argv)
{
int ret, filelist_idx = 0;
_cleanup_(sqlite3_close_cleanup) struct dbhandle *db = NULL;
char stdbuf[BUFSIZ];
setvbuf(stdout, stdbuf, _IOLBF, BUFSIZ);
init_filerec();
/* Set the default CPU limits before parsing the user options */
get_num_cpus(&(options.cpu_threads), &(options.io_threads));
ret = parse_options(argc, argv, &filelist_idx);
if (ret) {
exit(1);
}
/* Allow larger than unusal amount of open files. On linux
* this should bw increase form 1K to 512K open files
* simultaneously.
*
* On multicore SSD machines it's not hard to get to 1K open
* files.
*/
increase_limits();
if (options.fdupes_mode)
return process_fdupes();
if (list_only_opt)
return list_db_files(options.hashfile);
else if (rm_only_opt)
return rm_db_files(argc - filelist_idx, &argv[filelist_idx]);
db = dbfile_open_handle(options.hashfile);
if (!db)
goto out;
dbfile_set_gdb(db);
ret = dbfile_get_config(db->db, &dbfile_cfg);
if (ret)
goto out;
dedupe_seq = dbfile_cfg.dedupe_seq;
print_header();
if (use_hashfile == H_WRITE || use_hashfile == H_UPDATE) {
ret = dbfile_prune_unscanned_files(db);
if (ret) {
eprintf("Unable to prune unscanned files\n");
goto out;
}
ret = scan_files(argc, argv, filelist_idx, db);
if (ret)
goto out;
qprintf("Hashfile \"%s\" written\n",
options.hashfile);
}
if (use_hashfile == H_READ || use_hashfile == H_UPDATE)
process_duplicates(db);
out:
free_all_filerecs();
#ifdef DEBUG_BUILD
print_mem_stats();
#else
if (ret == ENOMEM || debug)
print_mem_stats();
#endif
return ret;
}
|