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 int nop;
70 char *old;
71 char *new;
72 STAILQ_HEAD(, got_patch_hunk) head;
73 };
75 static const struct got_error *
76 send_patch(struct imsgbuf *ibuf, int fd)
77 {
78 const struct got_error *err = NULL;
80 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
81 NULL, 0) == -1) {
82 err = got_error_from_errno(
83 "imsg_compose GOT_IMSG_PATCH_FILE");
84 close(fd);
85 return err;
86 }
88 if (imsg_flush(ibuf) == -1) {
89 err = got_error_from_errno("imsg_flush");
90 imsg_clear(ibuf);
91 }
93 return err;
94 }
96 static void
97 patch_free(struct got_patch *p)
98 {
99 struct got_patch_hunk *h;
100 size_t i;
102 while (!STAILQ_EMPTY(&p->head)) {
103 h = STAILQ_FIRST(&p->head);
104 STAILQ_REMOVE_HEAD(&p->head, entries);
106 for (i = 0; i < h->len; ++i)
107 free(h->lines[i]);
108 free(h->lines);
109 free(h);
112 free(p->new);
113 free(p->old);
116 static const struct got_error *
117 pushline(struct got_patch_hunk *h, const char *line)
119 void *t;
120 size_t newcap;
122 if (h->len == h->cap) {
123 if ((newcap = h->cap * 1.5) == 0)
124 newcap = 16;
125 t = recallocarray(h->lines, h->cap, newcap,
126 sizeof(h->lines[0]));
127 if (t == NULL)
128 return got_error_from_errno("recallocarray");
129 h->lines = t;
130 h->cap = newcap;
133 if ((t = strdup(line)) == NULL)
134 return got_error_from_errno("strdup");
136 h->lines[h->len++] = t;
137 return NULL;
140 static const struct got_error *
141 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
143 const struct got_error *err = NULL;
144 struct imsg imsg;
145 struct got_imsg_patch_hunk hdr;
146 struct got_imsg_patch patch;
147 struct got_patch_hunk *h = NULL;
148 size_t datalen;
150 memset(p, 0, sizeof(*p));
151 STAILQ_INIT(&p->head);
153 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
154 if (err)
155 return err;
156 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
157 *done = 1;
158 goto done;
160 if (imsg.hdr.type != GOT_IMSG_PATCH) {
161 err = got_error(GOT_ERR_PRIVSEP_MSG);
162 goto done;
164 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
165 if (datalen != sizeof(patch)) {
166 err = got_error(GOT_ERR_PRIVSEP_LEN);
167 goto done;
169 memcpy(&patch, imsg.data, sizeof(patch));
170 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
171 err = got_error_from_errno("strdup");
172 goto done;
174 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
175 err = got_error_from_errno("strdup");
176 goto done;
178 if (p->old == NULL && p->new == NULL) {
179 err = got_error(GOT_ERR_PATCH_MALFORMED);
180 goto done;
183 imsg_free(&imsg);
185 for (;;) {
186 char *t;
188 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
189 if (err)
190 return err;
192 switch (imsg.hdr.type) {
193 case GOT_IMSG_PATCH_DONE:
194 goto done;
195 case GOT_IMSG_PATCH_HUNK:
196 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
197 if (datalen != sizeof(hdr)) {
198 err = got_error(GOT_ERR_PRIVSEP_LEN);
199 goto done;
201 memcpy(&hdr, imsg.data, sizeof(hdr));
202 if ((h = calloc(1, sizeof(*h))) == NULL) {
203 err = got_error_from_errno("calloc");
204 goto done;
206 h->old_from = hdr.oldfrom;
207 h->old_lines = hdr.oldlines;
208 h->new_from = hdr.newfrom;
209 h->new_lines = hdr.newlines;
210 STAILQ_INSERT_TAIL(&p->head, h, entries);
211 break;
212 case GOT_IMSG_PATCH_LINE:
213 if (h == NULL) {
214 err = got_error(GOT_ERR_PRIVSEP_MSG);
215 goto done;
217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
218 t = imsg.data;
219 /* at least one char plus newline */
220 if (datalen < 2 || t[datalen-1] != '\0') {
221 err = got_error(GOT_ERR_PRIVSEP_MSG);
222 goto done;
224 if (*t != ' ' && *t != '-' && *t != '+') {
225 err = got_error(GOT_ERR_PRIVSEP_MSG);
226 goto done;
228 err = pushline(h, t);
229 if (err)
230 goto done;
231 break;
232 default:
233 err = got_error(GOT_ERR_PRIVSEP_MSG);
234 goto done;
237 imsg_free(&imsg);
240 done:
241 imsg_free(&imsg);
242 return err;
245 /*
246 * Copy data from orig starting at copypos until pos into tmp.
247 * If pos is -1, copy until EOF.
248 */
249 static const struct got_error *
250 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
252 char buf[BUFSIZ];
253 size_t len, r, w;
255 if (fseek(orig, copypos, SEEK_SET) == -1)
256 return got_error_from_errno("fseek");
258 while (pos == -1 || copypos < pos) {
259 len = sizeof(buf);
260 if (pos > 0)
261 len = MIN(len, (size_t)pos - copypos);
262 r = fread(buf, 1, len, orig);
263 if (r != len && ferror(orig))
264 return got_error_from_errno("fread");
265 w = fwrite(buf, 1, r, tmp);
266 if (w != r)
267 return got_error_from_errno("fwrite");
268 copypos += len;
269 if (r != len && feof(orig)) {
270 if (pos == -1)
271 return NULL;
272 return got_error(GOT_ERR_PATCH_DONT_APPLY);
275 return NULL;
278 static const struct got_error *
279 locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
281 const struct got_error *err = NULL;
282 char *line = NULL;
283 char mode = *h->lines[0];
284 size_t linesize = 0;
285 ssize_t linelen;
286 off_t match = -1;
287 long match_lineno = -1;
289 for (;;) {
290 linelen = getline(&line, &linesize, orig);
291 if (linelen == -1) {
292 if (ferror(orig))
293 err = got_error_from_errno("getline");
294 else if (match == -1)
295 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
296 break;
298 (*lineno)++;
300 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
301 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
302 (mode == '+' && *lineno == h->old_from)) {
303 match = ftello(orig);
304 if (match == -1) {
305 err = got_error_from_errno("ftello");
306 break;
308 match -= linelen;
309 match_lineno = (*lineno)-1;
312 if (*lineno >= h->old_from && match != -1)
313 break;
316 if (err == NULL) {
317 *pos = match;
318 *lineno = match_lineno;
319 if (fseek(orig, match, SEEK_SET) == -1)
320 err = got_error_from_errno("fseek");
323 free(line);
324 return err;
327 static const struct got_error *
328 test_hunk(FILE *orig, struct got_patch_hunk *h)
330 const struct got_error *err = NULL;
331 char *line = NULL;
332 size_t linesize = 0, i = 0;
333 ssize_t linelen;
335 for (i = 0; i < h->len; ++i) {
336 switch (*h->lines[i]) {
337 case '+':
338 continue;
339 case ' ':
340 case '-':
341 linelen = getline(&line, &linesize, orig);
342 if (linelen == -1) {
343 if (ferror(orig))
344 err = got_error_from_errno("getline");
345 else
346 err = got_error(
347 GOT_ERR_PATCH_DONT_APPLY);
348 goto done;
350 if (strcmp(h->lines[i]+1, line)) {
351 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
352 goto done;
354 break;
358 done:
359 free(line);
360 return err;
363 static const struct got_error *
364 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
366 size_t i = 0;
368 for (i = 0; i < h->len; ++i) {
369 switch (*h->lines[i]) {
370 case ' ':
371 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
372 return got_error_from_errno("fprintf");
373 /* fallthrough */
374 case '-':
375 (*lineno)++;
376 break;
377 case '+':
378 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
379 return got_error_from_errno("fprintf");
380 break;
383 return NULL;
386 static const struct got_error *
387 patch_file(struct got_patch *p, const char *path, FILE *tmp)
389 const struct got_error *err = NULL;
390 struct got_patch_hunk *h;
391 size_t i;
392 long lineno = 0;
393 FILE *orig;
394 off_t copypos, pos;
395 char *line = NULL;
396 size_t linesize = 0;
397 ssize_t linelen;
399 if (p->old == NULL) { /* create */
400 h = STAILQ_FIRST(&p->head);
401 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
402 return got_error(GOT_ERR_PATCH_MALFORMED);
403 if (p->nop)
404 return NULL;
405 for (i = 0; i < h->len; ++i) {
406 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
407 return got_error_from_errno("fprintf");
409 return err;
412 if ((orig = fopen(path, "r")) == NULL) {
413 err = got_error_from_errno2("fopen", path);
414 goto done;
417 copypos = 0;
418 STAILQ_FOREACH(h, &p->head, entries) {
419 if (h->lines == NULL)
420 break;
422 tryagain:
423 err = locate_hunk(orig, h, &pos, &lineno);
424 if (err != NULL)
425 goto done;
426 if (!p->nop)
427 err = copy(tmp, orig, copypos, pos);
428 if (err != NULL)
429 goto done;
430 copypos = pos;
432 err = test_hunk(orig, h);
433 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
434 /*
435 * try to apply the hunk again starting the search
436 * after the previous partial match.
437 */
438 if (fseek(orig, pos, SEEK_SET) == -1) {
439 err = got_error_from_errno("fseek");
440 goto done;
442 linelen = getline(&line, &linesize, orig);
443 if (linelen == -1) {
444 err = got_error_from_errno("getline");
445 goto done;
447 lineno++;
448 goto tryagain;
450 if (err != NULL)
451 goto done;
453 if (!p->nop)
454 err = apply_hunk(tmp, h, &lineno);
455 if (err != NULL)
456 goto done;
458 copypos = ftello(orig);
459 if (copypos == -1) {
460 err = got_error_from_errno("ftello");
461 goto done;
466 if (p->new == NULL) {
467 struct stat sb;
469 if (fstat(fileno(orig), &sb) == -1)
470 err = got_error_from_errno("fstat");
471 else if (sb.st_size != copypos)
472 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
473 } else if (!p->nop && !feof(orig))
474 err = copy(tmp, orig, copypos, -1);
476 done:
477 if (orig != NULL)
478 fclose(orig);
479 return err;
482 static const struct got_error *
483 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
484 struct got_worktree *worktree)
486 const struct got_error *err;
487 struct got_pathlist_entry *pe;
489 err = got_worktree_resolve_path(path, worktree, p);
490 if (err == NULL)
491 err = got_pathlist_insert(&pe, head, *path, NULL);
492 return err;
495 static const struct got_error *
496 can_rm(void *arg, unsigned char status, unsigned char staged_status,
497 const char *path, struct got_object_id *blob_id,
498 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
499 int dirfd, const char *de_name)
501 if (status == GOT_STATUS_NONEXISTENT)
502 return got_error_set_errno(ENOENT, path);
503 if (status != GOT_STATUS_NO_CHANGE &&
504 status != GOT_STATUS_ADD &&
505 status != GOT_STATUS_MODIFY &&
506 status != GOT_STATUS_MODE_CHANGE)
507 return got_error_path(path, GOT_ERR_FILE_STATUS);
508 if (staged_status == GOT_STATUS_DELETE)
509 return got_error_path(path, GOT_ERR_FILE_STATUS);
510 return NULL;
513 static const struct got_error *
514 can_add(void *arg, unsigned char status, unsigned char staged_status,
515 const char *path, struct got_object_id *blob_id,
516 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
517 int dirfd, const char *de_name)
519 if (status != GOT_STATUS_NONEXISTENT)
520 return got_error_path(path, GOT_ERR_FILE_STATUS);
521 return NULL;
524 static const struct got_error *
525 can_edit(void *arg, unsigned char status, unsigned char staged_status,
526 const char *path, struct got_object_id *blob_id,
527 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
528 int dirfd, const char *de_name)
530 if (status == GOT_STATUS_NONEXISTENT)
531 return got_error_set_errno(ENOENT, path);
532 if (status != GOT_STATUS_NO_CHANGE &&
533 status != GOT_STATUS_ADD &&
534 status != GOT_STATUS_MODIFY)
535 return got_error_path(path, GOT_ERR_FILE_STATUS);
536 if (staged_status == GOT_STATUS_DELETE)
537 return got_error_path(path, GOT_ERR_FILE_STATUS);
538 return NULL;
541 static const struct got_error *
542 check_file_status(struct got_patch *p, int file_renamed,
543 struct got_worktree *worktree, struct got_repository *repo,
544 struct got_pathlist_head *old, struct got_pathlist_head *new,
545 got_cancel_cb cancel_cb, void *cancel_arg)
547 static const struct got_error *err;
549 if (p->old != NULL && p->new == NULL)
550 return got_worktree_status(worktree, old, repo, 0,
551 can_rm, NULL, cancel_cb, cancel_arg);
552 else if (file_renamed) {
553 err = got_worktree_status(worktree, old, repo, 0,
554 can_rm, NULL, cancel_cb, cancel_arg);
555 if (err)
556 return err;
557 return got_worktree_status(worktree, new, repo, 0,
558 can_add, NULL, cancel_cb, cancel_arg);
559 } else if (p->old == NULL)
560 return got_worktree_status(worktree, new, repo, 0,
561 can_add, NULL, cancel_cb, cancel_arg);
562 else
563 return got_worktree_status(worktree, new, repo, 0,
564 can_edit, NULL, cancel_cb, cancel_arg);
567 static const struct got_error *
568 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
569 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
570 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
571 void *cancel_arg)
573 const struct got_error *err = NULL;
574 struct got_pathlist_head oldpaths, newpaths;
575 int file_renamed = 0;
576 char *oldpath = NULL, *newpath = NULL;
577 char *tmppath = NULL, *template = NULL;
578 FILE *tmp = NULL;
580 TAILQ_INIT(&oldpaths);
581 TAILQ_INIT(&newpaths);
583 err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
584 &oldpaths, worktree);
585 if (err)
586 goto done;
588 err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
589 &newpaths, worktree);
590 if (err)
591 goto done;
593 if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
594 file_renamed = 1;
596 err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
597 &newpaths, cancel_cb, cancel_arg);
598 if (err)
599 goto done;
601 if (asprintf(&template, "%s/got-patch",
602 got_worktree_get_root_path(worktree)) == -1) {
603 err = got_error_from_errno(template);
604 goto done;
607 if (!p->nop)
608 err = got_opentemp_named(&tmppath, &tmp, template);
609 if (err)
610 goto done;
611 err = patch_file(p, oldpath, tmp);
612 if (err)
613 goto done;
615 if (p->nop)
616 goto done;
618 if (p->old != NULL && p->new == NULL) {
619 err = got_worktree_schedule_delete(worktree, &oldpaths,
620 0, NULL, delete_cb, delete_arg, repo, 0, 0);
621 goto done;
624 if (rename(tmppath, newpath) == -1) {
625 err = got_error_from_errno3("rename", tmppath, newpath);
626 goto done;
629 if (file_renamed) {
630 err = got_worktree_schedule_delete(worktree, &oldpaths,
631 0, NULL, delete_cb, delete_arg, repo, 0, 0);
632 if (err == NULL)
633 err = got_worktree_schedule_add(worktree, &newpaths,
634 add_cb, add_arg, repo, 1);
635 } else if (p->old == NULL)
636 err = got_worktree_schedule_add(worktree, &newpaths,
637 add_cb, add_arg, repo, 1);
638 else
639 printf("M %s\n", oldpath); /* XXX */
641 done:
642 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
643 unlink(newpath);
644 free(template);
645 if (tmppath != NULL)
646 unlink(tmppath);
647 free(tmppath);
648 got_pathlist_free(&oldpaths);
649 got_pathlist_free(&newpaths);
650 free(oldpath);
651 free(newpath);
652 return err;
655 const struct got_error *
656 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
657 int nop, got_worktree_delete_cb delete_cb, void *delete_arg,
658 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
659 void *cancel_arg)
661 const struct got_error *err = NULL;
662 struct imsgbuf *ibuf;
663 int imsg_fds[2] = {-1, -1};
664 int done = 0;
665 pid_t pid;
667 ibuf = calloc(1, sizeof(*ibuf));
668 if (ibuf == NULL) {
669 err = got_error_from_errno("calloc");
670 goto done;
673 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
674 err = got_error_from_errno("socketpair");
675 goto done;
678 pid = fork();
679 if (pid == -1) {
680 err = got_error_from_errno("fork");
681 goto done;
682 } else if (pid == 0) {
683 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
684 NULL);
685 /* not reached */
688 if (close(imsg_fds[1]) == -1) {
689 err = got_error_from_errno("close");
690 goto done;
692 imsg_fds[1] = -1;
693 imsg_init(ibuf, imsg_fds[0]);
695 err = send_patch(ibuf, fd);
696 fd = -1;
697 if (err)
698 goto done;
700 while (!done && err == NULL) {
701 struct got_patch p;
703 err = recv_patch(ibuf, &done, &p);
704 if (err || done)
705 break;
707 p.nop = nop;
708 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
709 add_cb, add_arg, cancel_cb, cancel_arg);
710 patch_free(&p);
711 if (err)
712 break;
715 done:
716 if (fd != -1 && close(fd) == -1 && err == NULL)
717 err = got_error_from_errno("close");
718 if (ibuf != NULL)
719 imsg_clear(ibuf);
720 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
721 err = got_error_from_errno("close");
722 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
723 err = got_error_from_errno("close");
724 return err;