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 && blob != NULL) {
77 strlcpy(p.cid, commitid, sizeof(p.cid));
78 strlcpy(p.blob, blob, sizeof(p.blob));
79 }
81 p.git = git;
82 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
83 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
84 return NULL;
85 }
87 static const struct got_error *
88 send_patch_done(void)
89 {
90 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
91 NULL, 0) == -1)
92 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
93 if (imsg_flush(&ibuf) == -1)
94 return got_error_from_errno("imsg_flush");
95 return NULL;
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)
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 (!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 find_patch(int *done, FILE *fp)
153 const struct got_error *err = NULL;
154 char *old = NULL, *new = NULL;
155 char *commitid = NULL, *blob = NULL;
156 char *line = NULL;
157 size_t linesize = 0;
158 ssize_t linelen;
159 int create, rename = 0, git = 0;
161 while ((linelen = getline(&line, &linesize, fp)) != -1) {
162 /*
163 * Ignore the Index name like GNU and larry' patch,
164 * we don't have to follow POSIX.
165 */
167 if (!strncmp(line, "--- ", 4)) {
168 free(old);
169 err = filename(line+4, &old);
170 } else if (rename && !strncmp(line, "rename from ", 12)) {
171 free(old);
172 err = filename(line+12, &old);
173 } else if (!strncmp(line, "+++ ", 4)) {
174 free(new);
175 err = filename(line+4, &new);
176 } else if (!git && !strncmp(line, "blob - ", 7)) {
177 free(blob);
178 err = blobid(line + 7, &blob);
179 } else if (rename && !strncmp(line, "rename to ", 10)) {
180 free(new);
181 err = filename(line + 10, &new);
182 } else if (git && !strncmp(line, "similarity index 100%", 21))
183 rename = 1;
184 else if (!strncmp(line, "diff --git a/", 13)) {
185 git = 1;
186 free(commitid);
187 commitid = NULL;
188 free(blob);
189 blob = NULL;
190 } else if (!git && !strncmp(line, "diff ", 5)) {
191 free(commitid);
192 err = blobid(line + 5, &commitid);
195 if (err)
196 break;
198 /*
199 * Git-style diffs with "similarity index 100%" don't
200 * have any hunks and ends with the "rename to foobar"
201 * line.
202 */
203 if (rename && old != NULL && new != NULL) {
204 *done = 1;
205 err = send_patch(old, new, commitid,
206 blob, git);
207 break;
210 if (!strncmp(line, "@@ -", 4)) {
211 create = !strncmp(line+4, "0,0", 3);
212 if ((old == NULL && new == NULL) ||
213 (!create && old == NULL))
214 err = got_error(GOT_ERR_PATCH_MALFORMED);
215 else
216 err = send_patch(old, new, commitid,
217 blob, git);
219 if (err)
220 break;
222 /* rewind to previous line */
223 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
224 err = got_error_from_errno("fseeko");
225 break;
229 free(old);
230 free(new);
231 free(commitid);
232 free(blob);
233 free(line);
234 if (ferror(fp) && err == NULL)
235 err = got_error_from_errno("getline");
236 if (feof(fp) && err == NULL)
237 err = got_error(GOT_ERR_NO_PATCH);
238 return err;
241 static const struct got_error *
242 strtolnum(char **str, int *n)
244 char *p, c;
245 const char *errstr;
247 for (p = *str; isdigit((unsigned char)*p); ++p)
248 /* nop */;
250 c = *p;
251 *p = '\0';
253 *n = strtonum(*str, 0, INT_MAX, &errstr);
254 if (errstr != NULL)
255 return got_error(GOT_ERR_PATCH_MALFORMED);
257 *p = c;
258 *str = p;
259 return NULL;
262 static const struct got_error *
263 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
265 static const struct got_error *err = NULL;
267 if (strncmp(s, "@@ -", 4)) {
268 *done = 1;
269 return NULL;
272 s += 4;
273 if (!*s)
274 return NULL;
275 err = strtolnum(&s, &hdr->oldfrom);
276 if (err)
277 return err;
278 if (*s == ',') {
279 s++;
280 err = strtolnum(&s, &hdr->oldlines);
281 if (err)
282 return err;
283 } else
284 hdr->oldlines = 1;
286 if (*s == ' ')
287 s++;
289 if (*s != '+' || !*++s)
290 return got_error(GOT_ERR_PATCH_MALFORMED);
291 err = strtolnum(&s, &hdr->newfrom);
292 if (err)
293 return err;
294 if (*s == ',') {
295 s++;
296 err = strtolnum(&s, &hdr->newlines);
297 if (err)
298 return err;
299 } else
300 hdr->newlines = 1;
302 if (*s == ' ')
303 s++;
305 if (*s != '@')
306 return got_error(GOT_ERR_PATCH_MALFORMED);
308 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
309 hdr->newfrom >= INT_MAX - hdr->newlines ||
310 /* not so sure about this one */
311 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
312 (hdr->oldlines == 0 && hdr->newlines == 0))
313 return got_error(GOT_ERR_PATCH_MALFORMED);
315 if (hdr->oldlines == 0) {
316 /* larry says to "do append rather than insert"; I don't
317 * quite get it, but i trust him.
318 */
319 hdr->oldfrom++;
322 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
323 hdr, sizeof(*hdr)) == -1)
324 return got_error_from_errno(
325 "imsg_compose GOT_IMSG_PATCH_HUNK");
326 return NULL;
329 static const struct got_error *
330 send_line(const char *line)
332 static const struct got_error *err = NULL;
333 char *p = NULL;
335 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
336 if (asprintf(&p, " %s", line) == -1)
337 return got_error_from_errno("asprintf");
338 line = p;
341 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
342 line, strlen(line) + 1) == -1)
343 err = got_error_from_errno(
344 "imsg_compose GOT_IMSG_PATCH_LINE");
346 free(p);
347 return err;
350 static const struct got_error *
351 peek_special_line(FILE *fp)
353 const struct got_error *err;
354 int ch;
356 ch = fgetc(fp);
357 if (ch != EOF && ch != '\\') {
358 ungetc(ch, fp);
359 return NULL;
362 if (ch == '\\') {
363 err = send_line("\\");
364 if (err)
365 return err;
368 while (ch != EOF && ch != '\n')
369 ch = fgetc(fp);
371 if (ch != EOF || feof(fp))
372 return NULL;
373 return got_error(GOT_ERR_IO);
376 static const struct got_error *
377 parse_hunk(FILE *fp, int *done)
379 static const struct got_error *err = NULL;
380 struct got_imsg_patch_hunk hdr;
381 char *line = NULL, ch;
382 size_t linesize = 0;
383 ssize_t linelen;
384 int leftold, leftnew;
386 linelen = getline(&line, &linesize, fp);
387 if (linelen == -1) {
388 *done = 1;
389 goto done;
392 err = parse_hdr(line, done, &hdr);
393 if (err)
394 goto done;
395 if (*done) {
396 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
397 err = got_error_from_errno("fseeko");
398 goto done;
401 leftold = hdr.oldlines;
402 leftnew = hdr.newlines;
404 while (leftold > 0 || leftnew > 0) {
405 linelen = getline(&line, &linesize, fp);
406 if (linelen == -1) {
407 if (ferror(fp)) {
408 err = got_error_from_errno("getline");
409 goto done;
412 /* trailing newlines may be chopped */
413 if (leftold < 3 && leftnew < 3) {
414 *done = 1;
415 break;
418 err = got_error(GOT_ERR_PATCH_TRUNCATED);
419 goto done;
421 if (line[linelen - 1] == '\n')
422 line[linelen - 1] = '\0';
424 /* usr.bin/patch allows '=' as context char */
425 if (*line == '=')
426 *line = ' ';
428 ch = *line;
429 if (ch == '\t' || ch == '\0')
430 ch = ' '; /* the space got eaten */
432 switch (ch) {
433 case '-':
434 leftold--;
435 break;
436 case ' ':
437 leftold--;
438 leftnew--;
439 break;
440 case '+':
441 leftnew--;
442 break;
443 default:
444 err = got_error(GOT_ERR_PATCH_MALFORMED);
445 goto done;
448 if (leftold < 0 || leftnew < 0) {
449 err = got_error(GOT_ERR_PATCH_MALFORMED);
450 goto done;
453 err = send_line(line);
454 if (err)
455 goto done;
457 if ((ch == '-' && leftold == 0) ||
458 (ch == '+' && leftnew == 0)) {
459 err = peek_special_line(fp);
460 if (err)
461 goto done;
465 done:
466 free(line);
467 return err;
470 static const struct got_error *
471 read_patch(struct imsgbuf *ibuf, int fd)
473 const struct got_error *err = NULL;
474 FILE *fp;
475 int patch_found = 0;
477 if ((fp = fdopen(fd, "r")) == NULL) {
478 err = got_error_from_errno("fdopen");
479 close(fd);
480 return err;
483 while (!feof(fp)) {
484 int done = 0;
486 err = find_patch(&done, fp);
487 if (err)
488 goto done;
490 patch_found = 1;
492 while (!done) {
493 err = parse_hunk(fp, &done);
494 if (err)
495 goto done;
498 err = send_patch_done();
499 if (err)
500 goto done;
503 done:
504 fclose(fp);
506 /* ignore trailing gibberish */
507 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
508 err = NULL;
510 return err;
513 int
514 main(int argc, char **argv)
516 const struct got_error *err = NULL;
517 struct imsg imsg;
518 #if 0
519 static int attached;
520 while (!attached)
521 sleep(1);
522 #endif
524 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
525 #ifndef PROFILE
526 /* revoke access to most system calls */
527 if (pledge("stdio recvfd", NULL) == -1) {
528 err = got_error_from_errno("pledge");
529 got_privsep_send_error(&ibuf, err);
530 return 1;
533 /* revoke fs access */
534 if (landlock_no_fs() == -1) {
535 err = got_error_from_errno("landlock_no_fs");
536 got_privsep_send_error(&ibuf, err);
537 return 1;
539 #endif
541 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
542 if (err)
543 goto done;
544 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
545 err = got_error(GOT_ERR_PRIVSEP_MSG);
546 goto done;
549 err = read_patch(&ibuf, imsg.fd);
550 if (err)
551 goto done;
552 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
553 NULL, 0) == -1) {
554 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
555 goto done;
557 err = got_privsep_flush_imsg(&ibuf);
558 done:
559 imsg_free(&imsg);
560 if (err != NULL) {
561 got_privsep_send_error(&ibuf, err);
562 err = NULL;
564 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
565 err = got_error_from_errno("close");
566 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
567 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
568 return err ? 1 : 0;