shithub: clone

Download patch

ref: 91b8745cbe7660883d512157dc982a1b31e9f7e5
parent: c52095c4da2055ab62a481e8be4c15919d081ef3
author: kvik <kvik@a-b.xyz>
date: Tue Mar 12 19:12:20 EDT 2019

implement -T flag that disables the safe-mode

--- a/clone.c
+++ b/clone.c
@@ -35,6 +35,7 @@
 int keepmtime = 0;
 int keepuser = 0;
 int keepgroup = 0;
+int notemp = 0;
 int blksz = Blksz;
 int fileprocs = Nfileprocs;
 int blkprocs = Nblkprocs;
@@ -70,7 +71,7 @@
 void
 usage(void)
 {
-	fprint(2, "usage: %s [-gux] [-b blocksize] [-p fileprocs:blockprocs] from ... to\n", argv0);
+	fprint(2, "usage: %s [-guxT] [-b blocksize] [-p fileprocs:blockprocs] from ... to\n", argv0);
 	exits("usage");
 }
 
@@ -472,7 +473,7 @@
 void
 fileproc(void *v)
 {
-	char *tmp;
+	char *dst;
 	Dir d;
 	File *f;
 	WaitGroup *wg;
@@ -479,7 +480,7 @@
 	
 	threadsetname("fileproc");
 	
-	tmp = nil;
+	dst = nil;
 	wg = v;
 	for(;;){
 		f = recvp(filechan);
@@ -491,18 +492,23 @@
 			error("can't open: %r");
 			goto End;
 		}
-		tmp = smprint("%s.clone.%ld", f->dst, salt);
-		f->dfd = create(tmp, OWRITE, f->mode);
+		if(notemp)
+			dst = estrdup(f->dst);
+		else
+			dst = smprint("%s.clone.%ld", f->dst, salt);
+		f->dfd = create(dst, OWRITE, f->mode);
 		if(f->dfd < 0){
 			error("can't create: %r");
 			goto End;
 		}
 		if(clonefile(f) < 0){
-			if(remove(tmp) < 0)
+			if(remove(dst) < 0)
 				error("can't remove: %r");
 			goto End;
 		}
 		cloneattr(f->dfd, f);
+		if(notemp)
+			goto End;
 		if(dirstat(f->dst) != nil && remove(f->dst) < 0){
 			error("can't remove: %r");
 			goto End;
@@ -516,7 +522,7 @@
 		
 End:
 		filefree(f);
-		free(tmp);
+		free(dst);
 	}
 	wgdone(wg);
 }
@@ -546,6 +552,9 @@
 		break;
 	case 'g':
 		keepgroup = 1;
+		break;
+	case 'T':
+		notemp = 1;
 		break;
 	}ARGEND;
 	if(argc < 2)
--- a/clone.man
+++ b/clone.man
@@ -4,7 +4,7 @@
 .SH SYNOPSYS
 .B clone
 [
-.B -gux
+.B -guxT
 ]
 [
 .B -b
@@ -80,6 +80,13 @@
 being the number of processes that do the
 input / output (defaults to 4:16).
 .PP
+The
+.I -T
+option disables the default safe-mode behaviour
+where a file is fully copied into a temporary file
+in the target directory, the target file is removed,
+and, finally, the temporary file is renamed.
+.PP
 .SH SOURCE
 .B https://bitbucket.org/k-vik/clone
 .SH SEE ALSO
@@ -96,3 +103,12 @@
 time. Furthermore, all directories get the
 forced write permission, since not having
 one would mean no new file creation.
+.PP
+Safe-mode has to be disabled with the
+.I -T
+flag when copying files to Windows drawterm's
+.I /mnt/term;
+otherwise the Windows file system semantics
+prevent
+.IR clone (1)
+from renaming an already open file.