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 91a3d81f 2018-11-11 stsp
18 f334529e 2018-01-12 stsp #include <errno.h>
19 7d45c7f1 2019-05-15 stsp #include <limits.h>
20 4cc6a5a5 2020-12-15 stsp #include <stdarg.h>
21 8251fdbc 2018-01-12 stsp #include <stdio.h>
22 f334529e 2018-01-12 stsp #include <stdlib.h>
23 f334529e 2018-01-12 stsp #include <string.h>
24 91a3d81f 2018-11-11 stsp #include <zlib.h>
25 f334529e 2018-01-12 stsp
26 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
27 dd038bc6 2021-09-21 thomas.ad
28 4027f31a 2017-11-04 stsp #include "got_error.h"
29 91a3d81f 2018-11-11 stsp #include "got_object.h"
30 4027f31a 2017-11-04 stsp
31 91a3d81f 2018-11-11 stsp #include "got_lib_delta.h"
32 91a3d81f 2018-11-11 stsp #include "got_lib_inflate.h"
33 91a3d81f 2018-11-11 stsp #include "got_lib_object.h"
34 91a3d81f 2018-11-11 stsp #include "got_lib_sha1.h"
35 91a3d81f 2018-11-11 stsp
36 2b4402a2 2017-11-05 stsp #ifndef nitems
37 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 f0678b77 2021-09-21 thomas.ad #endif
39 f0678b77 2021-09-21 thomas.ad
40 f0678b77 2021-09-21 thomas.ad #if defined(__GLIBC__)
41 f0678b77 2021-09-21 thomas.ad /*
42 f0678b77 2021-09-21 thomas.ad * The autoconf test for strerror_r is broken in current versions
43 f0678b77 2021-09-21 thomas.ad * of autoconf: https://savannah.gnu.org/support/?110367
44 f0678b77 2021-09-21 thomas.ad */
45 f0678b77 2021-09-21 thomas.ad #define strerror_r __xpg_strerror_r
46 2b4402a2 2017-11-05 stsp #endif
47 4027f31a 2017-11-04 stsp
48 0349119b 2022-03-22 thomas static const struct got_error got_errors[] = {
49 0349119b 2022-03-22 thomas { GOT_ERR_OK, "no error occured?!?" },
50 0349119b 2022-03-22 thomas { GOT_ERR_ERRNO, "see errno" },
51 0349119b 2022-03-22 thomas { GOT_ERR_NOT_GIT_REPO, "no git repository found" },
52 0349119b 2022-03-22 thomas { GOT_ERR_BAD_FILETYPE, "bad file type" },
53 0349119b 2022-03-22 thomas { GOT_ERR_BAD_PATH, "bad path" },
54 0349119b 2022-03-22 thomas { GOT_ERR_NOT_REF, "no such reference found" },
55 0349119b 2022-03-22 thomas { GOT_ERR_IO, "input/output error" },
56 0349119b 2022-03-22 thomas { GOT_ERR_EOF, "unexpected end of file" },
57 0349119b 2022-03-22 thomas { GOT_ERR_DECOMPRESSION,"decompression failed" },
58 0349119b 2022-03-22 thomas { GOT_ERR_NO_SPACE, "buffer too small" },
59 0349119b 2022-03-22 thomas { GOT_ERR_BAD_OBJ_HDR, "bad object header" },
60 0349119b 2022-03-22 thomas { GOT_ERR_OBJ_TYPE, "wrong type of object" },
61 0349119b 2022-03-22 thomas { GOT_ERR_BAD_OBJ_DATA, "bad object data" },
62 0349119b 2022-03-22 thomas { GOT_ERR_AMBIGUOUS_ID, "ambiguous object ID" },
63 0349119b 2022-03-22 thomas { GOT_ERR_BAD_PACKIDX, "bad pack index file" },
64 0349119b 2022-03-22 thomas { GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
65 0349119b 2022-03-22 thomas { GOT_ERR_BAD_PACKFILE, "bad pack file" },
66 0349119b 2022-03-22 thomas { GOT_ERR_NO_OBJ, "object not found" },
67 0349119b 2022-03-22 thomas { GOT_ERR_NOT_IMPL, "feature not implemented" },
68 0349119b 2022-03-22 thomas { GOT_ERR_OBJ_NOT_PACKED,"object is not packed" },
69 0349119b 2022-03-22 thomas { GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" },
70 0349119b 2022-03-22 thomas { GOT_ERR_BAD_DELTA, "bad delta" },
71 0349119b 2022-03-22 thomas { GOT_ERR_COMPRESSION, "compression failed" },
72 0349119b 2022-03-22 thomas { GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
73 0349119b 2022-03-22 thomas { GOT_ERR_WORKTREE_EXISTS,"worktree already exists" },
74 0349119b 2022-03-22 thomas { GOT_ERR_WORKTREE_META,"bad worktree meta data" },
75 0349119b 2022-03-22 thomas { GOT_ERR_WORKTREE_VERS,"unsupported worktree format version" },
76 0349119b 2022-03-22 thomas { GOT_ERR_WORKTREE_BUSY,"worktree already locked" },
77 0349119b 2022-03-22 thomas { GOT_ERR_FILE_OBSTRUCTED,"file is obstructed" },
78 0349119b 2022-03-22 thomas { GOT_ERR_RECURSION, "recursion limit reached" },
79 0349119b 2022-03-22 thomas { GOT_ERR_TIMEOUT, "operation timed out" },
80 0349119b 2022-03-22 thomas { GOT_ERR_INTERRUPT, "operation interrupted" },
81 0349119b 2022-03-22 thomas { GOT_ERR_PRIVSEP_READ, "no data received in imsg" },
82 0349119b 2022-03-22 thomas { GOT_ERR_PRIVSEP_LEN, "unexpected amount of data received in imsg" },
83 0349119b 2022-03-22 thomas { GOT_ERR_PRIVSEP_PIPE, "privsep peer process closed pipe" },
84 0349119b 2022-03-22 thomas { GOT_ERR_PRIVSEP_NO_FD,"privsep file descriptor unavailable" },
85 0349119b 2022-03-22 thomas { GOT_ERR_PRIVSEP_MSG, "received unexpected privsep message" },
86 0349119b 2022-03-22 thomas { GOT_ERR_PRIVSEP_DIED, "unprivileged process died unexpectedly" },
87 0349119b 2022-03-22 thomas { GOT_ERR_PRIVSEP_EXIT, "bad exit code from unprivileged process" },
88 0349119b 2022-03-22 thomas { GOT_ERR_PACK_OFFSET, "bad offset in pack file" },
89 0349119b 2022-03-22 thomas { GOT_ERR_OBJ_EXISTS, "object already exists" },
90 0349119b 2022-03-22 thomas { GOT_ERR_BAD_OBJ_ID, "bad object id" },
91 0349119b 2022-03-22 thomas { GOT_ERR_ITER_BUSY, "iteration already in progress" },
92 0349119b 2022-03-22 thomas { GOT_ERR_ITER_COMPLETED,"iteration completed" },
93 0349119b 2022-03-22 thomas { GOT_ERR_RANGE, "value out of range" },
94 0349119b 2022-03-22 thomas { GOT_ERR_EXPECTED, "expected an error but have no error" },
95 0349119b 2022-03-22 thomas { GOT_ERR_CANCELLED, "operation in progress has been cancelled" },
96 0349119b 2022-03-22 thomas { GOT_ERR_NO_TREE_ENTRY,"no such entry found in tree" },
97 0349119b 2022-03-22 thomas { GOT_ERR_FILEIDX_SIG, "bad file index signature" },
98 0349119b 2022-03-22 thomas { GOT_ERR_FILEIDX_VER, "unknown file index format version" },
99 0349119b 2022-03-22 thomas { GOT_ERR_FILEIDX_CSUM, "bad file index checksum" },
100 0349119b 2022-03-22 thomas { GOT_ERR_PATH_PREFIX, "worktree already contains items from a "
101 0349119b 2022-03-22 thomas "different path prefix" },
102 0349119b 2022-03-22 thomas { GOT_ERR_ANCESTRY, "target commit is on a different branch" },
103 0349119b 2022-03-22 thomas { GOT_ERR_FILEIDX_BAD, "file index is corrupt" },
104 0349119b 2022-03-22 thomas { GOT_ERR_BAD_REF_DATA, "could not parse reference data" },
105 0349119b 2022-03-22 thomas { GOT_ERR_TREE_DUP_ENTRY,"duplicate entry in tree object" },
106 0349119b 2022-03-22 thomas { GOT_ERR_DIR_DUP_ENTRY,"duplicate entry in directory" },
107 0349119b 2022-03-22 thomas { GOT_ERR_NOT_WORKTREE, "no got work tree found" },
108 0349119b 2022-03-22 thomas { GOT_ERR_UUID_VERSION, "bad uuid version" },
109 0349119b 2022-03-22 thomas { GOT_ERR_UUID_INVALID, "uuid invalid" },
110 0349119b 2022-03-22 thomas { GOT_ERR_UUID, "uuid error" },
111 0349119b 2022-03-22 thomas { GOT_ERR_LOCKFILE_TIMEOUT,"lockfile timeout" },
112 0349119b 2022-03-22 thomas { GOT_ERR_BAD_REF_NAME, "bad reference name" },
113 0349119b 2022-03-22 thomas { GOT_ERR_WORKTREE_REPO,"cannot create worktree inside a git repository" },
114 0349119b 2022-03-22 thomas { GOT_ERR_FILE_MODIFIED,"file contains modifications" },
115 0349119b 2022-03-22 thomas { GOT_ERR_FILE_STATUS, "file has unexpected status" },
116 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_CONFLICT,"cannot commit file in conflicted status" },
117 0349119b 2022-03-22 thomas { GOT_ERR_BAD_REF_TYPE, "bad reference type" },
118 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_NO_AUTHOR,"GOT_AUTHOR environment variable is not set" },
119 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_HEAD_CHANGED, "branch head in repository has changed "
120 0349119b 2022-03-22 thomas "while commit was in progress" },
121 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_OUT_OF_DATE, "work tree must be updated before these "
122 0349119b 2022-03-22 thomas "changes can be committed" },
123 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_MSG_EMPTY, "commit message cannot be empty" },
124 0349119b 2022-03-22 thomas { GOT_ERR_DIR_NOT_EMPTY, "directory exists and is not empty" },
125 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_NO_CHANGES, "no changes to commit" },
126 0349119b 2022-03-22 thomas { GOT_ERR_BRANCH_MOVED, "work tree's head reference now points to a "
127 0349119b 2022-03-22 thomas "different branch; new head reference and/or update -b required" },
128 0349119b 2022-03-22 thomas { GOT_ERR_OBJ_TOO_LARGE, "object too large" },
129 0349119b 2022-03-22 thomas { GOT_ERR_SAME_BRANCH, "commit is already contained in this branch" },
130 0349119b 2022-03-22 thomas { GOT_ERR_ROOT_COMMIT, "specified commit has no parent commit" },
131 0349119b 2022-03-22 thomas { GOT_ERR_MIXED_COMMITS,"work tree contains files from multiple "
132 0349119b 2022-03-22 thomas "base commits; the entire work tree must be updated first" },
133 0349119b 2022-03-22 thomas { GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
134 0349119b 2022-03-22 thomas "conflicts must be resolved first" },
135 0349119b 2022-03-22 thomas { GOT_ERR_BRANCH_EXISTS,"specified branch already exists" },
136 0349119b 2022-03-22 thomas { GOT_ERR_MODIFIED, "work tree contains local changes; these "
137 0349119b 2022-03-22 thomas "changes must be committed or reverted first" },
138 0349119b 2022-03-22 thomas { GOT_ERR_NOT_REBASING, "rebase operation not in progress" },
139 0349119b 2022-03-22 thomas { GOT_ERR_EMPTY_REBASE, "no commits to rebase" },
140 0349119b 2022-03-22 thomas { GOT_ERR_REBASE_COMMITID,"rebase commit ID mismatch" },
141 0349119b 2022-03-22 thomas { GOT_ERR_REBASING, "a rebase operation is in progress in this "
142 0349119b 2022-03-22 thomas "work tree and must be continued or aborted first" },
143 0349119b 2022-03-22 thomas { GOT_ERR_REBASE_PATH, "cannot rebase branch which contains "
144 0349119b 2022-03-22 thomas "changes outside of this work tree's path prefix" },
145 0349119b 2022-03-22 thomas { GOT_ERR_NOT_HISTEDIT, "histedit operation not in progress" },
146 0349119b 2022-03-22 thomas { GOT_ERR_EMPTY_HISTEDIT,"no commits to edit; perhaps the work tree "
147 0349119b 2022-03-22 thomas "must be updated to an older commit first" },
148 0349119b 2022-03-22 thomas { GOT_ERR_NO_HISTEDIT_CMD,"no histedit commands provided" },
149 0349119b 2022-03-22 thomas { GOT_ERR_HISTEDIT_SYNTAX,"syntax error in histedit command list" },
150 0349119b 2022-03-22 thomas { GOT_ERR_HISTEDIT_CANCEL,"histedit operation cancelled" },
151 0349119b 2022-03-22 thomas { 95, "unused error code" },
152 0349119b 2022-03-22 thomas { GOT_ERR_HISTEDIT_BUSY,"histedit operation is in progress in this "
153 0349119b 2022-03-22 thomas "work tree and must be continued or aborted first" },
154 0349119b 2022-03-22 thomas { GOT_ERR_HISTEDIT_CMD, "bad histedit command" },
155 0349119b 2022-03-22 thomas { GOT_ERR_HISTEDIT_PATH, "cannot edit branch history which contains "
156 0349119b 2022-03-22 thomas "changes outside of this work tree's path prefix" },
157 0349119b 2022-03-22 thomas { 99, "unused error code" },
158 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_BRANCH, "will not commit to a branch outside the "
159 0349119b 2022-03-22 thomas "\"refs/heads/\" reference namespace" },
160 0349119b 2022-03-22 thomas { GOT_ERR_FILE_STAGED, "file is staged" },
161 0349119b 2022-03-22 thomas { GOT_ERR_STAGE_NO_CHANGE, "no changes to stage" },
162 0349119b 2022-03-22 thomas { GOT_ERR_STAGE_CONFLICT, "cannot stage file in conflicted status" },
163 0349119b 2022-03-22 thomas { GOT_ERR_STAGE_OUT_OF_DATE, "work tree must be updated before "
164 0349119b 2022-03-22 thomas "changes can be staged" },
165 0349119b 2022-03-22 thomas { GOT_ERR_FILE_NOT_STAGED, "file is not staged" },
166 0349119b 2022-03-22 thomas { GOT_ERR_STAGED_PATHS, "work tree contains files with staged "
167 0349119b 2022-03-22 thomas "changes; these changes must be committed or unstaged first" },
168 0349119b 2022-03-22 thomas { GOT_ERR_PATCH_CHOICE, "invalid patch choice" },
169 0349119b 2022-03-22 thomas { GOT_ERR_COMMIT_NO_EMAIL, "commit author's email address is required "
170 0349119b 2022-03-22 thomas "for compatibility with Git" },
171 0349119b 2022-03-22 thomas { GOT_ERR_TAG_EXISTS,"specified tag already exists" },
172 0349119b 2022-03-22 thomas { GOT_ERR_GIT_REPO_FORMAT,"unknown git repository format version" },
173 0349119b 2022-03-22 thomas { GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
174 0349119b 2022-03-22 thomas { GOT_ERR_REGEX, "regular expression error" },
175 0349119b 2022-03-22 thomas { GOT_ERR_REF_NAME_MINUS, "reference name may not start with '-'" },
176 0349119b 2022-03-22 thomas { GOT_ERR_GITCONFIG_SYNTAX, "gitconfig syntax error" },
177 0349119b 2022-03-22 thomas { GOT_ERR_REBASE_OUT_OF_DATE, "work tree must be updated before it "
178 0349119b 2022-03-22 thomas "can be used to rebase a branch" },
179 0349119b 2022-03-22 thomas { GOT_ERR_CACHE_DUP_ENTRY, "duplicate cache entry" },
180 0349119b 2022-03-22 thomas { GOT_ERR_QUERYSTRING, "bad querystring" },
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_COMMIT_OUT_OF_DATE, "merging cannot proceed because "
213 0349119b 2022-03-22 thomas "the work tree is no longer up-to-date; merge must be aborted "
214 0349119b 2022-03-22 thomas "and retried" },
215 0349119b 2022-03-22 thomas { GOT_ERR_MERGE_BUSY,"a merge operation is in progress in this "
216 0349119b 2022-03-22 thomas "work tree and must be continued or aborted first" },
217 0349119b 2022-03-22 thomas { GOT_ERR_MERGE_PATH, "cannot merge branch which contains "
218 0349119b 2022-03-22 thomas "changes outside of this work tree's path prefix" },
219 0349119b 2022-03-22 thomas { GOT_ERR_FILE_BINARY, "found a binary file instead of text" },
220 0349119b 2022-03-22 thomas { GOT_ERR_PATCH_MALFORMED, "malformed patch" },
221 0349119b 2022-03-22 thomas { GOT_ERR_PATCH_TRUNCATED, "patch truncated" },
222 0349119b 2022-03-22 thomas { GOT_ERR_NO_PATCH, "no patch found" },
223 0349119b 2022-03-22 thomas { GOT_ERR_HUNK_FAILED, "hunk failed to apply" },
224 0349119b 2022-03-22 thomas { GOT_ERR_PATCH_FAILED, "patch failed to apply" },
225 9fad5d8c 2022-04-16 thomas { GOT_ERR_FILEIDX_DUP_ENTRY, "duplicate file index entry" },
226 ec2b23c5 2022-07-01 thomas { GOT_ERR_PIN_PACK, "could not pin pack file" },
227 871bd038 2022-07-03 thomas { GOT_ERR_BAD_TAG_SIGNATURE, "invalid tag signature" },
228 871bd038 2022-07-03 thomas { GOT_ERR_VERIFY_TAG_SIGNATURE, "cannot verify signature" },
229 64313a9c 2022-07-03 thomas { GOT_ERR_SIGNING_TAG, "unable to sign tag" },
230 0349119b 2022-03-22 thomas };
231 0349119b 2022-03-22 thomas
232 c884fd0a 2020-12-21 stsp static struct got_custom_error {
233 c884fd0a 2020-12-21 stsp struct got_error err;
234 c884fd0a 2020-12-21 stsp char msg[4080];
235 c884fd0a 2020-12-21 stsp } custom_errors[16];
236 c884fd0a 2020-12-21 stsp
237 c884fd0a 2020-12-21 stsp static struct got_custom_error *
238 c884fd0a 2020-12-21 stsp get_custom_err(void)
239 c884fd0a 2020-12-21 stsp {
240 c884fd0a 2020-12-21 stsp static unsigned int idx;
241 c884fd0a 2020-12-21 stsp return &custom_errors[(idx++) % nitems(custom_errors)];
242 c884fd0a 2020-12-21 stsp }
243 c884fd0a 2020-12-21 stsp
244 4027f31a 2017-11-04 stsp const struct got_error *
245 4027f31a 2017-11-04 stsp got_error(int code)
246 4027f31a 2017-11-04 stsp {
247 16aeacf7 2020-11-26 stsp size_t i;
248 4027f31a 2017-11-04 stsp
249 19a6a6b5 2022-07-01 thomas if (code == GOT_ERR_PRIVSEP_LEN) abort();
250 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
251 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
252 4027f31a 2017-11-04 stsp return &got_errors[i];
253 4027f31a 2017-11-04 stsp }
254 4027f31a 2017-11-04 stsp
255 f334529e 2018-01-12 stsp abort();
256 4027f31a 2017-11-04 stsp }
257 f334529e 2018-01-12 stsp
258 f334529e 2018-01-12 stsp const struct got_error *
259 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
260 91a3d81f 2018-11-11 stsp {
261 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
262 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
263 16aeacf7 2020-11-26 stsp size_t i;
264 91a3d81f 2018-11-11 stsp
265 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
266 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
267 c884fd0a 2020-12-21 stsp err->code = code;
268 c884fd0a 2020-12-21 stsp strlcpy(cerr->msg, msg, sizeof(cerr->msg));
269 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
270 c884fd0a 2020-12-21 stsp return err;
271 91a3d81f 2018-11-11 stsp }
272 91a3d81f 2018-11-11 stsp }
273 91a3d81f 2018-11-11 stsp
274 91a3d81f 2018-11-11 stsp abort();
275 91a3d81f 2018-11-11 stsp }
276 91a3d81f 2018-11-11 stsp
277 91a3d81f 2018-11-11 stsp const struct got_error *
278 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
279 f334529e 2018-01-12 stsp {
280 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
281 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
282 9a02f8b7 2020-12-21 stsp char strerr[128];
283 f334529e 2018-01-12 stsp
284 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
285 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
286 230a42bd 2019-05-11 jcs
287 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
288 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
289 c884fd0a 2020-12-21 stsp return err;
290 f334529e 2018-01-12 stsp }
291 8251fdbc 2018-01-12 stsp
292 8251fdbc 2018-01-12 stsp const struct got_error *
293 638f9024 2019-05-13 stsp got_error_from_errno2(const char *prefix, const char *prefix2)
294 48b8b0eb 2019-05-11 jcs {
295 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
296 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
297 9a02f8b7 2020-12-21 stsp char strerr[128];
298 48b8b0eb 2019-05-11 jcs
299 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
300 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
301 9a02f8b7 2020-12-21 stsp strerr);
302 48b8b0eb 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 48b8b0eb 2019-05-11 jcs }
307 48b8b0eb 2019-05-11 jcs
308 48b8b0eb 2019-05-11 jcs const struct got_error *
309 638f9024 2019-05-13 stsp got_error_from_errno3(const char *prefix, const char *prefix2,
310 230a42bd 2019-05-11 jcs const char *prefix3)
311 230a42bd 2019-05-11 jcs {
312 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
313 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
314 9a02f8b7 2020-12-21 stsp char strerr[128];
315 230a42bd 2019-05-11 jcs
316 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
317 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
318 9a02f8b7 2020-12-21 stsp prefix2, prefix3, strerr);
319 230a42bd 2019-05-11 jcs
320 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
321 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
322 c884fd0a 2020-12-21 stsp return err;
323 230a42bd 2019-05-11 jcs }
324 230a42bd 2019-05-11 jcs
325 230a42bd 2019-05-11 jcs const struct got_error *
326 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
327 4cc6a5a5 2020-12-15 stsp {
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 4cc6a5a5 2020-12-15 stsp char buf[PATH_MAX * 4];
331 9a02f8b7 2020-12-21 stsp char strerr[128];
332 4cc6a5a5 2020-12-15 stsp va_list ap;
333 4cc6a5a5 2020-12-15 stsp
334 4cc6a5a5 2020-12-15 stsp va_start(ap, fmt);
335 4cc6a5a5 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
336 4cc6a5a5 2020-12-15 stsp va_end(ap);
337 4cc6a5a5 2020-12-15 stsp
338 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
339 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
340 4cc6a5a5 2020-12-15 stsp
341 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
342 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
343 c884fd0a 2020-12-21 stsp return err;
344 4cc6a5a5 2020-12-15 stsp }
345 4cc6a5a5 2020-12-15 stsp
346 4cc6a5a5 2020-12-15 stsp const struct got_error *
347 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
348 1a76625f 2018-10-22 stsp {
349 1a76625f 2018-10-22 stsp errno = code;
350 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
351 1a76625f 2018-10-22 stsp }
352 1a76625f 2018-10-22 stsp
353 1a76625f 2018-10-22 stsp const struct got_error *
354 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
355 8251fdbc 2018-01-12 stsp {
356 8251fdbc 2018-01-12 stsp if (ferror(f))
357 638f9024 2019-05-13 stsp return got_error_from_errno("");
358 8251fdbc 2018-01-12 stsp return got_error(code);
359 8251fdbc 2018-01-12 stsp }
360 91a3d81f 2018-11-11 stsp
361 91a3d81f 2018-11-11 stsp const struct got_error *
362 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
363 91a3d81f 2018-11-11 stsp {
364 c884fd0a 2020-12-21 stsp char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
365 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
366 91a3d81f 2018-11-11 stsp int ret;
367 91a3d81f 2018-11-11 stsp
368 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
369 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
370 91a3d81f 2018-11-11 stsp
371 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
372 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
373 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
374 91a3d81f 2018-11-11 stsp
375 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
376 91a3d81f 2018-11-11 stsp }
377 2aa0475c 2019-02-03 stsp
378 2aa0475c 2019-02-03 stsp const struct got_error *
379 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
380 2aa0475c 2019-02-03 stsp {
381 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
382 2aa0475c 2019-02-03 stsp int ret;
383 2aa0475c 2019-02-03 stsp
384 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
385 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
386 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
387 2aa0475c 2019-02-03 stsp
388 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
389 2aa0475c 2019-02-03 stsp }
390 09589288 2019-03-10 stsp
391 09589288 2019-03-10 stsp const struct got_error *
392 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
393 09589288 2019-03-10 stsp {
394 09589288 2019-03-10 stsp switch (uuid_status) {
395 09589288 2019-03-10 stsp case uuid_s_ok:
396 09589288 2019-03-10 stsp return NULL;
397 09589288 2019-03-10 stsp case uuid_s_bad_version:
398 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
399 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
400 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
401 09589288 2019-03-10 stsp case uuid_s_no_memory:
402 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
403 09589288 2019-03-10 stsp default:
404 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
405 09589288 2019-03-10 stsp }
406 09589288 2019-03-10 stsp }
407 df056ada 2019-05-15 stsp
408 df056ada 2019-05-15 stsp const struct got_error *
409 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
410 df056ada 2019-05-15 stsp {
411 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
412 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
413 16aeacf7 2020-11-26 stsp size_t i;
414 df056ada 2019-05-15 stsp
415 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
416 df056ada 2019-05-15 stsp if (code == got_errors[i].code) {
417 c884fd0a 2020-12-21 stsp err->code = code;
418 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
419 df056ada 2019-05-15 stsp got_errors[i].msg);
420 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
421 c884fd0a 2020-12-21 stsp return err;
422 df056ada 2019-05-15 stsp }
423 df056ada 2019-05-15 stsp }
424 df056ada 2019-05-15 stsp
425 df056ada 2019-05-15 stsp abort();
426 df056ada 2019-05-15 stsp }
427 73e7eb7d 2020-12-15 stsp
428 73e7eb7d 2020-12-15 stsp const struct got_error *
429 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
430 73e7eb7d 2020-12-15 stsp {
431 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
432 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
433 73e7eb7d 2020-12-15 stsp char buf[PATH_MAX * 4];
434 73e7eb7d 2020-12-15 stsp va_list ap;
435 73e7eb7d 2020-12-15 stsp size_t i;
436 73e7eb7d 2020-12-15 stsp
437 73e7eb7d 2020-12-15 stsp va_start(ap, fmt);
438 73e7eb7d 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
439 73e7eb7d 2020-12-15 stsp va_end(ap);
440 73e7eb7d 2020-12-15 stsp
441 73e7eb7d 2020-12-15 stsp for (i = 0; i < nitems(got_errors); i++) {
442 73e7eb7d 2020-12-15 stsp if (code == got_errors[i].code) {
443 c884fd0a 2020-12-21 stsp err->code = code;
444 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
445 73e7eb7d 2020-12-15 stsp got_errors[i].msg);
446 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
447 c884fd0a 2020-12-21 stsp return err;
448 73e7eb7d 2020-12-15 stsp }
449 73e7eb7d 2020-12-15 stsp }
450 73e7eb7d 2020-12-15 stsp
451 73e7eb7d 2020-12-15 stsp abort();
452 73e7eb7d 2020-12-15 stsp }
453 3dc1dc04 2021-09-27 thomas
454 3dc1dc04 2021-09-27 thomas int
455 3dc1dc04 2021-09-27 thomas got_err_open_nofollow_on_symlink(void)
456 3dc1dc04 2021-09-27 thomas {
457 3dc1dc04 2021-09-27 thomas /*
458 3dc1dc04 2021-09-27 thomas * Check whether open(2) with O_NOFOLLOW failed on a symlink.
459 3dc1dc04 2021-09-27 thomas * Posix mandates ELOOP and OpenBSD follows it. Others return
460 3dc1dc04 2021-09-27 thomas * different error codes. We carry this workaround to help the
461 3dc1dc04 2021-09-27 thomas * portable version a little.
462 3dc1dc04 2021-09-27 thomas */
463 3dc1dc04 2021-09-27 thomas return (errno == ELOOP
464 3dc1dc04 2021-09-27 thomas #ifdef EMLINK
465 3dc1dc04 2021-09-27 thomas || errno == EMLINK
466 3dc1dc04 2021-09-27 thomas #endif
467 3dc1dc04 2021-09-27 thomas #ifdef EFTYPE
468 3dc1dc04 2021-09-27 thomas || errno == EFTYPE
469 3dc1dc04 2021-09-27 thomas #endif
470 3dc1dc04 2021-09-27 thomas );
471 3dc1dc04 2021-09-27 thomas }