-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
388 lines (337 loc) · 7.84 KB
/
Copy pathmain.c
File metadata and controls
388 lines (337 loc) · 7.84 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "crc16.h"
#define FULLNAME_LEN 1024
struct list_entry {
struct list_entry *next;
unsigned long size;
char name[FULLNAME_LEN];
};
/* List entry for more than two files of the same size
*
* For this case first calculate CRC of each file part,
* then the files is compared if CRC is equal.
*/
struct crc_entry {
struct crc_entry *next;
char *name;
unsigned short crc;
int fd;
void *mm;
};
static int files_scanned_num, files_failed_num;
static void inc_files_scanned(void)
{
files_scanned_num++;
}
static void inc_files_failed(void)
{
files_failed_num++;
}
static int files_scanned(void)
{
return files_scanned_num;
}
static int files_failed(void)
{
return files_failed_num;
}
int file_size(char *name)
{
struct stat info;
if(stat(name,&info) != 0) {
printf("Error calculate size of file %s\n", name);
inc_files_failed();
return 0;
}
return info.st_size;
}
int scan_dir(char *dir_name, unsigned long min_size, int(*list_cb)(struct list_entry*, char*, unsigned long), struct list_entry *list)
{
DIR * dir;
struct dirent *entry;
char full_name[FULLNAME_LEN];
int size;
dir = opendir(dir_name);
if (dir == NULL) {
printf("Error reading of directory %s\n", dir_name);
return -1;
}
while (NULL != (entry = readdir(dir))) {
if((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))
continue;
strcpy(full_name, dir_name);
strcat(full_name, "/");
strcat(full_name, entry->d_name);
if(entry->d_type == 0x04) {
if(scan_dir(full_name, min_size, list_cb, list)) {
closedir(dir);
return -3; /* TODO: error code */
}
} else {
size = file_size(full_name);
if (size >= min_size)
if ((*list_cb)(list, full_name, size)) {
closedir(dir);
printf("Error list entry inserting\n");
inc_files_failed();
return -2;
}
}
}
closedir(dir);
return 0;
}
/* Inserting record into file list arranged by size */
int insert_file_entry(struct list_entry *list, char *name, unsigned long size)
{
struct list_entry *n; /* new entry */
n = malloc(strlen(name) + sizeof(struct list_entry) - FULLNAME_LEN + 1);
if(NULL == n)
return 1;
strcpy(&(n->name[0]), name);
n->size = size;
while(list != NULL){
if (list->next != NULL && size >= list->size && size <= list->next->size) {
n->next = list->next;
list->next = n;
break;
}
else if(list->next == NULL) {
n->next = NULL;
list->next = n;
break;
}
list = list->next;
}
inc_files_scanned();
return 0;
}
void clean_malloc(struct list_entry *list)
{
struct list_entry *curr_ptr;
while (list != NULL) {
curr_ptr = list;
list = list->next;
free(curr_ptr);
}
}
void unmap_and_close(struct crc_entry *list, int size)
{
while (list != NULL) {
if (NULL != list->mm)
munmap(list->mm, size);
if (0 != list->fd)
close(list->fd);
list = list->next;
}
}
void show_list(struct list_entry *list)
{
while(list != NULL) {
printf("size=%lu %s\n",list->size, list->name);
list = list->next;
}
}
int is_files_equal(char *f1, char *f2, size_t size)
{
#define ERROR 0;
int fd1, fd2, ret;
void *fm1, *fm2;
fd1 = open(f1, O_RDWR, S_IRUSR | S_IWUSR);
if(fd1 <= 0) {
printf("Error opening file %s\n", f1);
inc_files_failed();
return ERROR;
}
fd2 = open(f2, O_RDWR, S_IRUSR | S_IWUSR);
if(fd2 <= 0) {
printf("Error opening file %s\n", f2);
close(fd1);
inc_files_failed();
return ERROR;
}
fm1 = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
if(MAP_FAILED == fm1) {
printf("Error mapping file %s\n", f1);
close(fd1);
close(fd2);
inc_files_failed();
return ERROR;
}
fm2 = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd2, 0);
if(MAP_FAILED == fm2) {
printf("Error mapping file %s\n", f2);
close(fd1);
close(fd2);
munmap(fm1, size);
inc_files_failed();
return ERROR;
}
ret = memcmp(fm1, fm2, size);
munmap(fm1, size);
munmap(fm2, size);
close(fd1);
close(fd2);
return !ret;
}
int crc_calc(struct crc_entry *crc, struct list_entry *list)
{
void *fm;
int fd, size;
crc->fd = 0;
crc->mm = NULL;
fd = open(list->name, O_RDWR, S_IRUSR | S_IWUSR);
if(fd <= 1) {
printf("Unable to open file: %s\n", list->name);
return 1;
}
size = list->size > 512 ? 512 : list->size;
fm = mmap(0, list->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(MAP_FAILED == fm) {
printf("Unable to map file: %s\n", list->name);
close(fd);
return 1;
}
crc->crc = crc16(0, fm, size);
crc->fd = fd;
crc->mm = fm;
return 0;
}
int push_to_crc_list(struct crc_entry* *hash_list, struct list_entry *list)
{
struct crc_entry *new_ent;
new_ent = malloc(sizeof(struct crc_entry));
if(NULL == new_ent) {
printf("Memory allocation error\n");
return 1;
}
new_ent->name = list->name;
new_ent->next = *hash_list;
if(crc_calc(new_ent, list)) {
free(new_ent);
return 0; /* return success */
}
*hash_list = new_ent;
return 0;
}
void do_crc_comparisons(struct crc_entry* *hl, unsigned long size)
{
int files_eq, display_first = 0;
struct crc_entry *first = *hl, *list, *prev;
while(NULL != first && NULL != first->next) {
list = first->next;
prev = first;
while(list != NULL) {
if(first->crc != list->crc) {
list = list->next;
prev = prev->next;
continue;
}
files_eq = !memcmp(first->mm, list->mm, size);
if(files_eq){
if(!display_first) {
printf("\n%lu\n%s\n",size, first->name);
display_first = 1;
}
printf("%s\n", list->name);
/* nxt: */
prev->next = list->next; /* ??????? error if list->next == NULL */
munmap(list->mm, size);
close(list->fd);
free(list);
list = prev->next;
}else{
list = list->next;
prev = prev->next;
}
}
display_first = 0;
first = first->next;
}
}
void file_comparisons(struct list_entry *l)
{
struct list_entry *sf; /* second_file */
struct crc_entry **cl; /* crc list */
struct crc_entry *first;
if (NULL != l->next)
l = l->next; /* drop empty entry */
else
return;
while(NULL != l->next) {
if(l->size != l->next->size) {
l = l->next;
continue;
}
sf = l->next;
if((NULL != sf && l->size == sf->size) &&
((NULL != sf->next && sf->next->size != sf->size) || NULL == sf->next)) {
if(is_files_equal(l->name, sf->name, l->size))
printf("\n%lu\n%s\n%s\n", l->size, l->name, sf->name);
} else {
/* More than two files have the same size */
first = malloc(sizeof(struct crc_entry));
if(NULL == first) {
printf("Memory allocation error\n");
return;
}
first->name = l->name;
first->next = NULL;
crc_calc(first, l);
cl = &first;
while(NULL != sf && l->size == sf->size) {
if(push_to_crc_list(cl, sf)) {
printf("Insufficient of memory\n");
unmap_and_close(*cl, l->size);
clean_malloc((struct list_entry*) *cl);
return;
}
sf = sf->next;
l = l->next;
}
do_crc_comparisons(cl, l->size);
unmap_and_close(*cl, l->size);
clean_malloc((struct list_entry*) *cl);
}
l = l->next;
}
}
int file_dup_find(char *dir, unsigned long min_size)
{
struct list_entry *list;
int ret;
list = malloc(sizeof(struct list_entry));
list->next = NULL;
list->size = 0;
list->name[0] = 0;
printf("Scanning files...\n");
ret = scan_dir(dir, min_size, insert_file_entry, list);
printf("\nStarting comparisons...\n");
file_comparisons(list);
printf("%d files scanned, %d file(s) failed.\n", files_scanned(), files_failed());
clean_malloc(list);
return ret;
}
int main(int argc, char *argv[])
{
int str_end;
unsigned long min_size = 0;
if (argc < 2 || argc > 3) {
printf("Use: %s <Directory> [Minimal File size]\n", argv[0]);
return 0;
}
if(argc > 2) {
min_size = atoi(argv[2]);
}
str_end = strlen(argv[1]) - 1;
if (argv[1][str_end] == '/')
argv[1][str_end] = 0;
return file_dup_find(argv[1], min_size);
}