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"
59 55e9459f 2022-06-19 op #include "got_lib_sha1.h"
61 e9ce266e 2022-03-07 op struct imsgbuf ibuf;
63 e9ce266e 2022-03-07 op static const struct got_error *
64 d8b5af43 2022-06-19 op send_patch(const char *oldname, const char *newname, const char *commitid,
65 d8b5af43 2022-06-19 op const char *blob, int git)
67 e9ce266e 2022-03-07 op struct got_imsg_patch p;
69 e9ce266e 2022-03-07 op memset(&p, 0, sizeof(p));
71 9d6cabd5 2022-04-07 op if (oldname != NULL)
72 e9ce266e 2022-03-07 op strlcpy(p.old, oldname, sizeof(p.old));
74 e9ce266e 2022-03-07 op if (newname != NULL)
75 e9ce266e 2022-03-07 op strlcpy(p.new, newname, sizeof(p.new));
77 497a5915 2022-06-27 op if (commitid != NULL)
78 d8b5af43 2022-06-19 op strlcpy(p.cid, commitid, sizeof(p.cid));
80 497a5915 2022-06-27 op if (blob != NULL)
81 55e9459f 2022-06-19 op strlcpy(p.blob, blob, sizeof(p.blob));
84 55e9459f 2022-06-19 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
85 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
89 e9ce266e 2022-03-07 op static const struct got_error *
90 e9ce266e 2022-03-07 op send_patch_done(void)
92 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
93 e9ce266e 2022-03-07 op NULL, 0) == -1)
94 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
95 e9ce266e 2022-03-07 op if (imsg_flush(&ibuf) == -1)
96 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_flush");
100 e9ce266e 2022-03-07 op /* based on fetchname from usr.bin/patch/util.c */
101 e9ce266e 2022-03-07 op static const struct got_error *
102 9d6cabd5 2022-04-07 op filename(const char *at, char **name)
104 9d6cabd5 2022-04-07 op char *tmp, *t;
106 e9ce266e 2022-03-07 op *name = NULL;
107 e9ce266e 2022-03-07 op if (*at == '\0')
110 e9ce266e 2022-03-07 op while (isspace((unsigned char)*at))
113 e9ce266e 2022-03-07 op /* files can be created or removed by diffing against /dev/null */
114 46ebad13 2022-03-17 op if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
117 9d6cabd5 2022-04-07 op tmp = strdup(at);
118 9d6cabd5 2022-04-07 op if (tmp == NULL)
119 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
120 9d6cabd5 2022-04-07 op if ((t = strchr(tmp, '\t')) != NULL)
122 9d6cabd5 2022-04-07 op if ((t = strchr(tmp, '\n')) != NULL)
125 9d6cabd5 2022-04-07 op *name = strdup(tmp);
127 e9ce266e 2022-03-07 op if (*name == NULL)
128 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
132 e9ce266e 2022-03-07 op static const struct got_error *
133 db0dfdd7 2022-06-27 op blobid(const char *line, char **blob, int git)
135 55e9459f 2022-06-19 op uint8_t digest[SHA1_DIGEST_LENGTH];
138 55e9459f 2022-06-19 op *blob = NULL;
140 55e9459f 2022-06-19 op len = strspn(line, "0123456789abcdefABCDEF");
141 55e9459f 2022-06-19 op if ((*blob = strndup(line, len)) == NULL)
142 55e9459f 2022-06-19 op return got_error_from_errno("strndup");
144 db0dfdd7 2022-06-27 op if (!git && !got_parse_sha1_digest(digest, *blob)) {
145 55e9459f 2022-06-19 op /* silently ignore invalid blob ids */
147 55e9459f 2022-06-19 op *blob = NULL;
152 55e9459f 2022-06-19 op static const struct got_error *
153 acf749fc 2022-07-02 op patch_start(int *git, char **cid, FILE *fp)
155 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
156 e9ce266e 2022-03-07 op char *line = NULL;
157 e9ce266e 2022-03-07 op size_t linesize = 0;
158 e9ce266e 2022-03-07 op ssize_t linelen;
162 acf749fc 2022-07-02 op while ((linelen = getline(&line, &linesize, fp)) != -1) {
163 acf749fc 2022-07-02 op if (!strncmp(line, "diff --git ", 11)) {
168 acf749fc 2022-07-02 op } else if (!strncmp(line, "diff ", 5)) {
172 acf749fc 2022-07-02 op } else if (!strncmp(line, "commit - ", 9)) {
174 acf749fc 2022-07-02 op err = blobid(line + 9, cid, *git);
177 acf749fc 2022-07-02 op } else if (!strncmp(line, "--- ", 4) ||
178 acf749fc 2022-07-02 op !strncmp(line, "+++ ", 4) ||
179 acf749fc 2022-07-02 op !strncmp(line, "blob - ", 7)) {
180 acf749fc 2022-07-02 op /* rewind to previous line */
181 acf749fc 2022-07-02 op if (fseeko(fp, -linelen, SEEK_CUR) == -1)
182 acf749fc 2022-07-02 op err = got_error_from_errno("fseeko");
188 acf749fc 2022-07-02 op if (ferror(fp) && err == NULL)
189 acf749fc 2022-07-02 op err = got_error_from_errno("getline");
190 acf749fc 2022-07-02 op if (feof(fp) && err == NULL)
191 acf749fc 2022-07-02 op err = got_error(GOT_ERR_NO_PATCH);
195 acf749fc 2022-07-02 op static const struct got_error *
196 acf749fc 2022-07-02 op find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
198 acf749fc 2022-07-02 op const struct got_error *err = NULL;
199 acf749fc 2022-07-02 op char *old = NULL, *new = NULL;
200 acf749fc 2022-07-02 op char *blob = NULL;
201 acf749fc 2022-07-02 op char *line = NULL;
202 acf749fc 2022-07-02 op size_t linesize = 0;
203 acf749fc 2022-07-02 op ssize_t linelen;
204 acf749fc 2022-07-02 op int create, rename = 0;
208 e9ce266e 2022-03-07 op while ((linelen = getline(&line, &linesize, fp)) != -1) {
210 e9ce266e 2022-03-07 op * Ignore the Index name like GNU and larry' patch,
211 e9ce266e 2022-03-07 op * we don't have to follow POSIX.
214 9d6cabd5 2022-04-07 op if (!strncmp(line, "--- ", 4)) {
216 9d6cabd5 2022-04-07 op err = filename(line+4, &old);
217 4379a9aa 2022-05-02 op } else if (rename && !strncmp(line, "rename from ", 12)) {
219 4379a9aa 2022-05-02 op err = filename(line+12, &old);
220 e9ce266e 2022-03-07 op } else if (!strncmp(line, "+++ ", 4)) {
222 9d6cabd5 2022-04-07 op err = filename(line+4, &new);
223 55e9459f 2022-06-19 op } else if (!git && !strncmp(line, "blob - ", 7)) {
225 db0dfdd7 2022-06-27 op err = blobid(line + 7, &blob, git);
226 4379a9aa 2022-05-02 op } else if (rename && !strncmp(line, "rename to ", 10)) {
228 4379a9aa 2022-05-02 op err = filename(line + 10, &new);
229 4379a9aa 2022-05-02 op } else if (git && !strncmp(line, "similarity index 100%", 21))
231 db0dfdd7 2022-06-27 op else if (git && !strncmp(line, "index ", 6)) {
233 db0dfdd7 2022-06-27 op err = blobid(line + 6, &blob, git);
234 acf749fc 2022-07-02 op } else if (!strncmp(line, "diff ", 5)) {
235 acf749fc 2022-07-02 op /* rewind to previous line */
236 acf749fc 2022-07-02 op if (fseeko(fp, -linelen, SEEK_CUR) == -1)
237 acf749fc 2022-07-02 op err = got_error_from_errno("fseeko");
246 4379a9aa 2022-05-02 op * Git-style diffs with "similarity index 100%" don't
247 4379a9aa 2022-05-02 op * have any hunks and ends with the "rename to foobar"
250 4379a9aa 2022-05-02 op if (rename && old != NULL && new != NULL) {
252 d8b5af43 2022-06-19 op err = send_patch(old, new, commitid,
257 e9ce266e 2022-03-07 op if (!strncmp(line, "@@ -", 4)) {
258 e9ce266e 2022-03-07 op create = !strncmp(line+4, "0,0", 3);
259 e9ce266e 2022-03-07 op if ((old == NULL && new == NULL) ||
260 e9ce266e 2022-03-07 op (!create && old == NULL))
261 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
263 d8b5af43 2022-06-19 op err = send_patch(old, new, commitid,
269 e9ce266e 2022-03-07 op /* rewind to previous line */
270 e45f7eba 2022-05-14 naddy if (fseeko(fp, -linelen, SEEK_CUR) == -1)
271 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
280 e9ce266e 2022-03-07 op if (ferror(fp) && err == NULL)
281 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
282 e9ce266e 2022-03-07 op if (feof(fp) && err == NULL)
283 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_NO_PATCH);
287 e9ce266e 2022-03-07 op static const struct got_error *
288 35095610 2022-06-14 op strtolnum(char **str, int *n)
291 e9ce266e 2022-03-07 op const char *errstr;
293 e9ce266e 2022-03-07 op for (p = *str; isdigit((unsigned char)*p); ++p)
299 35095610 2022-06-14 op *n = strtonum(*str, 0, INT_MAX, &errstr);
300 e9ce266e 2022-03-07 op if (errstr != NULL)
301 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
308 e9ce266e 2022-03-07 op static const struct got_error *
309 d75b9573 2022-05-02 op parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
311 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
313 e9ce266e 2022-03-07 op if (strncmp(s, "@@ -", 4)) {
321 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->oldfrom);
324 e9ce266e 2022-03-07 op if (*s == ',') {
326 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->oldlines);
330 e9ce266e 2022-03-07 op hdr->oldlines = 1;
332 e9ce266e 2022-03-07 op if (*s == ' ')
335 e9ce266e 2022-03-07 op if (*s != '+' || !*++s)
336 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
337 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->newfrom);
340 e9ce266e 2022-03-07 op if (*s == ',') {
342 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->newlines);
346 e9ce266e 2022-03-07 op hdr->newlines = 1;
348 e9ce266e 2022-03-07 op if (*s == ' ')
351 e9ce266e 2022-03-07 op if (*s != '@')
352 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
354 35095610 2022-06-14 op if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
355 35095610 2022-06-14 op hdr->newfrom >= INT_MAX - hdr->newlines ||
356 e9ce266e 2022-03-07 op /* not so sure about this one */
357 35095610 2022-06-14 op hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
358 be33dff7 2022-05-13 op (hdr->oldlines == 0 && hdr->newlines == 0))
359 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
361 e9ce266e 2022-03-07 op if (hdr->oldlines == 0) {
362 e9ce266e 2022-03-07 op /* larry says to "do append rather than insert"; I don't
363 e9ce266e 2022-03-07 op * quite get it, but i trust him.
365 e9ce266e 2022-03-07 op hdr->oldfrom++;
368 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
369 e9ce266e 2022-03-07 op hdr, sizeof(*hdr)) == -1)
370 e9ce266e 2022-03-07 op return got_error_from_errno(
371 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_HUNK");
375 e9ce266e 2022-03-07 op static const struct got_error *
376 e9ce266e 2022-03-07 op send_line(const char *line)
378 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
379 e9ce266e 2022-03-07 op char *p = NULL;
381 b3c57ab2 2022-03-22 op if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
382 e9ce266e 2022-03-07 op if (asprintf(&p, " %s", line) == -1)
383 e9ce266e 2022-03-07 op return got_error_from_errno("asprintf");
387 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
388 46ebad13 2022-03-17 op line, strlen(line) + 1) == -1)
389 e9ce266e 2022-03-07 op err = got_error_from_errno(
390 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_LINE");
396 e9ce266e 2022-03-07 op static const struct got_error *
397 b2832778 2022-04-23 op peek_special_line(FILE *fp)
399 b3c57ab2 2022-03-22 op const struct got_error *err;
402 b3c57ab2 2022-03-22 op ch = fgetc(fp);
403 b3c57ab2 2022-03-22 op if (ch != EOF && ch != '\\') {
404 b3c57ab2 2022-03-22 op ungetc(ch, fp);
408 b2832778 2022-04-23 op if (ch == '\\') {
409 b3c57ab2 2022-03-22 op err = send_line("\\");
414 b3c57ab2 2022-03-22 op while (ch != EOF && ch != '\n')
415 b3c57ab2 2022-03-22 op ch = fgetc(fp);
417 b3c57ab2 2022-03-22 op if (ch != EOF || feof(fp))
419 b3c57ab2 2022-03-22 op return got_error(GOT_ERR_IO);
422 b3c57ab2 2022-03-22 op static const struct got_error *
423 d75b9573 2022-05-02 op parse_hunk(FILE *fp, int *done)
425 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
426 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
427 e9ce266e 2022-03-07 op char *line = NULL, ch;
428 e9ce266e 2022-03-07 op size_t linesize = 0;
429 e9ce266e 2022-03-07 op ssize_t linelen;
430 35095610 2022-06-14 op int leftold, leftnew;
432 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, fp);
433 e9ce266e 2022-03-07 op if (linelen == -1) {
438 d75b9573 2022-05-02 op err = parse_hdr(line, done, &hdr);
442 e45f7eba 2022-05-14 naddy if (fseeko(fp, -linelen, SEEK_CUR) == -1)
443 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
447 e9ce266e 2022-03-07 op leftold = hdr.oldlines;
448 e9ce266e 2022-03-07 op leftnew = hdr.newlines;
450 e9ce266e 2022-03-07 op while (leftold > 0 || leftnew > 0) {
451 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, fp);
452 e9ce266e 2022-03-07 op if (linelen == -1) {
453 e9ce266e 2022-03-07 op if (ferror(fp)) {
454 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
458 e9ce266e 2022-03-07 op /* trailing newlines may be chopped */
459 e9ce266e 2022-03-07 op if (leftold < 3 && leftnew < 3) {
464 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_TRUNCATED);
467 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
468 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
470 e9ce266e 2022-03-07 op /* usr.bin/patch allows '=' as context char */
471 e9ce266e 2022-03-07 op if (*line == '=')
475 b3c57ab2 2022-03-22 op if (ch == '\t' || ch == '\0')
476 e9ce266e 2022-03-07 op ch = ' '; /* the space got eaten */
478 e9ce266e 2022-03-07 op switch (ch) {
490 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
494 e9ce266e 2022-03-07 op if (leftold < 0 || leftnew < 0) {
495 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
499 e9ce266e 2022-03-07 op err = send_line(line);
503 b3c57ab2 2022-03-22 op if ((ch == '-' && leftold == 0) ||
504 b3c57ab2 2022-03-22 op (ch == '+' && leftnew == 0)) {
505 b2832778 2022-04-23 op err = peek_special_line(fp);
516 e9ce266e 2022-03-07 op static const struct got_error *
517 e9ce266e 2022-03-07 op read_patch(struct imsgbuf *ibuf, int fd)
519 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
521 acf749fc 2022-07-02 op int git, patch_found = 0;
522 acf749fc 2022-07-02 op char *cid = NULL;
524 e9ce266e 2022-03-07 op if ((fp = fdopen(fd, "r")) == NULL) {
525 e9ce266e 2022-03-07 op err = got_error_from_errno("fdopen");
530 acf749fc 2022-07-02 op while ((err = patch_start(&git, &cid, fp)) == NULL) {
531 acf749fc 2022-07-02 op int done, next;
533 acf749fc 2022-07-02 op err = find_diff(&done, &next, fp, git, cid);
539 e9ce266e 2022-03-07 op patch_found = 1;
541 d75b9573 2022-05-02 op while (!done) {
542 d75b9573 2022-05-02 op err = parse_hunk(fp, &done);
547 d75b9573 2022-05-02 op err = send_patch_done();
556 e9ce266e 2022-03-07 op /* ignore trailing gibberish */
557 e9ce266e 2022-03-07 op if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
564 e9ce266e 2022-03-07 op main(int argc, char **argv)
566 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
567 e9ce266e 2022-03-07 op struct imsg imsg;
569 e9ce266e 2022-03-07 op static int attached;
570 e9ce266e 2022-03-07 op while (!attached)
574 e9ce266e 2022-03-07 op imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
575 e9ce266e 2022-03-07 op #ifndef PROFILE
576 e9ce266e 2022-03-07 op /* revoke access to most system calls */
577 e9ce266e 2022-03-07 op if (pledge("stdio recvfd", NULL) == -1) {
578 e9ce266e 2022-03-07 op err = got_error_from_errno("pledge");
579 e9ce266e 2022-03-07 op got_privsep_send_error(&ibuf, err);
584 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
587 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
588 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
592 e9ce266e 2022-03-07 op err = read_patch(&ibuf, imsg.fd);
595 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
596 e9ce266e 2022-03-07 op NULL, 0) == -1) {
597 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
600 e9ce266e 2022-03-07 op err = got_privsep_flush_imsg(&ibuf);
602 e9ce266e 2022-03-07 op imsg_free(&imsg);
603 e9ce266e 2022-03-07 op if (err != NULL) {
604 e9ce266e 2022-03-07 op got_privsep_send_error(&ibuf, err);
607 e9ce266e 2022-03-07 op if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
608 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
609 e9ce266e 2022-03-07 op if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
610 e9ce266e 2022-03-07 op fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
611 e9ce266e 2022-03-07 op return err ? 1 : 0;