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 !strncmp(line, "file + ", 7)) {
236 xbit = filexbit(line);
237 } else if (!git && !strncmp(line, "blob - ", 7)) {
238 free(blob);
239 err = blobid(line + 7, &blob, git);
240 } else if (rename && !strncmp(line, "rename to ", 10)) {
241 free(new);
242 err = filename(line + 10, &new);
243 } else if (git && !strncmp(line, "similarity index 100%", 21))
244 rename = 1;
245 else if (git && !strncmp(line, "new file mode 100", 17))
246 xbit = strncmp(line + 17, "755", 3) == 0;
247 else if (git && !strncmp(line, "index ", 6)) {
248 free(blob);
249 err = blobid(line + 6, &blob, git);
250 } else if (!strncmp(line, "diff ", 5)) {
251 /* rewind to previous line */
252 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
253 err = got_error_from_errno("fseeko");
254 *next = 1;
255 break;
258 if (err)
259 break;
261 /*
262 * Git-style diffs with "similarity index 100%" don't
263 * have any hunks and ends with the "rename to foobar"
264 * line.
265 */
266 if (rename && old != NULL && new != NULL) {
267 *done = 1;
268 err = send_patch(old, new, commitid,
269 blob, xbit, git);
270 break;
273 if (!strncmp(line, "@@ -", 4)) {
274 create = !strncmp(line+4, "0,0", 3);
275 if ((old == NULL && new == NULL) ||
276 (!create && old == NULL))
277 err = got_error(GOT_ERR_PATCH_MALFORMED);
278 else
279 err = send_patch(old, new, commitid,
280 blob, xbit, git);
282 if (err)
283 break;
285 /* rewind to previous line */
286 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
287 err = got_error_from_errno("fseeko");
288 break;
292 free(old);
293 free(new);
294 free(blob);
295 free(line);
296 if (ferror(fp) && err == NULL)
297 err = got_error_from_errno("getline");
298 if (feof(fp) && err == NULL)
299 err = got_error(GOT_ERR_NO_PATCH);
300 return err;
303 static const struct got_error *
304 strtolnum(char **str, int *n)
306 char *p, c;
307 const char *errstr;
309 for (p = *str; isdigit((unsigned char)*p); ++p)
310 /* nop */;
312 c = *p;
313 *p = '\0';
315 *n = strtonum(*str, 0, INT_MAX, &errstr);
316 if (errstr != NULL)
317 return got_error(GOT_ERR_PATCH_MALFORMED);
319 *p = c;
320 *str = p;
321 return NULL;
324 static const struct got_error *
325 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
327 static const struct got_error *err = NULL;
329 if (strncmp(s, "@@ -", 4)) {
330 *done = 1;
331 return NULL;
334 s += 4;
335 if (!*s)
336 return NULL;
337 err = strtolnum(&s, &hdr->oldfrom);
338 if (err)
339 return err;
340 if (*s == ',') {
341 s++;
342 err = strtolnum(&s, &hdr->oldlines);
343 if (err)
344 return err;
345 } else
346 hdr->oldlines = 1;
348 if (*s == ' ')
349 s++;
351 if (*s != '+' || !*++s)
352 return got_error(GOT_ERR_PATCH_MALFORMED);
353 err = strtolnum(&s, &hdr->newfrom);
354 if (err)
355 return err;
356 if (*s == ',') {
357 s++;
358 err = strtolnum(&s, &hdr->newlines);
359 if (err)
360 return err;
361 } else
362 hdr->newlines = 1;
364 if (*s == ' ')
365 s++;
367 if (*s != '@')
368 return got_error(GOT_ERR_PATCH_MALFORMED);
370 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
371 hdr->newfrom >= INT_MAX - hdr->newlines ||
372 /* not so sure about this one */
373 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
374 (hdr->oldlines == 0 && hdr->newlines == 0))
375 return got_error(GOT_ERR_PATCH_MALFORMED);
377 if (hdr->oldlines == 0) {
378 /* larry says to "do append rather than insert"; I don't
379 * quite get it, but i trust him.
380 */
381 hdr->oldfrom++;
384 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
385 hdr, sizeof(*hdr)) == -1)
386 return got_error_from_errno(
387 "imsg_compose GOT_IMSG_PATCH_HUNK");
388 return NULL;
391 static const struct got_error *
392 send_line(const char *line)
394 static const struct got_error *err = NULL;
395 char *p = NULL;
397 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
398 if (asprintf(&p, " %s", line) == -1)
399 return got_error_from_errno("asprintf");
400 line = p;
403 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
404 line, strlen(line) + 1) == -1)
405 err = got_error_from_errno(
406 "imsg_compose GOT_IMSG_PATCH_LINE");
408 free(p);
409 return err;
412 static const struct got_error *
413 peek_special_line(FILE *fp)
415 const struct got_error *err;
416 int ch;
418 ch = fgetc(fp);
419 if (ch != EOF && ch != '\\') {
420 ungetc(ch, fp);
421 return NULL;
424 if (ch == '\\') {
425 err = send_line("\\");
426 if (err)
427 return err;
430 while (ch != EOF && ch != '\n')
431 ch = fgetc(fp);
433 if (ch != EOF || feof(fp))
434 return NULL;
435 return got_error(GOT_ERR_IO);
438 static const struct got_error *
439 parse_hunk(FILE *fp, int *done)
441 static const struct got_error *err = NULL;
442 struct got_imsg_patch_hunk hdr;
443 char *line = NULL, ch;
444 size_t linesize = 0;
445 ssize_t linelen;
446 int leftold, leftnew;
448 linelen = getline(&line, &linesize, fp);
449 if (linelen == -1) {
450 *done = 1;
451 goto done;
454 err = parse_hdr(line, done, &hdr);
455 if (err)
456 goto done;
457 if (*done) {
458 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
459 err = got_error_from_errno("fseeko");
460 goto done;
463 leftold = hdr.oldlines;
464 leftnew = hdr.newlines;
466 while (leftold > 0 || leftnew > 0) {
467 linelen = getline(&line, &linesize, fp);
468 if (linelen == -1) {
469 if (ferror(fp)) {
470 err = got_error_from_errno("getline");
471 goto done;
474 /* trailing newlines may be chopped */
475 if (leftold < 3 && leftnew < 3) {
476 *done = 1;
477 break;
480 err = got_error(GOT_ERR_PATCH_TRUNCATED);
481 goto done;
483 if (line[linelen - 1] == '\n')
484 line[linelen - 1] = '\0';
486 /* usr.bin/patch allows '=' as context char */
487 if (*line == '=')
488 *line = ' ';
490 ch = *line;
491 if (ch == '\t' || ch == '\0')
492 ch = ' '; /* the space got eaten */
494 switch (ch) {
495 case '-':
496 leftold--;
497 break;
498 case ' ':
499 leftold--;
500 leftnew--;
501 break;
502 case '+':
503 leftnew--;
504 break;
505 default:
506 err = got_error(GOT_ERR_PATCH_MALFORMED);
507 goto done;
510 if (leftold < 0 || leftnew < 0) {
511 err = got_error(GOT_ERR_PATCH_MALFORMED);
512 goto done;
515 err = send_line(line);
516 if (err)
517 goto done;
519 if ((ch == '-' && leftold == 0) ||
520 (ch == '+' && leftnew == 0)) {
521 err = peek_special_line(fp);
522 if (err)
523 goto done;
527 done:
528 free(line);
529 return err;
532 static const struct got_error *
533 read_patch(struct imsgbuf *ibuf, int fd)
535 const struct got_error *err = NULL;
536 FILE *fp;
537 int git, patch_found = 0;
538 char *cid = NULL;
540 if ((fp = fdopen(fd, "r")) == NULL) {
541 err = got_error_from_errno("fdopen");
542 close(fd);
543 return err;
546 while ((err = patch_start(&git, &cid, fp)) == NULL) {
547 int done, next;
549 err = find_diff(&done, &next, fp, git, cid);
550 if (err)
551 goto done;
552 if (next)
553 continue;
555 patch_found = 1;
557 while (!done) {
558 err = parse_hunk(fp, &done);
559 if (err)
560 goto done;
563 err = send_patch_done();
564 if (err)
565 goto done;
568 done:
569 fclose(fp);
570 free(cid);
572 /* ignore trailing gibberish */
573 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
574 err = NULL;
576 return err;
579 int
580 main(int argc, char **argv)
582 const struct got_error *err = NULL;
583 struct imsg imsg;
584 #if 0
585 static int attached;
586 while (!attached)
587 sleep(1);
588 #endif
590 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
591 #ifndef PROFILE
592 /* revoke access to most system calls */
593 if (pledge("stdio recvfd", NULL) == -1) {
594 err = got_error_from_errno("pledge");
595 got_privsep_send_error(&ibuf, err);
596 return 1;
598 #endif
600 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
601 if (err)
602 goto done;
603 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
604 err = got_error(GOT_ERR_PRIVSEP_MSG);
605 goto done;
608 err = read_patch(&ibuf, imsg.fd);
609 if (err)
610 goto done;
611 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
612 NULL, 0) == -1) {
613 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
614 goto done;
616 err = got_privsep_flush_imsg(&ibuf);
617 done:
618 imsg_free(&imsg);
619 if (err != NULL) {
620 got_privsep_send_error(&ibuf, err);
621 err = NULL;
623 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
624 err = got_error_from_errno("close");
625 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
626 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
627 return err ? 1 : 0;