summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2019-02-10 21:39:11 +0100
committerLeah Neukirchen <leah@vuxu.org>2019-02-10 21:39:11 +0100
commitbccacce8162444df385578bef443644254cead5f (patch)
tree82e0fcabdcaaf17b1accd811736b9ac336bfbb21
parent8d09a5339e18735d7492e3fe178edbad6abad3f0 (diff)
downloadmblaze-bccacce8162444df385578bef443644254cead5f.tar.gz
mblaze-bccacce8162444df385578bef443644254cead5f.tar.xz
mblaze-bccacce8162444df385578bef443644254cead5f.zip
mrefile: try using rename first, when possible
-rw-r--r--mdeliver.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/mdeliver.c b/mdeliver.c
index f300222..748127c 100644
--- a/mdeliver.c
+++ b/mdeliver.c
@@ -32,6 +32,7 @@ static char *Xflag;
 char *targetdir;
 long delivery;
 int preserve_mtime;
+int try_rename;
 
 char host[64];
 void
@@ -47,7 +48,7 @@ gethost() {
 }
 
 int
-deliver(FILE *infile)
+deliver(char *infilename, FILE *infile)
 {
 	int outfd;
 	FILE *outfile;
@@ -86,6 +87,16 @@ tryagain:
 
 		snprintf(tmp, sizeof tmp, "%s/tmp/%s", targetdir, id);
 
+		if (try_rename) {
+			snprintf(dst, sizeof dst, "%s/%s/%s:2,%s",
+			    targetdir, cflag ? "cur" : "new", id, Xflag);
+			if (rename(infilename, dst) == 0) {
+				if (vflag)
+					printf("%s\n", dst);
+				return 0;
+			}
+		}
+
 		struct stat st;
 		if (fstat(fileno(infile), &st) < 0)
 			st.st_mode = 0600;
@@ -227,13 +238,13 @@ refile(char *file)
 	else
 		Xflag = "";
 
-	if (deliver(f) < 0) {
+	if (deliver(file, f) < 0) {
 		perror("mrefile");
 		return;
 	}
 
 	fclose(f);
-	if (!kflag)
+	if (!kflag && !try_rename)
 		unlink(file);
 }
 
@@ -245,11 +256,12 @@ main(int argc, char *argv[])
 
 		cflag = 1;  // use cur/
 		preserve_mtime = 1;
+		try_rename = 1;
 
 		int c;
 		while ((c = getopt(argc, argv, "kv")) != -1)
 			switch (c) {
-			case 'k': kflag = 1; break;
+			case 'k': kflag = 1; try_rename = 0; break;
 			case 'v': vflag = 1; break;
 			default:
 usage:
@@ -296,7 +308,7 @@ usage:
 
 	gethost();
 
-	if (deliver(stdin) < 0) {
+	if (deliver(0, stdin) < 0) {
 		perror("mdeliver");
 		return 2;
 	}