Blame


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