Commit 2af4a0419220dba40c25f0f6af548638b5ae3a1b

joshua stein 2019-05-11T16:21:20

got_error_set_errno: take a char arg to pass to got_error_prefix_errno $> got rm a got: : No such file or directory becomes $> got rm a got: /path/to/a: No such file or directory

diff --git a/got/got.c b/got/got.c
index 679e74f..73e05dd 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2020,7 +2020,7 @@ cmd_rm(int argc, char *argv[])
 		goto done;
 
 	error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
-	if (error != NULL)
+	if (error)
 		goto done;
 
 	error = apply_unveil(got_repo_get_path(repo), 1,
diff --git a/include/got_error.h b/include/got_error.h
index 3fd5c96..604b154 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -208,9 +208,10 @@ const struct got_error *got_error_prefix_errno3(const char *, const char *,
 /*
  * Set errno to the specified error code and return a statically
  * allocated error object with code GOT_ERR_ERRNO and an error
- * message obtained from strerror(3).
+ * message obtained from strerror(3), optionally prefixed with a
+ * string.
  */
-const struct got_error *got_error_set_errno(int);
+const struct got_error *got_error_set_errno(int, const char *);
 
 /*
  * If ferror(3) indicates an error status for the FILE, obtain an error
diff --git a/lib/error.c b/lib/error.c
index 4936ab5..1fd38ce 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -111,10 +111,10 @@ got_error_prefix_errno3(const char *prefix, const char *prefix2,
 }
 
 const struct got_error *
-got_error_set_errno(int code)
+got_error_set_errno(int code, const char *prefix)
 {
 	errno = code;
-	return got_error_prefix_errno("");
+	return got_error_prefix_errno(prefix);
 }
 
 const struct got_error *
@@ -167,7 +167,7 @@ got_error_uuid(uint32_t uuid_status)
 	case uuid_s_invalid_string_uuid:
 		return got_error(GOT_ERR_UUID_INVALID);
 	case uuid_s_no_memory:
-		return got_error_set_errno(ENOMEM);
+		return got_error_set_errno(ENOMEM, NULL);
 	default:
 		return got_error(GOT_ERR_UUID);
 	}
diff --git a/lib/worktree.c b/lib/worktree.c
index 1ed6f1e..3ce4537 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1861,7 +1861,7 @@ got_worktree_schedule_add(struct got_worktree *worktree,
 		goto done;
 
 	if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
-		err = got_error_set_errno(EEXIST);
+		err = got_error_set_errno(EEXIST, relpath);
 		goto done;
 	}
 
@@ -1973,7 +1973,7 @@ got_worktree_schedule_delete(struct got_worktree *worktree,
 
 	if (status != GOT_STATUS_NO_CHANGE) {
 		if (status == GOT_STATUS_DELETE) {
-			err = got_error_set_errno(ENOENT);
+			err = got_error_set_errno(ENOENT, ondisk_path);
 			goto done;
 		}
 		if (status != GOT_STATUS_MODIFY) {
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
index 581cd66..e0f866b 100755
--- a/regress/cmdline/rm.sh
+++ b/regress/cmdline/rm.sh
@@ -97,7 +97,6 @@ function test_double_rm {
 	(cd $testroot/wt && got rm beta > /dev/null)
 
 	for fflag in "" "-f"; do
-		echo "got: No such file or directory" > $testroot/stderr.expected
 		(cd $testroot/wt && got rm $fflag beta 2> $testroot/stderr)
 		ret="$?"
 		if [ "$ret" == "0" ]; then
@@ -105,10 +104,11 @@ function test_double_rm {
 			test_done "$testroot" 1
 		fi
 
-		cmp $testroot/stderr.expected $testroot/stderr
+		grep "No such file or directory" $testroot/stderr > \
+			$testroot/stderr.actual
 		ret="$?"
 		if [ "$ret" != "0" ]; then
-			diff -u $testroot/stderr.expected $testroot/stderr
+			cat $testroot/stderr
 			test_done "$testroot" "$ret"
 		fi
 	done
diff --git a/tog/tog.c b/tog/tog.c
index c6e552d..3f82dba 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -535,11 +535,11 @@ view_input(struct tog_view **new, struct tog_view **dead,
 	/* Allow threads to make progress while we are waiting for input. */
 	errcode = pthread_mutex_unlock(&tog_mutex);
 	if (errcode)
-		return got_error_set_errno(errcode);
+		return got_error_set_errno(errcode, "pthread_mutex_unlock");
 	ch = wgetch(view->window);
 	errcode = pthread_mutex_lock(&tog_mutex);
 	if (errcode)
-		return got_error_set_errno(errcode);
+		return got_error_set_errno(errcode, "pthread_mutex_lock");
 	nodelay(stdscr, TRUE);
 
 	if (tog_sigwinch_received) {
@@ -655,7 +655,7 @@ view_loop(struct tog_view *view)
 
 	errcode = pthread_mutex_lock(&tog_mutex);
 	if (errcode)
-		return got_error_set_errno(errcode);
+		return got_error_set_errno(errcode, "pthread_mutex_lock");
 
 	TAILQ_INIT(&views);
 	TAILQ_INSERT_HEAD(&views, view, entry);
@@ -774,7 +774,7 @@ done:
 
 	errcode = pthread_mutex_unlock(&tog_mutex);
 	if (errcode)
-		return got_error_set_errno(errcode);
+		return got_error_set_errno(errcode, "pthread_mutex_unlock");
 
 	return err;
 }
@@ -1096,7 +1096,7 @@ queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
 
 		errcode = pthread_mutex_lock(&tog_mutex);
 		if (errcode) {
-			err = got_error_set_errno(errcode);
+			err = got_error_set_errno(errcode, "pthread_mutex_lock");
 			break;
 		}
 
@@ -1107,7 +1107,8 @@ queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
 
 		errcode = pthread_mutex_unlock(&tog_mutex);
 		if (errcode && err == NULL)
-			err = got_error_set_errno(errcode);
+			err = got_error_set_errno(errcode,
+			    "pthread_mutex_unlock");
 	}
 
 	return err;
@@ -1306,14 +1307,17 @@ trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
 		/* Wake the log thread. */
 		errcode = pthread_cond_signal(need_commits);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_cond_signal");
 		errcode = pthread_mutex_unlock(&tog_mutex);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_mutex_unlock");
 		pthread_yield();
 		errcode = pthread_mutex_lock(&tog_mutex);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_mutex_lock");
 
 		if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
 			/*
@@ -1448,7 +1452,8 @@ log_thread(void *arg)
 
 		errcode = pthread_mutex_lock(&tog_mutex);
 		if (errcode) {
-			err = got_error_set_errno(errcode);
+			err = got_error_set_errno(errcode,
+			    "pthread_mutex_lock");
 			break;
 		} else if (*a->quit)
 			done = 1;
@@ -1464,12 +1469,14 @@ log_thread(void *arg)
 			errcode = pthread_cond_wait(&a->need_commits,
 			    &tog_mutex);
 			if (errcode)
-				err = got_error_set_errno(errcode);
+				err = got_error_set_errno(errcode,
+				    "pthread_cond_wait");
 		}
 
 		errcode = pthread_mutex_unlock(&tog_mutex);
 		if (errcode && err == NULL)
-			err = got_error_set_errno(errcode);
+			err = got_error_set_errno(errcode,
+			    "pthread_mutex_unlock");
 	}
 	a->log_complete = 1;
 	return (void *)err;
@@ -1485,22 +1492,25 @@ stop_log_thread(struct tog_log_view_state *s)
 		s->quit = 1;
 		errcode = pthread_cond_signal(&s->thread_args.need_commits);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_cond_signal");
 		errcode = pthread_mutex_unlock(&tog_mutex);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_mutex_unlock");
 		errcode = pthread_join(s->thread, (void **)&err);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode, "pthread_join");
 		errcode = pthread_mutex_lock(&tog_mutex);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_mutex_lock");
 		s->thread = NULL;
 	}
 
 	errcode = pthread_cond_destroy(&s->thread_args.need_commits);
 	if (errcode && err == NULL)
-		err = got_error_set_errno(errcode);
+		err = got_error_set_errno(errcode, "pthread_cond_destroy");
 
 	if (s->thread_args.repo) {
 		got_repo_close(s->thread_args.repo);
@@ -1571,7 +1581,7 @@ open_log_view(struct tog_view *view, struct got_object_id *start_id,
 
 	errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
 	if (errcode) {
-		err = got_error_set_errno(errcode);
+		err = got_error_set_errno(errcode, "pthread_cond_init");
 		goto done;
 	}
 
@@ -1601,7 +1611,7 @@ show_log_view(struct tog_view *view)
 		int errcode = pthread_create(&s->thread, NULL, log_thread,
 		    &s->thread_args);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode, "pthread_create");
 	}
 
 	return draw_commits(view, &s->last_displayed_entry,
@@ -2685,7 +2695,7 @@ blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
 
 	errcode = pthread_mutex_lock(&tog_mutex);
 	if (errcode)
-		return got_error_set_errno(errcode);
+		return got_error_set_errno(errcode, "pthread_mutex_lock");
 
 	if (*a->quit) {	/* user has quit the blame view */
 		err = got_error(GOT_ERR_ITER_COMPLETED);
@@ -2708,7 +2718,7 @@ blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
 done:
 	errcode = pthread_mutex_unlock(&tog_mutex);
 	if (errcode)
-		err = got_error_set_errno(errcode);
+		err = got_error_set_errno(errcode, "pthread_mutex_unlock");
 	return err;
 }
 
@@ -2725,7 +2735,8 @@ blame_thread(void *arg)
 
 	errcode = pthread_mutex_lock(&tog_mutex);
 	if (errcode)
-		return (void *)got_error_set_errno(errcode);
+		return (void *)got_error_set_errno(errcode,
+		    "pthread_mutex_lock");
 
 	got_repo_close(ta->repo);
 	ta->repo = NULL;
@@ -2733,7 +2744,7 @@ blame_thread(void *arg)
 
 	errcode = pthread_mutex_unlock(&tog_mutex);
 	if (errcode && err == NULL)
-		err = got_error_set_errno(errcode);
+		err = got_error_set_errno(errcode, "pthread_mutex_unlock");
 
 	return (void *)err;
 }
@@ -2761,13 +2772,15 @@ stop_blame(struct tog_blame *blame)
 		int errcode;
 		errcode = pthread_mutex_unlock(&tog_mutex);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_mutex_unlock");
 		errcode = pthread_join(blame->thread, (void **)&err);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode, "pthread_join");
 		errcode = pthread_mutex_lock(&tog_mutex);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode,
+			    "pthread_mutex_lock");
 		if (err && err->code == GOT_ERR_ITER_COMPLETED)
 			err = NULL;
 		blame->thread = NULL;
@@ -2937,7 +2950,7 @@ show_blame_view(struct tog_view *view)
 		errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
 		    &s->blame.thread_args);
 		if (errcode)
-			return got_error_set_errno(errcode);
+			return got_error_set_errno(errcode, "pthread_create");
 	}
 
 	err = draw_blame(view, s->blamed_commit->id, s->blame.f,