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.6"
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 /**********************************************************************/
98 struct file_with_size {
102 struct file_with_size *next;
103 int group_id, dir_id;
106 void file_list_delete(struct file_with_size *head) {
107 struct file_with_size *next;
110 free(head->filename);
116 int file_list_length(struct file_with_size *head) {
125 /**********************************************************************/
127 int same_content(struct file_with_size *f1, struct file_with_size *f2,
128 char *buffer1, char *buffer2) {
129 int fd1, fd2, s1, s2;
131 fd1 = open(f1->filename, O_RDONLY);
132 fd2 = open(f2->filename, O_RDONLY);
134 if(fd1 >= 0 && fd2 >= 0) {
136 s1 = read(fd1, buffer1, READ_BUFFER_SIZE);
137 s2 = read(fd2, buffer2, READ_BUFFER_SIZE);
139 if(s1 < 0 || s2 < 0) {
151 if(memcmp(buffer1, buffer2, s1)) {
159 "Different read size without error on files of same size.\n");
167 "Can not open \"%s\" error: %s\n",
174 "Can not open \"%s\" error: %s\n",
182 int same_files(struct file_with_size *f1, struct file_with_size *f2,
183 char *buffer1, char *buffer2) {
184 if(ignore_same_inode && f1->inode == f2->inode) {
187 return f1->size == f2->size && same_content(f1, f2, buffer1, buffer2);
190 /**********************************************************************/
192 struct file_with_size *scan_directory(struct file_with_size *tail,
195 struct dirent *dir_e;
197 struct file_with_size *tmp;
198 char subname[PATH_MAX + 1];
200 if(lstat(name, &sb) != 0) {
201 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
205 if(S_ISLNK(sb.st_mode)) {
212 while((dir_e = readdir(dir))) {
213 if(!ignore_entry(dir_e->d_name)) {
214 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
215 tail = scan_directory(tail, subname);
220 if(S_ISREG(sb.st_mode)) {
221 if(!ignore_entry(name)) {
222 if(!ignore_empty_files || sb.st_size > 0) {
223 tmp = safe_malloc(sizeof(struct file_with_size));
225 tmp->filename = strdup(name);
226 tmp->size = sb.st_size;
227 tmp->inode = sb.st_ino;
239 void print_file(struct file_with_size *node) {
240 char tmp[PATH_MAX + 1];
242 if(realpath(node->filename, tmp)) {
244 printf("%d %s\n", node->group_id, tmp);
249 printf("Can not get the realpath of \"%s\": %s\n",
256 printf("%d %s\n", node->group_id, node->filename);
258 printf("%s\n", node->filename);
263 int compare_nodes(const void *x1, const void *x2) {
264 const struct file_with_size **f1, **f2;
266 f1 = (const struct file_with_size **) x1;
267 f2 = (const struct file_with_size **) x2;
269 if((*f1)->group_id < (*f2)->group_id) {
271 } else if((*f1)->group_id > (*f2)->group_id) {
274 if((*f1)->dir_id < (*f2)->dir_id) {
276 } else if((*f1)->dir_id > (*f2)->dir_id) {
285 void print_result(struct file_with_size *list1, struct file_with_size *list2) {
286 struct file_with_size *node1, *node2;
287 struct file_with_size **nodes;
291 for(node1 = list1; node1; node1 = node1->next) {
292 if(node1->group_id >= 0) { nb++; }
296 for(node2 = list2; node2; node2 = node2->next) {
297 if(node2->group_id >= 0) { nb++; }
301 nodes = safe_malloc(nb * sizeof(struct file_with_size *));
304 for(node1 = list1; node1; node1 = node1->next) {
305 if(node1->group_id >= 0) {
311 for(node2 = list2; node2; node2 = node2->next) {
312 if(node2->group_id >= 0) {
318 qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes);
320 for(n = 0; n < nb; n++) {
321 if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
324 print_file(nodes[n]);
330 void print_progress(int max, int n, int *pp) {
333 if(show_progress && tty_width > 0) {
334 width = tty_width - 7;
335 p = (width * n) / (max - 1);
337 for(k = 0; k < p; k++) {
338 fprintf(stderr, "+");
340 for(; k < width; k++) {
341 fprintf(stderr, "-");
344 p = (100 * n) / (max - 1);
345 fprintf(stderr, " [%3d%%]\r", p);
350 void start(const char *dirname1, const char *dirname2) {
351 struct file_with_size *list1, *list2;
352 struct file_with_size *node1, *node2;
354 int nb_groups, nb_nodes;
355 int list1_length, previous_progress;
357 char *buffer1 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
358 char *buffer2 = safe_malloc(sizeof(char) * READ_BUFFER_SIZE);
363 fprintf(stderr, "Scanning %s ... ", dirname1);
366 list1 = scan_directory(0, dirname1);
369 if(strncmp(dirname2, "not:", 4) == 0) {
371 /* groups are not computed in the not: mode */
374 } else if(strncmp(dirname2, "and:", 4) == 0) {
378 fprintf(stderr, "%s ... ", dirname2);
380 list2 = scan_directory(0, dirname2);
386 fprintf(stderr, "done.\n");
390 previous_progress = -1;
392 list1_length = file_list_length(list1);
395 for(node1 = list1; node1; node1 = node1->next) {
396 print_progress(list1_length, nb_nodes, &previous_progress);
401 for(node2 = list2; !found && node2; node2 = node2->next) {
402 if(same_files(node1, node2, buffer1, buffer2)) {
409 printf("%s\n", realpath(node1->filename, 0));
411 printf("%s\n", node1->filename);
417 for(node1 = list1; node1; node1 = node1->next) {
418 print_progress(list1_length, nb_nodes, &previous_progress);
421 for(node2 = list2; node2; node2 = node2->next) {
422 if(node1->group_id < 0 || node2->group_id < 0) {
423 if(same_files(node1, node2, buffer1, buffer2)) {
424 if(node1->group_id < 0) {
425 if(node2->group_id >= 0) {
426 node1->group_id = node2->group_id;
428 node1->group_id = nb_groups;
433 if(node2->group_id < 0) {
434 node2->group_id = node1->group_id;
444 fprintf(stderr, "\n");
448 print_result(list1, list2);
449 file_list_delete(list1);
450 file_list_delete(list2);
452 print_result(list1, 0);
453 file_list_delete(list1);
460 void print_help(FILE *out) {
461 fprintf(out, "Usage: finddup [OPTION]... DIR1 [[and:|not:]DIR2]\n");
462 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
463 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");
465 fprintf(out, " -h show this help\n");
466 fprintf(out, " -d ignore dot files and directories\n");
467 fprintf(out, " -0 ignore empty files\n");
468 fprintf(out, " -c do not show which files in DIR2 corresponds to those in DIR1\n");
469 fprintf(out, " -g do not show the file groups\n");
470 fprintf(out, " -p show progress\n");
471 fprintf(out, " -r show the real file paths\n");
472 fprintf(out, " -i consider files with same inode as different\n");
474 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
477 /**********************************************************************/
479 int main(int argc, char **argv) {
481 struct file_with_size *root;
486 setlocale (LC_ALL, "");
489 c = getopt(argc, argv, "hircgd0p");
506 ignore_empty_files = 1;
514 ignore_same_inode = 1;
535 isatty(STDOUT_FILENO) &&
536 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
537 tty_width = win.ws_col;
540 if(optind + 2 == argc) {
541 start(argv[optind], argv[optind + 1]);
542 } else if(optind + 1 == argc) {
543 ignore_same_inode = 1;
544 start(argv[optind], 0);