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 strerror_r(errno, strerr, sizeof(strerr));
155 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
157 err->code = GOT_ERR_ERRNO;
158 err->msg = cerr->msg;
159 return err;
162 const struct got_error *
163 got_error_set_errno(int code, const char *prefix)
165 errno = code;
166 return got_error_from_errno(prefix);
169 const struct got_error *
170 got_ferror(FILE *f, int code)
172 if (ferror(f))
173 return got_error_from_errno("");
174 return got_error(code);
177 const struct got_error *
178 got_error_no_obj(struct got_object_id *id)
180 char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
181 char id_str[SHA1_DIGEST_STRING_LENGTH];
182 int ret;
184 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
185 return got_error(GOT_ERR_NO_OBJ);
187 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
188 if (ret == -1 || ret >= sizeof(msg))
189 return got_error(GOT_ERR_NO_OBJ);
191 return got_error_msg(GOT_ERR_NO_OBJ, msg);
194 const struct got_error *
195 got_error_not_ref(const char *refname)
197 char msg[sizeof("reference not found") + 1004];
198 int ret;
200 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
201 if (ret == -1 || ret >= sizeof(msg))
202 return got_error(GOT_ERR_NOT_REF);
204 return got_error_msg(GOT_ERR_NOT_REF, msg);
207 const struct got_error *
208 got_error_uuid(uint32_t uuid_status, const char *prefix)
210 switch (uuid_status) {
211 case uuid_s_ok:
212 return NULL;
213 case uuid_s_bad_version:
214 return got_error(GOT_ERR_UUID_VERSION);
215 case uuid_s_invalid_string_uuid:
216 return got_error(GOT_ERR_UUID_INVALID);
217 case uuid_s_no_memory:
218 return got_error_set_errno(ENOMEM, prefix);
219 default:
220 return got_error(GOT_ERR_UUID);
224 const struct got_error *
225 got_error_path(const char *path, int code)
227 struct got_custom_error *cerr = get_custom_err();
228 struct got_error *err = &cerr->err;
229 size_t i;
231 for (i = 0; i < nitems(got_errors); i++) {
232 if (code == got_errors[i].code) {
233 err->code = code;
234 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
235 got_errors[i].msg);
236 err->msg = cerr->msg;
237 return err;
241 abort();
244 const struct got_error *
245 got_error_fmt(int code, const char *fmt, ...)
247 struct got_custom_error *cerr = get_custom_err();
248 struct got_error *err = &cerr->err;
249 char buf[PATH_MAX * 4];
250 va_list ap;
251 size_t i;
253 va_start(ap, fmt);
254 vsnprintf(buf, sizeof(buf), fmt, ap);
255 va_end(ap);
257 for (i = 0; i < nitems(got_errors); i++) {
258 if (code == got_errors[i].code) {
259 err->code = code;
260 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
261 got_errors[i].msg);
262 err->msg = cerr->msg;
263 return err;
267 abort();
270 int
271 got_err_open_nofollow_on_symlink(void)
273 /*
274 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
275 * Posix mandates ELOOP and OpenBSD follows it. Others return
276 * different error codes. We carry this workaround to help the
277 * portable version a little.
278 */
279 return (errno == ELOOP
280 #ifdef EMLINK
281 || errno == EMLINK
282 #endif
283 #ifdef EFTYPE
284 || errno == EFTYPE
285 #endif
286 );