Commit 48b8b0ebe312124b17b290c38f2bbd0b5fbc1338

joshua stein 2019-05-11T10:40:57

error: introduce got_error_prefix_errno for better errno messages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
diff --git a/include/got_error.h b/include/got_error.h
index 785766a..afb23f6 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -190,6 +190,13 @@ const struct got_error *got_error_msg(int, const char *);
 const struct got_error *got_error_from_errno(void);
 
 /*
+ * Get a statically allocated error object with code GOT_ERR_ERRNO
+ * and an error message obtained from strerror(3), prefixed with a
+ * string.
+ */
+const struct got_error *got_error_prefix_errno(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).
diff --git a/lib/error.c b/lib/error.c
index 8ac3f42..bb4d6fe 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -23,6 +23,7 @@
 #include <sha1.h>
 #include <zlib.h>
 #include <uuid.h>
+#include <sys/param.h>
 
 #include "got_error.h"
 #include "got_object.h"
@@ -77,6 +78,20 @@ got_error_from_errno(void)
 }
 
 const struct got_error *
+got_error_prefix_errno(const char *prefix)
+{
+	static struct got_error err;
+	static char err_msg[MAXPATHLEN + 20];
+
+	snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
+	    strerror(errno));
+
+	err.code = GOT_ERR_ERRNO;
+	err.msg = err_msg;
+	return &err;
+}
+
+const struct got_error *
 got_error_set_errno(int code)
 {
 	errno = code;
diff --git a/lib/object.c b/lib/object.c
index 8356628..8c71dc8 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -118,7 +118,7 @@ got_object_get_path(char **path, struct got_object_id *id,
 	*path = NULL;
 
 	if (path_objects == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("got_repo_get_path_objects");
 
 	err = got_object_id_str(&hex, id);
 	if (err)
@@ -126,7 +126,7 @@ got_object_get_path(char **path, struct got_object_id *id,
 
 	if (asprintf(path, "%s/%.2x/%s", path_objects,
 	    id->sha1[0], hex + 2) == -1)
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 
 done:
 	free(hex);
@@ -146,7 +146,7 @@ open_loose_object(int *fd, struct got_object_id *id,
 		return err;
 	*fd = open(path, O_RDONLY | O_NOFOLLOW);
 	if (*fd == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno(path);
 		goto done;
 	}
 done:
@@ -166,7 +166,7 @@ get_packfile_path(char **path_packfile, struct got_packidx *packidx)
 
 	*path_packfile = malloc(size);
 	if (*path_packfile == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("malloc");
 
 	/* Copy up to and excluding ".idx". */
 	if (strlcpy(*path_packfile, packidx->path_packidx,
@@ -183,19 +183,16 @@ static void
 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
 {
 	if (close(imsg_fds[0]) != 0) {
-		fprintf(stderr, "%s: %s\n", getprogname(),
-		    strerror(errno));
+		fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
 		_exit(1);
 	}
 
 	if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
-		fprintf(stderr, "%s: %s\n", getprogname(),
-		    strerror(errno));
+		fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
 		_exit(1);
 	}
 	if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
-		fprintf(stderr, "%s: %s\n", getprogname(),
-		    strerror(errno));
+		fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
 		_exit(1);
 	}
 
@@ -223,7 +220,7 @@ request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
 
 	(*obj)->path_packfile = strdup(pack->path_packfile);
 	if ((*obj)->path_packfile == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("strdup");
 		return err;
 	}
 	memcpy(&(*obj)->id, id, sizeof((*obj)->id));
@@ -241,23 +238,23 @@ start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
 
 	ibuf = calloc(1, sizeof(*ibuf));
 	if (ibuf == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
 	if (pack->privsep_child == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("calloc");
 		free(ibuf);
 		return err;
 	}
 
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("socketpair");
 		goto done;
 	}
 
 	pid = fork();
 	if (pid == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("fork");
 		goto done;
 	} else if (pid == 0) {
 		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
@@ -371,14 +368,14 @@ read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
 
 	ibuf = calloc(1, sizeof(*ibuf));
 	if (ibuf == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("socketpair");
 
 	pid = fork();
 	if (pid == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("fork");
 	else if (pid == 0) {
 		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
 		    repo->path);
@@ -428,7 +425,7 @@ got_object_open(struct got_object **obj, struct got_repository *repo,
 		if (errno == ENOENT)
 			err = got_error_no_obj(id);
 		else
-			err = got_error_from_errno();
+			err = got_error_prefix_errno(path);
 		goto done;
 	} else {
 		err = read_object_header_privsep(obj, repo, fd);
@@ -471,7 +468,7 @@ got_object_resolve_id_str(struct got_object_id **id,
 	*id = got_object_id_dup(got_object_get_id(obj));
 	got_object_close(obj);
 	if (*id == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("got_object_id_dup");
 
 	return NULL;
 }
@@ -536,14 +533,14 @@ read_commit_privsep(struct got_commit_object **commit, int obj_fd,
 
 	ibuf = calloc(1, sizeof(*ibuf));
 	if (ibuf == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("socketpair");
 
 	pid = fork();
 	if (pid == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("fork");
 	else if (pid == 0) {
 		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
 		    repo->path);
@@ -641,11 +638,11 @@ got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
 
 	*qid = calloc(1, sizeof(**qid));
 	if (*qid == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	(*qid)->id = got_object_id_dup(id);
 	if ((*qid)->id == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("got_object_id_dup");
 		got_object_qid_free(*qid);
 		*qid = NULL;
 		return err;
@@ -714,14 +711,14 @@ read_tree_privsep(struct got_tree_object **tree, int obj_fd,
 
 	ibuf = calloc(1, sizeof(*ibuf));
 	if (ibuf == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("socketpair");
 
 	pid = fork();
 	if (pid == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("fork");
 	else if (pid == 0) {
 		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
 		    repo->path);
@@ -829,14 +826,14 @@ request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
 
 	basefd = got_opentempfd();
 	if (basefd == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("got_opentempfd");
 	accumfd = got_opentempfd();
 	if (accumfd == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("got_opentempfd");
 
 	outfd_child = dup(outfd);
 	if (outfd_child == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("dup");
 
 	err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
 	if (err)
@@ -868,7 +865,7 @@ request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
 		return err;
 
 	if (lseek(outfd, SEEK_SET, 0) == -1)
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("lseek");
 
 	return err;
 }
@@ -899,7 +896,7 @@ request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
 
 	outfd_child = dup(outfd);
 	if (outfd_child == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("dup");
 
 	err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
 	if (err)
@@ -914,7 +911,7 @@ request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
 		return err;
 
 	if (lseek(outfd, SEEK_SET, 0) == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("lseek");
 
 	return err;
 }
@@ -934,14 +931,14 @@ read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
 
 	ibuf = calloc(1, sizeof(*ibuf));
 	if (ibuf == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("socketpair");
 
 	pid = fork();
 	if (pid == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("fork");
 	else if (pid == 0) {
 		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
 		    repo->path);
@@ -974,15 +971,15 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
 
 	*blob = calloc(1, sizeof(**blob));
 	if (*blob == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	outfd = got_opentempfd();
 	if (outfd == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("got_opentempfd");
 
 	(*blob)->read_buf = malloc(blocksize);
 	if ((*blob)->read_buf == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("malloc");
 		goto done;
 	}
 
@@ -1026,14 +1023,14 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
 		outfd = -1;
 		(*blob)->f = fmemopen(outbuf, size, "rb");
 		if ((*blob)->f == NULL) {
-			err = got_error_from_errno();
+			err = got_error_prefix_errno("fmemopen");
 			free(outbuf);
 			goto done;
 		}
 		(*blob)->data = outbuf;
 	} else {
 		if (fstat(outfd, &sb) == -1) {
-			err = got_error_from_errno();
+			err = got_error_prefix_errno("fstat");
 			goto done;
 		}
 
@@ -1044,7 +1041,7 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
 
 		(*blob)->f = fdopen(outfd, "rb");
 		if ((*blob)->f == NULL) {
-			err = got_error_from_errno();
+			err = got_error_prefix_errno("fdopen");
 			close(outfd);
 			outfd = -1;
 			goto done;
@@ -1161,7 +1158,7 @@ got_object_blob_dump_to_file(size_t *total_len, int *nlines,
 	} while (len != 0);
 
 	if (fflush(outfile) != 0)
-		return got_error_from_errno();
+		return got_error_prefix_errno("fflush");
 	rewind(outfile);
 
 	return NULL;
@@ -1227,14 +1224,14 @@ read_tag_privsep(struct got_tag_object **tag, int obj_fd,
 
 	ibuf = calloc(1, sizeof(*ibuf));
 	if (ibuf == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("socketpair");
 
 	pid = fork();
 	if (pid == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("fork");
 	else if (pid == 0) {
 		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
 		    repo->path);
@@ -1385,7 +1382,7 @@ got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
 	if (path[1] == '\0') {
 		*id = got_object_id_dup(commit->tree_id);
 		if (*id == NULL)
-			err = got_error_from_errno();
+			err = got_error_prefix_errno("got_object_id_dup");
 		goto done;
 	}
 
@@ -1433,7 +1430,7 @@ got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
 	if (te) {
 		*id = got_object_id_dup(te->id);
 		if (*id == NULL)
-			return got_error_from_errno();
+			return got_error_prefix_errno("got_object_id_dup");
 	} else
 		err = got_error(GOT_ERR_NO_TREE_ENTRY);
 done:
@@ -1547,18 +1544,18 @@ got_object_tree_entry_dup(struct got_tree_entry **new_te,
 
 	*new_te = calloc(1, sizeof(**new_te));
 	if (*new_te == NULL)
-		return got_error_from_errno();
+		return got_error_prefix_errno("calloc");
 
 	(*new_te)->mode = te->mode;
 	(*new_te)->name = strdup(te->name);
 	if ((*new_te)->name == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("strdup");
 		goto done;
 	}
 
 	(*new_te)->id = got_object_id_dup(te->id);
 	if ((*new_te)->id == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("got_object_id_dup");
 		goto done;
 	}
 done:
diff --git a/lib/worktree.c b/lib/worktree.c
index 14285e5..4f7289e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -65,7 +65,7 @@ create_meta_file(const char *path_got, const char *name, const char *content)
 	int fd = -1;
 
 	if (asprintf(&path, "%s/%s", path_got, name) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		path = NULL;
 		goto done;
 	}
@@ -73,7 +73,7 @@ create_meta_file(const char *path_got, const char *name, const char *content)
 	fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
 	    GOT_DEFAULT_FILE_MODE);
 	if (fd == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno(path);
 		goto done;
 	}
 
@@ -101,7 +101,7 @@ update_meta_file(const char *path_got, const char *name, const char *content)
 	char *path = NULL;
 
 	if (asprintf(&path, "%s/%s", path_got, name) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		path = NULL;
 		goto done;
 	}
@@ -113,13 +113,13 @@ update_meta_file(const char *path_got, const char *name, const char *content)
 	if (content) {
 		int len = fprintf(tmpfile, "%s\n", content);
 		if (len != strlen(content) + 1) {
-			err = got_error_from_errno();
+			err = got_error_prefix_errno(tmppath);
 			goto done;
 		}
 	}
 
 	if (rename(tmppath, path) != 0) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno(tmppath);
 		unlink(tmppath);
 		goto done;
 	}
@@ -127,7 +127,7 @@ update_meta_file(const char *path_got, const char *name, const char *content)
 done:
 	free(tmppath);
 	if (fclose(tmpfile) != 0 && err == NULL)
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("fclose");
 	return err;
 }
 
@@ -143,7 +143,7 @@ read_meta_file(char **content, const char *path_got, const char *name)
 	*content = NULL;
 
 	if (asprintf(&path, "%s/%s", path_got, name) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		path = NULL;
 		goto done;
 	}
@@ -153,28 +153,28 @@ read_meta_file(char **content, const char *path_got, const char *name)
 		if (errno == ENOENT)
 			err = got_error(GOT_ERR_WORKTREE_META);
 		else
-			err = got_error_from_errno();
+			err = got_error_prefix_errno(path);
 		goto done;
 	}
 	if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
 		err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
-		    : got_error_from_errno());
+		    : got_error_prefix_errno("flock"));
 		goto done;
 	}
 
 	if (lstat(path, &sb) != 0) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno(path);
 		goto done;
 	}
 	*content = calloc(1, sb.st_size);
 	if (*content == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("calloc");
 		goto done;
 	}
 
 	n = read(fd, *content, sb.st_size);
 	if (n != sb.st_size) {
-		err = (n == -1 ? got_error_from_errno() :
+		err = (n == -1 ? got_error_prefix_errno(path) :
 		    got_error(GOT_ERR_WORKTREE_META));
 		goto done;
 	}
@@ -186,7 +186,7 @@ read_meta_file(char **content, const char *path_got, const char *name)
 
 done:
 	if (fd != -1 && close(fd) == -1 && err == NULL)
-		err = got_error_from_errno();
+		err = got_error_prefix_errno(path_got);
 	free(path);
 	if (err) {
 		free(*content);
@@ -232,17 +232,17 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 
 	/* Create top-level directory (may already exist). */
 	if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno(path);
 		goto done;
 	}
 
 	/* Create .got directory (may already exist). */
 	if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		goto done;
 	}
 	if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno(path_got);
 		goto done;
 	}
 
@@ -303,7 +303,7 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 
 	/* Stamp work tree with format file. */
 	if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		goto done;
 	}
 	err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
@@ -338,13 +338,13 @@ open_worktree(struct got_worktree **worktree, const char *path)
 	*worktree = NULL;
 
 	if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		path_got = NULL;
 		goto done;
 	}
 
 	if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		path_lock = NULL;
 		goto done;
 	}
@@ -352,7 +352,7 @@ open_worktree(struct got_worktree **worktree, const char *path)
 	fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
 	if (fd == -1) {
 		err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
-		    : got_error_from_errno());
+		    : got_error_prefix_errno(path_lock));
 		goto done;
 	}
 
@@ -372,14 +372,14 @@ open_worktree(struct got_worktree **worktree, const char *path)
 
 	*worktree = calloc(1, sizeof(**worktree));
 	if (*worktree == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("calloc");
 		goto done;
 	}
 	(*worktree)->lockfd = -1;
 
 	(*worktree)->root_path = strdup(path);
 	if ((*worktree)->root_path == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("strdup");
 		goto done;
 	}
 	err = read_meta_file(&(*worktree)->repo_path, path_got,
@@ -498,7 +498,7 @@ got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
 
 	if (!got_path_is_absolute(path_prefix)) {
 		if (asprintf(&absprefix, "/%s", path_prefix) == -1)
-			return got_error_from_errno();
+			return got_error_prefix_errno("asprintf");
 	}
 	*match = (strcmp(absprefix ? absprefix : path_prefix,
 	    worktree->path_prefix) == 0);
@@ -529,7 +529,7 @@ got_worktree_set_base_commit_id(struct got_worktree *worktree,
 
 	if (asprintf(&path_got, "%s/%s", worktree->root_path,
 	    GOT_WORKTREE_GOT_DIR) == -1) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("asprintf");
 		path_got = NULL;
 		goto done;
 	}
@@ -554,7 +554,7 @@ got_worktree_set_base_commit_id(struct got_worktree *worktree,
 	free(worktree->base_commit_id);
 	worktree->base_commit_id = got_object_id_dup(commit_id);
 	if (worktree->base_commit_id == NULL) {
-		err = got_error_from_errno();
+		err = got_error_prefix_errno("got_object_id_dup");
 		goto done;
 	}
 done:
@@ -570,7 +570,7 @@ lock_worktree(struct got_worktree *worktree, int operation)
 {
 	if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
 		return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
-		    : got_error_from_errno());
+		    : got_error_prefix_errno("flock"));
 	return NULL;
 }
 
@@ -581,14 +581,14 @@ add_dir_on_disk(struct got_worktree *worktree, const char *path)
 	char *abspath;
 
 	if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
-		return got_error_from_errno();
+		return got_error_prefix_errno("asprintf");
 
 	err = got_path_mkdir(abspath);
 	if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
 		struct stat sb;
 		err = NULL;
 		if (lstat(abspath, &sb) == -1) {
-			err = got_error_from_errno();
+			err = got_error_prefix_errno("lstat");
 		} else if (!S_ISDIR(sb.st_mode)) {
 			/* TODO directory is obstructed; do something */
 			err = got_error(GOT_ERR_FILE_OBSTRUCTED);