Commit df056ada35bdbbcc6e248b7ec0ec8a7d25fd5147

Stefan Sperling 2019-05-15T07:27:06

introduce got_error_path()

diff --git a/got/got.c b/got/got.c
index 30aca9d..46b3ebb 100644
--- a/got/got.c
+++ b/got/got.c
@@ -226,7 +226,6 @@ apply_unveil(const char *repo_path, int repo_read_only,
     const char *worktree_path, int create_worktree)
 {
 	const struct got_error *err;
-	static char err_msg[MAXPATHLEN + 36];
 
 	if (create_worktree) {
 		/* Pre-create work tree path to avoid unveiling its parents. */
@@ -237,11 +236,8 @@ apply_unveil(const char *repo_path, int repo_read_only,
 				errno = 0;
 				err = NULL;
 			} else {
-				snprintf(err_msg, sizeof(err_msg),
-				    "%s: directory exists and is not empty",
-				    worktree_path);
-				err = got_error_msg(GOT_ERR_DIR_NOT_EMPTY,
-				    err_msg);
+				err = got_error_path(worktree_path,
+				    GOT_ERR_DIR_NOT_EMPTY);
 			}
 		}
 
diff --git a/include/got_error.h b/include/got_error.h
index 5672559..b5b08b3 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -247,3 +247,6 @@ const struct got_error *got_error_not_ref(const char *);
 
 /* Return an error based on a uuid(3) status code. */
 const struct got_error *got_error_uuid(uint32_t);
+
+/* Return an error with a path prefixed to the error message. */
+const struct got_error *got_error_path(const char *, int);
diff --git a/lib/error.c b/lib/error.c
index c3af78d..bb7df07 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -172,3 +172,23 @@ got_error_uuid(uint32_t uuid_status)
 		return got_error(GOT_ERR_UUID);
 	}
 }
+
+const struct got_error *
+got_error_path(const char *path, int code)
+{
+	static struct got_error err;
+	static char msg[MAXPATHLEN + 128];
+	int i;
+
+	for (i = 0; i < nitems(got_errors); i++) {
+		if (code == got_errors[i].code) {
+			err.code = code;
+			snprintf(msg, sizeof(msg), "%s: %s", path,
+			    got_errors[i].msg);
+			err.msg = msg;
+			return &err;
+		}
+	}
+
+	abort();
+}