#include "vfs.h"
#include <drivers/devfs/devfs.h>

#include <lib/memory.h>
#include <lib/string.h>
#include <lib/print.h>

static vfs_node_t* devfs_find(vfs_node_t* parent, const char* name) {
    vfs_node_t* cur = parent->first_child;
    if (!cur) return NULL;

    while (cur) {
        if (strcmp(cur->name, name) == 0) break;
        cur = cur->next_sibling;
    }

    return cur;
}

static vfs_node_t* devfs_create(vfs_node_t* parent, const char* name) {
    vfs_node_t* node = kalloc(sizeof(vfs_node_t*));
    memset((void*)node, 0, sizeof(vfs_node_t));
    strcpy(node->name, name);
    node->flags = VFS_CHARDEVICE;
    if (parent->first_child) {
        node->next_sibling = parent->first_child;
    }
    parent->first_child = node;
    return node;
}

vfs_node_t* create_devfs() {
    vfs_node_t* root = kalloc(sizeof(vfs_node_t*));
    memset((void*)root, 0, sizeof(vfs_node_t));
    strcpy(root->name, "dev");
    root->find = devfs_find;
    root->flags = VFS_DIRECTORY;
    root->create = devfs_create;
    return root;
}