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 find_patch(int *done, FILE *fp)
155 const struct got_error *err = NULL;
156 char *old = NULL, *new = NULL;
157 char *commitid = NULL, *blob = NULL;
158 char *line = NULL;
159 size_t linesize = 0;
160 ssize_t linelen;
161 int create, rename = 0, git = 0;
163 while ((linelen = getline(&line, &linesize, fp)) != -1) {
164 /*
165 * Ignore the Index name like GNU and larry' patch,
166 * we don't have to follow POSIX.
167 */
169 if (!strncmp(line, "--- ", 4)) {
170 free(old);
171 err = filename(line+4, &old);
172 } else if (rename && !strncmp(line, "rename from ", 12)) {
173 free(old);
174 err = filename(line+12, &old);
175 } else if (!strncmp(line, "+++ ", 4)) {
176 free(new);
177 err = filename(line+4, &new);
178 } else if (!git && !strncmp(line, "blob - ", 7)) {
179 free(blob);
180 err = blobid(line + 7, &blob, git);
181 } else if (rename && !strncmp(line, "rename to ", 10)) {
182 free(new);
183 err = filename(line + 10, &new);
184 } else if (git && !strncmp(line, "similarity index 100%", 21))
185 rename = 1;
186 else if (git && !strncmp(line, "index ", 6)) {
187 free(blob);
188 err = blobid(line + 6, &blob, git);
189 } else if (!strncmp(line, "diff --git a/", 13)) {
190 git = 1;
191 free(commitid);
192 commitid = NULL;
193 free(blob);
194 blob = NULL;
195 } else if (!git && !strncmp(line, "diff ", 5)) {
196 free(commitid);
197 err = blobid(line + 5, &commitid, git);
198 } else if (!git && !strncmp(line, "commit - ", 9)) {
199 free(commitid);
200 err = blobid(line + 9, &commitid, git);
203 if (err)
204 break;
206 /*
207 * Git-style diffs with "similarity index 100%" don't
208 * have any hunks and ends with the "rename to foobar"
209 * line.
210 */
211 if (rename && old != NULL && new != NULL) {
212 *done = 1;
213 err = send_patch(old, new, commitid,
214 blob, git);
215 break;
218 if (!strncmp(line, "@@ -", 4)) {
219 create = !strncmp(line+4, "0,0", 3);
220 if ((old == NULL && new == NULL) ||
221 (!create && old == NULL))
222 err = got_error(GOT_ERR_PATCH_MALFORMED);
223 else
224 err = send_patch(old, new, commitid,
225 blob, git);
227 if (err)
228 break;
230 /* rewind to previous line */
231 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
232 err = got_error_from_errno("fseeko");
233 break;
237 free(old);
238 free(new);
239 free(commitid);
240 free(blob);
241 free(line);
242 if (ferror(fp) && err == NULL)
243 err = got_error_from_errno("getline");
244 if (feof(fp) && err == NULL)
245 err = got_error(GOT_ERR_NO_PATCH);
246 return err;
249 static const struct got_error *
250 strtolnum(char **str, int *n)
252 char *p, c;
253 const char *errstr;
255 for (p = *str; isdigit((unsigned char)*p); ++p)
256 /* nop */;
258 c = *p;
259 *p = '\0';
261 *n = strtonum(*str, 0, INT_MAX, &errstr);
262 if (errstr != NULL)
263 return got_error(GOT_ERR_PATCH_MALFORMED);
265 *p = c;
266 *str = p;
267 return NULL;
270 static const struct got_error *
271 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
273 static const struct got_error *err = NULL;
275 if (strncmp(s, "@@ -", 4)) {
276 *done = 1;
277 return NULL;
280 s += 4;
281 if (!*s)
282 return NULL;
283 err = strtolnum(&s, &hdr->oldfrom);
284 if (err)
285 return err;
286 if (*s == ',') {
287 s++;
288 err = strtolnum(&s, &hdr->oldlines);
289 if (err)
290 return err;
291 } else
292 hdr->oldlines = 1;
294 if (*s == ' ')
295 s++;
297 if (*s != '+' || !*++s)
298 return got_error(GOT_ERR_PATCH_MALFORMED);
299 err = strtolnum(&s, &hdr->newfrom);
300 if (err)
301 return err;
302 if (*s == ',') {
303 s++;
304 err = strtolnum(&s, &hdr->newlines);
305 if (err)
306 return err;
307 } else
308 hdr->newlines = 1;
310 if (*s == ' ')
311 s++;
313 if (*s != '@')
314 return got_error(GOT_ERR_PATCH_MALFORMED);
316 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
317 hdr->newfrom >= INT_MAX - hdr->newlines ||
318 /* not so sure about this one */
319 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
320 (hdr->oldlines == 0 && hdr->newlines == 0))
321 return got_error(GOT_ERR_PATCH_MALFORMED);
323 if (hdr->oldlines == 0) {
324 /* larry says to "do append rather than insert"; I don't
325 * quite get it, but i trust him.
326 */
327 hdr->oldfrom++;
330 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
331 hdr, sizeof(*hdr)) == -1)
332 return got_error_from_errno(
333 "imsg_compose GOT_IMSG_PATCH_HUNK");
334 return NULL;
337 static const struct got_error *
338 send_line(const char *line)
340 static const struct got_error *err = NULL;
341 char *p = NULL;
343 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
344 if (asprintf(&p, " %s", line) == -1)
345 return got_error_from_errno("asprintf");
346 line = p;
349 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
350 line, strlen(line) + 1) == -1)
351 err = got_error_from_errno(
352 "imsg_compose GOT_IMSG_PATCH_LINE");
354 free(p);
355 return err;
358 static const struct got_error *
359 peek_special_line(FILE *fp)
361 const struct got_error *err;
362 int ch;
364 ch = fgetc(fp);
365 if (ch != EOF && ch != '\\') {
366 ungetc(ch, fp);
367 return NULL;
370 if (ch == '\\') {
371 err = send_line("\\");
372 if (err)
373 return err;
376 while (ch != EOF && ch != '\n')
377 ch = fgetc(fp);
379 if (ch != EOF || feof(fp))
380 return NULL;
381 return got_error(GOT_ERR_IO);
384 static const struct got_error *
385 parse_hunk(FILE *fp, int *done)
387 static const struct got_error *err = NULL;
388 struct got_imsg_patch_hunk hdr;
389 char *line = NULL, ch;
390 size_t linesize = 0;
391 ssize_t linelen;
392 int leftold, leftnew;
394 linelen = getline(&line, &linesize, fp);
395 if (linelen == -1) {
396 *done = 1;
397 goto done;
400 err = parse_hdr(line, done, &hdr);
401 if (err)
402 goto done;
403 if (*done) {
404 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
405 err = got_error_from_errno("fseeko");
406 goto done;
409 leftold = hdr.oldlines;
410 leftnew = hdr.newlines;
412 while (leftold > 0 || leftnew > 0) {
413 linelen = getline(&line, &linesize, fp);
414 if (linelen == -1) {
415 if (ferror(fp)) {
416 err = got_error_from_errno("getline");
417 goto done;
420 /* trailing newlines may be chopped */
421 if (leftold < 3 && leftnew < 3) {
422 *done = 1;
423 break;
426 err = got_error(GOT_ERR_PATCH_TRUNCATED);
427 goto done;
429 if (line[linelen - 1] == '\n')
430 line[linelen - 1] = '\0';
432 /* usr.bin/patch allows '=' as context char */
433 if (*line == '=')
434 *line = ' ';
436 ch = *line;
437 if (ch == '\t' || ch == '\0')
438 ch = ' '; /* the space got eaten */
440 switch (ch) {
441 case '-':
442 leftold--;
443 break;
444 case ' ':
445 leftold--;
446 leftnew--;
447 break;
448 case '+':
449 leftnew--;
450 break;
451 default:
452 err = got_error(GOT_ERR_PATCH_MALFORMED);
453 goto done;
456 if (leftold < 0 || leftnew < 0) {
457 err = got_error(GOT_ERR_PATCH_MALFORMED);
458 goto done;
461 err = send_line(line);
462 if (err)
463 goto done;
465 if ((ch == '-' && leftold == 0) ||
466 (ch == '+' && leftnew == 0)) {
467 err = peek_special_line(fp);
468 if (err)
469 goto done;
473 done:
474 free(line);
475 return err;
478 static const struct got_error *
479 read_patch(struct imsgbuf *ibuf, int fd)
481 const struct got_error *err = NULL;
482 FILE *fp;
483 int patch_found = 0;
485 if ((fp = fdopen(fd, "r")) == NULL) {
486 err = got_error_from_errno("fdopen");
487 close(fd);
488 return err;
491 while (!feof(fp)) {
492 int done = 0;
494 err = find_patch(&done, fp);
495 if (err)
496 goto done;
498 patch_found = 1;
500 while (!done) {
501 err = parse_hunk(fp, &done);
502 if (err)
503 goto done;
506 err = send_patch_done();
507 if (err)
508 goto done;
511 done:
512 fclose(fp);
514 /* ignore trailing gibberish */
515 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
516 err = NULL;
518 return err;
521 int
522 main(int argc, char **argv)
524 const struct got_error *err = NULL;
525 struct imsg imsg;
526 #if 0
527 static int attached;
528 while (!attached)
529 sleep(1);
530 #endif
532 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
533 #ifndef PROFILE
534 /* revoke access to most system calls */
535 if (pledge("stdio recvfd", NULL) == -1) {
536 err = got_error_from_errno("pledge");
537 got_privsep_send_error(&ibuf, err);
538 return 1;
540 #endif
542 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
543 if (err)
544 goto done;
545 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
546 err = got_error(GOT_ERR_PRIVSEP_MSG);
547 goto done;
550 err = read_patch(&ibuf, imsg.fd);
551 if (err)
552 goto done;
553 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
554 NULL, 0) == -1) {
555 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
556 goto done;
558 err = got_privsep_flush_imsg(&ibuf);
559 done:
560 imsg_free(&imsg);
561 if (err != NULL) {
562 got_privsep_send_error(&ibuf, err);
563 err = NULL;
565 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
566 err = got_error_from_errno("close");
567 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
568 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
569 return err ? 1 : 0;