2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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"
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #if defined(__GLIBC__)
44 * The autoconf test for strerror_r is broken in current versions
45 * of autoconf: https://savannah.gnu.org/support/?110367
47 #define strerror_r __xpg_strerror_r
50 static struct got_custom_error {
55 static struct got_custom_error *
58 static unsigned int idx;
59 return &custom_errors[(idx++) % nitems(custom_errors)];
62 const struct got_error *
67 for (i = 0; i < nitems(got_errors); i++) {
68 if (code == got_errors[i].code)
69 return &got_errors[i];
75 const struct got_error *
76 got_error_msg(int code, const char *msg)
78 struct got_custom_error *cerr = get_custom_err();
79 struct got_error *err = &cerr->err;
82 for (i = 0; i < nitems(got_errors); i++) {
83 if (code == got_errors[i].code) {
85 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
94 const struct got_error *
95 got_error_from_errno(const char *prefix)
97 struct got_custom_error *cerr = get_custom_err();
98 struct got_error *err = &cerr->err;
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;
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;
116 strerror_r(errno, strerr, sizeof(strerr));
117 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
120 err->code = GOT_ERR_ERRNO;
121 err->msg = cerr->msg;
125 const struct got_error *
126 got_error_from_errno3(const char *prefix, const char *prefix2,
129 struct got_custom_error *cerr = get_custom_err();
130 struct got_error *err = &cerr->err;
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;
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];
152 vsnprintf(buf, sizeof(buf), fmt, ap);
157 * The autoconf test for strerror_r is broken in current versions
158 * of autoconf: https://savannah.gnu.org/support/?110367
160 __xpg_strerror_r(errno, strerr, sizeof(strerr));
162 strerror_r(errno, strerr, sizeof(strerr));
164 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
166 err->code = GOT_ERR_ERRNO;
167 err->msg = cerr->msg;
171 const struct got_error *
172 got_error_set_errno(int code, const char *prefix)
175 return got_error_from_errno(prefix);
178 const struct got_error *
179 got_ferror(FILE *f, int code)
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];
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];
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) {
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);
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;
240 for (i = 0; i < nitems(got_errors); i++) {
241 if (code == got_errors[i].code) {
243 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
245 err->msg = cerr->msg;
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];
263 vsnprintf(buf, sizeof(buf), fmt, ap);
266 for (i = 0; i < nitems(got_errors); i++) {
267 if (code == got_errors[i].code) {
269 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
271 err->msg = cerr->msg;