Changed the syntax for negation from ^ to not:
[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 VERSION_NUMBER "0.5"
26
27 #define _BSD_SOURCE
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <sys/ioctl.h>
38 #include <locale.h>
39 #include <getopt.h>
40 #include <fcntl.h>
41
42 #define BUFFER_SIZE 4096
43
44 typedef int64_t size_sum_t;
45
46 /* Yeah, global variables! */
47
48 int ignore_dotfiles = 0; /* 1 means ignore files and directories
49                             starting with a dot */
50
51 int show_realpaths = 0; /* 1 means ignore files and directories
52                             starting with a dot */
53
54 int show_progress = 1; /* 1 means show a progress bar when we are in a
55                           tty */
56
57 /********************************************************************/
58
59 /* malloc with error checking.  */
60
61 void *safe_malloc(size_t n) {
62   void *p = malloc(n);
63   if (!p && n != 0) {
64     printf("Can not allocate memory: %s\n", strerror(errno));
65     exit(EXIT_FAILURE);
66   }
67   return p;
68 }
69
70 /********************************************************************/
71
72 int ignore_entry(const char *name) {
73   return
74     strcmp(name, ".") == 0 ||
75     strcmp(name, "..") == 0 ||
76     (ignore_dotfiles && name[0] == '.');
77 }
78
79 void print_size_sum(size_sum_t s) {
80   char tmp[100];
81   char *a = tmp + sizeof(tmp)/sizeof(char);
82   *(--a) = '\0';
83   if(s) {
84     while(s) {
85       *(--a) = s%10 + '0';
86       s /= 10;
87     }
88   } else {
89     *(--a) = '0';
90   }
91   printf(a);
92 }
93
94 /**********************************************************************/
95
96 struct file_with_size {
97   char *filename;
98   size_t size;
99   ino_t inode;
100   struct file_with_size *next;
101   int error;
102 };
103
104 void file_list_delete(struct file_with_size *head) {
105   struct file_with_size *next;
106   while(head) {
107     next = head->next;
108     free(head->filename);
109     free(head);
110     head = next;
111   }
112 }
113
114 int file_list_length(struct file_with_size *head) {
115   int l = 0;
116   while(head) {
117     l++;
118     head = head->next;
119   }
120   return l;
121 }
122
123 /**********************************************************************/
124
125 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
126   return f1->size == f2->size;
127 }
128
129 int same_content(struct file_with_size *f1, struct file_with_size *f2) {
130   int fd1, fd2, s1, s2;
131   char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
132
133   fd1 = open(f1->filename, O_RDONLY);
134   fd2 = open(f2->filename, O_RDONLY);
135
136   if(fd1 >= 0 && fd2 >= 0) {
137     while(1) {
138       s1 = read(fd1, buffer1, BUFFER_SIZE);
139       s2 = read(fd2, buffer2, BUFFER_SIZE);
140
141       if(s1 < 0 || s2 < 0) {
142         close(fd1);
143         close(fd2);
144         return 0;
145       }
146
147       if(s1 == s2) {
148         if(s1 == 0) {
149           close(fd1);
150           close(fd2);
151           return 1;
152         } else {
153           if(strncmp(buffer1, buffer2, s1)) {
154             close(fd1);
155             close(fd2);
156             return 0;
157           }
158         }
159       } else {
160         fprintf(stderr,
161                 "Different read size without error on files of same size.\n");
162         exit(EXIT_FAILURE);
163       }
164     }
165   } else {
166     if(fd1 >= 0) { close(fd1); }
167     if(fd2 >= 0) { close(fd2); }
168     return 0;
169   }
170 }
171
172 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
173   return same_size(f1, f2) && same_content(f1, f2);
174 }
175
176 /**********************************************************************/
177
178 struct file_with_size *scan_directory(struct file_with_size *tail,
179                                       const char *name) {
180   DIR *dir;
181   struct dirent *dir_e;
182   struct stat dummy;
183   struct file_with_size *tmp;
184   char subname[PATH_MAX];
185
186   if(lstat(name, &dummy) != 0) {
187     fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
188     exit(EXIT_FAILURE);
189   }
190
191   if(S_ISLNK(dummy.st_mode)) {
192     return tail;
193   }
194
195   dir = opendir(name);
196
197   if(dir) {
198     while((dir_e = readdir(dir))) {
199       if(!ignore_entry(dir_e->d_name)) {
200         snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
201         tail = scan_directory(tail, subname);
202       }
203     }
204     closedir(dir);
205   } else {
206     if(S_ISREG(dummy.st_mode)) {
207       tmp = safe_malloc(sizeof(struct file_with_size));
208       tmp->next = tail;
209       tmp->filename = strdup(name);
210       tmp->size = dummy.st_size;
211       tmp->inode = dummy.st_ino;
212       tail = tmp;
213     }
214   }
215
216   return tail;
217 }
218
219 void start(const char *dirname1, const char *dirname2) {
220   struct file_with_size *list1, *list2;
221   struct file_with_size *node1, *node2;
222   int not_in, found;
223
224   if(strncmp(dirname2, "not:", 4) == 0) {
225     not_in = 1;
226     dirname2 += 4;
227   } else {
228     not_in = 0;
229   }
230
231   list1 = scan_directory(0, dirname1);
232   list2 = scan_directory(0, dirname2);
233
234   if(not_in) {
235     for(node1 = list1; node1; node1 = node1->next) {
236       found = 0;
237
238       for(node2 = list2; !found && node2; node2 = node2->next) {
239         if(node1->inode != node2->inode && same_files(node1, node2)) {
240           found = 1;
241         }
242       }
243
244       if(!found) {
245         if(show_realpaths) {
246           printf("%s\n", realpath(node1->filename, 0));
247         } else {
248           printf("%s\n", node1->filename);
249         }
250       }
251     }
252
253   } else {
254
255     for(node1 = list1; node1; node1 = node1->next) {
256       for(node2 = list2; node2; node2 = node2->next) {
257         if(node1->inode != node2->inode && same_files(node1, node2)) {
258           if(show_realpaths) {
259             printf("%s %s\n",
260                    realpath(node1->filename, 0),
261                    realpath(node2->filename, 0));
262           } else {
263             printf("%s %s\n", node1->filename, node2->filename);
264           }
265         }
266       }
267     }
268   }
269
270   file_list_delete(list1);
271   file_list_delete(list2);
272 }
273
274 void print_help(FILE *out) {
275   fprintf(out, "Usage: finddup [OPTION]... DIR1 [[not:]DIR2]\n");
276   fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
277   fprintf(out, "Without DIR2, lists duplicate 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");
278   fprintf(out, "\n");
279   fprintf(out, "   -h   show this help.\n");
280   fprintf(out, "   -r   show the real file paths.\n");
281   fprintf(out, "\n");
282   fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
283 }
284
285 /**********************************************************************/
286
287 int main(int argc, char **argv) {
288   int c;
289   struct file_with_size *root;
290
291   root = 0;
292
293   setlocale (LC_ALL, "");
294
295   while (1) {
296     c = getopt(argc, argv, "hr");
297     if (c == -1)
298       break;
299
300     switch (c) {
301
302     case 'h':
303       print_help(stdout);
304       exit(EXIT_SUCCESS);
305
306       break;
307
308     case 'r':
309       show_realpaths = 1;
310       break;
311
312     default:
313       exit(EXIT_FAILURE);
314     }
315   }
316
317   if(optind + 1 < argc) {
318     start(argv[optind], argv[optind + 1]);
319   } else if(optind < argc) {
320     start(argv[optind], argv[optind]);
321   } else {
322     print_help(stderr);
323     exit(EXIT_FAILURE);
324   }
325
326   exit(EXIT_SUCCESS);
327 }