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 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 const struct got_error *
131 blobid(const char *line, char **blob, int git)
133 uint8_t digest[SHA1_DIGEST_LENGTH];
134 size_t len;
136 *blob = NULL;
138 len = strspn(line, "0123456789abcdefABCDEF");
139 if ((*blob = strndup(line, len)) == NULL)
140 return got_error_from_errno("strndup");
142 if (!git && !got_parse_sha1_digest(digest, *blob)) {
143 /* silently ignore invalid blob ids */
144 free(*blob);
145 *blob = NULL;
147 return NULL;
150 static const struct got_error *
151 patch_start(int *git, char **cid, FILE *fp)
153 const struct got_error *err = NULL;
154 char *line = NULL;
155 size_t linesize = 0;
156 ssize_t linelen;
158 *git = 0;
160 while ((linelen = getline(&line, &linesize, fp)) != -1) {
161 if (!strncmp(line, "diff --git ", 11)) {
162 *git = 1;
163 free(*cid);
164 *cid = NULL;
165 break;
166 } else if (!strncmp(line, "diff ", 5)) {
167 *git = 0;
168 free(*cid);
169 *cid = NULL;
170 } else if (!strncmp(line, "commit - ", 9)) {
171 free(*cid);
172 err = blobid(line + 9, cid, *git);
173 if (err)
174 break;
175 } else if (!strncmp(line, "--- ", 4) ||
176 !strncmp(line, "+++ ", 4) ||
177 !strncmp(line, "blob - ", 7)) {
178 /* rewind to previous line */
179 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
180 err = got_error_from_errno("fseeko");
181 break;
185 free(line);
186 if (ferror(fp) && err == NULL)
187 err = got_error_from_errno("getline");
188 if (feof(fp) && err == NULL)
189 err = got_error(GOT_ERR_NO_PATCH);
190 return err;
193 static const struct got_error *
194 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
196 const struct got_error *err = NULL;
197 char *old = NULL, *new = NULL;
198 char *blob = NULL;
199 char *line = NULL;
200 size_t linesize = 0;
201 ssize_t linelen;
202 int create, rename = 0;
204 *done = 0;
205 *next = 0;
206 while ((linelen = getline(&line, &linesize, fp)) != -1) {
207 /*
208 * Ignore the Index name like GNU and larry' patch,
209 * we don't have to follow POSIX.
210 */
212 if (!strncmp(line, "--- ", 4)) {
213 free(old);
214 err = filename(line+4, &old);
215 } else if (rename && !strncmp(line, "rename from ", 12)) {
216 free(old);
217 err = filename(line+12, &old);
218 } else if (!strncmp(line, "+++ ", 4)) {
219 free(new);
220 err = filename(line+4, &new);
221 } else if (!git && !strncmp(line, "blob - ", 7)) {
222 free(blob);
223 err = blobid(line + 7, &blob, git);
224 } else if (rename && !strncmp(line, "rename to ", 10)) {
225 free(new);
226 err = filename(line + 10, &new);
227 } else if (git && !strncmp(line, "similarity index 100%", 21))
228 rename = 1;
229 else if (git && !strncmp(line, "index ", 6)) {
230 free(blob);
231 err = blobid(line + 6, &blob, git);
232 } else if (!strncmp(line, "diff ", 5)) {
233 /* rewind to previous line */
234 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
235 err = got_error_from_errno("fseeko");
236 *next = 1;
237 break;
240 if (err)
241 break;
243 /*
244 * Git-style diffs with "similarity index 100%" don't
245 * have any hunks and ends with the "rename to foobar"
246 * line.
247 */
248 if (rename && old != NULL && new != NULL) {
249 *done = 1;
250 err = send_patch(old, new, commitid,
251 blob, git);
252 break;
255 if (!strncmp(line, "@@ -", 4)) {
256 create = !strncmp(line+4, "0,0", 3);
257 if ((old == NULL && new == NULL) ||
258 (!create && old == NULL))
259 err = got_error(GOT_ERR_PATCH_MALFORMED);
260 else
261 err = send_patch(old, new, commitid,
262 blob, git);
264 if (err)
265 break;
267 /* rewind to previous line */
268 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
269 err = got_error_from_errno("fseeko");
270 break;
274 free(old);
275 free(new);
276 free(blob);
277 free(line);
278 if (ferror(fp) && err == NULL)
279 err = got_error_from_errno("getline");
280 if (feof(fp) && err == NULL)
281 err = got_error(GOT_ERR_NO_PATCH);
282 return err;
285 static const struct got_error *
286 strtolnum(char **str, int *n)
288 char *p, c;
289 const char *errstr;
291 for (p = *str; isdigit((unsigned char)*p); ++p)
292 /* nop */;
294 c = *p;
295 *p = '\0';
297 *n = strtonum(*str, 0, INT_MAX, &errstr);
298 if (errstr != NULL)
299 return got_error(GOT_ERR_PATCH_MALFORMED);
301 *p = c;
302 *str = p;
303 return NULL;
306 static const struct got_error *
307 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
309 static const struct got_error *err = NULL;
311 if (strncmp(s, "@@ -", 4)) {
312 *done = 1;
313 return NULL;
316 s += 4;
317 if (!*s)
318 return NULL;
319 err = strtolnum(&s, &hdr->oldfrom);
320 if (err)
321 return err;
322 if (*s == ',') {
323 s++;
324 err = strtolnum(&s, &hdr->oldlines);
325 if (err)
326 return err;
327 } else
328 hdr->oldlines = 1;
330 if (*s == ' ')
331 s++;
333 if (*s != '+' || !*++s)
334 return got_error(GOT_ERR_PATCH_MALFORMED);
335 err = strtolnum(&s, &hdr->newfrom);
336 if (err)
337 return err;
338 if (*s == ',') {
339 s++;
340 err = strtolnum(&s, &hdr->newlines);
341 if (err)
342 return err;
343 } else
344 hdr->newlines = 1;
346 if (*s == ' ')
347 s++;
349 if (*s != '@')
350 return got_error(GOT_ERR_PATCH_MALFORMED);
352 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
353 hdr->newfrom >= INT_MAX - hdr->newlines ||
354 /* not so sure about this one */
355 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
356 (hdr->oldlines == 0 && hdr->newlines == 0))
357 return got_error(GOT_ERR_PATCH_MALFORMED);
359 if (hdr->oldlines == 0) {
360 /* larry says to "do append rather than insert"; I don't
361 * quite get it, but i trust him.
362 */
363 hdr->oldfrom++;
366 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
367 hdr, sizeof(*hdr)) == -1)
368 return got_error_from_errno(
369 "imsg_compose GOT_IMSG_PATCH_HUNK");
370 return NULL;
373 static const struct got_error *
374 send_line(const char *line)
376 static const struct got_error *err = NULL;
377 char *p = NULL;
379 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
380 if (asprintf(&p, " %s", line) == -1)
381 return got_error_from_errno("asprintf");
382 line = p;
385 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
386 line, strlen(line) + 1) == -1)
387 err = got_error_from_errno(
388 "imsg_compose GOT_IMSG_PATCH_LINE");
390 free(p);
391 return err;
394 static const struct got_error *
395 peek_special_line(FILE *fp)
397 const struct got_error *err;
398 int ch;
400 ch = fgetc(fp);
401 if (ch != EOF && ch != '\\') {
402 ungetc(ch, fp);
403 return NULL;
406 if (ch == '\\') {
407 err = send_line("\\");
408 if (err)
409 return err;
412 while (ch != EOF && ch != '\n')
413 ch = fgetc(fp);
415 if (ch != EOF || feof(fp))
416 return NULL;
417 return got_error(GOT_ERR_IO);
420 static const struct got_error *
421 parse_hunk(FILE *fp, int *done)
423 static const struct got_error *err = NULL;
424 struct got_imsg_patch_hunk hdr;
425 char *line = NULL, ch;
426 size_t linesize = 0;
427 ssize_t linelen;
428 int leftold, leftnew;
430 linelen = getline(&line, &linesize, fp);
431 if (linelen == -1) {
432 *done = 1;
433 goto done;
436 err = parse_hdr(line, done, &hdr);
437 if (err)
438 goto done;
439 if (*done) {
440 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
441 err = got_error_from_errno("fseeko");
442 goto done;
445 leftold = hdr.oldlines;
446 leftnew = hdr.newlines;
448 while (leftold > 0 || leftnew > 0) {
449 linelen = getline(&line, &linesize, fp);
450 if (linelen == -1) {
451 if (ferror(fp)) {
452 err = got_error_from_errno("getline");
453 goto done;
456 /* trailing newlines may be chopped */
457 if (leftold < 3 && leftnew < 3) {
458 *done = 1;
459 break;
462 err = got_error(GOT_ERR_PATCH_TRUNCATED);
463 goto done;
465 if (line[linelen - 1] == '\n')
466 line[linelen - 1] = '\0';
468 /* usr.bin/patch allows '=' as context char */
469 if (*line == '=')
470 *line = ' ';
472 ch = *line;
473 if (ch == '\t' || ch == '\0')
474 ch = ' '; /* the space got eaten */
476 switch (ch) {
477 case '-':
478 leftold--;
479 break;
480 case ' ':
481 leftold--;
482 leftnew--;
483 break;
484 case '+':
485 leftnew--;
486 break;
487 default:
488 err = got_error(GOT_ERR_PATCH_MALFORMED);
489 goto done;
492 if (leftold < 0 || leftnew < 0) {
493 err = got_error(GOT_ERR_PATCH_MALFORMED);
494 goto done;
497 err = send_line(line);
498 if (err)
499 goto done;
501 if ((ch == '-' && leftold == 0) ||
502 (ch == '+' && leftnew == 0)) {
503 err = peek_special_line(fp);
504 if (err)
505 goto done;
509 done:
510 free(line);
511 return err;
514 static const struct got_error *
515 read_patch(struct imsgbuf *ibuf, int fd)
517 const struct got_error *err = NULL;
518 FILE *fp;
519 int git, patch_found = 0;
520 char *cid = NULL;
522 if ((fp = fdopen(fd, "r")) == NULL) {
523 err = got_error_from_errno("fdopen");
524 close(fd);
525 return err;
528 while ((err = patch_start(&git, &cid, fp)) == NULL) {
529 int done, next;
531 err = find_diff(&done, &next, fp, git, cid);
532 if (err)
533 goto done;
534 if (next)
535 continue;
537 patch_found = 1;
539 while (!done) {
540 err = parse_hunk(fp, &done);
541 if (err)
542 goto done;
545 err = send_patch_done();
546 if (err)
547 goto done;
550 done:
551 fclose(fp);
552 free(cid);
554 /* ignore trailing gibberish */
555 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
556 err = NULL;
558 return err;
561 int
562 main(int argc, char **argv)
564 const struct got_error *err = NULL;
565 struct imsg imsg;
566 #if 0
567 static int attached;
568 while (!attached)
569 sleep(1);
570 #endif
572 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
573 #ifndef PROFILE
574 /* revoke access to most system calls */
575 if (pledge("stdio recvfd", NULL) == -1) {
576 err = got_error_from_errno("pledge");
577 got_privsep_send_error(&ibuf, err);
578 return 1;
580 #endif
582 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
583 if (err)
584 goto done;
585 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
586 err = got_error(GOT_ERR_PRIVSEP_MSG);
587 goto done;
590 err = read_patch(&ibuf, imsg.fd);
591 if (err)
592 goto done;
593 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
594 NULL, 0) == -1) {
595 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
596 goto done;
598 err = got_privsep_flush_imsg(&ibuf);
599 done:
600 imsg_free(&imsg);
601 if (err != NULL) {
602 got_privsep_send_error(&ibuf, err);
603 err = NULL;
605 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
606 err = got_error_from_errno("close");
607 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
608 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
609 return err ? 1 : 0;