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, 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.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 if (imsg_flush(&ibuf) == -1)
96 return got_error_from_errno("imsg_flush");
97 return NULL;
98 }
100 /* based on fetchname from usr.bin/patch/util.c */
101 static const struct got_error *
102 filename(const char *at, char **name)
104 char *tmp, *t;
106 *name = NULL;
107 if (*at == '\0')
108 return NULL;
110 while (isspace((unsigned char)*at))
111 at++;
113 /* files can be created or removed by diffing against /dev/null */
114 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
115 return NULL;
117 tmp = strdup(at);
118 if (tmp == NULL)
119 return got_error_from_errno("strdup");
120 if ((t = strchr(tmp, '\t')) != NULL)
121 *t = '\0';
122 if ((t = strchr(tmp, '\n')) != NULL)
123 *t = '\0';
125 *name = strdup(tmp);
126 free(tmp);
127 if (*name == NULL)
128 return got_error_from_errno("strdup");
129 return NULL;
132 static const struct got_error *
133 blobid(const char *line, char **blob, int git)
135 uint8_t digest[SHA1_DIGEST_LENGTH];
136 size_t len;
138 *blob = NULL;
140 len = strspn(line, "0123456789abcdefABCDEF");
141 if ((*blob = strndup(line, len)) == NULL)
142 return got_error_from_errno("strndup");
144 if (!git && !got_parse_sha1_digest(digest, *blob)) {
145 /* silently ignore invalid blob ids */
146 free(*blob);
147 *blob = NULL;
149 return NULL;
152 static const struct got_error *
153 patch_start(int *git, char **cid, FILE *fp)
155 const struct got_error *err = NULL;
156 char *line = NULL;
157 size_t linesize = 0;
158 ssize_t linelen;
160 *git = 0;
162 while ((linelen = getline(&line, &linesize, fp)) != -1) {
163 if (!strncmp(line, "diff --git ", 11)) {
164 *git = 1;
165 free(*cid);
166 *cid = NULL;
167 break;
168 } else if (!strncmp(line, "diff ", 5)) {
169 *git = 0;
170 free(*cid);
171 *cid = NULL;
172 } else if (!strncmp(line, "commit - ", 9)) {
173 free(*cid);
174 err = blobid(line + 9, cid, *git);
175 if (err)
176 break;
177 } else if (!strncmp(line, "--- ", 4) ||
178 !strncmp(line, "+++ ", 4) ||
179 !strncmp(line, "blob - ", 7)) {
180 /* rewind to previous line */
181 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
182 err = got_error_from_errno("fseeko");
183 break;
187 free(line);
188 if (ferror(fp) && err == NULL)
189 err = got_error_from_errno("getline");
190 if (feof(fp) && err == NULL)
191 err = got_error(GOT_ERR_NO_PATCH);
192 return err;
195 static const struct got_error *
196 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
198 const struct got_error *err = NULL;
199 char *old = NULL, *new = NULL;
200 char *blob = NULL;
201 char *line = NULL;
202 size_t linesize = 0;
203 ssize_t linelen;
204 int create, rename = 0;
206 *done = 0;
207 *next = 0;
208 while ((linelen = getline(&line, &linesize, fp)) != -1) {
209 /*
210 * Ignore the Index name like GNU and larry' patch,
211 * we don't have to follow POSIX.
212 */
214 if (!strncmp(line, "--- ", 4)) {
215 free(old);
216 err = filename(line+4, &old);
217 } else if (rename && !strncmp(line, "rename from ", 12)) {
218 free(old);
219 err = filename(line+12, &old);
220 } else if (!strncmp(line, "+++ ", 4)) {
221 free(new);
222 err = filename(line+4, &new);
223 } else if (!git && !strncmp(line, "blob - ", 7)) {
224 free(blob);
225 err = blobid(line + 7, &blob, git);
226 } else if (rename && !strncmp(line, "rename to ", 10)) {
227 free(new);
228 err = filename(line + 10, &new);
229 } else if (git && !strncmp(line, "similarity index 100%", 21))
230 rename = 1;
231 else if (git && !strncmp(line, "index ", 6)) {
232 free(blob);
233 err = blobid(line + 6, &blob, git);
234 } else if (!strncmp(line, "diff ", 5)) {
235 /* rewind to previous line */
236 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
237 err = got_error_from_errno("fseeko");
238 *next = 1;
239 break;
242 if (err)
243 break;
245 /*
246 * Git-style diffs with "similarity index 100%" don't
247 * have any hunks and ends with the "rename to foobar"
248 * line.
249 */
250 if (rename && old != NULL && new != NULL) {
251 *done = 1;
252 err = send_patch(old, new, commitid,
253 blob, git);
254 break;
257 if (!strncmp(line, "@@ -", 4)) {
258 create = !strncmp(line+4, "0,0", 3);
259 if ((old == NULL && new == NULL) ||
260 (!create && old == NULL))
261 err = got_error(GOT_ERR_PATCH_MALFORMED);
262 else
263 err = send_patch(old, new, commitid,
264 blob, git);
266 if (err)
267 break;
269 /* rewind to previous line */
270 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
271 err = got_error_from_errno("fseeko");
272 break;
276 free(old);
277 free(new);
278 free(blob);
279 free(line);
280 if (ferror(fp) && err == NULL)
281 err = got_error_from_errno("getline");
282 if (feof(fp) && err == NULL)
283 err = got_error(GOT_ERR_NO_PATCH);
284 return err;
287 static const struct got_error *
288 strtolnum(char **str, int *n)
290 char *p, c;
291 const char *errstr;
293 for (p = *str; isdigit((unsigned char)*p); ++p)
294 /* nop */;
296 c = *p;
297 *p = '\0';
299 *n = strtonum(*str, 0, INT_MAX, &errstr);
300 if (errstr != NULL)
301 return got_error(GOT_ERR_PATCH_MALFORMED);
303 *p = c;
304 *str = p;
305 return NULL;
308 static const struct got_error *
309 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
311 static const struct got_error *err = NULL;
313 if (strncmp(s, "@@ -", 4)) {
314 *done = 1;
315 return NULL;
318 s += 4;
319 if (!*s)
320 return NULL;
321 err = strtolnum(&s, &hdr->oldfrom);
322 if (err)
323 return err;
324 if (*s == ',') {
325 s++;
326 err = strtolnum(&s, &hdr->oldlines);
327 if (err)
328 return err;
329 } else
330 hdr->oldlines = 1;
332 if (*s == ' ')
333 s++;
335 if (*s != '+' || !*++s)
336 return got_error(GOT_ERR_PATCH_MALFORMED);
337 err = strtolnum(&s, &hdr->newfrom);
338 if (err)
339 return err;
340 if (*s == ',') {
341 s++;
342 err = strtolnum(&s, &hdr->newlines);
343 if (err)
344 return err;
345 } else
346 hdr->newlines = 1;
348 if (*s == ' ')
349 s++;
351 if (*s != '@')
352 return got_error(GOT_ERR_PATCH_MALFORMED);
354 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
355 hdr->newfrom >= INT_MAX - hdr->newlines ||
356 /* not so sure about this one */
357 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
358 (hdr->oldlines == 0 && hdr->newlines == 0))
359 return got_error(GOT_ERR_PATCH_MALFORMED);
361 if (hdr->oldlines == 0) {
362 /* larry says to "do append rather than insert"; I don't
363 * quite get it, but i trust him.
364 */
365 hdr->oldfrom++;
368 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
369 hdr, sizeof(*hdr)) == -1)
370 return got_error_from_errno(
371 "imsg_compose GOT_IMSG_PATCH_HUNK");
372 return NULL;
375 static const struct got_error *
376 send_line(const char *line)
378 static const struct got_error *err = NULL;
379 char *p = NULL;
381 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
382 if (asprintf(&p, " %s", line) == -1)
383 return got_error_from_errno("asprintf");
384 line = p;
387 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
388 line, strlen(line) + 1) == -1)
389 err = got_error_from_errno(
390 "imsg_compose GOT_IMSG_PATCH_LINE");
392 free(p);
393 return err;
396 static const struct got_error *
397 peek_special_line(FILE *fp)
399 const struct got_error *err;
400 int ch;
402 ch = fgetc(fp);
403 if (ch != EOF && ch != '\\') {
404 ungetc(ch, fp);
405 return NULL;
408 if (ch == '\\') {
409 err = send_line("\\");
410 if (err)
411 return err;
414 while (ch != EOF && ch != '\n')
415 ch = fgetc(fp);
417 if (ch != EOF || feof(fp))
418 return NULL;
419 return got_error(GOT_ERR_IO);
422 static const struct got_error *
423 parse_hunk(FILE *fp, int *done)
425 static const struct got_error *err = NULL;
426 struct got_imsg_patch_hunk hdr;
427 char *line = NULL, ch;
428 size_t linesize = 0;
429 ssize_t linelen;
430 int leftold, leftnew;
432 linelen = getline(&line, &linesize, fp);
433 if (linelen == -1) {
434 *done = 1;
435 goto done;
438 err = parse_hdr(line, done, &hdr);
439 if (err)
440 goto done;
441 if (*done) {
442 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
443 err = got_error_from_errno("fseeko");
444 goto done;
447 leftold = hdr.oldlines;
448 leftnew = hdr.newlines;
450 while (leftold > 0 || leftnew > 0) {
451 linelen = getline(&line, &linesize, fp);
452 if (linelen == -1) {
453 if (ferror(fp)) {
454 err = got_error_from_errno("getline");
455 goto done;
458 /* trailing newlines may be chopped */
459 if (leftold < 3 && leftnew < 3) {
460 *done = 1;
461 break;
464 err = got_error(GOT_ERR_PATCH_TRUNCATED);
465 goto done;
467 if (line[linelen - 1] == '\n')
468 line[linelen - 1] = '\0';
470 /* usr.bin/patch allows '=' as context char */
471 if (*line == '=')
472 *line = ' ';
474 ch = *line;
475 if (ch == '\t' || ch == '\0')
476 ch = ' '; /* the space got eaten */
478 switch (ch) {
479 case '-':
480 leftold--;
481 break;
482 case ' ':
483 leftold--;
484 leftnew--;
485 break;
486 case '+':
487 leftnew--;
488 break;
489 default:
490 err = got_error(GOT_ERR_PATCH_MALFORMED);
491 goto done;
494 if (leftold < 0 || leftnew < 0) {
495 err = got_error(GOT_ERR_PATCH_MALFORMED);
496 goto done;
499 err = send_line(line);
500 if (err)
501 goto done;
503 if ((ch == '-' && leftold == 0) ||
504 (ch == '+' && leftnew == 0)) {
505 err = peek_special_line(fp);
506 if (err)
507 goto done;
511 done:
512 free(line);
513 return err;
516 static const struct got_error *
517 read_patch(struct imsgbuf *ibuf, int fd)
519 const struct got_error *err = NULL;
520 FILE *fp;
521 int git, patch_found = 0;
522 char *cid = NULL;
524 if ((fp = fdopen(fd, "r")) == NULL) {
525 err = got_error_from_errno("fdopen");
526 close(fd);
527 return err;
530 while ((err = patch_start(&git, &cid, fp)) == NULL) {
531 int done, next;
533 err = find_diff(&done, &next, fp, git, cid);
534 if (err)
535 goto done;
536 if (next)
537 continue;
539 patch_found = 1;
541 while (!done) {
542 err = parse_hunk(fp, &done);
543 if (err)
544 goto done;
547 err = send_patch_done();
548 if (err)
549 goto done;
552 done:
553 fclose(fp);
554 free(cid);
556 /* ignore trailing gibberish */
557 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
558 err = NULL;
560 return err;
563 int
564 main(int argc, char **argv)
566 const struct got_error *err = NULL;
567 struct imsg imsg;
568 #if 0
569 static int attached;
570 while (!attached)
571 sleep(1);
572 #endif
574 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
575 #ifndef PROFILE
576 /* revoke access to most system calls */
577 if (pledge("stdio recvfd", NULL) == -1) {
578 err = got_error_from_errno("pledge");
579 got_privsep_send_error(&ibuf, err);
580 return 1;
582 #endif
584 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
585 if (err)
586 goto done;
587 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
588 err = got_error(GOT_ERR_PRIVSEP_MSG);
589 goto done;
592 err = read_patch(&ibuf, imsg.fd);
593 if (err)
594 goto done;
595 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
596 NULL, 0) == -1) {
597 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
598 goto done;
600 err = got_privsep_flush_imsg(&ibuf);
601 done:
602 imsg_free(&imsg);
603 if (err != NULL) {
604 got_privsep_send_error(&ibuf, err);
605 err = NULL;
607 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
608 err = got_error_from_errno("close");
609 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
610 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
611 return err ? 1 : 0;