ladybird/Kernel/FileSystem/FileDescriptor.h

126 lines
3.5 KiB
C
Raw Normal View History

#pragma once
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/FileSystem/InodeMetadata.h>
#include <Kernel/FileSystem/FIFO.h>
#include <Kernel/LinearAddress.h>
#include <AK/ByteBuffer.h>
#include <AK/CircularQueue.h>
#include <AK/Retainable.h>
#include <AK/Badge.h>
#include <Kernel/Net/Socket.h>
class File;
class TTY;
class MasterPTY;
class Process;
class Region;
class CharacterDevice;
class SharedMemory;
2018-11-07 10:37:54 +00:00
class FileDescriptor : public Retainable<FileDescriptor> {
public:
static Retained<FileDescriptor> create(RetainPtr<Socket>&&, SocketRole = SocketRole::None);
static Retained<FileDescriptor> create(RetainPtr<Inode>&&);
static Retained<FileDescriptor> create(RetainPtr<File>&&);
static Retained<FileDescriptor> create_pipe_writer(FIFO&);
static Retained<FileDescriptor> create_pipe_reader(FIFO&);
2018-11-07 10:37:54 +00:00
~FileDescriptor();
Retained<FileDescriptor> clone();
int close();
off_t seek(off_t, int whence);
ssize_t read(Process&, byte*, ssize_t);
ssize_t write(Process&, const byte* data, ssize_t);
KResult fstat(stat&);
KResult fchmod(mode_t);
bool can_read(Process&);
bool can_write(Process&);
ssize_t get_dir_entries(byte* buffer, ssize_t);
ByteBuffer read_entire_file(Process&);
KResultOr<String> absolute_path();
2018-12-02 23:39:25 +00:00
bool is_directory() const;
// FIXME: These should go away once everything is a File.
bool is_file() const { return m_file.ptr(); }
File* file() { return m_file.ptr(); }
const File* file() const { return m_file.ptr(); }
bool is_device() const;
2018-12-02 23:39:25 +00:00
bool is_tty() const;
const TTY* tty() const;
TTY* tty();
bool is_master_pty() const;
const MasterPTY* master_pty() const;
MasterPTY* master_pty();
InodeMetadata metadata() const;
Inode* inode() { return m_inode.ptr(); }
const Inode* inode() const { return m_inode.ptr(); }
KResultOr<Region*> mmap(Process&, LinearAddress, size_t offset, size_t, int prot);
2018-12-02 23:39:25 +00:00
bool is_blocking() const { return m_is_blocking; }
void set_blocking(bool b) { m_is_blocking = b; }
dword file_flags() const { return m_file_flags; }
void set_file_flags(dword flags) { m_file_flags = flags; }
bool is_socket() const { return m_socket; }
Socket* socket() { return m_socket.ptr(); }
const Socket* socket() const { return m_socket.ptr(); }
bool is_fifo() const { return m_fifo; }
FIFO::Direction fifo_direction() { return m_fifo_direction; }
bool is_fsfile() const;
bool is_shared_memory() const;
SharedMemory* shared_memory();
const SharedMemory* shared_memory() const;
2018-12-02 23:39:25 +00:00
ByteBuffer& generator_cache() { return m_generator_cache; }
void set_original_inode(Badge<VFS>, Retained<Inode>&& inode) { m_inode = move(inode); }
SocketRole socket_role() const { return m_socket_role; }
void set_socket_role(SocketRole);
KResult truncate(off_t);
private:
friend class VFS;
FileDescriptor(RetainPtr<Socket>&&, SocketRole);
explicit FileDescriptor(RetainPtr<Inode>&&);
explicit FileDescriptor(RetainPtr<File>&&);
FileDescriptor(FIFO&, FIFO::Direction);
RetainPtr<Inode> m_inode;
RetainPtr<File> m_file;
off_t m_current_offset { 0 };
2018-12-02 23:39:25 +00:00
ByteBuffer m_generator_cache;
2018-12-02 23:39:25 +00:00
bool m_is_blocking { true };
dword m_file_flags { 0 };
RetainPtr<Socket> m_socket;
SocketRole m_socket_role { SocketRole::None };
RetainPtr<FIFO> m_fifo;
FIFO::Direction m_fifo_direction { FIFO::Neither };
bool m_closed { false };
};