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