2 e9ce266e 2022-03-07 op * Copyright 1986, Larry Wall
4 e9ce266e 2022-03-07 op * Redistribution and use in source and binary forms, with or without
5 e9ce266e 2022-03-07 op * modification, are permitted provided that the following condition is met:
6 e9ce266e 2022-03-07 op * 1. Redistributions of source code must retain the above copyright notice,
7 e9ce266e 2022-03-07 op * this condition and the following disclaimer.
9 e9ce266e 2022-03-07 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 e9ce266e 2022-03-07 op * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 e9ce266e 2022-03-07 op * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 e9ce266e 2022-03-07 op * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 e9ce266e 2022-03-07 op * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 e9ce266e 2022-03-07 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 e9ce266e 2022-03-07 op * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 e9ce266e 2022-03-07 op * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 e9ce266e 2022-03-07 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 e9ce266e 2022-03-07 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19 e9ce266e 2022-03-07 op * SUCH DAMAGE.
23 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
25 e9ce266e 2022-03-07 op * Permission to use, copy, modify, and distribute this software for any
26 e9ce266e 2022-03-07 op * purpose with or without fee is hereby granted, provided that the above
27 e9ce266e 2022-03-07 op * copyright notice and this permission notice appear in all copies.
29 e9ce266e 2022-03-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
30 e9ce266e 2022-03-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
31 e9ce266e 2022-03-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
32 e9ce266e 2022-03-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
33 e9ce266e 2022-03-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
34 e9ce266e 2022-03-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
35 e9ce266e 2022-03-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 e9ce266e 2022-03-07 op #include <sys/types.h>
39 e9ce266e 2022-03-07 op #include <sys/queue.h>
40 e9ce266e 2022-03-07 op #include <sys/uio.h>
42 e9ce266e 2022-03-07 op #include <ctype.h>
43 e9ce266e 2022-03-07 op #include <limits.h>
44 e9ce266e 2022-03-07 op #include <paths.h>
45 e9ce266e 2022-03-07 op #include <sha1.h>
46 e9ce266e 2022-03-07 op #include <stdint.h>
47 e9ce266e 2022-03-07 op #include <stdio.h>
48 e9ce266e 2022-03-07 op #include <stdlib.h>
49 e9ce266e 2022-03-07 op #include <string.h>
50 e9ce266e 2022-03-07 op #include <unistd.h>
51 e9ce266e 2022-03-07 op #include <imsg.h>
53 e9ce266e 2022-03-07 op #include "got_error.h"
54 e9ce266e 2022-03-07 op #include "got_object.h"
56 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
57 e9ce266e 2022-03-07 op #include "got_lib_object.h"
58 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
60 e9ce266e 2022-03-07 op struct imsgbuf ibuf;
62 e9ce266e 2022-03-07 op static const struct got_error *
63 7a30b5cb 2022-03-20 op send_patch(const char *oldname, const char *newname, int git)
65 e9ce266e 2022-03-07 op struct got_imsg_patch p;
67 e9ce266e 2022-03-07 op memset(&p, 0, sizeof(p));
69 9d6cabd5 2022-04-07 op if (oldname != NULL)
70 e9ce266e 2022-03-07 op strlcpy(p.old, oldname, sizeof(p.old));
72 e9ce266e 2022-03-07 op if (newname != NULL)
73 e9ce266e 2022-03-07 op strlcpy(p.new, newname, sizeof(p.new));
76 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
77 e9ce266e 2022-03-07 op &p, sizeof(p)) == -1)
78 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
82 e9ce266e 2022-03-07 op static const struct got_error *
83 e9ce266e 2022-03-07 op send_patch_done(void)
85 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
86 e9ce266e 2022-03-07 op NULL, 0) == -1)
87 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
88 e9ce266e 2022-03-07 op if (imsg_flush(&ibuf) == -1)
89 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_flush");
93 e9ce266e 2022-03-07 op /* based on fetchname from usr.bin/patch/util.c */
94 e9ce266e 2022-03-07 op static const struct got_error *
95 9d6cabd5 2022-04-07 op filename(const char *at, char **name)
97 9d6cabd5 2022-04-07 op char *tmp, *t;
100 e9ce266e 2022-03-07 op if (*at == '\0')
103 e9ce266e 2022-03-07 op while (isspace((unsigned char)*at))
106 e9ce266e 2022-03-07 op /* files can be created or removed by diffing against /dev/null */
107 46ebad13 2022-03-17 op if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
110 9d6cabd5 2022-04-07 op tmp = strdup(at);
111 9d6cabd5 2022-04-07 op if (tmp == NULL)
112 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
113 9d6cabd5 2022-04-07 op if ((t = strchr(tmp, '\t')) != NULL)
115 9d6cabd5 2022-04-07 op if ((t = strchr(tmp, '\n')) != NULL)
118 9d6cabd5 2022-04-07 op *name = strdup(tmp);
120 e9ce266e 2022-03-07 op if (*name == NULL)
121 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
125 e9ce266e 2022-03-07 op static const struct got_error *
126 e9ce266e 2022-03-07 op find_patch(FILE *fp)
128 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
129 e9ce266e 2022-03-07 op char *old = NULL, *new = NULL;
130 e9ce266e 2022-03-07 op char *line = NULL;
131 e9ce266e 2022-03-07 op size_t linesize = 0;
132 e9ce266e 2022-03-07 op ssize_t linelen;
133 e9ce266e 2022-03-07 op int create, git = 0;
135 e9ce266e 2022-03-07 op while ((linelen = getline(&line, &linesize, fp)) != -1) {
137 e9ce266e 2022-03-07 op * Ignore the Index name like GNU and larry' patch,
138 e9ce266e 2022-03-07 op * we don't have to follow POSIX.
141 9d6cabd5 2022-04-07 op if (!strncmp(line, "--- ", 4)) {
143 9d6cabd5 2022-04-07 op err = filename(line+4, &old);
144 e9ce266e 2022-03-07 op } else if (!strncmp(line, "+++ ", 4)) {
146 9d6cabd5 2022-04-07 op err = filename(line+4, &new);
147 e9ce266e 2022-03-07 op } else if (!strncmp(line, "diff --git a/", 13))
153 e9ce266e 2022-03-07 op if (!strncmp(line, "@@ -", 4)) {
154 e9ce266e 2022-03-07 op create = !strncmp(line+4, "0,0", 3);
155 e9ce266e 2022-03-07 op if ((old == NULL && new == NULL) ||
156 e9ce266e 2022-03-07 op (!create && old == NULL))
157 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
159 7a30b5cb 2022-03-20 op err = send_patch(old, new, git);
164 e9ce266e 2022-03-07 op /* rewind to previous line */
165 e9ce266e 2022-03-07 op if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
166 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
174 e9ce266e 2022-03-07 op if (ferror(fp) && err == NULL)
175 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
176 e9ce266e 2022-03-07 op if (feof(fp) && err == NULL)
177 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_NO_PATCH);
181 e9ce266e 2022-03-07 op static const struct got_error *
182 e9ce266e 2022-03-07 op strtolnum(char **str, long *n)
185 e9ce266e 2022-03-07 op const char *errstr;
187 e9ce266e 2022-03-07 op for (p = *str; isdigit((unsigned char)*p); ++p)
193 e9ce266e 2022-03-07 op *n = strtonum(*str, 0, LONG_MAX, &errstr);
194 e9ce266e 2022-03-07 op if (errstr != NULL)
195 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
202 e9ce266e 2022-03-07 op static const struct got_error *
203 e9ce266e 2022-03-07 op parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
205 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
208 e9ce266e 2022-03-07 op if (strncmp(s, "@@ -", 4)) {
216 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->oldfrom);
219 e9ce266e 2022-03-07 op if (*s == ',') {
221 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->oldlines);
225 e9ce266e 2022-03-07 op hdr->oldlines = 1;
227 e9ce266e 2022-03-07 op if (*s == ' ')
230 e9ce266e 2022-03-07 op if (*s != '+' || !*++s)
231 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
232 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->newfrom);
235 e9ce266e 2022-03-07 op if (*s == ',') {
237 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->newlines);
241 e9ce266e 2022-03-07 op hdr->newlines = 1;
243 e9ce266e 2022-03-07 op if (*s == ' ')
246 e9ce266e 2022-03-07 op if (*s != '@')
247 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
249 e9ce266e 2022-03-07 op if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
250 e9ce266e 2022-03-07 op hdr->newfrom >= LONG_MAX - hdr->newlines ||
251 e9ce266e 2022-03-07 op /* not so sure about this one */
252 e9ce266e 2022-03-07 op hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
253 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
255 e9ce266e 2022-03-07 op if (hdr->oldlines == 0) {
256 e9ce266e 2022-03-07 op /* larry says to "do append rather than insert"; I don't
257 e9ce266e 2022-03-07 op * quite get it, but i trust him.
259 e9ce266e 2022-03-07 op hdr->oldfrom++;
262 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
263 e9ce266e 2022-03-07 op hdr, sizeof(*hdr)) == -1)
264 e9ce266e 2022-03-07 op return got_error_from_errno(
265 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_HUNK");
269 e9ce266e 2022-03-07 op static const struct got_error *
270 e9ce266e 2022-03-07 op send_line(const char *line)
272 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
273 e9ce266e 2022-03-07 op char *p = NULL;
275 b3c57ab2 2022-03-22 op if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
276 e9ce266e 2022-03-07 op if (asprintf(&p, " %s", line) == -1)
277 e9ce266e 2022-03-07 op return got_error_from_errno("asprintf");
281 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
282 46ebad13 2022-03-17 op line, strlen(line) + 1) == -1)
283 e9ce266e 2022-03-07 op err = got_error_from_errno(
284 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_LINE");
290 e9ce266e 2022-03-07 op static const struct got_error *
291 b3c57ab2 2022-03-22 op peek_special_line(FILE *fp, int send)
293 b3c57ab2 2022-03-22 op const struct got_error *err;
296 b3c57ab2 2022-03-22 op ch = fgetc(fp);
297 b3c57ab2 2022-03-22 op if (ch != EOF && ch != '\\') {
298 b3c57ab2 2022-03-22 op ungetc(ch, fp);
302 b3c57ab2 2022-03-22 op if (ch == '\\' && send) {
303 b3c57ab2 2022-03-22 op err = send_line("\\");
308 b3c57ab2 2022-03-22 op while (ch != EOF && ch != '\n')
309 b3c57ab2 2022-03-22 op ch = fgetc(fp);
311 b3c57ab2 2022-03-22 op if (ch != EOF || feof(fp))
313 b3c57ab2 2022-03-22 op return got_error(GOT_ERR_IO);
316 b3c57ab2 2022-03-22 op static const struct got_error *
317 e9ce266e 2022-03-07 op parse_hunk(FILE *fp, int *ok)
319 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
320 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
321 e9ce266e 2022-03-07 op char *line = NULL, ch;
322 e9ce266e 2022-03-07 op size_t linesize = 0;
323 e9ce266e 2022-03-07 op ssize_t linelen;
324 e9ce266e 2022-03-07 op long leftold, leftnew;
326 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, fp);
327 e9ce266e 2022-03-07 op if (linelen == -1) {
332 e9ce266e 2022-03-07 op err = parse_hdr(line, ok, &hdr);
336 e9ce266e 2022-03-07 op if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
337 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
341 e9ce266e 2022-03-07 op leftold = hdr.oldlines;
342 e9ce266e 2022-03-07 op leftnew = hdr.newlines;
344 e9ce266e 2022-03-07 op while (leftold > 0 || leftnew > 0) {
345 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, fp);
346 e9ce266e 2022-03-07 op if (linelen == -1) {
347 e9ce266e 2022-03-07 op if (ferror(fp)) {
348 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
352 e9ce266e 2022-03-07 op /* trailing newlines may be chopped */
353 e9ce266e 2022-03-07 op if (leftold < 3 && leftnew < 3) {
358 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_TRUNCATED);
361 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
362 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
364 e9ce266e 2022-03-07 op /* usr.bin/patch allows '=' as context char */
365 e9ce266e 2022-03-07 op if (*line == '=')
369 b3c57ab2 2022-03-22 op if (ch == '\t' || ch == '\0')
370 e9ce266e 2022-03-07 op ch = ' '; /* the space got eaten */
372 e9ce266e 2022-03-07 op switch (ch) {
384 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
388 e9ce266e 2022-03-07 op if (leftold < 0 || leftnew < 0) {
389 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
393 e9ce266e 2022-03-07 op err = send_line(line);
397 b3c57ab2 2022-03-22 op if ((ch == '-' && leftold == 0) ||
398 b3c57ab2 2022-03-22 op (ch == '+' && leftnew == 0)) {
399 b3c57ab2 2022-03-22 op err = peek_special_line(fp, ch == '+');
410 e9ce266e 2022-03-07 op static const struct got_error *
411 e9ce266e 2022-03-07 op read_patch(struct imsgbuf *ibuf, int fd)
413 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
415 e9ce266e 2022-03-07 op int ok, patch_found = 0;
417 e9ce266e 2022-03-07 op if ((fp = fdopen(fd, "r")) == NULL) {
418 e9ce266e 2022-03-07 op err = got_error_from_errno("fdopen");
423 e9ce266e 2022-03-07 op while (!feof(fp)) {
424 e9ce266e 2022-03-07 op err = find_patch(fp);
428 e9ce266e 2022-03-07 op patch_found = 1;
430 e9ce266e 2022-03-07 op err = parse_hunk(fp, &ok);
434 e9ce266e 2022-03-07 op err = send_patch_done();
445 e9ce266e 2022-03-07 op /* ignore trailing gibberish */
446 e9ce266e 2022-03-07 op if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
453 e9ce266e 2022-03-07 op main(int argc, char **argv)
455 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
456 e9ce266e 2022-03-07 op struct imsg imsg;
458 e9ce266e 2022-03-07 op static int attached;
459 e9ce266e 2022-03-07 op while (!attached)
463 e9ce266e 2022-03-07 op imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
464 e9ce266e 2022-03-07 op #ifndef PROFILE
465 e9ce266e 2022-03-07 op /* revoke access to most system calls */
466 e9ce266e 2022-03-07 op if (pledge("stdio recvfd", NULL) == -1) {
467 e9ce266e 2022-03-07 op err = got_error_from_errno("pledge");
468 e9ce266e 2022-03-07 op got_privsep_send_error(&ibuf, err);
473 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
476 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
477 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
481 e9ce266e 2022-03-07 op err = read_patch(&ibuf, imsg.fd);
484 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
485 e9ce266e 2022-03-07 op NULL, 0) == -1) {
486 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
489 e9ce266e 2022-03-07 op err = got_privsep_flush_imsg(&ibuf);
491 e9ce266e 2022-03-07 op imsg_free(&imsg);
492 e9ce266e 2022-03-07 op if (err != NULL) {
493 e9ce266e 2022-03-07 op got_privsep_send_error(&ibuf, err);
496 e9ce266e 2022-03-07 op if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
497 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
498 e9ce266e 2022-03-07 op if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
499 e9ce266e 2022-03-07 op fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
500 e9ce266e 2022-03-07 op return err ? 1 : 0;