Blame


1 7b19e0f1 2017-11-05 stsp /*
2 72bcf0f9 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 91a3d81f 2018-11-11 stsp #include <sys/queue.h>
18 91a3d81f 2018-11-11 stsp
19 f334529e 2018-01-12 stsp #include <errno.h>
20 7d45c7f1 2019-05-15 stsp #include <limits.h>
21 4cc6a5a5 2020-12-15 stsp #include <stdarg.h>
22 8251fdbc 2018-01-12 stsp #include <stdio.h>
23 f334529e 2018-01-12 stsp #include <stdlib.h>
24 f334529e 2018-01-12 stsp #include <string.h>
25 91a3d81f 2018-11-11 stsp #include <sha1.h>
26 91a3d81f 2018-11-11 stsp #include <zlib.h>
27 09589288 2019-03-10 stsp #include <uuid.h>
28 f334529e 2018-01-12 stsp
29 4027f31a 2017-11-04 stsp #include "got_error.h"
30 91a3d81f 2018-11-11 stsp #include "got_object.h"
31 4027f31a 2017-11-04 stsp
32 91a3d81f 2018-11-11 stsp #include "got_lib_delta.h"
33 91a3d81f 2018-11-11 stsp #include "got_lib_inflate.h"
34 91a3d81f 2018-11-11 stsp #include "got_lib_object.h"
35 91a3d81f 2018-11-11 stsp #include "got_lib_sha1.h"
36 91a3d81f 2018-11-11 stsp
37 2b4402a2 2017-11-05 stsp #ifndef nitems
38 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 2b4402a2 2017-11-05 stsp #endif
40 4027f31a 2017-11-04 stsp
41 4027f31a 2017-11-04 stsp const struct got_error *
42 4027f31a 2017-11-04 stsp got_error(int code)
43 4027f31a 2017-11-04 stsp {
44 16aeacf7 2020-11-26 stsp size_t i;
45 4027f31a 2017-11-04 stsp
46 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
47 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
48 4027f31a 2017-11-04 stsp return &got_errors[i];
49 4027f31a 2017-11-04 stsp }
50 4027f31a 2017-11-04 stsp
51 f334529e 2018-01-12 stsp abort();
52 4027f31a 2017-11-04 stsp }
53 f334529e 2018-01-12 stsp
54 f334529e 2018-01-12 stsp const struct got_error *
55 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
56 91a3d81f 2018-11-11 stsp {
57 91a3d81f 2018-11-11 stsp static struct got_error err;
58 16aeacf7 2020-11-26 stsp size_t i;
59 91a3d81f 2018-11-11 stsp
60 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
61 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
62 91a3d81f 2018-11-11 stsp err.code = code;
63 91a3d81f 2018-11-11 stsp err.msg = msg;
64 8fa9fd14 2018-11-11 stsp return &err;
65 91a3d81f 2018-11-11 stsp }
66 91a3d81f 2018-11-11 stsp }
67 91a3d81f 2018-11-11 stsp
68 91a3d81f 2018-11-11 stsp abort();
69 91a3d81f 2018-11-11 stsp }
70 91a3d81f 2018-11-11 stsp
71 91a3d81f 2018-11-11 stsp const struct got_error *
72 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
73 f334529e 2018-01-12 stsp {
74 f334529e 2018-01-12 stsp static struct got_error err;
75 7d45c7f1 2019-05-15 stsp static char err_msg[PATH_MAX + 20];
76 f334529e 2018-01-12 stsp
77 230a42bd 2019-05-11 jcs snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
78 230a42bd 2019-05-11 jcs strerror(errno));
79 230a42bd 2019-05-11 jcs
80 f334529e 2018-01-12 stsp err.code = GOT_ERR_ERRNO;
81 230a42bd 2019-05-11 jcs err.msg = err_msg;
82 f334529e 2018-01-12 stsp return &err;
83 f334529e 2018-01-12 stsp }
84 8251fdbc 2018-01-12 stsp
85 8251fdbc 2018-01-12 stsp const struct got_error *
86 638f9024 2019-05-13 stsp got_error_from_errno2(const char *prefix, const char *prefix2)
87 48b8b0eb 2019-05-11 jcs {
88 48b8b0eb 2019-05-11 jcs static struct got_error err;
89 7d45c7f1 2019-05-15 stsp static char err_msg[(PATH_MAX * 2) + 20];
90 48b8b0eb 2019-05-11 jcs
91 230a42bd 2019-05-11 jcs snprintf(err_msg, sizeof(err_msg), "%s: %s: %s", prefix, prefix2,
92 48b8b0eb 2019-05-11 jcs strerror(errno));
93 48b8b0eb 2019-05-11 jcs
94 48b8b0eb 2019-05-11 jcs err.code = GOT_ERR_ERRNO;
95 48b8b0eb 2019-05-11 jcs err.msg = err_msg;
96 48b8b0eb 2019-05-11 jcs return &err;
97 48b8b0eb 2019-05-11 jcs }
98 48b8b0eb 2019-05-11 jcs
99 48b8b0eb 2019-05-11 jcs const struct got_error *
100 638f9024 2019-05-13 stsp got_error_from_errno3(const char *prefix, const char *prefix2,
101 230a42bd 2019-05-11 jcs const char *prefix3)
102 230a42bd 2019-05-11 jcs {
103 230a42bd 2019-05-11 jcs static struct got_error err;
104 7d45c7f1 2019-05-15 stsp static char err_msg[(PATH_MAX * 3) + 20];
105 230a42bd 2019-05-11 jcs
106 230a42bd 2019-05-11 jcs snprintf(err_msg, sizeof(err_msg), "%s: %s: %s: %s", prefix, prefix2,
107 230a42bd 2019-05-11 jcs prefix3, strerror(errno));
108 230a42bd 2019-05-11 jcs
109 230a42bd 2019-05-11 jcs err.code = GOT_ERR_ERRNO;
110 230a42bd 2019-05-11 jcs err.msg = err_msg;
111 230a42bd 2019-05-11 jcs return &err;
112 230a42bd 2019-05-11 jcs }
113 230a42bd 2019-05-11 jcs
114 230a42bd 2019-05-11 jcs const struct got_error *
115 4cc6a5a5 2020-12-15 stsp got_error_from_errno_fmt(const char *fmt, ...)
116 4cc6a5a5 2020-12-15 stsp {
117 4cc6a5a5 2020-12-15 stsp static struct got_error err;
118 4cc6a5a5 2020-12-15 stsp static char err_msg[PATH_MAX * 4 + 64];
119 4cc6a5a5 2020-12-15 stsp char buf[PATH_MAX * 4];
120 4cc6a5a5 2020-12-15 stsp va_list ap;
121 4cc6a5a5 2020-12-15 stsp
122 4cc6a5a5 2020-12-15 stsp va_start(ap, fmt);
123 4cc6a5a5 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
124 4cc6a5a5 2020-12-15 stsp va_end(ap);
125 4cc6a5a5 2020-12-15 stsp
126 4cc6a5a5 2020-12-15 stsp snprintf(err_msg, sizeof(err_msg), "%s: %s", buf, strerror(errno));
127 4cc6a5a5 2020-12-15 stsp
128 4cc6a5a5 2020-12-15 stsp err.code = GOT_ERR_ERRNO;
129 4cc6a5a5 2020-12-15 stsp err.msg = err_msg;
130 4cc6a5a5 2020-12-15 stsp return &err;
131 4cc6a5a5 2020-12-15 stsp }
132 4cc6a5a5 2020-12-15 stsp
133 4cc6a5a5 2020-12-15 stsp const struct got_error *
134 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
135 1a76625f 2018-10-22 stsp {
136 1a76625f 2018-10-22 stsp errno = code;
137 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
138 1a76625f 2018-10-22 stsp }
139 1a76625f 2018-10-22 stsp
140 1a76625f 2018-10-22 stsp const struct got_error *
141 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
142 8251fdbc 2018-01-12 stsp {
143 8251fdbc 2018-01-12 stsp if (ferror(f))
144 638f9024 2019-05-13 stsp return got_error_from_errno("");
145 8251fdbc 2018-01-12 stsp return got_error(code);
146 8251fdbc 2018-01-12 stsp }
147 91a3d81f 2018-11-11 stsp
148 91a3d81f 2018-11-11 stsp const struct got_error *
149 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
150 91a3d81f 2018-11-11 stsp {
151 91a3d81f 2018-11-11 stsp static char msg[sizeof("object not found") +
152 91a3d81f 2018-11-11 stsp SHA1_DIGEST_STRING_LENGTH];
153 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
154 91a3d81f 2018-11-11 stsp int ret;
155 91a3d81f 2018-11-11 stsp
156 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
157 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
158 91a3d81f 2018-11-11 stsp
159 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
160 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
161 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
162 91a3d81f 2018-11-11 stsp
163 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
164 91a3d81f 2018-11-11 stsp }
165 2aa0475c 2019-02-03 stsp
166 2aa0475c 2019-02-03 stsp const struct got_error *
167 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
168 2aa0475c 2019-02-03 stsp {
169 2aa0475c 2019-02-03 stsp static char msg[sizeof("reference not found") + 1004];
170 2aa0475c 2019-02-03 stsp int ret;
171 2aa0475c 2019-02-03 stsp
172 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
173 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
174 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
175 2aa0475c 2019-02-03 stsp
176 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
177 2aa0475c 2019-02-03 stsp }
178 09589288 2019-03-10 stsp
179 09589288 2019-03-10 stsp const struct got_error *
180 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
181 09589288 2019-03-10 stsp {
182 09589288 2019-03-10 stsp switch (uuid_status) {
183 09589288 2019-03-10 stsp case uuid_s_ok:
184 09589288 2019-03-10 stsp return NULL;
185 09589288 2019-03-10 stsp case uuid_s_bad_version:
186 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
187 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
188 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
189 09589288 2019-03-10 stsp case uuid_s_no_memory:
190 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
191 09589288 2019-03-10 stsp default:
192 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
193 09589288 2019-03-10 stsp }
194 09589288 2019-03-10 stsp }
195 df056ada 2019-05-15 stsp
196 df056ada 2019-05-15 stsp const struct got_error *
197 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
198 df056ada 2019-05-15 stsp {
199 df056ada 2019-05-15 stsp static struct got_error err;
200 7d45c7f1 2019-05-15 stsp static char msg[PATH_MAX + 128];
201 16aeacf7 2020-11-26 stsp size_t i;
202 df056ada 2019-05-15 stsp
203 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
204 df056ada 2019-05-15 stsp if (code == got_errors[i].code) {
205 df056ada 2019-05-15 stsp err.code = code;
206 df056ada 2019-05-15 stsp snprintf(msg, sizeof(msg), "%s: %s", path,
207 df056ada 2019-05-15 stsp got_errors[i].msg);
208 df056ada 2019-05-15 stsp err.msg = msg;
209 df056ada 2019-05-15 stsp return &err;
210 df056ada 2019-05-15 stsp }
211 df056ada 2019-05-15 stsp }
212 df056ada 2019-05-15 stsp
213 df056ada 2019-05-15 stsp abort();
214 df056ada 2019-05-15 stsp }
215 73e7eb7d 2020-12-15 stsp
216 73e7eb7d 2020-12-15 stsp const struct got_error *
217 73e7eb7d 2020-12-15 stsp got_error_fmt(int code, const char *fmt, ...)
218 73e7eb7d 2020-12-15 stsp {
219 73e7eb7d 2020-12-15 stsp static struct got_error err;
220 73e7eb7d 2020-12-15 stsp static char msg[PATH_MAX * 4 + 128];
221 73e7eb7d 2020-12-15 stsp char buf[PATH_MAX * 4];
222 73e7eb7d 2020-12-15 stsp va_list ap;
223 73e7eb7d 2020-12-15 stsp size_t i;
224 73e7eb7d 2020-12-15 stsp
225 73e7eb7d 2020-12-15 stsp va_start(ap, fmt);
226 73e7eb7d 2020-12-15 stsp vsnprintf(buf, sizeof(buf), fmt, ap);
227 73e7eb7d 2020-12-15 stsp va_end(ap);
228 73e7eb7d 2020-12-15 stsp
229 73e7eb7d 2020-12-15 stsp for (i = 0; i < nitems(got_errors); i++) {
230 73e7eb7d 2020-12-15 stsp if (code == got_errors[i].code) {
231 73e7eb7d 2020-12-15 stsp err.code = code;
232 73e7eb7d 2020-12-15 stsp snprintf(msg, sizeof(msg), "%s: %s", buf,
233 73e7eb7d 2020-12-15 stsp got_errors[i].msg);
234 73e7eb7d 2020-12-15 stsp err.msg = msg;
235 73e7eb7d 2020-12-15 stsp return &err;
236 73e7eb7d 2020-12-15 stsp }
237 73e7eb7d 2020-12-15 stsp }
238 73e7eb7d 2020-12-15 stsp
239 73e7eb7d 2020-12-15 stsp abort();
240 73e7eb7d 2020-12-15 stsp }