add test for -D
fix b/c/d/FILE not copying into a/ with -D option

github.com/landley/toybox/issues/165
---
 tests/cp.test   | 7 +++++++
 toys/posix/cp.c | 7 +++++--
 2 files changed, 12 insertions(+), 2 deletions(-)
From d906c5388c9f645f6ecfb4b0248d7efd3d6953b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jarno=20M=C3=A4kip=C3=A4=C3=A4?= <[email protected]>
Date: Sat, 15 Feb 2020 13:31:48 +0200
Subject: [PATCH] cp: fix -D (--parents)

add test for -D
fix b/c/d/FILE not copying into a/ with -D option

github.com/landley/toybox/issues/165
---
 tests/cp.test   | 7 +++++++
 toys/posix/cp.c | 7 +++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/tests/cp.test b/tests/cp.test
index dfb80ea1..5c4a7474 100755
--- a/tests/cp.test
+++ b/tests/cp.test
@@ -120,6 +120,13 @@ testing "-T file" "cp -T b file && cat file" "b\n" "" ""
 testing "-T dir" "cp -T b dir 2>/dev/null || echo expected" "expected\n" "" ""
 rm b file
 
+mkdir -p b/c/d/
+mkdir a/
+echo a > b/c/d/file
+testing "-D b/c/d/file a/" "cp -D b/c/d/file a/ && cat a/b/c/d/file" "a\n" "" ""
+rm -rf a/
+rm -rf b/
+
 # cp -r ../source destdir
 # cp -r one/two/three missing
 # cp -r one/two/three two
diff --git a/toys/posix/cp.c b/toys/posix/cp.c
index 13bfd7ef..d6b3e973 100644
--- a/toys/posix/cp.c
+++ b/toys/posix/cp.c
@@ -415,10 +415,10 @@ void cp_main(void)
     if (*--trail == '/') *trail = 0;
 
     if (destdir) {
-      char *s = FLAG(D) ? dirname(src) : getbasename(src);
+      char *s = strdup(src);
 
-      TT.destname = xmprintf("%s/%s", destname, s);
       if (FLAG(D)) {
+        TT.destname = xmprintf("%s/%s", destname, s);
         if (!(s = fileunderdir(TT.destname, destname))) {
           error_msg("%s not under %s", TT.destname, destname);
           continue;
@@ -426,6 +426,9 @@ void cp_main(void)
         // TODO: .. follows abspath, not links...
         free(s);
         mkpath(TT.destname);
+      } else {
+        s = getbasename(s);
+        TT.destname = xmprintf("%s/%s", destname, s);
       }
     } else TT.destname = destname;
 
-- 
2.11.0

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to