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 <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <imsg.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"
61 struct imsgbuf ibuf;
63 static const struct got_error *
64 send_patch(const char *oldname, const char *newname)
65 {
66 struct got_imsg_patch p;
68 memset(&p, 0, sizeof(p));
70 if (oldname != NULL)
71 strlcpy(p.old, oldname, sizeof(p.old));
72 if (newname != NULL)
73 strlcpy(p.new, newname, sizeof(p.new));
75 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
76 &p, sizeof(p)) == -1)
77 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
78 return NULL;
79 }
81 static const struct got_error *
82 send_patch_done(void)
83 {
84 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
85 NULL, 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");
89 return NULL;
90 }
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)
95 {
96 char *fullname, *t;
97 int l, tab;
99 *name = NULL;
100 if (*at == '\0')
101 return NULL;
103 while (isspace((unsigned char)*at))
104 at++;
106 /* files can be created or removed by diffing against /dev/null */
107 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL)-1))
108 return NULL;
110 t = strdup(at);
111 if (t == NULL)
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 */
117 for (l = strip;
118 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
119 ++t) {
120 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
121 if (--l >= 0)
122 *name = t+1;
124 *t = '\0';
126 *name = strdup(*name);
127 free(fullname);
128 if (*name == NULL)
129 return got_error_from_errno("strdup");
130 return NULL;
133 static const struct got_error *
134 find_patch(FILE *fp)
136 const struct got_error *err = NULL;
137 char *old = NULL, *new = NULL;
138 char *line = NULL;
139 size_t linesize = 0;
140 ssize_t linelen;
141 int create, git = 0;
143 while ((linelen = getline(&line, &linesize, fp)) != -1) {
144 /*
145 * Ignore the Index name like GNU and larry' patch,
146 * we don't have to follow POSIX.
147 */
149 if (git && !strncmp(line, "--- a/", 6)) {
150 free(old);
151 err = filename(line+6, &old, 0);
152 } else if (!strncmp(line, "--- ", 4)) {
153 free(old);
154 err = filename(line+4, &old, 0);
155 } else if (git && !strncmp(line, "+++ b/", 6)) {
156 free(new);
157 err = filename(line+6, &new, 0);
158 } else if (!strncmp(line, "+++ ", 4)) {
159 free(new);
160 err = filename(line+4, &new, 0);
161 } else if (!strncmp(line, "diff --git a/", 13))
162 git = 1;
164 if (err)
165 break;
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);
172 else
173 err = send_patch(old, new);
175 free(old);
176 free(new);
178 if (err)
179 break;
181 /* rewind to previous line */
182 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
183 err = got_error_from_errno("fseek");
184 break;
188 free(line);
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);
193 return err;
196 static const struct got_error *
197 strtolnum(char **str, long *n)
199 char *p, c;
200 const char *errstr;
202 for (p = *str; isdigit((unsigned char)*p); ++p)
203 /* nop */;
205 c = *p;
206 *p = '\0';
208 *n = strtonum(*str, 0, LONG_MAX, &errstr);
209 if (errstr != NULL)
210 return got_error(GOT_ERR_PATCH_MALFORMED);
212 *p = c;
213 *str = p;
214 return NULL;
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;
222 *ok = 1;
223 if (strncmp(s, "@@ -", 4)) {
224 *ok = 0;
225 return NULL;
228 s += 4;
229 if (!*s)
230 return NULL;
231 err = strtolnum(&s, &hdr->oldfrom);
232 if (err)
233 return err;
234 if (*s == ',') {
235 s++;
236 err = strtolnum(&s, &hdr->oldlines);
237 if (err)
238 return err;
239 } else
240 hdr->oldlines = 1;
242 if (*s == ' ')
243 s++;
245 if (*s != '+' || !*++s)
246 return got_error(GOT_ERR_PATCH_MALFORMED);
247 err = strtolnum(&s, &hdr->newfrom);
248 if (err)
249 return err;
250 if (*s == ',') {
251 s++;
252 err = strtolnum(&s, &hdr->newlines);
253 if (err)
254 return err;
255 } else
256 hdr->newlines = 1;
258 if (*s == ' ')
259 s++;
261 if (*s != '@')
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.
273 */
274 hdr->oldfrom++;
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");
281 return NULL;
284 static const struct got_error *
285 send_line(const char *line)
287 static const struct got_error *err = NULL;
288 char *p = NULL;
290 if (*line != '+' && *line != '-' && *line != ' ') {
291 if (asprintf(&p, " %s", line) == -1)
292 return got_error_from_errno("asprintf");
293 line = p;
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");
301 free(p);
302 return err;
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;
311 size_t linesize = 0;
312 ssize_t linelen;
313 long leftold, leftnew;
315 linelen = getline(&line, &linesize, fp);
316 if (linelen == -1) {
317 *ok = 0;
318 goto done;
321 err = parse_hdr(line, ok, &hdr);
322 if (err)
323 goto done;
324 if (!*ok) {
325 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
326 err = got_error_from_errno("fseek");
327 goto done;
330 leftold = hdr.oldlines;
331 leftnew = hdr.newlines;
333 while (leftold > 0 || leftnew > 0) {
334 linelen = getline(&line, &linesize, fp);
335 if (linelen == -1) {
336 if (ferror(fp)) {
337 err = got_error_from_errno("getline");
338 goto done;
341 /* trailing newlines may be chopped */
342 if (leftold < 3 && leftnew < 3) {
343 *ok = 0;
344 break;
347 err = got_error(GOT_ERR_PATCH_TRUNCATED);
348 goto done;
351 /* usr.bin/patch allows '=' as context char */
352 if (*line == '=')
353 *line = ' ';
355 ch = *line;
356 if (ch == '\t' || ch == '\n')
357 ch = ' '; /* the space got eaten */
359 switch (ch) {
360 case '-':
361 leftold--;
362 break;
363 case ' ':
364 leftold--;
365 leftnew--;
366 break;
367 case '+':
368 leftnew--;
369 break;
370 default:
371 err = got_error(GOT_ERR_PATCH_MALFORMED);
372 goto done;
375 if (leftold < 0 || leftnew < 0) {
376 err = got_error(GOT_ERR_PATCH_MALFORMED);
377 goto done;
380 err = send_line(line);
381 if (err)
382 goto done;
385 done:
386 free(line);
387 return err;
390 static const struct got_error *
391 read_patch(struct imsgbuf *ibuf, int fd)
393 const struct got_error *err = NULL;
394 FILE *fp;
395 int ok, patch_found = 0;
397 if ((fp = fdopen(fd, "r")) == NULL) {
398 err = got_error_from_errno("fdopen");
399 close(fd);
400 return err;
403 while (!feof(fp)) {
404 err = find_patch(fp);
405 if (err)
406 goto done;
408 patch_found = 1;
409 for (;;) {
410 err = parse_hunk(fp, &ok);
411 if (err)
412 goto done;
413 if (!ok) {
414 err = send_patch_done();
415 if (err)
416 goto done;
417 break;
422 done:
423 fclose(fp);
425 /* ignore trailing gibberish */
426 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
427 err = NULL;
429 return err;
432 int
433 main(int argc, char **argv)
435 const struct got_error *err = NULL;
436 struct imsg imsg;
437 #if 0
438 static int attached;
439 while (!attached)
440 sleep(1);
441 #endif
443 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
444 #ifndef PROFILE
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);
449 return 1;
451 #endif
453 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
454 if (err)
455 goto done;
456 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
457 err = got_error(GOT_ERR_PRIVSEP_MSG);
458 goto done;
461 err = read_patch(&ibuf, imsg.fd);
462 if (err)
463 goto done;
464 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
465 NULL, 0) == -1) {
466 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
467 goto done;
469 err = got_privsep_flush_imsg(&ibuf);
470 done:
471 imsg_free(&imsg);
472 if (err != NULL) {
473 got_privsep_send_error(&ibuf, err);
474 err = NULL;
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);
480 return err ? 1 : 0;