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 int offset;
58 int old_nonl;
59 int new_nonl;
60 int old_from;
61 int old_lines;
62 int new_from;
63 int 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 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
228 switch (imsg.hdr.type) {
229 case GOT_IMSG_PATCH_DONE:
230 if (h != NULL && h->len == 0)
231 err = got_error(GOT_ERR_PATCH_MALFORMED);
232 goto done;
233 case GOT_IMSG_PATCH_HUNK:
234 if (h != NULL &&
235 (h->len == 0 || h->old_nonl || h->new_nonl)) {
236 err = got_error(GOT_ERR_PATCH_MALFORMED);
237 goto done;
239 lastmode = -1;
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 (hdr.oldfrom < 0 || hdr.newfrom < 0) {
246 err = got_error(GOT_ERR_PRIVSEP_LEN);
247 goto done;
249 if ((h = calloc(1, sizeof(*h))) == NULL) {
250 err = got_error_from_errno("calloc");
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 t = imsg.data;
265 /* at least one char */
266 if (datalen < 2 || t[datalen-1] != '\0') {
267 err = got_error(GOT_ERR_PRIVSEP_MSG);
268 goto done;
270 if (*t != ' ' && *t != '-' && *t != '+' &&
271 *t != '\\') {
272 err = got_error(GOT_ERR_PRIVSEP_MSG);
273 goto done;
276 if (*t != '\\')
277 err = pushline(h, t);
278 else if (lastmode == '-')
279 h->old_nonl = 1;
280 else if (lastmode == '+')
281 h->new_nonl = 1;
282 else
283 err = got_error(GOT_ERR_PATCH_MALFORMED);
285 if (err)
286 goto done;
288 lastmode = *t;
289 break;
290 default:
291 err = got_error(GOT_ERR_PRIVSEP_MSG);
292 goto done;
295 imsg_free(&imsg);
298 done:
299 if (err)
300 patch_free(p);
302 imsg_free(&imsg);
303 return err;
306 /*
307 * Copy data from orig starting at copypos until pos into tmp.
308 * If pos is -1, copy until EOF.
309 */
310 static const struct got_error *
311 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
313 char buf[BUFSIZ];
314 size_t len, r, w;
316 if (fseeko(orig, copypos, SEEK_SET) == -1)
317 return got_error_from_errno("fseeko");
319 while (pos == -1 || copypos < pos) {
320 len = sizeof(buf);
321 if (pos > 0)
322 len = MIN(len, (size_t)pos - copypos);
323 r = fread(buf, 1, len, orig);
324 if (r != len && ferror(orig))
325 return got_error_from_errno("fread");
326 w = fwrite(buf, 1, r, tmp);
327 if (w != r)
328 return got_error_from_errno("fwrite");
329 copypos += len;
330 if (r != len && feof(orig)) {
331 if (pos == -1)
332 return NULL;
333 return got_error(GOT_ERR_HUNK_FAILED);
336 return NULL;
339 static const struct got_error *
340 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
342 const struct got_error *err = NULL;
343 char *line = NULL;
344 char mode = *h->lines[0];
345 size_t linesize = 0;
346 ssize_t linelen;
347 off_t match = -1;
348 int match_lineno = -1;
350 for (;;) {
351 linelen = getline(&line, &linesize, orig);
352 if (linelen == -1) {
353 if (ferror(orig))
354 err = got_error_from_errno("getline");
355 else if (match == -1)
356 err = got_error(GOT_ERR_HUNK_FAILED);
357 break;
359 if (line[linelen - 1] == '\n')
360 line[linelen - 1] = '\0';
361 (*lineno)++;
363 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
364 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
365 (mode == '+' && *lineno == h->old_from)) {
366 match = ftello(orig);
367 if (match == -1) {
368 err = got_error_from_errno("ftello");
369 break;
371 match -= linelen;
372 match_lineno = (*lineno)-1;
375 if (*lineno >= h->old_from && match != -1)
376 break;
379 if (err == NULL) {
380 *pos = match;
381 *lineno = match_lineno;
382 if (fseeko(orig, match, SEEK_SET) == -1)
383 err = got_error_from_errno("fseeko");
386 free(line);
387 return err;
390 static const struct got_error *
391 test_hunk(FILE *orig, struct got_patch_hunk *h)
393 const struct got_error *err = NULL;
394 char *line = NULL;
395 size_t linesize = 0, i = 0;
396 ssize_t linelen;
398 for (i = 0; i < h->len; ++i) {
399 switch (*h->lines[i]) {
400 case '+':
401 continue;
402 case ' ':
403 case '-':
404 linelen = getline(&line, &linesize, orig);
405 if (linelen == -1) {
406 if (ferror(orig))
407 err = got_error_from_errno("getline");
408 else
409 err = got_error(
410 GOT_ERR_HUNK_FAILED);
411 goto done;
413 if (line[linelen - 1] == '\n')
414 line[linelen - 1] = '\0';
415 if (strcmp(h->lines[i] + 1, line)) {
416 err = got_error(GOT_ERR_HUNK_FAILED);
417 goto done;
419 break;
423 done:
424 free(line);
425 return err;
428 static const struct got_error *
429 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
431 size_t i, new = 0;
433 for (i = 0; i < h->len; ++i) {
434 switch (*h->lines[i]) {
435 case ' ':
436 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
437 return got_error_from_errno("fprintf");
438 /* fallthrough */
439 case '-':
440 (*lineno)++;
441 break;
442 case '+':
443 new++;
444 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
445 return got_error_from_errno("fprintf");
446 if (new != h->new_lines || !h->new_nonl) {
447 if (fprintf(tmp, "\n") < 0)
448 return got_error_from_errno(
449 "fprintf");
451 break;
454 return NULL;
457 static const struct got_error *
458 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, int nop, mode_t *mode)
460 const struct got_error *err = NULL;
461 struct got_patch_hunk *h;
462 struct stat sb;
463 int lineno = 0;
464 off_t copypos, pos;
465 char *line = NULL;
466 size_t linesize = 0;
467 ssize_t linelen;
469 if (p->old == NULL) { /* create */
470 h = STAILQ_FIRST(&p->head);
471 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
472 return got_error(GOT_ERR_PATCH_MALFORMED);
473 if (nop)
474 return NULL;
475 return apply_hunk(tmp, h, &lineno);
478 if (fstat(fileno(orig), &sb) == -1)
479 return got_error_from_errno("fstat");
480 *mode = sb.st_mode;
482 copypos = 0;
483 STAILQ_FOREACH(h, &p->head, entries) {
484 tryagain:
485 err = locate_hunk(orig, h, &pos, &lineno);
486 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
487 h->err = err;
488 if (err != NULL)
489 return err;
490 if (!nop)
491 err = copy(tmp, orig, copypos, pos);
492 if (err != NULL)
493 return err;
494 copypos = pos;
496 err = test_hunk(orig, h);
497 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
498 /*
499 * try to apply the hunk again starting the search
500 * after the previous partial match.
501 */
502 if (fseeko(orig, pos, SEEK_SET) == -1)
503 return got_error_from_errno("fseeko");
504 linelen = getline(&line, &linesize, orig);
505 if (linelen == -1)
506 return got_error_from_errno("getline");
507 lineno++;
508 goto tryagain;
510 if (err != NULL)
511 return err;
513 if (lineno + 1 != h->old_from)
514 h->offset = lineno + 1 - h->old_from;
516 if (!nop)
517 err = apply_hunk(tmp, h, &lineno);
518 if (err != NULL)
519 return err;
521 copypos = ftello(orig);
522 if (copypos == -1)
523 return got_error_from_errno("ftello");
526 if (p->new == NULL && sb.st_size != copypos) {
527 h = STAILQ_FIRST(&p->head);
528 h->err = got_error(GOT_ERR_HUNK_FAILED);
529 err = h->err;
530 } else if (!nop && !feof(orig))
531 err = copy(tmp, orig, copypos, -1);
533 return err;
536 static const struct got_error *
537 report_progress(struct patch_args *pa, const char *old, const char *new,
538 unsigned char status, const struct got_error *orig_error)
540 const struct got_error *err;
541 struct got_patch_hunk *h;
543 err = pa->progress_cb(pa->progress_arg, old, new, status,
544 orig_error, 0, 0, 0, 0, 0, NULL);
545 if (err)
546 return err;
548 STAILQ_FOREACH(h, pa->head, entries) {
549 if (h->offset == 0 && h->err == NULL)
550 continue;
552 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
553 h->old_from, h->old_lines, h->new_from, h->new_lines,
554 h->offset, h->err);
555 if (err)
556 return err;
559 return NULL;
562 static const struct got_error *
563 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
564 const char *path)
566 return report_progress(arg, path, NULL, status, NULL);
569 static const struct got_error *
570 patch_add(void *arg, unsigned char status, const char *path)
572 return report_progress(arg, NULL, path, status, NULL);
575 static const struct got_error *
576 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
577 struct got_fileindex *fileindex, const char *old, const char *new,
578 struct got_patch *p, int nop, struct patch_args *pa,
579 got_cancel_cb cancel_cb, void *cancel_arg)
581 const struct got_error *err = NULL;
582 int file_renamed = 0;
583 char *oldpath = NULL, *newpath = NULL;
584 char *tmppath = NULL, *template = NULL, *parent = NULL;
585 FILE *oldfile = NULL, *tmp = NULL;
586 mode_t mode = GOT_DEFAULT_FILE_MODE;
588 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
589 old) == -1) {
590 err = got_error_from_errno("asprintf");
591 goto done;
594 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
595 new) == -1) {
596 err = got_error_from_errno("asprintf");
597 goto done;
600 file_renamed = strcmp(oldpath, newpath);
602 if (asprintf(&template, "%s/got-patch",
603 got_worktree_get_root_path(worktree)) == -1) {
604 err = got_error_from_errno(template);
605 goto done;
608 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
609 err = got_error_from_errno2("open", oldpath);
610 goto done;
613 if (!nop)
614 err = got_opentemp_named(&tmppath, &tmp, template);
615 if (err)
616 goto done;
617 err = patch_file(p, oldfile, tmp, nop, &mode);
618 if (err)
619 goto done;
621 if (nop)
622 goto done;
624 if (p->old != NULL && p->new == NULL) {
625 err = got_worktree_patch_schedule_rm(old, repo, worktree,
626 fileindex, patch_delete, pa);
627 goto done;
630 if (fchmod(fileno(tmp), mode) == -1) {
631 err = got_error_from_errno2("chmod", tmppath);
632 goto done;
635 if (rename(tmppath, newpath) == -1) {
636 if (errno != ENOENT) {
637 err = got_error_from_errno3("rename", tmppath,
638 newpath);
639 goto done;
642 err = got_path_dirname(&parent, newpath);
643 if (err != NULL)
644 goto done;
645 err = got_path_mkdir(parent);
646 if (err != NULL)
647 goto done;
648 if (rename(tmppath, newpath) == -1) {
649 err = got_error_from_errno3("rename", tmppath,
650 newpath);
651 goto done;
655 if (file_renamed) {
656 err = got_worktree_patch_schedule_rm(old, repo, worktree,
657 fileindex, patch_delete, pa);
658 if (err == NULL)
659 err = got_worktree_patch_schedule_add(new, repo,
660 worktree, fileindex, patch_add,
661 pa);
662 if (err)
663 unlink(newpath);
664 } else if (p->old == NULL) {
665 err = got_worktree_patch_schedule_add(new, repo, worktree,
666 fileindex, patch_add, pa);
667 if (err)
668 unlink(newpath);
669 } else
670 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
672 done:
673 free(parent);
674 free(template);
675 if (tmppath != NULL)
676 unlink(tmppath);
677 if (tmp != NULL && fclose(tmp) == EOF && err == NULL)
678 err = got_error_from_errno("fclose");
679 free(tmppath);
680 free(oldpath);
681 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
682 err = got_error_from_errno("fclose");
683 free(newpath);
684 return err;
687 static void
688 reverse_patch(struct got_patch *p)
690 struct got_patch_hunk *h;
691 size_t i;
692 int tmp;
694 STAILQ_FOREACH(h, &p->head, entries) {
695 tmp = h->old_from;
696 h->old_from = h->new_from;
697 h->new_from = tmp;
699 tmp = h->old_lines;
700 h->old_lines = h->new_lines;
701 h->new_lines = tmp;
703 tmp = h->old_nonl;
704 h->old_nonl = h->new_nonl;
705 h->new_nonl = tmp;
707 for (i = 0; i < h->len; ++i) {
708 if (*h->lines[i] == '+')
709 *h->lines[i] = '-';
710 else if (*h->lines[i] == '-')
711 *h->lines[i] = '+';
716 const struct got_error *
717 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
718 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
719 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
721 const struct got_error *err = NULL, *complete_err = NULL;
722 struct got_fileindex *fileindex = NULL;
723 char *fileindex_path = NULL;
724 char *oldpath, *newpath;
725 struct imsgbuf *ibuf;
726 int imsg_fds[2] = {-1, -1};
727 int done = 0, failed = 0;
728 pid_t pid;
730 ibuf = calloc(1, sizeof(*ibuf));
731 if (ibuf == NULL) {
732 err = got_error_from_errno("calloc");
733 goto done;
736 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
737 err = got_error_from_errno("socketpair");
738 goto done;
741 pid = fork();
742 if (pid == -1) {
743 err = got_error_from_errno("fork");
744 goto done;
745 } else if (pid == 0) {
746 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
747 NULL);
748 /* not reached */
751 if (close(imsg_fds[1]) == -1) {
752 err = got_error_from_errno("close");
753 goto done;
755 imsg_fds[1] = -1;
756 imsg_init(ibuf, imsg_fds[0]);
758 err = send_patch(ibuf, fd);
759 fd = -1;
760 if (err)
761 goto done;
763 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
764 worktree);
765 if (err)
766 goto done;
768 while (!done && err == NULL) {
769 struct got_patch p;
770 struct patch_args pa;
772 pa.progress_cb = progress_cb;
773 pa.progress_arg = progress_arg;
774 pa.head = &p.head;
776 err = recv_patch(ibuf, &done, &p, strip);
777 if (err || done)
778 break;
780 if (reverse)
781 reverse_patch(&p);
783 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
784 &newpath, worktree, repo, fileindex);
785 if (err == NULL)
786 err = apply_patch(worktree, repo, fileindex, oldpath,
787 newpath, &p, nop, &pa, cancel_cb, cancel_arg);
788 if (err != NULL) {
789 failed = 1;
790 /* recoverable errors */
791 if (err->code == GOT_ERR_FILE_STATUS ||
792 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
793 err = report_progress(&pa, p.old, p.new,
794 GOT_STATUS_CANNOT_UPDATE, err);
795 else if (err->code == GOT_ERR_HUNK_FAILED)
796 err = report_progress(&pa, p.old, p.new,
797 GOT_STATUS_CANNOT_UPDATE, NULL);
800 free(oldpath);
801 free(newpath);
802 patch_free(&p);
804 if (err)
805 break;
808 done:
809 if (fileindex != NULL)
810 complete_err = got_worktree_patch_complete(fileindex,
811 fileindex_path);
812 if (complete_err && err == NULL)
813 err = complete_err;
814 free(fileindex_path);
815 if (fd != -1 && close(fd) == -1 && err == NULL)
816 err = got_error_from_errno("close");
817 if (ibuf != NULL)
818 imsg_clear(ibuf);
819 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
820 err = got_error_from_errno("close");
821 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
822 err = got_error_from_errno("close");
823 if (err == NULL && failed)
824 err = got_error(GOT_ERR_PATCH_FAILED);
825 return err;