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 /*
70 * Prefer the new name if it's not /dev/null and it's not
71 * a git-style diff.
72 */
73 if (!git && newname != NULL && oldname != NULL)
74 strlcpy(p.old, newname, sizeof(p.old));
75 else if (oldname != NULL)
76 strlcpy(p.old, oldname, sizeof(p.old));
78 if (newname != NULL)
79 strlcpy(p.new, newname, sizeof(p.new));
81 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
82 &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, int strip)
102 char *fullname, *t;
103 int l, tab;
105 *name = NULL;
106 if (*at == '\0')
107 return NULL;
109 while (isspace((unsigned char)*at))
110 at++;
112 /* files can be created or removed by diffing against /dev/null */
113 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
114 return NULL;
116 t = strdup(at);
117 if (t == NULL)
118 return got_error_from_errno("strdup");
119 *name = fullname = t;
120 tab = strchr(t, '\t') != NULL;
122 /* strip off path components and NUL-terminate */
123 for (l = strip;
124 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
125 ++t) {
126 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
127 if (--l >= 0)
128 *name = t + 1;
130 *t = '\0';
132 *name = strdup(*name);
133 free(fullname);
134 if (*name == NULL)
135 return got_error_from_errno("strdup");
136 return NULL;
139 static const struct got_error *
140 find_patch(FILE *fp)
142 const struct got_error *err = NULL;
143 char *old = NULL, *new = NULL;
144 char *line = NULL;
145 size_t linesize = 0;
146 ssize_t linelen;
147 int create, git = 0;
149 while ((linelen = getline(&line, &linesize, fp)) != -1) {
150 /*
151 * Ignore the Index name like GNU and larry' patch,
152 * we don't have to follow POSIX.
153 */
155 if (git && !strncmp(line, "--- a/", 6)) {
156 free(old);
157 err = filename(line+6, &old, 0);
158 } else if (!strncmp(line, "--- ", 4)) {
159 free(old);
160 err = filename(line+4, &old, 0);
161 } else if (git && !strncmp(line, "+++ b/", 6)) {
162 free(new);
163 err = filename(line+6, &new, 0);
164 } else if (!strncmp(line, "+++ ", 4)) {
165 free(new);
166 err = filename(line+4, &new, 0);
167 } else if (!strncmp(line, "diff --git a/", 13))
168 git = 1;
170 if (err)
171 break;
173 if (!strncmp(line, "@@ -", 4)) {
174 create = !strncmp(line+4, "0,0", 3);
175 if ((old == NULL && new == NULL) ||
176 (!create && old == NULL))
177 err = got_error(GOT_ERR_PATCH_MALFORMED);
178 else
179 err = send_patch(old, new, git);
181 if (err)
182 break;
184 /* rewind to previous line */
185 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
186 err = got_error_from_errno("fseek");
187 break;
191 free(old);
192 free(new);
193 free(line);
194 if (ferror(fp) && err == NULL)
195 err = got_error_from_errno("getline");
196 if (feof(fp) && err == NULL)
197 err = got_error(GOT_ERR_NO_PATCH);
198 return err;
201 static const struct got_error *
202 strtolnum(char **str, long *n)
204 char *p, c;
205 const char *errstr;
207 for (p = *str; isdigit((unsigned char)*p); ++p)
208 /* nop */;
210 c = *p;
211 *p = '\0';
213 *n = strtonum(*str, 0, LONG_MAX, &errstr);
214 if (errstr != NULL)
215 return got_error(GOT_ERR_PATCH_MALFORMED);
217 *p = c;
218 *str = p;
219 return NULL;
222 static const struct got_error *
223 parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
225 static const struct got_error *err = NULL;
227 *ok = 1;
228 if (strncmp(s, "@@ -", 4)) {
229 *ok = 0;
230 return NULL;
233 s += 4;
234 if (!*s)
235 return NULL;
236 err = strtolnum(&s, &hdr->oldfrom);
237 if (err)
238 return err;
239 if (*s == ',') {
240 s++;
241 err = strtolnum(&s, &hdr->oldlines);
242 if (err)
243 return err;
244 } else
245 hdr->oldlines = 1;
247 if (*s == ' ')
248 s++;
250 if (*s != '+' || !*++s)
251 return got_error(GOT_ERR_PATCH_MALFORMED);
252 err = strtolnum(&s, &hdr->newfrom);
253 if (err)
254 return err;
255 if (*s == ',') {
256 s++;
257 err = strtolnum(&s, &hdr->newlines);
258 if (err)
259 return err;
260 } else
261 hdr->newlines = 1;
263 if (*s == ' ')
264 s++;
266 if (*s != '@')
267 return got_error(GOT_ERR_PATCH_MALFORMED);
269 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
270 hdr->newfrom >= LONG_MAX - hdr->newlines ||
271 /* not so sure about this one */
272 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
273 return got_error(GOT_ERR_PATCH_MALFORMED);
275 if (hdr->oldlines == 0) {
276 /* larry says to "do append rather than insert"; I don't
277 * quite get it, but i trust him.
278 */
279 hdr->oldfrom++;
282 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
283 hdr, sizeof(*hdr)) == -1)
284 return got_error_from_errno(
285 "imsg_compose GOT_IMSG_PATCH_HUNK");
286 return NULL;
289 static const struct got_error *
290 send_line(const char *line)
292 static const struct got_error *err = NULL;
293 char *p = NULL;
295 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
296 if (asprintf(&p, " %s", line) == -1)
297 return got_error_from_errno("asprintf");
298 line = p;
301 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
302 line, strlen(line) + 1) == -1)
303 err = got_error_from_errno(
304 "imsg_compose GOT_IMSG_PATCH_LINE");
306 free(p);
307 return err;
310 static const struct got_error *
311 peek_special_line(FILE *fp, int send)
313 const struct got_error *err;
314 int ch;
316 ch = fgetc(fp);
317 if (ch != EOF && ch != '\\') {
318 ungetc(ch, fp);
319 return NULL;
322 if (ch == '\\' && send) {
323 err = send_line("\\");
324 if (err)
325 return err;
328 while (ch != EOF && ch != '\n')
329 ch = fgetc(fp);
331 if (ch != EOF || feof(fp))
332 return NULL;
333 return got_error(GOT_ERR_IO);
336 static const struct got_error *
337 parse_hunk(FILE *fp, int *ok)
339 static const struct got_error *err = NULL;
340 struct got_imsg_patch_hunk hdr;
341 char *line = NULL, ch;
342 size_t linesize = 0;
343 ssize_t linelen;
344 long leftold, leftnew;
346 linelen = getline(&line, &linesize, fp);
347 if (linelen == -1) {
348 *ok = 0;
349 goto done;
352 err = parse_hdr(line, ok, &hdr);
353 if (err)
354 goto done;
355 if (!*ok) {
356 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
357 err = got_error_from_errno("fseek");
358 goto done;
361 leftold = hdr.oldlines;
362 leftnew = hdr.newlines;
364 while (leftold > 0 || leftnew > 0) {
365 linelen = getline(&line, &linesize, fp);
366 if (linelen == -1) {
367 if (ferror(fp)) {
368 err = got_error_from_errno("getline");
369 goto done;
372 /* trailing newlines may be chopped */
373 if (leftold < 3 && leftnew < 3) {
374 *ok = 0;
375 break;
378 err = got_error(GOT_ERR_PATCH_TRUNCATED);
379 goto done;
381 if (line[linelen - 1] == '\n')
382 line[linelen - 1] = '\0';
384 /* usr.bin/patch allows '=' as context char */
385 if (*line == '=')
386 *line = ' ';
388 ch = *line;
389 if (ch == '\t' || ch == '\0')
390 ch = ' '; /* the space got eaten */
392 switch (ch) {
393 case '-':
394 leftold--;
395 break;
396 case ' ':
397 leftold--;
398 leftnew--;
399 break;
400 case '+':
401 leftnew--;
402 break;
403 default:
404 err = got_error(GOT_ERR_PATCH_MALFORMED);
405 goto done;
408 if (leftold < 0 || leftnew < 0) {
409 err = got_error(GOT_ERR_PATCH_MALFORMED);
410 goto done;
413 err = send_line(line);
414 if (err)
415 goto done;
417 if ((ch == '-' && leftold == 0) ||
418 (ch == '+' && leftnew == 0)) {
419 err = peek_special_line(fp, ch == '+');
420 if (err)
421 goto done;
425 done:
426 free(line);
427 return err;
430 static const struct got_error *
431 read_patch(struct imsgbuf *ibuf, int fd)
433 const struct got_error *err = NULL;
434 FILE *fp;
435 int ok, patch_found = 0;
437 if ((fp = fdopen(fd, "r")) == NULL) {
438 err = got_error_from_errno("fdopen");
439 close(fd);
440 return err;
443 while (!feof(fp)) {
444 err = find_patch(fp);
445 if (err)
446 goto done;
448 patch_found = 1;
449 for (;;) {
450 err = parse_hunk(fp, &ok);
451 if (err)
452 goto done;
453 if (!ok) {
454 err = send_patch_done();
455 if (err)
456 goto done;
457 break;
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;