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/socket.h>
36 #include "got_error.h"
37 #include "got_object.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;
65 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
69 struct got_patch_hunk_head head;
73 got_patch_progress_cb progress_cb;
75 struct got_patch_hunk_head *head;
78 static const struct got_error *
79 send_patch(struct imsgbuf *ibuf, int fd)
81 const struct got_error *err = NULL;
83 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
85 err = got_error_from_errno(
86 "imsg_compose GOT_IMSG_PATCH_FILE");
91 if (imsg_flush(ibuf) == -1) {
92 err = got_error_from_errno("imsg_flush");
100 patch_free(struct got_patch *p)
102 struct got_patch_hunk *h;
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)
119 static const struct got_error *
120 pushline(struct got_patch_hunk *h, const char *line)
125 if (h->len == h->cap) {
126 if ((newcap = h->cap * 1.5) == 0)
128 t = recallocarray(h->lines, h->cap, newcap,
129 sizeof(h->lines[0]));
131 return got_error_from_errno("recallocarray");
136 if ((t = strdup(line)) == NULL)
137 return got_error_from_errno("strdup");
139 h->lines[h->len++] = t;
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;
148 struct got_imsg_patch_hunk hdr;
149 struct got_imsg_patch patch;
150 struct got_patch_hunk *h = NULL;
153 memset(p, 0, sizeof(*p));
154 STAILQ_INIT(&p->head);
156 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
159 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
163 if (imsg.hdr.type != GOT_IMSG_PATCH) {
164 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
168 if (datalen != sizeof(patch)) {
169 err = got_error(GOT_ERR_PRIVSEP_LEN);
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");
177 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
178 err = got_error_from_errno("strdup");
181 if (p->old == NULL && p->new == NULL) {
182 err = got_error(GOT_ERR_PATCH_MALFORMED);
191 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
195 switch (imsg.hdr.type) {
196 case GOT_IMSG_PATCH_DONE:
198 case GOT_IMSG_PATCH_HUNK:
199 if (h != NULL && h->nonl) {
200 err = got_error(GOT_ERR_PATCH_MALFORMED);
203 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
204 if (datalen != sizeof(hdr)) {
205 err = got_error(GOT_ERR_PRIVSEP_LEN);
208 memcpy(&hdr, imsg.data, sizeof(hdr));
209 if ((h = calloc(1, sizeof(*h))) == NULL) {
210 err = got_error_from_errno("calloc");
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);
219 case GOT_IMSG_PATCH_LINE:
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
226 /* at least one char */
227 if (datalen < 2 || t[datalen-1] != '\0') {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
231 if (*t != ' ' && *t != '-' && *t != '+' &&
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
237 err = got_error(GOT_ERR_PATCH_MALFORMED);
241 err = pushline(h, t);
246 err = got_error(GOT_ERR_PRIVSEP_MSG);
259 * Copy data from orig starting at copypos until pos into tmp.
260 * If pos is -1, copy until EOF.
262 static const struct got_error *
263 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
268 if (fseek(orig, copypos, SEEK_SET) == -1)
269 return got_error_from_errno("fseek");
271 while (pos == -1 || copypos < pos) {
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);
280 return got_error_from_errno("fwrite");
282 if (r != len && feof(orig)) {
285 return got_error(GOT_ERR_HUNK_FAILED);
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;
296 char mode = *h->lines[0];
300 long match_lineno = -1;
303 linelen = getline(&line, &linesize, orig);
306 err = got_error_from_errno("getline");
307 else if (match == -1)
308 err = got_error(GOT_ERR_HUNK_FAILED);
311 if (line[linelen - 1] == '\n')
312 line[linelen - 1] = '\0';
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);
320 err = got_error_from_errno("ftello");
324 match_lineno = (*lineno)-1;
327 if (*lineno >= h->old_from && match != -1)
333 *lineno = match_lineno;
334 if (fseek(orig, match, SEEK_SET) == -1)
335 err = got_error_from_errno("fseek");
342 static const struct got_error *
343 test_hunk(FILE *orig, struct got_patch_hunk *h)
345 const struct got_error *err = NULL;
347 size_t linesize = 0, i = 0;
350 for (i = 0; i < h->len; ++i) {
351 switch (*h->lines[i]) {
356 linelen = getline(&line, &linesize, orig);
359 err = got_error_from_errno("getline");
362 GOT_ERR_HUNK_FAILED);
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);
380 static const struct got_error *
381 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
385 for (i = 0; i < h->len; ++i) {
386 switch (*h->lines[i]) {
388 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
389 return got_error_from_errno("fprintf");
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(
408 static const struct got_error *
409 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
412 const struct got_error *err = NULL;
413 struct got_patch_hunk *h;
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);
428 return apply_hunk(tmp, h, &lineno);
431 if ((orig = fopen(path, "r")) == NULL) {
432 err = got_error_from_errno2("fopen", path);
436 if (fstat(fileno(orig), &sb) == -1) {
437 err = got_error_from_errno("fstat");
443 STAILQ_FOREACH(h, &p->head, entries) {
444 if (h->lines == NULL)
448 err = locate_hunk(orig, h, &pos, &lineno);
449 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
454 err = copy(tmp, orig, copypos, pos);
459 err = test_hunk(orig, h);
460 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
462 * try to apply the hunk again starting the search
463 * after the previous partial match.
465 if (fseek(orig, pos, SEEK_SET) == -1) {
466 err = got_error_from_errno("fseek");
469 linelen = getline(&line, &linesize, orig);
471 err = got_error_from_errno("getline");
480 if (lineno + 1 != h->old_from)
481 h->offset = lineno + 1 - h->old_from;
484 err = apply_hunk(tmp, h, &lineno);
488 copypos = ftello(orig);
490 err = got_error_from_errno("ftello");
495 if (p->new == NULL && sb.st_size != copypos) {
496 h = STAILQ_FIRST(&p->head);
497 h->err = got_error(GOT_ERR_HUNK_FAILED);
499 } else if (!nop && !feof(orig))
500 err = copy(tmp, orig, copypos, -1);
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);
520 STAILQ_FOREACH(h, pa->head, entries) {
521 if (h->offset == 0 && h->err == NULL)
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,
534 static const struct got_error *
535 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
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;;
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);
566 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
570 file_renamed = strcmp(oldpath, newpath);
572 if (p->old != NULL && p->new == NULL) {
574 * special case: delete a file. don't try to match
575 * the lines but just schedule the removal.
577 err = got_worktree_schedule_delete(worktree, &oldpaths,
578 0, NULL, patch_delete, pa, repo, 0, 0);
582 if (asprintf(&template, "%s/got-patch",
583 got_worktree_get_root_path(worktree)) == -1) {
584 err = got_error_from_errno(template);
589 err = got_opentemp_named(&tmppath, &tmp, template);
592 err = patch_file(p, oldpath, tmp, nop, &mode);
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);
605 if (fchmod(fileno(tmp), mode) == -1) {
606 err = got_error_from_errno2("chmod", newpath);
610 if (rename(tmppath, newpath) == -1) {
611 if (errno != ENOENT) {
612 err = got_error_from_errno3("rename", tmppath,
617 err = got_path_dirname(&parent, newpath);
620 err = got_path_mkdir(parent);
623 if (rename(tmppath, newpath) == -1) {
624 err = got_error_from_errno3("rename", tmppath,
631 err = got_worktree_schedule_delete(worktree, &oldpaths,
632 0, NULL, patch_delete, pa, repo, 0, 0);
634 err = got_worktree_schedule_add(worktree, &newpaths,
635 patch_add, pa, repo, 1);
638 } else if (p->old == NULL) {
639 err = got_worktree_schedule_add(worktree, &newpaths,
640 patch_add, pa, repo, 1);
644 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
648 got_pathlist_free(&oldpaths);
649 got_pathlist_free(&newpaths);
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;
671 ibuf = calloc(1, sizeof(*ibuf));
673 err = got_error_from_errno("calloc");
677 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
678 err = got_error_from_errno("socketpair");
684 err = got_error_from_errno("fork");
686 } else if (pid == 0) {
687 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
692 if (close(imsg_fds[1]) == -1) {
693 err = got_error_from_errno("close");
697 imsg_init(ibuf, imsg_fds[0]);
699 err = send_patch(ibuf, fd);
704 err = got_worktree_patch_prepare(&fileindex, worktree);
708 while (!done && err == NULL) {
710 struct patch_args pa;
712 pa.progress_cb = progress_cb;
713 pa.progress_arg = progress_arg;
716 err = recv_patch(ibuf, &done, &p);
720 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
721 &newpath, worktree, repo, fileindex);
723 err = apply_patch(worktree, repo, oldpath, newpath,
724 &p, nop, &pa, cancel_cb, cancel_arg);
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);
747 got_worktree_patch_complete(fileindex);
748 if (fd != -1 && close(fd) == -1 && err == NULL)
749 err = got_error_from_errno("close");
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);