3 * finddup is a simple utility find duplicated files, files common to
4 * several directories, or files present in one directory and not in
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 #define BUFFER_SIZE 4096
45 #define LARGE_BUFFER_SIZE 65536
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 /********************************************************************/
73 /* malloc with error checking. */
75 void *safe_malloc(size_t n) {
78 printf("Can not allocate memory: %s\n", strerror(errno));
84 /********************************************************************/
86 int ignore_entry(const char *name) {
88 strcmp(name, ".") == 0 ||
89 strcmp(name, "..") == 0 ||
90 (ignore_dotfiles && name[0] == '.');
93 void print_size_sum(size_sum_t s) {
95 char *a = tmp + sizeof(tmp)/sizeof(char);
108 /**********************************************************************/
110 struct file_with_size {
114 struct file_with_size *next;
118 void file_list_delete(struct file_with_size *head) {
119 struct file_with_size *next;
122 free(head->filename);
128 int file_list_length(struct file_with_size *head) {
137 /**********************************************************************/
139 int same_content(struct file_with_size *f1, struct file_with_size *f2) {
140 int fd1, fd2, s1, s2;
141 char buffer1[LARGE_BUFFER_SIZE], buffer2[LARGE_BUFFER_SIZE];
143 fd1 = open(f1->filename, O_RDONLY);
144 fd2 = open(f2->filename, O_RDONLY);
146 if(fd1 >= 0 && fd2 >= 0) {
148 s1 = read(fd1, buffer1, LARGE_BUFFER_SIZE);
149 s2 = read(fd2, buffer2, LARGE_BUFFER_SIZE);
151 if(s1 < 0 || s2 < 0) {
163 if(memcmp(buffer1, buffer2, s1)) {
171 "Different read size without error on files of same size.\n");
179 "Can not open \"%s\" error: %s\n",
186 "Can not open \"%s\" error: %s\n",
194 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
195 if(ignore_same_inode && f1->inode == f2->inode) {
198 return f1->size == f2->size && same_content(f1, f2);
201 /**********************************************************************/
203 struct file_with_size *scan_directory(struct file_with_size *tail,
206 struct dirent *dir_e;
208 struct file_with_size *tmp;
209 char subname[PATH_MAX + 1];
211 if(lstat(name, &sb) != 0) {
212 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
216 if(S_ISLNK(sb.st_mode)) {
223 while((dir_e = readdir(dir))) {
224 if(!ignore_entry(dir_e->d_name)) {
225 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
226 tail = scan_directory(tail, subname);
231 if(S_ISREG(sb.st_mode)) {
232 if(!ignore_entry(name)) {
233 if(!ignore_empty_files || sb.st_size > 0) {
234 tmp = safe_malloc(sizeof(struct file_with_size));
236 tmp->filename = strdup(name);
237 tmp->size = sb.st_size;
238 tmp->inode = sb.st_ino;
249 void print_file(struct file_with_size *node) {
250 char tmp[PATH_MAX + 1];
253 realpath(node->filename, tmp);
254 printf("%d %s\n", node->group_id, tmp);
256 realpath(node->filename, tmp);
261 printf("%d %s\n", node->group_id, node->filename);
263 printf("%s\n", node->filename);
268 int compare_nodes(const void *x1, const void *x2) {
269 const struct file_with_size **f1, **f2;
271 f1 = (const struct file_with_size **) x1;
272 f2 = (const struct file_with_size **) x2;
274 if((*f1)->group_id < (*f2)->group_id) {
276 } else if((*f1)->group_id > (*f2)->group_id) {
284 void print_result(struct file_with_size *list1, struct file_with_size *list2) {
285 struct file_with_size *node1, *node2;
286 struct file_with_size **nodes;
290 for(node1 = list1; node1; node1 = node1->next) {
291 if(node1->group_id >= 0) { nb++; }
295 for(node2 = list2; node2; node2 = node2->next) {
296 if(node2->group_id >= 0) { nb++; }
300 nodes = safe_malloc(nb * sizeof(struct file_with_size *));
303 for(node1 = list1; node1; node1 = node1->next) {
304 if(node1->group_id >= 0) {
310 for(node2 = list2; node2; node2 = node2->next) {
311 if(node2->group_id >= 0) {
317 qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes);
319 for(n = 0; n < nb; n++) {
320 if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
323 print_file(nodes[n]);
329 void start(const char *dirname1, const char *dirname2) {
330 struct file_with_size *list1, *list2;
331 struct file_with_size *node1, *node2;
338 fprintf(stderr, "Scanning %s ... ", dirname1);
341 list1 = scan_directory(0, dirname1);
344 if(strncmp(dirname2, "not:", 4) == 0) {
349 fprintf(stderr, "%s ... ", dirname2);
351 list2 = scan_directory(0, dirname2);
357 fprintf(stderr, "done.\n");
363 l1 = file_list_length(list1);
366 for(node1 = list1; node1; node1 = node1->next) {
370 fprintf(stderr, "%d%%\n", p);
378 for(node2 = list2; !found && node2; node2 = node2->next) {
379 if(same_files(node1, node2)) {
386 printf("%s\n", realpath(node1->filename, 0));
388 printf("%s\n", node1->filename);
394 for(node1 = list1; node1; node1 = node1->next) {
398 fprintf(stderr, "%d%%\n", p);
404 for(node2 = list2; node2; node2 = node2->next) {
405 if(node1->group_id < 0 || node2->group_id < 0) {
406 if(same_files(node1, node2)) {
407 if(node1->group_id < 0) {
408 if(node2->group_id >= 0) {
409 node1->group_id = node2->group_id;
415 if(node2->group_id < 0) {
416 node2->group_id = node1->group_id;
425 print_result(list1, list2);
426 file_list_delete(list1);
427 file_list_delete(list2);
429 print_result(list1, 0);
430 file_list_delete(list1);
434 void print_help(FILE *out) {
435 fprintf(out, "Usage: finddup [OPTION]... DIR1 [[not:]DIR2]\n");
436 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
437 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.\n");
439 fprintf(out, " -h show this help\n");
440 fprintf(out, " -d ignore dot files and directories\n");
441 fprintf(out, " -0 ignore empty files\n");
442 fprintf(out, " -c do not show which files in DIR2 corresponds to those in DIR1\n");
443 fprintf(out, " -g do not show the file groups\n");
444 fprintf(out, " -p show progress\n");
445 fprintf(out, " -r show the real file paths\n");
446 fprintf(out, " -i consider files with same inode as different\n");
448 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
451 /**********************************************************************/
453 int main(int argc, char **argv) {
455 struct file_with_size *root;
459 setlocale (LC_ALL, "");
462 c = getopt(argc, argv, "hrcgd0p");
479 ignore_empty_files = 1;
487 ignore_same_inode = 1;
507 if(optind + 2 == argc) {
508 start(argv[optind], argv[optind + 1]);
509 } else if(optind + 1 == argc) {
510 ignore_same_inode = 1;
511 start(argv[optind], 0);