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>
54 #include "got_error.h"
55 #include "got_object.h"
57 #include "got_lib_delta.h"
58 #include "got_lib_object.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_hash.h"
64 static const struct got_error *
65 send_patch(const char *oldname, const char *newname, const char *commitid,
66 const char *blob, const int xbit, int git)
68 struct got_imsg_patch p;
70 memset(&p, 0, sizeof(p));
73 strlcpy(p.old, oldname, sizeof(p.old));
76 strlcpy(p.new, newname, sizeof(p.new));
79 strlcpy(p.cid, commitid, sizeof(p.cid));
82 strlcpy(p.blob, blob, sizeof(p.blob));
86 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
87 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
91 static const struct got_error *
94 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
96 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
97 return got_privsep_flush_imsg(&ibuf);
100 /* based on fetchname from usr.bin/patch/util.c */
101 static const struct got_error *
102 filename(const char *at, char **name)
110 while (isspace((unsigned char)*at))
113 /* files can be created or removed by diffing against /dev/null */
114 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
119 return got_error_from_errno("strdup");
120 if ((t = strchr(tmp, '\t')) != NULL)
122 if ((t = strchr(tmp, '\n')) != NULL)
128 return got_error_from_errno("strdup");
133 binary_deleted(const char *line)
135 const char *prefix = "Binary files ";
136 const char *suffix = " and /dev/null differ\n";
139 if (strncmp(line, prefix, strlen(prefix)) != 0)
141 line += strlen(prefix);
144 if (len <= strlen(suffix))
146 d = len - strlen(suffix);
147 return (strcmp(line + d, suffix) == 0);
150 static const struct got_error *
151 binaryfilename(const char *at, char **name)
153 const char *suffix = " and /dev/null differ\n";
159 if (len <= strlen(suffix))
162 d = len - strlen(suffix);
163 if (strcmp(at + d, suffix) != 0)
166 *name = strndup(at, d);
168 return got_error_from_errno("strndup");
173 filexbit(const char *line)
177 m = strchr(line, '(');
178 if (m && !strncmp(m + 1, "mode ", 5))
179 return strncmp(m + 6, "755", 3) == 0;
184 static const struct got_error *
185 blobid(const char *line, char **blob, int git)
187 uint8_t digest[SHA1_DIGEST_LENGTH];
192 len = strspn(line, "0123456789abcdefABCDEF");
193 if ((*blob = strndup(line, len)) == NULL)
194 return got_error_from_errno("strndup");
196 if (!git && !got_parse_hash_digest(digest, *blob, GOT_HASH_SHA1)) {
197 /* silently ignore invalid blob ids */
204 static const struct got_error *
205 patch_start(int *git, char **cid, FILE *fp)
207 const struct got_error *err = NULL;
214 while ((linelen = getline(&line, &linesize, fp)) != -1) {
215 if (!strncmp(line, "diff --git ", 11)) {
220 } else if (!strncmp(line, "diff ", 5)) {
224 } else if (!strncmp(line, "commit - ", 9)) {
226 err = blobid(line + 9, cid, *git);
229 } else if (!strncmp(line, "--- ", 4) ||
230 !strncmp(line, "+++ ", 4) ||
231 !strncmp(line, "blob - ", 7) ||
232 binary_deleted(line)) {
233 /* rewind to previous line */
234 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
235 err = got_error_from_errno("fseeko");
241 if (ferror(fp) && err == NULL)
242 err = got_error_from_errno("getline");
243 if (feof(fp) && err == NULL)
244 err = got_error(GOT_ERR_NO_PATCH);
248 static const struct got_error *
249 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
251 const struct got_error *err = NULL;
252 char *old = NULL, *new = NULL;
257 int create, delete_binary = 0, rename = 0, xbit = 0;
261 while ((linelen = getline(&line, &linesize, fp)) != -1) {
263 * Ignore the Index name like GNU and larry' patch,
264 * we don't have to follow POSIX.
267 if (!strncmp(line, "--- ", 4)) {
269 err = filename(line+4, &old);
270 } else if (rename && !strncmp(line, "rename from ", 12)) {
272 err = filename(line+12, &old);
273 } else if (!strncmp(line, "+++ ", 4)) {
275 err = filename(line+4, &new);
276 } else if (!strncmp(line, "blob + ", 7) ||
277 !strncmp(line, "file + ", 7)) {
278 xbit = filexbit(line);
279 } else if (!git && !strncmp(line, "blob - ", 7)) {
281 err = blobid(line + 7, &blob, git);
282 } else if (!strncmp(line, "Binary files ", 13)) {
285 err = binaryfilename(line + 13, &old);
286 } else if (rename && !strncmp(line, "rename to ", 10)) {
288 err = filename(line + 10, &new);
289 } else if (git && !strncmp(line, "similarity index 100%", 21))
291 else if (git && !strncmp(line, "new file mode 100", 17))
292 xbit = strncmp(line + 17, "755", 3) == 0;
293 else if (git && !strncmp(line, "index ", 6)) {
295 err = blobid(line + 6, &blob, git);
296 } else if (!strncmp(line, "diff ", 5)) {
297 /* rewind to previous line */
298 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
299 err = got_error_from_errno("fseeko");
308 * Git-style diffs with "similarity index 100%" don't
309 * have any hunks and ends with the "rename to foobar"
312 if (rename && old != NULL && new != NULL) {
314 err = send_patch(old, new, commitid,
320 * Diffs that remove binary files have no hunks.
322 if (delete_binary && old != NULL) {
324 err = send_patch(old, new, commitid,
329 if (!strncmp(line, "@@ -", 4)) {
330 create = !strncmp(line+4, "0,0", 3);
331 if ((old == NULL && new == NULL) ||
332 (!create && old == NULL))
333 err = got_error(GOT_ERR_PATCH_MALFORMED);
335 err = send_patch(old, new, commitid,
341 /* rewind to previous line */
342 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
343 err = got_error_from_errno("fseeko");
352 if (ferror(fp) && err == NULL)
353 err = got_error_from_errno("getline");
354 if (feof(fp) && err == NULL)
355 err = got_error(GOT_ERR_NO_PATCH);
359 static const struct got_error *
360 strtolnum(char **str, int *n)
365 for (p = *str; isdigit((unsigned char)*p); ++p)
371 *n = strtonum(*str, 0, INT_MAX, &errstr);
373 return got_error(GOT_ERR_PATCH_MALFORMED);
380 static const struct got_error *
381 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
383 static const struct got_error *err = NULL;
385 if (strncmp(s, "@@ -", 4)) {
393 err = strtolnum(&s, &hdr->oldfrom);
398 err = strtolnum(&s, &hdr->oldlines);
407 if (*s != '+' || !*++s)
408 return got_error(GOT_ERR_PATCH_MALFORMED);
409 err = strtolnum(&s, &hdr->newfrom);
414 err = strtolnum(&s, &hdr->newlines);
424 return got_error(GOT_ERR_PATCH_MALFORMED);
426 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
427 hdr->newfrom >= INT_MAX - hdr->newlines ||
428 /* not so sure about this one */
429 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
430 (hdr->oldlines == 0 && hdr->newlines == 0))
431 return got_error(GOT_ERR_PATCH_MALFORMED);
433 if (hdr->oldlines == 0) {
434 /* larry says to "do append rather than insert"; I don't
435 * quite get it, but i trust him.
440 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
441 hdr, sizeof(*hdr)) == -1)
442 return got_error_from_errno(
443 "imsg_compose GOT_IMSG_PATCH_HUNK");
447 static const struct got_error *
448 send_line(const char *line, size_t len)
450 static const struct got_error *err = NULL;
454 memset(&iov, 0, sizeof(iov));
456 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
457 iov[iovcnt].iov_base = (void *)" ";
458 iov[iovcnt].iov_len = 1;
462 iov[iovcnt].iov_base = (void *)line;
463 iov[iovcnt].iov_len = len;
466 if (imsg_composev(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
468 err = got_error_from_errno(
469 "imsg_compose GOT_IMSG_PATCH_LINE");
474 static const struct got_error *
475 peek_special_line(FILE *fp)
477 const struct got_error *err;
481 if (ch != EOF && ch != '\\') {
487 err = send_line("\\", 2);
492 while (ch != EOF && ch != '\n')
495 if (ch != EOF || feof(fp))
497 return got_error(GOT_ERR_IO);
500 static const struct got_error *
501 parse_hunk(FILE *fp, int *done)
503 static const struct got_error *err = NULL;
504 struct got_imsg_patch_hunk hdr;
505 char *line = NULL, ch;
508 int leftold, leftnew;
510 linelen = getline(&line, &linesize, fp);
516 err = parse_hdr(line, done, &hdr);
520 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
521 err = got_error_from_errno("fseeko");
525 leftold = hdr.oldlines;
526 leftnew = hdr.newlines;
528 while (leftold > 0 || leftnew > 0) {
529 linelen = getline(&line, &linesize, fp);
532 err = got_error_from_errno("getline");
536 /* trailing newlines may be chopped */
537 if (leftold < 3 && leftnew < 3) {
542 err = got_error(GOT_ERR_PATCH_TRUNCATED);
545 if (line[linelen - 1] == '\n')
546 line[linelen - 1] = '\0';
548 /* usr.bin/patch allows '=' as context char */
553 if (ch == '\t' || ch == '\0')
554 ch = ' '; /* the space got eaten */
568 err = got_error(GOT_ERR_PATCH_MALFORMED);
572 if (leftold < 0 || leftnew < 0) {
573 err = got_error(GOT_ERR_PATCH_MALFORMED);
577 err = send_line(line, linelen);
581 if ((ch == '-' && leftold == 0) ||
582 (ch == '+' && leftnew == 0)) {
583 err = peek_special_line(fp);
594 static const struct got_error *
595 read_patch(struct imsgbuf *ibuf, FILE *fp)
597 const struct got_error *err = NULL;
598 int git, patch_found = 0;
601 while ((err = patch_start(&git, &cid, fp)) == NULL) {
604 err = find_diff(&done, &next, fp, git, cid);
613 err = parse_hunk(fp, &done);
618 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;
646 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
648 /* revoke access to most system calls */
649 if (pledge("stdio recvfd", NULL) == -1) {
650 err = got_error_from_errno("pledge");
651 got_privsep_send_error(&ibuf, err);
656 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
659 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE) {
660 err = got_error(GOT_ERR_PRIVSEP_MSG);
663 fd = imsg_get_fd(&imsg);
665 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
669 fp = fdopen(fd, "r");
671 err = got_error_from_errno("fdopen");
676 err = read_patch(&ibuf, fp);
679 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
681 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
684 err = got_privsep_flush_imsg(&ibuf);
687 if (fd != -1 && close(fd) == -1 && err == NULL)
688 err = got_error_from_errno("close");
689 if (fp != NULL && fclose(fp) == EOF && err == NULL)
690 err = got_error_from_errno("fclose");
692 got_privsep_send_error(&ibuf, err);
695 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
696 err = got_error_from_errno("close");
697 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
698 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);