2 72bcf0f9 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
28 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
30 4027f31a 2017-11-04 stsp #include "got_error.h"
31 91a3d81f 2018-11-11 stsp #include "got_object.h"
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"
38 2b4402a2 2017-11-05 stsp #ifndef nitems
39 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 f0678b77 2021-09-21 thomas.ad #if defined(__GLIBC__)
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
47 f0678b77 2021-09-21 thomas.ad #define strerror_r __xpg_strerror_r
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];
55 c884fd0a 2020-12-21 stsp static struct got_custom_error *
56 c884fd0a 2020-12-21 stsp get_custom_err(void)
58 c884fd0a 2020-12-21 stsp static unsigned int idx;
59 c884fd0a 2020-12-21 stsp return &custom_errors[(idx++) % nitems(custom_errors)];
62 4027f31a 2017-11-04 stsp const struct got_error *
63 4027f31a 2017-11-04 stsp got_error(int code)
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];
75 f334529e 2018-01-12 stsp const struct got_error *
76 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
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;
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;
94 91a3d81f 2018-11-11 stsp const struct got_error *
95 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
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];
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);
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;
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)
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];
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,
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;
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)
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];
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);
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;
142 230a42bd 2019-05-11 jcs const struct got_error *
143 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
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;
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);
155 f0678b77 2021-09-21 thomas.ad #ifdef __GLIBC__
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
160 f0678b77 2021-09-21 thomas.ad __xpg_strerror_r(errno, strerr, sizeof(strerr));
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);
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;
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)
174 1a76625f 2018-10-22 stsp errno = code;
175 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
178 1a76625f 2018-10-22 stsp const struct got_error *
179 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
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);
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)
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];
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);
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);
200 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
203 2aa0475c 2019-02-03 stsp const struct got_error *
204 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
206 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
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);
213 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
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)
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);
229 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
233 df056ada 2019-05-15 stsp const struct got_error *
234 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
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;
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;
253 73e7eb7d 2020-12-15 stsp const struct got_error *
254 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
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;
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);
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;