2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
38 #include "got_error.h"
39 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_diff.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_sha1.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
59 const struct got_error *err;
73 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
79 struct got_patch_hunk_head head;
83 got_patch_progress_cb progress_cb;
85 struct got_patch_hunk_head *head;
88 static const struct got_error *
89 send_patch(struct imsgbuf *ibuf, int fd)
91 const struct got_error *err = NULL;
93 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
95 err = got_error_from_errno(
96 "imsg_compose GOT_IMSG_PATCH_FILE");
101 return got_privsep_flush_imsg(ibuf);
105 patch_free(struct got_patch *p)
107 struct got_patch_hunk *h;
110 while (!STAILQ_EMPTY(&p->head)) {
111 h = STAILQ_FIRST(&p->head);
112 STAILQ_REMOVE_HEAD(&p->head, entries);
114 for (i = 0; i < h->len; ++i)
123 memset(p, 0, sizeof(*p));
124 STAILQ_INIT(&p->head);
127 static const struct got_error *
128 pushline(struct got_patch_hunk *h, const char *line)
133 if (h->len == h->cap) {
134 if ((newcap = h->cap * 1.5) == 0)
136 t = recallocarray(h->lines, h->cap, newcap,
137 sizeof(h->lines[0]));
139 return got_error_from_errno("recallocarray");
144 if ((t = strdup(line)) == NULL)
145 return got_error_from_errno("strdup");
147 h->lines[h->len++] = t;
151 static const struct got_error *
152 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
154 const struct got_error *err = NULL;
156 struct got_imsg_patch_hunk hdr;
157 struct got_imsg_patch patch;
158 struct got_patch_hunk *h = NULL;
162 memset(p, 0, sizeof(*p));
163 STAILQ_INIT(&p->head);
165 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
168 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
172 if (imsg.hdr.type != GOT_IMSG_PATCH) {
173 err = got_error(GOT_ERR_PRIVSEP_MSG);
176 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
177 if (datalen != sizeof(patch)) {
178 err = got_error(GOT_ERR_PRIVSEP_LEN);
181 memcpy(&patch, imsg.data, sizeof(patch));
183 if (patch.old[sizeof(patch.old)-1] != '\0' ||
184 patch.new[sizeof(patch.new)-1] != '\0' ||
185 patch.cid[sizeof(patch.cid)-1] != '\0' ||
186 patch.blob[sizeof(patch.blob)-1] != '\0') {
187 err = got_error(GOT_ERR_PRIVSEP_LEN);
191 if (*patch.cid != '\0')
192 strlcpy(p->cid, patch.cid, sizeof(p->cid));
194 if (*patch.blob != '\0')
195 strlcpy(p->blob, patch.blob, sizeof(p->blob));
197 /* automatically set strip=1 for git-style diffs */
198 if (strip == -1 && patch.git &&
199 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
200 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
203 /* prefer the new name if not /dev/null for not git-style diffs */
204 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
205 err = got_path_strip(&p->old, patch.new, strip);
208 } else if (*patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.old, strip);
214 if (*patch.new != '\0') {
215 err = got_path_strip(&p->new, patch.new, strip);
220 if (p->old == NULL && p->new == NULL) {
221 err = got_error(GOT_ERR_PATCH_MALFORMED);
230 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
236 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
237 switch (imsg.hdr.type) {
238 case GOT_IMSG_PATCH_DONE:
239 if (h != NULL && h->len == 0)
240 err = got_error(GOT_ERR_PATCH_MALFORMED);
242 case GOT_IMSG_PATCH_HUNK:
244 (h->len == 0 || h->old_nonl || h->new_nonl)) {
245 err = got_error(GOT_ERR_PATCH_MALFORMED);
249 if (datalen != sizeof(hdr)) {
250 err = got_error(GOT_ERR_PRIVSEP_LEN);
253 memcpy(&hdr, imsg.data, sizeof(hdr));
254 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
255 err = got_error(GOT_ERR_PRIVSEP_LEN);
258 if ((h = calloc(1, sizeof(*h))) == NULL) {
259 err = got_error_from_errno("calloc");
262 h->old_from = hdr.oldfrom;
263 h->old_lines = hdr.oldlines;
264 h->new_from = hdr.newfrom;
265 h->new_lines = hdr.newlines;
266 STAILQ_INSERT_TAIL(&p->head, h, entries);
268 case GOT_IMSG_PATCH_LINE:
270 err = got_error(GOT_ERR_PRIVSEP_MSG);
274 /* at least one char */
275 if (datalen < 2 || t[datalen-1] != '\0') {
276 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 if (*t != ' ' && *t != '-' && *t != '+' &&
281 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 err = pushline(h, t);
287 else if (lastmode == '-')
289 else if (lastmode == '+')
292 err = got_error(GOT_ERR_PATCH_MALFORMED);
300 err = got_error(GOT_ERR_PRIVSEP_MSG);
316 reverse_patch(struct got_patch *p)
318 struct got_patch_hunk *h;
322 STAILQ_FOREACH(h, &p->head, entries) {
324 h->old_from = h->new_from;
328 h->old_lines = h->new_lines;
332 h->old_nonl = h->new_nonl;
335 for (i = 0; i < h->len; ++i) {
336 if (*h->lines[i] == '+')
338 else if (*h->lines[i] == '-')
345 * Copy data from orig starting at copypos until pos into tmp.
346 * If pos is -1, copy until EOF.
348 static const struct got_error *
349 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
354 if (fseeko(orig, copypos, SEEK_SET) == -1)
355 return got_error_from_errno("fseeko");
357 while (pos == -1 || copypos < pos) {
360 len = MIN(len, (size_t)pos - copypos);
361 r = fread(buf, 1, len, orig);
362 if (r != len && ferror(orig))
363 return got_error_from_errno("fread");
364 w = fwrite(buf, 1, r, tmp);
366 return got_error_from_errno("fwrite");
368 if (r != len && feof(orig)) {
371 return got_error(GOT_ERR_HUNK_FAILED);
377 static const struct got_error *
378 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
380 const struct got_error *err = NULL;
382 char mode = *h->lines[0];
386 int match_lineno = -1;
389 linelen = getline(&line, &linesize, orig);
392 err = got_error_from_errno("getline");
393 else if (match == -1)
394 err = got_error(GOT_ERR_HUNK_FAILED);
397 if (line[linelen - 1] == '\n')
398 line[linelen - 1] = '\0';
401 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
402 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
403 (mode == '+' && *lineno == h->old_from)) {
404 match = ftello(orig);
406 err = got_error_from_errno("ftello");
410 match_lineno = (*lineno)-1;
413 if (*lineno >= h->old_from && match != -1)
419 *lineno = match_lineno;
420 if (fseeko(orig, match, SEEK_SET) == -1)
421 err = got_error_from_errno("fseeko");
429 linecmp(const char *a, const char *b, int *mangled)
440 while (*a == '\t' || *a == ' ' || *a == '\f')
442 while (*b == '\t' || *b == ' ' || *b == '\f')
444 if (*a == '\0' || *a != *b)
452 static const struct got_error *
453 test_hunk(FILE *orig, struct got_patch_hunk *h)
455 const struct got_error *err = NULL;
457 size_t linesize = 0, i = 0;
461 for (i = 0; i < h->len; ++i) {
462 switch (*h->lines[i]) {
467 linelen = getline(&line, &linesize, orig);
470 err = got_error_from_errno("getline");
473 GOT_ERR_HUNK_FAILED);
476 if (line[linelen - 1] == '\n')
477 line[linelen - 1] = '\0';
478 if (linecmp(h->lines[i] + 1, line, &mangled)) {
479 err = got_error(GOT_ERR_HUNK_FAILED);
493 static const struct got_error *
494 apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
497 const struct got_error *err = NULL;
499 size_t linesize = 0, i, new = 0;
504 if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
505 return got_error_from_errno("fseeko");
507 for (i = 0; i < h->len; ++i) {
508 switch (mode = *h->lines[i]) {
513 linelen = getline(&line, &linesize, orig);
515 err = got_error_from_errno("getline");
518 if (line[linelen - 1] == '\n')
519 line[linelen - 1] = '\0';
525 if (fprintf(tmp, "%s\n", t) < 0) {
526 err = got_error_from_errno("fprintf");
532 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
533 err = got_error_from_errno("fprintf");
536 if (new != h->new_lines || !h->new_nonl) {
537 if (fprintf(tmp, "\n") < 0) {
538 err = got_error_from_errno("fprintf");
551 static const struct got_error *
552 patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
554 const struct got_error *err = NULL;
555 struct got_patch_hunk *h;
563 if (p->old == NULL) { /* create */
564 h = STAILQ_FIRST(&p->head);
565 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
566 return got_error(GOT_ERR_PATCH_MALFORMED);
567 return apply_hunk(orig, tmp, h, &lineno, 0);
570 if (fstat(fileno(orig), &sb) == -1)
571 return got_error_from_errno("fstat");
574 STAILQ_FOREACH(h, &p->head, entries) {
576 err = locate_hunk(orig, h, &pos, &lineno);
577 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
581 err = copy(tmp, orig, copypos, pos);
586 err = test_hunk(orig, h);
587 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
589 * try to apply the hunk again starting the search
590 * after the previous partial match.
592 if (fseeko(orig, pos, SEEK_SET) == -1)
593 return got_error_from_errno("fseeko");
594 linelen = getline(&line, &linesize, orig);
596 return got_error_from_errno("getline");
603 if (lineno + 1 != h->old_from)
604 h->offset = lineno + 1 - h->old_from;
606 err = apply_hunk(orig, tmp, h, &lineno, pos);
610 copypos = ftello(orig);
612 return got_error_from_errno("ftello");
615 if (p->new == NULL && sb.st_size != copypos) {
616 h = STAILQ_FIRST(&p->head);
617 h->err = got_error(GOT_ERR_HUNK_FAILED);
619 } else if (!feof(orig))
620 err = copy(tmp, orig, copypos, -1);
625 static const struct got_error *
626 report_progress(struct patch_args *pa, const char *old, const char *new,
627 unsigned char status, const struct got_error *orig_error)
629 const struct got_error *err;
630 struct got_patch_hunk *h;
632 err = pa->progress_cb(pa->progress_arg, old, new, status,
633 orig_error, 0, 0, 0, 0, 0, 0, NULL);
637 STAILQ_FOREACH(h, pa->head, entries) {
638 if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
641 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
642 h->old_from, h->old_lines, h->new_from, h->new_lines,
643 h->offset, h->ws_mangled, h->err);
651 static const struct got_error *
652 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
655 return report_progress(arg, path, NULL, status, NULL);
658 static const struct got_error *
659 patch_add(void *arg, unsigned char status, const char *path)
661 return report_progress(arg, NULL, path, status, NULL);
664 static const struct got_error *
665 open_blob(char **path, FILE **fp, const char *blobid,
666 struct got_repository *repo)
668 const struct got_error *err = NULL;
669 struct got_blob_object *blob = NULL;
670 struct got_object_id id, *idptr, *matched_id = NULL;
676 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
677 err = got_repo_match_object_id(&matched_id, NULL, blobid,
678 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
684 if (!got_parse_sha1_digest(id.sha1, blobid))
685 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
689 fd = got_opentempfd();
691 err = got_error_from_errno("got_opentempfd");
695 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
699 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
703 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
708 if (fd != -1 && close(fd) == -1 && err == NULL)
709 err = got_error_from_errno("close");
711 got_object_blob_close(blob);
712 if (matched_id != NULL)
726 static const struct got_error *
727 prepare_merge(int *do_merge, char **apath, FILE **afile,
728 struct got_worktree *worktree, struct got_repository *repo,
729 struct got_patch *p, struct got_object_id *commit_id,
730 struct got_tree_object *tree, const char *path)
732 const struct got_error *err = NULL;
738 /* don't run the diff3 merge on creations/deletions */
739 if (p->old == NULL || p->new == NULL)
743 struct got_object_id *id;
745 err = got_object_tree_find_path(&id, NULL, repo, tree, path);
748 got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
749 got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
751 err = open_blob(apath, afile, p->blob, repo);
752 *do_merge = err == NULL;
753 } else if (*p->blob != '\0') {
754 err = open_blob(apath, afile, p->blob, repo);
756 * ignore failures to open this blob, we might have
759 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
760 err->code != GOT_ERR_NO_OBJ)
762 *do_merge = err == NULL;
769 static const struct got_error *
770 apply_patch(int *overlapcnt, struct got_worktree *worktree,
771 struct got_repository *repo, struct got_fileindex *fileindex,
772 const char *old, const char *new, struct got_patch *p, int nop,
773 int reverse, struct got_object_id *commit_id,
774 struct got_tree_object *tree, struct patch_args *pa,
775 got_cancel_cb cancel_cb, void *cancel_arg)
777 const struct got_error *err = NULL;
779 int do_merge = 0, file_renamed = 0;
780 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
781 char *oldpath = NULL, *newpath = NULL;
782 char *tmppath = NULL, *template = NULL, *parent = NULL;
783 char *apath = NULL, *mergepath = NULL;
784 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
787 mode_t mode = GOT_DEFAULT_FILE_MODE;
791 err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
792 commit_id, tree, old);
796 if (reverse && !do_merge)
799 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
801 err = got_error_from_errno("asprintf");
805 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
807 err = got_error_from_errno("asprintf");
811 file_renamed = strcmp(oldpath, newpath);
813 if (asprintf(&template, "%s/got-patch",
814 got_worktree_get_root_path(worktree)) == -1) {
815 err = got_error_from_errno(template);
819 if (p->old != NULL) {
820 if ((oldfile = fopen(oldpath, "r")) == NULL) {
821 err = got_error_from_errno2("open", oldpath);
824 if (fstat(fileno(oldfile), &sb) == -1) {
825 err = got_error_from_errno2("fstat", oldpath);
831 err = got_opentemp_named(&tmppath, &tmpfile, template);
835 outfd = fileno(tmpfile);
836 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
841 const char *type, *id;
843 if (fseeko(afile, 0, SEEK_SET) == -1 ||
844 fseeko(oldfile, 0, SEEK_SET) == -1 ||
845 fseeko(tmpfile, 0, SEEK_SET) == -1) {
846 err = got_error_from_errno("fseeko");
850 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
851 err = got_error_from_errno("asprintf");
856 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
857 err = got_error_from_errno("asprintf");
862 if (*p->cid != '\0') {
870 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
871 err = got_error_from_errno("asprintf");
889 err = got_opentemp_named(&mergepath, &mergefile, template);
893 outfd = fileno(mergefile);
895 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
896 oldfile, oldlabel, anclabel, newlabel,
897 GOT_DIFF_ALGORITHM_PATIENCE);
905 if (p->old != NULL && p->new == NULL) {
906 err = got_worktree_patch_schedule_rm(old, repo, worktree,
907 fileindex, patch_delete, pa);
911 if (fchmod(outfd, mode) == -1) {
912 err = got_error_from_errno2("chmod", tmppath);
916 if (rename(outpath, newpath) == -1) {
917 if (errno != ENOENT) {
918 err = got_error_from_errno3("rename", outpath,
923 err = got_path_dirname(&parent, newpath);
926 err = got_path_mkdir(parent);
929 if (rename(outpath, newpath) == -1) {
930 err = got_error_from_errno3("rename", outpath,
937 err = got_worktree_patch_schedule_rm(old, repo, worktree,
938 fileindex, patch_delete, pa);
940 err = got_worktree_patch_schedule_add(new, repo,
941 worktree, fileindex, patch_add,
945 } else if (p->old == NULL) {
946 err = got_worktree_patch_schedule_add(new, repo, worktree,
947 fileindex, patch_add, pa);
950 } else if (*overlapcnt != 0)
951 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
953 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
955 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
963 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
964 err = got_error_from_errno("fclose");
968 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
969 err = got_error_from_errno("fclose");
973 if (afile != NULL && fclose(afile) == EOF && err == NULL)
974 err = got_error_from_errno("fclose");
977 if (mergepath != NULL)
979 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
980 err = got_error_from_errno("fclose");
990 const struct got_error *
991 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
992 int nop, int strip, int reverse, struct got_object_id *commit_id,
993 got_patch_progress_cb progress_cb, void *progress_arg,
994 got_cancel_cb cancel_cb, void *cancel_arg)
996 const struct got_error *err = NULL, *complete_err = NULL;
997 struct got_fileindex *fileindex = NULL;
998 struct got_commit_object *commit = NULL;
999 struct got_tree_object *tree = NULL;
1000 char *fileindex_path = NULL;
1001 char *oldpath, *newpath;
1002 struct imsgbuf *ibuf;
1003 int imsg_fds[2] = {-1, -1};
1004 int overlapcnt, done = 0, failed = 0;
1007 ibuf = calloc(1, sizeof(*ibuf));
1009 err = got_error_from_errno("calloc");
1013 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1014 err = got_error_from_errno("socketpair");
1020 err = got_error_from_errno("fork");
1022 } else if (pid == 0) {
1023 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1028 if (close(imsg_fds[1]) == -1) {
1029 err = got_error_from_errno("close");
1033 imsg_init(ibuf, imsg_fds[0]);
1035 err = send_patch(ibuf, fd);
1040 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1046 err = got_object_open_as_commit(&commit, repo, commit_id);
1050 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1055 while (!done && err == NULL) {
1057 struct patch_args pa;
1059 pa.progress_cb = progress_cb;
1060 pa.progress_arg = progress_arg;
1063 err = recv_patch(ibuf, &done, &p, strip);
1067 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1068 &newpath, worktree, repo, fileindex);
1070 err = apply_patch(&overlapcnt, worktree, repo,
1071 fileindex, oldpath, newpath, &p, nop, reverse,
1072 commit_id, tree, &pa, cancel_cb, cancel_arg);
1075 /* recoverable errors */
1076 if (err->code == GOT_ERR_FILE_STATUS ||
1077 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1078 err = report_progress(&pa, p.old, p.new,
1079 GOT_STATUS_CANNOT_UPDATE, err);
1080 else if (err->code == GOT_ERR_HUNK_FAILED)
1081 err = report_progress(&pa, p.old, p.new,
1082 GOT_STATUS_CANNOT_UPDATE, NULL);
1084 if (overlapcnt != 0)
1096 if (fileindex != NULL)
1097 complete_err = got_worktree_patch_complete(fileindex,
1099 if (complete_err && err == NULL)
1101 free(fileindex_path);
1103 got_object_tree_close(tree);
1105 got_object_commit_close(commit);
1106 if (fd != -1 && close(fd) == -1 && err == NULL)
1107 err = got_error_from_errno("close");
1110 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1111 err = got_error_from_errno("close");
1112 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1113 err = got_error_from_errno("close");
1114 if (err == NULL && failed)
1115 err = got_error(GOT_ERR_PATCH_FAILED);