-
Notifications
You must be signed in to change notification settings - Fork 3
/
f5ar_cmd.c
337 lines (268 loc) · 10.1 KB
/
f5ar_cmd.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
/*
* Written by Labunsky Artem <me@labunsky.info>
*
* This file is distributed under the Beerware licence.
*/
#include "f5ar_cmd.h"
#include "f5ar.h"
#include <tinydir.h>
#include <regex.h>
#include <time.h>
#include "f5ar_utils.c"
#define check_throw(action, err) err = action; if (err) return err
static int fill_w_regex(f5archive *archive, const char *path, const regex_t *reg) {
const size_t path_len = strlen(path);
if (path_len >= FILENAME_MAX - 1)
return F5AR_OK;
tinydir_dir dir = {};
tinydir_open(&dir, path);
int err = F5AR_OK;
while (dir.has_next && !err) {
tinydir_file file;
tinydir_readfile(&dir, &file);
if (file.name[0] == '.')
goto NEXT;
if (!file.is_dir) {
if (regexec(reg, file.name, 0, 0, 0))
goto NEXT;
if (f5ar_add_file(archive, file.path))
err = -3;
} else
err = fill_w_regex(archive, file.path, reg);
NEXT: tinydir_next(&dir);
}
tinydir_close(&dir);
return err;
}
static int fill_w_hashes(f5archive *archive, const char *path) {
const size_t path_len = strlen(path);
if (path_len == 0 || path_len >= FILENAME_MAX - 1)
return F5AR_OK;
tinydir_dir dir = {};
tinydir_open(&dir, path);
int err = F5AR_OK;
while (dir.has_next && !err) {
tinydir_file file;
tinydir_readfile(&dir, &file);
if (file.name[0] == '.')
goto NEXT;
if (!file.is_dir) {
err = f5ar_fill_file(archive, file.path);
switch (err) {
case F5AR_OK_COMPLETE:
return F5AR_OK;
case F5AR_NOT_FOUND:
err = 0;
}
} else
err = fill_w_hashes(archive, file.path);
NEXT: tinydir_next(&dir);
}
tinydir_close(&dir);
return F5AR_FAILURE;
}
#define fread_err(dest, size, file) fread(dest, 1, size, file) != size
static int archive_read(f5archive *archive, const char *path) {
int err = 0;
FILE* in = fopen(path, "rb");
if (!in) {
err = F5AR_FILEIO_ERR;
goto EXIT;
}
uint64_t order_size;
if (fread_err(&archive->meta.k, sizeof(uint8_t), in) ||
fread_err(&archive->meta.msg_size, sizeof(uint64_t), in) ||
fread_err(&order_size, sizeof(uint64_t), in)) {
err = F5AR_FILEIO_ERR;
goto CLOSE;
}
f5ar_blob* order = malloc(sizeof(f5ar_blob) + order_size);
if (!order) {
err = F5AR_MALLOC_ERR;
goto CLOSE;
}
order->size = order_size;
if (fread_err(order->body, order_size, in)) {
err = F5AR_FILEIO_ERR;
goto CLOSE;
}
err = f5ar_import_order(archive, order);
CLOSE: fclose(in);
EXIT: return err;
}
#define fwrite_err(dest, size, file) fwrite(dest, 1, size, file) != size
static int archive_write(f5archive* archive, const char* path) {
int err = F5AR_OK;
const f5ar_blob* order = f5ar_export_order_used(archive);
if (!order) {
err = F5AR_MALLOC_ERR;
goto EXIT;
}
FILE* out = fopen(path, "wb");
if (!out) {
err = F5AR_FILEIO_ERR;
goto EXIT;
}
const uint64_t order_size64 = order->size;
if (fwrite_err(&archive->meta.k, sizeof(uint8_t), out) ||
fwrite_err(&archive->meta.msg_size, sizeof(uint64_t), out) ||
fwrite_err(&order_size64, sizeof(uint64_t), out) ||
fwrite_err(order->body, order->size, out))
err = F5AR_FILEIO_ERR;
fclose(out);
EXIT: return err;
}
static void usage(char* argv[], int verbose) {
if (!verbose)
return;
printf("Usage: %s [FLAG] [[ARGS]]\n\n", argv[0]);
printf("Usable flags are:\n");
printf("-p [folder] [regex] [file] [name] \nCompress [file] in ([folder], [regex]) library to [archive name]\n\n");
printf("-u [archive] [file] \nDecompress [archive] and write result to the [file]\n\n");
printf("-a [folder] [regex] \nAnalyse ([folder], [regex]) library capacity\n\n");
printf("Examples:\n\n");
printf("Compress in.txt into *.jpg files in dogs folder and save as doge.arch:\n");
printf("%s -p dogs/ .*\\.jpg in.txt doge.arch\n\n", argv[0]);
printf("Decompress doge.arch in dogs/ folder to out.txt:\n");
printf("%s -u dogs/doge.arch out.txt\n", argv[0]);
}
static void check_capacity(f5archive archive, size_t msg_size, int verbose) {
if (!verbose)
return;
printf("Detected somewhat guaranteed capacity of %zu bytes\nDetected possible capacity of upto %zu bytes\n",
archive.capacity.full / 8,
(archive.capacity.full + archive.capacity.shrinkable - 1) / 8
);
if (archive.capacity.full / 8 < msg_size) {
printf("Warning, file size is larger than guaranteed library capacity by %zu bytes.\n",
msg_size - archive.capacity.full / 8);
printf("Compression could not be successful. Continue?\n");
char yn;
do {
printf("[Y/N]: ");
} while (scanf("%c", &yn) && yn != 'Y' && yn != 'N');
if (yn == 'N') {
printf("Exiting...\n");
exit(0);
}
}
}
#define do_timed_action(descr, action, verbose) {\
clock_t begin = clock();\
if (verbose) printf(#descr "...");\
fflush(stdout);\
{action;}\
const double time_spent = (double) (clock() - begin) / CLOCKS_PER_SEC;\
if (verbose) {if (time_spent > 0.1) printf(" done in %.1fs\n", time_spent);\
else printf(" ok\n");}\
}
int f5ar_cmd_exec(int argc, char* argv[], int verbose) {
int err;
if (argc < 2) {
usage(argv, verbose);
return F5AR_WRONG_ARGS;
}
f5archive archive;
memset(&archive, 0, sizeof(f5archive));
switch (argv[1][1]) {
case 'p': {
if (argc < 6) {
if (verbose) usage(argv, verbose);
return F5AR_WRONG_ARGS;
}
size_t msg_size = 0;
char *msg;
do_timed_action(Reading compressing file, ({
msg = file_read(argv[4], &msg_size);
if (!msg) {
if (verbose) printf("\nError reading file %s\n", argv[4]);
return F5AR_FILEIO_ERR;
}
}), verbose);
do_timed_action(Initializing the archive, ({
regex_t regex;
if (regcomp(®ex, argv[3], REG_EXTENDED | REG_NOSUB)) {
if (verbose) printf("Error compiling given regular expression");
return F5AR_WRONG_ARGS;
}
check_throw(f5ar_init(&archive), err);
fill_w_regex(&archive, argv[2], ®ex);
regfree(®ex);
}), verbose);
do_timed_action(Analysing library capacity, check_throw(f5ar_analyze(&archive), err), verbose);
check_capacity(archive, msg_size, verbose);
do_timed_action(Compressing, ({
err = f5ar_pack(&archive, msg, msg_size);
if (err == F5AR_FAILURE && verbose)
if (verbose) printf("Not enough capacity\n");
if (err) return err;
}), verbose);
do_timed_action(Saving the archive, ({
char archive_path[FILENAME_MAX];
memset(archive_path, 0, FILENAME_MAX);
strncpy(archive_path, argv[2], strlen(argv[2]));
const size_t path_len = strlen(argv[2]);
archive_path[path_len] = '/';
strncpy(archive_path + path_len + 1, argv[5], strlen(argv[5]));
check_throw(archive_write(&archive, archive_path), err);
}), verbose);
} break;
case 'u': {
if (argc < 4) {
usage(argv, verbose);
return F5AR_WRONG_ARGS;
}
do_timed_action(Initializing the archive, check_throw(f5ar_init(&archive), err), verbose);
do_timed_action(Reading the archive file, archive_read(&archive, argv[2]), verbose);
do_timed_action(Filling the archive with files, ({
char dir_path[FILENAME_MAX];
memset(dir_path, 0, FILENAME_MAX);
extract_dir_path(dir_path, argv[2]);
if (fill_w_hashes(&archive, dir_path))
return F5AR_NOT_COMPLETE;
}), verbose);
size_t msg_size = 0;
char *msg_ptr;
do_timed_action(Decompressing, ({
err = f5ar_unpack(&archive, &msg_ptr, &msg_size);
if (err) {
if (verbose) printf("Read %zu bytes, %zu expected\n", msg_size, archive.meta.msg_size);
return err;
}
}), verbose);
do_timed_action(Writing extracted data, ({
if (file_write(argv[3], msg_ptr, msg_size))
return F5AR_FILEIO_ERR;
}), verbose);
} break;
case 'a': {
if (argc < 4) {
usage(argv, verbose);
return F5AR_WRONG_ARGS;
}
do_timed_action(Initializing the archive, ({
regex_t regex;
if (regcomp(®ex, argv[3], REG_EXTENDED | REG_NOSUB)) {
if (verbose) printf("Error compiling given regular expression");
return F5AR_WRONG_ARGS;
}
check_throw(f5ar_init(&archive), err);
fill_w_regex(&archive, argv[2], ®ex);
regfree(®ex);
}), verbose);
do_timed_action(Analysing library capacity, f5ar_analyze(&archive), verbose);
printf("Detected somewhatguaranteed capacity of %zu bytes\nDetected possible capacity of upto %zu bytes\n",
archive.capacity.full / 8,
(archive.capacity.full + archive.capacity.shrinkable) / 8
);
} break;
default:
usage(argv, verbose);
return F5AR_WRONG_ARGS;
}
f5ar_destroy(&archive);
return F5AR_OK;
}
int main(int argc, char* argv[]) {
return f5ar_cmd_exec(argc, argv, 1);
}