3 * finddup is a simple utility to display the files and directories
4 * according to their total disk occupancy.
6 * Copyright (c) 2010 Francois Fleuret
7 * Written by Francois Fleuret <francois@fleuret.org>
9 * This file is part of finddup.
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.
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.
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/>.
25 #define VERSION_NUMBER "0.5"
29 #include <sys/types.h>
37 #include <sys/ioctl.h>
42 #define BUFFER_SIZE 4096
44 typedef int64_t size_sum_t;
46 /* Yeah, global variables! */
48 int ignore_dotfiles = 0; /* 1 means ignore files and directories
49 starting with a dot */
51 int show_realpaths = 0; /* 1 means ignore files and directories
52 starting with a dot */
54 int show_progress = 1; /* 1 means show a progress bar when we are in a
57 /********************************************************************/
59 /* malloc with error checking. */
61 void *safe_malloc(size_t n) {
64 printf("Can not allocate memory: %s\n", strerror(errno));
70 /********************************************************************/
72 int ignore_entry(const char *name) {
74 strcmp(name, ".") == 0 ||
75 strcmp(name, "..") == 0 ||
76 (ignore_dotfiles && name[0] == '.');
79 void print_size_sum(size_sum_t s) {
81 char *a = tmp + sizeof(tmp)/sizeof(char);
94 /**********************************************************************/
96 struct file_with_size {
100 struct file_with_size *next;
104 void file_list_delete(struct file_with_size *head) {
105 struct file_with_size *next;
108 free(head->filename);
114 int file_list_length(struct file_with_size *head) {
123 /**********************************************************************/
125 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
126 return f1->size == f2->size;
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];
133 fd1 = open(f1->filename, O_RDONLY);
134 fd2 = open(f2->filename, O_RDONLY);
136 if(fd1 >= 0 && fd2 >= 0) {
138 s1 = read(fd1, buffer1, BUFFER_SIZE);
139 s2 = read(fd2, buffer2, BUFFER_SIZE);
141 if(s1 < 0 || s2 < 0) {
153 if(strncmp(buffer1, buffer2, s1)) {
161 "Different read size without error on files of same size.\n");
166 if(fd1 >= 0) { close(fd1); }
167 if(fd2 >= 0) { close(fd2); }
172 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
173 return same_size(f1, f2) && same_content(f1, f2);
176 /**********************************************************************/
178 struct file_with_size *scan_directory(struct file_with_size *tail,
181 struct dirent *dir_e;
183 struct file_with_size *tmp;
184 char subname[PATH_MAX];
186 if(lstat(name, &dummy) != 0) {
187 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
191 if(S_ISLNK(dummy.st_mode)) {
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);
206 if(S_ISREG(dummy.st_mode)) {
207 tmp = safe_malloc(sizeof(struct file_with_size));
209 tmp->filename = strdup(name);
210 tmp->size = dummy.st_size;
211 tmp->inode = dummy.st_ino;
219 void start(const char *dirname1, const char *dirname2) {
220 struct file_with_size *list1, *list2;
221 struct file_with_size *node1, *node2;
224 if(dirname2[0] == '^') {
231 list1 = scan_directory(0, dirname1);
232 list2 = scan_directory(0, dirname2);
235 for(node1 = list1; node1; node1 = node1->next) {
238 for(node2 = list2; !found && node2; node2 = node2->next) {
239 if(node1->inode != node2->inode && same_files(node1, node2)) {
246 printf("%s\n", realpath(node1->filename, 0));
248 printf("%s\n", node1->filename);
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)) {
260 realpath(node1->filename, 0),
261 realpath(node2->filename, 0));
263 printf("%s %s\n", node1->filename, node2->filename);
270 file_list_delete(list1);
271 file_list_delete(list2);
274 void print_help(FILE *out) {
275 fprintf(out, "Usage: finddup [OPTION]... DIR1 [[^]DIR2]\n");
276 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
277 fprintf(out, "Without DIR2, lists duplicated files in DIR1. With DIR2, list files common to both directories. With DIR2 starting with a ^, list files found in DIR1 which do not exist in DIR2.\n");
279 fprintf(out, " -h show this help.\n");
280 fprintf(out, " -r show the real file paths.\n");
282 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
285 /**********************************************************************/
287 int main(int argc, char **argv) {
289 struct file_with_size *root;
293 setlocale (LC_ALL, "");
296 c = getopt(argc, argv, "hr");
317 if(optind + 1 < argc) {
318 start(argv[optind], argv[optind + 1]);
319 } else if(optind < argc) {
320 start(argv[optind], argv[optind]);