Now able to list files existing in DIR1 which do not in DIR2.
[finddup.git] / finddup.c
1
2 /*
3  *  finddup is a simple utility to display the files and directories
4  *  according to their total disk occupancy.
5  *
6  *  Copyright (c) 2010 Francois Fleuret
7  *  Written by Francois Fleuret <francois@fleuret.org>
8  *
9  *  This file is part of finddup.
10  *
11  *  finddup is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  finddup is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18  *  License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with finddup.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #define _BSD_SOURCE
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36 #include <locale.h>
37 #include <getopt.h>
38 #include <fcntl.h>
39
40 #define BUFFER_SIZE 4096
41
42 typedef int64_t size_sum_t;
43
44 /* Yeah, global variables! */
45
46 int ignore_dotfiles = 0; /* 1 means ignore files and directories
47                             starting with a dot */
48
49 int show_realpaths = 0; /* 1 means ignore files and directories
50                             starting with a dot */
51
52 int show_progress = 1; /* 1 means show a progress bar when we are in a
53                           tty */
54
55 /********************************************************************/
56
57 /* malloc with error checking.  */
58
59 void *safe_malloc(size_t n) {
60   void *p = malloc(n);
61   if (!p && n != 0) {
62     printf("Can not allocate memory: %s\n", strerror(errno));
63     exit(EXIT_FAILURE);
64   }
65   return p;
66 }
67
68 /********************************************************************/
69
70 int ignore_entry(const char *name) {
71   return
72     strcmp(name, ".") == 0 ||
73     strcmp(name, "..") == 0 ||
74     (ignore_dotfiles && name[0] == '.');
75 }
76
77 void print_size_sum(size_sum_t s) {
78   char tmp[100];
79   char *a = tmp + sizeof(tmp)/sizeof(char);
80   *(--a) = '\0';
81   if(s) {
82     while(s) {
83       *(--a) = s%10 + '0';
84       s /= 10;
85     }
86   } else {
87     *(--a) = '0';
88   }
89   printf(a);
90 }
91
92 /**********************************************************************/
93
94 struct file_with_size {
95   char *filename;
96   size_t size;
97   ino_t inode;
98   struct file_with_size *next;
99   int error;
100 };
101
102 void file_list_delete(struct file_with_size *head) {
103   struct file_with_size *next;
104   while(head) {
105     next = head->next;
106     free(head->filename);
107     free(head);
108     head = next;
109   }
110 }
111
112 int file_list_length(struct file_with_size *head) {
113   int l = 0;
114   while(head) {
115     l++;
116     head = head->next;
117   }
118   return l;
119 }
120
121 /**********************************************************************/
122
123 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
124   return f1->size == f2->size;
125 }
126
127 int same_content(struct file_with_size *f1, struct file_with_size *f2) {
128   int fd1, fd2, s1, s2;
129   char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
130
131   fd1 = open(f1->filename, O_RDONLY);
132   fd2 = open(f2->filename, O_RDONLY);
133
134   if(fd1 >= 0 && fd2 >= 0) {
135     while(1) {
136       s1 = read(fd1, buffer1, BUFFER_SIZE);
137       s2 = read(fd2, buffer2, BUFFER_SIZE);
138
139       if(s1 < 0 || s2 < 0) {
140         close(fd1);
141         close(fd2);
142         return 0;
143       }
144
145       if(s1 == s2) {
146         if(s1 == 0) {
147           close(fd1);
148           close(fd2);
149           return 1;
150         } else {
151           if(strncmp(buffer1, buffer2, s1)) {
152             close(fd1);
153             close(fd2);
154             return 0;
155           }
156         }
157       } else {
158         fprintf(stderr,
159                 "Different read size without error on files of same size.\n");
160         exit(EXIT_FAILURE);
161       }
162     }
163   } else {
164     if(fd1 >= 0) { close(fd1); }
165     if(fd2 >= 0) { close(fd2); }
166     return 0;
167   }
168 }
169
170 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
171   return same_size(f1, f2) && same_content(f1, f2);
172 }
173
174 /**********************************************************************/
175
176 struct file_with_size *scan_directory(struct file_with_size *tail,
177                                       const char *name) {
178   DIR *dir;
179   struct dirent *dir_e;
180   struct stat dummy;
181   struct file_with_size *tmp;
182   char subname[PATH_MAX];
183
184   if(lstat(name, &dummy) != 0) {
185     fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
186     exit(EXIT_FAILURE);
187   }
188
189   if(S_ISLNK(dummy.st_mode)) {
190     return tail;
191   }
192
193   dir = opendir(name);
194
195   if(dir) {
196     while((dir_e = readdir(dir))) {
197       if(!ignore_entry(dir_e->d_name)) {
198         snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
199         tail = scan_directory(tail, subname);
200       }
201     }
202     closedir(dir);
203   } else {
204     if(S_ISREG(dummy.st_mode)) {
205       tmp = safe_malloc(sizeof(struct file_with_size));
206       tmp->next = tail;
207       tmp->filename = strdup(name);
208       tmp->size = dummy.st_size;
209       tmp->inode = dummy.st_ino;
210       tail = tmp;
211     }
212   }
213
214   return tail;
215 }
216
217 void start(const char *dirname1, const char *dirname2) {
218   struct file_with_size *list1, *list2;
219   struct file_with_size *node1, *node2;
220   int not_in, found;
221
222   if(dirname2[0] == '^') {
223     not_in = 1;
224     dirname2++;
225   } else {
226     not_in = 0;
227   }
228
229   list1 = scan_directory(0, dirname1);
230   list2 = scan_directory(0, dirname2);
231
232   if(not_in) {
233     for(node1 = list1; node1; node1 = node1->next) {
234       found = 0;
235
236       for(node2 = list2; !found && node2; node2 = node2->next) {
237         if(node1->inode != node2->inode && same_files(node1, node2)) {
238           found = 1;
239         }
240       }
241
242       if(!found) {
243         if(show_realpaths) {
244           printf("%s\n", realpath(node1->filename, 0));
245         } else {
246           printf("%s\n", node1->filename);
247         }
248       }
249     }
250
251   } else {
252
253     for(node1 = list1; node1; node1 = node1->next) {
254       for(node2 = list2; node2; node2 = node2->next) {
255         if(node1->inode != node2->inode && same_files(node1, node2)) {
256           if(show_realpaths) {
257             printf("%s %s\n",
258                    realpath(node1->filename, 0),
259                    realpath(node2->filename, 0));
260           } else {
261             printf("%s %s\n", node1->filename, node2->filename);
262           }
263         }
264       }
265     }
266   }
267
268   file_list_delete(list1);
269   file_list_delete(list2);
270 }
271
272 /**********************************************************************/
273
274 int main(int argc, char **argv) {
275   int c;
276   struct file_with_size *root;
277
278   root = 0;
279
280   setlocale (LC_ALL, "");
281
282   while (1) {
283     c = getopt(argc, argv, "hr");
284     if (c == -1)
285       break;
286
287     switch (c) {
288
289     case 'h':
290       printf("Usage: finddup [OPTION]... [FILE]...\n");
291       printf("Report bugs and comments to <francois@fleuret.org>\n");
292       exit(EXIT_SUCCESS);
293
294       break;
295
296     case 'r':
297       show_realpaths = 1;
298       break;
299
300     default:
301       exit(EXIT_FAILURE);
302     }
303   }
304
305   if(optind + 1 < argc) {
306     start(argv[optind], argv[optind + 1]);
307   } else if(optind < argc) {
308     start(argv[optind], argv[optind]);
309   } else {
310     fprintf(stderr, "%s [OPTIONS] <dir1> [[^]<dir2>]\n", argv[0]);
311     exit(EXIT_FAILURE);
312   }
313
314   exit(EXIT_SUCCESS);
315 }