3 * finddup is a simple utility to find duplicated files, files common
4 * to several directories, or files present in one directory and not
7 * Copyright (c) 2010 Francois Fleuret
8 * Written by Francois Fleuret <francois@fleuret.org>
10 * This file is part of finddup.
12 * finddup is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 3 as
14 * published by the Free Software Foundation.
16 * finddup is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with finddup. If not, see <http://www.gnu.org/licenses/>.
26 #define VERSION_NUMBER "0.8"
30 #include <sys/types.h>
32 #include <sys/param.h>
39 #include <sys/ioctl.h>
44 /* 1M really helps compared to 64k */
45 #define READ_BUFFER_SIZE (1024 * 1024)
47 typedef int64_t size_sum_t;
49 /* Yeah, global variables! */
51 int ignore_dotfiles = 0; /* 1 means ignore files and directories
52 starting with a dot */
54 int ignore_empty_files = 0; /* 1 means ignore empty files */
56 int show_realpaths = 0; /* 1 means ignore files and directories
57 starting with a dot */
59 int show_progress = 0; /* 1 means show a progress bar when we are in a
62 int show_hits = 1; /* 1 means to show the files from dir2
63 corresponding to the ones from dir1 */
65 int show_groups = 1; /* 1 means to show the group IDs when printing
68 int ignore_same_inode = 0; /* 1 means that comparison between two file
69 with same inode will always be false */
71 int tty_width = -1; /* Positive value means what width to use to show
74 /********************************************************************/
76 /* malloc with error checking. */
78 void *safe_malloc(size_t n) {
81 printf("Can not allocate memory: %s\n", strerror(errno));
87 /********************************************************************/
89 int ignore_entry(const char *name) {
91 strcmp(name, ".") == 0 ||
92 strcmp(name, "..") == 0 ||
93 (ignore_dotfiles && name[0] == '.');
96 /**********************************************************************/
99 struct file_node *next;
103 int group_id; /* one per identical file content */
104 int dir_id; /* 1 for DIR1, and 2 for DIR2 */
107 void file_list_delete(struct file_node *head) {
108 struct file_node *next;
117 int file_list_length(struct file_node *head) {
126 /**********************************************************************/
128 int same_content(struct file_node *f1, struct file_node *f2,
129 char *buffer1, char *buffer2) {
130 int fd1, fd2, s1, s2;
132 fd1 = open(f1->name, O_RDONLY);
133 fd2 = open(f2->name, O_RDONLY);
135 if(fd1 >= 0 && fd2 >= 0) {
137 s1 = read(fd1, buffer1, READ_BUFFER_SIZE);
138 s2 = read(fd2, buffer2, READ_BUFFER_SIZE);
140 if(s1 < 0 || s2 < 0) {
152 if(memcmp(buffer1, buffer2, s1)) {
160 "Different read size without error on files of same size.\n");
168 "Can not open \"%s\" error: %s\n",
175 "Can not open \"%s\" error: %s\n",
183 int same_files(struct file_node *f1, struct file_node *f2,
184 char *buffer1, char *buffer2) {
185 if(ignore_same_inode && f1->inode == f2->inode) {
188 return f1->size == f2->size && same_content(f1, f2, buffer1, buffer2);
191 /**********************************************************************/
193 struct file_node *scan_directory(struct file_node *tail,
196 struct dirent *dir_e;
198 struct file_node *tmp;
199 char subname[PATH_MAX + 1];
201 if(lstat(name, &sb) != 0) {
202 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
206 if(S_ISLNK(sb.st_mode)) {
213 while((dir_e = readdir(dir))) {
214 if(!ignore_entry(dir_e->d_name)) {
215 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
216 tail = scan_directory(tail, subname);
221 if(S_ISREG(sb.st_mode)) {
222 if(!ignore_entry(name)) {
223 if(!ignore_empty_files || sb.st_size > 0) {
224 tmp = safe_malloc(sizeof(struct file_node));
226 tmp->name = strdup(name);
227 tmp->size = sb.st_size;
228 tmp->inode = sb.st_ino;
240 void print_file(struct file_node *node) {
241 char tmp[PATH_MAX + 1];
243 if(realpath(node->name, tmp)) {
245 printf("%d %s\n", node->group_id, tmp);
250 printf("Can not get the realpath of \"%s\": %s\n",
257 printf("%d %s\n", node->group_id, node->name);
259 printf("%s\n", node->name);
264 int compare_nodes(const void *x1, const void *x2) {
265 const struct file_node **f1, **f2;
267 f1 = (const struct file_node **) x1;
268 f2 = (const struct file_node **) x2;
270 if((*f1)->group_id < (*f2)->group_id) {
272 } else if((*f1)->group_id > (*f2)->group_id) {
275 if((*f1)->dir_id < (*f2)->dir_id) {
277 } else if((*f1)->dir_id > (*f2)->dir_id) {
286 void print_result(struct file_node *list1, struct file_node *list2) {
287 struct file_node *node1, *node2;
288 struct file_node **nodes;
292 for(node1 = list1; node1; node1 = node1->next) {
293 if(node1->group_id >= 0) { nb++; }
297 for(node2 = list2; node2; node2 = node2->next) {
298 if(node2->group_id >= 0) { nb++; }
302 nodes = safe_malloc(nb * sizeof(struct file_node *));
305 for(node1 = list1; node1; node1 = node1->next) {
306 if(node1->group_id >= 0) {
312 for(node2 = list2; node2; node2 = node2->next) {
313 if(node2->group_id >= 0) {
319 qsort(nodes, nb, sizeof(struct file_node *), compare_nodes);
321 for(n = 0; n < nb; n++) {
322 if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
325 print_file(nodes[n]);
331 void print_progress(int max, int n, int *pp) {
334 if(show_progress && tty_width > 0) {
335 width = tty_width - 7;
336 p = (width * n) / (max - 1);
338 for(k = 0; k < p; k++) {
339 fprintf(stderr, "+");
341 for(; k < width; k++) {
342 fprintf(stderr, "-");
345 p = (100 * n) / (max - 1);
346 fprintf(stderr, " [%3d%%]\r", p);
351 void start(const char *dirname1, const char *dirname2) {
352 struct file_node *list1, *list2;
353 struct file_node *node1, *node2;
355 int nb_groups, nb_nodes;
356 int list1_length, previous_progress;
359 char *buffer1 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
360 char *buffer2 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
365 if(isatty(STDOUT_FILENO) &&
366 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
367 tty_width = win.ws_col;
369 fprintf(stderr, "Scanning %s ... ", dirname1);
372 list1 = scan_directory(0, dirname1);
375 if(strncmp(dirname2, "not:", 4) == 0) {
377 /* groups are not computed in the not: mode */
380 } else if(strncmp(dirname2, "and:", 4) == 0) {
384 fprintf(stderr, "%s ... ", dirname2);
386 list2 = scan_directory(0, dirname2);
392 fprintf(stderr, "done.\n");
396 previous_progress = -1;
398 list1_length = file_list_length(list1);
401 for(node1 = list1; node1; node1 = node1->next) {
402 print_progress(list1_length, nb_nodes, &previous_progress);
407 for(node2 = list2; !found && node2; node2 = node2->next) {
408 if(same_files(node1, node2, buffer1, buffer2)) {
415 printf("%s\n", realpath(node1->name, 0));
417 printf("%s\n", node1->name);
423 for(node1 = list1; node1; node1 = node1->next) {
424 print_progress(list1_length, nb_nodes, &previous_progress);
427 for(node2 = list2; node2; node2 = node2->next) {
428 if(node1->group_id < 0 || node2->group_id < 0) {
429 if(same_files(node1, node2, buffer1, buffer2)) {
430 if(node1->group_id < 0) {
431 if(node2->group_id >= 0) {
432 node1->group_id = node2->group_id;
434 node1->group_id = nb_groups;
439 if(node2->group_id < 0) {
440 node2->group_id = node1->group_id;
450 fprintf(stderr, "\n");
454 print_result(list1, list2);
455 file_list_delete(list1);
456 file_list_delete(list2);
458 print_result(list1, 0);
459 file_list_delete(list1);
466 void print_help(FILE *out) {
467 fprintf(out, "Usage: finddup [OPTION]... DIR1 [[and:|not:]DIR2]\n");
468 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
469 fprintf(out, "Without DIR2, lists duplicated files found in DIR1. With DIR2, lists files common to both directories. With the not: prefix, lists files found in DIR1 which do not exist in DIR2. The and: prefix is the default and should be used only if you have a directory starting with 'not:'\n");
471 fprintf(out, " -h, --help\n");
472 fprintf(out, " show this help\n");
473 fprintf(out, " -d, --ignore-dots\n");
474 fprintf(out, " ignore dot files and directories\n");
475 fprintf(out, " -0, --ignore-empty\n");
476 fprintf(out, " ignore empty files\n");
477 fprintf(out, " -c, --hide-matchings\n");
478 fprintf(out, " do not show which files in DIR2 corresponds to those in DIR1\n");
479 fprintf(out, " -g, --no-group-ids\n");
480 fprintf(out, " do not show the file groups\n");
481 fprintf(out, " -p, --show-progress\n");
482 fprintf(out, " show progress\n");
483 fprintf(out, " -r, --real-paths\n");
484 fprintf(out, " show the real file paths\n");
485 fprintf(out, " -i, --same-inodes-are-different\n");
486 fprintf(out, " consider files with same inode as different\n");
488 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
491 /**********************************************************************/
493 static struct option long_options[] = {
494 { "help", no_argument, 0, 'h' },
495 { "same-inodes-are-different", no_argument, 0, 'i' },
496 { "real-paths", no_argument, 0, 'r' },
497 { "hide-matchings", no_argument, 0, 'c' },
498 { "no-group-ids", no_argument, 0, 'g' },
499 { "ignore-dots", no_argument, 0, 'd' },
500 { "ignore-empty", no_argument, 0, '0' },
501 { "show-progress", no_argument, 0, 'p' },
505 int main(int argc, char **argv) {
508 setlocale (LC_ALL, "");
510 while ((c = getopt_long(argc, argv, "hircgd0p",
511 long_options, NULL)) != -1) {
525 ignore_empty_files = 1;
533 ignore_same_inode = 1;
553 if(optind + 2 == argc) {
554 start(argv[optind], argv[optind + 1]);
555 } else if(optind + 1 == argc) {
556 ignore_same_inode = 1;
557 start(argv[optind], 0);