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')
196 strlcpy(p->cid, patch.cid, sizeof(p->cid));
198 if (*patch.blob != '\0')
199 strlcpy(p->blob, patch.blob, sizeof(p->blob));
201 /* automatically set strip=1 for git-style diffs */
202 if (strip == -1 && patch.git &&
203 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
204 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
205 strip = 1;
207 /* prefer the new name if not /dev/null for not git-style diffs */
208 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
209 err = got_path_strip(&p->old, patch.new, strip);
210 if (err)
211 goto done;
212 } else if (*patch.old != '\0') {
213 err = got_path_strip(&p->old, patch.old, strip);
214 if (err)
215 goto done;
218 if (*patch.new != '\0') {
219 err = got_path_strip(&p->new, patch.new, strip);
220 if (err)
221 goto done;
224 if (p->old == NULL && p->new == NULL) {
225 err = got_error(GOT_ERR_PATCH_MALFORMED);
226 goto done;
229 imsg_free(&imsg);
231 for (;;) {
232 char *t;
234 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
235 if (err) {
236 patch_free(p);
237 return err;
240 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
241 switch (imsg.hdr.type) {
242 case GOT_IMSG_PATCH_DONE:
243 if (h != NULL && h->len == 0)
244 err = got_error(GOT_ERR_PATCH_MALFORMED);
245 goto done;
246 case GOT_IMSG_PATCH_HUNK:
247 if (h != NULL &&
248 (h->len == 0 || h->old_nonl || h->new_nonl)) {
249 err = got_error(GOT_ERR_PATCH_MALFORMED);
250 goto done;
252 lastmode = -1;
253 if (datalen != sizeof(hdr)) {
254 err = got_error(GOT_ERR_PRIVSEP_LEN);
255 goto done;
257 memcpy(&hdr, imsg.data, sizeof(hdr));
258 if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
259 err = got_error(GOT_ERR_PRIVSEP_LEN);
260 goto done;
262 if ((h = calloc(1, sizeof(*h))) == NULL) {
263 err = got_error_from_errno("calloc");
264 goto done;
266 h->old_from = hdr.oldfrom;
267 h->old_lines = hdr.oldlines;
268 h->new_from = hdr.newfrom;
269 h->new_lines = hdr.newlines;
270 STAILQ_INSERT_TAIL(&p->head, h, entries);
271 break;
272 case GOT_IMSG_PATCH_LINE:
273 if (h == NULL) {
274 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 goto done;
277 t = imsg.data;
278 /* at least one char */
279 if (datalen < 2 || t[datalen-1] != '\0') {
280 err = got_error(GOT_ERR_PRIVSEP_MSG);
281 goto done;
283 if (*t != ' ' && *t != '-' && *t != '+' &&
284 *t != '\\') {
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 goto done;
289 if (*t != '\\')
290 err = pushline(h, t);
291 else if (lastmode == '-')
292 h->old_nonl = 1;
293 else if (lastmode == '+')
294 h->new_nonl = 1;
295 else
296 err = got_error(GOT_ERR_PATCH_MALFORMED);
298 if (err)
299 goto done;
301 lastmode = *t;
302 break;
303 default:
304 err = got_error(GOT_ERR_PRIVSEP_MSG);
305 goto done;
308 imsg_free(&imsg);
311 done:
312 if (err)
313 patch_free(p);
315 imsg_free(&imsg);
316 return err;
319 /*
320 * Copy data from orig starting at copypos until pos into tmp.
321 * If pos is -1, copy until EOF.
322 */
323 static const struct got_error *
324 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
326 char buf[BUFSIZ];
327 size_t len, r, w;
329 if (fseeko(orig, copypos, SEEK_SET) == -1)
330 return got_error_from_errno("fseeko");
332 while (pos == -1 || copypos < pos) {
333 len = sizeof(buf);
334 if (pos > 0)
335 len = MIN(len, (size_t)pos - copypos);
336 r = fread(buf, 1, len, orig);
337 if (r != len && ferror(orig))
338 return got_error_from_errno("fread");
339 w = fwrite(buf, 1, r, tmp);
340 if (w != r)
341 return got_error_from_errno("fwrite");
342 copypos += len;
343 if (r != len && feof(orig)) {
344 if (pos == -1)
345 return NULL;
346 return got_error(GOT_ERR_HUNK_FAILED);
349 return NULL;
352 static const struct got_error *
353 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
355 const struct got_error *err = NULL;
356 char *line = NULL;
357 char mode = *h->lines[0];
358 size_t linesize = 0;
359 ssize_t linelen;
360 off_t match = -1;
361 int match_lineno = -1;
363 for (;;) {
364 linelen = getline(&line, &linesize, orig);
365 if (linelen == -1) {
366 if (ferror(orig))
367 err = got_error_from_errno("getline");
368 else if (match == -1)
369 err = got_error(GOT_ERR_HUNK_FAILED);
370 break;
372 if (line[linelen - 1] == '\n')
373 line[linelen - 1] = '\0';
374 (*lineno)++;
376 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
377 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
378 (mode == '+' && *lineno == h->old_from)) {
379 match = ftello(orig);
380 if (match == -1) {
381 err = got_error_from_errno("ftello");
382 break;
384 match -= linelen;
385 match_lineno = (*lineno)-1;
388 if (*lineno >= h->old_from && match != -1)
389 break;
392 if (err == NULL) {
393 *pos = match;
394 *lineno = match_lineno;
395 if (fseeko(orig, match, SEEK_SET) == -1)
396 err = got_error_from_errno("fseeko");
399 free(line);
400 return err;
403 static const struct got_error *
404 test_hunk(FILE *orig, struct got_patch_hunk *h)
406 const struct got_error *err = NULL;
407 char *line = NULL;
408 size_t linesize = 0, i = 0;
409 ssize_t linelen;
411 for (i = 0; i < h->len; ++i) {
412 switch (*h->lines[i]) {
413 case '+':
414 continue;
415 case ' ':
416 case '-':
417 linelen = getline(&line, &linesize, orig);
418 if (linelen == -1) {
419 if (ferror(orig))
420 err = got_error_from_errno("getline");
421 else
422 err = got_error(
423 GOT_ERR_HUNK_FAILED);
424 goto done;
426 if (line[linelen - 1] == '\n')
427 line[linelen - 1] = '\0';
428 if (strcmp(h->lines[i] + 1, line)) {
429 err = got_error(GOT_ERR_HUNK_FAILED);
430 goto done;
432 break;
436 done:
437 free(line);
438 return err;
441 static const struct got_error *
442 apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
444 size_t i, new = 0;
446 for (i = 0; i < h->len; ++i) {
447 switch (*h->lines[i]) {
448 case ' ':
449 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
450 return got_error_from_errno("fprintf");
451 /* fallthrough */
452 case '-':
453 (*lineno)++;
454 break;
455 case '+':
456 new++;
457 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
458 return got_error_from_errno("fprintf");
459 if (new != h->new_lines || !h->new_nonl) {
460 if (fprintf(tmp, "\n") < 0)
461 return got_error_from_errno(
462 "fprintf");
464 break;
467 return NULL;
470 static const struct got_error *
471 patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
473 const struct got_error *err = NULL;
474 struct got_patch_hunk *h;
475 struct stat sb;
476 int lineno = 0;
477 off_t copypos, pos;
478 char *line = NULL;
479 size_t linesize = 0;
480 ssize_t linelen;
482 if (p->old == NULL) { /* create */
483 h = STAILQ_FIRST(&p->head);
484 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
485 return got_error(GOT_ERR_PATCH_MALFORMED);
486 return apply_hunk(tmp, h, &lineno);
489 if (fstat(fileno(orig), &sb) == -1)
490 return got_error_from_errno("fstat");
491 *mode = sb.st_mode;
493 copypos = 0;
494 STAILQ_FOREACH(h, &p->head, entries) {
495 tryagain:
496 err = locate_hunk(orig, h, &pos, &lineno);
497 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
498 h->err = err;
499 if (err != NULL)
500 return err;
501 err = copy(tmp, orig, copypos, pos);
502 if (err != NULL)
503 return err;
504 copypos = pos;
506 err = test_hunk(orig, h);
507 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
508 /*
509 * try to apply the hunk again starting the search
510 * after the previous partial match.
511 */
512 if (fseeko(orig, pos, SEEK_SET) == -1)
513 return got_error_from_errno("fseeko");
514 linelen = getline(&line, &linesize, orig);
515 if (linelen == -1)
516 return got_error_from_errno("getline");
517 lineno++;
518 goto tryagain;
520 if (err != NULL)
521 return err;
523 if (lineno + 1 != h->old_from)
524 h->offset = lineno + 1 - h->old_from;
526 err = apply_hunk(tmp, h, &lineno);
527 if (err != NULL)
528 return err;
530 copypos = ftello(orig);
531 if (copypos == -1)
532 return got_error_from_errno("ftello");
535 if (p->new == NULL && sb.st_size != copypos) {
536 h = STAILQ_FIRST(&p->head);
537 h->err = got_error(GOT_ERR_HUNK_FAILED);
538 err = h->err;
539 } else if (!feof(orig))
540 err = copy(tmp, orig, copypos, -1);
542 return err;
545 static const struct got_error *
546 report_progress(struct patch_args *pa, const char *old, const char *new,
547 unsigned char status, const struct got_error *orig_error)
549 const struct got_error *err;
550 struct got_patch_hunk *h;
552 err = pa->progress_cb(pa->progress_arg, old, new, status,
553 orig_error, 0, 0, 0, 0, 0, NULL);
554 if (err)
555 return err;
557 STAILQ_FOREACH(h, pa->head, entries) {
558 if (h->offset == 0 && h->err == NULL)
559 continue;
561 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
562 h->old_from, h->old_lines, h->new_from, h->new_lines,
563 h->offset, h->err);
564 if (err)
565 return err;
568 return NULL;
571 static const struct got_error *
572 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
573 const char *path)
575 return report_progress(arg, path, NULL, status, NULL);
578 static const struct got_error *
579 patch_add(void *arg, unsigned char status, const char *path)
581 return report_progress(arg, NULL, path, status, NULL);
584 static const struct got_error *
585 open_blob(char **path, FILE **fp, const char *blobid,
586 struct got_repository *repo)
588 const struct got_error *err = NULL;
589 struct got_blob_object *blob = NULL;
590 struct got_object_id id;
592 *fp = NULL;
593 *path = NULL;
595 if (!got_parse_sha1_digest(id.sha1, blobid))
596 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
598 err = got_object_open_as_blob(&blob, repo, &id, 8192);
599 if (err)
600 goto done;
602 err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
603 if (err)
604 goto done;
606 err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
607 if (err)
608 goto done;
610 done:
611 if (blob)
612 got_object_blob_close(blob);
613 if (err) {
614 if (*fp != NULL)
615 fclose(*fp);
616 if (*path != NULL)
617 unlink(*path);
618 free(*path);
619 *fp = NULL;
620 *path = NULL;
622 return err;
625 static const struct got_error *
626 apply_patch(int *overlapcnt, struct got_worktree *worktree,
627 struct got_repository *repo, struct got_fileindex *fileindex,
628 const char *old, const char *new, struct got_patch *p, int nop,
629 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
631 const struct got_error *err = NULL;
632 int do_merge = 0, file_renamed = 0;
633 char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
634 char *oldpath = NULL, *newpath = NULL;
635 char *tmppath = NULL, *template = NULL, *parent = NULL;
636 char *apath = NULL, *mergepath = NULL;
637 FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
638 int outfd;
639 const char *outpath;
640 mode_t mode = GOT_DEFAULT_FILE_MODE;
642 *overlapcnt = 0;
644 /* don't run the diff3 merge on creations/deletions */
645 if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
646 err = open_blob(&apath, &afile, p->blob, repo);
647 /*
648 * ignore failures to open this blob, we might have
649 * parsed gibberish.
650 */
651 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
652 return err;
653 else if (err == NULL)
654 do_merge = 1;
655 err = NULL;
658 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
659 old) == -1) {
660 err = got_error_from_errno("asprintf");
661 goto done;
664 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
665 new) == -1) {
666 err = got_error_from_errno("asprintf");
667 goto done;
670 file_renamed = strcmp(oldpath, newpath);
672 if (asprintf(&template, "%s/got-patch",
673 got_worktree_get_root_path(worktree)) == -1) {
674 err = got_error_from_errno(template);
675 goto done;
678 if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
679 err = got_error_from_errno2("open", oldpath);
680 goto done;
683 err = got_opentemp_named(&tmppath, &tmpfile, template);
684 if (err)
685 goto done;
686 outpath = tmppath;
687 outfd = fileno(tmpfile);
688 err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
689 if (err)
690 goto done;
692 if (do_merge) {
693 const char *type, *id;
695 if (fseeko(afile, 0, SEEK_SET) == -1 ||
696 fseeko(oldfile, 0, SEEK_SET) == -1 ||
697 fseeko(tmpfile, 0, SEEK_SET) == -1) {
698 err = got_error_from_errno("fseeko");
699 goto done;
702 if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
703 err = got_error_from_errno("asprintf");
704 oldlabel = NULL;
705 goto done;
708 if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
709 err = got_error_from_errno("asprintf");
710 newlabel = NULL;
711 goto done;
714 if (*p->cid != '\0') {
715 type = "commit";
716 id = p->cid;
717 } else {
718 type = "blob";
719 id = p->blob;
722 if (asprintf(&anclabel, "%s %s", type, id) == -1) {
723 err = got_error_from_errno("asprintf");
724 anclabel = NULL;
725 goto done;
728 err = got_opentemp_named(&mergepath, &mergefile, template);
729 if (err)
730 goto done;
731 outpath = mergepath;
732 outfd = fileno(mergefile);
734 err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
735 oldfile, oldlabel, anclabel, newlabel,
736 GOT_DIFF_ALGORITHM_PATIENCE);
737 if (err)
738 goto done;
741 if (nop)
742 goto done;
744 if (p->old != NULL && p->new == NULL) {
745 err = got_worktree_patch_schedule_rm(old, repo, worktree,
746 fileindex, patch_delete, pa);
747 goto done;
750 if (fchmod(outfd, mode) == -1) {
751 err = got_error_from_errno2("chmod", tmppath);
752 goto done;
755 if (rename(outpath, newpath) == -1) {
756 if (errno != ENOENT) {
757 err = got_error_from_errno3("rename", outpath,
758 newpath);
759 goto done;
762 err = got_path_dirname(&parent, newpath);
763 if (err != NULL)
764 goto done;
765 err = got_path_mkdir(parent);
766 if (err != NULL)
767 goto done;
768 if (rename(outpath, newpath) == -1) {
769 err = got_error_from_errno3("rename", outpath,
770 newpath);
771 goto done;
775 if (file_renamed) {
776 err = got_worktree_patch_schedule_rm(old, repo, worktree,
777 fileindex, patch_delete, pa);
778 if (err == NULL)
779 err = got_worktree_patch_schedule_add(new, repo,
780 worktree, fileindex, patch_add,
781 pa);
782 if (err)
783 unlink(newpath);
784 } else if (p->old == NULL) {
785 err = got_worktree_patch_schedule_add(new, repo, worktree,
786 fileindex, patch_add, pa);
787 if (err)
788 unlink(newpath);
789 } else if (*overlapcnt != 0)
790 err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
791 else if (do_merge)
792 err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
793 else
794 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
796 done:
797 free(parent);
798 free(template);
800 if (tmppath != NULL)
801 unlink(tmppath);
802 if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
803 err = got_error_from_errno("fclose");
804 free(tmppath);
806 free(oldpath);
807 if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
808 err = got_error_from_errno("fclose");
810 if (apath != NULL)
811 unlink(apath);
812 if (afile != NULL && fclose(afile) == EOF && err == NULL)
813 err = got_error_from_errno("fclose");
814 free(apath);
816 if (mergepath != NULL)
817 unlink(mergepath);
818 if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
819 err = got_error_from_errno("fclose");
820 free(mergepath);
822 free(newpath);
823 free(oldlabel);
824 free(newlabel);
825 free(anclabel);
826 return err;
829 static void
830 reverse_patch(struct got_patch *p)
832 struct got_patch_hunk *h;
833 size_t i;
834 int tmp;
836 STAILQ_FOREACH(h, &p->head, entries) {
837 tmp = h->old_from;
838 h->old_from = h->new_from;
839 h->new_from = tmp;
841 tmp = h->old_lines;
842 h->old_lines = h->new_lines;
843 h->new_lines = tmp;
845 tmp = h->old_nonl;
846 h->old_nonl = h->new_nonl;
847 h->new_nonl = tmp;
849 for (i = 0; i < h->len; ++i) {
850 if (*h->lines[i] == '+')
851 *h->lines[i] = '-';
852 else if (*h->lines[i] == '-')
853 *h->lines[i] = '+';
858 const struct got_error *
859 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
860 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
861 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
863 const struct got_error *err = NULL, *complete_err = NULL;
864 struct got_fileindex *fileindex = NULL;
865 char *fileindex_path = NULL;
866 char *oldpath, *newpath;
867 struct imsgbuf *ibuf;
868 int imsg_fds[2] = {-1, -1};
869 int overlapcnt, done = 0, failed = 0;
870 pid_t pid;
872 ibuf = calloc(1, sizeof(*ibuf));
873 if (ibuf == NULL) {
874 err = got_error_from_errno("calloc");
875 goto done;
878 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
879 err = got_error_from_errno("socketpair");
880 goto done;
883 pid = fork();
884 if (pid == -1) {
885 err = got_error_from_errno("fork");
886 goto done;
887 } else if (pid == 0) {
888 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
889 NULL);
890 /* not reached */
893 if (close(imsg_fds[1]) == -1) {
894 err = got_error_from_errno("close");
895 goto done;
897 imsg_fds[1] = -1;
898 imsg_init(ibuf, imsg_fds[0]);
900 err = send_patch(ibuf, fd);
901 fd = -1;
902 if (err)
903 goto done;
905 err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
906 worktree);
907 if (err)
908 goto done;
910 while (!done && err == NULL) {
911 struct got_patch p;
912 struct patch_args pa;
914 pa.progress_cb = progress_cb;
915 pa.progress_arg = progress_arg;
916 pa.head = &p.head;
918 err = recv_patch(ibuf, &done, &p, strip);
919 if (err || done)
920 break;
922 if (reverse)
923 reverse_patch(&p);
925 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
926 &newpath, worktree, repo, fileindex);
927 if (err == NULL)
928 err = apply_patch(&overlapcnt, worktree, repo,
929 fileindex, oldpath, newpath, &p, nop, &pa,
930 cancel_cb, cancel_arg);
931 if (err != NULL) {
932 failed = 1;
933 /* recoverable errors */
934 if (err->code == GOT_ERR_FILE_STATUS ||
935 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
936 err = report_progress(&pa, p.old, p.new,
937 GOT_STATUS_CANNOT_UPDATE, err);
938 else if (err->code == GOT_ERR_HUNK_FAILED)
939 err = report_progress(&pa, p.old, p.new,
940 GOT_STATUS_CANNOT_UPDATE, NULL);
942 if (overlapcnt != 0)
943 failed = 1;
945 free(oldpath);
946 free(newpath);
947 patch_free(&p);
949 if (err)
950 break;
953 done:
954 if (fileindex != NULL)
955 complete_err = got_worktree_patch_complete(fileindex,
956 fileindex_path);
957 if (complete_err && err == NULL)
958 err = complete_err;
959 free(fileindex_path);
960 if (fd != -1 && close(fd) == -1 && err == NULL)
961 err = got_error_from_errno("close");
962 if (ibuf != NULL)
963 imsg_clear(ibuf);
964 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
965 err = got_error_from_errno("close");
966 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
967 err = got_error_from_errno("close");
968 if (err == NULL && failed)
969 err = got_error(GOT_ERR_PATCH_FAILED);
970 return err;