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