Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/StringView.h>
8#include <Kernel/FileSystem/VirtualFileSystem.h>
9#include <Kernel/Process.h>
10
11namespace Kernel {
12
13ErrorOr<FlatPtr> Process::sys$unlink(int dirfd, Userspace<char const*> user_path, size_t path_length, int flags)
14{
15 VERIFY_NO_PROCESS_BIG_LOCK(this);
16 TRY(require_promise(Pledge::cpath));
17 auto path = TRY(get_syscall_path_argument(user_path, path_length));
18
19 if (flags & ~AT_REMOVEDIR)
20 return Error::from_errno(EINVAL);
21
22 RefPtr<Custody> base;
23 if (dirfd == AT_FDCWD) {
24 base = current_directory();
25 } else {
26 auto base_description = TRY(open_file_description(dirfd));
27 if (!base_description->custody())
28 return Error::from_errno(EINVAL);
29 base = base_description->custody();
30 }
31
32 if (flags & AT_REMOVEDIR)
33 TRY(VirtualFileSystem::the().rmdir(credentials(), path->view(), *base));
34 else
35 TRY(VirtualFileSystem::the().unlink(credentials(), path->view(), *base));
36 return 0;
37}
38
39}