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.
37 #include "got_compat.h"
39 #include <sys/types.h>
40 #include <sys/queue.h>
52 #include "got_error.h"
53 #include "got_object.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_object.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_hash.h"
62 static const struct got_error *
63 send_patch(const char *oldname, const char *newname, const char *commitid,
64 const char *blob, const int xbit, 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));
84 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
85 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
89 static const struct got_error *
92 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
94 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
95 return got_privsep_flush_imsg(&ibuf);
98 /* based on fetchname from usr.bin/patch/util.c */
99 static const struct got_error *
100 filename(const char *at, char **name)
108 while (isspace((unsigned char)*at))
111 /* files can be created or removed by diffing against /dev/null */
112 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
117 return got_error_from_errno("strdup");
118 if ((t = strchr(tmp, '\t')) != NULL)
120 if ((t = strchr(tmp, '\n')) != NULL)
126 return got_error_from_errno("strdup");
131 binary_deleted(const char *line)
133 const char *prefix = "Binary files ";
134 const char *suffix = " and /dev/null differ\n";
137 if (strncmp(line, prefix, strlen(prefix)) != 0)
139 line += strlen(prefix);
142 if (len <= strlen(suffix))
144 d = len - strlen(suffix);
145 return (strcmp(line + d, suffix) == 0);
148 static const struct got_error *
149 binaryfilename(const char *at, char **name)
151 const char *suffix = " and /dev/null differ\n";
157 if (len <= strlen(suffix))
160 d = len - strlen(suffix);
161 if (strcmp(at + d, suffix) != 0)
164 *name = strndup(at, d);
166 return got_error_from_errno("strndup");
171 filexbit(const char *line)
175 m = strchr(line, '(');
176 if (m && !strncmp(m + 1, "mode ", 5))
177 return strncmp(m + 6, "755", 3) == 0;
182 static const struct got_error *
183 blobid(const char *line, char **blob, int git)
185 uint8_t digest[SHA1_DIGEST_LENGTH];
190 len = strspn(line, "0123456789abcdefABCDEF");
191 if ((*blob = strndup(line, len)) == NULL)
192 return got_error_from_errno("strndup");
194 if (!git && !got_parse_hash_digest(digest, *blob, GOT_HASH_SHA1)) {
195 /* silently ignore invalid blob ids */
202 static const struct got_error *
203 patch_start(int *git, char **cid, FILE *fp)
205 const struct got_error *err = NULL;
212 while ((linelen = getline(&line, &linesize, fp)) != -1) {
213 if (!strncmp(line, "diff --git ", 11)) {
218 } else if (!strncmp(line, "diff ", 5)) {
222 } else if (!strncmp(line, "commit - ", 9)) {
224 err = blobid(line + 9, cid, *git);
227 } else if (!strncmp(line, "--- ", 4) ||
228 !strncmp(line, "+++ ", 4) ||
229 !strncmp(line, "blob - ", 7) ||
230 binary_deleted(line)) {
231 /* rewind to previous line */
232 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
233 err = got_error_from_errno("fseeko");
239 if (ferror(fp) && err == NULL)
240 err = got_error_from_errno("getline");
241 if (feof(fp) && err == NULL)
242 err = got_error(GOT_ERR_NO_PATCH);
246 static const struct got_error *
247 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
249 const struct got_error *err = NULL;
250 char *old = NULL, *new = NULL;
255 int create, delete_binary = 0, rename = 0, xbit = 0;
259 while ((linelen = getline(&line, &linesize, fp)) != -1) {
261 * Ignore the Index name like GNU and larry' patch,
262 * we don't have to follow POSIX.
265 if (!strncmp(line, "--- ", 4)) {
267 err = filename(line+4, &old);
268 } else if (rename && !strncmp(line, "rename from ", 12)) {
270 err = filename(line+12, &old);
271 } else if (!strncmp(line, "+++ ", 4)) {
273 err = filename(line+4, &new);
274 } else if (!strncmp(line, "blob + ", 7) ||
275 !strncmp(line, "file + ", 7)) {
276 xbit = filexbit(line);
277 } else if (!git && !strncmp(line, "blob - ", 7)) {
279 err = blobid(line + 7, &blob, git);
280 } else if (!strncmp(line, "Binary files ", 13)) {
283 err = binaryfilename(line + 13, &old);
284 } else if (rename && !strncmp(line, "rename to ", 10)) {
286 err = filename(line + 10, &new);
287 } else if (git && !strncmp(line, "similarity index 100%", 21))
289 else if (git && !strncmp(line, "new file mode 100", 17))
290 xbit = strncmp(line + 17, "755", 3) == 0;
291 else if (git && !strncmp(line, "index ", 6)) {
293 err = blobid(line + 6, &blob, git);
294 } else if (!strncmp(line, "diff ", 5)) {
295 /* rewind to previous line */
296 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
297 err = got_error_from_errno("fseeko");
306 * Git-style diffs with "similarity index 100%" don't
307 * have any hunks and ends with the "rename to foobar"
310 if (rename && old != NULL && new != NULL) {
312 err = send_patch(old, new, commitid,
318 * Diffs that remove binary files have no hunks.
320 if (delete_binary && old != NULL) {
322 err = send_patch(old, new, commitid,
327 if (!strncmp(line, "@@ -", 4)) {
328 create = !strncmp(line+4, "0,0", 3);
329 if ((old == NULL && new == NULL) ||
330 (!create && old == NULL))
331 err = got_error(GOT_ERR_PATCH_MALFORMED);
333 err = send_patch(old, new, commitid,
339 /* rewind to previous line */
340 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
341 err = got_error_from_errno("fseeko");
350 if (ferror(fp) && err == NULL)
351 err = got_error_from_errno("getline");
352 if (feof(fp) && err == NULL)
353 err = got_error(GOT_ERR_NO_PATCH);
357 static const struct got_error *
358 strtolnum(char **str, int *n)
363 for (p = *str; isdigit((unsigned char)*p); ++p)
369 *n = strtonum(*str, 0, INT_MAX, &errstr);
371 return got_error(GOT_ERR_PATCH_MALFORMED);
378 static const struct got_error *
379 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
381 static const struct got_error *err = NULL;
383 if (strncmp(s, "@@ -", 4)) {
391 err = strtolnum(&s, &hdr->oldfrom);
396 err = strtolnum(&s, &hdr->oldlines);
405 if (*s != '+' || !*++s)
406 return got_error(GOT_ERR_PATCH_MALFORMED);
407 err = strtolnum(&s, &hdr->newfrom);
412 err = strtolnum(&s, &hdr->newlines);
422 return got_error(GOT_ERR_PATCH_MALFORMED);
424 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
425 hdr->newfrom >= INT_MAX - hdr->newlines ||
426 /* not so sure about this one */
427 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
428 (hdr->oldlines == 0 && hdr->newlines == 0))
429 return got_error(GOT_ERR_PATCH_MALFORMED);
431 if (hdr->oldlines == 0) {
432 /* larry says to "do append rather than insert"; I don't
433 * quite get it, but i trust him.
438 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
439 hdr, sizeof(*hdr)) == -1)
440 return got_error_from_errno(
441 "imsg_compose GOT_IMSG_PATCH_HUNK");
445 static const struct got_error *
446 send_line(const char *line)
448 static const struct got_error *err = NULL;
451 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
452 if (asprintf(&p, " %s", line) == -1)
453 return got_error_from_errno("asprintf");
457 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
458 line, strlen(line) + 1) == -1)
459 err = got_error_from_errno(
460 "imsg_compose GOT_IMSG_PATCH_LINE");
466 static const struct got_error *
467 peek_special_line(FILE *fp)
469 const struct got_error *err;
473 if (ch != EOF && ch != '\\') {
479 err = send_line("\\");
484 while (ch != EOF && ch != '\n')
487 if (ch != EOF || feof(fp))
489 return got_error(GOT_ERR_IO);
492 static const struct got_error *
493 parse_hunk(FILE *fp, int *done)
495 static const struct got_error *err = NULL;
496 struct got_imsg_patch_hunk hdr;
497 char *line = NULL, ch;
500 int leftold, leftnew;
502 linelen = getline(&line, &linesize, fp);
508 err = parse_hdr(line, done, &hdr);
512 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
513 err = got_error_from_errno("fseeko");
517 leftold = hdr.oldlines;
518 leftnew = hdr.newlines;
520 while (leftold > 0 || leftnew > 0) {
521 linelen = getline(&line, &linesize, fp);
524 err = got_error_from_errno("getline");
528 /* trailing newlines may be chopped */
529 if (leftold < 3 && leftnew < 3) {
534 err = got_error(GOT_ERR_PATCH_TRUNCATED);
537 if (line[linelen - 1] == '\n')
538 line[linelen - 1] = '\0';
540 /* usr.bin/patch allows '=' as context char */
545 if (ch == '\t' || ch == '\0')
546 ch = ' '; /* the space got eaten */
560 err = got_error(GOT_ERR_PATCH_MALFORMED);
564 if (leftold < 0 || leftnew < 0) {
565 err = got_error(GOT_ERR_PATCH_MALFORMED);
569 err = send_line(line);
573 if ((ch == '-' && leftold == 0) ||
574 (ch == '+' && leftnew == 0)) {
575 err = peek_special_line(fp);
586 static const struct got_error *
587 read_patch(struct imsgbuf *ibuf, int fd)
589 const struct got_error *err = NULL;
591 int git, patch_found = 0;
594 if ((fp = fdopen(fd, "r")) == NULL) {
595 err = got_error_from_errno("fdopen");
600 while ((err = patch_start(&git, &cid, fp)) == NULL) {
603 err = find_diff(&done, &next, fp, git, cid);
612 err = parse_hunk(fp, &done);
617 err = send_patch_done();
626 /* ignore trailing gibberish */
627 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
634 main(int argc, char **argv)
636 const struct got_error *err = NULL;
644 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
646 /* revoke access to most system calls */
647 if (pledge("stdio recvfd", NULL) == -1) {
648 err = got_error_from_errno("pledge");
649 got_privsep_send_error(&ibuf, err);
653 /* revoke fs access */
654 if (landlock_no_fs() == -1) {
655 err = got_error_from_errno("landlock_no_fs");
656 got_privsep_send_error(&ibuf, err);
659 if (cap_enter() == -1) {
660 err = got_error_from_errno("cap_enter");
661 got_privsep_send_error(&ibuf, err);
666 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
669 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
670 err = got_error(GOT_ERR_PRIVSEP_MSG);
674 err = read_patch(&ibuf, imsg.fd);
677 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
679 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
682 err = got_privsep_flush_imsg(&ibuf);
686 got_privsep_send_error(&ibuf, err);
689 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
690 err = got_error_from_errno("close");
691 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
692 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);