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 filexbit(const char *line)
134 char *m;
136 m = strchr(line, '(');
137 if (m && !strncmp(m + 1, "mode ", 5))
138 return strncmp(m + 6, "755", 3) == 0;
140 return 0;
143 static const struct got_error *
144 blobid(const char *line, char **blob, int git)
146 uint8_t digest[SHA1_DIGEST_LENGTH];
147 size_t len;
149 *blob = NULL;
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 */
157 free(*blob);
158 *blob = NULL;
160 return NULL;
163 static const struct got_error *
164 patch_start(int *git, char **cid, FILE *fp)
166 const struct got_error *err = NULL;
167 char *line = NULL;
168 size_t linesize = 0;
169 ssize_t linelen;
171 *git = 0;
173 while ((linelen = getline(&line, &linesize, fp)) != -1) {
174 if (!strncmp(line, "diff --git ", 11)) {
175 *git = 1;
176 free(*cid);
177 *cid = NULL;
178 break;
179 } else if (!strncmp(line, "diff ", 5)) {
180 *git = 0;
181 free(*cid);
182 *cid = NULL;
183 } else if (!strncmp(line, "commit - ", 9)) {
184 free(*cid);
185 err = blobid(line + 9, cid, *git);
186 if (err)
187 break;
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");
194 break;
198 free(line);
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);
203 return err;
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;
211 char *blob = NULL;
212 char *line = NULL;
213 size_t linesize = 0;
214 ssize_t linelen;
215 int create, rename = 0, xbit = 0;
217 *done = 0;
218 *next = 0;
219 while ((linelen = getline(&line, &linesize, fp)) != -1) {
220 /*
221 * Ignore the Index name like GNU and larry' patch,
222 * we don't have to follow POSIX.
223 */
225 if (!strncmp(line, "--- ", 4)) {
226 free(old);
227 err = filename(line+4, &old);
228 } else if (rename && !strncmp(line, "rename from ", 12)) {
229 free(old);
230 err = filename(line+12, &old);
231 } else if (!strncmp(line, "+++ ", 4)) {
232 free(new);
233 err = filename(line+4, &new);
234 } else if (!strncmp(line, "blob + ", 7)) {
235 xbit = filexbit(line);
236 } else if (!git && !strncmp(line, "blob - ", 7)) {
237 free(blob);
238 err = blobid(line + 7, &blob, git);
239 } else if (rename && !strncmp(line, "rename to ", 10)) {
240 free(new);
241 err = filename(line + 10, &new);
242 } else if (git && !strncmp(line, "similarity index 100%", 21))
243 rename = 1;
244 else if (git && !strncmp(line, "new file mode 100", 17))
245 xbit = strncmp(line + 17, "755", 3) == 0;
246 else if (git && !strncmp(line, "index ", 6)) {
247 free(blob);
248 err = blobid(line + 6, &blob, git);
249 } else if (!strncmp(line, "diff ", 5)) {
250 /* rewind to previous line */
251 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
252 err = got_error_from_errno("fseeko");
253 *next = 1;
254 break;
257 if (err)
258 break;
260 /*
261 * Git-style diffs with "similarity index 100%" don't
262 * have any hunks and ends with the "rename to foobar"
263 * line.
264 */
265 if (rename && old != NULL && new != NULL) {
266 *done = 1;
267 err = send_patch(old, new, commitid,
268 blob, xbit, git);
269 break;
272 if (!strncmp(line, "@@ -", 4)) {
273 create = !strncmp(line+4, "0,0", 3);
274 if ((old == NULL && new == NULL) ||
275 (!create && old == NULL))
276 err = got_error(GOT_ERR_PATCH_MALFORMED);
277 else
278 err = send_patch(old, new, commitid,
279 blob, xbit, git);
281 if (err)
282 break;
284 /* rewind to previous line */
285 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
286 err = got_error_from_errno("fseeko");
287 break;
291 free(old);
292 free(new);
293 free(blob);
294 free(line);
295 if (ferror(fp) && err == NULL)
296 err = got_error_from_errno("getline");
297 if (feof(fp) && err == NULL)
298 err = got_error(GOT_ERR_NO_PATCH);
299 return err;
302 static const struct got_error *
303 strtolnum(char **str, int *n)
305 char *p, c;
306 const char *errstr;
308 for (p = *str; isdigit((unsigned char)*p); ++p)
309 /* nop */;
311 c = *p;
312 *p = '\0';
314 *n = strtonum(*str, 0, INT_MAX, &errstr);
315 if (errstr != NULL)
316 return got_error(GOT_ERR_PATCH_MALFORMED);
318 *p = c;
319 *str = p;
320 return NULL;
323 static const struct got_error *
324 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
326 static const struct got_error *err = NULL;
328 if (strncmp(s, "@@ -", 4)) {
329 *done = 1;
330 return NULL;
333 s += 4;
334 if (!*s)
335 return NULL;
336 err = strtolnum(&s, &hdr->oldfrom);
337 if (err)
338 return err;
339 if (*s == ',') {
340 s++;
341 err = strtolnum(&s, &hdr->oldlines);
342 if (err)
343 return err;
344 } else
345 hdr->oldlines = 1;
347 if (*s == ' ')
348 s++;
350 if (*s != '+' || !*++s)
351 return got_error(GOT_ERR_PATCH_MALFORMED);
352 err = strtolnum(&s, &hdr->newfrom);
353 if (err)
354 return err;
355 if (*s == ',') {
356 s++;
357 err = strtolnum(&s, &hdr->newlines);
358 if (err)
359 return err;
360 } else
361 hdr->newlines = 1;
363 if (*s == ' ')
364 s++;
366 if (*s != '@')
367 return got_error(GOT_ERR_PATCH_MALFORMED);
369 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
370 hdr->newfrom >= INT_MAX - hdr->newlines ||
371 /* not so sure about this one */
372 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
373 (hdr->oldlines == 0 && hdr->newlines == 0))
374 return got_error(GOT_ERR_PATCH_MALFORMED);
376 if (hdr->oldlines == 0) {
377 /* larry says to "do append rather than insert"; I don't
378 * quite get it, but i trust him.
379 */
380 hdr->oldfrom++;
383 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
384 hdr, sizeof(*hdr)) == -1)
385 return got_error_from_errno(
386 "imsg_compose GOT_IMSG_PATCH_HUNK");
387 return NULL;
390 static const struct got_error *
391 send_line(const char *line)
393 static const struct got_error *err = NULL;
394 char *p = NULL;
396 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
397 if (asprintf(&p, " %s", line) == -1)
398 return got_error_from_errno("asprintf");
399 line = p;
402 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
403 line, strlen(line) + 1) == -1)
404 err = got_error_from_errno(
405 "imsg_compose GOT_IMSG_PATCH_LINE");
407 free(p);
408 return err;
411 static const struct got_error *
412 peek_special_line(FILE *fp)
414 const struct got_error *err;
415 int ch;
417 ch = fgetc(fp);
418 if (ch != EOF && ch != '\\') {
419 ungetc(ch, fp);
420 return NULL;
423 if (ch == '\\') {
424 err = send_line("\\");
425 if (err)
426 return err;
429 while (ch != EOF && ch != '\n')
430 ch = fgetc(fp);
432 if (ch != EOF || feof(fp))
433 return NULL;
434 return got_error(GOT_ERR_IO);
437 static const struct got_error *
438 parse_hunk(FILE *fp, int *done)
440 static const struct got_error *err = NULL;
441 struct got_imsg_patch_hunk hdr;
442 char *line = NULL, ch;
443 size_t linesize = 0;
444 ssize_t linelen;
445 int leftold, leftnew;
447 linelen = getline(&line, &linesize, fp);
448 if (linelen == -1) {
449 *done = 1;
450 goto done;
453 err = parse_hdr(line, done, &hdr);
454 if (err)
455 goto done;
456 if (*done) {
457 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
458 err = got_error_from_errno("fseeko");
459 goto done;
462 leftold = hdr.oldlines;
463 leftnew = hdr.newlines;
465 while (leftold > 0 || leftnew > 0) {
466 linelen = getline(&line, &linesize, fp);
467 if (linelen == -1) {
468 if (ferror(fp)) {
469 err = got_error_from_errno("getline");
470 goto done;
473 /* trailing newlines may be chopped */
474 if (leftold < 3 && leftnew < 3) {
475 *done = 1;
476 break;
479 err = got_error(GOT_ERR_PATCH_TRUNCATED);
480 goto done;
482 if (line[linelen - 1] == '\n')
483 line[linelen - 1] = '\0';
485 /* usr.bin/patch allows '=' as context char */
486 if (*line == '=')
487 *line = ' ';
489 ch = *line;
490 if (ch == '\t' || ch == '\0')
491 ch = ' '; /* the space got eaten */
493 switch (ch) {
494 case '-':
495 leftold--;
496 break;
497 case ' ':
498 leftold--;
499 leftnew--;
500 break;
501 case '+':
502 leftnew--;
503 break;
504 default:
505 err = got_error(GOT_ERR_PATCH_MALFORMED);
506 goto done;
509 if (leftold < 0 || leftnew < 0) {
510 err = got_error(GOT_ERR_PATCH_MALFORMED);
511 goto done;
514 err = send_line(line);
515 if (err)
516 goto done;
518 if ((ch == '-' && leftold == 0) ||
519 (ch == '+' && leftnew == 0)) {
520 err = peek_special_line(fp);
521 if (err)
522 goto done;
526 done:
527 free(line);
528 return err;
531 static const struct got_error *
532 read_patch(struct imsgbuf *ibuf, int fd)
534 const struct got_error *err = NULL;
535 FILE *fp;
536 int git, patch_found = 0;
537 char *cid = NULL;
539 if ((fp = fdopen(fd, "r")) == NULL) {
540 err = got_error_from_errno("fdopen");
541 close(fd);
542 return err;
545 while ((err = patch_start(&git, &cid, fp)) == NULL) {
546 int done, next;
548 err = find_diff(&done, &next, fp, git, cid);
549 if (err)
550 goto done;
551 if (next)
552 continue;
554 patch_found = 1;
556 while (!done) {
557 err = parse_hunk(fp, &done);
558 if (err)
559 goto done;
562 err = send_patch_done();
563 if (err)
564 goto done;
567 done:
568 fclose(fp);
569 free(cid);
571 /* ignore trailing gibberish */
572 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
573 err = NULL;
575 return err;
578 int
579 main(int argc, char **argv)
581 const struct got_error *err = NULL;
582 struct imsg imsg;
583 #if 0
584 static int attached;
585 while (!attached)
586 sleep(1);
587 #endif
589 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
590 #ifndef PROFILE
591 /* revoke access to most system calls */
592 if (pledge("stdio recvfd", NULL) == -1) {
593 err = got_error_from_errno("pledge");
594 got_privsep_send_error(&ibuf, err);
595 return 1;
597 #endif
599 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
600 if (err)
601 goto done;
602 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
603 err = got_error(GOT_ERR_PRIVSEP_MSG);
604 goto done;
607 err = read_patch(&ibuf, imsg.fd);
608 if (err)
609 goto done;
610 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
611 NULL, 0) == -1) {
612 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
613 goto done;
615 err = got_privsep_flush_imsg(&ibuf);
616 done:
617 imsg_free(&imsg);
618 if (err != NULL) {
619 got_privsep_send_error(&ibuf, err);
620 err = NULL;
622 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
623 err = got_error_from_errno("close");
624 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
625 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
626 return err ? 1 : 0;