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 nonl;
56 long old_from;
57 long old_lines;
58 long new_from;
59 long new_lines;
60 size_t len;
61 size_t cap;
62 char **lines;
63 };
65 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
66 struct got_patch {
67 char *old;
68 char *new;
69 struct got_patch_hunk_head head;
70 };
72 struct patch_args {
73 got_patch_progress_cb progress_cb;
74 void *progress_arg;
75 struct got_patch_hunk_head *head;
76 };
78 static const struct got_error *
79 send_patch(struct imsgbuf *ibuf, int fd)
80 {
81 const struct got_error *err = NULL;
83 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
84 NULL, 0) == -1) {
85 err = got_error_from_errno(
86 "imsg_compose GOT_IMSG_PATCH_FILE");
87 close(fd);
88 return err;
89 }
91 if (imsg_flush(ibuf) == -1) {
92 err = got_error_from_errno("imsg_flush");
93 imsg_clear(ibuf);
94 }
96 return err;
97 }
99 static void
100 patch_free(struct got_patch *p)
102 struct got_patch_hunk *h;
103 size_t i;
105 while (!STAILQ_EMPTY(&p->head)) {
106 h = STAILQ_FIRST(&p->head);
107 STAILQ_REMOVE_HEAD(&p->head, entries);
109 for (i = 0; i < h->len; ++i)
110 free(h->lines[i]);
111 free(h->lines);
112 free(h);
115 free(p->new);
116 free(p->old);
119 static const struct got_error *
120 pushline(struct got_patch_hunk *h, const char *line)
122 void *t;
123 size_t newcap;
125 if (h->len == h->cap) {
126 if ((newcap = h->cap * 1.5) == 0)
127 newcap = 16;
128 t = recallocarray(h->lines, h->cap, newcap,
129 sizeof(h->lines[0]));
130 if (t == NULL)
131 return got_error_from_errno("recallocarray");
132 h->lines = t;
133 h->cap = newcap;
136 if ((t = strdup(line)) == NULL)
137 return got_error_from_errno("strdup");
139 h->lines[h->len++] = t;
140 return NULL;
143 static const struct got_error *
144 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
146 const struct got_error *err = NULL;
147 struct imsg imsg;
148 struct got_imsg_patch_hunk hdr;
149 struct got_imsg_patch patch;
150 struct got_patch_hunk *h = NULL;
151 size_t datalen;
153 memset(p, 0, sizeof(*p));
154 STAILQ_INIT(&p->head);
156 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
157 if (err)
158 return err;
159 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
160 *done = 1;
161 goto done;
163 if (imsg.hdr.type != GOT_IMSG_PATCH) {
164 err = got_error(GOT_ERR_PRIVSEP_MSG);
165 goto done;
167 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
168 if (datalen != sizeof(patch)) {
169 err = got_error(GOT_ERR_PRIVSEP_LEN);
170 goto done;
172 memcpy(&patch, imsg.data, sizeof(patch));
173 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
174 err = got_error_from_errno("strdup");
175 goto done;
177 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
178 err = got_error_from_errno("strdup");
179 goto done;
181 if (p->old == NULL && p->new == NULL) {
182 err = got_error(GOT_ERR_PATCH_MALFORMED);
183 goto done;
186 imsg_free(&imsg);
188 for (;;) {
189 char *t;
191 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
192 if (err)
193 return err;
195 switch (imsg.hdr.type) {
196 case GOT_IMSG_PATCH_DONE:
197 goto done;
198 case GOT_IMSG_PATCH_HUNK:
199 if (h != NULL && h->nonl) {
200 err = got_error(GOT_ERR_PATCH_MALFORMED);
201 goto done;
203 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
204 if (datalen != sizeof(hdr)) {
205 err = got_error(GOT_ERR_PRIVSEP_LEN);
206 goto done;
208 memcpy(&hdr, imsg.data, sizeof(hdr));
209 if ((h = calloc(1, sizeof(*h))) == NULL) {
210 err = got_error_from_errno("calloc");
211 goto done;
213 h->old_from = hdr.oldfrom;
214 h->old_lines = hdr.oldlines;
215 h->new_from = hdr.newfrom;
216 h->new_lines = hdr.newlines;
217 STAILQ_INSERT_TAIL(&p->head, h, entries);
218 break;
219 case GOT_IMSG_PATCH_LINE:
220 if (h == NULL) {
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
222 goto done;
224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
225 t = imsg.data;
226 /* at least one char */
227 if (datalen < 2 || t[datalen-1] != '\0') {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
229 goto done;
231 if (*t != ' ' && *t != '-' && *t != '+' &&
232 *t != '\\') {
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
234 goto done;
236 if (h->nonl)
237 err = got_error(GOT_ERR_PATCH_MALFORMED);
238 if (*t == '\\')
239 h->nonl = 1;
240 else
241 err = pushline(h, t);
242 if (err)
243 goto done;
244 break;
245 default:
246 err = got_error(GOT_ERR_PRIVSEP_MSG);
247 goto done;
250 imsg_free(&imsg);
253 done:
254 imsg_free(&imsg);
255 return err;
258 /*
259 * Copy data from orig starting at copypos until pos into tmp.
260 * If pos is -1, copy until EOF.
261 */
262 static const struct got_error *
263 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
265 char buf[BUFSIZ];
266 size_t len, r, w;
268 if (fseek(orig, copypos, SEEK_SET) == -1)
269 return got_error_from_errno("fseek");
271 while (pos == -1 || copypos < pos) {
272 len = sizeof(buf);
273 if (pos > 0)
274 len = MIN(len, (size_t)pos - copypos);
275 r = fread(buf, 1, len, orig);
276 if (r != len && ferror(orig))
277 return got_error_from_errno("fread");
278 w = fwrite(buf, 1, r, tmp);
279 if (w != r)
280 return got_error_from_errno("fwrite");
281 copypos += len;
282 if (r != len && feof(orig)) {
283 if (pos == -1)
284 return NULL;
285 return got_error(GOT_ERR_HUNK_FAILED);
288 return NULL;
291 static const struct got_error *
292 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
294 const struct got_error *err = NULL;
295 char *line = NULL;
296 char mode = *h->lines[0];
297 size_t linesize = 0;
298 ssize_t linelen;
299 off_t match = -1;
300 long match_lineno = -1;
302 for (;;) {
303 linelen = getline(&line, &linesize, orig);
304 if (linelen == -1) {
305 if (ferror(orig))
306 err = got_error_from_errno("getline");
307 else if (match == -1)
308 err = got_error(GOT_ERR_HUNK_FAILED);
309 break;
311 if (line[linelen - 1] == '\n')
312 line[linelen - 1] = '\0';
313 (*lineno)++;
315 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
316 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
317 (mode == '+' && *lineno == h->old_from)) {
318 match = ftello(orig);
319 if (match == -1) {
320 err = got_error_from_errno("ftello");
321 break;
323 match -= linelen;
324 match_lineno = (*lineno)-1;
327 if (*lineno >= h->old_from && match != -1)
328 break;
331 if (err == NULL) {
332 *pos = match;
333 *lineno = match_lineno;
334 if (fseek(orig, match, SEEK_SET) == -1)
335 err = got_error_from_errno("fseek");
338 free(line);
339 return err;
342 static const struct got_error *
343 test_hunk(FILE *orig, struct got_patch_hunk *h)
345 const struct got_error *err = NULL;
346 char *line = NULL;
347 size_t linesize = 0, i = 0;
348 ssize_t linelen;
350 for (i = 0; i < h->len; ++i) {
351 switch (*h->lines[i]) {
352 case '+':
353 continue;
354 case ' ':
355 case '-':
356 linelen = getline(&line, &linesize, orig);
357 if (linelen == -1) {
358 if (ferror(orig))
359 err = got_error_from_errno("getline");
360 else
361 err = got_error(
362 GOT_ERR_HUNK_FAILED);
363 goto done;
365 if (line[linelen - 1] == '\n')
366 line[linelen - 1] = '\0';
367 if (strcmp(h->lines[i] + 1, line)) {
368 err = got_error(GOT_ERR_HUNK_FAILED);
369 goto done;
371 break;
375 done:
376 free(line);
377 return err;
380 static const struct got_error *
381 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
383 size_t i = 0;
385 for (i = 0; i < h->len; ++i) {
386 switch (*h->lines[i]) {
387 case ' ':
388 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
389 return got_error_from_errno("fprintf");
390 /* fallthrough */
391 case '-':
392 (*lineno)++;
393 break;
394 case '+':
395 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
396 return got_error_from_errno("fprintf");
397 if (i != h->len - 1 || !h->nonl) {
398 if (fprintf(tmp, "\n") < 0)
399 return got_error_from_errno(
400 "fprintf");
402 break;
405 return NULL;
408 static const struct got_error *
409 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
410 mode_t *mode)
412 const struct got_error *err = NULL;
413 struct got_patch_hunk *h;
414 struct stat sb;
415 long lineno = 0;
416 FILE *orig;
417 off_t copypos, pos;
418 char *line = NULL;
419 size_t linesize = 0;
420 ssize_t linelen;
422 if (p->old == NULL) { /* create */
423 h = STAILQ_FIRST(&p->head);
424 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
425 return got_error(GOT_ERR_PATCH_MALFORMED);
426 if (nop)
427 return NULL;
428 return apply_hunk(tmp, h, &lineno);
431 if ((orig = fopen(path, "r")) == NULL) {
432 err = got_error_from_errno2("fopen", path);
433 goto done;
436 if (fstat(fileno(orig), &sb) == -1) {
437 err = got_error_from_errno("fstat");
438 goto done;
440 *mode = sb.st_mode;
442 copypos = 0;
443 STAILQ_FOREACH(h, &p->head, entries) {
444 if (h->lines == NULL)
445 break;
447 tryagain:
448 err = locate_hunk(orig, h, &pos, &lineno);
449 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
450 h->err = err;
451 if (err != NULL)
452 goto done;
453 if (!nop)
454 err = copy(tmp, orig, copypos, pos);
455 if (err != NULL)
456 goto done;
457 copypos = pos;
459 err = test_hunk(orig, h);
460 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
461 /*
462 * try to apply the hunk again starting the search
463 * after the previous partial match.
464 */
465 if (fseek(orig, pos, SEEK_SET) == -1) {
466 err = got_error_from_errno("fseek");
467 goto done;
469 linelen = getline(&line, &linesize, orig);
470 if (linelen == -1) {
471 err = got_error_from_errno("getline");
472 goto done;
474 lineno++;
475 goto tryagain;
477 if (err != NULL)
478 goto done;
480 if (lineno + 1 != h->old_from)
481 h->offset = lineno + 1 - h->old_from;
483 if (!nop)
484 err = apply_hunk(tmp, h, &lineno);
485 if (err != NULL)
486 goto done;
488 copypos = ftello(orig);
489 if (copypos == -1) {
490 err = got_error_from_errno("ftello");
491 goto done;
495 if (p->new == NULL && sb.st_size != copypos) {
496 h = STAILQ_FIRST(&p->head);
497 h->err = got_error(GOT_ERR_HUNK_FAILED);
498 err = h->err;
499 } else if (!nop && !feof(orig))
500 err = copy(tmp, orig, copypos, -1);
502 done:
503 if (orig != NULL)
504 fclose(orig);
505 return err;
508 static const struct got_error *
509 report_progress(struct patch_args *pa, const char *old, const char *new,
510 unsigned char status, const struct got_error *orig_error)
512 const struct got_error *err;
513 struct got_patch_hunk *h;
515 err = pa->progress_cb(pa->progress_arg, old, new, status,
516 orig_error, 0, 0, 0, 0, 0, NULL);
517 if (err)
518 return err;
520 STAILQ_FOREACH(h, pa->head, entries) {
521 if (h->offset == 0 && h->err == NULL)
522 continue;
524 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
525 h->old_from, h->old_lines, h->new_from, h->new_lines,
526 h->offset, h->err);
527 if (err)
528 return err;
531 return NULL;
534 static const struct got_error *
535 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
536 const char *path)
538 return report_progress(arg, path, NULL, status, NULL);
541 static const struct got_error *
542 patch_add(void *arg, unsigned char status, const char *path)
544 return report_progress(arg, NULL, path, status, NULL);
547 static const struct got_error *
548 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
549 const char *oldpath, const char *newpath, struct got_patch *p,
550 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
552 const struct got_error *err = NULL;
553 struct got_pathlist_head oldpaths, newpaths;
554 struct got_pathlist_entry *pe;
555 int file_renamed = 0;
556 char *tmppath = NULL, *template = NULL, *parent = NULL;;
557 FILE *tmp = NULL;
558 mode_t mode = GOT_DEFAULT_FILE_MODE;
560 TAILQ_INIT(&oldpaths);
561 TAILQ_INIT(&newpaths);
563 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
564 if (err)
565 goto done;
566 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
567 if (err)
568 goto done;
570 file_renamed = strcmp(oldpath, newpath);
572 if (p->old != NULL && p->new == NULL) {
573 /*
574 * special case: delete a file. don't try to match
575 * the lines but just schedule the removal.
576 */
577 err = got_worktree_schedule_delete(worktree, &oldpaths,
578 0, NULL, patch_delete, pa, repo, 0, 0);
579 goto done;
582 if (asprintf(&template, "%s/got-patch",
583 got_worktree_get_root_path(worktree)) == -1) {
584 err = got_error_from_errno(template);
585 goto done;
588 if (!nop)
589 err = got_opentemp_named(&tmppath, &tmp, template);
590 if (err)
591 goto done;
592 err = patch_file(p, oldpath, tmp, nop, &mode);
593 if (err)
594 goto done;
596 if (nop)
597 goto done;
599 if (p->old != NULL && p->new == NULL) {
600 err = got_worktree_schedule_delete(worktree, &oldpaths,
601 0, NULL, patch_delete, pa, repo, 0, 0);
602 goto done;
605 if (fchmod(fileno(tmp), mode) == -1) {
606 err = got_error_from_errno2("chmod", newpath);
607 goto done;
610 if (rename(tmppath, newpath) == -1) {
611 if (errno != ENOENT) {
612 err = got_error_from_errno3("rename", tmppath,
613 newpath);
614 goto done;
617 err = got_path_dirname(&parent, newpath);
618 if (err != NULL)
619 goto done;
620 err = got_path_mkdir(parent);
621 if (err != NULL)
622 goto done;
623 if (rename(tmppath, newpath) == -1) {
624 err = got_error_from_errno3("rename", tmppath,
625 newpath);
626 goto done;
630 if (file_renamed) {
631 err = got_worktree_schedule_delete(worktree, &oldpaths,
632 0, NULL, patch_delete, pa, repo, 0, 0);
633 if (err == NULL)
634 err = got_worktree_schedule_add(worktree, &newpaths,
635 patch_add, pa, repo, 1);
636 if (err)
637 unlink(newpath);
638 } else if (p->old == NULL) {
639 err = got_worktree_schedule_add(worktree, &newpaths,
640 patch_add, pa, repo, 1);
641 if (err)
642 unlink(newpath);
643 } else
644 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
645 NULL);
647 done:
648 got_pathlist_free(&oldpaths);
649 got_pathlist_free(&newpaths);
650 free(parent);
651 free(template);
652 if (tmppath != NULL)
653 unlink(tmppath);
654 free(tmppath);
655 return err;
658 const struct got_error *
659 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
660 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
661 got_cancel_cb cancel_cb, void *cancel_arg)
663 const struct got_error *err = NULL;
664 struct got_fileindex *fileindex = NULL;
665 char *oldpath, *newpath;
666 struct imsgbuf *ibuf;
667 int imsg_fds[2] = {-1, -1};
668 int done = 0, failed = 0;
669 pid_t pid;
671 ibuf = calloc(1, sizeof(*ibuf));
672 if (ibuf == NULL) {
673 err = got_error_from_errno("calloc");
674 goto done;
677 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
678 err = got_error_from_errno("socketpair");
679 goto done;
682 pid = fork();
683 if (pid == -1) {
684 err = got_error_from_errno("fork");
685 goto done;
686 } else if (pid == 0) {
687 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
688 NULL);
689 /* not reached */
692 if (close(imsg_fds[1]) == -1) {
693 err = got_error_from_errno("close");
694 goto done;
696 imsg_fds[1] = -1;
697 imsg_init(ibuf, imsg_fds[0]);
699 err = send_patch(ibuf, fd);
700 fd = -1;
701 if (err)
702 goto done;
704 err = got_worktree_patch_prepare(&fileindex, worktree);
705 if (err)
706 goto done;
708 while (!done && err == NULL) {
709 struct got_patch p;
710 struct patch_args pa;
712 pa.progress_cb = progress_cb;
713 pa.progress_arg = progress_arg;
714 pa.head = &p.head;
716 err = recv_patch(ibuf, &done, &p);
717 if (err || done)
718 break;
720 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
721 &newpath, worktree, repo, fileindex);
722 if (err == NULL)
723 err = apply_patch(worktree, repo, oldpath, newpath,
724 &p, nop, &pa, cancel_cb, cancel_arg);
725 if (err != NULL) {
726 failed = 1;
727 /* recoverable errors */
728 if (err->code == GOT_ERR_FILE_STATUS ||
729 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
730 err = report_progress(&pa, p.old, p.new,
731 GOT_STATUS_CANNOT_UPDATE, err);
732 else if (err->code == GOT_ERR_HUNK_FAILED)
733 err = report_progress(&pa, p.old, p.new,
734 GOT_STATUS_CANNOT_UPDATE, NULL);
737 free(oldpath);
738 free(newpath);
739 patch_free(&p);
741 if (err)
742 break;
745 done:
746 if (fileindex)
747 got_worktree_patch_complete(fileindex);
748 if (fd != -1 && close(fd) == -1 && err == NULL)
749 err = got_error_from_errno("close");
750 if (ibuf != NULL)
751 imsg_clear(ibuf);
752 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
753 err = got_error_from_errno("close");
754 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
755 err = got_error_from_errno("close");
756 if (err == NULL && failed)
757 err = got_error(GOT_ERR_PATCH_FAILED);
758 return err;