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