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 are still missing:
19 * + "No final newline" handling
21 * Things that we may want to support:
22 * + support indented patches?
23 * + support other kinds of patches?
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/socket.h>
41 #include "got_error.h"
42 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_opentemp.h"
48 #include "got_patch.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
54 #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 struct got_patch_hunk {
57 STAILQ_ENTRY(got_patch_hunk) entries;
58 const struct got_error *err;
69 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
73 struct got_patch_hunk_head head;
77 got_patch_progress_cb progress_cb;
79 struct got_patch_hunk_head *head;
82 static const struct got_error *
83 send_patch(struct imsgbuf *ibuf, int fd)
85 const struct got_error *err = NULL;
87 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
89 err = got_error_from_errno(
90 "imsg_compose GOT_IMSG_PATCH_FILE");
95 if (imsg_flush(ibuf) == -1) {
96 err = got_error_from_errno("imsg_flush");
104 patch_free(struct got_patch *p)
106 struct got_patch_hunk *h;
109 while (!STAILQ_EMPTY(&p->head)) {
110 h = STAILQ_FIRST(&p->head);
111 STAILQ_REMOVE_HEAD(&p->head, entries);
113 for (i = 0; i < h->len; ++i)
123 static const struct got_error *
124 pushline(struct got_patch_hunk *h, const char *line)
129 if (h->len == h->cap) {
130 if ((newcap = h->cap * 1.5) == 0)
132 t = recallocarray(h->lines, h->cap, newcap,
133 sizeof(h->lines[0]));
135 return got_error_from_errno("recallocarray");
140 if ((t = strdup(line)) == NULL)
141 return got_error_from_errno("strdup");
143 h->lines[h->len++] = t;
147 static const struct got_error *
148 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
150 const struct got_error *err = NULL;
152 struct got_imsg_patch_hunk hdr;
153 struct got_imsg_patch patch;
154 struct got_patch_hunk *h = NULL;
157 memset(p, 0, sizeof(*p));
158 STAILQ_INIT(&p->head);
160 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
163 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
167 if (imsg.hdr.type != GOT_IMSG_PATCH) {
168 err = got_error(GOT_ERR_PRIVSEP_MSG);
171 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
172 if (datalen != sizeof(patch)) {
173 err = got_error(GOT_ERR_PRIVSEP_LEN);
176 memcpy(&patch, imsg.data, sizeof(patch));
177 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
178 err = got_error_from_errno("strdup");
181 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
182 err = got_error_from_errno("strdup");
185 if (p->old == NULL && p->new == NULL) {
186 err = got_error(GOT_ERR_PATCH_MALFORMED);
195 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
199 switch (imsg.hdr.type) {
200 case GOT_IMSG_PATCH_DONE:
202 case GOT_IMSG_PATCH_HUNK:
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 plus newline */
227 if (datalen < 2 || t[datalen-1] != '\0') {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
231 if (*t != ' ' && *t != '-' && *t != '+') {
232 err = got_error(GOT_ERR_PRIVSEP_MSG);
235 err = pushline(h, t);
240 err = got_error(GOT_ERR_PRIVSEP_MSG);
253 * Copy data from orig starting at copypos until pos into tmp.
254 * If pos is -1, copy until EOF.
256 static const struct got_error *
257 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
262 if (fseek(orig, copypos, SEEK_SET) == -1)
263 return got_error_from_errno("fseek");
265 while (pos == -1 || copypos < pos) {
268 len = MIN(len, (size_t)pos - copypos);
269 r = fread(buf, 1, len, orig);
270 if (r != len && ferror(orig))
271 return got_error_from_errno("fread");
272 w = fwrite(buf, 1, r, tmp);
274 return got_error_from_errno("fwrite");
276 if (r != len && feof(orig)) {
279 return got_error(GOT_ERR_HUNK_FAILED);
285 static const struct got_error *
286 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
288 const struct got_error *err = NULL;
290 char mode = *h->lines[0];
294 long match_lineno = -1;
297 linelen = getline(&line, &linesize, orig);
300 err = got_error_from_errno("getline");
301 else if (match == -1)
302 err = got_error(GOT_ERR_HUNK_FAILED);
307 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
308 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
309 (mode == '+' && *lineno == h->old_from)) {
310 match = ftello(orig);
312 err = got_error_from_errno("ftello");
316 match_lineno = (*lineno)-1;
319 if (*lineno >= h->old_from && match != -1)
325 *lineno = match_lineno;
326 if (fseek(orig, match, SEEK_SET) == -1)
327 err = got_error_from_errno("fseek");
334 static const struct got_error *
335 test_hunk(FILE *orig, struct got_patch_hunk *h)
337 const struct got_error *err = NULL;
339 size_t linesize = 0, i = 0;
342 for (i = 0; i < h->len; ++i) {
343 switch (*h->lines[i]) {
348 linelen = getline(&line, &linesize, orig);
351 err = got_error_from_errno("getline");
354 GOT_ERR_HUNK_FAILED);
357 if (strcmp(h->lines[i] + 1, line)) {
358 err = got_error(GOT_ERR_HUNK_FAILED);
370 static const struct got_error *
371 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
375 for (i = 0; i < h->len; ++i) {
376 switch (*h->lines[i]) {
378 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
379 return got_error_from_errno("fprintf");
385 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
386 return got_error_from_errno("fprintf");
393 static const struct got_error *
394 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
397 const struct got_error *err = NULL;
398 struct got_patch_hunk *h;
408 if (p->old == NULL) { /* create */
409 h = STAILQ_FIRST(&p->head);
410 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
411 return got_error(GOT_ERR_PATCH_MALFORMED);
414 for (i = 0; i < h->len; ++i) {
415 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
416 return got_error_from_errno("fprintf");
421 if ((orig = fopen(path, "r")) == NULL) {
422 err = got_error_from_errno2("fopen", path);
426 if (fstat(fileno(orig), &sb) == -1) {
427 err = got_error_from_errno("fstat");
433 STAILQ_FOREACH(h, &p->head, entries) {
434 if (h->lines == NULL)
438 err = locate_hunk(orig, h, &pos, &lineno);
439 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
444 err = copy(tmp, orig, copypos, pos);
449 err = test_hunk(orig, h);
450 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
452 * try to apply the hunk again starting the search
453 * after the previous partial match.
455 if (fseek(orig, pos, SEEK_SET) == -1) {
456 err = got_error_from_errno("fseek");
459 linelen = getline(&line, &linesize, orig);
461 err = got_error_from_errno("getline");
470 if (lineno + 1 != h->old_from)
471 h->offset = lineno + 1 - h->old_from;
474 err = apply_hunk(tmp, h, &lineno);
478 copypos = ftello(orig);
480 err = got_error_from_errno("ftello");
485 if (p->new == NULL && sb.st_size != copypos) {
486 h = STAILQ_FIRST(&p->head);
487 h->err = got_error(GOT_ERR_HUNK_FAILED);
489 } else if (!nop && !feof(orig))
490 err = copy(tmp, orig, copypos, -1);
498 static const struct got_error *
499 report_progress(struct patch_args *pa, const char *old, const char *new,
500 unsigned char status, const struct got_error *orig_error)
502 const struct got_error *err;
503 struct got_patch_hunk *h;
505 err = pa->progress_cb(pa->progress_arg, old, new, status,
506 orig_error, 0, 0, 0, 0, 0, NULL);
510 STAILQ_FOREACH(h, pa->head, entries) {
511 if (h->offset == 0 && h->err == NULL)
514 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
515 h->old_from, h->old_lines, h->new_from, h->new_lines,
524 static const struct got_error *
525 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
528 return report_progress(arg, path, NULL, status, NULL);
531 static const struct got_error *
532 patch_add(void *arg, unsigned char status, const char *path)
534 return report_progress(arg, NULL, path, status, NULL);
537 static const struct got_error *
538 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
539 const char *oldpath, const char *newpath, struct got_patch *p,
540 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
542 const struct got_error *err = NULL;
543 struct got_pathlist_head oldpaths, newpaths;
544 struct got_pathlist_entry *pe;
545 int file_renamed = 0;
546 char *tmppath = NULL, *template = NULL, *parent = NULL;;
548 mode_t mode = GOT_DEFAULT_FILE_MODE;
550 TAILQ_INIT(&oldpaths);
551 TAILQ_INIT(&newpaths);
553 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
556 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
560 file_renamed = strcmp(oldpath, newpath);
562 if (p->old != NULL && p->new == NULL) {
564 * special case: delete a file. don't try to match
565 * the lines but just schedule the removal.
567 err = got_worktree_schedule_delete(worktree, &oldpaths,
568 0, NULL, delete_cb, delete_arg, repo, 0, 0);
572 if (asprintf(&template, "%s/got-patch",
573 got_worktree_get_root_path(worktree)) == -1) {
574 err = got_error_from_errno(template);
579 err = got_opentemp_named(&tmppath, &tmp, template);
582 err = patch_file(p, oldpath, tmp, nop, &mode);
589 if (p->old != NULL && p->new == NULL) {
590 err = got_worktree_schedule_delete(worktree, &oldpaths,
591 0, NULL, patch_delete, pa, repo, 0, 0);
595 if (fchmod(fileno(tmp), mode) == -1) {
596 err = got_error_from_errno2("chmod", newpath);
600 if (rename(tmppath, newpath) == -1) {
601 if (errno != ENOENT) {
602 err = got_error_from_errno3("rename", tmppath,
607 err = got_path_dirname(&parent, newpath);
610 err = got_path_mkdir(parent);
613 if (rename(tmppath, newpath) == -1) {
614 err = got_error_from_errno3("rename", tmppath,
621 err = got_worktree_schedule_delete(worktree, &oldpaths,
622 0, NULL, patch_delete, pa, repo, 0, 0);
624 err = got_worktree_schedule_add(worktree, &newpaths,
625 patch_add, pa, repo, 1);
628 } else if (p->old == NULL) {
629 err = got_worktree_schedule_add(worktree, &newpaths,
630 patch_add, pa, repo, 1);
634 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
638 got_pathlist_free(&oldpaths);
639 got_pathlist_free(&newpaths);
648 const struct got_error *
649 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
650 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
651 got_cancel_cb cancel_cb, void *cancel_arg)
653 const struct got_error *err = NULL;
654 struct got_fileindex *fileindex = NULL;
655 char *oldpath, *newpath;
656 struct imsgbuf *ibuf;
657 int imsg_fds[2] = {-1, -1};
658 int done = 0, failed = 0;
661 ibuf = calloc(1, sizeof(*ibuf));
663 err = got_error_from_errno("calloc");
667 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
668 err = got_error_from_errno("socketpair");
674 err = got_error_from_errno("fork");
676 } else if (pid == 0) {
677 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
682 if (close(imsg_fds[1]) == -1) {
683 err = got_error_from_errno("close");
687 imsg_init(ibuf, imsg_fds[0]);
689 err = send_patch(ibuf, fd);
694 err = got_worktree_patch_prepare(&fileindex, worktree);
698 while (!done && err == NULL) {
700 struct patch_args pa;
702 pa.progress_cb = progress_cb;
703 pa.progress_arg = progress_arg;
706 err = recv_patch(ibuf, &done, &p);
710 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
711 &newpath, worktree, repo, fileindex);
713 err = apply_patch(worktree, repo, oldpath, newpath,
714 &p, nop, &pa, cancel_cb, cancel_arg);
717 /* recoverable errors */
718 if (err->code == GOT_ERR_FILE_STATUS ||
719 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
720 err = report_progress(&pa, p.old, p.new,
721 GOT_STATUS_CANNOT_UPDATE, err);
722 else if (err->code == GOT_ERR_HUNK_FAILED)
723 err = report_progress(&pa, p.old, p.new,
724 GOT_STATUS_CANNOT_UPDATE, NULL);
737 got_worktree_patch_complete(fileindex);
738 if (fd != -1 && close(fd) == -1 && err == NULL)
739 err = got_error_from_errno("close");
742 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
743 err = got_error_from_errno("close");
744 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
745 err = got_error_from_errno("close");
746 if (err == NULL && failed)
747 err = got_error(GOT_ERR_PATCH_FAILED);