diff options
author | Benji Dial <benji@benjidial.net> | 2024-01-11 23:53:57 -0500 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2024-01-11 23:53:57 -0500 |
commit | c4ab2f6f440f060b1686991b24379a4998aa55a9 (patch) | |
tree | e1b4cc875554a18ac110847cca9ba55261fca9c9 /kernel/entry.cpp | |
parent | 88816732b53eb536fe0e8db3d9ed15f0d1c29bb4 (diff) | |
download | hilbert-os-c4ab2f6f440f060b1686991b24379a4998aa55a9.tar.gz |
file reading, tarfs directory listing
Diffstat (limited to 'kernel/entry.cpp')
-rw-r--r-- | kernel/entry.cpp | 105 |
1 files changed, 77 insertions, 28 deletions
diff --git a/kernel/entry.cpp b/kernel/entry.cpp index 915bc8b..aa6568a 100644 --- a/kernel/entry.cpp +++ b/kernel/entry.cpp @@ -179,33 +179,84 @@ extern "C" [[noreturn]] void entry() { } -[[noreturn]] static void halt() { +[[noreturn]] static void print_and_halt(const char *msg) { + terminal::put_string_sz(msg); while (1) ; } -static void find_file(const char *path, size_t path_len) { +static void dir_tree( + storage::block_device *bd, storage::node_id_t node, unsigned indent +) { + + storage::node_id_t child_node; + storage::directory_iter_t dir_iter; + storage::io_result result = + bd->mounted_as->get_first_child(node, child_node, dir_iter); + if (result == storage::io_result::not_found) { + for (unsigned i = 0; i < indent; ++i) + terminal::put_char(' '); + terminal::put_string_sz("[empty]\n"); + return; + } + else if (result) + print_and_halt("error getting first child."); + + while (true) { + + unsigned name_len; + if (bd->mounted_as->get_name_length(child_node, name_len)) + print_and_halt("failed to get name length."); + char *name_buf = new char[name_len]; + if (bd->mounted_as->get_name(child_node, name_buf, name_len)) + print_and_halt("failed to get name."); + + for (unsigned i = 0; i < indent; ++i) + terminal::put_char(' '); + terminal::put_string(name_buf, name_len); + terminal::put_string(":\n", 2); + + delete[] name_buf; + + storage::file_type type; + if (bd->mounted_as->get_file_type(child_node, type)) + print_and_halt("failed to get type."); + + if (type == storage::file_type::directory) + dir_tree(bd, child_node, indent + 2); + + else { + uint64_t len; + if (bd->mounted_as->get_file_length(child_node, len)) + print_and_halt("failed to get length."); + char *contents = new char[len]; + if (bd->mounted_as->read_bytes_from_file(child_node, 0, len, contents)) + print_and_halt("failed to read file."); + + if (contents[len - 1] == '\n') + len--; + + for (unsigned i = 0; i < indent + 2; ++i) + terminal::put_char(' '); + if (len == 0) + terminal::put_string_sz("[empty]\n"); + else { + terminal::put_string(contents, len); + terminal::put_char('\n'); + } - storage::canon_path cp; - storage::canonize_path(path, path_len, cp); + delete[] contents; + } - storage::block_device *bd; - storage::node_id_t node_id; - storage::canon_path cp_without_symlinks; + storage::io_result result + = bd->mounted_as->get_next_child(node, child_node, dir_iter); + if (result == storage::io_result::not_found) + return; + else if (result) + print_and_halt("error getting next child."); - if (storage::look_up_absolute_path(cp, bd, node_id, true, - cp_without_symlinks) != storage::io_result::success) { - terminal::put_string_sz("failed to look up "); - terminal::put_string(path, path_len); - terminal::put_string_sz(" in vfs."); - halt(); } - terminal::put_string(path, path_len); - terminal::put_string_sz(" has node id "); - terminal::put_int_decimal(node_id); - terminal::put_string_sz(" in its file system.\n"); - } [[noreturn]] static void with_kernel_p4() { @@ -219,11 +270,8 @@ static void find_file(const char *path, size_t path_len) { storage::canon_path root; storage::canonize_path("/", 1, root); - if (storage::mount_device(initfs_bd, root, &fs::tarfs_mounter) != - storage::io_result::success) { - terminal::put_string_sz("failed to mount initfs."); - halt(); - } + if (storage::mount_device(initfs_bd, root, &fs::tarfs_mounter)) + print_and_halt("failed to mount initfs."); terminal::put_string_sz("kernel initialization complete.\n"); @@ -235,11 +283,12 @@ static void find_file(const char *path, size_t path_len) { terminal::put_int_decimal(free_pram_kib); terminal::put_string_sz(" kiB physical memory free.\n"); - find_file("/", 1); - find_file("/test.txt", 9); - find_file("/dir", 4); - find_file("/dir/dir2", 9); + storage::node_id_t root_node; + if (initfs_bd->mounted_as->get_root_node(root_node)) + print_and_halt("failed to get root initfs node."); - halt(); + terminal::put_string_sz("initfs:\n"); + dir_tree(initfs_bd, root_node, 2); + print_and_halt("halting."); } |