1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:57:35 +00:00

mv: Support the '--no-clobber' option

This commit is contained in:
Eli Youngs 2022-10-19 21:33:56 -07:00 committed by Linus Groh
parent 0d7d759095
commit 7b05f2c4d6

View file

@ -19,15 +19,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
TRY(Core::System::pledge("stdio rpath wpath cpath fattr")); 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 force = false;
bool no_clobber = false;
bool verbose = false; bool verbose = false;
Vector<String> paths; Vector<String> paths;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_option(force, "Force", "force", 'f'); 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_option(verbose, "Verbose", "verbose", 'v');
args_parser.add_positional_argument(paths, "Paths to files being moved followed by target location", "paths"); args_parser.add_positional_argument(paths, "Paths to files being moved followed by target location", "paths");
args_parser.parse(arguments); args_parser.parse(arguments);
@ -37,6 +37,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return 1; return 1;
} }
if (force && no_clobber) {
warnln("-f (--force) overrides -n (--no-clobber)");
no_clobber = false;
}
auto original_new_path = paths.take_last(); auto original_new_path = paths.take_last();
struct stat st; struct stat st;
@ -64,6 +69,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
new_path = combined_new_path.characters(); new_path = combined_new_path.characters();
} }
if (no_clobber && Core::File::exists(new_path))
continue;
rc = rename(old_path.characters(), new_path.characters()); rc = rename(old_path.characters(), new_path.characters());
if (rc < 0) { if (rc < 0) {
if (errno == EXDEV) { if (errno == EXDEV) {