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 long offset;
55 int old_nonl;
56 int new_nonl;
57 long old_from;
58 long old_lines;
59 long new_from;
60 long 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);
120 static const struct got_error *
121 pushline(struct got_patch_hunk *h, const char *line)
123 void *t;
124 size_t newcap;
126 if (h->len == h->cap) {
127 if ((newcap = h->cap * 1.5) == 0)
128 newcap = 16;
129 t = recallocarray(h->lines, h->cap, newcap,
130 sizeof(h->lines[0]));
131 if (t == NULL)
132 return got_error_from_errno("recallocarray");
133 h->lines = t;
134 h->cap = newcap;
137 if ((t = strdup(line)) == NULL)
138 return got_error_from_errno("strdup");
140 h->lines[h->len++] = t;
141 return NULL;
144 static const struct got_error *
145 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
147 const struct got_error *err = NULL;
148 struct imsg imsg;
149 struct got_imsg_patch_hunk hdr;
150 struct got_imsg_patch patch;
151 struct got_patch_hunk *h = NULL;
152 size_t datalen;
153 int lastmode = -1;
155 memset(p, 0, sizeof(*p));
156 STAILQ_INIT(&p->head);
158 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
159 if (err)
160 return err;
161 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
162 *done = 1;
163 goto done;
165 if (imsg.hdr.type != GOT_IMSG_PATCH) {
166 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 goto done;
169 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
170 if (datalen != sizeof(patch)) {
171 err = got_error(GOT_ERR_PRIVSEP_LEN);
172 goto done;
174 memcpy(&patch, imsg.data, sizeof(patch));
176 if (patch.old[sizeof(patch.old)-1] != '\0' ||
177 patch.new[sizeof(patch.new)-1] != '\0') {
178 err = got_error(GOT_ERR_PRIVSEP_LEN);
179 goto done;
182 /* automatically set strip=1 for git-style diffs */
183 if (strip == -1 && patch.git &&
184 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
185 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
186 strip = 1;
188 /* prefer the new name if not /dev/null for not git-style diffs */
189 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
190 err = got_path_strip(&p->old, patch.new, strip);
191 if (err)
192 goto done;
193 } else if (*patch.old != '\0') {
194 err = got_path_strip(&p->old, patch.old, strip);
195 if (err)
196 goto done;
199 if (*patch.new != '\0') {
200 err = got_path_strip(&p->new, patch.new, strip);
201 if (err)
202 goto done;
205 if (p->old == NULL && p->new == NULL) {
206 err = got_error(GOT_ERR_PATCH_MALFORMED);
207 goto done;
210 imsg_free(&imsg);
212 for (;;) {
213 char *t;
215 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
216 if (err)
217 return err;
219 switch (imsg.hdr.type) {
220 case GOT_IMSG_PATCH_DONE:
221 if (h != NULL && h->len == 0)
222 err = got_error(GOT_ERR_PATCH_MALFORMED);
223 goto done;
224 case GOT_IMSG_PATCH_HUNK:
225 if (h != NULL &&
226 (h->len == 0 || h->old_nonl || h->new_nonl)) {
227 err = got_error(GOT_ERR_PATCH_MALFORMED);
228 goto done;
230 lastmode = -1;
231 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
232 if (datalen != sizeof(hdr)) {
233 err = got_error(GOT_ERR_PRIVSEP_LEN);
234 goto done;
236 memcpy(&hdr, imsg.data, sizeof(hdr));
237 if ((h = calloc(1, sizeof(*h))) == NULL) {
238 err = got_error_from_errno("calloc");
239 goto done;
241 h->old_from = hdr.oldfrom;
242 h->old_lines = hdr.oldlines;
243 h->new_from = hdr.newfrom;
244 h->new_lines = hdr.newlines;
245 STAILQ_INSERT_TAIL(&p->head, h, entries);
246 break;
247 case GOT_IMSG_PATCH_LINE:
248 if (h == NULL) {
249 err = got_error(GOT_ERR_PRIVSEP_MSG);
250 goto done;
252 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
253 t = imsg.data;
254 /* at least one char */
255 if (datalen < 2 || t[datalen-1] != '\0') {
256 err = got_error(GOT_ERR_PRIVSEP_MSG);
257 goto done;
259 if (*t != ' ' && *t != '-' && *t != '+' &&
260 *t != '\\') {
261 err = got_error(GOT_ERR_PRIVSEP_MSG);
262 goto done;
265 if (*t != '\\')
266 err = pushline(h, t);
267 else if (lastmode == '-')
268 h->old_nonl = 1;
269 else if (lastmode == '+')
270 h->new_nonl = 1;
271 else
272 err = got_error(GOT_ERR_PATCH_MALFORMED);
274 if (err)
275 goto done;
277 lastmode = *t;
278 break;
279 default:
280 err = got_error(GOT_ERR_PRIVSEP_MSG);
281 goto done;
284 imsg_free(&imsg);
287 done:
288 imsg_free(&imsg);
289 return err;
292 /*
293 * Copy data from orig starting at copypos until pos into tmp.
294 * If pos is -1, copy until EOF.
295 */
296 static const struct got_error *
297 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
299 char buf[BUFSIZ];
300 size_t len, r, w;
302 if (fseeko(orig, copypos, SEEK_SET) == -1)
303 return got_error_from_errno("fseeko");
305 while (pos == -1 || copypos < pos) {
306 len = sizeof(buf);
307 if (pos > 0)
308 len = MIN(len, (size_t)pos - copypos);
309 r = fread(buf, 1, len, orig);
310 if (r != len && ferror(orig))
311 return got_error_from_errno("fread");
312 w = fwrite(buf, 1, r, tmp);
313 if (w != r)
314 return got_error_from_errno("fwrite");
315 copypos += len;
316 if (r != len && feof(orig)) {
317 if (pos == -1)
318 return NULL;
319 return got_error(GOT_ERR_HUNK_FAILED);
322 return NULL;
325 static const struct got_error *
326 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
328 const struct got_error *err = NULL;
329 char *line = NULL;
330 char mode = *h->lines[0];
331 size_t linesize = 0;
332 ssize_t linelen;
333 off_t match = -1;
334 long match_lineno = -1;
336 for (;;) {
337 linelen = getline(&line, &linesize, orig);
338 if (linelen == -1) {
339 if (ferror(orig))
340 err = got_error_from_errno("getline");
341 else if (match == -1)
342 err = got_error(GOT_ERR_HUNK_FAILED);
343 break;
345 if (line[linelen - 1] == '\n')
346 line[linelen - 1] = '\0';
347 (*lineno)++;
349 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
350 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
351 (mode == '+' && *lineno == h->old_from)) {
352 match = ftello(orig);
353 if (match == -1) {
354 err = got_error_from_errno("ftello");
355 break;
357 match -= linelen;
358 match_lineno = (*lineno)-1;
361 if (*lineno >= h->old_from && match != -1)
362 break;
365 if (err == NULL) {
366 *pos = match;
367 *lineno = match_lineno;
368 if (fseeko(orig, match, SEEK_SET) == -1)
369 err = got_error_from_errno("fseeko");
372 free(line);
373 return err;
376 static const struct got_error *
377 test_hunk(FILE *orig, struct got_patch_hunk *h)
379 const struct got_error *err = NULL;
380 char *line = NULL;
381 size_t linesize = 0, i = 0;
382 ssize_t linelen;
384 for (i = 0; i < h->len; ++i) {
385 switch (*h->lines[i]) {
386 case '+':
387 continue;
388 case ' ':
389 case '-':
390 linelen = getline(&line, &linesize, orig);
391 if (linelen == -1) {
392 if (ferror(orig))
393 err = got_error_from_errno("getline");
394 else
395 err = got_error(
396 GOT_ERR_HUNK_FAILED);
397 goto done;
399 if (line[linelen - 1] == '\n')
400 line[linelen - 1] = '\0';
401 if (strcmp(h->lines[i] + 1, line)) {
402 err = got_error(GOT_ERR_HUNK_FAILED);
403 goto done;
405 break;
409 done:
410 free(line);
411 return err;
414 static const struct got_error *
415 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
417 size_t i, new = 0;
419 for (i = 0; i < h->len; ++i) {
420 switch (*h->lines[i]) {
421 case ' ':
422 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
423 return got_error_from_errno("fprintf");
424 /* fallthrough */
425 case '-':
426 (*lineno)++;
427 break;
428 case '+':
429 new++;
430 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
431 return got_error_from_errno("fprintf");
432 if (new != h->new_lines || !h->new_nonl) {
433 if (fprintf(tmp, "\n") < 0)
434 return got_error_from_errno(
435 "fprintf");
437 break;
440 return NULL;
443 static const struct got_error *
444 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
445 mode_t *mode)
447 const struct got_error *err = NULL;
448 struct got_patch_hunk *h;
449 struct stat sb;
450 long lineno = 0;
451 FILE *orig;
452 off_t copypos, pos;
453 char *line = NULL;
454 size_t linesize = 0;
455 ssize_t linelen;
457 if (p->old == NULL) { /* create */
458 h = STAILQ_FIRST(&p->head);
459 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
460 return got_error(GOT_ERR_PATCH_MALFORMED);
461 if (nop)
462 return NULL;
463 return apply_hunk(tmp, h, &lineno);
466 if ((orig = fopen(path, "r")) == NULL) {
467 err = got_error_from_errno2("fopen", path);
468 goto done;
471 if (fstat(fileno(orig), &sb) == -1) {
472 err = got_error_from_errno("fstat");
473 goto done;
475 *mode = sb.st_mode;
477 copypos = 0;
478 STAILQ_FOREACH(h, &p->head, entries) {
479 tryagain:
480 err = locate_hunk(orig, h, &pos, &lineno);
481 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
482 h->err = err;
483 if (err != NULL)
484 goto done;
485 if (!nop)
486 err = copy(tmp, orig, copypos, pos);
487 if (err != NULL)
488 goto done;
489 copypos = pos;
491 err = test_hunk(orig, h);
492 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
493 /*
494 * try to apply the hunk again starting the search
495 * after the previous partial match.
496 */
497 if (fseeko(orig, pos, SEEK_SET) == -1) {
498 err = got_error_from_errno("fseeko");
499 goto done;
501 linelen = getline(&line, &linesize, orig);
502 if (linelen == -1) {
503 err = got_error_from_errno("getline");
504 goto done;
506 lineno++;
507 goto tryagain;
509 if (err != NULL)
510 goto done;
512 if (lineno + 1 != h->old_from)
513 h->offset = lineno + 1 - h->old_from;
515 if (!nop)
516 err = apply_hunk(tmp, h, &lineno);
517 if (err != NULL)
518 goto done;
520 copypos = ftello(orig);
521 if (copypos == -1) {
522 err = got_error_from_errno("ftello");
523 goto done;
527 if (p->new == NULL && sb.st_size != copypos) {
528 h = STAILQ_FIRST(&p->head);
529 h->err = got_error(GOT_ERR_HUNK_FAILED);
530 err = h->err;
531 } else if (!nop && !feof(orig))
532 err = copy(tmp, orig, copypos, -1);
534 done:
535 if (orig != NULL)
536 fclose(orig);
537 return err;
540 static const struct got_error *
541 report_progress(struct patch_args *pa, const char *old, const char *new,
542 unsigned char status, const struct got_error *orig_error)
544 const struct got_error *err;
545 struct got_patch_hunk *h;
547 err = pa->progress_cb(pa->progress_arg, old, new, status,
548 orig_error, 0, 0, 0, 0, 0, NULL);
549 if (err)
550 return err;
552 STAILQ_FOREACH(h, pa->head, entries) {
553 if (h->offset == 0 && h->err == NULL)
554 continue;
556 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
557 h->old_from, h->old_lines, h->new_from, h->new_lines,
558 h->offset, h->err);
559 if (err)
560 return err;
563 return NULL;
566 static const struct got_error *
567 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
568 const char *path)
570 return report_progress(arg, path, NULL, status, NULL);
573 static const struct got_error *
574 patch_add(void *arg, unsigned char status, const char *path)
576 return report_progress(arg, NULL, path, status, NULL);
579 static const struct got_error *
580 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
581 struct got_fileindex *fileindex, const char *old, const char *new,
582 struct got_patch *p, int nop, struct patch_args *pa,
583 got_cancel_cb cancel_cb, void *cancel_arg)
585 const struct got_error *err = NULL;
586 int file_renamed = 0;
587 char *oldpath = NULL, *newpath = NULL;
588 char *tmppath = NULL, *template = NULL, *parent = NULL;;
589 FILE *tmp = NULL;
590 mode_t mode = GOT_DEFAULT_FILE_MODE;
592 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
593 old) == -1) {
594 err = got_error_from_errno("asprintf");
595 goto done;
598 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
599 new) == -1) {
600 err = got_error_from_errno("asprintf");
601 goto done;
604 file_renamed = strcmp(oldpath, newpath);
606 if (asprintf(&template, "%s/got-patch",
607 got_worktree_get_root_path(worktree)) == -1) {
608 err = got_error_from_errno(template);
609 goto done;
612 if (!nop)
613 err = got_opentemp_named(&tmppath, &tmp, template);
614 if (err)
615 goto done;
616 err = patch_file(p, oldpath, tmp, nop, &mode);
617 if (err)
618 goto done;
620 if (nop)
621 goto done;
623 if (p->old != NULL && p->new == NULL) {
624 err = got_worktree_patch_schedule_rm(old, repo, worktree,
625 fileindex, patch_delete, pa);
626 goto done;
629 if (fchmod(fileno(tmp), mode) == -1) {
630 err = got_error_from_errno2("chmod", tmppath);
631 goto done;
634 if (rename(tmppath, newpath) == -1) {
635 if (errno != ENOENT) {
636 err = got_error_from_errno3("rename", tmppath,
637 newpath);
638 goto done;
641 err = got_path_dirname(&parent, newpath);
642 if (err != NULL)
643 goto done;
644 err = got_path_mkdir(parent);
645 if (err != NULL)
646 goto done;
647 if (rename(tmppath, newpath) == -1) {
648 err = got_error_from_errno3("rename", tmppath,
649 newpath);
650 goto done;
654 if (file_renamed) {
655 err = got_worktree_patch_schedule_rm(old, repo, worktree,
656 fileindex, patch_delete, pa);
657 if (err == NULL)
658 err = got_worktree_patch_schedule_add(new, repo,
659 worktree, fileindex, patch_add,
660 pa);
661 if (err)
662 unlink(newpath);
663 } else if (p->old == NULL) {
664 err = got_worktree_patch_schedule_add(new, repo, worktree,
665 fileindex, patch_add, pa);
666 if (err)
667 unlink(newpath);
668 } else
669 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
671 done:
672 free(parent);
673 free(template);
674 if (tmppath != NULL)
675 unlink(tmppath);
676 free(tmppath);
677 free(oldpath);
678 free(newpath);
679 return err;
682 static void
683 reverse_patch(struct got_patch *p)
685 struct got_patch_hunk *h;
686 size_t i;
687 long tmp;
689 STAILQ_FOREACH(h, &p->head, entries) {
690 tmp = h->old_from;
691 h->old_from = h->new_from;
692 h->new_from = tmp;
694 tmp = h->old_lines;
695 h->old_lines = h->new_lines;
696 h->new_lines = tmp;
698 tmp = h->old_nonl;
699 h->old_nonl = h->new_nonl;
700 h->new_nonl = tmp;
702 for (i = 0; i < h->len; ++i) {
703 if (*h->lines[i] == '+')
704 *h->lines[i] = '-';
705 else if (*h->lines[i] == '-')
706 *h->lines[i] = '+';
711 const struct got_error *
712 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
713 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
714 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
716 const struct got_error *err = NULL, *complete_err = NULL;
717 struct got_fileindex *fileindex = NULL;
718 char *fileindex_path = NULL;
719 char *oldpath, *newpath;
720 struct imsgbuf *ibuf;
721 int imsg_fds[2] = {-1, -1};
722 int done = 0, failed = 0;
723 pid_t pid;
725 ibuf = calloc(1, sizeof(*ibuf));
726 if (ibuf == NULL) {
727 err = got_error_from_errno("calloc");
728 goto done;
731 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
732 err = got_error_from_errno("socketpair");
733 goto done;
736 pid = fork();
737 if (pid == -1) {
738 err = got_error_from_errno("fork");
739 goto done;
740 } else if (pid == 0) {
741 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
742 NULL);
743 /* not reached */
746 if (close(imsg_fds[1]) == -1) {
747 err = got_error_from_errno("close");
748 goto done;
750 imsg_fds[1] = -1;
751 imsg_init(ibuf, imsg_fds[0]);
753 err = send_patch(ibuf, fd);
754 fd = -1;
755 if (err)
756 goto done;
758 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
759 worktree);
760 if (err)
761 goto done;
763 while (!done && err == NULL) {
764 struct got_patch p;
765 struct patch_args pa;
767 pa.progress_cb = progress_cb;
768 pa.progress_arg = progress_arg;
769 pa.head = &p.head;
771 err = recv_patch(ibuf, &done, &p, strip);
772 if (err || done)
773 break;
775 if (reverse)
776 reverse_patch(&p);
778 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
779 &newpath, worktree, repo, fileindex);
780 if (err == NULL)
781 err = apply_patch(worktree, repo, fileindex, oldpath,
782 newpath, &p, nop, &pa, cancel_cb, cancel_arg);
783 if (err != NULL) {
784 failed = 1;
785 /* recoverable errors */
786 if (err->code == GOT_ERR_FILE_STATUS ||
787 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
788 err = report_progress(&pa, p.old, p.new,
789 GOT_STATUS_CANNOT_UPDATE, err);
790 else if (err->code == GOT_ERR_HUNK_FAILED)
791 err = report_progress(&pa, p.old, p.new,
792 GOT_STATUS_CANNOT_UPDATE, NULL);
795 free(oldpath);
796 free(newpath);
797 patch_free(&p);
799 if (err)
800 break;
803 done:
804 if (fileindex != NULL)
805 complete_err = got_worktree_patch_complete(fileindex,
806 fileindex_path);
807 if (complete_err && err == NULL)
808 err = complete_err;
809 free(fileindex_path);
810 if (fd != -1 && close(fd) == -1 && err == NULL)
811 err = got_error_from_errno("close");
812 if (ibuf != NULL)
813 imsg_clear(ibuf);
814 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
815 err = got_error_from_errno("close");
816 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
817 err = got_error_from_errno("close");
818 if (err == NULL && failed)
819 err = got_error(GOT_ERR_PATCH_FAILED);
820 return err;