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"
59 struct imsgbuf ibuf;
61 static const struct got_error *
62 send_patch(const char *oldname, const char *newname, int git)
63 {
64 struct got_imsg_patch p;
66 memset(&p, 0, sizeof(p));
68 if (oldname != NULL)
69 strlcpy(p.old, oldname, sizeof(p.old));
71 if (newname != NULL)
72 strlcpy(p.new, newname, sizeof(p.new));
74 p.git = git;
75 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
76 &p, sizeof(p)) == -1)
77 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
78 return NULL;
79 }
81 static const struct got_error *
82 send_patch_done(void)
83 {
84 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
85 NULL, 0) == -1)
86 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
87 if (imsg_flush(&ibuf) == -1)
88 return got_error_from_errno("imsg_flush");
89 return NULL;
90 }
92 /* based on fetchname from usr.bin/patch/util.c */
93 static const struct got_error *
94 filename(const char *at, char **name)
95 {
96 char *tmp, *t;
98 *name = NULL;
99 if (*at == '\0')
100 return NULL;
102 while (isspace((unsigned char)*at))
103 at++;
105 /* files can be created or removed by diffing against /dev/null */
106 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
107 return NULL;
109 tmp = strdup(at);
110 if (tmp == NULL)
111 return got_error_from_errno("strdup");
112 if ((t = strchr(tmp, '\t')) != NULL)
113 *t = '\0';
114 if ((t = strchr(tmp, '\n')) != NULL)
115 *t = '\0';
117 *name = strdup(tmp);
118 free(tmp);
119 if (*name == NULL)
120 return got_error_from_errno("strdup");
121 return NULL;
124 static const struct got_error *
125 find_patch(int *done, FILE *fp)
127 const struct got_error *err = NULL;
128 char *old = NULL, *new = NULL;
129 char *line = NULL;
130 size_t linesize = 0;
131 ssize_t linelen;
132 int create, rename = 0, git = 0;
134 while ((linelen = getline(&line, &linesize, fp)) != -1) {
135 /*
136 * Ignore the Index name like GNU and larry' patch,
137 * we don't have to follow POSIX.
138 */
140 if (!strncmp(line, "--- ", 4)) {
141 free(old);
142 err = filename(line+4, &old);
143 } else if (rename && !strncmp(line, "rename from ", 12)) {
144 free(old);
145 err = filename(line+12, &old);
146 } else if (!strncmp(line, "+++ ", 4)) {
147 free(new);
148 err = filename(line+4, &new);
149 } else if (rename && !strncmp(line, "rename to ", 10)) {
150 free(new);
151 err = filename(line + 10, &new);
152 } else if (git && !strncmp(line, "similarity index 100%", 21))
153 rename = 1;
154 else if (!strncmp(line, "diff --git a/", 13))
155 git = 1;
157 if (err)
158 break;
160 /*
161 * Git-style diffs with "similarity index 100%" don't
162 * have any hunks and ends with the "rename to foobar"
163 * line.
164 */
165 if (rename && old != NULL && new != NULL) {
166 *done = 1;
167 err = send_patch(old, new, git);
168 break;
171 if (!strncmp(line, "@@ -", 4)) {
172 create = !strncmp(line+4, "0,0", 3);
173 if ((old == NULL && new == NULL) ||
174 (!create && old == NULL))
175 err = got_error(GOT_ERR_PATCH_MALFORMED);
176 else
177 err = send_patch(old, new, git);
179 if (err)
180 break;
182 /* rewind to previous line */
183 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
184 err = got_error_from_errno("fseek");
185 break;
189 free(old);
190 free(new);
191 free(line);
192 if (ferror(fp) && err == NULL)
193 err = got_error_from_errno("getline");
194 if (feof(fp) && err == NULL)
195 err = got_error(GOT_ERR_NO_PATCH);
196 return err;
199 static const struct got_error *
200 strtolnum(char **str, long *n)
202 char *p, c;
203 const char *errstr;
205 for (p = *str; isdigit((unsigned char)*p); ++p)
206 /* nop */;
208 c = *p;
209 *p = '\0';
211 *n = strtonum(*str, 0, LONG_MAX, &errstr);
212 if (errstr != NULL)
213 return got_error(GOT_ERR_PATCH_MALFORMED);
215 *p = c;
216 *str = p;
217 return NULL;
220 static const struct got_error *
221 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
223 static const struct got_error *err = NULL;
225 if (strncmp(s, "@@ -", 4)) {
226 *done = 1;
227 return NULL;
230 s += 4;
231 if (!*s)
232 return NULL;
233 err = strtolnum(&s, &hdr->oldfrom);
234 if (err)
235 return err;
236 if (*s == ',') {
237 s++;
238 err = strtolnum(&s, &hdr->oldlines);
239 if (err)
240 return err;
241 } else
242 hdr->oldlines = 1;
244 if (*s == ' ')
245 s++;
247 if (*s != '+' || !*++s)
248 return got_error(GOT_ERR_PATCH_MALFORMED);
249 err = strtolnum(&s, &hdr->newfrom);
250 if (err)
251 return err;
252 if (*s == ',') {
253 s++;
254 err = strtolnum(&s, &hdr->newlines);
255 if (err)
256 return err;
257 } else
258 hdr->newlines = 1;
260 if (*s == ' ')
261 s++;
263 if (*s != '@')
264 return got_error(GOT_ERR_PATCH_MALFORMED);
266 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
267 hdr->newfrom >= LONG_MAX - hdr->newlines ||
268 /* not so sure about this one */
269 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
270 return got_error(GOT_ERR_PATCH_MALFORMED);
272 if (hdr->oldlines == 0) {
273 /* larry says to "do append rather than insert"; I don't
274 * quite get it, but i trust him.
275 */
276 hdr->oldfrom++;
279 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
280 hdr, sizeof(*hdr)) == -1)
281 return got_error_from_errno(
282 "imsg_compose GOT_IMSG_PATCH_HUNK");
283 return NULL;
286 static const struct got_error *
287 send_line(const char *line)
289 static const struct got_error *err = NULL;
290 char *p = NULL;
292 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
293 if (asprintf(&p, " %s", line) == -1)
294 return got_error_from_errno("asprintf");
295 line = p;
298 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
299 line, strlen(line) + 1) == -1)
300 err = got_error_from_errno(
301 "imsg_compose GOT_IMSG_PATCH_LINE");
303 free(p);
304 return err;
307 static const struct got_error *
308 peek_special_line(FILE *fp)
310 const struct got_error *err;
311 int ch;
313 ch = fgetc(fp);
314 if (ch != EOF && ch != '\\') {
315 ungetc(ch, fp);
316 return NULL;
319 if (ch == '\\') {
320 err = send_line("\\");
321 if (err)
322 return err;
325 while (ch != EOF && ch != '\n')
326 ch = fgetc(fp);
328 if (ch != EOF || feof(fp))
329 return NULL;
330 return got_error(GOT_ERR_IO);
333 static const struct got_error *
334 parse_hunk(FILE *fp, int *done)
336 static const struct got_error *err = NULL;
337 struct got_imsg_patch_hunk hdr;
338 char *line = NULL, ch;
339 size_t linesize = 0;
340 ssize_t linelen;
341 long leftold, leftnew;
343 linelen = getline(&line, &linesize, fp);
344 if (linelen == -1) {
345 *done = 1;
346 goto done;
349 err = parse_hdr(line, done, &hdr);
350 if (err)
351 goto done;
352 if (*done) {
353 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
354 err = got_error_from_errno("fseek");
355 goto done;
358 leftold = hdr.oldlines;
359 leftnew = hdr.newlines;
361 while (leftold > 0 || leftnew > 0) {
362 linelen = getline(&line, &linesize, fp);
363 if (linelen == -1) {
364 if (ferror(fp)) {
365 err = got_error_from_errno("getline");
366 goto done;
369 /* trailing newlines may be chopped */
370 if (leftold < 3 && leftnew < 3) {
371 *done = 1;
372 break;
375 err = got_error(GOT_ERR_PATCH_TRUNCATED);
376 goto done;
378 if (line[linelen - 1] == '\n')
379 line[linelen - 1] = '\0';
381 /* usr.bin/patch allows '=' as context char */
382 if (*line == '=')
383 *line = ' ';
385 ch = *line;
386 if (ch == '\t' || ch == '\0')
387 ch = ' '; /* the space got eaten */
389 switch (ch) {
390 case '-':
391 leftold--;
392 break;
393 case ' ':
394 leftold--;
395 leftnew--;
396 break;
397 case '+':
398 leftnew--;
399 break;
400 default:
401 err = got_error(GOT_ERR_PATCH_MALFORMED);
402 goto done;
405 if (leftold < 0 || leftnew < 0) {
406 err = got_error(GOT_ERR_PATCH_MALFORMED);
407 goto done;
410 err = send_line(line);
411 if (err)
412 goto done;
414 if ((ch == '-' && leftold == 0) ||
415 (ch == '+' && leftnew == 0)) {
416 err = peek_special_line(fp);
417 if (err)
418 goto done;
422 done:
423 free(line);
424 return err;
427 static const struct got_error *
428 read_patch(struct imsgbuf *ibuf, int fd)
430 const struct got_error *err = NULL;
431 FILE *fp;
432 int patch_found = 0;
434 if ((fp = fdopen(fd, "r")) == NULL) {
435 err = got_error_from_errno("fdopen");
436 close(fd);
437 return err;
440 while (!feof(fp)) {
441 int done = 0;
443 err = find_patch(&done, fp);
444 if (err)
445 goto done;
447 patch_found = 1;
449 while (!done) {
450 err = parse_hunk(fp, &done);
451 if (err)
452 goto done;
455 err = send_patch_done();
456 if (err)
457 goto done;
460 done:
461 fclose(fp);
463 /* ignore trailing gibberish */
464 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
465 err = NULL;
467 return err;
470 int
471 main(int argc, char **argv)
473 const struct got_error *err = NULL;
474 struct imsg imsg;
475 #if 0
476 static int attached;
477 while (!attached)
478 sleep(1);
479 #endif
481 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
482 #ifndef PROFILE
483 /* revoke access to most system calls */
484 if (pledge("stdio recvfd", NULL) == -1) {
485 err = got_error_from_errno("pledge");
486 got_privsep_send_error(&ibuf, err);
487 return 1;
490 /* revoke fs access */
491 if (landlock_no_fs() == -1) {
492 err = got_error_from_errno("landlock_no_fs");
493 got_privsep_send_error(&ibuf, err);
494 return 1;
496 #endif
498 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
499 if (err)
500 goto done;
501 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
502 err = got_error(GOT_ERR_PRIVSEP_MSG);
503 goto done;
506 err = read_patch(&ibuf, imsg.fd);
507 if (err)
508 goto done;
509 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
510 NULL, 0) == -1) {
511 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
512 goto done;
514 err = got_privsep_flush_imsg(&ibuf);
515 done:
516 imsg_free(&imsg);
517 if (err != NULL) {
518 got_privsep_send_error(&ibuf, err);
519 err = NULL;
521 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
522 err = got_error_from_errno("close");
523 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
524 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
525 return err ? 1 : 0;