Commit bb63914a95fa51c7f5dc16d02b8f4ae2736e2e15

Stefan Sperling 2020-02-17T21:57:56

make tmp dir location a compile-time setting and change gotweb's tmp dir We are not sure whether a gotweb package can own /var/www/tmp on OpenBSD. Moving gotweb's tmp dir to /var/www/got/tmp sidesteps that issue.

diff --git a/README b/README
index 5061408..7efbdf7 100644
--- a/README
+++ b/README
@@ -61,7 +61,7 @@ This will create the following files:
   helper programs from the libexec directory in /var/www/cgi-bin/gotweb/libexec
   several template files in /var/www/cgi-bin/gw_tmpl/
   html, css, and image files in /var/www/htdocs/gotweb/
-  the directory /var/www/tmp/
+  the directory /var/www/got/tmp/
   man pages (only installed if building sources from a Got release tarball)
 
 Documentation is available in manual pages:
diff --git a/got/got.c b/got/got.c
index 4329f78..4a5d5fc 100644
--- a/got/got.c
+++ b/got/got.c
@@ -297,8 +297,8 @@ apply_unveil(const char *repo_path, int repo_read_only,
 	if (worktree_path && unveil(worktree_path, "rwc") != 0)
 		return got_error_from_errno2("unveil", worktree_path);
 
-	if (unveil("/tmp", "rwc") != 0)
-		return got_error_from_errno2("unveil", "/tmp");
+	if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
+		return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
 
 	err = got_privsep_unveil_exec_helpers();
 	if (err != NULL)
@@ -482,7 +482,8 @@ collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
 	    branch_name) == -1)
 		return got_error_from_errno("asprintf");
 
-	err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
+	err = got_opentemp_named_fd(logmsg_path, &fd,
+	    GOT_TMPDIR_STR "/got-importmsg");
 	if (err)
 		goto done;
 
@@ -3789,7 +3790,7 @@ get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
 	char *editor = NULL;
 	int fd = -1;
 
-	if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
+	if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto done;
 	}
@@ -5802,7 +5803,8 @@ histedit_edit_logmsg(struct got_histedit_list_entry *hle,
 	if (err)
 		goto done;
 
-	err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
+	err = got_opentemp_named_fd(&logmsg_path, &fd,
+	    GOT_TMPDIR_STR "/got-logmsg");
 	if (err)
 		goto done;
 
diff --git a/gotweb/Makefile.inc b/gotweb/Makefile.inc
index 445d054..b892a27 100644
--- a/gotweb/Makefile.inc
+++ b/gotweb/Makefile.inc
@@ -7,10 +7,13 @@ LIBEXECDIR =	${GOTWEB_DIR}/libexec
 LIBEXEC_DIR =	${CHROOT_DIR}${LIBEXECDIR}
 ETC_DIR =	${CHROOT_DIR}/etc
 HTTPD_DIR =	${CHROOT_DIR}/htdocs
-TMP_DIR =	${CHROOT_DIR}/tmp
+GOTWEB_TMP_DIR ?=	/got/tmp
+TMP_DIR =	${CHROOT_DIR}${GOTWEB_TMP_DIR}
 PROG_DIR =	${HTTPD_DIR}/${PROG}
 CGI_DIR =	${CHROOT_DIR}${GOTWEB_DIR}
 TMPL_DIR =	${CGI_DIR}/gw_tmpl
 PUB_REPOS_DIR =	${CHROOT_DIR}/got/public
 WWWUSR ?=	www
 WWWGRP ?=	www
+
+CPPFLAGS +=	-DGOT_TMPDIR=${GOTWEB_TMP_DIR}
diff --git a/gotweb/gotweb.8 b/gotweb/gotweb.8
index 1b2ecef..d8224f9 100644
--- a/gotweb/gotweb.8
+++ b/gotweb/gotweb.8
@@ -117,7 +117,7 @@ to read Git repositories.
 .It Pa /var/www/htdocs/gotweb/
 Directory containing HTML, CSS, and image files used by
 .Nm .
-.It Pa /var/www/tmp/
+.It Pa /var/www/got/tmp/
 Directory for temporary files created by
 .Nm .
 .El
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index fbf1ba3..509390f 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -296,8 +296,8 @@ gw_apply_unveil(const char *repo_path)
 	if (repo_path && unveil(repo_path, "r") != 0)
 		return got_error_from_errno2("unveil", repo_path);
 
-	if (unveil("/tmp", "rwc") != 0)
-		return got_error_from_errno2("unveil", "/tmp");
+	if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
+		return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
 
 	err = got_privsep_unveil_exec_helpers();
 	if (err != NULL)
diff --git a/include/got_opentemp.h b/include/got_opentemp.h
index 86beac8..82d4fbd 100644
--- a/include/got_opentemp.h
+++ b/include/got_opentemp.h
@@ -16,6 +16,13 @@
 
 /* Utilities for opening temporary files. */
 
+#ifndef GOT_TMPDIR
+#define GOT_TMPDIR /tmp
+#endif
+#define GOT_STRINGIFY_TMP(x) #x
+#define GOT_STRINGVAL_TMP(x) GOT_STRINGIFY_TMP(x)
+#define GOT_TMPDIR_STR GOT_STRINGVAL_TMP(GOT_TMPDIR)
+
 /* Open a file descriptor to a new temporary file for writing.
  * The file is not visible in the filesystem. */
 int got_opentempfd(void);
diff --git a/include/got_version.h b/include/got_version.h
index eacaf1a..e8abac2 100644
--- a/include/got_version.h
+++ b/include/got_version.h
@@ -18,10 +18,10 @@
 #error "GOT_VERSION is undefined"
 #endif
 
-#define GOT_STRINGIFY(x) #x
-#define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
+#define GOT_STRINGIFY_VERSION(x) #x
+#define GOT_STRINGVAL_VERSION(x) GOT_STRINGIFY_VERSION(x)
 
-#define GOT_VERSION_STR GOT_STRINGVAL(GOT_VERSION)
+#define GOT_VERSION_STR GOT_STRINGVAL_VERSION(GOT_VERSION)
 
 static inline void
 got_version_print_str(void)
diff --git a/lib/diff3.c b/lib/diff3.c
index 9d0709e..f17c676 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -219,7 +219,8 @@ diffreg(BUF **d, const char *path1, const char *path2)
 		goto done;
 	}
 
-	err = got_opentemp_named(&outpath, &outfile, "/tmp/got-diffreg");
+	err = got_opentemp_named(&outpath, &outfile,
+	    GOT_TMPDIR_STR "/got-diffreg");
 	if (err)
 		goto done;
 
@@ -305,15 +306,15 @@ got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2,
 	if (err)
 		goto out;
 
-	if (asprintf(&path1, "/tmp/got-diff1.XXXXXXXX") == -1) {
+	if (asprintf(&path1, GOT_TMPDIR_STR "/got-diff1.XXXXXXXX") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto out;
 	}
-	if (asprintf(&path2, "/tmp/got-diff2.XXXXXXXX") == -1) {
+	if (asprintf(&path2, GOT_TMPDIR_STR "/got-diff2.XXXXXXXX") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto out;
 	}
-	if (asprintf(&path3, "/tmp/got-diff3.XXXXXXXX") == -1) {
+	if (asprintf(&path3, GOT_TMPDIR_STR "/got-diff3.XXXXXXXX") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto out;
 	}
@@ -345,7 +346,7 @@ got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2,
 		goto out;
 	}
 
-	if (asprintf(&dp13, "/tmp/got-d13.XXXXXXXXXX") == -1) {
+	if (asprintf(&dp13, GOT_TMPDIR_STR "/got-d13.XXXXXXXXXX") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto out;
 	}
@@ -356,7 +357,7 @@ got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2,
 	buf_free(d1);
 	d1 = NULL;
 
-	if (asprintf(&dp23, "/tmp/got-d23.XXXXXXXXXX") == -1) {
+	if (asprintf(&dp23, GOT_TMPDIR_STR "/got-d23.XXXXXXXXXX") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto out;
 	}
diff --git a/lib/opentemp.c b/lib/opentemp.c
index 8476310..9bc6f86 100644
--- a/lib/opentemp.c
+++ b/lib/opentemp.c
@@ -29,7 +29,8 @@ got_opentempfd(void)
 	char name[PATH_MAX];
 	int fd;
 
-	if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
+	if (strlcpy(name, GOT_TMPDIR_STR "/got.XXXXXXXX", sizeof(name))
+	    >= sizeof(name))
 		return -1;
 
 	fd = mkstemp(name);
diff --git a/regress/delta/delta_test.c b/regress/delta/delta_test.c
index 5f086a5..0ee8340 100644
--- a/regress/delta/delta_test.c
+++ b/regress/delta/delta_test.c
@@ -137,7 +137,7 @@ main(int argc, const char *argv[])
 	if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
 		err(1, "pledge");
 #endif
-	if (unveil("/tmp", "rwc") != 0)
+	if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
 		err(1, "unveil");
 
 	if (unveil(NULL, NULL) != 0)
diff --git a/tog/tog.c b/tog/tog.c
index ca4cead..e6738ac 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2466,8 +2466,8 @@ apply_unveil(const char *repo_path, const char *worktree_path)
 	if (worktree_path && unveil(worktree_path, "rwc") != 0)
 		return got_error_from_errno2("unveil", worktree_path);
 
-	if (unveil("/tmp", "rwc") != 0)
-		return got_error_from_errno2("unveil", "/tmp");
+	if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
+		return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
 
 	error = got_privsep_unveil_exec_helpers();
 	if (error != NULL)