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 8251fdbc 2018-01-12 stsp #include <stdio.h>
22 f334529e 2018-01-12 stsp #include <stdlib.h>
23 f334529e 2018-01-12 stsp #include <string.h>
24 91a3d81f 2018-11-11 stsp #include <sha1.h>
25 91a3d81f 2018-11-11 stsp #include <zlib.h>
26 09589288 2019-03-10 stsp #include <uuid.h>
27 f334529e 2018-01-12 stsp
28 4027f31a 2017-11-04 stsp #include "got_error.h"
29 91a3d81f 2018-11-11 stsp #include "got_object.h"
30 4027f31a 2017-11-04 stsp
31 91a3d81f 2018-11-11 stsp #include "got_lib_delta.h"
32 91a3d81f 2018-11-11 stsp #include "got_lib_inflate.h"
33 91a3d81f 2018-11-11 stsp #include "got_lib_object.h"
34 91a3d81f 2018-11-11 stsp #include "got_lib_sha1.h"
35 91a3d81f 2018-11-11 stsp
36 2b4402a2 2017-11-05 stsp #ifndef nitems
37 2b4402a2 2017-11-05 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 2b4402a2 2017-11-05 stsp #endif
39 4027f31a 2017-11-04 stsp
40 4027f31a 2017-11-04 stsp const struct got_error *
41 4027f31a 2017-11-04 stsp got_error(int code)
42 4027f31a 2017-11-04 stsp {
43 4027f31a 2017-11-04 stsp int i;
44 4027f31a 2017-11-04 stsp
45 4027f31a 2017-11-04 stsp for (i = 0; i < nitems(got_errors); i++) {
46 4027f31a 2017-11-04 stsp if (code == got_errors[i].code)
47 4027f31a 2017-11-04 stsp return &got_errors[i];
48 4027f31a 2017-11-04 stsp }
49 4027f31a 2017-11-04 stsp
50 f334529e 2018-01-12 stsp abort();
51 4027f31a 2017-11-04 stsp }
52 f334529e 2018-01-12 stsp
53 f334529e 2018-01-12 stsp const struct got_error *
54 91a3d81f 2018-11-11 stsp got_error_msg(int code, const char *msg)
55 91a3d81f 2018-11-11 stsp {
56 91a3d81f 2018-11-11 stsp static struct got_error err;
57 91a3d81f 2018-11-11 stsp int i;
58 91a3d81f 2018-11-11 stsp
59 91a3d81f 2018-11-11 stsp for (i = 0; i < nitems(got_errors); i++) {
60 91a3d81f 2018-11-11 stsp if (code == got_errors[i].code) {
61 91a3d81f 2018-11-11 stsp err.code = code;
62 91a3d81f 2018-11-11 stsp err.msg = msg;
63 8fa9fd14 2018-11-11 stsp return &err;
64 91a3d81f 2018-11-11 stsp }
65 91a3d81f 2018-11-11 stsp }
66 91a3d81f 2018-11-11 stsp
67 91a3d81f 2018-11-11 stsp abort();
68 91a3d81f 2018-11-11 stsp }
69 91a3d81f 2018-11-11 stsp
70 91a3d81f 2018-11-11 stsp const struct got_error *
71 638f9024 2019-05-13 stsp got_error_from_errno(const char *prefix)
72 f334529e 2018-01-12 stsp {
73 f334529e 2018-01-12 stsp static struct got_error err;
74 7d45c7f1 2019-05-15 stsp static char err_msg[PATH_MAX + 20];
75 f334529e 2018-01-12 stsp
76 230a42bd 2019-05-11 jcs snprintf(err_msg, sizeof(err_msg), "%s: %s", prefix,
77 230a42bd 2019-05-11 jcs strerror(errno));
78 230a42bd 2019-05-11 jcs
79 f334529e 2018-01-12 stsp err.code = GOT_ERR_ERRNO;
80 230a42bd 2019-05-11 jcs err.msg = err_msg;
81 f334529e 2018-01-12 stsp return &err;
82 f334529e 2018-01-12 stsp }
83 8251fdbc 2018-01-12 stsp
84 8251fdbc 2018-01-12 stsp const struct got_error *
85 638f9024 2019-05-13 stsp got_error_from_errno2(const char *prefix, const char *prefix2)
86 48b8b0eb 2019-05-11 jcs {
87 48b8b0eb 2019-05-11 jcs static struct got_error err;
88 7d45c7f1 2019-05-15 stsp static char err_msg[(PATH_MAX * 2) + 20];
89 48b8b0eb 2019-05-11 jcs
90 230a42bd 2019-05-11 jcs snprintf(err_msg, sizeof(err_msg), "%s: %s: %s", prefix, prefix2,
91 48b8b0eb 2019-05-11 jcs strerror(errno));
92 48b8b0eb 2019-05-11 jcs
93 48b8b0eb 2019-05-11 jcs err.code = GOT_ERR_ERRNO;
94 48b8b0eb 2019-05-11 jcs err.msg = err_msg;
95 48b8b0eb 2019-05-11 jcs return &err;
96 48b8b0eb 2019-05-11 jcs }
97 48b8b0eb 2019-05-11 jcs
98 48b8b0eb 2019-05-11 jcs const struct got_error *
99 638f9024 2019-05-13 stsp got_error_from_errno3(const char *prefix, const char *prefix2,
100 230a42bd 2019-05-11 jcs const char *prefix3)
101 230a42bd 2019-05-11 jcs {
102 230a42bd 2019-05-11 jcs static struct got_error err;
103 7d45c7f1 2019-05-15 stsp static char err_msg[(PATH_MAX * 3) + 20];
104 230a42bd 2019-05-11 jcs
105 230a42bd 2019-05-11 jcs snprintf(err_msg, sizeof(err_msg), "%s: %s: %s: %s", prefix, prefix2,
106 230a42bd 2019-05-11 jcs prefix3, strerror(errno));
107 230a42bd 2019-05-11 jcs
108 230a42bd 2019-05-11 jcs err.code = GOT_ERR_ERRNO;
109 230a42bd 2019-05-11 jcs err.msg = err_msg;
110 230a42bd 2019-05-11 jcs return &err;
111 230a42bd 2019-05-11 jcs }
112 230a42bd 2019-05-11 jcs
113 230a42bd 2019-05-11 jcs const struct got_error *
114 2af4a041 2019-05-11 jcs got_error_set_errno(int code, const char *prefix)
115 1a76625f 2018-10-22 stsp {
116 1a76625f 2018-10-22 stsp errno = code;
117 638f9024 2019-05-13 stsp return got_error_from_errno(prefix);
118 1a76625f 2018-10-22 stsp }
119 1a76625f 2018-10-22 stsp
120 1a76625f 2018-10-22 stsp const struct got_error *
121 8251fdbc 2018-01-12 stsp got_ferror(FILE *f, int code)
122 8251fdbc 2018-01-12 stsp {
123 8251fdbc 2018-01-12 stsp if (ferror(f))
124 638f9024 2019-05-13 stsp return got_error_from_errno("");
125 8251fdbc 2018-01-12 stsp return got_error(code);
126 8251fdbc 2018-01-12 stsp }
127 91a3d81f 2018-11-11 stsp
128 91a3d81f 2018-11-11 stsp const struct got_error *
129 91a3d81f 2018-11-11 stsp got_error_no_obj(struct got_object_id *id)
130 91a3d81f 2018-11-11 stsp {
131 91a3d81f 2018-11-11 stsp static char msg[sizeof("object not found") +
132 91a3d81f 2018-11-11 stsp SHA1_DIGEST_STRING_LENGTH];
133 91a3d81f 2018-11-11 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
134 91a3d81f 2018-11-11 stsp int ret;
135 91a3d81f 2018-11-11 stsp
136 91a3d81f 2018-11-11 stsp if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str)))
137 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
138 91a3d81f 2018-11-11 stsp
139 91a3d81f 2018-11-11 stsp ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
140 91a3d81f 2018-11-11 stsp if (ret == -1 || ret >= sizeof(msg))
141 91a3d81f 2018-11-11 stsp return got_error(GOT_ERR_NO_OBJ);
142 91a3d81f 2018-11-11 stsp
143 91a3d81f 2018-11-11 stsp return got_error_msg(GOT_ERR_NO_OBJ, msg);
144 91a3d81f 2018-11-11 stsp }
145 2aa0475c 2019-02-03 stsp
146 2aa0475c 2019-02-03 stsp const struct got_error *
147 2aa0475c 2019-02-03 stsp got_error_not_ref(const char *refname)
148 2aa0475c 2019-02-03 stsp {
149 2aa0475c 2019-02-03 stsp static char msg[sizeof("reference not found") + 1004];
150 2aa0475c 2019-02-03 stsp int ret;
151 2aa0475c 2019-02-03 stsp
152 2aa0475c 2019-02-03 stsp ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
153 2aa0475c 2019-02-03 stsp if (ret == -1 || ret >= sizeof(msg))
154 2aa0475c 2019-02-03 stsp return got_error(GOT_ERR_NOT_REF);
155 2aa0475c 2019-02-03 stsp
156 2aa0475c 2019-02-03 stsp return got_error_msg(GOT_ERR_NOT_REF, msg);
157 2aa0475c 2019-02-03 stsp }
158 09589288 2019-03-10 stsp
159 09589288 2019-03-10 stsp const struct got_error *
160 cc483380 2019-09-01 stsp got_error_uuid(uint32_t uuid_status, const char *prefix)
161 09589288 2019-03-10 stsp {
162 09589288 2019-03-10 stsp switch (uuid_status) {
163 09589288 2019-03-10 stsp case uuid_s_ok:
164 09589288 2019-03-10 stsp return NULL;
165 09589288 2019-03-10 stsp case uuid_s_bad_version:
166 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_VERSION);
167 09589288 2019-03-10 stsp case uuid_s_invalid_string_uuid:
168 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID_INVALID);
169 09589288 2019-03-10 stsp case uuid_s_no_memory:
170 cc483380 2019-09-01 stsp return got_error_set_errno(ENOMEM, prefix);
171 09589288 2019-03-10 stsp default:
172 09589288 2019-03-10 stsp return got_error(GOT_ERR_UUID);
173 09589288 2019-03-10 stsp }
174 09589288 2019-03-10 stsp }
175 df056ada 2019-05-15 stsp
176 df056ada 2019-05-15 stsp const struct got_error *
177 df056ada 2019-05-15 stsp got_error_path(const char *path, int code)
178 df056ada 2019-05-15 stsp {
179 df056ada 2019-05-15 stsp static struct got_error err;
180 7d45c7f1 2019-05-15 stsp static char msg[PATH_MAX + 128];
181 df056ada 2019-05-15 stsp int i;
182 df056ada 2019-05-15 stsp
183 df056ada 2019-05-15 stsp for (i = 0; i < nitems(got_errors); i++) {
184 df056ada 2019-05-15 stsp if (code == got_errors[i].code) {
185 df056ada 2019-05-15 stsp err.code = code;
186 df056ada 2019-05-15 stsp snprintf(msg, sizeof(msg), "%s: %s", path,
187 df056ada 2019-05-15 stsp got_errors[i].msg);
188 df056ada 2019-05-15 stsp err.msg = msg;
189 df056ada 2019-05-15 stsp return &err;
190 df056ada 2019-05-15 stsp }
191 df056ada 2019-05-15 stsp }
192 df056ada 2019-05-15 stsp
193 df056ada 2019-05-15 stsp abort();
194 df056ada 2019-05-15 stsp }