commit - 10689f3a4e8324ff47f0192606e1b0962435ec46
commit + 48b8b0ebe312124b17b290c38f2bbd0b5fbc1338
blob - 785766a030c1661d6a5e8806b18a277e3aebef8a
blob + afb23f67b0069ffb34bd9a781d2741d3ee1d5e8c
--- include/got_error.h
+++ include/got_error.h
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
#include <sha1.h>
#include <zlib.h>
#include <uuid.h>
+#include <sys/param.h>
#include "got_error.h"
#include "got_object.h"
}
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
*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)
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);
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:
*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,
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);
}
(*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));
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,
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);
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);
*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;
}
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);
*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;
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);
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)
return err;
if (lseek(outfd, SEEK_SET, 0) == -1)
- err = got_error_from_errno();
+ err = got_error_prefix_errno("lseek");
return err;
}
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)
return err;
if (lseek(outfd, SEEK_SET, 0) == -1)
- return got_error_from_errno();
+ return got_error_prefix_errno("lseek");
return err;
}
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);
*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;
}
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;
}
(*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;
} while (len != 0);
if (fflush(outfile) != 0)
- return got_error_from_errno();
+ return got_error_prefix_errno("fflush");
rewind(outfile);
return NULL;
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);
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;
}
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:
*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
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;
}
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;
}
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;
}
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;
}
done:
free(tmppath);
if (fclose(tmpfile) != 0 && err == NULL)
- err = got_error_from_errno();
+ err = got_error_prefix_errno("fclose");
return err;
}
*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;
}
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;
}
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);
/* 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;
}
/* 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);
*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;
}
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;
}
*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,
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);
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;
}
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:
{
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;
}
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);