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 forced_width = 0; /* -1 means no width limit, strictly positive
50 means limit, 0 means not active */
52 int forced_height = 0; /* -1 means no height limit, strictly positive
53 means limit, 0 means not active */
55 int fancy_size_display = 0; /* 1 means to use floating values with K, M and G
58 int reverse_sorting = 0; /* 1 means to show the large ones first */
60 int show_top = 0; /* 1 means to show the top of the sorted list
61 instead of the bottom */
63 /********************************************************************/
65 /* malloc with error checking. */
67 void *safe_malloc(size_t n) {
70 printf("Can not allocate memory: %s\n", strerror(errno));
76 /********************************************************************/
78 int ignore_entry(const char *name) {
80 strcmp(name, ".") == 0 ||
81 strcmp(name, "..") == 0 ||
82 (ignore_dotfiles && name[0] == '.');
85 void print_size_sum(size_sum_t s) {
87 char *a = tmp + sizeof(tmp)/sizeof(char);
100 /**********************************************************************/
102 struct file_with_size {
106 struct file_with_size *next;
110 void file_list_delete(struct file_with_size *head) {
111 struct file_with_size *next;
114 free(head->filename);
120 int file_list_length(struct file_with_size *head) {
129 /**********************************************************************/
131 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
132 return f1->size == f2->size;
135 int same_content(struct file_with_size *f1, struct file_with_size *f2) {
136 int fd1, fd2, s1, s2;
137 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
139 fd1 = open(f1->filename, O_RDONLY);
140 fd2 = open(f2->filename, O_RDONLY);
142 if(fd1 >= 0 && fd2 >= 0) {
144 s1 = read(fd1, buffer1, BUFFER_SIZE);
145 s2 = read(fd2, buffer2, BUFFER_SIZE);
147 if(s1 < 0 || s2 < 0) {
157 if(strncmp(buffer1, buffer2, s1)) {
165 "Different read size without error on files of same size.\n");
174 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
175 return same_size(f1, f2) && same_content(f1, f2);
178 /**********************************************************************/
180 struct file_with_size *scan_directory(struct file_with_size *tail,
183 struct dirent *dir_e;
185 struct file_with_size *tmp;
186 char subname[BUFFER_SIZE];
188 if(lstat(name, &dummy) != 0) {
189 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
193 if(S_ISLNK(dummy.st_mode)) {
200 while((dir_e = readdir(dir))) {
201 if(!ignore_entry(dir_e->d_name)) {
202 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
203 tail = scan_directory(tail, subname);
208 if(S_ISREG(dummy.st_mode)) {
209 tmp = safe_malloc(sizeof(struct file_with_size));
211 tmp->filename = strdup(name);
212 tmp->size = dummy.st_size;
213 tmp->inode = dummy.st_ino;
221 void start(const char *dirname1, const char *dirname2) {
222 struct file_with_size *list1, *list2;
223 struct file_with_size *node1, *node2;
224 list1 = scan_directory(0, dirname1);
225 list2 = scan_directory(0, dirname2);
227 for(node1 = list1; node1; node1 = node1->next) {
228 for(node2 = list2; node2; node2 = node2->next) {
229 if(node1->inode != node2->inode && same_files(node1, node2)) {
230 printf("%s %s \n", node1->filename, node2->filename);
235 file_list_delete(list1);
236 file_list_delete(list2);
239 /**********************************************************************/
241 int main(int argc, char **argv) {
243 struct file_with_size *root;
247 setlocale (LC_ALL, "");
250 c = getopt(argc, argv, "h");
257 printf("Usage: finddup [OPTION]... [FILE]...\n");
258 printf("Report bugs and comments to <francois@fleuret.org>\n");
268 if(optind + 1 < argc) {
269 start(argv[optind], argv[optind + 1]);
271 fprintf(stderr, "%s [OPTIONS] <dir1> <dir2>\n", argv[0]);