FileUtils.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "FileUtils.h"
  8. #include "FileOperationProgressWidget.h"
  9. #include <AK/LexicalPath.h>
  10. #include <LibCore/MimeData.h>
  11. #include <LibCore/System.h>
  12. #include <LibFileSystem/FileSystem.h>
  13. #include <LibGUI/Event.h>
  14. #include <LibGUI/MessageBox.h>
  15. #include <unistd.h>
  16. namespace FileManager {
  17. HashTable<NonnullRefPtr<GUI::Window>> file_operation_windows;
  18. void delete_paths(Vector<DeprecatedString> const& paths, bool should_confirm, GUI::Window* parent_window)
  19. {
  20. DeprecatedString message;
  21. if (paths.size() == 1) {
  22. message = DeprecatedString::formatted("Are you sure you want to delete \"{}\"?", LexicalPath::basename(paths[0]));
  23. } else {
  24. message = DeprecatedString::formatted("Are you sure you want to delete {} files?", paths.size());
  25. }
  26. if (should_confirm) {
  27. auto result = GUI::MessageBox::show(parent_window,
  28. message,
  29. "Confirm Deletion"sv,
  30. GUI::MessageBox::Type::Warning,
  31. GUI::MessageBox::InputType::OKCancel);
  32. if (result == GUI::MessageBox::ExecResult::Cancel)
  33. return;
  34. }
  35. if (run_file_operation(FileOperation::Delete, paths, {}, parent_window).is_error())
  36. _exit(1);
  37. }
  38. ErrorOr<void> run_file_operation(FileOperation operation, Vector<DeprecatedString> const& sources, DeprecatedString const& destination, GUI::Window* parent_window)
  39. {
  40. auto pipe_fds = TRY(Core::System::pipe2(0));
  41. pid_t child_pid = TRY(Core::System::fork());
  42. if (!child_pid) {
  43. TRY(Core::System::close(pipe_fds[0]));
  44. TRY(Core::System::dup2(pipe_fds[1], STDOUT_FILENO));
  45. Vector<StringView> file_operation_args;
  46. file_operation_args.append("/bin/FileOperation"sv);
  47. switch (operation) {
  48. case FileOperation::Copy:
  49. file_operation_args.append("Copy"sv);
  50. break;
  51. case FileOperation::Move:
  52. file_operation_args.append("Move"sv);
  53. break;
  54. case FileOperation::Delete:
  55. file_operation_args.append("Delete"sv);
  56. break;
  57. default:
  58. VERIFY_NOT_REACHED();
  59. }
  60. for (auto& source : sources)
  61. file_operation_args.append(source.view());
  62. if (operation != FileOperation::Delete)
  63. file_operation_args.append(destination.view());
  64. TRY(Core::System::exec(file_operation_args.first(), file_operation_args, Core::System::SearchInPath::Yes));
  65. VERIFY_NOT_REACHED();
  66. } else {
  67. TRY(Core::System::close(pipe_fds[1]));
  68. }
  69. auto window = GUI::Window::construct();
  70. TRY(file_operation_windows.try_set(window));
  71. switch (operation) {
  72. case FileOperation::Copy:
  73. window->set_title("Copying Files...");
  74. break;
  75. case FileOperation::Move:
  76. window->set_title("Moving Files...");
  77. break;
  78. case FileOperation::Delete:
  79. window->set_title("Deleting Files...");
  80. break;
  81. default:
  82. VERIFY_NOT_REACHED();
  83. }
  84. auto pipe_input_file = TRY(Core::File::adopt_fd(pipe_fds[0], Core::File::OpenMode::Read));
  85. auto buffered_pipe = TRY(Core::InputBufferedFile::create(move(pipe_input_file)));
  86. (void)TRY(window->set_main_widget<FileOperationProgressWidget>(operation, move(buffered_pipe), pipe_fds[0]));
  87. window->resize(320, 190);
  88. if (parent_window)
  89. window->center_within(*parent_window);
  90. window->show();
  91. return {};
  92. }
  93. ErrorOr<bool> handle_drop(GUI::DropEvent const& event, DeprecatedString const& destination, GUI::Window* window)
  94. {
  95. bool has_accepted_drop = false;
  96. if (!event.mime_data().has_urls())
  97. return has_accepted_drop;
  98. auto const urls = event.mime_data().urls();
  99. if (urls.is_empty()) {
  100. dbgln("No files to drop");
  101. return has_accepted_drop;
  102. }
  103. auto const target = LexicalPath::canonicalized_path(destination);
  104. if (!FileSystem::is_directory(target))
  105. return has_accepted_drop;
  106. Vector<DeprecatedString> paths_to_copy;
  107. for (auto& url_to_copy : urls) {
  108. auto file_path = url_to_copy.serialize_path();
  109. if (!url_to_copy.is_valid() || file_path == target)
  110. continue;
  111. auto new_path = DeprecatedString::formatted("{}/{}", target, LexicalPath::basename(file_path));
  112. if (file_path == new_path)
  113. continue;
  114. paths_to_copy.append(file_path);
  115. has_accepted_drop = true;
  116. }
  117. if (!paths_to_copy.is_empty())
  118. TRY(run_file_operation(FileOperation::Copy, paths_to_copy, target, window));
  119. return has_accepted_drop;
  120. }
  121. }