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.
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"
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #if defined(__GLIBC__)
43 * The autoconf test for strerror_r is broken in current versions
44 * of autoconf: https://savannah.gnu.org/support/?110367
46 #define strerror_r __xpg_strerror_r
49 static struct got_custom_error {
54 static struct got_custom_error *
57 static unsigned int idx;
58 return &custom_errors[(idx++) % nitems(custom_errors)];
61 const struct got_error *
66 for (i = 0; i < nitems(got_errors); i++) {
67 if (code == got_errors[i].code)
68 return &got_errors[i];
74 const struct got_error *
75 got_error_msg(int code, const char *msg)
77 struct got_custom_error *cerr = get_custom_err();
78 struct got_error *err = &cerr->err;
81 for (i = 0; i < nitems(got_errors); i++) {
82 if (code == got_errors[i].code) {
84 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
93 const struct got_error *
94 got_error_from_errno(const char *prefix)
96 struct got_custom_error *cerr = get_custom_err();
97 struct got_error *err = &cerr->err;
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;
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;
115 strerror_r(errno, strerr, sizeof(strerr));
116 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
119 err->code = GOT_ERR_ERRNO;
120 err->msg = cerr->msg;
124 const struct got_error *
125 got_error_from_errno3(const char *prefix, const char *prefix2,
128 struct got_custom_error *cerr = get_custom_err();
129 struct got_error *err = &cerr->err;
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;
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];
151 vsnprintf(buf, sizeof(buf), fmt, 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;
162 const struct got_error *
163 got_error_set_errno(int code, const char *prefix)
166 return got_error_from_errno(prefix);
169 const struct got_error *
170 got_ferror(FILE *f, int code)
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];
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];
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) {
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);
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;
231 for (i = 0; i < nitems(got_errors); i++) {
232 if (code == got_errors[i].code) {
234 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
236 err->msg = cerr->msg;
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];
254 vsnprintf(buf, sizeof(buf), fmt, ap);
257 for (i = 0; i < nitems(got_errors); i++) {
258 if (code == got_errors[i].code) {
260 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
262 err->msg = cerr->msg;
271 got_err_open_nofollow_on_symlink(void)
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.
279 return (errno == ELOOP