2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2022-02-03 01:44:46 +03:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2021-05-12 22:17:51 +03:00
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-09-15 13:24:14 +03:00
|
|
|
#include <AK/MemoryStream.h>
|
2021-09-12 14:29:28 +03:00
|
|
|
#include <Kernel/API/POSIX/errno.h>
|
2019-05-30 14:39:17 +03:00
|
|
|
#include <Kernel/Devices/BlockDevice.h>
|
2019-05-30 19:58:59 +03:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2019-04-06 21:29:48 +03:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
2019-05-30 14:39:17 +03:00
|
|
|
#include <Kernel/FileSystem/InodeFile.h>
|
2021-05-12 22:17:51 +03:00
|
|
|
#include <Kernel/FileSystem/InodeWatcher.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2022-08-19 18:16:06 +03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2019-04-06 21:29:48 +03:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-02-16 11:57:42 +03:00
|
|
|
#include <Kernel/Process.h>
|
2019-05-30 14:39:17 +03:00
|
|
|
#include <Kernel/TTY/MasterPTY.h>
|
|
|
|
#include <Kernel/TTY/TTY.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-11-07 14:05:51 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<OpenFileDescription>> OpenFileDescription::try_create(Custody& custody)
|
2018-11-05 21:01:22 +03:00
|
|
|
{
|
2021-09-05 15:43:51 +03:00
|
|
|
auto inode_file = TRY(InodeFile::create(custody.inode()));
|
2022-08-19 21:53:40 +03:00
|
|
|
auto description = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) OpenFileDescription(move(inode_file))));
|
2021-05-11 13:55:20 +03:00
|
|
|
|
2022-08-21 02:04:35 +03:00
|
|
|
description->m_state.with([&](auto& state) { state.custody = custody; });
|
2021-09-05 22:33:10 +03:00
|
|
|
TRY(description->attach());
|
2021-09-05 15:43:51 +03:00
|
|
|
return description;
|
2019-01-16 14:57:07 +03:00
|
|
|
}
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<OpenFileDescription>> OpenFileDescription::try_create(File& file)
|
2019-01-16 14:57:07 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
auto description = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) OpenFileDescription(file)));
|
2021-09-05 22:33:10 +03:00
|
|
|
TRY(description->attach());
|
2021-09-05 15:43:51 +03:00
|
|
|
return description;
|
2019-02-14 16:17:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
OpenFileDescription::OpenFileDescription(File& file)
|
2019-08-11 10:32:21 +03:00
|
|
|
: m_file(file)
|
2019-02-14 16:17:38 +03:00
|
|
|
{
|
2019-08-11 10:32:21 +03:00
|
|
|
if (file.is_inode())
|
|
|
|
m_inode = static_cast<InodeFile&>(file).inode();
|
2020-09-17 22:51:09 +03:00
|
|
|
|
2022-02-03 01:44:46 +03:00
|
|
|
auto metadata = this->metadata();
|
|
|
|
m_state.with([&](auto& state) { state.is_directory = metadata.is_directory(); });
|
2019-02-14 16:17:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
OpenFileDescription::~OpenFileDescription()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-09-17 22:51:09 +03:00
|
|
|
m_file->detach(*this);
|
2019-04-29 05:55:54 +03:00
|
|
|
if (is_fifo())
|
2022-02-03 01:44:46 +03:00
|
|
|
static_cast<FIFO*>(m_file.ptr())->detach(fifo_direction());
|
2020-08-05 12:13:30 +03:00
|
|
|
// FIXME: Should this error path be observed somehow?
|
2020-09-17 22:51:09 +03:00
|
|
|
(void)m_file->close();
|
|
|
|
if (m_inode)
|
|
|
|
m_inode->detach(*this);
|
2021-07-19 08:29:56 +03:00
|
|
|
|
|
|
|
if (m_inode)
|
|
|
|
m_inode->remove_flocks_for_description(*this);
|
2020-09-17 22:51:09 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::attach()
|
2020-09-17 22:51:09 +03:00
|
|
|
{
|
2021-09-05 17:12:39 +03:00
|
|
|
if (m_inode)
|
|
|
|
TRY(m_inode->attach(*this));
|
2020-09-17 22:51:09 +03:00
|
|
|
return m_file->attach(*this);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-14 05:04:56 +03:00
|
|
|
void OpenFileDescription::set_original_custody(Badge<VirtualFileSystem>, Custody& custody)
|
|
|
|
{
|
2022-08-21 02:04:35 +03:00
|
|
|
m_state.with([&](auto& state) { state.custody = custody; });
|
2021-08-14 05:04:56 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
Thread::FileBlocker::BlockFlags OpenFileDescription::should_unblock(Thread::FileBlocker::BlockFlags block_flags) const
|
2020-11-30 02:05:27 +03:00
|
|
|
{
|
2021-03-07 14:01:11 +03:00
|
|
|
using BlockFlags = Thread::FileBlocker::BlockFlags;
|
|
|
|
BlockFlags unblock_flags = BlockFlags::None;
|
|
|
|
if (has_flag(block_flags, BlockFlags::Read) && can_read())
|
|
|
|
unblock_flags |= BlockFlags::Read;
|
|
|
|
if (has_flag(block_flags, BlockFlags::Write) && can_write())
|
|
|
|
unblock_flags |= BlockFlags::Write;
|
2020-11-30 02:05:27 +03:00
|
|
|
// TODO: Implement Thread::FileBlocker::BlockFlags::Exception
|
|
|
|
|
2021-07-14 14:59:22 +03:00
|
|
|
if (has_any_flag(block_flags, BlockFlags::SocketFlags)) {
|
2021-12-15 16:55:18 +03:00
|
|
|
auto const* sock = socket();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(sock);
|
2021-03-07 14:01:11 +03:00
|
|
|
if (has_flag(block_flags, BlockFlags::Accept) && sock->can_accept())
|
|
|
|
unblock_flags |= BlockFlags::Accept;
|
|
|
|
if (has_flag(block_flags, BlockFlags::Connect) && sock->setup_state() == Socket::SetupState::Completed)
|
|
|
|
unblock_flags |= BlockFlags::Connect;
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
2021-03-07 14:01:11 +03:00
|
|
|
return unblock_flags;
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
|
|
|
|
2021-12-17 13:22:27 +03:00
|
|
|
ErrorOr<struct stat> OpenFileDescription::stat()
|
2018-10-14 23:57:41 +03:00
|
|
|
{
|
2021-06-11 11:55:43 +03:00
|
|
|
// FIXME: This is due to the Device class not overriding File::stat().
|
|
|
|
if (m_inode)
|
2021-12-17 13:22:27 +03:00
|
|
|
return m_inode->metadata().stat();
|
|
|
|
return m_file->stat();
|
2018-10-14 23:57:41 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<off_t> OpenFileDescription::seek(off_t offset, int whence)
|
2018-10-14 22:19:27 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
if (!m_file->is_seekable())
|
2021-03-19 12:43:58 +03:00
|
|
|
return ESPIPE;
|
2018-10-14 22:19:27 +03:00
|
|
|
|
2022-02-03 01:44:46 +03:00
|
|
|
auto metadata = this->metadata();
|
2018-10-14 22:19:27 +03:00
|
|
|
|
2022-02-03 01:44:46 +03:00
|
|
|
auto new_offset = TRY(m_state.with([&](auto& state) -> ErrorOr<off_t> {
|
|
|
|
off_t new_offset;
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
|
|
|
new_offset = offset;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
if (Checked<off_t>::addition_would_overflow(state.current_offset, offset))
|
|
|
|
return EOVERFLOW;
|
|
|
|
new_offset = state.current_offset + offset;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
if (!metadata.is_valid())
|
|
|
|
return EIO;
|
|
|
|
if (Checked<off_t>::addition_would_overflow(metadata.size, offset))
|
|
|
|
return EOVERFLOW;
|
|
|
|
new_offset = metadata.size + offset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if (new_offset < 0)
|
|
|
|
return EINVAL;
|
|
|
|
state.current_offset = new_offset;
|
|
|
|
return new_offset;
|
|
|
|
}));
|
2018-10-14 22:19:27 +03:00
|
|
|
|
2021-03-19 12:43:58 +03:00
|
|
|
// FIXME: Return EINVAL if attempting to seek past the end of a seekable device.
|
2019-05-16 16:44:01 +03:00
|
|
|
|
2020-09-17 22:51:09 +03:00
|
|
|
m_file->did_seek(*this, new_offset);
|
|
|
|
if (m_inode)
|
|
|
|
m_inode->did_seek(*this, new_offset);
|
2020-11-30 02:05:27 +03:00
|
|
|
evaluate_block_conditions();
|
2022-02-03 01:44:46 +03:00
|
|
|
return new_offset;
|
2018-10-14 22:19:27 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<size_t> OpenFileDescription::read(UserOrKernelBuffer& buffer, u64 offset, size_t count)
|
2021-07-16 01:35:48 +03:00
|
|
|
{
|
|
|
|
if (Checked<u64>::addition_would_overflow(offset, count))
|
|
|
|
return EOVERFLOW;
|
|
|
|
return m_file->read(*this, offset, buffer, count);
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<size_t> OpenFileDescription::write(u64 offset, UserOrKernelBuffer const& data, size_t data_size)
|
2021-07-16 01:35:48 +03:00
|
|
|
{
|
|
|
|
if (Checked<u64>::addition_would_overflow(offset, data_size))
|
|
|
|
return EOVERFLOW;
|
|
|
|
return m_file->write(*this, offset, data, data_size);
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<size_t> OpenFileDescription::read(UserOrKernelBuffer& buffer, size_t count)
|
2018-10-14 22:19:27 +03:00
|
|
|
{
|
2022-02-03 01:44:46 +03:00
|
|
|
auto offset = TRY(m_state.with([&](auto& state) -> ErrorOr<off_t> {
|
|
|
|
if (Checked<off_t>::addition_would_overflow(state.current_offset, count))
|
|
|
|
return EOVERFLOW;
|
|
|
|
return state.current_offset;
|
|
|
|
}));
|
|
|
|
auto nread = TRY(m_file->read(*this, offset, buffer, count));
|
2021-09-05 15:43:51 +03:00
|
|
|
if (m_file->is_seekable())
|
2022-02-03 01:44:46 +03:00
|
|
|
m_state.with([&](auto& state) { state.current_offset = offset + nread; });
|
2021-09-05 15:43:51 +03:00
|
|
|
evaluate_block_conditions();
|
|
|
|
return nread;
|
2018-10-14 22:19:27 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> OpenFileDescription::write(UserOrKernelBuffer const& data, size_t size)
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2022-02-03 01:44:46 +03:00
|
|
|
auto offset = TRY(m_state.with([&](auto& state) -> ErrorOr<off_t> {
|
|
|
|
if (Checked<off_t>::addition_would_overflow(state.current_offset, size))
|
|
|
|
return EOVERFLOW;
|
|
|
|
return state.current_offset;
|
|
|
|
}));
|
|
|
|
auto nwritten = TRY(m_file->write(*this, offset, data, size));
|
|
|
|
|
2021-09-05 15:43:51 +03:00
|
|
|
if (m_file->is_seekable())
|
2022-02-03 01:44:46 +03:00
|
|
|
m_state.with([&](auto& state) { state.current_offset = offset + nwritten; });
|
|
|
|
|
2021-09-05 15:43:51 +03:00
|
|
|
evaluate_block_conditions();
|
|
|
|
return nwritten;
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::can_write() const
|
2018-11-12 03:28:46 +03:00
|
|
|
{
|
2020-04-10 12:44:42 +03:00
|
|
|
return m_file->can_write(*this, offset());
|
2018-11-12 03:28:46 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::can_read() const
|
2018-10-25 14:07:59 +03:00
|
|
|
{
|
2020-04-10 12:44:42 +03:00
|
|
|
return m_file->can_read(*this, offset());
|
2018-10-25 14:07:59 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KBuffer>> OpenFileDescription::read_entire_file()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-04-28 16:02:55 +03:00
|
|
|
// HACK ALERT: (This entire function)
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_file->is_inode());
|
|
|
|
VERIFY(m_inode);
|
2019-01-16 14:57:07 +03:00
|
|
|
return m_inode->read_entire(this);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<size_t> OpenFileDescription::get_dir_entries(UserOrKernelBuffer& output_buffer, size_t size)
|
2018-10-24 13:43:52 +03:00
|
|
|
{
|
2019-10-25 10:22:22 +03:00
|
|
|
if (!is_directory())
|
2021-05-11 14:13:04 +03:00
|
|
|
return ENOTDIR;
|
2019-10-25 10:22:22 +03:00
|
|
|
|
2019-01-16 14:57:07 +03:00
|
|
|
auto metadata = this->metadata();
|
2019-01-31 19:31:23 +03:00
|
|
|
if (!metadata.is_valid())
|
2021-05-11 14:13:04 +03:00
|
|
|
return EIO;
|
2018-10-24 13:43:52 +03:00
|
|
|
|
2021-05-11 19:35:36 +03:00
|
|
|
size_t remaining = size;
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> error;
|
2021-05-11 19:35:36 +03:00
|
|
|
u8 stack_buffer[PAGE_SIZE];
|
|
|
|
Bytes temp_buffer(stack_buffer, sizeof(stack_buffer));
|
2020-09-15 13:24:14 +03:00
|
|
|
OutputMemoryStream stream { temp_buffer };
|
|
|
|
|
2021-05-11 19:35:36 +03:00
|
|
|
auto flush_stream_to_output_buffer = [&error, &stream, &remaining, &output_buffer]() -> bool {
|
2021-08-14 16:15:11 +03:00
|
|
|
if (error.is_error())
|
2021-05-11 19:35:36 +03:00
|
|
|
return false;
|
|
|
|
if (stream.size() == 0)
|
|
|
|
return true;
|
|
|
|
if (remaining < stream.size()) {
|
2021-05-11 14:13:04 +03:00
|
|
|
error = EINVAL;
|
2021-05-11 19:35:36 +03:00
|
|
|
return false;
|
2021-09-07 13:09:52 +03:00
|
|
|
}
|
|
|
|
if (auto result = output_buffer.write(stream.bytes()); result.is_error()) {
|
|
|
|
error = result.release_error();
|
2021-05-11 19:35:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
output_buffer = output_buffer.offset(stream.size());
|
|
|
|
remaining -= stream.size();
|
|
|
|
stream.reset();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-11-10 17:42:39 +03:00
|
|
|
ErrorOr<void> result = VirtualFileSystem::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &error, &stream, this](auto& entry) -> ErrorOr<void> {
|
2021-05-11 19:35:36 +03:00
|
|
|
size_t serialized_size = sizeof(ino_t) + sizeof(u8) + sizeof(size_t) + sizeof(char) * entry.name.length();
|
|
|
|
if (serialized_size > stream.remaining()) {
|
2021-11-10 17:42:39 +03:00
|
|
|
if (!flush_stream_to_output_buffer())
|
|
|
|
return error;
|
2021-05-11 19:35:36 +03:00
|
|
|
}
|
2021-08-08 21:23:27 +03:00
|
|
|
stream << (u64)entry.inode.index().value();
|
2020-08-29 21:25:01 +03:00
|
|
|
stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
|
2020-08-18 13:41:27 +03:00
|
|
|
stream << (u32)entry.name.length();
|
2020-09-15 13:24:14 +03:00
|
|
|
stream << entry.name.bytes();
|
2021-11-10 17:42:39 +03:00
|
|
|
return {};
|
2018-10-24 13:43:52 +03:00
|
|
|
});
|
2021-05-11 19:35:36 +03:00
|
|
|
flush_stream_to_output_buffer();
|
2020-08-05 12:07:31 +03:00
|
|
|
|
2021-05-11 19:35:36 +03:00
|
|
|
if (result.is_error()) {
|
2021-08-06 01:35:27 +03:00
|
|
|
// We should only return EFAULT when the userspace buffer is too small,
|
2021-05-11 19:35:36 +03:00
|
|
|
// so that userspace can reliably use it as a signal to increase its
|
|
|
|
// buffer size.
|
2021-11-08 02:51:39 +03:00
|
|
|
VERIFY(result.error().code() != EFAULT);
|
|
|
|
return result.release_error();
|
2021-05-11 19:35:36 +03:00
|
|
|
}
|
2020-08-05 12:07:31 +03:00
|
|
|
|
2021-08-14 16:15:11 +03:00
|
|
|
if (error.is_error())
|
2021-11-08 02:51:39 +03:00
|
|
|
return error.release_error();
|
2021-05-11 19:35:36 +03:00
|
|
|
return size - remaining;
|
2018-10-24 13:43:52 +03:00
|
|
|
}
|
2019-01-16 14:57:07 +03:00
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::is_device() const
|
2019-04-28 16:02:55 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_device();
|
2019-04-28 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Device const* OpenFileDescription::device() const
|
2020-01-12 18:28:23 +03:00
|
|
|
{
|
|
|
|
if (!is_device())
|
|
|
|
return nullptr;
|
2022-04-01 20:58:27 +03:00
|
|
|
return static_cast<Device const*>(m_file.ptr());
|
2020-01-12 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
Device* OpenFileDescription::device()
|
2020-01-12 18:28:23 +03:00
|
|
|
{
|
|
|
|
if (!is_device())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<Device*>(m_file.ptr());
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::is_tty() const
|
2018-10-31 00:03:02 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_tty();
|
2018-10-31 00:03:02 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
const TTY* OpenFileDescription::tty() const
|
2018-10-31 00:03:02 +03:00
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
if (!is_tty())
|
2018-11-12 03:28:46 +03:00
|
|
|
return nullptr;
|
2019-04-28 16:02:55 +03:00
|
|
|
return static_cast<const TTY*>(m_file.ptr());
|
2018-10-31 00:03:02 +03:00
|
|
|
}
|
2018-11-01 16:00:28 +03:00
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
TTY* OpenFileDescription::tty()
|
2018-11-02 15:14:25 +03:00
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
if (!is_tty())
|
2018-11-12 03:28:46 +03:00
|
|
|
return nullptr;
|
2019-04-28 16:02:55 +03:00
|
|
|
return static_cast<TTY*>(m_file.ptr());
|
2018-11-02 15:14:25 +03:00
|
|
|
}
|
2019-01-15 08:30:19 +03:00
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::is_inode_watcher() const
|
2021-05-12 22:17:51 +03:00
|
|
|
{
|
|
|
|
return m_file->is_inode_watcher();
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
InodeWatcher const* OpenFileDescription::inode_watcher() const
|
2021-05-12 22:17:51 +03:00
|
|
|
{
|
|
|
|
if (!is_inode_watcher())
|
|
|
|
return nullptr;
|
2022-04-01 20:58:27 +03:00
|
|
|
return static_cast<InodeWatcher const*>(m_file.ptr());
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
InodeWatcher* OpenFileDescription::inode_watcher()
|
2021-05-12 22:17:51 +03:00
|
|
|
{
|
|
|
|
if (!is_inode_watcher())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<InodeWatcher*>(m_file.ptr());
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::is_master_pty() const
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_master_pty();
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
MasterPTY const* OpenFileDescription::master_pty() const
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2022-04-01 20:58:27 +03:00
|
|
|
return static_cast<MasterPTY const*>(m_file.ptr());
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
MasterPTY* OpenFileDescription::master_pty()
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2019-04-28 16:02:55 +03:00
|
|
|
return static_cast<MasterPTY*>(m_file.ptr());
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
2018-11-02 15:14:25 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::close()
|
2018-11-01 16:00:28 +03:00
|
|
|
{
|
2021-04-30 11:33:33 +03:00
|
|
|
if (m_file->attach_count() > 0)
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2020-06-02 19:20:05 +03:00
|
|
|
return m_file->close();
|
2018-11-01 16:00:28 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> OpenFileDescription::original_absolute_path() const
|
2021-09-07 14:16:38 +03:00
|
|
|
{
|
2022-08-21 02:04:35 +03:00
|
|
|
if (auto custody = this->custody())
|
|
|
|
return custody->try_serialize_absolute_path();
|
|
|
|
return ENOENT;
|
2021-09-07 14:16:38 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> OpenFileDescription::pseudo_path() const
|
2018-11-01 16:00:28 +03:00
|
|
|
{
|
2022-08-21 02:04:35 +03:00
|
|
|
if (auto custody = this->custody())
|
|
|
|
return custody->try_serialize_absolute_path();
|
2021-10-30 01:45:23 +03:00
|
|
|
return m_file->pseudo_path(*this);
|
2018-11-01 16:00:28 +03:00
|
|
|
}
|
2018-11-12 03:28:46 +03:00
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
InodeMetadata OpenFileDescription::metadata() const
|
2019-01-16 14:57:07 +03:00
|
|
|
{
|
|
|
|
if (m_inode)
|
|
|
|
return m_inode->metadata();
|
2019-06-07 12:43:58 +03:00
|
|
|
return {};
|
2019-01-16 14:57:07 +03:00
|
|
|
}
|
2019-02-16 11:57:42 +03:00
|
|
|
|
2022-08-23 19:51:18 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> OpenFileDescription::vmobject_for_mmap(Process& process, Memory::VirtualRange const& range, u64& offset, bool shared)
|
2019-02-16 11:57:42 +03:00
|
|
|
{
|
2022-08-23 19:51:18 +03:00
|
|
|
return m_file->vmobject_for_mmap(process, range, offset, shared);
|
2019-02-16 11:57:42 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::truncate(u64 length)
|
2019-04-09 02:10:00 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->truncate(length);
|
2019-04-09 02:10:00 +03:00
|
|
|
}
|
2019-04-28 23:31:31 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::sync()
|
2021-09-12 06:28:59 +03:00
|
|
|
{
|
|
|
|
return m_file->sync();
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::is_fifo() const
|
2019-04-29 05:55:54 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_fifo();
|
2019-04-29 05:55:54 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
FIFO* OpenFileDescription::fifo()
|
2019-04-29 05:55:54 +03:00
|
|
|
{
|
|
|
|
if (!is_fifo())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<FIFO*>(m_file.ptr());
|
|
|
|
}
|
2019-05-03 21:42:43 +03:00
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
bool OpenFileDescription::is_socket() const
|
2019-05-03 21:42:43 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_socket();
|
2019-05-03 21:42:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
Socket* OpenFileDescription::socket()
|
2019-05-03 21:42:43 +03:00
|
|
|
{
|
|
|
|
if (!is_socket())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<Socket*>(m_file.ptr());
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Socket const* OpenFileDescription::socket() const
|
2019-05-03 21:42:43 +03:00
|
|
|
{
|
|
|
|
if (!is_socket())
|
|
|
|
return nullptr;
|
2022-04-01 20:58:27 +03:00
|
|
|
return static_cast<Socket const*>(m_file.ptr());
|
2019-05-03 21:42:43 +03:00
|
|
|
}
|
2019-05-30 16:37:51 +03:00
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
void OpenFileDescription::set_file_flags(u32 flags)
|
2019-05-30 16:37:51 +03:00
|
|
|
{
|
2022-02-03 01:44:46 +03:00
|
|
|
m_state.with([&](auto& state) {
|
|
|
|
state.is_blocking = !(flags & O_NONBLOCK);
|
|
|
|
state.should_append = flags & O_APPEND;
|
|
|
|
state.direct = flags & O_DIRECT;
|
|
|
|
state.file_flags = flags;
|
|
|
|
});
|
2019-05-30 16:37:51 +03:00
|
|
|
}
|
2019-06-01 21:31:36 +03:00
|
|
|
|
2022-08-21 17:15:29 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::chmod(Credentials const& credentials, mode_t mode)
|
2020-01-03 22:14:56 +03:00
|
|
|
{
|
2022-08-21 17:15:29 +03:00
|
|
|
return m_file->chmod(credentials, *this, mode);
|
2020-01-03 22:14:56 +03:00
|
|
|
}
|
|
|
|
|
2022-08-21 17:15:29 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::chown(Credentials const& credentials, UserID uid, GroupID gid)
|
2019-06-01 21:31:36 +03:00
|
|
|
{
|
2022-08-21 17:15:29 +03:00
|
|
|
return m_file->chown(credentials, *this, uid, gid);
|
2019-06-01 21:31:36 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
FileBlockerSet& OpenFileDescription::blocker_set()
|
2020-11-30 02:05:27 +03:00
|
|
|
{
|
2021-08-22 16:59:47 +03:00
|
|
|
return m_file->blocker_set();
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
|
|
|
|
2022-07-14 02:17:01 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::apply_flock(Process const& process, Userspace<flock const*> lock, ShouldBlock should_block)
|
2021-07-19 08:29:56 +03:00
|
|
|
{
|
|
|
|
if (!m_inode)
|
|
|
|
return EBADF;
|
|
|
|
|
2022-07-14 02:17:01 +03:00
|
|
|
return m_inode->apply_flock(process, *this, lock, should_block);
|
2021-07-19 08:29:56 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> OpenFileDescription::get_flock(Userspace<flock*> lock) const
|
2021-07-19 08:29:56 +03:00
|
|
|
{
|
|
|
|
if (!m_inode)
|
|
|
|
return EBADF;
|
|
|
|
|
|
|
|
return m_inode->get_flock(*this, lock);
|
|
|
|
}
|
2022-02-03 01:44:46 +03:00
|
|
|
bool OpenFileDescription::is_readable() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.readable; });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenFileDescription::is_writable() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.writable; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFileDescription::set_readable(bool b)
|
|
|
|
{
|
|
|
|
m_state.with([&](auto& state) { state.readable = b; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFileDescription::set_writable(bool b)
|
|
|
|
{
|
|
|
|
m_state.with([&](auto& state) { state.writable = b; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFileDescription::set_rw_mode(int options)
|
|
|
|
{
|
|
|
|
m_state.with([&](auto& state) {
|
|
|
|
state.readable = (options & O_RDONLY) == O_RDONLY;
|
|
|
|
state.writable = (options & O_WRONLY) == O_WRONLY;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenFileDescription::is_direct() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.direct; });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenFileDescription::is_directory() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.is_directory; });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenFileDescription::is_blocking() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.is_blocking; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFileDescription::set_blocking(bool b)
|
|
|
|
{
|
|
|
|
m_state.with([&](auto& state) { state.is_blocking = b; });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenFileDescription::should_append() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.should_append; });
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 OpenFileDescription::file_flags() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.file_flags; });
|
|
|
|
}
|
|
|
|
|
|
|
|
FIFO::Direction OpenFileDescription::fifo_direction() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.fifo_direction; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFileDescription::set_fifo_direction(Badge<FIFO>, FIFO::Direction direction)
|
|
|
|
{
|
|
|
|
m_state.with([&](auto& state) { state.fifo_direction = direction; });
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr<OpenFileDescriptionData>& OpenFileDescription::data()
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) -> OwnPtr<OpenFileDescriptionData>& { return state.data; });
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t OpenFileDescription::offset() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.current_offset; });
|
|
|
|
}
|
|
|
|
|
2022-08-21 02:04:35 +03:00
|
|
|
RefPtr<Custody const> OpenFileDescription::custody() const
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.custody; });
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Custody> OpenFileDescription::custody()
|
|
|
|
{
|
|
|
|
return m_state.with([](auto& state) { return state.custody; });
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|