This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
filesystem.c
585 lines (478 loc) · 12.8 KB
/
filesystem.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/**
* Copyright (c) 2017 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/stat.h>
#include "lib/microtar/microtar.h"
#include "lib/dmt/dmt.h"
#include "filesystem.h"
#define MAX_MOUNTS 8
#define MAX_PATH 256
enum {
FILESYSTEM_TNONE,
FILESYSTEM_TREG,
FILESYSTEM_TDIR,
};
typedef struct mount_t mount_t;
struct mount_t {
void (*unmount)(mount_t *mnt);
int (*exists)(mount_t *mnt, const char *filename);
int (*isFile)(mount_t *mnt, const char *filename);
int (*isDirectory)(mount_t *mnt, const char *filename);
void *(*read)(mount_t *mnt, const char *filename, int *size);
void *udata;
char path[MAX_PATH];
};
int filesystem_mountIdx;
mount_t filesystem_mounts[MAX_MOUNTS];
char filesystem_writeDir[MAX_PATH];
#define FOREACH_MOUNT(var)\
for (mount_t *var = &filesystem_mounts[filesystem_mountIdx - 1];\
var >= filesystem_mounts;\
var--)
static int get_file_type(const char *filename) {
/* The use of `stat` is intentionally avoided here, a stat call seems to
* block for a long time on DOS -- over 500ms in Dosbox at 26800 cycles */
DIR *dir = opendir(filename);
if (dir) {
closedir(dir);
return FILESYSTEM_TDIR;
}
FILE *fp = fopen(filename, "rb");
if (fp) {
fclose(fp);
return FILESYSTEM_TREG;
}
return FILESYSTEM_TNONE;
}
static int concat_path(char *dst, const char *dir, const char *filename) {
int dirlen = strlen(dir);
int filenamelen = strlen(filename);
/* Fail if the resultant path would overflow buffer */
if (dirlen + filenamelen + 2 > MAX_PATH) {
return FILESYSTEM_ETOOLONG;
}
/* Write full name to buffer and return ok */
if ( dir[dirlen - 1] == '/' ) {
sprintf(dst, "%s%s", dir, filename);
} else {
sprintf(dst, "%s/%s", dir, filename);
}
return FILESYSTEM_ESUCCESS;
}
static int concat_and_get_file_type(const char *dir, const char *filename) {
char buf[MAX_PATH];
/* Make fullpath */
int err = concat_path(buf, dir, filename);
if (err) {
return err;
}
/* Stat */
return get_file_type(buf);
}
static unsigned hash_string_ignore_case(const char *str) {
unsigned hash = 5381;
while (*str) {
hash = ((hash << 5) + hash) ^ tolower(*str++);
}
return hash;
}
static int strings_equal_ignore_case(const char *a, const char *b) {
while (*a) {
if (tolower(*a++) != tolower(*b++)) {
return 0;
}
}
return !*b;
}
static void strip_trailing_slash(char *str) {
int len = strlen(str);
if (len > 0 && str[len - 1] == '/') {
str[len - 1] = '\0';
}
}
static int is_separator(int chr) {
return (chr == '/' || chr == '\\');
}
static int make_dirs(const char *path) {
char str[MAX_PATH];
char *p = str;
int err = concat_path(str, path, "");
if (err) {
return err;
}
if (p[0] == '/') p++;
if (p[0] && p[1] == ':' && p[2] == '\\') p += 3;
while (*p) {
if (is_separator(*p)) {
*p = '\0';
if (get_file_type(str) != FILESYSTEM_TDIR) {
if (mkdir(str, S_IRWXU) == -1) {
return FILESYSTEM_EMKDIRFAIL;
}
}
*p = '/';
}
p++;
}
return FILESYSTEM_ESUCCESS;
}
/*==================*/
/* Directory mount */
/*==================*/
static void dir_unmount(mount_t *mnt) {
/* Intentionally empty */
}
static int dir_exists(mount_t *mnt, const char *filename) {
return concat_and_get_file_type(mnt->path, filename) != FILESYSTEM_TNONE;
}
static int dir_isFile(mount_t *mnt, const char *filename) {
return concat_and_get_file_type(mnt->path, filename) == FILESYSTEM_TREG;
}
static int dir_isDirectory(mount_t *mnt, const char *filename) {
return concat_and_get_file_type(mnt->path, filename) == FILESYSTEM_TDIR;
}
static void* dir_read(mount_t *mnt, const char *filename, int *size) {
char buf[MAX_PATH];
/* Make fullpath */
int err = concat_path(buf, mnt->path, filename);
if (err) {
return NULL;
}
/* Open file */
FILE *fp = fopen(buf, "rb");
if (!fp) {
return NULL;
}
/* Get size */
fseek(fp, 0, SEEK_END);
*size = ftell(fp);
fseek(fp, 0, SEEK_SET);
/* Load data */
void *p = dmt_malloc(*size);
if (!p) {
return NULL;
}
fread(p, 1, *size, fp);
/* Close file and return data */
fclose(fp);
return p;
}
static int dir_mount(mount_t *mnt, const char *path) {
/* Check the path is actually a directory */
if ( get_file_type(path) != FILESYSTEM_TDIR ) {
return FILESYSTEM_EFAILURE;
}
/* Init mount */
mnt->udata = NULL;
mnt->unmount = dir_unmount;
mnt->exists = dir_exists;
mnt->isFile = dir_isFile;
mnt->isDirectory = dir_isDirectory;
mnt->read = dir_read;
/* Return ok */
return FILESYSTEM_ESUCCESS;
}
/*==================*/
/* Tar mount */
/*==================*/
typedef struct { unsigned hash, pos; } tar_file_ref_t;
typedef struct {
mtar_t tar;
FILE *fp;
int offset;
tar_file_ref_t *map;
int nfiles;
} tar_mount_t;
static int tar_find(mount_t *mnt, const char *filename, mtar_header_t *h) {
/* Hash filename and linear search map for matching hash, read header and
* check against filename if the hashes match */
tar_mount_t *tm = mnt->udata;
unsigned hash = hash_string_ignore_case(filename);
int i;
for (i = 0; i < tm->nfiles; i++) {
if (tm->map[i].hash == hash) {
/* Seek to and load header */
mtar_seek(&tm->tar, tm->map[i].pos);
mtar_read_header(&tm->tar, h);
/* Compare names */
strip_trailing_slash(h->name);
if (strings_equal_ignore_case(h->name, filename)) {
return FILESYSTEM_ESUCCESS;
}
}
}
return FILESYSTEM_EFAILURE;
}
static void tar_unmount(mount_t *mnt) {
tar_mount_t *tm = mnt->udata;
mtar_close(&tm->tar);
dmt_free(tm->map);
dmt_free(tm);
}
static int tar_exists(mount_t *mnt, const char *filename) {
mtar_header_t h;
return tar_find(mnt, filename, &h) == FILESYSTEM_ESUCCESS;
}
static int tar_isFile(mount_t *mnt, const char *filename) {
mtar_header_t h;
int err = tar_find(mnt, filename, &h);
if (err) {
return 0;
}
return h.type == MTAR_TREG;
}
static int tar_isDirectory(mount_t *mnt, const char *filename) {
mtar_header_t h;
int err = tar_find(mnt, filename, &h);
if (err) {
return 0;
}
return h.type == MTAR_TDIR;
}
static void* tar_read(mount_t *mnt, const char *filename, int *size) {
mtar_t *tar = mnt->udata;
int err;
mtar_header_t h;
/* Find and load header for file */
err = tar_find(mnt, filename, &h);
if (err) {
return 0;
}
/* Allocate and read data, set size and return */
char *p = dmt_malloc(h.size);
err = mtar_read_data(tar, p, h.size);
if (err) {
dmt_free(p);
return NULL;
}
*size = h.size;
return p;
}
static int tar_stream_read(mtar_t *tar, void *data, unsigned size) {
tar_mount_t *tm = tar->stream;
unsigned res = fread(data, 1, size, tm->fp);
return (res == size) ? MTAR_ESUCCESS : MTAR_EREADFAIL;
}
static int tar_stream_seek(mtar_t *tar, unsigned offset) {
tar_mount_t *tm = tar->stream;
int res = fseek(tm->fp, tm->offset + offset, SEEK_SET);
return (res == 0) ? MTAR_ESUCCESS : MTAR_ESEEKFAIL;
}
static int tar_stream_close(mtar_t *tar) {
tar_mount_t *tm = tar->stream;
fclose(tm->fp);
return MTAR_ESUCCESS;
}
static int tar_mount(mount_t *mnt, const char *path) {
tar_mount_t *tm = NULL;
FILE *fp = NULL;
/* Try to open file */
fp = fopen(path, "rb");
if (!fp) {
goto fail;
}
/* Init tar_mount_t */
tm = dmt_calloc(1, sizeof(*tm));
tm->fp = fp;
/* Init tar */
mtar_t *tar = &tm->tar;
tar->read = tar_stream_read;
tar->seek = tar_stream_seek;
tar->close = tar_stream_close;
tar->stream = tm;
/* Check start of file for valid tar header */
mtar_header_t h;
int err = mtar_read_header(tar, &h);
/* If checking the start of the file failed then check the end of file for a
* "TAR\0" tag and offset, this would have been added when packaging (see
* `package.c`) to indicate the offset of the tar archive's beginning from the
* file's end */
if (err) {
int offset;
char buf[4] = "";
fseek(fp, -8, SEEK_END);
fread(buf, 1, 4, fp);
fread(&offset, 1, 4, fp);
if ( !memcmp(buf, "TAR\0", 4) ) {
fseek(fp, -offset, SEEK_END);
tm->offset = ftell(fp);
}
mtar_rewind(tar);
err = mtar_read_header(tar, &h);
if (err) {
goto fail;
}
}
/* Iterate all files and store [namehash:position] pairs; this is used by
* tar_find() */
mtar_rewind(tar);
int n = 0;
int cap = 0;
while ( (mtar_read_header(tar, &h)) == MTAR_ESUCCESS ) {
/* Realloc if map capacity was reached */
if (n >= cap) {
cap = cap ? (cap << 1) : 16;
tm->map = dmt_realloc(tm->map, cap * sizeof(*tm->map));
}
/* Store entry */
strip_trailing_slash(h.name);
tm->map[n].hash = hash_string_ignore_case(h.name);
tm->map[n].pos = tar->pos;
/* Next */
mtar_next(tar);
n++;
}
tm->nfiles = n;
/* Init mount */
mnt->udata = tar;
mnt->unmount = tar_unmount;
mnt->exists = tar_exists;
mnt->isFile = tar_isFile;
mnt->isDirectory = tar_isDirectory;
mnt->read = tar_read;
/* Return ok */
return FILESYSTEM_ESUCCESS;
fail:
if (fp) fclose(fp);
if (tm) {
dmt_free(tm->map);
dmt_free(tm);
}
return FILESYSTEM_EFAILURE;
}
/*==================*/
/* Filesystem */
/*==================*/
const char* filesystem_strerror(int err) {
switch (err) {
case FILESYSTEM_ESUCCESS : return "success";
case FILESYSTEM_EFAILURE : return "failure";
case FILESYSTEM_ETOOLONG : return "path too long";
case FILESYSTEM_EMOUNTED : return "path already mounted";
case FILESYSTEM_ENOMOUNT : return "path is not mounted";
case FILESYSTEM_EMOUNTFAIL : return "could not mount path";
case FILESYSTEM_ENOWRITEDIR : return "no write directory set";
case FILESYSTEM_EWRITEFAIL : return "could not write file";
case FILESYSTEM_EMKDIRFAIL : return "could not make directory";
}
return "unknown error";
}
void filesystem_deinit(void) {
FOREACH_MOUNT(mnt) {
mnt->unmount(mnt);
}
filesystem_mountIdx = 0;
}
int filesystem_mount(const char *path) {
/* Check path length is ok */
if ( strlen(path) >= MAX_PATH ) {
return FILESYSTEM_ETOOLONG;
}
/* Check path isn't already mounted */
FOREACH_MOUNT(m) {
if ( !strcmp(m->path, path) ) {
return FILESYSTEM_EMOUNTED;
}
}
/* Get mount slot */
if (filesystem_mountIdx >= MAX_MOUNTS) {
return FILESYSTEM_EFAILURE;
}
mount_t *mnt = &filesystem_mounts[filesystem_mountIdx++];
/* Copy path name */
strcpy(mnt->path, path);
/* Try to mount path */
if ( tar_mount(mnt, path) == FILESYSTEM_ESUCCESS ) goto success;
if ( dir_mount(mnt, path) == FILESYSTEM_ESUCCESS ) goto success;
/* Fail */
filesystem_mountIdx--;
return FILESYSTEM_EMOUNTFAIL;
success:
return FILESYSTEM_ESUCCESS;
}
int filesystem_unmount(const char *path) {
FOREACH_MOUNT(mnt) {
if ( !strcmp(mnt->path, path) ) {
/* Unmount */
mnt->unmount(mnt);
/* Shift remaining mounts to fill gap and decrement idx */
int idx = mnt - filesystem_mounts;
memmove(mnt, mnt + 1, (filesystem_mountIdx - idx - 1) * sizeof(mount_t));
filesystem_mountIdx--;
return FILESYSTEM_ESUCCESS;
}
}
return FILESYSTEM_ENOMOUNT;
}
int filesystem_exists(const char *filename) {
FOREACH_MOUNT(mnt) {
if ( mnt->exists(mnt, filename) ) {
return 1;
}
}
return 0;
}
int filesystem_isFile(const char *filename) {
FOREACH_MOUNT(mnt) {
if ( mnt->exists(mnt, filename) ) {
return mnt->isFile(mnt, filename);
}
}
return 0;
}
int filesystem_isDirectory(const char *filename) {
FOREACH_MOUNT(mnt) {
if ( mnt->exists(mnt, filename) ) {
return mnt->isDirectory(mnt, filename);
}
}
return 0;
}
void* filesystem_read(const char *filename, int *size) {
FOREACH_MOUNT(mnt) {
if ( mnt->exists(mnt, filename) && mnt->isFile(mnt, filename) ) {
return mnt->read(mnt, filename, size);
}
}
return NULL;
}
void filesystem_free(void *ptr) {
dmt_free(ptr);
}
int filesystem_setWriteDir(const char *path) {
if (strlen(path) >= MAX_PATH) {
return FILESYSTEM_ETOOLONG;
}
int err = make_dirs(path);
if (err) {
return err;
}
strcpy(filesystem_writeDir, path);
return FILESYSTEM_ESUCCESS;
}
int filesystem_write(const char *filename, const void *data, int size) {
int err, n;
char buf[MAX_PATH];
if (!*filesystem_writeDir) {
return FILESYSTEM_ENOWRITEDIR;
}
err = concat_path(buf, filesystem_writeDir, filename);
if (err) {
return err;
}
FILE *fp = fopen(buf, "wb");
if (!fp) {
return FILESYSTEM_EWRITEFAIL;
}
n = fwrite(data, 1, size, fp);
fclose(fp);
return n == size ? FILESYSTEM_ESUCCESS : FILESYSTEM_EWRITEFAIL;
}