Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
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.
7 *
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.
15 *
16 * Apply patches.
17 *
18 * Things that we may want to support:
19 * + support indented patches?
20 * + support other kinds of patches?
21 */
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/uio.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_path.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;
54 long offset;
55 int nonl;
56 long old_from;
57 long old_lines;
58 long new_from;
59 long new_lines;
60 size_t len;
61 size_t cap;
62 char **lines;
63 };
65 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
66 struct got_patch {
67 char *old;
68 char *new;
69 struct got_patch_hunk_head head;
70 };
72 struct patch_args {
73 got_patch_progress_cb progress_cb;
74 void *progress_arg;
75 struct got_patch_hunk_head *head;
76 };
78 static const struct got_error *
79 send_patch(struct imsgbuf *ibuf, int fd)
80 {
81 const struct got_error *err = NULL;
83 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
84 NULL, 0) == -1) {
85 err = got_error_from_errno(
86 "imsg_compose GOT_IMSG_PATCH_FILE");
87 close(fd);
88 return err;
89 }
91 if (imsg_flush(ibuf) == -1) {
92 err = got_error_from_errno("imsg_flush");
93 imsg_clear(ibuf);
94 }
96 return err;
97 }
99 static void
100 patch_free(struct got_patch *p)
102 struct got_patch_hunk *h;
103 size_t i;
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)
110 free(h->lines[i]);
111 free(h->lines);
112 free(h);
115 free(p->new);
116 free(p->old);
119 static const struct got_error *
120 pushline(struct got_patch_hunk *h, const char *line)
122 void *t;
123 size_t newcap;
125 if (h->len == h->cap) {
126 if ((newcap = h->cap * 1.5) == 0)
127 newcap = 16;
128 t = recallocarray(h->lines, h->cap, newcap,
129 sizeof(h->lines[0]));
130 if (t == NULL)
131 return got_error_from_errno("recallocarray");
132 h->lines = t;
133 h->cap = newcap;
136 if ((t = strdup(line)) == NULL)
137 return got_error_from_errno("strdup");
139 h->lines[h->len++] = t;
140 return NULL;
143 static const struct got_error *
144 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
146 const struct got_error *err = NULL;
147 struct imsg imsg;
148 struct got_imsg_patch_hunk hdr;
149 struct got_imsg_patch patch;
150 struct got_patch_hunk *h = NULL;
151 size_t datalen;
153 memset(p, 0, sizeof(*p));
154 STAILQ_INIT(&p->head);
156 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
157 if (err)
158 return err;
159 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
160 *done = 1;
161 goto done;
163 if (imsg.hdr.type != GOT_IMSG_PATCH) {
164 err = got_error(GOT_ERR_PRIVSEP_MSG);
165 goto done;
167 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
168 if (datalen != sizeof(patch)) {
169 err = got_error(GOT_ERR_PRIVSEP_LEN);
170 goto done;
172 memcpy(&patch, imsg.data, sizeof(patch));
174 /* automatically set strip=1 for git-style diffs */
175 if (strip == -1 && patch.git &&
176 (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
177 (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
178 strip = 1;
180 /* prefer the new name if not /dev/null for not git-style diffs */
181 if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
182 err = got_path_strip(&p->old, patch.new, strip);
183 if (err)
184 goto done;
185 } else if (*patch.old != '\0') {
186 err = got_path_strip(&p->old, patch.old, strip);
187 if (err)
188 goto done;
191 if (*patch.new != '\0') {
192 err = got_path_strip(&p->new, patch.new, strip);
193 if (err)
194 goto done;
197 if (p->old == NULL && p->new == NULL) {
198 err = got_error(GOT_ERR_PATCH_MALFORMED);
199 goto done;
202 imsg_free(&imsg);
204 for (;;) {
205 char *t;
207 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
208 if (err)
209 return err;
211 switch (imsg.hdr.type) {
212 case GOT_IMSG_PATCH_DONE:
213 goto done;
214 case GOT_IMSG_PATCH_HUNK:
215 if (h != NULL && h->nonl) {
216 err = got_error(GOT_ERR_PATCH_MALFORMED);
217 goto done;
219 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 if (datalen != sizeof(hdr)) {
221 err = got_error(GOT_ERR_PRIVSEP_LEN);
222 goto done;
224 memcpy(&hdr, imsg.data, sizeof(hdr));
225 if ((h = calloc(1, sizeof(*h))) == NULL) {
226 err = got_error_from_errno("calloc");
227 goto done;
229 h->old_from = hdr.oldfrom;
230 h->old_lines = hdr.oldlines;
231 h->new_from = hdr.newfrom;
232 h->new_lines = hdr.newlines;
233 STAILQ_INSERT_TAIL(&p->head, h, entries);
234 break;
235 case GOT_IMSG_PATCH_LINE:
236 if (h == NULL) {
237 err = got_error(GOT_ERR_PRIVSEP_MSG);
238 goto done;
240 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
241 t = imsg.data;
242 /* at least one char */
243 if (datalen < 2 || t[datalen-1] != '\0') {
244 err = got_error(GOT_ERR_PRIVSEP_MSG);
245 goto done;
247 if (*t != ' ' && *t != '-' && *t != '+' &&
248 *t != '\\') {
249 err = got_error(GOT_ERR_PRIVSEP_MSG);
250 goto done;
252 if (h->nonl)
253 err = got_error(GOT_ERR_PATCH_MALFORMED);
254 if (*t == '\\')
255 h->nonl = 1;
256 else
257 err = pushline(h, t);
258 if (err)
259 goto done;
260 break;
261 default:
262 err = got_error(GOT_ERR_PRIVSEP_MSG);
263 goto done;
266 imsg_free(&imsg);
269 done:
270 imsg_free(&imsg);
271 return err;
274 /*
275 * Copy data from orig starting at copypos until pos into tmp.
276 * If pos is -1, copy until EOF.
277 */
278 static const struct got_error *
279 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
281 char buf[BUFSIZ];
282 size_t len, r, w;
284 if (fseek(orig, copypos, SEEK_SET) == -1)
285 return got_error_from_errno("fseek");
287 while (pos == -1 || copypos < pos) {
288 len = sizeof(buf);
289 if (pos > 0)
290 len = MIN(len, (size_t)pos - copypos);
291 r = fread(buf, 1, len, orig);
292 if (r != len && ferror(orig))
293 return got_error_from_errno("fread");
294 w = fwrite(buf, 1, r, tmp);
295 if (w != r)
296 return got_error_from_errno("fwrite");
297 copypos += len;
298 if (r != len && feof(orig)) {
299 if (pos == -1)
300 return NULL;
301 return got_error(GOT_ERR_HUNK_FAILED);
304 return NULL;
307 static const struct got_error *
308 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
310 const struct got_error *err = NULL;
311 char *line = NULL;
312 char mode = *h->lines[0];
313 size_t linesize = 0;
314 ssize_t linelen;
315 off_t match = -1;
316 long match_lineno = -1;
318 for (;;) {
319 linelen = getline(&line, &linesize, orig);
320 if (linelen == -1) {
321 if (ferror(orig))
322 err = got_error_from_errno("getline");
323 else if (match == -1)
324 err = got_error(GOT_ERR_HUNK_FAILED);
325 break;
327 if (line[linelen - 1] == '\n')
328 line[linelen - 1] = '\0';
329 (*lineno)++;
331 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
332 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
333 (mode == '+' && *lineno == h->old_from)) {
334 match = ftello(orig);
335 if (match == -1) {
336 err = got_error_from_errno("ftello");
337 break;
339 match -= linelen;
340 match_lineno = (*lineno)-1;
343 if (*lineno >= h->old_from && match != -1)
344 break;
347 if (err == NULL) {
348 *pos = match;
349 *lineno = match_lineno;
350 if (fseek(orig, match, SEEK_SET) == -1)
351 err = got_error_from_errno("fseek");
354 free(line);
355 return err;
358 static const struct got_error *
359 test_hunk(FILE *orig, struct got_patch_hunk *h)
361 const struct got_error *err = NULL;
362 char *line = NULL;
363 size_t linesize = 0, i = 0;
364 ssize_t linelen;
366 for (i = 0; i < h->len; ++i) {
367 switch (*h->lines[i]) {
368 case '+':
369 continue;
370 case ' ':
371 case '-':
372 linelen = getline(&line, &linesize, orig);
373 if (linelen == -1) {
374 if (ferror(orig))
375 err = got_error_from_errno("getline");
376 else
377 err = got_error(
378 GOT_ERR_HUNK_FAILED);
379 goto done;
381 if (line[linelen - 1] == '\n')
382 line[linelen - 1] = '\0';
383 if (strcmp(h->lines[i] + 1, line)) {
384 err = got_error(GOT_ERR_HUNK_FAILED);
385 goto done;
387 break;
391 done:
392 free(line);
393 return err;
396 static const struct got_error *
397 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
399 size_t i = 0;
401 for (i = 0; i < h->len; ++i) {
402 switch (*h->lines[i]) {
403 case ' ':
404 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
405 return got_error_from_errno("fprintf");
406 /* fallthrough */
407 case '-':
408 (*lineno)++;
409 break;
410 case '+':
411 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
412 return got_error_from_errno("fprintf");
413 if (i != h->len - 1 || !h->nonl) {
414 if (fprintf(tmp, "\n") < 0)
415 return got_error_from_errno(
416 "fprintf");
418 break;
421 return NULL;
424 static const struct got_error *
425 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
426 mode_t *mode)
428 const struct got_error *err = NULL;
429 struct got_patch_hunk *h;
430 struct stat sb;
431 long lineno = 0;
432 FILE *orig;
433 off_t copypos, pos;
434 char *line = NULL;
435 size_t linesize = 0;
436 ssize_t linelen;
438 if (p->old == NULL) { /* create */
439 h = STAILQ_FIRST(&p->head);
440 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
441 return got_error(GOT_ERR_PATCH_MALFORMED);
442 if (nop)
443 return NULL;
444 return apply_hunk(tmp, h, &lineno);
447 if ((orig = fopen(path, "r")) == NULL) {
448 err = got_error_from_errno2("fopen", path);
449 goto done;
452 if (fstat(fileno(orig), &sb) == -1) {
453 err = got_error_from_errno("fstat");
454 goto done;
456 *mode = sb.st_mode;
458 copypos = 0;
459 STAILQ_FOREACH(h, &p->head, entries) {
460 if (h->lines == NULL)
461 break;
463 tryagain:
464 err = locate_hunk(orig, h, &pos, &lineno);
465 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
466 h->err = err;
467 if (err != NULL)
468 goto done;
469 if (!nop)
470 err = copy(tmp, orig, copypos, pos);
471 if (err != NULL)
472 goto done;
473 copypos = pos;
475 err = test_hunk(orig, h);
476 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
477 /*
478 * try to apply the hunk again starting the search
479 * after the previous partial match.
480 */
481 if (fseek(orig, pos, SEEK_SET) == -1) {
482 err = got_error_from_errno("fseek");
483 goto done;
485 linelen = getline(&line, &linesize, orig);
486 if (linelen == -1) {
487 err = got_error_from_errno("getline");
488 goto done;
490 lineno++;
491 goto tryagain;
493 if (err != NULL)
494 goto done;
496 if (lineno + 1 != h->old_from)
497 h->offset = lineno + 1 - h->old_from;
499 if (!nop)
500 err = apply_hunk(tmp, h, &lineno);
501 if (err != NULL)
502 goto done;
504 copypos = ftello(orig);
505 if (copypos == -1) {
506 err = got_error_from_errno("ftello");
507 goto done;
511 if (p->new == NULL && sb.st_size != copypos) {
512 h = STAILQ_FIRST(&p->head);
513 h->err = got_error(GOT_ERR_HUNK_FAILED);
514 err = h->err;
515 } else if (!nop && !feof(orig))
516 err = copy(tmp, orig, copypos, -1);
518 done:
519 if (orig != NULL)
520 fclose(orig);
521 return err;
524 static const struct got_error *
525 report_progress(struct patch_args *pa, const char *old, const char *new,
526 unsigned char status, const struct got_error *orig_error)
528 const struct got_error *err;
529 struct got_patch_hunk *h;
531 err = pa->progress_cb(pa->progress_arg, old, new, status,
532 orig_error, 0, 0, 0, 0, 0, NULL);
533 if (err)
534 return err;
536 STAILQ_FOREACH(h, pa->head, entries) {
537 if (h->offset == 0 && h->err == NULL)
538 continue;
540 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
541 h->old_from, h->old_lines, h->new_from, h->new_lines,
542 h->offset, h->err);
543 if (err)
544 return err;
547 return NULL;
550 static const struct got_error *
551 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
552 const char *path)
554 return report_progress(arg, path, NULL, status, NULL);
557 static const struct got_error *
558 patch_add(void *arg, unsigned char status, const char *path)
560 return report_progress(arg, NULL, path, status, NULL);
563 static const struct got_error *
564 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
565 const char *oldpath, const char *newpath, struct got_patch *p,
566 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
568 const struct got_error *err = NULL;
569 struct got_pathlist_head oldpaths, newpaths;
570 struct got_pathlist_entry *pe;
571 int file_renamed = 0;
572 char *tmppath = NULL, *template = NULL, *parent = NULL;;
573 FILE *tmp = NULL;
574 mode_t mode = GOT_DEFAULT_FILE_MODE;
576 TAILQ_INIT(&oldpaths);
577 TAILQ_INIT(&newpaths);
579 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
580 if (err)
581 goto done;
582 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
583 if (err)
584 goto done;
586 file_renamed = strcmp(oldpath, newpath);
588 if (asprintf(&template, "%s/got-patch",
589 got_worktree_get_root_path(worktree)) == -1) {
590 err = got_error_from_errno(template);
591 goto done;
594 if (!nop)
595 err = got_opentemp_named(&tmppath, &tmp, template);
596 if (err)
597 goto done;
598 err = patch_file(p, oldpath, tmp, nop, &mode);
599 if (err)
600 goto done;
602 if (nop)
603 goto done;
605 if (p->old != NULL && p->new == NULL) {
606 err = got_worktree_schedule_delete(worktree, &oldpaths,
607 0, NULL, patch_delete, pa, repo, 0, 0);
608 goto done;
611 if (fchmod(fileno(tmp), mode) == -1) {
612 err = got_error_from_errno2("chmod", newpath);
613 goto done;
616 if (rename(tmppath, newpath) == -1) {
617 if (errno != ENOENT) {
618 err = got_error_from_errno3("rename", tmppath,
619 newpath);
620 goto done;
623 err = got_path_dirname(&parent, newpath);
624 if (err != NULL)
625 goto done;
626 err = got_path_mkdir(parent);
627 if (err != NULL)
628 goto done;
629 if (rename(tmppath, newpath) == -1) {
630 err = got_error_from_errno3("rename", tmppath,
631 newpath);
632 goto done;
636 if (file_renamed) {
637 err = got_worktree_schedule_delete(worktree, &oldpaths,
638 0, NULL, patch_delete, pa, repo, 0, 0);
639 if (err == NULL)
640 err = got_worktree_schedule_add(worktree, &newpaths,
641 patch_add, pa, repo, 1);
642 if (err)
643 unlink(newpath);
644 } else if (p->old == NULL) {
645 err = got_worktree_schedule_add(worktree, &newpaths,
646 patch_add, pa, repo, 1);
647 if (err)
648 unlink(newpath);
649 } else
650 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
651 NULL);
653 done:
654 got_pathlist_free(&oldpaths);
655 got_pathlist_free(&newpaths);
656 free(parent);
657 free(template);
658 if (tmppath != NULL)
659 unlink(tmppath);
660 free(tmppath);
661 return err;
664 const struct got_error *
665 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
666 int nop, int strip, got_patch_progress_cb progress_cb, void *progress_arg,
667 got_cancel_cb cancel_cb, void *cancel_arg)
669 const struct got_error *err = NULL;
670 struct got_fileindex *fileindex = NULL;
671 char *oldpath, *newpath;
672 struct imsgbuf *ibuf;
673 int imsg_fds[2] = {-1, -1};
674 int done = 0, failed = 0;
675 pid_t pid;
677 ibuf = calloc(1, sizeof(*ibuf));
678 if (ibuf == NULL) {
679 err = got_error_from_errno("calloc");
680 goto done;
683 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
684 err = got_error_from_errno("socketpair");
685 goto done;
688 pid = fork();
689 if (pid == -1) {
690 err = got_error_from_errno("fork");
691 goto done;
692 } else if (pid == 0) {
693 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
694 NULL);
695 /* not reached */
698 if (close(imsg_fds[1]) == -1) {
699 err = got_error_from_errno("close");
700 goto done;
702 imsg_fds[1] = -1;
703 imsg_init(ibuf, imsg_fds[0]);
705 err = send_patch(ibuf, fd);
706 fd = -1;
707 if (err)
708 goto done;
710 err = got_worktree_patch_prepare(&fileindex, worktree);
711 if (err)
712 goto done;
714 while (!done && err == NULL) {
715 struct got_patch p;
716 struct patch_args pa;
718 pa.progress_cb = progress_cb;
719 pa.progress_arg = progress_arg;
720 pa.head = &p.head;
722 err = recv_patch(ibuf, &done, &p, strip);
723 if (err || done)
724 break;
726 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
727 &newpath, worktree, repo, fileindex);
728 if (err == NULL)
729 err = apply_patch(worktree, repo, oldpath, newpath,
730 &p, nop, &pa, cancel_cb, cancel_arg);
731 if (err != NULL) {
732 failed = 1;
733 /* recoverable errors */
734 if (err->code == GOT_ERR_FILE_STATUS ||
735 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
736 err = report_progress(&pa, p.old, p.new,
737 GOT_STATUS_CANNOT_UPDATE, err);
738 else if (err->code == GOT_ERR_HUNK_FAILED)
739 err = report_progress(&pa, p.old, p.new,
740 GOT_STATUS_CANNOT_UPDATE, NULL);
743 free(oldpath);
744 free(newpath);
745 patch_free(&p);
747 if (err)
748 break;
751 done:
752 if (fileindex)
753 got_worktree_patch_complete(fileindex);
754 if (fd != -1 && close(fd) == -1 && err == NULL)
755 err = got_error_from_errno("close");
756 if (ibuf != NULL)
757 imsg_clear(ibuf);
758 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
759 err = got_error_from_errno("close");
760 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
761 err = got_error_from_errno("close");
762 if (err == NULL && failed)
763 err = got_error(GOT_ERR_PATCH_FAILED);
764 return err;