2 069bbb86 2022-03-07 thomas * Copyright 1986, Larry Wall
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.
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.
23 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
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>
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>
53 069bbb86 2022-03-07 thomas #include "got_error.h"
54 069bbb86 2022-03-07 thomas #include "got_object.h"
56 973f3f6e 2022-03-07 thomas #include "got_compat.h"
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"
63 069bbb86 2022-03-07 thomas struct imsgbuf ibuf;
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)
69 069bbb86 2022-03-07 thomas struct got_imsg_patch p;
71 069bbb86 2022-03-07 thomas memset(&p, 0, sizeof(p));
73 d9db2ff9 2022-04-16 thomas if (oldname != NULL)
74 069bbb86 2022-03-07 thomas strlcpy(p.old, oldname, sizeof(p.old));
76 069bbb86 2022-03-07 thomas if (newname != NULL)
77 069bbb86 2022-03-07 thomas strlcpy(p.new, newname, sizeof(p.new));
79 8afec5d5 2022-07-01 thomas if (commitid != NULL)
80 016bfe4b 2022-06-23 thomas strlcpy(p.cid, commitid, sizeof(p.cid));
82 8afec5d5 2022-07-01 thomas if (blob != NULL)
83 0f76ab83 2022-06-23 thomas strlcpy(p.blob, blob, sizeof(p.blob));
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;
92 069bbb86 2022-03-07 thomas static const struct got_error *
93 069bbb86 2022-03-07 thomas send_patch_done(void)
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);
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)
105 d9db2ff9 2022-04-16 thomas char *tmp, *t;
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;
111 069bbb86 2022-03-07 thomas while (isspace((unsigned char)*at))
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;
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';
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;
133 37e766f4 2022-09-21 thomas static int
134 650a3405 2023-01-02 thomas binary_deleted(const char *line)
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;
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);
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);
151 650a3405 2023-01-02 thomas static const struct got_error *
152 650a3405 2023-01-02 thomas binaryfilename(const char *at, char **name)
154 650a3405 2023-01-02 thomas const char *suffix = " and /dev/null differ\n";
155 650a3405 2023-01-02 thomas size_t len, d;
157 2b790b76 2023-01-02 thomas *name = NULL;
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;
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;
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;
173 650a3405 2023-01-02 thomas static int
174 37e766f4 2022-09-21 thomas filexbit(const char *line)
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;
182 37e766f4 2022-09-21 thomas return 0;
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)
188 0f76ab83 2022-06-23 thomas uint8_t digest[SHA1_DIGEST_LENGTH];
189 0f76ab83 2022-06-23 thomas size_t len;
191 0f76ab83 2022-06-23 thomas *blob = NULL;
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");
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;
202 0f76ab83 2022-06-23 thomas return NULL;
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)
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;
213 68ceedb3 2022-07-03 thomas *git = 0;
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;
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);
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");
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;
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)
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;
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) {
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.
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;
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"
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);
321 650a3405 2023-01-02 thomas * Diffs that remove binary files have no hunks.
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);
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);
336 016bfe4b 2022-06-23 thomas err = send_patch(old, new, commitid,
337 37e766f4 2022-09-21 thomas blob, xbit, git);
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");
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;
360 069bbb86 2022-03-07 thomas static const struct got_error *
361 8ebb3daa 2022-06-23 thomas strtolnum(char **str, int *n)
363 069bbb86 2022-03-07 thomas char *p, c;
364 069bbb86 2022-03-07 thomas const char *errstr;
366 069bbb86 2022-03-07 thomas for (p = *str; isdigit((unsigned char)*p); ++p)
367 069bbb86 2022-03-07 thomas /* nop */;
370 069bbb86 2022-03-07 thomas *p = '\0';
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);
377 069bbb86 2022-03-07 thomas *str = p;
378 069bbb86 2022-03-07 thomas return NULL;
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)
384 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
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;
393 069bbb86 2022-03-07 thomas return NULL;
394 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldfrom);
396 069bbb86 2022-03-07 thomas return err;
397 069bbb86 2022-03-07 thomas if (*s == ',') {
399 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldlines);
401 069bbb86 2022-03-07 thomas return err;
403 069bbb86 2022-03-07 thomas hdr->oldlines = 1;
405 069bbb86 2022-03-07 thomas if (*s == ' ')
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);
412 069bbb86 2022-03-07 thomas return err;
413 069bbb86 2022-03-07 thomas if (*s == ',') {
415 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->newlines);
417 069bbb86 2022-03-07 thomas return err;
419 069bbb86 2022-03-07 thomas hdr->newlines = 1;
421 069bbb86 2022-03-07 thomas if (*s == ' ')
424 069bbb86 2022-03-07 thomas if (*s != '@')
425 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
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);
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.
438 069bbb86 2022-03-07 thomas hdr->oldfrom++;
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;
448 069bbb86 2022-03-07 thomas static const struct got_error *
449 069bbb86 2022-03-07 thomas send_line(const char *line)
451 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
452 069bbb86 2022-03-07 thomas char *p = NULL;
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;
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");
466 069bbb86 2022-03-07 thomas return err;
469 069bbb86 2022-03-07 thomas static const struct got_error *
470 656c2baa 2022-04-23 thomas peek_special_line(FILE *fp)
472 ff7f34d3 2022-03-22 thomas const struct got_error *err;
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;
481 656c2baa 2022-04-23 thomas if (ch == '\\') {
482 ff7f34d3 2022-03-22 thomas err = send_line("\\");
484 ff7f34d3 2022-03-22 thomas return err;
487 ff7f34d3 2022-03-22 thomas while (ch != EOF && ch != '\n')
488 ff7f34d3 2022-03-22 thomas ch = fgetc(fp);
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);
495 ff7f34d3 2022-03-22 thomas static const struct got_error *
496 1992b6cb 2022-05-12 thomas parse_hunk(FILE *fp, int *done)
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;
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;
511 1992b6cb 2022-05-12 thomas err = parse_hdr(line, done, &hdr);
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;
520 069bbb86 2022-03-07 thomas leftold = hdr.oldlines;
521 069bbb86 2022-03-07 thomas leftnew = hdr.newlines;
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;
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;
537 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_TRUNCATED);
538 069bbb86 2022-03-07 thomas goto done;
540 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
541 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
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 = ' ';
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 */
551 069bbb86 2022-03-07 thomas switch (ch) {
552 069bbb86 2022-03-07 thomas case '-':
553 069bbb86 2022-03-07 thomas leftold--;
555 069bbb86 2022-03-07 thomas case ' ':
556 069bbb86 2022-03-07 thomas leftold--;
557 069bbb86 2022-03-07 thomas leftnew--;
559 069bbb86 2022-03-07 thomas case '+':
560 069bbb86 2022-03-07 thomas leftnew--;
563 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
564 069bbb86 2022-03-07 thomas goto done;
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;
572 069bbb86 2022-03-07 thomas err = send_line(line);
574 069bbb86 2022-03-07 thomas goto done;
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);
580 ff7f34d3 2022-03-22 thomas goto done;
585 069bbb86 2022-03-07 thomas free(line);
586 069bbb86 2022-03-07 thomas return err;
589 069bbb86 2022-03-07 thomas static const struct got_error *
590 069bbb86 2022-03-07 thomas read_patch(struct imsgbuf *ibuf, int fd)
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;
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;
603 68ceedb3 2022-07-03 thomas while ((err = patch_start(&git, &cid, fp)) == NULL) {
604 68ceedb3 2022-07-03 thomas int done, next;
606 68ceedb3 2022-07-03 thomas err = find_diff(&done, &next, fp, git, cid);
608 069bbb86 2022-03-07 thomas goto done;
609 68ceedb3 2022-07-03 thomas if (next)
610 68ceedb3 2022-07-03 thomas continue;
612 069bbb86 2022-03-07 thomas patch_found = 1;
614 1992b6cb 2022-05-12 thomas while (!done) {
615 1992b6cb 2022-05-12 thomas err = parse_hunk(fp, &done);
617 069bbb86 2022-03-07 thomas goto done;
620 1992b6cb 2022-05-12 thomas err = send_patch_done();
622 1992b6cb 2022-05-12 thomas goto done;
626 069bbb86 2022-03-07 thomas fclose(fp);
627 68ceedb3 2022-07-03 thomas free(cid);
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;
633 069bbb86 2022-03-07 thomas return err;
637 069bbb86 2022-03-07 thomas main(int argc, char **argv)
639 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
640 069bbb86 2022-03-07 thomas struct imsg imsg;
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);
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;
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;
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);
669 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
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;
677 069bbb86 2022-03-07 thomas err = read_patch(&ibuf, imsg.fd);
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;
685 069bbb86 2022-03-07 thomas err = got_privsep_flush_imsg(&ibuf);
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;
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;