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)
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));
176 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
177 err = got_error_from_errno("strdup");
178 goto done;
180 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
181 err = got_error_from_errno("strdup");
182 goto done;
184 if (p->old == NULL && p->new == NULL) {
185 err = got_error(GOT_ERR_PATCH_MALFORMED);
186 goto done;
189 imsg_free(&imsg);
191 for (;;) {
192 char *t;
194 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
195 if (err)
196 return err;
198 switch (imsg.hdr.type) {
199 case GOT_IMSG_PATCH_DONE:
200 goto done;
201 case GOT_IMSG_PATCH_HUNK:
202 if (h != NULL && h->nonl) {
203 err = got_error(GOT_ERR_PATCH_MALFORMED);
204 goto done;
206 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
207 if (datalen != sizeof(hdr)) {
208 err = got_error(GOT_ERR_PRIVSEP_LEN);
209 goto done;
211 memcpy(&hdr, imsg.data, sizeof(hdr));
212 if ((h = calloc(1, sizeof(*h))) == NULL) {
213 err = got_error_from_errno("calloc");
214 goto done;
216 h->old_from = hdr.oldfrom;
217 h->old_lines = hdr.oldlines;
218 h->new_from = hdr.newfrom;
219 h->new_lines = hdr.newlines;
220 STAILQ_INSERT_TAIL(&p->head, h, entries);
221 break;
222 case GOT_IMSG_PATCH_LINE:
223 if (h == NULL) {
224 err = got_error(GOT_ERR_PRIVSEP_MSG);
225 goto done;
227 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
228 t = imsg.data;
229 /* at least one char */
230 if (datalen < 2 || t[datalen-1] != '\0') {
231 err = got_error(GOT_ERR_PRIVSEP_MSG);
232 goto done;
234 if (*t != ' ' && *t != '-' && *t != '+' &&
235 *t != '\\') {
236 err = got_error(GOT_ERR_PRIVSEP_MSG);
237 goto done;
239 if (h->nonl)
240 err = got_error(GOT_ERR_PATCH_MALFORMED);
241 if (*t == '\\')
242 h->nonl = 1;
243 else
244 err = pushline(h, t);
245 if (err)
246 goto done;
247 break;
248 default:
249 err = got_error(GOT_ERR_PRIVSEP_MSG);
250 goto done;
253 imsg_free(&imsg);
256 done:
257 imsg_free(&imsg);
258 return err;
261 /*
262 * Copy data from orig starting at copypos until pos into tmp.
263 * If pos is -1, copy until EOF.
264 */
265 static const struct got_error *
266 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
268 char buf[BUFSIZ];
269 size_t len, r, w;
271 if (fseek(orig, copypos, SEEK_SET) == -1)
272 return got_error_from_errno("fseek");
274 while (pos == -1 || copypos < pos) {
275 len = sizeof(buf);
276 if (pos > 0)
277 len = MIN(len, (size_t)pos - copypos);
278 r = fread(buf, 1, len, orig);
279 if (r != len && ferror(orig))
280 return got_error_from_errno("fread");
281 w = fwrite(buf, 1, r, tmp);
282 if (w != r)
283 return got_error_from_errno("fwrite");
284 copypos += len;
285 if (r != len && feof(orig)) {
286 if (pos == -1)
287 return NULL;
288 return got_error(GOT_ERR_HUNK_FAILED);
291 return NULL;
294 static const struct got_error *
295 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
297 const struct got_error *err = NULL;
298 char *line = NULL;
299 char mode = *h->lines[0];
300 size_t linesize = 0;
301 ssize_t linelen;
302 off_t match = -1;
303 long match_lineno = -1;
305 for (;;) {
306 linelen = getline(&line, &linesize, orig);
307 if (linelen == -1) {
308 if (ferror(orig))
309 err = got_error_from_errno("getline");
310 else if (match == -1)
311 err = got_error(GOT_ERR_HUNK_FAILED);
312 break;
314 if (line[linelen - 1] == '\n')
315 line[linelen - 1] = '\0';
316 (*lineno)++;
318 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
319 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
320 (mode == '+' && *lineno == h->old_from)) {
321 match = ftello(orig);
322 if (match == -1) {
323 err = got_error_from_errno("ftello");
324 break;
326 match -= linelen;
327 match_lineno = (*lineno)-1;
330 if (*lineno >= h->old_from && match != -1)
331 break;
334 if (err == NULL) {
335 *pos = match;
336 *lineno = match_lineno;
337 if (fseek(orig, match, SEEK_SET) == -1)
338 err = got_error_from_errno("fseek");
341 free(line);
342 return err;
345 static const struct got_error *
346 test_hunk(FILE *orig, struct got_patch_hunk *h)
348 const struct got_error *err = NULL;
349 char *line = NULL;
350 size_t linesize = 0, i = 0;
351 ssize_t linelen;
353 for (i = 0; i < h->len; ++i) {
354 switch (*h->lines[i]) {
355 case '+':
356 continue;
357 case ' ':
358 case '-':
359 linelen = getline(&line, &linesize, orig);
360 if (linelen == -1) {
361 if (ferror(orig))
362 err = got_error_from_errno("getline");
363 else
364 err = got_error(
365 GOT_ERR_HUNK_FAILED);
366 goto done;
368 if (line[linelen - 1] == '\n')
369 line[linelen - 1] = '\0';
370 if (strcmp(h->lines[i] + 1, line)) {
371 err = got_error(GOT_ERR_HUNK_FAILED);
372 goto done;
374 break;
378 done:
379 free(line);
380 return err;
383 static const struct got_error *
384 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
386 size_t i = 0;
388 for (i = 0; i < h->len; ++i) {
389 switch (*h->lines[i]) {
390 case ' ':
391 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
392 return got_error_from_errno("fprintf");
393 /* fallthrough */
394 case '-':
395 (*lineno)++;
396 break;
397 case '+':
398 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
399 return got_error_from_errno("fprintf");
400 if (i != h->len - 1 || !h->nonl) {
401 if (fprintf(tmp, "\n") < 0)
402 return got_error_from_errno(
403 "fprintf");
405 break;
408 return NULL;
411 static const struct got_error *
412 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
413 mode_t *mode)
415 const struct got_error *err = NULL;
416 struct got_patch_hunk *h;
417 struct stat sb;
418 long lineno = 0;
419 FILE *orig;
420 off_t copypos, pos;
421 char *line = NULL;
422 size_t linesize = 0;
423 ssize_t linelen;
425 if (p->old == NULL) { /* create */
426 h = STAILQ_FIRST(&p->head);
427 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
428 return got_error(GOT_ERR_PATCH_MALFORMED);
429 if (nop)
430 return NULL;
431 return apply_hunk(tmp, h, &lineno);
434 if ((orig = fopen(path, "r")) == NULL) {
435 err = got_error_from_errno2("fopen", path);
436 goto done;
439 if (fstat(fileno(orig), &sb) == -1) {
440 err = got_error_from_errno("fstat");
441 goto done;
443 *mode = sb.st_mode;
445 copypos = 0;
446 STAILQ_FOREACH(h, &p->head, entries) {
447 if (h->lines == NULL)
448 break;
450 tryagain:
451 err = locate_hunk(orig, h, &pos, &lineno);
452 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
453 h->err = err;
454 if (err != NULL)
455 goto done;
456 if (!nop)
457 err = copy(tmp, orig, copypos, pos);
458 if (err != NULL)
459 goto done;
460 copypos = pos;
462 err = test_hunk(orig, h);
463 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
464 /*
465 * try to apply the hunk again starting the search
466 * after the previous partial match.
467 */
468 if (fseek(orig, pos, SEEK_SET) == -1) {
469 err = got_error_from_errno("fseek");
470 goto done;
472 linelen = getline(&line, &linesize, orig);
473 if (linelen == -1) {
474 err = got_error_from_errno("getline");
475 goto done;
477 lineno++;
478 goto tryagain;
480 if (err != NULL)
481 goto done;
483 if (lineno + 1 != h->old_from)
484 h->offset = lineno + 1 - h->old_from;
486 if (!nop)
487 err = apply_hunk(tmp, h, &lineno);
488 if (err != NULL)
489 goto done;
491 copypos = ftello(orig);
492 if (copypos == -1) {
493 err = got_error_from_errno("ftello");
494 goto done;
498 if (p->new == NULL && sb.st_size != copypos) {
499 h = STAILQ_FIRST(&p->head);
500 h->err = got_error(GOT_ERR_HUNK_FAILED);
501 err = h->err;
502 } else if (!nop && !feof(orig))
503 err = copy(tmp, orig, copypos, -1);
505 done:
506 if (orig != NULL)
507 fclose(orig);
508 return err;
511 static const struct got_error *
512 report_progress(struct patch_args *pa, const char *old, const char *new,
513 unsigned char status, const struct got_error *orig_error)
515 const struct got_error *err;
516 struct got_patch_hunk *h;
518 err = pa->progress_cb(pa->progress_arg, old, new, status,
519 orig_error, 0, 0, 0, 0, 0, NULL);
520 if (err)
521 return err;
523 STAILQ_FOREACH(h, pa->head, entries) {
524 if (h->offset == 0 && h->err == NULL)
525 continue;
527 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
528 h->old_from, h->old_lines, h->new_from, h->new_lines,
529 h->offset, h->err);
530 if (err)
531 return err;
534 return NULL;
537 static const struct got_error *
538 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
539 const char *path)
541 return report_progress(arg, path, NULL, status, NULL);
544 static const struct got_error *
545 patch_add(void *arg, unsigned char status, const char *path)
547 return report_progress(arg, NULL, path, status, NULL);
550 static const struct got_error *
551 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
552 const char *oldpath, const char *newpath, struct got_patch *p,
553 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
555 const struct got_error *err = NULL;
556 struct got_pathlist_head oldpaths, newpaths;
557 struct got_pathlist_entry *pe;
558 int file_renamed = 0;
559 char *tmppath = NULL, *template = NULL, *parent = NULL;;
560 FILE *tmp = NULL;
561 mode_t mode = GOT_DEFAULT_FILE_MODE;
563 TAILQ_INIT(&oldpaths);
564 TAILQ_INIT(&newpaths);
566 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
567 if (err)
568 goto done;
569 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
570 if (err)
571 goto done;
573 file_renamed = strcmp(oldpath, newpath);
575 if (asprintf(&template, "%s/got-patch",
576 got_worktree_get_root_path(worktree)) == -1) {
577 err = got_error_from_errno(template);
578 goto done;
581 if (!nop)
582 err = got_opentemp_named(&tmppath, &tmp, template);
583 if (err)
584 goto done;
585 err = patch_file(p, oldpath, tmp, nop, &mode);
586 if (err)
587 goto done;
589 if (nop)
590 goto done;
592 if (p->old != NULL && p->new == NULL) {
593 err = got_worktree_schedule_delete(worktree, &oldpaths,
594 0, NULL, patch_delete, pa, repo, 0, 0);
595 goto done;
598 if (fchmod(fileno(tmp), mode) == -1) {
599 err = got_error_from_errno2("chmod", newpath);
600 goto done;
603 if (rename(tmppath, newpath) == -1) {
604 if (errno != ENOENT) {
605 err = got_error_from_errno3("rename", tmppath,
606 newpath);
607 goto done;
610 err = got_path_dirname(&parent, newpath);
611 if (err != NULL)
612 goto done;
613 err = got_path_mkdir(parent);
614 if (err != NULL)
615 goto done;
616 if (rename(tmppath, newpath) == -1) {
617 err = got_error_from_errno3("rename", tmppath,
618 newpath);
619 goto done;
623 if (file_renamed) {
624 err = got_worktree_schedule_delete(worktree, &oldpaths,
625 0, NULL, patch_delete, pa, repo, 0, 0);
626 if (err == NULL)
627 err = got_worktree_schedule_add(worktree, &newpaths,
628 patch_add, pa, repo, 1);
629 if (err)
630 unlink(newpath);
631 } else if (p->old == NULL) {
632 err = got_worktree_schedule_add(worktree, &newpaths,
633 patch_add, pa, repo, 1);
634 if (err)
635 unlink(newpath);
636 } else
637 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
638 NULL);
640 done:
641 got_pathlist_free(&oldpaths);
642 got_pathlist_free(&newpaths);
643 free(parent);
644 free(template);
645 if (tmppath != NULL)
646 unlink(tmppath);
647 free(tmppath);
648 return err;
651 const struct got_error *
652 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
653 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
654 got_cancel_cb cancel_cb, void *cancel_arg)
656 const struct got_error *err = NULL;
657 struct got_fileindex *fileindex = NULL;
658 char *oldpath, *newpath;
659 struct imsgbuf *ibuf;
660 int imsg_fds[2] = {-1, -1};
661 int done = 0, failed = 0;
662 pid_t pid;
664 ibuf = calloc(1, sizeof(*ibuf));
665 if (ibuf == NULL) {
666 err = got_error_from_errno("calloc");
667 goto done;
670 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
671 err = got_error_from_errno("socketpair");
672 goto done;
675 pid = fork();
676 if (pid == -1) {
677 err = got_error_from_errno("fork");
678 goto done;
679 } else if (pid == 0) {
680 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
681 NULL);
682 /* not reached */
685 if (close(imsg_fds[1]) == -1) {
686 err = got_error_from_errno("close");
687 goto done;
689 imsg_fds[1] = -1;
690 imsg_init(ibuf, imsg_fds[0]);
692 err = send_patch(ibuf, fd);
693 fd = -1;
694 if (err)
695 goto done;
697 err = got_worktree_patch_prepare(&fileindex, worktree);
698 if (err)
699 goto done;
701 while (!done && err == NULL) {
702 struct got_patch p;
703 struct patch_args pa;
705 pa.progress_cb = progress_cb;
706 pa.progress_arg = progress_arg;
707 pa.head = &p.head;
709 err = recv_patch(ibuf, &done, &p);
710 if (err || done)
711 break;
713 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
714 &newpath, worktree, repo, fileindex);
715 if (err == NULL)
716 err = apply_patch(worktree, repo, oldpath, newpath,
717 &p, nop, &pa, cancel_cb, cancel_arg);
718 if (err != NULL) {
719 failed = 1;
720 /* recoverable errors */
721 if (err->code == GOT_ERR_FILE_STATUS ||
722 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
723 err = report_progress(&pa, p.old, p.new,
724 GOT_STATUS_CANNOT_UPDATE, err);
725 else if (err->code == GOT_ERR_HUNK_FAILED)
726 err = report_progress(&pa, p.old, p.new,
727 GOT_STATUS_CANNOT_UPDATE, NULL);
730 free(oldpath);
731 free(newpath);
732 patch_free(&p);
734 if (err)
735 break;
738 done:
739 if (fileindex)
740 got_worktree_patch_complete(fileindex);
741 if (fd != -1 && close(fd) == -1 && err == NULL)
742 err = got_error_from_errno("close");
743 if (ibuf != NULL)
744 imsg_clear(ibuf);
745 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
746 err = got_error_from_errno("close");
747 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
748 err = got_error_from_errno("close");
749 if (err == NULL && failed)
750 err = got_error(GOT_ERR_PATCH_FAILED);
751 return err;