changeset 271:7d625cbdde25

Update cp -r to work better, add relevant tests to test suite.
author Rob Landley <rob@landley.net>
date Tue, 25 Mar 2008 17:09:40 -0500
parents e9f75ffd3200
children a5652aa22f38
files scripts/test/cp.test toys/cp.c
diffstat 2 files changed, 27 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/test/cp.test	Mon Mar 24 05:34:58 2008 -0500
+++ b/scripts/test/cp.test	Tue Mar 25 17:09:40 2008 -0500
@@ -72,7 +72,24 @@
 rm one two random
 rm -rf dir
 
+mkdir -p one/two/three/four
+touch one/two/three/five
+touch one/{six,seven,eight}
+testing "cp -r /abspath dest" \
+	"cp -r \"$(readlink -f one)\" dir && diff -r one dir && echo yes" \
+	"yes\n" "" ""
+mkdir dir2
+testing "cp -r dir1/* dir2" \
+	"cp -r one/* dir2 && diff -r one dir2 && echo yes" "yes\n" "" ""
+rm -rf one dir2
+
 # cp -r ../source destdir
-
+# cp -r one/two/three missing
+# cp -r one/two/three two
+# mkdir one; touch one/two; ln -s two one/three
 # cp file1 file2 dir
 # cp file1 missing file2 -> dir
+
+# Make sure it's truncating existing file
+# copy with -d at top level, with -d in directory, without -d at top level,
+#      without -d in directory
--- a/toys/cp.c	Mon Mar 24 05:34:58 2008 -0500
+++ b/toys/cp.c	Tue Mar 25 17:09:40 2008 -0500
@@ -123,7 +123,9 @@
 
 	// Find appropriate chunk of path for destination.
 
-	for (n = node;;n = n->parent) {
+	n = node;
+	if (!TT.destisdir) n = n->parent;
+	for (;;n = n->parent) {
 		while (s!=path) {
 			if (*(--s)=='/') break;
 		}
@@ -180,13 +182,14 @@
 			continue;
 		}
 
-		dst = strrchr(src, '/');
-		if (dst) dst++;
-		else dst=src;
-
 		// Copy directory or file.
 
-		if (TT.destisdir) dst = xmsprintf("%s/%s", TT.destname, dst);
+		if (TT.destisdir) {
+			dst = strrchr(src, '/');
+			if (dst) dst++;
+			else dst=src;
+			dst = xmsprintf("%s/%s", TT.destname, dst);
+		} else dst = TT.destname;
 		if (S_ISDIR(st.st_mode)) {
 			if (toys.optflags & FLAG_r) {
 				cp_file(src, dst, &st);