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 size_sum_t file_or_dir_size(const char *name) {
102 struct dirent *dir_e;
105 char subname[BUFFER_SIZE];
109 if(lstat(name, &dummy) != 0) {
110 printf("Can not stat %s: %s\n", name, strerror(errno));
114 if(S_ISLNK(dummy.st_mode)) {
121 while((dir_e = readdir(dir))) {
122 if(!ignore_entry(dir_e->d_name)) {
123 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
124 result += file_or_dir_size(subname);
129 if(S_ISREG(dummy.st_mode)) {
130 result += dummy.st_size;
137 /**********************************************************************/
139 struct file_with_size {
142 struct file_with_size *next;
147 struct file_with_size *head, *tail;
150 void merge(struct *file_list list1, struct *file_list list2) {
151 if(list1->head && list2->head) {
152 struct file_list result;
153 result.head = list1.head;
163 struct file_with_size *create(char *name, struct file_with_size *current) {
164 struct file_with_size *result;
165 result = safe_malloc(sizeof(struct file_with_size));
166 result->filename = strdup(name);
167 result->size = file_or_dir_size(name);
168 result->next = current;
172 void destroy(struct file_with_size *node) {
173 struct file_with_size *next;
176 free(node->filename);
182 /**********************************************************************/
184 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
185 return f1->size == f2->size;
188 int same_content(struct file_with_size *f1, struct file_with_size *f2) {
189 int fd1, fd2, eq, s1, s2;
190 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
192 fd1 = open(f1->filename, O_RDONLY);
193 fd2 = open(f2->filename, O_RDONLY);
196 if(fd1 >= 0 && fd2 >= 0) {
198 s1 = read(fd1, buffer1, BUFFER_SIZE);
203 s2 = read(fd2, buffer2, BUFFER_SIZE);
208 if(s1 > 0 && s2 > 0) {
210 eq = (strncmp(buffer1, buffer2, s1) == 0);
213 "Different read size without error on files of same size.\n");
237 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
238 return same_size(f1, f2) && same_content(f1, f2);
241 /**********************************************************************/
243 struct file_list scan_directory(const char *name) {
245 struct dirent *dir_e;
246 struct file_list result, sublist;
248 char subname[BUFFER_SIZE];
253 if(lstat(name, &dummy) != 0) {
254 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
258 if(S_ISLNK(dummy.st_mode)) {
265 while((dir_e = readdir(dir))) {
266 if(!ignore_entry(dir_e->d_name)) {
267 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
268 sublist = scan_directory(subname);
269 result = concat(result, sublist);
274 if(S_ISREG(dummy.st_mode)) {
275 result += dummy.st_size;
282 void start(const char *dirname1, const char *dirname2) {
283 struct file_with_size *list1, *list2;
284 list1 = scan_directory(dirname1);
285 list2 = scan_directory(dirname2);
290 /**********************************************************************/
292 int main(int argc, char **argv) {
294 struct file_with_size *root;
298 setlocale (LC_ALL, "");
301 c = getopt(argc, argv, "h");
308 printf("Usage: finddup [OPTION]... [FILE]...\n");
309 printf("Report bugs and comments to <francois@fleuret.org>\n");
319 if (optind + 2 < argc) {
320 start(argv[optind + 1], argv[optind + 2]);
322 fprintf(stderr, "%s [OPTIONS] <dir1> <dir2>\n", argv[0]);