diff --git a/Userland/Applications/FileManager/main.cpp b/Userland/Applications/FileManager/main.cpp index 7f3a91628a..1e7f1e98b7 100644 --- a/Userland/Applications/FileManager/main.cpp +++ b/Userland/Applications/FileManager/main.cpp @@ -1169,7 +1169,7 @@ int run_in_windowed_mode(String initial_location, String entry_focused_on_init) continue; if (auto result = Core::File::copy_file_or_directory(url_to_copy.path(), new_path); result.is_error()) { - auto error_message = String::formatted("Could not copy {} into {}:\n {}", url_to_copy.to_string(), new_path, result.error().error_code); + auto error_message = String::formatted("Could not copy {} into {}:\n {}", url_to_copy.to_string(), new_path, static_cast(result.error())); GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error); } else { had_accepted_copy = true; diff --git a/Userland/Applications/Spreadsheet/ExportDialog.cpp b/Userland/Applications/Spreadsheet/ExportDialog.cpp index 52321f7e1c..62a82f53be 100644 --- a/Userland/Applications/Spreadsheet/ExportDialog.cpp +++ b/Userland/Applications/Spreadsheet/ExportDialog.cpp @@ -231,7 +231,7 @@ Result CSVExportDialogPage::move_into(const String& target) Core::File::AddDuplicateFileMarker::No); if (result.is_error()) - return String { result.error().error_code.string() }; + return String::formatted("{}", static_cast(result.error())); return {}; } diff --git a/Userland/DevTools/HackStudio/ProjectTemplate.cpp b/Userland/DevTools/HackStudio/ProjectTemplate.cpp index 5dcf75336c..34a2523624 100644 --- a/Userland/DevTools/HackStudio/ProjectTemplate.cpp +++ b/Userland/DevTools/HackStudio/ProjectTemplate.cpp @@ -72,7 +72,7 @@ Result ProjectTemplate::create_project(const String& name, const S auto result = Core::File::copy_file_or_directory(path, content_path()); dbgln("Copying {} -> {}", content_path(), path); if (result.is_error()) - return String::formatted("Failed to copy template contents. Error code: {}", result.error().error_code); + return String::formatted("Failed to copy template contents. Error code: {}", static_cast(result.error())); } else { dbgln("No template content directory found for '{}', creating an empty directory for the project.", m_id); int rc; diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index 7a9803b58f..64993d68fb 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -347,7 +347,7 @@ static String get_duplicate_name(String const& path, int duplicate_count) return duplicated_name.build(); } -Result File::copy_file_or_directory(String const& dst_path, String const& src_path, RecursionMode recursion_mode, LinkMode link_mode, AddDuplicateFileMarker add_duplicate_file_marker, PreserveMode preserve_mode) +ErrorOr File::copy_file_or_directory(String const& dst_path, String const& src_path, RecursionMode recursion_mode, LinkMode link_mode, AddDuplicateFileMarker add_duplicate_file_marker, PreserveMode preserve_mode) { if (add_duplicate_file_marker == AddDuplicateFileMarker::Yes) { int duplicate_count = 0; @@ -361,23 +361,23 @@ Result File::copy_file_or_directory(String const& dst_pat auto source_or_error = File::open(src_path, OpenMode::ReadOnly); if (source_or_error.is_error()) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; auto& source = *source_or_error.value(); struct stat src_stat; if (fstat(source.fd(), &src_stat) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; if (source.is_directory()) { if (recursion_mode == RecursionMode::Disallowed) - return CopyError { OSError(errno), true }; + return CopyError { errno, true }; return copy_directory(dst_path, src_path, src_stat); } if (link_mode == LinkMode::Allowed) { if (link(src_path.characters(), dst_path.characters()) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; return {}; } @@ -385,31 +385,31 @@ Result File::copy_file_or_directory(String const& dst_pat return copy_file(dst_path, src_stat, source, preserve_mode); } -Result File::copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode preserve_mode) +ErrorOr File::copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode preserve_mode) { int dst_fd = creat(dst_path.characters(), 0666); if (dst_fd < 0) { if (errno != EISDIR) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; auto dst_dir_path = String::formatted("{}/{}", dst_path, LexicalPath::basename(source.filename())); dst_fd = creat(dst_dir_path.characters(), 0666); if (dst_fd < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; } ScopeGuard close_fd_guard([dst_fd]() { ::close(dst_fd); }); if (src_stat.st_size > 0) { if (ftruncate(dst_fd, src_stat.st_size) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; } for (;;) { char buffer[32768]; ssize_t nread = ::read(source.fd(), buffer, sizeof(buffer)); if (nread < 0) { - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; } if (nread == 0) break; @@ -418,7 +418,7 @@ Result File::copy_file(String const& dst_path, struct sta while (remaining_to_write) { ssize_t nwritten = ::write(dst_fd, bufptr, remaining_to_write); if (nwritten < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; VERIFY(nwritten > 0); remaining_to_write -= nwritten; @@ -433,27 +433,27 @@ Result File::copy_file(String const& dst_path, struct sta my_umask |= 06000; if (fchmod(dst_fd, src_stat.st_mode & ~my_umask) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; if (preserve_mode == PreserveMode::PermissionsOwnershipTimestamps) { if (fchown(dst_fd, src_stat.st_uid, src_stat.st_gid) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; // FIXME: Implement utimens() and use it here. struct utimbuf timbuf; timbuf.actime = src_stat.st_atime; timbuf.modtime = src_stat.st_mtime; if (utime(dst_path.characters(), &timbuf) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; } return {}; } -Result File::copy_directory(String const& dst_path, String const& src_path, struct stat const& src_stat, LinkMode link, PreserveMode preserve_mode) +ErrorOr File::copy_directory(String const& dst_path, String const& src_path, struct stat const& src_stat, LinkMode link, PreserveMode preserve_mode) { if (mkdir(dst_path.characters(), 0755) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; String src_rp = File::real_path_for(src_path); src_rp = String::formatted("{}/", src_rp); @@ -461,11 +461,11 @@ Result File::copy_directory(String const& dst_path, Strin dst_rp = String::formatted("{}/", dst_rp); if (!dst_rp.is_empty() && dst_rp.starts_with(src_rp)) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; DirIterator di(src_path, DirIterator::SkipDots); if (di.has_error()) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; while (di.has_next()) { String filename = di.next_path(); @@ -481,18 +481,18 @@ Result File::copy_directory(String const& dst_path, Strin umask(my_umask); if (chmod(dst_path.characters(), src_stat.st_mode & ~my_umask) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; if (preserve_mode == PreserveMode::PermissionsOwnershipTimestamps) { if (chown(dst_path.characters(), src_stat.st_uid, src_stat.st_gid) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; // FIXME: Implement utimens() and use it here. struct utimbuf timbuf; timbuf.actime = src_stat.st_atime; timbuf.modtime = src_stat.st_atime; if (utime(dst_path.characters(), &timbuf) < 0) - return CopyError { OSError(errno), false }; + return CopyError { errno, false }; } return {}; diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index 87603881d0..b36668785e 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -59,14 +60,18 @@ public: PermissionsOwnershipTimestamps, }; - struct CopyError { - OSError error_code; + struct CopyError : public Error { + CopyError(int error_code, bool t) + : Error(error_code) + , tried_recursing(t) + { + } bool tried_recursing; }; - static Result copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing); - static Result copy_directory(String const& dst_path, String const& src_path, struct stat const& src_stat, LinkMode = LinkMode::Disallowed, PreserveMode = PreserveMode::Nothing); - static Result copy_file_or_directory(String const& dst_path, String const& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes, PreserveMode = PreserveMode::Nothing); + static ErrorOr copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing); + static ErrorOr copy_directory(String const& dst_path, String const& src_path, struct stat const& src_stat, LinkMode = LinkMode::Disallowed, PreserveMode = PreserveMode::Nothing); + static ErrorOr copy_file_or_directory(String const& dst_path, String const& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes, PreserveMode = PreserveMode::Nothing); static String real_path_for(String const& filename); static String read_link(String const& link_path); diff --git a/Userland/Utilities/cp.cpp b/Userland/Utilities/cp.cpp index b27b033476..b5b1cfa668 100644 --- a/Userland/Utilities/cp.cpp +++ b/Userland/Utilities/cp.cpp @@ -61,7 +61,7 @@ int main(int argc, char** argv) if (result.error().tried_recursing) warnln("cp: -R not specified; omitting directory '{}'", source); else - warnln("cp: unable to copy '{}' to '{}': {}", source, destination_path, result.error().error_code); + warnln("cp: unable to copy '{}' to '{}': {}", source, destination_path, static_cast(result.error())); return 1; } diff --git a/Userland/Utilities/mv.cpp b/Userland/Utilities/mv.cpp index 1e917f69b7..29f607d2be 100644 --- a/Userland/Utilities/mv.cpp +++ b/Userland/Utilities/mv.cpp @@ -74,7 +74,7 @@ int main(int argc, char** argv) Core::File::AddDuplicateFileMarker::No); if (result.is_error()) { - warnln("mv: could not move '{}': {}", old_path, result.error().error_code); + warnln("mv: could not move '{}': {}", old_path, static_cast(result.error())); return 1; } rc = unlink(old_path); diff --git a/Userland/Utilities/usermod.cpp b/Userland/Utilities/usermod.cpp index 7b70659517..53e425b893 100644 --- a/Userland/Utilities/usermod.cpp +++ b/Userland/Utilities/usermod.cpp @@ -125,7 +125,7 @@ int main(int argc, char** argv) Core::File::AddDuplicateFileMarker::No); if (result.is_error()) { - warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), result.error().error_code); + warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), static_cast(result.error())); return 1; } rc = unlink(target_account.home_directory().characters());