2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2022-02-02 22:44:46 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-19 15:26:07 +00:00
|
|
|
#include <AK/AtomicRefCounted.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Badge.h>
|
2022-08-20 23:04:35 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2022-08-19 15:16:06 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2022-08-19 15:16:06 +00:00
|
|
|
#include <Kernel/Forward.h>
|
2023-02-24 18:10:59 +00:00
|
|
|
#include <Kernel/Library/KBuffer.h>
|
2023-02-24 17:54:30 +00:00
|
|
|
#include <Kernel/Memory/VirtualAddress.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
class OpenFileDescriptionData {
|
2020-09-17 19:51:09 +00:00
|
|
|
public:
|
2021-09-07 11:39:11 +00:00
|
|
|
virtual ~OpenFileDescriptionData() = default;
|
2020-09-17 19:51:09 +00:00
|
|
|
};
|
|
|
|
|
2022-08-19 15:26:07 +00:00
|
|
|
class OpenFileDescription final : public AtomicRefCounted<OpenFileDescription> {
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2023-03-06 18:29:25 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<OpenFileDescription>> try_create(Custody&);
|
|
|
|
static ErrorOr<NonnullRefPtr<OpenFileDescription>> try_create(File&);
|
2021-09-07 11:39:11 +00:00
|
|
|
~OpenFileDescription();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
Thread::FileBlocker::BlockFlags should_unblock(Thread::FileBlocker::BlockFlags) const;
|
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
bool is_readable() const;
|
|
|
|
void set_readable(bool);
|
2020-01-03 02:29:59 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
bool is_writable() const;
|
|
|
|
void set_writable(bool);
|
2020-01-03 02:29:59 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
void set_rw_mode(int options);
|
2020-01-03 02:29:59 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> close();
|
2018-11-01 13:00:28 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<off_t> seek(off_t, int whence);
|
|
|
|
ErrorOr<size_t> read(UserOrKernelBuffer&, size_t);
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<size_t> write(UserOrKernelBuffer const& data, size_t);
|
2021-12-17 10:22:27 +00:00
|
|
|
ErrorOr<struct stat> stat();
|
2018-10-14 19:19:27 +00:00
|
|
|
|
2021-07-15 22:35:48 +00:00
|
|
|
// NOTE: These ignore the current offset of this file description.
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<size_t> read(UserOrKernelBuffer&, u64 offset, size_t);
|
|
|
|
ErrorOr<size_t> write(u64 offset, UserOrKernelBuffer const&, size_t);
|
2021-07-15 22:35:48 +00:00
|
|
|
|
2022-08-21 14:15:29 +00:00
|
|
|
ErrorOr<void> chmod(Credentials const& credentials, mode_t);
|
2019-03-01 09:39:19 +00:00
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
bool can_read() const;
|
|
|
|
bool can_write() const;
|
2018-10-25 11:07:59 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<size_t> get_dir_entries(UserOrKernelBuffer& buffer, size_t);
|
2018-10-24 10:43:52 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> original_absolute_path() const;
|
|
|
|
ErrorOr<NonnullOwnPtr<KString>> pseudo_path() const;
|
2018-10-24 12:28:22 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
bool is_direct() const;
|
2019-11-05 18:35:12 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
bool is_directory() const;
|
2018-10-26 12:24:11 +00:00
|
|
|
|
2019-05-30 11:39:17 +00:00
|
|
|
File& file() { return *m_file; }
|
2022-04-01 17:58:27 +00:00
|
|
|
File const& file() const { return *m_file; }
|
2019-02-16 08:57:42 +00:00
|
|
|
|
2019-04-28 13:02:55 +00:00
|
|
|
bool is_device() const;
|
2022-04-01 17:58:27 +00:00
|
|
|
Device const* device() const;
|
2020-01-12 15:28:23 +00:00
|
|
|
Device* device();
|
2018-11-16 12:11:21 +00:00
|
|
|
|
2018-12-02 23:39:25 +00:00
|
|
|
bool is_tty() const;
|
2018-10-30 21:03:02 +00:00
|
|
|
const TTY* tty() const;
|
2018-11-02 12:14:25 +00:00
|
|
|
TTY* tty();
|
2019-01-15 05:30:19 +00:00
|
|
|
|
2021-05-12 19:17:51 +00:00
|
|
|
bool is_inode_watcher() const;
|
2022-04-01 17:58:27 +00:00
|
|
|
InodeWatcher const* inode_watcher() const;
|
2021-05-12 19:17:51 +00:00
|
|
|
InodeWatcher* inode_watcher();
|
|
|
|
|
2022-12-15 09:42:40 +00:00
|
|
|
bool is_mount_file() const;
|
|
|
|
MountFile const* mount_file() const;
|
|
|
|
MountFile* mount_file();
|
|
|
|
|
2019-01-15 05:30:19 +00:00
|
|
|
bool is_master_pty() const;
|
2022-04-01 17:58:27 +00:00
|
|
|
MasterPTY const* master_pty() const;
|
2019-01-15 05:30:19 +00:00
|
|
|
MasterPTY* master_pty();
|
2018-10-30 21:03:02 +00:00
|
|
|
|
2019-01-16 11:57:07 +00:00
|
|
|
InodeMetadata metadata() const;
|
|
|
|
Inode* inode() { return m_inode.ptr(); }
|
2022-04-01 17:58:27 +00:00
|
|
|
Inode const* inode() const { return m_inode.ptr(); }
|
2018-10-27 14:43:03 +00:00
|
|
|
|
2022-08-20 23:04:35 +00:00
|
|
|
RefPtr<Custody> custody();
|
|
|
|
RefPtr<Custody const> custody() const;
|
2019-05-30 16:58:59 +00:00
|
|
|
|
2022-08-23 16:51:18 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared);
|
2018-10-26 12:24:11 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
bool is_blocking() const;
|
|
|
|
void set_blocking(bool b);
|
2018-11-11 14:36:40 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
bool should_append() const;
|
|
|
|
|
|
|
|
u32 file_flags() const;
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_file_flags(u32);
|
2018-11-12 00:28:46 +00:00
|
|
|
|
2019-05-03 18:42:43 +00:00
|
|
|
bool is_socket() const;
|
|
|
|
Socket* socket();
|
2022-04-01 17:58:27 +00:00
|
|
|
Socket const* socket() const;
|
2019-02-14 13:17:38 +00:00
|
|
|
|
2019-04-29 02:55:54 +00:00
|
|
|
bool is_fifo() const;
|
|
|
|
FIFO* fifo();
|
2022-02-02 22:44:46 +00:00
|
|
|
FIFO::Direction fifo_direction() const;
|
|
|
|
void set_fifo_direction(Badge<FIFO>, FIFO::Direction direction);
|
2018-10-18 08:27:07 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
OwnPtr<OpenFileDescriptionData>& data();
|
2018-10-26 22:14:24 +00:00
|
|
|
|
2023-03-07 11:25:00 +00:00
|
|
|
void set_original_inode(Badge<VirtualFileSystem>, NonnullRefPtr<Inode> inode) { m_inode = move(inode); }
|
2021-08-14 02:04:56 +00:00
|
|
|
void set_original_custody(Badge<VirtualFileSystem>, Custody& custody);
|
2019-01-31 04:05:57 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> truncate(u64);
|
|
|
|
ErrorOr<void> sync();
|
2019-04-08 23:10:00 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
off_t offset() const;
|
2019-05-30 11:39:17 +00:00
|
|
|
|
2022-08-21 14:15:29 +00:00
|
|
|
ErrorOr<void> chown(Credentials const& credentials, UserID, GroupID);
|
2019-06-01 18:31:36 +00:00
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
FileBlockerSet& blocker_set();
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2022-07-13 23:17:01 +00:00
|
|
|
ErrorOr<void> apply_flock(Process const&, Userspace<flock const*>, ShouldBlock);
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> get_flock(Userspace<flock*>) const;
|
2021-07-19 05:29:56 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2021-07-10 22:25:24 +00:00
|
|
|
friend class VirtualFileSystem;
|
2021-09-07 11:39:11 +00:00
|
|
|
explicit OpenFileDescription(File&);
|
2020-09-17 19:51:09 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> attach();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
void evaluate_block_conditions()
|
|
|
|
{
|
2021-08-22 15:38:16 +00:00
|
|
|
blocker_set().unblock_all_blockers_whose_conditions_are_met();
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 11:25:00 +00:00
|
|
|
RefPtr<Inode> m_inode;
|
2023-04-07 11:49:49 +00:00
|
|
|
NonnullRefPtr<File> const m_file;
|
2018-10-14 19:19:27 +00:00
|
|
|
|
2022-02-02 22:44:46 +00:00
|
|
|
struct State {
|
|
|
|
OwnPtr<OpenFileDescriptionData> data;
|
2022-08-20 23:04:35 +00:00
|
|
|
RefPtr<Custody> custody;
|
2022-02-02 22:44:46 +00:00
|
|
|
off_t current_offset { 0 };
|
|
|
|
u32 file_flags { 0 };
|
|
|
|
bool readable : 1 { false };
|
|
|
|
bool writable : 1 { false };
|
|
|
|
bool is_blocking : 1 { true };
|
|
|
|
bool is_directory : 1 { false };
|
|
|
|
bool should_append : 1 { false };
|
|
|
|
bool direct : 1 { false };
|
|
|
|
FIFO::Direction fifo_direction : 2 { FIFO::Direction::Neither };
|
|
|
|
};
|
|
|
|
|
2022-11-09 10:39:58 +00:00
|
|
|
SpinlockProtected<State, LockRank::None> m_state {};
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|