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 const struct got_error *
46 for (i = 0; i < nitems(got_errors); i++) {
47 if (code == got_errors[i].code)
48 return &got_errors[i];
54 const struct got_error *
55 got_error_msg(int code, const char *msg)
57 static struct got_error err;
60 for (i = 0; i < nitems(got_errors); i++) {
61 if (code == got_errors[i].code) {
71 const struct got_error *
72 got_error_from_errno(const char *prefix)
74 static struct got_error err;
75 static char err_msg[PATH_MAX + 20];
77 snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
80 err.code = GOT_ERR_ERRNO;
85 const struct got_error *
86 got_error_from_errno2(const char *prefix, const char *prefix2)
88 static struct got_error err;
89 static char err_msg[(PATH_MAX * 2) + 20];
91 snprintf(err_msg, sizeof(err_msg), "%s: %s: %s", prefix, prefix2,
94 err.code = GOT_ERR_ERRNO;
99 const struct got_error *
100 got_error_from_errno3(const char *prefix, const char *prefix2,
103 static struct got_error err;
104 static char err_msg[(PATH_MAX * 3) + 20];
106 snprintf(err_msg, sizeof(err_msg), "%s: %s: %s: %s", prefix, prefix2,
107 prefix3, strerror(errno));
109 err.code = GOT_ERR_ERRNO;
114 const struct got_error *
115 got_error_from_errno_fmt(const char *fmt, ...)
117 static struct got_error err;
118 static char err_msg[PATH_MAX * 4 + 64];
119 char buf[PATH_MAX * 4];
123 vsnprintf(buf, sizeof(buf), fmt, ap);
126 snprintf(err_msg, sizeof(err_msg), "%s: %s", buf, strerror(errno));
128 err.code = GOT_ERR_ERRNO;
133 const struct got_error *
134 got_error_set_errno(int code, const char *prefix)
137 return got_error_from_errno(prefix);
140 const struct got_error *
141 got_ferror(FILE *f, int code)
144 return got_error_from_errno("");
145 return got_error(code);
148 const struct got_error *
149 got_error_no_obj(struct got_object_id *id)
151 static char msg[sizeof("object not found") +
152 SHA1_DIGEST_STRING_LENGTH];
153 char id_str[SHA1_DIGEST_STRING_LENGTH];
156 if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
157 return got_error(GOT_ERR_NO_OBJ);
159 ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
160 if (ret == -1 || ret >= sizeof(msg))
161 return got_error(GOT_ERR_NO_OBJ);
163 return got_error_msg(GOT_ERR_NO_OBJ, msg);
166 const struct got_error *
167 got_error_not_ref(const char *refname)
169 static char msg[sizeof("reference not found") + 1004];
172 ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
173 if (ret == -1 || ret >= sizeof(msg))
174 return got_error(GOT_ERR_NOT_REF);
176 return got_error_msg(GOT_ERR_NOT_REF, msg);
179 const struct got_error *
180 got_error_uuid(uint32_t uuid_status, const char *prefix)
182 switch (uuid_status) {
185 case uuid_s_bad_version:
186 return got_error(GOT_ERR_UUID_VERSION);
187 case uuid_s_invalid_string_uuid:
188 return got_error(GOT_ERR_UUID_INVALID);
189 case uuid_s_no_memory:
190 return got_error_set_errno(ENOMEM, prefix);
192 return got_error(GOT_ERR_UUID);
196 const struct got_error *
197 got_error_path(const char *path, int code)
199 static struct got_error err;
200 static char msg[PATH_MAX + 128];
203 for (i = 0; i < nitems(got_errors); i++) {
204 if (code == got_errors[i].code) {
206 snprintf(msg, sizeof(msg), "%s: %s", path,
216 const struct got_error *
217 got_error_fmt(int code, const char *fmt, ...)
219 static struct got_error err;
220 static char msg[PATH_MAX * 4 + 128];
221 char buf[PATH_MAX * 4];
226 vsnprintf(buf, sizeof(buf), fmt, ap);
229 for (i = 0; i < nitems(got_errors); i++) {
230 if (code == got_errors[i].code) {
232 snprintf(msg, sizeof(msg), "%s: %s", buf,