Blame


1 069bbb86 2022-03-07 thomas /*
2 069bbb86 2022-03-07 thomas * Copyright 1986, Larry Wall
3 39807ab2 2022-05-12 thomas *
4 069bbb86 2022-03-07 thomas * Redistribution and use in source and binary forms, with or without
5 069bbb86 2022-03-07 thomas * modification, are permitted provided that the following condition is met:
6 069bbb86 2022-03-07 thomas * 1. Redistributions of source code must retain the above copyright notice,
7 069bbb86 2022-03-07 thomas * this condition and the following disclaimer.
8 39807ab2 2022-05-12 thomas *
9 069bbb86 2022-03-07 thomas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 069bbb86 2022-03-07 thomas * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 069bbb86 2022-03-07 thomas * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 069bbb86 2022-03-07 thomas * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 069bbb86 2022-03-07 thomas * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 069bbb86 2022-03-07 thomas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 069bbb86 2022-03-07 thomas * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 069bbb86 2022-03-07 thomas * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 069bbb86 2022-03-07 thomas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 069bbb86 2022-03-07 thomas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19 069bbb86 2022-03-07 thomas * SUCH DAMAGE.
20 069bbb86 2022-03-07 thomas */
21 069bbb86 2022-03-07 thomas
22 069bbb86 2022-03-07 thomas /*
23 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
24 069bbb86 2022-03-07 thomas *
25 069bbb86 2022-03-07 thomas * Permission to use, copy, modify, and distribute this software for any
26 069bbb86 2022-03-07 thomas * purpose with or without fee is hereby granted, provided that the above
27 069bbb86 2022-03-07 thomas * copyright notice and this permission notice appear in all copies.
28 069bbb86 2022-03-07 thomas *
29 069bbb86 2022-03-07 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
30 069bbb86 2022-03-07 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
31 069bbb86 2022-03-07 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
32 069bbb86 2022-03-07 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
33 069bbb86 2022-03-07 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
34 069bbb86 2022-03-07 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
35 069bbb86 2022-03-07 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 069bbb86 2022-03-07 thomas */
37 069bbb86 2022-03-07 thomas
38 069bbb86 2022-03-07 thomas #include <sys/types.h>
39 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
40 069bbb86 2022-03-07 thomas #include <sys/uio.h>
41 069bbb86 2022-03-07 thomas
42 069bbb86 2022-03-07 thomas #include <ctype.h>
43 069bbb86 2022-03-07 thomas #include <limits.h>
44 069bbb86 2022-03-07 thomas #include <paths.h>
45 588a8092 2023-02-23 thomas #include <sha1.h>
46 588a8092 2023-02-23 thomas #include <sha2.h>
47 069bbb86 2022-03-07 thomas #include <stdint.h>
48 069bbb86 2022-03-07 thomas #include <stdio.h>
49 069bbb86 2022-03-07 thomas #include <stdlib.h>
50 069bbb86 2022-03-07 thomas #include <string.h>
51 069bbb86 2022-03-07 thomas #include <unistd.h>
52 069bbb86 2022-03-07 thomas
53 069bbb86 2022-03-07 thomas #include "got_error.h"
54 069bbb86 2022-03-07 thomas #include "got_object.h"
55 069bbb86 2022-03-07 thomas
56 973f3f6e 2022-03-07 thomas #include "got_compat.h"
57 973f3f6e 2022-03-07 thomas
58 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
59 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
60 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
61 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
62 069bbb86 2022-03-07 thomas
63 069bbb86 2022-03-07 thomas struct imsgbuf ibuf;
64 069bbb86 2022-03-07 thomas
65 069bbb86 2022-03-07 thomas static const struct got_error *
66 016bfe4b 2022-06-23 thomas send_patch(const char *oldname, const char *newname, const char *commitid,
67 37e766f4 2022-09-21 thomas const char *blob, const int xbit, int git)
68 069bbb86 2022-03-07 thomas {
69 069bbb86 2022-03-07 thomas struct got_imsg_patch p;
70 069bbb86 2022-03-07 thomas
71 069bbb86 2022-03-07 thomas memset(&p, 0, sizeof(p));
72 069bbb86 2022-03-07 thomas
73 d9db2ff9 2022-04-16 thomas if (oldname != NULL)
74 069bbb86 2022-03-07 thomas strlcpy(p.old, oldname, sizeof(p.old));
75 be53ddb1 2022-03-22 thomas
76 069bbb86 2022-03-07 thomas if (newname != NULL)
77 069bbb86 2022-03-07 thomas strlcpy(p.new, newname, sizeof(p.new));
78 069bbb86 2022-03-07 thomas
79 8afec5d5 2022-07-01 thomas if (commitid != NULL)
80 016bfe4b 2022-06-23 thomas strlcpy(p.cid, commitid, sizeof(p.cid));
81 8afec5d5 2022-07-01 thomas
82 8afec5d5 2022-07-01 thomas if (blob != NULL)
83 0f76ab83 2022-06-23 thomas strlcpy(p.blob, blob, sizeof(p.blob));
84 0f76ab83 2022-06-23 thomas
85 37e766f4 2022-09-21 thomas p.xbit = xbit;
86 d9db2ff9 2022-04-16 thomas p.git = git;
87 0f76ab83 2022-06-23 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
88 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
89 069bbb86 2022-03-07 thomas return NULL;
90 069bbb86 2022-03-07 thomas }
91 069bbb86 2022-03-07 thomas
92 069bbb86 2022-03-07 thomas static const struct got_error *
93 069bbb86 2022-03-07 thomas send_patch_done(void)
94 069bbb86 2022-03-07 thomas {
95 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
96 069bbb86 2022-03-07 thomas NULL, 0) == -1)
97 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
98 dab315a5 2022-07-07 thomas return got_privsep_flush_imsg(&ibuf);
99 069bbb86 2022-03-07 thomas }
100 069bbb86 2022-03-07 thomas
101 069bbb86 2022-03-07 thomas /* based on fetchname from usr.bin/patch/util.c */
102 069bbb86 2022-03-07 thomas static const struct got_error *
103 d9db2ff9 2022-04-16 thomas filename(const char *at, char **name)
104 069bbb86 2022-03-07 thomas {
105 d9db2ff9 2022-04-16 thomas char *tmp, *t;
106 069bbb86 2022-03-07 thomas
107 069bbb86 2022-03-07 thomas *name = NULL;
108 069bbb86 2022-03-07 thomas if (*at == '\0')
109 069bbb86 2022-03-07 thomas return NULL;
110 069bbb86 2022-03-07 thomas
111 069bbb86 2022-03-07 thomas while (isspace((unsigned char)*at))
112 069bbb86 2022-03-07 thomas at++;
113 069bbb86 2022-03-07 thomas
114 069bbb86 2022-03-07 thomas /* files can be created or removed by diffing against /dev/null */
115 17d6446a 2022-03-22 thomas if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
116 069bbb86 2022-03-07 thomas return NULL;
117 069bbb86 2022-03-07 thomas
118 d9db2ff9 2022-04-16 thomas tmp = strdup(at);
119 d9db2ff9 2022-04-16 thomas if (tmp == NULL)
120 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
121 d9db2ff9 2022-04-16 thomas if ((t = strchr(tmp, '\t')) != NULL)
122 d9db2ff9 2022-04-16 thomas *t = '\0';
123 d9db2ff9 2022-04-16 thomas if ((t = strchr(tmp, '\n')) != NULL)
124 d9db2ff9 2022-04-16 thomas *t = '\0';
125 069bbb86 2022-03-07 thomas
126 d9db2ff9 2022-04-16 thomas *name = strdup(tmp);
127 d9db2ff9 2022-04-16 thomas free(tmp);
128 069bbb86 2022-03-07 thomas if (*name == NULL)
129 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
130 069bbb86 2022-03-07 thomas return NULL;
131 069bbb86 2022-03-07 thomas }
132 069bbb86 2022-03-07 thomas
133 37e766f4 2022-09-21 thomas static int
134 650a3405 2023-01-02 thomas binary_deleted(const char *line)
135 650a3405 2023-01-02 thomas {
136 650a3405 2023-01-02 thomas const char *prefix = "Binary files ";
137 650a3405 2023-01-02 thomas const char *suffix = " and /dev/null differ\n";
138 650a3405 2023-01-02 thomas size_t len, d;
139 650a3405 2023-01-02 thomas
140 650a3405 2023-01-02 thomas if (strncmp(line, prefix, strlen(prefix)) != 0)
141 650a3405 2023-01-02 thomas return 0;
142 650a3405 2023-01-02 thomas line += strlen(prefix);
143 650a3405 2023-01-02 thomas
144 650a3405 2023-01-02 thomas len = strlen(line);
145 650a3405 2023-01-02 thomas if (len <= strlen(suffix))
146 650a3405 2023-01-02 thomas return 0;
147 650a3405 2023-01-02 thomas d = len - strlen(suffix);
148 650a3405 2023-01-02 thomas return (strcmp(line + d, suffix) == 0);
149 650a3405 2023-01-02 thomas }
150 650a3405 2023-01-02 thomas
151 650a3405 2023-01-02 thomas static const struct got_error *
152 650a3405 2023-01-02 thomas binaryfilename(const char *at, char **name)
153 650a3405 2023-01-02 thomas {
154 650a3405 2023-01-02 thomas const char *suffix = " and /dev/null differ\n";
155 650a3405 2023-01-02 thomas size_t len, d;
156 2b790b76 2023-01-02 thomas
157 2b790b76 2023-01-02 thomas *name = NULL;
158 650a3405 2023-01-02 thomas
159 650a3405 2023-01-02 thomas len = strlen(at);
160 650a3405 2023-01-02 thomas if (len <= strlen(suffix))
161 650a3405 2023-01-02 thomas return NULL;
162 650a3405 2023-01-02 thomas
163 650a3405 2023-01-02 thomas d = len - strlen(suffix);
164 650a3405 2023-01-02 thomas if (strcmp(at + d, suffix) != 0)
165 650a3405 2023-01-02 thomas return NULL;
166 650a3405 2023-01-02 thomas
167 650a3405 2023-01-02 thomas *name = strndup(at, d);
168 650a3405 2023-01-02 thomas if (*name == NULL)
169 650a3405 2023-01-02 thomas return got_error_from_errno("strndup");
170 650a3405 2023-01-02 thomas return NULL;
171 650a3405 2023-01-02 thomas }
172 650a3405 2023-01-02 thomas
173 650a3405 2023-01-02 thomas static int
174 37e766f4 2022-09-21 thomas filexbit(const char *line)
175 37e766f4 2022-09-21 thomas {
176 37e766f4 2022-09-21 thomas char *m;
177 37e766f4 2022-09-21 thomas
178 37e766f4 2022-09-21 thomas m = strchr(line, '(');
179 37e766f4 2022-09-21 thomas if (m && !strncmp(m + 1, "mode ", 5))
180 37e766f4 2022-09-21 thomas return strncmp(m + 6, "755", 3) == 0;
181 37e766f4 2022-09-21 thomas
182 37e766f4 2022-09-21 thomas return 0;
183 37e766f4 2022-09-21 thomas }
184 37e766f4 2022-09-21 thomas
185 069bbb86 2022-03-07 thomas static const struct got_error *
186 19dd85cb 2022-07-01 thomas blobid(const char *line, char **blob, int git)
187 0f76ab83 2022-06-23 thomas {
188 0f76ab83 2022-06-23 thomas uint8_t digest[SHA1_DIGEST_LENGTH];
189 0f76ab83 2022-06-23 thomas size_t len;
190 0f76ab83 2022-06-23 thomas
191 0f76ab83 2022-06-23 thomas *blob = NULL;
192 0f76ab83 2022-06-23 thomas
193 0f76ab83 2022-06-23 thomas len = strspn(line, "0123456789abcdefABCDEF");
194 0f76ab83 2022-06-23 thomas if ((*blob = strndup(line, len)) == NULL)
195 0f76ab83 2022-06-23 thomas return got_error_from_errno("strndup");
196 0f76ab83 2022-06-23 thomas
197 19dd85cb 2022-07-01 thomas if (!git && !got_parse_sha1_digest(digest, *blob)) {
198 0f76ab83 2022-06-23 thomas /* silently ignore invalid blob ids */
199 0f76ab83 2022-06-23 thomas free(*blob);
200 0f76ab83 2022-06-23 thomas *blob = NULL;
201 0f76ab83 2022-06-23 thomas }
202 0f76ab83 2022-06-23 thomas return NULL;
203 0f76ab83 2022-06-23 thomas }
204 0f76ab83 2022-06-23 thomas
205 0f76ab83 2022-06-23 thomas static const struct got_error *
206 68ceedb3 2022-07-03 thomas patch_start(int *git, char **cid, FILE *fp)
207 069bbb86 2022-03-07 thomas {
208 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
209 069bbb86 2022-03-07 thomas char *line = NULL;
210 069bbb86 2022-03-07 thomas size_t linesize = 0;
211 069bbb86 2022-03-07 thomas ssize_t linelen;
212 68ceedb3 2022-07-03 thomas
213 68ceedb3 2022-07-03 thomas *git = 0;
214 68ceedb3 2022-07-03 thomas
215 68ceedb3 2022-07-03 thomas while ((linelen = getline(&line, &linesize, fp)) != -1) {
216 68ceedb3 2022-07-03 thomas if (!strncmp(line, "diff --git ", 11)) {
217 68ceedb3 2022-07-03 thomas *git = 1;
218 68ceedb3 2022-07-03 thomas free(*cid);
219 68ceedb3 2022-07-03 thomas *cid = NULL;
220 68ceedb3 2022-07-03 thomas break;
221 68ceedb3 2022-07-03 thomas } else if (!strncmp(line, "diff ", 5)) {
222 68ceedb3 2022-07-03 thomas *git = 0;
223 68ceedb3 2022-07-03 thomas free(*cid);
224 68ceedb3 2022-07-03 thomas *cid = NULL;
225 68ceedb3 2022-07-03 thomas } else if (!strncmp(line, "commit - ", 9)) {
226 68ceedb3 2022-07-03 thomas free(*cid);
227 68ceedb3 2022-07-03 thomas err = blobid(line + 9, cid, *git);
228 68ceedb3 2022-07-03 thomas if (err)
229 68ceedb3 2022-07-03 thomas break;
230 68ceedb3 2022-07-03 thomas } else if (!strncmp(line, "--- ", 4) ||
231 68ceedb3 2022-07-03 thomas !strncmp(line, "+++ ", 4) ||
232 650a3405 2023-01-02 thomas !strncmp(line, "blob - ", 7) ||
233 650a3405 2023-01-02 thomas binary_deleted(line)) {
234 68ceedb3 2022-07-03 thomas /* rewind to previous line */
235 68ceedb3 2022-07-03 thomas if (fseeko(fp, -linelen, SEEK_CUR) == -1)
236 68ceedb3 2022-07-03 thomas err = got_error_from_errno("fseeko");
237 68ceedb3 2022-07-03 thomas break;
238 68ceedb3 2022-07-03 thomas }
239 68ceedb3 2022-07-03 thomas }
240 68ceedb3 2022-07-03 thomas
241 68ceedb3 2022-07-03 thomas free(line);
242 68ceedb3 2022-07-03 thomas if (ferror(fp) && err == NULL)
243 68ceedb3 2022-07-03 thomas err = got_error_from_errno("getline");
244 68ceedb3 2022-07-03 thomas if (feof(fp) && err == NULL)
245 68ceedb3 2022-07-03 thomas err = got_error(GOT_ERR_NO_PATCH);
246 68ceedb3 2022-07-03 thomas return err;
247 68ceedb3 2022-07-03 thomas }
248 68ceedb3 2022-07-03 thomas
249 68ceedb3 2022-07-03 thomas static const struct got_error *
250 68ceedb3 2022-07-03 thomas find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
251 68ceedb3 2022-07-03 thomas {
252 68ceedb3 2022-07-03 thomas const struct got_error *err = NULL;
253 68ceedb3 2022-07-03 thomas char *old = NULL, *new = NULL;
254 68ceedb3 2022-07-03 thomas char *blob = NULL;
255 68ceedb3 2022-07-03 thomas char *line = NULL;
256 68ceedb3 2022-07-03 thomas size_t linesize = 0;
257 68ceedb3 2022-07-03 thomas ssize_t linelen;
258 650a3405 2023-01-02 thomas int create, delete_binary = 0, rename = 0, xbit = 0;
259 069bbb86 2022-03-07 thomas
260 68ceedb3 2022-07-03 thomas *done = 0;
261 68ceedb3 2022-07-03 thomas *next = 0;
262 069bbb86 2022-03-07 thomas while ((linelen = getline(&line, &linesize, fp)) != -1) {
263 069bbb86 2022-03-07 thomas /*
264 069bbb86 2022-03-07 thomas * Ignore the Index name like GNU and larry' patch,
265 069bbb86 2022-03-07 thomas * we don't have to follow POSIX.
266 069bbb86 2022-03-07 thomas */
267 069bbb86 2022-03-07 thomas
268 d9db2ff9 2022-04-16 thomas if (!strncmp(line, "--- ", 4)) {
269 069bbb86 2022-03-07 thomas free(old);
270 d9db2ff9 2022-04-16 thomas err = filename(line+4, &old);
271 8afe1f71 2022-05-12 thomas } else if (rename && !strncmp(line, "rename from ", 12)) {
272 8afe1f71 2022-05-12 thomas free(old);
273 8afe1f71 2022-05-12 thomas err = filename(line+12, &old);
274 069bbb86 2022-03-07 thomas } else if (!strncmp(line, "+++ ", 4)) {
275 069bbb86 2022-03-07 thomas free(new);
276 d9db2ff9 2022-04-16 thomas err = filename(line+4, &new);
277 6d054bb9 2022-09-23 thomas } else if (!strncmp(line, "blob + ", 7) ||
278 6d054bb9 2022-09-23 thomas !strncmp(line, "file + ", 7)) {
279 37e766f4 2022-09-21 thomas xbit = filexbit(line);
280 0f76ab83 2022-06-23 thomas } else if (!git && !strncmp(line, "blob - ", 7)) {
281 0f76ab83 2022-06-23 thomas free(blob);
282 19dd85cb 2022-07-01 thomas err = blobid(line + 7, &blob, git);
283 650a3405 2023-01-02 thomas } else if (!strncmp(line, "Binary files ", 13)) {
284 650a3405 2023-01-02 thomas delete_binary = 1;
285 650a3405 2023-01-02 thomas free(old);
286 650a3405 2023-01-02 thomas err = binaryfilename(line + 13, &old);
287 8afe1f71 2022-05-12 thomas } else if (rename && !strncmp(line, "rename to ", 10)) {
288 8afe1f71 2022-05-12 thomas free(new);
289 8afe1f71 2022-05-12 thomas err = filename(line + 10, &new);
290 8afe1f71 2022-05-12 thomas } else if (git && !strncmp(line, "similarity index 100%", 21))
291 8afe1f71 2022-05-12 thomas rename = 1;
292 37e766f4 2022-09-21 thomas else if (git && !strncmp(line, "new file mode 100", 17))
293 37e766f4 2022-09-21 thomas xbit = strncmp(line + 17, "755", 3) == 0;
294 19dd85cb 2022-07-01 thomas else if (git && !strncmp(line, "index ", 6)) {
295 19dd85cb 2022-07-01 thomas free(blob);
296 19dd85cb 2022-07-01 thomas err = blobid(line + 6, &blob, git);
297 68ceedb3 2022-07-03 thomas } else if (!strncmp(line, "diff ", 5)) {
298 68ceedb3 2022-07-03 thomas /* rewind to previous line */
299 68ceedb3 2022-07-03 thomas if (fseeko(fp, -linelen, SEEK_CUR) == -1)
300 68ceedb3 2022-07-03 thomas err = got_error_from_errno("fseeko");
301 68ceedb3 2022-07-03 thomas *next = 1;
302 68ceedb3 2022-07-03 thomas break;
303 0f76ab83 2022-06-23 thomas }
304 069bbb86 2022-03-07 thomas
305 069bbb86 2022-03-07 thomas if (err)
306 8afe1f71 2022-05-12 thomas break;
307 8afe1f71 2022-05-12 thomas
308 8afe1f71 2022-05-12 thomas /*
309 8afe1f71 2022-05-12 thomas * Git-style diffs with "similarity index 100%" don't
310 8afe1f71 2022-05-12 thomas * have any hunks and ends with the "rename to foobar"
311 8afe1f71 2022-05-12 thomas * line.
312 8afe1f71 2022-05-12 thomas */
313 8afe1f71 2022-05-12 thomas if (rename && old != NULL && new != NULL) {
314 650a3405 2023-01-02 thomas *done = 1;
315 650a3405 2023-01-02 thomas err = send_patch(old, new, commitid,
316 650a3405 2023-01-02 thomas blob, xbit, git);
317 650a3405 2023-01-02 thomas break;
318 650a3405 2023-01-02 thomas }
319 650a3405 2023-01-02 thomas
320 650a3405 2023-01-02 thomas /*
321 650a3405 2023-01-02 thomas * Diffs that remove binary files have no hunks.
322 650a3405 2023-01-02 thomas */
323 650a3405 2023-01-02 thomas if (delete_binary && old != NULL) {
324 3b488fde 2022-05-12 thomas *done = 1;
325 016bfe4b 2022-06-23 thomas err = send_patch(old, new, commitid,
326 37e766f4 2022-09-21 thomas blob, xbit, git);
327 069bbb86 2022-03-07 thomas break;
328 8afe1f71 2022-05-12 thomas }
329 069bbb86 2022-03-07 thomas
330 069bbb86 2022-03-07 thomas if (!strncmp(line, "@@ -", 4)) {
331 069bbb86 2022-03-07 thomas create = !strncmp(line+4, "0,0", 3);
332 069bbb86 2022-03-07 thomas if ((old == NULL && new == NULL) ||
333 069bbb86 2022-03-07 thomas (!create && old == NULL))
334 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
335 069bbb86 2022-03-07 thomas else
336 016bfe4b 2022-06-23 thomas err = send_patch(old, new, commitid,
337 37e766f4 2022-09-21 thomas blob, xbit, git);
338 069bbb86 2022-03-07 thomas
339 069bbb86 2022-03-07 thomas if (err)
340 069bbb86 2022-03-07 thomas break;
341 069bbb86 2022-03-07 thomas
342 069bbb86 2022-03-07 thomas /* rewind to previous line */
343 bafaf650 2022-05-14 thomas if (fseeko(fp, -linelen, SEEK_CUR) == -1)
344 bafaf650 2022-05-14 thomas err = got_error_from_errno("fseeko");
345 069bbb86 2022-03-07 thomas break;
346 069bbb86 2022-03-07 thomas }
347 069bbb86 2022-03-07 thomas }
348 069bbb86 2022-03-07 thomas
349 19f1a2ac 2022-03-13 thomas free(old);
350 19f1a2ac 2022-03-13 thomas free(new);
351 0f76ab83 2022-06-23 thomas free(blob);
352 069bbb86 2022-03-07 thomas free(line);
353 069bbb86 2022-03-07 thomas if (ferror(fp) && err == NULL)
354 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
355 069bbb86 2022-03-07 thomas if (feof(fp) && err == NULL)
356 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_NO_PATCH);
357 069bbb86 2022-03-07 thomas return err;
358 069bbb86 2022-03-07 thomas }
359 069bbb86 2022-03-07 thomas
360 069bbb86 2022-03-07 thomas static const struct got_error *
361 8ebb3daa 2022-06-23 thomas strtolnum(char **str, int *n)
362 069bbb86 2022-03-07 thomas {
363 069bbb86 2022-03-07 thomas char *p, c;
364 069bbb86 2022-03-07 thomas const char *errstr;
365 069bbb86 2022-03-07 thomas
366 069bbb86 2022-03-07 thomas for (p = *str; isdigit((unsigned char)*p); ++p)
367 069bbb86 2022-03-07 thomas /* nop */;
368 069bbb86 2022-03-07 thomas
369 069bbb86 2022-03-07 thomas c = *p;
370 069bbb86 2022-03-07 thomas *p = '\0';
371 069bbb86 2022-03-07 thomas
372 8ebb3daa 2022-06-23 thomas *n = strtonum(*str, 0, INT_MAX, &errstr);
373 069bbb86 2022-03-07 thomas if (errstr != NULL)
374 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
375 069bbb86 2022-03-07 thomas
376 069bbb86 2022-03-07 thomas *p = c;
377 069bbb86 2022-03-07 thomas *str = p;
378 069bbb86 2022-03-07 thomas return NULL;
379 069bbb86 2022-03-07 thomas }
380 069bbb86 2022-03-07 thomas
381 069bbb86 2022-03-07 thomas static const struct got_error *
382 1992b6cb 2022-05-12 thomas parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
383 069bbb86 2022-03-07 thomas {
384 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
385 069bbb86 2022-03-07 thomas
386 069bbb86 2022-03-07 thomas if (strncmp(s, "@@ -", 4)) {
387 1992b6cb 2022-05-12 thomas *done = 1;
388 069bbb86 2022-03-07 thomas return NULL;
389 069bbb86 2022-03-07 thomas }
390 069bbb86 2022-03-07 thomas
391 069bbb86 2022-03-07 thomas s += 4;
392 069bbb86 2022-03-07 thomas if (!*s)
393 069bbb86 2022-03-07 thomas return NULL;
394 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldfrom);
395 069bbb86 2022-03-07 thomas if (err)
396 069bbb86 2022-03-07 thomas return err;
397 069bbb86 2022-03-07 thomas if (*s == ',') {
398 069bbb86 2022-03-07 thomas s++;
399 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldlines);
400 069bbb86 2022-03-07 thomas if (err)
401 069bbb86 2022-03-07 thomas return err;
402 069bbb86 2022-03-07 thomas } else
403 069bbb86 2022-03-07 thomas hdr->oldlines = 1;
404 069bbb86 2022-03-07 thomas
405 069bbb86 2022-03-07 thomas if (*s == ' ')
406 069bbb86 2022-03-07 thomas s++;
407 069bbb86 2022-03-07 thomas
408 069bbb86 2022-03-07 thomas if (*s != '+' || !*++s)
409 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
410 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->newfrom);
411 069bbb86 2022-03-07 thomas if (err)
412 069bbb86 2022-03-07 thomas return err;
413 069bbb86 2022-03-07 thomas if (*s == ',') {
414 069bbb86 2022-03-07 thomas s++;
415 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->newlines);
416 069bbb86 2022-03-07 thomas if (err)
417 069bbb86 2022-03-07 thomas return err;
418 069bbb86 2022-03-07 thomas } else
419 069bbb86 2022-03-07 thomas hdr->newlines = 1;
420 069bbb86 2022-03-07 thomas
421 069bbb86 2022-03-07 thomas if (*s == ' ')
422 069bbb86 2022-03-07 thomas s++;
423 069bbb86 2022-03-07 thomas
424 069bbb86 2022-03-07 thomas if (*s != '@')
425 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
426 069bbb86 2022-03-07 thomas
427 8ebb3daa 2022-06-23 thomas if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
428 8ebb3daa 2022-06-23 thomas hdr->newfrom >= INT_MAX - hdr->newlines ||
429 069bbb86 2022-03-07 thomas /* not so sure about this one */
430 8ebb3daa 2022-06-23 thomas hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
431 88c260f4 2022-05-14 thomas (hdr->oldlines == 0 && hdr->newlines == 0))
432 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
433 069bbb86 2022-03-07 thomas
434 069bbb86 2022-03-07 thomas if (hdr->oldlines == 0) {
435 069bbb86 2022-03-07 thomas /* larry says to "do append rather than insert"; I don't
436 069bbb86 2022-03-07 thomas * quite get it, but i trust him.
437 069bbb86 2022-03-07 thomas */
438 069bbb86 2022-03-07 thomas hdr->oldfrom++;
439 069bbb86 2022-03-07 thomas }
440 069bbb86 2022-03-07 thomas
441 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
442 069bbb86 2022-03-07 thomas hdr, sizeof(*hdr)) == -1)
443 069bbb86 2022-03-07 thomas return got_error_from_errno(
444 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_HUNK");
445 069bbb86 2022-03-07 thomas return NULL;
446 069bbb86 2022-03-07 thomas }
447 069bbb86 2022-03-07 thomas
448 069bbb86 2022-03-07 thomas static const struct got_error *
449 069bbb86 2022-03-07 thomas send_line(const char *line)
450 069bbb86 2022-03-07 thomas {
451 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
452 069bbb86 2022-03-07 thomas char *p = NULL;
453 069bbb86 2022-03-07 thomas
454 ff7f34d3 2022-03-22 thomas if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
455 069bbb86 2022-03-07 thomas if (asprintf(&p, " %s", line) == -1)
456 069bbb86 2022-03-07 thomas return got_error_from_errno("asprintf");
457 069bbb86 2022-03-07 thomas line = p;
458 069bbb86 2022-03-07 thomas }
459 069bbb86 2022-03-07 thomas
460 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
461 17d6446a 2022-03-22 thomas line, strlen(line) + 1) == -1)
462 069bbb86 2022-03-07 thomas err = got_error_from_errno(
463 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_LINE");
464 069bbb86 2022-03-07 thomas
465 069bbb86 2022-03-07 thomas free(p);
466 069bbb86 2022-03-07 thomas return err;
467 069bbb86 2022-03-07 thomas }
468 069bbb86 2022-03-07 thomas
469 069bbb86 2022-03-07 thomas static const struct got_error *
470 656c2baa 2022-04-23 thomas peek_special_line(FILE *fp)
471 ff7f34d3 2022-03-22 thomas {
472 ff7f34d3 2022-03-22 thomas const struct got_error *err;
473 1f954382 2022-03-22 thomas int ch;
474 ff7f34d3 2022-03-22 thomas
475 ff7f34d3 2022-03-22 thomas ch = fgetc(fp);
476 ff7f34d3 2022-03-22 thomas if (ch != EOF && ch != '\\') {
477 ff7f34d3 2022-03-22 thomas ungetc(ch, fp);
478 ff7f34d3 2022-03-22 thomas return NULL;
479 ff7f34d3 2022-03-22 thomas }
480 ff7f34d3 2022-03-22 thomas
481 656c2baa 2022-04-23 thomas if (ch == '\\') {
482 ff7f34d3 2022-03-22 thomas err = send_line("\\");
483 ff7f34d3 2022-03-22 thomas if (err)
484 ff7f34d3 2022-03-22 thomas return err;
485 ff7f34d3 2022-03-22 thomas }
486 ff7f34d3 2022-03-22 thomas
487 ff7f34d3 2022-03-22 thomas while (ch != EOF && ch != '\n')
488 ff7f34d3 2022-03-22 thomas ch = fgetc(fp);
489 ff7f34d3 2022-03-22 thomas
490 ff7f34d3 2022-03-22 thomas if (ch != EOF || feof(fp))
491 ff7f34d3 2022-03-22 thomas return NULL;
492 ff7f34d3 2022-03-22 thomas return got_error(GOT_ERR_IO);
493 ff7f34d3 2022-03-22 thomas }
494 ff7f34d3 2022-03-22 thomas
495 ff7f34d3 2022-03-22 thomas static const struct got_error *
496 1992b6cb 2022-05-12 thomas parse_hunk(FILE *fp, int *done)
497 069bbb86 2022-03-07 thomas {
498 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
499 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
500 069bbb86 2022-03-07 thomas char *line = NULL, ch;
501 069bbb86 2022-03-07 thomas size_t linesize = 0;
502 069bbb86 2022-03-07 thomas ssize_t linelen;
503 8ebb3daa 2022-06-23 thomas int leftold, leftnew;
504 069bbb86 2022-03-07 thomas
505 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, fp);
506 069bbb86 2022-03-07 thomas if (linelen == -1) {
507 1992b6cb 2022-05-12 thomas *done = 1;
508 069bbb86 2022-03-07 thomas goto done;
509 069bbb86 2022-03-07 thomas }
510 069bbb86 2022-03-07 thomas
511 1992b6cb 2022-05-12 thomas err = parse_hdr(line, done, &hdr);
512 069bbb86 2022-03-07 thomas if (err)
513 069bbb86 2022-03-07 thomas goto done;
514 1992b6cb 2022-05-12 thomas if (*done) {
515 bafaf650 2022-05-14 thomas if (fseeko(fp, -linelen, SEEK_CUR) == -1)
516 bafaf650 2022-05-14 thomas err = got_error_from_errno("fseeko");
517 069bbb86 2022-03-07 thomas goto done;
518 069bbb86 2022-03-07 thomas }
519 069bbb86 2022-03-07 thomas
520 069bbb86 2022-03-07 thomas leftold = hdr.oldlines;
521 069bbb86 2022-03-07 thomas leftnew = hdr.newlines;
522 069bbb86 2022-03-07 thomas
523 069bbb86 2022-03-07 thomas while (leftold > 0 || leftnew > 0) {
524 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, fp);
525 069bbb86 2022-03-07 thomas if (linelen == -1) {
526 069bbb86 2022-03-07 thomas if (ferror(fp)) {
527 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
528 069bbb86 2022-03-07 thomas goto done;
529 069bbb86 2022-03-07 thomas }
530 069bbb86 2022-03-07 thomas
531 069bbb86 2022-03-07 thomas /* trailing newlines may be chopped */
532 069bbb86 2022-03-07 thomas if (leftold < 3 && leftnew < 3) {
533 1992b6cb 2022-05-12 thomas *done = 1;
534 069bbb86 2022-03-07 thomas break;
535 069bbb86 2022-03-07 thomas }
536 069bbb86 2022-03-07 thomas
537 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_TRUNCATED);
538 069bbb86 2022-03-07 thomas goto done;
539 069bbb86 2022-03-07 thomas }
540 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
541 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
542 069bbb86 2022-03-07 thomas
543 069bbb86 2022-03-07 thomas /* usr.bin/patch allows '=' as context char */
544 069bbb86 2022-03-07 thomas if (*line == '=')
545 069bbb86 2022-03-07 thomas *line = ' ';
546 069bbb86 2022-03-07 thomas
547 069bbb86 2022-03-07 thomas ch = *line;
548 ff7f34d3 2022-03-22 thomas if (ch == '\t' || ch == '\0')
549 069bbb86 2022-03-07 thomas ch = ' '; /* the space got eaten */
550 069bbb86 2022-03-07 thomas
551 069bbb86 2022-03-07 thomas switch (ch) {
552 069bbb86 2022-03-07 thomas case '-':
553 069bbb86 2022-03-07 thomas leftold--;
554 069bbb86 2022-03-07 thomas break;
555 069bbb86 2022-03-07 thomas case ' ':
556 069bbb86 2022-03-07 thomas leftold--;
557 069bbb86 2022-03-07 thomas leftnew--;
558 069bbb86 2022-03-07 thomas break;
559 069bbb86 2022-03-07 thomas case '+':
560 069bbb86 2022-03-07 thomas leftnew--;
561 069bbb86 2022-03-07 thomas break;
562 069bbb86 2022-03-07 thomas default:
563 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
564 069bbb86 2022-03-07 thomas goto done;
565 069bbb86 2022-03-07 thomas }
566 069bbb86 2022-03-07 thomas
567 069bbb86 2022-03-07 thomas if (leftold < 0 || leftnew < 0) {
568 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
569 069bbb86 2022-03-07 thomas goto done;
570 069bbb86 2022-03-07 thomas }
571 069bbb86 2022-03-07 thomas
572 069bbb86 2022-03-07 thomas err = send_line(line);
573 069bbb86 2022-03-07 thomas if (err)
574 069bbb86 2022-03-07 thomas goto done;
575 ff7f34d3 2022-03-22 thomas
576 ff7f34d3 2022-03-22 thomas if ((ch == '-' && leftold == 0) ||
577 ff7f34d3 2022-03-22 thomas (ch == '+' && leftnew == 0)) {
578 656c2baa 2022-04-23 thomas err = peek_special_line(fp);
579 ff7f34d3 2022-03-22 thomas if (err)
580 ff7f34d3 2022-03-22 thomas goto done;
581 ff7f34d3 2022-03-22 thomas }
582 069bbb86 2022-03-07 thomas }
583 069bbb86 2022-03-07 thomas
584 069bbb86 2022-03-07 thomas done:
585 069bbb86 2022-03-07 thomas free(line);
586 069bbb86 2022-03-07 thomas return err;
587 069bbb86 2022-03-07 thomas }
588 069bbb86 2022-03-07 thomas
589 069bbb86 2022-03-07 thomas static const struct got_error *
590 069bbb86 2022-03-07 thomas read_patch(struct imsgbuf *ibuf, int fd)
591 069bbb86 2022-03-07 thomas {
592 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
593 069bbb86 2022-03-07 thomas FILE *fp;
594 68ceedb3 2022-07-03 thomas int git, patch_found = 0;
595 68ceedb3 2022-07-03 thomas char *cid = NULL;
596 069bbb86 2022-03-07 thomas
597 069bbb86 2022-03-07 thomas if ((fp = fdopen(fd, "r")) == NULL) {
598 069bbb86 2022-03-07 thomas err = got_error_from_errno("fdopen");
599 069bbb86 2022-03-07 thomas close(fd);
600 069bbb86 2022-03-07 thomas return err;
601 069bbb86 2022-03-07 thomas }
602 069bbb86 2022-03-07 thomas
603 68ceedb3 2022-07-03 thomas while ((err = patch_start(&git, &cid, fp)) == NULL) {
604 68ceedb3 2022-07-03 thomas int done, next;
605 8afe1f71 2022-05-12 thomas
606 68ceedb3 2022-07-03 thomas err = find_diff(&done, &next, fp, git, cid);
607 069bbb86 2022-03-07 thomas if (err)
608 069bbb86 2022-03-07 thomas goto done;
609 68ceedb3 2022-07-03 thomas if (next)
610 68ceedb3 2022-07-03 thomas continue;
611 069bbb86 2022-03-07 thomas
612 069bbb86 2022-03-07 thomas patch_found = 1;
613 1992b6cb 2022-05-12 thomas
614 1992b6cb 2022-05-12 thomas while (!done) {
615 1992b6cb 2022-05-12 thomas err = parse_hunk(fp, &done);
616 069bbb86 2022-03-07 thomas if (err)
617 069bbb86 2022-03-07 thomas goto done;
618 069bbb86 2022-03-07 thomas }
619 1992b6cb 2022-05-12 thomas
620 1992b6cb 2022-05-12 thomas err = send_patch_done();
621 1992b6cb 2022-05-12 thomas if (err)
622 1992b6cb 2022-05-12 thomas goto done;
623 069bbb86 2022-03-07 thomas }
624 069bbb86 2022-03-07 thomas
625 069bbb86 2022-03-07 thomas done:
626 069bbb86 2022-03-07 thomas fclose(fp);
627 68ceedb3 2022-07-03 thomas free(cid);
628 069bbb86 2022-03-07 thomas
629 069bbb86 2022-03-07 thomas /* ignore trailing gibberish */
630 069bbb86 2022-03-07 thomas if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
631 069bbb86 2022-03-07 thomas err = NULL;
632 069bbb86 2022-03-07 thomas
633 069bbb86 2022-03-07 thomas return err;
634 069bbb86 2022-03-07 thomas }
635 069bbb86 2022-03-07 thomas
636 069bbb86 2022-03-07 thomas int
637 069bbb86 2022-03-07 thomas main(int argc, char **argv)
638 069bbb86 2022-03-07 thomas {
639 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
640 069bbb86 2022-03-07 thomas struct imsg imsg;
641 069bbb86 2022-03-07 thomas #if 0
642 069bbb86 2022-03-07 thomas static int attached;
643 069bbb86 2022-03-07 thomas while (!attached)
644 069bbb86 2022-03-07 thomas sleep(1);
645 069bbb86 2022-03-07 thomas #endif
646 069bbb86 2022-03-07 thomas
647 069bbb86 2022-03-07 thomas imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
648 069bbb86 2022-03-07 thomas #ifndef PROFILE
649 069bbb86 2022-03-07 thomas /* revoke access to most system calls */
650 069bbb86 2022-03-07 thomas if (pledge("stdio recvfd", NULL) == -1) {
651 069bbb86 2022-03-07 thomas err = got_error_from_errno("pledge");
652 069bbb86 2022-03-07 thomas got_privsep_send_error(&ibuf, err);
653 069bbb86 2022-03-07 thomas return 1;
654 069bbb86 2022-03-07 thomas }
655 762ddcd8 2022-03-09 thomas
656 762ddcd8 2022-03-09 thomas /* revoke fs access */
657 762ddcd8 2022-03-09 thomas if (landlock_no_fs() == -1) {
658 762ddcd8 2022-03-09 thomas err = got_error_from_errno("landlock_no_fs");
659 762ddcd8 2022-03-09 thomas got_privsep_send_error(&ibuf, err);
660 762ddcd8 2022-03-09 thomas return 1;
661 762ddcd8 2022-03-09 thomas }
662 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
663 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
664 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
665 5d120ea8 2022-06-23 op return 1;
666 5d120ea8 2022-06-23 op }
667 069bbb86 2022-03-07 thomas #endif
668 069bbb86 2022-03-07 thomas
669 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
670 069bbb86 2022-03-07 thomas if (err)
671 069bbb86 2022-03-07 thomas goto done;
672 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
673 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
674 069bbb86 2022-03-07 thomas goto done;
675 069bbb86 2022-03-07 thomas }
676 069bbb86 2022-03-07 thomas
677 069bbb86 2022-03-07 thomas err = read_patch(&ibuf, imsg.fd);
678 069bbb86 2022-03-07 thomas if (err)
679 069bbb86 2022-03-07 thomas goto done;
680 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
681 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
682 069bbb86 2022-03-07 thomas err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
683 069bbb86 2022-03-07 thomas goto done;
684 069bbb86 2022-03-07 thomas }
685 069bbb86 2022-03-07 thomas err = got_privsep_flush_imsg(&ibuf);
686 069bbb86 2022-03-07 thomas done:
687 069bbb86 2022-03-07 thomas imsg_free(&imsg);
688 069bbb86 2022-03-07 thomas if (err != NULL) {
689 069bbb86 2022-03-07 thomas got_privsep_send_error(&ibuf, err);
690 069bbb86 2022-03-07 thomas err = NULL;
691 069bbb86 2022-03-07 thomas }
692 069bbb86 2022-03-07 thomas if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
693 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
694 069bbb86 2022-03-07 thomas if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
695 069bbb86 2022-03-07 thomas fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
696 069bbb86 2022-03-07 thomas return err ? 1 : 0;
697 069bbb86 2022-03-07 thomas }