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_repository.h"
43 #include "got_opentemp.h"
44 #include "got_patch.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_diff.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_sha1.h"
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 struct got_patch_hunk {
55 STAILQ_ENTRY(got_patch_hunk) entries;
56 const struct got_error *err;
57 int offset;
58 int old_nonl;
59 int new_nonl;
60 int old_from;
61 int old_lines;
62 int new_from;
63 int 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 char cid[41];
74 char blob[41];
75 struct got_patch_hunk_head head;
76 };
78 struct patch_args {
79 got_patch_progress_cb progress_cb;
80 void *progress_arg;
81 struct got_patch_hunk_head *head;
82 };
84 static const struct got_error *
85 send_patch(struct imsgbuf *ibuf, int fd)
86 {
87 const struct got_error *err = NULL;
89 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
90 NULL, 0) == -1) {
91 err = got_error_from_errno(
92 "imsg_compose GOT_IMSG_PATCH_FILE");
93 close(fd);
94 return err;
95 }
97 if (imsg_flush(ibuf) == -1) {
98 err = got_error_from_errno("imsg_flush");
99 imsg_clear(ibuf);
102 return err;
105 static void
106 patch_free(struct got_patch *p)
108 struct got_patch_hunk *h;
109 size_t i;
111 while (!STAILQ_EMPTY(&p->head)) {
112 h = STAILQ_FIRST(&p->head);
113 STAILQ_REMOVE_HEAD(&p->head, entries);
115 for (i = 0; i < h->len; ++i)
116 free(h->lines[i]);
117 free(h->lines);
118 free(h);
121 free(p->new);
122 free(p->old);
124 memset(p, 0, sizeof(*p));
125 STAILQ_INIT(&p->head);
128 static const struct got_error *
129 pushline(struct got_patch_hunk *h, const char *line)
131 void *t;
132 size_t newcap;
134 if (h->len == h->cap) {
135 if ((newcap = h->cap * 1.5) == 0)
136 newcap = 16;
137 t = recallocarray(h->lines, h->cap, newcap,
138 sizeof(h->lines[0]));
139 if (t == NULL)
140 return got_error_from_errno("recallocarray");
141 h->lines = t;
142 h->cap = newcap;
145 if ((t = strdup(line)) == NULL)
146 return got_error_from_errno("strdup");
148 h->lines[h->len++] = t;
149 return NULL;
152 static const struct got_error *
153 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
155 const struct got_error *err = NULL;
156 struct imsg imsg;
157 struct got_imsg_patch_hunk hdr;
158 struct got_imsg_patch patch;
159 struct got_patch_hunk *h = NULL;
160 size_t datalen;
161 int lastmode = -1;
163 memset(p, 0, sizeof(*p));
164 STAILQ_INIT(&p->head);
166 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
167 if (err)
168 return err;
169 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
170 *done = 1;
171 goto done;
173 if (imsg.hdr.type != GOT_IMSG_PATCH) {
174 err = got_error(GOT_ERR_PRIVSEP_MSG);
175 goto done;
177 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
178 if (datalen != sizeof(patch)) {
179 err = got_error(GOT_ERR_PRIVSEP_LEN);
180 goto done;
182 memcpy(&patch, imsg.data, sizeof(patch));
184 if (patch.old[sizeof(patch.old)-1] != '\0' ||
185 patch.new[sizeof(patch.new)-1] != '\0' ||
186 patch.cid[sizeof(patch.cid)-1] != '\0' ||
187 patch.blob[sizeof(patch.blob)-1] != '\0') {
188 err = got_error(GOT_ERR_PRIVSEP_LEN);
189 goto done;
192 if (*patch.cid != '\0')
193 strlcpy(p->cid, patch.cid, sizeof(p->cid));
195 if (*patch.blob != '\0')
196 strlcpy(p->blob, patch.blob, sizeof(p->blob));
198 /* automatically set strip=1 for git-style diffs */
199 if (strip == -1 && patch.git &&
200 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
201 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
202 strip = 1;
204 /* prefer the new name if not /dev/null for not git-style diffs */
205 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
206 err = got_path_strip(&p->old, patch.new, strip);
207 if (err)
208 goto done;
209 } else if (*patch.old != '\0') {
210 err = got_path_strip(&p->old, patch.old, strip);
211 if (err)
212 goto done;
215 if (*patch.new != '\0') {
216 err = got_path_strip(&p->new, patch.new, strip);
217 if (err)
218 goto done;
221 if (p->old == NULL && p->new == NULL) {
222 err = got_error(GOT_ERR_PATCH_MALFORMED);
223 goto done;
226 imsg_free(&imsg);
228 for (;;) {
229 char *t;
231 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
232 if (err) {
233 patch_free(p);
234 return err;
237 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
238 switch (imsg.hdr.type) {
239 case GOT_IMSG_PATCH_DONE:
240 if (h != NULL && h->len == 0)
241 err = got_error(GOT_ERR_PATCH_MALFORMED);
242 goto done;
243 case GOT_IMSG_PATCH_HUNK:
244 if (h != NULL &&
245 (h->len == 0 || h->old_nonl || h->new_nonl)) {
246 err = got_error(GOT_ERR_PATCH_MALFORMED);
247 goto done;
249 lastmode = -1;
250 if (datalen != sizeof(hdr)) {
251 err = got_error(GOT_ERR_PRIVSEP_LEN);
252 goto done;
254 memcpy(&hdr, imsg.data, sizeof(hdr));
255 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
256 err = got_error(GOT_ERR_PRIVSEP_LEN);
257 goto done;
259 if ((h = calloc(1, sizeof(*h))) == NULL) {
260 err = got_error_from_errno("calloc");
261 goto done;
263 h->old_from = hdr.oldfrom;
264 h->old_lines = hdr.oldlines;
265 h->new_from = hdr.newfrom;
266 h->new_lines = hdr.newlines;
267 STAILQ_INSERT_TAIL(&p->head, h, entries);
268 break;
269 case GOT_IMSG_PATCH_LINE:
270 if (h == NULL) {
271 err = got_error(GOT_ERR_PRIVSEP_MSG);
272 goto done;
274 t = imsg.data;
275 /* at least one char */
276 if (datalen < 2 || t[datalen-1] != '\0') {
277 err = got_error(GOT_ERR_PRIVSEP_MSG);
278 goto done;
280 if (*t != ' ' && *t != '-' && *t != '+' &&
281 *t != '\\') {
282 err = got_error(GOT_ERR_PRIVSEP_MSG);
283 goto done;
286 if (*t != '\\')
287 err = pushline(h, t);
288 else if (lastmode == '-')
289 h->old_nonl = 1;
290 else if (lastmode == '+')
291 h->new_nonl = 1;
292 else
293 err = got_error(GOT_ERR_PATCH_MALFORMED);
295 if (err)
296 goto done;
298 lastmode = *t;
299 break;
300 default:
301 err = got_error(GOT_ERR_PRIVSEP_MSG);
302 goto done;
305 imsg_free(&imsg);
308 done:
309 if (err)
310 patch_free(p);
312 imsg_free(&imsg);
313 return err;
316 /*
317 * Copy data from orig starting at copypos until pos into tmp.
318 * If pos is -1, copy until EOF.
319 */
320 static const struct got_error *
321 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
323 char buf[BUFSIZ];
324 size_t len, r, w;
326 if (fseeko(orig, copypos, SEEK_SET) == -1)
327 return got_error_from_errno("fseeko");
329 while (pos == -1 || copypos < pos) {
330 len = sizeof(buf);
331 if (pos > 0)
332 len = MIN(len, (size_t)pos - copypos);
333 r = fread(buf, 1, len, orig);
334 if (r != len && ferror(orig))
335 return got_error_from_errno("fread");
336 w = fwrite(buf, 1, r, tmp);
337 if (w != r)
338 return got_error_from_errno("fwrite");
339 copypos += len;
340 if (r != len && feof(orig)) {
341 if (pos == -1)
342 return NULL;
343 return got_error(GOT_ERR_HUNK_FAILED);
346 return NULL;
349 static const struct got_error *
350 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
352 const struct got_error *err = NULL;
353 char *line = NULL;
354 char mode = *h->lines[0];
355 size_t linesize = 0;
356 ssize_t linelen;
357 off_t match = -1;
358 int match_lineno = -1;
360 for (;;) {
361 linelen = getline(&line, &linesize, orig);
362 if (linelen == -1) {
363 if (ferror(orig))
364 err = got_error_from_errno("getline");
365 else if (match == -1)
366 err = got_error(GOT_ERR_HUNK_FAILED);
367 break;
369 if (line[linelen - 1] == '\n')
370 line[linelen - 1] = '\0';
371 (*lineno)++;
373 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
374 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
375 (mode == '+' && *lineno == h->old_from)) {
376 match = ftello(orig);
377 if (match == -1) {
378 err = got_error_from_errno("ftello");
379 break;
381 match -= linelen;
382 match_lineno = (*lineno)-1;
385 if (*lineno >= h->old_from && match != -1)
386 break;
389 if (err == NULL) {
390 *pos = match;
391 *lineno = match_lineno;
392 if (fseeko(orig, match, SEEK_SET) == -1)
393 err = got_error_from_errno("fseeko");
396 free(line);
397 return err;
400 static const struct got_error *
401 test_hunk(FILE *orig, struct got_patch_hunk *h)
403 const struct got_error *err = NULL;
404 char *line = NULL;
405 size_t linesize = 0, i = 0;
406 ssize_t linelen;
408 for (i = 0; i < h->len; ++i) {
409 switch (*h->lines[i]) {
410 case '+':
411 continue;
412 case ' ':
413 case '-':
414 linelen = getline(&line, &linesize, orig);
415 if (linelen == -1) {
416 if (ferror(orig))
417 err = got_error_from_errno("getline");
418 else
419 err = got_error(
420 GOT_ERR_HUNK_FAILED);
421 goto done;
423 if (line[linelen - 1] == '\n')
424 line[linelen - 1] = '\0';
425 if (strcmp(h->lines[i] + 1, line)) {
426 err = got_error(GOT_ERR_HUNK_FAILED);
427 goto done;
429 break;
433 done:
434 free(line);
435 return err;
438 static const struct got_error *
439 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
441 size_t i, new = 0;
443 for (i = 0; i < h->len; ++i) {
444 switch (*h->lines[i]) {
445 case ' ':
446 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
447 return got_error_from_errno("fprintf");
448 /* fallthrough */
449 case '-':
450 (*lineno)++;
451 break;
452 case '+':
453 new++;
454 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
455 return got_error_from_errno("fprintf");
456 if (new != h->new_lines || !h->new_nonl) {
457 if (fprintf(tmp, "\n") < 0)
458 return got_error_from_errno(
459 "fprintf");
461 break;
464 return NULL;
467 static const struct got_error *
468 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
470 const struct got_error *err = NULL;
471 struct got_patch_hunk *h;
472 struct stat sb;
473 int lineno = 0;
474 off_t copypos, pos;
475 char *line = NULL;
476 size_t linesize = 0;
477 ssize_t linelen;
479 if (p->old == NULL) { /* create */
480 h = STAILQ_FIRST(&p->head);
481 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
482 return got_error(GOT_ERR_PATCH_MALFORMED);
483 return apply_hunk(tmp, h, &lineno);
486 if (fstat(fileno(orig), &sb) == -1)
487 return got_error_from_errno("fstat");
488 *mode = sb.st_mode;
490 copypos = 0;
491 STAILQ_FOREACH(h, &p->head, entries) {
492 tryagain:
493 err = locate_hunk(orig, h, &pos, &lineno);
494 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
495 h->err = err;
496 if (err != NULL)
497 return err;
498 err = copy(tmp, orig, copypos, pos);
499 if (err != NULL)
500 return err;
501 copypos = pos;
503 err = test_hunk(orig, h);
504 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
505 /*
506 * try to apply the hunk again starting the search
507 * after the previous partial match.
508 */
509 if (fseeko(orig, pos, SEEK_SET) == -1)
510 return got_error_from_errno("fseeko");
511 linelen = getline(&line, &linesize, orig);
512 if (linelen == -1)
513 return got_error_from_errno("getline");
514 lineno++;
515 goto tryagain;
517 if (err != NULL)
518 return err;
520 if (lineno + 1 != h->old_from)
521 h->offset = lineno + 1 - h->old_from;
523 err = apply_hunk(tmp, h, &lineno);
524 if (err != NULL)
525 return err;
527 copypos = ftello(orig);
528 if (copypos == -1)
529 return got_error_from_errno("ftello");
532 if (p->new == NULL && sb.st_size != copypos) {
533 h = STAILQ_FIRST(&p->head);
534 h->err = got_error(GOT_ERR_HUNK_FAILED);
535 err = h->err;
536 } else if (!feof(orig))
537 err = copy(tmp, orig, copypos, -1);
539 return err;
542 static const struct got_error *
543 report_progress(struct patch_args *pa, const char *old, const char *new,
544 unsigned char status, const struct got_error *orig_error)
546 const struct got_error *err;
547 struct got_patch_hunk *h;
549 err = pa->progress_cb(pa->progress_arg, old, new, status,
550 orig_error, 0, 0, 0, 0, 0, NULL);
551 if (err)
552 return err;
554 STAILQ_FOREACH(h, pa->head, entries) {
555 if (h->offset == 0 && h->err == NULL)
556 continue;
558 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
559 h->old_from, h->old_lines, h->new_from, h->new_lines,
560 h->offset, h->err);
561 if (err)
562 return err;
565 return NULL;
568 static const struct got_error *
569 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
570 const char *path)
572 return report_progress(arg, path, NULL, status, NULL);
575 static const struct got_error *
576 patch_add(void *arg, unsigned char status, const char *path)
578 return report_progress(arg, NULL, path, status, NULL);
581 static const struct got_error *
582 open_blob(char **path, FILE **fp, const char *blobid,
583 struct got_repository *repo)
585 const struct got_error *err = NULL;
586 struct got_blob_object *blob = NULL;
587 struct got_object_id id, *idptr, *matched_id = NULL;
588 int fd = -1;
590 *fp = NULL;
591 *path = NULL;
593 if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
594 err = got_repo_match_object_id(&matched_id, NULL, blobid,
595 GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
596 repo);
597 if (err)
598 return err;
599 idptr = matched_id;
600 } else {
601 if (!got_parse_sha1_digest(id.sha1, blobid))
602 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
603 idptr = &id;
606 fd = got_opentempfd();
607 if (fd == -1) {
608 err = got_error_from_errno("got_opentempfd");
609 goto done;
612 err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
613 if (err)
614 goto done;
616 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
617 if (err)
618 goto done;
620 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
621 if (err)
622 goto done;
624 done:
625 if (fd != -1 && close(fd) == -1 && err == NULL)
626 err = got_error_from_errno("close");
627 if (blob)
628 got_object_blob_close(blob);
629 if (matched_id != NULL)
630 free(matched_id);
631 if (err) {
632 if (*fp != NULL)
633 fclose(*fp);
634 if (*path != NULL)
635 unlink(*path);
636 free(*path);
637 *fp = NULL;
638 *path = NULL;
640 return err;
643 static const struct got_error *
644 apply_patch(int *overlapcnt, struct got_worktree *worktree,
645 struct got_repository *repo, struct got_fileindex *fileindex,
646 const char *old, const char *new, struct got_patch *p, int nop,
647 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
649 const struct got_error *err = NULL;
650 int do_merge = 0, file_renamed = 0;
651 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
652 char *oldpath = NULL, *newpath = NULL;
653 char *tmppath = NULL, *template = NULL, *parent = NULL;
654 char *apath = NULL, *mergepath = NULL;
655 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
656 int outfd;
657 const char *outpath;
658 mode_t mode = GOT_DEFAULT_FILE_MODE;
660 *overlapcnt = 0;
662 /* don't run the diff3 merge on creations/deletions */
663 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
664 err = open_blob(&apath, &afile, p->blob, repo);
665 /*
666 * ignore failures to open this blob, we might have
667 * parsed gibberish.
668 */
669 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
670 err->code != GOT_ERR_NO_OBJ)
671 return err;
672 else if (err == NULL)
673 do_merge = 1;
674 err = NULL;
677 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
678 old) == -1) {
679 err = got_error_from_errno("asprintf");
680 goto done;
683 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
684 new) == -1) {
685 err = got_error_from_errno("asprintf");
686 goto done;
689 file_renamed = strcmp(oldpath, newpath);
691 if (asprintf(&template, "%s/got-patch",
692 got_worktree_get_root_path(worktree)) == -1) {
693 err = got_error_from_errno(template);
694 goto done;
697 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
698 err = got_error_from_errno2("open", oldpath);
699 goto done;
702 err = got_opentemp_named(&tmppath, &tmpfile, template);
703 if (err)
704 goto done;
705 outpath = tmppath;
706 outfd = fileno(tmpfile);
707 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
708 if (err)
709 goto done;
711 if (do_merge) {
712 const char *type, *id;
714 if (fseeko(afile, 0, SEEK_SET) == -1 ||
715 fseeko(oldfile, 0, SEEK_SET) == -1 ||
716 fseeko(tmpfile, 0, SEEK_SET) == -1) {
717 err = got_error_from_errno("fseeko");
718 goto done;
721 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
722 err = got_error_from_errno("asprintf");
723 oldlabel = NULL;
724 goto done;
727 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
728 err = got_error_from_errno("asprintf");
729 newlabel = NULL;
730 goto done;
733 if (*p->cid != '\0') {
734 type = "commit";
735 id = p->cid;
736 } else {
737 type = "blob";
738 id = p->blob;
741 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
742 err = got_error_from_errno("asprintf");
743 anclabel = NULL;
744 goto done;
747 err = got_opentemp_named(&mergepath, &mergefile, template);
748 if (err)
749 goto done;
750 outpath = mergepath;
751 outfd = fileno(mergefile);
753 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
754 oldfile, oldlabel, anclabel, newlabel,
755 GOT_DIFF_ALGORITHM_PATIENCE);
756 if (err)
757 goto done;
760 if (nop)
761 goto done;
763 if (p->old != NULL && p->new == NULL) {
764 err = got_worktree_patch_schedule_rm(old, repo, worktree,
765 fileindex, patch_delete, pa);
766 goto done;
769 if (fchmod(outfd, mode) == -1) {
770 err = got_error_from_errno2("chmod", tmppath);
771 goto done;
774 if (rename(outpath, newpath) == -1) {
775 if (errno != ENOENT) {
776 err = got_error_from_errno3("rename", outpath,
777 newpath);
778 goto done;
781 err = got_path_dirname(&parent, newpath);
782 if (err != NULL)
783 goto done;
784 err = got_path_mkdir(parent);
785 if (err != NULL)
786 goto done;
787 if (rename(outpath, newpath) == -1) {
788 err = got_error_from_errno3("rename", outpath,
789 newpath);
790 goto done;
794 if (file_renamed) {
795 err = got_worktree_patch_schedule_rm(old, repo, worktree,
796 fileindex, patch_delete, pa);
797 if (err == NULL)
798 err = got_worktree_patch_schedule_add(new, repo,
799 worktree, fileindex, patch_add,
800 pa);
801 if (err)
802 unlink(newpath);
803 } else if (p->old == NULL) {
804 err = got_worktree_patch_schedule_add(new, repo, worktree,
805 fileindex, patch_add, pa);
806 if (err)
807 unlink(newpath);
808 } else if (*overlapcnt != 0)
809 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
810 else if (do_merge)
811 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
812 else
813 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
815 done:
816 free(parent);
817 free(template);
819 if (tmppath != NULL)
820 unlink(tmppath);
821 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
822 err = got_error_from_errno("fclose");
823 free(tmppath);
825 free(oldpath);
826 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
827 err = got_error_from_errno("fclose");
829 if (apath != NULL)
830 unlink(apath);
831 if (afile != NULL && fclose(afile) == EOF && err == NULL)
832 err = got_error_from_errno("fclose");
833 free(apath);
835 if (mergepath != NULL)
836 unlink(mergepath);
837 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
838 err = got_error_from_errno("fclose");
839 free(mergepath);
841 free(newpath);
842 free(oldlabel);
843 free(newlabel);
844 free(anclabel);
845 return err;
848 static void
849 reverse_patch(struct got_patch *p)
851 struct got_patch_hunk *h;
852 size_t i;
853 int tmp;
855 STAILQ_FOREACH(h, &p->head, entries) {
856 tmp = h->old_from;
857 h->old_from = h->new_from;
858 h->new_from = tmp;
860 tmp = h->old_lines;
861 h->old_lines = h->new_lines;
862 h->new_lines = tmp;
864 tmp = h->old_nonl;
865 h->old_nonl = h->new_nonl;
866 h->new_nonl = tmp;
868 for (i = 0; i < h->len; ++i) {
869 if (*h->lines[i] == '+')
870 *h->lines[i] = '-';
871 else if (*h->lines[i] == '-')
872 *h->lines[i] = '+';
877 const struct got_error *
878 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
879 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
880 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
882 const struct got_error *err = NULL, *complete_err = NULL;
883 struct got_fileindex *fileindex = NULL;
884 char *fileindex_path = NULL;
885 char *oldpath, *newpath;
886 struct imsgbuf *ibuf;
887 int imsg_fds[2] = {-1, -1};
888 int overlapcnt, done = 0, failed = 0;
889 pid_t pid;
891 ibuf = calloc(1, sizeof(*ibuf));
892 if (ibuf == NULL) {
893 err = got_error_from_errno("calloc");
894 goto done;
897 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
898 err = got_error_from_errno("socketpair");
899 goto done;
902 pid = fork();
903 if (pid == -1) {
904 err = got_error_from_errno("fork");
905 goto done;
906 } else if (pid == 0) {
907 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
908 NULL);
909 /* not reached */
912 if (close(imsg_fds[1]) == -1) {
913 err = got_error_from_errno("close");
914 goto done;
916 imsg_fds[1] = -1;
917 imsg_init(ibuf, imsg_fds[0]);
919 err = send_patch(ibuf, fd);
920 fd = -1;
921 if (err)
922 goto done;
924 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
925 worktree);
926 if (err)
927 goto done;
929 while (!done && err == NULL) {
930 struct got_patch p;
931 struct patch_args pa;
933 pa.progress_cb = progress_cb;
934 pa.progress_arg = progress_arg;
935 pa.head = &p.head;
937 err = recv_patch(ibuf, &done, &p, strip);
938 if (err || done)
939 break;
941 if (reverse)
942 reverse_patch(&p);
944 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
945 &newpath, worktree, repo, fileindex);
946 if (err == NULL)
947 err = apply_patch(&overlapcnt, worktree, repo,
948 fileindex, oldpath, newpath, &p, nop, &pa,
949 cancel_cb, cancel_arg);
950 if (err != NULL) {
951 failed = 1;
952 /* recoverable errors */
953 if (err->code == GOT_ERR_FILE_STATUS ||
954 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
955 err = report_progress(&pa, p.old, p.new,
956 GOT_STATUS_CANNOT_UPDATE, err);
957 else if (err->code == GOT_ERR_HUNK_FAILED)
958 err = report_progress(&pa, p.old, p.new,
959 GOT_STATUS_CANNOT_UPDATE, NULL);
961 if (overlapcnt != 0)
962 failed = 1;
964 free(oldpath);
965 free(newpath);
966 patch_free(&p);
968 if (err)
969 break;
972 done:
973 if (fileindex != NULL)
974 complete_err = got_worktree_patch_complete(fileindex,
975 fileindex_path);
976 if (complete_err && err == NULL)
977 err = complete_err;
978 free(fileindex_path);
979 if (fd != -1 && close(fd) == -1 && err == NULL)
980 err = got_error_from_errno("close");
981 if (ibuf != NULL)
982 imsg_clear(ibuf);
983 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
984 err = got_error_from_errno("close");
985 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
986 err = got_error_from_errno("close");
987 if (err == NULL && failed)
988 err = got_error(GOT_ERR_PATCH_FAILED);
989 return err;