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 are still missing:
19 * + "No final newline" handling
20 *
21 * Things that we may want to support:
22 * + support indented patches?
23 * + support other kinds of patches?
24 */
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <sha1.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <imsg.h>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_path.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;
60 long offset;
61 long old_from;
62 long old_lines;
63 long new_from;
64 long new_lines;
65 size_t len;
66 size_t cap;
67 char **lines;
68 };
70 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
71 struct got_patch {
72 char *old;
73 char *new;
74 struct got_patch_hunk_head head;
75 };
77 struct patch_args {
78 got_patch_progress_cb progress_cb;
79 void *progress_arg;
80 struct got_patch_hunk_head *head;
81 };
83 static const struct got_error *
84 send_patch(struct imsgbuf *ibuf, int fd)
85 {
86 const struct got_error *err = NULL;
88 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
89 NULL, 0) == -1) {
90 err = got_error_from_errno(
91 "imsg_compose GOT_IMSG_PATCH_FILE");
92 close(fd);
93 return err;
94 }
96 if (imsg_flush(ibuf) == -1) {
97 err = got_error_from_errno("imsg_flush");
98 imsg_clear(ibuf);
99 }
101 return err;
104 static void
105 patch_free(struct got_patch *p)
107 struct got_patch_hunk *h;
108 size_t i;
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)
115 free(h->lines[i]);
116 free(h->lines);
117 free(h);
120 free(p->new);
121 free(p->old);
124 static const struct got_error *
125 pushline(struct got_patch_hunk *h, const char *line)
127 void *t;
128 size_t newcap;
130 if (h->len == h->cap) {
131 if ((newcap = h->cap * 1.5) == 0)
132 newcap = 16;
133 t = recallocarray(h->lines, h->cap, newcap,
134 sizeof(h->lines[0]));
135 if (t == NULL)
136 return got_error_from_errno("recallocarray");
137 h->lines = t;
138 h->cap = newcap;
141 if ((t = strdup(line)) == NULL)
142 return got_error_from_errno("strdup");
144 h->lines[h->len++] = t;
145 return NULL;
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;
152 struct imsg imsg;
153 struct got_imsg_patch_hunk hdr;
154 struct got_imsg_patch patch;
155 struct got_patch_hunk *h = NULL;
156 size_t datalen;
158 memset(p, 0, sizeof(*p));
159 STAILQ_INIT(&p->head);
161 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
162 if (err)
163 return err;
164 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
165 *done = 1;
166 goto done;
168 if (imsg.hdr.type != GOT_IMSG_PATCH) {
169 err = got_error(GOT_ERR_PRIVSEP_MSG);
170 goto done;
172 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
173 if (datalen != sizeof(patch)) {
174 err = got_error(GOT_ERR_PRIVSEP_LEN);
175 goto done;
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");
180 goto done;
182 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
183 err = got_error_from_errno("strdup");
184 goto done;
186 if (p->old == NULL && p->new == NULL) {
187 err = got_error(GOT_ERR_PATCH_MALFORMED);
188 goto done;
191 imsg_free(&imsg);
193 for (;;) {
194 char *t;
196 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
197 if (err)
198 return err;
200 switch (imsg.hdr.type) {
201 case GOT_IMSG_PATCH_DONE:
202 goto 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);
207 goto done;
209 memcpy(&hdr, imsg.data, sizeof(hdr));
210 if ((h = calloc(1, sizeof(*h))) == NULL) {
211 err = got_error_from_errno("calloc");
212 goto done;
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);
219 break;
220 case GOT_IMSG_PATCH_LINE:
221 if (h == NULL) {
222 err = got_error(GOT_ERR_PRIVSEP_MSG);
223 goto done;
225 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
226 t = imsg.data;
227 /* at least one char plus newline */
228 if (datalen < 2 || t[datalen-1] != '\0') {
229 err = got_error(GOT_ERR_PRIVSEP_MSG);
230 goto done;
232 if (*t != ' ' && *t != '-' && *t != '+') {
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
234 goto done;
236 err = pushline(h, t);
237 if (err)
238 goto done;
239 break;
240 default:
241 err = got_error(GOT_ERR_PRIVSEP_MSG);
242 goto done;
245 imsg_free(&imsg);
248 done:
249 imsg_free(&imsg);
250 return err;
253 /*
254 * Copy data from orig starting at copypos until pos into tmp.
255 * If pos is -1, copy until EOF.
256 */
257 static const struct got_error *
258 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
260 char buf[BUFSIZ];
261 size_t len, r, w;
263 if (fseek(orig, copypos, SEEK_SET) == -1)
264 return got_error_from_errno("fseek");
266 while (pos == -1 || copypos < pos) {
267 len = sizeof(buf);
268 if (pos > 0)
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);
274 if (w != r)
275 return got_error_from_errno("fwrite");
276 copypos += len;
277 if (r != len && feof(orig)) {
278 if (pos == -1)
279 return NULL;
280 return got_error(GOT_ERR_HUNK_FAILED);
283 return NULL;
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;
290 char *line = NULL;
291 char mode = *h->lines[0];
292 size_t linesize = 0;
293 ssize_t linelen;
294 off_t match = -1;
295 long match_lineno = -1;
297 for (;;) {
298 linelen = getline(&line, &linesize, orig);
299 if (linelen == -1) {
300 if (ferror(orig))
301 err = got_error_from_errno("getline");
302 else if (match == -1)
303 err = got_error(GOT_ERR_HUNK_FAILED);
304 break;
306 (*lineno)++;
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);
312 if (match == -1) {
313 err = got_error_from_errno("ftello");
314 break;
316 match -= linelen;
317 match_lineno = (*lineno)-1;
320 if (*lineno >= h->old_from && match != -1)
321 break;
324 if (err == NULL) {
325 *pos = match;
326 *lineno = match_lineno;
327 if (fseek(orig, match, SEEK_SET) == -1)
328 err = got_error_from_errno("fseek");
331 free(line);
332 return err;
335 static const struct got_error *
336 test_hunk(FILE *orig, struct got_patch_hunk *h)
338 const struct got_error *err = NULL;
339 char *line = NULL;
340 size_t linesize = 0, i = 0;
341 ssize_t linelen;
343 for (i = 0; i < h->len; ++i) {
344 switch (*h->lines[i]) {
345 case '+':
346 continue;
347 case ' ':
348 case '-':
349 linelen = getline(&line, &linesize, orig);
350 if (linelen == -1) {
351 if (ferror(orig))
352 err = got_error_from_errno("getline");
353 else
354 err = got_error(
355 GOT_ERR_HUNK_FAILED);
356 goto done;
358 if (strcmp(h->lines[i] + 1, line)) {
359 err = got_error(GOT_ERR_HUNK_FAILED);
360 goto done;
362 break;
366 done:
367 free(line);
368 return err;
371 static const struct got_error *
372 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
374 size_t i = 0;
376 for (i = 0; i < h->len; ++i) {
377 switch (*h->lines[i]) {
378 case ' ':
379 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
380 return got_error_from_errno("fprintf");
381 /* fallthrough */
382 case '-':
383 (*lineno)++;
384 break;
385 case '+':
386 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
387 return got_error_from_errno("fprintf");
388 break;
391 return NULL;
394 static const struct got_error *
395 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
396 mode_t *mode)
398 const struct got_error *err = NULL;
399 struct got_patch_hunk *h;
400 struct stat sb;
401 size_t i;
402 long lineno = 0;
403 FILE *orig;
404 off_t copypos, pos;
405 char *line = NULL;
406 size_t linesize = 0;
407 ssize_t linelen;
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);
413 if (nop)
414 return NULL;
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");
419 return err;
422 if ((orig = fopen(path, "r")) == NULL) {
423 err = got_error_from_errno2("fopen", path);
424 goto done;
427 if (fstat(fileno(orig), &sb) == -1) {
428 err = got_error_from_errno("fstat");
429 goto done;
431 *mode = sb.st_mode;
433 copypos = 0;
434 STAILQ_FOREACH(h, &p->head, entries) {
435 if (h->lines == NULL)
436 break;
438 tryagain:
439 err = locate_hunk(orig, h, &pos, &lineno);
440 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
441 h->err = err;
442 if (err != NULL)
443 goto done;
444 if (!nop)
445 err = copy(tmp, orig, copypos, pos);
446 if (err != NULL)
447 goto done;
448 copypos = pos;
450 err = test_hunk(orig, h);
451 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
452 /*
453 * try to apply the hunk again starting the search
454 * after the previous partial match.
455 */
456 if (fseek(orig, pos, SEEK_SET) == -1) {
457 err = got_error_from_errno("fseek");
458 goto done;
460 linelen = getline(&line, &linesize, orig);
461 if (linelen == -1) {
462 err = got_error_from_errno("getline");
463 goto done;
465 lineno++;
466 goto tryagain;
468 if (err != NULL)
469 goto done;
471 if (lineno + 1 != h->old_from)
472 h->offset = lineno + 1 - h->old_from;
474 if (!nop)
475 err = apply_hunk(tmp, h, &lineno);
476 if (err != NULL)
477 goto done;
479 copypos = ftello(orig);
480 if (copypos == -1) {
481 err = got_error_from_errno("ftello");
482 goto done;
486 if (p->new == NULL && sb.st_size != copypos) {
487 h = STAILQ_FIRST(&p->head);
488 h->err = got_error(GOT_ERR_HUNK_FAILED);
489 err = h->err;
490 } else if (!nop && !feof(orig))
491 err = copy(tmp, orig, copypos, -1);
493 done:
494 if (orig != NULL)
495 fclose(orig);
496 return err;
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);
508 if (err)
509 return err;
511 STAILQ_FOREACH(h, pa->head, entries) {
512 if (h->offset == 0 && h->err == NULL)
513 continue;
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,
517 h->offset, h->err);
518 if (err)
519 return err;
522 return NULL;
525 static const struct got_error *
526 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
527 const char *path)
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;;
548 FILE *tmp = 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);
555 if (err)
556 goto done;
557 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
558 if (err)
559 goto done;
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);
566 goto done;
569 if (!nop)
570 err = got_opentemp_named(&tmppath, &tmp, template);
571 if (err)
572 goto done;
573 err = patch_file(p, oldpath, tmp, nop, &mode);
574 if (err)
575 goto done;
577 if (nop)
578 goto done;
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);
583 goto done;
586 if (fchmod(fileno(tmp), mode) == -1) {
587 err = got_error_from_errno2("chmod", newpath);
588 goto done;
591 if (rename(tmppath, newpath) == -1) {
592 if (errno != ENOENT) {
593 err = got_error_from_errno3("rename", tmppath,
594 newpath);
595 goto done;
598 err = got_path_dirname(&parent, newpath);
599 if (err != NULL)
600 goto done;
601 err = got_path_mkdir(parent);
602 if (err != NULL)
603 goto done;
604 if (rename(tmppath, newpath) == -1) {
605 err = got_error_from_errno3("rename", tmppath,
606 newpath);
607 goto done;
611 if (file_renamed) {
612 err = got_worktree_schedule_delete(worktree, &oldpaths,
613 0, NULL, patch_delete, pa, repo, 0, 0);
614 if (err == NULL)
615 err = got_worktree_schedule_add(worktree, &newpaths,
616 patch_add, pa, repo, 1);
617 if (err)
618 unlink(newpath);
619 } else if (p->old == NULL) {
620 err = got_worktree_schedule_add(worktree, &newpaths,
621 patch_add, pa, repo, 1);
622 if (err)
623 unlink(newpath);
624 } else
625 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
626 NULL);
628 done:
629 got_pathlist_free(&oldpaths);
630 got_pathlist_free(&newpaths);
631 free(parent);
632 free(template);
633 if (tmppath != NULL)
634 unlink(tmppath);
635 free(tmppath);
636 return err;
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;
650 pid_t pid;
652 ibuf = calloc(1, sizeof(*ibuf));
653 if (ibuf == NULL) {
654 err = got_error_from_errno("calloc");
655 goto done;
658 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
659 err = got_error_from_errno("socketpair");
660 goto done;
663 pid = fork();
664 if (pid == -1) {
665 err = got_error_from_errno("fork");
666 goto done;
667 } else if (pid == 0) {
668 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
669 NULL);
670 /* not reached */
673 if (close(imsg_fds[1]) == -1) {
674 err = got_error_from_errno("close");
675 goto done;
677 imsg_fds[1] = -1;
678 imsg_init(ibuf, imsg_fds[0]);
680 err = send_patch(ibuf, fd);
681 fd = -1;
682 if (err)
683 goto done;
685 err = got_worktree_patch_prepare(&fileindex, worktree);
686 if (err)
687 goto done;
689 while (!done && err == NULL) {
690 struct got_patch p;
691 struct patch_args pa;
693 pa.progress_cb = progress_cb;
694 pa.progress_arg = progress_arg;
695 pa.head = &p.head;
697 err = recv_patch(ibuf, &done, &p);
698 if (err || done)
699 break;
701 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
702 &newpath, worktree, repo, fileindex);
703 if (err == NULL)
704 err = apply_patch(worktree, repo, oldpath, newpath,
705 &p, nop, &pa, cancel_cb, cancel_arg);
706 if (err != NULL) {
707 failed = 1;
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);
718 free(oldpath);
719 free(newpath);
720 patch_free(&p);
722 if (err)
723 break;
726 done:
727 if (fileindex)
728 got_worktree_patch_complete(fileindex);
729 if (fd != -1 && close(fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
731 if (ibuf != NULL)
732 imsg_clear(ibuf);
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);
739 return err;