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>
26 #include "got_compat.h"
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_lib_delta.h"
32 #include "got_lib_inflate.h"
33 #include "got_lib_object.h"
34 #include "got_lib_sha1.h"
36 #ifndef nitems
37 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 #endif
40 #if defined(__GLIBC__)
41 /*
42 * The autoconf test for strerror_r is broken in current versions
43 * of autoconf: https://savannah.gnu.org/support/?110367
44 */
45 #define strerror_r __xpg_strerror_r
46 #endif
48 static struct got_custom_error {
49 struct got_error err;
50 char msg[4080];
51 } custom_errors[16];
53 static struct got_custom_error *
54 get_custom_err(void)
55 {
56 static unsigned int idx;
57 return &custom_errors[(idx++) % nitems(custom_errors)];
58 }
60 const struct got_error *
61 got_error(int code)
62 {
63 size_t i;
65 for (i = 0; i < nitems(got_errors); i++) {
66 if (code == got_errors[i].code)
67 return &got_errors[i];
68 }
70 abort();
71 }
73 const struct got_error *
74 got_error_msg(int code, const char *msg)
75 {
76 struct got_custom_error *cerr = get_custom_err();
77 struct got_error *err = &cerr->err;
78 size_t i;
80 for (i = 0; i < nitems(got_errors); i++) {
81 if (code == got_errors[i].code) {
82 err->code = code;
83 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
84 err->msg = cerr->msg;
85 return err;
86 }
87 }
89 abort();
90 }
92 const struct got_error *
93 got_error_from_errno(const char *prefix)
94 {
95 struct got_custom_error *cerr = get_custom_err();
96 struct got_error *err = &cerr->err;
97 char strerr[128];
99 strerror_r(errno, strerr, sizeof(strerr));
100 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
102 err->code = GOT_ERR_ERRNO;
103 err->msg = cerr->msg;
104 return err;
107 const struct got_error *
108 got_error_from_errno2(const char *prefix, const char *prefix2)
110 struct got_custom_error *cerr = get_custom_err();
111 struct got_error *err = &cerr->err;
112 char strerr[128];
114 strerror_r(errno, strerr, sizeof(strerr));
115 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
116 strerr);
118 err->code = GOT_ERR_ERRNO;
119 err->msg = cerr->msg;
120 return err;
123 const struct got_error *
124 got_error_from_errno3(const char *prefix, const char *prefix2,
125 const char *prefix3)
127 struct got_custom_error *cerr = get_custom_err();
128 struct got_error *err = &cerr->err;
129 char strerr[128];
131 strerror_r(errno, strerr, sizeof(strerr));
132 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
133 prefix2, prefix3, strerr);
135 err->code = GOT_ERR_ERRNO;
136 err->msg = cerr->msg;
137 return err;
140 const struct got_error *
141 got_error_from_errno_fmt(const char *fmt, ...)
143 struct got_custom_error *cerr = get_custom_err();
144 struct got_error *err = &cerr->err;
145 char buf[PATH_MAX * 4];
146 char strerr[128];
147 va_list ap;
149 va_start(ap, fmt);
150 vsnprintf(buf, sizeof(buf), fmt, ap);
151 va_end(ap);
153 strerror_r(errno, strerr, sizeof(strerr));
154 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
156 err->code = GOT_ERR_ERRNO;
157 err->msg = cerr->msg;
158 return err;
161 const struct got_error *
162 got_error_set_errno(int code, const char *prefix)
164 errno = code;
165 return got_error_from_errno(prefix);
168 const struct got_error *
169 got_ferror(FILE *f, int code)
171 if (ferror(f))
172 return got_error_from_errno("");
173 return got_error(code);
176 const struct got_error *
177 got_error_no_obj(struct got_object_id *id)
179 char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
180 char id_str[SHA1_DIGEST_STRING_LENGTH];
181 int ret;
183 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
184 return got_error(GOT_ERR_NO_OBJ);
186 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
187 if (ret == -1 || ret >= sizeof(msg))
188 return got_error(GOT_ERR_NO_OBJ);
190 return got_error_msg(GOT_ERR_NO_OBJ, msg);
193 const struct got_error *
194 got_error_not_ref(const char *refname)
196 char msg[sizeof("reference not found") + 1004];
197 int ret;
199 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
200 if (ret == -1 || ret >= sizeof(msg))
201 return got_error(GOT_ERR_NOT_REF);
203 return got_error_msg(GOT_ERR_NOT_REF, msg);
206 const struct got_error *
207 got_error_uuid(uint32_t uuid_status, const char *prefix)
209 switch (uuid_status) {
210 case uuid_s_ok:
211 return NULL;
212 case uuid_s_bad_version:
213 return got_error(GOT_ERR_UUID_VERSION);
214 case uuid_s_invalid_string_uuid:
215 return got_error(GOT_ERR_UUID_INVALID);
216 case uuid_s_no_memory:
217 return got_error_set_errno(ENOMEM, prefix);
218 default:
219 return got_error(GOT_ERR_UUID);
223 const struct got_error *
224 got_error_path(const char *path, int code)
226 struct got_custom_error *cerr = get_custom_err();
227 struct got_error *err = &cerr->err;
228 size_t i;
230 for (i = 0; i < nitems(got_errors); i++) {
231 if (code == got_errors[i].code) {
232 err->code = code;
233 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
234 got_errors[i].msg);
235 err->msg = cerr->msg;
236 return err;
240 abort();
243 const struct got_error *
244 got_error_fmt(int code, const char *fmt, ...)
246 struct got_custom_error *cerr = get_custom_err();
247 struct got_error *err = &cerr->err;
248 char buf[PATH_MAX * 4];
249 va_list ap;
250 size_t i;
252 va_start(ap, fmt);
253 vsnprintf(buf, sizeof(buf), fmt, ap);
254 va_end(ap);
256 for (i = 0; i < nitems(got_errors); i++) {
257 if (code == got_errors[i].code) {
258 err->code = code;
259 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
260 got_errors[i].msg);
261 err->msg = cerr->msg;
262 return err;
266 abort();
269 int
270 got_err_open_nofollow_on_symlink(void)
272 /*
273 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
274 * Posix mandates ELOOP and OpenBSD follows it. Others return
275 * different error codes. We carry this workaround to help the
276 * portable version a little.
277 */
278 return (errno == ELOOP
279 #ifdef EMLINK
280 || errno == EMLINK
281 #endif
282 #ifdef EFTYPE
283 || errno == EFTYPE
284 #endif
285 );