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>
50 #include "got_error.h"
51 #include "got_object.h"
53 #include "got_compat.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_object.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
62 static const struct got_error *
63 send_patch(const char *oldname, const char *newname, const char *commitid,
64 const char *blob, int git)
66 struct got_imsg_patch p;
68 memset(&p, 0, sizeof(p));
71 strlcpy(p.old, oldname, sizeof(p.old));
74 strlcpy(p.new, newname, sizeof(p.new));
77 strlcpy(p.cid, commitid, sizeof(p.cid));
80 strlcpy(p.blob, blob, sizeof(p.blob));
83 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
84 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
88 static const struct got_error *
91 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
93 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
94 if (imsg_flush(&ibuf) == -1)
95 return got_error_from_errno("imsg_flush");
99 /* based on fetchname from usr.bin/patch/util.c */
100 static const struct got_error *
101 filename(const char *at, char **name)
109 while (isspace((unsigned char)*at))
112 /* files can be created or removed by diffing against /dev/null */
113 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
118 return got_error_from_errno("strdup");
119 if ((t = strchr(tmp, '\t')) != NULL)
121 if ((t = strchr(tmp, '\n')) != NULL)
127 return got_error_from_errno("strdup");
131 static const struct got_error *
132 blobid(const char *line, char **blob, int git)
134 uint8_t digest[SHA1_DIGEST_LENGTH];
139 len = strspn(line, "0123456789abcdefABCDEF");
140 if ((*blob = strndup(line, len)) == NULL)
141 return got_error_from_errno("strndup");
143 if (!git && !got_parse_sha1_digest(digest, *blob)) {
144 /* silently ignore invalid blob ids */
151 static const struct got_error *
152 patch_start(int *git, char **cid, FILE *fp)
154 const struct got_error *err = NULL;
161 while ((linelen = getline(&line, &linesize, fp)) != -1) {
162 if (!strncmp(line, "diff --git ", 11)) {
167 } else if (!strncmp(line, "diff ", 5)) {
171 } else if (!strncmp(line, "commit - ", 9)) {
173 err = blobid(line + 9, cid, *git);
176 } else if (!strncmp(line, "--- ", 4) ||
177 !strncmp(line, "+++ ", 4) ||
178 !strncmp(line, "blob - ", 7)) {
179 /* rewind to previous line */
180 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
181 err = got_error_from_errno("fseeko");
187 if (ferror(fp) && err == NULL)
188 err = got_error_from_errno("getline");
189 if (feof(fp) && err == NULL)
190 err = got_error(GOT_ERR_NO_PATCH);
194 static const struct got_error *
195 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
197 const struct got_error *err = NULL;
198 char *old = NULL, *new = NULL;
203 int create, rename = 0;
207 while ((linelen = getline(&line, &linesize, fp)) != -1) {
209 * Ignore the Index name like GNU and larry' patch,
210 * we don't have to follow POSIX.
213 if (!strncmp(line, "--- ", 4)) {
215 err = filename(line+4, &old);
216 } else if (rename && !strncmp(line, "rename from ", 12)) {
218 err = filename(line+12, &old);
219 } else if (!strncmp(line, "+++ ", 4)) {
221 err = filename(line+4, &new);
222 } else if (!git && !strncmp(line, "blob - ", 7)) {
224 err = blobid(line + 7, &blob, git);
225 } else if (rename && !strncmp(line, "rename to ", 10)) {
227 err = filename(line + 10, &new);
228 } else if (git && !strncmp(line, "similarity index 100%", 21))
230 else if (git && !strncmp(line, "index ", 6)) {
232 err = blobid(line + 6, &blob, git);
233 } else if (!strncmp(line, "diff ", 5)) {
234 /* rewind to previous line */
235 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
236 err = got_error_from_errno("fseeko");
245 * Git-style diffs with "similarity index 100%" don't
246 * have any hunks and ends with the "rename to foobar"
249 if (rename && old != NULL && new != NULL) {
251 err = send_patch(old, new, commitid,
256 if (!strncmp(line, "@@ -", 4)) {
257 create = !strncmp(line+4, "0,0", 3);
258 if ((old == NULL && new == NULL) ||
259 (!create && old == NULL))
260 err = got_error(GOT_ERR_PATCH_MALFORMED);
262 err = send_patch(old, new, commitid,
268 /* rewind to previous line */
269 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
270 err = got_error_from_errno("fseeko");
279 if (ferror(fp) && err == NULL)
280 err = got_error_from_errno("getline");
281 if (feof(fp) && err == NULL)
282 err = got_error(GOT_ERR_NO_PATCH);
286 static const struct got_error *
287 strtolnum(char **str, int *n)
292 for (p = *str; isdigit((unsigned char)*p); ++p)
298 *n = strtonum(*str, 0, INT_MAX, &errstr);
300 return got_error(GOT_ERR_PATCH_MALFORMED);
307 static const struct got_error *
308 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
310 static const struct got_error *err = NULL;
312 if (strncmp(s, "@@ -", 4)) {
320 err = strtolnum(&s, &hdr->oldfrom);
325 err = strtolnum(&s, &hdr->oldlines);
334 if (*s != '+' || !*++s)
335 return got_error(GOT_ERR_PATCH_MALFORMED);
336 err = strtolnum(&s, &hdr->newfrom);
341 err = strtolnum(&s, &hdr->newlines);
351 return got_error(GOT_ERR_PATCH_MALFORMED);
353 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
354 hdr->newfrom >= INT_MAX - hdr->newlines ||
355 /* not so sure about this one */
356 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
357 (hdr->oldlines == 0 && hdr->newlines == 0))
358 return got_error(GOT_ERR_PATCH_MALFORMED);
360 if (hdr->oldlines == 0) {
361 /* larry says to "do append rather than insert"; I don't
362 * quite get it, but i trust him.
367 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
368 hdr, sizeof(*hdr)) == -1)
369 return got_error_from_errno(
370 "imsg_compose GOT_IMSG_PATCH_HUNK");
374 static const struct got_error *
375 send_line(const char *line)
377 static const struct got_error *err = NULL;
380 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
381 if (asprintf(&p, " %s", line) == -1)
382 return got_error_from_errno("asprintf");
386 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
387 line, strlen(line) + 1) == -1)
388 err = got_error_from_errno(
389 "imsg_compose GOT_IMSG_PATCH_LINE");
395 static const struct got_error *
396 peek_special_line(FILE *fp)
398 const struct got_error *err;
402 if (ch != EOF && ch != '\\') {
408 err = send_line("\\");
413 while (ch != EOF && ch != '\n')
416 if (ch != EOF || feof(fp))
418 return got_error(GOT_ERR_IO);
421 static const struct got_error *
422 parse_hunk(FILE *fp, int *done)
424 static const struct got_error *err = NULL;
425 struct got_imsg_patch_hunk hdr;
426 char *line = NULL, ch;
429 int leftold, leftnew;
431 linelen = getline(&line, &linesize, fp);
437 err = parse_hdr(line, done, &hdr);
441 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
442 err = got_error_from_errno("fseeko");
446 leftold = hdr.oldlines;
447 leftnew = hdr.newlines;
449 while (leftold > 0 || leftnew > 0) {
450 linelen = getline(&line, &linesize, fp);
453 err = got_error_from_errno("getline");
457 /* trailing newlines may be chopped */
458 if (leftold < 3 && leftnew < 3) {
463 err = got_error(GOT_ERR_PATCH_TRUNCATED);
466 if (line[linelen - 1] == '\n')
467 line[linelen - 1] = '\0';
469 /* usr.bin/patch allows '=' as context char */
474 if (ch == '\t' || ch == '\0')
475 ch = ' '; /* the space got eaten */
489 err = got_error(GOT_ERR_PATCH_MALFORMED);
493 if (leftold < 0 || leftnew < 0) {
494 err = got_error(GOT_ERR_PATCH_MALFORMED);
498 err = send_line(line);
502 if ((ch == '-' && leftold == 0) ||
503 (ch == '+' && leftnew == 0)) {
504 err = peek_special_line(fp);
515 static const struct got_error *
516 read_patch(struct imsgbuf *ibuf, int fd)
518 const struct got_error *err = NULL;
520 int git, patch_found = 0;
523 if ((fp = fdopen(fd, "r")) == NULL) {
524 err = got_error_from_errno("fdopen");
529 while ((err = patch_start(&git, &cid, fp)) == NULL) {
532 err = find_diff(&done, &next, fp, git, cid);
541 err = parse_hunk(fp, &done);
546 err = send_patch_done();
555 /* ignore trailing gibberish */
556 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
563 main(int argc, char **argv)
565 const struct got_error *err = NULL;
573 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
575 /* revoke access to most system calls */
576 if (pledge("stdio recvfd", NULL) == -1) {
577 err = got_error_from_errno("pledge");
578 got_privsep_send_error(&ibuf, err);
582 /* revoke fs access */
583 if (landlock_no_fs() == -1) {
584 err = got_error_from_errno("landlock_no_fs");
585 got_privsep_send_error(&ibuf, err);
588 if (cap_enter() == -1) {
589 err = got_error_from_errno("cap_enter");
590 got_privsep_send_error(&ibuf, err);
595 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
598 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
599 err = got_error(GOT_ERR_PRIVSEP_MSG);
603 err = read_patch(&ibuf, imsg.fd);
606 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
608 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
611 err = got_privsep_flush_imsg(&ibuf);
615 got_privsep_send_error(&ibuf, err);
618 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
619 err = got_error_from_errno("close");
620 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
621 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);