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