summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorEli Youngs <eli.m.youngs@gmail.com>2022-10-19 21:33:56 -0700
committerLinus Groh <mail@linusgroh.de>2022-10-24 23:48:14 +0100
commit7b05f2c4d64126ac869aff6dc358db6bb4ad0b34 (patch)
treef0524ca94050e8fd72e7e49f084bd990e2a26934 /Userland/Utilities
parent0d7d759095e5cab6cc1c9799574ca712caba8bc0 (diff)
downloadserenity-7b05f2c4d64126ac869aff6dc358db6bb4ad0b34.zip
mv: Support the '--no-clobber' option
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/mv.cpp12
1 files 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<int> 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<String> 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<int> 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<int> 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) {