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 <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <imsg.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_path.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;
59 long offset;
60 long old_from;
61 long old_lines;
62 long new_from;
63 long new_lines;
64 size_t len;
65 size_t cap;
66 char **lines;
67 };
69 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
70 struct got_patch {
71 char *old;
72 char *new;
73 struct got_patch_hunk_head head;
74 };
76 struct patch_args {
77 got_patch_progress_cb progress_cb;
78 void *progress_arg;
79 struct got_patch_hunk_head *head;
80 };
82 static const struct got_error *
83 send_patch(struct imsgbuf *ibuf, int fd)
84 {
85 const struct got_error *err = NULL;
87 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
88 NULL, 0) == -1) {
89 err = got_error_from_errno(
90 "imsg_compose GOT_IMSG_PATCH_FILE");
91 close(fd);
92 return err;
93 }
95 if (imsg_flush(ibuf) == -1) {
96 err = got_error_from_errno("imsg_flush");
97 imsg_clear(ibuf);
98 }
100 return err;
103 static void
104 patch_free(struct got_patch *p)
106 struct got_patch_hunk *h;
107 size_t i;
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)
114 free(h->lines[i]);
115 free(h->lines);
116 free(h);
119 free(p->new);
120 free(p->old);
123 static const struct got_error *
124 pushline(struct got_patch_hunk *h, const char *line)
126 void *t;
127 size_t newcap;
129 if (h->len == h->cap) {
130 if ((newcap = h->cap * 1.5) == 0)
131 newcap = 16;
132 t = recallocarray(h->lines, h->cap, newcap,
133 sizeof(h->lines[0]));
134 if (t == NULL)
135 return got_error_from_errno("recallocarray");
136 h->lines = t;
137 h->cap = newcap;
140 if ((t = strdup(line)) == NULL)
141 return got_error_from_errno("strdup");
143 h->lines[h->len++] = t;
144 return NULL;
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;
151 struct imsg imsg;
152 struct got_imsg_patch_hunk hdr;
153 struct got_imsg_patch patch;
154 struct got_patch_hunk *h = NULL;
155 size_t datalen;
157 memset(p, 0, sizeof(*p));
158 STAILQ_INIT(&p->head);
160 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
161 if (err)
162 return err;
163 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
164 *done = 1;
165 goto done;
167 if (imsg.hdr.type != GOT_IMSG_PATCH) {
168 err = got_error(GOT_ERR_PRIVSEP_MSG);
169 goto done;
171 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
172 if (datalen != sizeof(patch)) {
173 err = got_error(GOT_ERR_PRIVSEP_LEN);
174 goto done;
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");
179 goto done;
181 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
182 err = got_error_from_errno("strdup");
183 goto done;
185 if (p->old == NULL && p->new == NULL) {
186 err = got_error(GOT_ERR_PATCH_MALFORMED);
187 goto done;
190 imsg_free(&imsg);
192 for (;;) {
193 char *t;
195 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
196 if (err)
197 return err;
199 switch (imsg.hdr.type) {
200 case GOT_IMSG_PATCH_DONE:
201 goto 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);
206 goto done;
208 memcpy(&hdr, imsg.data, sizeof(hdr));
209 if ((h = calloc(1, sizeof(*h))) == NULL) {
210 err = got_error_from_errno("calloc");
211 goto done;
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);
218 break;
219 case GOT_IMSG_PATCH_LINE:
220 if (h == NULL) {
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
222 goto done;
224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
225 t = imsg.data;
226 /* at least one char plus newline */
227 if (datalen < 2 || t[datalen-1] != '\0') {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
229 goto done;
231 if (*t != ' ' && *t != '-' && *t != '+') {
232 err = got_error(GOT_ERR_PRIVSEP_MSG);
233 goto done;
235 err = pushline(h, t);
236 if (err)
237 goto done;
238 break;
239 default:
240 err = got_error(GOT_ERR_PRIVSEP_MSG);
241 goto done;
244 imsg_free(&imsg);
247 done:
248 imsg_free(&imsg);
249 return err;
252 /*
253 * Copy data from orig starting at copypos until pos into tmp.
254 * If pos is -1, copy until EOF.
255 */
256 static const struct got_error *
257 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
259 char buf[BUFSIZ];
260 size_t len, r, w;
262 if (fseek(orig, copypos, SEEK_SET) == -1)
263 return got_error_from_errno("fseek");
265 while (pos == -1 || copypos < pos) {
266 len = sizeof(buf);
267 if (pos > 0)
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);
273 if (w != r)
274 return got_error_from_errno("fwrite");
275 copypos += len;
276 if (r != len && feof(orig)) {
277 if (pos == -1)
278 return NULL;
279 return got_error(GOT_ERR_HUNK_FAILED);
282 return NULL;
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;
289 char *line = NULL;
290 char mode = *h->lines[0];
291 size_t linesize = 0;
292 ssize_t linelen;
293 off_t match = -1;
294 long match_lineno = -1;
296 for (;;) {
297 linelen = getline(&line, &linesize, orig);
298 if (linelen == -1) {
299 if (ferror(orig))
300 err = got_error_from_errno("getline");
301 else if (match == -1)
302 err = got_error(GOT_ERR_HUNK_FAILED);
303 break;
305 (*lineno)++;
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);
311 if (match == -1) {
312 err = got_error_from_errno("ftello");
313 break;
315 match -= linelen;
316 match_lineno = (*lineno)-1;
319 if (*lineno >= h->old_from && match != -1)
320 break;
323 if (err == NULL) {
324 *pos = match;
325 *lineno = match_lineno;
326 if (fseek(orig, match, SEEK_SET) == -1)
327 err = got_error_from_errno("fseek");
330 free(line);
331 return err;
334 static const struct got_error *
335 test_hunk(FILE *orig, struct got_patch_hunk *h)
337 const struct got_error *err = NULL;
338 char *line = NULL;
339 size_t linesize = 0, i = 0;
340 ssize_t linelen;
342 for (i = 0; i < h->len; ++i) {
343 switch (*h->lines[i]) {
344 case '+':
345 continue;
346 case ' ':
347 case '-':
348 linelen = getline(&line, &linesize, orig);
349 if (linelen == -1) {
350 if (ferror(orig))
351 err = got_error_from_errno("getline");
352 else
353 err = got_error(
354 GOT_ERR_HUNK_FAILED);
355 goto done;
357 if (strcmp(h->lines[i] + 1, line)) {
358 err = got_error(GOT_ERR_HUNK_FAILED);
359 goto done;
361 break;
365 done:
366 free(line);
367 return err;
370 static const struct got_error *
371 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
373 size_t i = 0;
375 for (i = 0; i < h->len; ++i) {
376 switch (*h->lines[i]) {
377 case ' ':
378 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
379 return got_error_from_errno("fprintf");
380 /* fallthrough */
381 case '-':
382 (*lineno)++;
383 break;
384 case '+':
385 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
386 return got_error_from_errno("fprintf");
387 break;
390 return NULL;
393 static const struct got_error *
394 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
395 mode_t *mode)
397 const struct got_error *err = NULL;
398 struct got_patch_hunk *h;
399 struct stat sb;
400 size_t i;
401 long lineno = 0;
402 FILE *orig;
403 off_t copypos, pos;
404 char *line = NULL;
405 size_t linesize = 0;
406 ssize_t linelen;
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);
412 if (nop)
413 return NULL;
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");
418 return err;
421 if ((orig = fopen(path, "r")) == NULL) {
422 err = got_error_from_errno2("fopen", path);
423 goto done;
426 if (fstat(fileno(orig), &sb) == -1) {
427 err = got_error_from_errno("fstat");
428 goto done;
430 *mode = sb.st_mode;
432 copypos = 0;
433 STAILQ_FOREACH(h, &p->head, entries) {
434 if (h->lines == NULL)
435 break;
437 tryagain:
438 err = locate_hunk(orig, h, &pos, &lineno);
439 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
440 h->err = err;
441 if (err != NULL)
442 goto done;
443 if (!nop)
444 err = copy(tmp, orig, copypos, pos);
445 if (err != NULL)
446 goto done;
447 copypos = pos;
449 err = test_hunk(orig, h);
450 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
451 /*
452 * try to apply the hunk again starting the search
453 * after the previous partial match.
454 */
455 if (fseek(orig, pos, SEEK_SET) == -1) {
456 err = got_error_from_errno("fseek");
457 goto done;
459 linelen = getline(&line, &linesize, orig);
460 if (linelen == -1) {
461 err = got_error_from_errno("getline");
462 goto done;
464 lineno++;
465 goto tryagain;
467 if (err != NULL)
468 goto done;
470 if (lineno + 1 != h->old_from)
471 h->offset = lineno + 1 - h->old_from;
473 if (!nop)
474 err = apply_hunk(tmp, h, &lineno);
475 if (err != NULL)
476 goto done;
478 copypos = ftello(orig);
479 if (copypos == -1) {
480 err = got_error_from_errno("ftello");
481 goto done;
485 if (p->new == NULL && sb.st_size != copypos) {
486 h = STAILQ_FIRST(&p->head);
487 h->err = got_error(GOT_ERR_HUNK_FAILED);
488 err = h->err;
489 } else if (!nop && !feof(orig))
490 err = copy(tmp, orig, copypos, -1);
492 done:
493 if (orig != NULL)
494 fclose(orig);
495 return err;
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);
507 if (err)
508 return err;
510 STAILQ_FOREACH(h, pa->head, entries) {
511 if (h->offset == 0 && h->err == NULL)
512 continue;
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,
516 h->offset, h->err);
517 if (err)
518 return err;
521 return NULL;
524 static const struct got_error *
525 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
526 const char *path)
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;;
547 FILE *tmp = 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);
554 if (err)
555 goto done;
556 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
557 if (err)
558 goto done;
560 file_renamed = strcmp(oldpath, newpath);
562 if (p->old != NULL && p->new == NULL) {
563 /*
564 * special case: delete a file. don't try to match
565 * the lines but just schedule the removal.
566 */
567 err = got_worktree_schedule_delete(worktree, &oldpaths,
568 0, NULL, delete_cb, delete_arg, repo, 0, 0);
569 goto done;
572 if (asprintf(&template, "%s/got-patch",
573 got_worktree_get_root_path(worktree)) == -1) {
574 err = got_error_from_errno(template);
575 goto done;
578 if (!nop)
579 err = got_opentemp_named(&tmppath, &tmp, template);
580 if (err)
581 goto done;
582 err = patch_file(p, oldpath, tmp, nop, &mode);
583 if (err)
584 goto done;
586 if (nop)
587 goto done;
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);
592 goto done;
595 if (fchmod(fileno(tmp), mode) == -1) {
596 err = got_error_from_errno2("chmod", newpath);
597 goto done;
600 if (rename(tmppath, newpath) == -1) {
601 if (errno != ENOENT) {
602 err = got_error_from_errno3("rename", tmppath,
603 newpath);
604 goto done;
607 err = got_path_dirname(&parent, newpath);
608 if (err != NULL)
609 goto done;
610 err = got_path_mkdir(parent);
611 if (err != NULL)
612 goto done;
613 if (rename(tmppath, newpath) == -1) {
614 err = got_error_from_errno3("rename", tmppath,
615 newpath);
616 goto done;
620 if (file_renamed) {
621 err = got_worktree_schedule_delete(worktree, &oldpaths,
622 0, NULL, patch_delete, pa, repo, 0, 0);
623 if (err == NULL)
624 err = got_worktree_schedule_add(worktree, &newpaths,
625 patch_add, pa, repo, 1);
626 if (err)
627 unlink(newpath);
628 } else if (p->old == NULL) {
629 err = got_worktree_schedule_add(worktree, &newpaths,
630 patch_add, pa, repo, 1);
631 if (err)
632 unlink(newpath);
633 } else
634 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
635 NULL);
637 done:
638 got_pathlist_free(&oldpaths);
639 got_pathlist_free(&newpaths);
640 free(parent);
641 free(template);
642 if (tmppath != NULL)
643 unlink(tmppath);
644 free(tmppath);
645 return err;
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;
659 pid_t pid;
661 ibuf = calloc(1, sizeof(*ibuf));
662 if (ibuf == NULL) {
663 err = got_error_from_errno("calloc");
664 goto done;
667 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
668 err = got_error_from_errno("socketpair");
669 goto done;
672 pid = fork();
673 if (pid == -1) {
674 err = got_error_from_errno("fork");
675 goto done;
676 } else if (pid == 0) {
677 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
678 NULL);
679 /* not reached */
682 if (close(imsg_fds[1]) == -1) {
683 err = got_error_from_errno("close");
684 goto done;
686 imsg_fds[1] = -1;
687 imsg_init(ibuf, imsg_fds[0]);
689 err = send_patch(ibuf, fd);
690 fd = -1;
691 if (err)
692 goto done;
694 err = got_worktree_patch_prepare(&fileindex, worktree);
695 if (err)
696 goto done;
698 while (!done && err == NULL) {
699 struct got_patch p;
700 struct patch_args pa;
702 pa.progress_cb = progress_cb;
703 pa.progress_arg = progress_arg;
704 pa.head = &p.head;
706 err = recv_patch(ibuf, &done, &p);
707 if (err || done)
708 break;
710 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
711 &newpath, worktree, repo, fileindex);
712 if (err == NULL)
713 err = apply_patch(worktree, repo, oldpath, newpath,
714 &p, nop, &pa, cancel_cb, cancel_arg);
715 if (err != NULL) {
716 failed = 1;
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);
727 free(oldpath);
728 free(newpath);
729 patch_free(&p);
731 if (err)
732 break;
735 done:
736 if (fileindex)
737 got_worktree_patch_complete(fileindex);
738 if (fd != -1 && close(fd) == -1 && err == NULL)
739 err = got_error_from_errno("close");
740 if (ibuf != NULL)
741 imsg_clear(ibuf);
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);
748 return err;