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 <sha1.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <imsg.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"
60 struct imsgbuf ibuf;
62 static const struct got_error *
63 send_patch(const char *oldname, const char *newname, int git)
64 {
65 struct got_imsg_patch p;
67 memset(&p, 0, sizeof(p));
69 /*
70 * Prefer the new name if it's not /dev/null and it's not
71 * a git-style diff.
72 */
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));
78 if (newname != NULL)
79 strlcpy(p.new, newname, sizeof(p.new));
81 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
82 &p, sizeof(p)) == -1)
83 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
84 return NULL;
85 }
87 static const struct got_error *
88 send_patch_done(void)
89 {
90 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
91 NULL, 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");
95 return NULL;
96 }
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)
102 char *fullname, *t;
103 int l, tab;
105 *name = NULL;
106 if (*at == '\0')
107 return NULL;
109 while (isspace((unsigned char)*at))
110 at++;
112 /* files can be created or removed by diffing against /dev/null */
113 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
114 return NULL;
116 t = strdup(at);
117 if (t == NULL)
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 */
123 for (l = strip;
124 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
125 ++t) {
126 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
127 if (--l >= 0)
128 *name = t + 1;
130 *t = '\0';
132 *name = strdup(*name);
133 free(fullname);
134 if (*name == NULL)
135 return got_error_from_errno("strdup");
136 return NULL;
139 static const struct got_error *
140 find_patch(FILE *fp)
142 const struct got_error *err = NULL;
143 char *old = NULL, *new = NULL;
144 char *line = NULL;
145 size_t linesize = 0;
146 ssize_t linelen;
147 int create, git = 0;
149 while ((linelen = getline(&line, &linesize, fp)) != -1) {
150 /*
151 * Ignore the Index name like GNU and larry' patch,
152 * we don't have to follow POSIX.
153 */
155 if (git && !strncmp(line, "--- a/", 6)) {
156 free(old);
157 err = filename(line+6, &old, 0);
158 } else if (!strncmp(line, "--- ", 4)) {
159 free(old);
160 err = filename(line+4, &old, 0);
161 } else if (git && !strncmp(line, "+++ b/", 6)) {
162 free(new);
163 err = filename(line+6, &new, 0);
164 } else if (!strncmp(line, "+++ ", 4)) {
165 free(new);
166 err = filename(line+4, &new, 0);
167 } else if (!strncmp(line, "diff --git a/", 13))
168 git = 1;
170 if (err)
171 break;
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);
178 else
179 err = send_patch(old, new, git);
181 if (err)
182 break;
184 /* rewind to previous line */
185 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
186 err = got_error_from_errno("fseek");
187 break;
191 free(old);
192 free(new);
193 free(line);
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);
198 return err;
201 static const struct got_error *
202 strtolnum(char **str, long *n)
204 char *p, c;
205 const char *errstr;
207 for (p = *str; isdigit((unsigned char)*p); ++p)
208 /* nop */;
210 c = *p;
211 *p = '\0';
213 *n = strtonum(*str, 0, LONG_MAX, &errstr);
214 if (errstr != NULL)
215 return got_error(GOT_ERR_PATCH_MALFORMED);
217 *p = c;
218 *str = p;
219 return NULL;
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;
227 *ok = 1;
228 if (strncmp(s, "@@ -", 4)) {
229 *ok = 0;
230 return NULL;
233 s += 4;
234 if (!*s)
235 return NULL;
236 err = strtolnum(&s, &hdr->oldfrom);
237 if (err)
238 return err;
239 if (*s == ',') {
240 s++;
241 err = strtolnum(&s, &hdr->oldlines);
242 if (err)
243 return err;
244 } else
245 hdr->oldlines = 1;
247 if (*s == ' ')
248 s++;
250 if (*s != '+' || !*++s)
251 return got_error(GOT_ERR_PATCH_MALFORMED);
252 err = strtolnum(&s, &hdr->newfrom);
253 if (err)
254 return err;
255 if (*s == ',') {
256 s++;
257 err = strtolnum(&s, &hdr->newlines);
258 if (err)
259 return err;
260 } else
261 hdr->newlines = 1;
263 if (*s == ' ')
264 s++;
266 if (*s != '@')
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.
278 */
279 hdr->oldfrom++;
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");
286 return NULL;
289 static const struct got_error *
290 send_line(const char *line)
292 static const struct got_error *err = NULL;
293 char *p = NULL;
295 if (*line != '+' && *line != '-' && *line != ' ') {
296 if (asprintf(&p, " %s", line) == -1)
297 return got_error_from_errno("asprintf");
298 line = p;
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");
306 free(p);
307 return err;
310 static const struct got_error *
311 parse_hunk(FILE *fp, int *ok)
313 static const struct got_error *err = NULL;
314 struct got_imsg_patch_hunk hdr;
315 char *line = NULL, ch;
316 size_t linesize = 0;
317 ssize_t linelen;
318 long leftold, leftnew;
320 linelen = getline(&line, &linesize, fp);
321 if (linelen == -1) {
322 *ok = 0;
323 goto done;
326 err = parse_hdr(line, ok, &hdr);
327 if (err)
328 goto done;
329 if (!*ok) {
330 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
331 err = got_error_from_errno("fseek");
332 goto done;
335 leftold = hdr.oldlines;
336 leftnew = hdr.newlines;
338 while (leftold > 0 || leftnew > 0) {
339 linelen = getline(&line, &linesize, fp);
340 if (linelen == -1) {
341 if (ferror(fp)) {
342 err = got_error_from_errno("getline");
343 goto done;
346 /* trailing newlines may be chopped */
347 if (leftold < 3 && leftnew < 3) {
348 *ok = 0;
349 break;
352 err = got_error(GOT_ERR_PATCH_TRUNCATED);
353 goto done;
356 /* usr.bin/patch allows '=' as context char */
357 if (*line == '=')
358 *line = ' ';
360 ch = *line;
361 if (ch == '\t' || ch == '\n')
362 ch = ' '; /* the space got eaten */
364 switch (ch) {
365 case '-':
366 leftold--;
367 break;
368 case ' ':
369 leftold--;
370 leftnew--;
371 break;
372 case '+':
373 leftnew--;
374 break;
375 default:
376 err = got_error(GOT_ERR_PATCH_MALFORMED);
377 goto done;
380 if (leftold < 0 || leftnew < 0) {
381 err = got_error(GOT_ERR_PATCH_MALFORMED);
382 goto done;
385 err = send_line(line);
386 if (err)
387 goto done;
390 done:
391 free(line);
392 return err;
395 static const struct got_error *
396 read_patch(struct imsgbuf *ibuf, int fd)
398 const struct got_error *err = NULL;
399 FILE *fp;
400 int ok, patch_found = 0;
402 if ((fp = fdopen(fd, "r")) == NULL) {
403 err = got_error_from_errno("fdopen");
404 close(fd);
405 return err;
408 while (!feof(fp)) {
409 err = find_patch(fp);
410 if (err)
411 goto done;
413 patch_found = 1;
414 for (;;) {
415 err = parse_hunk(fp, &ok);
416 if (err)
417 goto done;
418 if (!ok) {
419 err = send_patch_done();
420 if (err)
421 goto done;
422 break;
427 done:
428 fclose(fp);
430 /* ignore trailing gibberish */
431 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
432 err = NULL;
434 return err;
437 int
438 main(int argc, char **argv)
440 const struct got_error *err = NULL;
441 struct imsg imsg;
442 #if 0
443 static int attached;
444 while (!attached)
445 sleep(1);
446 #endif
448 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
449 #ifndef PROFILE
450 /* revoke access to most system calls */
451 if (pledge("stdio recvfd", NULL) == -1) {
452 err = got_error_from_errno("pledge");
453 got_privsep_send_error(&ibuf, err);
454 return 1;
456 #endif
458 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
459 if (err)
460 goto done;
461 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
462 err = got_error(GOT_ERR_PRIVSEP_MSG);
463 goto done;
466 err = read_patch(&ibuf, imsg.fd);
467 if (err)
468 goto done;
469 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
470 NULL, 0) == -1) {
471 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
472 goto done;
474 err = got_privsep_flush_imsg(&ibuf);
475 done:
476 imsg_free(&imsg);
477 if (err != NULL) {
478 got_privsep_send_error(&ibuf, err);
479 err = NULL;
481 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
482 err = got_error_from_errno("close");
483 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
484 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
485 return err ? 1 : 0;