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>
38 #include "got_error.h"
39 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
45 #include "got_patch.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 struct got_patch_hunk {
54 STAILQ_ENTRY(got_patch_hunk) entries;
55 const struct got_error *err;
67 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
71 struct got_patch_hunk_head head;
75 got_patch_progress_cb progress_cb;
77 struct got_patch_hunk_head *head;
80 static const struct got_error *
81 send_patch(struct imsgbuf *ibuf, int fd)
83 const struct got_error *err = NULL;
85 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
87 err = got_error_from_errno(
88 "imsg_compose GOT_IMSG_PATCH_FILE");
93 if (imsg_flush(ibuf) == -1) {
94 err = got_error_from_errno("imsg_flush");
102 patch_free(struct got_patch *p)
104 struct got_patch_hunk *h;
107 while (!STAILQ_EMPTY(&p->head)) {
108 h = STAILQ_FIRST(&p->head);
109 STAILQ_REMOVE_HEAD(&p->head, entries);
111 for (i = 0; i < h->len; ++i)
121 static const struct got_error *
122 pushline(struct got_patch_hunk *h, const char *line)
127 if (h->len == h->cap) {
128 if ((newcap = h->cap * 1.5) == 0)
130 t = recallocarray(h->lines, h->cap, newcap,
131 sizeof(h->lines[0]));
133 return got_error_from_errno("recallocarray");
138 if ((t = strdup(line)) == NULL)
139 return got_error_from_errno("strdup");
141 h->lines[h->len++] = t;
145 static const struct got_error *
146 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
148 const struct got_error *err = NULL;
150 struct got_imsg_patch_hunk hdr;
151 struct got_imsg_patch patch;
152 struct got_patch_hunk *h = NULL;
155 memset(p, 0, sizeof(*p));
156 STAILQ_INIT(&p->head);
158 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
161 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
165 if (imsg.hdr.type != GOT_IMSG_PATCH) {
166 err = got_error(GOT_ERR_PRIVSEP_MSG);
169 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
170 if (datalen != sizeof(patch)) {
171 err = got_error(GOT_ERR_PRIVSEP_LEN);
174 memcpy(&patch, imsg.data, sizeof(patch));
175 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
176 err = got_error_from_errno("strdup");
179 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
180 err = got_error_from_errno("strdup");
183 if (p->old == NULL && p->new == NULL) {
184 err = got_error(GOT_ERR_PATCH_MALFORMED);
193 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
197 switch (imsg.hdr.type) {
198 case GOT_IMSG_PATCH_DONE:
200 case GOT_IMSG_PATCH_HUNK:
201 if (h != NULL && h->nonl) {
202 err = got_error(GOT_ERR_PATCH_MALFORMED);
205 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
206 if (datalen != sizeof(hdr)) {
207 err = got_error(GOT_ERR_PRIVSEP_LEN);
210 memcpy(&hdr, imsg.data, sizeof(hdr));
211 if ((h = calloc(1, sizeof(*h))) == NULL) {
212 err = got_error_from_errno("calloc");
215 h->old_from = hdr.oldfrom;
216 h->old_lines = hdr.oldlines;
217 h->new_from = hdr.newfrom;
218 h->new_lines = hdr.newlines;
219 STAILQ_INSERT_TAIL(&p->head, h, entries);
221 case GOT_IMSG_PATCH_LINE:
223 err = got_error(GOT_ERR_PRIVSEP_MSG);
226 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
228 /* at least one char */
229 if (datalen < 2 || t[datalen-1] != '\0') {
230 err = got_error(GOT_ERR_PRIVSEP_MSG);
233 if (*t != ' ' && *t != '-' && *t != '+' &&
235 err = got_error(GOT_ERR_PRIVSEP_MSG);
239 err = got_error(GOT_ERR_PATCH_MALFORMED);
243 err = pushline(h, t);
248 err = got_error(GOT_ERR_PRIVSEP_MSG);
261 * Copy data from orig starting at copypos until pos into tmp.
262 * If pos is -1, copy until EOF.
264 static const struct got_error *
265 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
270 if (fseek(orig, copypos, SEEK_SET) == -1)
271 return got_error_from_errno("fseek");
273 while (pos == -1 || copypos < pos) {
276 len = MIN(len, (size_t)pos - copypos);
277 r = fread(buf, 1, len, orig);
278 if (r != len && ferror(orig))
279 return got_error_from_errno("fread");
280 w = fwrite(buf, 1, r, tmp);
282 return got_error_from_errno("fwrite");
284 if (r != len && feof(orig)) {
287 return got_error(GOT_ERR_HUNK_FAILED);
293 static const struct got_error *
294 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
296 const struct got_error *err = NULL;
298 char mode = *h->lines[0];
302 long match_lineno = -1;
305 linelen = getline(&line, &linesize, orig);
308 err = got_error_from_errno("getline");
309 else if (match == -1)
310 err = got_error(GOT_ERR_HUNK_FAILED);
313 if (line[linelen - 1] == '\n')
314 line[linelen - 1] = '\0';
317 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
318 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
319 (mode == '+' && *lineno == h->old_from)) {
320 match = ftello(orig);
322 err = got_error_from_errno("ftello");
326 match_lineno = (*lineno)-1;
329 if (*lineno >= h->old_from && match != -1)
335 *lineno = match_lineno;
336 if (fseek(orig, match, SEEK_SET) == -1)
337 err = got_error_from_errno("fseek");
344 static const struct got_error *
345 test_hunk(FILE *orig, struct got_patch_hunk *h)
347 const struct got_error *err = NULL;
349 size_t linesize = 0, i = 0;
352 for (i = 0; i < h->len; ++i) {
353 switch (*h->lines[i]) {
358 linelen = getline(&line, &linesize, orig);
361 err = got_error_from_errno("getline");
364 GOT_ERR_HUNK_FAILED);
367 if (line[linelen - 1] == '\n')
368 line[linelen - 1] = '\0';
369 if (strcmp(h->lines[i] + 1, line)) {
370 err = got_error(GOT_ERR_HUNK_FAILED);
382 static const struct got_error *
383 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
387 for (i = 0; i < h->len; ++i) {
388 switch (*h->lines[i]) {
390 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
391 return got_error_from_errno("fprintf");
397 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
398 return got_error_from_errno("fprintf");
399 if (i != h->len - 1 || !h->nonl) {
400 if (fprintf(tmp, "\n") < 0)
401 return got_error_from_errno(
410 static const struct got_error *
411 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
414 const struct got_error *err = NULL;
415 struct got_patch_hunk *h;
424 if (p->old == NULL) { /* create */
425 h = STAILQ_FIRST(&p->head);
426 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
427 return got_error(GOT_ERR_PATCH_MALFORMED);
430 return apply_hunk(tmp, h, &lineno);
433 if ((orig = fopen(path, "r")) == NULL) {
434 err = got_error_from_errno2("fopen", path);
438 if (fstat(fileno(orig), &sb) == -1) {
439 err = got_error_from_errno("fstat");
445 STAILQ_FOREACH(h, &p->head, entries) {
446 if (h->lines == NULL)
450 err = locate_hunk(orig, h, &pos, &lineno);
451 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
456 err = copy(tmp, orig, copypos, pos);
461 err = test_hunk(orig, h);
462 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
464 * try to apply the hunk again starting the search
465 * after the previous partial match.
467 if (fseek(orig, pos, SEEK_SET) == -1) {
468 err = got_error_from_errno("fseek");
471 linelen = getline(&line, &linesize, orig);
473 err = got_error_from_errno("getline");
482 if (lineno + 1 != h->old_from)
483 h->offset = lineno + 1 - h->old_from;
486 err = apply_hunk(tmp, h, &lineno);
490 copypos = ftello(orig);
492 err = got_error_from_errno("ftello");
497 if (p->new == NULL && sb.st_size != copypos) {
498 h = STAILQ_FIRST(&p->head);
499 h->err = got_error(GOT_ERR_HUNK_FAILED);
501 } else if (!nop && !feof(orig))
502 err = copy(tmp, orig, copypos, -1);
510 static const struct got_error *
511 report_progress(struct patch_args *pa, const char *old, const char *new,
512 unsigned char status, const struct got_error *orig_error)
514 const struct got_error *err;
515 struct got_patch_hunk *h;
517 err = pa->progress_cb(pa->progress_arg, old, new, status,
518 orig_error, 0, 0, 0, 0, 0, NULL);
522 STAILQ_FOREACH(h, pa->head, entries) {
523 if (h->offset == 0 && h->err == NULL)
526 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
527 h->old_from, h->old_lines, h->new_from, h->new_lines,
536 static const struct got_error *
537 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
540 return report_progress(arg, path, NULL, status, NULL);
543 static const struct got_error *
544 patch_add(void *arg, unsigned char status, const char *path)
546 return report_progress(arg, NULL, path, status, NULL);
549 static const struct got_error *
550 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
551 const char *oldpath, const char *newpath, struct got_patch *p,
552 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
554 const struct got_error *err = NULL;
555 struct got_pathlist_head oldpaths, newpaths;
556 struct got_pathlist_entry *pe;
557 int file_renamed = 0;
558 char *tmppath = NULL, *template = NULL, *parent = NULL;;
560 mode_t mode = GOT_DEFAULT_FILE_MODE;
562 TAILQ_INIT(&oldpaths);
563 TAILQ_INIT(&newpaths);
565 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
568 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
572 file_renamed = strcmp(oldpath, newpath);
574 if (p->old != NULL && p->new == NULL) {
576 * special case: delete a file. don't try to match
577 * the lines but just schedule the removal.
579 err = got_worktree_schedule_delete(worktree, &oldpaths,
580 0, NULL, delete_cb, delete_arg, repo, 0, 0);
584 if (asprintf(&template, "%s/got-patch",
585 got_worktree_get_root_path(worktree)) == -1) {
586 err = got_error_from_errno(template);
591 err = got_opentemp_named(&tmppath, &tmp, template);
594 err = patch_file(p, oldpath, tmp, nop, &mode);
601 if (p->old != NULL && p->new == NULL) {
602 err = got_worktree_schedule_delete(worktree, &oldpaths,
603 0, NULL, patch_delete, pa, repo, 0, 0);
607 if (fchmod(fileno(tmp), mode) == -1) {
608 err = got_error_from_errno2("chmod", newpath);
612 if (rename(tmppath, newpath) == -1) {
613 if (errno != ENOENT) {
614 err = got_error_from_errno3("rename", tmppath,
619 err = got_path_dirname(&parent, newpath);
622 err = got_path_mkdir(parent);
625 if (rename(tmppath, newpath) == -1) {
626 err = got_error_from_errno3("rename", tmppath,
633 err = got_worktree_schedule_delete(worktree, &oldpaths,
634 0, NULL, patch_delete, pa, repo, 0, 0);
636 err = got_worktree_schedule_add(worktree, &newpaths,
637 patch_add, pa, repo, 1);
640 } else if (p->old == NULL) {
641 err = got_worktree_schedule_add(worktree, &newpaths,
642 patch_add, pa, repo, 1);
646 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
650 got_pathlist_free(&oldpaths);
651 got_pathlist_free(&newpaths);
660 const struct got_error *
661 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
662 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
663 got_cancel_cb cancel_cb, void *cancel_arg)
665 const struct got_error *err = NULL;
666 struct got_fileindex *fileindex = NULL;
667 char *oldpath, *newpath;
668 struct imsgbuf *ibuf;
669 int imsg_fds[2] = {-1, -1};
670 int done = 0, failed = 0;
673 ibuf = calloc(1, sizeof(*ibuf));
675 err = got_error_from_errno("calloc");
679 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
680 err = got_error_from_errno("socketpair");
686 err = got_error_from_errno("fork");
688 } else if (pid == 0) {
689 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
694 if (close(imsg_fds[1]) == -1) {
695 err = got_error_from_errno("close");
699 imsg_init(ibuf, imsg_fds[0]);
701 err = send_patch(ibuf, fd);
706 err = got_worktree_patch_prepare(&fileindex, worktree);
710 while (!done && err == NULL) {
712 struct patch_args pa;
714 pa.progress_cb = progress_cb;
715 pa.progress_arg = progress_arg;
718 err = recv_patch(ibuf, &done, &p);
722 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
723 &newpath, worktree, repo, fileindex);
725 err = apply_patch(worktree, repo, oldpath, newpath,
726 &p, nop, &pa, cancel_cb, cancel_arg);
729 /* recoverable errors */
730 if (err->code == GOT_ERR_FILE_STATUS ||
731 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
732 err = report_progress(&pa, p.old, p.new,
733 GOT_STATUS_CANNOT_UPDATE, err);
734 else if (err->code == GOT_ERR_HUNK_FAILED)
735 err = report_progress(&pa, p.old, p.new,
736 GOT_STATUS_CANNOT_UPDATE, NULL);
749 got_worktree_patch_complete(fileindex);
750 if (fd != -1 && close(fd) == -1 && err == NULL)
751 err = got_error_from_errno("close");
754 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
755 err = got_error_from_errno("close");
756 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
757 err = got_error_from_errno("close");
758 if (err == NULL && failed)
759 err = got_error(GOT_ERR_PATCH_FAILED);