store ibuf used by got_repo_read_gitconfig() on the stack
[got-portable.git] / libexec / got-read-patch / got-read-patch.c
blob05635602a7363af6461c1e736a79c4690a78da65
1 /*
2 * Copyright 1986, Larry Wall
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19 * SUCH DAMAGE.
23 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
25 * Permission to use, copy, modify, and distribute this software for any
26 * purpose with or without fee is hereby granted, provided that the above
27 * copyright notice and this permission notice appear in all copies.
29 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
30 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
32 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
33 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
34 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
35 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 #include "got_compat.h"
39 #include <sys/types.h>
40 #include <sys/queue.h>
41 #include <sys/uio.h>
43 #include <err.h>
44 #include <ctype.h>
45 #include <limits.h>
46 #include <paths.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
53 #include "got_error.h"
54 #include "got_object.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_hash.h"
58 #include "got_lib_object.h"
59 #include "got_lib_privsep.h"
61 struct imsgbuf ibuf;
63 static const struct got_error *
64 send_patch(const char *oldname, const char *newname, const char *commitid,
65 const char *blob, const int xbit, int git)
67 struct got_imsg_patch p;
69 memset(&p, 0, sizeof(p));
71 if (oldname != NULL)
72 strlcpy(p.old, oldname, sizeof(p.old));
74 if (newname != NULL)
75 strlcpy(p.new, newname, sizeof(p.new));
77 if (commitid != NULL)
78 strlcpy(p.cid, commitid, sizeof(p.cid));
80 if (blob != NULL)
81 strlcpy(p.blob, blob, sizeof(p.blob));
83 p.xbit = xbit;
84 p.git = git;
85 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
86 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
87 return NULL;
90 static const struct got_error *
91 send_patch_done(void)
93 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
94 NULL, 0) == -1)
95 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
96 return got_privsep_flush_imsg(&ibuf);
99 /* based on fetchname from usr.bin/patch/util.c */
100 static const struct got_error *
101 filename(const char *at, char **name)
103 char *tmp, *t;
105 *name = NULL;
106 if (*at == '\0')
107 return NULL;
109 while (isspace((unsigned char)*at))
110 at++;
112 /* files can be created or removed by diffing against /dev/null */
113 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
114 return NULL;
116 tmp = strdup(at);
117 if (tmp == NULL)
118 return got_error_from_errno("strdup");
119 if ((t = strchr(tmp, '\t')) != NULL)
120 *t = '\0';
121 if ((t = strchr(tmp, '\n')) != NULL)
122 *t = '\0';
124 *name = strdup(tmp);
125 free(tmp);
126 if (*name == NULL)
127 return got_error_from_errno("strdup");
128 return NULL;
131 static int
132 binary_deleted(const char *line)
134 const char *prefix = "Binary files ";
135 const char *suffix = " and /dev/null differ\n";
136 size_t len, d;
138 if (strncmp(line, prefix, strlen(prefix)) != 0)
139 return 0;
140 line += strlen(prefix);
142 len = strlen(line);
143 if (len <= strlen(suffix))
144 return 0;
145 d = len - strlen(suffix);
146 return (strcmp(line + d, suffix) == 0);
149 static const struct got_error *
150 binaryfilename(const char *at, char **name)
152 const char *suffix = " and /dev/null differ\n";
153 size_t len, d;
155 *name = NULL;
157 len = strlen(at);
158 if (len <= strlen(suffix))
159 return NULL;
161 d = len - strlen(suffix);
162 if (strcmp(at + d, suffix) != 0)
163 return NULL;
165 *name = strndup(at, d);
166 if (*name == NULL)
167 return got_error_from_errno("strndup");
168 return NULL;
171 static int
172 filexbit(const char *line)
174 char *m;
176 m = strchr(line, '(');
177 if (m && !strncmp(m + 1, "mode ", 5))
178 return strncmp(m + 6, "755", 3) == 0;
180 return 0;
183 static const struct got_error *
184 blobid(const char *line, char **blob, int git)
186 size_t len;
188 *blob = NULL;
190 len = strspn(line, "0123456789abcdefABCDEF");
191 if ((*blob = strndup(line, len)) == NULL)
192 return got_error_from_errno("strndup");
194 if (git)
195 return NULL;
197 if (len != got_hash_digest_string_length(GOT_HASH_SHA1) - 1 &&
198 len != got_hash_digest_string_length(GOT_HASH_SHA256) - 1) {
199 /* silently ignore invalid blob ids */
200 free(*blob);
201 *blob = NULL;
203 return NULL;
206 static const struct got_error *
207 patch_start(int *git, char **cid, FILE *fp)
209 const struct got_error *err = NULL;
210 char *line = NULL;
211 size_t linesize = 0;
212 ssize_t linelen;
214 *git = 0;
216 while ((linelen = getline(&line, &linesize, fp)) != -1) {
217 if (!strncmp(line, "diff --git ", 11)) {
218 *git = 1;
219 free(*cid);
220 *cid = NULL;
221 break;
222 } else if (!strncmp(line, "diff ", 5)) {
223 *git = 0;
224 free(*cid);
225 *cid = NULL;
226 } else if (!strncmp(line, "commit - ", 9)) {
227 free(*cid);
228 err = blobid(line + 9, cid, *git);
229 if (err)
230 break;
231 } else if (!strncmp(line, "--- ", 4) ||
232 !strncmp(line, "+++ ", 4) ||
233 !strncmp(line, "blob - ", 7) ||
234 binary_deleted(line)) {
235 /* rewind to previous line */
236 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
237 err = got_error_from_errno("fseeko");
238 break;
242 free(line);
243 if (ferror(fp) && err == NULL)
244 err = got_error_from_errno("getline");
245 if (feof(fp) && err == NULL)
246 err = got_error(GOT_ERR_NO_PATCH);
247 return err;
250 static const struct got_error *
251 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
253 const struct got_error *err = NULL;
254 char *old = NULL, *new = NULL;
255 char *blob = NULL;
256 char *line = NULL;
257 size_t linesize = 0;
258 ssize_t linelen;
259 int create, delete_binary = 0, rename = 0, xbit = 0;
261 *done = 0;
262 *next = 0;
263 while ((linelen = getline(&line, &linesize, fp)) != -1) {
265 * Ignore the Index name like GNU and larry' patch,
266 * we don't have to follow POSIX.
269 if (!strncmp(line, "--- ", 4)) {
270 free(old);
271 err = filename(line+4, &old);
272 } else if (rename && !strncmp(line, "rename from ", 12)) {
273 free(old);
274 err = filename(line+12, &old);
275 } else if (!strncmp(line, "+++ ", 4)) {
276 free(new);
277 err = filename(line+4, &new);
278 } else if (!strncmp(line, "blob + ", 7) ||
279 !strncmp(line, "file + ", 7)) {
280 xbit = filexbit(line);
281 } else if (!git && !strncmp(line, "blob - ", 7)) {
282 free(blob);
283 err = blobid(line + 7, &blob, git);
284 } else if (!strncmp(line, "Binary files ", 13)) {
285 delete_binary = 1;
286 free(old);
287 err = binaryfilename(line + 13, &old);
288 } else if (rename && !strncmp(line, "rename to ", 10)) {
289 free(new);
290 err = filename(line + 10, &new);
291 } else if (git && !strncmp(line, "similarity index 100%", 21))
292 rename = 1;
293 else if (git && !strncmp(line, "new file mode 100", 17))
294 xbit = strncmp(line + 17, "755", 3) == 0;
295 else if (git && !strncmp(line, "index ", 6)) {
296 free(blob);
297 err = blobid(line + 6, &blob, git);
298 } else if (!strncmp(line, "diff ", 5)) {
299 /* rewind to previous line */
300 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
301 err = got_error_from_errno("fseeko");
302 *next = 1;
303 break;
306 if (err)
307 break;
310 * Git-style diffs with "similarity index 100%" don't
311 * have any hunks and ends with the "rename to foobar"
312 * line.
314 if (rename && old != NULL && new != NULL) {
315 *done = 1;
316 err = send_patch(old, new, commitid,
317 blob, xbit, git);
318 break;
322 * Diffs that remove binary files have no hunks.
324 if (delete_binary && old != NULL) {
325 *done = 1;
326 err = send_patch(old, new, commitid,
327 blob, xbit, git);
328 break;
331 if (!strncmp(line, "@@ -", 4)) {
332 create = !strncmp(line+4, "0,0", 3);
333 if ((old == NULL && new == NULL) ||
334 (!create && old == NULL))
335 err = got_error_fmt(GOT_ERR_PATCH_MALFORMED,
336 "%s", line);
337 else
338 err = send_patch(old, new, commitid,
339 blob, xbit, git);
341 if (err)
342 break;
344 /* rewind to previous line */
345 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
346 err = got_error_from_errno("fseeko");
347 break;
351 free(old);
352 free(new);
353 free(blob);
354 free(line);
355 if (ferror(fp) && err == NULL)
356 err = got_error_from_errno("getline");
357 if (feof(fp) && err == NULL)
358 err = got_error(GOT_ERR_NO_PATCH);
359 return err;
362 static const struct got_error *
363 strtolnum(char **str, int *n)
365 char *p, c;
366 const char *errstr;
368 for (p = *str; isdigit((unsigned char)*p); ++p)
369 /* nop */;
371 c = *p;
372 *p = '\0';
374 *n = strtonum(*str, 0, INT_MAX, &errstr);
375 if (errstr != NULL)
376 return got_error_fmt(GOT_ERR_PATCH_MALFORMED,
377 "%s: %s", *str, errstr);
379 *p = c;
380 *str = p;
381 return NULL;
384 static const struct got_error *
385 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
387 const struct got_error *err = NULL;
388 char *s0 = s;
390 if (strncmp(s, "@@ -", 4)) {
391 *done = 1;
392 return NULL;
395 s += 4;
396 if (!*s)
397 return NULL;
398 err = strtolnum(&s, &hdr->oldfrom);
399 if (err)
400 return err;
401 if (*s == ',') {
402 s++;
403 err = strtolnum(&s, &hdr->oldlines);
404 if (err)
405 return err;
406 } else
407 hdr->oldlines = 1;
409 if (*s == ' ')
410 s++;
412 if (*s != '+' || !*++s)
413 return got_error_fmt(GOT_ERR_PATCH_MALFORMED, "%s", s0);
414 err = strtolnum(&s, &hdr->newfrom);
415 if (err)
416 return err;
417 if (*s == ',') {
418 s++;
419 err = strtolnum(&s, &hdr->newlines);
420 if (err)
421 return err;
422 } else
423 hdr->newlines = 1;
425 if (*s == ' ')
426 s++;
428 if (*s != '@')
429 return got_error_fmt(GOT_ERR_PATCH_MALFORMED, "%s", s0);
431 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
432 hdr->newfrom >= INT_MAX - hdr->newlines ||
433 /* not so sure about this one */
434 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
435 (hdr->oldlines == 0 && hdr->newlines == 0))
436 return got_error_fmt(GOT_ERR_PATCH_MALFORMED, "%s", s0);
438 if (hdr->oldlines == 0) {
439 /* larry says to "do append rather than insert"; I don't
440 * quite get it, but i trust him.
442 hdr->oldfrom++;
445 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
446 hdr, sizeof(*hdr)) == -1)
447 return got_error_from_errno(
448 "imsg_compose GOT_IMSG_PATCH_HUNK");
449 return NULL;
452 static const struct got_error *
453 send_line(const char *line, size_t len)
455 const struct got_error *err = NULL;
456 struct iovec iov[2];
457 int iovcnt = 0;
459 memset(&iov, 0, sizeof(iov));
461 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
462 iov[iovcnt].iov_base = (void *)" ";
463 iov[iovcnt].iov_len = 1;
464 iovcnt++;
467 iov[iovcnt].iov_base = (void *)line;
468 iov[iovcnt].iov_len = len;
469 iovcnt++;
471 if (imsg_composev(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
472 iov, iovcnt) == -1)
473 err = got_error_from_errno(
474 "imsg_compose GOT_IMSG_PATCH_LINE");
476 return err;
479 static const struct got_error *
480 peek_special_line(FILE *fp)
482 const struct got_error *err;
483 int ch;
485 ch = fgetc(fp);
486 if (ch != EOF && ch != '\\') {
487 ungetc(ch, fp);
488 return NULL;
491 if (ch == '\\') {
492 err = send_line("\\", 2);
493 if (err)
494 return err;
497 while (ch != EOF && ch != '\n')
498 ch = fgetc(fp);
500 if (ch != EOF || feof(fp))
501 return NULL;
502 return got_error(GOT_ERR_IO);
505 static const struct got_error *
506 parse_hunk(FILE *fp, int *done)
508 const struct got_error *err = NULL;
509 struct got_imsg_patch_hunk hdr;
510 char *line = NULL, ch;
511 size_t linesize = 0;
512 ssize_t linelen;
513 int leftold, leftnew;
515 linelen = getline(&line, &linesize, fp);
516 if (linelen == -1) {
517 *done = 1;
518 goto done;
520 if (line[linelen - 1] == '\n')
521 line[linelen - 1] = '\0';
523 err = parse_hdr(line, done, &hdr);
524 if (err)
525 goto done;
526 if (*done) {
527 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
528 err = got_error_from_errno("fseeko");
529 goto done;
532 leftold = hdr.oldlines;
533 leftnew = hdr.newlines;
535 while (leftold > 0 || leftnew > 0) {
536 linelen = getline(&line, &linesize, fp);
537 if (linelen == -1) {
538 if (ferror(fp)) {
539 err = got_error_from_errno("getline");
540 goto done;
543 /* trailing newlines may be chopped */
544 if (leftold < 3 && leftnew < 3) {
545 *done = 1;
546 break;
549 err = got_error(GOT_ERR_PATCH_TRUNCATED);
550 goto done;
552 if (line[linelen - 1] == '\n')
553 line[linelen - 1] = '\0';
555 /* usr.bin/patch allows '=' as context char */
556 if (*line == '=')
557 *line = ' ';
559 ch = *line;
560 if (ch == '\t' || ch == '\0')
561 ch = ' '; /* the space got eaten */
563 switch (ch) {
564 case '-':
565 leftold--;
566 break;
567 case ' ':
568 leftold--;
569 leftnew--;
570 break;
571 case '+':
572 leftnew--;
573 break;
574 default:
575 err = got_error_fmt(GOT_ERR_PATCH_MALFORMED,
576 "%s", line);
577 goto done;
580 if (leftold < 0 || leftnew < 0) {
581 err = got_error_fmt(GOT_ERR_PATCH_MALFORMED,
582 "%s", line);
583 goto done;
586 err = send_line(line, linelen);
587 if (err)
588 goto done;
590 if ((ch == '-' && leftold == 0) ||
591 (ch == '+' && leftnew == 0)) {
592 err = peek_special_line(fp);
593 if (err)
594 goto done;
598 done:
599 free(line);
600 return err;
603 static const struct got_error *
604 read_patch(struct imsgbuf *ibuf, FILE *fp)
606 const struct got_error *err = NULL;
607 int git, patch_found = 0;
608 char *cid = NULL;
610 while ((err = patch_start(&git, &cid, fp)) == NULL) {
611 int done, next;
613 err = find_diff(&done, &next, fp, git, cid);
614 if (err)
615 goto done;
616 if (next)
617 continue;
619 patch_found = 1;
621 while (!done) {
622 err = parse_hunk(fp, &done);
623 if (err)
624 goto done;
627 err = send_patch_done();
628 if (err)
629 goto done;
632 done:
633 free(cid);
635 /* ignore trailing gibberish */
636 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
637 err = NULL;
639 return err;
643 main(int argc, char **argv)
645 const struct got_error *err = NULL;
646 struct imsg imsg;
647 FILE *fp = NULL;
648 int fd = -1;
649 #if 0
650 static int attached;
651 while (!attached)
652 sleep(1);
653 #endif
655 if (imsgbuf_init(&ibuf, GOT_IMSG_FD_CHILD) == -1) {
656 warn("imsgbuf_init");
657 return 1;
659 imsgbuf_allow_fdpass(&ibuf);
660 #ifndef PROFILE
661 /* revoke access to most system calls */
662 if (pledge("stdio recvfd", NULL) == -1) {
663 err = got_error_from_errno("pledge");
664 got_privsep_send_error(&ibuf, err);
665 return 1;
668 /* revoke fs access */
669 if (landlock_no_fs() == -1) {
670 err = got_error_from_errno("landlock_no_fs");
671 got_privsep_send_error(&ibuf, err);
672 return 1;
674 if (cap_enter() == -1) {
675 err = got_error_from_errno("cap_enter");
676 got_privsep_send_error(&ibuf, err);
677 return 1;
679 #endif
681 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
682 if (err)
683 goto done;
684 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE) {
685 err = got_error(GOT_ERR_PRIVSEP_MSG);
686 goto done;
688 fd = imsg_get_fd(&imsg);
689 if (fd == -1) {
690 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
691 goto done;
694 fp = fdopen(fd, "r");
695 if (fp == NULL) {
696 err = got_error_from_errno("fdopen");
697 goto done;
699 fd = -1;
701 err = read_patch(&ibuf, fp);
702 if (err)
703 goto done;
704 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
705 NULL, 0) == -1) {
706 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
707 goto done;
709 err = got_privsep_flush_imsg(&ibuf);
710 imsg_free(&imsg);
711 done:
712 if (err != NULL) {
713 if (err->code != GOT_ERR_PRIVSEP_PIPE) {
714 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
715 fflush(stderr);
717 got_privsep_send_error(&ibuf, err);
719 if (fd != -1 && close(fd) == -1 && err == NULL)
720 err = got_error_from_errno("close");
721 if (fp != NULL && fclose(fp) == EOF && err == NULL)
722 err = got_error_from_errno("fclose");
723 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 return err ? 1 : 0;