瀏覽代碼

FileManager: Use TRY in run_file_operation()

The exception is `execvp()` since that has no Core::System wrapper yet.
Sam Atkins 3 年之前
父節點
當前提交
dc5a318aa9

+ 1 - 1
Userland/Applications/FileManager/DirectoryView.cpp

@@ -650,7 +650,7 @@ void DirectoryView::handle_drop(GUI::ModelIndex const& index, GUI::DropEvent con
     }
 
     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)
         on_accepted_drop();

+ 16 - 29
Userland/Applications/FileManager/FileUtils.cpp

@@ -1,6 +1,6 @@
 /*
  * 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
  */
@@ -9,6 +9,7 @@
 #include "FileOperationProgressWidget.h"
 #include <AK/LexicalPath.h>
 #include <LibCore/File.h>
+#include <LibCore/System.h>
 #include <LibGUI/MessageBox.h>
 #include <unistd.h>
 
@@ -35,32 +36,19 @@ void delete_paths(Vector<String> const& paths, bool should_confirm, GUI::Window*
             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];
-    if (pipe(pipe_fds) < 0) {
-        perror("pipe");
-        VERIFY_NOT_REACHED();
-    }
+    auto pipe_fds = TRY(Core::System::pipe2(0));
 
-    pid_t child_pid = fork();
-    if (child_pid < 0) {
-        perror("fork");
-        VERIFY_NOT_REACHED();
-    }
+    pid_t child_pid = TRY(Core::System::fork());
 
     if (!child_pid) {
-        if (close(pipe_fds[0]) < 0) {
-            perror("close");
-            _exit(1);
-        }
-        if (dup2(pipe_fds[1], STDOUT_FILENO) < 0) {
-            perror("dup2");
-            _exit(1);
-        }
+        TRY(Core::System::close(pipe_fds[0]));
+        TRY(Core::System::dup2(pipe_fds[1], STDOUT_FILENO));
 
         Vector<char const*> file_operation_args;
         file_operation_args.append("/bin/FileOperation");
@@ -93,16 +81,13 @@ void run_file_operation(FileOperation operation, Vector<String> const& sources,
         }
         VERIFY_NOT_REACHED();
     } else {
-        if (close(pipe_fds[1]) < 0) {
-            perror("close");
-            _exit(1);
-        }
+        TRY(Core::System::close(pipe_fds[1]));
     }
 
-    auto window = GUI::Window::construct();
-    file_operation_windows.set(window);
+    auto window = TRY(GUI::Window::try_create());
+    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);
 
     switch (operation) {
@@ -119,11 +104,13 @@ void run_file_operation(FileOperation operation, Vector<String> const& sources,
         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);
     if (parent_window)
         window->center_within(*parent_window);
     window->show();
+
+    return {};
 }
 
 }

+ 2 - 2
Userland/Applications/FileManager/FileUtils.h

@@ -1,6 +1,6 @@
 /*
  * 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
  */
@@ -21,5 +21,5 @@ enum class FileOperation {
 
 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*);
 }

+ 4 - 2
Userland/Applications/FileManager/main.cpp

@@ -176,8 +176,10 @@ void do_paste(String const& target_directory, GUI::Window* window)
         source_paths.append(url.path());
     }
 
-    if (!source_paths.is_empty())
-        run_file_operation(file_operation, source_paths, target_directory, window);
+    if (!source_paths.is_empty()) {
+        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)