a8847f5be4c4ac0602a510841b263807d5183ab0
[finddup.git] / finddup.c
1
2 /*
3  *  finddup is a simple utility find duplicated files, files common to
4  *  several directories, or files present in one directory and not in
5  *  another one.
6  *
7  *  Copyright (c) 2010 Francois Fleuret
8  *  Written by Francois Fleuret <francois@fleuret.org>
9  *
10  *  This file is part of finddup.
11  *
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.
15  *
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.
20  *
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/>.
23  *
24  */
25
26 #define VERSION_NUMBER "0.6"
27
28 #define _BSD_SOURCE
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <dirent.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <sys/ioctl.h>
40 #include <locale.h>
41 #include <getopt.h>
42 #include <fcntl.h>
43
44 #define BUFFER_SIZE 4096
45 #define LARGE_BUFFER_SIZE 65536
46
47 typedef int64_t size_sum_t;
48
49 /* Yeah, global variables! */
50
51 int ignore_dotfiles = 0; /* 1 means ignore files and directories
52                             starting with a dot */
53
54 int ignore_empty_files = 0; /* 1 means ignore empty files */
55
56 int show_realpaths = 0; /* 1 means ignore files and directories
57                             starting with a dot */
58
59 int show_progress = 0; /* 1 means show a progress bar when we are in a
60                           tty */
61
62 int show_hits = 1; /* 1 means to show the files from dir2
63                       corresponding to the ones from dir1 */
64
65 int show_groups = 1; /* 1 means to show the group IDs when printing
66                         file names */
67
68 int ignore_same_inode = 0; /* 1 means that comparison between two file
69                               with same inode will always be false */
70
71 /********************************************************************/
72
73 /* malloc with error checking.  */
74
75 void *safe_malloc(size_t n) {
76   void *p = malloc(n);
77   if (!p && n != 0) {
78     printf("Can not allocate memory: %s\n", strerror(errno));
79     exit(EXIT_FAILURE);
80   }
81   return p;
82 }
83
84 /********************************************************************/
85
86 int ignore_entry(const char *name) {
87   return
88     strcmp(name, ".") == 0 ||
89     strcmp(name, "..") == 0 ||
90     (ignore_dotfiles && name[0] == '.');
91 }
92
93 void print_size_sum(size_sum_t s) {
94   char tmp[100];
95   char *a = tmp + sizeof(tmp)/sizeof(char);
96   *(--a) = '\0';
97   if(s) {
98     while(s) {
99       *(--a) = s%10 + '0';
100       s /= 10;
101     }
102   } else {
103     *(--a) = '0';
104   }
105   printf(a);
106 }
107
108 /**********************************************************************/
109
110 struct file_with_size {
111   char *filename;
112   size_t size;
113   ino_t inode;
114   struct file_with_size *next;
115   int group_id;
116 };
117
118 void file_list_delete(struct file_with_size *head) {
119   struct file_with_size *next;
120   while(head) {
121     next = head->next;
122     free(head->filename);
123     free(head);
124     head = next;
125   }
126 }
127
128 int file_list_length(struct file_with_size *head) {
129   int l = 0;
130   while(head) {
131     l++;
132     head = head->next;
133   }
134   return l;
135 }
136
137 /**********************************************************************/
138
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];
142
143   fd1 = open(f1->filename, O_RDONLY);
144   fd2 = open(f2->filename, O_RDONLY);
145
146   if(fd1 >= 0 && fd2 >= 0) {
147     while(1) {
148       s1 = read(fd1, buffer1, LARGE_BUFFER_SIZE);
149       s2 = read(fd2, buffer2, LARGE_BUFFER_SIZE);
150
151       if(s1 < 0 || s2 < 0) {
152         close(fd1);
153         close(fd2);
154         return 0;
155       }
156
157       if(s1 == s2) {
158         if(s1 == 0) {
159           close(fd1);
160           close(fd2);
161           return 1;
162         } else {
163           if(memcmp(buffer1, buffer2, s1)) {
164             close(fd1);
165             close(fd2);
166             return 0;
167           }
168         }
169       } else {
170         fprintf(stderr,
171                 "Different read size without error on files of same size.\n");
172         exit(EXIT_FAILURE);
173       }
174     }
175   } else {
176
177     if(fd1 < 0) {
178       fprintf(stderr,
179               "Can not open \"%s\" error: %s\n",
180               f1->filename,
181               strerror(errno));
182     }
183
184     if(fd2 < 0) {
185       fprintf(stderr,
186               "Can not open \"%s\" error: %s\n",
187               f2->filename,
188               strerror(errno));
189     }
190     exit(EXIT_FAILURE);
191   }
192 }
193
194 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
195   if(ignore_same_inode && f1->inode == f2->inode) {
196     return 0;
197   }
198   return f1->size == f2->size && same_content(f1, f2);
199 }
200
201 /**********************************************************************/
202
203 struct file_with_size *scan_directory(struct file_with_size *tail,
204                                       const char *name) {
205   DIR *dir;
206   struct dirent *dir_e;
207   struct stat sb;
208   struct file_with_size *tmp;
209   char subname[PATH_MAX + 1];
210
211   if(lstat(name, &sb) != 0) {
212     fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
213     exit(EXIT_FAILURE);
214   }
215
216   if(S_ISLNK(sb.st_mode)) {
217     return tail;
218   }
219
220   dir = opendir(name);
221
222   if(dir) {
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);
227       }
228     }
229     closedir(dir);
230   } else {
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));
235           tmp->next = tail;
236           tmp->filename = strdup(name);
237           tmp->size = sb.st_size;
238           tmp->inode = sb.st_ino;
239           tmp->group_id = -1;
240           tail = tmp;
241         }
242       }
243     }
244   }
245
246   return tail;
247 }
248
249 void print_file(struct file_with_size *node) {
250   char tmp[PATH_MAX + 1];
251   if(show_realpaths) {
252     if(show_groups) {
253       realpath(node->filename, tmp);
254       printf("%d %s\n", node->group_id, tmp);
255     } else {
256       realpath(node->filename, tmp);
257       printf("%s\n", tmp);
258     }
259   } else {
260     if(show_groups) {
261       printf("%d %s\n", node->group_id, node->filename);
262     } else {
263       printf("%s\n", node->filename);
264     }
265   }
266 }
267
268 int compare_nodes(const void *x1, const void *x2) {
269   const struct file_with_size **f1, **f2;
270
271   f1 = (const struct file_with_size **) x1;
272   f2 = (const struct file_with_size **) x2;
273
274   if((*f1)->group_id < (*f2)->group_id) {
275     return -1;
276   } else if((*f1)->group_id > (*f2)->group_id) {
277     return 1;
278   } else {
279     return 0;
280   }
281 }
282
283
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;
287   int nb, n;
288
289   nb = 0;
290   for(node1 = list1; node1; node1 = node1->next) {
291     if(node1->group_id >= 0) { nb++; }
292   }
293
294   if(list2) {
295     for(node2 = list2; node2; node2 = node2->next) {
296       if(node2->group_id >= 0) { nb++; }
297     }
298   }
299
300   nodes = safe_malloc(nb * sizeof(struct file_with_size *));
301
302   n = 0;
303   for(node1 = list1; node1; node1 = node1->next) {
304     if(node1->group_id >= 0) {
305       nodes[n++] = node1;
306     }
307   }
308
309   if(list2) {
310     for(node2 = list2; node2; node2 = node2->next) {
311       if(node2->group_id >= 0) {
312         nodes[n++] = node2;
313       }
314     }
315   }
316
317   qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes);
318
319   for(n = 0; n < nb; n++) {
320     if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
321       printf("\n");
322     }
323     print_file(nodes[n]);
324   }
325
326   free(nodes);
327 }
328
329 void start(const char *dirname1, const char *dirname2) {
330   struct file_with_size *list1, *list2;
331   struct file_with_size *node1, *node2;
332   int not_in, found;
333   int k, p, pp, l1, n;
334
335   not_in = 0;
336
337   if(show_progress) {
338     fprintf(stderr, "Scanning %s ... ", dirname1);
339   }
340
341   list1 = scan_directory(0, dirname1);
342
343   if(dirname2) {
344     if(strncmp(dirname2, "not:", 4) == 0) {
345       not_in = 1;
346       dirname2 += 4;
347     }
348     if(show_progress) {
349       fprintf(stderr, "%s ... ", dirname2);
350     }
351     list2 = scan_directory(0, dirname2);
352   } else {
353     list2 = list1;
354   }
355
356   if(show_progress) {
357     fprintf(stderr, "done.\n");
358   }
359
360   k = 0;
361   pp = -1;
362   n = 0;
363   l1 = file_list_length(list1);
364
365   if(not_in) {
366     for(node1 = list1; node1; node1 = node1->next) {
367       if(show_progress) {
368         p = (100 * n)/l1;
369         if(p > pp) {
370           fprintf(stderr, "%d%%\n", p);
371           pp = p;
372         }
373         n++;
374       }
375
376       found = 0;
377
378       for(node2 = list2; !found && node2; node2 = node2->next) {
379         if(same_files(node1, node2)) {
380           found = 1;
381         }
382       }
383
384       if(!found) {
385         if(show_realpaths) {
386           printf("%s\n", realpath(node1->filename, 0));
387         } else {
388           printf("%s\n", node1->filename);
389         }
390       }
391     }
392
393   } else {
394     for(node1 = list1; node1; node1 = node1->next) {
395       if(show_progress) {
396         p = (100 * n)/l1;
397         if(p > pp) {
398           fprintf(stderr, "%d%%\n", p);
399           pp = p;
400         }
401         n++;
402       }
403
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;
410               } else {
411                 node1->group_id = k;
412                 k++;
413               }
414             }
415             if(node2->group_id < 0) {
416               node2->group_id = node1->group_id;
417             }
418           }
419         }
420       }
421     }
422   }
423
424   if(dirname2) {
425     print_result(list1, list2);
426     file_list_delete(list1);
427     file_list_delete(list2);
428   } else {
429     print_result(list1, 0);
430     file_list_delete(list1);
431   }
432 }
433
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");
438   fprintf(out, "\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");
447   fprintf(out, "\n");
448   fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
449 }
450
451 /**********************************************************************/
452
453 int main(int argc, char **argv) {
454   int c;
455   struct file_with_size *root;
456
457   root = 0;
458
459   setlocale (LC_ALL, "");
460
461   while (1) {
462     c = getopt(argc, argv, "hrcgd0p");
463     if (c == -1)
464       break;
465
466     switch (c) {
467
468     case 'h':
469       print_help(stdout);
470       exit(EXIT_SUCCESS);
471
472       break;
473
474     case 'd':
475       ignore_dotfiles = 1;
476       break;
477
478     case '0':
479       ignore_empty_files = 1;
480       break;
481
482     case 'r':
483       show_realpaths = 1;
484       break;
485
486     case 'i':
487       ignore_same_inode = 1;
488       break;
489
490     case 'g':
491       show_groups = 0;
492       break;
493
494     case 'p':
495       show_progress = 1;
496       break;
497
498     case 'c':
499       show_hits = 0;
500       break;
501
502     default:
503       exit(EXIT_FAILURE);
504     }
505   }
506
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);
512   } else {
513     print_help(stderr);
514     exit(EXIT_FAILURE);
515   }
516
517   exit(EXIT_SUCCESS);
518 }