Fixed a bug due to using strncmp instead of memcmp (ahem).
[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     print_file(nodes[n]);
321   }
322
323   free(nodes);
324 }
325
326 void start(const char *dirname1, const char *dirname2) {
327   struct file_with_size *list1, *list2;
328   struct file_with_size *node1, *node2;
329   int not_in, found;
330   int k, p, pp, l1, n;
331
332   not_in = 0;
333
334   if(show_progress) {
335     fprintf(stderr, "Scanning %s ... ", dirname1);
336   }
337
338   list1 = scan_directory(0, dirname1);
339
340   if(dirname2) {
341     if(strncmp(dirname2, "not:", 4) == 0) {
342       not_in = 1;
343       dirname2 += 4;
344     }
345     if(show_progress) {
346       fprintf(stderr, "%s ... ", dirname2);
347     }
348     list2 = scan_directory(0, dirname2);
349   } else {
350     list2 = list1;
351   }
352
353   if(show_progress) {
354     fprintf(stderr, "done.\n");
355   }
356
357   k = 0;
358   pp = -1;
359   n = 0;
360   l1 = file_list_length(list1);
361
362   if(not_in) {
363     for(node1 = list1; node1; node1 = node1->next) {
364       if(show_progress) {
365         p = (100 * n)/l1;
366         if(p > pp) {
367           fprintf(stderr, "%d%%\n", p);
368           pp = p;
369         }
370         n++;
371       }
372
373       found = 0;
374
375       for(node2 = list2; !found && node2; node2 = node2->next) {
376         if(same_files(node1, node2)) {
377           found = 1;
378         }
379       }
380
381       if(!found) {
382         if(show_realpaths) {
383           printf("%s\n", realpath(node1->filename, 0));
384         } else {
385           printf("%s\n", node1->filename);
386         }
387       }
388     }
389
390   } else {
391     for(node1 = list1; node1; node1 = node1->next) {
392       if(show_progress) {
393         p = (100 * n)/l1;
394         if(p > pp) {
395           fprintf(stderr, "%d%%\n", p);
396           pp = p;
397         }
398         n++;
399       }
400
401       for(node2 = list2; node2; node2 = node2->next) {
402         if(node1->group_id < 0 || node2->group_id < 0) {
403           if(same_files(node1, node2)) {
404             if(node1->group_id < 0) {
405               if(node2->group_id >= 0) {
406                 node1->group_id = node2->group_id;
407               } else {
408                 node1->group_id = k;
409                 k++;
410               }
411             }
412             if(node2->group_id < 0) {
413               node2->group_id = node1->group_id;
414             }
415           }
416         }
417       }
418     }
419   }
420
421   if(dirname2) {
422     print_result(list1, list2);
423     file_list_delete(list1);
424     file_list_delete(list2);
425   } else {
426     print_result(list1, 0);
427     file_list_delete(list1);
428   }
429 }
430
431 void print_help(FILE *out) {
432   fprintf(out, "Usage: finddup [OPTION]... DIR1 [[not:]DIR2]\n");
433   fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
434   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");
435   fprintf(out, "\n");
436   fprintf(out, "   -h   show this help\n");
437   fprintf(out, "   -d   ignore dot files and directories\n");
438   fprintf(out, "   -0   ignore empty files\n");
439   fprintf(out, "   -c   do not show which files in DIR2 corresponds to those in DIR1\n");
440   fprintf(out, "   -g   do not show the file groups\n");
441   fprintf(out, "   -p   show progress\n");
442   fprintf(out, "   -r   show the real file paths\n");
443   fprintf(out, "   -i   consider files with same inode as different\n");
444   fprintf(out, "\n");
445   fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
446 }
447
448 /**********************************************************************/
449
450 int main(int argc, char **argv) {
451   int c;
452   struct file_with_size *root;
453
454   root = 0;
455
456   setlocale (LC_ALL, "");
457
458   while (1) {
459     c = getopt(argc, argv, "hrcgd0p");
460     if (c == -1)
461       break;
462
463     switch (c) {
464
465     case 'h':
466       print_help(stdout);
467       exit(EXIT_SUCCESS);
468
469       break;
470
471     case 'd':
472       ignore_dotfiles = 1;
473       break;
474
475     case '0':
476       ignore_empty_files = 1;
477       break;
478
479     case 'r':
480       show_realpaths = 1;
481       break;
482
483     case 'i':
484       ignore_same_inode = 1;
485       break;
486
487     case 'g':
488       show_groups = 0;
489       break;
490
491     case 'p':
492       show_progress = 1;
493       break;
494
495     case 'c':
496       show_hits = 0;
497       break;
498
499     default:
500       exit(EXIT_FAILURE);
501     }
502   }
503
504   if(optind + 2 == argc) {
505     start(argv[optind], argv[optind + 1]);
506   } else if(optind + 1 == argc) {
507     ignore_same_inode = 1;
508     start(argv[optind], 0);
509   } else {
510     print_help(stderr);
511     exit(EXIT_FAILURE);
512   }
513
514   exit(EXIT_SUCCESS);
515 }