about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2021-03-13 16:38:51 +0100
committerLeah Neukirchen <leah@vuxu.org>2021-03-13 16:41:53 +0100
commita377c0550bb92b3d03a54b2f6edf3248bf80d61a (patch)
treee3e770af058461b9ea023ed4775bbef9416ca0af
parent4f92a29b943e5d7906b884009758627bc7c3376b (diff)
downloadnq-a377c0550bb92b3d03a54b2f6edf3248bf80d61a.tar.gz
nq-a377c0550bb92b3d03a54b2f6edf3248bf80d61a.tar.xz
nq-a377c0550bb92b3d03a54b2f6edf3248bf80d61a.zip
nq: add support for a $NQDONEDIR
-rw-r--r--nq.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/nq.c b/nq.c
index 8043fe5..5feb7b7 100644
--- a/nq.c
+++ b/nq.c
@@ -107,7 +107,7 @@ int
 main(int argc, char *argv[])
 {
 	int64_t ms;
-	int dirfd = 0, lockfd = 0;
+	int dirfd = 0, donedirfd = 0, lockfd = 0;
 	int opt = 0, cflag = 0, qflag = 0, tflag = 0, wflag = 0;
 	int pipefd[2];
 	char lockfile[64], newestlocked[64];
@@ -152,7 +152,7 @@ usage:
 
 	if (mkdir(path, 0777) < 0) {
 		if (errno != EEXIST) {
-			perror("mkdir");
+			perror("mkdir $NQDIR");
 			exit(111);
 		}
 	}
@@ -167,6 +167,26 @@ usage:
 		exit(111);
 	}
 
+	char *donepath = getenv("NQDONEDIR");
+	if (donepath) {
+		if (mkdir(path, 0777) < 0) {
+			if (errno != EEXIST) {
+				perror("mkdir $NQDONEDIR");
+				exit(111);
+			}
+		}
+
+#ifdef O_DIRECTORY
+		donedirfd = open(donepath, O_RDONLY | O_DIRECTORY);
+#else
+		donedirfd = open(donepath, O_RDONLY);
+#endif
+		if (donedirfd < 0) {
+			perror("dir open");
+			exit(111);
+		}
+	}
+
 	if (tflag || wflag) {
 		snprintf(lockfile, sizeof lockfile,
 		    ".,%011" PRIx64 ".%d", ms, getpid());
@@ -229,8 +249,14 @@ usage:
 		if (WIFEXITED(status)) {
 			dprintf(lockfd, "\n[exited with status %d.]\n",
 			    WEXITSTATUS(status));
-			if (cflag && WEXITSTATUS(status) == 0)
-				unlinkat(dirfd, lockfile, 0);
+			if (WEXITSTATUS(status) == 0) {
+				if (cflag)
+					unlinkat(dirfd, lockfile, 0);
+				else if (donepath)
+					renameat(dirfd, lockfile,
+					    donedirfd, lockfile);
+				/* errors above are ignored */
+			}
 		} else {
 			dprintf(lockfd, "\n[killed by signal %d.]\n",
 			    WTERMSIG(status));