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