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"
60 struct imsgbuf ibuf;
62 static const struct got_error *
63 send_patch(const char *oldname, const char *newname, int git)
64 {
65 struct got_imsg_patch p;
67 memset(&p, 0, sizeof(p));
69 if (oldname != NULL)
70 strlcpy(p.old, oldname, sizeof(p.old));
72 if (newname != NULL)
73 strlcpy(p.new, newname, sizeof(p.new));
75 p.git = git;
76 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
77 &p, sizeof(p)) == -1)
78 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
79 return NULL;
80 }
82 static const struct got_error *
83 send_patch_done(void)
84 {
85 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
86 NULL, 0) == -1)
87 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
88 if (imsg_flush(&ibuf) == -1)
89 return got_error_from_errno("imsg_flush");
90 return NULL;
91 }
93 /* based on fetchname from usr.bin/patch/util.c */
94 static const struct got_error *
95 filename(const char *at, char **name)
96 {
97 char *tmp, *t;
99 *name = NULL;
100 if (*at == '\0')
101 return NULL;
103 while (isspace((unsigned char)*at))
104 at++;
106 /* files can be created or removed by diffing against /dev/null */
107 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
108 return NULL;
110 tmp = strdup(at);
111 if (tmp == NULL)
112 return got_error_from_errno("strdup");
113 if ((t = strchr(tmp, '\t')) != NULL)
114 *t = '\0';
115 if ((t = strchr(tmp, '\n')) != NULL)
116 *t = '\0';
118 *name = strdup(tmp);
119 free(tmp);
120 if (*name == NULL)
121 return got_error_from_errno("strdup");
122 return NULL;
125 static const struct got_error *
126 find_patch(int *done, FILE *fp)
128 const struct got_error *err = NULL;
129 char *old = NULL, *new = NULL;
130 char *line = NULL;
131 size_t linesize = 0;
132 ssize_t linelen;
133 int create, rename = 0, git = 0;
135 while ((linelen = getline(&line, &linesize, fp)) != -1) {
136 /*
137 * Ignore the Index name like GNU and larry' patch,
138 * we don't have to follow POSIX.
139 */
141 if (!strncmp(line, "--- ", 4)) {
142 free(old);
143 err = filename(line+4, &old);
144 } else if (rename && !strncmp(line, "rename from ", 12)) {
145 free(old);
146 err = filename(line+12, &old);
147 } else if (!strncmp(line, "+++ ", 4)) {
148 free(new);
149 err = filename(line+4, &new);
150 } else if (rename && !strncmp(line, "rename to ", 10)) {
151 free(new);
152 err = filename(line + 10, &new);
153 } else if (git && !strncmp(line, "similarity index 100%", 21))
154 rename = 1;
155 else if (!strncmp(line, "diff --git a/", 13))
156 git = 1;
158 if (err)
159 break;
161 /*
162 * Git-style diffs with "similarity index 100%" don't
163 * have any hunks and ends with the "rename to foobar"
164 * line.
165 */
166 if (rename && old != NULL && new != NULL) {
167 *done = 1;
168 err = send_patch(old, new, git);
169 break;
172 if (!strncmp(line, "@@ -", 4)) {
173 create = !strncmp(line+4, "0,0", 3);
174 if ((old == NULL && new == NULL) ||
175 (!create && old == NULL))
176 err = got_error(GOT_ERR_PATCH_MALFORMED);
177 else
178 err = send_patch(old, new, git);
180 if (err)
181 break;
183 /* rewind to previous line */
184 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
185 err = got_error_from_errno("fseeko");
186 break;
190 free(old);
191 free(new);
192 free(line);
193 if (ferror(fp) && err == NULL)
194 err = got_error_from_errno("getline");
195 if (feof(fp) && err == NULL)
196 err = got_error(GOT_ERR_NO_PATCH);
197 return err;
200 static const struct got_error *
201 strtolnum(char **str, long *n)
203 char *p, c;
204 const char *errstr;
206 for (p = *str; isdigit((unsigned char)*p); ++p)
207 /* nop */;
209 c = *p;
210 *p = '\0';
212 *n = strtonum(*str, 0, LONG_MAX, &errstr);
213 if (errstr != NULL)
214 return got_error(GOT_ERR_PATCH_MALFORMED);
216 *p = c;
217 *str = p;
218 return NULL;
221 static const struct got_error *
222 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
224 static const struct got_error *err = NULL;
226 if (strncmp(s, "@@ -", 4)) {
227 *done = 1;
228 return NULL;
231 s += 4;
232 if (!*s)
233 return NULL;
234 err = strtolnum(&s, &hdr->oldfrom);
235 if (err)
236 return err;
237 if (*s == ',') {
238 s++;
239 err = strtolnum(&s, &hdr->oldlines);
240 if (err)
241 return err;
242 } else
243 hdr->oldlines = 1;
245 if (*s == ' ')
246 s++;
248 if (*s != '+' || !*++s)
249 return got_error(GOT_ERR_PATCH_MALFORMED);
250 err = strtolnum(&s, &hdr->newfrom);
251 if (err)
252 return err;
253 if (*s == ',') {
254 s++;
255 err = strtolnum(&s, &hdr->newlines);
256 if (err)
257 return err;
258 } else
259 hdr->newlines = 1;
261 if (*s == ' ')
262 s++;
264 if (*s != '@')
265 return got_error(GOT_ERR_PATCH_MALFORMED);
267 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
268 hdr->newfrom >= LONG_MAX - hdr->newlines ||
269 /* not so sure about this one */
270 hdr->oldlines >= LONG_MAX - hdr->newlines - 1 ||
271 (hdr->oldlines == 0 && hdr->newlines == 0))
272 return got_error(GOT_ERR_PATCH_MALFORMED);
274 if (hdr->oldlines == 0) {
275 /* larry says to "do append rather than insert"; I don't
276 * quite get it, but i trust him.
277 */
278 hdr->oldfrom++;
281 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
282 hdr, sizeof(*hdr)) == -1)
283 return got_error_from_errno(
284 "imsg_compose GOT_IMSG_PATCH_HUNK");
285 return NULL;
288 static const struct got_error *
289 send_line(const char *line)
291 static const struct got_error *err = NULL;
292 char *p = NULL;
294 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
295 if (asprintf(&p, " %s", line) == -1)
296 return got_error_from_errno("asprintf");
297 line = p;
300 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
301 line, strlen(line) + 1) == -1)
302 err = got_error_from_errno(
303 "imsg_compose GOT_IMSG_PATCH_LINE");
305 free(p);
306 return err;
309 static const struct got_error *
310 peek_special_line(FILE *fp)
312 const struct got_error *err;
313 int ch;
315 ch = fgetc(fp);
316 if (ch != EOF && ch != '\\') {
317 ungetc(ch, fp);
318 return NULL;
321 if (ch == '\\') {
322 err = send_line("\\");
323 if (err)
324 return err;
327 while (ch != EOF && ch != '\n')
328 ch = fgetc(fp);
330 if (ch != EOF || feof(fp))
331 return NULL;
332 return got_error(GOT_ERR_IO);
335 static const struct got_error *
336 parse_hunk(FILE *fp, int *done)
338 static const struct got_error *err = NULL;
339 struct got_imsg_patch_hunk hdr;
340 char *line = NULL, ch;
341 size_t linesize = 0;
342 ssize_t linelen;
343 long leftold, leftnew;
345 linelen = getline(&line, &linesize, fp);
346 if (linelen == -1) {
347 *done = 1;
348 goto done;
351 err = parse_hdr(line, done, &hdr);
352 if (err)
353 goto done;
354 if (*done) {
355 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
356 err = got_error_from_errno("fseeko");
357 goto done;
360 leftold = hdr.oldlines;
361 leftnew = hdr.newlines;
363 while (leftold > 0 || leftnew > 0) {
364 linelen = getline(&line, &linesize, fp);
365 if (linelen == -1) {
366 if (ferror(fp)) {
367 err = got_error_from_errno("getline");
368 goto done;
371 /* trailing newlines may be chopped */
372 if (leftold < 3 && leftnew < 3) {
373 *done = 1;
374 break;
377 err = got_error(GOT_ERR_PATCH_TRUNCATED);
378 goto done;
380 if (line[linelen - 1] == '\n')
381 line[linelen - 1] = '\0';
383 /* usr.bin/patch allows '=' as context char */
384 if (*line == '=')
385 *line = ' ';
387 ch = *line;
388 if (ch == '\t' || ch == '\0')
389 ch = ' '; /* the space got eaten */
391 switch (ch) {
392 case '-':
393 leftold--;
394 break;
395 case ' ':
396 leftold--;
397 leftnew--;
398 break;
399 case '+':
400 leftnew--;
401 break;
402 default:
403 err = got_error(GOT_ERR_PATCH_MALFORMED);
404 goto done;
407 if (leftold < 0 || leftnew < 0) {
408 err = got_error(GOT_ERR_PATCH_MALFORMED);
409 goto done;
412 err = send_line(line);
413 if (err)
414 goto done;
416 if ((ch == '-' && leftold == 0) ||
417 (ch == '+' && leftnew == 0)) {
418 err = peek_special_line(fp);
419 if (err)
420 goto done;
424 done:
425 free(line);
426 return err;
429 static const struct got_error *
430 read_patch(struct imsgbuf *ibuf, int fd)
432 const struct got_error *err = NULL;
433 FILE *fp;
434 int patch_found = 0;
436 if ((fp = fdopen(fd, "r")) == NULL) {
437 err = got_error_from_errno("fdopen");
438 close(fd);
439 return err;
442 while (!feof(fp)) {
443 int done = 0;
445 err = find_patch(&done, fp);
446 if (err)
447 goto done;
449 patch_found = 1;
451 while (!done) {
452 err = parse_hunk(fp, &done);
453 if (err)
454 goto done;
457 err = send_patch_done();
458 if (err)
459 goto done;
462 done:
463 fclose(fp);
465 /* ignore trailing gibberish */
466 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
467 err = NULL;
469 return err;
472 int
473 main(int argc, char **argv)
475 const struct got_error *err = NULL;
476 struct imsg imsg;
477 #if 0
478 static int attached;
479 while (!attached)
480 sleep(1);
481 #endif
483 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
484 #ifndef PROFILE
485 /* revoke access to most system calls */
486 if (pledge("stdio recvfd", NULL) == -1) {
487 err = got_error_from_errno("pledge");
488 got_privsep_send_error(&ibuf, err);
489 return 1;
491 #endif
493 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
494 if (err)
495 goto done;
496 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
497 err = got_error(GOT_ERR_PRIVSEP_MSG);
498 goto done;
501 err = read_patch(&ibuf, imsg.fd);
502 if (err)
503 goto done;
504 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
505 NULL, 0) == -1) {
506 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
507 goto done;
509 err = got_privsep_flush_imsg(&ibuf);
510 done:
511 imsg_free(&imsg);
512 if (err != NULL) {
513 got_privsep_send_error(&ibuf, err);
514 err = NULL;
516 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
517 err = got_error_from_errno("close");
518 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
519 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
520 return err ? 1 : 0;