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.
17 #include <sys/queue.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 static struct got_custom_error {
46 static struct got_custom_error *
49 static unsigned int idx;
50 return &custom_errors[(idx++) % nitems(custom_errors)];
53 const struct got_error *
58 for (i = 0; i < nitems(got_errors); i++) {
59 if (code == got_errors[i].code)
60 return &got_errors[i];
66 const struct got_error *
67 got_error_msg(int code, const char *msg)
69 struct got_custom_error *cerr = get_custom_err();
70 struct got_error *err = &cerr->err;
73 for (i = 0; i < nitems(got_errors); i++) {
74 if (code == got_errors[i].code) {
76 strlcpy(cerr->msg, msg, sizeof(cerr->msg));
85 const struct got_error *
86 got_error_from_errno(const char *prefix)
88 struct got_custom_error *cerr = get_custom_err();
89 struct got_error *err = &cerr->err;
92 strerror_r(errno, strerr, sizeof(strerr));
93 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", prefix, strerr);
95 err->code = GOT_ERR_ERRNO;
100 const struct got_error *
101 got_error_from_errno2(const char *prefix, const char *prefix2)
103 struct got_custom_error *cerr = get_custom_err();
104 struct got_error *err = &cerr->err;
107 strerror_r(errno, strerr, sizeof(strerr));
108 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s", prefix, prefix2,
111 err->code = GOT_ERR_ERRNO;
112 err->msg = cerr->msg;
116 const struct got_error *
117 got_error_from_errno3(const char *prefix, const char *prefix2,
120 struct got_custom_error *cerr = get_custom_err();
121 struct got_error *err = &cerr->err;
124 strerror_r(errno, strerr, sizeof(strerr));
125 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s: %s: %s", prefix,
126 prefix2, prefix3, strerr);
128 err->code = GOT_ERR_ERRNO;
129 err->msg = cerr->msg;
133 const struct got_error *
134 got_error_from_errno_fmt(const char *fmt, ...)
136 struct got_custom_error *cerr = get_custom_err();
137 struct got_error *err = &cerr->err;
138 char buf[PATH_MAX * 4];
143 vsnprintf(buf, sizeof(buf), fmt, ap);
146 strerror_r(errno, strerr, sizeof(strerr));
147 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf, strerr);
149 err->code = GOT_ERR_ERRNO;
150 err->msg = cerr->msg;
154 const struct got_error *
155 got_error_set_errno(int code, const char *prefix)
158 return got_error_from_errno(prefix);
161 const struct got_error *
162 got_ferror(FILE *f, int code)
165 return got_error_from_errno("");
166 return got_error(code);
169 const struct got_error *
170 got_error_no_obj(struct got_object_id *id)
172 char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH];
173 char id_str[SHA1_DIGEST_STRING_LENGTH];
176 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
177 return got_error(GOT_ERR_NO_OBJ);
179 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
180 if (ret == -1 || ret >= sizeof(msg))
181 return got_error(GOT_ERR_NO_OBJ);
183 return got_error_msg(GOT_ERR_NO_OBJ, msg);
186 const struct got_error *
187 got_error_not_ref(const char *refname)
189 char msg[sizeof("reference not found") + 1004];
192 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
193 if (ret == -1 || ret >= sizeof(msg))
194 return got_error(GOT_ERR_NOT_REF);
196 return got_error_msg(GOT_ERR_NOT_REF, msg);
199 const struct got_error *
200 got_error_uuid(uint32_t uuid_status, const char *prefix)
202 switch (uuid_status) {
205 case uuid_s_bad_version:
206 return got_error(GOT_ERR_UUID_VERSION);
207 case uuid_s_invalid_string_uuid:
208 return got_error(GOT_ERR_UUID_INVALID);
209 case uuid_s_no_memory:
210 return got_error_set_errno(ENOMEM, prefix);
212 return got_error(GOT_ERR_UUID);
216 const struct got_error *
217 got_error_path(const char *path, int code)
219 struct got_custom_error *cerr = get_custom_err();
220 struct got_error *err = &cerr->err;
223 for (i = 0; i < nitems(got_errors); i++) {
224 if (code == got_errors[i].code) {
226 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", path,
228 err->msg = cerr->msg;
236 const struct got_error *
237 got_error_fmt(int code, const char *fmt, ...)
239 struct got_custom_error *cerr = get_custom_err();
240 struct got_error *err = &cerr->err;
241 char buf[PATH_MAX * 4];
246 vsnprintf(buf, sizeof(buf), fmt, ap);
249 for (i = 0; i < nitems(got_errors); i++) {
250 if (code == got_errors[i].code) {
252 snprintf(cerr->msg, sizeof(cerr->msg), "%s: %s", buf,
254 err->msg = cerr->msg;
263 got_err_open_nofollow_on_symlink(void)
266 * Check whether open(2) with O_NOFOLLOW failed on a symlink.
267 * Posix mandates ELOOP and OpenBSD follows it. Others return
268 * different error codes. We carry this workaround to help the
269 * portable version a little.
271 return (errno == ELOOP