Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <zlib.h>
25 #include <uuid.h>
27 #include "got_compat.h"
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_delta.h"
33 #include "got_lib_inflate.h"
34 #include "got_lib_object.h"
35 #include "got_lib_sha1.h"
37 #ifndef nitems
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 #endif
41 #if defined(__GLIBC__)
42 /*
43 * The autoconf test for strerror_r is broken in current versions
44 * of autoconf: https://savannah.gnu.org/support/?110367
45 */
46 #define strerror_r __xpg_strerror_r
47 #endif
49 static struct got_custom_error {
50 struct got_error err;
51 char msg[4080];
52 } custom_errors[16];
54 static struct got_custom_error *
55 get_custom_err(void)
56 {
57 static unsigned int idx;
58 return &custom_errors[(idx++) % nitems(custom_errors)];
59 }
61 const struct got_error *
62 got_error(int code)
63 {
64 size_t i;
66 for (i = 0; i < nitems(got_errors); i++) {
67 if (code == got_errors[i].code)
68 return &got_errors[i];
69 }
71 abort();
72 }
74 const struct got_error *
75 got_error_msg(int code, const char *msg)
76 {
77 struct got_custom_error *cerr = get_custom_err();
78 struct got_error *err = &cerr->err;
79 size_t i;
81 for (i = 0; i < nitems(got_errors); i++) {
82 if (code == got_errors[i].code) {
83 err->code = code;
84 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
85 err->msg = cerr->msg;
86 return err;
87 }
88 }
90 abort();
91 }
93 const struct got_error *
94 got_error_from_errno(const char *prefix)
95 {
96 struct got_custom_error *cerr = get_custom_err();
97 struct got_error *err = &cerr->err;
98 char strerr[128];
100 strerror_r(errno, strerr, sizeof(strerr));
101 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
103 err->code = GOT_ERR_ERRNO;
104 err->msg = cerr->msg;
105 return err;
108 const struct got_error *
109 got_error_from_errno2(const char *prefix, const char *prefix2)
111 struct got_custom_error *cerr = get_custom_err();
112 struct got_error *err = &cerr->err;
113 char strerr[128];
115 strerror_r(errno, strerr, sizeof(strerr));
116 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
117 strerr);
119 err->code = GOT_ERR_ERRNO;
120 err->msg = cerr->msg;
121 return err;
124 const struct got_error *
125 got_error_from_errno3(const char *prefix, const char *prefix2,
126 const char *prefix3)
128 struct got_custom_error *cerr = get_custom_err();
129 struct got_error *err = &cerr->err;
130 char strerr[128];
132 strerror_r(errno, strerr, sizeof(strerr));
133 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
134 prefix2, prefix3, strerr);
136 err->code = GOT_ERR_ERRNO;
137 err->msg = cerr->msg;
138 return err;
141 const struct got_error *
142 got_error_from_errno_fmt(const char *fmt, ...)
144 struct got_custom_error *cerr = get_custom_err();
145 struct got_error *err = &cerr->err;
146 char buf[PATH_MAX * 4];
147 char strerr[128];
148 va_list ap;
150 va_start(ap, fmt);
151 vsnprintf(buf, sizeof(buf), fmt, ap);
152 va_end(ap);
154 #ifdef __GLIBC__
155 /*
156 * The autoconf test for strerror_r is broken in current versions
157 * of autoconf: https://savannah.gnu.org/support/?110367
158 */
159 __xpg_strerror_r(errno, strerr, sizeof(strerr));
160 #else
161 strerror_r(errno, strerr, sizeof(strerr));
162 #endif
163 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
165 err->code = GOT_ERR_ERRNO;
166 err->msg = cerr->msg;
167 return err;
170 const struct got_error *
171 got_error_set_errno(int code, const char *prefix)
173 errno = code;
174 return got_error_from_errno(prefix);
177 const struct got_error *
178 got_ferror(FILE *f, int code)
180 if (ferror(f))
181 return got_error_from_errno("");
182 return got_error(code);
185 const struct got_error *
186 got_error_no_obj(struct got_object_id *id)
188 char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
189 char id_str[SHA1_DIGEST_STRING_LENGTH];
190 int ret;
192 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
193 return got_error(GOT_ERR_NO_OBJ);
195 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
196 if (ret == -1 || ret >= sizeof(msg))
197 return got_error(GOT_ERR_NO_OBJ);
199 return got_error_msg(GOT_ERR_NO_OBJ, msg);
202 const struct got_error *
203 got_error_not_ref(const char *refname)
205 char msg[sizeof("reference not found") + 1004];
206 int ret;
208 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
209 if (ret == -1 || ret >= sizeof(msg))
210 return got_error(GOT_ERR_NOT_REF);
212 return got_error_msg(GOT_ERR_NOT_REF, msg);
215 const struct got_error *
216 got_error_uuid(uint32_t uuid_status, const char *prefix)
218 switch (uuid_status) {
219 case uuid_s_ok:
220 return NULL;
221 case uuid_s_bad_version:
222 return got_error(GOT_ERR_UUID_VERSION);
223 case uuid_s_invalid_string_uuid:
224 return got_error(GOT_ERR_UUID_INVALID);
225 case uuid_s_no_memory:
226 return got_error_set_errno(ENOMEM, prefix);
227 default:
228 return got_error(GOT_ERR_UUID);
232 const struct got_error *
233 got_error_path(const char *path, int code)
235 struct got_custom_error *cerr = get_custom_err();
236 struct got_error *err = &cerr->err;
237 size_t i;
239 for (i = 0; i < nitems(got_errors); i++) {
240 if (code == got_errors[i].code) {
241 err->code = code;
242 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
243 got_errors[i].msg);
244 err->msg = cerr->msg;
245 return err;
249 abort();
252 const struct got_error *
253 got_error_fmt(int code, const char *fmt, ...)
255 struct got_custom_error *cerr = get_custom_err();
256 struct got_error *err = &cerr->err;
257 char buf[PATH_MAX * 4];
258 va_list ap;
259 size_t i;
261 va_start(ap, fmt);
262 vsnprintf(buf, sizeof(buf), fmt, ap);
263 va_end(ap);
265 for (i = 0; i < nitems(got_errors); i++) {
266 if (code == got_errors[i].code) {
267 err->code = code;
268 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
269 got_errors[i].msg);
270 err->msg = cerr->msg;
271 return err;
275 abort();
278 int
279 got_err_open_nofollow_on_symlink(void)
281 /*
282 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
283 * Posix mandates ELOOP and OpenBSD follows it. Others return
284 * different error codes. We carry this workaround to help the
285 * portable version a little.
286 */
287 return (errno == ELOOP
288 #ifdef EMLINK
289 || errno == EMLINK
290 #endif
291 #ifdef EFTYPE
292 || errno == EFTYPE
293 #endif
294 );