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_opentemp.h"
46 #include "got_patch.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.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 long offset;
58 int nonl;
59 long old_from;
60 long old_lines;
61 long new_from;
62 long new_lines;
63 size_t len;
64 size_t cap;
65 char **lines;
66 };
68 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
69 struct got_patch {
70 char *old;
71 char *new;
72 struct got_patch_hunk_head head;
73 };
75 struct patch_args {
76 got_patch_progress_cb progress_cb;
77 void *progress_arg;
78 struct got_patch_hunk_head *head;
79 };
81 static const struct got_error *
82 send_patch(struct imsgbuf *ibuf, int fd)
83 {
84 const struct got_error *err = NULL;
86 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
87 NULL, 0) == -1) {
88 err = got_error_from_errno(
89 "imsg_compose GOT_IMSG_PATCH_FILE");
90 close(fd);
91 return err;
92 }
94 if (imsg_flush(ibuf) == -1) {
95 err = got_error_from_errno("imsg_flush");
96 imsg_clear(ibuf);
97 }
99 return err;
102 static void
103 patch_free(struct got_patch *p)
105 struct got_patch_hunk *h;
106 size_t i;
108 while (!STAILQ_EMPTY(&p->head)) {
109 h = STAILQ_FIRST(&p->head);
110 STAILQ_REMOVE_HEAD(&p->head, entries);
112 for (i = 0; i < h->len; ++i)
113 free(h->lines[i]);
114 free(h->lines);
115 free(h);
118 free(p->new);
119 free(p->old);
122 static const struct got_error *
123 pushline(struct got_patch_hunk *h, const char *line)
125 void *t;
126 size_t newcap;
128 if (h->len == h->cap) {
129 if ((newcap = h->cap * 1.5) == 0)
130 newcap = 16;
131 t = recallocarray(h->lines, h->cap, newcap,
132 sizeof(h->lines[0]));
133 if (t == NULL)
134 return got_error_from_errno("recallocarray");
135 h->lines = t;
136 h->cap = newcap;
139 if ((t = strdup(line)) == NULL)
140 return got_error_from_errno("strdup");
142 h->lines[h->len++] = t;
143 return NULL;
146 static const struct got_error *
147 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
149 const struct got_error *err = NULL;
150 struct imsg imsg;
151 struct got_imsg_patch_hunk hdr;
152 struct got_imsg_patch patch;
153 struct got_patch_hunk *h = NULL;
154 size_t datalen;
156 memset(p, 0, sizeof(*p));
157 STAILQ_INIT(&p->head);
159 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
160 if (err)
161 return err;
162 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
163 *done = 1;
164 goto done;
166 if (imsg.hdr.type != GOT_IMSG_PATCH) {
167 err = got_error(GOT_ERR_PRIVSEP_MSG);
168 goto done;
170 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
171 if (datalen != sizeof(patch)) {
172 err = got_error(GOT_ERR_PRIVSEP_LEN);
173 goto done;
175 memcpy(&patch, imsg.data, sizeof(patch));
177 /* automatically set strip=1 for git-style diffs */
178 if (strip == -1 && patch.git &&
179 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
180 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
181 strip = 1;
183 /* prefer the new name if not /dev/null for not git-style diffs */
184 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
185 err = got_path_strip(&p->old, patch.new, strip);
186 if (err)
187 goto done;
188 } else if (*patch.old != '\0') {
189 err = got_path_strip(&p->old, patch.old, strip);
190 if (err)
191 goto done;
194 if (*patch.new != '\0') {
195 err = got_path_strip(&p->new, patch.new, strip);
196 if (err)
197 goto done;
200 if (p->old == NULL && p->new == NULL) {
201 err = got_error(GOT_ERR_PATCH_MALFORMED);
202 goto done;
205 imsg_free(&imsg);
207 for (;;) {
208 char *t;
210 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
211 if (err)
212 return err;
214 switch (imsg.hdr.type) {
215 case GOT_IMSG_PATCH_DONE:
216 goto done;
217 case GOT_IMSG_PATCH_HUNK:
218 if (h != NULL && h->nonl) {
219 err = got_error(GOT_ERR_PATCH_MALFORMED);
220 goto done;
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;
255 if (h->nonl)
256 err = got_error(GOT_ERR_PATCH_MALFORMED);
257 if (*t == '\\')
258 h->nonl = 1;
259 else
260 err = pushline(h, t);
261 if (err)
262 goto done;
263 break;
264 default:
265 err = got_error(GOT_ERR_PRIVSEP_MSG);
266 goto done;
269 imsg_free(&imsg);
272 done:
273 imsg_free(&imsg);
274 return err;
277 /*
278 * Copy data from orig starting at copypos until pos into tmp.
279 * If pos is -1, copy until EOF.
280 */
281 static const struct got_error *
282 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
284 char buf[BUFSIZ];
285 size_t len, r, w;
287 if (fseek(orig, copypos, SEEK_SET) == -1)
288 return got_error_from_errno("fseek");
290 while (pos == -1 || copypos < pos) {
291 len = sizeof(buf);
292 if (pos > 0)
293 len = MIN(len, (size_t)pos - copypos);
294 r = fread(buf, 1, len, orig);
295 if (r != len && ferror(orig))
296 return got_error_from_errno("fread");
297 w = fwrite(buf, 1, r, tmp);
298 if (w != r)
299 return got_error_from_errno("fwrite");
300 copypos += len;
301 if (r != len && feof(orig)) {
302 if (pos == -1)
303 return NULL;
304 return got_error(GOT_ERR_HUNK_FAILED);
307 return NULL;
310 static const struct got_error *
311 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
313 const struct got_error *err = NULL;
314 char *line = NULL;
315 char mode = *h->lines[0];
316 size_t linesize = 0;
317 ssize_t linelen;
318 off_t match = -1;
319 long match_lineno = -1;
321 for (;;) {
322 linelen = getline(&line, &linesize, orig);
323 if (linelen == -1) {
324 if (ferror(orig))
325 err = got_error_from_errno("getline");
326 else if (match == -1)
327 err = got_error(GOT_ERR_HUNK_FAILED);
328 break;
330 if (line[linelen - 1] == '\n')
331 line[linelen - 1] = '\0';
332 (*lineno)++;
334 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
335 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
336 (mode == '+' && *lineno == h->old_from)) {
337 match = ftello(orig);
338 if (match == -1) {
339 err = got_error_from_errno("ftello");
340 break;
342 match -= linelen;
343 match_lineno = (*lineno)-1;
346 if (*lineno >= h->old_from && match != -1)
347 break;
350 if (err == NULL) {
351 *pos = match;
352 *lineno = match_lineno;
353 if (fseek(orig, match, SEEK_SET) == -1)
354 err = got_error_from_errno("fseek");
357 free(line);
358 return err;
361 static const struct got_error *
362 test_hunk(FILE *orig, struct got_patch_hunk *h)
364 const struct got_error *err = NULL;
365 char *line = NULL;
366 size_t linesize = 0, i = 0;
367 ssize_t linelen;
369 for (i = 0; i < h->len; ++i) {
370 switch (*h->lines[i]) {
371 case '+':
372 continue;
373 case ' ':
374 case '-':
375 linelen = getline(&line, &linesize, orig);
376 if (linelen == -1) {
377 if (ferror(orig))
378 err = got_error_from_errno("getline");
379 else
380 err = got_error(
381 GOT_ERR_HUNK_FAILED);
382 goto done;
384 if (line[linelen - 1] == '\n')
385 line[linelen - 1] = '\0';
386 if (strcmp(h->lines[i] + 1, line)) {
387 err = got_error(GOT_ERR_HUNK_FAILED);
388 goto done;
390 break;
394 done:
395 free(line);
396 return err;
399 static const struct got_error *
400 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
402 size_t i = 0;
404 for (i = 0; i < h->len; ++i) {
405 switch (*h->lines[i]) {
406 case ' ':
407 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
408 return got_error_from_errno("fprintf");
409 /* fallthrough */
410 case '-':
411 (*lineno)++;
412 break;
413 case '+':
414 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
415 return got_error_from_errno("fprintf");
416 if (i != h->len - 1 || !h->nonl) {
417 if (fprintf(tmp, "\n") < 0)
418 return got_error_from_errno(
419 "fprintf");
421 break;
424 return NULL;
427 static const struct got_error *
428 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
429 mode_t *mode)
431 const struct got_error *err = NULL;
432 struct got_patch_hunk *h;
433 struct stat sb;
434 long lineno = 0;
435 FILE *orig;
436 off_t copypos, pos;
437 char *line = NULL;
438 size_t linesize = 0;
439 ssize_t linelen;
441 if (p->old == NULL) { /* create */
442 h = STAILQ_FIRST(&p->head);
443 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
444 return got_error(GOT_ERR_PATCH_MALFORMED);
445 if (nop)
446 return NULL;
447 return apply_hunk(tmp, h, &lineno);
450 if ((orig = fopen(path, "r")) == NULL) {
451 err = got_error_from_errno2("fopen", path);
452 goto done;
455 if (fstat(fileno(orig), &sb) == -1) {
456 err = got_error_from_errno("fstat");
457 goto done;
459 *mode = sb.st_mode;
461 copypos = 0;
462 STAILQ_FOREACH(h, &p->head, entries) {
463 if (h->lines == NULL)
464 break;
466 tryagain:
467 err = locate_hunk(orig, h, &pos, &lineno);
468 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
469 h->err = err;
470 if (err != NULL)
471 goto done;
472 if (!nop)
473 err = copy(tmp, orig, copypos, pos);
474 if (err != NULL)
475 goto done;
476 copypos = pos;
478 err = test_hunk(orig, h);
479 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
480 /*
481 * try to apply the hunk again starting the search
482 * after the previous partial match.
483 */
484 if (fseek(orig, pos, SEEK_SET) == -1) {
485 err = got_error_from_errno("fseek");
486 goto done;
488 linelen = getline(&line, &linesize, orig);
489 if (linelen == -1) {
490 err = got_error_from_errno("getline");
491 goto done;
493 lineno++;
494 goto tryagain;
496 if (err != NULL)
497 goto done;
499 if (lineno + 1 != h->old_from)
500 h->offset = lineno + 1 - h->old_from;
502 if (!nop)
503 err = apply_hunk(tmp, h, &lineno);
504 if (err != NULL)
505 goto done;
507 copypos = ftello(orig);
508 if (copypos == -1) {
509 err = got_error_from_errno("ftello");
510 goto done;
514 if (p->new == NULL && sb.st_size != copypos) {
515 h = STAILQ_FIRST(&p->head);
516 h->err = got_error(GOT_ERR_HUNK_FAILED);
517 err = h->err;
518 } else if (!nop && !feof(orig))
519 err = copy(tmp, orig, copypos, -1);
521 done:
522 if (orig != NULL)
523 fclose(orig);
524 return err;
527 static const struct got_error *
528 report_progress(struct patch_args *pa, const char *old, const char *new,
529 unsigned char status, const struct got_error *orig_error)
531 const struct got_error *err;
532 struct got_patch_hunk *h;
534 err = pa->progress_cb(pa->progress_arg, old, new, status,
535 orig_error, 0, 0, 0, 0, 0, NULL);
536 if (err)
537 return err;
539 STAILQ_FOREACH(h, pa->head, entries) {
540 if (h->offset == 0 && h->err == NULL)
541 continue;
543 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
544 h->old_from, h->old_lines, h->new_from, h->new_lines,
545 h->offset, h->err);
546 if (err)
547 return err;
550 return NULL;
553 static const struct got_error *
554 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
555 const char *path)
557 return report_progress(arg, path, NULL, status, NULL);
560 static const struct got_error *
561 patch_add(void *arg, unsigned char status, const char *path)
563 return report_progress(arg, NULL, path, status, NULL);
566 static const struct got_error *
567 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
568 const char *oldpath, const char *newpath, struct got_patch *p,
569 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
571 const struct got_error *err = NULL;
572 struct got_pathlist_head oldpaths, newpaths;
573 struct got_pathlist_entry *pe;
574 int file_renamed = 0;
575 char *tmppath = NULL, *template = NULL, *parent = NULL;;
576 FILE *tmp = NULL;
577 mode_t mode = GOT_DEFAULT_FILE_MODE;
579 TAILQ_INIT(&oldpaths);
580 TAILQ_INIT(&newpaths);
582 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
583 if (err)
584 goto done;
585 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
586 if (err)
587 goto done;
589 file_renamed = strcmp(oldpath, newpath);
591 if (asprintf(&template, "%s/got-patch",
592 got_worktree_get_root_path(worktree)) == -1) {
593 err = got_error_from_errno(template);
594 goto done;
597 if (!nop)
598 err = got_opentemp_named(&tmppath, &tmp, template);
599 if (err)
600 goto done;
601 err = patch_file(p, oldpath, tmp, nop, &mode);
602 if (err)
603 goto done;
605 if (nop)
606 goto done;
608 if (p->old != NULL && p->new == NULL) {
609 err = got_worktree_schedule_delete(worktree, &oldpaths,
610 0, NULL, patch_delete, pa, repo, 0, 0);
611 goto done;
614 if (fchmod(fileno(tmp), mode) == -1) {
615 err = got_error_from_errno2("chmod", newpath);
616 goto done;
619 if (rename(tmppath, newpath) == -1) {
620 if (errno != ENOENT) {
621 err = got_error_from_errno3("rename", tmppath,
622 newpath);
623 goto done;
626 err = got_path_dirname(&parent, newpath);
627 if (err != NULL)
628 goto done;
629 err = got_path_mkdir(parent);
630 if (err != NULL)
631 goto done;
632 if (rename(tmppath, newpath) == -1) {
633 err = got_error_from_errno3("rename", tmppath,
634 newpath);
635 goto done;
639 if (file_renamed) {
640 err = got_worktree_schedule_delete(worktree, &oldpaths,
641 0, NULL, patch_delete, pa, repo, 0, 0);
642 if (err == NULL)
643 err = got_worktree_schedule_add(worktree, &newpaths,
644 patch_add, pa, repo, 1);
645 if (err)
646 unlink(newpath);
647 } else if (p->old == NULL) {
648 err = got_worktree_schedule_add(worktree, &newpaths,
649 patch_add, pa, repo, 1);
650 if (err)
651 unlink(newpath);
652 } else
653 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
654 NULL);
656 done:
657 got_pathlist_free(&oldpaths);
658 got_pathlist_free(&newpaths);
659 free(parent);
660 free(template);
661 if (tmppath != NULL)
662 unlink(tmppath);
663 free(tmppath);
664 return err;
667 const struct got_error *
668 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
669 int nop, int strip, got_patch_progress_cb progress_cb, void *progress_arg,
670 got_cancel_cb cancel_cb, void *cancel_arg)
672 const struct got_error *err = NULL;
673 struct got_fileindex *fileindex = NULL;
674 char *oldpath, *newpath;
675 struct imsgbuf *ibuf;
676 int imsg_fds[2] = {-1, -1};
677 int done = 0, failed = 0;
678 pid_t pid;
680 ibuf = calloc(1, sizeof(*ibuf));
681 if (ibuf == NULL) {
682 err = got_error_from_errno("calloc");
683 goto done;
686 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
687 err = got_error_from_errno("socketpair");
688 goto done;
691 pid = fork();
692 if (pid == -1) {
693 err = got_error_from_errno("fork");
694 goto done;
695 } else if (pid == 0) {
696 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
697 NULL);
698 /* not reached */
701 if (close(imsg_fds[1]) == -1) {
702 err = got_error_from_errno("close");
703 goto done;
705 imsg_fds[1] = -1;
706 imsg_init(ibuf, imsg_fds[0]);
708 err = send_patch(ibuf, fd);
709 fd = -1;
710 if (err)
711 goto done;
713 err = got_worktree_patch_prepare(&fileindex, worktree);
714 if (err)
715 goto done;
717 while (!done && err == NULL) {
718 struct got_patch p;
719 struct patch_args pa;
721 pa.progress_cb = progress_cb;
722 pa.progress_arg = progress_arg;
723 pa.head = &p.head;
725 err = recv_patch(ibuf, &done, &p, strip);
726 if (err || done)
727 break;
729 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
730 &newpath, worktree, repo, fileindex);
731 if (err == NULL)
732 err = apply_patch(worktree, repo, oldpath, newpath,
733 &p, nop, &pa, cancel_cb, cancel_arg);
734 if (err != NULL) {
735 failed = 1;
736 /* recoverable errors */
737 if (err->code == GOT_ERR_FILE_STATUS ||
738 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
739 err = report_progress(&pa, p.old, p.new,
740 GOT_STATUS_CANNOT_UPDATE, err);
741 else if (err->code == GOT_ERR_HUNK_FAILED)
742 err = report_progress(&pa, p.old, p.new,
743 GOT_STATUS_CANNOT_UPDATE, NULL);
746 free(oldpath);
747 free(newpath);
748 patch_free(&p);
750 if (err)
751 break;
754 done:
755 if (fileindex)
756 got_worktree_patch_complete(fileindex);
757 if (fd != -1 && close(fd) == -1 && err == NULL)
758 err = got_error_from_errno("close");
759 if (ibuf != NULL)
760 imsg_clear(ibuf);
761 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
762 err = got_error_from_errno("close");
763 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
764 err = got_error_from_errno("close");
765 if (err == NULL && failed)
766 err = got_error(GOT_ERR_PATCH_FAILED);
767 return err;