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.5"
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 = 1; /* 1 means show a progress bar when we are in a
58 /********************************************************************/
60 /* malloc with error checking. */
62 void *safe_malloc(size_t n) {
65 printf("Can not allocate memory: %s\n", strerror(errno));
71 /********************************************************************/
73 int ignore_entry(const char *name) {
75 strcmp(name, ".") == 0 ||
76 strcmp(name, "..") == 0 ||
77 (ignore_dotfiles && name[0] == '.');
80 void print_size_sum(size_sum_t s) {
82 char *a = tmp + sizeof(tmp)/sizeof(char);
95 /**********************************************************************/
97 struct file_with_size {
101 struct file_with_size *next;
105 void file_list_delete(struct file_with_size *head) {
106 struct file_with_size *next;
109 free(head->filename);
115 int file_list_length(struct file_with_size *head) {
124 /**********************************************************************/
126 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
127 return f1->size == f2->size;
130 int same_content(struct file_with_size *f1, struct file_with_size *f2) {
131 int fd1, fd2, s1, s2;
132 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
134 fd1 = open(f1->filename, O_RDONLY);
135 fd2 = open(f2->filename, O_RDONLY);
137 if(fd1 >= 0 && fd2 >= 0) {
139 s1 = read(fd1, buffer1, BUFFER_SIZE);
140 s2 = read(fd2, buffer2, BUFFER_SIZE);
142 if(s1 < 0 || s2 < 0) {
154 if(strncmp(buffer1, buffer2, s1)) {
162 "Different read size without error on files of same size.\n");
167 if(fd1 >= 0) { close(fd1); }
168 if(fd2 >= 0) { close(fd2); }
173 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
174 return same_size(f1, f2) && same_content(f1, f2);
177 /**********************************************************************/
179 struct file_with_size *scan_directory(struct file_with_size *tail,
182 struct dirent *dir_e;
184 struct file_with_size *tmp;
185 char subname[PATH_MAX];
187 if(lstat(name, &dummy) != 0) {
188 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
192 if(S_ISLNK(dummy.st_mode)) {
199 while((dir_e = readdir(dir))) {
200 if(!ignore_entry(dir_e->d_name)) {
201 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
202 tail = scan_directory(tail, subname);
207 if(S_ISREG(dummy.st_mode)) {
208 tmp = safe_malloc(sizeof(struct file_with_size));
210 tmp->filename = strdup(name);
211 tmp->size = dummy.st_size;
212 tmp->inode = dummy.st_ino;
220 void start(const char *dirname1, const char *dirname2) {
221 struct file_with_size *list1, *list2;
222 struct file_with_size *node1, *node2;
225 if(strncmp(dirname2, "not:", 4) == 0) {
232 list1 = scan_directory(0, dirname1);
233 list2 = scan_directory(0, dirname2);
236 for(node1 = list1; node1; node1 = node1->next) {
239 for(node2 = list2; !found && node2; node2 = node2->next) {
240 if(node1->inode != node2->inode && same_files(node1, node2)) {
247 printf("%s\n", realpath(node1->filename, 0));
249 printf("%s\n", node1->filename);
256 for(node1 = list1; node1; node1 = node1->next) {
257 for(node2 = list2; node2; node2 = node2->next) {
258 if(node1->inode != node2->inode && same_files(node1, node2)) {
261 realpath(node1->filename, 0),
262 realpath(node2->filename, 0));
264 printf("%s %s\n", node1->filename, node2->filename);
271 file_list_delete(list1);
272 file_list_delete(list2);
275 void print_help(FILE *out) {
276 fprintf(out, "Usage: finddup [OPTION]... DIR1 [[not:]DIR2]\n");
277 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
278 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");
280 fprintf(out, " -h show this help.\n");
281 fprintf(out, " -r show the real file paths.\n");
283 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
286 /**********************************************************************/
288 int main(int argc, char **argv) {
290 struct file_with_size *root;
294 setlocale (LC_ALL, "");
297 c = getopt(argc, argv, "hr");
318 if(optind + 1 < argc) {
319 start(argv[optind], argv[optind + 1]);
320 } else if(optind < argc) {
321 start(argv[optind], argv[optind]);