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 <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <imsg.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_reference.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_opentemp.h"
48 #include "got_patch.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
54 #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 struct got_patch_hunk {
57 STAILQ_ENTRY(got_patch_hunk) entries;
58 const struct got_error *err;
59 long offset;
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)
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;
157 memset(p, 0, sizeof(*p));
158 STAILQ_INIT(&p->head);
160 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
161 if (err)
162 return err;
163 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
164 *done = 1;
165 goto done;
167 if (imsg.hdr.type != GOT_IMSG_PATCH) {
168 err = got_error(GOT_ERR_PRIVSEP_MSG);
169 goto done;
171 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
172 if (datalen != sizeof(patch)) {
173 err = got_error(GOT_ERR_PRIVSEP_LEN);
174 goto done;
176 memcpy(&patch, imsg.data, sizeof(patch));
177 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
178 err = got_error_from_errno("strdup");
179 goto done;
181 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
182 err = got_error_from_errno("strdup");
183 goto done;
185 if (p->old == NULL && p->new == NULL) {
186 err = got_error(GOT_ERR_PATCH_MALFORMED);
187 goto done;
190 imsg_free(&imsg);
192 for (;;) {
193 char *t;
195 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
196 if (err)
197 return err;
199 switch (imsg.hdr.type) {
200 case GOT_IMSG_PATCH_DONE:
201 goto done;
202 case GOT_IMSG_PATCH_HUNK:
203 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
204 if (datalen != sizeof(hdr)) {
205 err = got_error(GOT_ERR_PRIVSEP_LEN);
206 goto done;
208 memcpy(&hdr, imsg.data, sizeof(hdr));
209 if ((h = calloc(1, sizeof(*h))) == NULL) {
210 err = got_error_from_errno("calloc");
211 goto done;
213 h->old_from = hdr.oldfrom;
214 h->old_lines = hdr.oldlines;
215 h->new_from = hdr.newfrom;
216 h->new_lines = hdr.newlines;
217 STAILQ_INSERT_TAIL(&p->head, h, entries);
218 break;
219 case GOT_IMSG_PATCH_LINE:
220 if (h == NULL) {
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
222 goto done;
224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
225 t = imsg.data;
226 /* at least one char plus newline */
227 if (datalen < 2 || t[datalen-1] != '\0') {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
229 goto done;
231 if (*t != ' ' && *t != '-' && *t != '+') {
232 err = got_error(GOT_ERR_PRIVSEP_MSG);
233 goto done;
235 err = pushline(h, t);
236 if (err)
237 goto done;
238 break;
239 default:
240 err = got_error(GOT_ERR_PRIVSEP_MSG);
241 goto done;
244 imsg_free(&imsg);
247 done:
248 imsg_free(&imsg);
249 return err;
252 /*
253 * Copy data from orig starting at copypos until pos into tmp.
254 * If pos is -1, copy until EOF.
255 */
256 static const struct got_error *
257 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
259 char buf[BUFSIZ];
260 size_t len, r, w;
262 if (fseek(orig, copypos, SEEK_SET) == -1)
263 return got_error_from_errno("fseek");
265 while (pos == -1 || copypos < pos) {
266 len = sizeof(buf);
267 if (pos > 0)
268 len = MIN(len, (size_t)pos - copypos);
269 r = fread(buf, 1, len, orig);
270 if (r != len && ferror(orig))
271 return got_error_from_errno("fread");
272 w = fwrite(buf, 1, r, tmp);
273 if (w != r)
274 return got_error_from_errno("fwrite");
275 copypos += len;
276 if (r != len && feof(orig)) {
277 if (pos == -1)
278 return NULL;
279 return got_error(GOT_ERR_HUNK_FAILED);
282 return NULL;
285 static const struct got_error *
286 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
288 const struct got_error *err = NULL;
289 char *line = NULL;
290 char mode = *h->lines[0];
291 size_t linesize = 0;
292 ssize_t linelen;
293 off_t match = -1;
294 long match_lineno = -1;
296 for (;;) {
297 linelen = getline(&line, &linesize, orig);
298 if (linelen == -1) {
299 if (ferror(orig))
300 err = got_error_from_errno("getline");
301 else if (match == -1)
302 err = got_error(GOT_ERR_HUNK_FAILED);
303 break;
305 (*lineno)++;
307 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
308 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
309 (mode == '+' && *lineno == h->old_from)) {
310 match = ftello(orig);
311 if (match == -1) {
312 err = got_error_from_errno("ftello");
313 break;
315 match -= linelen;
316 match_lineno = (*lineno)-1;
319 if (*lineno >= h->old_from && match != -1)
320 break;
323 if (err == NULL) {
324 *pos = match;
325 *lineno = match_lineno;
326 if (fseek(orig, match, SEEK_SET) == -1)
327 err = got_error_from_errno("fseek");
330 free(line);
331 return err;
334 static const struct got_error *
335 test_hunk(FILE *orig, struct got_patch_hunk *h)
337 const struct got_error *err = NULL;
338 char *line = NULL;
339 size_t linesize = 0, i = 0;
340 ssize_t linelen;
342 for (i = 0; i < h->len; ++i) {
343 switch (*h->lines[i]) {
344 case '+':
345 continue;
346 case ' ':
347 case '-':
348 linelen = getline(&line, &linesize, orig);
349 if (linelen == -1) {
350 if (ferror(orig))
351 err = got_error_from_errno("getline");
352 else
353 err = got_error(
354 GOT_ERR_HUNK_FAILED);
355 goto done;
357 if (strcmp(h->lines[i] + 1, line)) {
358 err = got_error(GOT_ERR_HUNK_FAILED);
359 goto done;
361 break;
365 done:
366 free(line);
367 return err;
370 static const struct got_error *
371 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
373 size_t i = 0;
375 for (i = 0; i < h->len; ++i) {
376 switch (*h->lines[i]) {
377 case ' ':
378 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
379 return got_error_from_errno("fprintf");
380 /* fallthrough */
381 case '-':
382 (*lineno)++;
383 break;
384 case '+':
385 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
386 return got_error_from_errno("fprintf");
387 break;
390 return NULL;
393 static const struct got_error *
394 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
395 mode_t *mode)
397 const struct got_error *err = NULL;
398 struct got_patch_hunk *h;
399 struct stat sb;
400 size_t i;
401 long lineno = 0;
402 FILE *orig;
403 off_t copypos, pos;
404 char *line = NULL;
405 size_t linesize = 0;
406 ssize_t linelen;
408 if (p->old == NULL) { /* create */
409 h = STAILQ_FIRST(&p->head);
410 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
411 return got_error(GOT_ERR_PATCH_MALFORMED);
412 if (nop)
413 return NULL;
414 for (i = 0; i < h->len; ++i) {
415 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
416 return got_error_from_errno("fprintf");
418 return err;
421 if ((orig = fopen(path, "r")) == NULL) {
422 err = got_error_from_errno2("fopen", path);
423 goto done;
426 if (fstat(fileno(orig), &sb) == -1) {
427 err = got_error_from_errno("fstat");
428 goto done;
430 *mode = sb.st_mode;
432 copypos = 0;
433 STAILQ_FOREACH(h, &p->head, entries) {
434 if (h->lines == NULL)
435 break;
437 tryagain:
438 err = locate_hunk(orig, h, &pos, &lineno);
439 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
440 h->err = err;
441 if (err != NULL)
442 goto done;
443 if (!nop)
444 err = copy(tmp, orig, copypos, pos);
445 if (err != NULL)
446 goto done;
447 copypos = pos;
449 err = test_hunk(orig, h);
450 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
451 /*
452 * try to apply the hunk again starting the search
453 * after the previous partial match.
454 */
455 if (fseek(orig, pos, SEEK_SET) == -1) {
456 err = got_error_from_errno("fseek");
457 goto done;
459 linelen = getline(&line, &linesize, orig);
460 if (linelen == -1) {
461 err = got_error_from_errno("getline");
462 goto done;
464 lineno++;
465 goto tryagain;
467 if (err != NULL)
468 goto done;
470 if (lineno + 1 != h->old_from)
471 h->offset = lineno + 1 - h->old_from;
473 if (!nop)
474 err = apply_hunk(tmp, h, &lineno);
475 if (err != NULL)
476 goto done;
478 copypos = ftello(orig);
479 if (copypos == -1) {
480 err = got_error_from_errno("ftello");
481 goto done;
485 if (p->new == NULL && sb.st_size != copypos) {
486 h = STAILQ_FIRST(&p->head);
487 h->err = got_error(GOT_ERR_HUNK_FAILED);
488 err = h->err;
489 } else if (!nop && !feof(orig))
490 err = copy(tmp, orig, copypos, -1);
492 done:
493 if (orig != NULL)
494 fclose(orig);
495 return err;
498 static const struct got_error *
499 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
500 struct got_worktree *worktree)
502 const struct got_error *err;
503 struct got_pathlist_entry *pe;
505 err = got_worktree_resolve_path(path, worktree, p);
506 if (err == NULL)
507 err = got_pathlist_insert(&pe, head, *path, NULL);
508 return err;
511 static const struct got_error *
512 can_rm(void *arg, unsigned char status, unsigned char staged_status,
513 const char *path, struct got_object_id *blob_id,
514 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
515 int dirfd, const char *de_name)
517 if (status == GOT_STATUS_NONEXISTENT)
518 return got_error_set_errno(ENOENT, path);
519 if (status != GOT_STATUS_NO_CHANGE &&
520 status != GOT_STATUS_ADD &&
521 status != GOT_STATUS_MODIFY &&
522 status != GOT_STATUS_MODE_CHANGE)
523 return got_error_path(path, GOT_ERR_FILE_STATUS);
524 if (staged_status == GOT_STATUS_DELETE)
525 return got_error_path(path, GOT_ERR_FILE_STATUS);
526 return NULL;
529 static const struct got_error *
530 can_add(void *arg, unsigned char status, unsigned char staged_status,
531 const char *path, struct got_object_id *blob_id,
532 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
533 int dirfd, const char *de_name)
535 if (status != GOT_STATUS_NONEXISTENT)
536 return got_error_path(path, GOT_ERR_FILE_STATUS);
537 return NULL;
540 static const struct got_error *
541 can_edit(void *arg, unsigned char status, unsigned char staged_status,
542 const char *path, struct got_object_id *blob_id,
543 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
544 int dirfd, const char *de_name)
546 if (status == GOT_STATUS_NONEXISTENT)
547 return got_error_set_errno(ENOENT, path);
548 if (status != GOT_STATUS_NO_CHANGE &&
549 status != GOT_STATUS_ADD &&
550 status != GOT_STATUS_MODIFY)
551 return got_error_path(path, GOT_ERR_FILE_STATUS);
552 if (staged_status == GOT_STATUS_DELETE)
553 return got_error_path(path, GOT_ERR_FILE_STATUS);
554 return NULL;
557 static const struct got_error *
558 check_file_status(struct got_patch *p, int file_renamed,
559 struct got_worktree *worktree, struct got_repository *repo,
560 struct got_pathlist_head *old, struct got_pathlist_head *new,
561 got_cancel_cb cancel_cb, void *cancel_arg)
563 static const struct got_error *err;
565 if (p->old != NULL && p->new == NULL)
566 return got_worktree_status(worktree, old, repo, 0,
567 can_rm, NULL, cancel_cb, cancel_arg);
568 else if (file_renamed) {
569 err = got_worktree_status(worktree, old, repo, 0,
570 can_rm, NULL, cancel_cb, cancel_arg);
571 if (err)
572 return err;
573 return got_worktree_status(worktree, new, repo, 0,
574 can_add, NULL, cancel_cb, cancel_arg);
575 } else if (p->old == NULL)
576 return got_worktree_status(worktree, new, repo, 0,
577 can_add, NULL, cancel_cb, cancel_arg);
578 else
579 return got_worktree_status(worktree, new, repo, 0,
580 can_edit, NULL, cancel_cb, cancel_arg);
583 static const struct got_error *
584 report_progress(struct patch_args *pa, const char *old, const char *new,
585 unsigned char status, const struct got_error *orig_error)
587 const struct got_error *err;
588 struct got_patch_hunk *h;
590 err = pa->progress_cb(pa->progress_arg, old, new, status,
591 orig_error, 0, 0, 0, 0, 0, NULL);
592 if (err)
593 return err;
595 STAILQ_FOREACH(h, pa->head, entries) {
596 if (h->offset == 0 && h->err == NULL)
597 continue;
599 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
600 h->old_from, h->old_lines, h->new_from, h->new_lines,
601 h->offset, h->err);
602 if (err)
603 return err;
606 return NULL;
609 static const struct got_error *
610 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
611 const char *path)
613 return report_progress(arg, path, NULL, status, NULL);
616 static const struct got_error *
617 patch_add(void *arg, unsigned char status, const char *path)
619 return report_progress(arg, NULL, path, status, NULL);
622 static const struct got_error *
623 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
624 struct got_pathlist_head *oldpaths, struct got_pathlist_head *newpaths,
625 const char *oldpath, const char *newpath, struct got_patch *p,
626 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
628 const struct got_error *err = NULL;
629 int file_renamed = 0;
630 char *tmppath = NULL, *template = NULL, *parent = NULL;;
631 FILE *tmp = NULL;
632 mode_t mode = GOT_DEFAULT_FILE_MODE;
634 file_renamed = strcmp(oldpath, newpath);
636 err = check_file_status(p, file_renamed, worktree, repo, oldpaths,
637 newpaths, cancel_cb, cancel_arg);
638 if (err)
639 goto done;
641 if (p->old != NULL && p->new == NULL) {
642 /*
643 * special case: delete a file. don't try to match
644 * the lines but just schedule the removal.
645 */
646 err = got_worktree_schedule_delete(worktree, &oldpaths,
647 0, NULL, delete_cb, delete_arg, repo, 0, 0);
648 goto done;
651 if (asprintf(&template, "%s/got-patch",
652 got_worktree_get_root_path(worktree)) == -1) {
653 err = got_error_from_errno(template);
654 goto done;
657 if (!nop)
658 err = got_opentemp_named(&tmppath, &tmp, template);
659 if (err)
660 goto done;
661 err = patch_file(p, oldpath, tmp, nop, &mode);
662 if (err)
663 goto done;
665 if (nop)
666 goto done;
668 if (p->old != NULL && p->new == NULL) {
669 err = got_worktree_schedule_delete(worktree, oldpaths,
670 0, NULL, patch_delete, pa, repo, 0, 0);
671 goto done;
674 if (fchmod(fileno(tmp), mode) == -1) {
675 err = got_error_from_errno2("chmod", newpath);
676 goto done;
679 if (rename(tmppath, newpath) == -1) {
680 if (errno != ENOENT) {
681 err = got_error_from_errno3("rename", tmppath,
682 newpath);
683 goto done;
686 err = got_path_dirname(&parent, newpath);
687 if (err != NULL)
688 goto done;
689 err = got_path_mkdir(parent);
690 if (err != NULL)
691 goto done;
692 if (rename(tmppath, newpath) == -1) {
693 err = got_error_from_errno3("rename", tmppath,
694 newpath);
695 goto done;
699 if (file_renamed) {
700 err = got_worktree_schedule_delete(worktree, oldpaths,
701 0, NULL, patch_delete, pa, repo, 0, 0);
702 if (err == NULL)
703 err = got_worktree_schedule_add(worktree, newpaths,
704 patch_add, pa, repo, 1);
705 } else if (p->old == NULL)
706 err = got_worktree_schedule_add(worktree, newpaths,
707 patch_add, pa, repo, 1);
708 else
709 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
710 NULL);
712 done:
713 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
714 unlink(newpath);
715 free(parent);
716 free(template);
717 if (tmppath != NULL)
718 unlink(tmppath);
719 free(tmppath);
720 return err;
723 static const struct got_error *
724 resolve_paths(struct got_patch *p, struct got_worktree *worktree,
725 struct got_repository *repo, struct got_pathlist_head *oldpaths,
726 struct got_pathlist_head *newpaths, char **old, char **new)
728 const struct got_error *err;
730 TAILQ_INIT(oldpaths);
731 TAILQ_INIT(newpaths);
732 *old = NULL;
733 *new = NULL;
735 err = build_pathlist(p->old != NULL ? p->old : p->new, old,
736 oldpaths, worktree);
737 if (err)
738 goto err;
740 err = build_pathlist(p->new != NULL ? p->new : p->old, new,
741 newpaths, worktree);
742 if (err)
743 goto err;
744 return NULL;
746 err:
747 free(*old);
748 free(*new);
749 got_pathlist_free(oldpaths);
750 got_pathlist_free(newpaths);
751 return err;
754 const struct got_error *
755 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
756 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
757 got_cancel_cb cancel_cb, void *cancel_arg)
759 const struct got_error *err = NULL;
760 struct got_pathlist_head oldpaths, newpaths;
761 char *oldpath, *newpath;
762 struct imsgbuf *ibuf;
763 int imsg_fds[2] = {-1, -1};
764 int done = 0, failed = 0;
765 pid_t pid;
767 ibuf = calloc(1, sizeof(*ibuf));
768 if (ibuf == NULL) {
769 err = got_error_from_errno("calloc");
770 goto done;
773 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
774 err = got_error_from_errno("socketpair");
775 goto done;
778 pid = fork();
779 if (pid == -1) {
780 err = got_error_from_errno("fork");
781 goto done;
782 } else if (pid == 0) {
783 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
784 NULL);
785 /* not reached */
788 if (close(imsg_fds[1]) == -1) {
789 err = got_error_from_errno("close");
790 goto done;
792 imsg_fds[1] = -1;
793 imsg_init(ibuf, imsg_fds[0]);
795 err = send_patch(ibuf, fd);
796 fd = -1;
797 if (err)
798 goto done;
800 while (!done && err == NULL) {
801 struct got_patch p;
802 struct patch_args pa;
804 pa.progress_cb = progress_cb;
805 pa.progress_arg = progress_arg;
806 pa.head = &p.head;
808 err = recv_patch(ibuf, &done, &p);
809 if (err || done)
810 break;
812 err = resolve_paths(&p, worktree, repo, &oldpaths,
813 &newpaths, &oldpath, &newpath);
814 if (err)
815 break;
817 err = apply_patch(worktree, repo, &oldpaths, &newpaths,
818 oldpath, newpath, &p, nop, &pa, cancel_cb, cancel_arg);
819 if (err != NULL) {
820 failed = 1;
821 /* recoverable errors */
822 if (err->code == GOT_ERR_FILE_STATUS ||
823 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
824 err = report_progress(&pa, p.old, p.new,
825 GOT_STATUS_CANNOT_UPDATE, err);
826 else if (err->code == GOT_ERR_HUNK_FAILED)
827 err = report_progress(&pa, p.old, p.new,
828 GOT_STATUS_CANNOT_UPDATE, NULL);
831 free(oldpath);
832 free(newpath);
833 got_pathlist_free(&oldpaths);
834 got_pathlist_free(&newpaths);
835 patch_free(&p);
837 if (err)
838 break;
841 done:
842 if (fd != -1 && close(fd) == -1 && err == NULL)
843 err = got_error_from_errno("close");
844 if (ibuf != NULL)
845 imsg_clear(ibuf);
846 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
847 err = got_error_from_errno("close");
848 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
849 err = got_error_from_errno("close");
850 if (err == NULL && failed)
851 err = got_error(GOT_ERR_PATCH_FAILED);
852 return err;