Enforce the display of files from DIR1 before those from DIR2.
[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, dir_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           tmp->dir_id = -1;
241           tail = tmp;
242         }
243       }
244     }
245   }
246
247   return tail;
248 }
249
250 void print_file(struct file_with_size *node) {
251   char tmp[PATH_MAX + 1];
252   if(show_realpaths) {
253     if(show_groups) {
254       realpath(node->filename, tmp);
255       printf("%d %s\n", node->group_id, tmp);
256     } else {
257       realpath(node->filename, tmp);
258       printf("%s\n", tmp);
259     }
260   } else {
261     if(show_groups) {
262       printf("%d %s\n", node->group_id, node->filename);
263     } else {
264       printf("%s\n", node->filename);
265     }
266   }
267 }
268
269 int compare_nodes(const void *x1, const void *x2) {
270   const struct file_with_size **f1, **f2;
271
272   f1 = (const struct file_with_size **) x1;
273   f2 = (const struct file_with_size **) x2;
274
275   if((*f1)->group_id < (*f2)->group_id) {
276     return -1;
277   } else if((*f1)->group_id > (*f2)->group_id) {
278     return 1;
279   } else {
280     if((*f1)->dir_id < (*f2)->dir_id) {
281       return -1;
282     } else if((*f1)->dir_id > (*f2)->dir_id) {
283       return 1;
284     } else {
285       return 0;
286     }
287   }
288 }
289
290
291 void print_result(struct file_with_size *list1, struct file_with_size *list2) {
292   struct file_with_size *node1, *node2;
293   struct file_with_size **nodes;
294   int nb, n;
295
296   nb = 0;
297   for(node1 = list1; node1; node1 = node1->next) {
298     if(node1->group_id >= 0) { nb++; }
299   }
300
301   if(list2) {
302     for(node2 = list2; node2; node2 = node2->next) {
303       if(node2->group_id >= 0) { nb++; }
304     }
305   }
306
307   nodes = safe_malloc(nb * sizeof(struct file_with_size *));
308
309   n = 0;
310   for(node1 = list1; node1; node1 = node1->next) {
311     if(node1->group_id >= 0) {
312       nodes[n++] = node1;
313     }
314   }
315
316   if(list2) {
317     for(node2 = list2; node2; node2 = node2->next) {
318       if(node2->group_id >= 0) {
319         nodes[n++] = node2;
320       }
321     }
322   }
323
324   qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes);
325
326   for(n = 0; n < nb; n++) {
327     if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
328       printf("\n");
329     }
330     print_file(nodes[n]);
331   }
332
333   free(nodes);
334 }
335
336 void start(const char *dirname1, const char *dirname2) {
337   struct file_with_size *list1, *list2;
338   struct file_with_size *node1, *node2;
339   int not_in, found;
340   int k, p, pp, l1, n;
341
342   not_in = 0;
343
344   if(show_progress) {
345     fprintf(stderr, "Scanning %s ... ", dirname1);
346   }
347
348   list1 = scan_directory(0, dirname1);
349
350   if(dirname2) {
351     if(strncmp(dirname2, "not:", 4) == 0) {
352       not_in = 1;
353       dirname2 += 4;
354     }
355     if(show_progress) {
356       fprintf(stderr, "%s ... ", dirname2);
357     }
358     list2 = scan_directory(0, dirname2);
359   } else {
360     list2 = list1;
361   }
362
363   if(show_progress) {
364     fprintf(stderr, "done.\n");
365   }
366
367   k = 0;
368   pp = -1;
369   n = 0;
370   l1 = file_list_length(list1);
371
372   if(not_in) {
373     for(node1 = list1; node1; node1 = node1->next) {
374       if(show_progress) {
375         p = (100 * n)/l1;
376         if(p > pp) {
377           fprintf(stderr, "%d%%\n", p);
378           pp = p;
379         }
380         n++;
381       }
382
383       found = 0;
384
385       for(node2 = list2; !found && node2; node2 = node2->next) {
386         if(same_files(node1, node2)) {
387           found = 1;
388         }
389       }
390
391       if(!found) {
392         if(show_realpaths) {
393           printf("%s\n", realpath(node1->filename, 0));
394         } else {
395           printf("%s\n", node1->filename);
396         }
397       }
398     }
399
400   } else {
401     for(node1 = list1; node1; node1 = node1->next) {
402       if(show_progress) {
403         p = (100 * n)/l1;
404         if(p > pp) {
405           fprintf(stderr, "%d%%\n", p);
406           pp = p;
407         }
408         n++;
409       }
410
411       for(node2 = list2; node2; node2 = node2->next) {
412         if(node1->group_id < 0 || node2->group_id < 0) {
413           if(same_files(node1, node2)) {
414             if(node1->group_id < 0) {
415               if(node2->group_id >= 0) {
416                 node1->group_id = node2->group_id;
417               } else {
418                 node1->group_id = k;
419                 node1->dir_id = 1;
420                 k++;
421               }
422             }
423             if(node2->group_id < 0) {
424               node2->group_id = node1->group_id;
425               node2->dir_id = 2;
426             }
427           }
428         }
429       }
430     }
431   }
432
433   if(dirname2) {
434     print_result(list1, list2);
435     file_list_delete(list1);
436     file_list_delete(list2);
437   } else {
438     print_result(list1, 0);
439     file_list_delete(list1);
440   }
441 }
442
443 void print_help(FILE *out) {
444   fprintf(out, "Usage: finddup [OPTION]... DIR1 [[not:]DIR2]\n");
445   fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
446   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");
447   fprintf(out, "\n");
448   fprintf(out, "   -h   show this help\n");
449   fprintf(out, "   -d   ignore dot files and directories\n");
450   fprintf(out, "   -0   ignore empty files\n");
451   fprintf(out, "   -c   do not show which files in DIR2 corresponds to those in DIR1\n");
452   fprintf(out, "   -g   do not show the file groups\n");
453   fprintf(out, "   -p   show progress\n");
454   fprintf(out, "   -r   show the real file paths\n");
455   fprintf(out, "   -i   consider files with same inode as different\n");
456   fprintf(out, "\n");
457   fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
458 }
459
460 /**********************************************************************/
461
462 int main(int argc, char **argv) {
463   int c;
464   struct file_with_size *root;
465
466   root = 0;
467
468   setlocale (LC_ALL, "");
469
470   while (1) {
471     c = getopt(argc, argv, "hrcgd0p");
472     if (c == -1)
473       break;
474
475     switch (c) {
476
477     case 'h':
478       print_help(stdout);
479       exit(EXIT_SUCCESS);
480
481       break;
482
483     case 'd':
484       ignore_dotfiles = 1;
485       break;
486
487     case '0':
488       ignore_empty_files = 1;
489       break;
490
491     case 'r':
492       show_realpaths = 1;
493       break;
494
495     case 'i':
496       ignore_same_inode = 1;
497       break;
498
499     case 'g':
500       show_groups = 0;
501       break;
502
503     case 'p':
504       show_progress = 1;
505       break;
506
507     case 'c':
508       show_hits = 0;
509       break;
510
511     default:
512       exit(EXIT_FAILURE);
513     }
514   }
515
516   if(optind + 2 == argc) {
517     start(argv[optind], argv[optind + 1]);
518   } else if(optind + 1 == argc) {
519     ignore_same_inode = 1;
520     start(argv[optind], 0);
521   } else {
522     print_help(stderr);
523     exit(EXIT_FAILURE);
524   }
525
526   exit(EXIT_SUCCESS);
527 }