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 if (oldname != NULL)
70 strlcpy(p.old, oldname, sizeof(p.old));
72 if (newname != NULL)
73 strlcpy(p.new, newname, sizeof(p.new));
75 p.git = git;
76 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
77 &p, sizeof(p)) == -1)
78 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
79 return NULL;
80 }
82 static const struct got_error *
83 send_patch_done(void)
84 {
85 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
86 NULL, 0) == -1)
87 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
88 if (imsg_flush(&ibuf) == -1)
89 return got_error_from_errno("imsg_flush");
90 return NULL;
91 }
93 /* based on fetchname from usr.bin/patch/util.c */
94 static const struct got_error *
95 filename(const char *at, char **name)
96 {
97 char *tmp, *t;
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 tmp = strdup(at);
111 if (tmp == NULL)
112 return got_error_from_errno("strdup");
113 if ((t = strchr(tmp, '\t')) != NULL)
114 *t = '\0';
115 if ((t = strchr(tmp, '\n')) != NULL)
116 *t = '\0';
118 *name = strdup(tmp);
119 free(tmp);
120 if (*name == NULL)
121 return got_error_from_errno("strdup");
122 return NULL;
125 static const struct got_error *
126 find_patch(FILE *fp)
128 const struct got_error *err = NULL;
129 char *old = NULL, *new = NULL;
130 char *line = NULL;
131 size_t linesize = 0;
132 ssize_t linelen;
133 int create, git = 0;
135 while ((linelen = getline(&line, &linesize, fp)) != -1) {
136 /*
137 * Ignore the Index name like GNU and larry' patch,
138 * we don't have to follow POSIX.
139 */
141 if (!strncmp(line, "--- ", 4)) {
142 free(old);
143 err = filename(line+4, &old);
144 } else if (!strncmp(line, "+++ ", 4)) {
145 free(new);
146 err = filename(line+4, &new);
147 } else if (!strncmp(line, "diff --git a/", 13))
148 git = 1;
150 if (err)
151 break;
153 if (!strncmp(line, "@@ -", 4)) {
154 create = !strncmp(line+4, "0,0", 3);
155 if ((old == NULL && new == NULL) ||
156 (!create && old == NULL))
157 err = got_error(GOT_ERR_PATCH_MALFORMED);
158 else
159 err = send_patch(old, new, git);
161 if (err)
162 break;
164 /* rewind to previous line */
165 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
166 err = got_error_from_errno("fseek");
167 break;
171 free(old);
172 free(new);
173 free(line);
174 if (ferror(fp) && err == NULL)
175 err = got_error_from_errno("getline");
176 if (feof(fp) && err == NULL)
177 err = got_error(GOT_ERR_NO_PATCH);
178 return err;
181 static const struct got_error *
182 strtolnum(char **str, long *n)
184 char *p, c;
185 const char *errstr;
187 for (p = *str; isdigit((unsigned char)*p); ++p)
188 /* nop */;
190 c = *p;
191 *p = '\0';
193 *n = strtonum(*str, 0, LONG_MAX, &errstr);
194 if (errstr != NULL)
195 return got_error(GOT_ERR_PATCH_MALFORMED);
197 *p = c;
198 *str = p;
199 return NULL;
202 static const struct got_error *
203 parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
205 static const struct got_error *err = NULL;
207 *ok = 1;
208 if (strncmp(s, "@@ -", 4)) {
209 *ok = 0;
210 return NULL;
213 s += 4;
214 if (!*s)
215 return NULL;
216 err = strtolnum(&s, &hdr->oldfrom);
217 if (err)
218 return err;
219 if (*s == ',') {
220 s++;
221 err = strtolnum(&s, &hdr->oldlines);
222 if (err)
223 return err;
224 } else
225 hdr->oldlines = 1;
227 if (*s == ' ')
228 s++;
230 if (*s != '+' || !*++s)
231 return got_error(GOT_ERR_PATCH_MALFORMED);
232 err = strtolnum(&s, &hdr->newfrom);
233 if (err)
234 return err;
235 if (*s == ',') {
236 s++;
237 err = strtolnum(&s, &hdr->newlines);
238 if (err)
239 return err;
240 } else
241 hdr->newlines = 1;
243 if (*s == ' ')
244 s++;
246 if (*s != '@')
247 return got_error(GOT_ERR_PATCH_MALFORMED);
249 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
250 hdr->newfrom >= LONG_MAX - hdr->newlines ||
251 /* not so sure about this one */
252 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
253 return got_error(GOT_ERR_PATCH_MALFORMED);
255 if (hdr->oldlines == 0) {
256 /* larry says to "do append rather than insert"; I don't
257 * quite get it, but i trust him.
258 */
259 hdr->oldfrom++;
262 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
263 hdr, sizeof(*hdr)) == -1)
264 return got_error_from_errno(
265 "imsg_compose GOT_IMSG_PATCH_HUNK");
266 return NULL;
269 static const struct got_error *
270 send_line(const char *line)
272 static const struct got_error *err = NULL;
273 char *p = NULL;
275 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
276 if (asprintf(&p, " %s", line) == -1)
277 return got_error_from_errno("asprintf");
278 line = p;
281 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
282 line, strlen(line) + 1) == -1)
283 err = got_error_from_errno(
284 "imsg_compose GOT_IMSG_PATCH_LINE");
286 free(p);
287 return err;
290 static const struct got_error *
291 peek_special_line(FILE *fp)
293 const struct got_error *err;
294 int ch;
296 ch = fgetc(fp);
297 if (ch != EOF && ch != '\\') {
298 ungetc(ch, fp);
299 return NULL;
302 if (ch == '\\') {
303 err = send_line("\\");
304 if (err)
305 return err;
308 while (ch != EOF && ch != '\n')
309 ch = fgetc(fp);
311 if (ch != EOF || feof(fp))
312 return NULL;
313 return got_error(GOT_ERR_IO);
316 static const struct got_error *
317 parse_hunk(FILE *fp, int *ok)
319 static const struct got_error *err = NULL;
320 struct got_imsg_patch_hunk hdr;
321 char *line = NULL, ch;
322 size_t linesize = 0;
323 ssize_t linelen;
324 long leftold, leftnew;
326 linelen = getline(&line, &linesize, fp);
327 if (linelen == -1) {
328 *ok = 0;
329 goto done;
332 err = parse_hdr(line, ok, &hdr);
333 if (err)
334 goto done;
335 if (!*ok) {
336 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
337 err = got_error_from_errno("fseek");
338 goto done;
341 leftold = hdr.oldlines;
342 leftnew = hdr.newlines;
344 while (leftold > 0 || leftnew > 0) {
345 linelen = getline(&line, &linesize, fp);
346 if (linelen == -1) {
347 if (ferror(fp)) {
348 err = got_error_from_errno("getline");
349 goto done;
352 /* trailing newlines may be chopped */
353 if (leftold < 3 && leftnew < 3) {
354 *ok = 0;
355 break;
358 err = got_error(GOT_ERR_PATCH_TRUNCATED);
359 goto done;
361 if (line[linelen - 1] == '\n')
362 line[linelen - 1] = '\0';
364 /* usr.bin/patch allows '=' as context char */
365 if (*line == '=')
366 *line = ' ';
368 ch = *line;
369 if (ch == '\t' || ch == '\0')
370 ch = ' '; /* the space got eaten */
372 switch (ch) {
373 case '-':
374 leftold--;
375 break;
376 case ' ':
377 leftold--;
378 leftnew--;
379 break;
380 case '+':
381 leftnew--;
382 break;
383 default:
384 err = got_error(GOT_ERR_PATCH_MALFORMED);
385 goto done;
388 if (leftold < 0 || leftnew < 0) {
389 err = got_error(GOT_ERR_PATCH_MALFORMED);
390 goto done;
393 err = send_line(line);
394 if (err)
395 goto done;
397 if ((ch == '-' && leftold == 0) ||
398 (ch == '+' && leftnew == 0)) {
399 err = peek_special_line(fp);
400 if (err)
401 goto done;
405 done:
406 free(line);
407 return err;
410 static const struct got_error *
411 read_patch(struct imsgbuf *ibuf, int fd)
413 const struct got_error *err = NULL;
414 FILE *fp;
415 int ok, patch_found = 0;
417 if ((fp = fdopen(fd, "r")) == NULL) {
418 err = got_error_from_errno("fdopen");
419 close(fd);
420 return err;
423 while (!feof(fp)) {
424 err = find_patch(fp);
425 if (err)
426 goto done;
428 patch_found = 1;
429 for (;;) {
430 err = parse_hunk(fp, &ok);
431 if (err)
432 goto done;
433 if (!ok) {
434 err = send_patch_done();
435 if (err)
436 goto done;
437 break;
442 done:
443 fclose(fp);
445 /* ignore trailing gibberish */
446 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
447 err = NULL;
449 return err;
452 int
453 main(int argc, char **argv)
455 const struct got_error *err = NULL;
456 struct imsg imsg;
457 #if 0
458 static int attached;
459 while (!attached)
460 sleep(1);
461 #endif
463 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
464 #ifndef PROFILE
465 /* revoke access to most system calls */
466 if (pledge("stdio recvfd", NULL) == -1) {
467 err = got_error_from_errno("pledge");
468 got_privsep_send_error(&ibuf, err);
469 return 1;
471 #endif
473 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
474 if (err)
475 goto done;
476 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
477 err = got_error(GOT_ERR_PRIVSEP_MSG);
478 goto done;
481 err = read_patch(&ibuf, imsg.fd);
482 if (err)
483 goto done;
484 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
485 NULL, 0) == -1) {
486 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
487 goto done;
489 err = got_privsep_flush_imsg(&ibuf);
490 done:
491 imsg_free(&imsg);
492 if (err != NULL) {
493 got_privsep_send_error(&ibuf, err);
494 err = NULL;
496 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
497 err = got_error_from_errno("close");
498 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
499 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
500 return err ? 1 : 0;