about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPulux <pulux@pf4sh.eu>2022-08-05 05:52:25 +0200
committerLeah Neukirchen <leah@vuxu.org>2022-09-25 19:05:37 +0200
commit83029dff5de737fb4d6de9dbe64d39ce494eade7 (patch)
tree90ba742cf0e04afb2010af929219dd3cfe2c562d
parent5bb7de35cac53bbc1b666e3cb38cb095fa7ef713 (diff)
downloadnq-83029dff5de737fb4d6de9dbe64d39ce494eade7.tar.gz
nq-83029dff5de737fb4d6de9dbe64d39ce494eade7.tar.xz
nq-83029dff5de737fb4d6de9dbe64d39ce494eade7.zip
nq: add support for a $NQFAILDIR
-rw-r--r--nq.19
-rw-r--r--nq.c22
2 files changed, 30 insertions, 1 deletions
diff --git a/nq.1 b/nq.1
index f6905ee..ff09dc6 100644
--- a/nq.1
+++ b/nq.1
@@ -71,6 +71,7 @@ listed job ids are done.
 .El
 .Sh ENVIRONMENT
 .Bl -hang -width "NQDONEDIR"
+.Bl -hang -width "NQFAILDIR"
 .It Ev NQDIR
 Directory where lock files/job output resides.
 Each
@@ -93,6 +94,14 @@ to after successful execution of the job.
 Ignored when
 .Fl c
 is used.
+.It Ev NQFAILDIR
+When set, specifies a directory
+.Po
+must be on the same file system as
+.Ev NQDIR
+.Pc
+where lock files/job output is moved
+to after unsuccessful execution of the job.
 .It Ev NQJOBID
 The job id of the currently running job,
 exposed to the job itself.
diff --git a/nq.c b/nq.c
index 564782d..1aff8f9 100644
--- a/nq.c
+++ b/nq.c
@@ -123,7 +123,7 @@ int
 main(int argc, char *argv[])
 {
 	int64_t ms;
-	int dirfd = -1, donedirfd = -1, lockfd = -1;
+	int dirfd = -1, donedirfd = -1, faildirfd = -1, lockfd = -1;
 	int opt = 0, cflag = 0, qflag = 0, tflag = 0, wflag = 0;
 	int pipefd[2];
 	char lockfile[64], newestlocked[64];
@@ -195,6 +195,22 @@ usage:
 		}
 	}
 
+	char *failpath = getenv("NQFAILDIR");
+	if (failpath) {
+		if (mkdir(failpath, 0777) < 0) {
+			if (errno != EEXIST) {
+				perror("mkdir $NQFAILDIR");
+				exit(111);
+			}
+		}
+
+		faildirfd = open(failpath, O_RDONLY | O_DIRECTORY);
+		if (faildirfd < 0) {
+			perror("dir open");
+			exit(111);
+		}
+	}
+
 	if (tflag || wflag) {
 		snprintf(lockfile, sizeof lockfile,
 		    ".,%011" PRIx64 ".%d", ms, getpid());
@@ -263,6 +279,10 @@ usage:
 				else if (donepath)
 					renameat(dirfd, lockfile,
 					    donedirfd, lockfile);
+			}
+			if (WEXITSTATUS(status) != 0) {
+				if (failpath)
+					renameat(dirfd, lockfile, faildirfd, lockfile);
 				/* errors above are ignored */
 			}
 		} else {