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 9a02f8b7 2020-12-21 stsp char strerr[128];
91 f334529e 2018-01-12 stsp
92 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
93 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
94 230a42bd 2019-05-11 jcs
95 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
96 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
97 c884fd0a 2020-12-21 stsp return err;
98 f334529e 2018-01-12 stsp }
99 8251fdbc 2018-01-12 stsp
100 8251fdbc 2018-01-12 stsp const struct got_error *
101 638f9024 2019-05-13 stsp got_error_from_errno2(const char *prefix, const char *prefix2)
102 48b8b0eb 2019-05-11 jcs {
103 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
104 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
105 9a02f8b7 2020-12-21 stsp char strerr[128];
106 48b8b0eb 2019-05-11 jcs
107 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
108 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
109 9a02f8b7 2020-12-21 stsp strerr);
110 48b8b0eb 2019-05-11 jcs
111 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
112 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
113 c884fd0a 2020-12-21 stsp return err;
114 48b8b0eb 2019-05-11 jcs }
115 48b8b0eb 2019-05-11 jcs
116 48b8b0eb 2019-05-11 jcs const struct got_error *
117 638f9024 2019-05-13 stsp got_error_from_errno3(const char *prefix, const char *prefix2,
118 230a42bd 2019-05-11 jcs const char *prefix3)
119 230a42bd 2019-05-11 jcs {
120 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
121 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
122 9a02f8b7 2020-12-21 stsp char strerr[128];
123 230a42bd 2019-05-11 jcs
124 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
125 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
126 9a02f8b7 2020-12-21 stsp prefix2, prefix3, strerr);
127 230a42bd 2019-05-11 jcs
128 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
129 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
130 c884fd0a 2020-12-21 stsp return err;
131 230a42bd 2019-05-11 jcs }
132 230a42bd 2019-05-11 jcs
133 230a42bd 2019-05-11 jcs const struct got_error *
134 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
135 4cc6a5a5 2020-12-15 stsp {
136 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
137 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
138 4cc6a5a5 2020-12-15 stsp char buf[PATH_MAX * 4];
139 9a02f8b7 2020-12-21 stsp char strerr[128];
140 4cc6a5a5 2020-12-15 stsp va_list ap;
141 4cc6a5a5 2020-12-15 stsp
142 4cc6a5a5 2020-12-15 stsp va_start(ap, fmt);
143 4cc6a5a5 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
144 4cc6a5a5 2020-12-15 stsp va_end(ap);
145 4cc6a5a5 2020-12-15 stsp
146 9a02f8b7 2020-12-21 stsp strerror_r(errno, strerr, sizeof(strerr));
147 9a02f8b7 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
148 4cc6a5a5 2020-12-15 stsp
149 c884fd0a 2020-12-21 stsp err->code = GOT_ERR_ERRNO;
150 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
151 c884fd0a 2020-12-21 stsp return err;
152 4cc6a5a5 2020-12-15 stsp }
153 4cc6a5a5 2020-12-15 stsp
154 4cc6a5a5 2020-12-15 stsp const struct got_error *
155 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
156 1a76625f 2018-10-22 stsp {
157 1a76625f 2018-10-22 stsp errno = code;
158 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
159 1a76625f 2018-10-22 stsp }
160 1a76625f 2018-10-22 stsp
161 1a76625f 2018-10-22 stsp const struct got_error *
162 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
163 8251fdbc 2018-01-12 stsp {
164 8251fdbc 2018-01-12 stsp if (ferror(f))
165 638f9024 2019-05-13 stsp return got_error_from_errno("");
166 8251fdbc 2018-01-12 stsp return got_error(code);
167 8251fdbc 2018-01-12 stsp }
168 91a3d81f 2018-11-11 stsp
169 91a3d81f 2018-11-11 stsp const struct got_error *
170 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
171 91a3d81f 2018-11-11 stsp {
172 c884fd0a 2020-12-21 stsp char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
173 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
174 91a3d81f 2018-11-11 stsp int ret;
175 91a3d81f 2018-11-11 stsp
176 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
177 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
178 91a3d81f 2018-11-11 stsp
179 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
180 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
181 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
182 91a3d81f 2018-11-11 stsp
183 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
184 91a3d81f 2018-11-11 stsp }
185 2aa0475c 2019-02-03 stsp
186 2aa0475c 2019-02-03 stsp const struct got_error *
187 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
188 2aa0475c 2019-02-03 stsp {
189 c884fd0a 2020-12-21 stsp char msg[sizeof("reference not found") + 1004];
190 2aa0475c 2019-02-03 stsp int ret;
191 2aa0475c 2019-02-03 stsp
192 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
193 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
194 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
195 2aa0475c 2019-02-03 stsp
196 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
197 2aa0475c 2019-02-03 stsp }
198 09589288 2019-03-10 stsp
199 09589288 2019-03-10 stsp const struct got_error *
200 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
201 09589288 2019-03-10 stsp {
202 09589288 2019-03-10 stsp switch (uuid_status) {
203 09589288 2019-03-10 stsp case uuid_s_ok:
204 09589288 2019-03-10 stsp return NULL;
205 09589288 2019-03-10 stsp case uuid_s_bad_version:
206 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
207 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
208 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
209 09589288 2019-03-10 stsp case uuid_s_no_memory:
210 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
211 09589288 2019-03-10 stsp default:
212 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
213 09589288 2019-03-10 stsp }
214 09589288 2019-03-10 stsp }
215 df056ada 2019-05-15 stsp
216 df056ada 2019-05-15 stsp const struct got_error *
217 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
218 df056ada 2019-05-15 stsp {
219 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
220 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
221 16aeacf7 2020-11-26 stsp size_t i;
222 df056ada 2019-05-15 stsp
223 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
224 df056ada 2019-05-15 stsp if (code == got_errors[i].code) {
225 c884fd0a 2020-12-21 stsp err->code = code;
226 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
227 df056ada 2019-05-15 stsp got_errors[i].msg);
228 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
229 c884fd0a 2020-12-21 stsp return err;
230 df056ada 2019-05-15 stsp }
231 df056ada 2019-05-15 stsp }
232 df056ada 2019-05-15 stsp
233 df056ada 2019-05-15 stsp abort();
234 df056ada 2019-05-15 stsp }
235 73e7eb7d 2020-12-15 stsp
236 73e7eb7d 2020-12-15 stsp const struct got_error *
237 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
238 73e7eb7d 2020-12-15 stsp {
239 c884fd0a 2020-12-21 stsp struct got_custom_error *cerr = get_custom_err();
240 c884fd0a 2020-12-21 stsp struct got_error *err = &cerr->err;
241 73e7eb7d 2020-12-15 stsp char buf[PATH_MAX * 4];
242 73e7eb7d 2020-12-15 stsp va_list ap;
243 73e7eb7d 2020-12-15 stsp size_t i;
244 73e7eb7d 2020-12-15 stsp
245 73e7eb7d 2020-12-15 stsp va_start(ap, fmt);
246 73e7eb7d 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
247 73e7eb7d 2020-12-15 stsp va_end(ap);
248 73e7eb7d 2020-12-15 stsp
249 73e7eb7d 2020-12-15 stsp for (i = 0; i < nitems(got_errors); i++) {
250 73e7eb7d 2020-12-15 stsp if (code == got_errors[i].code) {
251 c884fd0a 2020-12-21 stsp err->code = code;
252 c884fd0a 2020-12-21 stsp snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
253 73e7eb7d 2020-12-15 stsp got_errors[i].msg);
254 c884fd0a 2020-12-21 stsp err->msg = cerr->msg;
255 c884fd0a 2020-12-21 stsp return err;
256 73e7eb7d 2020-12-15 stsp }
257 73e7eb7d 2020-12-15 stsp }
258 73e7eb7d 2020-12-15 stsp
259 73e7eb7d 2020-12-15 stsp abort();
260 73e7eb7d 2020-12-15 stsp }
261 5c02d2a5 2021-09-26 stsp
262 5c02d2a5 2021-09-26 stsp int
263 5c02d2a5 2021-09-26 stsp got_err_open_nofollow_on_symlink(void)
264 5c02d2a5 2021-09-26 stsp {
265 5c02d2a5 2021-09-26 stsp /*
266 5c02d2a5 2021-09-26 stsp * Check whether open(2) with O_NOFOLLOW failed on a symlink.
267 5c02d2a5 2021-09-26 stsp * Posix mandates ELOOP and OpenBSD follows it. Others return
268 5c02d2a5 2021-09-26 stsp * different error codes. We carry this workaround to help the
269 5c02d2a5 2021-09-26 stsp * portable version a little.
270 5c02d2a5 2021-09-26 stsp */
271 5c02d2a5 2021-09-26 stsp return (errno == ELOOP
272 5c02d2a5 2021-09-26 stsp #ifdef EMLINK
273 5c02d2a5 2021-09-26 stsp || errno == EMLINK
274 5c02d2a5 2021-09-26 stsp #endif
275 5c02d2a5 2021-09-26 stsp #ifdef EFTYPE
276 5c02d2a5 2021-09-26 stsp || errno == EFTYPE
277 5c02d2a5 2021-09-26 stsp #endif
278 5c02d2a5 2021-09-26 stsp );
279 5c02d2a5 2021-09-26 stsp }