|
5 | 5 | #include "stdlib.h"
|
6 | 6 | #include "string.h"
|
7 | 7 |
|
8 |
| -struct file_t* f; |
| 8 | +struct file_t* first_file; |
| 9 | +struct file_t* last_file; |
9 | 10 |
|
10 |
| -void initialize_filesystem(struct module* mod) { |
11 |
| - void* mod_virtual_start = (void*) (mod->mod_start + UPPER_GB_START); |
12 |
| - |
13 |
| - char* cursor = (char*) mod_virtual_start; |
14 |
| - |
15 |
| - if (strncmp(cursor, "FILE ", 5) != 0) { |
| 11 | +struct file_t* parse_file(char** cursor) { |
| 12 | + if (strncmp(*cursor, "FILE ", 5) != 0) { |
16 | 13 | fprintf(LOG, "ERROR: First bytes of metadata should be `FILE ` but were `");
|
17 | 14 | for (int i = 0; i < 5; i++) {
|
18 |
| - fprintf(LOG, "%c", cursor[i]); |
| 15 | + fprintf(LOG, "%c", (*cursor)[i]); |
19 | 16 | }
|
20 | 17 | fprintf(LOG, "`\n");
|
21 |
| - fprintf(LOG, "strncmp returned %i\n", strncmp(cursor, "FILE ", 5)); |
| 18 | + fprintf(LOG, "strncmp returned %i\n", strncmp(*cursor, "FILE ", 5)); |
22 | 19 | fprintf(LOG, "cursor: %s", cursor);
|
| 20 | + while(1){} |
23 | 21 | }
|
24 |
| - cursor += 5; |
| 22 | + (*cursor) += 5; |
25 | 23 |
|
26 | 24 | struct file_t* file = malloc(sizeof(struct file_t));
|
27 | 25 |
|
28 | 26 | uint32_t file_name_length = 0;
|
29 |
| - while (*cursor != ' ' && file_name_length < FILE_NAME_MAX_LENGTH) { |
30 |
| - file->name[file_name_length] = *cursor; |
31 |
| - cursor++; |
| 27 | + while (*(*cursor) != ' ' && file_name_length < FILE_NAME_MAX_LENGTH) { |
| 28 | + file->name[file_name_length] = *(*cursor); |
| 29 | + (*cursor)++; |
32 | 30 | file_name_length++;
|
33 | 31 | }
|
34 |
| - cursor++; |
| 32 | + (*cursor)++; |
35 | 33 |
|
36 |
| - uint32_t file_size = atoi(cursor); |
| 34 | + uint32_t file_size = atoi(*cursor); |
37 | 35 | file->size = file_size;
|
38 |
| - while (*cursor != '\n') { |
39 |
| - cursor++; |
| 36 | + while (*(*cursor) != '\n') { |
| 37 | + (*cursor)++; |
40 | 38 | }
|
41 |
| - cursor++; |
42 |
| - file->bytes = cursor; |
| 39 | + (*cursor)++; |
| 40 | + file->bytes = (*cursor); |
| 41 | + |
| 42 | + (*cursor) += file->size + 1; |
| 43 | + |
| 44 | + return file; |
| 45 | +} |
43 | 46 |
|
44 |
| - f = file; |
| 47 | +void initialize_filesystem(struct module* mod) { |
| 48 | + void* mod_virtual_start = (void*) (mod->mod_start + UPPER_GB_START); |
| 49 | + char* mod_virtual_end = (mod->mod_end + UPPER_GB_START); |
| 50 | + char* cursor = (char*) mod_virtual_start; |
| 51 | + |
| 52 | + while (cursor < mod_virtual_end) { |
| 53 | + struct file_t* new_file = parse_file(&cursor); |
| 54 | + if (!first_file) { |
| 55 | + first_file = new_file; |
| 56 | + } |
| 57 | + if (last_file) { |
| 58 | + last_file->next_sibling = new_file; |
| 59 | + } else { |
| 60 | + last_file = new_file; |
| 61 | + } |
| 62 | + } |
45 | 63 | }
|
46 | 64 |
|
47 | 65 | struct file_t* get_file() {
|
48 |
| - return f; |
| 66 | + return first_file; |
49 | 67 | }
|
0 commit comments