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 c884fd0a 2020-12-21 stsp static struct got_custom_error {
49 c884fd0a 2020-12-21 stsp struct got_error err;
50 c884fd0a 2020-12-21 stsp char msg[4080];
51 c884fd0a 2020-12-21 stsp } custom_errors[16];
52 c884fd0a 2020-12-21 stsp
53 c884fd0a 2020-12-21 stsp static struct got_custom_error *
54 c884fd0a 2020-12-21 stsp get_custom_err(void)
55 c884fd0a 2020-12-21 stsp {
56 c884fd0a 2020-12-21 stsp static unsigned int idx;
57 c884fd0a 2020-12-21 stsp return &custom_errors[(idx++) % nitems(custom_errors)];
58 c884fd0a 2020-12-21 stsp }
59 c884fd0a 2020-12-21 stsp
60 4027f31a 2017-11-04 stsp const struct got_error *
61 4027f31a 2017-11-04 stsp got_error(int code)
62 4027f31a 2017-11-04 stsp {
63 16aeacf7 2020-11-26 stsp size_t i;
64 4027f31a 2017-11-04 stsp
65 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
66 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
67 4027f31a 2017-11-04 stsp return &got_errors[i];
68 4027f31a 2017-11-04 stsp }
69 4027f31a 2017-11-04 stsp
70 f334529e 2018-01-12 stsp abort();
71 4027f31a 2017-11-04 stsp }
72 f334529e 2018-01-12 stsp
73 f334529e 2018-01-12 stsp const struct got_error *
74 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
75 91a3d81f 2018-11-11 stsp {
76 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
77 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
78 16aeacf7 2020-11-26 stsp size_t i;
79 91a3d81f 2018-11-11 stsp
80 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
81 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
82 c884fd0a 2020-12-21 stsp err->code = code;
83 c884fd0a 2020-12-21 stsp strlcpy(cerr->msg, msg, sizeof(cerr->msg));
84 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
85 c884fd0a 2020-12-21 stsp return err;
86 91a3d81f 2018-11-11 stsp }
87 91a3d81f 2018-11-11 stsp }
88 91a3d81f 2018-11-11 stsp
89 91a3d81f 2018-11-11 stsp abort();
90 91a3d81f 2018-11-11 stsp }
91 91a3d81f 2018-11-11 stsp
92 91a3d81f 2018-11-11 stsp const struct got_error *
93 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
94 f334529e 2018-01-12 stsp {
95 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
96 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
97 9a02f8b7 2020-12-21 stsp char strerr[128];
98 f334529e 2018-01-12 stsp
99 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
100 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
101 230a42bd 2019-05-11 jcs
102 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
103 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
104 c884fd0a 2020-12-21 stsp return err;
105 f334529e 2018-01-12 stsp }
106 8251fdbc 2018-01-12 stsp
107 8251fdbc 2018-01-12 stsp const struct got_error *
108 638f9024 2019-05-13 stsp got_error_from_errno2(const char *prefix, const char *prefix2)
109 48b8b0eb 2019-05-11 jcs {
110 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
111 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
112 9a02f8b7 2020-12-21 stsp char strerr[128];
113 48b8b0eb 2019-05-11 jcs
114 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
115 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
116 9a02f8b7 2020-12-21 stsp strerr);
117 48b8b0eb 2019-05-11 jcs
118 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
119 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
120 c884fd0a 2020-12-21 stsp return err;
121 48b8b0eb 2019-05-11 jcs }
122 48b8b0eb 2019-05-11 jcs
123 48b8b0eb 2019-05-11 jcs const struct got_error *
124 638f9024 2019-05-13 stsp got_error_from_errno3(const char *prefix, const char *prefix2,
125 230a42bd 2019-05-11 jcs const char *prefix3)
126 230a42bd 2019-05-11 jcs {
127 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
128 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
129 9a02f8b7 2020-12-21 stsp char strerr[128];
130 230a42bd 2019-05-11 jcs
131 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
132 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
133 9a02f8b7 2020-12-21 stsp prefix2, prefix3, strerr);
134 230a42bd 2019-05-11 jcs
135 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
136 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
137 c884fd0a 2020-12-21 stsp return err;
138 230a42bd 2019-05-11 jcs }
139 230a42bd 2019-05-11 jcs
140 230a42bd 2019-05-11 jcs const struct got_error *
141 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
142 4cc6a5a5 2020-12-15 stsp {
143 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
144 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
145 4cc6a5a5 2020-12-15 stsp char buf[PATH_MAX * 4];
146 9a02f8b7 2020-12-21 stsp char strerr[128];
147 4cc6a5a5 2020-12-15 stsp va_list ap;
148 4cc6a5a5 2020-12-15 stsp
149 4cc6a5a5 2020-12-15 stsp va_start(ap, fmt);
150 4cc6a5a5 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
151 4cc6a5a5 2020-12-15 stsp va_end(ap);
152 4cc6a5a5 2020-12-15 stsp
153 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
154 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
155 4cc6a5a5 2020-12-15 stsp
156 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
157 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
158 c884fd0a 2020-12-21 stsp return err;
159 4cc6a5a5 2020-12-15 stsp }
160 4cc6a5a5 2020-12-15 stsp
161 4cc6a5a5 2020-12-15 stsp const struct got_error *
162 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
163 1a76625f 2018-10-22 stsp {
164 1a76625f 2018-10-22 stsp errno = code;
165 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
166 1a76625f 2018-10-22 stsp }
167 1a76625f 2018-10-22 stsp
168 1a76625f 2018-10-22 stsp const struct got_error *
169 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
170 8251fdbc 2018-01-12 stsp {
171 8251fdbc 2018-01-12 stsp if (ferror(f))
172 638f9024 2019-05-13 stsp return got_error_from_errno("");
173 8251fdbc 2018-01-12 stsp return got_error(code);
174 8251fdbc 2018-01-12 stsp }
175 91a3d81f 2018-11-11 stsp
176 91a3d81f 2018-11-11 stsp const struct got_error *
177 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
178 91a3d81f 2018-11-11 stsp {
179 c884fd0a 2020-12-21 stsp char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
180 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
181 91a3d81f 2018-11-11 stsp int ret;
182 91a3d81f 2018-11-11 stsp
183 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
184 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
185 91a3d81f 2018-11-11 stsp
186 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
187 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
188 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
189 91a3d81f 2018-11-11 stsp
190 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
191 91a3d81f 2018-11-11 stsp }
192 2aa0475c 2019-02-03 stsp
193 2aa0475c 2019-02-03 stsp const struct got_error *
194 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
195 2aa0475c 2019-02-03 stsp {
196 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
197 2aa0475c 2019-02-03 stsp int ret;
198 2aa0475c 2019-02-03 stsp
199 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
200 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
201 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
202 2aa0475c 2019-02-03 stsp
203 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
204 2aa0475c 2019-02-03 stsp }
205 09589288 2019-03-10 stsp
206 09589288 2019-03-10 stsp const struct got_error *
207 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
208 09589288 2019-03-10 stsp {
209 09589288 2019-03-10 stsp switch (uuid_status) {
210 09589288 2019-03-10 stsp case uuid_s_ok:
211 09589288 2019-03-10 stsp return NULL;
212 09589288 2019-03-10 stsp case uuid_s_bad_version:
213 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
214 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
215 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
216 09589288 2019-03-10 stsp case uuid_s_no_memory:
217 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
218 09589288 2019-03-10 stsp default:
219 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
220 09589288 2019-03-10 stsp }
221 09589288 2019-03-10 stsp }
222 df056ada 2019-05-15 stsp
223 df056ada 2019-05-15 stsp const struct got_error *
224 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
225 df056ada 2019-05-15 stsp {
226 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
227 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
228 16aeacf7 2020-11-26 stsp size_t i;
229 df056ada 2019-05-15 stsp
230 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
231 df056ada 2019-05-15 stsp if (code == got_errors[i].code) {
232 c884fd0a 2020-12-21 stsp err->code = code;
233 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
234 df056ada 2019-05-15 stsp got_errors[i].msg);
235 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
236 c884fd0a 2020-12-21 stsp return err;
237 df056ada 2019-05-15 stsp }
238 df056ada 2019-05-15 stsp }
239 df056ada 2019-05-15 stsp
240 df056ada 2019-05-15 stsp abort();
241 df056ada 2019-05-15 stsp }
242 73e7eb7d 2020-12-15 stsp
243 73e7eb7d 2020-12-15 stsp const struct got_error *
244 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
245 73e7eb7d 2020-12-15 stsp {
246 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
247 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
248 73e7eb7d 2020-12-15 stsp char buf[PATH_MAX * 4];
249 73e7eb7d 2020-12-15 stsp va_list ap;
250 73e7eb7d 2020-12-15 stsp size_t i;
251 73e7eb7d 2020-12-15 stsp
252 73e7eb7d 2020-12-15 stsp va_start(ap, fmt);
253 73e7eb7d 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
254 73e7eb7d 2020-12-15 stsp va_end(ap);
255 73e7eb7d 2020-12-15 stsp
256 73e7eb7d 2020-12-15 stsp for (i = 0; i < nitems(got_errors); i++) {
257 73e7eb7d 2020-12-15 stsp if (code == got_errors[i].code) {
258 c884fd0a 2020-12-21 stsp err->code = code;
259 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
260 73e7eb7d 2020-12-15 stsp got_errors[i].msg);
261 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
262 c884fd0a 2020-12-21 stsp return err;
263 73e7eb7d 2020-12-15 stsp }
264 73e7eb7d 2020-12-15 stsp }
265 73e7eb7d 2020-12-15 stsp
266 73e7eb7d 2020-12-15 stsp abort();
267 73e7eb7d 2020-12-15 stsp }
268 3dc1dc04 2021-09-27 thomas
269 3dc1dc04 2021-09-27 thomas int
270 3dc1dc04 2021-09-27 thomas got_err_open_nofollow_on_symlink(void)
271 3dc1dc04 2021-09-27 thomas {
272 3dc1dc04 2021-09-27 thomas /*
273 3dc1dc04 2021-09-27 thomas * Check whether open(2) with O_NOFOLLOW failed on a symlink.
274 3dc1dc04 2021-09-27 thomas * Posix mandates ELOOP and OpenBSD follows it. Others return
275 3dc1dc04 2021-09-27 thomas * different error codes. We carry this workaround to help the
276 3dc1dc04 2021-09-27 thomas * portable version a little.
277 3dc1dc04 2021-09-27 thomas */
278 3dc1dc04 2021-09-27 thomas return (errno == ELOOP
279 3dc1dc04 2021-09-27 thomas #ifdef EMLINK
280 3dc1dc04 2021-09-27 thomas || errno == EMLINK
281 3dc1dc04 2021-09-27 thomas #endif
282 3dc1dc04 2021-09-27 thomas #ifdef EFTYPE
283 3dc1dc04 2021-09-27 thomas || errno == EFTYPE
284 3dc1dc04 2021-09-27 thomas #endif
285 3dc1dc04 2021-09-27 thomas );
286 3dc1dc04 2021-09-27 thomas }