Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <zlib.h>
27 #include "got_compat.h"
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_delta.h"
33 #include "got_lib_inflate.h"
34 #include "got_lib_object.h"
35 #include "got_lib_sha1.h"
37 #ifndef nitems
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 #endif
41 #if defined(__GLIBC__)
42 /*
43 * The autoconf test for strerror_r is broken in current versions
44 * of autoconf: https://savannah.gnu.org/support/?110367
45 */
46 #define strerror_r __xpg_strerror_r
47 #endif
49 static const struct got_error got_errors[] = {
50 { GOT_ERR_OK, "no error occured?!?" },
51 { GOT_ERR_ERRNO, "see errno" },
52 { GOT_ERR_NOT_GIT_REPO, "no git repository found" },
53 { GOT_ERR_BAD_FILETYPE, "bad file type" },
54 { GOT_ERR_BAD_PATH, "bad path" },
55 { GOT_ERR_NOT_REF, "no such reference found" },
56 { GOT_ERR_IO, "input/output error" },
57 { GOT_ERR_EOF, "unexpected end of file" },
58 { GOT_ERR_DECOMPRESSION,"decompression failed" },
59 { GOT_ERR_NO_SPACE, "buffer too small" },
60 { GOT_ERR_BAD_OBJ_HDR, "bad object header" },
61 { GOT_ERR_OBJ_TYPE, "wrong type of object" },
62 { GOT_ERR_BAD_OBJ_DATA, "bad object data" },
63 { GOT_ERR_AMBIGUOUS_ID, "ambiguous object ID" },
64 { GOT_ERR_BAD_PACKIDX, "bad pack index file" },
65 { GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
66 { GOT_ERR_BAD_PACKFILE, "bad pack file" },
67 { GOT_ERR_NO_OBJ, "object not found" },
68 { GOT_ERR_NOT_IMPL, "feature not implemented" },
69 { GOT_ERR_OBJ_NOT_PACKED,"object is not packed" },
70 { GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" },
71 { GOT_ERR_BAD_DELTA, "bad delta" },
72 { GOT_ERR_COMPRESSION, "compression failed" },
73 { GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
74 { GOT_ERR_WORKTREE_EXISTS,"worktree already exists" },
75 { GOT_ERR_WORKTREE_META,"bad worktree meta data" },
76 { GOT_ERR_WORKTREE_VERS,"unsupported worktree format version" },
77 { GOT_ERR_WORKTREE_BUSY,"worktree already locked" },
78 { GOT_ERR_FILE_OBSTRUCTED,"file is obstructed" },
79 { GOT_ERR_RECURSION, "recursion limit reached" },
80 { GOT_ERR_TIMEOUT, "operation timed out" },
81 { GOT_ERR_INTERRUPT, "operation interrupted" },
82 { GOT_ERR_PRIVSEP_READ, "no data received in imsg" },
83 { GOT_ERR_PRIVSEP_LEN, "unexpected amount of data received in imsg" },
84 { GOT_ERR_PRIVSEP_PIPE, "privsep peer process closed pipe" },
85 { GOT_ERR_PRIVSEP_NO_FD,"privsep file descriptor unavailable" },
86 { GOT_ERR_PRIVSEP_MSG, "received unexpected privsep message" },
87 { GOT_ERR_PRIVSEP_DIED, "unprivileged process died unexpectedly" },
88 { GOT_ERR_PRIVSEP_EXIT, "bad exit code from unprivileged process" },
89 { GOT_ERR_PACK_OFFSET, "bad offset in pack file" },
90 { GOT_ERR_OBJ_EXISTS, "object already exists" },
91 { GOT_ERR_BAD_OBJ_ID, "bad object id" },
92 { GOT_ERR_ITER_BUSY, "iteration already in progress" },
93 { GOT_ERR_ITER_COMPLETED,"iteration completed" },
94 { GOT_ERR_RANGE, "value out of range" },
95 { GOT_ERR_EXPECTED, "expected an error but have no error" },
96 { GOT_ERR_CANCELLED, "operation in progress has been cancelled" },
97 { GOT_ERR_NO_TREE_ENTRY,"no such entry found in tree" },
98 { GOT_ERR_FILEIDX_SIG, "bad file index signature" },
99 { GOT_ERR_FILEIDX_VER, "unknown file index format version" },
100 { GOT_ERR_FILEIDX_CSUM, "bad file index checksum" },
101 { GOT_ERR_PATH_PREFIX, "worktree already contains items from a "
102 "different path prefix" },
103 { GOT_ERR_ANCESTRY, "target commit is on a different branch" },
104 { GOT_ERR_FILEIDX_BAD, "file index is corrupt" },
105 { GOT_ERR_BAD_REF_DATA, "could not parse reference data" },
106 { GOT_ERR_TREE_DUP_ENTRY,"duplicate entry in tree object" },
107 { GOT_ERR_DIR_DUP_ENTRY,"duplicate entry in directory" },
108 { GOT_ERR_NOT_WORKTREE, "no got work tree found" },
109 { GOT_ERR_UUID_VERSION, "bad uuid version" },
110 { GOT_ERR_UUID_INVALID, "uuid invalid" },
111 { GOT_ERR_UUID, "uuid error" },
112 { GOT_ERR_LOCKFILE_TIMEOUT,"lockfile timeout" },
113 { GOT_ERR_BAD_REF_NAME, "bad reference name" },
114 { GOT_ERR_WORKTREE_REPO,"cannot create worktree inside a git repository" },
115 { GOT_ERR_FILE_MODIFIED,"file contains modifications" },
116 { GOT_ERR_FILE_STATUS, "file has unexpected status" },
117 { GOT_ERR_COMMIT_CONFLICT,"cannot commit file in conflicted status" },
118 { GOT_ERR_BAD_REF_TYPE, "bad reference type" },
119 { GOT_ERR_COMMIT_NO_AUTHOR,"GOT_AUTHOR environment variable is not set" },
120 { GOT_ERR_COMMIT_HEAD_CHANGED, "branch head in repository has changed "
121 "while commit was in progress" },
122 { GOT_ERR_COMMIT_OUT_OF_DATE, "work tree must be updated before these "
123 "changes can be committed" },
124 { GOT_ERR_COMMIT_MSG_EMPTY, "commit message cannot be empty" },
125 { GOT_ERR_DIR_NOT_EMPTY, "directory exists and is not empty" },
126 { GOT_ERR_COMMIT_NO_CHANGES, "no changes to commit" },
127 { GOT_ERR_BRANCH_MOVED, "work tree's head reference now points to a "
128 "different branch; new head reference and/or update -b required" },
129 { GOT_ERR_OBJ_TOO_LARGE, "object too large" },
130 { GOT_ERR_SAME_BRANCH, "commit is already contained in this branch" },
131 { GOT_ERR_ROOT_COMMIT, "specified commit has no parent commit" },
132 { GOT_ERR_MIXED_COMMITS,"work tree contains files from multiple "
133 "base commits; the entire work tree must be updated first" },
134 { GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
135 "conflicts must be resolved first" },
136 { GOT_ERR_BRANCH_EXISTS,"specified branch already exists" },
137 { GOT_ERR_MODIFIED, "work tree contains local changes; these "
138 "changes must be committed or reverted first" },
139 { GOT_ERR_NOT_REBASING, "rebase operation not in progress" },
140 { GOT_ERR_EMPTY_REBASE, "no commits to rebase" },
141 { GOT_ERR_REBASE_COMMITID,"rebase commit ID mismatch" },
142 { GOT_ERR_REBASING, "a rebase operation is in progress in this "
143 "work tree and must be continued or aborted first" },
144 { GOT_ERR_REBASE_PATH, "cannot rebase branch which contains "
145 "changes outside of this work tree's path prefix" },
146 { GOT_ERR_NOT_HISTEDIT, "histedit operation not in progress" },
147 { GOT_ERR_EMPTY_HISTEDIT,"no commits to edit; perhaps the work tree "
148 "must be updated to an older commit first" },
149 { GOT_ERR_NO_HISTEDIT_CMD,"no histedit commands provided" },
150 { GOT_ERR_HISTEDIT_SYNTAX,"syntax error in histedit command list" },
151 { GOT_ERR_HISTEDIT_CANCEL,"histedit operation cancelled" },
152 { 95, "unused error code" },
153 { GOT_ERR_HISTEDIT_BUSY,"histedit operation is in progress in this "
154 "work tree and must be continued or aborted first" },
155 { GOT_ERR_HISTEDIT_CMD, "bad histedit command" },
156 { GOT_ERR_HISTEDIT_PATH, "cannot edit branch history which contains "
157 "changes outside of this work tree's path prefix" },
158 { 99, "unused error code" },
159 { GOT_ERR_COMMIT_BRANCH, "will not commit to a branch outside the "
160 "\"refs/heads/\" reference namespace" },
161 { GOT_ERR_FILE_STAGED, "file is staged" },
162 { GOT_ERR_STAGE_NO_CHANGE, "no changes to stage" },
163 { GOT_ERR_STAGE_CONFLICT, "cannot stage file in conflicted status" },
164 { GOT_ERR_STAGE_OUT_OF_DATE, "work tree must be updated before "
165 "changes can be staged" },
166 { GOT_ERR_FILE_NOT_STAGED, "file is not staged" },
167 { GOT_ERR_STAGED_PATHS, "work tree contains files with staged "
168 "changes; these changes must be committed or unstaged first" },
169 { GOT_ERR_PATCH_CHOICE, "invalid patch choice" },
170 { GOT_ERR_COMMIT_NO_EMAIL, "commit author's email address is required "
171 "for compatibility with Git" },
172 { GOT_ERR_TAG_EXISTS,"specified tag already exists" },
173 { GOT_ERR_GIT_REPO_FORMAT,"unknown git repository format version" },
174 { GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
175 { GOT_ERR_REGEX, "regular expression error" },
176 { GOT_ERR_REF_NAME_MINUS, "reference name may not start with '-'" },
177 { GOT_ERR_GITCONFIG_SYNTAX, "gitconfig syntax error" },
178 { GOT_ERR_REBASE_OUT_OF_DATE, "work tree must be updated before it "
179 "can be used to rebase a branch" },
180 { GOT_ERR_CACHE_DUP_ENTRY, "duplicate cache entry" },
181 { GOT_ERR_QUERYSTRING, "bad querystring" },
182 { GOT_ERR_FETCH_FAILED, "fetch failed" },
183 { GOT_ERR_PARSE_URI, "failed to parse uri" },
184 { GOT_ERR_BAD_PROTO, "unknown protocol" },
185 { GOT_ERR_ADDRINFO, "getaddrinfo failed" },
186 { GOT_ERR_BAD_PACKET, "bad packet received" },
187 { GOT_ERR_NO_REMOTE, "remote repository not found" },
188 { GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
189 { GOT_ERR_FETCH_BAD_REF, "reference cannot be fetched" },
190 { GOT_ERR_TREE_ENTRY_TYPE, "unexpected tree entry type" },
191 { GOT_ERR_PARSE_CONFIG, "configuration file syntax error" },
192 { GOT_ERR_NO_CONFIG_FILE, "configuration file doesn't exit" },
193 { GOT_ERR_BAD_SYMLINK, "symbolic link points outside of paths under "
194 "version control" },
195 { GOT_ERR_GIT_REPO_EXT, "unsupported repository format extension" },
196 { GOT_ERR_CANNOT_PACK, "not enough objects to pack" },
197 { GOT_ERR_LONELY_PACKIDX, "pack index has no corresponding pack file; "
198 "pack file must be restored or 'gotadmin cleanup -p' must be run" },
199 { GOT_ERR_OBJ_CSUM, "bad object checksum" },
200 { GOT_ERR_SEND_BAD_REF, "reference cannot be sent" },
201 { GOT_ERR_SEND_FAILED, "could not send pack file" },
202 { GOT_ERR_SEND_EMPTY, "no references to send" },
203 { GOT_ERR_SEND_ANCESTRY, "fetch and rebase required" },
204 { GOT_ERR_CAPA_DELETE_REFS, "server cannot delete references" },
205 { GOT_ERR_SEND_DELETE_REF, "reference cannot be deleted" },
206 { GOT_ERR_SEND_TAG_EXISTS, "tag already exists on server" },
207 { GOT_ERR_NOT_MERGING, "merge operation not in progress" },
208 { GOT_ERR_MERGE_OUT_OF_DATE, "work tree must be updated before it "
209 "can be used to merge a branch" },
210 { GOT_ERR_MERGE_STAGED_PATHS, "work tree contains files with staged "
211 "changes; these changes must be unstaged before merging can "
212 "proceed" },
213 { GOT_ERR_MERGE_COMMIT_OUT_OF_DATE, "merging cannot proceed because "
214 "the work tree is no longer up-to-date; merge must be aborted "
215 "and retried" },
216 { GOT_ERR_MERGE_BUSY,"a merge operation is in progress in this "
217 "work tree and must be continued or aborted first" },
218 { GOT_ERR_MERGE_PATH, "cannot merge branch which contains "
219 "changes outside of this work tree's path prefix" },
220 { GOT_ERR_FILE_BINARY, "found a binary file instead of text" },
221 { GOT_ERR_PATCH_MALFORMED, "malformed patch" },
222 { GOT_ERR_PATCH_TRUNCATED, "patch truncated" },
223 { GOT_ERR_NO_PATCH, "no patch found" },
224 { GOT_ERR_HUNK_FAILED, "hunk failed to apply" },
225 { GOT_ERR_PATCH_FAILED, "patch failed to apply" },
226 { GOT_ERR_FILEIDX_DUP_ENTRY, "duplicate file index entry" },
227 { GOT_ERR_PIN_PACK, "could not pin pack file" },
228 { GOT_ERR_BAD_TAG_SIGNATURE, "invalid tag signature" },
229 { GOT_ERR_VERIFY_TAG_SIGNATURE, "cannot verify signature" },
230 { GOT_ERR_SIGNING_TAG, "unable to sign tag" },
231 { GOT_ERR_COMMIT_REDUNDANT_AUTHOR, "specified author is equal to the "
232 "default one"},
233 { GOT_ERR_BAD_QUERYSTRING, "invalid query string" },
234 };
236 static struct got_custom_error {
237 struct got_error err;
238 char msg[4080];
239 } custom_errors[16];
241 static struct got_custom_error *
242 get_custom_err(void)
244 static unsigned int idx;
245 return &custom_errors[(idx++) % nitems(custom_errors)];
248 const struct got_error *
249 got_error(int code)
251 size_t i;
253 if (code == GOT_ERR_PRIVSEP_LEN) abort();
254 for (i = 0; i < nitems(got_errors); i++) {
255 if (code == got_errors[i].code)
256 return &got_errors[i];
259 abort();
262 const struct got_error *
263 got_error_msg(int code, const char *msg)
265 struct got_custom_error *cerr = get_custom_err();
266 struct got_error *err = &cerr->err;
267 size_t i;
269 for (i = 0; i < nitems(got_errors); i++) {
270 if (code == got_errors[i].code) {
271 err->code = code;
272 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
273 err->msg = cerr->msg;
274 return err;
278 abort();
281 const struct got_error *
282 got_error_from_errno(const char *prefix)
284 struct got_custom_error *cerr = get_custom_err();
285 struct got_error *err = &cerr->err;
286 char strerr[128];
288 strerror_r(errno, strerr, sizeof(strerr));
289 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
291 err->code = GOT_ERR_ERRNO;
292 err->msg = cerr->msg;
293 return err;
296 const struct got_error *
297 got_error_from_errno2(const char *prefix, const char *prefix2)
299 struct got_custom_error *cerr = get_custom_err();
300 struct got_error *err = &cerr->err;
301 char strerr[128];
303 strerror_r(errno, strerr, sizeof(strerr));
304 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
305 strerr);
307 err->code = GOT_ERR_ERRNO;
308 err->msg = cerr->msg;
309 return err;
312 const struct got_error *
313 got_error_from_errno3(const char *prefix, const char *prefix2,
314 const char *prefix3)
316 struct got_custom_error *cerr = get_custom_err();
317 struct got_error *err = &cerr->err;
318 char strerr[128];
320 strerror_r(errno, strerr, sizeof(strerr));
321 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
322 prefix2, prefix3, strerr);
324 err->code = GOT_ERR_ERRNO;
325 err->msg = cerr->msg;
326 return err;
329 const struct got_error *
330 got_error_from_errno_fmt(const char *fmt, ...)
332 struct got_custom_error *cerr = get_custom_err();
333 struct got_error *err = &cerr->err;
334 char buf[PATH_MAX * 4];
335 char strerr[128];
336 va_list ap;
338 va_start(ap, fmt);
339 vsnprintf(buf, sizeof(buf), fmt, ap);
340 va_end(ap);
342 strerror_r(errno, strerr, sizeof(strerr));
343 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
345 err->code = GOT_ERR_ERRNO;
346 err->msg = cerr->msg;
347 return err;
350 const struct got_error *
351 got_error_set_errno(int code, const char *prefix)
353 errno = code;
354 return got_error_from_errno(prefix);
357 const struct got_error *
358 got_ferror(FILE *f, int code)
360 if (ferror(f))
361 return got_error_from_errno("");
362 return got_error(code);
365 const struct got_error *
366 got_error_no_obj(struct got_object_id *id)
368 char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
369 char id_str[SHA1_DIGEST_STRING_LENGTH];
370 int ret;
372 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
373 return got_error(GOT_ERR_NO_OBJ);
375 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
376 if (ret < 0 || (size_t)ret >= sizeof(msg))
377 return got_error(GOT_ERR_NO_OBJ);
379 return got_error_msg(GOT_ERR_NO_OBJ, msg);
382 const struct got_error *
383 got_error_not_ref(const char *refname)
385 char msg[sizeof("reference not found") + 1004];
386 int ret;
388 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
389 if (ret < 0 || (size_t)ret >= sizeof(msg))
390 return got_error(GOT_ERR_NOT_REF);
392 return got_error_msg(GOT_ERR_NOT_REF, msg);
395 const struct got_error *
396 got_error_uuid(uint32_t uuid_status, const char *prefix)
398 switch (uuid_status) {
399 case uuid_s_ok:
400 return NULL;
401 case uuid_s_bad_version:
402 return got_error(GOT_ERR_UUID_VERSION);
403 case uuid_s_invalid_string_uuid:
404 return got_error(GOT_ERR_UUID_INVALID);
405 case uuid_s_no_memory:
406 return got_error_set_errno(ENOMEM, prefix);
407 default:
408 return got_error(GOT_ERR_UUID);
412 const struct got_error *
413 got_error_path(const char *path, int code)
415 struct got_custom_error *cerr = get_custom_err();
416 struct got_error *err = &cerr->err;
417 size_t i;
419 for (i = 0; i < nitems(got_errors); i++) {
420 if (code == got_errors[i].code) {
421 err->code = code;
422 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
423 got_errors[i].msg);
424 err->msg = cerr->msg;
425 return err;
429 abort();
432 const struct got_error *
433 got_error_fmt(int code, const char *fmt, ...)
435 struct got_custom_error *cerr = get_custom_err();
436 struct got_error *err = &cerr->err;
437 char buf[PATH_MAX * 4];
438 va_list ap;
439 size_t i;
441 va_start(ap, fmt);
442 vsnprintf(buf, sizeof(buf), fmt, ap);
443 va_end(ap);
445 for (i = 0; i < nitems(got_errors); i++) {
446 if (code == got_errors[i].code) {
447 err->code = code;
448 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
449 got_errors[i].msg);
450 err->msg = cerr->msg;
451 return err;
455 abort();
458 int
459 got_err_open_nofollow_on_symlink(void)
461 /*
462 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
463 * Posix mandates ELOOP and OpenBSD follows it. Others return
464 * different error codes. We carry this workaround to help the
465 * portable version a little.
466 */
467 return (errno == ELOOP
468 #ifdef EMLINK
469 || errno == EMLINK
470 #endif
471 #ifdef EFTYPE
472 || errno == EFTYPE
473 #endif
474 );