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.9"
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 #define PROGRESS_BUFFER_SIZE 1024
49 typedef int64_t size_sum_t;
51 /* Yeah, global variables! */
53 int ignore_dotfiles = 0; /* 1 means ignore files and directories
54 starting with a dot */
56 int ignore_empty_files = 0; /* 1 means ignore empty files */
58 int show_realpaths = 0; /* 1 means ignore files and directories
59 starting with a dot */
61 int show_progress = 0; /* 1 means show a progress bar when we are in a
64 int show_hits = 1; /* 1 means to show the files from dir2
65 corresponding to the ones from dir1 */
67 int show_groups = 1; /* 1 means to show the group IDs when printing
70 int same_inodes_are_different = 0; /* 1 means that comparison between
71 two files with same inode will
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] == '.' && name[1] != '/');
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)) {
153 /* printf("size_to_read = %d\n", size_to_read); */
161 "Different read size without error on files of same size.\n");
169 "Can not open \"%s\" error: %s\n",
176 "Can not open \"%s\" error: %s\n",
185 int same_files(struct file_node *f1, struct file_node *f2,
186 char *buffer1, char *buffer2) {
187 if(same_inodes_are_different && f1->inode == f2->inode) {
191 return f1->size == f2->size && same_content(f1, f2, buffer1, buffer2);
194 /**********************************************************************/
196 struct file_node *scan_directory(struct file_node *tail, const char *name) {
198 struct dirent *dir_e;
200 struct file_node *tmp;
201 char subname[PATH_MAX + 1];
203 if(lstat(name, &sb) != 0) {
204 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
208 if(S_ISLNK(sb.st_mode)) {
215 while((dir_e = readdir(dir))) {
216 if(!ignore_entry(dir_e->d_name)) {
217 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
218 tail = scan_directory(tail, subname);
223 if(S_ISREG(sb.st_mode)) {
224 if(!ignore_entry(name)) {
225 if(!ignore_empty_files || sb.st_size > 0) {
226 tmp = safe_malloc(sizeof(struct file_node));
228 tmp->name = strdup(name);
229 tmp->size = sb.st_size;
230 tmp->inode = sb.st_ino;
242 void print_file(struct file_node *node) {
243 char tmp[PATH_MAX + 1];
245 if(realpath(node->name, tmp)) {
247 printf("%d %s\n", node->group_id, tmp);
252 printf("Can not get the realpath of \"%s\": %s\n",
259 printf("%d %s\n", node->group_id, node->name);
261 printf("%s\n", node->name);
266 int compare_nodes(const void *x1, const void *x2) {
267 const struct file_node **f1, **f2;
269 f1 = (const struct file_node **) x1;
270 f2 = (const struct file_node **) x2;
272 if((*f1)->group_id < (*f2)->group_id) {
274 } else if((*f1)->group_id > (*f2)->group_id) {
277 if((*f1)->dir_id < (*f2)->dir_id) {
279 } else if((*f1)->dir_id > (*f2)->dir_id) {
288 void print_result(struct file_node *list1, struct file_node *list2) {
289 struct file_node *node1, *node2;
290 struct file_node **nodes;
294 for(node1 = list1; node1; node1 = node1->next) {
295 if(node1->group_id >= 0) { nb++; }
299 for(node2 = list2; node2; node2 = node2->next) {
300 if(node2->group_id >= 0) { nb++; }
304 nodes = safe_malloc(nb * sizeof(struct file_node *));
307 for(node1 = list1; node1; node1 = node1->next) {
308 if(node1->group_id >= 0) {
314 for(node2 = list2; node2; node2 = node2->next) {
315 if(node2->group_id >= 0) {
321 qsort(nodes, nb, sizeof(struct file_node *), compare_nodes);
323 for(n = 0; n < nb; n++) {
324 if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
327 print_file(nodes[n]);
333 struct progress_state {
335 int nb_values, value;
339 void print_progress(struct progress_state *state) {
342 char buffer[PROGRESS_BUFFER_SIZE];
346 /* We use the previous bar_width to compute the position, so that
347 we avoid doing too many ioctls */
348 position = (state->bar_width * state->value) / (state->nb_values - 1);
349 if(state->bar_width <= 0 || position != state->last_position) {
350 if(!ioctl (STDERR_FILENO, TIOCGWINSZ, (char *) &win)) {
351 /* Something weird is going on if the previous test is wrong */
352 if(win.ws_col >= PROGRESS_BUFFER_SIZE - 3) {
353 state->bar_width = PROGRESS_BUFFER_SIZE - 10;
355 state->bar_width = win.ws_col - 7;
357 position = (state->bar_width * state->value) / (state->nb_values - 1);
358 state->last_position = position;
360 for(k = 0; k < position; k++) {
363 for(; k < state->bar_width; k++) {
367 /* We need four % because of the fprintf that follows */
368 sprintf(s, " [%3d%%%%]\r",
369 (100 * state->value) / (state->nb_values - 1));
371 fprintf(stderr, buffer);
377 void start(const char *dirname1, const char *dirname2) {
378 struct file_node *list1, *list2;
379 struct file_node *node1, *node2;
380 struct progress_state progress_state;
382 int nb_groups, nb_nodes;
383 int list1_length, list2_length, previous_progress;
385 char *buffer1 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
386 char *buffer2 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
391 fprintf(stderr, "Scanning '%s' ... ", dirname1);
394 list1 = scan_directory(0, dirname1);
396 list1_length = file_list_length(list1);
399 fprintf(stderr, "done (%d file%s).\n",
400 list1_length, (list1_length > 1 ? "s" : ""));
404 if(strncmp(dirname2, "not:", 4) == 0) {
406 /* groups are not computed in the not: mode */
409 } else if(strncmp(dirname2, "and:", 4) == 0) {
413 fprintf(stderr, "Scanning '%s' ... ", dirname2);
415 list2 = scan_directory(0, dirname2);
417 list2_length = file_list_length(list2);
418 fprintf(stderr, "done (%d file%s).\n",
419 list2_length, (list2_length > 1 ? "s" : ""));
426 fprintf(stderr, "Now looking for identical files (this may take a while).\n");
430 previous_progress = -1;
433 progress_state.bar_width = -1;
434 progress_state.last_position = -1;
435 progress_state.nb_values = list1_length;
438 for(node1 = list1; node1; node1 = node1->next) {
439 progress_state.value = nb_nodes;
440 print_progress(&progress_state);
445 for(node2 = list2; !found && node2; node2 = node2->next) {
446 if(same_files(node1, node2, buffer1, buffer2)) {
453 printf("%s\n", realpath(node1->name, 0));
455 printf("%s\n", node1->name);
461 for(node1 = list1; node1; node1 = node1->next) {
462 progress_state.value = nb_nodes;
463 print_progress(&progress_state);
466 for(node2 = list2; node2; node2 = node2->next) {
467 if(node1->group_id < 0 || node2->group_id < 0) {
468 if(same_files(node1, node2, buffer1, buffer2)) {
469 if(node1->group_id < 0) {
470 if(node2->group_id >= 0) {
471 node1->group_id = node2->group_id;
473 node1->group_id = nb_groups;
478 if(node2->group_id < 0) {
479 node2->group_id = node1->group_id;
489 fprintf(stderr, "\n");
493 print_result(list1, list2);
494 file_list_delete(list1);
495 file_list_delete(list2);
497 print_result(list1, 0);
498 file_list_delete(list1);
505 void usage(FILE *out) {
506 fprintf(out, "Usage: finddup [OPTION]... [DIR1 [[and:|not:]DIR2]]\n");
507 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
508 fprintf(out, "Without DIR2, lists duplicated files found in DIR1, or the current directory if DIR1 is not provided. 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");
510 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789*/
511 fprintf(out, " -h, --help show this help\n");
512 fprintf(out, " -d, --ignore-dots ignore dot files and directories\n");
513 fprintf(out, " -0, --ignore-empty ignore empty files\n");
514 fprintf(out, " -c, --hide-matchings do not show which files in DIR2 corresponds to\n");
515 fprintf(out, " those in DIR1\n");
516 fprintf(out, " -g, --no-group-ids do not show the file groups\n");
517 fprintf(out, " -p, --show-progress show progress\n");
518 fprintf(out, " -r, --real-paths show the real file paths\n");
519 fprintf(out, " -i, --same-inodes-are-different\n");
520 fprintf(out, " consider files with same inode as different\n");
522 fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
525 /**********************************************************************/
527 static struct option long_options[] = {
528 { "help", no_argument, 0, 'h' },
529 { "same-inodes-are-different", no_argument, 0, 'i' },
530 { "real-paths", no_argument, 0, 'r' },
531 { "hide-matchings", no_argument, 0, 'c' },
532 { "no-group-ids", no_argument, 0, 'g' },
533 { "ignore-dots", no_argument, 0, 'd' },
534 { "ignore-empty", no_argument, 0, '0' },
535 { "show-progress", no_argument, 0, 'p' },
539 int main(int argc, char **argv) {
542 setlocale (LC_ALL, "");
544 while ((c = getopt_long(argc, argv, "hircgd0pm",
545 long_options, NULL)) != -1) {
559 ignore_empty_files = 1;
567 same_inodes_are_different = 1;
588 if(!isatty(STDERR_FILENO)) {
592 if(optind + 2 == argc) {
593 start(argv[optind], argv[optind + 1]);
594 } else if(optind + 1 == argc) {
595 same_inodes_are_different = 1;
596 start(argv[optind], 0);
597 } else if(optind == argc) {
598 same_inodes_are_different = 1;