Kernel: Make File's can_read/can_write take a const FileDescription&

Asking a File if we could possibly read or write it will never mutate
the asking FileDescription&, so it should be const.
This commit is contained in:
Andreas Kling 2019-11-04 14:03:14 +01:00
parent e8fee92357
commit 1b2ef8582c
47 changed files with 97 additions and 91 deletions

View file

@ -28,7 +28,7 @@ Console::~Console()
{
}
bool Console::can_read(FileDescription&) const
bool Console::can_read(const FileDescription&) const
{
return false;
}

View file

@ -20,8 +20,8 @@ public:
virtual ~Console() override;
// ^CharacterDevice
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override { return true; }
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual const char* class_name() const override { return "Console"; }

View file

@ -150,12 +150,12 @@ int BXVGADevice::ioctl(FileDescription&, unsigned request, unsigned arg)
};
}
bool BXVGADevice::can_read(FileDescription&) const
bool BXVGADevice::can_read(const FileDescription&) const
{
ASSERT_NOT_REACHED();
}
bool BXVGADevice::can_write(FileDescription&) const
bool BXVGADevice::can_write(const FileDescription&) const
{
ASSERT_NOT_REACHED();
}

View file

@ -17,8 +17,8 @@ public:
private:
virtual const char* class_name() const override { return "BXVGA"; }
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;

View file

@ -11,7 +11,7 @@ private:
// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override { return 0; }
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override { return true; }
virtual const char* class_name() const override { return "DebugLogDevice"; }
};

View file

@ -15,9 +15,9 @@ public:
// ^BlockDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override { return 0; }
virtual bool can_read(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override { return true; }
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override { return 0; }
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
private:
virtual const char* class_name() const override;

View file

@ -143,9 +143,9 @@ public:
// ^BlockDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override { return 0; }
virtual bool can_read(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override { return true; }
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override { return 0; }
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
protected:
explicit FloppyDiskDevice(DriveType);

View file

@ -12,7 +12,7 @@ FullDevice::~FullDevice()
{
}
bool FullDevice::can_read(FileDescription&) const
bool FullDevice::can_read(const FileDescription&) const
{
return true;
}

View file

@ -12,7 +12,7 @@ private:
// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override { return true; }
virtual const char* class_name() const override { return "FullDevice"; }
};

View file

@ -368,7 +368,7 @@ KeyboardDevice::~KeyboardDevice()
{
}
bool KeyboardDevice::can_read(FileDescription&) const
bool KeyboardDevice::can_read(const FileDescription&) const
{
return !m_queue.is_empty();
}

View file

@ -24,9 +24,9 @@ public:
// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8* buffer, ssize_t) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual ssize_t write(FileDescription&, const u8* buffer, ssize_t) override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
private:
// ^IRQHandler

View file

@ -82,12 +82,12 @@ int MBVGADevice::ioctl(FileDescription&, unsigned request, unsigned arg)
};
}
bool MBVGADevice::can_read(FileDescription&) const
bool MBVGADevice::can_read(const FileDescription&) const
{
ASSERT_NOT_REACHED();
}
bool MBVGADevice::can_write(FileDescription&) const
bool MBVGADevice::can_write(const FileDescription&) const
{
ASSERT_NOT_REACHED();
}

View file

@ -17,8 +17,8 @@ public:
private:
virtual const char* class_name() const override { return "MBVGA"; }
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;

View file

@ -20,7 +20,7 @@ NullDevice::~NullDevice()
{
}
bool NullDevice::can_read(FileDescription&) const
bool NullDevice::can_read(const FileDescription&) const
{
return true;
}

View file

@ -14,7 +14,7 @@ private:
// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override;
virtual const char* class_name() const override { return "NullDevice"; }
};

View file

@ -36,9 +36,9 @@ public:
// ^BlockDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override { return 0; }
virtual bool can_read(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override { return true; }
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override { return 0; }
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
protected:
explicit PATADiskDevice(PATAChannel&, DriveType, int, int);

View file

@ -255,7 +255,7 @@ u8 PS2MouseDevice::mouse_read()
return IO::in8(I8042_BUFFER);
}
bool PS2MouseDevice::can_read(FileDescription&) const
bool PS2MouseDevice::can_read(const FileDescription&) const
{
return !m_queue.is_empty();
}

View file

@ -14,10 +14,10 @@ public:
static PS2MouseDevice& the();
// ^CharacterDevice
virtual bool can_read(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
private:
// ^IRQHandler

View file

@ -26,7 +26,7 @@ static void mysrand(unsigned seed)
}
#endif
bool RandomDevice::can_read(FileDescription&) const
bool RandomDevice::can_read(const FileDescription&) const
{
return true;
}

View file

@ -14,7 +14,7 @@ private:
// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override { return true; }
virtual const char* class_name() const override { return "RandomDevice"; }
};

View file

@ -84,7 +84,7 @@ void SB16::initialize()
enable_irq();
}
bool SB16::can_read(FileDescription&) const
bool SB16::can_read(const FileDescription&) const
{
return false;
}

View file

@ -17,10 +17,10 @@ public:
static SB16& the();
// ^CharacterDevice
virtual bool can_read(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
private:
// ^IRQHandler

View file

@ -12,7 +12,7 @@ SerialDevice::~SerialDevice()
{
}
bool SerialDevice::can_read(FileDescription&) const
bool SerialDevice::can_read(const FileDescription&) const
{
return (get_line_status() & DataReady) != 0;
}
@ -30,7 +30,7 @@ ssize_t SerialDevice::read(FileDescription&, u8* buffer, ssize_t size)
return 1;
}
bool SerialDevice::can_write(FileDescription&) const
bool SerialDevice::can_write(const FileDescription&) const
{
return (get_line_status() & EmptyTransmitterHoldingRegister) != 0;
}

View file

@ -12,9 +12,9 @@ public:
virtual ~SerialDevice() override;
// ^CharacterDevice
virtual bool can_read(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
enum InterruptEnable {

View file

@ -11,7 +11,7 @@ ZeroDevice::~ZeroDevice()
{
}
bool ZeroDevice::can_read(FileDescription&) const
bool ZeroDevice::can_read(const FileDescription&) const
{
return true;
}

View file

@ -12,7 +12,7 @@ private:
// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override { return true; }
virtual const char* class_name() const override { return "ZeroDevice"; }
};

View file

@ -83,12 +83,12 @@ void FIFO::detach(Direction direction)
}
}
bool FIFO::can_read(FileDescription&) const
bool FIFO::can_read(const FileDescription&) const
{
return !m_buffer.is_empty() || !m_writers;
}
bool FIFO::can_write(FileDescription&) const
bool FIFO::can_write(const FileDescription&) const
{
return m_buffer.bytes_in_write_buffer() < 4096 || !m_readers;
}

View file

@ -30,8 +30,8 @@ private:
// ^File
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual String absolute_path(const FileDescription&) const override;
virtual const char* class_name() const override { return "FIFO"; }
virtual bool is_fifo() const override { return true; }

View file

@ -46,8 +46,8 @@ public:
virtual KResultOr<NonnullRefPtr<FileDescription>> open(int options);
virtual void close();
virtual bool can_read(FileDescription&) const = 0;
virtual bool can_write(FileDescription&) const = 0;
virtual bool can_read(const FileDescription&) const = 0;
virtual bool can_write(const FileDescription&) const = 0;
virtual ssize_t read(FileDescription&, u8*, ssize_t) = 0;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) = 0;

View file

@ -121,14 +121,12 @@ ssize_t FileDescription::write(const u8* data, ssize_t size)
bool FileDescription::can_write() const
{
// FIXME: Remove this const_cast.
return m_file->can_write(const_cast<FileDescription&>(*this));
return m_file->can_write(*this);
}
bool FileDescription::can_read() const
{
// FIXME: Remove this const_cast.
return m_file->can_read(const_cast<FileDescription&>(*this));
return m_file->can_read(*this);
}
ByteBuffer FileDescription::read_entire_file()

View file

@ -16,8 +16,8 @@ public:
const Inode& inode() const { return *m_inode; }
Inode& inode() { return *m_inode; }
virtual bool can_read(FileDescription&) const override { return true; }
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;

View file

@ -18,12 +18,12 @@ InodeWatcher::~InodeWatcher()
safe_inode->unregister_watcher({}, *this);
}
bool InodeWatcher::can_read(FileDescription&) const
bool InodeWatcher::can_read(const FileDescription&) const
{
return !m_queue.is_empty() || !m_inode;
}
bool InodeWatcher::can_write(FileDescription&) const
bool InodeWatcher::can_write(const FileDescription&) const
{
return true;
}

View file

@ -21,8 +21,8 @@ public:
Type type { Type::Invalid };
};
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual String absolute_path(const FileDescription&) const override;

View file

@ -24,8 +24,8 @@ public:
private:
// ^File
virtual bool can_read(FileDescription&) const override { return true; }
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
virtual int read(FileDescription&, u8*, int) override;
virtual int write(FileDescription&, const u8*, int) override;
virtual String absolute_path(const FileDescription&) const override;

View file

@ -144,7 +144,7 @@ void IPv4Socket::detach(FileDescription&)
{
}
bool IPv4Socket::can_read(FileDescription&) const
bool IPv4Socket::can_read(const FileDescription&) const
{
if (m_role == Role::Listener)
return can_accept();
@ -153,7 +153,7 @@ bool IPv4Socket::can_read(FileDescription&) const
return m_can_read;
}
bool IPv4Socket::can_write(FileDescription&) const
bool IPv4Socket::can_write(const FileDescription&) const
{
return is_connected();
}
@ -282,9 +282,13 @@ ssize_t IPv4Socket::recvfrom(FileDescription& description, void* buffer, size_t
return protocol_receive(packet.data.value(), buffer, buffer_length, flags);
}
void IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, KBuffer&& packet)
bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, KBuffer&& packet)
{
LOCKER(lock());
if (m_receive_queue.size_slow() > 2000) {
kprintf("IPv4Socket(%p): did_receive refusing packet since queue is full.\n", this);
return false;
}
auto packet_size = packet.size();
m_receive_queue.append({ source_address, source_port, move(packet) });
m_can_read = true;
@ -292,6 +296,7 @@ void IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port,
#ifdef IPV4_SOCKET_DEBUG
kprintf("IPv4Socket(%p): did_receive %d bytes, total_received=%u, packets in queue: %d\n", this, packet_size, m_bytes_received, m_receive_queue.size_slow());
#endif
return true;
}
String IPv4Socket::absolute_path(const FileDescription&) const

View file

@ -27,8 +27,8 @@ public:
virtual bool get_peer_address(sockaddr*, socklen_t*) override;
virtual void attach(FileDescription&) override;
virtual void detach(FileDescription&) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual ssize_t sendto(FileDescription&, const void*, size_t, int, const sockaddr*, socklen_t) override;
virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) override;
virtual KResult setsockopt(int level, int option, const void*, socklen_t) override;
@ -36,7 +36,7 @@ public:
virtual int ioctl(FileDescription&, unsigned request, unsigned arg) override;
void did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&);
bool did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&);
const IPv4Address& local_address() const { return m_local_address; }
u16 local_port() const { return m_local_port; }

View file

@ -186,7 +186,7 @@ void LocalSocket::detach(FileDescription& description)
}
}
bool LocalSocket::can_read(FileDescription& description) const
bool LocalSocket::can_read(const FileDescription& description) const
{
auto role = this->role(description);
if (role == Role::Listener)
@ -208,7 +208,7 @@ bool LocalSocket::has_attached_peer(const FileDescription& description) const
ASSERT_NOT_REACHED();
}
bool LocalSocket::can_write(FileDescription& description) const
bool LocalSocket::can_write(const FileDescription& description) const
{
auto role = this->role(description);
if (role == Role::Accepted)

View file

@ -25,8 +25,8 @@ public:
virtual bool get_peer_address(sockaddr*, socklen_t*) override;
virtual void attach(FileDescription&) override;
virtual void detach(FileDescription&) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual ssize_t sendto(FileDescription&, const void*, size_t, int, const sockaddr*, socklen_t) override;
virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) override;

View file

@ -574,9 +574,12 @@ void handle_tcp(const IPv4Packet& ipv4_packet)
socket->sequence_number());
#endif
socket->send_tcp_packet(TCPFlags::ACK);
bool should_ack = true;
if (payload_size != 0) {
should_ack = socket->did_receive(ipv4_packet.source(), tcp_packet.source_port(), KBuffer::copy(&ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size()));
}
if (payload_size != 0)
socket->did_receive(ipv4_packet.source(), tcp_packet.source_port(), KBuffer::copy(&ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size()));
if (should_ack)
socket->send_tcp_packet(TCPFlags::ACK);
}
}

View file

@ -12,10 +12,10 @@ public:
bool is_dead() const { return m_dead; }
void set_dead() { m_dead = true; }
virtual bool can_read(FileDescription&) const override { return !m_calls.is_empty() || m_dead; }
virtual bool can_read(const FileDescription&) const override { return !m_calls.is_empty() || m_dead; }
virtual int read(FileDescription&, u8*, int) override;
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
virtual int write(FileDescription&, const u8*, int) override { return -EIO; }
virtual String absolute_path(const FileDescription&) const override;

View file

@ -46,14 +46,14 @@ ssize_t MasterPTY::write(FileDescription&, const u8* buffer, ssize_t size)
return size;
}
bool MasterPTY::can_read(FileDescription&) const
bool MasterPTY::can_read(const FileDescription&) const
{
if (!m_slave)
return true;
return !m_buffer.is_empty();
}
bool MasterPTY::can_write(FileDescription&) const
bool MasterPTY::can_write(const FileDescription&) const
{
return true;
}

View file

@ -24,8 +24,8 @@ private:
// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual void close() override;
virtual bool is_master_pty() const override { return true; }
virtual int ioctl(FileDescription&, unsigned request, unsigned arg) override;

View file

@ -18,8 +18,8 @@ public:
virtual KResultOr<NonnullRefPtr<FileDescription>> open(int options) override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override { return 0; }
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override { return 0; }
virtual bool can_read(FileDescription&) const override { return true; }
virtual bool can_write(FileDescription&) const override { return true; }
virtual bool can_read(const FileDescription&) const override { return true; }
virtual bool can_write(const FileDescription&) const override { return true; }
void notify_master_destroyed(Badge<MasterPTY>, unsigned index);

View file

@ -48,12 +48,12 @@ ssize_t SlavePTY::on_tty_write(const u8* data, ssize_t size)
return m_master->on_slave_write(data, size);
}
bool SlavePTY::can_write(FileDescription&) const
bool SlavePTY::can_write(const FileDescription&) const
{
return m_master->can_write_from_slave();
}
bool SlavePTY::can_read(FileDescription& description) const
bool SlavePTY::can_read(const FileDescription& description) const
{
if (m_master->is_closed())
return true;

View file

@ -19,9 +19,9 @@ private:
virtual void echo(u8) override;
// ^CharacterDevice
virtual bool can_read(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual const char* class_name() const override { return "SlavePTY"; }
virtual void close() override;

View file

@ -68,7 +68,7 @@ ssize_t TTY::write(FileDescription&, const u8* buffer, ssize_t size)
return size;
}
bool TTY::can_read(FileDescription&) const
bool TTY::can_read(const FileDescription&) const
{
if (in_canonical_mode()) {
return m_available_lines > 0;
@ -76,7 +76,7 @@ bool TTY::can_read(FileDescription&) const
return !m_input_buffer.is_empty();
}
bool TTY::can_write(FileDescription&) const
bool TTY::can_write(const FileDescription&) const
{
return true;
}

View file

@ -13,8 +13,8 @@ public:
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override;
virtual bool can_read(FileDescription&) const override;
virtual bool can_write(FileDescription&) const override;
virtual bool can_read(const FileDescription&) const override;
virtual bool can_write(const FileDescription&) const override;
virtual int ioctl(FileDescription&, unsigned request, unsigned arg) override final;
virtual String absolute_path(const FileDescription&) const override { return tty_name(); }