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 <sha1.h>
25 #include <zlib.h>
26 #include <uuid.h>
28 #include "got_compat.h"
30 #include "got_error.h"
31 #include "got_object.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_object.h"
36 #include "got_lib_sha1.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #endif
42 #if defined(__GLIBC__)
43 /*
44 * The autoconf test for strerror_r is broken in current versions
45 * of autoconf: https://savannah.gnu.org/support/?110367
46 */
47 #define strerror_r __xpg_strerror_r
48 #endif
50 static struct got_custom_error {
51 struct got_error err;
52 char msg[4080];
53 } custom_errors[16];
55 static struct got_custom_error *
56 get_custom_err(void)
57 {
58 static unsigned int idx;
59 return &custom_errors[(idx++) % nitems(custom_errors)];
60 }
62 const struct got_error *
63 got_error(int code)
64 {
65 size_t i;
67 for (i = 0; i < nitems(got_errors); i++) {
68 if (code == got_errors[i].code)
69 return &got_errors[i];
70 }
72 abort();
73 }
75 const struct got_error *
76 got_error_msg(int code, const char *msg)
77 {
78 struct got_custom_error *cerr = get_custom_err();
79 struct got_error *err = &cerr->err;
80 size_t i;
82 for (i = 0; i < nitems(got_errors); i++) {
83 if (code == got_errors[i].code) {
84 err->code = code;
85 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
86 err->msg = cerr->msg;
87 return err;
88 }
89 }
91 abort();
92 }
94 const struct got_error *
95 got_error_from_errno(const char *prefix)
96 {
97 struct got_custom_error *cerr = get_custom_err();
98 struct got_error *err = &cerr->err;
99 char strerr[128];
101 strerror_r(errno, strerr, sizeof(strerr));
102 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
104 err->code = GOT_ERR_ERRNO;
105 err->msg = cerr->msg;
106 return err;
109 const struct got_error *
110 got_error_from_errno2(const char *prefix, const char *prefix2)
112 struct got_custom_error *cerr = get_custom_err();
113 struct got_error *err = &cerr->err;
114 char strerr[128];
116 strerror_r(errno, strerr, sizeof(strerr));
117 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
118 strerr);
120 err->code = GOT_ERR_ERRNO;
121 err->msg = cerr->msg;
122 return err;
125 const struct got_error *
126 got_error_from_errno3(const char *prefix, const char *prefix2,
127 const char *prefix3)
129 struct got_custom_error *cerr = get_custom_err();
130 struct got_error *err = &cerr->err;
131 char strerr[128];
133 strerror_r(errno, strerr, sizeof(strerr));
134 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
135 prefix2, prefix3, strerr);
137 err->code = GOT_ERR_ERRNO;
138 err->msg = cerr->msg;
139 return err;
142 const struct got_error *
143 got_error_from_errno_fmt(const char *fmt, ...)
145 struct got_custom_error *cerr = get_custom_err();
146 struct got_error *err = &cerr->err;
147 char buf[PATH_MAX * 4];
148 char strerr[128];
149 va_list ap;
151 va_start(ap, fmt);
152 vsnprintf(buf, sizeof(buf), fmt, ap);
153 va_end(ap);
155 #ifdef __GLIBC__
156 /*
157 * The autoconf test for strerror_r is broken in current versions
158 * of autoconf: https://savannah.gnu.org/support/?110367
159 */
160 __xpg_strerror_r(errno, strerr, sizeof(strerr));
161 #else
162 strerror_r(errno, strerr, sizeof(strerr));
163 #endif
164 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
166 err->code = GOT_ERR_ERRNO;
167 err->msg = cerr->msg;
168 return err;
171 const struct got_error *
172 got_error_set_errno(int code, const char *prefix)
174 errno = code;
175 return got_error_from_errno(prefix);
178 const struct got_error *
179 got_ferror(FILE *f, int code)
181 if (ferror(f))
182 return got_error_from_errno("");
183 return got_error(code);
186 const struct got_error *
187 got_error_no_obj(struct got_object_id *id)
189 char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
190 char id_str[SHA1_DIGEST_STRING_LENGTH];
191 int ret;
193 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
194 return got_error(GOT_ERR_NO_OBJ);
196 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
197 if (ret == -1 || ret >= sizeof(msg))
198 return got_error(GOT_ERR_NO_OBJ);
200 return got_error_msg(GOT_ERR_NO_OBJ, msg);
203 const struct got_error *
204 got_error_not_ref(const char *refname)
206 char msg[sizeof("reference not found") + 1004];
207 int ret;
209 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
210 if (ret == -1 || ret >= sizeof(msg))
211 return got_error(GOT_ERR_NOT_REF);
213 return got_error_msg(GOT_ERR_NOT_REF, msg);
216 const struct got_error *
217 got_error_uuid(uint32_t uuid_status, const char *prefix)
219 switch (uuid_status) {
220 case uuid_s_ok:
221 return NULL;
222 case uuid_s_bad_version:
223 return got_error(GOT_ERR_UUID_VERSION);
224 case uuid_s_invalid_string_uuid:
225 return got_error(GOT_ERR_UUID_INVALID);
226 case uuid_s_no_memory:
227 return got_error_set_errno(ENOMEM, prefix);
228 default:
229 return got_error(GOT_ERR_UUID);
233 const struct got_error *
234 got_error_path(const char *path, int code)
236 struct got_custom_error *cerr = get_custom_err();
237 struct got_error *err = &cerr->err;
238 size_t i;
240 for (i = 0; i < nitems(got_errors); i++) {
241 if (code == got_errors[i].code) {
242 err->code = code;
243 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
244 got_errors[i].msg);
245 err->msg = cerr->msg;
246 return err;
250 abort();
253 const struct got_error *
254 got_error_fmt(int code, const char *fmt, ...)
256 struct got_custom_error *cerr = get_custom_err();
257 struct got_error *err = &cerr->err;
258 char buf[PATH_MAX * 4];
259 va_list ap;
260 size_t i;
262 va_start(ap, fmt);
263 vsnprintf(buf, sizeof(buf), fmt, ap);
264 va_end(ap);
266 for (i = 0; i < nitems(got_errors); i++) {
267 if (code == got_errors[i].code) {
268 err->code = code;
269 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
270 got_errors[i].msg);
271 err->msg = cerr->msg;
272 return err;
276 abort();