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>
39 #include <sys/queue.h>
52 #include "got_error.h"
53 #include "got_object.h"
55 #include "got_compat.h"
57 #include "got_lib_delta.h"
58 #include "got_lib_object.h"
59 #include "got_lib_privsep.h"
63 static const struct got_error *
64 send_patch(const char *oldname, const char *newname)
66 struct got_imsg_patch p;
68 memset(&p, 0, sizeof(p));
71 strlcpy(p.old, oldname, sizeof(p.old));
73 strlcpy(p.new, newname, sizeof(p.new));
75 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
77 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
81 static const struct got_error *
84 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 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");
92 /* based on fetchname from usr.bin/patch/util.c */
93 static const struct got_error *
94 filename(const char *at, char **name, int strip)
103 while (isspace((unsigned char)*at))
106 /* files can be created or removed by diffing against /dev/null */
107 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL)-1))
112 return got_error_from_errno("strdup");
113 *name = fullname = t;
114 tab = strchr(t, '\t') != NULL;
116 /* strip off path components and NUL-terminate */
118 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
120 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
126 *name = strdup(*name);
129 return got_error_from_errno("strdup");
133 static const struct got_error *
136 const struct got_error *err = NULL;
137 char *old = NULL, *new = NULL;
143 while ((linelen = getline(&line, &linesize, fp)) != -1) {
145 * Ignore the Index name like GNU and larry' patch,
146 * we don't have to follow POSIX.
149 if (git && !strncmp(line, "--- a/", 6)) {
151 err = filename(line+6, &old, 0);
152 } else if (!strncmp(line, "--- ", 4)) {
154 err = filename(line+4, &old, 0);
155 } else if (git && !strncmp(line, "+++ b/", 6)) {
157 err = filename(line+6, &new, 0);
158 } else if (!strncmp(line, "+++ ", 4)) {
160 err = filename(line+4, &new, 0);
161 } else if (!strncmp(line, "diff --git a/", 13))
167 if (!strncmp(line, "@@ -", 4)) {
168 create = !strncmp(line+4, "0,0", 3);
169 if ((old == NULL && new == NULL) ||
170 (!create && old == NULL))
171 err = got_error(GOT_ERR_PATCH_MALFORMED);
173 err = send_patch(old, new);
181 /* rewind to previous line */
182 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
183 err = got_error_from_errno("fseek");
189 if (ferror(fp) && err == NULL)
190 err = got_error_from_errno("getline");
191 if (feof(fp) && err == NULL)
192 err = got_error(GOT_ERR_NO_PATCH);
196 static const struct got_error *
197 strtolnum(char **str, long *n)
202 for (p = *str; isdigit((unsigned char)*p); ++p)
208 *n = strtonum(*str, 0, LONG_MAX, &errstr);
210 return got_error(GOT_ERR_PATCH_MALFORMED);
217 static const struct got_error *
218 parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
220 static const struct got_error *err = NULL;
223 if (strncmp(s, "@@ -", 4)) {
231 err = strtolnum(&s, &hdr->oldfrom);
236 err = strtolnum(&s, &hdr->oldlines);
245 if (*s != '+' || !*++s)
246 return got_error(GOT_ERR_PATCH_MALFORMED);
247 err = strtolnum(&s, &hdr->newfrom);
252 err = strtolnum(&s, &hdr->newlines);
262 return got_error(GOT_ERR_PATCH_MALFORMED);
264 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
265 hdr->newfrom >= LONG_MAX - hdr->newlines ||
266 /* not so sure about this one */
267 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
268 return got_error(GOT_ERR_PATCH_MALFORMED);
270 if (hdr->oldlines == 0) {
271 /* larry says to "do append rather than insert"; I don't
272 * quite get it, but i trust him.
277 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
278 hdr, sizeof(*hdr)) == -1)
279 return got_error_from_errno(
280 "imsg_compose GOT_IMSG_PATCH_HUNK");
284 static const struct got_error *
285 send_line(const char *line)
287 static const struct got_error *err = NULL;
290 if (*line != '+' && *line != '-' && *line != ' ') {
291 if (asprintf(&p, " %s", line) == -1)
292 return got_error_from_errno("asprintf");
296 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
297 line, strlen(line)+1) == -1)
298 err = got_error_from_errno(
299 "imsg_compose GOT_IMSG_PATCH_LINE");
305 static const struct got_error *
306 parse_hunk(FILE *fp, int *ok)
308 static const struct got_error *err = NULL;
309 struct got_imsg_patch_hunk hdr;
310 char *line = NULL, ch;
313 long leftold, leftnew;
315 linelen = getline(&line, &linesize, fp);
321 err = parse_hdr(line, ok, &hdr);
325 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
326 err = got_error_from_errno("fseek");
330 leftold = hdr.oldlines;
331 leftnew = hdr.newlines;
333 while (leftold > 0 || leftnew > 0) {
334 linelen = getline(&line, &linesize, fp);
337 err = got_error_from_errno("getline");
341 /* trailing newlines may be chopped */
342 if (leftold < 3 && leftnew < 3) {
347 err = got_error(GOT_ERR_PATCH_TRUNCATED);
351 /* usr.bin/patch allows '=' as context char */
356 if (ch == '\t' || ch == '\n')
357 ch = ' '; /* the space got eaten */
371 err = got_error(GOT_ERR_PATCH_MALFORMED);
375 if (leftold < 0 || leftnew < 0) {
376 err = got_error(GOT_ERR_PATCH_MALFORMED);
380 err = send_line(line);
390 static const struct got_error *
391 read_patch(struct imsgbuf *ibuf, int fd)
393 const struct got_error *err = NULL;
395 int ok, patch_found = 0;
397 if ((fp = fdopen(fd, "r")) == NULL) {
398 err = got_error_from_errno("fdopen");
404 err = find_patch(fp);
410 err = parse_hunk(fp, &ok);
414 err = send_patch_done();
425 /* ignore trailing gibberish */
426 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
433 main(int argc, char **argv)
435 const struct got_error *err = NULL;
443 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
445 /* revoke access to most system calls */
446 if (pledge("stdio recvfd", NULL) == -1) {
447 err = got_error_from_errno("pledge");
448 got_privsep_send_error(&ibuf, err);
453 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
456 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
457 err = got_error(GOT_ERR_PRIVSEP_MSG);
461 err = read_patch(&ibuf, imsg.fd);
464 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
466 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
469 err = got_privsep_flush_imsg(&ibuf);
473 got_privsep_send_error(&ibuf, err);
476 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
477 err = got_error_from_errno("close");
478 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
479 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);