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 long old_from;
60 long old_lines;
61 long new_from;
62 long new_lines;
63 size_t len;
64 size_t cap;
65 char **lines;
66 };
68 struct got_patch {
69 char *old;
70 char *new;
71 STAILQ_HEAD(, got_patch_hunk) head;
72 };
74 static const struct got_error *
75 send_patch(struct imsgbuf *ibuf, int fd)
76 {
77 const struct got_error *err = NULL;
79 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
80 NULL, 0) == -1) {
81 err = got_error_from_errno(
82 "imsg_compose GOT_IMSG_PATCH_FILE");
83 close(fd);
84 return err;
85 }
87 if (imsg_flush(ibuf) == -1) {
88 err = got_error_from_errno("imsg_flush");
89 imsg_clear(ibuf);
90 }
92 return err;
93 }
95 static void
96 patch_free(struct got_patch *p)
97 {
98 struct got_patch_hunk *h;
99 size_t i;
101 while (!STAILQ_EMPTY(&p->head)) {
102 h = STAILQ_FIRST(&p->head);
103 STAILQ_REMOVE_HEAD(&p->head, entries);
105 for (i = 0; i < h->len; ++i)
106 free(h->lines[i]);
107 free(h->lines);
108 free(h);
111 free(p->new);
112 free(p->old);
115 static const struct got_error *
116 pushline(struct got_patch_hunk *h, const char *line)
118 void *t;
119 size_t newcap;
121 if (h->len == h->cap) {
122 if ((newcap = h->cap * 1.5) == 0)
123 newcap = 16;
124 t = recallocarray(h->lines, h->cap, newcap,
125 sizeof(h->lines[0]));
126 if (t == NULL)
127 return got_error_from_errno("recallocarray");
128 h->lines = t;
129 h->cap = newcap;
132 if ((t = strdup(line)) == NULL)
133 return got_error_from_errno("strdup");
135 h->lines[h->len++] = t;
136 return NULL;
139 static const struct got_error *
140 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
142 const struct got_error *err = NULL;
143 struct imsg imsg;
144 struct got_imsg_patch_hunk hdr;
145 struct got_imsg_patch patch;
146 struct got_patch_hunk *h = NULL;
147 size_t datalen;
149 memset(p, 0, sizeof(*p));
150 STAILQ_INIT(&p->head);
152 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
153 if (err)
154 return err;
155 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
156 *done = 1;
157 goto done;
159 if (imsg.hdr.type != GOT_IMSG_PATCH) {
160 err = got_error(GOT_ERR_PRIVSEP_MSG);
161 goto done;
163 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
164 if (datalen != sizeof(patch)) {
165 err = got_error(GOT_ERR_PRIVSEP_LEN);
166 goto done;
168 memcpy(&patch, imsg.data, sizeof(patch));
169 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
170 err = got_error_from_errno("strdup");
171 goto done;
173 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
174 err = got_error_from_errno("strdup");
175 goto done;
177 if (p->old == NULL && p->new == NULL) {
178 err = got_error(GOT_ERR_PATCH_MALFORMED);
179 goto done;
182 imsg_free(&imsg);
184 for (;;) {
185 char *t;
187 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
188 if (err)
189 return err;
191 switch (imsg.hdr.type) {
192 case GOT_IMSG_PATCH_DONE:
193 goto done;
194 case GOT_IMSG_PATCH_HUNK:
195 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
196 if (datalen != sizeof(hdr)) {
197 err = got_error(GOT_ERR_PRIVSEP_LEN);
198 goto done;
200 memcpy(&hdr, imsg.data, sizeof(hdr));
201 if ((h = calloc(1, sizeof(*h))) == NULL) {
202 err = got_error_from_errno("calloc");
203 goto done;
205 h->old_from = hdr.oldfrom;
206 h->old_lines = hdr.oldlines;
207 h->new_from = hdr.newfrom;
208 h->new_lines = hdr.newlines;
209 STAILQ_INSERT_TAIL(&p->head, h, entries);
210 break;
211 case GOT_IMSG_PATCH_LINE:
212 if (h == NULL) {
213 err = got_error(GOT_ERR_PRIVSEP_MSG);
214 goto done;
216 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
217 t = imsg.data;
218 /* at least one char plus newline */
219 if (datalen < 2 || t[datalen-1] != '\0') {
220 err = got_error(GOT_ERR_PRIVSEP_MSG);
221 goto done;
223 if (*t != ' ' && *t != '-' && *t != '+') {
224 err = got_error(GOT_ERR_PRIVSEP_MSG);
225 goto done;
227 err = pushline(h, t);
228 if (err)
229 goto done;
230 break;
231 default:
232 err = got_error(GOT_ERR_PRIVSEP_MSG);
233 goto done;
236 imsg_free(&imsg);
239 done:
240 imsg_free(&imsg);
241 return err;
244 /*
245 * Copy data from orig starting at copypos until pos into tmp.
246 * If pos is -1, copy until EOF.
247 */
248 static const struct got_error *
249 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
251 char buf[BUFSIZ];
252 size_t len, r, w;
254 if (fseek(orig, copypos, SEEK_SET) == -1)
255 return got_error_from_errno("fseek");
257 while (pos == -1 || copypos < pos) {
258 len = sizeof(buf);
259 if (pos > 0)
260 len = MIN(len, (size_t)pos - copypos);
261 r = fread(buf, 1, len, orig);
262 if (r != len && ferror(orig))
263 return got_error_from_errno("fread");
264 w = fwrite(buf, 1, r, tmp);
265 if (w != r)
266 return got_error_from_errno("fwrite");
267 copypos += len;
268 if (r != len && feof(orig)) {
269 if (pos == -1)
270 return NULL;
271 return got_error(GOT_ERR_PATCH_DONT_APPLY);
274 return NULL;
277 static const struct got_error *
278 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
280 const struct got_error *err = NULL;
281 char *line = NULL;
282 char mode = *h->lines[0];
283 size_t linesize = 0;
284 ssize_t linelen;
285 off_t match = -1;
286 long match_lineno = -1;
288 for (;;) {
289 linelen = getline(&line, &linesize, orig);
290 if (linelen == -1) {
291 if (ferror(orig))
292 err = got_error_from_errno("getline");
293 else if (match == -1)
294 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
295 break;
297 (*lineno)++;
299 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
300 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
301 (mode == '+' && *lineno == h->old_from)) {
302 match = ftello(orig);
303 if (match == -1) {
304 err = got_error_from_errno("ftello");
305 break;
307 match -= linelen;
308 match_lineno = (*lineno)-1;
311 if (*lineno >= h->old_from && match != -1)
312 break;
315 if (err == NULL) {
316 *pos = match;
317 *lineno = match_lineno;
318 if (fseek(orig, match, SEEK_SET) == -1)
319 err = got_error_from_errno("fseek");
322 free(line);
323 return err;
326 static const struct got_error *
327 test_hunk(FILE *orig, struct got_patch_hunk *h)
329 const struct got_error *err = NULL;
330 char *line = NULL;
331 size_t linesize = 0, i = 0;
332 ssize_t linelen;
334 for (i = 0; i < h->len; ++i) {
335 switch (*h->lines[i]) {
336 case '+':
337 continue;
338 case ' ':
339 case '-':
340 linelen = getline(&line, &linesize, orig);
341 if (linelen == -1) {
342 if (ferror(orig))
343 err = got_error_from_errno("getline");
344 else
345 err = got_error(
346 GOT_ERR_PATCH_DONT_APPLY);
347 goto done;
349 if (strcmp(h->lines[i]+1, line)) {
350 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
351 goto done;
353 break;
357 done:
358 free(line);
359 return err;
362 static const struct got_error *
363 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
365 size_t i = 0;
367 for (i = 0; i < h->len; ++i) {
368 switch (*h->lines[i]) {
369 case ' ':
370 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
371 return got_error_from_errno("fprintf");
372 /* fallthrough */
373 case '-':
374 (*lineno)++;
375 break;
376 case '+':
377 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
378 return got_error_from_errno("fprintf");
379 break;
382 return NULL;
385 static const struct got_error *
386 patch_file(struct got_patch *p, const char *path, FILE *tmp)
388 const struct got_error *err = NULL;
389 struct got_patch_hunk *h;
390 size_t i;
391 long lineno = 0;
392 FILE *orig;
393 off_t copypos, pos;
394 char *line = NULL;
395 size_t linesize = 0;
396 ssize_t linelen;
398 if (p->old == NULL) { /* create */
399 h = STAILQ_FIRST(&p->head);
400 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
401 return got_error(GOT_ERR_PATCH_MALFORMED);
402 for (i = 0; i < h->len; ++i) {
403 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
404 return got_error_from_errno("fprintf");
406 return err;
409 if ((orig = fopen(path, "r")) == NULL) {
410 err = got_error_from_errno2("fopen", path);
411 goto done;
414 copypos = 0;
415 STAILQ_FOREACH(h, &p->head, entries) {
416 if (h->lines == NULL)
417 break;
419 tryagain:
420 err = locate_hunk(orig, h, &pos, &lineno);
421 if (err != NULL)
422 goto done;
423 err = copy(tmp, orig, copypos, pos);
424 if (err != NULL)
425 goto done;
426 copypos = pos;
428 err = test_hunk(orig, h);
429 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
430 /*
431 * try to apply the hunk again starting the search
432 * after the previous partial match.
433 */
434 if (fseek(orig, pos, SEEK_SET) == -1) {
435 err = got_error_from_errno("fseek");
436 goto done;
438 linelen = getline(&line, &linesize, orig);
439 if (linelen == -1) {
440 err = got_error_from_errno("getline");
441 goto done;
443 lineno++;
444 goto tryagain;
446 if (err != NULL)
447 goto done;
449 err = apply_hunk(tmp, h, &lineno);
450 if (err != NULL)
451 goto done;
453 copypos = ftello(orig);
454 if (copypos == -1) {
455 err = got_error_from_errno("ftello");
456 goto done;
461 if (p->new == NULL) {
462 struct stat sb;
464 if (fstat(fileno(orig), &sb) == -1)
465 err = got_error_from_errno("fstat");
466 else if (sb.st_size != copypos)
467 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
468 } else if (!feof(orig))
469 err = copy(tmp, orig, copypos, -1);
471 done:
472 if (orig != NULL)
473 fclose(orig);
474 return err;
477 static const struct got_error *
478 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
479 struct got_worktree *worktree)
481 const struct got_error *err;
482 struct got_pathlist_entry *pe;
484 err = got_worktree_resolve_path(path, worktree, p);
485 if (err == NULL)
486 err = got_pathlist_insert(&pe, head, *path, NULL);
487 return err;
490 static const struct got_error *
491 can_rm(void *arg, unsigned char status, unsigned char staged_status,
492 const char *path, struct got_object_id *blob_id,
493 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
494 int dirfd, const char *de_name)
496 if (status == GOT_STATUS_NONEXISTENT)
497 return got_error_set_errno(ENOENT, path);
498 if (status != GOT_STATUS_NO_CHANGE &&
499 status != GOT_STATUS_ADD &&
500 status != GOT_STATUS_MODIFY &&
501 status != GOT_STATUS_MODE_CHANGE)
502 return got_error_path(path, GOT_ERR_FILE_STATUS);
503 if (staged_status == GOT_STATUS_DELETE)
504 return got_error_path(path, GOT_ERR_FILE_STATUS);
505 return NULL;
508 static const struct got_error *
509 can_add(void *arg, unsigned char status, unsigned char staged_status,
510 const char *path, struct got_object_id *blob_id,
511 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
512 int dirfd, const char *de_name)
514 if (status != GOT_STATUS_NONEXISTENT)
515 return got_error_path(path, GOT_ERR_FILE_STATUS);
516 return NULL;
519 static const struct got_error *
520 can_edit(void *arg, unsigned char status, unsigned char staged_status,
521 const char *path, struct got_object_id *blob_id,
522 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
523 int dirfd, const char *de_name)
525 if (status == GOT_STATUS_NONEXISTENT)
526 return got_error_set_errno(ENOENT, path);
527 if (status != GOT_STATUS_NO_CHANGE &&
528 status != GOT_STATUS_ADD &&
529 status != GOT_STATUS_MODIFY)
530 return got_error_path(path, GOT_ERR_FILE_STATUS);
531 if (staged_status == GOT_STATUS_DELETE)
532 return got_error_path(path, GOT_ERR_FILE_STATUS);
533 return NULL;
536 static const struct got_error *
537 check_file_status(struct got_patch *p, int file_renamed,
538 struct got_worktree *worktree, struct got_repository *repo,
539 struct got_pathlist_head *old, struct got_pathlist_head *new,
540 got_cancel_cb cancel_cb, void *cancel_arg)
542 static const struct got_error *err;
544 if (p->old != NULL && p->new == NULL)
545 return got_worktree_status(worktree, old, repo, 0,
546 can_rm, NULL, cancel_cb, cancel_arg);
547 else if (file_renamed) {
548 err = got_worktree_status(worktree, old, repo, 0,
549 can_rm, NULL, cancel_cb, cancel_arg);
550 if (err)
551 return err;
552 return got_worktree_status(worktree, new, repo, 0,
553 can_add, NULL, cancel_cb, cancel_arg);
554 } else if (p->old == NULL)
555 return got_worktree_status(worktree, new, repo, 0,
556 can_add, NULL, cancel_cb, cancel_arg);
557 else
558 return got_worktree_status(worktree, new, repo, 0,
559 can_edit, NULL, cancel_cb, cancel_arg);
562 static const struct got_error *
563 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
564 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
565 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
566 void *cancel_arg)
568 const struct got_error *err = NULL;
569 struct got_pathlist_head oldpaths, newpaths;
570 int file_renamed = 0;
571 char *oldpath = NULL, *newpath = NULL;
572 char *tmppath = NULL, *template = NULL;
573 FILE *tmp = NULL;
575 TAILQ_INIT(&oldpaths);
576 TAILQ_INIT(&newpaths);
578 err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
579 &oldpaths, worktree);
580 if (err)
581 goto done;
583 err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
584 &newpaths, worktree);
585 if (err)
586 goto done;
588 if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
589 file_renamed = 1;
591 err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
592 &newpaths, cancel_cb, cancel_arg);
593 if (err)
594 goto done;
596 if (asprintf(&template, "%s/got-patch",
597 got_worktree_get_root_path(worktree)) == -1) {
598 err = got_error_from_errno(template);
599 goto done;
602 err = got_opentemp_named(&tmppath, &tmp, template);
603 if (err)
604 goto done;
605 err = patch_file(p, oldpath, tmp);
606 if (err)
607 goto done;
609 if (p->old != NULL && p->new == NULL) {
610 err = got_worktree_schedule_delete(worktree, &oldpaths,
611 0, NULL, delete_cb, delete_arg, repo, 0, 0);
612 goto done;
615 if (rename(tmppath, newpath) == -1) {
616 err = got_error_from_errno3("rename", tmppath, newpath);
617 goto done;
620 if (file_renamed) {
621 err = got_worktree_schedule_delete(worktree, &oldpaths,
622 0, NULL, delete_cb, delete_arg, repo, 0, 0);
623 if (err == NULL)
624 err = got_worktree_schedule_add(worktree, &newpaths,
625 add_cb, add_arg, repo, 1);
626 } else if (p->old == NULL)
627 err = got_worktree_schedule_add(worktree, &newpaths,
628 add_cb, add_arg, repo, 1);
629 else
630 printf("M %s\n", oldpath); /* XXX */
632 done:
633 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
634 unlink(newpath);
635 free(template);
636 if (tmppath != NULL)
637 unlink(tmppath);
638 free(tmppath);
639 got_pathlist_free(&oldpaths);
640 got_pathlist_free(&newpaths);
641 free(oldpath);
642 free(newpath);
643 return err;
646 const struct got_error *
647 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
648 got_worktree_delete_cb delete_cb, void *delete_arg,
649 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
650 void *cancel_arg)
652 const struct got_error *err = NULL;
653 struct imsgbuf *ibuf;
654 int imsg_fds[2] = {-1, -1};
655 int done = 0;
656 pid_t pid;
658 ibuf = calloc(1, sizeof(*ibuf));
659 if (ibuf == NULL) {
660 err = got_error_from_errno("calloc");
661 goto done;
664 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
665 err = got_error_from_errno("socketpair");
666 goto done;
669 pid = fork();
670 if (pid == -1) {
671 err = got_error_from_errno("fork");
672 goto done;
673 } else if (pid == 0) {
674 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
675 NULL);
676 /* not reached */
679 if (close(imsg_fds[1]) == -1) {
680 err = got_error_from_errno("close");
681 goto done;
683 imsg_fds[1] = -1;
684 imsg_init(ibuf, imsg_fds[0]);
686 err = send_patch(ibuf, fd);
687 fd = -1;
688 if (err)
689 goto done;
691 while (!done && err == NULL) {
692 struct got_patch p;
694 err = recv_patch(ibuf, &done, &p);
695 if (err || done)
696 break;
698 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
699 add_cb, add_arg, cancel_cb, cancel_arg);
700 patch_free(&p);
701 if (err)
702 break;
705 done:
706 if (fd != -1 && close(fd) == -1 && err == NULL)
707 err = got_error_from_errno("close");
708 if (ibuf != NULL)
709 imsg_clear(ibuf);
710 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
711 err = got_error_from_errno("close");
712 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
713 err = got_error_from_errno("close");
714 return err;