2 * Copyright 1986, Larry Wall
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.
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
23 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
38 #include <sys/types.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"
61 static const struct got_error *
62 send_patch(const char *oldname, const char *newname)
64 struct got_imsg_patch p;
66 memset(&p, 0, sizeof(p));
69 strlcpy(p.old, oldname, sizeof(p.old));
71 strlcpy(p.new, newname, sizeof(p.new));
73 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
75 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
79 static const struct got_error *
82 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
84 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
85 if (imsg_flush(&ibuf) == -1)
86 return got_error_from_errno("imsg_flush");
90 /* based on fetchname from usr.bin/patch/util.c */
91 static const struct got_error *
92 filename(const char *at, char **name, int strip)
101 while (isspace((unsigned char)*at))
104 /* files can be created or removed by diffing against /dev/null */
105 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL)-1))
110 return got_error_from_errno("strdup");
111 *name = fullname = t;
112 tab = strchr(t, '\t') != NULL;
114 /* strip off path components and NUL-terminate */
116 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
118 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
124 *name = strdup(*name);
127 return got_error_from_errno("strdup");
131 static const struct got_error *
134 const struct got_error *err = NULL;
135 char *old = NULL, *new = NULL;
141 while ((linelen = getline(&line, &linesize, fp)) != -1) {
143 * Ignore the Index name like GNU and larry' patch,
144 * we don't have to follow POSIX.
147 if (git && !strncmp(line, "--- a/", 6)) {
149 err = filename(line+6, &old, 0);
150 } else if (!strncmp(line, "--- ", 4)) {
152 err = filename(line+4, &old, 0);
153 } else if (git && !strncmp(line, "+++ b/", 6)) {
155 err = filename(line+6, &new, 0);
156 } else if (!strncmp(line, "+++ ", 4)) {
158 err = filename(line+4, &new, 0);
159 } else if (!strncmp(line, "diff --git a/", 13))
165 if (!strncmp(line, "@@ -", 4)) {
166 create = !strncmp(line+4, "0,0", 3);
167 if ((old == NULL && new == NULL) ||
168 (!create && old == NULL))
169 err = got_error(GOT_ERR_PATCH_MALFORMED);
171 err = send_patch(old, new);
176 /* rewind to previous line */
177 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
178 err = got_error_from_errno("fseek");
186 if (ferror(fp) && err == NULL)
187 err = got_error_from_errno("getline");
188 if (feof(fp) && err == NULL)
189 err = got_error(GOT_ERR_NO_PATCH);
193 static const struct got_error *
194 strtolnum(char **str, long *n)
199 for (p = *str; isdigit((unsigned char)*p); ++p)
205 *n = strtonum(*str, 0, LONG_MAX, &errstr);
207 return got_error(GOT_ERR_PATCH_MALFORMED);
214 static const struct got_error *
215 parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
217 static const struct got_error *err = NULL;
220 if (strncmp(s, "@@ -", 4)) {
228 err = strtolnum(&s, &hdr->oldfrom);
233 err = strtolnum(&s, &hdr->oldlines);
242 if (*s != '+' || !*++s)
243 return got_error(GOT_ERR_PATCH_MALFORMED);
244 err = strtolnum(&s, &hdr->newfrom);
249 err = strtolnum(&s, &hdr->newlines);
259 return got_error(GOT_ERR_PATCH_MALFORMED);
261 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
262 hdr->newfrom >= LONG_MAX - hdr->newlines ||
263 /* not so sure about this one */
264 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
265 return got_error(GOT_ERR_PATCH_MALFORMED);
267 if (hdr->oldlines == 0) {
268 /* larry says to "do append rather than insert"; I don't
269 * quite get it, but i trust him.
274 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
275 hdr, sizeof(*hdr)) == -1)
276 return got_error_from_errno(
277 "imsg_compose GOT_IMSG_PATCH_HUNK");
281 static const struct got_error *
282 send_line(const char *line)
284 static const struct got_error *err = NULL;
287 if (*line != '+' && *line != '-' && *line != ' ') {
288 if (asprintf(&p, " %s", line) == -1)
289 return got_error_from_errno("asprintf");
293 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
294 line, strlen(line)+1) == -1)
295 err = got_error_from_errno(
296 "imsg_compose GOT_IMSG_PATCH_LINE");
302 static const struct got_error *
303 parse_hunk(FILE *fp, int *ok)
305 static const struct got_error *err = NULL;
306 struct got_imsg_patch_hunk hdr;
307 char *line = NULL, ch;
310 long leftold, leftnew;
312 linelen = getline(&line, &linesize, fp);
318 err = parse_hdr(line, ok, &hdr);
322 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
323 err = got_error_from_errno("fseek");
327 leftold = hdr.oldlines;
328 leftnew = hdr.newlines;
330 while (leftold > 0 || leftnew > 0) {
331 linelen = getline(&line, &linesize, fp);
334 err = got_error_from_errno("getline");
338 /* trailing newlines may be chopped */
339 if (leftold < 3 && leftnew < 3) {
344 err = got_error(GOT_ERR_PATCH_TRUNCATED);
348 /* usr.bin/patch allows '=' as context char */
353 if (ch == '\t' || ch == '\n')
354 ch = ' '; /* the space got eaten */
368 err = got_error(GOT_ERR_PATCH_MALFORMED);
372 if (leftold < 0 || leftnew < 0) {
373 err = got_error(GOT_ERR_PATCH_MALFORMED);
377 err = send_line(line);
387 static const struct got_error *
388 read_patch(struct imsgbuf *ibuf, int fd)
390 const struct got_error *err = NULL;
392 int ok, patch_found = 0;
394 if ((fp = fdopen(fd, "r")) == NULL) {
395 err = got_error_from_errno("fdopen");
401 err = find_patch(fp);
407 err = parse_hunk(fp, &ok);
411 err = send_patch_done();
422 /* ignore trailing gibberish */
423 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
430 main(int argc, char **argv)
432 const struct got_error *err = NULL;
440 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
442 /* revoke access to most system calls */
443 if (pledge("stdio recvfd", NULL) == -1) {
444 err = got_error_from_errno("pledge");
445 got_privsep_send_error(&ibuf, err);
449 /* revoke fs access */
450 if (landlock_no_fs() == -1) {
451 err = got_error_from_errno("landlock_no_fs");
452 got_privsep_send_error(&ibuf, err);
457 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
460 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
461 err = got_error(GOT_ERR_PRIVSEP_MSG);
465 err = read_patch(&ibuf, imsg.fd);
468 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
470 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
473 err = got_privsep_flush_imsg(&ibuf);
477 got_privsep_send_error(&ibuf, err);
480 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
481 err = got_error_from_errno("close");
482 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
483 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);