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 <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <imsg.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"
61 struct imsgbuf ibuf;
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)
66 {
67 struct got_imsg_patch p;
69 memset(&p, 0, sizeof(p));
71 if (oldname != NULL)
72 strlcpy(p.old, oldname, sizeof(p.old));
74 if (newname != NULL)
75 strlcpy(p.new, newname, sizeof(p.new));
77 if (commitid != NULL)
78 strlcpy(p.cid, commitid, sizeof(p.cid));
80 if (blob != NULL)
81 strlcpy(p.blob, blob, sizeof(p.blob));
83 p.xbit = xbit;
84 p.git = git;
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");
87 return NULL;
88 }
90 static const struct got_error *
91 send_patch_done(void)
92 {
93 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
94 NULL, 0) == -1)
95 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
96 return got_privsep_flush_imsg(&ibuf);
97 }
99 /* based on fetchname from usr.bin/patch/util.c */
100 static const struct got_error *
101 filename(const char *at, char **name)
103 char *tmp, *t;
105 *name = NULL;
106 if (*at == '\0')
107 return NULL;
109 while (isspace((unsigned char)*at))
110 at++;
112 /* files can be created or removed by diffing against /dev/null */
113 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
114 return NULL;
116 tmp = strdup(at);
117 if (tmp == NULL)
118 return got_error_from_errno("strdup");
119 if ((t = strchr(tmp, '\t')) != NULL)
120 *t = '\0';
121 if ((t = strchr(tmp, '\n')) != NULL)
122 *t = '\0';
124 *name = strdup(tmp);
125 free(tmp);
126 if (*name == NULL)
127 return got_error_from_errno("strdup");
128 return NULL;
131 static int
132 binary_deleted(const char *line)
134 const char *prefix = "Binary files ";
135 const char *suffix = " and /dev/null differ\n";
136 size_t len, d;
138 if (strncmp(line, prefix, strlen(prefix)) != 0)
139 return 0;
140 line += strlen(prefix);
142 len = strlen(line);
143 if (len <= strlen(suffix))
144 return 0;
145 d = len - strlen(suffix);
146 return (strcmp(line + d, suffix) == 0);
149 static const struct got_error *
150 binaryfilename(const char *at, char **name)
152 const char *suffix = " and /dev/null differ\n";
153 size_t len, d;
155 *name = NULL;
157 len = strlen(at);
158 if (len <= strlen(suffix))
159 return NULL;
161 d = len - strlen(suffix);
162 if (strcmp(at + d, suffix) != 0)
163 return NULL;
165 *name = strndup(at, d);
166 if (*name == NULL)
167 return got_error_from_errno("strndup");
168 return NULL;
171 static int
172 filexbit(const char *line)
174 char *m;
176 m = strchr(line, '(');
177 if (m && !strncmp(m + 1, "mode ", 5))
178 return strncmp(m + 6, "755", 3) == 0;
180 return 0;
183 static const struct got_error *
184 blobid(const char *line, char **blob, int git)
186 uint8_t digest[SHA1_DIGEST_LENGTH];
187 size_t len;
189 *blob = NULL;
191 len = strspn(line, "0123456789abcdefABCDEF");
192 if ((*blob = strndup(line, len)) == NULL)
193 return got_error_from_errno("strndup");
195 if (!git && !got_parse_sha1_digest(digest, *blob)) {
196 /* silently ignore invalid blob ids */
197 free(*blob);
198 *blob = NULL;
200 return NULL;
203 static const struct got_error *
204 patch_start(int *git, char **cid, FILE *fp)
206 const struct got_error *err = NULL;
207 char *line = NULL;
208 size_t linesize = 0;
209 ssize_t linelen;
211 *git = 0;
213 while ((linelen = getline(&line, &linesize, fp)) != -1) {
214 if (!strncmp(line, "diff --git ", 11)) {
215 *git = 1;
216 free(*cid);
217 *cid = NULL;
218 break;
219 } else if (!strncmp(line, "diff ", 5)) {
220 *git = 0;
221 free(*cid);
222 *cid = NULL;
223 } else if (!strncmp(line, "commit - ", 9)) {
224 free(*cid);
225 err = blobid(line + 9, cid, *git);
226 if (err)
227 break;
228 } else if (!strncmp(line, "--- ", 4) ||
229 !strncmp(line, "+++ ", 4) ||
230 !strncmp(line, "blob - ", 7) ||
231 binary_deleted(line)) {
232 /* rewind to previous line */
233 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
234 err = got_error_from_errno("fseeko");
235 break;
239 free(line);
240 if (ferror(fp) && err == NULL)
241 err = got_error_from_errno("getline");
242 if (feof(fp) && err == NULL)
243 err = got_error(GOT_ERR_NO_PATCH);
244 return err;
247 static const struct got_error *
248 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
250 const struct got_error *err = NULL;
251 char *old = NULL, *new = NULL;
252 char *blob = NULL;
253 char *line = NULL;
254 size_t linesize = 0;
255 ssize_t linelen;
256 int create, delete_binary = 0, rename = 0, xbit = 0;
258 *done = 0;
259 *next = 0;
260 while ((linelen = getline(&line, &linesize, fp)) != -1) {
261 /*
262 * Ignore the Index name like GNU and larry' patch,
263 * we don't have to follow POSIX.
264 */
266 if (!strncmp(line, "--- ", 4)) {
267 free(old);
268 err = filename(line+4, &old);
269 } else if (rename && !strncmp(line, "rename from ", 12)) {
270 free(old);
271 err = filename(line+12, &old);
272 } else if (!strncmp(line, "+++ ", 4)) {
273 free(new);
274 err = filename(line+4, &new);
275 } else if (!strncmp(line, "blob + ", 7) ||
276 !strncmp(line, "file + ", 7)) {
277 xbit = filexbit(line);
278 } else if (!git && !strncmp(line, "blob - ", 7)) {
279 free(blob);
280 err = blobid(line + 7, &blob, git);
281 } else if (!strncmp(line, "Binary files ", 13)) {
282 delete_binary = 1;
283 free(old);
284 err = binaryfilename(line + 13, &old);
285 } else if (rename && !strncmp(line, "rename to ", 10)) {
286 free(new);
287 err = filename(line + 10, &new);
288 } else if (git && !strncmp(line, "similarity index 100%", 21))
289 rename = 1;
290 else if (git && !strncmp(line, "new file mode 100", 17))
291 xbit = strncmp(line + 17, "755", 3) == 0;
292 else if (git && !strncmp(line, "index ", 6)) {
293 free(blob);
294 err = blobid(line + 6, &blob, git);
295 } else if (!strncmp(line, "diff ", 5)) {
296 /* rewind to previous line */
297 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
298 err = got_error_from_errno("fseeko");
299 *next = 1;
300 break;
303 if (err)
304 break;
306 /*
307 * Git-style diffs with "similarity index 100%" don't
308 * have any hunks and ends with the "rename to foobar"
309 * line.
310 */
311 if (rename && old != NULL && new != NULL) {
312 *done = 1;
313 err = send_patch(old, new, commitid,
314 blob, xbit, git);
315 break;
318 /*
319 * Diffs that remove binary files have no hunks.
320 */
321 if (delete_binary && old != NULL) {
322 *done = 1;
323 err = send_patch(old, new, commitid,
324 blob, xbit, git);
325 break;
328 if (!strncmp(line, "@@ -", 4)) {
329 create = !strncmp(line+4, "0,0", 3);
330 if ((old == NULL && new == NULL) ||
331 (!create && old == NULL))
332 err = got_error(GOT_ERR_PATCH_MALFORMED);
333 else
334 err = send_patch(old, new, commitid,
335 blob, xbit, git);
337 if (err)
338 break;
340 /* rewind to previous line */
341 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
342 err = got_error_from_errno("fseeko");
343 break;
347 free(old);
348 free(new);
349 free(blob);
350 free(line);
351 if (ferror(fp) && err == NULL)
352 err = got_error_from_errno("getline");
353 if (feof(fp) && err == NULL)
354 err = got_error(GOT_ERR_NO_PATCH);
355 return err;
358 static const struct got_error *
359 strtolnum(char **str, int *n)
361 char *p, c;
362 const char *errstr;
364 for (p = *str; isdigit((unsigned char)*p); ++p)
365 /* nop */;
367 c = *p;
368 *p = '\0';
370 *n = strtonum(*str, 0, INT_MAX, &errstr);
371 if (errstr != NULL)
372 return got_error(GOT_ERR_PATCH_MALFORMED);
374 *p = c;
375 *str = p;
376 return NULL;
379 static const struct got_error *
380 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
382 static const struct got_error *err = NULL;
384 if (strncmp(s, "@@ -", 4)) {
385 *done = 1;
386 return NULL;
389 s += 4;
390 if (!*s)
391 return NULL;
392 err = strtolnum(&s, &hdr->oldfrom);
393 if (err)
394 return err;
395 if (*s == ',') {
396 s++;
397 err = strtolnum(&s, &hdr->oldlines);
398 if (err)
399 return err;
400 } else
401 hdr->oldlines = 1;
403 if (*s == ' ')
404 s++;
406 if (*s != '+' || !*++s)
407 return got_error(GOT_ERR_PATCH_MALFORMED);
408 err = strtolnum(&s, &hdr->newfrom);
409 if (err)
410 return err;
411 if (*s == ',') {
412 s++;
413 err = strtolnum(&s, &hdr->newlines);
414 if (err)
415 return err;
416 } else
417 hdr->newlines = 1;
419 if (*s == ' ')
420 s++;
422 if (*s != '@')
423 return got_error(GOT_ERR_PATCH_MALFORMED);
425 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
426 hdr->newfrom >= INT_MAX - hdr->newlines ||
427 /* not so sure about this one */
428 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
429 (hdr->oldlines == 0 && hdr->newlines == 0))
430 return got_error(GOT_ERR_PATCH_MALFORMED);
432 if (hdr->oldlines == 0) {
433 /* larry says to "do append rather than insert"; I don't
434 * quite get it, but i trust him.
435 */
436 hdr->oldfrom++;
439 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
440 hdr, sizeof(*hdr)) == -1)
441 return got_error_from_errno(
442 "imsg_compose GOT_IMSG_PATCH_HUNK");
443 return NULL;
446 static const struct got_error *
447 send_line(const char *line)
449 static const struct got_error *err = NULL;
450 char *p = NULL;
452 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
453 if (asprintf(&p, " %s", line) == -1)
454 return got_error_from_errno("asprintf");
455 line = p;
458 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
459 line, strlen(line) + 1) == -1)
460 err = got_error_from_errno(
461 "imsg_compose GOT_IMSG_PATCH_LINE");
463 free(p);
464 return err;
467 static const struct got_error *
468 peek_special_line(FILE *fp)
470 const struct got_error *err;
471 int ch;
473 ch = fgetc(fp);
474 if (ch != EOF && ch != '\\') {
475 ungetc(ch, fp);
476 return NULL;
479 if (ch == '\\') {
480 err = send_line("\\");
481 if (err)
482 return err;
485 while (ch != EOF && ch != '\n')
486 ch = fgetc(fp);
488 if (ch != EOF || feof(fp))
489 return NULL;
490 return got_error(GOT_ERR_IO);
493 static const struct got_error *
494 parse_hunk(FILE *fp, int *done)
496 static const struct got_error *err = NULL;
497 struct got_imsg_patch_hunk hdr;
498 char *line = NULL, ch;
499 size_t linesize = 0;
500 ssize_t linelen;
501 int leftold, leftnew;
503 linelen = getline(&line, &linesize, fp);
504 if (linelen == -1) {
505 *done = 1;
506 goto done;
509 err = parse_hdr(line, done, &hdr);
510 if (err)
511 goto done;
512 if (*done) {
513 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
514 err = got_error_from_errno("fseeko");
515 goto done;
518 leftold = hdr.oldlines;
519 leftnew = hdr.newlines;
521 while (leftold > 0 || leftnew > 0) {
522 linelen = getline(&line, &linesize, fp);
523 if (linelen == -1) {
524 if (ferror(fp)) {
525 err = got_error_from_errno("getline");
526 goto done;
529 /* trailing newlines may be chopped */
530 if (leftold < 3 && leftnew < 3) {
531 *done = 1;
532 break;
535 err = got_error(GOT_ERR_PATCH_TRUNCATED);
536 goto done;
538 if (line[linelen - 1] == '\n')
539 line[linelen - 1] = '\0';
541 /* usr.bin/patch allows '=' as context char */
542 if (*line == '=')
543 *line = ' ';
545 ch = *line;
546 if (ch == '\t' || ch == '\0')
547 ch = ' '; /* the space got eaten */
549 switch (ch) {
550 case '-':
551 leftold--;
552 break;
553 case ' ':
554 leftold--;
555 leftnew--;
556 break;
557 case '+':
558 leftnew--;
559 break;
560 default:
561 err = got_error(GOT_ERR_PATCH_MALFORMED);
562 goto done;
565 if (leftold < 0 || leftnew < 0) {
566 err = got_error(GOT_ERR_PATCH_MALFORMED);
567 goto done;
570 err = send_line(line);
571 if (err)
572 goto done;
574 if ((ch == '-' && leftold == 0) ||
575 (ch == '+' && leftnew == 0)) {
576 err = peek_special_line(fp);
577 if (err)
578 goto done;
582 done:
583 free(line);
584 return err;
587 static const struct got_error *
588 read_patch(struct imsgbuf *ibuf, int fd)
590 const struct got_error *err = NULL;
591 FILE *fp;
592 int git, patch_found = 0;
593 char *cid = NULL;
595 if ((fp = fdopen(fd, "r")) == NULL) {
596 err = got_error_from_errno("fdopen");
597 close(fd);
598 return err;
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 fclose(fp);
625 free(cid);
627 /* ignore trailing gibberish */
628 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
629 err = NULL;
631 return err;
634 int
635 main(int argc, char **argv)
637 const struct got_error *err = NULL;
638 struct imsg imsg;
639 #if 0
640 static int attached;
641 while (!attached)
642 sleep(1);
643 #endif
645 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
646 #ifndef PROFILE
647 /* revoke access to most system calls */
648 if (pledge("stdio recvfd", NULL) == -1) {
649 err = got_error_from_errno("pledge");
650 got_privsep_send_error(&ibuf, err);
651 return 1;
653 #endif
655 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
656 if (err)
657 goto done;
658 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
659 err = got_error(GOT_ERR_PRIVSEP_MSG);
660 goto done;
663 err = read_patch(&ibuf, imsg.fd);
664 if (err)
665 goto done;
666 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
667 NULL, 0) == -1) {
668 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
669 goto done;
671 err = got_privsep_flush_imsg(&ibuf);
672 done:
673 imsg_free(&imsg);
674 if (err != NULL) {
675 got_privsep_send_error(&ibuf, err);
676 err = NULL;
678 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
679 err = got_error_from_errno("close");
680 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
681 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
682 return err ? 1 : 0;