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 are still missing:
19 * + "No final newline" handling
20 *
21 * Things that we may want to support:
22 * + support indented patches?
23 * + support other kinds of patches?
24 */
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <sha1.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <imsg.h>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_path.h"
45 #include "got_reference.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_opentemp.h"
49 #include "got_patch.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_privsep.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;
60 long offset;
61 long old_from;
62 long old_lines;
63 long new_from;
64 long new_lines;
65 size_t len;
66 size_t cap;
67 char **lines;
68 };
70 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
71 struct got_patch {
72 char *old;
73 char *new;
74 struct got_patch_hunk_head head;
75 };
77 struct patch_args {
78 got_patch_progress_cb progress_cb;
79 void *progress_arg;
80 struct got_patch_hunk_head *head;
81 };
83 static const struct got_error *
84 send_patch(struct imsgbuf *ibuf, int fd)
85 {
86 const struct got_error *err = NULL;
88 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
89 NULL, 0) == -1) {
90 err = got_error_from_errno(
91 "imsg_compose GOT_IMSG_PATCH_FILE");
92 close(fd);
93 return err;
94 }
96 if (imsg_flush(ibuf) == -1) {
97 err = got_error_from_errno("imsg_flush");
98 imsg_clear(ibuf);
99 }
101 return err;
104 static void
105 patch_free(struct got_patch *p)
107 struct got_patch_hunk *h;
108 size_t i;
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)
115 free(h->lines[i]);
116 free(h->lines);
117 free(h);
120 free(p->new);
121 free(p->old);
124 static const struct got_error *
125 pushline(struct got_patch_hunk *h, const char *line)
127 void *t;
128 size_t newcap;
130 if (h->len == h->cap) {
131 if ((newcap = h->cap * 1.5) == 0)
132 newcap = 16;
133 t = recallocarray(h->lines, h->cap, newcap,
134 sizeof(h->lines[0]));
135 if (t == NULL)
136 return got_error_from_errno("recallocarray");
137 h->lines = t;
138 h->cap = newcap;
141 if ((t = strdup(line)) == NULL)
142 return got_error_from_errno("strdup");
144 h->lines[h->len++] = t;
145 return NULL;
148 static const struct got_error *
149 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
151 const struct got_error *err = NULL;
152 struct imsg imsg;
153 struct got_imsg_patch_hunk hdr;
154 struct got_imsg_patch patch;
155 struct got_patch_hunk *h = NULL;
156 size_t datalen;
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));
178 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
179 err = got_error_from_errno("strdup");
180 goto done;
182 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
183 err = got_error_from_errno("strdup");
184 goto done;
186 if (p->old == NULL && p->new == NULL) {
187 err = got_error(GOT_ERR_PATCH_MALFORMED);
188 goto done;
191 imsg_free(&imsg);
193 for (;;) {
194 char *t;
196 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
197 if (err)
198 return err;
200 switch (imsg.hdr.type) {
201 case GOT_IMSG_PATCH_DONE:
202 goto done;
203 case GOT_IMSG_PATCH_HUNK:
204 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
205 if (datalen != sizeof(hdr)) {
206 err = got_error(GOT_ERR_PRIVSEP_LEN);
207 goto done;
209 memcpy(&hdr, imsg.data, sizeof(hdr));
210 if ((h = calloc(1, sizeof(*h))) == NULL) {
211 err = got_error_from_errno("calloc");
212 goto done;
214 h->old_from = hdr.oldfrom;
215 h->old_lines = hdr.oldlines;
216 h->new_from = hdr.newfrom;
217 h->new_lines = hdr.newlines;
218 STAILQ_INSERT_TAIL(&p->head, h, entries);
219 break;
220 case GOT_IMSG_PATCH_LINE:
221 if (h == NULL) {
222 err = got_error(GOT_ERR_PRIVSEP_MSG);
223 goto done;
225 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
226 t = imsg.data;
227 /* at least one char plus newline */
228 if (datalen < 2 || t[datalen-1] != '\0') {
229 err = got_error(GOT_ERR_PRIVSEP_MSG);
230 goto done;
232 if (*t != ' ' && *t != '-' && *t != '+') {
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
234 goto done;
236 err = pushline(h, t);
237 if (err)
238 goto done;
239 break;
240 default:
241 err = got_error(GOT_ERR_PRIVSEP_MSG);
242 goto done;
245 imsg_free(&imsg);
248 done:
249 imsg_free(&imsg);
250 return err;
253 /*
254 * Copy data from orig starting at copypos until pos into tmp.
255 * If pos is -1, copy until EOF.
256 */
257 static const struct got_error *
258 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
260 char buf[BUFSIZ];
261 size_t len, r, w;
263 if (fseek(orig, copypos, SEEK_SET) == -1)
264 return got_error_from_errno("fseek");
266 while (pos == -1 || copypos < pos) {
267 len = sizeof(buf);
268 if (pos > 0)
269 len = MIN(len, (size_t)pos - copypos);
270 r = fread(buf, 1, len, orig);
271 if (r != len && ferror(orig))
272 return got_error_from_errno("fread");
273 w = fwrite(buf, 1, r, tmp);
274 if (w != r)
275 return got_error_from_errno("fwrite");
276 copypos += len;
277 if (r != len && feof(orig)) {
278 if (pos == -1)
279 return NULL;
280 return got_error(GOT_ERR_HUNK_FAILED);
283 return NULL;
286 static const struct got_error *
287 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
289 const struct got_error *err = NULL;
290 char *line = NULL;
291 char mode = *h->lines[0];
292 size_t linesize = 0;
293 ssize_t linelen;
294 off_t match = -1;
295 long match_lineno = -1;
297 for (;;) {
298 linelen = getline(&line, &linesize, orig);
299 if (linelen == -1) {
300 if (ferror(orig))
301 err = got_error_from_errno("getline");
302 else if (match == -1)
303 err = got_error(GOT_ERR_HUNK_FAILED);
304 break;
306 (*lineno)++;
308 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
309 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
310 (mode == '+' && *lineno == h->old_from)) {
311 match = ftello(orig);
312 if (match == -1) {
313 err = got_error_from_errno("ftello");
314 break;
316 match -= linelen;
317 match_lineno = (*lineno)-1;
320 if (*lineno >= h->old_from && match != -1)
321 break;
324 if (err == NULL) {
325 *pos = match;
326 *lineno = match_lineno;
327 if (fseek(orig, match, SEEK_SET) == -1)
328 err = got_error_from_errno("fseek");
331 free(line);
332 return err;
335 static const struct got_error *
336 test_hunk(FILE *orig, struct got_patch_hunk *h)
338 const struct got_error *err = NULL;
339 char *line = NULL;
340 size_t linesize = 0, i = 0;
341 ssize_t linelen;
343 for (i = 0; i < h->len; ++i) {
344 switch (*h->lines[i]) {
345 case '+':
346 continue;
347 case ' ':
348 case '-':
349 linelen = getline(&line, &linesize, orig);
350 if (linelen == -1) {
351 if (ferror(orig))
352 err = got_error_from_errno("getline");
353 else
354 err = got_error(
355 GOT_ERR_HUNK_FAILED);
356 goto done;
358 if (strcmp(h->lines[i] + 1, line)) {
359 err = got_error(GOT_ERR_HUNK_FAILED);
360 goto done;
362 break;
366 done:
367 free(line);
368 return err;
371 static const struct got_error *
372 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
374 size_t i = 0;
376 for (i = 0; i < h->len; ++i) {
377 switch (*h->lines[i]) {
378 case ' ':
379 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
380 return got_error_from_errno("fprintf");
381 /* fallthrough */
382 case '-':
383 (*lineno)++;
384 break;
385 case '+':
386 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
387 return got_error_from_errno("fprintf");
388 break;
391 return NULL;
394 static const struct got_error *
395 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
396 mode_t *mode)
398 const struct got_error *err = NULL;
399 struct got_patch_hunk *h;
400 struct stat sb;
401 size_t i;
402 long lineno = 0;
403 FILE *orig;
404 off_t copypos, pos;
405 char *line = NULL;
406 size_t linesize = 0;
407 ssize_t linelen;
409 if (p->old == NULL) { /* create */
410 h = STAILQ_FIRST(&p->head);
411 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
412 return got_error(GOT_ERR_PATCH_MALFORMED);
413 if (nop)
414 return NULL;
415 for (i = 0; i < h->len; ++i) {
416 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
417 return got_error_from_errno("fprintf");
419 return err;
422 if ((orig = fopen(path, "r")) == NULL) {
423 err = got_error_from_errno2("fopen", path);
424 goto done;
427 if (fstat(fileno(orig), &sb) == -1) {
428 err = got_error_from_errno("fstat");
429 goto done;
431 *mode = sb.st_mode;
433 copypos = 0;
434 STAILQ_FOREACH(h, &p->head, entries) {
435 if (h->lines == NULL)
436 break;
438 tryagain:
439 err = locate_hunk(orig, h, &pos, &lineno);
440 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
441 h->err = err;
442 if (err != NULL)
443 goto done;
444 if (!nop)
445 err = copy(tmp, orig, copypos, pos);
446 if (err != NULL)
447 goto done;
448 copypos = pos;
450 err = test_hunk(orig, h);
451 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
452 /*
453 * try to apply the hunk again starting the search
454 * after the previous partial match.
455 */
456 if (fseek(orig, pos, SEEK_SET) == -1) {
457 err = got_error_from_errno("fseek");
458 goto done;
460 linelen = getline(&line, &linesize, orig);
461 if (linelen == -1) {
462 err = got_error_from_errno("getline");
463 goto done;
465 lineno++;
466 goto tryagain;
468 if (err != NULL)
469 goto done;
471 if (lineno + 1 != h->old_from)
472 h->offset = lineno + 1 - h->old_from;
474 if (!nop)
475 err = apply_hunk(tmp, h, &lineno);
476 if (err != NULL)
477 goto done;
479 copypos = ftello(orig);
480 if (copypos == -1) {
481 err = got_error_from_errno("ftello");
482 goto done;
486 if (p->new == NULL && sb.st_size != copypos) {
487 h = STAILQ_FIRST(&p->head);
488 h->err = got_error(GOT_ERR_HUNK_FAILED);
489 err = h->err;
490 } else if (!nop && !feof(orig))
491 err = copy(tmp, orig, copypos, -1);
493 done:
494 if (orig != NULL)
495 fclose(orig);
496 return err;
499 static const struct got_error *
500 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
501 struct got_worktree *worktree)
503 const struct got_error *err;
504 struct got_pathlist_entry *pe;
506 err = got_worktree_resolve_path(path, worktree, p);
507 if (err == NULL)
508 err = got_pathlist_insert(&pe, head, *path, NULL);
509 return err;
512 static const struct got_error *
513 can_rm(void *arg, unsigned char status, unsigned char staged_status,
514 const char *path, struct got_object_id *blob_id,
515 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
516 int dirfd, const char *de_name)
518 if (status == GOT_STATUS_NONEXISTENT)
519 return got_error_set_errno(ENOENT, path);
520 if (status != GOT_STATUS_NO_CHANGE &&
521 status != GOT_STATUS_ADD &&
522 status != GOT_STATUS_MODIFY &&
523 status != GOT_STATUS_MODE_CHANGE)
524 return got_error_path(path, GOT_ERR_FILE_STATUS);
525 if (staged_status == GOT_STATUS_DELETE)
526 return got_error_path(path, GOT_ERR_FILE_STATUS);
527 return NULL;
530 static const struct got_error *
531 can_add(void *arg, unsigned char status, unsigned char staged_status,
532 const char *path, struct got_object_id *blob_id,
533 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
534 int dirfd, const char *de_name)
536 if (status != GOT_STATUS_NONEXISTENT)
537 return got_error_path(path, GOT_ERR_FILE_STATUS);
538 return NULL;
541 static const struct got_error *
542 can_edit(void *arg, unsigned char status, unsigned char staged_status,
543 const char *path, struct got_object_id *blob_id,
544 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
545 int dirfd, const char *de_name)
547 if (status == GOT_STATUS_NONEXISTENT)
548 return got_error_set_errno(ENOENT, path);
549 if (status != GOT_STATUS_NO_CHANGE &&
550 status != GOT_STATUS_ADD &&
551 status != GOT_STATUS_MODIFY)
552 return got_error_path(path, GOT_ERR_FILE_STATUS);
553 if (staged_status == GOT_STATUS_DELETE)
554 return got_error_path(path, GOT_ERR_FILE_STATUS);
555 return NULL;
558 static const struct got_error *
559 check_file_status(struct got_patch *p, int file_renamed,
560 struct got_worktree *worktree, struct got_repository *repo,
561 struct got_pathlist_head *old, struct got_pathlist_head *new,
562 got_cancel_cb cancel_cb, void *cancel_arg)
564 static const struct got_error *err;
566 if (p->old != NULL && p->new == NULL)
567 return got_worktree_status(worktree, old, repo, 0,
568 can_rm, NULL, cancel_cb, cancel_arg);
569 else if (file_renamed) {
570 err = got_worktree_status(worktree, old, repo, 0,
571 can_rm, NULL, cancel_cb, cancel_arg);
572 if (err)
573 return err;
574 return got_worktree_status(worktree, new, repo, 0,
575 can_add, NULL, cancel_cb, cancel_arg);
576 } else if (p->old == NULL)
577 return got_worktree_status(worktree, new, repo, 0,
578 can_add, NULL, cancel_cb, cancel_arg);
579 else
580 return got_worktree_status(worktree, new, repo, 0,
581 can_edit, NULL, cancel_cb, cancel_arg);
584 static const struct got_error *
585 report_progress(struct patch_args *pa, const char *old, const char *new,
586 unsigned char status, const struct got_error *orig_error)
588 const struct got_error *err;
589 struct got_patch_hunk *h;
591 err = pa->progress_cb(pa->progress_arg, old, new, status,
592 orig_error, 0, 0, 0, 0, 0, NULL);
593 if (err)
594 return err;
596 STAILQ_FOREACH(h, pa->head, entries) {
597 if (h->offset == 0 && h->err == NULL)
598 continue;
600 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
601 h->old_from, h->old_lines, h->new_from, h->new_lines,
602 h->offset, h->err);
603 if (err)
604 return err;
607 return NULL;
610 static const struct got_error *
611 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
612 const char *path)
614 return report_progress(arg, path, NULL, status, NULL);
617 static const struct got_error *
618 patch_add(void *arg, unsigned char status, const char *path)
620 return report_progress(arg, NULL, path, status, NULL);
623 static const struct got_error *
624 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
625 struct got_pathlist_head *oldpaths, struct got_pathlist_head *newpaths,
626 const char *oldpath, const char *newpath, struct got_patch *p,
627 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
629 const struct got_error *err = NULL;
630 int file_renamed = 0;
631 char *tmppath = NULL, *template = NULL, *parent = NULL;;
632 FILE *tmp = NULL;
633 mode_t mode = GOT_DEFAULT_FILE_MODE;
635 file_renamed = strcmp(oldpath, newpath);
637 err = check_file_status(p, file_renamed, worktree, repo, oldpaths,
638 newpaths, cancel_cb, cancel_arg);
639 if (err)
640 goto done;
642 if (asprintf(&template, "%s/got-patch",
643 got_worktree_get_root_path(worktree)) == -1) {
644 err = got_error_from_errno(template);
645 goto done;
648 if (!nop)
649 err = got_opentemp_named(&tmppath, &tmp, template);
650 if (err)
651 goto done;
652 err = patch_file(p, oldpath, tmp, nop, &mode);
653 if (err)
654 goto done;
656 if (nop)
657 goto done;
659 if (p->old != NULL && p->new == NULL) {
660 err = got_worktree_schedule_delete(worktree, oldpaths,
661 0, NULL, patch_delete, pa, repo, 0, 0);
662 goto done;
665 if (fchmod(fileno(tmp), mode) == -1) {
666 err = got_error_from_errno2("chmod", newpath);
667 goto done;
670 if (rename(tmppath, newpath) == -1) {
671 if (errno != ENOENT) {
672 err = got_error_from_errno3("rename", tmppath,
673 newpath);
674 goto done;
677 err = got_path_dirname(&parent, newpath);
678 if (err != NULL)
679 goto done;
680 err = got_path_mkdir(parent);
681 if (err != NULL)
682 goto done;
683 if (rename(tmppath, newpath) == -1) {
684 err = got_error_from_errno3("rename", tmppath,
685 newpath);
686 goto done;
690 if (file_renamed) {
691 err = got_worktree_schedule_delete(worktree, oldpaths,
692 0, NULL, patch_delete, pa, repo, 0, 0);
693 if (err == NULL)
694 err = got_worktree_schedule_add(worktree, newpaths,
695 patch_add, pa, repo, 1);
696 } else if (p->old == NULL)
697 err = got_worktree_schedule_add(worktree, newpaths,
698 patch_add, pa, repo, 1);
699 else
700 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
701 NULL);
703 done:
704 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
705 unlink(newpath);
706 free(parent);
707 free(template);
708 if (tmppath != NULL)
709 unlink(tmppath);
710 free(tmppath);
711 return err;
714 static const struct got_error *
715 resolve_paths(struct got_patch *p, struct got_worktree *worktree,
716 struct got_repository *repo, struct got_pathlist_head *oldpaths,
717 struct got_pathlist_head *newpaths, char **old, char **new)
719 const struct got_error *err;
721 TAILQ_INIT(oldpaths);
722 TAILQ_INIT(newpaths);
723 *old = NULL;
724 *new = NULL;
726 err = build_pathlist(p->old != NULL ? p->old : p->new, old,
727 oldpaths, worktree);
728 if (err)
729 goto err;
731 err = build_pathlist(p->new != NULL ? p->new : p->old, new,
732 newpaths, worktree);
733 if (err)
734 goto err;
735 return NULL;
737 err:
738 free(*old);
739 free(*new);
740 got_pathlist_free(oldpaths);
741 got_pathlist_free(newpaths);
742 return err;
745 const struct got_error *
746 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
747 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
748 got_cancel_cb cancel_cb, void *cancel_arg)
750 const struct got_error *err = NULL;
751 struct got_pathlist_head oldpaths, newpaths;
752 char *oldpath, *newpath;
753 struct imsgbuf *ibuf;
754 int imsg_fds[2] = {-1, -1};
755 int done = 0, failed = 0;
756 pid_t pid;
758 ibuf = calloc(1, sizeof(*ibuf));
759 if (ibuf == NULL) {
760 err = got_error_from_errno("calloc");
761 goto done;
764 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
765 err = got_error_from_errno("socketpair");
766 goto done;
769 pid = fork();
770 if (pid == -1) {
771 err = got_error_from_errno("fork");
772 goto done;
773 } else if (pid == 0) {
774 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
775 NULL);
776 /* not reached */
779 if (close(imsg_fds[1]) == -1) {
780 err = got_error_from_errno("close");
781 goto done;
783 imsg_fds[1] = -1;
784 imsg_init(ibuf, imsg_fds[0]);
786 err = send_patch(ibuf, fd);
787 fd = -1;
788 if (err)
789 goto done;
791 while (!done && err == NULL) {
792 struct got_patch p;
793 struct patch_args pa;
795 pa.progress_cb = progress_cb;
796 pa.progress_arg = progress_arg;
797 pa.head = &p.head;
799 err = recv_patch(ibuf, &done, &p);
800 if (err || done)
801 break;
803 err = resolve_paths(&p, worktree, repo, &oldpaths,
804 &newpaths, &oldpath, &newpath);
805 if (err)
806 break;
808 err = apply_patch(worktree, repo, &oldpaths, &newpaths,
809 oldpath, newpath, &p, nop, &pa, cancel_cb, cancel_arg);
810 if (err != NULL) {
811 failed = 1;
812 /* recoverable errors */
813 if (err->code == GOT_ERR_FILE_STATUS ||
814 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
815 err = report_progress(&pa, p.old, p.new,
816 GOT_STATUS_CANNOT_UPDATE, err);
817 else if (err->code == GOT_ERR_HUNK_FAILED)
818 err = report_progress(&pa, p.old, p.new,
819 GOT_STATUS_CANNOT_UPDATE, NULL);
822 free(oldpath);
823 free(newpath);
824 got_pathlist_free(&oldpaths);
825 got_pathlist_free(&newpaths);
826 patch_free(&p);
828 if (err)
829 break;
832 done:
833 if (fd != -1 && close(fd) == -1 && err == NULL)
834 err = got_error_from_errno("close");
835 if (ibuf != NULL)
836 imsg_clear(ibuf);
837 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
838 err = got_error_from_errno("close");
839 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
840 err = got_error_from_errno("close");
841 if (err == NULL && failed)
842 err = got_error(GOT_ERR_PATCH_FAILED);
843 return err;