-
Notifications
You must be signed in to change notification settings - Fork 0
/
dupf.c
320 lines (264 loc) · 7.68 KB
/
dupf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <openssl/md5.h>
#include <sys/mman.h>
#include <fcntl.h>
unsigned char md5sum[MD5_DIGEST_LENGTH];
#define NAME "dupf"
#define VERSION "0.1"
#define NODEBUG
#ifdef DEBUG
# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
#else
# define PDEBUG(fmt, args...)
#endif
typedef struct args {
char* dir;
} args_t;
typedef struct _file {
struct _file *next;
char *md5sum;
char *name;
} file_t;
typedef struct _fsize {
struct _fsize *next;
off_t size;
int count;
struct _file *file;
} fsize_t;
void free_file (file_t *file) {
while(file) {
file_t *next = file->next;
free(file->name);
free(file->md5sum);
free(file);
file = next;
}
}
void free_fsize (fsize_t *fsize) {
while(fsize != NULL) {
fsize_t *next = fsize->next;
free_file(fsize->file);
free(fsize);
fsize = next;
}
}
void usage() {
printf("Usage: " NAME " -d <dir>\n");
printf("Description:\n");
printf(" Find files in a folder.:\n");
}
void get_arguments(int argc, char** argv, args_t* arguments) {
opterr = 0;
int c;
while((c = getopt(argc, argv, "d:")) != -1) {
switch (c) {
case 'd':
arguments->dir = optarg;
break;
case '?':
if(optopt == 'd')
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
usage();
exit(EXIT_FAILURE);
default:
usage();
exit(EXIT_FAILURE);
}
}
// Exit if not argument is given
if(optind == 1) {
usage();
exit(EXIT_FAILURE);
}
}
void print_md5sum(unsigned char* md) {
int i;
for(i=0; i <MD5_DIGEST_LENGTH; i++) {
printf("%02x",md[i]);
}
printf("\n");
}
char* md5sum_file(char *filename, off_t file_size) {
int file_descript;
char* file_buffer;
memset(md5sum, '\0', sizeof(md5sum));
file_descript = open(filename, O_RDONLY);
if(file_descript < 0) {
fprintf(stderr, "ERROR: Unable to open file %s (%s)\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
file_buffer = mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0);
if(file_buffer == MAP_FAILED) {
fprintf(stderr, "ERROR: Unable to load %s (%s).\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
MD5((unsigned char*) file_buffer, file_size, md5sum);
munmap(file_buffer, file_size);
close(file_descript);
return md5sum;
}
void remove_nonduplicate(fsize_t *size_db) {
int i;
while(size_db != NULL) {
for(i = 1; i <= size_db->count; i++) {
}
}
}
void traverse_fsize_list (fsize_t *size_db) {
fsize_t *s;
file_t *f;
for(s = size_db; s != NULL; s = s->next) {
printf("%ld(%d)\n", s->size, s->count);
for(f = s->file; f != NULL; f = f->next) {
if(f->next == NULL) {
printf("└─%s ", f->name);
} else {
printf("├─%s ", f->name);
}
print_md5sum(f->md5sum);
}
}
}
fsize_t* find_size(off_t size, fsize_t *current) {
while(current) {
if(current->size == size) {
return current;
} else {
current = current->next;
}
}
// current will be NULL
return current;
}
void free_single_size (fsize_t *target, fsize_t *fsize) {
fsize_t *target_next = target->next;
while(fsize) {
fsize_t *next = fsize->next;
if(next == target) {
fsize->next = target_next;
/*
* BUG: Memory leak here, but we can't free it because it
* will cause the caller loop variable to illegal memory addr.
*/
//free_file(target->file);
//free(*target);
break;
}
fsize = next;
}
}
void md5sum_same_size(fsize_t *size_db) {
fsize_t *s;
file_t *f;
for(s = size_db; s != NULL; s = s->next) {
if(s->count == 1 && s != size_db) {
/*
* Remove list with single file, don't do it for first
* element (i.e 0) otherwise we'll have dangling pointer
*/
free_single_size(s, size_db);
} else if(s->size != 0) {
for(f = s->file; f != NULL; f = f->next) {
f->md5sum = strdup(md5sum_file(f->name, s->size));
}
}
}
}
void sort_with_size(char* filename, fsize_t *size_db) {
struct stat attributes;
fsize_t *found_size;
stat(filename, &attributes);
found_size = find_size(attributes.st_size, size_db);
if(found_size) {
file_t *current = malloc(sizeof(file_t));
if(!current) {
fprintf(stderr, "FATAL: Unable to allocated memory.\n");
exit(EXIT_FAILURE);
}
current->name = strdup(filename);
/*
* Check because the first object will not have
* memory allocated for file structure
*/
if(found_size->file) {
current->next = found_size->file->next;
found_size->file->next = current;
} else {
current->next = NULL;
found_size->file = current;
}
found_size->count++;
} else {
fsize_t *current = malloc(sizeof(fsize_t));
if(!current) {
fprintf(stderr, "FATAL: Unable to allocated memory.\n");
exit(EXIT_FAILURE);
}
current->size = attributes.st_size;
current->next = size_db->next;
size_db->next = current;
current->file = malloc(sizeof(fsize_t));
if(!current->file) {
fprintf(stderr, "FATAL: Unable to allocated memory.\n");
exit(EXIT_FAILURE);
}
current->file->name = strdup(filename);
current->file->next = NULL;
current->count = 1;
}
}
void get_files_list(char* path, fsize_t *size_db) {
DIR *dir;
struct dirent *entry;
char *apath;
if(dir = opendir(path)) {
while(entry = readdir(dir)) {
// Skip .. & .
if(strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, ".") == 0)
continue;
// Size + 2 for backslash and null termination
int len = strlen(path) + strlen(entry->d_name) + 2;
apath = malloc(len);
if(apath) {
apath[len - 1] = '0';
// Make absolute path of file
snprintf(apath, len, "%s/%s", path, entry->d_name);
} else {
fprintf(stderr, "ERROR: Unable to allocate memory (%s).\n", strerror(errno));
exit(EXIT_FAILURE);
}
// Read directory recursively
if(entry->d_type == DT_DIR) {
get_files_list(apath, size_db);
} else if(entry->d_type == DT_REG) {
PDEBUG("File: %s/%s\n", path, entry->d_name);
sort_with_size(apath, size_db);
}
free(apath);
}
closedir(dir);
} else {
fprintf(stderr, "ERROR: Unable to open directory \"%s\" (%s).\n", path, strerror(errno));
}
}
int main(int argc, char **argv) {
args_t arguments;
fsize_t size_db;
// Initialize the object
size_db.file = NULL;
size_db.size = 0;
size_db.count = 0;
size_db.next = NULL;
get_arguments(argc, argv, &arguments);
printf("Scanning directory: %s\n", arguments.dir);
get_files_list(arguments.dir, &size_db);
md5sum_same_size(&size_db);
traverse_fsize_list(&size_db);
}