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/uio.h>
41 #include <ctype.h>
42 #include <limits.h>
43 #include <paths.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 #include "got_error.h"
51 #include "got_object.h"
53 #include "got_compat.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_object.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.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, 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.git = git;
83 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
84 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
85 return NULL;
86 }
88 static const struct got_error *
89 send_patch_done(void)
90 {
91 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
92 NULL, 0) == -1)
93 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
94 if (imsg_flush(&ibuf) == -1)
95 return got_error_from_errno("imsg_flush");
96 return NULL;
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 const struct got_error *
132 blobid(const char *line, char **blob, int git)
134 uint8_t digest[SHA1_DIGEST_LENGTH];
135 size_t len;
137 *blob = NULL;
139 len = strspn(line, "0123456789abcdefABCDEF");
140 if ((*blob = strndup(line, len)) == NULL)
141 return got_error_from_errno("strndup");
143 if (!git && !got_parse_sha1_digest(digest, *blob)) {
144 /* silently ignore invalid blob ids */
145 free(*blob);
146 *blob = NULL;
148 return NULL;
151 static const struct got_error *
152 patch_start(int *git, char **cid, FILE *fp)
154 const struct got_error *err = NULL;
155 char *line = NULL;
156 size_t linesize = 0;
157 ssize_t linelen;
159 *git = 0;
161 while ((linelen = getline(&line, &linesize, fp)) != -1) {
162 if (!strncmp(line, "diff --git ", 11)) {
163 *git = 1;
164 free(*cid);
165 *cid = NULL;
166 break;
167 } else if (!strncmp(line, "diff ", 5)) {
168 *git = 0;
169 free(*cid);
170 *cid = NULL;
171 } else if (!strncmp(line, "commit - ", 9)) {
172 free(*cid);
173 err = blobid(line + 9, cid, *git);
174 if (err)
175 break;
176 } else if (!strncmp(line, "--- ", 4) ||
177 !strncmp(line, "+++ ", 4) ||
178 !strncmp(line, "blob - ", 7)) {
179 /* rewind to previous line */
180 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
181 err = got_error_from_errno("fseeko");
182 break;
186 free(line);
187 if (ferror(fp) && err == NULL)
188 err = got_error_from_errno("getline");
189 if (feof(fp) && err == NULL)
190 err = got_error(GOT_ERR_NO_PATCH);
191 return err;
194 static const struct got_error *
195 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
197 const struct got_error *err = NULL;
198 char *old = NULL, *new = NULL;
199 char *blob = NULL;
200 char *line = NULL;
201 size_t linesize = 0;
202 ssize_t linelen;
203 int create, rename = 0;
205 *done = 0;
206 *next = 0;
207 while ((linelen = getline(&line, &linesize, fp)) != -1) {
208 /*
209 * Ignore the Index name like GNU and larry' patch,
210 * we don't have to follow POSIX.
211 */
213 if (!strncmp(line, "--- ", 4)) {
214 free(old);
215 err = filename(line+4, &old);
216 } else if (rename && !strncmp(line, "rename from ", 12)) {
217 free(old);
218 err = filename(line+12, &old);
219 } else if (!strncmp(line, "+++ ", 4)) {
220 free(new);
221 err = filename(line+4, &new);
222 } else if (!git && !strncmp(line, "blob - ", 7)) {
223 free(blob);
224 err = blobid(line + 7, &blob, git);
225 } else if (rename && !strncmp(line, "rename to ", 10)) {
226 free(new);
227 err = filename(line + 10, &new);
228 } else if (git && !strncmp(line, "similarity index 100%", 21))
229 rename = 1;
230 else if (git && !strncmp(line, "index ", 6)) {
231 free(blob);
232 err = blobid(line + 6, &blob, git);
233 } else if (!strncmp(line, "diff ", 5)) {
234 /* rewind to previous line */
235 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
236 err = got_error_from_errno("fseeko");
237 *next = 1;
238 break;
241 if (err)
242 break;
244 /*
245 * Git-style diffs with "similarity index 100%" don't
246 * have any hunks and ends with the "rename to foobar"
247 * line.
248 */
249 if (rename && old != NULL && new != NULL) {
250 *done = 1;
251 err = send_patch(old, new, commitid,
252 blob, git);
253 break;
256 if (!strncmp(line, "@@ -", 4)) {
257 create = !strncmp(line+4, "0,0", 3);
258 if ((old == NULL && new == NULL) ||
259 (!create && old == NULL))
260 err = got_error(GOT_ERR_PATCH_MALFORMED);
261 else
262 err = send_patch(old, new, commitid,
263 blob, git);
265 if (err)
266 break;
268 /* rewind to previous line */
269 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
270 err = got_error_from_errno("fseeko");
271 break;
275 free(old);
276 free(new);
277 free(blob);
278 free(line);
279 if (ferror(fp) && err == NULL)
280 err = got_error_from_errno("getline");
281 if (feof(fp) && err == NULL)
282 err = got_error(GOT_ERR_NO_PATCH);
283 return err;
286 static const struct got_error *
287 strtolnum(char **str, int *n)
289 char *p, c;
290 const char *errstr;
292 for (p = *str; isdigit((unsigned char)*p); ++p)
293 /* nop */;
295 c = *p;
296 *p = '\0';
298 *n = strtonum(*str, 0, INT_MAX, &errstr);
299 if (errstr != NULL)
300 return got_error(GOT_ERR_PATCH_MALFORMED);
302 *p = c;
303 *str = p;
304 return NULL;
307 static const struct got_error *
308 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
310 static const struct got_error *err = NULL;
312 if (strncmp(s, "@@ -", 4)) {
313 *done = 1;
314 return NULL;
317 s += 4;
318 if (!*s)
319 return NULL;
320 err = strtolnum(&s, &hdr->oldfrom);
321 if (err)
322 return err;
323 if (*s == ',') {
324 s++;
325 err = strtolnum(&s, &hdr->oldlines);
326 if (err)
327 return err;
328 } else
329 hdr->oldlines = 1;
331 if (*s == ' ')
332 s++;
334 if (*s != '+' || !*++s)
335 return got_error(GOT_ERR_PATCH_MALFORMED);
336 err = strtolnum(&s, &hdr->newfrom);
337 if (err)
338 return err;
339 if (*s == ',') {
340 s++;
341 err = strtolnum(&s, &hdr->newlines);
342 if (err)
343 return err;
344 } else
345 hdr->newlines = 1;
347 if (*s == ' ')
348 s++;
350 if (*s != '@')
351 return got_error(GOT_ERR_PATCH_MALFORMED);
353 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
354 hdr->newfrom >= INT_MAX - hdr->newlines ||
355 /* not so sure about this one */
356 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
357 (hdr->oldlines == 0 && hdr->newlines == 0))
358 return got_error(GOT_ERR_PATCH_MALFORMED);
360 if (hdr->oldlines == 0) {
361 /* larry says to "do append rather than insert"; I don't
362 * quite get it, but i trust him.
363 */
364 hdr->oldfrom++;
367 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
368 hdr, sizeof(*hdr)) == -1)
369 return got_error_from_errno(
370 "imsg_compose GOT_IMSG_PATCH_HUNK");
371 return NULL;
374 static const struct got_error *
375 send_line(const char *line)
377 static const struct got_error *err = NULL;
378 char *p = NULL;
380 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
381 if (asprintf(&p, " %s", line) == -1)
382 return got_error_from_errno("asprintf");
383 line = p;
386 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
387 line, strlen(line) + 1) == -1)
388 err = got_error_from_errno(
389 "imsg_compose GOT_IMSG_PATCH_LINE");
391 free(p);
392 return err;
395 static const struct got_error *
396 peek_special_line(FILE *fp)
398 const struct got_error *err;
399 int ch;
401 ch = fgetc(fp);
402 if (ch != EOF && ch != '\\') {
403 ungetc(ch, fp);
404 return NULL;
407 if (ch == '\\') {
408 err = send_line("\\");
409 if (err)
410 return err;
413 while (ch != EOF && ch != '\n')
414 ch = fgetc(fp);
416 if (ch != EOF || feof(fp))
417 return NULL;
418 return got_error(GOT_ERR_IO);
421 static const struct got_error *
422 parse_hunk(FILE *fp, int *done)
424 static const struct got_error *err = NULL;
425 struct got_imsg_patch_hunk hdr;
426 char *line = NULL, ch;
427 size_t linesize = 0;
428 ssize_t linelen;
429 int leftold, leftnew;
431 linelen = getline(&line, &linesize, fp);
432 if (linelen == -1) {
433 *done = 1;
434 goto done;
437 err = parse_hdr(line, done, &hdr);
438 if (err)
439 goto done;
440 if (*done) {
441 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
442 err = got_error_from_errno("fseeko");
443 goto done;
446 leftold = hdr.oldlines;
447 leftnew = hdr.newlines;
449 while (leftold > 0 || leftnew > 0) {
450 linelen = getline(&line, &linesize, fp);
451 if (linelen == -1) {
452 if (ferror(fp)) {
453 err = got_error_from_errno("getline");
454 goto done;
457 /* trailing newlines may be chopped */
458 if (leftold < 3 && leftnew < 3) {
459 *done = 1;
460 break;
463 err = got_error(GOT_ERR_PATCH_TRUNCATED);
464 goto done;
466 if (line[linelen - 1] == '\n')
467 line[linelen - 1] = '\0';
469 /* usr.bin/patch allows '=' as context char */
470 if (*line == '=')
471 *line = ' ';
473 ch = *line;
474 if (ch == '\t' || ch == '\0')
475 ch = ' '; /* the space got eaten */
477 switch (ch) {
478 case '-':
479 leftold--;
480 break;
481 case ' ':
482 leftold--;
483 leftnew--;
484 break;
485 case '+':
486 leftnew--;
487 break;
488 default:
489 err = got_error(GOT_ERR_PATCH_MALFORMED);
490 goto done;
493 if (leftold < 0 || leftnew < 0) {
494 err = got_error(GOT_ERR_PATCH_MALFORMED);
495 goto done;
498 err = send_line(line);
499 if (err)
500 goto done;
502 if ((ch == '-' && leftold == 0) ||
503 (ch == '+' && leftnew == 0)) {
504 err = peek_special_line(fp);
505 if (err)
506 goto done;
510 done:
511 free(line);
512 return err;
515 static const struct got_error *
516 read_patch(struct imsgbuf *ibuf, int fd)
518 const struct got_error *err = NULL;
519 FILE *fp;
520 int git, patch_found = 0;
521 char *cid = NULL;
523 if ((fp = fdopen(fd, "r")) == NULL) {
524 err = got_error_from_errno("fdopen");
525 close(fd);
526 return err;
529 while ((err = patch_start(&git, &cid, fp)) == NULL) {
530 int done, next;
532 err = find_diff(&done, &next, fp, git, cid);
533 if (err)
534 goto done;
535 if (next)
536 continue;
538 patch_found = 1;
540 while (!done) {
541 err = parse_hunk(fp, &done);
542 if (err)
543 goto done;
546 err = send_patch_done();
547 if (err)
548 goto done;
551 done:
552 fclose(fp);
553 free(cid);
555 /* ignore trailing gibberish */
556 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
557 err = NULL;
559 return err;
562 int
563 main(int argc, char **argv)
565 const struct got_error *err = NULL;
566 struct imsg imsg;
567 #if 0
568 static int attached;
569 while (!attached)
570 sleep(1);
571 #endif
573 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
574 #ifndef PROFILE
575 /* revoke access to most system calls */
576 if (pledge("stdio recvfd", NULL) == -1) {
577 err = got_error_from_errno("pledge");
578 got_privsep_send_error(&ibuf, err);
579 return 1;
582 /* revoke fs access */
583 if (landlock_no_fs() == -1) {
584 err = got_error_from_errno("landlock_no_fs");
585 got_privsep_send_error(&ibuf, err);
586 return 1;
588 if (cap_enter() == -1) {
589 err = got_error_from_errno("cap_enter");
590 got_privsep_send_error(&ibuf, err);
591 return 1;
593 #endif
595 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
596 if (err)
597 goto done;
598 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
599 err = got_error(GOT_ERR_PRIVSEP_MSG);
600 goto done;
603 err = read_patch(&ibuf, imsg.fd);
604 if (err)
605 goto done;
606 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
607 NULL, 0) == -1) {
608 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
609 goto done;
611 err = got_privsep_flush_imsg(&ibuf);
612 done:
613 imsg_free(&imsg);
614 if (err != NULL) {
615 got_privsep_send_error(&ibuf, err);
616 err = NULL;
618 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
619 err = got_error_from_errno("close");
620 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
621 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
622 return err ? 1 : 0;