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);
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 return err;
222 switch (imsg.hdr.type) {
223 case GOT_IMSG_PATCH_DONE:
224 if (h != NULL && h->len == 0)
225 err = got_error(GOT_ERR_PATCH_MALFORMED);
226 goto done;
227 case GOT_IMSG_PATCH_HUNK:
228 if (h != NULL &&
229 (h->len == 0 || h->old_nonl || h->new_nonl)) {
230 err = got_error(GOT_ERR_PATCH_MALFORMED);
231 goto done;
233 lastmode = -1;
234 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
235 if (datalen != sizeof(hdr)) {
236 err = got_error(GOT_ERR_PRIVSEP_LEN);
237 goto done;
239 memcpy(&hdr, imsg.data, sizeof(hdr));
240 if ((h = calloc(1, sizeof(*h))) == NULL) {
241 err = got_error_from_errno("calloc");
242 goto done;
244 h->old_from = hdr.oldfrom;
245 h->old_lines = hdr.oldlines;
246 h->new_from = hdr.newfrom;
247 h->new_lines = hdr.newlines;
248 STAILQ_INSERT_TAIL(&p->head, h, entries);
249 break;
250 case GOT_IMSG_PATCH_LINE:
251 if (h == NULL) {
252 err = got_error(GOT_ERR_PRIVSEP_MSG);
253 goto done;
255 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
256 t = imsg.data;
257 /* at least one char */
258 if (datalen < 2 || t[datalen-1] != '\0') {
259 err = got_error(GOT_ERR_PRIVSEP_MSG);
260 goto done;
262 if (*t != ' ' && *t != '-' && *t != '+' &&
263 *t != '\\') {
264 err = got_error(GOT_ERR_PRIVSEP_MSG);
265 goto done;
268 if (*t != '\\')
269 err = pushline(h, t);
270 else if (lastmode == '-')
271 h->old_nonl = 1;
272 else if (lastmode == '+')
273 h->new_nonl = 1;
274 else
275 err = got_error(GOT_ERR_PATCH_MALFORMED);
277 if (err)
278 goto done;
280 lastmode = *t;
281 break;
282 default:
283 err = got_error(GOT_ERR_PRIVSEP_MSG);
284 goto done;
287 imsg_free(&imsg);
290 done:
291 imsg_free(&imsg);
292 return err;
295 /*
296 * Copy data from orig starting at copypos until pos into tmp.
297 * If pos is -1, copy until EOF.
298 */
299 static const struct got_error *
300 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
302 char buf[BUFSIZ];
303 size_t len, r, w;
305 if (fseeko(orig, copypos, SEEK_SET) == -1)
306 return got_error_from_errno("fseeko");
308 while (pos == -1 || copypos < pos) {
309 len = sizeof(buf);
310 if (pos > 0)
311 len = MIN(len, (size_t)pos - copypos);
312 r = fread(buf, 1, len, orig);
313 if (r != len && ferror(orig))
314 return got_error_from_errno("fread");
315 w = fwrite(buf, 1, r, tmp);
316 if (w != r)
317 return got_error_from_errno("fwrite");
318 copypos += len;
319 if (r != len && feof(orig)) {
320 if (pos == -1)
321 return NULL;
322 return got_error(GOT_ERR_HUNK_FAILED);
325 return NULL;
328 static const struct got_error *
329 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
331 const struct got_error *err = NULL;
332 char *line = NULL;
333 char mode = *h->lines[0];
334 size_t linesize = 0;
335 ssize_t linelen;
336 off_t match = -1;
337 long match_lineno = -1;
339 for (;;) {
340 linelen = getline(&line, &linesize, orig);
341 if (linelen == -1) {
342 if (ferror(orig))
343 err = got_error_from_errno("getline");
344 else if (match == -1)
345 err = got_error(GOT_ERR_HUNK_FAILED);
346 break;
348 if (line[linelen - 1] == '\n')
349 line[linelen - 1] = '\0';
350 (*lineno)++;
352 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
353 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
354 (mode == '+' && *lineno == h->old_from)) {
355 match = ftello(orig);
356 if (match == -1) {
357 err = got_error_from_errno("ftello");
358 break;
360 match -= linelen;
361 match_lineno = (*lineno)-1;
364 if (*lineno >= h->old_from && match != -1)
365 break;
368 if (err == NULL) {
369 *pos = match;
370 *lineno = match_lineno;
371 if (fseeko(orig, match, SEEK_SET) == -1)
372 err = got_error_from_errno("fseeko");
375 free(line);
376 return err;
379 static const struct got_error *
380 test_hunk(FILE *orig, struct got_patch_hunk *h)
382 const struct got_error *err = NULL;
383 char *line = NULL;
384 size_t linesize = 0, i = 0;
385 ssize_t linelen;
387 for (i = 0; i < h->len; ++i) {
388 switch (*h->lines[i]) {
389 case '+':
390 continue;
391 case ' ':
392 case '-':
393 linelen = getline(&line, &linesize, orig);
394 if (linelen == -1) {
395 if (ferror(orig))
396 err = got_error_from_errno("getline");
397 else
398 err = got_error(
399 GOT_ERR_HUNK_FAILED);
400 goto done;
402 if (line[linelen - 1] == '\n')
403 line[linelen - 1] = '\0';
404 if (strcmp(h->lines[i] + 1, line)) {
405 err = got_error(GOT_ERR_HUNK_FAILED);
406 goto done;
408 break;
412 done:
413 free(line);
414 return err;
417 static const struct got_error *
418 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
420 size_t i, new = 0;
422 for (i = 0; i < h->len; ++i) {
423 switch (*h->lines[i]) {
424 case ' ':
425 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
426 return got_error_from_errno("fprintf");
427 /* fallthrough */
428 case '-':
429 (*lineno)++;
430 break;
431 case '+':
432 new++;
433 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
434 return got_error_from_errno("fprintf");
435 if (new != h->new_lines || !h->new_nonl) {
436 if (fprintf(tmp, "\n") < 0)
437 return got_error_from_errno(
438 "fprintf");
440 break;
443 return NULL;
446 static const struct got_error *
447 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
448 mode_t *mode)
450 const struct got_error *err = NULL;
451 struct got_patch_hunk *h;
452 struct stat sb;
453 long lineno = 0;
454 FILE *orig;
455 off_t copypos, pos;
456 char *line = NULL;
457 size_t linesize = 0;
458 ssize_t linelen;
460 if (p->old == NULL) { /* create */
461 h = STAILQ_FIRST(&p->head);
462 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
463 return got_error(GOT_ERR_PATCH_MALFORMED);
464 if (nop)
465 return NULL;
466 return apply_hunk(tmp, h, &lineno);
469 if ((orig = fopen(path, "r")) == NULL) {
470 err = got_error_from_errno2("fopen", path);
471 goto done;
474 if (fstat(fileno(orig), &sb) == -1) {
475 err = got_error_from_errno("fstat");
476 goto done;
478 *mode = sb.st_mode;
480 copypos = 0;
481 STAILQ_FOREACH(h, &p->head, entries) {
482 tryagain:
483 err = locate_hunk(orig, h, &pos, &lineno);
484 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
485 h->err = err;
486 if (err != NULL)
487 goto done;
488 if (!nop)
489 err = copy(tmp, orig, copypos, pos);
490 if (err != NULL)
491 goto done;
492 copypos = pos;
494 err = test_hunk(orig, h);
495 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
496 /*
497 * try to apply the hunk again starting the search
498 * after the previous partial match.
499 */
500 if (fseeko(orig, pos, SEEK_SET) == -1) {
501 err = got_error_from_errno("fseeko");
502 goto done;
504 linelen = getline(&line, &linesize, orig);
505 if (linelen == -1) {
506 err = got_error_from_errno("getline");
507 goto done;
509 lineno++;
510 goto tryagain;
512 if (err != NULL)
513 goto done;
515 if (lineno + 1 != h->old_from)
516 h->offset = lineno + 1 - h->old_from;
518 if (!nop)
519 err = apply_hunk(tmp, h, &lineno);
520 if (err != NULL)
521 goto done;
523 copypos = ftello(orig);
524 if (copypos == -1) {
525 err = got_error_from_errno("ftello");
526 goto done;
530 if (p->new == NULL && sb.st_size != copypos) {
531 h = STAILQ_FIRST(&p->head);
532 h->err = got_error(GOT_ERR_HUNK_FAILED);
533 err = h->err;
534 } else if (!nop && !feof(orig))
535 err = copy(tmp, orig, copypos, -1);
537 done:
538 if (orig != NULL)
539 fclose(orig);
540 return err;
543 static const struct got_error *
544 report_progress(struct patch_args *pa, const char *old, const char *new,
545 unsigned char status, const struct got_error *orig_error)
547 const struct got_error *err;
548 struct got_patch_hunk *h;
550 err = pa->progress_cb(pa->progress_arg, old, new, status,
551 orig_error, 0, 0, 0, 0, 0, NULL);
552 if (err)
553 return err;
555 STAILQ_FOREACH(h, pa->head, entries) {
556 if (h->offset == 0 && h->err == NULL)
557 continue;
559 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
560 h->old_from, h->old_lines, h->new_from, h->new_lines,
561 h->offset, h->err);
562 if (err)
563 return err;
566 return NULL;
569 static const struct got_error *
570 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
571 const char *path)
573 return report_progress(arg, path, NULL, status, NULL);
576 static const struct got_error *
577 patch_add(void *arg, unsigned char status, const char *path)
579 return report_progress(arg, NULL, path, status, NULL);
582 static const struct got_error *
583 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
584 struct got_fileindex *fileindex, const char *old, const char *new,
585 struct got_patch *p, int nop, struct patch_args *pa,
586 got_cancel_cb cancel_cb, void *cancel_arg)
588 const struct got_error *err = NULL;
589 int file_renamed = 0;
590 char *oldpath = NULL, *newpath = NULL;
591 char *tmppath = NULL, *template = NULL, *parent = NULL;;
592 FILE *tmp = NULL;
593 mode_t mode = GOT_DEFAULT_FILE_MODE;
595 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
596 old) == -1) {
597 err = got_error_from_errno("asprintf");
598 goto done;
601 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
602 new) == -1) {
603 err = got_error_from_errno("asprintf");
604 goto done;
607 file_renamed = strcmp(oldpath, newpath);
609 if (asprintf(&template, "%s/got-patch",
610 got_worktree_get_root_path(worktree)) == -1) {
611 err = got_error_from_errno(template);
612 goto done;
615 if (!nop)
616 err = got_opentemp_named(&tmppath, &tmp, template);
617 if (err)
618 goto done;
619 err = patch_file(p, oldpath, tmp, nop, &mode);
620 if (err)
621 goto done;
623 if (nop)
624 goto done;
626 if (p->old != NULL && p->new == NULL) {
627 err = got_worktree_patch_schedule_rm(old, repo, worktree,
628 fileindex, patch_delete, pa);
629 goto done;
632 if (fchmod(fileno(tmp), mode) == -1) {
633 err = got_error_from_errno2("chmod", tmppath);
634 goto done;
637 if (rename(tmppath, newpath) == -1) {
638 if (errno != ENOENT) {
639 err = got_error_from_errno3("rename", tmppath,
640 newpath);
641 goto done;
644 err = got_path_dirname(&parent, newpath);
645 if (err != NULL)
646 goto done;
647 err = got_path_mkdir(parent);
648 if (err != NULL)
649 goto done;
650 if (rename(tmppath, newpath) == -1) {
651 err = got_error_from_errno3("rename", tmppath,
652 newpath);
653 goto done;
657 if (file_renamed) {
658 err = got_worktree_patch_schedule_rm(old, repo, worktree,
659 fileindex, patch_delete, pa);
660 if (err == NULL)
661 err = got_worktree_patch_schedule_add(new, repo,
662 worktree, fileindex, patch_add,
663 pa);
664 if (err)
665 unlink(newpath);
666 } else if (p->old == NULL) {
667 err = got_worktree_patch_schedule_add(new, repo, worktree,
668 fileindex, patch_add, pa);
669 if (err)
670 unlink(newpath);
671 } else
672 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
674 done:
675 free(parent);
676 free(template);
677 if (tmppath != NULL)
678 unlink(tmppath);
679 free(tmppath);
680 free(oldpath);
681 free(newpath);
682 return err;
685 static void
686 reverse_patch(struct got_patch *p)
688 struct got_patch_hunk *h;
689 size_t i;
690 long tmp;
692 STAILQ_FOREACH(h, &p->head, entries) {
693 tmp = h->old_from;
694 h->old_from = h->new_from;
695 h->new_from = tmp;
697 tmp = h->old_lines;
698 h->old_lines = h->new_lines;
699 h->new_lines = tmp;
701 tmp = h->old_nonl;
702 h->old_nonl = h->new_nonl;
703 h->new_nonl = tmp;
705 for (i = 0; i < h->len; ++i) {
706 if (*h->lines[i] == '+')
707 *h->lines[i] = '-';
708 else if (*h->lines[i] == '-')
709 *h->lines[i] = '+';
714 const struct got_error *
715 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
716 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
717 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
719 const struct got_error *err = NULL, *complete_err = NULL;
720 struct got_fileindex *fileindex = NULL;
721 char *fileindex_path = NULL;
722 char *oldpath, *newpath;
723 struct imsgbuf *ibuf;
724 int imsg_fds[2] = {-1, -1};
725 int done = 0, failed = 0;
726 pid_t pid;
728 ibuf = calloc(1, sizeof(*ibuf));
729 if (ibuf == NULL) {
730 err = got_error_from_errno("calloc");
731 goto done;
734 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
735 err = got_error_from_errno("socketpair");
736 goto done;
739 pid = fork();
740 if (pid == -1) {
741 err = got_error_from_errno("fork");
742 goto done;
743 } else if (pid == 0) {
744 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
745 NULL);
746 /* not reached */
749 if (close(imsg_fds[1]) == -1) {
750 err = got_error_from_errno("close");
751 goto done;
753 imsg_fds[1] = -1;
754 imsg_init(ibuf, imsg_fds[0]);
756 err = send_patch(ibuf, fd);
757 fd = -1;
758 if (err)
759 goto done;
761 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
762 worktree);
763 if (err)
764 goto done;
766 while (!done && err == NULL) {
767 struct got_patch p;
768 struct patch_args pa;
770 pa.progress_cb = progress_cb;
771 pa.progress_arg = progress_arg;
772 pa.head = &p.head;
774 err = recv_patch(ibuf, &done, &p, strip);
775 if (err || done)
776 break;
778 if (reverse)
779 reverse_patch(&p);
781 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
782 &newpath, worktree, repo, fileindex);
783 if (err == NULL)
784 err = apply_patch(worktree, repo, fileindex, oldpath,
785 newpath, &p, nop, &pa, cancel_cb, cancel_arg);
786 if (err != NULL) {
787 failed = 1;
788 /* recoverable errors */
789 if (err->code == GOT_ERR_FILE_STATUS ||
790 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
791 err = report_progress(&pa, p.old, p.new,
792 GOT_STATUS_CANNOT_UPDATE, err);
793 else if (err->code == GOT_ERR_HUNK_FAILED)
794 err = report_progress(&pa, p.old, p.new,
795 GOT_STATUS_CANNOT_UPDATE, NULL);
798 free(oldpath);
799 free(newpath);
800 patch_free(&p);
802 if (err)
803 break;
806 done:
807 if (fileindex != NULL)
808 complete_err = got_worktree_patch_complete(fileindex,
809 fileindex_path);
810 if (complete_err && err == NULL)
811 err = complete_err;
812 free(fileindex_path);
813 if (fd != -1 && close(fd) == -1 && err == NULL)
814 err = got_error_from_errno("close");
815 if (ibuf != NULL)
816 imsg_clear(ibuf);
817 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
818 err = got_error_from_errno("close");
819 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
820 err = got_error_from_errno("close");
821 if (err == NULL && failed)
822 err = got_error(GOT_ERR_PATCH_FAILED);
823 return err;