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

FileManager: Use TRY in run_file_operation()

The exception is `execvp()` since that has no Core::System wrapper yet.
This commit is contained in:
Sam Atkins 2022-03-12 12:44:37 +00:00 committed by Andreas Kling
parent 6a23dfbc92
commit dc5a318aa9
4 changed files with 23 additions and 34 deletions

View file

@ -650,7 +650,7 @@ void DirectoryView::handle_drop(GUI::ModelIndex const& index, GUI::DropEvent con
} }
if (!paths_to_copy.is_empty()) if (!paths_to_copy.is_empty())
run_file_operation(FileOperation::Copy, paths_to_copy, target_node.full_path(), window()); MUST(run_file_operation(FileOperation::Copy, paths_to_copy, target_node.full_path(), window()));
if (had_accepted_drop && on_accepted_drop) if (had_accepted_drop && on_accepted_drop)
on_accepted_drop(); on_accepted_drop();

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -9,6 +9,7 @@
#include "FileOperationProgressWidget.h" #include "FileOperationProgressWidget.h"
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibGUI/MessageBox.h> #include <LibGUI/MessageBox.h>
#include <unistd.h> #include <unistd.h>
@ -35,32 +36,19 @@ void delete_paths(Vector<String> const& paths, bool should_confirm, GUI::Window*
return; return;
} }
run_file_operation(FileOperation::Delete, paths, {}, parent_window); if (run_file_operation(FileOperation::Delete, paths, {}, parent_window).is_error())
_exit(1);
} }
void run_file_operation(FileOperation operation, Vector<String> const& sources, String const& destination, GUI::Window* parent_window) ErrorOr<void> run_file_operation(FileOperation operation, Vector<String> const& sources, String const& destination, GUI::Window* parent_window)
{ {
int pipe_fds[2]; auto pipe_fds = TRY(Core::System::pipe2(0));
if (pipe(pipe_fds) < 0) {
perror("pipe");
VERIFY_NOT_REACHED();
}
pid_t child_pid = fork(); pid_t child_pid = TRY(Core::System::fork());
if (child_pid < 0) {
perror("fork");
VERIFY_NOT_REACHED();
}
if (!child_pid) { if (!child_pid) {
if (close(pipe_fds[0]) < 0) { TRY(Core::System::close(pipe_fds[0]));
perror("close"); TRY(Core::System::dup2(pipe_fds[1], STDOUT_FILENO));
_exit(1);
}
if (dup2(pipe_fds[1], STDOUT_FILENO) < 0) {
perror("dup2");
_exit(1);
}
Vector<char const*> file_operation_args; Vector<char const*> file_operation_args;
file_operation_args.append("/bin/FileOperation"); file_operation_args.append("/bin/FileOperation");
@ -93,16 +81,13 @@ void run_file_operation(FileOperation operation, Vector<String> const& sources,
} }
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} else { } else {
if (close(pipe_fds[1]) < 0) { TRY(Core::System::close(pipe_fds[1]));
perror("close");
_exit(1);
}
} }
auto window = GUI::Window::construct(); auto window = TRY(GUI::Window::try_create());
file_operation_windows.set(window); TRY(file_operation_windows.try_set(window));
auto pipe_input_file = Core::File::construct(); auto pipe_input_file = TRY(Core::File::try_create());
pipe_input_file->open(pipe_fds[0], Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes); pipe_input_file->open(pipe_fds[0], Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes);
switch (operation) { switch (operation) {
@ -119,11 +104,13 @@ void run_file_operation(FileOperation operation, Vector<String> const& sources,
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
window->set_main_widget<FileOperationProgressWidget>(operation, pipe_input_file); (void)TRY(window->try_set_main_widget<FileOperationProgressWidget>(operation, pipe_input_file));
window->resize(320, 190); window->resize(320, 190);
if (parent_window) if (parent_window)
window->center_within(*parent_window); window->center_within(*parent_window);
window->show(); window->show();
return {};
} }
} }

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -21,5 +21,5 @@ enum class FileOperation {
void delete_paths(Vector<String> const&, bool should_confirm, GUI::Window*); void delete_paths(Vector<String> const&, bool should_confirm, GUI::Window*);
void run_file_operation(FileOperation, Vector<String> const& sources, String const& destination, GUI::Window*); ErrorOr<void> run_file_operation(FileOperation, Vector<String> const& sources, String const& destination, GUI::Window*);
} }

View file

@ -176,8 +176,10 @@ void do_paste(String const& target_directory, GUI::Window* window)
source_paths.append(url.path()); source_paths.append(url.path());
} }
if (!source_paths.is_empty()) if (!source_paths.is_empty()) {
run_file_operation(file_operation, source_paths, target_directory, window); if (auto result = run_file_operation(file_operation, source_paths, target_directory, window); result.is_error())
dbgln("Failed to paste files: {}", result.error());
}
} }
void do_create_link(Vector<String> const& selected_file_paths, GUI::Window* window) void do_create_link(Vector<String> const& selected_file_paths, GUI::Window* window)