mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 00:47:45 +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:
parent
1d5b45f7d9
commit
d5871f5717
109 changed files with 474 additions and 329 deletions
|
@ -40,7 +40,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
Array<u8, 32768> buffer;
|
||||
for (auto const& file : files) {
|
||||
while (!file->is_eof()) {
|
||||
auto const buffer_span = TRY(file->read(buffer));
|
||||
auto const buffer_span = TRY(file->read_some(buffer));
|
||||
out("{:s}", buffer_span);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,13 +66,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
Array<u8, PAGE_SIZE> buffer;
|
||||
if (!verify_from_paths) {
|
||||
while (!file->is_eof())
|
||||
hash.update(TRY(file->read(buffer)));
|
||||
hash.update(TRY(file->read_some(buffer)));
|
||||
outln("{:hex-dump} {}", hash.digest().bytes(), path);
|
||||
} else {
|
||||
StringBuilder checksum_list_contents;
|
||||
Array<u8, 1> checksum_list_buffer;
|
||||
while (!file->is_eof())
|
||||
checksum_list_contents.append(TRY(file->read(checksum_list_buffer)).data()[0]);
|
||||
checksum_list_contents.append(TRY(file->read_some(checksum_list_buffer)).data()[0]);
|
||||
Vector<StringView> const lines = checksum_list_contents.string_view().split_view("\n"sv);
|
||||
|
||||
for (size_t i = 0; i < lines.size(); ++i) {
|
||||
|
@ -96,7 +96,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto file_from_filename = file_from_filename_or_error.release_value();
|
||||
hash.reset();
|
||||
while (!file_from_filename->is_eof())
|
||||
hash.update(TRY(file_from_filename->read(buffer)));
|
||||
hash.update(TRY(file_from_filename->read_some(buffer)));
|
||||
if (DeprecatedString::formatted("{:hex-dump}", hash.digest().bytes()) == line[0])
|
||||
outln("{}: OK", filename);
|
||||
else {
|
||||
|
|
|
@ -59,7 +59,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
if (algorithm == "crc32") {
|
||||
Crypto::Checksum::CRC32 crc32;
|
||||
while (!file->is_eof()) {
|
||||
auto data_or_error = file->read(buffer);
|
||||
auto data_or_error = file->read_some(buffer);
|
||||
if (data_or_error.is_error()) {
|
||||
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
|
||||
fail = true;
|
||||
|
@ -72,7 +72,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
} else if (algorithm == "adler32") {
|
||||
Crypto::Checksum::Adler32 adler32;
|
||||
while (!file->is_eof()) {
|
||||
auto data_or_error = file->read(buffer);
|
||||
auto data_or_error = file->read_some(buffer);
|
||||
if (data_or_error.is_error()) {
|
||||
warnln("{}: Failed to read {}: {}", arguments.strings[0], filepath, data_or_error.error());
|
||||
fail = true;
|
||||
|
|
|
@ -75,8 +75,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
};
|
||||
|
||||
while (true) {
|
||||
TRY(file1->read(buffer1));
|
||||
TRY(file2->read(buffer2));
|
||||
TRY(file1->read_some(buffer1));
|
||||
TRY(file2->read_some(buffer2));
|
||||
|
||||
if (file1->is_eof() && file2->is_eof())
|
||||
break;
|
||||
|
|
|
@ -187,7 +187,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
} else if (!file_size_in_bytes) {
|
||||
outln("{}: empty", path);
|
||||
} else {
|
||||
auto bytes = TRY(file->read(buffer));
|
||||
auto bytes = TRY(file->read_some(buffer));
|
||||
auto file_name_guess = Core::guess_mime_type_based_on_filename(path);
|
||||
auto mime_type = Core::guess_mime_type_based_on_sniffed_bytes(bytes).value_or(file_name_guess);
|
||||
auto human_readable_description = get_description_from_mime_type(mime_type, path).value_or(mime_type);
|
||||
|
|
|
@ -28,7 +28,8 @@ static ErrorOr<bool> format_file(StringView path, bool inplace)
|
|||
return true;
|
||||
TRY(file->seek(0, SeekMode::SetPosition));
|
||||
TRY(file->truncate(0));
|
||||
TRY(file->write(formatted_gml.bytes()));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(file->write_some(formatted_gml.bytes()));
|
||||
} else {
|
||||
out("{}", formatted_gml);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ static ErrorOr<void> decompress_file(NonnullOwnPtr<Core::File> input_stream, Str
|
|||
|
||||
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
||||
while (!gzip_stream.is_eof()) {
|
||||
auto span = TRY(gzip_stream.read(buffer));
|
||||
auto span = TRY(gzip_stream.read_some(buffer));
|
||||
TRY(output_stream.write_entire_buffer(span));
|
||||
}
|
||||
|
||||
|
|
|
@ -174,7 +174,8 @@ static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Cor
|
|||
|
||||
auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write));
|
||||
auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot));
|
||||
MUST(output_file->write(image_buffer.bytes()));
|
||||
// FIXME: This should write the entire buffer.
|
||||
MUST(output_file->write_some(image_buffer.bytes()));
|
||||
} else {
|
||||
warnln("No screenshot available");
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
}
|
||||
|
||||
bytes = contents.span().slice(0, bytes_to_read);
|
||||
bytes = TRY(file->read(bytes));
|
||||
bytes = TRY(file->read_some(bytes));
|
||||
|
||||
total_bytes_read += bytes.size();
|
||||
|
||||
|
|
|
@ -188,7 +188,8 @@ static ErrorOr<void> write_to_file(String const& path)
|
|||
for (size_t i = 0; i < g_repl_statements.size(); i++) {
|
||||
auto line = g_repl_statements[i].bytes();
|
||||
if (line.size() > 0 && i != g_repl_statements.size() - 1) {
|
||||
TRY(file->write(line));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(file->write_some(line));
|
||||
}
|
||||
if (i != g_repl_statements.size() - 1) {
|
||||
TRY(file->write_value('\n'));
|
||||
|
|
|
@ -82,7 +82,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto nread = TRY(Core::System::read(STDIN_FILENO, buffer_span));
|
||||
buffer_span = buffer_span.trim(nread);
|
||||
|
||||
TRY(socket->write({ buffer_span.data(), static_cast<size_t>(nread) }));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(socket->write_some({ buffer_span.data(), static_cast<size_t>(nread) }));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -112,18 +112,18 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override
|
||||
virtual ErrorOr<Bytes> read_some(Bytes) override
|
||||
{
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override
|
||||
virtual ErrorOr<size_t> write_some(ReadonlyBytes bytes) override
|
||||
{
|
||||
// Pretend that we wrote the whole buffer if the condition is untrue.
|
||||
if (!m_condition())
|
||||
return bytes.size();
|
||||
|
||||
return m_stream->write(bytes);
|
||||
return m_stream->write_some(bytes);
|
||||
}
|
||||
|
||||
virtual bool is_eof() const override
|
||||
|
|
|
@ -14,7 +14,8 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
auto file = TRY(Core::File::open("/sys/kernel/power_state"sv, Core::File::OpenMode::Write));
|
||||
|
||||
const DeprecatedString file_contents = "1";
|
||||
TRY(file->write(file_contents.bytes()));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(file->write_some(file_contents.bytes()));
|
||||
file->close();
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -141,8 +141,9 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
|
||||
if (maybe_output_file.has_value()) {
|
||||
auto const& output_file = maybe_output_file.value();
|
||||
TRY(output_file->write(result.bytes()));
|
||||
TRY(output_file->write("\n"sv.bytes()));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(output_file->write_some(result.bytes()));
|
||||
TRY(output_file->write_some("\n"sv.bytes()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,7 +167,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
|
||||
auto& file = *file_or_error.value();
|
||||
TRY(file.write(encoded_bitmap.bytes()));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(file.write_some(encoded_bitmap.bytes()));
|
||||
|
||||
if (edit_image)
|
||||
TRY(Core::Process::spawn("/bin/PixelPaint"sv, Array { output_path }));
|
||||
|
|
|
@ -18,7 +18,8 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
auto file = TRY(Core::File::open("/sys/kernel/power_state"sv, Core::File::OpenMode::Write));
|
||||
|
||||
const DeprecatedString file_contents = "2";
|
||||
TRY(file->write(file_contents.bytes()));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(file->write_some(file_contents.bytes()));
|
||||
file->close();
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -932,6 +932,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
FormattedSyscallBuilder builder(syscall_name);
|
||||
TRY(format_syscall(builder, syscall_function, arg1, arg2, arg3, res));
|
||||
|
||||
TRY(trace_file->write(builder.string_view().bytes()));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(trace_file->write_some(builder.string_view().bytes()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ static ErrorOr<void> process_strings_in_file(StringView path, bool show_paths, S
|
|||
size_t string_offset_position = 0;
|
||||
bool did_show_path = false;
|
||||
while (!file->is_eof()) {
|
||||
auto buffer_span = TRY(file->read(buffer));
|
||||
auto buffer_span = TRY(file->read_some(buffer));
|
||||
while (!buffer_span.is_empty()) {
|
||||
string_offset_position += processed_characters;
|
||||
processed_characters = process_characters_in_span(output_characters, buffer_span);
|
||||
|
|
|
@ -48,7 +48,8 @@ static bool write_variable(StringView name, StringView value)
|
|||
warnln("Failed to open {}: {}", path, file.error());
|
||||
return false;
|
||||
}
|
||||
if (auto result = file.value()->write(value.bytes()); result.is_error()) {
|
||||
// FIXME: This should write the entire span.
|
||||
if (auto result = file.value()->write_some(value.bytes()); result.is_error()) {
|
||||
warnln("Failed to write {}: {}", path, result.error());
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -35,7 +35,8 @@ static ErrorOr<off_t> find_seek_pos(Core::File& file, int wanted_lines)
|
|||
if (file.is_eof())
|
||||
break;
|
||||
Array<u8, 1> buffer;
|
||||
auto ch = TRY(file.read(buffer));
|
||||
// FIXME: This should read the entire span.
|
||||
auto ch = TRY(file.read_some(buffer));
|
||||
if (*ch.data() == '\n' && (end - pos) > 1) {
|
||||
lines++;
|
||||
if (lines == wanted_lines)
|
||||
|
|
|
@ -127,7 +127,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
Array<u8, buffer_size> buffer;
|
||||
|
||||
while (!file_stream.is_eof()) {
|
||||
auto slice = TRY(file_stream.read(buffer));
|
||||
auto slice = TRY(file_stream.read_some(buffer));
|
||||
long_name.append(reinterpret_cast<char*>(slice.data()), slice.size());
|
||||
}
|
||||
|
||||
|
@ -162,7 +162,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
Array<u8, buffer_size> buffer;
|
||||
while (!file_stream.is_eof()) {
|
||||
auto slice = TRY(file_stream.read(buffer));
|
||||
auto slice = TRY(file_stream.read_some(buffer));
|
||||
TRY(Core::System::write(fd, slice));
|
||||
}
|
||||
|
||||
|
|
|
@ -17,10 +17,11 @@ static ErrorOr<void> write_line_content(StringView line, size_t count, bool dupl
|
|||
if (duplicates_only && count <= 1)
|
||||
return {};
|
||||
|
||||
// FIXME: This should write the entire span.
|
||||
if (print_count)
|
||||
TRY(outfile.write(DeprecatedString::formatted("{} {}\n", count, line).bytes()));
|
||||
TRY(outfile.write_some(DeprecatedString::formatted("{} {}\n", count, line).bytes()));
|
||||
else
|
||||
TRY(outfile.write(DeprecatedString::formatted("{}\n", line).bytes()));
|
||||
TRY(outfile.write_some(DeprecatedString::formatted("{}\n", line).bytes()));
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
TRY(Core::System::pledge("stdio"));
|
||||
|
||||
Array<u8, BUFSIZ> buffer;
|
||||
auto read_buffer = TRY(file->read(buffer));
|
||||
auto read_buffer = TRY(file->read_some(buffer));
|
||||
auto maybe_seconds = AK::StringUtils::convert_to_uint(StringView(read_buffer));
|
||||
if (!maybe_seconds.has_value())
|
||||
return Error::from_string_literal("Couldn't convert to number");
|
||||
|
|
|
@ -72,7 +72,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
TRY(file->seek(0, SeekMode::SetPosition));
|
||||
TRY(file->truncate(0));
|
||||
TRY(file->write(json.to_deprecated_string().bytes()));
|
||||
// FIXME: This should write the entire span.
|
||||
TRY(file->write_some(json.to_deprecated_string().bytes()));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -53,12 +53,14 @@ static bool pre_interpret_hook(Wasm::Configuration& config, Wasm::InstructionPoi
|
|||
if (always_print_stack)
|
||||
config.dump_stack();
|
||||
if (always_print_instruction) {
|
||||
g_stdout->write(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
// FIXME: This should write the entire span.
|
||||
g_stdout->write_some(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
g_printer->print(instr);
|
||||
}
|
||||
if (g_continue)
|
||||
return true;
|
||||
g_stdout->write(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
// FIXME: This should write the entire span.
|
||||
g_stdout->write_some(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
g_printer->print(instr);
|
||||
DeprecatedString last_command = "";
|
||||
for (;;) {
|
||||
|
@ -214,7 +216,8 @@ static bool pre_interpret_hook(Wasm::Configuration& config, Wasm::InstructionPoi
|
|||
if (!result.values().is_empty())
|
||||
warnln("Returned:");
|
||||
for (auto& value : result.values()) {
|
||||
g_stdout->write(" -> "sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
// FIXME: This should write the entire span.
|
||||
g_stdout->write_some(" -> "sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
g_printer->print(value);
|
||||
}
|
||||
}
|
||||
|
@ -454,15 +457,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
auto print_func = [&](auto const& address) {
|
||||
Wasm::FunctionInstance* fn = machine.store().get(address);
|
||||
g_stdout->write(DeprecatedString::formatted("- Function with address {}, ptr = {}\n", address.value(), fn).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
// FIXME: This should write the entire span.
|
||||
g_stdout->write_some(DeprecatedString::formatted("- Function with address {}, ptr = {}\n", address.value(), fn).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
if (fn) {
|
||||
g_stdout->write(DeprecatedString::formatted(" wasm function? {}\n", fn->has<Wasm::WasmFunction>()).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
// FIXME: This should write the entire span.
|
||||
g_stdout->write_some(DeprecatedString::formatted(" wasm function? {}\n", fn->has<Wasm::WasmFunction>()).bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
fn->visit(
|
||||
[&](Wasm::WasmFunction const& func) {
|
||||
Wasm::Printer printer { *g_stdout, 3 };
|
||||
g_stdout->write(" type:\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
// FIXME: This should write the entire span.
|
||||
g_stdout->write_some(" type:\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
printer.print(func.type());
|
||||
g_stdout->write(" code:\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
g_stdout->write_some(" code:\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
printer.print(func.code());
|
||||
},
|
||||
[](Wasm::HostFunction const&) {});
|
||||
|
@ -526,7 +532,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
if (!result.values().is_empty())
|
||||
warnln("Returned:");
|
||||
for (auto& value : result.values()) {
|
||||
g_stdout->write(" -> "sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
// FIXME: This should write the entire span.
|
||||
g_stdout->write_some(" -> "sv.bytes()).release_value_but_fixme_should_propagate_errors();
|
||||
g_printer->print(value);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue