Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Apply patches.
17 *
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
21 */
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/uio.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <sha1.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <imsg.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 struct got_patch_hunk {
55 STAILQ_ENTRY(got_patch_hunk) entries;
56 const struct got_error *err;
57 long offset;
58 int old_nonl;
59 int new_nonl;
60 long old_from;
61 long old_lines;
62 long new_from;
63 long new_lines;
64 size_t len;
65 size_t cap;
66 char **lines;
67 };
69 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
70 struct got_patch {
71 char *old;
72 char *new;
73 struct got_patch_hunk_head head;
74 };
76 struct patch_args {
77 got_patch_progress_cb progress_cb;
78 void *progress_arg;
79 struct got_patch_hunk_head *head;
80 };
82 static const struct got_error *
83 send_patch(struct imsgbuf *ibuf, int fd)
84 {
85 const struct got_error *err = NULL;
87 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
88 NULL, 0) == -1) {
89 err = got_error_from_errno(
90 "imsg_compose GOT_IMSG_PATCH_FILE");
91 close(fd);
92 return err;
93 }
95 if (imsg_flush(ibuf) == -1) {
96 err = got_error_from_errno("imsg_flush");
97 imsg_clear(ibuf);
98 }
100 return err;
103 static void
104 patch_free(struct got_patch *p)
106 struct got_patch_hunk *h;
107 size_t i;
109 while (!STAILQ_EMPTY(&p->head)) {
110 h = STAILQ_FIRST(&p->head);
111 STAILQ_REMOVE_HEAD(&p->head, entries);
113 for (i = 0; i < h->len; ++i)
114 free(h->lines[i]);
115 free(h->lines);
116 free(h);
119 free(p->new);
120 free(p->old);
122 memset(p, 0, sizeof(*p));
123 STAILQ_INIT(&p->head);
126 static const struct got_error *
127 pushline(struct got_patch_hunk *h, const char *line)
129 void *t;
130 size_t newcap;
132 if (h->len == h->cap) {
133 if ((newcap = h->cap * 1.5) == 0)
134 newcap = 16;
135 t = recallocarray(h->lines, h->cap, newcap,
136 sizeof(h->lines[0]));
137 if (t == NULL)
138 return got_error_from_errno("recallocarray");
139 h->lines = t;
140 h->cap = newcap;
143 if ((t = strdup(line)) == NULL)
144 return got_error_from_errno("strdup");
146 h->lines[h->len++] = t;
147 return NULL;
150 static const struct got_error *
151 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
153 const struct got_error *err = NULL;
154 struct imsg imsg;
155 struct got_imsg_patch_hunk hdr;
156 struct got_imsg_patch patch;
157 struct got_patch_hunk *h = NULL;
158 size_t datalen;
159 int lastmode = -1;
161 memset(p, 0, sizeof(*p));
162 STAILQ_INIT(&p->head);
164 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
165 if (err)
166 return err;
167 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
168 *done = 1;
169 goto done;
171 if (imsg.hdr.type != GOT_IMSG_PATCH) {
172 err = got_error(GOT_ERR_PRIVSEP_MSG);
173 goto done;
175 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
176 if (datalen != sizeof(patch)) {
177 err = got_error(GOT_ERR_PRIVSEP_LEN);
178 goto done;
180 memcpy(&patch, imsg.data, sizeof(patch));
182 if (patch.old[sizeof(patch.old)-1] != '\0' ||
183 patch.new[sizeof(patch.new)-1] != '\0') {
184 err = got_error(GOT_ERR_PRIVSEP_LEN);
185 goto done;
188 /* automatically set strip=1 for git-style diffs */
189 if (strip == -1 && patch.git &&
190 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
191 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
192 strip = 1;
194 /* prefer the new name if not /dev/null for not git-style diffs */
195 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
196 err = got_path_strip(&p->old, patch.new, strip);
197 if (err)
198 goto done;
199 } else if (*patch.old != '\0') {
200 err = got_path_strip(&p->old, patch.old, strip);
201 if (err)
202 goto done;
205 if (*patch.new != '\0') {
206 err = got_path_strip(&p->new, patch.new, strip);
207 if (err)
208 goto done;
211 if (p->old == NULL && p->new == NULL) {
212 err = got_error(GOT_ERR_PATCH_MALFORMED);
213 goto done;
216 imsg_free(&imsg);
218 for (;;) {
219 char *t;
221 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
222 if (err) {
223 patch_free(p);
224 return err;
227 switch (imsg.hdr.type) {
228 case GOT_IMSG_PATCH_DONE:
229 if (h != NULL && h->len == 0)
230 err = got_error(GOT_ERR_PATCH_MALFORMED);
231 goto done;
232 case GOT_IMSG_PATCH_HUNK:
233 if (h != NULL &&
234 (h->len == 0 || h->old_nonl || h->new_nonl)) {
235 err = got_error(GOT_ERR_PATCH_MALFORMED);
236 goto done;
238 lastmode = -1;
239 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
240 if (datalen != sizeof(hdr)) {
241 err = got_error(GOT_ERR_PRIVSEP_LEN);
242 goto done;
244 memcpy(&hdr, imsg.data, sizeof(hdr));
245 if ((h = calloc(1, sizeof(*h))) == NULL) {
246 err = got_error_from_errno("calloc");
247 goto done;
249 if (h->old_from < 0 || h->new_from < 0) {
250 err = got_error(GOT_ERR_PRIVSEP_LEN);
251 goto done;
253 h->old_from = hdr.oldfrom;
254 h->old_lines = hdr.oldlines;
255 h->new_from = hdr.newfrom;
256 h->new_lines = hdr.newlines;
257 STAILQ_INSERT_TAIL(&p->head, h, entries);
258 break;
259 case GOT_IMSG_PATCH_LINE:
260 if (h == NULL) {
261 err = got_error(GOT_ERR_PRIVSEP_MSG);
262 goto done;
264 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
265 t = imsg.data;
266 /* at least one char */
267 if (datalen < 2 || t[datalen-1] != '\0') {
268 err = got_error(GOT_ERR_PRIVSEP_MSG);
269 goto done;
271 if (*t != ' ' && *t != '-' && *t != '+' &&
272 *t != '\\') {
273 err = got_error(GOT_ERR_PRIVSEP_MSG);
274 goto done;
277 if (*t != '\\')
278 err = pushline(h, t);
279 else if (lastmode == '-')
280 h->old_nonl = 1;
281 else if (lastmode == '+')
282 h->new_nonl = 1;
283 else
284 err = got_error(GOT_ERR_PATCH_MALFORMED);
286 if (err)
287 goto done;
289 lastmode = *t;
290 break;
291 default:
292 err = got_error(GOT_ERR_PRIVSEP_MSG);
293 goto done;
296 imsg_free(&imsg);
299 done:
300 if (err)
301 patch_free(p);
303 imsg_free(&imsg);
304 return err;
307 /*
308 * Copy data from orig starting at copypos until pos into tmp.
309 * If pos is -1, copy until EOF.
310 */
311 static const struct got_error *
312 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
314 char buf[BUFSIZ];
315 size_t len, r, w;
317 if (fseeko(orig, copypos, SEEK_SET) == -1)
318 return got_error_from_errno("fseeko");
320 while (pos == -1 || copypos < pos) {
321 len = sizeof(buf);
322 if (pos > 0)
323 len = MIN(len, (size_t)pos - copypos);
324 r = fread(buf, 1, len, orig);
325 if (r != len && ferror(orig))
326 return got_error_from_errno("fread");
327 w = fwrite(buf, 1, r, tmp);
328 if (w != r)
329 return got_error_from_errno("fwrite");
330 copypos += len;
331 if (r != len && feof(orig)) {
332 if (pos == -1)
333 return NULL;
334 return got_error(GOT_ERR_HUNK_FAILED);
337 return NULL;
340 static const struct got_error *
341 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
343 const struct got_error *err = NULL;
344 char *line = NULL;
345 char mode = *h->lines[0];
346 size_t linesize = 0;
347 ssize_t linelen;
348 off_t match = -1;
349 long match_lineno = -1;
351 for (;;) {
352 linelen = getline(&line, &linesize, orig);
353 if (linelen == -1) {
354 if (ferror(orig))
355 err = got_error_from_errno("getline");
356 else if (match == -1)
357 err = got_error(GOT_ERR_HUNK_FAILED);
358 break;
360 if (line[linelen - 1] == '\n')
361 line[linelen - 1] = '\0';
362 (*lineno)++;
364 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
365 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
366 (mode == '+' && *lineno == h->old_from)) {
367 match = ftello(orig);
368 if (match == -1) {
369 err = got_error_from_errno("ftello");
370 break;
372 match -= linelen;
373 match_lineno = (*lineno)-1;
376 if (*lineno >= h->old_from && match != -1)
377 break;
380 if (err == NULL) {
381 *pos = match;
382 *lineno = match_lineno;
383 if (fseeko(orig, match, SEEK_SET) == -1)
384 err = got_error_from_errno("fseeko");
387 free(line);
388 return err;
391 static const struct got_error *
392 test_hunk(FILE *orig, struct got_patch_hunk *h)
394 const struct got_error *err = NULL;
395 char *line = NULL;
396 size_t linesize = 0, i = 0;
397 ssize_t linelen;
399 for (i = 0; i < h->len; ++i) {
400 switch (*h->lines[i]) {
401 case '+':
402 continue;
403 case ' ':
404 case '-':
405 linelen = getline(&line, &linesize, orig);
406 if (linelen == -1) {
407 if (ferror(orig))
408 err = got_error_from_errno("getline");
409 else
410 err = got_error(
411 GOT_ERR_HUNK_FAILED);
412 goto done;
414 if (line[linelen - 1] == '\n')
415 line[linelen - 1] = '\0';
416 if (strcmp(h->lines[i] + 1, line)) {
417 err = got_error(GOT_ERR_HUNK_FAILED);
418 goto done;
420 break;
424 done:
425 free(line);
426 return err;
429 static const struct got_error *
430 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
432 size_t i, new = 0;
434 for (i = 0; i < h->len; ++i) {
435 switch (*h->lines[i]) {
436 case ' ':
437 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
438 return got_error_from_errno("fprintf");
439 /* fallthrough */
440 case '-':
441 (*lineno)++;
442 break;
443 case '+':
444 new++;
445 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
446 return got_error_from_errno("fprintf");
447 if (new != h->new_lines || !h->new_nonl) {
448 if (fprintf(tmp, "\n") < 0)
449 return got_error_from_errno(
450 "fprintf");
452 break;
455 return NULL;
458 static const struct got_error *
459 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
460 mode_t *mode)
462 const struct got_error *err = NULL;
463 struct got_patch_hunk *h;
464 struct stat sb;
465 long lineno = 0;
466 FILE *orig;
467 off_t copypos, pos;
468 char *line = NULL;
469 size_t linesize = 0;
470 ssize_t linelen;
472 if (p->old == NULL) { /* create */
473 h = STAILQ_FIRST(&p->head);
474 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
475 return got_error(GOT_ERR_PATCH_MALFORMED);
476 if (nop)
477 return NULL;
478 return apply_hunk(tmp, h, &lineno);
481 if ((orig = fopen(path, "r")) == NULL) {
482 err = got_error_from_errno2("fopen", path);
483 goto done;
486 if (fstat(fileno(orig), &sb) == -1) {
487 err = got_error_from_errno("fstat");
488 goto done;
490 *mode = sb.st_mode;
492 copypos = 0;
493 STAILQ_FOREACH(h, &p->head, entries) {
494 tryagain:
495 err = locate_hunk(orig, h, &pos, &lineno);
496 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
497 h->err = err;
498 if (err != NULL)
499 goto done;
500 if (!nop)
501 err = copy(tmp, orig, copypos, pos);
502 if (err != NULL)
503 goto done;
504 copypos = pos;
506 err = test_hunk(orig, h);
507 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
508 /*
509 * try to apply the hunk again starting the search
510 * after the previous partial match.
511 */
512 if (fseeko(orig, pos, SEEK_SET) == -1) {
513 err = got_error_from_errno("fseeko");
514 goto done;
516 linelen = getline(&line, &linesize, orig);
517 if (linelen == -1) {
518 err = got_error_from_errno("getline");
519 goto done;
521 lineno++;
522 goto tryagain;
524 if (err != NULL)
525 goto done;
527 if (lineno + 1 != h->old_from)
528 h->offset = lineno + 1 - h->old_from;
530 if (!nop)
531 err = apply_hunk(tmp, h, &lineno);
532 if (err != NULL)
533 goto done;
535 copypos = ftello(orig);
536 if (copypos == -1) {
537 err = got_error_from_errno("ftello");
538 goto done;
542 if (p->new == NULL && sb.st_size != copypos) {
543 h = STAILQ_FIRST(&p->head);
544 h->err = got_error(GOT_ERR_HUNK_FAILED);
545 err = h->err;
546 } else if (!nop && !feof(orig))
547 err = copy(tmp, orig, copypos, -1);
549 done:
550 if (orig != NULL)
551 fclose(orig);
552 return err;
555 static const struct got_error *
556 report_progress(struct patch_args *pa, const char *old, const char *new,
557 unsigned char status, const struct got_error *orig_error)
559 const struct got_error *err;
560 struct got_patch_hunk *h;
562 err = pa->progress_cb(pa->progress_arg, old, new, status,
563 orig_error, 0, 0, 0, 0, 0, NULL);
564 if (err)
565 return err;
567 STAILQ_FOREACH(h, pa->head, entries) {
568 if (h->offset == 0 && h->err == NULL)
569 continue;
571 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
572 h->old_from, h->old_lines, h->new_from, h->new_lines,
573 h->offset, h->err);
574 if (err)
575 return err;
578 return NULL;
581 static const struct got_error *
582 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
583 const char *path)
585 return report_progress(arg, path, NULL, status, NULL);
588 static const struct got_error *
589 patch_add(void *arg, unsigned char status, const char *path)
591 return report_progress(arg, NULL, path, status, NULL);
594 static const struct got_error *
595 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
596 struct got_fileindex *fileindex, const char *old, const char *new,
597 struct got_patch *p, int nop, struct patch_args *pa,
598 got_cancel_cb cancel_cb, void *cancel_arg)
600 const struct got_error *err = NULL;
601 int file_renamed = 0;
602 char *oldpath = NULL, *newpath = NULL;
603 char *tmppath = NULL, *template = NULL, *parent = NULL;;
604 FILE *tmp = NULL;
605 mode_t mode = GOT_DEFAULT_FILE_MODE;
607 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
608 old) == -1) {
609 err = got_error_from_errno("asprintf");
610 goto done;
613 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
614 new) == -1) {
615 err = got_error_from_errno("asprintf");
616 goto done;
619 file_renamed = strcmp(oldpath, newpath);
621 if (asprintf(&template, "%s/got-patch",
622 got_worktree_get_root_path(worktree)) == -1) {
623 err = got_error_from_errno(template);
624 goto done;
627 if (!nop)
628 err = got_opentemp_named(&tmppath, &tmp, template);
629 if (err)
630 goto done;
631 err = patch_file(p, oldpath, tmp, nop, &mode);
632 if (err)
633 goto done;
635 if (nop)
636 goto done;
638 if (p->old != NULL && p->new == NULL) {
639 err = got_worktree_patch_schedule_rm(old, repo, worktree,
640 fileindex, patch_delete, pa);
641 goto done;
644 if (fchmod(fileno(tmp), mode) == -1) {
645 err = got_error_from_errno2("chmod", tmppath);
646 goto done;
649 if (rename(tmppath, newpath) == -1) {
650 if (errno != ENOENT) {
651 err = got_error_from_errno3("rename", tmppath,
652 newpath);
653 goto done;
656 err = got_path_dirname(&parent, newpath);
657 if (err != NULL)
658 goto done;
659 err = got_path_mkdir(parent);
660 if (err != NULL)
661 goto done;
662 if (rename(tmppath, newpath) == -1) {
663 err = got_error_from_errno3("rename", tmppath,
664 newpath);
665 goto done;
669 if (file_renamed) {
670 err = got_worktree_patch_schedule_rm(old, repo, worktree,
671 fileindex, patch_delete, pa);
672 if (err == NULL)
673 err = got_worktree_patch_schedule_add(new, repo,
674 worktree, fileindex, patch_add,
675 pa);
676 if (err)
677 unlink(newpath);
678 } else if (p->old == NULL) {
679 err = got_worktree_patch_schedule_add(new, repo, worktree,
680 fileindex, patch_add, pa);
681 if (err)
682 unlink(newpath);
683 } else
684 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
686 done:
687 free(parent);
688 free(template);
689 if (tmppath != NULL)
690 unlink(tmppath);
691 free(tmppath);
692 free(oldpath);
693 free(newpath);
694 return err;
697 static void
698 reverse_patch(struct got_patch *p)
700 struct got_patch_hunk *h;
701 size_t i;
702 long tmp;
704 STAILQ_FOREACH(h, &p->head, entries) {
705 tmp = h->old_from;
706 h->old_from = h->new_from;
707 h->new_from = tmp;
709 tmp = h->old_lines;
710 h->old_lines = h->new_lines;
711 h->new_lines = tmp;
713 tmp = h->old_nonl;
714 h->old_nonl = h->new_nonl;
715 h->new_nonl = tmp;
717 for (i = 0; i < h->len; ++i) {
718 if (*h->lines[i] == '+')
719 *h->lines[i] = '-';
720 else if (*h->lines[i] == '-')
721 *h->lines[i] = '+';
726 const struct got_error *
727 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
728 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
729 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
731 const struct got_error *err = NULL, *complete_err = NULL;
732 struct got_fileindex *fileindex = NULL;
733 char *fileindex_path = NULL;
734 char *oldpath, *newpath;
735 struct imsgbuf *ibuf;
736 int imsg_fds[2] = {-1, -1};
737 int done = 0, failed = 0;
738 pid_t pid;
740 ibuf = calloc(1, sizeof(*ibuf));
741 if (ibuf == NULL) {
742 err = got_error_from_errno("calloc");
743 goto done;
746 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
747 err = got_error_from_errno("socketpair");
748 goto done;
751 pid = fork();
752 if (pid == -1) {
753 err = got_error_from_errno("fork");
754 goto done;
755 } else if (pid == 0) {
756 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
757 NULL);
758 /* not reached */
761 if (close(imsg_fds[1]) == -1) {
762 err = got_error_from_errno("close");
763 goto done;
765 imsg_fds[1] = -1;
766 imsg_init(ibuf, imsg_fds[0]);
768 err = send_patch(ibuf, fd);
769 fd = -1;
770 if (err)
771 goto done;
773 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
774 worktree);
775 if (err)
776 goto done;
778 while (!done && err == NULL) {
779 struct got_patch p;
780 struct patch_args pa;
782 pa.progress_cb = progress_cb;
783 pa.progress_arg = progress_arg;
784 pa.head = &p.head;
786 err = recv_patch(ibuf, &done, &p, strip);
787 if (err || done)
788 break;
790 if (reverse)
791 reverse_patch(&p);
793 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
794 &newpath, worktree, repo, fileindex);
795 if (err == NULL)
796 err = apply_patch(worktree, repo, fileindex, oldpath,
797 newpath, &p, nop, &pa, cancel_cb, cancel_arg);
798 if (err != NULL) {
799 failed = 1;
800 /* recoverable errors */
801 if (err->code == GOT_ERR_FILE_STATUS ||
802 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
803 err = report_progress(&pa, p.old, p.new,
804 GOT_STATUS_CANNOT_UPDATE, err);
805 else if (err->code == GOT_ERR_HUNK_FAILED)
806 err = report_progress(&pa, p.old, p.new,
807 GOT_STATUS_CANNOT_UPDATE, NULL);
810 free(oldpath);
811 free(newpath);
812 patch_free(&p);
814 if (err)
815 break;
818 done:
819 if (fileindex != NULL)
820 complete_err = got_worktree_patch_complete(fileindex,
821 fileindex_path);
822 if (complete_err && err == NULL)
823 err = complete_err;
824 free(fileindex_path);
825 if (fd != -1 && close(fd) == -1 && err == NULL)
826 err = got_error_from_errno("close");
827 if (ibuf != NULL)
828 imsg_clear(ibuf);
829 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
830 err = got_error_from_errno("close");
831 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
832 err = got_error_from_errno("close");
833 if (err == NULL && failed)
834 err = got_error(GOT_ERR_PATCH_FAILED);
835 return err;