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