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