-
Notifications
You must be signed in to change notification settings - Fork 0
/
smp.c
522 lines (448 loc) · 16 KB
/
smp.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <archive.h>
#include <archive_entry.h>
#include <ctype.h>
#include <ftw.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include "hash/hash.h"
#include "log/log.h"
#include "map/map.h"
#include "vec/vec.h"
#include "file_entry.h"
#ifdef _WIN32
#else
#include "mkdir_p/mkdir_p.h"
#endif
const char *kPathSeparator =
#ifdef _WIN32
"\\";
#else
"/";
#endif
typedef vec_t(File_entry*) vec_file_entry_t;
typedef map_t(vec_file_entry_t) map_vec_file_entry_t;
char* strlwr(char* str){
unsigned char* p = (unsigned char*)str;
while (*p) {
*p = tolower((unsigned char)*p);
p++;
}
return str;
}
int copy_to_file(const unsigned char* buffer, const int size, char* path) {
char* tmp = strdup(path);
char* out_dir = dirname(tmp);
mkdir_p(out_dir);
FILE* f = fopen(path, "w");
fwrite(buffer, 1, size, f);
fclose(f);
}
const char* get_file_ext(const char* filename) {
const char* dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot;
}
void open_archive(struct archive* a, char* archive) {
archive_write_set_format_filter_by_ext(a, archive);
int status = archive_write_open_filename(a, archive);
if (status != ARCHIVE_OK)
log_error("%s", archive_error_string(a));
}
void copy_to_archive(const unsigned char* buffer, const int size, struct archive* a, char* archive_path) {
struct archive_entry* a_entry = archive_entry_new();
archive_entry_set_pathname(a_entry, archive_path);
archive_entry_set_size(a_entry, size);
archive_entry_set_filetype(a_entry, AE_IFREG);
archive_entry_set_perm(a_entry, 0644);
//archive_entry_set_mtime(a_entry, 123456789, 0);
int status = archive_write_header(a, a_entry);
if (status == ARCHIVE_OK) {
archive_write_data(a, buffer, size);
} else {
log_error("%s", archive_error_string(a));
}
//archive_write_finish_entry(a);
archive_entry_free(a_entry);
}
void close_archive(struct archive* a) {
int status = archive_write_close(a);
if (status != ARCHIVE_OK)
log_error("%s", archive_error_string(a));
status = archive_write_free(a);
if (status != ARCHIVE_OK)
log_error("%s", archive_error_string(a));
}
void handle_to_folder(const unsigned char* buffer, const int size, char* output_folder, char* output_entry) {
char output_path[PATH_MAX];
strcpy(output_path, output_folder);
strcat(output_path, kPathSeparator);
strcat(output_path, output_entry);
log_debug("Copying to file %s", output_path);
copy_to_file(buffer, size, output_path);
}
void handle_to_archive(const unsigned char* buffer, const int size, struct archive* output_archive, char* output_path) {
log_debug("Copying to entry %s", output_path);
copy_to_archive(buffer, size, output_archive, output_path);
}
int handle_archive(map_vec_file_entry_t* db, int* found, const char* input_archive, char* output_folder, struct archive* output_archive) {
struct archive* a = archive_read_new();
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
int status = archive_read_open_filename(a, input_archive, 4096);
if (status != ARCHIVE_OK) {
log_error("%s", archive_error_string(a));
return status;
}
int found_in_archive = 0;
struct archive_entry* entry;
while ((status = archive_read_next_header(a, &entry)) == ARCHIVE_OK) {
const char* entry_pathname = archive_entry_pathname(entry);
int size = archive_entry_size(entry);
unsigned char* buffer = malloc(size);
archive_read_data(a, buffer, size);
Hash* hash = get_buffer_hash(buffer, size);
log_trace("entry: %s", entry_pathname);
log_trace("size: %d", size);
log_trace("sha256: %s", hash->sha256);
log_trace("sha1: %s", hash->sha1);
log_trace("md5: %s", hash->md5);
log_trace("crc32: %s", hash->crc32);
vec_file_entry_t* file_entries = map_get(db, hash->sha256);
if (file_entries != NULL) {
int i;
File_entry* file_entry;
vec_foreach(file_entries, file_entry, i) {
log_debug("Entry: %s", entry_pathname);
log_trace("Of size: %d", size);
log_debug("Matches: %s", file_entry->path);
(*found)++;
found_in_archive++;
if (output_folder) {
handle_to_folder(buffer, size, output_folder, (char*) file_entry->path);
}
if (output_archive) {
handle_to_archive(buffer, size, output_archive, (char*) file_entry->path);
}
}
free(file_entry);
map_remove(db, hash->sha256);
}
free(buffer);
}
log_debug("%d entries matched in %s", found_in_archive, input_archive);
if (status != ARCHIVE_EOF)
log_error("%s", archive_error_string(a));
status = archive_read_free(a);
if (status != ARCHIVE_OK)
log_error("%s", archive_error_string(a));
return status;
}
int handle_file(map_vec_file_entry_t* db, int* found, const char* file_in, char* output_folder, struct archive* output_archive) {
FILE* f = fopen(file_in, "rb");
fseek(f, 0, SEEK_END);
int size = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char* buffer = malloc(size);
fread(buffer, size, 1, f);
fclose(f);
Hash* hash = get_buffer_hash(buffer, size);
log_trace("size: %d", size);
log_trace("sha256: %s", hash->sha256);
log_trace("sha1: %s", hash->sha1);
log_trace("md5: %s", hash->md5);
log_trace("crc32: %s", hash->crc32);
vec_file_entry_t* file_entries = map_get(db, hash->sha256);
if (file_entries != NULL) {
int i;
File_entry* file_entry;
vec_foreach(file_entries, file_entry, i) {
log_debug("File: %s", file_in);
log_trace("Of size: %d", size);
log_debug("Matches: %s", file_entry->path);
(*found)++;
if (output_folder) {
handle_to_folder(buffer, size, output_folder, (char*) file_entry->path);
}
if (output_archive) {
handle_to_archive(buffer, size, output_archive, (char*) file_entry->path);
}
}
free(file_entry);
map_remove(db, hash->sha256);
}
free(buffer);
}
void handle_create_db_from_archive(FILE* db_file, const char* archive) {
struct archive* a = archive_read_new();
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
int status = archive_read_open_filename(a, archive, 4096);
if (status != ARCHIVE_OK) {
log_error("%s", archive_error_string(a));
}
struct archive_entry* entry;
while ((status = archive_read_next_header(a, &entry)) == ARCHIVE_OK) {
const char* entry_pathname = archive_entry_pathname(entry);
int size = archive_entry_size(entry);
unsigned char* buffer = malloc(size);
archive_read_data(a, buffer, size);
Hash* hash = get_buffer_hash(buffer, size);
log_trace("entry: %s", entry_pathname);
log_trace("size: %d", size);
log_trace("sha256: %s", hash->sha256);
log_trace("sha1: %s", hash->sha1);
log_trace("md5: %s", hash->md5);
log_trace("crc32: %s", hash->crc32);
File_entry* file_entry = create_file_entry(entry_pathname, hash);
write_entry_to_file(db_file, file_entry);
free_file_entry(file_entry);
free(buffer);
}
if (status != ARCHIVE_EOF)
log_error("%s", archive_error_string(a));
status = archive_read_free(a);
if (status != ARCHIVE_OK)
log_error("%s", archive_error_string(a));
}
void handle_create_db_from_folder(FILE* db_file, const char* file, char* folder) {
log_trace("folder: %s", folder);
log_trace("file: %s", file);
int i = 0;
const char* entry = file;
for(; i < strlen(folder); i++)
if (folder[i] == file[i])
entry++;
if (file[i] == '/')
entry++;
log_trace("entry: %s", entry);
File_entry* file_entry = create_file_entry_from_file(entry, file);
log_trace("%s", file_entry->hash->sha256);
log_trace("%s", file_entry->hash->sha1);
log_trace("%s", file_entry->hash->md5);
log_trace("%s", file_entry->hash->crc32);
write_entry_to_file(db_file, file_entry);
free_file_entry(file_entry);
}
int create_db(const char* file, map_vec_file_entry_t *db) {
int entries = 0;
FILE* f = fopen(file, "r");
size_t len = 0;
ssize_t read;
if (!f) {
log_error("Could not open %s", file);
return 1;
}
while(1) {
char* line = NULL;
read = getline(&line, &len, f);
if (read <= 0) break;
entries++;
char* sha256 = strlwr(strtok(line, "\t"));
char* path = strtok(NULL, "\t");
char* sha1 = strlwr(strtok(NULL, "\t"));
char* md5 = strlwr(strtok(NULL, "\t"));
char* crc32 = strlwr(strtok(NULL, "\t"));
Hash* hash = create_hash(sha256, sha1, md5, crc32);
File_entry* file_entry = create_file_entry(path, hash);
vec_file_entry_t* val = map_get(db, sha256);
if (val == NULL ) {
vec_file_entry_t v;
vec_init(&v);
vec_push(&v, file_entry);
map_set(db, sha256, v);
} else {
vec_push(val, file_entry);
}
}
fclose(f);
return entries;
}
void print_usage() {
printf("Usage: \n");
printf(" smp -d database_file -i input_directory [-o output_directory] [-a output_archive]\n\n");
printf(" smp -D database_file (-P parse_directory | -A parse_archive)\n\n");
printf("build pack:\n");
printf(" -d database_file\n");
printf(" set location of the database file\n\n");
printf(" -i input_directory\n");
printf(" set an input directory\n\n");
printf(" -o output_directory\n");
printf(" set an output directory\n\n");
printf(" -a output_archive\n");
printf(" set an output archive\n\n");
printf(" -m missing_file\n");
printf(" set a missing file\n\n");
printf("build db:\n");
printf(" -D database_file\n");
printf(" set location of the database file\n\n");
printf(" -P parse_directory\n");
printf(" create a database file from the provided directory\n\n");
printf(" -A parse_archive\n");
printf(" create a database file from the provided archive\n\n");
printf("logging:\n");
printf(" -l log_file\n");
printf(" set log file\n\n");
printf("flags:\n");
printf(" -v verbose output\n");
printf(" -vv very verbose output\n");
printf(" -q quite output\n");
}
int main(int argc, char** argv) {
setlocale(LC_ALL, "");
log_set_level(LOG_INFO);
vec_str_t input_folders;
vec_init(&input_folders);
char* database = NULL;
char* parse_folder = NULL;
char* parse_archive = NULL;
char* output_folder = NULL;
char* output_archive = NULL;
char* log = NULL;
char* missing = NULL;
int c;
opterr = 0;
while((c = getopt(argc, argv, "i:d:D:P:A:o:a:l:m:vqh")) != -1) {
switch(c) {
case 'i':
vec_push(&input_folders, optarg);
break;
case 'd':
case 'D':
database = optarg;
break;
case 'P':
parse_folder = optarg;
break;
case 'A':
parse_archive = optarg;
break;
case 'o':
output_folder = optarg;
break;
case 'a':
output_archive = optarg;
break;
case 'l':
log = optarg;
break;
case 'm':
missing = optarg;
break;
case 'v':
if (log_level_is(LOG_DEBUG))
log_set_level(LOG_TRACE);
else
log_set_level(LOG_DEBUG);
break;
case 'q':
log_set_quiet(1);
break;
case 'h':
print_usage();
exit(1);
break;
case '?':
break;
default:
break;
}
}
if (database == NULL || (parse_archive == NULL && parse_folder == NULL && input_folders.length == 0)) {
print_usage();
exit(1);
}
FILE* log_file = NULL;
if (log) {
log_file = fopen(log, "w");
log_set_fp(log_file);
}
log_trace("parse_folder = %s", parse_folder);
log_trace("parse_archive = %s", parse_archive);
log_trace("output_folder = %s", output_folder);
log_trace("output_archive = %s", output_archive);
FILE* db_file;
map_vec_file_entry_t db;
map_init(&db);
int entries_in_db = 0;
if (database != NULL && (parse_folder != NULL || parse_archive != NULL)) {
log_info("Creating database file %s", database);
db_file = fopen(database, "w");
} else if (database != NULL && input_folders.length > 0) {
log_info("Using database %s", database);
entries_in_db = create_db(database, &db);
log_info("Number of entries in database: %d", entries_in_db);
}
struct archive* output_a = NULL;
if (output_archive) {
output_a = archive_write_new();
open_archive(output_a, output_archive);
}
int found = 0;
int list(const char* file, const struct stat* status, int type) {
if(type == FTW_F){
log_trace("Processing file: %s", file);
const char *file_ext = get_file_ext(file);
if (parse_folder != NULL) {
//log_debug("Process %s as archive.", file);
handle_create_db_from_folder(db_file, file, parse_folder);
} else if (strcasecmp(file_ext, ".zip") == 0 ||
strcasecmp(file_ext, ".7z") == 0 ||
strcasecmp(file_ext, ".rar") == 0) {
log_debug("Process %s as archive.", file);
handle_archive(&db, &found, file, output_folder, output_a);
} else {
log_debug("Process %s as file.", file);
handle_file(&db, &found, file, output_folder, output_a);
}
}
return 0;
}
if (parse_archive != NULL) {
handle_create_db_from_archive(db_file, parse_archive);
} else if (parse_folder != NULL) {
ftw(parse_folder, list, 1);
} else if (input_folders.length > 0) {
int i = 0;
char* input_folder;
vec_foreach(&input_folders, input_folder, i) {
log_debug("Processing input_folder: %s", input_folder);
ftw(input_folder, list, 1);
}
}
if (output_a) {
close_archive(output_a);
}
if (input_folders.length > 0) {
int missed = entries_in_db - found;
double coverage = 100 * (double) found / entries_in_db;
log_info("Coverage: %d/%d {%.2f\%}", found, entries_in_db, coverage);
if (missed > 0) {
log_info("Missing: %d", missed);
}
FILE* missing_file = NULL;
if (missing) {
log_trace("missing = %s", missing);
missing_file = fopen(missing, "w");
int i = 0;
const char* key;
map_iter_t iter = map_iter(&db);
while ((key = map_next(&db, &iter))) {
vec_file_entry_t* val = map_get(&db, key);
File_entry* file_entry;
vec_foreach(val, file_entry, i) {
write_entry_to_file(missing_file, file_entry);
}
}
fclose(missing_file);
}
vec_deinit(&input_folders);
}
map_deinit(&db);
if (log_file) {
fclose(log_file);
}
if (db_file) {
fclose(db_file);
}
}