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 "1.1"
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 int sort_by_time = 0; /* 1 means to sort files in each group according
75 to the modification time */
77 /********************************************************************/
79 /* malloc with error checking. */
81 void *safe_malloc(size_t n) {
85 "finddup: Can not allocate memory: %s\n", strerror(errno));
91 /********************************************************************/
93 int ignore_entry(const char *name) {
95 strcmp(name, ".") == 0 ||
96 strcmp(name, "..") == 0 ||
97 (ignore_dotfiles && name[0] == '.' && name[1] != '/');
100 /**********************************************************************/
103 struct file_node *next;
106 time_t atime, mtime, ctime;
108 int group_id; /* one per identical file content */
109 int dir_id; /* 1 for DIR1, and 2 for DIR2 */
112 void file_list_delete(struct file_node *head) {
113 struct file_node *next;
122 int file_list_length(struct file_node *head) {
131 /**********************************************************************/
133 int same_content(struct file_node *f1, struct file_node *f2,
134 char *buffer1, char *buffer2) {
135 int fd1, fd2, s1, s2;
137 fd1 = open(f1->name, O_RDONLY);
138 fd2 = open(f2->name, O_RDONLY);
140 if(fd1 >= 0 && fd2 >= 0) {
142 s1 = read(fd1, buffer1, READ_BUFFER_SIZE);
143 s2 = read(fd2, buffer2, READ_BUFFER_SIZE);
145 if(s1 < 0 || s2 < 0) {
157 if(memcmp(buffer1, buffer2, s1)) {
158 /* printf("size_to_read = %d\n", size_to_read); */
166 "finddup: Different read size without error on files of same size.\n");
174 "finddup: Can not open \"%s\" error: %s\n",
181 "finddup: Can not open \"%s\" error: %s\n",
190 int same_files(struct file_node *f1, struct file_node *f2,
191 char *buffer1, char *buffer2) {
192 if(same_inodes_are_different && f1->inode == f2->inode) {
196 return f1->size == f2->size && same_content(f1, f2, buffer1, buffer2);
199 /**********************************************************************/
201 struct file_node *scan_directory_rec(struct file_node *tail, const char *name) {
203 struct dirent *dir_e;
205 struct file_node *tmp;
206 char subname[PATH_MAX + 1];
208 if(lstat(name, &sb) != 0) {
209 fprintf(stderr, "finddup: Can not stat \"%s\": %s\n", name, strerror(errno));
213 if(S_ISLNK(sb.st_mode)) {
220 while((dir_e = readdir(dir))) {
221 if(!ignore_entry(dir_e->d_name)) {
222 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
223 tail = scan_directory_rec(tail, subname);
228 if(S_ISREG(sb.st_mode)) {
229 if(!ignore_entry(name)) {
230 if(!ignore_empty_files || sb.st_size > 0) {
231 tmp = safe_malloc(sizeof(struct file_node));
233 tmp->name = strdup(name);
234 tmp->size = sb.st_size;
235 tmp->atime = sb.st_atime;
236 tmp->mtime = sb.st_mtime;
237 tmp->ctime = sb.st_ctime;
238 tmp->inode = sb.st_ino;
250 struct file_node *scan_directory(struct file_node *tail, const char *name) {
251 struct file_node *result;
255 fprintf(stderr, "Scanning '%s' ... ", name);
258 result = scan_directory_rec(tail, name);
259 length = file_list_length(result);
262 fprintf(stderr, "done (%d file%s).\n",
263 length, (length > 1 ? "s" : ""));
270 void print_file(struct file_node *node) {
271 char tmp[PATH_MAX + 1];
273 if(realpath(node->name, tmp)) {
275 printf("%d %s\n", node->group_id, tmp);
281 "finddup: Can not get the realpath of \"%s\": %s\n",
288 printf("%d %s\n", node->group_id, node->name);
290 printf("%s\n", node->name);
295 int compare_nodes(const void *x1, const void *x2) {
296 const struct file_node **f1, **f2;
298 f1 = (const struct file_node **) x1;
299 f2 = (const struct file_node **) x2;
301 if((*f1)->group_id < (*f2)->group_id) {
303 } else if((*f1)->group_id > (*f2)->group_id) {
307 if((*f1)->mtime < (*f2)->mtime) {
309 } else if((*f1)->mtime > (*f2)->mtime) {
315 if((*f1)->dir_id < (*f2)->dir_id) {
317 } else if((*f1)->dir_id > (*f2)->dir_id) {
327 void print_result(struct file_node *list1, struct file_node *list2) {
328 struct file_node *node1, *node2;
329 struct file_node **nodes;
333 for(node1 = list1; node1; node1 = node1->next) {
334 if(node1->group_id >= 0) { nb++; }
338 for(node2 = list2; node2; node2 = node2->next) {
339 if(node2->group_id >= 0) { nb++; }
343 nodes = safe_malloc(nb * sizeof(struct file_node *));
346 for(node1 = list1; node1; node1 = node1->next) {
347 if(node1->group_id >= 0) {
353 for(node2 = list2; node2; node2 = node2->next) {
354 if(node2->group_id >= 0) {
360 qsort(nodes, nb, sizeof(struct file_node *), compare_nodes);
362 for(n = 0; n < nb; n++) {
363 if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
366 print_file(nodes[n]);
372 struct progress_state {
374 int nb_values, value;
378 void print_progress(struct progress_state *state) {
379 int position, k, normalizer;
381 char buffer[PROGRESS_BUFFER_SIZE];
384 normalizer = (state->nb_values > 1 ? state->nb_values - 1 : 1);
387 /* We use the previous bar_width to compute the position, so that
388 we avoid doing too many ioctls */
389 position = (state->bar_width * state->value) / normalizer;
390 if(state->bar_width <= 0 || position != state->last_position) {
391 if(!ioctl (STDERR_FILENO, TIOCGWINSZ, (char *) &win)) {
392 /* Something weird is going on if the previous test is wrong */
393 if(win.ws_col >= PROGRESS_BUFFER_SIZE - 3) {
394 state->bar_width = PROGRESS_BUFFER_SIZE - 10;
396 state->bar_width = win.ws_col - 7;
398 position = (state->bar_width * state->value) / normalizer;
399 state->last_position = position;
401 for(k = 0; k < position; k++) {
404 for(; k < state->bar_width; k++) {
408 /* We need four % because of the fprintf that follows */
409 sprintf(s, " [%3d%%%%]\r",
410 (100 * state->value) / normalizer);
412 fprintf(stderr, buffer);
418 void start(const char *dirname1, const char *dirname2) {
419 struct file_node *list1, *list2;
420 struct file_node *node1, *node2;
421 struct progress_state progress_state;
423 int nb_groups, nb_nodes;
424 int list1_length, previous_progress;
426 char *buffer1 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
427 char *buffer2 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
431 list1 = scan_directory(0, dirname1);
432 list1_length = file_list_length(list1);
435 if(strncmp(dirname2, "not:", 4) == 0) {
437 /* groups are not computed in the not: mode */
440 } else if(strncmp(dirname2, "and:", 4) == 0) {
443 list2 = scan_directory(0, dirname2);
450 "Now looking for identical files (this may take a while).\n");
454 previous_progress = -1;
457 progress_state.bar_width = -1;
458 progress_state.last_position = -1;
459 progress_state.nb_values = list1_length;
462 for(node1 = list1; node1; node1 = node1->next) {
463 progress_state.value = nb_nodes;
464 print_progress(&progress_state);
469 for(node2 = list2; !found && node2; node2 = node2->next) {
470 if(same_files(node1, node2, buffer1, buffer2)) {
477 printf("%s\n", realpath(node1->name, 0));
479 printf("%s\n", node1->name);
485 for(node1 = list1; node1; node1 = node1->next) {
486 progress_state.value = nb_nodes;
487 print_progress(&progress_state);
490 for(node2 = list2; node2; node2 = node2->next) {
491 if(node1->group_id < 0 || node2->group_id < 0) {
492 if(same_files(node1, node2, buffer1, buffer2)) {
493 if(node1->group_id < 0) {
494 if(node2->group_id >= 0) {
495 node1->group_id = node2->group_id;
497 node1->group_id = nb_groups;
502 if(node2->group_id < 0) {
503 node2->group_id = node1->group_id;
513 fprintf(stderr, "\n");
517 print_result(list1, list2);
518 file_list_delete(list1);
519 file_list_delete(list2);
521 print_result(list1, 0);
522 file_list_delete(list1);
529 void usage(FILE *out) {
530 fprintf(out, "Usage: finddup [OPTION]... [DIR1 [[and:|not:]DIR2]]\n");
531 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
532 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");
534 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789*/
535 fprintf(out, " -h, --help show this help\n");
536 fprintf(out, " -d, --ignore-dots ignore dot files and directories\n");
537 fprintf(out, " -0, --ignore-empty ignore empty files\n");
538 fprintf(out, " -c, --hide-matchings do not show which files in DIR2 corresponds to\n");
539 fprintf(out, " those in DIR1\n");
540 fprintf(out, " -g, --no-group-ids do not show the file groups\n");
541 fprintf(out, " -t, --time-sort sort according to modification time in each group\n");
542 fprintf(out, " -p, --show-progress show progress\n");
543 fprintf(out, " -r, --real-paths show the real file paths\n");
544 fprintf(out, " -i, --same-inodes-are-different\n");
545 fprintf(out, " consider files with same inode as different\n");
547 fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
550 /**********************************************************************/
552 static struct option long_options[] = {
553 { "version", no_argument, 0, 'v' },
554 { "help", no_argument, 0, 'h' },
555 { "same-inodes-are-different", no_argument, 0, 'i' },
556 { "real-paths", no_argument, 0, 'r' },
557 { "hide-matchings", no_argument, 0, 'c' },
558 { "no-group-ids", no_argument, 0, 'g' },
559 { "time-sort", no_argument, 0, 't' },
560 { "ignore-dots", no_argument, 0, 'd' },
561 { "ignore-empty", no_argument, 0, '0' },
562 { "show-progress", no_argument, 0, 'p' },
566 int main(int argc, char **argv) {
569 setlocale (LC_ALL, "");
571 while ((c = getopt_long(argc, argv, "vhircgtd0pm",
572 long_options, NULL)) != -1) {
576 printf("finddup version %s (%s)\n", VERSION_NUMBER, UNAME);
591 ignore_empty_files = 1;
599 same_inodes_are_different = 1;
624 if(!isatty(STDERR_FILENO)) {
628 if(optind + 2 == argc) {
629 start(argv[optind], argv[optind + 1]);
630 } else if(optind + 1 == argc) {
631 same_inodes_are_different = 1;
632 start(argv[optind], 0);
633 } else if(optind == argc) {
634 same_inodes_are_different = 1;