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_lib_delta.h"
57 #include "got_lib_object.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_sha1.h"
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));
72 strlcpy(p.old, oldname, sizeof(p.old));
75 strlcpy(p.new, newname, sizeof(p.new));
78 strlcpy(p.cid, commitid, sizeof(p.cid));
81 strlcpy(p.blob, blob, sizeof(p.blob));
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");
90 static const struct got_error *
93 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 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)
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");
132 filexbit(const char *line)
136 m = strchr(line, '(');
137 if (m && !strncmp(m + 1, "mode ", 5))
138 return strncmp(m + 6, "755", 3) == 0;
143 static const struct got_error *
144 blobid(const char *line, char **blob, int git)
146 uint8_t digest[SHA1_DIGEST_LENGTH];
151 len = strspn(line, "0123456789abcdefABCDEF");
152 if ((*blob = strndup(line, len)) == NULL)
153 return got_error_from_errno("strndup");
155 if (!git && !got_parse_sha1_digest(digest, *blob)) {
156 /* silently ignore invalid blob ids */
163 static const struct got_error *
164 patch_start(int *git, char **cid, FILE *fp)
166 const struct got_error *err = NULL;
173 while ((linelen = getline(&line, &linesize, fp)) != -1) {
174 if (!strncmp(line, "diff --git ", 11)) {
179 } else if (!strncmp(line, "diff ", 5)) {
183 } else if (!strncmp(line, "commit - ", 9)) {
185 err = blobid(line + 9, cid, *git);
188 } else if (!strncmp(line, "--- ", 4) ||
189 !strncmp(line, "+++ ", 4) ||
190 !strncmp(line, "blob - ", 7)) {
191 /* rewind to previous line */
192 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
193 err = got_error_from_errno("fseeko");
199 if (ferror(fp) && err == NULL)
200 err = got_error_from_errno("getline");
201 if (feof(fp) && err == NULL)
202 err = got_error(GOT_ERR_NO_PATCH);
206 static const struct got_error *
207 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
209 const struct got_error *err = NULL;
210 char *old = NULL, *new = NULL;
215 int create, rename = 0, xbit = 0;
219 while ((linelen = getline(&line, &linesize, fp)) != -1) {
221 * Ignore the Index name like GNU and larry' patch,
222 * we don't have to follow POSIX.
225 if (!strncmp(line, "--- ", 4)) {
227 err = filename(line+4, &old);
228 } else if (rename && !strncmp(line, "rename from ", 12)) {
230 err = filename(line+12, &old);
231 } else if (!strncmp(line, "+++ ", 4)) {
233 err = filename(line+4, &new);
234 } else if (!strncmp(line, "blob + ", 7) ||
235 !strncmp(line, "file + ", 7)) {
236 xbit = filexbit(line);
237 } else if (!git && !strncmp(line, "blob - ", 7)) {
239 err = blobid(line + 7, &blob, git);
240 } else if (rename && !strncmp(line, "rename to ", 10)) {
242 err = filename(line + 10, &new);
243 } else if (git && !strncmp(line, "similarity index 100%", 21))
245 else if (git && !strncmp(line, "new file mode 100", 17))
246 xbit = strncmp(line + 17, "755", 3) == 0;
247 else if (git && !strncmp(line, "index ", 6)) {
249 err = blobid(line + 6, &blob, git);
250 } else if (!strncmp(line, "diff ", 5)) {
251 /* rewind to previous line */
252 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
253 err = got_error_from_errno("fseeko");
262 * Git-style diffs with "similarity index 100%" don't
263 * have any hunks and ends with the "rename to foobar"
266 if (rename && old != NULL && new != NULL) {
268 err = send_patch(old, new, commitid,
273 if (!strncmp(line, "@@ -", 4)) {
274 create = !strncmp(line+4, "0,0", 3);
275 if ((old == NULL && new == NULL) ||
276 (!create && old == NULL))
277 err = got_error(GOT_ERR_PATCH_MALFORMED);
279 err = send_patch(old, new, commitid,
285 /* rewind to previous line */
286 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
287 err = got_error_from_errno("fseeko");
296 if (ferror(fp) && err == NULL)
297 err = got_error_from_errno("getline");
298 if (feof(fp) && err == NULL)
299 err = got_error(GOT_ERR_NO_PATCH);
303 static const struct got_error *
304 strtolnum(char **str, int *n)
309 for (p = *str; isdigit((unsigned char)*p); ++p)
315 *n = strtonum(*str, 0, INT_MAX, &errstr);
317 return got_error(GOT_ERR_PATCH_MALFORMED);
324 static const struct got_error *
325 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
327 static const struct got_error *err = NULL;
329 if (strncmp(s, "@@ -", 4)) {
337 err = strtolnum(&s, &hdr->oldfrom);
342 err = strtolnum(&s, &hdr->oldlines);
351 if (*s != '+' || !*++s)
352 return got_error(GOT_ERR_PATCH_MALFORMED);
353 err = strtolnum(&s, &hdr->newfrom);
358 err = strtolnum(&s, &hdr->newlines);
368 return got_error(GOT_ERR_PATCH_MALFORMED);
370 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
371 hdr->newfrom >= INT_MAX - hdr->newlines ||
372 /* not so sure about this one */
373 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
374 (hdr->oldlines == 0 && hdr->newlines == 0))
375 return got_error(GOT_ERR_PATCH_MALFORMED);
377 if (hdr->oldlines == 0) {
378 /* larry says to "do append rather than insert"; I don't
379 * quite get it, but i trust him.
384 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
385 hdr, sizeof(*hdr)) == -1)
386 return got_error_from_errno(
387 "imsg_compose GOT_IMSG_PATCH_HUNK");
391 static const struct got_error *
392 send_line(const char *line)
394 static const struct got_error *err = NULL;
397 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
398 if (asprintf(&p, " %s", line) == -1)
399 return got_error_from_errno("asprintf");
403 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
404 line, strlen(line) + 1) == -1)
405 err = got_error_from_errno(
406 "imsg_compose GOT_IMSG_PATCH_LINE");
412 static const struct got_error *
413 peek_special_line(FILE *fp)
415 const struct got_error *err;
419 if (ch != EOF && ch != '\\') {
425 err = send_line("\\");
430 while (ch != EOF && ch != '\n')
433 if (ch != EOF || feof(fp))
435 return got_error(GOT_ERR_IO);
438 static const struct got_error *
439 parse_hunk(FILE *fp, int *done)
441 static const struct got_error *err = NULL;
442 struct got_imsg_patch_hunk hdr;
443 char *line = NULL, ch;
446 int leftold, leftnew;
448 linelen = getline(&line, &linesize, fp);
454 err = parse_hdr(line, done, &hdr);
458 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
459 err = got_error_from_errno("fseeko");
463 leftold = hdr.oldlines;
464 leftnew = hdr.newlines;
466 while (leftold > 0 || leftnew > 0) {
467 linelen = getline(&line, &linesize, fp);
470 err = got_error_from_errno("getline");
474 /* trailing newlines may be chopped */
475 if (leftold < 3 && leftnew < 3) {
480 err = got_error(GOT_ERR_PATCH_TRUNCATED);
483 if (line[linelen - 1] == '\n')
484 line[linelen - 1] = '\0';
486 /* usr.bin/patch allows '=' as context char */
491 if (ch == '\t' || ch == '\0')
492 ch = ' '; /* the space got eaten */
506 err = got_error(GOT_ERR_PATCH_MALFORMED);
510 if (leftold < 0 || leftnew < 0) {
511 err = got_error(GOT_ERR_PATCH_MALFORMED);
515 err = send_line(line);
519 if ((ch == '-' && leftold == 0) ||
520 (ch == '+' && leftnew == 0)) {
521 err = peek_special_line(fp);
532 static const struct got_error *
533 read_patch(struct imsgbuf *ibuf, int fd)
535 const struct got_error *err = NULL;
537 int git, patch_found = 0;
540 if ((fp = fdopen(fd, "r")) == NULL) {
541 err = got_error_from_errno("fdopen");
546 while ((err = patch_start(&git, &cid, fp)) == NULL) {
549 err = find_diff(&done, &next, fp, git, cid);
558 err = parse_hunk(fp, &done);
563 err = send_patch_done();
572 /* ignore trailing gibberish */
573 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
580 main(int argc, char **argv)
582 const struct got_error *err = NULL;
590 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
592 /* revoke access to most system calls */
593 if (pledge("stdio recvfd", NULL) == -1) {
594 err = got_error_from_errno("pledge");
595 got_privsep_send_error(&ibuf, err);
600 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
603 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
604 err = got_error(GOT_ERR_PRIVSEP_MSG);
608 err = read_patch(&ibuf, imsg.fd);
611 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
613 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
616 err = got_privsep_flush_imsg(&ibuf);
620 got_privsep_send_error(&ibuf, err);
623 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
624 err = got_error_from_errno("close");
625 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
626 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);