33 lines
917 B
C++
33 lines
917 B
C++
|
#include "memory/MemoryManager.h"
|
||
|
#include "sys/Syscall.h"
|
||
|
#include "thread/Scheduler.h"
|
||
|
#include <luna/PathParser.h>
|
||
|
|
||
|
Result<u64> sys_unlink(Registers*, SyscallArgs args)
|
||
|
{
|
||
|
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
|
||
|
int flags = (int)args[1];
|
||
|
|
||
|
Thread* current = Scheduler::current();
|
||
|
|
||
|
PathParser parser = TRY(PathParser::create(path.chars()));
|
||
|
|
||
|
auto dirname = TRY(parser.dirname());
|
||
|
auto basename = TRY(parser.basename());
|
||
|
|
||
|
if (basename.view() == ".") return err(EINVAL);
|
||
|
|
||
|
auto inode = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
|
||
|
if (!VFS::can_write(inode, current->auth)) return err(EACCES);
|
||
|
|
||
|
if (flags > 0)
|
||
|
{
|
||
|
auto child = TRY(inode->find(basename.chars()));
|
||
|
if (child->type() != VFS::InodeType::Directory) return err(ENOTDIR);
|
||
|
}
|
||
|
|
||
|
TRY(inode->remove_entry(basename.chars()));
|
||
|
|
||
|
return 0;
|
||
|
}
|