diff --git a/Userland/Services/FileOperation/main.cpp b/Userland/Services/FileOperation/main.cpp index 10bc872652..f27caa229d 100644 --- a/Userland/Services/FileOperation/main.cpp +++ b/Userland/Services/FileOperation/main.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2021, Andreas Kling - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -38,7 +38,7 @@ static ErrorOr perform_copy(Vector const& sources, String const static ErrorOr perform_move(Vector const& sources, String const& destination); static ErrorOr perform_delete(Vector const& sources); static ErrorOr execute_work_items(Vector const& items); -static ErrorOr> open_destination_file(String const& destination); +static ErrorOr> open_destination_file(String const& destination); static String deduplicate_destination_file_name(String const& destination); ErrorOr serenity_main(Main::Arguments arguments) @@ -230,22 +230,23 @@ ErrorOr execute_work_items(Vector const& items) }; auto copy_file = [&](String const& source, String const& destination) -> ErrorOr { - auto source_file = TRY(Core::File::open(source, Core::OpenMode::ReadOnly)); + auto source_file = TRY(Core::Stream::File::open(source, Core::Stream::OpenMode::Read)); // FIXME: When the file already exists, let the user choose the next action instead of renaming it by default. auto destination_file = TRY(open_destination_file(destination)); + auto buffer = TRY(ByteBuffer::create_zeroed(64 * KiB)); while (true) { print_progress(); - auto buffer = source_file->read(65536); - if (buffer.is_empty()) + auto bytes_read = TRY(source_file->read(buffer.bytes())); + if (bytes_read == 0) break; - if (auto result = destination_file->write(buffer); !result) { + if (auto result = destination_file->write(buffer); result.is_error()) { // FIXME: Return the formatted string directly. There is no way to do this right now without the temporary going out of scope and being destroyed. - report_warning(String::formatted("Failed to write to destination file: {}", destination_file->error_string())); - return result; + report_warning(String::formatted("Failed to write to destination file: {}", result.error())); + return result.error(); } - item_done += buffer.size(); - executed_work_bytes += buffer.size(); + item_done += bytes_read; + executed_work_bytes += bytes_read; print_progress(); // FIXME: Remove this once the kernel is smart enough to schedule other threads // while we're doing heavy I/O. Right now, copying a large file will totally @@ -326,9 +327,9 @@ ErrorOr execute_work_items(Vector const& items) return 0; } -ErrorOr> open_destination_file(String const& destination) +ErrorOr> open_destination_file(String const& destination) { - auto destination_file_or_error = Core::File::open(destination, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate | Core::OpenMode::MustBeNew)); + auto destination_file_or_error = Core::Stream::File::open(destination, (Core::Stream::OpenMode)(Core::Stream::OpenMode::Write | Core::Stream::OpenMode::Truncate | Core::Stream::OpenMode::MustBeNew)); if (destination_file_or_error.is_error() && destination_file_or_error.error().code() == EEXIST) { return open_destination_file(deduplicate_destination_file_name(destination)); }