From 907e0b9e1dce4396eb90e53e7555911aaa022c82 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sun, 18 Jun 2023 21:12:09 +0100 Subject: [PATCH] chown: Don't bail immediately on error Previously, chown would exit immediately if there was an error changing file ownership. We now print an error to stderr and continue when an error occurs. --- Userland/Utilities/chown.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/Userland/Utilities/chown.cpp b/Userland/Utilities/chown.cpp index 57c9bbcdf0..2f5d0eeaf9 100644 --- a/Userland/Utilities/chown.cpp +++ b/Userland/Utilities/chown.cpp @@ -70,31 +70,42 @@ ErrorOr serenity_main(Main::Arguments arguments) } } - Function(StringView)> update_path_owner = [&](StringView path) -> ErrorOr { - auto stat = TRY(Core::System::lstat(path)); + Function update_path_owner = [&](StringView path) { + auto stat_or_error = Core::System::lstat(path); + if (stat_or_error.is_error()) { + warnln("Could not stat '{}': {}", path, stat_or_error.release_error()); + return false; + } + + auto stat = stat_or_error.release_value(); if (S_ISLNK(stat.st_mode) && !follow_symlinks && !paths.contains_slow(path)) - return {}; + return false; - if (no_dereference) { - TRY(Core::System::lchown(path, new_uid, new_gid)); - } else { - TRY(Core::System::chown(path, new_uid, new_gid)); + auto success = true; + auto maybe_error = no_dereference + ? Core::System::lchown(path, new_uid, new_gid) + : Core::System::chown(path, new_uid, new_gid); + + if (maybe_error.is_error()) { + warnln("Failed to change owner of '{}': {}", path, maybe_error.release_error()); + success = false; } if (recursive && S_ISDIR(stat.st_mode)) { Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir); while (it.has_next()) - TRY(update_path_owner(it.next_full_path())); + success &= update_path_owner(it.next_full_path()); } - return {}; + return success; }; - for (auto path : paths) { - TRY(update_path_owner(path)); + auto success = true; + for (auto const& path : paths) { + success &= update_path_owner(path); } - return 0; + return success ? 0 : 1; }