From 7b05f2c4d64126ac869aff6dc358db6bb4ad0b34 Mon Sep 17 00:00:00 2001 From: Eli Youngs Date: Wed, 19 Oct 2022 21:33:56 -0700 Subject: [PATCH] mv: Support the '--no-clobber' option --- Userland/Utilities/mv.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Utilities/mv.cpp b/Userland/Utilities/mv.cpp index fbfda02ee8..a9b942fda5 100644 --- a/Userland/Utilities/mv.cpp +++ b/Userland/Utilities/mv.cpp @@ -19,15 +19,15 @@ ErrorOr serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath wpath cpath fattr")); - // NOTE: The "force" option is a dummy for now, it's just here to silence scripts that use "mv -f" - // In the future, it might be used to cancel out an "-i" interactive option. bool force = false; + bool no_clobber = false; bool verbose = false; Vector paths; Core::ArgsParser args_parser; args_parser.add_option(force, "Force", "force", 'f'); + args_parser.add_option(no_clobber, "Do not overwrite existing files", "no-clobber", 'n'); args_parser.add_option(verbose, "Verbose", "verbose", 'v'); args_parser.add_positional_argument(paths, "Paths to files being moved followed by target location", "paths"); args_parser.parse(arguments); @@ -37,6 +37,11 @@ ErrorOr serenity_main(Main::Arguments arguments) return 1; } + if (force && no_clobber) { + warnln("-f (--force) overrides -n (--no-clobber)"); + no_clobber = false; + } + auto original_new_path = paths.take_last(); struct stat st; @@ -64,6 +69,9 @@ ErrorOr serenity_main(Main::Arguments arguments) new_path = combined_new_path.characters(); } + if (no_clobber && Core::File::exists(new_path)) + continue; + rc = rename(old_path.characters(), new_path.characters()); if (rc < 0) { if (errno == EXDEV) {