From 16fcbe555e3c97b12dbbc3118df2c7c3c70d64c6 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Wed, 21 Jun 2023 18:52:12 +0100 Subject: [PATCH] touch: Don't bail immediately on error Previously, touch would exit immediately if there was an error changing file permissions. We now print an error to stderr and continue when an error occurs. --- Userland/Utilities/touch.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Userland/Utilities/touch.cpp b/Userland/Utilities/touch.cpp index f89bc67506..ce12d4ccdf 100644 --- a/Userland/Utilities/touch.cpp +++ b/Userland/Utilities/touch.cpp @@ -239,16 +239,28 @@ ErrorOr serenity_main(Main::Arguments arguments) if (update_mtime && !update_atime) atime.tv_nsec = UTIME_OMIT; + auto has_errors = false; for (auto path : paths) { if (FileSystem::exists(path)) { - if (utimensat(AT_FDCWD, path.characters(), times, 0) == -1) - err("failed to touch '{}': {}", path, strerror(errno)); + if (utimensat(AT_FDCWD, path.characters(), times, 0) == -1) { + warnln("failed to touch '{}': {}", path, strerror(errno)); + has_errors = true; + } } else if (!no_create_file) { - int fd = TRY(Core::System::open(path, O_CREAT, 0100644)); - if (futimens(fd, times) == -1) - err("failed to touch '{}': {}", path, strerror(errno)); - TRY(Core::System::close(fd)); + auto error_or_fd = Core::System::open(path, O_CREAT, 0100644); + if (error_or_fd.is_error()) { + warnln("failed to open '{}': {}", path, strerror(error_or_fd.error().code())); + has_errors = true; + continue; + } + + if (futimens(error_or_fd.value(), times) == -1) { + warnln("failed to touch '{}': {}", path, strerror(errno)); + has_errors = true; + continue; + } + (void)Core::System::close(error_or_fd.value()); } } - return 0; + return has_errors ? 1 : 0; }