changeset 1455:70f5188b4848 draft

Add cp -F to force delete of existing destination files, and make install command use that. -F is equivalent to the gnu/dammit flag --we-think-long-options-without-short-options-are-a-good-idea
author Rob Landley <rob@landley.net>
date Mon, 01 Sep 2014 12:47:32 -0500
parents ab91ee85afbd
children 5d51c5d6df72
files toys/posix/cp.c
diffstat 1 files changed, 8 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/toys/posix/cp.c	Sun Aug 31 22:07:43 2014 -0500
+++ b/toys/posix/cp.c	Mon Sep 01 12:47:32 2014 -0500
@@ -7,8 +7,8 @@
 // This is subtle: MV options must be in same order (right to left) as CP
 // for FLAG_X macros to work out right.
 
-USE_CP(NEWTOY(cp, "<2RHLPp"USE_CP_MORE("rdaslvn")"fi[-HLPd]"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
-USE_CP_MV(OLDTOY(mv, cp, "<2"USE_CP_MORE("vn")"fi"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
+USE_CP(NEWTOY(cp, "<2RHLPp"USE_CP_MORE("rdaslvnF")"fi[-HLPd]"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
+USE_CP_MV(OLDTOY(mv, cp, "<2"USE_CP_MORE("vnF")"fi"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
 USE_INSTALL(NEWTOY(install, "<1cdDpsvm:o:g:", TOYFLAG_USR|TOYFLAG_BIN))
 *
 
@@ -21,7 +21,8 @@
     Copy files from SOURCE to DEST.  If more than one SOURCE, DEST must
     be a directory.
 
-    -f	force copy by deleting destination file
+    -f	delete destination files we can't write to
+    -F	delete any existing destination file first (breaks hardlinks)
     -i	interactive, prompt before overwriting existing DEST
     -p	preserve timestamps, ownership, and permissions
     -R	recurse into subdirectories (DEST must be a directory)
@@ -142,6 +143,9 @@
         error_msg("dir at '%s'", s = dirtree_path(try, 0));
         free(s);
         return 0;
+      } else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0)) {
+        error_msg("unlink '%s'", catch);
+        return 0;
       } else if (flags & FLAG_n) return 0;
       else if (flags & FLAG_i) {
         fprintf(stderr, "cp: overwrite '%s'", s = dirtree_path(try, 0));
@@ -365,7 +369,7 @@
   if (toys.optc < 2) error_exit("needs 2 args");
 
   // Translate flags from install to cp
-  toys.optflags = 4;  // Force cp's FLAG_n
+  toys.optflags = 4;  // Force cp's FLAG_F
   if (flags & FLAG_v) toys.optflags |= 8; // cp's FLAG_v
   if (flags & (FLAG_p|FLAG_o|FLAG_g)) toys.optflags |= 512; // cp's FLAG_p