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 #include <sys/queue.h>
18 91a3d81f 2018-11-11 stsp
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>
28 f334529e 2018-01-12 stsp
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 2b4402a2 2017-11-05 stsp #endif
40 4027f31a 2017-11-04 stsp
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];
45 c884fd0a 2020-12-21 stsp
46 c884fd0a 2020-12-21 stsp static struct got_custom_error *
47 c884fd0a 2020-12-21 stsp get_custom_err(void)
48 c884fd0a 2020-12-21 stsp {
49 c884fd0a 2020-12-21 stsp static unsigned int idx;
50 c884fd0a 2020-12-21 stsp return &custom_errors[(idx++) % nitems(custom_errors)];
51 c884fd0a 2020-12-21 stsp }
52 c884fd0a 2020-12-21 stsp
53 4027f31a 2017-11-04 stsp const struct got_error *
54 4027f31a 2017-11-04 stsp got_error(int code)
55 4027f31a 2017-11-04 stsp {
56 16aeacf7 2020-11-26 stsp size_t i;
57 4027f31a 2017-11-04 stsp
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];
61 4027f31a 2017-11-04 stsp }
62 4027f31a 2017-11-04 stsp
63 f334529e 2018-01-12 stsp abort();
64 4027f31a 2017-11-04 stsp }
65 f334529e 2018-01-12 stsp
66 f334529e 2018-01-12 stsp const struct got_error *
67 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
68 91a3d81f 2018-11-11 stsp {
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;
71 16aeacf7 2020-11-26 stsp size_t i;
72 91a3d81f 2018-11-11 stsp
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;
78 c884fd0a 2020-12-21 stsp return err;
79 91a3d81f 2018-11-11 stsp }
80 91a3d81f 2018-11-11 stsp }
81 91a3d81f 2018-11-11 stsp
82 91a3d81f 2018-11-11 stsp abort();
83 91a3d81f 2018-11-11 stsp }
84 91a3d81f 2018-11-11 stsp
85 91a3d81f 2018-11-11 stsp const struct got_error *
86 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
87 f334529e 2018-01-12 stsp {
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;
90 f334529e 2018-01-12 stsp
91 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix,
92 230a42bd 2019-05-11 jcs strerror(errno));
93 230a42bd 2019-05-11 jcs
94 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
95 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
96 c884fd0a 2020-12-21 stsp return err;
97 f334529e 2018-01-12 stsp }
98 8251fdbc 2018-01-12 stsp
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)
101 48b8b0eb 2019-05-11 jcs {
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;
104 48b8b0eb 2019-05-11 jcs
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));
107 48b8b0eb 2019-05-11 jcs
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;
111 48b8b0eb 2019-05-11 jcs }
112 48b8b0eb 2019-05-11 jcs
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)
116 230a42bd 2019-05-11 jcs {
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;
119 230a42bd 2019-05-11 jcs
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));
122 230a42bd 2019-05-11 jcs
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;
126 230a42bd 2019-05-11 jcs }
127 230a42bd 2019-05-11 jcs
128 230a42bd 2019-05-11 jcs const struct got_error *
129 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
130 4cc6a5a5 2020-12-15 stsp {
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;
135 4cc6a5a5 2020-12-15 stsp
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);
139 4cc6a5a5 2020-12-15 stsp
140 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerror(errno));
141 4cc6a5a5 2020-12-15 stsp
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;
145 4cc6a5a5 2020-12-15 stsp }
146 4cc6a5a5 2020-12-15 stsp
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)
149 1a76625f 2018-10-22 stsp {
150 1a76625f 2018-10-22 stsp errno = code;
151 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
152 1a76625f 2018-10-22 stsp }
153 1a76625f 2018-10-22 stsp
154 1a76625f 2018-10-22 stsp const struct got_error *
155 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
156 8251fdbc 2018-01-12 stsp {
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);
160 8251fdbc 2018-01-12 stsp }
161 91a3d81f 2018-11-11 stsp
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)
164 91a3d81f 2018-11-11 stsp {
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];
167 91a3d81f 2018-11-11 stsp int ret;
168 91a3d81f 2018-11-11 stsp
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);
171 91a3d81f 2018-11-11 stsp
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);
175 91a3d81f 2018-11-11 stsp
176 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
177 91a3d81f 2018-11-11 stsp }
178 2aa0475c 2019-02-03 stsp
179 2aa0475c 2019-02-03 stsp const struct got_error *
180 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
181 2aa0475c 2019-02-03 stsp {
182 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
183 2aa0475c 2019-02-03 stsp int ret;
184 2aa0475c 2019-02-03 stsp
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);
188 2aa0475c 2019-02-03 stsp
189 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
190 2aa0475c 2019-02-03 stsp }
191 09589288 2019-03-10 stsp
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)
194 09589288 2019-03-10 stsp {
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);
204 09589288 2019-03-10 stsp default:
205 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
206 09589288 2019-03-10 stsp }
207 09589288 2019-03-10 stsp }
208 df056ada 2019-05-15 stsp
209 df056ada 2019-05-15 stsp const struct got_error *
210 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
211 df056ada 2019-05-15 stsp {
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;
214 16aeacf7 2020-11-26 stsp size_t i;
215 df056ada 2019-05-15 stsp
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;
223 df056ada 2019-05-15 stsp }
224 df056ada 2019-05-15 stsp }
225 df056ada 2019-05-15 stsp
226 df056ada 2019-05-15 stsp abort();
227 df056ada 2019-05-15 stsp }
228 73e7eb7d 2020-12-15 stsp
229 73e7eb7d 2020-12-15 stsp const struct got_error *
230 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
231 73e7eb7d 2020-12-15 stsp {
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;
236 73e7eb7d 2020-12-15 stsp size_t i;
237 73e7eb7d 2020-12-15 stsp
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);
241 73e7eb7d 2020-12-15 stsp
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;
249 73e7eb7d 2020-12-15 stsp }
250 73e7eb7d 2020-12-15 stsp }
251 73e7eb7d 2020-12-15 stsp
252 73e7eb7d 2020-12-15 stsp abort();
253 73e7eb7d 2020-12-15 stsp }