1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:07:34 +00:00

AK: Rename Stream::{read,write} to Stream::{read_some,write_some}

Similar to POSIX read, the basic read and write functions of AK::Stream
do not have a lower limit of how much data they read or write (apart
from "none at all").

Rename the functions to "read some [data]" and "write some [data]" (with
"data" being omitted, since everything here is reading and writing data)
to make them sufficiently distinct from the functions that ensure to
use the entire buffer (which should be the go-to function for most
usages).

No functional changes, just a lot of new FIXMEs.
This commit is contained in:
Tim Schumacher 2023-02-24 22:38:01 +01:00 committed by Linus Groh
parent 1d5b45f7d9
commit d5871f5717
109 changed files with 474 additions and 329 deletions

View file

@ -179,10 +179,11 @@ ErrorOr<void> ConfigFile::sync()
TRY(m_file->seek(0, SeekMode::SetPosition));
for (auto& it : m_groups) {
TRY(m_file->write(DeprecatedString::formatted("[{}]\n", it.key).bytes()));
// FIXME: This should write the entire span.
TRY(m_file->write_some(DeprecatedString::formatted("[{}]\n", it.key).bytes()));
for (auto& jt : it.value)
TRY(m_file->write(DeprecatedString::formatted("{}={}\n", jt.key, jt.value).bytes()));
TRY(m_file->write("\n"sv.bytes()));
TRY(m_file->write_some(DeprecatedString::formatted("{}={}\n", jt.key, jt.value).bytes()));
TRY(m_file->write_some("\n"sv.bytes()));
}
m_dirty = false;

View file

@ -169,7 +169,7 @@ private:
#ifdef AK_OS_SERENITY
m_socket->on_ready_to_read = [this] {
u32 length;
auto maybe_bytes_read = m_socket->read({ (u8*)&length, sizeof(length) });
auto maybe_bytes_read = m_socket->read_some({ (u8*)&length, sizeof(length) });
if (maybe_bytes_read.is_error()) {
dbgln("InspectorServerConnection: Failed to read message length from inspector server connection: {}", maybe_bytes_read.error());
shutdown();
@ -186,7 +186,7 @@ private:
VERIFY(bytes_read.size() == sizeof(length));
auto request_buffer = ByteBuffer::create_uninitialized(length).release_value();
maybe_bytes_read = m_socket->read(request_buffer.bytes());
maybe_bytes_read = m_socket->read_some(request_buffer.bytes());
if (maybe_bytes_read.is_error()) {
dbgln("InspectorServerConnection: Failed to read message content from inspector server connection: {}", maybe_bytes_read.error());
shutdown();
@ -221,10 +221,11 @@ public:
auto bytes_to_send = serialized.bytes();
u32 length = bytes_to_send.size();
// FIXME: Propagate errors
auto sent = MUST(m_socket->write({ (u8 const*)&length, sizeof(length) }));
// FIXME: This should write the entire span.
auto sent = MUST(m_socket->write_some({ (u8 const*)&length, sizeof(length) }));
VERIFY(sent == sizeof(length));
while (!bytes_to_send.is_empty()) {
size_t bytes_sent = MUST(m_socket->write(bytes_to_send));
size_t bytes_sent = MUST(m_socket->write_some(bytes_to_send));
bytes_to_send = bytes_to_send.slice(bytes_sent);
}
}

View file

@ -99,7 +99,7 @@ ErrorOr<void> File::open_path(StringView filename, mode_t permissions)
return {};
}
ErrorOr<Bytes> File::read(Bytes buffer)
ErrorOr<Bytes> File::read_some(Bytes buffer)
{
if (!has_flag(m_mode, OpenMode::Read)) {
// NOTE: POSIX says that if the fd is not open for reading, the call
@ -121,7 +121,7 @@ ErrorOr<ByteBuffer> File::read_until_eof(size_t block_size)
return read_until_eof_impl(block_size, potential_file_size);
}
ErrorOr<size_t> File::write(ReadonlyBytes buffer)
ErrorOr<size_t> File::write_some(ReadonlyBytes buffer)
{
if (!has_flag(m_mode, OpenMode::Write)) {
// NOTE: Same deal as Read.

View file

@ -59,9 +59,9 @@ public:
return *this;
}
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<Bytes> read_some(Bytes) override;
virtual ErrorOr<ByteBuffer> read_until_eof(size_t block_size = 4096) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;

View file

@ -57,7 +57,7 @@ protected:
void did_fail(Error);
void did_progress(Optional<u32> total_size, u32 downloaded);
ErrorOr<size_t> do_write(ReadonlyBytes bytes) { return m_output_stream.write(bytes); }
ErrorOr<size_t> do_write(ReadonlyBytes bytes) { return m_output_stream.write_some(bytes); }
private:
RefPtr<NetworkResponse> m_response;

View file

@ -102,12 +102,14 @@ ErrorOr<void> send_version_identifier_and_method_selection_message(Core::Socket&
.method_count = 1,
.methods = { to_underlying(method) },
};
auto size = TRY(socket.write({ &message, sizeof(message) }));
// FIXME: This should write the entire span.
auto size = TRY(socket.write_some({ &message, sizeof(message) }));
if (size != sizeof(message))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send version identifier and method selection message");
Socks5InitialResponse response;
size = TRY(socket.read({ &response, sizeof(response) })).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some({ &response, sizeof(response) })).size();
if (size != sizeof(response))
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive initial response");
@ -133,7 +135,8 @@ ErrorOr<Reply> send_connect_request_message(Core::Socket& socket, Core::SOCKSPro
.port = htons(port),
};
auto size = TRY(stream.write({ &header, sizeof(header) }));
// FIXME: This should write the entire span.
auto size = TRY(stream.write_some({ &header, sizeof(header) }));
if (size != sizeof(header))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send connect request header");
@ -142,10 +145,12 @@ ErrorOr<Reply> send_connect_request_message(Core::Socket& socket, Core::SOCKSPro
u8 address_data[2];
address_data[0] = to_underlying(AddressType::DomainName);
address_data[1] = hostname.length();
auto size = TRY(stream.write({ address_data, sizeof(address_data) }));
// FIXME: This should write the entire span.
auto size = TRY(stream.write_some({ address_data, sizeof(address_data) }));
if (size != array_size(address_data))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send connect request address data");
TRY(stream.write({ hostname.characters(), hostname.length() }));
// FIXME: This should write the entire span.
TRY(stream.write_some({ hostname.characters(), hostname.length() }));
return {};
},
[&](u32 ipv4) -> ErrorOr<void> {
@ -153,25 +158,29 @@ ErrorOr<Reply> send_connect_request_message(Core::Socket& socket, Core::SOCKSPro
address_data[0] = to_underlying(AddressType::IPV4);
u32 network_ordered_ipv4 = NetworkOrdered<u32>(ipv4);
memcpy(address_data + 1, &network_ordered_ipv4, sizeof(network_ordered_ipv4));
auto size = TRY(stream.write({ address_data, sizeof(address_data) }));
// FIXME: This should write the entire span.
auto size = TRY(stream.write_some({ address_data, sizeof(address_data) }));
if (size != array_size(address_data))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send connect request address data");
return {};
}));
size = TRY(stream.write({ &trailer, sizeof(trailer) }));
// FIXME: This should write the entire span.
size = TRY(stream.write_some({ &trailer, sizeof(trailer) }));
if (size != sizeof(trailer))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send connect request trailer");
auto buffer = TRY(ByteBuffer::create_uninitialized(stream.used_buffer_size()));
TRY(stream.read_entire_buffer(buffer.bytes()));
size = TRY(socket.write({ buffer.data(), buffer.size() }));
// FIXME: This should write the entire span.
size = TRY(socket.write_some({ buffer.data(), buffer.size() }));
if (size != buffer.size())
return Error::from_string_literal("SOCKS negotiation failed: Failed to send connect request");
Socks5ConnectResponseHeader response_header;
size = TRY(socket.read({ &response_header, sizeof(response_header) })).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some({ &response_header, sizeof(response_header) })).size();
if (size != sizeof(response_header))
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive connect response header");
@ -179,26 +188,30 @@ ErrorOr<Reply> send_connect_request_message(Core::Socket& socket, Core::SOCKSPro
return Error::from_string_literal("SOCKS negotiation failed: Invalid version identifier");
u8 response_address_type;
size = TRY(socket.read({ &response_address_type, sizeof(response_address_type) })).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some({ &response_address_type, sizeof(response_address_type) })).size();
if (size != sizeof(response_address_type))
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive connect response address type");
switch (AddressType(response_address_type)) {
case AddressType::IPV4: {
u8 response_address_data[4];
size = TRY(socket.read({ response_address_data, sizeof(response_address_data) })).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some({ response_address_data, sizeof(response_address_data) })).size();
if (size != sizeof(response_address_data))
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive connect response address data");
break;
}
case AddressType::DomainName: {
u8 response_address_length;
size = TRY(socket.read({ &response_address_length, sizeof(response_address_length) })).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some({ &response_address_length, sizeof(response_address_length) })).size();
if (size != sizeof(response_address_length))
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive connect response address length");
ByteBuffer buffer;
buffer.resize(response_address_length);
size = TRY(socket.read(buffer)).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some(buffer)).size();
if (size != response_address_length)
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive connect response address data");
break;
@ -209,7 +222,8 @@ ErrorOr<Reply> send_connect_request_message(Core::Socket& socket, Core::SOCKSPro
}
u16 bound_port;
size = TRY(socket.read({ &bound_port, sizeof(bound_port) })).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some({ &bound_port, sizeof(bound_port) })).size();
if (size != sizeof(bound_port))
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive connect response bound port");
@ -221,37 +235,44 @@ ErrorOr<u8> send_username_password_authentication_message(Core::Socket& socket,
AllocatingMemoryStream stream;
u8 version = 0x01;
auto size = TRY(stream.write({ &version, sizeof(version) }));
// FIXME: This should write the entire span.
auto size = TRY(stream.write_some({ &version, sizeof(version) }));
if (size != sizeof(version))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send username/password authentication message");
u8 username_length = auth_data.username.length();
size = TRY(stream.write({ &username_length, sizeof(username_length) }));
// FIXME: This should write the entire span.
size = TRY(stream.write_some({ &username_length, sizeof(username_length) }));
if (size != sizeof(username_length))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send username/password authentication message");
size = TRY(stream.write({ auth_data.username.characters(), auth_data.username.length() }));
// FIXME: This should write the entire span.
size = TRY(stream.write_some({ auth_data.username.characters(), auth_data.username.length() }));
if (size != auth_data.username.length())
return Error::from_string_literal("SOCKS negotiation failed: Failed to send username/password authentication message");
u8 password_length = auth_data.password.length();
size = TRY(stream.write({ &password_length, sizeof(password_length) }));
// FIXME: This should write the entire span.
size = TRY(stream.write_some({ &password_length, sizeof(password_length) }));
if (size != sizeof(password_length))
return Error::from_string_literal("SOCKS negotiation failed: Failed to send username/password authentication message");
size = TRY(stream.write({ auth_data.password.characters(), auth_data.password.length() }));
// FIXME: This should write the entire span.
size = TRY(stream.write_some({ auth_data.password.characters(), auth_data.password.length() }));
if (size != auth_data.password.length())
return Error::from_string_literal("SOCKS negotiation failed: Failed to send username/password authentication message");
auto buffer = TRY(ByteBuffer::create_uninitialized(stream.used_buffer_size()));
TRY(stream.read_entire_buffer(buffer.bytes()));
size = TRY(socket.write(buffer));
// FIXME: This should write the entire span.
size = TRY(socket.write_some(buffer));
if (size != buffer.size())
return Error::from_string_literal("SOCKS negotiation failed: Failed to send username/password authentication message");
Socks5UsernamePasswordResponse response;
size = TRY(socket.read({ &response, sizeof(response) })).size();
// FIXME: This should read the entire span.
size = TRY(socket.read_some({ &response, sizeof(response) })).size();
if (size != sizeof(response))
return Error::from_string_literal("SOCKS negotiation failed: Failed to receive username/password authentication response");

View file

@ -37,8 +37,8 @@ public:
virtual ~SOCKSProxyClient() override;
// ^Stream::Stream
virtual ErrorOr<Bytes> read(Bytes bytes) override { return m_socket.read(bytes); }
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_socket.write(bytes); }
virtual ErrorOr<Bytes> read_some(Bytes bytes) override { return m_socket.read_some(bytes); }
virtual ErrorOr<size_t> write_some(ReadonlyBytes bytes) override { return m_socket.write_some(bytes); }
virtual bool is_eof() const override { return m_socket.is_eof(); }
virtual bool is_open() const override { return m_socket.is_open(); }
virtual void close() override { m_socket.close(); }

View file

@ -176,8 +176,8 @@ public:
return *this;
}
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual ErrorOr<Bytes> read_some(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
virtual ErrorOr<size_t> write_some(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
virtual bool is_open() const override { return m_helper.is_open(); };
virtual void close() override { m_helper.close(); };
@ -236,7 +236,7 @@ public:
return *this;
}
virtual ErrorOr<Bytes> read(Bytes buffer) override
virtual ErrorOr<Bytes> read_some(Bytes buffer) override
{
auto pending_bytes = TRY(this->pending_bytes());
if (pending_bytes > buffer.size()) {
@ -251,7 +251,7 @@ public:
return m_helper.read(buffer, default_flags());
}
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual ErrorOr<size_t> write_some(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
virtual bool is_open() const override { return m_helper.is_open(); }
virtual void close() override { m_helper.close(); }
@ -310,8 +310,8 @@ public:
return *this;
}
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual ErrorOr<Bytes> read_some(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
virtual ErrorOr<size_t> write_some(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
virtual bool is_open() const override { return m_helper.is_open(); }
virtual void close() override { m_helper.close(); }
@ -398,8 +398,8 @@ public:
return *this;
}
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(move(buffer)); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.stream().write(buffer); }
virtual ErrorOr<Bytes> read_some(Bytes buffer) override { return m_helper.read(move(buffer)); }
virtual ErrorOr<size_t> write_some(ReadonlyBytes buffer) override { return m_helper.stream().write_some(buffer); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
virtual bool is_open() const override { return m_helper.stream().is_open(); }
virtual void close() override { m_helper.stream().close(); }
@ -483,8 +483,8 @@ public:
return {};
}
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_socket.read(move(buffer)); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_socket.write(buffer); }
virtual ErrorOr<Bytes> read_some(Bytes buffer) override { return m_socket.read(move(buffer)); }
virtual ErrorOr<size_t> write_some(ReadonlyBytes buffer) override { return m_socket.write(buffer); }
virtual bool is_eof() const override { return m_socket.is_eof(); }
virtual bool is_open() const override { return m_socket.is_open(); }
virtual void close() override { m_socket.close(); }