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 create(struct file_list *list, const char *name) {
151 list->head = safe_malloc(sizeof(struct file_with_size));
153 list->head->filename = strdup(name);
154 list->head->size = file_or_dir_size(name);
155 list->head->next = 0;
158 void destroy(struct file_with_size *node) {
159 struct file_with_size *next;
162 free(node->filename);
168 /**********************************************************************/
170 int same_size(struct file_with_size *f1, struct file_with_size *f2) {
171 return f1->size == f2->size;
174 int same_content(struct file_with_size *f1, struct file_with_size *f2) {
175 int fd1, fd2, eq, s1, s2;
176 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
178 fd1 = open(f1->filename, O_RDONLY);
179 fd2 = open(f2->filename, O_RDONLY);
182 if(fd1 >= 0 && fd2 >= 0) {
184 s1 = read(fd1, buffer1, BUFFER_SIZE);
189 s2 = read(fd2, buffer2, BUFFER_SIZE);
194 if(s1 > 0 && s2 > 0) {
196 eq = (strncmp(buffer1, buffer2, s1) == 0);
199 "Different read size without error on files of same size.\n");
223 int same_files(struct file_with_size *f1, struct file_with_size *f2) {
224 return same_size(f1, f2) && same_content(f1, f2);
227 /**********************************************************************/
229 struct file_with_size *scan_directory(struct file_with_size *tail, const char *name) {
231 struct dirent *dir_e;
233 struct file_with_size *tmp;
234 char subname[BUFFER_SIZE];
236 if(lstat(name, &dummy) != 0) {
237 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
241 if(S_ISLNK(dummy.st_mode)) {
248 while((dir_e = readdir(dir))) {
249 if(!ignore_entry(dir_e->d_name)) {
250 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
251 tail = scan_directory(tail, subname);
256 if(S_ISREG(dummy.st_mode)) {
257 tmp = safe_malloc(sizeof(struct file_with_size));
259 tmp->filename = strdup(name);
260 tmp->size = file_or_dir_size(name);
268 void start(const char *dirname1, const char *dirname2) {
269 struct file_with_size *list1, *list2;
270 struct file_with_size *node;
271 list1 = scan_directory(0, dirname1);
272 list2 = scan_directory(0, dirname2);
274 for(node = list1; node; node = node->next) {
275 printf("list1: %s\n", node->filename);
278 for(node = list2; node; node = node->next) {
279 printf("list2: %s\n", node->filename);
286 /**********************************************************************/
288 int main(int argc, char **argv) {
290 struct file_with_size *root;
294 setlocale (LC_ALL, "");
297 c = getopt(argc, argv, "h");
304 printf("Usage: finddup [OPTION]... [FILE]...\n");
305 printf("Report bugs and comments to <francois@fleuret.org>\n");
315 if(optind + 1 < argc) {
316 start(argv[optind], argv[optind + 1]);
318 fprintf(stderr, "%s [OPTIONS] <dir1> <dir2>\n", argv[0]);