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 */
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <zlib.h>
26 #include "got_compat.h"
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_lib_delta.h"
32 #include "got_lib_inflate.h"
33 #include "got_lib_object.h"
34 #include "got_lib_sha1.h"
36 #ifndef nitems
37 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 #endif
40 #if defined(__GLIBC__)
41 /*
42 * The autoconf test for strerror_r is broken in current versions
43 * of autoconf: https://savannah.gnu.org/support/?110367
44 */
45 #define strerror_r __xpg_strerror_r
46 #endif
48 static const struct got_error got_errors[] = {
49 { GOT_ERR_OK, "no error occured?!?" },
50 { GOT_ERR_ERRNO, "see errno" },
51 { GOT_ERR_NOT_GIT_REPO, "no git repository found" },
52 { GOT_ERR_BAD_FILETYPE, "bad file type" },
53 { GOT_ERR_BAD_PATH, "bad path" },
54 { GOT_ERR_NOT_REF, "no such reference found" },
55 { GOT_ERR_IO, "input/output error" },
56 { GOT_ERR_EOF, "unexpected end of file" },
57 { GOT_ERR_DECOMPRESSION,"decompression failed" },
58 { GOT_ERR_NO_SPACE, "buffer too small" },
59 { GOT_ERR_BAD_OBJ_HDR, "bad object header" },
60 { GOT_ERR_OBJ_TYPE, "wrong type of object" },
61 { GOT_ERR_BAD_OBJ_DATA, "bad object data" },
62 { GOT_ERR_AMBIGUOUS_ID, "ambiguous object ID" },
63 { GOT_ERR_BAD_PACKIDX, "bad pack index file" },
64 { GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
65 { GOT_ERR_BAD_PACKFILE, "bad pack file" },
66 { GOT_ERR_NO_OBJ, "object not found" },
67 { GOT_ERR_NOT_IMPL, "feature not implemented" },
68 { GOT_ERR_OBJ_NOT_PACKED,"object is not packed" },
69 { GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" },
70 { GOT_ERR_BAD_DELTA, "bad delta" },
71 { GOT_ERR_COMPRESSION, "compression failed" },
72 { GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
73 { GOT_ERR_WORKTREE_EXISTS,"worktree already exists" },
74 { GOT_ERR_WORKTREE_META,"bad worktree meta data" },
75 { GOT_ERR_WORKTREE_VERS,"unsupported worktree format version" },
76 { GOT_ERR_WORKTREE_BUSY,"worktree already locked" },
77 { GOT_ERR_FILE_OBSTRUCTED,"file is obstructed" },
78 { GOT_ERR_RECURSION, "recursion limit reached" },
79 { GOT_ERR_TIMEOUT, "operation timed out" },
80 { GOT_ERR_INTERRUPT, "operation interrupted" },
81 { GOT_ERR_PRIVSEP_READ, "no data received in imsg" },
82 { GOT_ERR_PRIVSEP_LEN, "unexpected amount of data received in imsg" },
83 { GOT_ERR_PRIVSEP_PIPE, "privsep peer process closed pipe" },
84 { GOT_ERR_PRIVSEP_NO_FD,"privsep file descriptor unavailable" },
85 { GOT_ERR_PRIVSEP_MSG, "received unexpected privsep message" },
86 { GOT_ERR_PRIVSEP_DIED, "unprivileged process died unexpectedly" },
87 { GOT_ERR_PRIVSEP_EXIT, "bad exit code from unprivileged process" },
88 { GOT_ERR_PACK_OFFSET, "bad offset in pack file" },
89 { GOT_ERR_OBJ_EXISTS, "object already exists" },
90 { GOT_ERR_BAD_OBJ_ID, "bad object id" },
91 { GOT_ERR_ITER_BUSY, "iteration already in progress" },
92 { GOT_ERR_ITER_COMPLETED,"iteration completed" },
93 { GOT_ERR_RANGE, "value out of range" },
94 { GOT_ERR_EXPECTED, "expected an error but have no error" },
95 { GOT_ERR_CANCELLED, "operation in progress has been cancelled" },
96 { GOT_ERR_NO_TREE_ENTRY,"no such entry found in tree" },
97 { GOT_ERR_FILEIDX_SIG, "bad file index signature" },
98 { GOT_ERR_FILEIDX_VER, "unknown file index format version" },
99 { GOT_ERR_FILEIDX_CSUM, "bad file index checksum" },
100 { GOT_ERR_PATH_PREFIX, "worktree already contains items from a "
101 "different path prefix" },
102 { GOT_ERR_ANCESTRY, "target commit is on a different branch" },
103 { GOT_ERR_FILEIDX_BAD, "file index is corrupt" },
104 { GOT_ERR_BAD_REF_DATA, "could not parse reference data" },
105 { GOT_ERR_TREE_DUP_ENTRY,"duplicate entry in tree object" },
106 { GOT_ERR_DIR_DUP_ENTRY,"duplicate entry in directory" },
107 { GOT_ERR_NOT_WORKTREE, "no got work tree found" },
108 { GOT_ERR_UUID_VERSION, "bad uuid version" },
109 { GOT_ERR_UUID_INVALID, "uuid invalid" },
110 { GOT_ERR_UUID, "uuid error" },
111 { GOT_ERR_LOCKFILE_TIMEOUT,"lockfile timeout" },
112 { GOT_ERR_BAD_REF_NAME, "bad reference name" },
113 { GOT_ERR_WORKTREE_REPO,"cannot create worktree inside a git repository" },
114 { GOT_ERR_FILE_MODIFIED,"file contains modifications" },
115 { GOT_ERR_FILE_STATUS, "file has unexpected status" },
116 { GOT_ERR_COMMIT_CONFLICT,"cannot commit file in conflicted status" },
117 { GOT_ERR_BAD_REF_TYPE, "bad reference type" },
118 { GOT_ERR_COMMIT_NO_AUTHOR,"GOT_AUTHOR environment variable is not set" },
119 { GOT_ERR_COMMIT_HEAD_CHANGED, "branch head in repository has changed "
120 "while commit was in progress" },
121 { GOT_ERR_COMMIT_OUT_OF_DATE, "work tree must be updated before these "
122 "changes can be committed" },
123 { GOT_ERR_COMMIT_MSG_EMPTY, "commit message cannot be empty" },
124 { GOT_ERR_DIR_NOT_EMPTY, "directory exists and is not empty" },
125 { GOT_ERR_COMMIT_NO_CHANGES, "no changes to commit" },
126 { GOT_ERR_BRANCH_MOVED, "work tree's head reference now points to a "
127 "different branch; new head reference and/or update -b required" },
128 { GOT_ERR_OBJ_TOO_LARGE, "object too large" },
129 { GOT_ERR_SAME_BRANCH, "commit is already contained in this branch" },
130 { GOT_ERR_ROOT_COMMIT, "specified commit has no parent commit" },
131 { GOT_ERR_MIXED_COMMITS,"work tree contains files from multiple "
132 "base commits; the entire work tree must be updated first" },
133 { GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
134 "conflicts must be resolved first" },
135 { GOT_ERR_BRANCH_EXISTS,"specified branch already exists" },
136 { GOT_ERR_MODIFIED, "work tree contains local changes; these "
137 "changes must be committed or reverted first" },
138 { GOT_ERR_NOT_REBASING, "rebase operation not in progress" },
139 { GOT_ERR_EMPTY_REBASE, "no commits to rebase" },
140 { GOT_ERR_REBASE_COMMITID,"rebase commit ID mismatch" },
141 { GOT_ERR_REBASING, "a rebase operation is in progress in this "
142 "work tree and must be continued or aborted first" },
143 { GOT_ERR_REBASE_PATH, "cannot rebase branch which contains "
144 "changes outside of this work tree's path prefix" },
145 { GOT_ERR_NOT_HISTEDIT, "histedit operation not in progress" },
146 { GOT_ERR_EMPTY_HISTEDIT,"no commits to edit; perhaps the work tree "
147 "must be updated to an older commit first" },
148 { GOT_ERR_NO_HISTEDIT_CMD,"no histedit commands provided" },
149 { GOT_ERR_HISTEDIT_SYNTAX,"syntax error in histedit command list" },
150 { GOT_ERR_HISTEDIT_CANCEL,"histedit operation cancelled" },
151 { 95, "unused error code" },
152 { GOT_ERR_HISTEDIT_BUSY,"histedit operation is in progress in this "
153 "work tree and must be continued or aborted first" },
154 { GOT_ERR_HISTEDIT_CMD, "bad histedit command" },
155 { GOT_ERR_HISTEDIT_PATH, "cannot edit branch history which contains "
156 "changes outside of this work tree's path prefix" },
157 { 99, "unused error code" },
158 { GOT_ERR_COMMIT_BRANCH, "will not commit to a branch outside the "
159 "\"refs/heads/\" reference namespace" },
160 { GOT_ERR_FILE_STAGED, "file is staged" },
161 { GOT_ERR_STAGE_NO_CHANGE, "no changes to stage" },
162 { GOT_ERR_STAGE_CONFLICT, "cannot stage file in conflicted status" },
163 { GOT_ERR_STAGE_OUT_OF_DATE, "work tree must be updated before "
164 "changes can be staged" },
165 { GOT_ERR_FILE_NOT_STAGED, "file is not staged" },
166 { GOT_ERR_STAGED_PATHS, "work tree contains files with staged "
167 "changes; these changes must be committed or unstaged first" },
168 { GOT_ERR_PATCH_CHOICE, "invalid patch choice" },
169 { GOT_ERR_COMMIT_NO_EMAIL, "commit author's email address is required "
170 "for compatibility with Git" },
171 { GOT_ERR_TAG_EXISTS,"specified tag already exists" },
172 { GOT_ERR_GIT_REPO_FORMAT,"unknown git repository format version" },
173 { GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
174 { GOT_ERR_REGEX, "regular expression error" },
175 { GOT_ERR_REF_NAME_MINUS, "reference name may not start with '-'" },
176 { GOT_ERR_GITCONFIG_SYNTAX, "gitconfig syntax error" },
177 { GOT_ERR_REBASE_OUT_OF_DATE, "work tree must be updated before it "
178 "can be used to rebase a branch" },
179 { GOT_ERR_CACHE_DUP_ENTRY, "duplicate cache entry" },
180 { GOT_ERR_QUERYSTRING, "bad querystring" },
181 { GOT_ERR_FETCH_FAILED, "fetch failed" },
182 { GOT_ERR_PARSE_URI, "failed to parse uri" },
183 { GOT_ERR_BAD_PROTO, "unknown protocol" },
184 { GOT_ERR_ADDRINFO, "getaddrinfo failed" },
185 { GOT_ERR_BAD_PACKET, "bad packet received" },
186 { GOT_ERR_NO_REMOTE, "remote repository not found" },
187 { GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
188 { GOT_ERR_FETCH_BAD_REF, "reference cannot be fetched" },
189 { GOT_ERR_TREE_ENTRY_TYPE, "unexpected tree entry type" },
190 { GOT_ERR_PARSE_CONFIG, "configuration file syntax error" },
191 { GOT_ERR_NO_CONFIG_FILE, "configuration file doesn't exit" },
192 { GOT_ERR_BAD_SYMLINK, "symbolic link points outside of paths under "
193 "version control" },
194 { GOT_ERR_GIT_REPO_EXT, "unsupported repository format extension" },
195 { GOT_ERR_CANNOT_PACK, "not enough objects to pack" },
196 { GOT_ERR_LONELY_PACKIDX, "pack index has no corresponding pack file; "
197 "pack file must be restored or 'gotadmin cleanup -p' must be run" },
198 { GOT_ERR_OBJ_CSUM, "bad object checksum" },
199 { GOT_ERR_SEND_BAD_REF, "reference cannot be sent" },
200 { GOT_ERR_SEND_FAILED, "could not send pack file" },
201 { GOT_ERR_SEND_EMPTY, "no references to send" },
202 { GOT_ERR_SEND_ANCESTRY, "fetch and rebase required" },
203 { GOT_ERR_CAPA_DELETE_REFS, "server cannot delete references" },
204 { GOT_ERR_SEND_DELETE_REF, "reference cannot be deleted" },
205 { GOT_ERR_SEND_TAG_EXISTS, "tag already exists on server" },
206 { GOT_ERR_NOT_MERGING, "merge operation not in progress" },
207 { GOT_ERR_MERGE_OUT_OF_DATE, "work tree must be updated before it "
208 "can be used to merge a branch" },
209 { GOT_ERR_MERGE_STAGED_PATHS, "work tree contains files with staged "
210 "changes; these changes must be unstaged before merging can "
211 "proceed" },
212 { GOT_ERR_MERGE_COMMIT_OUT_OF_DATE, "merging cannot proceed because "
213 "the work tree is no longer up-to-date; merge must be aborted "
214 "and retried" },
215 { GOT_ERR_MERGE_BUSY,"a merge operation is in progress in this "
216 "work tree and must be continued or aborted first" },
217 { GOT_ERR_MERGE_PATH, "cannot merge branch which contains "
218 "changes outside of this work tree's path prefix" },
219 { GOT_ERR_FILE_BINARY, "found a binary file instead of text" },
220 { GOT_ERR_PATCH_MALFORMED, "malformed patch" },
221 { GOT_ERR_PATCH_TRUNCATED, "patch truncated" },
222 { GOT_ERR_NO_PATCH, "no patch found" },
223 { GOT_ERR_HUNK_FAILED, "hunk failed to apply" },
224 { GOT_ERR_PATCH_FAILED, "patch failed to apply" },
225 { GOT_ERR_FILEIDX_DUP_ENTRY, "duplicate file index entry" },
226 };
228 static struct got_custom_error {
229 struct got_error err;
230 char msg[4080];
231 } custom_errors[16];
233 static struct got_custom_error *
234 get_custom_err(void)
236 static unsigned int idx;
237 return &custom_errors[(idx++) % nitems(custom_errors)];
240 const struct got_error *
241 got_error(int code)
243 size_t i;
245 for (i = 0; i < nitems(got_errors); i++) {
246 if (code == got_errors[i].code)
247 return &got_errors[i];
250 abort();
253 const struct got_error *
254 got_error_msg(int code, const char *msg)
256 struct got_custom_error *cerr = get_custom_err();
257 struct got_error *err = &cerr->err;
258 size_t i;
260 for (i = 0; i < nitems(got_errors); i++) {
261 if (code == got_errors[i].code) {
262 err->code = code;
263 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
264 err->msg = cerr->msg;
265 return err;
269 abort();
272 const struct got_error *
273 got_error_from_errno(const char *prefix)
275 struct got_custom_error *cerr = get_custom_err();
276 struct got_error *err = &cerr->err;
277 char strerr[128];
279 strerror_r(errno, strerr, sizeof(strerr));
280 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
282 err->code = GOT_ERR_ERRNO;
283 err->msg = cerr->msg;
284 return err;
287 const struct got_error *
288 got_error_from_errno2(const char *prefix, const char *prefix2)
290 struct got_custom_error *cerr = get_custom_err();
291 struct got_error *err = &cerr->err;
292 char strerr[128];
294 strerror_r(errno, strerr, sizeof(strerr));
295 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
296 strerr);
298 err->code = GOT_ERR_ERRNO;
299 err->msg = cerr->msg;
300 return err;
303 const struct got_error *
304 got_error_from_errno3(const char *prefix, const char *prefix2,
305 const char *prefix3)
307 struct got_custom_error *cerr = get_custom_err();
308 struct got_error *err = &cerr->err;
309 char strerr[128];
311 strerror_r(errno, strerr, sizeof(strerr));
312 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
313 prefix2, prefix3, strerr);
315 err->code = GOT_ERR_ERRNO;
316 err->msg = cerr->msg;
317 return err;
320 const struct got_error *
321 got_error_from_errno_fmt(const char *fmt, ...)
323 struct got_custom_error *cerr = get_custom_err();
324 struct got_error *err = &cerr->err;
325 char buf[PATH_MAX * 4];
326 char strerr[128];
327 va_list ap;
329 va_start(ap, fmt);
330 vsnprintf(buf, sizeof(buf), fmt, ap);
331 va_end(ap);
333 strerror_r(errno, strerr, sizeof(strerr));
334 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
336 err->code = GOT_ERR_ERRNO;
337 err->msg = cerr->msg;
338 return err;
341 const struct got_error *
342 got_error_set_errno(int code, const char *prefix)
344 errno = code;
345 return got_error_from_errno(prefix);
348 const struct got_error *
349 got_ferror(FILE *f, int code)
351 if (ferror(f))
352 return got_error_from_errno("");
353 return got_error(code);
356 const struct got_error *
357 got_error_no_obj(struct got_object_id *id)
359 char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
360 char id_str[SHA1_DIGEST_STRING_LENGTH];
361 int ret;
363 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
364 return got_error(GOT_ERR_NO_OBJ);
366 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
367 if (ret == -1 || ret >= sizeof(msg))
368 return got_error(GOT_ERR_NO_OBJ);
370 return got_error_msg(GOT_ERR_NO_OBJ, msg);
373 const struct got_error *
374 got_error_not_ref(const char *refname)
376 char msg[sizeof("reference not found") + 1004];
377 int ret;
379 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
380 if (ret == -1 || ret >= sizeof(msg))
381 return got_error(GOT_ERR_NOT_REF);
383 return got_error_msg(GOT_ERR_NOT_REF, msg);
386 const struct got_error *
387 got_error_uuid(uint32_t uuid_status, const char *prefix)
389 switch (uuid_status) {
390 case uuid_s_ok:
391 return NULL;
392 case uuid_s_bad_version:
393 return got_error(GOT_ERR_UUID_VERSION);
394 case uuid_s_invalid_string_uuid:
395 return got_error(GOT_ERR_UUID_INVALID);
396 case uuid_s_no_memory:
397 return got_error_set_errno(ENOMEM, prefix);
398 default:
399 return got_error(GOT_ERR_UUID);
403 const struct got_error *
404 got_error_path(const char *path, int code)
406 struct got_custom_error *cerr = get_custom_err();
407 struct got_error *err = &cerr->err;
408 size_t i;
410 for (i = 0; i < nitems(got_errors); i++) {
411 if (code == got_errors[i].code) {
412 err->code = code;
413 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
414 got_errors[i].msg);
415 err->msg = cerr->msg;
416 return err;
420 abort();
423 const struct got_error *
424 got_error_fmt(int code, const char *fmt, ...)
426 struct got_custom_error *cerr = get_custom_err();
427 struct got_error *err = &cerr->err;
428 char buf[PATH_MAX * 4];
429 va_list ap;
430 size_t i;
432 va_start(ap, fmt);
433 vsnprintf(buf, sizeof(buf), fmt, ap);
434 va_end(ap);
436 for (i = 0; i < nitems(got_errors); i++) {
437 if (code == got_errors[i].code) {
438 err->code = code;
439 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
440 got_errors[i].msg);
441 err->msg = cerr->msg;
442 return err;
446 abort();
449 int
450 got_err_open_nofollow_on_symlink(void)
452 /*
453 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
454 * Posix mandates ELOOP and OpenBSD follows it. Others return
455 * different error codes. We carry this workaround to help the
456 * portable version a little.
457 */
458 return (errno == ELOOP
459 #ifdef EMLINK
460 || errno == EMLINK
461 #endif
462 #ifdef EFTYPE
463 || errno == EFTYPE
464 #endif
465 );