#include "fs/Mount.h"

LinkedList<MountInode> g_mounts;

Result<SharedPtr<VFS::Inode>> MountInode::create(SharedPtr<VFS::Inode> source, SharedPtr<VFS::FileSystem> fs)
{
    auto inode = TRY(adopt_shared_if_nonnull(new (std::nothrow) MountInode()));

    inode->m_source = source;
    inode->m_mountee = fs;
    inode->m_mount_root_inode = fs->root_inode();

    auto parent = TRY(source->find(".."));
    TRY(fs->set_mount_dir(parent));

    source->add_handle();

    g_mounts.append(inode.ptr());

    return (SharedPtr<VFS::Inode>)inode;
}

MountInode::~MountInode()
{
    m_source->remove_handle();
}