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 #include <openssl/md5.h>
47 /* 1M really helps compared to 64k */
48 #define READ_BUFFER_SIZE (1024 * 1024)
50 typedef int64_t size_sum_t;
52 /* Yeah, global variables! */
54 int ignore_dotfiles = 0; /* 1 means ignore files and directories
55 starting with a dot */
57 int ignore_empty_files = 0; /* 1 means ignore empty files */
59 int show_realpaths = 0; /* 1 means ignore files and directories
60 starting with a dot */
62 int show_progress = 0; /* 1 means show a progress bar when we are in a
65 int show_hits = 1; /* 1 means to show the files from dir2
66 corresponding to the ones from dir1 */
68 int show_groups = 1; /* 1 means to show the group IDs when printing
71 int same_inodes_are_different = 0; /* 1 means that comparison between
72 two files with same inode will
76 int use_md5 = 0; /* 1 means we keep an MD5 signature for each file */
79 /********************************************************************/
81 /* malloc with error checking. */
83 void *safe_malloc(size_t n) {
86 printf("Can not allocate memory: %s\n", strerror(errno));
92 /********************************************************************/
94 int ignore_entry(const char *name) {
96 strcmp(name, ".") == 0 ||
97 strcmp(name, "..") == 0 ||
98 (ignore_dotfiles && name[0] == '.' && name[1] != '/');
101 /**********************************************************************/
104 struct file_node *next;
108 int group_id; /* one per identical file content */
109 int dir_id; /* 1 for DIR1, and 2 for DIR2 */
112 unsigned char md5[MD5_DIGEST_LENGTH];
116 void file_list_delete(struct file_node *head) {
117 struct file_node *next;
126 int file_list_length(struct file_node *head) {
135 /**********************************************************************/
137 int same_content(struct file_node *f1, struct file_node *f2,
138 char *buffer1, char *buffer2) {
139 int fd1, fd2, s1, s2;
145 if(f1->md5_computed && f2->md5_computed) {
146 if(!memcmp(f1->md5, f2->md5, MD5_DIGEST_LENGTH)) {
150 if(!f1->md5_computed) {
153 if(!f2->md5_computed) {
160 fd1 = open(f1->name, O_RDONLY);
161 fd2 = open(f2->name, O_RDONLY);
163 if(fd1 >= 0 && fd2 >= 0) {
165 s1 = read(fd1, buffer1, READ_BUFFER_SIZE);
166 s2 = read(fd2, buffer2, READ_BUFFER_SIZE);
168 if(s1 < 0 || s2 < 0) {
180 if(!f1->md5_computed) {
181 MD5_Final(f1->md5, &c1);
182 f1->md5_computed = 1;
184 if(!f2->md5_computed) {
185 MD5_Final(f2->md5, &c2);
186 f2->md5_computed = 1;
192 if(memcmp(buffer1, buffer2, s1)) {
199 if(!f1->md5_computed) {
200 MD5_Update(&c1, buffer1, s1);
202 if(!f2->md5_computed) {
203 MD5_Update(&c2, buffer2, s2);
210 "Different read size without error on files of same size.\n");
218 "Can not open \"%s\" error: %s\n",
225 "Can not open \"%s\" error: %s\n",
234 int same_files(struct file_node *f1, struct file_node *f2,
235 char *buffer1, char *buffer2) {
236 if(same_inodes_are_different && f1->inode == f2->inode) {
240 return f1->size == f2->size && same_content(f1, f2, buffer1, buffer2);
243 /**********************************************************************/
245 struct file_node *scan_directory(struct file_node *tail, const char *name) {
247 struct dirent *dir_e;
249 struct file_node *tmp;
250 char subname[PATH_MAX + 1];
252 if(lstat(name, &sb) != 0) {
253 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
257 if(S_ISLNK(sb.st_mode)) {
264 while((dir_e = readdir(dir))) {
265 if(!ignore_entry(dir_e->d_name)) {
266 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
267 tail = scan_directory(tail, subname);
272 if(S_ISREG(sb.st_mode)) {
273 if(!ignore_entry(name)) {
274 if(!ignore_empty_files || sb.st_size > 0) {
275 tmp = safe_malloc(sizeof(struct file_node));
277 tmp->name = strdup(name);
278 tmp->size = sb.st_size;
279 tmp->inode = sb.st_ino;
283 tmp->md5_computed = 0;
294 void print_file(struct file_node *node) {
295 char tmp[PATH_MAX + 1];
297 if(realpath(node->name, tmp)) {
299 printf("%d %s\n", node->group_id, tmp);
304 printf("Can not get the realpath of \"%s\": %s\n",
311 printf("%d %s\n", node->group_id, node->name);
313 printf("%s\n", node->name);
318 int compare_nodes(const void *x1, const void *x2) {
319 const struct file_node **f1, **f2;
321 f1 = (const struct file_node **) x1;
322 f2 = (const struct file_node **) x2;
324 if((*f1)->group_id < (*f2)->group_id) {
326 } else if((*f1)->group_id > (*f2)->group_id) {
329 if((*f1)->dir_id < (*f2)->dir_id) {
331 } else if((*f1)->dir_id > (*f2)->dir_id) {
340 void print_result(struct file_node *list1, struct file_node *list2) {
341 struct file_node *node1, *node2;
342 struct file_node **nodes;
346 for(node1 = list1; node1; node1 = node1->next) {
347 if(node1->group_id >= 0) { nb++; }
351 for(node2 = list2; node2; node2 = node2->next) {
352 if(node2->group_id >= 0) { nb++; }
356 nodes = safe_malloc(nb * sizeof(struct file_node *));
359 for(node1 = list1; node1; node1 = node1->next) {
360 if(node1->group_id >= 0) {
366 for(node2 = list2; node2; node2 = node2->next) {
367 if(node2->group_id >= 0) {
373 qsort(nodes, nb, sizeof(struct file_node *), compare_nodes);
375 for(n = 0; n < nb; n++) {
376 if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
379 print_file(nodes[n]);
385 void print_progress(int max, int n, int *pp) {
387 int width, tty_width;
391 isatty(STDOUT_FILENO) &&
392 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
393 tty_width = win.ws_col;
394 width = tty_width - 7;
395 p = (width * n) / (max - 1);
397 for(k = 0; k < p; k++) {
398 fprintf(stderr, "+");
400 for(; k < width; k++) {
401 fprintf(stderr, "-");
404 p = (100 * n) / (max - 1);
405 fprintf(stderr, " [%3d%%]\r", p);
410 void start(const char *dirname1, const char *dirname2) {
411 struct file_node *list1, *list2;
412 struct file_node *node1, *node2;
414 int nb_groups, nb_nodes;
415 int list1_length, previous_progress;
417 char *buffer1 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
418 char *buffer2 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
423 fprintf(stderr, "Scanning %s ... ", dirname1);
426 list1 = scan_directory(0, dirname1);
429 if(strncmp(dirname2, "not:", 4) == 0) {
431 /* groups are not computed in the not: mode */
434 } else if(strncmp(dirname2, "and:", 4) == 0) {
438 fprintf(stderr, "%s ... ", dirname2);
440 list2 = scan_directory(0, dirname2);
446 fprintf(stderr, "done.\n");
450 previous_progress = -1;
452 list1_length = file_list_length(list1);
455 for(node1 = list1; node1; node1 = node1->next) {
456 print_progress(list1_length, nb_nodes, &previous_progress);
461 for(node2 = list2; !found && node2; node2 = node2->next) {
462 if(same_files(node1, node2, buffer1, buffer2)) {
469 printf("%s\n", realpath(node1->name, 0));
471 printf("%s\n", node1->name);
477 for(node1 = list1; node1; node1 = node1->next) {
478 print_progress(list1_length, nb_nodes, &previous_progress);
481 for(node2 = list2; node2; node2 = node2->next) {
482 if(node1->group_id < 0 || node2->group_id < 0) {
483 if(same_files(node1, node2, buffer1, buffer2)) {
484 if(node1->group_id < 0) {
485 if(node2->group_id >= 0) {
486 node1->group_id = node2->group_id;
488 node1->group_id = nb_groups;
493 if(node2->group_id < 0) {
494 node2->group_id = node1->group_id;
504 fprintf(stderr, "\n");
508 print_result(list1, list2);
509 file_list_delete(list1);
510 file_list_delete(list2);
512 print_result(list1, 0);
513 file_list_delete(list1);
520 void usage(FILE *out) {
521 fprintf(out, "Usage: finddup [OPTION]... DIR1 [[and:|not:]DIR2]\n");
522 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
523 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");
525 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789*/
526 fprintf(out, " -h, --help show this help\n");
527 fprintf(out, " -d, --ignore-dots ignore dot files and directories\n");
528 fprintf(out, " -0, --ignore-empty ignore empty files\n");
529 fprintf(out, " -c, --hide-matchings do not show which files in DIR2 corresponds to\n");
530 fprintf(out, " those in DIR1\n");
531 fprintf(out, " -g, --no-group-ids do not show the file groups\n");
532 fprintf(out, " -p, --show-progress show progress\n");
533 fprintf(out, " -r, --real-paths show the real file paths\n");
534 fprintf(out, " -i, --same-inodes-are-different\n");
535 fprintf(out, " consider files with same inode as different\n");
537 fprintf(out, " -m, --md5 use MD5 hashing\n");
540 fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
543 /**********************************************************************/
545 static struct option long_options[] = {
546 { "help", no_argument, 0, 'h' },
547 { "same-inodes-are-different", no_argument, 0, 'i' },
548 { "real-paths", no_argument, 0, 'r' },
549 { "hide-matchings", no_argument, 0, 'c' },
550 { "no-group-ids", no_argument, 0, 'g' },
551 { "ignore-dots", no_argument, 0, 'd' },
552 { "ignore-empty", no_argument, 0, '0' },
553 { "show-progress", no_argument, 0, 'p' },
554 { "md5", no_argument, 0, 'm' },
558 int main(int argc, char **argv) {
561 setlocale (LC_ALL, "");
563 while ((c = getopt_long(argc, argv, "hircgd0pm",
564 long_options, NULL)) != -1) {
578 ignore_empty_files = 1;
586 same_inodes_are_different = 1;
606 "finddup has not been compiled with MD5 hashing.\n");
618 if(optind + 2 == argc) {
619 start(argv[optind], argv[optind + 1]);
620 } else if(optind + 1 == argc) {
621 same_inodes_are_different = 1;
622 start(argv[optind], 0);