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
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.
38 #include <sys/types.h>
39 #include <sys/queue.h>
53 #include "got_error.h"
54 #include "got_object.h"
56 #include "got_compat.h"
58 #include "got_lib_delta.h"
59 #include "got_lib_object.h"
60 #include "got_lib_privsep.h"
61 #include "got_lib_hash.h"
65 static const struct got_error *
66 send_patch(const char *oldname, const char *newname, const char *commitid,
67 const char *blob, const int xbit, int git)
69 struct got_imsg_patch p;
71 memset(&p, 0, sizeof(p));
74 strlcpy(p.old, oldname, sizeof(p.old));
77 strlcpy(p.new, newname, sizeof(p.new));
80 strlcpy(p.cid, commitid, sizeof(p.cid));
83 strlcpy(p.blob, blob, sizeof(p.blob));
87 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
88 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
92 static const struct got_error *
95 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
97 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
98 return got_privsep_flush_imsg(&ibuf);
101 /* based on fetchname from usr.bin/patch/util.c */
102 static const struct got_error *
103 filename(const char *at, char **name)
111 while (isspace((unsigned char)*at))
114 /* files can be created or removed by diffing against /dev/null */
115 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
120 return got_error_from_errno("strdup");
121 if ((t = strchr(tmp, '\t')) != NULL)
123 if ((t = strchr(tmp, '\n')) != NULL)
129 return got_error_from_errno("strdup");
134 binary_deleted(const char *line)
136 const char *prefix = "Binary files ";
137 const char *suffix = " and /dev/null differ\n";
140 if (strncmp(line, prefix, strlen(prefix)) != 0)
142 line += strlen(prefix);
145 if (len <= strlen(suffix))
147 d = len - strlen(suffix);
148 return (strcmp(line + d, suffix) == 0);
151 static const struct got_error *
152 binaryfilename(const char *at, char **name)
154 const char *suffix = " and /dev/null differ\n";
160 if (len <= strlen(suffix))
163 d = len - strlen(suffix);
164 if (strcmp(at + d, suffix) != 0)
167 *name = strndup(at, d);
169 return got_error_from_errno("strndup");
174 filexbit(const char *line)
178 m = strchr(line, '(');
179 if (m && !strncmp(m + 1, "mode ", 5))
180 return strncmp(m + 6, "755", 3) == 0;
185 static const struct got_error *
186 blobid(const char *line, char **blob, int git)
188 uint8_t digest[SHA1_DIGEST_LENGTH];
193 len = strspn(line, "0123456789abcdefABCDEF");
194 if ((*blob = strndup(line, len)) == NULL)
195 return got_error_from_errno("strndup");
197 if (!git && !got_parse_sha1_digest(digest, *blob)) {
198 /* silently ignore invalid blob ids */
205 static const struct got_error *
206 patch_start(int *git, char **cid, FILE *fp)
208 const struct got_error *err = NULL;
215 while ((linelen = getline(&line, &linesize, fp)) != -1) {
216 if (!strncmp(line, "diff --git ", 11)) {
221 } else if (!strncmp(line, "diff ", 5)) {
225 } else if (!strncmp(line, "commit - ", 9)) {
227 err = blobid(line + 9, cid, *git);
230 } else if (!strncmp(line, "--- ", 4) ||
231 !strncmp(line, "+++ ", 4) ||
232 !strncmp(line, "blob - ", 7) ||
233 binary_deleted(line)) {
234 /* rewind to previous line */
235 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
236 err = got_error_from_errno("fseeko");
242 if (ferror(fp) && err == NULL)
243 err = got_error_from_errno("getline");
244 if (feof(fp) && err == NULL)
245 err = got_error(GOT_ERR_NO_PATCH);
249 static const struct got_error *
250 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
252 const struct got_error *err = NULL;
253 char *old = NULL, *new = NULL;
258 int create, delete_binary = 0, rename = 0, xbit = 0;
262 while ((linelen = getline(&line, &linesize, fp)) != -1) {
264 * Ignore the Index name like GNU and larry' patch,
265 * we don't have to follow POSIX.
268 if (!strncmp(line, "--- ", 4)) {
270 err = filename(line+4, &old);
271 } else if (rename && !strncmp(line, "rename from ", 12)) {
273 err = filename(line+12, &old);
274 } else if (!strncmp(line, "+++ ", 4)) {
276 err = filename(line+4, &new);
277 } else if (!strncmp(line, "blob + ", 7) ||
278 !strncmp(line, "file + ", 7)) {
279 xbit = filexbit(line);
280 } else if (!git && !strncmp(line, "blob - ", 7)) {
282 err = blobid(line + 7, &blob, git);
283 } else if (!strncmp(line, "Binary files ", 13)) {
286 err = binaryfilename(line + 13, &old);
287 } else if (rename && !strncmp(line, "rename to ", 10)) {
289 err = filename(line + 10, &new);
290 } else if (git && !strncmp(line, "similarity index 100%", 21))
292 else if (git && !strncmp(line, "new file mode 100", 17))
293 xbit = strncmp(line + 17, "755", 3) == 0;
294 else if (git && !strncmp(line, "index ", 6)) {
296 err = blobid(line + 6, &blob, git);
297 } else if (!strncmp(line, "diff ", 5)) {
298 /* rewind to previous line */
299 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
300 err = got_error_from_errno("fseeko");
309 * Git-style diffs with "similarity index 100%" don't
310 * have any hunks and ends with the "rename to foobar"
313 if (rename && old != NULL && new != NULL) {
315 err = send_patch(old, new, commitid,
321 * Diffs that remove binary files have no hunks.
323 if (delete_binary && old != NULL) {
325 err = send_patch(old, new, commitid,
330 if (!strncmp(line, "@@ -", 4)) {
331 create = !strncmp(line+4, "0,0", 3);
332 if ((old == NULL && new == NULL) ||
333 (!create && old == NULL))
334 err = got_error(GOT_ERR_PATCH_MALFORMED);
336 err = send_patch(old, new, commitid,
342 /* rewind to previous line */
343 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
344 err = got_error_from_errno("fseeko");
353 if (ferror(fp) && err == NULL)
354 err = got_error_from_errno("getline");
355 if (feof(fp) && err == NULL)
356 err = got_error(GOT_ERR_NO_PATCH);
360 static const struct got_error *
361 strtolnum(char **str, int *n)
366 for (p = *str; isdigit((unsigned char)*p); ++p)
372 *n = strtonum(*str, 0, INT_MAX, &errstr);
374 return got_error(GOT_ERR_PATCH_MALFORMED);
381 static const struct got_error *
382 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
384 static const struct got_error *err = NULL;
386 if (strncmp(s, "@@ -", 4)) {
394 err = strtolnum(&s, &hdr->oldfrom);
399 err = strtolnum(&s, &hdr->oldlines);
408 if (*s != '+' || !*++s)
409 return got_error(GOT_ERR_PATCH_MALFORMED);
410 err = strtolnum(&s, &hdr->newfrom);
415 err = strtolnum(&s, &hdr->newlines);
425 return got_error(GOT_ERR_PATCH_MALFORMED);
427 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
428 hdr->newfrom >= INT_MAX - hdr->newlines ||
429 /* not so sure about this one */
430 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
431 (hdr->oldlines == 0 && hdr->newlines == 0))
432 return got_error(GOT_ERR_PATCH_MALFORMED);
434 if (hdr->oldlines == 0) {
435 /* larry says to "do append rather than insert"; I don't
436 * quite get it, but i trust him.
441 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
442 hdr, sizeof(*hdr)) == -1)
443 return got_error_from_errno(
444 "imsg_compose GOT_IMSG_PATCH_HUNK");
448 static const struct got_error *
449 send_line(const char *line)
451 static const struct got_error *err = NULL;
454 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
455 if (asprintf(&p, " %s", line) == -1)
456 return got_error_from_errno("asprintf");
460 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
461 line, strlen(line) + 1) == -1)
462 err = got_error_from_errno(
463 "imsg_compose GOT_IMSG_PATCH_LINE");
469 static const struct got_error *
470 peek_special_line(FILE *fp)
472 const struct got_error *err;
476 if (ch != EOF && ch != '\\') {
482 err = send_line("\\");
487 while (ch != EOF && ch != '\n')
490 if (ch != EOF || feof(fp))
492 return got_error(GOT_ERR_IO);
495 static const struct got_error *
496 parse_hunk(FILE *fp, int *done)
498 static const struct got_error *err = NULL;
499 struct got_imsg_patch_hunk hdr;
500 char *line = NULL, ch;
503 int leftold, leftnew;
505 linelen = getline(&line, &linesize, fp);
511 err = parse_hdr(line, done, &hdr);
515 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
516 err = got_error_from_errno("fseeko");
520 leftold = hdr.oldlines;
521 leftnew = hdr.newlines;
523 while (leftold > 0 || leftnew > 0) {
524 linelen = getline(&line, &linesize, fp);
527 err = got_error_from_errno("getline");
531 /* trailing newlines may be chopped */
532 if (leftold < 3 && leftnew < 3) {
537 err = got_error(GOT_ERR_PATCH_TRUNCATED);
540 if (line[linelen - 1] == '\n')
541 line[linelen - 1] = '\0';
543 /* usr.bin/patch allows '=' as context char */
548 if (ch == '\t' || ch == '\0')
549 ch = ' '; /* the space got eaten */
563 err = got_error(GOT_ERR_PATCH_MALFORMED);
567 if (leftold < 0 || leftnew < 0) {
568 err = got_error(GOT_ERR_PATCH_MALFORMED);
572 err = send_line(line);
576 if ((ch == '-' && leftold == 0) ||
577 (ch == '+' && leftnew == 0)) {
578 err = peek_special_line(fp);
589 static const struct got_error *
590 read_patch(struct imsgbuf *ibuf, int fd)
592 const struct got_error *err = NULL;
594 int git, patch_found = 0;
597 if ((fp = fdopen(fd, "r")) == NULL) {
598 err = got_error_from_errno("fdopen");
603 while ((err = patch_start(&git, &cid, fp)) == NULL) {
606 err = find_diff(&done, &next, fp, git, cid);
615 err = parse_hunk(fp, &done);
620 err = send_patch_done();
629 /* ignore trailing gibberish */
630 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
637 main(int argc, char **argv)
639 const struct got_error *err = NULL;
647 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
649 /* revoke access to most system calls */
650 if (pledge("stdio recvfd", NULL) == -1) {
651 err = got_error_from_errno("pledge");
652 got_privsep_send_error(&ibuf, err);
656 /* revoke fs access */
657 if (landlock_no_fs() == -1) {
658 err = got_error_from_errno("landlock_no_fs");
659 got_privsep_send_error(&ibuf, err);
662 if (cap_enter() == -1) {
663 err = got_error_from_errno("cap_enter");
664 got_privsep_send_error(&ibuf, err);
669 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
672 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
673 err = got_error(GOT_ERR_PRIVSEP_MSG);
677 err = read_patch(&ibuf, imsg.fd);
680 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
682 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
685 err = got_privsep_flush_imsg(&ibuf);
689 got_privsep_send_error(&ibuf, err);
692 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
693 err = got_error_from_errno("close");
694 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
695 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);