Ver código fonte

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

Eli Youngs 2 anos atrás
pai
commit
7b05f2c4d6
1 arquivos alterados com 10 adições e 2 exclusões
  1. 10 2
      Userland/Utilities/mv.cpp

+ 10 - 2
Userland/Utilities/mv.cpp

@@ -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) {