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 if (err && err->code != GOT_ERR_NOT_REF)
647 return err;
648 else if (err == NULL)
649 do_merge = 1;
650 err = NULL;
653 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
654 old) == -1) {
655 err = got_error_from_errno("asprintf");
656 goto done;
659 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
660 new) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 file_renamed = strcmp(oldpath, newpath);
667 if (asprintf(&template, "%s/got-patch",
668 got_worktree_get_root_path(worktree)) == -1) {
669 err = got_error_from_errno(template);
670 goto done;
673 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
674 err = got_error_from_errno2("open", oldpath);
675 goto done;
678 err = got_opentemp_named(&tmppath, &tmpfile, template);
679 if (err)
680 goto done;
681 outpath = tmppath;
682 outfd = fileno(tmpfile);
683 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
684 if (err)
685 goto done;
687 if (do_merge) {
688 if (fseeko(afile, 0, SEEK_SET) == -1 ||
689 fseeko(oldfile, 0, SEEK_SET) == -1 ||
690 fseeko(tmpfile, 0, SEEK_SET) == -1) {
691 err = got_error_from_errno("fseeko");
692 goto done;
695 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
696 err = got_error_from_errno("asprintf");
697 oldlabel = NULL;
698 goto done;
701 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
702 err = got_error_from_errno("asprintf");
703 newlabel = NULL;
704 goto done;
707 if (asprintf(&anclabel, "commit %s", p->cid) == -1) {
708 err = got_error_from_errno("asprintf");
709 anclabel = NULL;
710 goto done;
713 err = got_opentemp_named(&mergepath, &mergefile, template);
714 if (err)
715 goto done;
716 outpath = mergepath;
717 outfd = fileno(mergefile);
719 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
720 oldfile, oldlabel, anclabel, newlabel,
721 GOT_DIFF_ALGORITHM_PATIENCE);
722 if (err)
723 goto done;
726 if (nop)
727 goto done;
729 if (p->old != NULL && p->new == NULL) {
730 err = got_worktree_patch_schedule_rm(old, repo, worktree,
731 fileindex, patch_delete, pa);
732 goto done;
735 if (fchmod(outfd, mode) == -1) {
736 err = got_error_from_errno2("chmod", tmppath);
737 goto done;
740 if (rename(outpath, newpath) == -1) {
741 if (errno != ENOENT) {
742 err = got_error_from_errno3("rename", outpath,
743 newpath);
744 goto done;
747 err = got_path_dirname(&parent, newpath);
748 if (err != NULL)
749 goto done;
750 err = got_path_mkdir(parent);
751 if (err != NULL)
752 goto done;
753 if (rename(outpath, newpath) == -1) {
754 err = got_error_from_errno3("rename", outpath,
755 newpath);
756 goto done;
760 if (file_renamed) {
761 err = got_worktree_patch_schedule_rm(old, repo, worktree,
762 fileindex, patch_delete, pa);
763 if (err == NULL)
764 err = got_worktree_patch_schedule_add(new, repo,
765 worktree, fileindex, patch_add,
766 pa);
767 if (err)
768 unlink(newpath);
769 } else if (p->old == NULL) {
770 err = got_worktree_patch_schedule_add(new, repo, worktree,
771 fileindex, patch_add, pa);
772 if (err)
773 unlink(newpath);
774 } else if (*overlapcnt != 0)
775 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
776 else
777 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
779 done:
780 free(parent);
781 free(template);
783 if (tmppath != NULL)
784 unlink(tmppath);
785 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
786 err = got_error_from_errno("fclose");
787 free(tmppath);
789 free(oldpath);
790 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
791 err = got_error_from_errno("fclose");
793 if (apath != NULL)
794 unlink(apath);
795 if (afile != NULL && fclose(afile) == EOF && err == NULL)
796 err = got_error_from_errno("fclose");
797 free(apath);
799 if (mergepath != NULL)
800 unlink(mergepath);
801 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
802 err = got_error_from_errno("fclose");
803 free(mergepath);
805 free(newpath);
806 free(oldlabel);
807 free(newlabel);
808 free(anclabel);
809 return err;
812 static void
813 reverse_patch(struct got_patch *p)
815 struct got_patch_hunk *h;
816 size_t i;
817 int tmp;
819 STAILQ_FOREACH(h, &p->head, entries) {
820 tmp = h->old_from;
821 h->old_from = h->new_from;
822 h->new_from = tmp;
824 tmp = h->old_lines;
825 h->old_lines = h->new_lines;
826 h->new_lines = tmp;
828 tmp = h->old_nonl;
829 h->old_nonl = h->new_nonl;
830 h->new_nonl = tmp;
832 for (i = 0; i < h->len; ++i) {
833 if (*h->lines[i] == '+')
834 *h->lines[i] = '-';
835 else if (*h->lines[i] == '-')
836 *h->lines[i] = '+';
841 const struct got_error *
842 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
843 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
844 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
846 const struct got_error *err = NULL, *complete_err = NULL;
847 struct got_fileindex *fileindex = NULL;
848 char *fileindex_path = NULL;
849 char *oldpath, *newpath;
850 struct imsgbuf *ibuf;
851 int imsg_fds[2] = {-1, -1};
852 int overlapcnt, done = 0, failed = 0;
853 pid_t pid;
855 ibuf = calloc(1, sizeof(*ibuf));
856 if (ibuf == NULL) {
857 err = got_error_from_errno("calloc");
858 goto done;
861 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
862 err = got_error_from_errno("socketpair");
863 goto done;
866 pid = fork();
867 if (pid == -1) {
868 err = got_error_from_errno("fork");
869 goto done;
870 } else if (pid == 0) {
871 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
872 NULL);
873 /* not reached */
876 if (close(imsg_fds[1]) == -1) {
877 err = got_error_from_errno("close");
878 goto done;
880 imsg_fds[1] = -1;
881 imsg_init(ibuf, imsg_fds[0]);
883 err = send_patch(ibuf, fd);
884 fd = -1;
885 if (err)
886 goto done;
888 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
889 worktree);
890 if (err)
891 goto done;
893 while (!done && err == NULL) {
894 struct got_patch p;
895 struct patch_args pa;
897 pa.progress_cb = progress_cb;
898 pa.progress_arg = progress_arg;
899 pa.head = &p.head;
901 err = recv_patch(ibuf, &done, &p, strip);
902 if (err || done)
903 break;
905 if (reverse)
906 reverse_patch(&p);
908 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
909 &newpath, worktree, repo, fileindex);
910 if (err == NULL)
911 err = apply_patch(&overlapcnt, worktree, repo,
912 fileindex, oldpath, newpath, &p, nop, &pa,
913 cancel_cb, cancel_arg);
914 if (err != NULL) {
915 failed = 1;
916 /* recoverable errors */
917 if (err->code == GOT_ERR_FILE_STATUS ||
918 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
919 err = report_progress(&pa, p.old, p.new,
920 GOT_STATUS_CANNOT_UPDATE, err);
921 else if (err->code == GOT_ERR_HUNK_FAILED)
922 err = report_progress(&pa, p.old, p.new,
923 GOT_STATUS_CANNOT_UPDATE, NULL);
925 if (overlapcnt != 0)
926 failed = 1;
928 free(oldpath);
929 free(newpath);
930 patch_free(&p);
932 if (err)
933 break;
936 done:
937 if (fileindex != NULL)
938 complete_err = got_worktree_patch_complete(fileindex,
939 fileindex_path);
940 if (complete_err && err == NULL)
941 err = complete_err;
942 free(fileindex_path);
943 if (fd != -1 && close(fd) == -1 && err == NULL)
944 err = got_error_from_errno("close");
945 if (ibuf != NULL)
946 imsg_clear(ibuf);
947 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
948 err = got_error_from_errno("close");
949 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
950 err = got_error_from_errno("close");
951 if (err == NULL && failed)
952 err = got_error(GOT_ERR_PATCH_FAILED);
953 return err;