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.
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"
37 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #if defined(__GLIBC__)
42 * The autoconf test for strerror_r is broken in current versions
43 * of autoconf: https://savannah.gnu.org/support/?110367
45 #define strerror_r __xpg_strerror_r
48 static struct got_custom_error {
53 static struct got_custom_error *
56 static unsigned int idx;
57 return &custom_errors[(idx++) % nitems(custom_errors)];
60 const struct got_error *
65 for (i = 0; i < nitems(got_errors); i++) {
66 if (code == got_errors[i].code)
67 return &got_errors[i];
73 const struct got_error *
74 got_error_msg(int code, const char *msg)
76 struct got_custom_error *cerr = get_custom_err();
77 struct got_error *err = &cerr->err;
80 for (i = 0; i < nitems(got_errors); i++) {
81 if (code == got_errors[i].code) {
83 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
92 const struct got_error *
93 got_error_from_errno(const char *prefix)
95 struct got_custom_error *cerr = get_custom_err();
96 struct got_error *err = &cerr->err;
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;
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;
114 strerror_r(errno, strerr, sizeof(strerr));
115 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
118 err->code = GOT_ERR_ERRNO;
119 err->msg = cerr->msg;
123 const struct got_error *
124 got_error_from_errno3(const char *prefix, const char *prefix2,
127 struct got_custom_error *cerr = get_custom_err();
128 struct got_error *err = &cerr->err;
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;
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];
150 vsnprintf(buf, sizeof(buf), fmt, 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;
161 const struct got_error *
162 got_error_set_errno(int code, const char *prefix)
165 return got_error_from_errno(prefix);
168 const struct got_error *
169 got_ferror(FILE *f, int code)
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];
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];
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) {
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);
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;
230 for (i = 0; i < nitems(got_errors); i++) {
231 if (code == got_errors[i].code) {
233 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
235 err->msg = cerr->msg;
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];
253 vsnprintf(buf, sizeof(buf), fmt, ap);
256 for (i = 0; i < nitems(got_errors); i++) {
257 if (code == got_errors[i].code) {
259 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
261 err->msg = cerr->msg;
270 got_err_open_nofollow_on_symlink(void)
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.
278 return (errno == ELOOP