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