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_opentemp.h"
43 #include "got_patch.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 struct got_patch_hunk {
52 STAILQ_ENTRY(got_patch_hunk) entries;
53 const struct got_error *err;
54 long offset;
55 int old_nonl;
56 int new_nonl;
57 long old_from;
58 long old_lines;
59 long new_from;
60 long new_lines;
61 size_t len;
62 size_t cap;
63 char **lines;
64 };
66 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
67 struct got_patch {
68 char *old;
69 char *new;
70 struct got_patch_hunk_head head;
71 };
73 struct patch_args {
74 got_patch_progress_cb progress_cb;
75 void *progress_arg;
76 struct got_patch_hunk_head *head;
77 };
79 static const struct got_error *
80 send_patch(struct imsgbuf *ibuf, int fd)
81 {
82 const struct got_error *err = NULL;
84 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
85 NULL, 0) == -1) {
86 err = got_error_from_errno(
87 "imsg_compose GOT_IMSG_PATCH_FILE");
88 close(fd);
89 return err;
90 }
92 if (imsg_flush(ibuf) == -1) {
93 err = got_error_from_errno("imsg_flush");
94 imsg_clear(ibuf);
95 }
97 return err;
98 }
100 static void
101 patch_free(struct got_patch *p)
103 struct got_patch_hunk *h;
104 size_t i;
106 while (!STAILQ_EMPTY(&p->head)) {
107 h = STAILQ_FIRST(&p->head);
108 STAILQ_REMOVE_HEAD(&p->head, entries);
110 for (i = 0; i < h->len; ++i)
111 free(h->lines[i]);
112 free(h->lines);
113 free(h);
116 free(p->new);
117 free(p->old);
120 static const struct got_error *
121 pushline(struct got_patch_hunk *h, const char *line)
123 void *t;
124 size_t newcap;
126 if (h->len == h->cap) {
127 if ((newcap = h->cap * 1.5) == 0)
128 newcap = 16;
129 t = recallocarray(h->lines, h->cap, newcap,
130 sizeof(h->lines[0]));
131 if (t == NULL)
132 return got_error_from_errno("recallocarray");
133 h->lines = t;
134 h->cap = newcap;
137 if ((t = strdup(line)) == NULL)
138 return got_error_from_errno("strdup");
140 h->lines[h->len++] = t;
141 return NULL;
144 static const struct got_error *
145 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
147 const struct got_error *err = NULL;
148 struct imsg imsg;
149 struct got_imsg_patch_hunk hdr;
150 struct got_imsg_patch patch;
151 struct got_patch_hunk *h = NULL;
152 size_t datalen;
153 int lastmode = -1;
155 memset(p, 0, sizeof(*p));
156 STAILQ_INIT(&p->head);
158 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
159 if (err)
160 return err;
161 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
162 *done = 1;
163 goto done;
165 if (imsg.hdr.type != GOT_IMSG_PATCH) {
166 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 goto done;
169 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
170 if (datalen != sizeof(patch)) {
171 err = got_error(GOT_ERR_PRIVSEP_LEN);
172 goto done;
174 memcpy(&patch, imsg.data, sizeof(patch));
176 /* automatically set strip=1 for git-style diffs */
177 if (strip == -1 && patch.git &&
178 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
179 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
180 strip = 1;
182 /* prefer the new name if not /dev/null for not git-style diffs */
183 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
184 err = got_path_strip(&p->old, patch.new, strip);
185 if (err)
186 goto done;
187 } else if (*patch.old != '\0') {
188 err = got_path_strip(&p->old, patch.old, strip);
189 if (err)
190 goto done;
193 if (*patch.new != '\0') {
194 err = got_path_strip(&p->new, patch.new, strip);
195 if (err)
196 goto done;
199 if (p->old == NULL && p->new == NULL) {
200 err = got_error(GOT_ERR_PATCH_MALFORMED);
201 goto done;
204 imsg_free(&imsg);
206 for (;;) {
207 char *t;
209 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
210 if (err)
211 return err;
213 switch (imsg.hdr.type) {
214 case GOT_IMSG_PATCH_DONE:
215 goto done;
216 case GOT_IMSG_PATCH_HUNK:
217 if (h != NULL && (h->old_nonl || h->new_nonl)) {
218 err = got_error(GOT_ERR_PATCH_MALFORMED);
219 goto done;
221 lastmode = -1;
222 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
223 if (datalen != sizeof(hdr)) {
224 err = got_error(GOT_ERR_PRIVSEP_LEN);
225 goto done;
227 memcpy(&hdr, imsg.data, sizeof(hdr));
228 if ((h = calloc(1, sizeof(*h))) == NULL) {
229 err = got_error_from_errno("calloc");
230 goto done;
232 h->old_from = hdr.oldfrom;
233 h->old_lines = hdr.oldlines;
234 h->new_from = hdr.newfrom;
235 h->new_lines = hdr.newlines;
236 STAILQ_INSERT_TAIL(&p->head, h, entries);
237 break;
238 case GOT_IMSG_PATCH_LINE:
239 if (h == NULL) {
240 err = got_error(GOT_ERR_PRIVSEP_MSG);
241 goto done;
243 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
244 t = imsg.data;
245 /* at least one char */
246 if (datalen < 2 || t[datalen-1] != '\0') {
247 err = got_error(GOT_ERR_PRIVSEP_MSG);
248 goto done;
250 if (*t != ' ' && *t != '-' && *t != '+' &&
251 *t != '\\') {
252 err = got_error(GOT_ERR_PRIVSEP_MSG);
253 goto done;
256 if (*t != '\\')
257 err = pushline(h, t);
258 else if (lastmode == '-')
259 h->old_nonl = 1;
260 else if (lastmode == '+')
261 h->new_nonl = 1;
262 else
263 err = got_error(GOT_ERR_PATCH_MALFORMED);
265 if (err)
266 goto done;
268 lastmode = *t;
269 break;
270 default:
271 err = got_error(GOT_ERR_PRIVSEP_MSG);
272 goto done;
275 imsg_free(&imsg);
278 done:
279 imsg_free(&imsg);
280 return err;
283 /*
284 * Copy data from orig starting at copypos until pos into tmp.
285 * If pos is -1, copy until EOF.
286 */
287 static const struct got_error *
288 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
290 char buf[BUFSIZ];
291 size_t len, r, w;
293 if (fseek(orig, copypos, SEEK_SET) == -1)
294 return got_error_from_errno("fseek");
296 while (pos == -1 || copypos < pos) {
297 len = sizeof(buf);
298 if (pos > 0)
299 len = MIN(len, (size_t)pos - copypos);
300 r = fread(buf, 1, len, orig);
301 if (r != len && ferror(orig))
302 return got_error_from_errno("fread");
303 w = fwrite(buf, 1, r, tmp);
304 if (w != r)
305 return got_error_from_errno("fwrite");
306 copypos += len;
307 if (r != len && feof(orig)) {
308 if (pos == -1)
309 return NULL;
310 return got_error(GOT_ERR_HUNK_FAILED);
313 return NULL;
316 static const struct got_error *
317 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
319 const struct got_error *err = NULL;
320 char *line = NULL;
321 char mode = *h->lines[0];
322 size_t linesize = 0;
323 ssize_t linelen;
324 off_t match = -1;
325 long match_lineno = -1;
327 for (;;) {
328 linelen = getline(&line, &linesize, orig);
329 if (linelen == -1) {
330 if (ferror(orig))
331 err = got_error_from_errno("getline");
332 else if (match == -1)
333 err = got_error(GOT_ERR_HUNK_FAILED);
334 break;
336 if (line[linelen - 1] == '\n')
337 line[linelen - 1] = '\0';
338 (*lineno)++;
340 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
341 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
342 (mode == '+' && *lineno == h->old_from)) {
343 match = ftello(orig);
344 if (match == -1) {
345 err = got_error_from_errno("ftello");
346 break;
348 match -= linelen;
349 match_lineno = (*lineno)-1;
352 if (*lineno >= h->old_from && match != -1)
353 break;
356 if (err == NULL) {
357 *pos = match;
358 *lineno = match_lineno;
359 if (fseek(orig, match, SEEK_SET) == -1)
360 err = got_error_from_errno("fseek");
363 free(line);
364 return err;
367 static const struct got_error *
368 test_hunk(FILE *orig, struct got_patch_hunk *h)
370 const struct got_error *err = NULL;
371 char *line = NULL;
372 size_t linesize = 0, i = 0;
373 ssize_t linelen;
375 for (i = 0; i < h->len; ++i) {
376 switch (*h->lines[i]) {
377 case '+':
378 continue;
379 case ' ':
380 case '-':
381 linelen = getline(&line, &linesize, orig);
382 if (linelen == -1) {
383 if (ferror(orig))
384 err = got_error_from_errno("getline");
385 else
386 err = got_error(
387 GOT_ERR_HUNK_FAILED);
388 goto done;
390 if (line[linelen - 1] == '\n')
391 line[linelen - 1] = '\0';
392 if (strcmp(h->lines[i] + 1, line)) {
393 err = got_error(GOT_ERR_HUNK_FAILED);
394 goto done;
396 break;
400 done:
401 free(line);
402 return err;
405 static const struct got_error *
406 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
408 size_t i, new = 0;
410 for (i = 0; i < h->len; ++i) {
411 switch (*h->lines[i]) {
412 case ' ':
413 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
414 return got_error_from_errno("fprintf");
415 /* fallthrough */
416 case '-':
417 (*lineno)++;
418 break;
419 case '+':
420 new++;
421 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
422 return got_error_from_errno("fprintf");
423 if (new != h->new_lines || !h->new_nonl) {
424 if (fprintf(tmp, "\n") < 0)
425 return got_error_from_errno(
426 "fprintf");
428 break;
431 return NULL;
434 static const struct got_error *
435 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
436 mode_t *mode)
438 const struct got_error *err = NULL;
439 struct got_patch_hunk *h;
440 struct stat sb;
441 long lineno = 0;
442 FILE *orig;
443 off_t copypos, pos;
444 char *line = NULL;
445 size_t linesize = 0;
446 ssize_t linelen;
448 if (p->old == NULL) { /* create */
449 h = STAILQ_FIRST(&p->head);
450 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
451 return got_error(GOT_ERR_PATCH_MALFORMED);
452 if (nop)
453 return NULL;
454 return apply_hunk(tmp, h, &lineno);
457 if ((orig = fopen(path, "r")) == NULL) {
458 err = got_error_from_errno2("fopen", path);
459 goto done;
462 if (fstat(fileno(orig), &sb) == -1) {
463 err = got_error_from_errno("fstat");
464 goto done;
466 *mode = sb.st_mode;
468 copypos = 0;
469 STAILQ_FOREACH(h, &p->head, entries) {
470 if (h->lines == NULL)
471 break;
473 tryagain:
474 err = locate_hunk(orig, h, &pos, &lineno);
475 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
476 h->err = err;
477 if (err != NULL)
478 goto done;
479 if (!nop)
480 err = copy(tmp, orig, copypos, pos);
481 if (err != NULL)
482 goto done;
483 copypos = pos;
485 err = test_hunk(orig, h);
486 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
487 /*
488 * try to apply the hunk again starting the search
489 * after the previous partial match.
490 */
491 if (fseek(orig, pos, SEEK_SET) == -1) {
492 err = got_error_from_errno("fseek");
493 goto done;
495 linelen = getline(&line, &linesize, orig);
496 if (linelen == -1) {
497 err = got_error_from_errno("getline");
498 goto done;
500 lineno++;
501 goto tryagain;
503 if (err != NULL)
504 goto done;
506 if (lineno + 1 != h->old_from)
507 h->offset = lineno + 1 - h->old_from;
509 if (!nop)
510 err = apply_hunk(tmp, h, &lineno);
511 if (err != NULL)
512 goto done;
514 copypos = ftello(orig);
515 if (copypos == -1) {
516 err = got_error_from_errno("ftello");
517 goto done;
521 if (p->new == NULL && sb.st_size != copypos) {
522 h = STAILQ_FIRST(&p->head);
523 h->err = got_error(GOT_ERR_HUNK_FAILED);
524 err = h->err;
525 } else if (!nop && !feof(orig))
526 err = copy(tmp, orig, copypos, -1);
528 done:
529 if (orig != NULL)
530 fclose(orig);
531 return err;
534 static const struct got_error *
535 report_progress(struct patch_args *pa, const char *old, const char *new,
536 unsigned char status, const struct got_error *orig_error)
538 const struct got_error *err;
539 struct got_patch_hunk *h;
541 err = pa->progress_cb(pa->progress_arg, old, new, status,
542 orig_error, 0, 0, 0, 0, 0, NULL);
543 if (err)
544 return err;
546 STAILQ_FOREACH(h, pa->head, entries) {
547 if (h->offset == 0 && h->err == NULL)
548 continue;
550 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
551 h->old_from, h->old_lines, h->new_from, h->new_lines,
552 h->offset, h->err);
553 if (err)
554 return err;
557 return NULL;
560 static const struct got_error *
561 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
562 const char *path)
564 return report_progress(arg, path, NULL, status, NULL);
567 static const struct got_error *
568 patch_add(void *arg, unsigned char status, const char *path)
570 return report_progress(arg, NULL, path, status, NULL);
573 static const struct got_error *
574 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
575 const char *old, const char *new, struct got_patch *p, int nop,
576 struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
578 const struct got_error *err = NULL;
579 struct got_pathlist_head oldpaths, newpaths;
580 struct got_pathlist_entry *pe;
581 int file_renamed = 0;
582 char *oldpath = NULL, *newpath = NULL;
583 char *tmppath = NULL, *template = NULL, *parent = NULL;;
584 FILE *tmp = NULL;
585 mode_t mode = GOT_DEFAULT_FILE_MODE;
587 TAILQ_INIT(&oldpaths);
588 TAILQ_INIT(&newpaths);
590 err = got_pathlist_insert(&pe, &oldpaths, old, NULL);
591 if (err)
592 goto done;
593 err = got_pathlist_insert(&pe, &newpaths, new, NULL);
594 if (err)
595 goto done;
597 if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
598 old) == -1) {
599 err = got_error_from_errno("asprintf");
600 goto done;
603 if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
604 new) == -1) {
605 err = got_error_from_errno("asprintf");
606 goto done;
609 file_renamed = strcmp(oldpath, newpath);
611 if (asprintf(&template, "%s/got-patch",
612 got_worktree_get_root_path(worktree)) == -1) {
613 err = got_error_from_errno(template);
614 goto done;
617 if (!nop)
618 err = got_opentemp_named(&tmppath, &tmp, template);
619 if (err)
620 goto done;
621 err = patch_file(p, oldpath, tmp, nop, &mode);
622 if (err)
623 goto done;
625 if (nop)
626 goto done;
628 if (p->old != NULL && p->new == NULL) {
629 err = got_worktree_schedule_delete(worktree, &oldpaths,
630 0, NULL, patch_delete, pa, repo, 0, 0);
631 goto done;
634 if (fchmod(fileno(tmp), mode) == -1) {
635 err = got_error_from_errno2("chmod", newpath);
636 goto done;
639 if (rename(tmppath, newpath) == -1) {
640 if (errno != ENOENT) {
641 err = got_error_from_errno3("rename", tmppath,
642 newpath);
643 goto done;
646 err = got_path_dirname(&parent, newpath);
647 if (err != NULL)
648 goto done;
649 err = got_path_mkdir(parent);
650 if (err != NULL)
651 goto done;
652 if (rename(tmppath, newpath) == -1) {
653 err = got_error_from_errno3("rename", tmppath,
654 newpath);
655 goto done;
659 if (file_renamed) {
660 err = got_worktree_schedule_delete(worktree, &oldpaths,
661 0, NULL, patch_delete, pa, repo, 0, 0);
662 if (err == NULL)
663 err = got_worktree_schedule_add(worktree, &newpaths,
664 patch_add, pa, repo, 1);
665 if (err)
666 unlink(newpath);
667 } else if (p->old == NULL) {
668 err = got_worktree_schedule_add(worktree, &newpaths,
669 patch_add, pa, repo, 1);
670 if (err)
671 unlink(newpath);
672 } else
673 err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
675 done:
676 got_pathlist_free(&oldpaths);
677 got_pathlist_free(&newpaths);
678 free(parent);
679 free(template);
680 if (tmppath != NULL)
681 unlink(tmppath);
682 free(tmppath);
683 free(oldpath);
684 free(newpath);
685 return err;
688 static void
689 reverse_patch(struct got_patch *p)
691 struct got_patch_hunk *h;
692 size_t i;
693 long tmp;
695 STAILQ_FOREACH(h, &p->head, entries) {
696 tmp = h->old_from;
697 h->old_from = h->new_from;
698 h->new_from = tmp;
700 tmp = h->old_lines;
701 h->old_lines = h->new_lines;
702 h->new_lines = tmp;
704 tmp = h->old_nonl;
705 h->old_nonl = h->new_nonl;
706 h->new_nonl = tmp;
708 for (i = 0; i < h->len; ++i) {
709 if (*h->lines[i] == '+')
710 *h->lines[i] = '-';
711 else if (*h->lines[i] == '-')
712 *h->lines[i] = '+';
717 const struct got_error *
718 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
719 int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
720 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
722 const struct got_error *err = NULL;
723 struct got_fileindex *fileindex = NULL;
724 char *oldpath, *newpath;
725 struct imsgbuf *ibuf;
726 int imsg_fds[2] = {-1, -1};
727 int done = 0, failed = 0;
728 pid_t pid;
730 ibuf = calloc(1, sizeof(*ibuf));
731 if (ibuf == NULL) {
732 err = got_error_from_errno("calloc");
733 goto done;
736 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
737 err = got_error_from_errno("socketpair");
738 goto done;
741 pid = fork();
742 if (pid == -1) {
743 err = got_error_from_errno("fork");
744 goto done;
745 } else if (pid == 0) {
746 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
747 NULL);
748 /* not reached */
751 if (close(imsg_fds[1]) == -1) {
752 err = got_error_from_errno("close");
753 goto done;
755 imsg_fds[1] = -1;
756 imsg_init(ibuf, imsg_fds[0]);
758 err = send_patch(ibuf, fd);
759 fd = -1;
760 if (err)
761 goto done;
763 err = got_worktree_patch_prepare(&fileindex, worktree);
764 if (err)
765 goto done;
767 while (!done && err == NULL) {
768 struct got_patch p;
769 struct patch_args pa;
771 pa.progress_cb = progress_cb;
772 pa.progress_arg = progress_arg;
773 pa.head = &p.head;
775 err = recv_patch(ibuf, &done, &p, strip);
776 if (err || done)
777 break;
779 if (reverse)
780 reverse_patch(&p);
782 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
783 &newpath, worktree, repo, fileindex);
784 if (err == NULL)
785 err = apply_patch(worktree, repo, oldpath, newpath,
786 &p, nop, &pa, cancel_cb, cancel_arg);
787 if (err != NULL) {
788 failed = 1;
789 /* recoverable errors */
790 if (err->code == GOT_ERR_FILE_STATUS ||
791 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
792 err = report_progress(&pa, p.old, p.new,
793 GOT_STATUS_CANNOT_UPDATE, err);
794 else if (err->code == GOT_ERR_HUNK_FAILED)
795 err = report_progress(&pa, p.old, p.new,
796 GOT_STATUS_CANNOT_UPDATE, NULL);
799 free(oldpath);
800 free(newpath);
801 patch_free(&p);
803 if (err)
804 break;
807 done:
808 if (fileindex)
809 got_worktree_patch_complete(fileindex);
810 if (fd != -1 && close(fd) == -1 && err == NULL)
811 err = got_error_from_errno("close");
812 if (ibuf != NULL)
813 imsg_clear(ibuf);
814 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
815 err = got_error_from_errno("close");
816 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
817 err = got_error_from_errno("close");
818 if (err == NULL && failed)
819 err = got_error(GOT_ERR_PATCH_FAILED);
820 return err;