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_repository.h"
46 #include "got_opentemp.h"
47 #include "got_patch.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_diff.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_sha1.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 int offset;
61 int old_nonl;
62 int new_nonl;
63 int old_from;
64 int old_lines;
65 int new_from;
66 int new_lines;
67 size_t len;
68 size_t cap;
69 char **lines;
70 };
72 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
73 struct got_patch {
74 char *old;
75 char *new;
76 char cid[41];
77 char blob[41];
78 struct got_patch_hunk_head head;
79 };
81 struct patch_args {
82 got_patch_progress_cb progress_cb;
83 void *progress_arg;
84 struct got_patch_hunk_head *head;
85 };
87 static const struct got_error *
88 send_patch(struct imsgbuf *ibuf, int fd)
89 {
90 const struct got_error *err = NULL;
92 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
93 NULL, 0) == -1) {
94 err = got_error_from_errno(
95 "imsg_compose GOT_IMSG_PATCH_FILE");
96 close(fd);
97 return err;
98 }
100 if (imsg_flush(ibuf) == -1) {
101 err = got_error_from_errno("imsg_flush");
102 imsg_clear(ibuf);
105 return err;
108 static void
109 patch_free(struct got_patch *p)
111 struct got_patch_hunk *h;
112 size_t i;
114 while (!STAILQ_EMPTY(&p->head)) {
115 h = STAILQ_FIRST(&p->head);
116 STAILQ_REMOVE_HEAD(&p->head, entries);
118 for (i = 0; i < h->len; ++i)
119 free(h->lines[i]);
120 free(h->lines);
121 free(h);
124 free(p->new);
125 free(p->old);
127 memset(p, 0, sizeof(*p));
128 STAILQ_INIT(&p->head);
131 static const struct got_error *
132 pushline(struct got_patch_hunk *h, const char *line)
134 void *t;
135 size_t newcap;
137 if (h->len == h->cap) {
138 if ((newcap = h->cap * 1.5) == 0)
139 newcap = 16;
140 t = recallocarray(h->lines, h->cap, newcap,
141 sizeof(h->lines[0]));
142 if (t == NULL)
143 return got_error_from_errno("recallocarray");
144 h->lines = t;
145 h->cap = newcap;
148 if ((t = strdup(line)) == NULL)
149 return got_error_from_errno("strdup");
151 h->lines[h->len++] = t;
152 return NULL;
155 static const struct got_error *
156 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
158 const struct got_error *err = NULL;
159 struct imsg imsg;
160 struct got_imsg_patch_hunk hdr;
161 struct got_imsg_patch patch;
162 struct got_patch_hunk *h = NULL;
163 size_t datalen;
164 int lastmode = -1;
166 memset(p, 0, sizeof(*p));
167 STAILQ_INIT(&p->head);
169 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
170 if (err)
171 return err;
172 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
173 *done = 1;
174 goto done;
176 if (imsg.hdr.type != GOT_IMSG_PATCH) {
177 err = got_error(GOT_ERR_PRIVSEP_MSG);
178 goto done;
180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
181 if (datalen != sizeof(patch)) {
182 err = got_error(GOT_ERR_PRIVSEP_LEN);
183 goto done;
185 memcpy(&patch, imsg.data, sizeof(patch));
187 if (patch.old[sizeof(patch.old)-1] != '\0' ||
188 patch.new[sizeof(patch.new)-1] != '\0' ||
189 patch.cid[sizeof(patch.cid)-1] != '\0' ||
190 patch.blob[sizeof(patch.blob)-1] != '\0') {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 goto done;
195 if (*patch.cid != '\0' && *patch.blob != '\0') {
196 strlcpy(p->cid, patch.cid, sizeof(p->cid));
197 strlcpy(p->blob, patch.blob, sizeof(p->blob));
200 /* automatically set strip=1 for git-style diffs */
201 if (strip == -1 && patch.git &&
202 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
203 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
204 strip = 1;
206 /* prefer the new name if not /dev/null for not git-style diffs */
207 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
208 err = got_path_strip(&p->old, patch.new, strip);
209 if (err)
210 goto done;
211 } else if (*patch.old != '\0') {
212 err = got_path_strip(&p->old, patch.old, strip);
213 if (err)
214 goto done;
217 if (*patch.new != '\0') {
218 err = got_path_strip(&p->new, patch.new, strip);
219 if (err)
220 goto done;
223 if (p->old == NULL && p->new == NULL) {
224 err = got_error(GOT_ERR_PATCH_MALFORMED);
225 goto done;
228 imsg_free(&imsg);
230 for (;;) {
231 char *t;
233 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
234 if (err) {
235 patch_free(p);
236 return err;
239 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
240 switch (imsg.hdr.type) {
241 case GOT_IMSG_PATCH_DONE:
242 if (h != NULL && h->len == 0)
243 err = got_error(GOT_ERR_PATCH_MALFORMED);
244 goto done;
245 case GOT_IMSG_PATCH_HUNK:
246 if (h != NULL &&
247 (h->len == 0 || h->old_nonl || h->new_nonl)) {
248 err = got_error(GOT_ERR_PATCH_MALFORMED);
249 goto done;
251 lastmode = -1;
252 if (datalen != sizeof(hdr)) {
253 err = got_error(GOT_ERR_PRIVSEP_LEN);
254 goto done;
256 memcpy(&hdr, imsg.data, sizeof(hdr));
257 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
258 err = got_error(GOT_ERR_PRIVSEP_LEN);
259 goto done;
261 if ((h = calloc(1, sizeof(*h))) == NULL) {
262 err = got_error_from_errno("calloc");
263 goto done;
265 h->old_from = hdr.oldfrom;
266 h->old_lines = hdr.oldlines;
267 h->new_from = hdr.newfrom;
268 h->new_lines = hdr.newlines;
269 STAILQ_INSERT_TAIL(&p->head, h, entries);
270 break;
271 case GOT_IMSG_PATCH_LINE:
272 if (h == NULL) {
273 err = got_error(GOT_ERR_PRIVSEP_MSG);
274 goto done;
276 t = imsg.data;
277 /* at least one char */
278 if (datalen < 2 || t[datalen-1] != '\0') {
279 err = got_error(GOT_ERR_PRIVSEP_MSG);
280 goto done;
282 if (*t != ' ' && *t != '-' && *t != '+' &&
283 *t != '\\') {
284 err = got_error(GOT_ERR_PRIVSEP_MSG);
285 goto done;
288 if (*t != '\\')
289 err = pushline(h, t);
290 else if (lastmode == '-')
291 h->old_nonl = 1;
292 else if (lastmode == '+')
293 h->new_nonl = 1;
294 else
295 err = got_error(GOT_ERR_PATCH_MALFORMED);
297 if (err)
298 goto done;
300 lastmode = *t;
301 break;
302 default:
303 err = got_error(GOT_ERR_PRIVSEP_MSG);
304 goto done;
307 imsg_free(&imsg);
310 done:
311 if (err)
312 patch_free(p);
314 imsg_free(&imsg);
315 return err;
318 /*
319 * Copy data from orig starting at copypos until pos into tmp.
320 * If pos is -1, copy until EOF.
321 */
322 static const struct got_error *
323 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
325 char buf[BUFSIZ];
326 size_t len, r, w;
328 if (fseeko(orig, copypos, SEEK_SET) == -1)
329 return got_error_from_errno("fseeko");
331 while (pos == -1 || copypos < pos) {
332 len = sizeof(buf);
333 if (pos > 0)
334 len = MIN(len, (size_t)pos - copypos);
335 r = fread(buf, 1, len, orig);
336 if (r != len && ferror(orig))
337 return got_error_from_errno("fread");
338 w = fwrite(buf, 1, r, tmp);
339 if (w != r)
340 return got_error_from_errno("fwrite");
341 copypos += len;
342 if (r != len && feof(orig)) {
343 if (pos == -1)
344 return NULL;
345 return got_error(GOT_ERR_HUNK_FAILED);
348 return NULL;
351 static const struct got_error *
352 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
354 const struct got_error *err = NULL;
355 char *line = NULL;
356 char mode = *h->lines[0];
357 size_t linesize = 0;
358 ssize_t linelen;
359 off_t match = -1;
360 int match_lineno = -1;
362 for (;;) {
363 linelen = getline(&line, &linesize, orig);
364 if (linelen == -1) {
365 if (ferror(orig))
366 err = got_error_from_errno("getline");
367 else if (match == -1)
368 err = got_error(GOT_ERR_HUNK_FAILED);
369 break;
371 if (line[linelen - 1] == '\n')
372 line[linelen - 1] = '\0';
373 (*lineno)++;
375 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
376 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
377 (mode == '+' && *lineno == h->old_from)) {
378 match = ftello(orig);
379 if (match == -1) {
380 err = got_error_from_errno("ftello");
381 break;
383 match -= linelen;
384 match_lineno = (*lineno)-1;
387 if (*lineno >= h->old_from && match != -1)
388 break;
391 if (err == NULL) {
392 *pos = match;
393 *lineno = match_lineno;
394 if (fseeko(orig, match, SEEK_SET) == -1)
395 err = got_error_from_errno("fseeko");
398 free(line);
399 return err;
402 static const struct got_error *
403 test_hunk(FILE *orig, struct got_patch_hunk *h)
405 const struct got_error *err = NULL;
406 char *line = NULL;
407 size_t linesize = 0, i = 0;
408 ssize_t linelen;
410 for (i = 0; i < h->len; ++i) {
411 switch (*h->lines[i]) {
412 case '+':
413 continue;
414 case ' ':
415 case '-':
416 linelen = getline(&line, &linesize, orig);
417 if (linelen == -1) {
418 if (ferror(orig))
419 err = got_error_from_errno("getline");
420 else
421 err = got_error(
422 GOT_ERR_HUNK_FAILED);
423 goto done;
425 if (line[linelen - 1] == '\n')
426 line[linelen - 1] = '\0';
427 if (strcmp(h->lines[i] + 1, line)) {
428 err = got_error(GOT_ERR_HUNK_FAILED);
429 goto done;
431 break;
435 done:
436 free(line);
437 return err;
440 static const struct got_error *
441 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
443 size_t i, new = 0;
445 for (i = 0; i < h->len; ++i) {
446 switch (*h->lines[i]) {
447 case ' ':
448 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
449 return got_error_from_errno("fprintf");
450 /* fallthrough */
451 case '-':
452 (*lineno)++;
453 break;
454 case '+':
455 new++;
456 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
457 return got_error_from_errno("fprintf");
458 if (new != h->new_lines || !h->new_nonl) {
459 if (fprintf(tmp, "\n") < 0)
460 return got_error_from_errno(
461 "fprintf");
463 break;
466 return NULL;
469 static const struct got_error *
470 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
472 const struct got_error *err = NULL;
473 struct got_patch_hunk *h;
474 struct stat sb;
475 int lineno = 0;
476 off_t copypos, pos;
477 char *line = NULL;
478 size_t linesize = 0;
479 ssize_t linelen;
481 if (p->old == NULL) { /* create */
482 h = STAILQ_FIRST(&p->head);
483 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
484 return got_error(GOT_ERR_PATCH_MALFORMED);
485 return apply_hunk(tmp, h, &lineno);
488 if (fstat(fileno(orig), &sb) == -1)
489 return got_error_from_errno("fstat");
490 *mode = sb.st_mode;
492 copypos = 0;
493 STAILQ_FOREACH(h, &p->head, entries) {
494 tryagain:
495 err = locate_hunk(orig, h, &pos, &lineno);
496 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
497 h->err = err;
498 if (err != NULL)
499 return err;
500 err = copy(tmp, orig, copypos, pos);
501 if (err != NULL)
502 return err;
503 copypos = pos;
505 err = test_hunk(orig, h);
506 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
507 /*
508 * try to apply the hunk again starting the search
509 * after the previous partial match.
510 */
511 if (fseeko(orig, pos, SEEK_SET) == -1)
512 return got_error_from_errno("fseeko");
513 linelen = getline(&line, &linesize, orig);
514 if (linelen == -1)
515 return got_error_from_errno("getline");
516 lineno++;
517 goto tryagain;
519 if (err != NULL)
520 return err;
522 if (lineno + 1 != h->old_from)
523 h->offset = lineno + 1 - h->old_from;
525 err = apply_hunk(tmp, h, &lineno);
526 if (err != NULL)
527 return err;
529 copypos = ftello(orig);
530 if (copypos == -1)
531 return got_error_from_errno("ftello");
534 if (p->new == NULL && sb.st_size != copypos) {
535 h = STAILQ_FIRST(&p->head);
536 h->err = got_error(GOT_ERR_HUNK_FAILED);
537 err = h->err;
538 } else if (!feof(orig))
539 err = copy(tmp, orig, copypos, -1);
541 return err;
544 static const struct got_error *
545 report_progress(struct patch_args *pa, const char *old, const char *new,
546 unsigned char status, const struct got_error *orig_error)
548 const struct got_error *err;
549 struct got_patch_hunk *h;
551 err = pa->progress_cb(pa->progress_arg, old, new, status,
552 orig_error, 0, 0, 0, 0, 0, NULL);
553 if (err)
554 return err;
556 STAILQ_FOREACH(h, pa->head, entries) {
557 if (h->offset == 0 && h->err == NULL)
558 continue;
560 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
561 h->old_from, h->old_lines, h->new_from, h->new_lines,
562 h->offset, h->err);
563 if (err)
564 return err;
567 return NULL;
570 static const struct got_error *
571 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
572 const char *path)
574 return report_progress(arg, path, NULL, status, NULL);
577 static const struct got_error *
578 patch_add(void *arg, unsigned char status, const char *path)
580 return report_progress(arg, NULL, path, status, NULL);
583 static const struct got_error *
584 open_blob(char **path, FILE **fp, const char *blobid,
585 struct got_repository *repo)
587 const struct got_error *err = NULL;
588 struct got_blob_object *blob = NULL;
589 struct got_object_id id;
591 *fp = NULL;
592 *path = NULL;
594 if (!got_parse_sha1_digest(id.sha1, blobid))
595 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
597 err = got_object_open_as_blob(&blob, repo, &id, 8192);
598 if (err)
599 goto done;
601 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
602 if (err)
603 goto done;
605 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
606 if (err)
607 goto done;
609 done:
610 if (blob)
611 got_object_blob_close(blob);
612 if (err) {
613 if (*fp != NULL)
614 fclose(*fp);
615 if (*path != NULL)
616 unlink(*path);
617 free(*path);
618 *fp = NULL;
619 *path = NULL;
621 return err;
624 static const struct got_error *
625 apply_patch(int *overlapcnt, struct got_worktree *worktree,
626 struct got_repository *repo, struct got_fileindex *fileindex,
627 const char *old, const char *new, struct got_patch *p, int nop,
628 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
630 const struct got_error *err = NULL;
631 int do_merge = 0, file_renamed = 0;
632 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
633 char *oldpath = NULL, *newpath = NULL;
634 char *tmppath = NULL, *template = NULL, *parent = NULL;
635 char *apath = NULL, *mergepath = NULL;
636 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
637 int outfd;
638 const char *outpath;
639 mode_t mode = GOT_DEFAULT_FILE_MODE;
641 *overlapcnt = 0;
643 /* don't run the diff3 merge on creations/deletions */
644 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
645 err = open_blob(&apath, &afile, p->blob, repo);
646 /*
647 * ignore failures to open this blob, we might have
648 * parsed gibberish.
649 */
650 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
651 return err;
652 else if (err == NULL)
653 do_merge = 1;
654 err = NULL;
657 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
658 old) == -1) {
659 err = got_error_from_errno("asprintf");
660 goto done;
663 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
664 new) == -1) {
665 err = got_error_from_errno("asprintf");
666 goto done;
669 file_renamed = strcmp(oldpath, newpath);
671 if (asprintf(&template, "%s/got-patch",
672 got_worktree_get_root_path(worktree)) == -1) {
673 err = got_error_from_errno(template);
674 goto done;
677 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
678 err = got_error_from_errno2("open", oldpath);
679 goto done;
682 err = got_opentemp_named(&tmppath, &tmpfile, template);
683 if (err)
684 goto done;
685 outpath = tmppath;
686 outfd = fileno(tmpfile);
687 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
688 if (err)
689 goto done;
691 if (do_merge) {
692 if (fseeko(afile, 0, SEEK_SET) == -1 ||
693 fseeko(oldfile, 0, SEEK_SET) == -1 ||
694 fseeko(tmpfile, 0, SEEK_SET) == -1) {
695 err = got_error_from_errno("fseeko");
696 goto done;
699 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
700 err = got_error_from_errno("asprintf");
701 oldlabel = NULL;
702 goto done;
705 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
706 err = got_error_from_errno("asprintf");
707 newlabel = NULL;
708 goto done;
711 if (asprintf(&anclabel, "commit %s", p->cid) == -1) {
712 err = got_error_from_errno("asprintf");
713 anclabel = NULL;
714 goto done;
717 err = got_opentemp_named(&mergepath, &mergefile, template);
718 if (err)
719 goto done;
720 outpath = mergepath;
721 outfd = fileno(mergefile);
723 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
724 oldfile, oldlabel, anclabel, newlabel,
725 GOT_DIFF_ALGORITHM_PATIENCE);
726 if (err)
727 goto done;
730 if (nop)
731 goto done;
733 if (p->old != NULL && p->new == NULL) {
734 err = got_worktree_patch_schedule_rm(old, repo, worktree,
735 fileindex, patch_delete, pa);
736 goto done;
739 if (fchmod(outfd, mode) == -1) {
740 err = got_error_from_errno2("chmod", tmppath);
741 goto done;
744 if (rename(outpath, newpath) == -1) {
745 if (errno != ENOENT) {
746 err = got_error_from_errno3("rename", outpath,
747 newpath);
748 goto done;
751 err = got_path_dirname(&parent, newpath);
752 if (err != NULL)
753 goto done;
754 err = got_path_mkdir(parent);
755 if (err != NULL)
756 goto done;
757 if (rename(outpath, newpath) == -1) {
758 err = got_error_from_errno3("rename", outpath,
759 newpath);
760 goto done;
764 if (file_renamed) {
765 err = got_worktree_patch_schedule_rm(old, repo, worktree,
766 fileindex, patch_delete, pa);
767 if (err == NULL)
768 err = got_worktree_patch_schedule_add(new, repo,
769 worktree, fileindex, patch_add,
770 pa);
771 if (err)
772 unlink(newpath);
773 } else if (p->old == NULL) {
774 err = got_worktree_patch_schedule_add(new, repo, worktree,
775 fileindex, patch_add, pa);
776 if (err)
777 unlink(newpath);
778 } else if (*overlapcnt != 0)
779 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
780 else if (do_merge)
781 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
782 else
783 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
785 done:
786 free(parent);
787 free(template);
789 if (tmppath != NULL)
790 unlink(tmppath);
791 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
792 err = got_error_from_errno("fclose");
793 free(tmppath);
795 free(oldpath);
796 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
797 err = got_error_from_errno("fclose");
799 if (apath != NULL)
800 unlink(apath);
801 if (afile != NULL && fclose(afile) == EOF && err == NULL)
802 err = got_error_from_errno("fclose");
803 free(apath);
805 if (mergepath != NULL)
806 unlink(mergepath);
807 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
808 err = got_error_from_errno("fclose");
809 free(mergepath);
811 free(newpath);
812 free(oldlabel);
813 free(newlabel);
814 free(anclabel);
815 return err;
818 static void
819 reverse_patch(struct got_patch *p)
821 struct got_patch_hunk *h;
822 size_t i;
823 int tmp;
825 STAILQ_FOREACH(h, &p->head, entries) {
826 tmp = h->old_from;
827 h->old_from = h->new_from;
828 h->new_from = tmp;
830 tmp = h->old_lines;
831 h->old_lines = h->new_lines;
832 h->new_lines = tmp;
834 tmp = h->old_nonl;
835 h->old_nonl = h->new_nonl;
836 h->new_nonl = tmp;
838 for (i = 0; i < h->len; ++i) {
839 if (*h->lines[i] == '+')
840 *h->lines[i] = '-';
841 else if (*h->lines[i] == '-')
842 *h->lines[i] = '+';
847 const struct got_error *
848 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
849 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
850 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
852 const struct got_error *err = NULL, *complete_err = NULL;
853 struct got_fileindex *fileindex = NULL;
854 char *fileindex_path = NULL;
855 char *oldpath, *newpath;
856 struct imsgbuf *ibuf;
857 int imsg_fds[2] = {-1, -1};
858 int overlapcnt, done = 0, failed = 0;
859 pid_t pid;
861 ibuf = calloc(1, sizeof(*ibuf));
862 if (ibuf == NULL) {
863 err = got_error_from_errno("calloc");
864 goto done;
867 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
868 err = got_error_from_errno("socketpair");
869 goto done;
872 pid = fork();
873 if (pid == -1) {
874 err = got_error_from_errno("fork");
875 goto done;
876 } else if (pid == 0) {
877 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
878 NULL);
879 /* not reached */
882 if (close(imsg_fds[1]) == -1) {
883 err = got_error_from_errno("close");
884 goto done;
886 imsg_fds[1] = -1;
887 imsg_init(ibuf, imsg_fds[0]);
889 err = send_patch(ibuf, fd);
890 fd = -1;
891 if (err)
892 goto done;
894 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
895 worktree);
896 if (err)
897 goto done;
899 while (!done && err == NULL) {
900 struct got_patch p;
901 struct patch_args pa;
903 pa.progress_cb = progress_cb;
904 pa.progress_arg = progress_arg;
905 pa.head = &p.head;
907 err = recv_patch(ibuf, &done, &p, strip);
908 if (err || done)
909 break;
911 if (reverse)
912 reverse_patch(&p);
914 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
915 &newpath, worktree, repo, fileindex);
916 if (err == NULL)
917 err = apply_patch(&overlapcnt, worktree, repo,
918 fileindex, oldpath, newpath, &p, nop, &pa,
919 cancel_cb, cancel_arg);
920 if (err != NULL) {
921 failed = 1;
922 /* recoverable errors */
923 if (err->code == GOT_ERR_FILE_STATUS ||
924 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
925 err = report_progress(&pa, p.old, p.new,
926 GOT_STATUS_CANNOT_UPDATE, err);
927 else if (err->code == GOT_ERR_HUNK_FAILED)
928 err = report_progress(&pa, p.old, p.new,
929 GOT_STATUS_CANNOT_UPDATE, NULL);
931 if (overlapcnt != 0)
932 failed = 1;
934 free(oldpath);
935 free(newpath);
936 patch_free(&p);
938 if (err)
939 break;
942 done:
943 if (fileindex != NULL)
944 complete_err = got_worktree_patch_complete(fileindex,
945 fileindex_path);
946 if (complete_err && err == NULL)
947 err = complete_err;
948 free(fileindex_path);
949 if (fd != -1 && close(fd) == -1 && err == NULL)
950 err = got_error_from_errno("close");
951 if (ibuf != NULL)
952 imsg_clear(ibuf);
953 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
954 err = got_error_from_errno("close");
955 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
956 err = got_error_from_errno("close");
957 if (err == NULL && failed)
958 err = got_error(GOT_ERR_PATCH_FAILED);
959 return err;