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/socket.h>
25 #include <sys/stat.h>
26 #include <sys/uio.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_path.h"
39 #include "got_reference.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
43 #include "got_patch.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 struct got_patch_hunk {
52 STAILQ_ENTRY(got_patch_hunk) entries;
53 const struct got_error *err;
54 int offset;
55 int old_nonl;
56 int new_nonl;
57 int old_from;
58 int old_lines;
59 int new_from;
60 int new_lines;
61 size_t len;
62 size_t cap;
63 char **lines;
64 };
66 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
67 struct got_patch {
68 char *old;
69 char *new;
70 struct got_patch_hunk_head head;
71 };
73 struct patch_args {
74 got_patch_progress_cb progress_cb;
75 void *progress_arg;
76 struct got_patch_hunk_head *head;
77 };
79 static const struct got_error *
80 send_patch(struct imsgbuf *ibuf, int fd)
81 {
82 const struct got_error *err = NULL;
84 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
85 NULL, 0) == -1) {
86 err = got_error_from_errno(
87 "imsg_compose GOT_IMSG_PATCH_FILE");
88 close(fd);
89 return err;
90 }
92 if (imsg_flush(ibuf) == -1) {
93 err = got_error_from_errno("imsg_flush");
94 imsg_clear(ibuf);
95 }
97 return err;
98 }
100 static void
101 patch_free(struct got_patch *p)
103 struct got_patch_hunk *h;
104 size_t i;
106 while (!STAILQ_EMPTY(&p->head)) {
107 h = STAILQ_FIRST(&p->head);
108 STAILQ_REMOVE_HEAD(&p->head, entries);
110 for (i = 0; i < h->len; ++i)
111 free(h->lines[i]);
112 free(h->lines);
113 free(h);
116 free(p->new);
117 free(p->old);
119 memset(p, 0, sizeof(*p));
120 STAILQ_INIT(&p->head);
123 static const struct got_error *
124 pushline(struct got_patch_hunk *h, const char *line)
126 void *t;
127 size_t newcap;
129 if (h->len == h->cap) {
130 if ((newcap = h->cap * 1.5) == 0)
131 newcap = 16;
132 t = recallocarray(h->lines, h->cap, newcap,
133 sizeof(h->lines[0]));
134 if (t == NULL)
135 return got_error_from_errno("recallocarray");
136 h->lines = t;
137 h->cap = newcap;
140 if ((t = strdup(line)) == NULL)
141 return got_error_from_errno("strdup");
143 h->lines[h->len++] = t;
144 return NULL;
147 static const struct got_error *
148 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
150 const struct got_error *err = NULL;
151 struct imsg imsg;
152 struct got_imsg_patch_hunk hdr;
153 struct got_imsg_patch patch;
154 struct got_patch_hunk *h = NULL;
155 size_t datalen;
156 int lastmode = -1;
158 memset(p, 0, sizeof(*p));
159 STAILQ_INIT(&p->head);
161 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
162 if (err)
163 return err;
164 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
165 *done = 1;
166 goto done;
168 if (imsg.hdr.type != GOT_IMSG_PATCH) {
169 err = got_error(GOT_ERR_PRIVSEP_MSG);
170 goto done;
172 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
173 if (datalen != sizeof(patch)) {
174 err = got_error(GOT_ERR_PRIVSEP_LEN);
175 goto done;
177 memcpy(&patch, imsg.data, sizeof(patch));
179 if (patch.old[sizeof(patch.old)-1] != '\0' ||
180 patch.new[sizeof(patch.new)-1] != '\0') {
181 err = got_error(GOT_ERR_PRIVSEP_LEN);
182 goto done;
185 /* automatically set strip=1 for git-style diffs */
186 if (strip == -1 && patch.git &&
187 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
188 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
189 strip = 1;
191 /* prefer the new name if not /dev/null for not git-style diffs */
192 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
193 err = got_path_strip(&p->old, patch.new, strip);
194 if (err)
195 goto done;
196 } else if (*patch.old != '\0') {
197 err = got_path_strip(&p->old, patch.old, strip);
198 if (err)
199 goto done;
202 if (*patch.new != '\0') {
203 err = got_path_strip(&p->new, patch.new, strip);
204 if (err)
205 goto done;
208 if (p->old == NULL && p->new == NULL) {
209 err = got_error(GOT_ERR_PATCH_MALFORMED);
210 goto done;
213 imsg_free(&imsg);
215 for (;;) {
216 char *t;
218 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
219 if (err) {
220 patch_free(p);
221 return err;
224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
225 switch (imsg.hdr.type) {
226 case GOT_IMSG_PATCH_DONE:
227 if (h != NULL && h->len == 0)
228 err = got_error(GOT_ERR_PATCH_MALFORMED);
229 goto done;
230 case GOT_IMSG_PATCH_HUNK:
231 if (h != NULL &&
232 (h->len == 0 || h->old_nonl || h->new_nonl)) {
233 err = got_error(GOT_ERR_PATCH_MALFORMED);
234 goto done;
236 lastmode = -1;
237 if (datalen != sizeof(hdr)) {
238 err = got_error(GOT_ERR_PRIVSEP_LEN);
239 goto done;
241 memcpy(&hdr, imsg.data, sizeof(hdr));
242 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
243 err = got_error(GOT_ERR_PRIVSEP_LEN);
244 goto done;
246 if ((h = calloc(1, sizeof(*h))) == NULL) {
247 err = got_error_from_errno("calloc");
248 goto done;
250 h->old_from = hdr.oldfrom;
251 h->old_lines = hdr.oldlines;
252 h->new_from = hdr.newfrom;
253 h->new_lines = hdr.newlines;
254 STAILQ_INSERT_TAIL(&p->head, h, entries);
255 break;
256 case GOT_IMSG_PATCH_LINE:
257 if (h == NULL) {
258 err = got_error(GOT_ERR_PRIVSEP_MSG);
259 goto done;
261 t = imsg.data;
262 /* at least one char */
263 if (datalen < 2 || t[datalen-1] != '\0') {
264 err = got_error(GOT_ERR_PRIVSEP_MSG);
265 goto done;
267 if (*t != ' ' && *t != '-' && *t != '+' &&
268 *t != '\\') {
269 err = got_error(GOT_ERR_PRIVSEP_MSG);
270 goto done;
273 if (*t != '\\')
274 err = pushline(h, t);
275 else if (lastmode == '-')
276 h->old_nonl = 1;
277 else if (lastmode == '+')
278 h->new_nonl = 1;
279 else
280 err = got_error(GOT_ERR_PATCH_MALFORMED);
282 if (err)
283 goto done;
285 lastmode = *t;
286 break;
287 default:
288 err = got_error(GOT_ERR_PRIVSEP_MSG);
289 goto done;
292 imsg_free(&imsg);
295 done:
296 if (err)
297 patch_free(p);
299 imsg_free(&imsg);
300 return err;
303 /*
304 * Copy data from orig starting at copypos until pos into tmp.
305 * If pos is -1, copy until EOF.
306 */
307 static const struct got_error *
308 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
310 char buf[BUFSIZ];
311 size_t len, r, w;
313 if (fseeko(orig, copypos, SEEK_SET) == -1)
314 return got_error_from_errno("fseeko");
316 while (pos == -1 || copypos < pos) {
317 len = sizeof(buf);
318 if (pos > 0)
319 len = MIN(len, (size_t)pos - copypos);
320 r = fread(buf, 1, len, orig);
321 if (r != len && ferror(orig))
322 return got_error_from_errno("fread");
323 w = fwrite(buf, 1, r, tmp);
324 if (w != r)
325 return got_error_from_errno("fwrite");
326 copypos += len;
327 if (r != len && feof(orig)) {
328 if (pos == -1)
329 return NULL;
330 return got_error(GOT_ERR_HUNK_FAILED);
333 return NULL;
336 static const struct got_error *
337 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
339 const struct got_error *err = NULL;
340 char *line = NULL;
341 char mode = *h->lines[0];
342 size_t linesize = 0;
343 ssize_t linelen;
344 off_t match = -1;
345 int match_lineno = -1;
347 for (;;) {
348 linelen = getline(&line, &linesize, orig);
349 if (linelen == -1) {
350 if (ferror(orig))
351 err = got_error_from_errno("getline");
352 else if (match == -1)
353 err = got_error(GOT_ERR_HUNK_FAILED);
354 break;
356 if (line[linelen - 1] == '\n')
357 line[linelen - 1] = '\0';
358 (*lineno)++;
360 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
361 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
362 (mode == '+' && *lineno == h->old_from)) {
363 match = ftello(orig);
364 if (match == -1) {
365 err = got_error_from_errno("ftello");
366 break;
368 match -= linelen;
369 match_lineno = (*lineno)-1;
372 if (*lineno >= h->old_from && match != -1)
373 break;
376 if (err == NULL) {
377 *pos = match;
378 *lineno = match_lineno;
379 if (fseeko(orig, match, SEEK_SET) == -1)
380 err = got_error_from_errno("fseeko");
383 free(line);
384 return err;
387 static const struct got_error *
388 test_hunk(FILE *orig, struct got_patch_hunk *h)
390 const struct got_error *err = NULL;
391 char *line = NULL;
392 size_t linesize = 0, i = 0;
393 ssize_t linelen;
395 for (i = 0; i < h->len; ++i) {
396 switch (*h->lines[i]) {
397 case '+':
398 continue;
399 case ' ':
400 case '-':
401 linelen = getline(&line, &linesize, orig);
402 if (linelen == -1) {
403 if (ferror(orig))
404 err = got_error_from_errno("getline");
405 else
406 err = got_error(
407 GOT_ERR_HUNK_FAILED);
408 goto done;
410 if (line[linelen - 1] == '\n')
411 line[linelen - 1] = '\0';
412 if (strcmp(h->lines[i] + 1, line)) {
413 err = got_error(GOT_ERR_HUNK_FAILED);
414 goto done;
416 break;
420 done:
421 free(line);
422 return err;
425 static const struct got_error *
426 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
428 size_t i, new = 0;
430 for (i = 0; i < h->len; ++i) {
431 switch (*h->lines[i]) {
432 case ' ':
433 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
434 return got_error_from_errno("fprintf");
435 /* fallthrough */
436 case '-':
437 (*lineno)++;
438 break;
439 case '+':
440 new++;
441 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
442 return got_error_from_errno("fprintf");
443 if (new != h->new_lines || !h->new_nonl) {
444 if (fprintf(tmp, "\n") < 0)
445 return got_error_from_errno(
446 "fprintf");
448 break;
451 return NULL;
454 static const struct got_error *
455 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
456 mode_t *mode)
458 const struct got_error *err = NULL;
459 struct got_patch_hunk *h;
460 struct stat sb;
461 int lineno = 0;
462 FILE *orig;
463 off_t copypos, pos;
464 char *line = NULL;
465 size_t linesize = 0;
466 ssize_t linelen;
468 if (p->old == NULL) { /* create */
469 h = STAILQ_FIRST(&p->head);
470 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
471 return got_error(GOT_ERR_PATCH_MALFORMED);
472 if (nop)
473 return NULL;
474 return apply_hunk(tmp, h, &lineno);
477 if ((orig = fopen(path, "r")) == NULL) {
478 err = got_error_from_errno2("fopen", path);
479 goto done;
482 if (fstat(fileno(orig), &sb) == -1) {
483 err = got_error_from_errno("fstat");
484 goto done;
486 *mode = sb.st_mode;
488 copypos = 0;
489 STAILQ_FOREACH(h, &p->head, entries) {
490 tryagain:
491 err = locate_hunk(orig, h, &pos, &lineno);
492 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
493 h->err = err;
494 if (err != NULL)
495 goto done;
496 if (!nop)
497 err = copy(tmp, orig, copypos, pos);
498 if (err != NULL)
499 goto done;
500 copypos = pos;
502 err = test_hunk(orig, h);
503 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
504 /*
505 * try to apply the hunk again starting the search
506 * after the previous partial match.
507 */
508 if (fseeko(orig, pos, SEEK_SET) == -1) {
509 err = got_error_from_errno("fseeko");
510 goto done;
512 linelen = getline(&line, &linesize, orig);
513 if (linelen == -1) {
514 err = got_error_from_errno("getline");
515 goto done;
517 lineno++;
518 goto tryagain;
520 if (err != NULL)
521 goto done;
523 if (lineno + 1 != h->old_from)
524 h->offset = lineno + 1 - h->old_from;
526 if (!nop)
527 err = apply_hunk(tmp, h, &lineno);
528 if (err != NULL)
529 goto done;
531 copypos = ftello(orig);
532 if (copypos == -1) {
533 err = got_error_from_errno("ftello");
534 goto done;
538 if (p->new == NULL && sb.st_size != copypos) {
539 h = STAILQ_FIRST(&p->head);
540 h->err = got_error(GOT_ERR_HUNK_FAILED);
541 err = h->err;
542 } else if (!nop && !feof(orig))
543 err = copy(tmp, orig, copypos, -1);
545 done:
546 if (orig != NULL && fclose(orig) == EOF && err == NULL)
547 err = got_error_from_errno("fclose");
548 return err;
551 static const struct got_error *
552 report_progress(struct patch_args *pa, const char *old, const char *new,
553 unsigned char status, const struct got_error *orig_error)
555 const struct got_error *err;
556 struct got_patch_hunk *h;
558 err = pa->progress_cb(pa->progress_arg, old, new, status,
559 orig_error, 0, 0, 0, 0, 0, NULL);
560 if (err)
561 return err;
563 STAILQ_FOREACH(h, pa->head, entries) {
564 if (h->offset == 0 && h->err == NULL)
565 continue;
567 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
568 h->old_from, h->old_lines, h->new_from, h->new_lines,
569 h->offset, h->err);
570 if (err)
571 return err;
574 return NULL;
577 static const struct got_error *
578 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
579 const char *path)
581 return report_progress(arg, path, NULL, status, NULL);
584 static const struct got_error *
585 patch_add(void *arg, unsigned char status, const char *path)
587 return report_progress(arg, NULL, path, status, NULL);
590 static const struct got_error *
591 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
592 struct got_fileindex *fileindex, const char *old, const char *new,
593 struct got_patch *p, int nop, struct patch_args *pa,
594 got_cancel_cb cancel_cb, void *cancel_arg)
596 const struct got_error *err = NULL;
597 int file_renamed = 0;
598 char *oldpath = NULL, *newpath = NULL;
599 char *tmppath = NULL, *template = NULL, *parent = NULL;;
600 FILE *tmp = NULL;
601 mode_t mode = GOT_DEFAULT_FILE_MODE;
603 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
604 old) == -1) {
605 err = got_error_from_errno("asprintf");
606 goto done;
609 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
610 new) == -1) {
611 err = got_error_from_errno("asprintf");
612 goto done;
615 file_renamed = strcmp(oldpath, newpath);
617 if (asprintf(&template, "%s/got-patch",
618 got_worktree_get_root_path(worktree)) == -1) {
619 err = got_error_from_errno(template);
620 goto done;
623 if (!nop)
624 err = got_opentemp_named(&tmppath, &tmp, template);
625 if (err)
626 goto done;
627 err = patch_file(p, oldpath, tmp, nop, &mode);
628 if (err)
629 goto done;
631 if (nop)
632 goto done;
634 if (p->old != NULL && p->new == NULL) {
635 err = got_worktree_patch_schedule_rm(old, repo, worktree,
636 fileindex, patch_delete, pa);
637 goto done;
640 if (fchmod(fileno(tmp), mode) == -1) {
641 err = got_error_from_errno2("chmod", tmppath);
642 goto done;
645 if (rename(tmppath, newpath) == -1) {
646 if (errno != ENOENT) {
647 err = got_error_from_errno3("rename", tmppath,
648 newpath);
649 goto done;
652 err = got_path_dirname(&parent, newpath);
653 if (err != NULL)
654 goto done;
655 err = got_path_mkdir(parent);
656 if (err != NULL)
657 goto done;
658 if (rename(tmppath, newpath) == -1) {
659 err = got_error_from_errno3("rename", tmppath,
660 newpath);
661 goto done;
665 if (file_renamed) {
666 err = got_worktree_patch_schedule_rm(old, repo, worktree,
667 fileindex, patch_delete, pa);
668 if (err == NULL)
669 err = got_worktree_patch_schedule_add(new, repo,
670 worktree, fileindex, patch_add,
671 pa);
672 if (err)
673 unlink(newpath);
674 } else if (p->old == NULL) {
675 err = got_worktree_patch_schedule_add(new, repo, worktree,
676 fileindex, patch_add, pa);
677 if (err)
678 unlink(newpath);
679 } else
680 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
682 done:
683 free(parent);
684 free(template);
685 if (tmppath != NULL)
686 unlink(tmppath);
687 if (tmp != NULL && fclose(tmp) == EOF && err == NULL)
688 err = got_error_from_errno("fclose");
689 free(tmppath);
690 free(oldpath);
691 free(newpath);
692 return err;
695 static void
696 reverse_patch(struct got_patch *p)
698 struct got_patch_hunk *h;
699 size_t i;
700 int tmp;
702 STAILQ_FOREACH(h, &p->head, entries) {
703 tmp = h->old_from;
704 h->old_from = h->new_from;
705 h->new_from = tmp;
707 tmp = h->old_lines;
708 h->old_lines = h->new_lines;
709 h->new_lines = tmp;
711 tmp = h->old_nonl;
712 h->old_nonl = h->new_nonl;
713 h->new_nonl = tmp;
715 for (i = 0; i < h->len; ++i) {
716 if (*h->lines[i] == '+')
717 *h->lines[i] = '-';
718 else if (*h->lines[i] == '-')
719 *h->lines[i] = '+';
724 const struct got_error *
725 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
726 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
727 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
729 const struct got_error *err = NULL, *complete_err = NULL;
730 struct got_fileindex *fileindex = NULL;
731 char *fileindex_path = NULL;
732 char *oldpath, *newpath;
733 struct imsgbuf *ibuf;
734 int imsg_fds[2] = {-1, -1};
735 int done = 0, failed = 0;
736 pid_t pid;
738 ibuf = calloc(1, sizeof(*ibuf));
739 if (ibuf == NULL) {
740 err = got_error_from_errno("calloc");
741 goto done;
744 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
745 err = got_error_from_errno("socketpair");
746 goto done;
749 pid = fork();
750 if (pid == -1) {
751 err = got_error_from_errno("fork");
752 goto done;
753 } else if (pid == 0) {
754 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
755 NULL);
756 /* not reached */
759 if (close(imsg_fds[1]) == -1) {
760 err = got_error_from_errno("close");
761 goto done;
763 imsg_fds[1] = -1;
764 imsg_init(ibuf, imsg_fds[0]);
766 err = send_patch(ibuf, fd);
767 fd = -1;
768 if (err)
769 goto done;
771 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
772 worktree);
773 if (err)
774 goto done;
776 while (!done && err == NULL) {
777 struct got_patch p;
778 struct patch_args pa;
780 pa.progress_cb = progress_cb;
781 pa.progress_arg = progress_arg;
782 pa.head = &p.head;
784 err = recv_patch(ibuf, &done, &p, strip);
785 if (err || done)
786 break;
788 if (reverse)
789 reverse_patch(&p);
791 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
792 &newpath, worktree, repo, fileindex);
793 if (err == NULL)
794 err = apply_patch(worktree, repo, fileindex, oldpath,
795 newpath, &p, nop, &pa, cancel_cb, cancel_arg);
796 if (err != NULL) {
797 failed = 1;
798 /* recoverable errors */
799 if (err->code == GOT_ERR_FILE_STATUS ||
800 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
801 err = report_progress(&pa, p.old, p.new,
802 GOT_STATUS_CANNOT_UPDATE, err);
803 else if (err->code == GOT_ERR_HUNK_FAILED)
804 err = report_progress(&pa, p.old, p.new,
805 GOT_STATUS_CANNOT_UPDATE, NULL);
808 free(oldpath);
809 free(newpath);
810 patch_free(&p);
812 if (err)
813 break;
816 done:
817 if (fileindex != NULL)
818 complete_err = got_worktree_patch_complete(fileindex,
819 fileindex_path);
820 if (complete_err && err == NULL)
821 err = complete_err;
822 free(fileindex_path);
823 if (fd != -1 && close(fd) == -1 && err == NULL)
824 err = got_error_from_errno("close");
825 if (ibuf != NULL)
826 imsg_clear(ibuf);
827 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
828 err = got_error_from_errno("close");
829 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
830 err = got_error_from_errno("close");
831 if (err == NULL && failed)
832 err = got_error(GOT_ERR_PATCH_FAILED);
833 return err;