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)
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));
71 if (newname != NULL)
72 strlcpy(p.new, newname, sizeof(p.new));
74 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
75 &p, sizeof(p)) == -1)
76 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
77 return NULL;
78 }
80 static const struct got_error *
81 send_patch_done(void)
82 {
83 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
84 NULL, 0) == -1)
85 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
86 if (imsg_flush(&ibuf) == -1)
87 return got_error_from_errno("imsg_flush");
88 return NULL;
89 }
91 /* based on fetchname from usr.bin/patch/util.c */
92 static const struct got_error *
93 filename(const char *at, char **name, int strip)
94 {
95 char *fullname, *t;
96 int l, tab;
98 *name = NULL;
99 if (*at == '\0')
100 return NULL;
102 while (isspace((unsigned char)*at))
103 at++;
105 /* files can be created or removed by diffing against /dev/null */
106 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL)-1))
107 return NULL;
109 t = strdup(at);
110 if (t == NULL)
111 return got_error_from_errno("strdup");
112 *name = fullname = t;
113 tab = strchr(t, '\t') != NULL;
115 /* strip off path components and NUL-terminate */
116 for (l = strip;
117 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
118 ++t) {
119 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
120 if (--l >= 0)
121 *name = t+1;
123 *t = '\0';
125 *name = strdup(*name);
126 free(fullname);
127 if (*name == NULL)
128 return got_error_from_errno("strdup");
129 return NULL;
132 static const struct got_error *
133 find_patch(FILE *fp)
135 const struct got_error *err = NULL;
136 char *old = NULL, *new = NULL;
137 char *line = NULL;
138 size_t linesize = 0;
139 ssize_t linelen;
140 int create, git = 0;
142 while ((linelen = getline(&line, &linesize, fp)) != -1) {
143 /*
144 * Ignore the Index name like GNU and larry' patch,
145 * we don't have to follow POSIX.
146 */
148 if (git && !strncmp(line, "--- a/", 6)) {
149 free(old);
150 err = filename(line+6, &old, 0);
151 } else if (!strncmp(line, "--- ", 4)) {
152 free(old);
153 err = filename(line+4, &old, 0);
154 } else if (git && !strncmp(line, "+++ b/", 6)) {
155 free(new);
156 err = filename(line+6, &new, 0);
157 } else if (!strncmp(line, "+++ ", 4)) {
158 free(new);
159 err = filename(line+4, &new, 0);
160 } else if (!strncmp(line, "diff --git a/", 13))
161 git = 1;
163 if (err)
164 break;
166 if (!strncmp(line, "@@ -", 4)) {
167 create = !strncmp(line+4, "0,0", 3);
168 if ((old == NULL && new == NULL) ||
169 (!create && old == NULL))
170 err = got_error(GOT_ERR_PATCH_MALFORMED);
171 else
172 err = send_patch(old, new);
174 if (err)
175 break;
177 /* rewind to previous line */
178 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
179 err = got_error_from_errno("fseek");
180 break;
184 free(old);
185 free(new);
186 free(line);
187 if (ferror(fp) && err == NULL)
188 err = got_error_from_errno("getline");
189 if (feof(fp) && err == NULL)
190 err = got_error(GOT_ERR_NO_PATCH);
191 return err;
194 static const struct got_error *
195 strtolnum(char **str, long *n)
197 char *p, c;
198 const char *errstr;
200 for (p = *str; isdigit((unsigned char)*p); ++p)
201 /* nop */;
203 c = *p;
204 *p = '\0';
206 *n = strtonum(*str, 0, LONG_MAX, &errstr);
207 if (errstr != NULL)
208 return got_error(GOT_ERR_PATCH_MALFORMED);
210 *p = c;
211 *str = p;
212 return NULL;
215 static const struct got_error *
216 parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
218 static const struct got_error *err = NULL;
220 *ok = 1;
221 if (strncmp(s, "@@ -", 4)) {
222 *ok = 0;
223 return NULL;
226 s += 4;
227 if (!*s)
228 return NULL;
229 err = strtolnum(&s, &hdr->oldfrom);
230 if (err)
231 return err;
232 if (*s == ',') {
233 s++;
234 err = strtolnum(&s, &hdr->oldlines);
235 if (err)
236 return err;
237 } else
238 hdr->oldlines = 1;
240 if (*s == ' ')
241 s++;
243 if (*s != '+' || !*++s)
244 return got_error(GOT_ERR_PATCH_MALFORMED);
245 err = strtolnum(&s, &hdr->newfrom);
246 if (err)
247 return err;
248 if (*s == ',') {
249 s++;
250 err = strtolnum(&s, &hdr->newlines);
251 if (err)
252 return err;
253 } else
254 hdr->newlines = 1;
256 if (*s == ' ')
257 s++;
259 if (*s != '@')
260 return got_error(GOT_ERR_PATCH_MALFORMED);
262 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
263 hdr->newfrom >= LONG_MAX - hdr->newlines ||
264 /* not so sure about this one */
265 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
266 return got_error(GOT_ERR_PATCH_MALFORMED);
268 if (hdr->oldlines == 0) {
269 /* larry says to "do append rather than insert"; I don't
270 * quite get it, but i trust him.
271 */
272 hdr->oldfrom++;
275 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
276 hdr, sizeof(*hdr)) == -1)
277 return got_error_from_errno(
278 "imsg_compose GOT_IMSG_PATCH_HUNK");
279 return NULL;
282 static const struct got_error *
283 send_line(const char *line)
285 static const struct got_error *err = NULL;
286 char *p = NULL;
288 if (*line != '+' && *line != '-' && *line != ' ') {
289 if (asprintf(&p, " %s", line) == -1)
290 return got_error_from_errno("asprintf");
291 line = p;
294 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
295 line, strlen(line)+1) == -1)
296 err = got_error_from_errno(
297 "imsg_compose GOT_IMSG_PATCH_LINE");
299 free(p);
300 return err;
303 static const struct got_error *
304 parse_hunk(FILE *fp, int *ok)
306 static const struct got_error *err = NULL;
307 struct got_imsg_patch_hunk hdr;
308 char *line = NULL, ch;
309 size_t linesize = 0;
310 ssize_t linelen;
311 long leftold, leftnew;
313 linelen = getline(&line, &linesize, fp);
314 if (linelen == -1) {
315 *ok = 0;
316 goto done;
319 err = parse_hdr(line, ok, &hdr);
320 if (err)
321 goto done;
322 if (!*ok) {
323 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
324 err = got_error_from_errno("fseek");
325 goto done;
328 leftold = hdr.oldlines;
329 leftnew = hdr.newlines;
331 while (leftold > 0 || leftnew > 0) {
332 linelen = getline(&line, &linesize, fp);
333 if (linelen == -1) {
334 if (ferror(fp)) {
335 err = got_error_from_errno("getline");
336 goto done;
339 /* trailing newlines may be chopped */
340 if (leftold < 3 && leftnew < 3) {
341 *ok = 0;
342 break;
345 err = got_error(GOT_ERR_PATCH_TRUNCATED);
346 goto done;
349 /* usr.bin/patch allows '=' as context char */
350 if (*line == '=')
351 *line = ' ';
353 ch = *line;
354 if (ch == '\t' || ch == '\n')
355 ch = ' '; /* the space got eaten */
357 switch (ch) {
358 case '-':
359 leftold--;
360 break;
361 case ' ':
362 leftold--;
363 leftnew--;
364 break;
365 case '+':
366 leftnew--;
367 break;
368 default:
369 err = got_error(GOT_ERR_PATCH_MALFORMED);
370 goto done;
373 if (leftold < 0 || leftnew < 0) {
374 err = got_error(GOT_ERR_PATCH_MALFORMED);
375 goto done;
378 err = send_line(line);
379 if (err)
380 goto done;
383 done:
384 free(line);
385 return err;
388 static const struct got_error *
389 read_patch(struct imsgbuf *ibuf, int fd)
391 const struct got_error *err = NULL;
392 FILE *fp;
393 int ok, patch_found = 0;
395 if ((fp = fdopen(fd, "r")) == NULL) {
396 err = got_error_from_errno("fdopen");
397 close(fd);
398 return err;
401 while (!feof(fp)) {
402 err = find_patch(fp);
403 if (err)
404 goto done;
406 patch_found = 1;
407 for (;;) {
408 err = parse_hunk(fp, &ok);
409 if (err)
410 goto done;
411 if (!ok) {
412 err = send_patch_done();
413 if (err)
414 goto done;
415 break;
420 done:
421 fclose(fp);
423 /* ignore trailing gibberish */
424 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
425 err = NULL;
427 return err;
430 int
431 main(int argc, char **argv)
433 const struct got_error *err = NULL;
434 struct imsg imsg;
435 #if 0
436 static int attached;
437 while (!attached)
438 sleep(1);
439 #endif
441 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
442 #ifndef PROFILE
443 /* revoke access to most system calls */
444 if (pledge("stdio recvfd", NULL) == -1) {
445 err = got_error_from_errno("pledge");
446 got_privsep_send_error(&ibuf, err);
447 return 1;
449 #endif
451 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
452 if (err)
453 goto done;
454 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
455 err = got_error(GOT_ERR_PRIVSEP_MSG);
456 goto done;
459 err = read_patch(&ibuf, imsg.fd);
460 if (err)
461 goto done;
462 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
463 NULL, 0) == -1) {
464 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
465 goto done;
467 err = got_privsep_flush_imsg(&ibuf);
468 done:
469 imsg_free(&imsg);
470 if (err != NULL) {
471 got_privsep_send_error(&ibuf, err);
472 err = NULL;
474 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
475 err = got_error_from_errno("close");
476 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
477 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
478 return err ? 1 : 0;