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