diff toys/posix/cp.c @ 1388:c4f5f82adce6 draft

Implement -HL for cp.
author Rob Landley <rob@landley.net>
date Mon, 14 Jul 2014 05:38:27 -0500
parents 37ea9dff9c27
children ffc7f606ce5b
line wrap: on
line diff
--- a/toys/posix/cp.c	Mon Jul 07 07:32:56 2014 -0500
+++ b/toys/posix/cp.c	Mon Jul 14 05:38:27 2014 -0500
@@ -2,12 +2,12 @@
  *
  * See http://opengroup.org/onlinepubs/9699919799/utilities/cp.html
  *
- * TODO: sHLP
+ * Posix says "cp -Rf dir file" shouldn't delete file, but our -f does.
 
 // 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"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
+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))
 
 config CP
@@ -66,8 +66,6 @@
 #define FOR_cp
 #include "toys.h"
 
-// TODO: PLHlsd
-
 GLOBALS(
   char *destname;
   struct stat top;
@@ -155,8 +153,9 @@
 
         if (!mkdirat(cfd, catch, try->st.st_mode | 0200) || errno == EEXIST)
           if (-1 != (try->extra = openat(cfd, catch, O_NOFOLLOW)))
-            if (!fstat(try->extra, &st2))
-              if (S_ISDIR(st2.st_mode)) return DIRTREE_COMEAGAIN;
+            if (!fstat(try->extra, &st2) && S_ISDIR(st2.st_mode))
+              return DIRTREE_COMEAGAIN
+                     | (DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
 
       // Hardlink
 
@@ -283,8 +282,9 @@
 
     // Skip nonexistent sources
     if (rc) {
-      if (errno != EXDEV ||
-        !(new = dirtree_add_node(0, src, !(toys.optflags & (FLAG_d|FLAG_a)))))
+      int symfollow = toys.optflags & (FLAG_H|FLAG_L);
+
+      if (errno != EXDEV || !(new = dirtree_add_node(0, src, symfollow)))
           perror_msg("bad '%s'", src);
       else dirtree_handle_callback(new, cp_node);
     }