shithub: clone

Download patch

ref: 2c1e39a34e71f4b8e5c7d916ef4caf212319de6f
parent: 77d6351a86a29a70aeb1d67e34a657cc0e677dd3
author: kvik <kvik@a-b.xyz>
date: Tue Oct 30 23:10:43 EDT 2018

properly handle cloning multiple files / dirs to a new directory

if there's multiple source arguments, always create the destination
directory and clone sources into the new directory.

--- a/clone.c
+++ b/clone.c
@@ -24,6 +24,7 @@
 	vlong offset;
 } Blk;
 
+int multisrc = 0;
 int keepmode = 0;
 int keepmtime = 0;
 int keepuser = 0;
@@ -189,15 +190,18 @@
 	File *f;
 	
 	sd = dirstat(src);
-	if(sd == nil)
-		sysfatal("can't stat");
+	if(sd == nil){
+		fprint(2, "clone: can't stat: %r\n");
+		return;
+	}
 	dd = nil;
 	if(access(dst, AEXIST) >= 0){
 		dd = dirstat(dst);
 		if(dd == nil)
 			sysfatal("can't stat");
-	}
-	
+	}else if(multisrc)
+		dd = skipdir = mkdir(dst, sd, 1);
+
 	/* clone a file */
 	if(!(sd->mode & DMDIR)){
 		if(dd && dd->mode & DMDIR)
@@ -210,7 +214,10 @@
 	/* clone a directory */
 	if(dd)
 		dst = smprint("%s/%s", dst, filename(src));
-	skipdir = mkdir(dst, sd, 1);
+	if(skipdir)
+		mkdir(dst, sd, 0);
+	else
+		skipdir = mkdir(dst, sd, 1);
 	clonedir(src, dst);
 }
 
@@ -391,6 +398,8 @@
 	}ARGEND;
 	if(argc < 2)
 		usage();
+	if(argc > 2)
+		multisrc = 1;
 	dst = argv[argc - 1];
 
 	filechan = chancreate(sizeof(File*), fileprocs);