Quellcode durchsuchen

LibCore: Use ErrorOr<T> for Core::File::remove()

This function returns a subclass of Error, which is now possible.
Andreas Kling vor 3 Jahren
Ursprung
Commit
c7e62d448c

+ 1 - 1
Userland/Applications/SpaceAnalyzer/main.cpp

@@ -353,7 +353,7 @@ int main(int argc, char* argv[])
                 auto retry_message_result = GUI::MessageBox::show(window,
                 auto retry_message_result = GUI::MessageBox::show(window,
                     String::formatted("Failed to delete \"{}\": {}. Retry?",
                     String::formatted("Failed to delete \"{}\": {}. Retry?",
                         deletion_result.error().file,
                         deletion_result.error().file,
-                        deletion_result.error().error_code.string()),
+                        static_cast<Error const&>(deletion_result.error())),
                     "Deletion failed",
                     "Deletion failed",
                     GUI::MessageBox::Type::Error,
                     GUI::MessageBox::Type::Error,
                     GUI::MessageBox::InputType::YesNo);
                     GUI::MessageBox::InputType::YesNo);

+ 3 - 4
Userland/DevTools/HackStudio/HackStudioWidget.cpp

@@ -546,17 +546,16 @@ NonnullRefPtr<GUI::Action> HackStudioWidget::create_delete_action()
             }
             }
 
 
             bool is_directory = S_ISDIR(st.st_mode);
             bool is_directory = S_ISDIR(st.st_mode);
-            auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false);
-            if (result.is_error()) {
+            if (auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false); !result.is_error()) {
                 auto& error = result.error();
                 auto& error = result.error();
                 if (is_directory) {
                 if (is_directory) {
                     GUI::MessageBox::show(window(),
                     GUI::MessageBox::show(window(),
-                        String::formatted("Removing directory {} from the project failed: {}", error.file, error.error_code),
+                        String::formatted("Removing directory {} from the project failed: {}", error.file, static_cast<Error const&>(error)),
                         "Removal failed",
                         "Removal failed",
                         GUI::MessageBox::Type::Error);
                         GUI::MessageBox::Type::Error);
                 } else {
                 } else {
                     GUI::MessageBox::show(window(),
                     GUI::MessageBox::show(window(),
-                        String::formatted("Removing file {} from the project failed: {}", error.file, error.error_code),
+                        String::formatted("Removing file {} from the project failed: {}", error.file, static_cast<Error const&>(error)),
                         "Removal failed",
                         "Removal failed",
                         GUI::MessageBox::Type::Error);
                         GUI::MessageBox::Type::Error);
                 }
                 }

+ 5 - 5
Userland/Libraries/LibCore/File.cpp

@@ -512,19 +512,19 @@ ErrorOr<void> File::link_file(String const& dst_path, String const& src_path)
     return {};
     return {};
 }
 }
 
 
-Result<void, File::RemoveError> File::remove(String const& path, RecursionMode mode, bool force)
+ErrorOr<void, File::RemoveError> File::remove(String const& path, RecursionMode mode, bool force)
 {
 {
     struct stat path_stat;
     struct stat path_stat;
     if (lstat(path.characters(), &path_stat) < 0) {
     if (lstat(path.characters(), &path_stat) < 0) {
         if (!force)
         if (!force)
-            return RemoveError { path, OSError(errno) };
+            return RemoveError { path, errno };
         return {};
         return {};
     }
     }
 
 
     if (S_ISDIR(path_stat.st_mode) && mode == RecursionMode::Allowed) {
     if (S_ISDIR(path_stat.st_mode) && mode == RecursionMode::Allowed) {
         auto di = DirIterator(path, DirIterator::SkipParentAndBaseDir);
         auto di = DirIterator(path, DirIterator::SkipParentAndBaseDir);
         if (di.has_error())
         if (di.has_error())
-            return RemoveError { path, OSError(di.error()) };
+            return RemoveError { path, di.error() };
 
 
         while (di.has_next()) {
         while (di.has_next()) {
             auto result = remove(di.next_full_path(), RecursionMode::Allowed, true);
             auto result = remove(di.next_full_path(), RecursionMode::Allowed, true);
@@ -533,10 +533,10 @@ Result<void, File::RemoveError> File::remove(String const& path, RecursionMode m
         }
         }
 
 
         if (rmdir(path.characters()) < 0 && !force)
         if (rmdir(path.characters()) < 0 && !force)
-            return RemoveError { path, OSError(errno) };
+            return RemoveError { path, errno };
     } else {
     } else {
         if (unlink(path.characters()) < 0 && !force)
         if (unlink(path.characters()) < 0 && !force)
-            return RemoveError { path, OSError(errno) };
+            return RemoveError { path, errno };
     }
     }
 
 
     return {};
     return {};

+ 7 - 3
Userland/Libraries/LibCore/File.h

@@ -72,11 +72,15 @@ public:
     static String read_link(String const& link_path);
     static String read_link(String const& link_path);
     static ErrorOr<void> link_file(String const& dst_path, String const& src_path);
     static ErrorOr<void> link_file(String const& dst_path, String const& src_path);
 
 
-    struct RemoveError {
+    struct RemoveError : public Error {
+        RemoveError(String f, int error_code)
+            : Error(error_code)
+            , file(move(f))
+        {
+        }
         String file;
         String file;
-        OSError error_code;
     };
     };
-    static Result<void, RemoveError> remove(String const& path, RecursionMode, bool force);
+    static ErrorOr<void, RemoveError> remove(String const& path, RecursionMode, bool force);
 
 
     virtual bool open(OpenMode) override;
     virtual bool open(OpenMode) override;
 
 

+ 1 - 1
Userland/Utilities/rm.cpp

@@ -40,7 +40,7 @@ int main(int argc, char** argv)
         auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force);
         auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force);
 
 
         if (result.is_error()) {
         if (result.is_error()) {
-            warnln("rm: cannot remove '{}': {}", path, result.error().error_code);
+            warnln("rm: cannot remove '{}': {}", path, static_cast<Error const&>(result.error()));
             had_errors = true;
             had_errors = true;
         }
         }