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/queue.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/uio.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <imsg.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_cancel.h"
43 #include "got_worktree.h"
44 #include "got_opentemp.h"
45 #include "got_patch.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 struct got_patch_hunk {
54 STAILQ_ENTRY(got_patch_hunk) entries;
55 const struct got_error *err;
56 long offset;
57 int nonl;
58 long old_from;
59 long old_lines;
60 long new_from;
61 long new_lines;
62 size_t len;
63 size_t cap;
64 char **lines;
65 };
67 STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
68 struct got_patch {
69 char *old;
70 char *new;
71 struct got_patch_hunk_head head;
72 };
74 struct patch_args {
75 got_patch_progress_cb progress_cb;
76 void *progress_arg;
77 struct got_patch_hunk_head *head;
78 };
80 static const struct got_error *
81 send_patch(struct imsgbuf *ibuf, int fd)
82 {
83 const struct got_error *err = NULL;
85 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
86 NULL, 0) == -1) {
87 err = got_error_from_errno(
88 "imsg_compose GOT_IMSG_PATCH_FILE");
89 close(fd);
90 return err;
91 }
93 if (imsg_flush(ibuf) == -1) {
94 err = got_error_from_errno("imsg_flush");
95 imsg_clear(ibuf);
96 }
98 return err;
99 }
101 static void
102 patch_free(struct got_patch *p)
104 struct got_patch_hunk *h;
105 size_t i;
107 while (!STAILQ_EMPTY(&p->head)) {
108 h = STAILQ_FIRST(&p->head);
109 STAILQ_REMOVE_HEAD(&p->head, entries);
111 for (i = 0; i < h->len; ++i)
112 free(h->lines[i]);
113 free(h->lines);
114 free(h);
117 free(p->new);
118 free(p->old);
121 static const struct got_error *
122 pushline(struct got_patch_hunk *h, const char *line)
124 void *t;
125 size_t newcap;
127 if (h->len == h->cap) {
128 if ((newcap = h->cap * 1.5) == 0)
129 newcap = 16;
130 t = recallocarray(h->lines, h->cap, newcap,
131 sizeof(h->lines[0]));
132 if (t == NULL)
133 return got_error_from_errno("recallocarray");
134 h->lines = t;
135 h->cap = newcap;
138 if ((t = strdup(line)) == NULL)
139 return got_error_from_errno("strdup");
141 h->lines[h->len++] = t;
142 return NULL;
145 static const struct got_error *
146 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
148 const struct got_error *err = NULL;
149 struct imsg imsg;
150 struct got_imsg_patch_hunk hdr;
151 struct got_imsg_patch patch;
152 struct got_patch_hunk *h = NULL;
153 size_t datalen;
155 memset(p, 0, sizeof(*p));
156 STAILQ_INIT(&p->head);
158 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
159 if (err)
160 return err;
161 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
162 *done = 1;
163 goto done;
165 if (imsg.hdr.type != GOT_IMSG_PATCH) {
166 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 goto done;
169 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
170 if (datalen != sizeof(patch)) {
171 err = got_error(GOT_ERR_PRIVSEP_LEN);
172 goto done;
174 memcpy(&patch, imsg.data, sizeof(patch));
175 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
176 err = got_error_from_errno("strdup");
177 goto done;
179 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
180 err = got_error_from_errno("strdup");
181 goto done;
183 if (p->old == NULL && p->new == NULL) {
184 err = got_error(GOT_ERR_PATCH_MALFORMED);
185 goto done;
188 imsg_free(&imsg);
190 for (;;) {
191 char *t;
193 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
194 if (err)
195 return err;
197 switch (imsg.hdr.type) {
198 case GOT_IMSG_PATCH_DONE:
199 goto done;
200 case GOT_IMSG_PATCH_HUNK:
201 if (h != NULL && h->nonl) {
202 err = got_error(GOT_ERR_PATCH_MALFORMED);
203 goto done;
205 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
206 if (datalen != sizeof(hdr)) {
207 err = got_error(GOT_ERR_PRIVSEP_LEN);
208 goto done;
210 memcpy(&hdr, imsg.data, sizeof(hdr));
211 if ((h = calloc(1, sizeof(*h))) == NULL) {
212 err = got_error_from_errno("calloc");
213 goto done;
215 h->old_from = hdr.oldfrom;
216 h->old_lines = hdr.oldlines;
217 h->new_from = hdr.newfrom;
218 h->new_lines = hdr.newlines;
219 STAILQ_INSERT_TAIL(&p->head, h, entries);
220 break;
221 case GOT_IMSG_PATCH_LINE:
222 if (h == NULL) {
223 err = got_error(GOT_ERR_PRIVSEP_MSG);
224 goto done;
226 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
227 t = imsg.data;
228 /* at least one char */
229 if (datalen < 2 || t[datalen-1] != '\0') {
230 err = got_error(GOT_ERR_PRIVSEP_MSG);
231 goto done;
233 if (*t != ' ' && *t != '-' && *t != '+' &&
234 *t != '\\') {
235 err = got_error(GOT_ERR_PRIVSEP_MSG);
236 goto done;
238 if (h->nonl)
239 err = got_error(GOT_ERR_PATCH_MALFORMED);
240 if (*t == '\\')
241 h->nonl = 1;
242 else
243 err = pushline(h, t);
244 if (err)
245 goto done;
246 break;
247 default:
248 err = got_error(GOT_ERR_PRIVSEP_MSG);
249 goto done;
252 imsg_free(&imsg);
255 done:
256 imsg_free(&imsg);
257 return err;
260 /*
261 * Copy data from orig starting at copypos until pos into tmp.
262 * If pos is -1, copy until EOF.
263 */
264 static const struct got_error *
265 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
267 char buf[BUFSIZ];
268 size_t len, r, w;
270 if (fseek(orig, copypos, SEEK_SET) == -1)
271 return got_error_from_errno("fseek");
273 while (pos == -1 || copypos < pos) {
274 len = sizeof(buf);
275 if (pos > 0)
276 len = MIN(len, (size_t)pos - copypos);
277 r = fread(buf, 1, len, orig);
278 if (r != len && ferror(orig))
279 return got_error_from_errno("fread");
280 w = fwrite(buf, 1, r, tmp);
281 if (w != r)
282 return got_error_from_errno("fwrite");
283 copypos += len;
284 if (r != len && feof(orig)) {
285 if (pos == -1)
286 return NULL;
287 return got_error(GOT_ERR_HUNK_FAILED);
290 return NULL;
293 static const struct got_error *
294 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
296 const struct got_error *err = NULL;
297 char *line = NULL;
298 char mode = *h->lines[0];
299 size_t linesize = 0;
300 ssize_t linelen;
301 off_t match = -1;
302 long match_lineno = -1;
304 for (;;) {
305 linelen = getline(&line, &linesize, orig);
306 if (linelen == -1) {
307 if (ferror(orig))
308 err = got_error_from_errno("getline");
309 else if (match == -1)
310 err = got_error(GOT_ERR_HUNK_FAILED);
311 break;
313 if (line[linelen - 1] == '\n')
314 line[linelen - 1] = '\0';
315 (*lineno)++;
317 if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
318 (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
319 (mode == '+' && *lineno == h->old_from)) {
320 match = ftello(orig);
321 if (match == -1) {
322 err = got_error_from_errno("ftello");
323 break;
325 match -= linelen;
326 match_lineno = (*lineno)-1;
329 if (*lineno >= h->old_from && match != -1)
330 break;
333 if (err == NULL) {
334 *pos = match;
335 *lineno = match_lineno;
336 if (fseek(orig, match, SEEK_SET) == -1)
337 err = got_error_from_errno("fseek");
340 free(line);
341 return err;
344 static const struct got_error *
345 test_hunk(FILE *orig, struct got_patch_hunk *h)
347 const struct got_error *err = NULL;
348 char *line = NULL;
349 size_t linesize = 0, i = 0;
350 ssize_t linelen;
352 for (i = 0; i < h->len; ++i) {
353 switch (*h->lines[i]) {
354 case '+':
355 continue;
356 case ' ':
357 case '-':
358 linelen = getline(&line, &linesize, orig);
359 if (linelen == -1) {
360 if (ferror(orig))
361 err = got_error_from_errno("getline");
362 else
363 err = got_error(
364 GOT_ERR_HUNK_FAILED);
365 goto done;
367 if (line[linelen - 1] == '\n')
368 line[linelen - 1] = '\0';
369 if (strcmp(h->lines[i] + 1, line)) {
370 err = got_error(GOT_ERR_HUNK_FAILED);
371 goto done;
373 break;
377 done:
378 free(line);
379 return err;
382 static const struct got_error *
383 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
385 size_t i = 0;
387 for (i = 0; i < h->len; ++i) {
388 switch (*h->lines[i]) {
389 case ' ':
390 if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
391 return got_error_from_errno("fprintf");
392 /* fallthrough */
393 case '-':
394 (*lineno)++;
395 break;
396 case '+':
397 if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
398 return got_error_from_errno("fprintf");
399 if (i != h->len - 1 || !h->nonl) {
400 if (fprintf(tmp, "\n") < 0)
401 return got_error_from_errno(
402 "fprintf");
404 break;
407 return NULL;
410 static const struct got_error *
411 patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
412 mode_t *mode)
414 const struct got_error *err = NULL;
415 struct got_patch_hunk *h;
416 struct stat sb;
417 long lineno = 0;
418 FILE *orig;
419 off_t copypos, pos;
420 char *line = NULL;
421 size_t linesize = 0;
422 ssize_t linelen;
424 if (p->old == NULL) { /* create */
425 h = STAILQ_FIRST(&p->head);
426 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
427 return got_error(GOT_ERR_PATCH_MALFORMED);
428 if (nop)
429 return NULL;
430 return apply_hunk(tmp, h, &lineno);
433 if ((orig = fopen(path, "r")) == NULL) {
434 err = got_error_from_errno2("fopen", path);
435 goto done;
438 if (fstat(fileno(orig), &sb) == -1) {
439 err = got_error_from_errno("fstat");
440 goto done;
442 *mode = sb.st_mode;
444 copypos = 0;
445 STAILQ_FOREACH(h, &p->head, entries) {
446 if (h->lines == NULL)
447 break;
449 tryagain:
450 err = locate_hunk(orig, h, &pos, &lineno);
451 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
452 h->err = err;
453 if (err != NULL)
454 goto done;
455 if (!nop)
456 err = copy(tmp, orig, copypos, pos);
457 if (err != NULL)
458 goto done;
459 copypos = pos;
461 err = test_hunk(orig, h);
462 if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
463 /*
464 * try to apply the hunk again starting the search
465 * after the previous partial match.
466 */
467 if (fseek(orig, pos, SEEK_SET) == -1) {
468 err = got_error_from_errno("fseek");
469 goto done;
471 linelen = getline(&line, &linesize, orig);
472 if (linelen == -1) {
473 err = got_error_from_errno("getline");
474 goto done;
476 lineno++;
477 goto tryagain;
479 if (err != NULL)
480 goto done;
482 if (lineno + 1 != h->old_from)
483 h->offset = lineno + 1 - h->old_from;
485 if (!nop)
486 err = apply_hunk(tmp, h, &lineno);
487 if (err != NULL)
488 goto done;
490 copypos = ftello(orig);
491 if (copypos == -1) {
492 err = got_error_from_errno("ftello");
493 goto done;
497 if (p->new == NULL && sb.st_size != copypos) {
498 h = STAILQ_FIRST(&p->head);
499 h->err = got_error(GOT_ERR_HUNK_FAILED);
500 err = h->err;
501 } else if (!nop && !feof(orig))
502 err = copy(tmp, orig, copypos, -1);
504 done:
505 if (orig != NULL)
506 fclose(orig);
507 return err;
510 static const struct got_error *
511 report_progress(struct patch_args *pa, const char *old, const char *new,
512 unsigned char status, const struct got_error *orig_error)
514 const struct got_error *err;
515 struct got_patch_hunk *h;
517 err = pa->progress_cb(pa->progress_arg, old, new, status,
518 orig_error, 0, 0, 0, 0, 0, NULL);
519 if (err)
520 return err;
522 STAILQ_FOREACH(h, pa->head, entries) {
523 if (h->offset == 0 && h->err == NULL)
524 continue;
526 err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
527 h->old_from, h->old_lines, h->new_from, h->new_lines,
528 h->offset, h->err);
529 if (err)
530 return err;
533 return NULL;
536 static const struct got_error *
537 patch_delete(void *arg, unsigned char status, unsigned char staged_status,
538 const char *path)
540 return report_progress(arg, path, NULL, status, NULL);
543 static const struct got_error *
544 patch_add(void *arg, unsigned char status, const char *path)
546 return report_progress(arg, NULL, path, status, NULL);
549 static const struct got_error *
550 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
551 const char *oldpath, const char *newpath, struct got_patch *p,
552 int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
554 const struct got_error *err = NULL;
555 struct got_pathlist_head oldpaths, newpaths;
556 struct got_pathlist_entry *pe;
557 int file_renamed = 0;
558 char *tmppath = NULL, *template = NULL, *parent = NULL;;
559 FILE *tmp = NULL;
560 mode_t mode = GOT_DEFAULT_FILE_MODE;
562 TAILQ_INIT(&oldpaths);
563 TAILQ_INIT(&newpaths);
565 err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
566 if (err)
567 goto done;
568 err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
569 if (err)
570 goto done;
572 file_renamed = strcmp(oldpath, newpath);
574 if (p->old != NULL && p->new == NULL) {
575 /*
576 * special case: delete a file. don't try to match
577 * the lines but just schedule the removal.
578 */
579 err = got_worktree_schedule_delete(worktree, &oldpaths,
580 0, NULL, delete_cb, delete_arg, repo, 0, 0);
581 goto done;
584 if (asprintf(&template, "%s/got-patch",
585 got_worktree_get_root_path(worktree)) == -1) {
586 err = got_error_from_errno(template);
587 goto done;
590 if (!nop)
591 err = got_opentemp_named(&tmppath, &tmp, template);
592 if (err)
593 goto done;
594 err = patch_file(p, oldpath, tmp, nop, &mode);
595 if (err)
596 goto done;
598 if (nop)
599 goto done;
601 if (p->old != NULL && p->new == NULL) {
602 err = got_worktree_schedule_delete(worktree, &oldpaths,
603 0, NULL, patch_delete, pa, repo, 0, 0);
604 goto done;
607 if (fchmod(fileno(tmp), mode) == -1) {
608 err = got_error_from_errno2("chmod", newpath);
609 goto done;
612 if (rename(tmppath, newpath) == -1) {
613 if (errno != ENOENT) {
614 err = got_error_from_errno3("rename", tmppath,
615 newpath);
616 goto done;
619 err = got_path_dirname(&parent, newpath);
620 if (err != NULL)
621 goto done;
622 err = got_path_mkdir(parent);
623 if (err != NULL)
624 goto done;
625 if (rename(tmppath, newpath) == -1) {
626 err = got_error_from_errno3("rename", tmppath,
627 newpath);
628 goto done;
632 if (file_renamed) {
633 err = got_worktree_schedule_delete(worktree, &oldpaths,
634 0, NULL, patch_delete, pa, repo, 0, 0);
635 if (err == NULL)
636 err = got_worktree_schedule_add(worktree, &newpaths,
637 patch_add, pa, repo, 1);
638 if (err)
639 unlink(newpath);
640 } else if (p->old == NULL) {
641 err = got_worktree_schedule_add(worktree, &newpaths,
642 patch_add, pa, repo, 1);
643 if (err)
644 unlink(newpath);
645 } else
646 err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
647 NULL);
649 done:
650 got_pathlist_free(&oldpaths);
651 got_pathlist_free(&newpaths);
652 free(parent);
653 free(template);
654 if (tmppath != NULL)
655 unlink(tmppath);
656 free(tmppath);
657 return err;
660 const struct got_error *
661 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
662 int nop, got_patch_progress_cb progress_cb, void *progress_arg,
663 got_cancel_cb cancel_cb, void *cancel_arg)
665 const struct got_error *err = NULL;
666 struct got_fileindex *fileindex = NULL;
667 char *oldpath, *newpath;
668 struct imsgbuf *ibuf;
669 int imsg_fds[2] = {-1, -1};
670 int done = 0, failed = 0;
671 pid_t pid;
673 ibuf = calloc(1, sizeof(*ibuf));
674 if (ibuf == NULL) {
675 err = got_error_from_errno("calloc");
676 goto done;
679 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
680 err = got_error_from_errno("socketpair");
681 goto done;
684 pid = fork();
685 if (pid == -1) {
686 err = got_error_from_errno("fork");
687 goto done;
688 } else if (pid == 0) {
689 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
690 NULL);
691 /* not reached */
694 if (close(imsg_fds[1]) == -1) {
695 err = got_error_from_errno("close");
696 goto done;
698 imsg_fds[1] = -1;
699 imsg_init(ibuf, imsg_fds[0]);
701 err = send_patch(ibuf, fd);
702 fd = -1;
703 if (err)
704 goto done;
706 err = got_worktree_patch_prepare(&fileindex, worktree);
707 if (err)
708 goto done;
710 while (!done && err == NULL) {
711 struct got_patch p;
712 struct patch_args pa;
714 pa.progress_cb = progress_cb;
715 pa.progress_arg = progress_arg;
716 pa.head = &p.head;
718 err = recv_patch(ibuf, &done, &p);
719 if (err || done)
720 break;
722 err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
723 &newpath, worktree, repo, fileindex);
724 if (err == NULL)
725 err = apply_patch(worktree, repo, oldpath, newpath,
726 &p, nop, &pa, cancel_cb, cancel_arg);
727 if (err != NULL) {
728 failed = 1;
729 /* recoverable errors */
730 if (err->code == GOT_ERR_FILE_STATUS ||
731 (err->code == GOT_ERR_ERRNO && errno == ENOENT))
732 err = report_progress(&pa, p.old, p.new,
733 GOT_STATUS_CANNOT_UPDATE, err);
734 else if (err->code == GOT_ERR_HUNK_FAILED)
735 err = report_progress(&pa, p.old, p.new,
736 GOT_STATUS_CANNOT_UPDATE, NULL);
739 free(oldpath);
740 free(newpath);
741 patch_free(&p);
743 if (err)
744 break;
747 done:
748 if (fileindex)
749 got_worktree_patch_complete(fileindex);
750 if (fd != -1 && close(fd) == -1 && err == NULL)
751 err = got_error_from_errno("close");
752 if (ibuf != NULL)
753 imsg_clear(ibuf);
754 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
755 err = got_error_from_errno("close");
756 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
757 err = got_error_from_errno("close");
758 if (err == NULL && failed)
759 err = got_error(GOT_ERR_PATCH_FAILED);
760 return err;