Skip to content

Commit 240d041

Browse files
committed
Parse multiple files.
1 parent 4bb7398 commit 240d041

File tree

5 files changed

+59
-32
lines changed

5 files changed

+59
-32
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ user_program.o: user_program.c
6363
user_program.bin: user_program.o start_user_program.o $(STDLIB)
6464
$(LD) -T link_user_program.ld -melf_i386 $^ -o $@
6565

66-
built_file_system: file_system_root/*
66+
built_file_system: file_system_root/* user_program.bin
6767
script/build_file_system
6868

6969
os.iso: kernel.elf user_program.bin menu.lst built_file_system

filesystem.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,63 @@
55
#include "stdlib.h"
66
#include "string.h"
77

8-
struct file_t* f;
8+
struct file_t* first_file;
9+
struct file_t* last_file;
910

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) {
1613
fprintf(LOG, "ERROR: First bytes of metadata should be `FILE ` but were `");
1714
for (int i = 0; i < 5; i++) {
18-
fprintf(LOG, "%c", cursor[i]);
15+
fprintf(LOG, "%c", (*cursor)[i]);
1916
}
2017
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));
2219
fprintf(LOG, "cursor: %s", cursor);
20+
while(1){}
2321
}
24-
cursor += 5;
22+
(*cursor) += 5;
2523

2624
struct file_t* file = malloc(sizeof(struct file_t));
2725

2826
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)++;
3230
file_name_length++;
3331
}
34-
cursor++;
32+
(*cursor)++;
3533

36-
uint32_t file_size = atoi(cursor);
34+
uint32_t file_size = atoi(*cursor);
3735
file->size = file_size;
38-
while (*cursor != '\n') {
39-
cursor++;
36+
while (*(*cursor) != '\n') {
37+
(*cursor)++;
4038
}
41-
cursor++;
42-
file->bytes = cursor;
39+
(*cursor)++;
40+
file->bytes = (*cursor);
41+
42+
(*cursor) += file->size + 1;
43+
44+
return file;
45+
}
4346

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+
}
4563
}
4664

4765
struct file_t* get_file() {
48-
return f;
66+
return first_file;
4967
}

interrupts.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ void interrupt_handler(struct cpu_state cpu, uint32_t interrupt_number, uint32_t
6464
keyboard_interrupt_handler();
6565
break;
6666
case(INT_PAGE_FAULT):
67-
log_interrupt_details("INT_PAGE_FAULT", error_code, eip, &cpu);
6867
if ((error_code & 0b1) == 0) {
6968
// Caused by page-not-present
7069
page_in((void*) cpu.cr2);

kmain.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ void kmain(struct kernel_memory_descriptor_t kernel_memory, uint32_t ebx) {
138138
fprintf(LOG, " - done\n");
139139

140140
struct file_t* file = get_file();
141-
fprintf(LOG, "Name of file: %s\n", file->name);
142-
fprintf(LOG, "Size of file: %i\n", file->size);
143-
fprintf(LOG, "Contents of file:\n----\n%s\n----\n", file->bytes);
141+
142+
while (file) {
143+
fprintf(LOG, "%s (%i bytes)\n----\n%s\n----\n", file->name, file->size, file->bytes);
144+
file = file->next_sibling;
145+
}
144146

145147
// fprintf(LOG, "- Creating a user process...\n");
146148
// create_process(first_module(mbinfo));

script/build_file_system

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
#!/usr/bin/env ruby
2-
dirname = "#{File.dirname(__FILE__)}/../file_system_root"
2+
3+
def include_file(path, output_file)
4+
f2 = File.open(path)
5+
filename = path.split("/").last
6+
output_file.write("FILE #{filename} #{f2.size}\n")
7+
output_file.write(f2.read)
8+
output_file.write("\n")
9+
f2.close
10+
end
11+
312
f = File.open('built_file_system', 'w')
13+
include_file("#{File.dirname(__FILE__)}/../user_program.bin", f)
14+
15+
dirname = "#{File.dirname(__FILE__)}/../file_system_root"
416
Dir.foreach(dirname) do |item|
517
next if item == '.' or item == '..'
6-
f2 = File.open("#{dirname}/#{item}")
7-
f.write("FILE #{item} #{f2.size}\n")
8-
f.write(f2.read)
9-
f.write("\n")
10-
f2.close
18+
include_file("#{dirname}/#{item}", f)
1119
end
1220

1321
f.close

0 commit comments

Comments
 (0)