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