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.
17 91a3d81f 2018-11-11 stsp #include <sys/queue.h>
19 f334529e 2018-01-12 stsp #include <errno.h>
20 7d45c7f1 2019-05-15 stsp #include <limits.h>
21 4cc6a5a5 2020-12-15 stsp #include <stdarg.h>
22 8251fdbc 2018-01-12 stsp #include <stdio.h>
23 f334529e 2018-01-12 stsp #include <stdlib.h>
24 f334529e 2018-01-12 stsp #include <string.h>
25 91a3d81f 2018-11-11 stsp #include <sha1.h>
26 91a3d81f 2018-11-11 stsp #include <zlib.h>
27 09589288 2019-03-10 stsp #include <uuid.h>
29 4027f31a 2017-11-04 stsp #include "got_error.h"
30 91a3d81f 2018-11-11 stsp #include "got_object.h"
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"
37 2b4402a2 2017-11-05 stsp #ifndef nitems
38 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 c884fd0a 2020-12-21 stsp static struct got_custom_error {
42 c884fd0a 2020-12-21 stsp struct got_error err;
43 c884fd0a 2020-12-21 stsp char msg[4080];
44 c884fd0a 2020-12-21 stsp } custom_errors[16];
46 c884fd0a 2020-12-21 stsp static struct got_custom_error *
47 c884fd0a 2020-12-21 stsp get_custom_err(void)
49 c884fd0a 2020-12-21 stsp static unsigned int idx;
50 c884fd0a 2020-12-21 stsp return &custom_errors[(idx++) % nitems(custom_errors)];
53 4027f31a 2017-11-04 stsp const struct got_error *
54 4027f31a 2017-11-04 stsp got_error(int code)
58 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
59 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
60 4027f31a 2017-11-04 stsp return &got_errors[i];
66 f334529e 2018-01-12 stsp const struct got_error *
67 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
69 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
70 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
73 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
74 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
75 c884fd0a 2020-12-21 stsp err->code = code;
76 c884fd0a 2020-12-21 stsp strlcpy(cerr->msg, msg, sizeof(cerr->msg));
77 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
85 91a3d81f 2018-11-11 stsp const struct got_error *
86 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
88 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
89 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
91 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix,
92 230a42bd 2019-05-11 jcs strerror(errno));
94 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
95 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
99 8251fdbc 2018-01-12 stsp const struct got_error *
100 638f9024 2019-05-13 stsp got_error_from_errno2(const char *prefix, const char *prefix2)
102 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
103 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
105 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
106 48b8b0eb 2019-05-11 jcs strerror(errno));
108 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
109 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
110 c884fd0a 2020-12-21 stsp return err;
113 48b8b0eb 2019-05-11 jcs const struct got_error *
114 638f9024 2019-05-13 stsp got_error_from_errno3(const char *prefix, const char *prefix2,
115 230a42bd 2019-05-11 jcs const char *prefix3)
117 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
118 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
120 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
121 c884fd0a 2020-12-21 stsp prefix2, prefix3, strerror(errno));
123 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
124 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
125 c884fd0a 2020-12-21 stsp return err;
128 230a42bd 2019-05-11 jcs const struct got_error *
129 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
131 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
132 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
133 4cc6a5a5 2020-12-15 stsp char buf[PATH_MAX * 4];
134 4cc6a5a5 2020-12-15 stsp va_list ap;
136 4cc6a5a5 2020-12-15 stsp va_start(ap, fmt);
137 4cc6a5a5 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
138 4cc6a5a5 2020-12-15 stsp va_end(ap);
140 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerror(errno));
142 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
143 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
144 c884fd0a 2020-12-21 stsp return err;
147 4cc6a5a5 2020-12-15 stsp const struct got_error *
148 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
150 1a76625f 2018-10-22 stsp errno = code;
151 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
154 1a76625f 2018-10-22 stsp const struct got_error *
155 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
157 8251fdbc 2018-01-12 stsp if (ferror(f))
158 638f9024 2019-05-13 stsp return got_error_from_errno("");
159 8251fdbc 2018-01-12 stsp return got_error(code);
162 91a3d81f 2018-11-11 stsp const struct got_error *
163 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
165 c884fd0a 2020-12-21 stsp char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
166 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
169 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
170 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
172 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
173 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
174 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
176 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
179 2aa0475c 2019-02-03 stsp const struct got_error *
180 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
182 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
185 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
186 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
187 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
189 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
192 09589288 2019-03-10 stsp const struct got_error *
193 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
195 09589288 2019-03-10 stsp switch (uuid_status) {
196 09589288 2019-03-10 stsp case uuid_s_ok:
197 09589288 2019-03-10 stsp return NULL;
198 09589288 2019-03-10 stsp case uuid_s_bad_version:
199 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
200 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
201 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
202 09589288 2019-03-10 stsp case uuid_s_no_memory:
203 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
205 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
209 df056ada 2019-05-15 stsp const struct got_error *
210 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
212 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
213 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
216 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
217 df056ada 2019-05-15 stsp if (code == got_errors[i].code) {
218 c884fd0a 2020-12-21 stsp err->code = code;
219 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
220 df056ada 2019-05-15 stsp got_errors[i].msg);
221 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
222 c884fd0a 2020-12-21 stsp return err;
229 73e7eb7d 2020-12-15 stsp const struct got_error *
230 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
232 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
233 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
234 73e7eb7d 2020-12-15 stsp char buf[PATH_MAX * 4];
235 73e7eb7d 2020-12-15 stsp va_list ap;
238 73e7eb7d 2020-12-15 stsp va_start(ap, fmt);
239 73e7eb7d 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
240 73e7eb7d 2020-12-15 stsp va_end(ap);
242 73e7eb7d 2020-12-15 stsp for (i = 0; i < nitems(got_errors); i++) {
243 73e7eb7d 2020-12-15 stsp if (code == got_errors[i].code) {
244 c884fd0a 2020-12-21 stsp err->code = code;
245 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
246 73e7eb7d 2020-12-15 stsp got_errors[i].msg);
247 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
248 c884fd0a 2020-12-21 stsp return err;