3 * finddup is a simple utility find duplicated files, files common to
4 * several directories, or files present in one directory and not in
7 * Copyright (c) 2010 Francois Fleuret
8 * Written by Francois Fleuret <francois@fleuret.org>
10 * This file is part of finddup.
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.
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.
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/>.
26 #define VERSION_NUMBER "0.6"
30 #include <sys/types.h>
38 #include <sys/ioctl.h>
43 #define BUFFER_SIZE 4096
45 typedef int64_t size_sum_t;
47 /* Yeah, global variables! */
49 int ignore_dotfiles = 0; /* 1 means ignore files and directories
50 starting with a dot */
52 int show_realpaths = 0; /* 1 means ignore files and directories
53 starting with a dot */
55 int show_progress = 0; /* 1 means show a progress bar when we are in a
58 int show_hits = 1; /* 1 means to show the files from dir2
59 corresponding to the ones from dir1 */
61 int show_groups = 1; /* 1 means to show the group IDs when printing
64 /********************************************************************/
66 /* malloc with error checking. */
68 void *safe_malloc(size_t n) {
71 printf("Can not allocate memory: %s\n", strerror(errno));
77 /********************************************************************/
79 int ignore_entry(const char *name) {
81 strcmp(name, ".") == 0 ||
82 strcmp(name, "..") == 0 ||
83 (ignore_dotfiles && name[0] == '.');
86 void print_size_sum(size_sum_t s) {
88 char *a = tmp + sizeof(tmp)/sizeof(char);
101 /**********************************************************************/
103 struct file_with_size {
107 struct file_with_size *next;
111 void file_list_delete(struct file_with_size *head) {
112 struct file_with_size *next;
115 free(head->filename);
121 int file_list_length(struct file_with_size *head) {
130 /**********************************************************************/
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];
136 fd1 = open(f1->filename, O_RDONLY);
137 fd2 = open(f2->filename, O_RDONLY);
139 if(fd1 >= 0 && fd2 >= 0) {
141 s1 = read(fd1, buffer1, BUFFER_SIZE);
142 s2 = read(fd2, buffer2, BUFFER_SIZE);
144 if(s1 < 0 || s2 < 0) {
156 if(strncmp(buffer1, buffer2, s1)) {
164 "Different read size without error on files of same size.\n");
172 "Can not open \"%s\" error: %s\n",
179 "Can not open \"%s\" error: %s\n",
187 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
188 return f1->size == f2->size && same_content(f1, f2);
191 /**********************************************************************/
193 struct file_with_size *scan_directory(struct file_with_size *tail,
196 struct dirent *dir_e;
198 struct file_with_size *tmp;
199 char subname[PATH_MAX];
201 if(lstat(name, &sb) != 0) {
202 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
206 if(S_ISLNK(sb.st_mode)) {
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);
221 if(S_ISREG(sb.st_mode)) {
222 tmp = safe_malloc(sizeof(struct file_with_size));
224 tmp->filename = strdup(name);
225 tmp->size = sb.st_size;
226 tmp->inode = sb.st_ino;
235 void print_file(struct file_with_size *node) {
239 realpath(node->filename, tmp);
240 printf("%d %s\n", node->group_id, tmp);
242 realpath(node->filename, tmp);
247 printf("%d %s\n", node->group_id, node->filename);
249 printf("%s\n", node->filename);
254 int compare_nodes(const void *x1, const void *x2) {
255 const struct file_with_size **f1, **f2;
257 f1 = (const struct file_with_size **) x1;
258 f2 = (const struct file_with_size **) x2;
260 if((*f1)->group_id < (*f2)->group_id) {
262 } else if((*f1)->group_id > (*f2)->group_id) {
270 void print_result(struct file_with_size *list1, struct file_with_size *list2) {
271 struct file_with_size *node1, *node2;
272 struct file_with_size **nodes;
276 for(node1 = list1; node1; node1 = node1->next) {
277 if(node1->group_id >= 0) { nb++; }
281 for(node2 = list2; node2; node2 = node2->next) {
282 if(node2->group_id >= 0) { nb++; }
286 nodes = safe_malloc(nb * sizeof(struct file_with_size *));
289 for(node1 = list1; node1; node1 = node1->next) {
290 if(node1->group_id >= 0) {
296 for(node2 = list2; node2; node2 = node2->next) {
297 if(node2->group_id >= 0) {
303 qsort(nodes, nb, sizeof(struct file_with_size *), compare_nodes);
305 for(n = 0; n < nb; n++) {
306 print_file(nodes[n]);
312 void start(const char *dirname1, const char *dirname2) {
313 struct file_with_size *list1, *list2;
314 struct file_with_size *node1, *node2;
315 struct stat sb1, sb2;
316 int not_in, found, same_dir;
319 if(strncmp(dirname2, "not:", 4) == 0) {
326 if(lstat(dirname1, &sb1) != 0) {
327 fprintf(stderr, "Can not stat \"%s\": %s\n", dirname1, strerror(errno));
331 if(lstat(dirname2, &sb2) != 0) {
332 fprintf(stderr, "Can not stat \"%s\": %s\n", dirname2, strerror(errno));
336 same_dir = (sb1.st_ino == sb2.st_ino);
339 fprintf(stderr, "Scanning %s ... ", dirname1);
342 list1 = scan_directory(0, dirname1);
348 fprintf(stderr, "%s ... ", dirname2);
350 list2 = scan_directory(0, dirname2);
354 fprintf(stderr, "done.\n");
360 l1 = file_list_length(list1);
363 for(node1 = list1; node1; node1 = node1->next) {
367 fprintf(stderr, "%d%%\n", p);
375 for(node2 = list2; !found && node2; node2 = node2->next) {
376 if(node1->inode != node2->inode && same_files(node1, node2)) {
383 printf("%s\n", realpath(node1->filename, 0));
385 printf("%s\n", node1->filename);
391 for(node1 = list1; node1; node1 = node1->next) {
395 fprintf(stderr, "%d%%\n", p);
401 for(node2 = list2; node2; node2 = node2->next) {
402 if(node1->inode != node2->inode && same_files(node1, node2)) {
403 if(node1->group_id < 0) {
404 if(node2->group_id >= 0) {
405 node1->group_id = node2->group_id;
411 if(node2->group_id < 0) {
412 node2->group_id = node1->group_id;
419 print_result(list1, list2);
421 file_list_delete(list1);
423 file_list_delete(list2);
427 void print_help(FILE *out) {
428 fprintf(out, "Usage: finddup [OPTION]... DIR1 [[not:]DIR2]\n");
429 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
430 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");
432 fprintf(out, " -h show this help\n");
433 fprintf(out, " -d ignore dot files and directories\n");
434 fprintf(out, " -c do not show which files in DIR2 corresponds to those in DIR1\n");
435 fprintf(out, " -g do not show the file groups\n");
436 fprintf(out, " -p show progress\n");
437 fprintf(out, " -r show the real file paths\n");
439 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
442 /**********************************************************************/
444 int main(int argc, char **argv) {
446 struct file_with_size *root;
450 setlocale (LC_ALL, "");
453 c = getopt(argc, argv, "hrcgdp");
490 if(optind + 1 < argc) {
491 start(argv[optind], argv[optind + 1]);
492 } else if(optind < argc) {
493 start(argv[optind], argv[optind]);