commit 48b8b0ebe312124b17b290c38f2bbd0b5fbc1338 from: joshua stein date: Sat May 11 14:40:57 2019 UTC error: introduce got_error_prefix_errno for better errno messages commit - 10689f3a4e8324ff47f0192606e1b0962435ec46 commit + 48b8b0ebe312124b17b290c38f2bbd0b5fbc1338 blob - 785766a030c1661d6a5e8806b18a277e3aebef8a blob + afb23f67b0069ffb34bd9a781d2741d3ee1d5e8c --- include/got_error.h +++ 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). blob - 8ac3f42bd600ad7877b6a4793eb7b96c9260ecd4 blob + bb4d6fe2b481570d5cfbdd728ea7e4e291c9bc26 --- lib/error.c +++ lib/error.c @@ -23,6 +23,7 @@ #include #include #include +#include #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; blob - 8356628a161963e4861146e9b08423ccb7f8cecb blob + 8c71dc8c1c85d1c0e244dee4252aeebc06044c4c --- lib/object.c +++ lib/object.c @@ -118,7 +118,7 @@ got_object_get_path(char **path, struct got_object_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 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_pac *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 (*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 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, st 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_re 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, 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, stru *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 o 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, si 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, si 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 *h 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 *h 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 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_re *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_re 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_re (*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 *n } 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_ 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, struc 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, struc 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_ *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: blob - 14285e597290f7d3ef572c716a5a998b24c7a4ff blob + 4f7289e40f1cc7cb018515aeb36977371856a3b0 --- lib/worktree.c +++ lib/worktree.c @@ -65,7 +65,7 @@ create_meta_file(const char *path_got, const char *nam 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 *nam 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 *nam 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 *nam 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 *nam 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, c *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, c 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, c 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_referen /* 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_referen /* 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 ch *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 ch 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 ch *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_ 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 *w 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 *w 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 opera { 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 c 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);