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_hash.h"
36 #include "got_lib_object_parse.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #endif
42 #if defined(__GLIBC__)
43 /*
44 * The autoconf test for strerror_r is broken in current versions
45 * of autoconf: https://savannah.gnu.org/support/?110367
46 */
47 char *__xpg_strerror_r(int, char *, size_t);
48 #define strerror_r __xpg_strerror_r
49 #endif
51 static const struct got_error got_errors[] = {
52 { GOT_ERR_OK, "no error occured?!?" },
53 { GOT_ERR_ERRNO, "see errno" },
54 { GOT_ERR_NOT_GIT_REPO, "no git repository found" },
55 { GOT_ERR_BAD_FILETYPE, "bad file type" },
56 { GOT_ERR_BAD_PATH, "bad path" },
57 { GOT_ERR_NOT_REF, "no such reference found" },
58 { GOT_ERR_IO, "input/output error" },
59 { GOT_ERR_EOF, "unexpected end of file" },
60 { GOT_ERR_DECOMPRESSION,"decompression failed" },
61 { GOT_ERR_NO_SPACE, "buffer too small" },
62 { GOT_ERR_BAD_OBJ_HDR, "bad object header" },
63 { GOT_ERR_OBJ_TYPE, "wrong type of object" },
64 { GOT_ERR_BAD_OBJ_DATA, "bad object data" },
65 { GOT_ERR_AMBIGUOUS_ID, "ambiguous object ID" },
66 { GOT_ERR_BAD_PACKIDX, "bad pack index file" },
67 { GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
68 { GOT_ERR_BAD_PACKFILE, "bad pack file" },
69 { GOT_ERR_NO_OBJ, "object not found" },
70 { GOT_ERR_NOT_IMPL, "feature not implemented" },
71 { GOT_ERR_OBJ_NOT_PACKED,"object is not packed" },
72 { GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" },
73 { GOT_ERR_BAD_DELTA, "bad delta" },
74 { GOT_ERR_COMPRESSION, "compression failed" },
75 { GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
76 { GOT_ERR_WORKTREE_EXISTS,"worktree already exists" },
77 { GOT_ERR_WORKTREE_META,"bad worktree meta data" },
78 { GOT_ERR_WORKTREE_VERS,"unsupported worktree format version" },
79 { GOT_ERR_WORKTREE_BUSY,"worktree already locked" },
80 { GOT_ERR_FILE_OBSTRUCTED,"file is obstructed" },
81 { GOT_ERR_RECURSION, "recursion limit reached" },
82 { GOT_ERR_TIMEOUT, "operation timed out" },
83 { GOT_ERR_INTERRUPT, "operation interrupted" },
84 { GOT_ERR_PRIVSEP_READ, "no data received in imsg" },
85 { GOT_ERR_PRIVSEP_LEN, "unexpected amount of data received in imsg" },
86 { GOT_ERR_PRIVSEP_PIPE, "privsep peer process closed pipe" },
87 { GOT_ERR_PRIVSEP_NO_FD,"privsep file descriptor unavailable" },
88 { GOT_ERR_PRIVSEP_MSG, "received unexpected privsep message" },
89 { GOT_ERR_PRIVSEP_DIED, "unprivileged process died unexpectedly" },
90 { GOT_ERR_PRIVSEP_EXIT, "bad exit code from unprivileged process" },
91 { GOT_ERR_PACK_OFFSET, "bad offset in pack file" },
92 { GOT_ERR_OBJ_EXISTS, "object already exists" },
93 { GOT_ERR_BAD_OBJ_ID, "bad object id" },
94 { GOT_ERR_ITER_BUSY, "iteration already in progress" },
95 { GOT_ERR_ITER_COMPLETED,"iteration completed" },
96 { GOT_ERR_RANGE, "value out of range" },
97 { GOT_ERR_EXPECTED, "expected an error but have no error" },
98 { GOT_ERR_CANCELLED, "operation in progress has been cancelled" },
99 { GOT_ERR_NO_TREE_ENTRY,"no such entry found in tree" },
100 { GOT_ERR_FILEIDX_SIG, "bad file index signature" },
101 { GOT_ERR_FILEIDX_VER, "unknown file index format version" },
102 { GOT_ERR_FILEIDX_CSUM, "bad file index checksum" },
103 { GOT_ERR_PATH_PREFIX, "worktree already contains items from a "
104 "different path prefix" },
105 { GOT_ERR_ANCESTRY, "target commit is on a different branch" },
106 { GOT_ERR_FILEIDX_BAD, "file index is corrupt" },
107 { GOT_ERR_BAD_REF_DATA, "could not parse reference data" },
108 { GOT_ERR_TREE_DUP_ENTRY,"duplicate entry in tree object" },
109 { GOT_ERR_DIR_DUP_ENTRY,"duplicate entry in directory" },
110 { GOT_ERR_NOT_WORKTREE, "no work tree found" },
111 { GOT_ERR_UUID_VERSION, "bad uuid version" },
112 { GOT_ERR_UUID_INVALID, "uuid invalid" },
113 { GOT_ERR_UUID, "uuid error" },
114 { GOT_ERR_LOCKFILE_TIMEOUT,"lockfile timeout" },
115 { GOT_ERR_BAD_REF_NAME, "bad reference name" },
116 { GOT_ERR_WORKTREE_REPO,"cannot create worktree inside a git repository" },
117 { GOT_ERR_FILE_MODIFIED,"file contains modifications" },
118 { GOT_ERR_FILE_STATUS, "file has unexpected status" },
119 { GOT_ERR_COMMIT_CONFLICT,"cannot commit file in conflicted status" },
120 { GOT_ERR_BAD_REF_TYPE, "bad reference type" },
121 { GOT_ERR_COMMIT_NO_AUTHOR,"GOT_AUTHOR environment variable is not set" },
122 { GOT_ERR_COMMIT_HEAD_CHANGED, "branch head in repository has changed "
123 "while commit was in progress" },
124 { GOT_ERR_COMMIT_OUT_OF_DATE, "work tree must be updated before these "
125 "changes can be committed" },
126 { GOT_ERR_COMMIT_MSG_EMPTY, "commit message cannot be empty" },
127 { GOT_ERR_DIR_NOT_EMPTY, "directory exists and is not empty" },
128 { GOT_ERR_COMMIT_NO_CHANGES, "no changes to commit" },
129 { GOT_ERR_BRANCH_MOVED, "work tree's head reference now points to a "
130 "different branch; new head reference and/or update -b required" },
131 { GOT_ERR_OBJ_TOO_LARGE, "object too large" },
132 { GOT_ERR_SAME_BRANCH, "commit is already contained in this branch" },
133 { GOT_ERR_ROOT_COMMIT, "specified commit has no parent commit" },
134 { GOT_ERR_MIXED_COMMITS,"work tree contains files from multiple "
135 "base commits; the entire work tree must be updated first" },
136 { GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
137 "conflicts must be resolved first" },
138 { GOT_ERR_BRANCH_EXISTS,"specified branch already exists" },
139 { GOT_ERR_MODIFIED, "work tree contains local changes; these "
140 "changes must be committed or reverted first" },
141 { GOT_ERR_NOT_REBASING, "rebase operation not in progress" },
142 { GOT_ERR_REBASE_COMMITID,"rebase commit ID mismatch" },
143 { GOT_ERR_REBASING, "a rebase operation is in progress in this "
144 "work tree and must be continued or aborted first" },
145 { GOT_ERR_REBASE_PATH, "cannot rebase branch which contains "
146 "changes outside of this work tree's path prefix" },
147 { GOT_ERR_NOT_HISTEDIT, "histedit operation not in progress" },
148 { GOT_ERR_EMPTY_HISTEDIT,"no commits to edit; perhaps the work tree "
149 "must be updated to an older commit first" },
150 { GOT_ERR_NO_HISTEDIT_CMD,"no histedit commands provided" },
151 { GOT_ERR_HISTEDIT_SYNTAX,"syntax error in histedit command list" },
152 { GOT_ERR_HISTEDIT_CANCEL,"histedit operation cancelled" },
153 { 95, "unused error code" },
154 { GOT_ERR_HISTEDIT_BUSY,"histedit operation is in progress in this "
155 "work tree and must be continued or aborted first" },
156 { GOT_ERR_HISTEDIT_CMD, "bad histedit command" },
157 { GOT_ERR_HISTEDIT_PATH, "cannot edit branch history which contains "
158 "changes outside of this work tree's path prefix" },
159 { GOT_ERR_PACKFILE_CSUM, "pack file checksum error" },
160 { GOT_ERR_COMMIT_BRANCH, "will not commit to a branch outside the "
161 "\"refs/heads/\" reference namespace" },
162 { GOT_ERR_FILE_STAGED, "file is staged" },
163 { GOT_ERR_STAGE_NO_CHANGE, "no changes to stage" },
164 { GOT_ERR_STAGE_CONFLICT, "cannot stage file in conflicted status" },
165 { GOT_ERR_STAGE_OUT_OF_DATE, "work tree must be updated before "
166 "changes can be staged" },
167 { GOT_ERR_FILE_NOT_STAGED, "file is not staged" },
168 { GOT_ERR_STAGED_PATHS, "work tree contains files with staged "
169 "changes; these changes must be committed or unstaged first" },
170 { GOT_ERR_PATCH_CHOICE, "invalid patch choice" },
171 { GOT_ERR_COMMIT_NO_EMAIL, "commit author's email address is required "
172 "for compatibility with Git" },
173 { GOT_ERR_TAG_EXISTS,"specified tag already exists" },
174 { GOT_ERR_GIT_REPO_FORMAT,"unknown git repository format version" },
175 { GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
176 { GOT_ERR_REGEX, "regular expression error" },
177 { GOT_ERR_REF_NAME_MINUS, "reference name may not start with '-'" },
178 { GOT_ERR_GITCONFIG_SYNTAX, "gitconfig syntax error" },
179 { GOT_ERR_REBASE_OUT_OF_DATE, "work tree must be updated before it "
180 "can be used to rebase a branch" },
181 { GOT_ERR_CACHE_DUP_ENTRY, "duplicate cache entry" },
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_BUSY,"a merge operation is in progress in this "
214 "work tree and must be continued or aborted first" },
215 { GOT_ERR_MERGE_PATH, "cannot merge branch which contains "
216 "changes outside of this work tree's path prefix" },
217 { GOT_ERR_FILE_BINARY, "found a binary file instead of text" },
218 { GOT_ERR_PATCH_MALFORMED, "malformed patch" },
219 { GOT_ERR_PATCH_TRUNCATED, "patch truncated" },
220 { GOT_ERR_NO_PATCH, "no patch found" },
221 { GOT_ERR_HUNK_FAILED, "hunk failed to apply" },
222 { GOT_ERR_PATCH_FAILED, "patch failed to apply" },
223 { GOT_ERR_FILEIDX_DUP_ENTRY, "duplicate file index entry" },
224 { GOT_ERR_PIN_PACK, "could not pin pack file" },
225 { GOT_ERR_BAD_TAG_SIGNATURE, "invalid tag signature" },
226 { GOT_ERR_VERIFY_TAG_SIGNATURE, "cannot verify signature" },
227 { GOT_ERR_SIGNING_TAG, "unable to sign tag" },
228 { GOT_ERR_BAD_OPTION, "option cannot be used" },
229 { GOT_ERR_BAD_QUERYSTRING, "invalid query string" },
230 { GOT_ERR_INTEGRATE_BRANCH, "will not integrate into a reference "
231 "outside the \"refs/heads/\" reference namespace" },
232 { GOT_ERR_BAD_REQUEST, "unexpected request received" },
233 { GOT_ERR_CLIENT_ID, "unknown client identifier" },
234 { GOT_ERR_REPO_TEMPFILE, "no repository tempfile available" },
235 { GOT_ERR_REFS_PROTECTED, "reference namespace is protected" },
236 { GOT_ERR_REF_PROTECTED, "reference is protected" },
237 { GOT_ERR_REF_BUSY, "reference cannot be updated; please try again" },
238 { GOT_ERR_COMMIT_BAD_AUTHOR, "commit author formatting would "
239 "make Git unhappy" },
240 { GOT_ERR_UID, "bad user ID" },
241 { GOT_ERR_GID, "bad group ID" },
242 { GOT_ERR_NO_PROG, "command not found or not accessible" },
243 { GOT_ERR_MERGE_COMMIT_OUT_OF_DATE, "merging cannot proceed because "
244 "the work tree is no longer up-to-date; merge must be aborted "
245 "and retried" },
246 { GOT_ERR_BUNDLE_FORMAT, "unknown git bundle version" },
247 { GOT_ERR_BAD_KEYWORD, "invalid commit keyword" }
248 };
250 static struct got_custom_error {
251 struct got_error err;
252 char msg[GOT_ERR_MAX_MSG_SIZE];
253 } custom_errors[16];
255 static struct got_custom_error *
256 get_custom_err(void)
258 static unsigned int idx;
259 return &custom_errors[(idx++) % nitems(custom_errors)];
262 const struct got_error *
263 got_error(int code)
265 size_t i;
267 for (i = 0; i < nitems(got_errors); i++) {
268 if (code == got_errors[i].code)
269 return &got_errors[i];
272 abort();
275 const struct got_error *
276 got_error_msg(int code, const char *msg)
278 struct got_custom_error *cerr = get_custom_err();
279 struct got_error *err = &cerr->err;
280 size_t i;
282 for (i = 0; i < nitems(got_errors); i++) {
283 if (code == got_errors[i].code) {
284 err->code = code;
285 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
286 err->msg = cerr->msg;
287 return err;
291 abort();
294 const struct got_error *
295 got_error_from_errno(const char *prefix)
297 struct got_custom_error *cerr = get_custom_err();
298 struct got_error *err = &cerr->err;
299 char strerr[128];
301 strerror_r(errno, strerr, sizeof(strerr));
302 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
304 err->code = GOT_ERR_ERRNO;
305 err->msg = cerr->msg;
306 return err;
309 const struct got_error *
310 got_error_from_errno2(const char *prefix, const char *prefix2)
312 struct got_custom_error *cerr = get_custom_err();
313 struct got_error *err = &cerr->err;
314 char strerr[128];
316 strerror_r(errno, strerr, sizeof(strerr));
317 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
318 strerr);
320 err->code = GOT_ERR_ERRNO;
321 err->msg = cerr->msg;
322 return err;
325 const struct got_error *
326 got_error_from_errno3(const char *prefix, const char *prefix2,
327 const char *prefix3)
329 struct got_custom_error *cerr = get_custom_err();
330 struct got_error *err = &cerr->err;
331 char strerr[128];
333 strerror_r(errno, strerr, sizeof(strerr));
334 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
335 prefix2, prefix3, strerr);
337 err->code = GOT_ERR_ERRNO;
338 err->msg = cerr->msg;
339 return err;
342 const struct got_error *
343 got_error_from_errno_fmt(const char *fmt, ...)
345 struct got_custom_error *cerr = get_custom_err();
346 struct got_error *err = &cerr->err;
347 char buf[PATH_MAX * 4];
348 char strerr[128];
349 va_list ap;
351 va_start(ap, fmt);
352 vsnprintf(buf, sizeof(buf), fmt, ap);
353 va_end(ap);
355 strerror_r(errno, strerr, sizeof(strerr));
356 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
358 err->code = GOT_ERR_ERRNO;
359 err->msg = cerr->msg;
360 return err;
363 const struct got_error *
364 got_error_set_errno(int code, const char *prefix)
366 errno = code;
367 return got_error_from_errno(prefix);
370 const struct got_error *
371 got_ferror(FILE *f, int code)
373 if (ferror(f))
374 return got_error_from_errno("");
375 return got_error(code);
378 const struct got_error *
379 got_error_no_obj(struct got_object_id *id)
381 char id_str[GOT_OBJECT_ID_HEX_MAXLEN];
382 char msg[sizeof("object not found") + sizeof(id_str)];
383 int ret;
385 if (!got_object_id_hex(id, id_str, sizeof(id_str)))
386 return got_error(GOT_ERR_NO_OBJ);
388 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
389 if (ret < 0 || (size_t)ret >= sizeof(msg))
390 return got_error(GOT_ERR_NO_OBJ);
392 return got_error_msg(GOT_ERR_NO_OBJ, msg);
395 const struct got_error *
396 got_error_checksum(struct got_object_id *id)
398 char id_str[GOT_OBJECT_ID_HEX_MAXLEN];
399 char msg[sizeof("checksum failure for object ") + sizeof(id_str)];
400 int ret;
402 if (!got_object_id_hex(id, id_str, sizeof(id_str)))
403 return got_error(GOT_ERR_OBJ_CSUM);
405 ret = snprintf(msg, sizeof(msg), "checksum failure for object %s",
406 id_str);
407 if (ret < 0 || (size_t)ret >= sizeof(msg))
408 return got_error(GOT_ERR_OBJ_CSUM);
410 return got_error_msg(GOT_ERR_OBJ_CSUM, msg);
413 const struct got_error *
414 got_error_not_ref(const char *refname)
416 char msg[sizeof("reference not found") + 1004];
417 int ret;
419 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
420 if (ret < 0 || (size_t)ret >= sizeof(msg))
421 return got_error(GOT_ERR_NOT_REF);
423 return got_error_msg(GOT_ERR_NOT_REF, msg);
426 const struct got_error *
427 got_error_uuid(uint32_t uuid_status, const char *prefix)
429 switch (uuid_status) {
430 case uuid_s_ok:
431 return NULL;
432 case uuid_s_bad_version:
433 return got_error(GOT_ERR_UUID_VERSION);
434 case uuid_s_invalid_string_uuid:
435 return got_error(GOT_ERR_UUID_INVALID);
436 case uuid_s_no_memory:
437 return got_error_set_errno(ENOMEM, prefix);
438 default:
439 return got_error(GOT_ERR_UUID);
443 const struct got_error *
444 got_error_path(const char *path, int code)
446 struct got_custom_error *cerr = get_custom_err();
447 struct got_error *err = &cerr->err;
448 size_t i;
450 for (i = 0; i < nitems(got_errors); i++) {
451 if (code == got_errors[i].code) {
452 err->code = code;
453 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
454 got_errors[i].msg);
455 err->msg = cerr->msg;
456 return err;
460 abort();
463 const struct got_error *
464 got_error_fmt(int code, const char *fmt, ...)
466 struct got_custom_error *cerr = get_custom_err();
467 struct got_error *err = &cerr->err;
468 char buf[PATH_MAX * 4];
469 va_list ap;
470 size_t i;
472 va_start(ap, fmt);
473 vsnprintf(buf, sizeof(buf), fmt, ap);
474 va_end(ap);
476 for (i = 0; i < nitems(got_errors); i++) {
477 if (code == got_errors[i].code) {
478 err->code = code;
479 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
480 got_errors[i].msg);
481 err->msg = cerr->msg;
482 return err;
486 abort();
489 int
490 got_err_open_nofollow_on_symlink(void)
492 /*
493 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
494 * Posix mandates ELOOP and OpenBSD follows it. Others return
495 * different error codes. We carry this workaround to help the
496 * portable version a little.
497 */
498 return (errno == ELOOP
499 #ifdef EMLINK
500 || errno == EMLINK
501 #endif
502 #ifdef EFTYPE
503 || errno == EFTYPE
504 #endif
505 );