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 5822e79e 2023-02-23 op #include <sha2.h>
27 91a3d81f 2018-11-11 stsp #include <zlib.h>
28 09589288 2019-03-10 stsp #include <uuid.h>
29 f334529e 2018-01-12 stsp
30 4027f31a 2017-11-04 stsp #include "got_error.h"
31 91a3d81f 2018-11-11 stsp #include "got_object.h"
32 4027f31a 2017-11-04 stsp
33 91a3d81f 2018-11-11 stsp #include "got_lib_delta.h"
34 91a3d81f 2018-11-11 stsp #include "got_lib_inflate.h"
35 91a3d81f 2018-11-11 stsp #include "got_lib_object.h"
36 2f43cd69 2023-04-14 stsp #include "got_lib_hash.h"
37 bbc740ac 2023-02-04 op #include "got_lib_object_parse.h"
38 91a3d81f 2018-11-11 stsp
39 2b4402a2 2017-11-05 stsp #ifndef nitems
40 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 2b4402a2 2017-11-05 stsp #endif
42 4027f31a 2017-11-04 stsp
43 70cc9832 2022-03-19 naddy static const struct got_error got_errors[] = {
44 e0586a74 2023-12-05 mark { GOT_ERR_OK, "no error occurred?!?" },
45 70cc9832 2022-03-19 naddy { GOT_ERR_ERRNO, "see errno" },
46 70cc9832 2022-03-19 naddy { GOT_ERR_NOT_GIT_REPO, "no git repository found" },
47 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_FILETYPE, "bad file type" },
48 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_PATH, "bad path" },
49 70cc9832 2022-03-19 naddy { GOT_ERR_NOT_REF, "no such reference found" },
50 70cc9832 2022-03-19 naddy { GOT_ERR_IO, "input/output error" },
51 70cc9832 2022-03-19 naddy { GOT_ERR_EOF, "unexpected end of file" },
52 70cc9832 2022-03-19 naddy { GOT_ERR_DECOMPRESSION,"decompression failed" },
53 70cc9832 2022-03-19 naddy { GOT_ERR_NO_SPACE, "buffer too small" },
54 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_OBJ_HDR, "bad object header" },
55 70cc9832 2022-03-19 naddy { GOT_ERR_OBJ_TYPE, "wrong type of object" },
56 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_OBJ_DATA, "bad object data" },
57 70cc9832 2022-03-19 naddy { GOT_ERR_AMBIGUOUS_ID, "ambiguous object ID" },
58 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_PACKIDX, "bad pack index file" },
59 70cc9832 2022-03-19 naddy { GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
60 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_PACKFILE, "bad pack file" },
61 70cc9832 2022-03-19 naddy { GOT_ERR_NO_OBJ, "object not found" },
62 70cc9832 2022-03-19 naddy { GOT_ERR_NOT_IMPL, "feature not implemented" },
63 70cc9832 2022-03-19 naddy { GOT_ERR_OBJ_NOT_PACKED,"object is not packed" },
64 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" },
65 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_DELTA, "bad delta" },
66 70cc9832 2022-03-19 naddy { GOT_ERR_COMPRESSION, "compression failed" },
67 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
68 70cc9832 2022-03-19 naddy { GOT_ERR_WORKTREE_EXISTS,"worktree already exists" },
69 70cc9832 2022-03-19 naddy { GOT_ERR_WORKTREE_META,"bad worktree meta data" },
70 70cc9832 2022-03-19 naddy { GOT_ERR_WORKTREE_VERS,"unsupported worktree format version" },
71 70cc9832 2022-03-19 naddy { GOT_ERR_WORKTREE_BUSY,"worktree already locked" },
72 70cc9832 2022-03-19 naddy { GOT_ERR_FILE_OBSTRUCTED,"file is obstructed" },
73 70cc9832 2022-03-19 naddy { GOT_ERR_RECURSION, "recursion limit reached" },
74 70cc9832 2022-03-19 naddy { GOT_ERR_TIMEOUT, "operation timed out" },
75 70cc9832 2022-03-19 naddy { GOT_ERR_INTERRUPT, "operation interrupted" },
76 70cc9832 2022-03-19 naddy { GOT_ERR_PRIVSEP_READ, "no data received in imsg" },
77 70cc9832 2022-03-19 naddy { GOT_ERR_PRIVSEP_LEN, "unexpected amount of data received in imsg" },
78 70cc9832 2022-03-19 naddy { GOT_ERR_PRIVSEP_PIPE, "privsep peer process closed pipe" },
79 70cc9832 2022-03-19 naddy { GOT_ERR_PRIVSEP_NO_FD,"privsep file descriptor unavailable" },
80 70cc9832 2022-03-19 naddy { GOT_ERR_PRIVSEP_MSG, "received unexpected privsep message" },
81 70cc9832 2022-03-19 naddy { GOT_ERR_PRIVSEP_DIED, "unprivileged process died unexpectedly" },
82 70cc9832 2022-03-19 naddy { GOT_ERR_PRIVSEP_EXIT, "bad exit code from unprivileged process" },
83 70cc9832 2022-03-19 naddy { GOT_ERR_PACK_OFFSET, "bad offset in pack file" },
84 70cc9832 2022-03-19 naddy { GOT_ERR_OBJ_EXISTS, "object already exists" },
85 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_OBJ_ID, "bad object id" },
86 70cc9832 2022-03-19 naddy { GOT_ERR_ITER_COMPLETED,"iteration completed" },
87 70cc9832 2022-03-19 naddy { GOT_ERR_RANGE, "value out of range" },
88 70cc9832 2022-03-19 naddy { GOT_ERR_EXPECTED, "expected an error but have no error" },
89 70cc9832 2022-03-19 naddy { GOT_ERR_CANCELLED, "operation in progress has been cancelled" },
90 70cc9832 2022-03-19 naddy { GOT_ERR_NO_TREE_ENTRY,"no such entry found in tree" },
91 70cc9832 2022-03-19 naddy { GOT_ERR_FILEIDX_SIG, "bad file index signature" },
92 70cc9832 2022-03-19 naddy { GOT_ERR_FILEIDX_VER, "unknown file index format version" },
93 70cc9832 2022-03-19 naddy { GOT_ERR_FILEIDX_CSUM, "bad file index checksum" },
94 70cc9832 2022-03-19 naddy { GOT_ERR_PATH_PREFIX, "worktree already contains items from a "
95 70cc9832 2022-03-19 naddy "different path prefix" },
96 70cc9832 2022-03-19 naddy { GOT_ERR_ANCESTRY, "target commit is on a different branch" },
97 70cc9832 2022-03-19 naddy { GOT_ERR_FILEIDX_BAD, "file index is corrupt" },
98 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_REF_DATA, "could not parse reference data" },
99 70cc9832 2022-03-19 naddy { GOT_ERR_TREE_DUP_ENTRY,"duplicate entry in tree object" },
100 70cc9832 2022-03-19 naddy { GOT_ERR_DIR_DUP_ENTRY,"duplicate entry in directory" },
101 df6221c7 2023-07-19 stsp { GOT_ERR_NOT_WORKTREE, "no work tree found" },
102 70cc9832 2022-03-19 naddy { GOT_ERR_UUID_VERSION, "bad uuid version" },
103 70cc9832 2022-03-19 naddy { GOT_ERR_UUID_INVALID, "uuid invalid" },
104 70cc9832 2022-03-19 naddy { GOT_ERR_UUID, "uuid error" },
105 70cc9832 2022-03-19 naddy { GOT_ERR_LOCKFILE_TIMEOUT,"lockfile timeout" },
106 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_REF_NAME, "bad reference name" },
107 70cc9832 2022-03-19 naddy { GOT_ERR_WORKTREE_REPO,"cannot create worktree inside a git repository" },
108 70cc9832 2022-03-19 naddy { GOT_ERR_FILE_MODIFIED,"file contains modifications" },
109 70cc9832 2022-03-19 naddy { GOT_ERR_FILE_STATUS, "file has unexpected status" },
110 70cc9832 2022-03-19 naddy { GOT_ERR_COMMIT_CONFLICT,"cannot commit file in conflicted status" },
111 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_REF_TYPE, "bad reference type" },
112 70cc9832 2022-03-19 naddy { GOT_ERR_COMMIT_NO_AUTHOR,"GOT_AUTHOR environment variable is not set" },
113 70cc9832 2022-03-19 naddy { GOT_ERR_COMMIT_HEAD_CHANGED, "branch head in repository has changed "
114 70cc9832 2022-03-19 naddy "while commit was in progress" },
115 70cc9832 2022-03-19 naddy { GOT_ERR_COMMIT_OUT_OF_DATE, "work tree must be updated before these "
116 70cc9832 2022-03-19 naddy "changes can be committed" },
117 70cc9832 2022-03-19 naddy { GOT_ERR_COMMIT_MSG_EMPTY, "commit message cannot be empty" },
118 70cc9832 2022-03-19 naddy { GOT_ERR_DIR_NOT_EMPTY, "directory exists and is not empty" },
119 70cc9832 2022-03-19 naddy { GOT_ERR_COMMIT_NO_CHANGES, "no changes to commit" },
120 70cc9832 2022-03-19 naddy { GOT_ERR_BRANCH_MOVED, "work tree's head reference now points to a "
121 70cc9832 2022-03-19 naddy "different branch; new head reference and/or update -b required" },
122 70cc9832 2022-03-19 naddy { GOT_ERR_OBJ_TOO_LARGE, "object too large" },
123 70cc9832 2022-03-19 naddy { GOT_ERR_SAME_BRANCH, "commit is already contained in this branch" },
124 70cc9832 2022-03-19 naddy { GOT_ERR_ROOT_COMMIT, "specified commit has no parent commit" },
125 70cc9832 2022-03-19 naddy { GOT_ERR_MIXED_COMMITS,"work tree contains files from multiple "
126 70cc9832 2022-03-19 naddy "base commits; the entire work tree must be updated first" },
127 70cc9832 2022-03-19 naddy { GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
128 70cc9832 2022-03-19 naddy "conflicts must be resolved first" },
129 70cc9832 2022-03-19 naddy { GOT_ERR_BRANCH_EXISTS,"specified branch already exists" },
130 70cc9832 2022-03-19 naddy { GOT_ERR_MODIFIED, "work tree contains local changes; these "
131 70cc9832 2022-03-19 naddy "changes must be committed or reverted first" },
132 70cc9832 2022-03-19 naddy { GOT_ERR_NOT_REBASING, "rebase operation not in progress" },
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 13b2bc37 2022-10-23 stsp { GOT_ERR_PACKFILE_CSUM, "pack file checksum error" },
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_FETCH_FAILED, "fetch failed" },
174 70cc9832 2022-03-19 naddy { GOT_ERR_PARSE_URI, "failed to parse uri" },
175 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_PROTO, "unknown protocol" },
176 70cc9832 2022-03-19 naddy { GOT_ERR_ADDRINFO, "getaddrinfo failed" },
177 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_PACKET, "bad packet received" },
178 70cc9832 2022-03-19 naddy { GOT_ERR_NO_REMOTE, "remote repository not found" },
179 70cc9832 2022-03-19 naddy { GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
180 70cc9832 2022-03-19 naddy { GOT_ERR_FETCH_BAD_REF, "reference cannot be fetched" },
181 70cc9832 2022-03-19 naddy { GOT_ERR_TREE_ENTRY_TYPE, "unexpected tree entry type" },
182 70cc9832 2022-03-19 naddy { GOT_ERR_PARSE_CONFIG, "configuration file syntax error" },
183 70cc9832 2022-03-19 naddy { GOT_ERR_NO_CONFIG_FILE, "configuration file doesn't exit" },
184 70cc9832 2022-03-19 naddy { GOT_ERR_BAD_SYMLINK, "symbolic link points outside of paths under "
185 70cc9832 2022-03-19 naddy "version control" },
186 70cc9832 2022-03-19 naddy { GOT_ERR_GIT_REPO_EXT, "unsupported repository format extension" },
187 70cc9832 2022-03-19 naddy { GOT_ERR_CANNOT_PACK, "not enough objects to pack" },
188 70cc9832 2022-03-19 naddy { GOT_ERR_LONELY_PACKIDX, "pack index has no corresponding pack file; "
189 70cc9832 2022-03-19 naddy "pack file must be restored or 'gotadmin cleanup -p' must be run" },
190 70cc9832 2022-03-19 naddy { GOT_ERR_OBJ_CSUM, "bad object checksum" },
191 70cc9832 2022-03-19 naddy { GOT_ERR_SEND_BAD_REF, "reference cannot be sent" },
192 70cc9832 2022-03-19 naddy { GOT_ERR_SEND_FAILED, "could not send pack file" },
193 70cc9832 2022-03-19 naddy { GOT_ERR_SEND_EMPTY, "no references to send" },
194 70cc9832 2022-03-19 naddy { GOT_ERR_SEND_ANCESTRY, "fetch and rebase required" },
195 70cc9832 2022-03-19 naddy { GOT_ERR_CAPA_DELETE_REFS, "server cannot delete references" },
196 70cc9832 2022-03-19 naddy { GOT_ERR_SEND_DELETE_REF, "reference cannot be deleted" },
197 70cc9832 2022-03-19 naddy { GOT_ERR_SEND_TAG_EXISTS, "tag already exists on server" },
198 70cc9832 2022-03-19 naddy { GOT_ERR_NOT_MERGING, "merge operation not in progress" },
199 70cc9832 2022-03-19 naddy { GOT_ERR_MERGE_OUT_OF_DATE, "work tree must be updated before it "
200 70cc9832 2022-03-19 naddy "can be used to merge a branch" },
201 70cc9832 2022-03-19 naddy { GOT_ERR_MERGE_STAGED_PATHS, "work tree contains files with staged "
202 70cc9832 2022-03-19 naddy "changes; these changes must be unstaged before merging can "
203 70cc9832 2022-03-19 naddy "proceed" },
204 70cc9832 2022-03-19 naddy { GOT_ERR_MERGE_BUSY,"a merge operation is in progress in this "
205 70cc9832 2022-03-19 naddy "work tree and must be continued or aborted first" },
206 70cc9832 2022-03-19 naddy { GOT_ERR_MERGE_PATH, "cannot merge branch which contains "
207 70cc9832 2022-03-19 naddy "changes outside of this work tree's path prefix" },
208 70cc9832 2022-03-19 naddy { GOT_ERR_FILE_BINARY, "found a binary file instead of text" },
209 70cc9832 2022-03-19 naddy { GOT_ERR_PATCH_MALFORMED, "malformed patch" },
210 70cc9832 2022-03-19 naddy { GOT_ERR_PATCH_TRUNCATED, "patch truncated" },
211 70cc9832 2022-03-19 naddy { GOT_ERR_NO_PATCH, "no patch found" },
212 70cc9832 2022-03-19 naddy { GOT_ERR_HUNK_FAILED, "hunk failed to apply" },
213 70cc9832 2022-03-19 naddy { GOT_ERR_PATCH_FAILED, "patch failed to apply" },
214 a7472cb3 2022-04-14 stsp { GOT_ERR_FILEIDX_DUP_ENTRY, "duplicate file index entry" },
215 61af9b21 2022-06-28 stsp { GOT_ERR_PIN_PACK, "could not pin pack file" },
216 4d5ee956 2022-07-02 jrick { GOT_ERR_BAD_TAG_SIGNATURE, "invalid tag signature" },
217 4d5ee956 2022-07-02 jrick { GOT_ERR_VERIFY_TAG_SIGNATURE, "cannot verify signature" },
218 91d845ad 2022-07-03 jrick { GOT_ERR_SIGNING_TAG, "unable to sign tag" },
219 f25e229e 2023-06-22 stsp { GOT_ERR_BAD_OPTION, "option cannot be used" },
220 58381f70 2022-09-03 op { GOT_ERR_BAD_QUERYSTRING, "invalid query string" },
221 e02b422b 2022-09-27 jrick { GOT_ERR_INTEGRATE_BRANCH, "will not integrate into a reference "
222 e02b422b 2022-09-27 jrick "outside the \"refs/heads/\" reference namespace" },
223 13b2bc37 2022-10-23 stsp { GOT_ERR_BAD_REQUEST, "unexpected request received" },
224 13b2bc37 2022-10-23 stsp { GOT_ERR_CLIENT_ID, "unknown client identifier" },
225 13b2bc37 2022-10-23 stsp { GOT_ERR_REPO_TEMPFILE, "no repository tempfile available" },
226 9afa3de2 2023-04-04 stsp { GOT_ERR_REFS_PROTECTED, "reference namespace is protected" },
227 9afa3de2 2023-04-04 stsp { GOT_ERR_REF_PROTECTED, "reference is protected" },
228 13b2bc37 2022-10-23 stsp { GOT_ERR_REF_BUSY, "reference cannot be updated; please try again" },
229 21f07726 2022-10-31 stsp { GOT_ERR_COMMIT_BAD_AUTHOR, "commit author formatting would "
230 21f07726 2022-10-31 stsp "make Git unhappy" },
231 365cf0f3 2022-12-29 stsp { GOT_ERR_UID, "bad user ID" },
232 365cf0f3 2022-12-29 stsp { GOT_ERR_GID, "bad group ID" },
233 6c5befc7 2023-03-28 stsp { GOT_ERR_NO_PROG, "command not found or not accessible" },
234 6b5246e4 2023-06-05 stsp { GOT_ERR_MERGE_COMMIT_OUT_OF_DATE, "merging cannot proceed because "
235 6b5246e4 2023-06-05 stsp "the work tree is no longer up-to-date; merge must be aborted "
236 6b5246e4 2023-06-05 stsp "and retried" },
237 f7d653fc 2023-07-09 op { GOT_ERR_BUNDLE_FORMAT, "unknown git bundle version" },
238 fd7552d2 2023-07-17 mark { GOT_ERR_BAD_KEYWORD, "invalid commit keyword" }
239 70cc9832 2022-03-19 naddy };
240 70cc9832 2022-03-19 naddy
241 c884fd0a 2020-12-21 stsp static struct got_custom_error {
242 c884fd0a 2020-12-21 stsp struct got_error err;
243 13b2bc37 2022-10-23 stsp char msg[GOT_ERR_MAX_MSG_SIZE];
244 c884fd0a 2020-12-21 stsp } custom_errors[16];
245 c884fd0a 2020-12-21 stsp
246 c884fd0a 2020-12-21 stsp static struct got_custom_error *
247 c884fd0a 2020-12-21 stsp get_custom_err(void)
248 c884fd0a 2020-12-21 stsp {
249 c884fd0a 2020-12-21 stsp static unsigned int idx;
250 c884fd0a 2020-12-21 stsp return &custom_errors[(idx++) % nitems(custom_errors)];
251 c884fd0a 2020-12-21 stsp }
252 c884fd0a 2020-12-21 stsp
253 4027f31a 2017-11-04 stsp const struct got_error *
254 4027f31a 2017-11-04 stsp got_error(int code)
255 4027f31a 2017-11-04 stsp {
256 16aeacf7 2020-11-26 stsp size_t i;
257 4027f31a 2017-11-04 stsp
258 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
259 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
260 4027f31a 2017-11-04 stsp return &got_errors[i];
261 4027f31a 2017-11-04 stsp }
262 4027f31a 2017-11-04 stsp
263 f334529e 2018-01-12 stsp abort();
264 4027f31a 2017-11-04 stsp }
265 f334529e 2018-01-12 stsp
266 f334529e 2018-01-12 stsp const struct got_error *
267 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
268 91a3d81f 2018-11-11 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 16aeacf7 2020-11-26 stsp size_t i;
272 91a3d81f 2018-11-11 stsp
273 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
274 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
275 c884fd0a 2020-12-21 stsp err->code = code;
276 c884fd0a 2020-12-21 stsp strlcpy(cerr->msg, msg, sizeof(cerr->msg));
277 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
278 c884fd0a 2020-12-21 stsp return err;
279 91a3d81f 2018-11-11 stsp }
280 91a3d81f 2018-11-11 stsp }
281 91a3d81f 2018-11-11 stsp
282 91a3d81f 2018-11-11 stsp abort();
283 91a3d81f 2018-11-11 stsp }
284 91a3d81f 2018-11-11 stsp
285 91a3d81f 2018-11-11 stsp const struct got_error *
286 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
287 f334529e 2018-01-12 stsp {
288 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
289 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
290 9a02f8b7 2020-12-21 stsp char strerr[128];
291 f334529e 2018-01-12 stsp
292 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
293 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
294 230a42bd 2019-05-11 jcs
295 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
296 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
297 c884fd0a 2020-12-21 stsp return err;
298 f334529e 2018-01-12 stsp }
299 8251fdbc 2018-01-12 stsp
300 8251fdbc 2018-01-12 stsp const struct got_error *
301 638f9024 2019-05-13 stsp got_error_from_errno2(const char *prefix, const char *prefix2)
302 48b8b0eb 2019-05-11 jcs {
303 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
304 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
305 9a02f8b7 2020-12-21 stsp char strerr[128];
306 48b8b0eb 2019-05-11 jcs
307 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
308 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
309 9a02f8b7 2020-12-21 stsp strerr);
310 48b8b0eb 2019-05-11 jcs
311 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
312 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
313 c884fd0a 2020-12-21 stsp return err;
314 48b8b0eb 2019-05-11 jcs }
315 48b8b0eb 2019-05-11 jcs
316 48b8b0eb 2019-05-11 jcs const struct got_error *
317 638f9024 2019-05-13 stsp got_error_from_errno3(const char *prefix, const char *prefix2,
318 230a42bd 2019-05-11 jcs const char *prefix3)
319 230a42bd 2019-05-11 jcs {
320 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
321 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
322 9a02f8b7 2020-12-21 stsp char strerr[128];
323 230a42bd 2019-05-11 jcs
324 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
325 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
326 9a02f8b7 2020-12-21 stsp prefix2, prefix3, strerr);
327 230a42bd 2019-05-11 jcs
328 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
329 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
330 c884fd0a 2020-12-21 stsp return err;
331 230a42bd 2019-05-11 jcs }
332 230a42bd 2019-05-11 jcs
333 230a42bd 2019-05-11 jcs const struct got_error *
334 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
335 4cc6a5a5 2020-12-15 stsp {
336 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
337 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
338 4cc6a5a5 2020-12-15 stsp char buf[PATH_MAX * 4];
339 9a02f8b7 2020-12-21 stsp char strerr[128];
340 4cc6a5a5 2020-12-15 stsp va_list ap;
341 4cc6a5a5 2020-12-15 stsp
342 4cc6a5a5 2020-12-15 stsp va_start(ap, fmt);
343 4cc6a5a5 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
344 4cc6a5a5 2020-12-15 stsp va_end(ap);
345 4cc6a5a5 2020-12-15 stsp
346 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
347 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
348 4cc6a5a5 2020-12-15 stsp
349 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
350 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
351 c884fd0a 2020-12-21 stsp return err;
352 4cc6a5a5 2020-12-15 stsp }
353 4cc6a5a5 2020-12-15 stsp
354 4cc6a5a5 2020-12-15 stsp const struct got_error *
355 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
356 1a76625f 2018-10-22 stsp {
357 1a76625f 2018-10-22 stsp errno = code;
358 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
359 1a76625f 2018-10-22 stsp }
360 1a76625f 2018-10-22 stsp
361 1a76625f 2018-10-22 stsp const struct got_error *
362 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
363 8251fdbc 2018-01-12 stsp {
364 8251fdbc 2018-01-12 stsp if (ferror(f))
365 638f9024 2019-05-13 stsp return got_error_from_errno("");
366 8251fdbc 2018-01-12 stsp return got_error(code);
367 8251fdbc 2018-01-12 stsp }
368 91a3d81f 2018-11-11 stsp
369 91a3d81f 2018-11-11 stsp const struct got_error *
370 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
371 91a3d81f 2018-11-11 stsp {
372 bbc740ac 2023-02-04 op char id_str[GOT_OBJECT_ID_HEX_MAXLEN];
373 bbc740ac 2023-02-04 op char msg[sizeof("object not found") + sizeof(id_str)];
374 91a3d81f 2018-11-11 stsp int ret;
375 91a3d81f 2018-11-11 stsp
376 bbc740ac 2023-02-04 op if (!got_object_id_hex(id, id_str, sizeof(id_str)))
377 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
378 91a3d81f 2018-11-11 stsp
379 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
380 438d0cc3 2022-08-16 op if (ret < 0 || (size_t)ret >= sizeof(msg))
381 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
382 91a3d81f 2018-11-11 stsp
383 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
384 3c23f6cd 2023-02-04 op }
385 3c23f6cd 2023-02-04 op
386 3c23f6cd 2023-02-04 op const struct got_error *
387 3c23f6cd 2023-02-04 op got_error_checksum(struct got_object_id *id)
388 3c23f6cd 2023-02-04 op {
389 3c23f6cd 2023-02-04 op char id_str[GOT_OBJECT_ID_HEX_MAXLEN];
390 3c23f6cd 2023-02-04 op char msg[sizeof("checksum failure for object ") + sizeof(id_str)];
391 3c23f6cd 2023-02-04 op int ret;
392 3c23f6cd 2023-02-04 op
393 3c23f6cd 2023-02-04 op if (!got_object_id_hex(id, id_str, sizeof(id_str)))
394 3c23f6cd 2023-02-04 op return got_error(GOT_ERR_OBJ_CSUM);
395 3c23f6cd 2023-02-04 op
396 3c23f6cd 2023-02-04 op ret = snprintf(msg, sizeof(msg), "checksum failure for object %s",
397 3c23f6cd 2023-02-04 op id_str);
398 3c23f6cd 2023-02-04 op if (ret < 0 || (size_t)ret >= sizeof(msg))
399 3c23f6cd 2023-02-04 op return got_error(GOT_ERR_OBJ_CSUM);
400 3c23f6cd 2023-02-04 op
401 3c23f6cd 2023-02-04 op return got_error_msg(GOT_ERR_OBJ_CSUM, msg);
402 91a3d81f 2018-11-11 stsp }
403 2aa0475c 2019-02-03 stsp
404 2aa0475c 2019-02-03 stsp const struct got_error *
405 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
406 2aa0475c 2019-02-03 stsp {
407 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
408 2aa0475c 2019-02-03 stsp int ret;
409 2aa0475c 2019-02-03 stsp
410 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
411 438d0cc3 2022-08-16 op if (ret < 0 || (size_t)ret >= sizeof(msg))
412 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
413 2aa0475c 2019-02-03 stsp
414 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
415 2aa0475c 2019-02-03 stsp }
416 09589288 2019-03-10 stsp
417 09589288 2019-03-10 stsp const struct got_error *
418 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
419 09589288 2019-03-10 stsp {
420 09589288 2019-03-10 stsp switch (uuid_status) {
421 09589288 2019-03-10 stsp case uuid_s_ok:
422 09589288 2019-03-10 stsp return NULL;
423 09589288 2019-03-10 stsp case uuid_s_bad_version:
424 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
425 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
426 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
427 09589288 2019-03-10 stsp case uuid_s_no_memory:
428 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
429 09589288 2019-03-10 stsp default:
430 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
431 09589288 2019-03-10 stsp }
432 09589288 2019-03-10 stsp }
433 df056ada 2019-05-15 stsp
434 df056ada 2019-05-15 stsp const struct got_error *
435 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
436 df056ada 2019-05-15 stsp {
437 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
438 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
439 16aeacf7 2020-11-26 stsp size_t i;
440 df056ada 2019-05-15 stsp
441 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
442 df056ada 2019-05-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", path,
445 df056ada 2019-05-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 df056ada 2019-05-15 stsp }
449 df056ada 2019-05-15 stsp }
450 df056ada 2019-05-15 stsp
451 df056ada 2019-05-15 stsp abort();
452 df056ada 2019-05-15 stsp }
453 73e7eb7d 2020-12-15 stsp
454 73e7eb7d 2020-12-15 stsp const struct got_error *
455 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
456 73e7eb7d 2020-12-15 stsp {
457 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
458 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
459 73e7eb7d 2020-12-15 stsp char buf[PATH_MAX * 4];
460 73e7eb7d 2020-12-15 stsp va_list ap;
461 73e7eb7d 2020-12-15 stsp size_t i;
462 73e7eb7d 2020-12-15 stsp
463 73e7eb7d 2020-12-15 stsp va_start(ap, fmt);
464 73e7eb7d 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
465 73e7eb7d 2020-12-15 stsp va_end(ap);
466 73e7eb7d 2020-12-15 stsp
467 73e7eb7d 2020-12-15 stsp for (i = 0; i < nitems(got_errors); i++) {
468 73e7eb7d 2020-12-15 stsp if (code == got_errors[i].code) {
469 c884fd0a 2020-12-21 stsp err->code = code;
470 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
471 73e7eb7d 2020-12-15 stsp got_errors[i].msg);
472 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
473 c884fd0a 2020-12-21 stsp return err;
474 73e7eb7d 2020-12-15 stsp }
475 73e7eb7d 2020-12-15 stsp }
476 73e7eb7d 2020-12-15 stsp
477 73e7eb7d 2020-12-15 stsp abort();
478 73e7eb7d 2020-12-15 stsp }
479 5c02d2a5 2021-09-26 stsp
480 5c02d2a5 2021-09-26 stsp int
481 5c02d2a5 2021-09-26 stsp got_err_open_nofollow_on_symlink(void)
482 5c02d2a5 2021-09-26 stsp {
483 5c02d2a5 2021-09-26 stsp /*
484 5c02d2a5 2021-09-26 stsp * Check whether open(2) with O_NOFOLLOW failed on a symlink.
485 5c02d2a5 2021-09-26 stsp * Posix mandates ELOOP and OpenBSD follows it. Others return
486 5c02d2a5 2021-09-26 stsp * different error codes. We carry this workaround to help the
487 5c02d2a5 2021-09-26 stsp * portable version a little.
488 5c02d2a5 2021-09-26 stsp */
489 5c02d2a5 2021-09-26 stsp return (errno == ELOOP
490 5c02d2a5 2021-09-26 stsp #ifdef EMLINK
491 5c02d2a5 2021-09-26 stsp || errno == EMLINK
492 5c02d2a5 2021-09-26 stsp #endif
493 5c02d2a5 2021-09-26 stsp #ifdef EFTYPE
494 5c02d2a5 2021-09-26 stsp || errno == EFTYPE
495 5c02d2a5 2021-09-26 stsp #endif
496 5c02d2a5 2021-09-26 stsp );
497 5c02d2a5 2021-09-26 stsp }