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 */
37 #include "got_compat.h"
39 #include <sys/types.h>
40 #include <sys/queue.h>
41 #include <sys/uio.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <paths.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.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"
60 struct imsgbuf ibuf;
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)
65 {
66 struct got_imsg_patch p;
68 memset(&p, 0, sizeof(p));
70 if (oldname != NULL)
71 strlcpy(p.old, oldname, sizeof(p.old));
73 if (newname != NULL)
74 strlcpy(p.new, newname, sizeof(p.new));
76 if (commitid != NULL)
77 strlcpy(p.cid, commitid, sizeof(p.cid));
79 if (blob != NULL)
80 strlcpy(p.blob, blob, sizeof(p.blob));
82 p.xbit = xbit;
83 p.git = git;
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");
86 return NULL;
87 }
89 static const struct got_error *
90 send_patch_done(void)
91 {
92 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
93 NULL, 0) == -1)
94 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
95 return got_privsep_flush_imsg(&ibuf);
96 }
98 /* based on fetchname from usr.bin/patch/util.c */
99 static const struct got_error *
100 filename(const char *at, char **name)
102 char *tmp, *t;
104 *name = NULL;
105 if (*at == '\0')
106 return NULL;
108 while (isspace((unsigned char)*at))
109 at++;
111 /* files can be created or removed by diffing against /dev/null */
112 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
113 return NULL;
115 tmp = strdup(at);
116 if (tmp == NULL)
117 return got_error_from_errno("strdup");
118 if ((t = strchr(tmp, '\t')) != NULL)
119 *t = '\0';
120 if ((t = strchr(tmp, '\n')) != NULL)
121 *t = '\0';
123 *name = strdup(tmp);
124 free(tmp);
125 if (*name == NULL)
126 return got_error_from_errno("strdup");
127 return NULL;
130 static int
131 binary_deleted(const char *line)
133 const char *prefix = "Binary files ";
134 const char *suffix = " and /dev/null differ\n";
135 size_t len, d;
137 if (strncmp(line, prefix, strlen(prefix)) != 0)
138 return 0;
139 line += strlen(prefix);
141 len = strlen(line);
142 if (len <= strlen(suffix))
143 return 0;
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";
152 size_t len, d;
154 *name = NULL;
156 len = strlen(at);
157 if (len <= strlen(suffix))
158 return NULL;
160 d = len - strlen(suffix);
161 if (strcmp(at + d, suffix) != 0)
162 return NULL;
164 *name = strndup(at, d);
165 if (*name == NULL)
166 return got_error_from_errno("strndup");
167 return NULL;
170 static int
171 filexbit(const char *line)
173 char *m;
175 m = strchr(line, '(');
176 if (m && !strncmp(m + 1, "mode ", 5))
177 return strncmp(m + 6, "755", 3) == 0;
179 return 0;
182 static const struct got_error *
183 blobid(const char *line, char **blob, int git)
185 uint8_t digest[SHA1_DIGEST_LENGTH];
186 size_t len;
188 *blob = NULL;
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 */
196 free(*blob);
197 *blob = NULL;
199 return NULL;
202 static const struct got_error *
203 patch_start(int *git, char **cid, FILE *fp)
205 const struct got_error *err = NULL;
206 char *line = NULL;
207 size_t linesize = 0;
208 ssize_t linelen;
210 *git = 0;
212 while ((linelen = getline(&line, &linesize, fp)) != -1) {
213 if (!strncmp(line, "diff --git ", 11)) {
214 *git = 1;
215 free(*cid);
216 *cid = NULL;
217 break;
218 } else if (!strncmp(line, "diff ", 5)) {
219 *git = 0;
220 free(*cid);
221 *cid = NULL;
222 } else if (!strncmp(line, "commit - ", 9)) {
223 free(*cid);
224 err = blobid(line + 9, cid, *git);
225 if (err)
226 break;
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");
234 break;
238 free(line);
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);
243 return err;
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;
251 char *blob = NULL;
252 char *line = NULL;
253 size_t linesize = 0;
254 ssize_t linelen;
255 int create, delete_binary = 0, rename = 0, xbit = 0;
257 *done = 0;
258 *next = 0;
259 while ((linelen = getline(&line, &linesize, fp)) != -1) {
260 /*
261 * Ignore the Index name like GNU and larry' patch,
262 * we don't have to follow POSIX.
263 */
265 if (!strncmp(line, "--- ", 4)) {
266 free(old);
267 err = filename(line+4, &old);
268 } else if (rename && !strncmp(line, "rename from ", 12)) {
269 free(old);
270 err = filename(line+12, &old);
271 } else if (!strncmp(line, "+++ ", 4)) {
272 free(new);
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)) {
278 free(blob);
279 err = blobid(line + 7, &blob, git);
280 } else if (!strncmp(line, "Binary files ", 13)) {
281 delete_binary = 1;
282 free(old);
283 err = binaryfilename(line + 13, &old);
284 } else if (rename && !strncmp(line, "rename to ", 10)) {
285 free(new);
286 err = filename(line + 10, &new);
287 } else if (git && !strncmp(line, "similarity index 100%", 21))
288 rename = 1;
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)) {
292 free(blob);
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");
298 *next = 1;
299 break;
302 if (err)
303 break;
305 /*
306 * Git-style diffs with "similarity index 100%" don't
307 * have any hunks and ends with the "rename to foobar"
308 * line.
309 */
310 if (rename && old != NULL && new != NULL) {
311 *done = 1;
312 err = send_patch(old, new, commitid,
313 blob, xbit, git);
314 break;
317 /*
318 * Diffs that remove binary files have no hunks.
319 */
320 if (delete_binary && old != NULL) {
321 *done = 1;
322 err = send_patch(old, new, commitid,
323 blob, xbit, git);
324 break;
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);
332 else
333 err = send_patch(old, new, commitid,
334 blob, xbit, git);
336 if (err)
337 break;
339 /* rewind to previous line */
340 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
341 err = got_error_from_errno("fseeko");
342 break;
346 free(old);
347 free(new);
348 free(blob);
349 free(line);
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);
354 return err;
357 static const struct got_error *
358 strtolnum(char **str, int *n)
360 char *p, c;
361 const char *errstr;
363 for (p = *str; isdigit((unsigned char)*p); ++p)
364 /* nop */;
366 c = *p;
367 *p = '\0';
369 *n = strtonum(*str, 0, INT_MAX, &errstr);
370 if (errstr != NULL)
371 return got_error(GOT_ERR_PATCH_MALFORMED);
373 *p = c;
374 *str = p;
375 return NULL;
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)) {
384 *done = 1;
385 return NULL;
388 s += 4;
389 if (!*s)
390 return NULL;
391 err = strtolnum(&s, &hdr->oldfrom);
392 if (err)
393 return err;
394 if (*s == ',') {
395 s++;
396 err = strtolnum(&s, &hdr->oldlines);
397 if (err)
398 return err;
399 } else
400 hdr->oldlines = 1;
402 if (*s == ' ')
403 s++;
405 if (*s != '+' || !*++s)
406 return got_error(GOT_ERR_PATCH_MALFORMED);
407 err = strtolnum(&s, &hdr->newfrom);
408 if (err)
409 return err;
410 if (*s == ',') {
411 s++;
412 err = strtolnum(&s, &hdr->newlines);
413 if (err)
414 return err;
415 } else
416 hdr->newlines = 1;
418 if (*s == ' ')
419 s++;
421 if (*s != '@')
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.
434 */
435 hdr->oldfrom++;
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");
442 return NULL;
445 static const struct got_error *
446 send_line(const char *line)
448 static const struct got_error *err = NULL;
449 char *p = NULL;
451 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
452 if (asprintf(&p, " %s", line) == -1)
453 return got_error_from_errno("asprintf");
454 line = p;
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");
462 free(p);
463 return err;
466 static const struct got_error *
467 peek_special_line(FILE *fp)
469 const struct got_error *err;
470 int ch;
472 ch = fgetc(fp);
473 if (ch != EOF && ch != '\\') {
474 ungetc(ch, fp);
475 return NULL;
478 if (ch == '\\') {
479 err = send_line("\\");
480 if (err)
481 return err;
484 while (ch != EOF && ch != '\n')
485 ch = fgetc(fp);
487 if (ch != EOF || feof(fp))
488 return NULL;
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;
498 size_t linesize = 0;
499 ssize_t linelen;
500 int leftold, leftnew;
502 linelen = getline(&line, &linesize, fp);
503 if (linelen == -1) {
504 *done = 1;
505 goto done;
508 err = parse_hdr(line, done, &hdr);
509 if (err)
510 goto done;
511 if (*done) {
512 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
513 err = got_error_from_errno("fseeko");
514 goto done;
517 leftold = hdr.oldlines;
518 leftnew = hdr.newlines;
520 while (leftold > 0 || leftnew > 0) {
521 linelen = getline(&line, &linesize, fp);
522 if (linelen == -1) {
523 if (ferror(fp)) {
524 err = got_error_from_errno("getline");
525 goto done;
528 /* trailing newlines may be chopped */
529 if (leftold < 3 && leftnew < 3) {
530 *done = 1;
531 break;
534 err = got_error(GOT_ERR_PATCH_TRUNCATED);
535 goto done;
537 if (line[linelen - 1] == '\n')
538 line[linelen - 1] = '\0';
540 /* usr.bin/patch allows '=' as context char */
541 if (*line == '=')
542 *line = ' ';
544 ch = *line;
545 if (ch == '\t' || ch == '\0')
546 ch = ' '; /* the space got eaten */
548 switch (ch) {
549 case '-':
550 leftold--;
551 break;
552 case ' ':
553 leftold--;
554 leftnew--;
555 break;
556 case '+':
557 leftnew--;
558 break;
559 default:
560 err = got_error(GOT_ERR_PATCH_MALFORMED);
561 goto done;
564 if (leftold < 0 || leftnew < 0) {
565 err = got_error(GOT_ERR_PATCH_MALFORMED);
566 goto done;
569 err = send_line(line);
570 if (err)
571 goto done;
573 if ((ch == '-' && leftold == 0) ||
574 (ch == '+' && leftnew == 0)) {
575 err = peek_special_line(fp);
576 if (err)
577 goto done;
581 done:
582 free(line);
583 return err;
586 static const struct got_error *
587 read_patch(struct imsgbuf *ibuf, int fd)
589 const struct got_error *err = NULL;
590 FILE *fp;
591 int git, patch_found = 0;
592 char *cid = NULL;
594 if ((fp = fdopen(fd, "r")) == NULL) {
595 err = got_error_from_errno("fdopen");
596 close(fd);
597 return err;
600 while ((err = patch_start(&git, &cid, fp)) == NULL) {
601 int done, next;
603 err = find_diff(&done, &next, fp, git, cid);
604 if (err)
605 goto done;
606 if (next)
607 continue;
609 patch_found = 1;
611 while (!done) {
612 err = parse_hunk(fp, &done);
613 if (err)
614 goto done;
617 err = send_patch_done();
618 if (err)
619 goto done;
622 done:
623 fclose(fp);
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 #if 0
639 static int attached;
640 while (!attached)
641 sleep(1);
642 #endif
644 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
645 #ifndef PROFILE
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);
650 return 1;
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);
657 return 1;
659 if (cap_enter() == -1) {
660 err = got_error_from_errno("cap_enter");
661 got_privsep_send_error(&ibuf, err);
662 return 1;
664 #endif
666 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
667 if (err)
668 goto done;
669 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
670 err = got_error(GOT_ERR_PRIVSEP_MSG);
671 goto done;
674 err = read_patch(&ibuf, imsg.fd);
675 if (err)
676 goto done;
677 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
678 NULL, 0) == -1) {
679 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
680 goto done;
682 err = got_privsep_flush_imsg(&ibuf);
683 done:
684 imsg_free(&imsg);
685 if (err != NULL) {
686 got_privsep_send_error(&ibuf, err);
687 err = NULL;
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);
693 return err ? 1 : 0;