Kernel/LibC: Add syscall and wrapper for unlink

This commit is contained in:
Bananymous 2023-10-25 21:45:04 +03:00
parent b7007016c0
commit 87ff38664a
5 changed files with 31 additions and 1 deletions

View File

@ -98,6 +98,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_write(int fd, const void* buffer, size_t count);
BAN::ErrorOr<long> sys_create(const char*, mode_t);
BAN::ErrorOr<long> sys_create_dir(const char*, mode_t);
BAN::ErrorOr<long> sys_unlink(const char*);
BAN::ErrorOr<long> sys_chmod(const char*, mode_t);

View File

@ -634,7 +634,7 @@ namespace Kernel
auto directory = absolute_path.sv().substring(0, index);
auto file_name = absolute_path.sv().substring(index);
auto parent_inode = TRY(VirtualFileSystem::get().file_from_absolute_path(m_credentials, directory, O_WRONLY)).inode;
auto parent_inode = TRY(VirtualFileSystem::get().file_from_absolute_path(m_credentials, directory, O_EXEC | O_WRONLY)).inode;
if (Inode::Mode(mode).ifdir())
TRY(parent_inode->create_directory(file_name, mode, m_credentials.euid(), m_credentials.egid()));
@ -762,6 +762,26 @@ namespace Kernel
return 0;
}
BAN::ErrorOr<long> Process::sys_unlink(const char* path)
{
LockGuard _(m_lock);
validate_string_access(path);
auto absolute_path = TRY(absolute_path_of(path));
size_t index = absolute_path.size();
for (; index > 0; index--)
if (absolute_path[index - 1] == '/')
break;
auto directory = absolute_path.sv().substring(0, index);
auto file_name = absolute_path.sv().substring(index);
auto parent = TRY(VirtualFileSystem::get().file_from_absolute_path(m_credentials, directory, O_EXEC | O_WRONLY)).inode;
TRY(parent->unlink(file_name));
return 0;
}
BAN::ErrorOr<long> Process::sys_chmod(const char* path, mode_t mode)
{
if (mode & S_IFMASK)

View File

@ -208,6 +208,9 @@ namespace Kernel
case SYS_CREATE_DIR:
ret = Process::current().sys_create_dir((const char*)arg1, (mode_t)arg2);
break;
case SYS_UNLINK:
ret = Process::current().sys_unlink((const char*)arg1);
break;
default:
dwarnln("Unknown syscall {}", syscall);
break;

View File

@ -58,6 +58,7 @@ __BEGIN_DECLS
#define SYS_CHMOD 55
#define SYS_CREATE 56 // creat, mkfifo
#define SYS_CREATE_DIR 57 // mkdir
#define SYS_UNLINK 58
__END_DECLS

View File

@ -210,6 +210,11 @@ void syncsync(int should_block)
syscall(SYS_SYNC, should_block);
}
int unlink(const char* path)
{
return syscall(SYS_UNLINK, path);
}
pid_t getpid(void)
{
return syscall(SYS_GET_PID);