-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathkernel_filesystem.c
80 lines (66 loc) · 1.79 KB
/
kernel_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
#include "kernel_filesystem.h"
#include "memory.h"
#include "kernel_stdio.h"
#include "stdlib.h"
#include "stdlib/string.h"
struct file_t* first_file;
struct file_t* last_file;
struct file_t* parse_file(char** cursor) {
if (strncmp(*cursor, "FILE ", 5) != 0) {
fprintf(LOG, "ERROR: First bytes of metadata should be `FILE ` but were `");
for (int i = 0; i < 5; i++) {
fprintf(LOG, "%c", (*cursor)[i]);
}
fprintf(LOG, "`\n");
fprintf(LOG, "strncmp returned %i\n", strncmp(*cursor, "FILE ", 5));
fprintf(LOG, "cursor: %s", cursor);
while(1){}
}
(*cursor) += 5;
struct file_t* file = malloc(sizeof(struct file_t));
uint32_t file_name_length = 0;
while (*(*cursor) != ' ' && file_name_length < FILE_NAME_MAX_LENGTH) {
file->name[file_name_length] = *(*cursor);
(*cursor)++;
file_name_length++;
}
(*cursor)++;
uint32_t file_size = atoi(*cursor);
file->size = file_size;
while (*(*cursor) != '\n') {
(*cursor)++;
}
(*cursor)++;
file->bytes = (*cursor);
(*cursor) += file->size + 1;
return file;
}
void initialize_filesystem(struct module* mod) {
void* mod_virtual_start = (void*) (mod->mod_start + UPPER_GB_START);
char* mod_virtual_end = (mod->mod_end + UPPER_GB_START);
char* cursor = (char*) mod_virtual_start;
while (cursor < mod_virtual_end) {
struct file_t* new_file = parse_file(&cursor);
if (!first_file) {
first_file = new_file;
}
if (last_file) {
last_file->next_sibling = new_file;
} else {
last_file = new_file;
}
}
}
struct file_t* get_file(char* name) {
struct file_t* file = first_file;
while (file) {
if (strcmp(file->name, name) == 0) {
return file;
}
file = file->next_sibling;
}
return 0;
}
struct file_t* get_first_file() {
return first_file;
}