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 f0678b77 2021-09-21 thomas.ad #ifdef __GLIBC__
155 f0678b77 2021-09-21 thomas.ad /*
156 f0678b77 2021-09-21 thomas.ad * The autoconf test for strerror_r is broken in current versions
157 f0678b77 2021-09-21 thomas.ad * of autoconf: https://savannah.gnu.org/support/?110367
158 f0678b77 2021-09-21 thomas.ad */
159 f0678b77 2021-09-21 thomas.ad __xpg_strerror_r(errno, strerr, sizeof(strerr));
160 f0678b77 2021-09-21 thomas.ad #else
161 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
162 f0678b77 2021-09-21 thomas.ad #endif
163 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
164 4cc6a5a5 2020-12-15 stsp
165 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
166 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
167 c884fd0a 2020-12-21 stsp return err;
168 4cc6a5a5 2020-12-15 stsp }
169 4cc6a5a5 2020-12-15 stsp
170 4cc6a5a5 2020-12-15 stsp const struct got_error *
171 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
172 1a76625f 2018-10-22 stsp {
173 1a76625f 2018-10-22 stsp errno = code;
174 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
175 1a76625f 2018-10-22 stsp }
176 1a76625f 2018-10-22 stsp
177 1a76625f 2018-10-22 stsp const struct got_error *
178 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
179 8251fdbc 2018-01-12 stsp {
180 8251fdbc 2018-01-12 stsp if (ferror(f))
181 638f9024 2019-05-13 stsp return got_error_from_errno("");
182 8251fdbc 2018-01-12 stsp return got_error(code);
183 8251fdbc 2018-01-12 stsp }
184 91a3d81f 2018-11-11 stsp
185 91a3d81f 2018-11-11 stsp const struct got_error *
186 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
187 91a3d81f 2018-11-11 stsp {
188 c884fd0a 2020-12-21 stsp char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
189 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
190 91a3d81f 2018-11-11 stsp int ret;
191 91a3d81f 2018-11-11 stsp
192 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
193 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
194 91a3d81f 2018-11-11 stsp
195 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
196 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
197 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
198 91a3d81f 2018-11-11 stsp
199 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
200 91a3d81f 2018-11-11 stsp }
201 2aa0475c 2019-02-03 stsp
202 2aa0475c 2019-02-03 stsp const struct got_error *
203 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
204 2aa0475c 2019-02-03 stsp {
205 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
206 2aa0475c 2019-02-03 stsp int ret;
207 2aa0475c 2019-02-03 stsp
208 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
209 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
210 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
211 2aa0475c 2019-02-03 stsp
212 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
213 2aa0475c 2019-02-03 stsp }
214 09589288 2019-03-10 stsp
215 09589288 2019-03-10 stsp const struct got_error *
216 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
217 09589288 2019-03-10 stsp {
218 09589288 2019-03-10 stsp switch (uuid_status) {
219 09589288 2019-03-10 stsp case uuid_s_ok:
220 09589288 2019-03-10 stsp return NULL;
221 09589288 2019-03-10 stsp case uuid_s_bad_version:
222 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
223 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
224 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
225 09589288 2019-03-10 stsp case uuid_s_no_memory:
226 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
227 09589288 2019-03-10 stsp default:
228 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
229 09589288 2019-03-10 stsp }
230 09589288 2019-03-10 stsp }
231 df056ada 2019-05-15 stsp
232 df056ada 2019-05-15 stsp const struct got_error *
233 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
234 df056ada 2019-05-15 stsp {
235 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
236 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
237 16aeacf7 2020-11-26 stsp size_t i;
238 df056ada 2019-05-15 stsp
239 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
240 df056ada 2019-05-15 stsp if (code == got_errors[i].code) {
241 c884fd0a 2020-12-21 stsp err->code = code;
242 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
243 df056ada 2019-05-15 stsp got_errors[i].msg);
244 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
245 c884fd0a 2020-12-21 stsp return err;
246 df056ada 2019-05-15 stsp }
247 df056ada 2019-05-15 stsp }
248 df056ada 2019-05-15 stsp
249 df056ada 2019-05-15 stsp abort();
250 df056ada 2019-05-15 stsp }
251 73e7eb7d 2020-12-15 stsp
252 73e7eb7d 2020-12-15 stsp const struct got_error *
253 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
254 73e7eb7d 2020-12-15 stsp {
255 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
256 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
257 73e7eb7d 2020-12-15 stsp char buf[PATH_MAX * 4];
258 73e7eb7d 2020-12-15 stsp va_list ap;
259 73e7eb7d 2020-12-15 stsp size_t i;
260 73e7eb7d 2020-12-15 stsp
261 73e7eb7d 2020-12-15 stsp va_start(ap, fmt);
262 73e7eb7d 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
263 73e7eb7d 2020-12-15 stsp va_end(ap);
264 73e7eb7d 2020-12-15 stsp
265 73e7eb7d 2020-12-15 stsp for (i = 0; i < nitems(got_errors); i++) {
266 73e7eb7d 2020-12-15 stsp if (code == got_errors[i].code) {
267 c884fd0a 2020-12-21 stsp err->code = code;
268 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
269 73e7eb7d 2020-12-15 stsp got_errors[i].msg);
270 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
271 c884fd0a 2020-12-21 stsp return err;
272 73e7eb7d 2020-12-15 stsp }
273 73e7eb7d 2020-12-15 stsp }
274 73e7eb7d 2020-12-15 stsp
275 73e7eb7d 2020-12-15 stsp abort();
276 73e7eb7d 2020-12-15 stsp }