Blob


1 /*
2 * Copyright 1986, Larry Wall
3 *
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.
8 *
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
19 * SUCH DAMAGE.
20 */
22 /*
23 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
24 *
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.
28 *
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.
36 */
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/uio.h>
42 #include <ctype.h>
43 #include <limits.h>
44 #include <paths.h>
45 #include <sha1.h>
46 #include <sha2.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <imsg.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"
62 struct imsgbuf ibuf;
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)
67 {
68 struct got_imsg_patch p;
70 memset(&p, 0, sizeof(p));
72 if (oldname != NULL)
73 strlcpy(p.old, oldname, sizeof(p.old));
75 if (newname != NULL)
76 strlcpy(p.new, newname, sizeof(p.new));
78 if (commitid != NULL)
79 strlcpy(p.cid, commitid, sizeof(p.cid));
81 if (blob != NULL)
82 strlcpy(p.blob, blob, sizeof(p.blob));
84 p.xbit = xbit;
85 p.git = git;
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");
88 return NULL;
89 }
91 static const struct got_error *
92 send_patch_done(void)
93 {
94 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
95 NULL, 0) == -1)
96 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
97 return got_privsep_flush_imsg(&ibuf);
98 }
100 /* based on fetchname from usr.bin/patch/util.c */
101 static const struct got_error *
102 filename(const char *at, char **name)
104 char *tmp, *t;
106 *name = NULL;
107 if (*at == '\0')
108 return NULL;
110 while (isspace((unsigned char)*at))
111 at++;
113 /* files can be created or removed by diffing against /dev/null */
114 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
115 return NULL;
117 tmp = strdup(at);
118 if (tmp == NULL)
119 return got_error_from_errno("strdup");
120 if ((t = strchr(tmp, '\t')) != NULL)
121 *t = '\0';
122 if ((t = strchr(tmp, '\n')) != NULL)
123 *t = '\0';
125 *name = strdup(tmp);
126 free(tmp);
127 if (*name == NULL)
128 return got_error_from_errno("strdup");
129 return NULL;
132 static int
133 binary_deleted(const char *line)
135 const char *prefix = "Binary files ";
136 const char *suffix = " and /dev/null differ\n";
137 size_t len, d;
139 if (strncmp(line, prefix, strlen(prefix)) != 0)
140 return 0;
141 line += strlen(prefix);
143 len = strlen(line);
144 if (len <= strlen(suffix))
145 return 0;
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";
154 size_t len, d;
156 *name = NULL;
158 len = strlen(at);
159 if (len <= strlen(suffix))
160 return NULL;
162 d = len - strlen(suffix);
163 if (strcmp(at + d, suffix) != 0)
164 return NULL;
166 *name = strndup(at, d);
167 if (*name == NULL)
168 return got_error_from_errno("strndup");
169 return NULL;
172 static int
173 filexbit(const char *line)
175 char *m;
177 m = strchr(line, '(');
178 if (m && !strncmp(m + 1, "mode ", 5))
179 return strncmp(m + 6, "755", 3) == 0;
181 return 0;
184 static const struct got_error *
185 blobid(const char *line, char **blob, int git)
187 uint8_t digest[SHA1_DIGEST_LENGTH];
188 size_t len;
190 *blob = NULL;
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 */
198 free(*blob);
199 *blob = NULL;
201 return NULL;
204 static const struct got_error *
205 patch_start(int *git, char **cid, FILE *fp)
207 const struct got_error *err = NULL;
208 char *line = NULL;
209 size_t linesize = 0;
210 ssize_t linelen;
212 *git = 0;
214 while ((linelen = getline(&line, &linesize, fp)) != -1) {
215 if (!strncmp(line, "diff --git ", 11)) {
216 *git = 1;
217 free(*cid);
218 *cid = NULL;
219 break;
220 } else if (!strncmp(line, "diff ", 5)) {
221 *git = 0;
222 free(*cid);
223 *cid = NULL;
224 } else if (!strncmp(line, "commit - ", 9)) {
225 free(*cid);
226 err = blobid(line + 9, cid, *git);
227 if (err)
228 break;
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");
236 break;
240 free(line);
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);
245 return err;
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;
253 char *blob = NULL;
254 char *line = NULL;
255 size_t linesize = 0;
256 ssize_t linelen;
257 int create, delete_binary = 0, rename = 0, xbit = 0;
259 *done = 0;
260 *next = 0;
261 while ((linelen = getline(&line, &linesize, fp)) != -1) {
262 /*
263 * Ignore the Index name like GNU and larry' patch,
264 * we don't have to follow POSIX.
265 */
267 if (!strncmp(line, "--- ", 4)) {
268 free(old);
269 err = filename(line+4, &old);
270 } else if (rename && !strncmp(line, "rename from ", 12)) {
271 free(old);
272 err = filename(line+12, &old);
273 } else if (!strncmp(line, "+++ ", 4)) {
274 free(new);
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)) {
280 free(blob);
281 err = blobid(line + 7, &blob, git);
282 } else if (!strncmp(line, "Binary files ", 13)) {
283 delete_binary = 1;
284 free(old);
285 err = binaryfilename(line + 13, &old);
286 } else if (rename && !strncmp(line, "rename to ", 10)) {
287 free(new);
288 err = filename(line + 10, &new);
289 } else if (git && !strncmp(line, "similarity index 100%", 21))
290 rename = 1;
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)) {
294 free(blob);
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");
300 *next = 1;
301 break;
304 if (err)
305 break;
307 /*
308 * Git-style diffs with "similarity index 100%" don't
309 * have any hunks and ends with the "rename to foobar"
310 * line.
311 */
312 if (rename && old != NULL && new != NULL) {
313 *done = 1;
314 err = send_patch(old, new, commitid,
315 blob, xbit, git);
316 break;
319 /*
320 * Diffs that remove binary files have no hunks.
321 */
322 if (delete_binary && old != NULL) {
323 *done = 1;
324 err = send_patch(old, new, commitid,
325 blob, xbit, git);
326 break;
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);
334 else
335 err = send_patch(old, new, commitid,
336 blob, xbit, git);
338 if (err)
339 break;
341 /* rewind to previous line */
342 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
343 err = got_error_from_errno("fseeko");
344 break;
348 free(old);
349 free(new);
350 free(blob);
351 free(line);
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);
356 return err;
359 static const struct got_error *
360 strtolnum(char **str, int *n)
362 char *p, c;
363 const char *errstr;
365 for (p = *str; isdigit((unsigned char)*p); ++p)
366 /* nop */;
368 c = *p;
369 *p = '\0';
371 *n = strtonum(*str, 0, INT_MAX, &errstr);
372 if (errstr != NULL)
373 return got_error(GOT_ERR_PATCH_MALFORMED);
375 *p = c;
376 *str = p;
377 return NULL;
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)) {
386 *done = 1;
387 return NULL;
390 s += 4;
391 if (!*s)
392 return NULL;
393 err = strtolnum(&s, &hdr->oldfrom);
394 if (err)
395 return err;
396 if (*s == ',') {
397 s++;
398 err = strtolnum(&s, &hdr->oldlines);
399 if (err)
400 return err;
401 } else
402 hdr->oldlines = 1;
404 if (*s == ' ')
405 s++;
407 if (*s != '+' || !*++s)
408 return got_error(GOT_ERR_PATCH_MALFORMED);
409 err = strtolnum(&s, &hdr->newfrom);
410 if (err)
411 return err;
412 if (*s == ',') {
413 s++;
414 err = strtolnum(&s, &hdr->newlines);
415 if (err)
416 return err;
417 } else
418 hdr->newlines = 1;
420 if (*s == ' ')
421 s++;
423 if (*s != '@')
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.
436 */
437 hdr->oldfrom++;
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");
444 return NULL;
447 static const struct got_error *
448 send_line(const char *line, size_t len)
450 static const struct got_error *err = NULL;
451 struct iovec iov[2];
452 int iovcnt = 0;
454 memset(&iov, 0, sizeof(iov));
456 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
457 iov[iovcnt].iov_base = (void *)" ";
458 iov[iovcnt].iov_len = 1;
459 iovcnt++;
462 iov[iovcnt].iov_base = (void *)line;
463 iov[iovcnt].iov_len = len;
464 iovcnt++;
466 if (imsg_composev(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
467 iov, iovcnt) == -1)
468 err = got_error_from_errno(
469 "imsg_compose GOT_IMSG_PATCH_LINE");
471 return err;
474 static const struct got_error *
475 peek_special_line(FILE *fp)
477 const struct got_error *err;
478 int ch;
480 ch = fgetc(fp);
481 if (ch != EOF && ch != '\\') {
482 ungetc(ch, fp);
483 return NULL;
486 if (ch == '\\') {
487 err = send_line("\\", 2);
488 if (err)
489 return err;
492 while (ch != EOF && ch != '\n')
493 ch = fgetc(fp);
495 if (ch != EOF || feof(fp))
496 return NULL;
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;
506 size_t linesize = 0;
507 ssize_t linelen;
508 int leftold, leftnew;
510 linelen = getline(&line, &linesize, fp);
511 if (linelen == -1) {
512 *done = 1;
513 goto done;
516 err = parse_hdr(line, done, &hdr);
517 if (err)
518 goto done;
519 if (*done) {
520 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
521 err = got_error_from_errno("fseeko");
522 goto done;
525 leftold = hdr.oldlines;
526 leftnew = hdr.newlines;
528 while (leftold > 0 || leftnew > 0) {
529 linelen = getline(&line, &linesize, fp);
530 if (linelen == -1) {
531 if (ferror(fp)) {
532 err = got_error_from_errno("getline");
533 goto done;
536 /* trailing newlines may be chopped */
537 if (leftold < 3 && leftnew < 3) {
538 *done = 1;
539 break;
542 err = got_error(GOT_ERR_PATCH_TRUNCATED);
543 goto done;
545 if (line[linelen - 1] == '\n')
546 line[linelen - 1] = '\0';
548 /* usr.bin/patch allows '=' as context char */
549 if (*line == '=')
550 *line = ' ';
552 ch = *line;
553 if (ch == '\t' || ch == '\0')
554 ch = ' '; /* the space got eaten */
556 switch (ch) {
557 case '-':
558 leftold--;
559 break;
560 case ' ':
561 leftold--;
562 leftnew--;
563 break;
564 case '+':
565 leftnew--;
566 break;
567 default:
568 err = got_error(GOT_ERR_PATCH_MALFORMED);
569 goto done;
572 if (leftold < 0 || leftnew < 0) {
573 err = got_error(GOT_ERR_PATCH_MALFORMED);
574 goto done;
577 err = send_line(line, linelen);
578 if (err)
579 goto done;
581 if ((ch == '-' && leftold == 0) ||
582 (ch == '+' && leftnew == 0)) {
583 err = peek_special_line(fp);
584 if (err)
585 goto done;
589 done:
590 free(line);
591 return err;
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;
599 char *cid = NULL;
601 while ((err = patch_start(&git, &cid, fp)) == NULL) {
602 int done, next;
604 err = find_diff(&done, &next, fp, git, cid);
605 if (err)
606 goto done;
607 if (next)
608 continue;
610 patch_found = 1;
612 while (!done) {
613 err = parse_hunk(fp, &done);
614 if (err)
615 goto done;
618 err = send_patch_done();
619 if (err)
620 goto done;
623 done:
624 free(cid);
626 /* ignore trailing gibberish */
627 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
628 err = NULL;
630 return err;
633 int
634 main(int argc, char **argv)
636 const struct got_error *err = NULL;
637 struct imsg imsg;
638 FILE *fp = NULL;
639 int fd = -1;
640 #if 0
641 static int attached;
642 while (!attached)
643 sleep(1);
644 #endif
646 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
647 #ifndef PROFILE
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);
652 return 1;
654 #endif
656 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
657 if (err)
658 goto done;
659 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE) {
660 err = got_error(GOT_ERR_PRIVSEP_MSG);
661 goto done;
663 fd = imsg_get_fd(&imsg);
664 if (fd == -1) {
665 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
666 goto done;
669 fp = fdopen(fd, "r");
670 if (fp == NULL) {
671 err = got_error_from_errno("fdopen");
672 goto done;
674 fd = -1;
676 err = read_patch(&ibuf, fp);
677 if (err)
678 goto done;
679 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
680 NULL, 0) == -1) {
681 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
682 goto done;
684 err = got_privsep_flush_imsg(&ibuf);
685 imsg_free(&imsg);
686 done:
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");
691 if (err != NULL) {
692 got_privsep_send_error(&ibuf, err);
693 err = NULL;
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);
699 return err ? 1 : 0;