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>
42 #include "got_error.h"
43 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_opentemp.h"
49 #include "got_patch.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_privsep.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 struct got_patch_hunk {
58 STAILQ_ENTRY(got_patch_hunk) entries;
59 const struct got_error *err;
70 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
74 struct got_patch_hunk_head head;
78 got_patch_progress_cb progress_cb;
80 struct got_patch_hunk_head *head;
83 static const struct got_error *
84 send_patch(struct imsgbuf *ibuf, int fd)
86 const struct got_error *err = NULL;
88 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
90 err = got_error_from_errno(
91 "imsg_compose GOT_IMSG_PATCH_FILE");
96 if (imsg_flush(ibuf) == -1) {
97 err = got_error_from_errno("imsg_flush");
105 patch_free(struct got_patch *p)
107 struct got_patch_hunk *h;
110 while (!STAILQ_EMPTY(&p->head)) {
111 h = STAILQ_FIRST(&p->head);
112 STAILQ_REMOVE_HEAD(&p->head, entries);
114 for (i = 0; i < h->len; ++i)
124 static const struct got_error *
125 pushline(struct got_patch_hunk *h, const char *line)
130 if (h->len == h->cap) {
131 if ((newcap = h->cap * 1.5) == 0)
133 t = recallocarray(h->lines, h->cap, newcap,
134 sizeof(h->lines[0]));
136 return got_error_from_errno("recallocarray");
141 if ((t = strdup(line)) == NULL)
142 return got_error_from_errno("strdup");
144 h->lines[h->len++] = t;
148 static const struct got_error *
149 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
151 const struct got_error *err = NULL;
153 struct got_imsg_patch_hunk hdr;
154 struct got_imsg_patch patch;
155 struct got_patch_hunk *h = NULL;
158 memset(p, 0, sizeof(*p));
159 STAILQ_INIT(&p->head);
161 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
164 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
168 if (imsg.hdr.type != GOT_IMSG_PATCH) {
169 err = got_error(GOT_ERR_PRIVSEP_MSG);
172 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
173 if (datalen != sizeof(patch)) {
174 err = got_error(GOT_ERR_PRIVSEP_LEN);
177 memcpy(&patch, imsg.data, sizeof(patch));
178 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
179 err = got_error_from_errno("strdup");
182 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
183 err = got_error_from_errno("strdup");
186 if (p->old == NULL && p->new == NULL) {
187 err = got_error(GOT_ERR_PATCH_MALFORMED);
196 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
200 switch (imsg.hdr.type) {
201 case GOT_IMSG_PATCH_DONE:
203 case GOT_IMSG_PATCH_HUNK:
204 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
205 if (datalen != sizeof(hdr)) {
206 err = got_error(GOT_ERR_PRIVSEP_LEN);
209 memcpy(&hdr, imsg.data, sizeof(hdr));
210 if ((h = calloc(1, sizeof(*h))) == NULL) {
211 err = got_error_from_errno("calloc");
214 h->old_from = hdr.oldfrom;
215 h->old_lines = hdr.oldlines;
216 h->new_from = hdr.newfrom;
217 h->new_lines = hdr.newlines;
218 STAILQ_INSERT_TAIL(&p->head, h, entries);
220 case GOT_IMSG_PATCH_LINE:
222 err = got_error(GOT_ERR_PRIVSEP_MSG);
225 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
227 /* at least one char plus newline */
228 if (datalen < 2 || t[datalen-1] != '\0') {
229 err = got_error(GOT_ERR_PRIVSEP_MSG);
232 if (*t != ' ' && *t != '-' && *t != '+') {
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
236 err = pushline(h, t);
241 err = got_error(GOT_ERR_PRIVSEP_MSG);
254 * Copy data from orig starting at copypos until pos into tmp.
255 * If pos is -1, copy until EOF.
257 static const struct got_error *
258 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
263 if (fseek(orig, copypos, SEEK_SET) == -1)
264 return got_error_from_errno("fseek");
266 while (pos == -1 || copypos < pos) {
269 len = MIN(len, (size_t)pos - copypos);
270 r = fread(buf, 1, len, orig);
271 if (r != len && ferror(orig))
272 return got_error_from_errno("fread");
273 w = fwrite(buf, 1, r, tmp);
275 return got_error_from_errno("fwrite");
277 if (r != len && feof(orig)) {
280 return got_error(GOT_ERR_HUNK_FAILED);
286 static const struct got_error *
287 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
289 const struct got_error *err = NULL;
291 char mode = *h->lines[0];
295 long match_lineno = -1;
298 linelen = getline(&line, &linesize, orig);
301 err = got_error_from_errno("getline");
302 else if (match == -1)
303 err = got_error(GOT_ERR_HUNK_FAILED);
308 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
309 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
310 (mode == '+' && *lineno == h->old_from)) {
311 match = ftello(orig);
313 err = got_error_from_errno("ftello");
317 match_lineno = (*lineno)-1;
320 if (*lineno >= h->old_from && match != -1)
326 *lineno = match_lineno;
327 if (fseek(orig, match, SEEK_SET) == -1)
328 err = got_error_from_errno("fseek");
335 static const struct got_error *
336 test_hunk(FILE *orig, struct got_patch_hunk *h)
338 const struct got_error *err = NULL;
340 size_t linesize = 0, i = 0;
343 for (i = 0; i < h->len; ++i) {
344 switch (*h->lines[i]) {
349 linelen = getline(&line, &linesize, orig);
352 err = got_error_from_errno("getline");
355 GOT_ERR_HUNK_FAILED);
358 if (strcmp(h->lines[i] + 1, line)) {
359 err = got_error(GOT_ERR_HUNK_FAILED);
371 static const struct got_error *
372 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
376 for (i = 0; i < h->len; ++i) {
377 switch (*h->lines[i]) {
379 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
380 return got_error_from_errno("fprintf");
386 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
387 return got_error_from_errno("fprintf");
394 static const struct got_error *
395 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
398 const struct got_error *err = NULL;
399 struct got_patch_hunk *h;
409 if (p->old == NULL) { /* create */
410 h = STAILQ_FIRST(&p->head);
411 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
412 return got_error(GOT_ERR_PATCH_MALFORMED);
415 for (i = 0; i < h->len; ++i) {
416 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
417 return got_error_from_errno("fprintf");
422 if ((orig = fopen(path, "r")) == NULL) {
423 err = got_error_from_errno2("fopen", path);
427 if (fstat(fileno(orig), &sb) == -1) {
428 err = got_error_from_errno("fstat");
434 STAILQ_FOREACH(h, &p->head, entries) {
435 if (h->lines == NULL)
439 err = locate_hunk(orig, h, &pos, &lineno);
440 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
445 err = copy(tmp, orig, copypos, pos);
450 err = test_hunk(orig, h);
451 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
453 * try to apply the hunk again starting the search
454 * after the previous partial match.
456 if (fseek(orig, pos, SEEK_SET) == -1) {
457 err = got_error_from_errno("fseek");
460 linelen = getline(&line, &linesize, orig);
462 err = got_error_from_errno("getline");
471 if (lineno + 1 != h->old_from)
472 h->offset = lineno + 1 - h->old_from;
475 err = apply_hunk(tmp, h, &lineno);
479 copypos = ftello(orig);
481 err = got_error_from_errno("ftello");
486 if (p->new == NULL && sb.st_size != copypos) {
487 h = STAILQ_FIRST(&p->head);
488 h->err = got_error(GOT_ERR_HUNK_FAILED);
490 } else if (!nop && !feof(orig))
491 err = copy(tmp, orig, copypos, -1);
499 static const struct got_error *
500 report_progress(struct patch_args *pa, const char *old, const char *new,
501 unsigned char status, const struct got_error *orig_error)
503 const struct got_error *err;
504 struct got_patch_hunk *h;
506 err = pa->progress_cb(pa->progress_arg, old, new, status,
507 orig_error, 0, 0, 0, 0, 0, NULL);
511 STAILQ_FOREACH(h, pa->head, entries) {
512 if (h->offset == 0 && h->err == NULL)
515 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
516 h->old_from, h->old_lines, h->new_from, h->new_lines,
525 static const struct got_error *
526 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
529 return report_progress(arg, path, NULL, status, NULL);
532 static const struct got_error *
533 patch_add(void *arg, unsigned char status, const char *path)
535 return report_progress(arg, NULL, path, status, NULL);
538 static const struct got_error *
539 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
540 const char *oldpath, const char *newpath, struct got_patch *p,
541 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
543 const struct got_error *err = NULL;
544 struct got_pathlist_head oldpaths, newpaths;
545 struct got_pathlist_entry *pe;
546 int file_renamed = 0;
547 char *tmppath = NULL, *template = NULL, *parent = NULL;;
549 mode_t mode = GOT_DEFAULT_FILE_MODE;
551 TAILQ_INIT(&oldpaths);
552 TAILQ_INIT(&newpaths);
554 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
557 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
561 file_renamed = strcmp(oldpath, newpath);
563 if (asprintf(&template, "%s/got-patch",
564 got_worktree_get_root_path(worktree)) == -1) {
565 err = got_error_from_errno(template);
570 err = got_opentemp_named(&tmppath, &tmp, template);
573 err = patch_file(p, oldpath, tmp, nop, &mode);
580 if (p->old != NULL && p->new == NULL) {
581 err = got_worktree_schedule_delete(worktree, &oldpaths,
582 0, NULL, patch_delete, pa, repo, 0, 0);
586 if (fchmod(fileno(tmp), mode) == -1) {
587 err = got_error_from_errno2("chmod", newpath);
591 if (rename(tmppath, newpath) == -1) {
592 if (errno != ENOENT) {
593 err = got_error_from_errno3("rename", tmppath,
598 err = got_path_dirname(&parent, newpath);
601 err = got_path_mkdir(parent);
604 if (rename(tmppath, newpath) == -1) {
605 err = got_error_from_errno3("rename", tmppath,
612 err = got_worktree_schedule_delete(worktree, &oldpaths,
613 0, NULL, patch_delete, pa, repo, 0, 0);
615 err = got_worktree_schedule_add(worktree, &newpaths,
616 patch_add, pa, repo, 1);
619 } else if (p->old == NULL) {
620 err = got_worktree_schedule_add(worktree, &newpaths,
621 patch_add, pa, repo, 1);
625 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
629 got_pathlist_free(&oldpaths);
630 got_pathlist_free(&newpaths);
639 const struct got_error *
640 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
641 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
642 got_cancel_cb cancel_cb, void *cancel_arg)
644 const struct got_error *err = NULL;
645 struct got_fileindex *fileindex = NULL;
646 char *oldpath, *newpath;
647 struct imsgbuf *ibuf;
648 int imsg_fds[2] = {-1, -1};
649 int done = 0, failed = 0;
652 ibuf = calloc(1, sizeof(*ibuf));
654 err = got_error_from_errno("calloc");
658 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
659 err = got_error_from_errno("socketpair");
665 err = got_error_from_errno("fork");
667 } else if (pid == 0) {
668 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
673 if (close(imsg_fds[1]) == -1) {
674 err = got_error_from_errno("close");
678 imsg_init(ibuf, imsg_fds[0]);
680 err = send_patch(ibuf, fd);
685 err = got_worktree_patch_prepare(&fileindex, worktree);
689 while (!done && err == NULL) {
691 struct patch_args pa;
693 pa.progress_cb = progress_cb;
694 pa.progress_arg = progress_arg;
697 err = recv_patch(ibuf, &done, &p);
701 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
702 &newpath, worktree, repo, fileindex);
704 err = apply_patch(worktree, repo, oldpath, newpath,
705 &p, nop, &pa, cancel_cb, cancel_arg);
708 /* recoverable errors */
709 if (err->code == GOT_ERR_FILE_STATUS ||
710 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
711 err = report_progress(&pa, p.old, p.new,
712 GOT_STATUS_CANNOT_UPDATE, err);
713 else if (err->code == GOT_ERR_HUNK_FAILED)
714 err = report_progress(&pa, p.old, p.new,
715 GOT_STATUS_CANNOT_UPDATE, NULL);
728 got_worktree_patch_complete(fileindex);
729 if (fd != -1 && close(fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
733 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
734 err = got_error_from_errno("close");
735 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
736 err = got_error_from_errno("close");
737 if (err == NULL && failed)
738 err = got_error(GOT_ERR_PATCH_FAILED);