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