2 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 e9ce266e 2022-03-07 op * Permission to use, copy, modify, and distribute this software for any
5 e9ce266e 2022-03-07 op * purpose with or without fee is hereby granted, provided that the above
6 e9ce266e 2022-03-07 op * copyright notice and this permission notice appear in all copies.
8 e9ce266e 2022-03-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e9ce266e 2022-03-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e9ce266e 2022-03-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e9ce266e 2022-03-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e9ce266e 2022-03-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e9ce266e 2022-03-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e9ce266e 2022-03-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 e9ce266e 2022-03-07 op * Apply patches.
18 e9ce266e 2022-03-07 op * Things that are still missing:
19 e9ce266e 2022-03-07 op * + "No final newline" handling
21 e9ce266e 2022-03-07 op * Things that we may want to support:
22 e9ce266e 2022-03-07 op * + support indented patches?
23 e9ce266e 2022-03-07 op * + support other kinds of patches?
26 e9ce266e 2022-03-07 op #include <sys/types.h>
27 e9ce266e 2022-03-07 op #include <sys/queue.h>
28 e9ce266e 2022-03-07 op #include <sys/socket.h>
29 e9ce266e 2022-03-07 op #include <sys/uio.h>
31 e9ce266e 2022-03-07 op #include <limits.h>
32 e9ce266e 2022-03-07 op #include <sha1.h>
33 e9ce266e 2022-03-07 op #include <stdint.h>
34 e9ce266e 2022-03-07 op #include <stdio.h>
35 e9ce266e 2022-03-07 op #include <stdlib.h>
36 e9ce266e 2022-03-07 op #include <string.h>
37 e9ce266e 2022-03-07 op #include <unistd.h>
38 e9ce266e 2022-03-07 op #include <imsg.h>
40 e9ce266e 2022-03-07 op #include "got_error.h"
41 e9ce266e 2022-03-07 op #include "got_object.h"
42 e9ce266e 2022-03-07 op #include "got_path.h"
43 e9ce266e 2022-03-07 op #include "got_reference.h"
44 e9ce266e 2022-03-07 op #include "got_cancel.h"
45 e9ce266e 2022-03-07 op #include "got_worktree.h"
46 e9ce266e 2022-03-07 op #include "got_opentemp.h"
47 e9ce266e 2022-03-07 op #include "got_patch.h"
49 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
50 e9ce266e 2022-03-07 op #include "got_lib_object.h"
51 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
53 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 e9ce266e 2022-03-07 op struct got_patch_hunk {
56 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
57 e9ce266e 2022-03-07 op long old_from;
58 e9ce266e 2022-03-07 op long old_lines;
59 e9ce266e 2022-03-07 op long new_from;
60 e9ce266e 2022-03-07 op long new_lines;
66 e9ce266e 2022-03-07 op struct got_patch {
69 e9ce266e 2022-03-07 op STAILQ_HEAD(, got_patch_hunk) head;
72 e9ce266e 2022-03-07 op static const struct got_error *
73 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
75 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
77 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
78 e9ce266e 2022-03-07 op NULL, 0) == -1) {
79 e9ce266e 2022-03-07 op err = got_error_from_errno(
80 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
85 e9ce266e 2022-03-07 op if (imsg_flush(ibuf) == -1) {
86 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_flush");
87 e9ce266e 2022-03-07 op imsg_clear(ibuf);
94 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
96 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
99 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
100 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
101 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
103 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
104 e9ce266e 2022-03-07 op free(h->lines[i]);
105 e9ce266e 2022-03-07 op free(h->lines);
109 e9ce266e 2022-03-07 op free(p->new);
110 e9ce266e 2022-03-07 op free(p->old);
113 e9ce266e 2022-03-07 op static const struct got_error *
114 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
117 e9ce266e 2022-03-07 op size_t newcap;
119 e9ce266e 2022-03-07 op if (h->len == h->cap) {
120 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
122 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
123 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
124 e9ce266e 2022-03-07 op if (t == NULL)
125 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
126 e9ce266e 2022-03-07 op h->lines = t;
127 e9ce266e 2022-03-07 op h->cap = newcap;
130 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
131 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
133 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
137 e9ce266e 2022-03-07 op static const struct got_error *
138 e9ce266e 2022-03-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
140 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
141 e9ce266e 2022-03-07 op struct imsg imsg;
142 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
143 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
144 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
145 e9ce266e 2022-03-07 op size_t datalen;
147 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
148 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
150 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
153 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
157 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
158 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
161 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
162 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
163 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
166 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
167 e9ce266e 2022-03-07 op if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
168 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
171 e9ce266e 2022-03-07 op if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
172 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
176 e9ce266e 2022-03-07 op imsg_free(&imsg);
181 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
185 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
186 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
188 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
189 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
190 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
191 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
194 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
195 e9ce266e 2022-03-07 op if ((h = calloc(1, sizeof(*h))) == NULL) {
196 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
199 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
200 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
201 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
202 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
203 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
205 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
206 e9ce266e 2022-03-07 op if (h == NULL) {
207 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
210 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
211 e9ce266e 2022-03-07 op t = imsg.data;
212 e9ce266e 2022-03-07 op /* at least one char plus newline */
213 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
214 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
217 e9ce266e 2022-03-07 op if (*t != ' ' && *t != '-' && *t != '+') {
218 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
221 e9ce266e 2022-03-07 op err = pushline(h, t);
226 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
230 e9ce266e 2022-03-07 op imsg_free(&imsg);
234 e9ce266e 2022-03-07 op imsg_free(&imsg);
239 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
240 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
242 e9ce266e 2022-03-07 op static const struct got_error *
243 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
245 e9ce266e 2022-03-07 op char buf[BUFSIZ];
246 e9ce266e 2022-03-07 op size_t len, r, w;
248 e9ce266e 2022-03-07 op if (fseek(orig, copypos, SEEK_SET) == -1)
249 e9ce266e 2022-03-07 op return got_error_from_errno("fseek");
251 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
252 e9ce266e 2022-03-07 op len = sizeof(buf);
254 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
255 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
256 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
257 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
258 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
260 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
261 e9ce266e 2022-03-07 op copypos += len;
262 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
263 e9ce266e 2022-03-07 op if (pos == -1)
265 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_DONT_APPLY);
271 e9ce266e 2022-03-07 op static const struct got_error *
272 e9ce266e 2022-03-07 op locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
274 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
275 e9ce266e 2022-03-07 op char *line = NULL;
276 e9ce266e 2022-03-07 op char mode = *h->lines[0];
277 e9ce266e 2022-03-07 op size_t linesize = 0;
278 e9ce266e 2022-03-07 op ssize_t linelen;
279 e9ce266e 2022-03-07 op off_t match = -1;
280 e9ce266e 2022-03-07 op long match_lineno = -1;
283 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
284 e9ce266e 2022-03-07 op if (linelen == -1) {
285 e9ce266e 2022-03-07 op if (ferror(orig))
286 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
287 e9ce266e 2022-03-07 op else if (match == -1)
288 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_DONT_APPLY);
293 e9ce266e 2022-03-07 op if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
294 e9ce266e 2022-03-07 op (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
295 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
296 e9ce266e 2022-03-07 op match = ftello(orig);
297 e9ce266e 2022-03-07 op if (match == -1) {
298 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
301 e9ce266e 2022-03-07 op match -= linelen;
302 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
305 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
309 e9ce266e 2022-03-07 op if (err == NULL) {
310 e9ce266e 2022-03-07 op *lineno = match_lineno;
311 e9ce266e 2022-03-07 op if (fseek(orig, match, SEEK_SET) == -1)
312 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
319 e9ce266e 2022-03-07 op static const struct got_error *
320 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
322 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
323 e9ce266e 2022-03-07 op char *line = NULL;
324 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
325 e9ce266e 2022-03-07 op ssize_t linelen;
327 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
328 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
333 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
334 e9ce266e 2022-03-07 op if (linelen == -1) {
335 e9ce266e 2022-03-07 op if (ferror(orig))
336 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
338 e9ce266e 2022-03-07 op err = got_error(
339 e9ce266e 2022-03-07 op GOT_ERR_PATCH_DONT_APPLY);
342 e9ce266e 2022-03-07 op if (strcmp(h->lines[i]+1, line)) {
343 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_DONT_APPLY);
355 e9ce266e 2022-03-07 op static const struct got_error *
356 e9ce266e 2022-03-07 op apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
358 e9ce266e 2022-03-07 op size_t i = 0;
360 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
361 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
363 e9ce266e 2022-03-07 op if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
364 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
365 e9ce266e 2022-03-07 op /* fallthrough */
370 e9ce266e 2022-03-07 op if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
371 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
378 e9ce266e 2022-03-07 op static const struct got_error *
379 e9ce266e 2022-03-07 op apply_patch(struct got_worktree *worktree, struct got_repository *repo,
380 d955343d 2022-03-08 op struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
381 d955343d 2022-03-08 op got_worktree_checkout_cb add_cb, void *add_arg)
383 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
384 e9ce266e 2022-03-07 op struct got_pathlist_head paths;
385 e9ce266e 2022-03-07 op struct got_pathlist_entry *pe;
386 6f5cb1bd 2022-03-08 op char *path = NULL, *tmppath = NULL, *template = NULL;
387 e9ce266e 2022-03-07 op FILE *orig = NULL, *tmp = NULL;
388 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
390 e9ce266e 2022-03-07 op long lineno = 0;
391 e9ce266e 2022-03-07 op off_t copypos, pos;
392 e9ce266e 2022-03-07 op char *line = NULL;
393 e9ce266e 2022-03-07 op size_t linesize = 0;
394 e9ce266e 2022-03-07 op ssize_t linelen;
396 e9ce266e 2022-03-07 op TAILQ_INIT(&paths);
398 e9ce266e 2022-03-07 op if (p->old == NULL && p->new == NULL)
399 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
401 e9ce266e 2022-03-07 op err = got_worktree_resolve_path(&path, worktree,
402 e9ce266e 2022-03-07 op p->new != NULL ? p->new : p->old);
405 e9ce266e 2022-03-07 op err = got_pathlist_insert(&pe, &paths, path, NULL);
409 e9ce266e 2022-03-07 op if (p->old != NULL && p->new == NULL) {
411 e9ce266e 2022-03-07 op * special case: delete a file. don't try to match
412 e9ce266e 2022-03-07 op * the lines but just schedule the removal.
414 e9ce266e 2022-03-07 op err = got_worktree_schedule_delete(worktree, &paths,
415 d955343d 2022-03-08 op 0, NULL, delete_cb, delete_arg, repo, 0, 0);
417 e9ce266e 2022-03-07 op } else if (p->old != NULL && strcmp(p->old, p->new)) {
418 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_PATHS_DIFFER);
422 6f5cb1bd 2022-03-08 op if (asprintf(&template, "%s/got-patch",
423 6f5cb1bd 2022-03-08 op got_worktree_get_root_path(worktree)) == -1) {
424 6f5cb1bd 2022-03-08 op err = got_error_from_errno(template);
428 6f5cb1bd 2022-03-08 op err = got_opentemp_named(&tmppath, &tmp, template);
432 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
433 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
434 e9ce266e 2022-03-07 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL) {
435 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
438 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
439 e9ce266e 2022-03-07 op if (fprintf(tmp, "%s", h->lines[i]+1) < 0) {
440 e9ce266e 2022-03-07 op err = got_error_from_errno("fprintf");
447 e9ce266e 2022-03-07 op if ((orig = fopen(path, "r")) == NULL) {
448 e9ce266e 2022-03-07 op err = got_error_from_errno2("fopen", path);
453 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
455 e9ce266e 2022-03-07 op err = locate_hunk(orig, h, &lineno);
456 e9ce266e 2022-03-07 op if (err != NULL)
458 e9ce266e 2022-03-07 op if ((pos = ftello(orig)) == -1) {
459 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
462 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, pos);
463 e9ce266e 2022-03-07 op if (err != NULL)
465 e9ce266e 2022-03-07 op copypos = pos;
467 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
468 e9ce266e 2022-03-07 op if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
470 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
471 e9ce266e 2022-03-07 op * after the previous partial match.
473 e9ce266e 2022-03-07 op if (fseek(orig, pos, SEEK_SET) == -1) {
474 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
477 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
478 e9ce266e 2022-03-07 op if (linelen == -1) {
479 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
483 e9ce266e 2022-03-07 op goto tryagain;
485 e9ce266e 2022-03-07 op if (err != NULL)
488 e9ce266e 2022-03-07 op err = apply_hunk(tmp, h, &lineno);
489 e9ce266e 2022-03-07 op if (err != NULL)
492 e9ce266e 2022-03-07 op copypos = ftello(orig);
493 e9ce266e 2022-03-07 op if (copypos == -1) {
494 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
499 e9ce266e 2022-03-07 op if (!feof(orig)) {
500 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
506 e9ce266e 2022-03-07 op if (rename(tmppath, path) == -1) {
507 e9ce266e 2022-03-07 op err = got_error_from_errno3("rename", tmppath, path);
511 e9ce266e 2022-03-07 op if (p->old == NULL)
512 e9ce266e 2022-03-07 op err = got_worktree_schedule_add(worktree, &paths,
513 d955343d 2022-03-08 op add_cb, add_arg, repo, 1);
515 e9ce266e 2022-03-07 op printf("M %s\n", path); /* XXX */
517 6f5cb1bd 2022-03-08 op free(template);
518 e9ce266e 2022-03-07 op if (err != NULL && p->old == NULL && path != NULL)
519 e9ce266e 2022-03-07 op unlink(path);
520 e9ce266e 2022-03-07 op if (tmp != NULL)
522 e9ce266e 2022-03-07 op if (tmppath != NULL)
523 e9ce266e 2022-03-07 op unlink(tmppath);
524 e9ce266e 2022-03-07 op free(tmppath);
525 e9ce266e 2022-03-07 op if (orig != NULL) {
526 e9ce266e 2022-03-07 op if (p->old == NULL && err != NULL)
527 e9ce266e 2022-03-07 op unlink(path);
528 e9ce266e 2022-03-07 op fclose(orig);
532 e9ce266e 2022-03-07 op got_pathlist_free(&paths);
536 e9ce266e 2022-03-07 op const struct got_error *
537 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
538 d955343d 2022-03-08 op got_worktree_delete_cb delete_cb, void *delete_arg,
539 d955343d 2022-03-08 op got_worktree_checkout_cb add_cb, void *add_arg)
541 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
542 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
543 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
544 e9ce266e 2022-03-07 op int done = 0;
547 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
548 e9ce266e 2022-03-07 op if (ibuf == NULL) {
549 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
553 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
554 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
558 e9ce266e 2022-03-07 op pid = fork();
559 e9ce266e 2022-03-07 op if (pid == -1) {
560 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
562 e9ce266e 2022-03-07 op } else if (pid == 0) {
563 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
565 e9ce266e 2022-03-07 op /* not reached */
568 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
569 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
572 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
573 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
575 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
580 e9ce266e 2022-03-07 op while (!done && err == NULL) {
581 e9ce266e 2022-03-07 op struct got_patch p;
583 e9ce266e 2022-03-07 op err = recv_patch(ibuf, &done, &p);
584 e9ce266e 2022-03-07 op if (err || done)
587 d955343d 2022-03-08 op err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
588 d955343d 2022-03-08 op add_cb, add_arg);
589 e9ce266e 2022-03-07 op patch_free(&p);
595 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
596 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
597 e9ce266e 2022-03-07 op if (ibuf != NULL)
598 e9ce266e 2022-03-07 op imsg_clear(ibuf);
599 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
600 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
601 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
602 e9ce266e 2022-03-07 op err = got_error_from_errno("close");