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 /* automatically set strip=1 for git-style diffs */
180 if (strip == -1 && patch.git &&
181 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
182 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
183 strip = 1;
185 /* prefer the new name if not /dev/null for not git-style diffs */
186 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
187 err = got_path_strip(&p->old, patch.new, strip);
188 if (err)
189 goto done;
190 } else if (*patch.old != '\0') {
191 err = got_path_strip(&p->old, patch.old, strip);
192 if (err)
193 goto done;
196 if (*patch.new != '\0') {
197 err = got_path_strip(&p->new, patch.new, strip);
198 if (err)
199 goto done;
202 if (p->old == NULL && p->new == NULL) {
203 err = got_error(GOT_ERR_PATCH_MALFORMED);
204 goto done;
207 imsg_free(&imsg);
209 for (;;) {
210 char *t;
212 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
213 if (err)
214 return err;
216 switch (imsg.hdr.type) {
217 case GOT_IMSG_PATCH_DONE:
218 goto done;
219 case GOT_IMSG_PATCH_HUNK:
220 if (h != NULL && (h->old_nonl || h->new_nonl)) {
221 err = got_error(GOT_ERR_PATCH_MALFORMED);
222 goto done;
224 lastmode = -1;
225 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
226 if (datalen != sizeof(hdr)) {
227 err = got_error(GOT_ERR_PRIVSEP_LEN);
228 goto done;
230 memcpy(&hdr, imsg.data, sizeof(hdr));
231 if ((h = calloc(1, sizeof(*h))) == NULL) {
232 err = got_error_from_errno("calloc");
233 goto done;
235 h->old_from = hdr.oldfrom;
236 h->old_lines = hdr.oldlines;
237 h->new_from = hdr.newfrom;
238 h->new_lines = hdr.newlines;
239 STAILQ_INSERT_TAIL(&p->head, h, entries);
240 break;
241 case GOT_IMSG_PATCH_LINE:
242 if (h == NULL) {
243 err = got_error(GOT_ERR_PRIVSEP_MSG);
244 goto done;
246 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
247 t = imsg.data;
248 /* at least one char */
249 if (datalen < 2 || t[datalen-1] != '\0') {
250 err = got_error(GOT_ERR_PRIVSEP_MSG);
251 goto done;
253 if (*t != ' ' && *t != '-' && *t != '+' &&
254 *t != '\\') {
255 err = got_error(GOT_ERR_PRIVSEP_MSG);
256 goto done;
259 if (*t != '\\')
260 err = pushline(h, t);
261 else if (lastmode == '-')
262 h->old_nonl = 1;
263 else if (lastmode == '+')
264 h->new_nonl = 1;
265 else
266 err = got_error(GOT_ERR_PATCH_MALFORMED);
268 if (err)
269 goto done;
271 lastmode = *t;
272 break;
273 default:
274 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 goto done;
278 imsg_free(&imsg);
281 done:
282 imsg_free(&imsg);
283 return err;
286 /*
287 * Copy data from orig starting at copypos until pos into tmp.
288 * If pos is -1, copy until EOF.
289 */
290 static const struct got_error *
291 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
293 char buf[BUFSIZ];
294 size_t len, r, w;
296 if (fseek(orig, copypos, SEEK_SET) == -1)
297 return got_error_from_errno("fseek");
299 while (pos == -1 || copypos < pos) {
300 len = sizeof(buf);
301 if (pos > 0)
302 len = MIN(len, (size_t)pos - copypos);
303 r = fread(buf, 1, len, orig);
304 if (r != len && ferror(orig))
305 return got_error_from_errno("fread");
306 w = fwrite(buf, 1, r, tmp);
307 if (w != r)
308 return got_error_from_errno("fwrite");
309 copypos += len;
310 if (r != len && feof(orig)) {
311 if (pos == -1)
312 return NULL;
313 return got_error(GOT_ERR_HUNK_FAILED);
316 return NULL;
319 static const struct got_error *
320 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
322 const struct got_error *err = NULL;
323 char *line = NULL;
324 char mode = *h->lines[0];
325 size_t linesize = 0;
326 ssize_t linelen;
327 off_t match = -1;
328 long match_lineno = -1;
330 for (;;) {
331 linelen = getline(&line, &linesize, orig);
332 if (linelen == -1) {
333 if (ferror(orig))
334 err = got_error_from_errno("getline");
335 else if (match == -1)
336 err = got_error(GOT_ERR_HUNK_FAILED);
337 break;
339 if (line[linelen - 1] == '\n')
340 line[linelen - 1] = '\0';
341 (*lineno)++;
343 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
344 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
345 (mode == '+' && *lineno == h->old_from)) {
346 match = ftello(orig);
347 if (match == -1) {
348 err = got_error_from_errno("ftello");
349 break;
351 match -= linelen;
352 match_lineno = (*lineno)-1;
355 if (*lineno >= h->old_from && match != -1)
356 break;
359 if (err == NULL) {
360 *pos = match;
361 *lineno = match_lineno;
362 if (fseek(orig, match, SEEK_SET) == -1)
363 err = got_error_from_errno("fseek");
366 free(line);
367 return err;
370 static const struct got_error *
371 test_hunk(FILE *orig, struct got_patch_hunk *h)
373 const struct got_error *err = NULL;
374 char *line = NULL;
375 size_t linesize = 0, i = 0;
376 ssize_t linelen;
378 for (i = 0; i < h->len; ++i) {
379 switch (*h->lines[i]) {
380 case '+':
381 continue;
382 case ' ':
383 case '-':
384 linelen = getline(&line, &linesize, orig);
385 if (linelen == -1) {
386 if (ferror(orig))
387 err = got_error_from_errno("getline");
388 else
389 err = got_error(
390 GOT_ERR_HUNK_FAILED);
391 goto done;
393 if (line[linelen - 1] == '\n')
394 line[linelen - 1] = '\0';
395 if (strcmp(h->lines[i] + 1, line)) {
396 err = got_error(GOT_ERR_HUNK_FAILED);
397 goto done;
399 break;
403 done:
404 free(line);
405 return err;
408 static const struct got_error *
409 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
411 size_t i, new = 0;
413 for (i = 0; i < h->len; ++i) {
414 switch (*h->lines[i]) {
415 case ' ':
416 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
417 return got_error_from_errno("fprintf");
418 /* fallthrough */
419 case '-':
420 (*lineno)++;
421 break;
422 case '+':
423 new++;
424 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
425 return got_error_from_errno("fprintf");
426 if (new != h->new_lines || !h->new_nonl) {
427 if (fprintf(tmp, "\n") < 0)
428 return got_error_from_errno(
429 "fprintf");
431 break;
434 return NULL;
437 static const struct got_error *
438 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
439 mode_t *mode)
441 const struct got_error *err = NULL;
442 struct got_patch_hunk *h;
443 struct stat sb;
444 long lineno = 0;
445 FILE *orig;
446 off_t copypos, pos;
447 char *line = NULL;
448 size_t linesize = 0;
449 ssize_t linelen;
451 if (p->old == NULL) { /* create */
452 h = STAILQ_FIRST(&p->head);
453 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
454 return got_error(GOT_ERR_PATCH_MALFORMED);
455 if (nop)
456 return NULL;
457 return apply_hunk(tmp, h, &lineno);
460 if ((orig = fopen(path, "r")) == NULL) {
461 err = got_error_from_errno2("fopen", path);
462 goto done;
465 if (fstat(fileno(orig), &sb) == -1) {
466 err = got_error_from_errno("fstat");
467 goto done;
469 *mode = sb.st_mode;
471 copypos = 0;
472 STAILQ_FOREACH(h, &p->head, entries) {
473 if (h->lines == NULL)
474 break;
476 tryagain:
477 err = locate_hunk(orig, h, &pos, &lineno);
478 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
479 h->err = err;
480 if (err != NULL)
481 goto done;
482 if (!nop)
483 err = copy(tmp, orig, copypos, pos);
484 if (err != NULL)
485 goto done;
486 copypos = pos;
488 err = test_hunk(orig, h);
489 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
490 /*
491 * try to apply the hunk again starting the search
492 * after the previous partial match.
493 */
494 if (fseek(orig, pos, SEEK_SET) == -1) {
495 err = got_error_from_errno("fseek");
496 goto done;
498 linelen = getline(&line, &linesize, orig);
499 if (linelen == -1) {
500 err = got_error_from_errno("getline");
501 goto done;
503 lineno++;
504 goto tryagain;
506 if (err != NULL)
507 goto done;
509 if (lineno + 1 != h->old_from)
510 h->offset = lineno + 1 - h->old_from;
512 if (!nop)
513 err = apply_hunk(tmp, h, &lineno);
514 if (err != NULL)
515 goto done;
517 copypos = ftello(orig);
518 if (copypos == -1) {
519 err = got_error_from_errno("ftello");
520 goto done;
524 if (p->new == NULL && sb.st_size != copypos) {
525 h = STAILQ_FIRST(&p->head);
526 h->err = got_error(GOT_ERR_HUNK_FAILED);
527 err = h->err;
528 } else if (!nop && !feof(orig))
529 err = copy(tmp, orig, copypos, -1);
531 done:
532 if (orig != NULL)
533 fclose(orig);
534 return err;
537 static const struct got_error *
538 report_progress(struct patch_args *pa, const char *old, const char *new,
539 unsigned char status, const struct got_error *orig_error)
541 const struct got_error *err;
542 struct got_patch_hunk *h;
544 err = pa->progress_cb(pa->progress_arg, old, new, status,
545 orig_error, 0, 0, 0, 0, 0, NULL);
546 if (err)
547 return err;
549 STAILQ_FOREACH(h, pa->head, entries) {
550 if (h->offset == 0 && h->err == NULL)
551 continue;
553 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
554 h->old_from, h->old_lines, h->new_from, h->new_lines,
555 h->offset, h->err);
556 if (err)
557 return err;
560 return NULL;
563 static const struct got_error *
564 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
565 const char *path)
567 return report_progress(arg, path, NULL, status, NULL);
570 static const struct got_error *
571 patch_add(void *arg, unsigned char status, const char *path)
573 return report_progress(arg, NULL, path, status, NULL);
576 static const struct got_error *
577 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
578 const char *old, const char *new, struct got_patch *p, int nop,
579 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
581 const struct got_error *err = NULL;
582 struct got_pathlist_head oldpaths, newpaths;
583 struct got_pathlist_entry *pe;
584 int file_renamed = 0;
585 char *oldpath = NULL, *newpath = NULL;
586 char *tmppath = NULL, *template = NULL, *parent = NULL;;
587 FILE *tmp = NULL;
588 mode_t mode = GOT_DEFAULT_FILE_MODE;
590 TAILQ_INIT(&oldpaths);
591 TAILQ_INIT(&newpaths);
593 err = got_pathlist_insert(&pe, &oldpaths, old, NULL);
594 if (err)
595 goto done;
596 err = got_pathlist_insert(&pe, &newpaths, new, NULL);
597 if (err)
598 goto done;
600 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
601 old) == -1) {
602 err = got_error_from_errno("asprintf");
603 goto done;
606 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
607 new) == -1) {
608 err = got_error_from_errno("asprintf");
609 goto done;
612 file_renamed = strcmp(oldpath, newpath);
614 if (asprintf(&template, "%s/got-patch",
615 got_worktree_get_root_path(worktree)) == -1) {
616 err = got_error_from_errno(template);
617 goto done;
620 if (!nop)
621 err = got_opentemp_named(&tmppath, &tmp, template);
622 if (err)
623 goto done;
624 err = patch_file(p, oldpath, tmp, nop, &mode);
625 if (err)
626 goto done;
628 if (nop)
629 goto done;
631 if (p->old != NULL && p->new == NULL) {
632 err = got_worktree_schedule_delete(worktree, &oldpaths,
633 0, NULL, patch_delete, pa, repo, 0, 0);
634 goto done;
637 if (fchmod(fileno(tmp), mode) == -1) {
638 err = got_error_from_errno2("chmod", tmppath);
639 goto done;
642 if (rename(tmppath, newpath) == -1) {
643 if (errno != ENOENT) {
644 err = got_error_from_errno3("rename", tmppath,
645 newpath);
646 goto done;
649 err = got_path_dirname(&parent, newpath);
650 if (err != NULL)
651 goto done;
652 err = got_path_mkdir(parent);
653 if (err != NULL)
654 goto done;
655 if (rename(tmppath, newpath) == -1) {
656 err = got_error_from_errno3("rename", tmppath,
657 newpath);
658 goto done;
662 if (file_renamed) {
663 err = got_worktree_schedule_delete(worktree, &oldpaths,
664 0, NULL, patch_delete, pa, repo, 0, 0);
665 if (err == NULL)
666 err = got_worktree_schedule_add(worktree, &newpaths,
667 patch_add, pa, repo, 1);
668 if (err)
669 unlink(newpath);
670 } else if (p->old == NULL) {
671 err = got_worktree_schedule_add(worktree, &newpaths,
672 patch_add, pa, repo, 1);
673 if (err)
674 unlink(newpath);
675 } else
676 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
678 done:
679 got_pathlist_free(&oldpaths);
680 got_pathlist_free(&newpaths);
681 free(parent);
682 free(template);
683 if (tmppath != NULL)
684 unlink(tmppath);
685 free(tmppath);
686 free(oldpath);
687 free(newpath);
688 return err;
691 static void
692 reverse_patch(struct got_patch *p)
694 struct got_patch_hunk *h;
695 size_t i;
696 long tmp;
698 STAILQ_FOREACH(h, &p->head, entries) {
699 tmp = h->old_from;
700 h->old_from = h->new_from;
701 h->new_from = tmp;
703 tmp = h->old_lines;
704 h->old_lines = h->new_lines;
705 h->new_lines = tmp;
707 tmp = h->old_nonl;
708 h->old_nonl = h->new_nonl;
709 h->new_nonl = tmp;
711 for (i = 0; i < h->len; ++i) {
712 if (*h->lines[i] == '+')
713 *h->lines[i] = '-';
714 else if (*h->lines[i] == '-')
715 *h->lines[i] = '+';
720 const struct got_error *
721 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
722 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
723 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
725 const struct got_error *err = NULL;
726 struct got_fileindex *fileindex = NULL;
727 char *oldpath, *newpath;
728 struct imsgbuf *ibuf;
729 int imsg_fds[2] = {-1, -1};
730 int done = 0, failed = 0;
731 pid_t pid;
733 ibuf = calloc(1, sizeof(*ibuf));
734 if (ibuf == NULL) {
735 err = got_error_from_errno("calloc");
736 goto done;
739 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
740 err = got_error_from_errno("socketpair");
741 goto done;
744 pid = fork();
745 if (pid == -1) {
746 err = got_error_from_errno("fork");
747 goto done;
748 } else if (pid == 0) {
749 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
750 NULL);
751 /* not reached */
754 if (close(imsg_fds[1]) == -1) {
755 err = got_error_from_errno("close");
756 goto done;
758 imsg_fds[1] = -1;
759 imsg_init(ibuf, imsg_fds[0]);
761 err = send_patch(ibuf, fd);
762 fd = -1;
763 if (err)
764 goto done;
766 err = got_worktree_patch_prepare(&fileindex, worktree);
767 if (err)
768 goto done;
770 while (!done && err == NULL) {
771 struct got_patch p;
772 struct patch_args pa;
774 pa.progress_cb = progress_cb;
775 pa.progress_arg = progress_arg;
776 pa.head = &p.head;
778 err = recv_patch(ibuf, &done, &p, strip);
779 if (err || done)
780 break;
782 if (reverse)
783 reverse_patch(&p);
785 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
786 &newpath, worktree, repo, fileindex);
787 if (err == NULL)
788 err = apply_patch(worktree, repo, oldpath, newpath,
789 &p, nop, &pa, cancel_cb, cancel_arg);
790 if (err != NULL) {
791 failed = 1;
792 /* recoverable errors */
793 if (err->code == GOT_ERR_FILE_STATUS ||
794 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
795 err = report_progress(&pa, p.old, p.new,
796 GOT_STATUS_CANNOT_UPDATE, err);
797 else if (err->code == GOT_ERR_HUNK_FAILED)
798 err = report_progress(&pa, p.old, p.new,
799 GOT_STATUS_CANNOT_UPDATE, NULL);
802 free(oldpath);
803 free(newpath);
804 patch_free(&p);
806 if (err)
807 break;
810 done:
811 if (fileindex)
812 got_worktree_patch_complete(fileindex);
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;