2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
39 #include "got_error.h"
40 #include "got_object.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;
68 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
72 struct got_patch_hunk_head head;
76 got_patch_progress_cb progress_cb;
78 struct got_patch_hunk_head *head;
81 static const struct got_error *
82 send_patch(struct imsgbuf *ibuf, int fd)
84 const struct got_error *err = NULL;
86 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
88 err = got_error_from_errno(
89 "imsg_compose GOT_IMSG_PATCH_FILE");
94 if (imsg_flush(ibuf) == -1) {
95 err = got_error_from_errno("imsg_flush");
103 patch_free(struct got_patch *p)
105 struct got_patch_hunk *h;
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)
122 static const struct got_error *
123 pushline(struct got_patch_hunk *h, const char *line)
128 if (h->len == h->cap) {
129 if ((newcap = h->cap * 1.5) == 0)
131 t = recallocarray(h->lines, h->cap, newcap,
132 sizeof(h->lines[0]));
134 return got_error_from_errno("recallocarray");
139 if ((t = strdup(line)) == NULL)
140 return got_error_from_errno("strdup");
142 h->lines[h->len++] = t;
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;
151 struct got_imsg_patch_hunk hdr;
152 struct got_imsg_patch patch;
153 struct got_patch_hunk *h = NULL;
156 memset(p, 0, sizeof(*p));
157 STAILQ_INIT(&p->head);
159 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
162 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
166 if (imsg.hdr.type != GOT_IMSG_PATCH) {
167 err = got_error(GOT_ERR_PRIVSEP_MSG);
170 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
171 if (datalen != sizeof(patch)) {
172 err = got_error(GOT_ERR_PRIVSEP_LEN);
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");
180 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
181 err = got_error_from_errno("strdup");
184 if (p->old == NULL && p->new == NULL) {
185 err = got_error(GOT_ERR_PATCH_MALFORMED);
194 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
198 switch (imsg.hdr.type) {
199 case GOT_IMSG_PATCH_DONE:
201 case GOT_IMSG_PATCH_HUNK:
202 if (h != NULL && h->nonl) {
203 err = got_error(GOT_ERR_PATCH_MALFORMED);
206 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
207 if (datalen != sizeof(hdr)) {
208 err = got_error(GOT_ERR_PRIVSEP_LEN);
211 memcpy(&hdr, imsg.data, sizeof(hdr));
212 if ((h = calloc(1, sizeof(*h))) == NULL) {
213 err = got_error_from_errno("calloc");
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);
222 case GOT_IMSG_PATCH_LINE:
224 err = got_error(GOT_ERR_PRIVSEP_MSG);
227 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
229 /* at least one char */
230 if (datalen < 2 || t[datalen-1] != '\0') {
231 err = got_error(GOT_ERR_PRIVSEP_MSG);
234 if (*t != ' ' && *t != '-' && *t != '+' &&
236 err = got_error(GOT_ERR_PRIVSEP_MSG);
240 err = got_error(GOT_ERR_PATCH_MALFORMED);
244 err = pushline(h, t);
249 err = got_error(GOT_ERR_PRIVSEP_MSG);
262 * Copy data from orig starting at copypos until pos into tmp.
263 * If pos is -1, copy until EOF.
265 static const struct got_error *
266 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
271 if (fseek(orig, copypos, SEEK_SET) == -1)
272 return got_error_from_errno("fseek");
274 while (pos == -1 || copypos < pos) {
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);
283 return got_error_from_errno("fwrite");
285 if (r != len && feof(orig)) {
288 return got_error(GOT_ERR_HUNK_FAILED);
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;
299 char mode = *h->lines[0];
303 long match_lineno = -1;
306 linelen = getline(&line, &linesize, orig);
309 err = got_error_from_errno("getline");
310 else if (match == -1)
311 err = got_error(GOT_ERR_HUNK_FAILED);
314 if (line[linelen - 1] == '\n')
315 line[linelen - 1] = '\0';
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);
323 err = got_error_from_errno("ftello");
327 match_lineno = (*lineno)-1;
330 if (*lineno >= h->old_from && match != -1)
336 *lineno = match_lineno;
337 if (fseek(orig, match, SEEK_SET) == -1)
338 err = got_error_from_errno("fseek");
345 static const struct got_error *
346 test_hunk(FILE *orig, struct got_patch_hunk *h)
348 const struct got_error *err = NULL;
350 size_t linesize = 0, i = 0;
353 for (i = 0; i < h->len; ++i) {
354 switch (*h->lines[i]) {
359 linelen = getline(&line, &linesize, orig);
362 err = got_error_from_errno("getline");
365 GOT_ERR_HUNK_FAILED);
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);
383 static const struct got_error *
384 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
388 for (i = 0; i < h->len; ++i) {
389 switch (*h->lines[i]) {
391 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
392 return got_error_from_errno("fprintf");
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(
411 static const struct got_error *
412 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
415 const struct got_error *err = NULL;
416 struct got_patch_hunk *h;
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);
431 return apply_hunk(tmp, h, &lineno);
434 if ((orig = fopen(path, "r")) == NULL) {
435 err = got_error_from_errno2("fopen", path);
439 if (fstat(fileno(orig), &sb) == -1) {
440 err = got_error_from_errno("fstat");
446 STAILQ_FOREACH(h, &p->head, entries) {
447 if (h->lines == NULL)
451 err = locate_hunk(orig, h, &pos, &lineno);
452 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
457 err = copy(tmp, orig, copypos, pos);
462 err = test_hunk(orig, h);
463 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
465 * try to apply the hunk again starting the search
466 * after the previous partial match.
468 if (fseek(orig, pos, SEEK_SET) == -1) {
469 err = got_error_from_errno("fseek");
472 linelen = getline(&line, &linesize, orig);
474 err = got_error_from_errno("getline");
483 if (lineno + 1 != h->old_from)
484 h->offset = lineno + 1 - h->old_from;
487 err = apply_hunk(tmp, h, &lineno);
491 copypos = ftello(orig);
493 err = got_error_from_errno("ftello");
498 if (p->new == NULL && sb.st_size != copypos) {
499 h = STAILQ_FIRST(&p->head);
500 h->err = got_error(GOT_ERR_HUNK_FAILED);
502 } else if (!nop && !feof(orig))
503 err = copy(tmp, orig, copypos, -1);
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);
523 STAILQ_FOREACH(h, pa->head, entries) {
524 if (h->offset == 0 && h->err == NULL)
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,
537 static const struct got_error *
538 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
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;;
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);
569 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
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);
582 err = got_opentemp_named(&tmppath, &tmp, template);
585 err = patch_file(p, oldpath, tmp, nop, &mode);
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);
598 if (fchmod(fileno(tmp), mode) == -1) {
599 err = got_error_from_errno2("chmod", newpath);
603 if (rename(tmppath, newpath) == -1) {
604 if (errno != ENOENT) {
605 err = got_error_from_errno3("rename", tmppath,
610 err = got_path_dirname(&parent, newpath);
613 err = got_path_mkdir(parent);
616 if (rename(tmppath, newpath) == -1) {
617 err = got_error_from_errno3("rename", tmppath,
624 err = got_worktree_schedule_delete(worktree, &oldpaths,
625 0, NULL, patch_delete, pa, repo, 0, 0);
627 err = got_worktree_schedule_add(worktree, &newpaths,
628 patch_add, pa, repo, 1);
631 } else if (p->old == NULL) {
632 err = got_worktree_schedule_add(worktree, &newpaths,
633 patch_add, pa, repo, 1);
637 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
641 got_pathlist_free(&oldpaths);
642 got_pathlist_free(&newpaths);
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;
664 ibuf = calloc(1, sizeof(*ibuf));
666 err = got_error_from_errno("calloc");
670 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
671 err = got_error_from_errno("socketpair");
677 err = got_error_from_errno("fork");
679 } else if (pid == 0) {
680 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
685 if (close(imsg_fds[1]) == -1) {
686 err = got_error_from_errno("close");
690 imsg_init(ibuf, imsg_fds[0]);
692 err = send_patch(ibuf, fd);
697 err = got_worktree_patch_prepare(&fileindex, worktree);
701 while (!done && err == NULL) {
703 struct patch_args pa;
705 pa.progress_cb = progress_cb;
706 pa.progress_arg = progress_arg;
709 err = recv_patch(ibuf, &done, &p);
713 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
714 &newpath, worktree, repo, fileindex);
716 err = apply_patch(worktree, repo, oldpath, newpath,
717 &p, nop, &pa, cancel_cb, cancel_arg);
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);
740 got_worktree_patch_complete(fileindex);
741 if (fd != -1 && close(fd) == -1 && err == NULL)
742 err = got_error_from_errno("close");
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);