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 <sha1.h>
26 #include <sha2.h>
27 #include <zlib.h>
28 #include <uuid.h>
30 #include "got_error.h"
31 #include "got_object.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_object.h"
36 #include "got_lib_hash.h"
37 #include "got_lib_object_parse.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #endif
43 static const struct got_error got_errors[] = {
44 { GOT_ERR_OK, "no error occurred?!?" },
45 { GOT_ERR_ERRNO, "see errno" },
46 { GOT_ERR_NOT_GIT_REPO, "no git repository found" },
47 { GOT_ERR_BAD_FILETYPE, "bad file type" },
48 { GOT_ERR_BAD_PATH, "bad path" },
49 { GOT_ERR_NOT_REF, "no such reference found" },
50 { GOT_ERR_IO, "input/output error" },
51 { GOT_ERR_EOF, "unexpected end of file" },
52 { GOT_ERR_DECOMPRESSION,"decompression failed" },
53 { GOT_ERR_NO_SPACE, "buffer too small" },
54 { GOT_ERR_BAD_OBJ_HDR, "bad object header" },
55 { GOT_ERR_OBJ_TYPE, "wrong type of object" },
56 { GOT_ERR_BAD_OBJ_DATA, "bad object data" },
57 { GOT_ERR_AMBIGUOUS_ID, "ambiguous object ID" },
58 { GOT_ERR_BAD_PACKIDX, "bad pack index file" },
59 { GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
60 { GOT_ERR_BAD_PACKFILE, "bad pack file" },
61 { GOT_ERR_NO_OBJ, "object not found" },
62 { GOT_ERR_NOT_IMPL, "feature not implemented" },
63 { GOT_ERR_OBJ_NOT_PACKED,"object is not packed" },
64 { GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" },
65 { GOT_ERR_BAD_DELTA, "bad delta" },
66 { GOT_ERR_COMPRESSION, "compression failed" },
67 { GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
68 { GOT_ERR_WORKTREE_EXISTS,"worktree already exists" },
69 { GOT_ERR_WORKTREE_META,"bad worktree meta data" },
70 { GOT_ERR_WORKTREE_VERS,"unsupported worktree format version" },
71 { GOT_ERR_WORKTREE_BUSY,"worktree already locked" },
72 { GOT_ERR_FILE_OBSTRUCTED,"file is obstructed" },
73 { GOT_ERR_RECURSION, "recursion limit reached" },
74 { GOT_ERR_TIMEOUT, "operation timed out" },
75 { GOT_ERR_INTERRUPT, "operation interrupted" },
76 { GOT_ERR_PRIVSEP_READ, "no data received in imsg" },
77 { GOT_ERR_PRIVSEP_LEN, "unexpected amount of data received in imsg" },
78 { GOT_ERR_PRIVSEP_PIPE, "privsep peer process closed pipe" },
79 { GOT_ERR_PRIVSEP_NO_FD,"privsep file descriptor unavailable" },
80 { GOT_ERR_PRIVSEP_MSG, "received unexpected privsep message" },
81 { GOT_ERR_PRIVSEP_DIED, "unprivileged process died unexpectedly" },
82 { GOT_ERR_PRIVSEP_EXIT, "bad exit code from unprivileged process" },
83 { GOT_ERR_PACK_OFFSET, "bad offset in pack file" },
84 { GOT_ERR_OBJ_EXISTS, "object already exists" },
85 { GOT_ERR_BAD_OBJ_ID, "bad object id" },
86 { GOT_ERR_ITER_COMPLETED,"iteration completed" },
87 { GOT_ERR_RANGE, "value out of range" },
88 { GOT_ERR_EXPECTED, "expected an error but have no error" },
89 { GOT_ERR_CANCELLED, "operation in progress has been cancelled" },
90 { GOT_ERR_NO_TREE_ENTRY,"no such entry found in tree" },
91 { GOT_ERR_FILEIDX_SIG, "bad file index signature" },
92 { GOT_ERR_FILEIDX_VER, "unknown file index format version" },
93 { GOT_ERR_FILEIDX_CSUM, "bad file index checksum" },
94 { GOT_ERR_PATH_PREFIX, "worktree already contains items from a "
95 "different path prefix" },
96 { GOT_ERR_ANCESTRY, "target commit is on a different branch" },
97 { GOT_ERR_FILEIDX_BAD, "file index is corrupt" },
98 { GOT_ERR_BAD_REF_DATA, "could not parse reference data" },
99 { GOT_ERR_TREE_DUP_ENTRY,"duplicate entry in tree object" },
100 { GOT_ERR_DIR_DUP_ENTRY,"duplicate entry in directory" },
101 { GOT_ERR_NOT_WORKTREE, "no work tree found" },
102 { GOT_ERR_UUID_VERSION, "bad uuid version" },
103 { GOT_ERR_UUID_INVALID, "uuid invalid" },
104 { GOT_ERR_UUID, "uuid error" },
105 { GOT_ERR_LOCKFILE_TIMEOUT,"lockfile timeout" },
106 { GOT_ERR_BAD_REF_NAME, "bad reference name" },
107 { GOT_ERR_WORKTREE_REPO,"cannot create worktree inside a git repository" },
108 { GOT_ERR_FILE_MODIFIED,"file contains modifications" },
109 { GOT_ERR_FILE_STATUS, "file has unexpected status" },
110 { GOT_ERR_COMMIT_CONFLICT,"cannot commit file in conflicted status" },
111 { GOT_ERR_BAD_REF_TYPE, "bad reference type" },
112 { GOT_ERR_COMMIT_NO_AUTHOR,"GOT_AUTHOR environment variable is not set" },
113 { GOT_ERR_COMMIT_HEAD_CHANGED, "branch head in repository has changed "
114 "while commit was in progress" },
115 { GOT_ERR_COMMIT_OUT_OF_DATE, "work tree must be updated before these "
116 "changes can be committed" },
117 { GOT_ERR_COMMIT_MSG_EMPTY, "commit message cannot be empty" },
118 { GOT_ERR_DIR_NOT_EMPTY, "directory exists and is not empty" },
119 { GOT_ERR_COMMIT_NO_CHANGES, "no changes to commit" },
120 { GOT_ERR_BRANCH_MOVED, "work tree's head reference now points to a "
121 "different branch; new head reference and/or update -b required" },
122 { GOT_ERR_OBJ_TOO_LARGE, "object too large" },
123 { GOT_ERR_SAME_BRANCH, "commit is already contained in this branch" },
124 { GOT_ERR_ROOT_COMMIT, "specified commit has no parent commit" },
125 { GOT_ERR_MIXED_COMMITS,"work tree contains files from multiple "
126 "base commits; the entire work tree must be updated first" },
127 { GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
128 "conflicts must be resolved first" },
129 { GOT_ERR_BRANCH_EXISTS,"specified branch already exists" },
130 { GOT_ERR_MODIFIED, "work tree contains local changes; these "
131 "changes must be committed or reverted first" },
132 { GOT_ERR_NOT_REBASING, "rebase operation not in progress" },
133 { GOT_ERR_REBASE_COMMITID,"rebase commit ID mismatch" },
134 { GOT_ERR_REBASING, "a rebase operation is in progress in this "
135 "work tree and must be continued or aborted first" },
136 { GOT_ERR_REBASE_PATH, "cannot rebase branch which contains "
137 "changes outside of this work tree's path prefix" },
138 { GOT_ERR_NOT_HISTEDIT, "histedit operation not in progress" },
139 { GOT_ERR_EMPTY_HISTEDIT,"no commits to edit; perhaps the work tree "
140 "must be updated to an older commit first" },
141 { GOT_ERR_NO_HISTEDIT_CMD,"no histedit commands provided" },
142 { GOT_ERR_HISTEDIT_SYNTAX,"syntax error in histedit command list" },
143 { GOT_ERR_HISTEDIT_CANCEL,"histedit operation cancelled" },
144 { 95, "unused error code" },
145 { GOT_ERR_HISTEDIT_BUSY,"histedit operation is in progress in this "
146 "work tree and must be continued or aborted first" },
147 { GOT_ERR_HISTEDIT_CMD, "bad histedit command" },
148 { GOT_ERR_HISTEDIT_PATH, "cannot edit branch history which contains "
149 "changes outside of this work tree's path prefix" },
150 { GOT_ERR_PACKFILE_CSUM, "pack file checksum error" },
151 { GOT_ERR_COMMIT_BRANCH, "will not commit to a branch outside the "
152 "\"refs/heads/\" reference namespace" },
153 { GOT_ERR_FILE_STAGED, "file is staged" },
154 { GOT_ERR_STAGE_NO_CHANGE, "no changes to stage" },
155 { GOT_ERR_STAGE_CONFLICT, "cannot stage file in conflicted status" },
156 { GOT_ERR_STAGE_OUT_OF_DATE, "work tree must be updated before "
157 "changes can be staged" },
158 { GOT_ERR_FILE_NOT_STAGED, "file is not staged" },
159 { GOT_ERR_STAGED_PATHS, "work tree contains files with staged "
160 "changes; these changes must be committed or unstaged first" },
161 { GOT_ERR_PATCH_CHOICE, "invalid patch choice" },
162 { GOT_ERR_COMMIT_NO_EMAIL, "commit author's email address is required "
163 "for compatibility with Git" },
164 { GOT_ERR_TAG_EXISTS,"specified tag already exists" },
165 { GOT_ERR_GIT_REPO_FORMAT,"unknown git repository format version" },
166 { GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
167 { GOT_ERR_REGEX, "regular expression error" },
168 { GOT_ERR_REF_NAME_MINUS, "reference name may not start with '-'" },
169 { GOT_ERR_GITCONFIG_SYNTAX, "gitconfig syntax error" },
170 { GOT_ERR_REBASE_OUT_OF_DATE, "work tree must be updated before it "
171 "can be used to rebase a branch" },
172 { GOT_ERR_CACHE_DUP_ENTRY, "duplicate cache entry" },
173 { GOT_ERR_FETCH_FAILED, "fetch failed" },
174 { GOT_ERR_PARSE_URI, "failed to parse uri" },
175 { GOT_ERR_BAD_PROTO, "unknown protocol" },
176 { GOT_ERR_ADDRINFO, "getaddrinfo failed" },
177 { GOT_ERR_BAD_PACKET, "bad packet received" },
178 { GOT_ERR_NO_REMOTE, "remote repository not found" },
179 { GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
180 { GOT_ERR_FETCH_BAD_REF, "reference cannot be fetched" },
181 { GOT_ERR_TREE_ENTRY_TYPE, "unexpected tree entry type" },
182 { GOT_ERR_PARSE_CONFIG, "configuration file syntax error" },
183 { GOT_ERR_NO_CONFIG_FILE, "configuration file doesn't exit" },
184 { GOT_ERR_BAD_SYMLINK, "symbolic link points outside of paths under "
185 "version control" },
186 { GOT_ERR_GIT_REPO_EXT, "unsupported repository format extension" },
187 { GOT_ERR_CANNOT_PACK, "not enough objects to pack" },
188 { GOT_ERR_LONELY_PACKIDX, "pack index has no corresponding pack file; "
189 "pack file must be restored or 'gotadmin cleanup -p' must be run" },
190 { GOT_ERR_OBJ_CSUM, "bad object checksum" },
191 { GOT_ERR_SEND_BAD_REF, "reference cannot be sent" },
192 { GOT_ERR_SEND_FAILED, "could not send pack file" },
193 { GOT_ERR_SEND_EMPTY, "no references to send" },
194 { GOT_ERR_SEND_ANCESTRY, "fetch and rebase required" },
195 { GOT_ERR_CAPA_DELETE_REFS, "server cannot delete references" },
196 { GOT_ERR_SEND_DELETE_REF, "reference cannot be deleted" },
197 { GOT_ERR_SEND_TAG_EXISTS, "tag already exists on server" },
198 { GOT_ERR_NOT_MERGING, "merge operation not in progress" },
199 { GOT_ERR_MERGE_OUT_OF_DATE, "work tree must be updated before it "
200 "can be used to merge a branch" },
201 { GOT_ERR_MERGE_STAGED_PATHS, "work tree contains files with staged "
202 "changes; these changes must be unstaged before merging can "
203 "proceed" },
204 { GOT_ERR_MERGE_BUSY,"a merge operation is in progress in this "
205 "work tree and must be continued or aborted first" },
206 { GOT_ERR_MERGE_PATH, "cannot merge branch which contains "
207 "changes outside of this work tree's path prefix" },
208 { GOT_ERR_FILE_BINARY, "found a binary file instead of text" },
209 { GOT_ERR_PATCH_MALFORMED, "malformed patch" },
210 { GOT_ERR_PATCH_TRUNCATED, "patch truncated" },
211 { GOT_ERR_NO_PATCH, "no patch found" },
212 { GOT_ERR_HUNK_FAILED, "hunk failed to apply" },
213 { GOT_ERR_PATCH_FAILED, "patch failed to apply" },
214 { GOT_ERR_FILEIDX_DUP_ENTRY, "duplicate file index entry" },
215 { GOT_ERR_PIN_PACK, "could not pin pack file" },
216 { GOT_ERR_BAD_TAG_SIGNATURE, "invalid tag signature" },
217 { GOT_ERR_VERIFY_TAG_SIGNATURE, "cannot verify signature" },
218 { GOT_ERR_SIGNING_TAG, "unable to sign tag" },
219 { GOT_ERR_BAD_OPTION, "option cannot be used" },
220 { GOT_ERR_BAD_QUERYSTRING, "invalid query string" },
221 { GOT_ERR_INTEGRATE_BRANCH, "will not integrate into a reference "
222 "outside the \"refs/heads/\" reference namespace" },
223 { GOT_ERR_BAD_REQUEST, "unexpected request received" },
224 { GOT_ERR_CLIENT_ID, "unknown client identifier" },
225 { GOT_ERR_REPO_TEMPFILE, "no repository tempfile available" },
226 { GOT_ERR_REFS_PROTECTED, "reference namespace is protected" },
227 { GOT_ERR_REF_PROTECTED, "reference is protected" },
228 { GOT_ERR_REF_BUSY, "reference cannot be updated; please try again" },
229 { GOT_ERR_COMMIT_BAD_AUTHOR, "commit author formatting would "
230 "make Git unhappy" },
231 { GOT_ERR_UID, "bad user ID" },
232 { GOT_ERR_GID, "bad group ID" },
233 { GOT_ERR_NO_PROG, "command not found or not accessible" },
234 { GOT_ERR_MERGE_COMMIT_OUT_OF_DATE, "merging cannot proceed because "
235 "the work tree is no longer up-to-date; merge must be aborted "
236 "and retried" },
237 { GOT_ERR_BUNDLE_FORMAT, "unknown git bundle version" },
238 { GOT_ERR_BAD_KEYWORD, "invalid commit keyword" }
239 };
241 static struct got_custom_error {
242 struct got_error err;
243 char msg[GOT_ERR_MAX_MSG_SIZE];
244 } custom_errors[16];
246 static struct got_custom_error *
247 get_custom_err(void)
249 static unsigned int idx;
250 return &custom_errors[(idx++) % nitems(custom_errors)];
253 const struct got_error *
254 got_error(int code)
256 size_t i;
258 for (i = 0; i < nitems(got_errors); i++) {
259 if (code == got_errors[i].code)
260 return &got_errors[i];
263 abort();
266 const struct got_error *
267 got_error_msg(int code, const char *msg)
269 struct got_custom_error *cerr = get_custom_err();
270 struct got_error *err = &cerr->err;
271 size_t i;
273 for (i = 0; i < nitems(got_errors); i++) {
274 if (code == got_errors[i].code) {
275 err->code = code;
276 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
277 err->msg = cerr->msg;
278 return err;
282 abort();
285 const struct got_error *
286 got_error_from_errno(const char *prefix)
288 struct got_custom_error *cerr = get_custom_err();
289 struct got_error *err = &cerr->err;
290 char strerr[128];
292 strerror_r(errno, strerr, sizeof(strerr));
293 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
295 err->code = GOT_ERR_ERRNO;
296 err->msg = cerr->msg;
297 return err;
300 const struct got_error *
301 got_error_from_errno2(const char *prefix, const char *prefix2)
303 struct got_custom_error *cerr = get_custom_err();
304 struct got_error *err = &cerr->err;
305 char strerr[128];
307 strerror_r(errno, strerr, sizeof(strerr));
308 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
309 strerr);
311 err->code = GOT_ERR_ERRNO;
312 err->msg = cerr->msg;
313 return err;
316 const struct got_error *
317 got_error_from_errno3(const char *prefix, const char *prefix2,
318 const char *prefix3)
320 struct got_custom_error *cerr = get_custom_err();
321 struct got_error *err = &cerr->err;
322 char strerr[128];
324 strerror_r(errno, strerr, sizeof(strerr));
325 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
326 prefix2, prefix3, strerr);
328 err->code = GOT_ERR_ERRNO;
329 err->msg = cerr->msg;
330 return err;
333 const struct got_error *
334 got_error_from_errno_fmt(const char *fmt, ...)
336 struct got_custom_error *cerr = get_custom_err();
337 struct got_error *err = &cerr->err;
338 char buf[PATH_MAX * 4];
339 char strerr[128];
340 va_list ap;
342 va_start(ap, fmt);
343 vsnprintf(buf, sizeof(buf), fmt, ap);
344 va_end(ap);
346 strerror_r(errno, strerr, sizeof(strerr));
347 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
349 err->code = GOT_ERR_ERRNO;
350 err->msg = cerr->msg;
351 return err;
354 const struct got_error *
355 got_error_set_errno(int code, const char *prefix)
357 errno = code;
358 return got_error_from_errno(prefix);
361 const struct got_error *
362 got_ferror(FILE *f, int code)
364 if (ferror(f))
365 return got_error_from_errno("");
366 return got_error(code);
369 const struct got_error *
370 got_error_no_obj(struct got_object_id *id)
372 char id_str[GOT_OBJECT_ID_HEX_MAXLEN];
373 char msg[sizeof("object not found") + sizeof(id_str)];
374 int ret;
376 if (!got_object_id_hex(id, id_str, sizeof(id_str)))
377 return got_error(GOT_ERR_NO_OBJ);
379 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
380 if (ret < 0 || (size_t)ret >= sizeof(msg))
381 return got_error(GOT_ERR_NO_OBJ);
383 return got_error_msg(GOT_ERR_NO_OBJ, msg);
386 const struct got_error *
387 got_error_checksum(struct got_object_id *id)
389 char id_str[GOT_OBJECT_ID_HEX_MAXLEN];
390 char msg[sizeof("checksum failure for object ") + sizeof(id_str)];
391 int ret;
393 if (!got_object_id_hex(id, id_str, sizeof(id_str)))
394 return got_error(GOT_ERR_OBJ_CSUM);
396 ret = snprintf(msg, sizeof(msg), "checksum failure for object %s",
397 id_str);
398 if (ret < 0 || (size_t)ret >= sizeof(msg))
399 return got_error(GOT_ERR_OBJ_CSUM);
401 return got_error_msg(GOT_ERR_OBJ_CSUM, msg);
404 const struct got_error *
405 got_error_not_ref(const char *refname)
407 char msg[sizeof("reference not found") + 1004];
408 int ret;
410 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
411 if (ret < 0 || (size_t)ret >= sizeof(msg))
412 return got_error(GOT_ERR_NOT_REF);
414 return got_error_msg(GOT_ERR_NOT_REF, msg);
417 const struct got_error *
418 got_error_uuid(uint32_t uuid_status, const char *prefix)
420 switch (uuid_status) {
421 case uuid_s_ok:
422 return NULL;
423 case uuid_s_bad_version:
424 return got_error(GOT_ERR_UUID_VERSION);
425 case uuid_s_invalid_string_uuid:
426 return got_error(GOT_ERR_UUID_INVALID);
427 case uuid_s_no_memory:
428 return got_error_set_errno(ENOMEM, prefix);
429 default:
430 return got_error(GOT_ERR_UUID);
434 const struct got_error *
435 got_error_path(const char *path, int code)
437 struct got_custom_error *cerr = get_custom_err();
438 struct got_error *err = &cerr->err;
439 size_t i;
441 for (i = 0; i < nitems(got_errors); i++) {
442 if (code == got_errors[i].code) {
443 err->code = code;
444 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
445 got_errors[i].msg);
446 err->msg = cerr->msg;
447 return err;
451 abort();
454 const struct got_error *
455 got_error_fmt(int code, const char *fmt, ...)
457 struct got_custom_error *cerr = get_custom_err();
458 struct got_error *err = &cerr->err;
459 char buf[PATH_MAX * 4];
460 va_list ap;
461 size_t i;
463 va_start(ap, fmt);
464 vsnprintf(buf, sizeof(buf), fmt, ap);
465 va_end(ap);
467 for (i = 0; i < nitems(got_errors); i++) {
468 if (code == got_errors[i].code) {
469 err->code = code;
470 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
471 got_errors[i].msg);
472 err->msg = cerr->msg;
473 return err;
477 abort();
480 int
481 got_err_open_nofollow_on_symlink(void)
483 /*
484 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
485 * Posix mandates ELOOP and OpenBSD follows it. Others return
486 * different error codes. We carry this workaround to help the
487 * portable version a little.
488 */
489 return (errno == ELOOP
490 #ifdef EMLINK
491 || errno == EMLINK
492 #endif
493 #ifdef EFTYPE
494 || errno == EFTYPE
495 #endif
496 );