Kernel: Add Inode API for creating directories

This commit is contained in:
Bananymous 2023-10-25 19:36:04 +03:00
parent fd1b331b86
commit 18e90d305d
4 changed files with 32 additions and 4 deletions

View File

@ -90,6 +90,7 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode(BAN::StringView);
BAN::ErrorOr<void> list_next_inodes(off_t, DirectoryEntryList*, size_t);
BAN::ErrorOr<void> create_file(BAN::StringView, mode_t, uid_t, gid_t);
BAN::ErrorOr<void> create_directory(BAN::StringView, mode_t, uid_t, gid_t);
BAN::ErrorOr<void> delete_inode(BAN::StringView);
// Link API
@ -107,6 +108,7 @@ namespace Kernel
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> create_directory_impl(BAN::StringView, mode_t, uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> delete_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
// Link API

View File

@ -89,7 +89,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_getegid() const { return m_credentials.egid(); }
BAN::ErrorOr<long> sys_getpgid(pid_t);
BAN::ErrorOr<void> create_file(BAN::StringView name, mode_t mode);
BAN::ErrorOr<void> create_file_or_dir(BAN::StringView name, mode_t mode);
BAN::ErrorOr<long> open_file(BAN::StringView path, int, mode_t = 0);
BAN::ErrorOr<long> sys_open(const char* path, int, mode_t);
BAN::ErrorOr<long> sys_openat(int, const char* path, int, mode_t);

View File

@ -81,9 +81,22 @@ namespace Kernel
Thread::TerminateBlocker blocker(Thread::current());
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (Mode(mode).ifdir())
return BAN::Error::from_errno(EINVAL);
return create_file_impl(name, mode, uid, gid);
}
BAN::ErrorOr<void> Inode::create_directory(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{
LockGuard _(m_lock);
Thread::TerminateBlocker blocker(Thread::current());
if (!this->mode().ifdir())
return BAN::Error::from_errno(ENOTDIR);
if (!Mode(mode).ifdir())
return BAN::Error::from_errno(EINVAL);
return create_directory_impl(name, mode, uid, gid);
}
BAN::ErrorOr<void> Inode::delete_inode(BAN::StringView name)
{
LockGuard _(m_lock);

View File

@ -611,8 +611,17 @@ namespace Kernel
return 0;
}
BAN::ErrorOr<void> Process::create_file(BAN::StringView path, mode_t mode)
BAN::ErrorOr<void> Process::create_file_or_dir(BAN::StringView path, mode_t mode)
{
switch (mode & Inode::Mode::TYPE_MASK)
{
case Inode::Mode::IFREG: break;
case Inode::Mode::IFDIR: break;
case Inode::Mode::IFIFO: break;
default:
return BAN::Error::from_errno(EINVAL);
}
LockGuard _(m_lock);
auto absolute_path = TRY(absolute_path_of(path));
@ -626,7 +635,11 @@ namespace Kernel
auto file_name = absolute_path.sv().substring(index);
auto parent_inode = TRY(VirtualFileSystem::get().file_from_absolute_path(m_credentials, directory, O_WRONLY)).inode;
TRY(parent_inode->create_file(file_name, S_IFREG | (mode & 0777), m_credentials.euid(), m_credentials.egid()));
if (Inode::Mode(mode).ifdir())
TRY(parent_inode->create_directory(file_name, mode, m_credentials.euid(), m_credentials.egid()));
else
TRY(parent_inode->create_file(file_name, mode, m_credentials.euid(), m_credentials.egid()));
return {};
}
@ -672,7 +685,7 @@ namespace Kernel
if (file_or_error.is_error())
{
if (file_or_error.error().get_error_code() == ENOENT)
TRY(create_file(path, mode));
TRY(create_file_or_dir(path, Inode::Mode::IFREG | mode));
else
return file_or_error.release_error();
}