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>
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"
62 static const struct got_error *
63 send_patch(const char *oldname, const char *newname, int git)
65 struct got_imsg_patch p;
67 memset(&p, 0, sizeof(p));
70 * Prefer the new name if it's not /dev/null and it's not
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));
79 strlcpy(p.new, newname, sizeof(p.new));
81 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
83 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
87 static const struct got_error *
90 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 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");
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)
109 while (isspace((unsigned char)*at))
112 /* files can be created or removed by diffing against /dev/null */
113 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
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 */
124 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
126 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
132 *name = strdup(*name);
135 return got_error_from_errno("strdup");
139 static const struct got_error *
142 const struct got_error *err = NULL;
143 char *old = NULL, *new = NULL;
149 while ((linelen = getline(&line, &linesize, fp)) != -1) {
151 * Ignore the Index name like GNU and larry' patch,
152 * we don't have to follow POSIX.
155 if (git && !strncmp(line, "--- a/", 6)) {
157 err = filename(line+6, &old, 0);
158 } else if (!strncmp(line, "--- ", 4)) {
160 err = filename(line+4, &old, 0);
161 } else if (git && !strncmp(line, "+++ b/", 6)) {
163 err = filename(line+6, &new, 0);
164 } else if (!strncmp(line, "+++ ", 4)) {
166 err = filename(line+4, &new, 0);
167 } else if (!strncmp(line, "diff --git a/", 13))
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);
179 err = send_patch(old, new, git);
184 /* rewind to previous line */
185 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
186 err = got_error_from_errno("fseek");
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);
201 static const struct got_error *
202 strtolnum(char **str, long *n)
207 for (p = *str; isdigit((unsigned char)*p); ++p)
213 *n = strtonum(*str, 0, LONG_MAX, &errstr);
215 return got_error(GOT_ERR_PATCH_MALFORMED);
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;
228 if (strncmp(s, "@@ -", 4)) {
236 err = strtolnum(&s, &hdr->oldfrom);
241 err = strtolnum(&s, &hdr->oldlines);
250 if (*s != '+' || !*++s)
251 return got_error(GOT_ERR_PATCH_MALFORMED);
252 err = strtolnum(&s, &hdr->newfrom);
257 err = strtolnum(&s, &hdr->newlines);
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.
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");
289 static const struct got_error *
290 send_line(const char *line)
292 static const struct got_error *err = NULL;
295 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
296 if (asprintf(&p, " %s", line) == -1)
297 return got_error_from_errno("asprintf");
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");
310 static const struct got_error *
311 peek_special_line(FILE *fp, int send)
313 const struct got_error *err;
317 if (ch != EOF && ch != '\\') {
322 if (ch == '\\' && send) {
323 err = send_line("\\");
328 while (ch != EOF && ch != '\n')
331 if (ch != EOF || feof(fp))
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;
344 long leftold, leftnew;
346 linelen = getline(&line, &linesize, fp);
352 err = parse_hdr(line, ok, &hdr);
356 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
357 err = got_error_from_errno("fseek");
361 leftold = hdr.oldlines;
362 leftnew = hdr.newlines;
364 while (leftold > 0 || leftnew > 0) {
365 linelen = getline(&line, &linesize, fp);
368 err = got_error_from_errno("getline");
372 /* trailing newlines may be chopped */
373 if (leftold < 3 && leftnew < 3) {
378 err = got_error(GOT_ERR_PATCH_TRUNCATED);
381 if (line[linelen - 1] == '\n')
382 line[linelen - 1] = '\0';
384 /* usr.bin/patch allows '=' as context char */
389 if (ch == '\t' || ch == '\0')
390 ch = ' '; /* the space got eaten */
404 err = got_error(GOT_ERR_PATCH_MALFORMED);
408 if (leftold < 0 || leftnew < 0) {
409 err = got_error(GOT_ERR_PATCH_MALFORMED);
413 err = send_line(line);
417 if ((ch == '-' && leftold == 0) ||
418 (ch == '+' && leftnew == 0)) {
419 err = peek_special_line(fp, ch == '+');
430 static const struct got_error *
431 read_patch(struct imsgbuf *ibuf, int fd)
433 const struct got_error *err = NULL;
435 int ok, patch_found = 0;
437 if ((fp = fdopen(fd, "r")) == NULL) {
438 err = got_error_from_errno("fdopen");
444 err = find_patch(fp);
450 err = parse_hunk(fp, &ok);
454 err = send_patch_done();
465 /* ignore trailing gibberish */
466 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
473 main(int argc, char **argv)
475 const struct got_error *err = NULL;
483 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
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);
493 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
496 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
497 err = got_error(GOT_ERR_PRIVSEP_MSG);
501 err = read_patch(&ibuf, imsg.fd);
504 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
506 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
509 err = got_privsep_flush_imsg(&ibuf);
513 got_privsep_send_error(&ibuf, err);
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);