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/>.
27 #include <sys/types.h>
35 #include <sys/ioctl.h>
40 #define BUFFER_SIZE 4096
42 typedef int64_t size_sum_t;
44 /* Yeah, global variables! */
46 int ignore_dotfiles = 0; /* 1 means ignore files and directories
47 starting with a dot */
49 int show_realpaths = 0; /* 1 means ignore files and directories
50 starting with a dot */
52 int show_progress = 1; /* 1 means show a progress bar when we are in a
55 /********************************************************************/
57 /* malloc with error checking. */
59 void *safe_malloc(size_t n) {
62 printf("Can not allocate memory: %s\n", strerror(errno));
68 /********************************************************************/
70 int ignore_entry(const char *name) {
72 strcmp(name, ".") == 0 ||
73 strcmp(name, "..") == 0 ||
74 (ignore_dotfiles && name[0] == '.');
77 void print_size_sum(size_sum_t s) {
79 char *a = tmp + sizeof(tmp)/sizeof(char);
92 /**********************************************************************/
94 struct file_with_size {
98 struct file_with_size *next;
102 void file_list_delete(struct file_with_size *head) {
103 struct file_with_size *next;
106 free(head->filename);
112 int file_list_length(struct file_with_size *head) {
121 /**********************************************************************/
123 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
124 return f1->size == f2->size;
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];
131 fd1 = open(f1->filename, O_RDONLY);
132 fd2 = open(f2->filename, O_RDONLY);
134 if(fd1 >= 0 && fd2 >= 0) {
136 s1 = read(fd1, buffer1, BUFFER_SIZE);
137 s2 = read(fd2, buffer2, BUFFER_SIZE);
139 if(s1 < 0 || s2 < 0) {
151 if(strncmp(buffer1, buffer2, s1)) {
159 "Different read size without error on files of same size.\n");
164 if(fd1 >= 0) { close(fd1); }
165 if(fd2 >= 0) { close(fd2); }
170 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
171 return same_size(f1, f2) && same_content(f1, f2);
174 /**********************************************************************/
176 struct file_with_size *scan_directory(struct file_with_size *tail,
179 struct dirent *dir_e;
181 struct file_with_size *tmp;
182 char subname[PATH_MAX];
184 if(lstat(name, &dummy) != 0) {
185 fprintf(stderr, "Can not stat \"%s\": %s\n", name, strerror(errno));
189 if(S_ISLNK(dummy.st_mode)) {
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);
204 if(S_ISREG(dummy.st_mode)) {
205 tmp = safe_malloc(sizeof(struct file_with_size));
207 tmp->filename = strdup(name);
208 tmp->size = dummy.st_size;
209 tmp->inode = dummy.st_ino;
217 void start(const char *dirname1, const char *dirname2) {
218 struct file_with_size *list1, *list2;
219 struct file_with_size *node1, *node2;
222 if(dirname2[0] == '^') {
229 list1 = scan_directory(0, dirname1);
230 list2 = scan_directory(0, dirname2);
233 for(node1 = list1; node1; node1 = node1->next) {
236 for(node2 = list2; !found && node2; node2 = node2->next) {
237 if(node1->inode != node2->inode && same_files(node1, node2)) {
244 printf("%s\n", realpath(node1->filename, 0));
246 printf("%s\n", node1->filename);
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)) {
258 realpath(node1->filename, 0),
259 realpath(node2->filename, 0));
261 printf("%s %s\n", node1->filename, node2->filename);
268 file_list_delete(list1);
269 file_list_delete(list2);
272 /**********************************************************************/
274 int main(int argc, char **argv) {
276 struct file_with_size *root;
280 setlocale (LC_ALL, "");
283 c = getopt(argc, argv, "hr");
290 printf("Usage: finddup [OPTION]... [FILE]...\n");
291 printf("Report bugs and comments to <francois@fleuret.org>\n");
305 if(optind + 1 < argc) {
306 start(argv[optind], argv[optind + 1]);
307 } else if(optind < argc) {
308 start(argv[optind], argv[optind]);
310 fprintf(stderr, "%s [OPTIONS] <dir1> [[^]<dir2>]\n", argv[0]);