main: add initramfs read test

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-10 12:48:55 -04:00
commit cfda99c7c7

View file

@ -23,6 +23,7 @@
#include <drivers/apic.h>
#include <fs/vfs.h>
#include <fs/inode.h>
#include <fs/cpio.h>
#include <lib/stdio.h>
@ -42,6 +43,42 @@ static void do_initcalls()
(*fn)();
}
static void indent(int indent)
{
for (int i = 0; i < indent; i++) {
kprintf(" ");
}
}
void printf_dir_recursive(dirent_t *dir, int ind)
{
if (!dir || !dir->inode)
return;
dirent_t *cursor;
struct dir_context ctx = {0};
int res;
while (!(res = vfs_readdir(dir, &ctx, &cursor))) {
if (!cursor || !cursor->inode)
continue;
indent(ind);
kprintf("[inode] %s\n", cursor->name);
if (cursor->inode->mode & S_IFDIR) {
printf_dir_recursive(cursor, ind + 2);
} else {
indent(ind + 1);
char *buf = kalloc(cursor->inode->file_size + 1, ALLOC_KERN);
buf[cursor->inode->file_size] = 0;
vfs_read(cursor->inode, buf, cursor->inode->file_size, 0);
kprintf("%s", buf);
kfree(buf);
}
}
}
static void kmain()
{
/* data types */
@ -77,6 +114,11 @@ static void kmain()
if (res < 0)
panic("failed to load initramfs: %s\n", strerror(-res));
krdy("initramfs loaded\n");
/* read entire initramfs filesystem */
printf_dir_recursive(initramfs->root_dirent, 0);
/* do nothing, for now :) */
yield();
}