Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sha1.h>
26 #include <zlib.h>
27 #include <uuid.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"
37 #ifndef nitems
38 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 #endif
41 const struct got_error *
42 got_error(int code)
43 {
44 size_t i;
46 for (i = 0; i < nitems(got_errors); i++) {
47 if (code == got_errors[i].code)
48 return &got_errors[i];
49 }
51 abort();
52 }
54 const struct got_error *
55 got_error_msg(int code, const char *msg)
56 {
57 static struct got_error err;
58 size_t i;
60 for (i = 0; i < nitems(got_errors); i++) {
61 if (code == got_errors[i].code) {
62 err.code = code;
63 err.msg = msg;
64 return &err;
65 }
66 }
68 abort();
69 }
71 const struct got_error *
72 got_error_from_errno(const char *prefix)
73 {
74 static struct got_error err;
75 static char err_msg[PATH_MAX + 20];
77 snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
78 strerror(errno));
80 err.code = GOT_ERR_ERRNO;
81 err.msg = err_msg;
82 return &err;
83 }
85 const struct got_error *
86 got_error_from_errno2(const char *prefix, const char *prefix2)
87 {
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,
92 strerror(errno));
94 err.code = GOT_ERR_ERRNO;
95 err.msg = err_msg;
96 return &err;
97 }
99 const struct got_error *
100 got_error_from_errno3(const char *prefix, const char *prefix2,
101 const char *prefix3)
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;
110 err.msg = err_msg;
111 return &err;
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];
120 va_list ap;
122 va_start(ap, fmt);
123 vsnprintf(buf, sizeof(buf), fmt, ap);
124 va_end(ap);
126 snprintf(err_msg, sizeof(err_msg), "%s: %s", buf, strerror(errno));
128 err.code = GOT_ERR_ERRNO;
129 err.msg = err_msg;
130 return &err;
133 const struct got_error *
134 got_error_set_errno(int code, const char *prefix)
136 errno = code;
137 return got_error_from_errno(prefix);
140 const struct got_error *
141 got_ferror(FILE *f, int code)
143 if (ferror(f))
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];
154 int ret;
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];
170 int ret;
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) {
183 case uuid_s_ok:
184 return NULL;
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);
191 default:
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];
201 size_t i;
203 for (i = 0; i < nitems(got_errors); i++) {
204 if (code == got_errors[i].code) {
205 err.code = code;
206 snprintf(msg, sizeof(msg), "%s: %s", path,
207 got_errors[i].msg);
208 err.msg = msg;
209 return &err;
213 abort();
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];
222 va_list ap;
223 size_t i;
225 va_start(ap, fmt);
226 vsnprintf(buf, sizeof(buf), fmt, ap);
227 va_end(ap);
229 for (i = 0; i < nitems(got_errors); i++) {
230 if (code == got_errors[i].code) {
231 err.code = code;
232 snprintf(msg, sizeof(msg), "%s: %s", buf,
233 got_errors[i].msg);
234 err.msg = msg;
235 return &err;
239 abort();