2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2021-05-14 18:34:31 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$fstat(int fd, Userspace<stat*> user_statbuf)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2021-06-22 18:22:17 +00:00
|
|
|
auto description = fds().file_description(fd);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!description)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-08-09 21:57:50 +00:00
|
|
|
stat buffer = {};
|
2021-08-14 13:15:11 +00:00
|
|
|
auto result = description->stat(buffer);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(user_statbuf, &buffer))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-08-14 13:15:11 +00:00
|
|
|
return result;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$stat(Userspace<const Syscall::SC_stat_params*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
REQUIRE_PROMISE(rpath);
|
|
|
|
Syscall::SC_stat_params params;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
auto path = get_syscall_path_argument(params.path);
|
|
|
|
if (path.is_error())
|
|
|
|
return path.error();
|
2021-05-14 18:34:31 +00:00
|
|
|
RefPtr<Custody> base;
|
|
|
|
if (params.dirfd == AT_FDCWD) {
|
|
|
|
base = current_directory();
|
|
|
|
} else {
|
2021-06-22 18:22:17 +00:00
|
|
|
auto base_description = fds().file_description(params.dirfd);
|
2021-05-14 18:34:31 +00:00
|
|
|
if (!base_description)
|
|
|
|
return EBADF;
|
|
|
|
if (!base_description->is_directory())
|
|
|
|
return ENOTDIR;
|
|
|
|
if (!base_description->custody())
|
|
|
|
return EINVAL;
|
|
|
|
base = base_description->custody();
|
|
|
|
}
|
2021-07-10 22:25:24 +00:00
|
|
|
auto metadata_or_error = VirtualFileSystem::the().lookup_metadata(path.value()->view(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (metadata_or_error.is_error())
|
|
|
|
return metadata_or_error.error();
|
|
|
|
stat statbuf;
|
|
|
|
auto result = metadata_or_error.value().stat(statbuf);
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(params.statbuf, &statbuf))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|