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/uio.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <imsg.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_reference.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_patch.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
53 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 struct got_patch_hunk {
56 STAILQ_ENTRY(got_patch_hunk) entries;
57 long old_from;
58 long old_lines;
59 long new_from;
60 long new_lines;
61 size_t len;
62 size_t cap;
63 char **lines;
64 };
66 struct got_patch {
67 char *old;
68 char *new;
69 STAILQ_HEAD(, got_patch_hunk) head;
70 };
72 static const struct got_error *
73 send_patch(struct imsgbuf *ibuf, int fd)
74 {
75 const struct got_error *err = NULL;
77 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
78 NULL, 0) == -1) {
79 err = got_error_from_errno(
80 "imsg_compose GOT_IMSG_PATCH_FILE");
81 close(fd);
82 return err;
83 }
85 if (imsg_flush(ibuf) == -1) {
86 err = got_error_from_errno("imsg_flush");
87 imsg_clear(ibuf);
88 }
90 return err;
91 }
93 static void
94 patch_free(struct got_patch *p)
95 {
96 struct got_patch_hunk *h;
97 size_t i;
99 while (!STAILQ_EMPTY(&p->head)) {
100 h = STAILQ_FIRST(&p->head);
101 STAILQ_REMOVE_HEAD(&p->head, entries);
103 for (i = 0; i < h->len; ++i)
104 free(h->lines[i]);
105 free(h->lines);
106 free(h);
109 free(p->new);
110 free(p->old);
113 static const struct got_error *
114 pushline(struct got_patch_hunk *h, const char *line)
116 void *t;
117 size_t newcap;
119 if (h->len == h->cap) {
120 if ((newcap = h->cap * 1.5) == 0)
121 newcap = 16;
122 t = recallocarray(h->lines, h->cap, newcap,
123 sizeof(h->lines[0]));
124 if (t == NULL)
125 return got_error_from_errno("recallocarray");
126 h->lines = t;
127 h->cap = newcap;
130 if ((t = strdup(line)) == NULL)
131 return got_error_from_errno("strdup");
133 h->lines[h->len++] = t;
134 return NULL;
137 static const struct got_error *
138 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
140 const struct got_error *err = NULL;
141 struct imsg imsg;
142 struct got_imsg_patch_hunk hdr;
143 struct got_imsg_patch patch;
144 struct got_patch_hunk *h = NULL;
145 size_t datalen;
147 memset(p, 0, sizeof(*p));
148 STAILQ_INIT(&p->head);
150 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
151 if (err)
152 return err;
153 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
154 *done = 1;
155 goto done;
157 if (imsg.hdr.type != GOT_IMSG_PATCH) {
158 err = got_error(GOT_ERR_PRIVSEP_MSG);
159 goto done;
161 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
162 if (datalen != sizeof(patch)) {
163 err = got_error(GOT_ERR_PRIVSEP_LEN);
164 goto done;
166 memcpy(&patch, imsg.data, sizeof(patch));
167 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
168 err = got_error_from_errno("strdup");
169 goto done;
171 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
172 err = got_error_from_errno("strdup");
173 goto done;
175 if (p->old == NULL && p->new == NULL) {
176 err = got_error(GOT_ERR_PATCH_MALFORMED);
177 goto done;
180 imsg_free(&imsg);
182 for (;;) {
183 char *t;
185 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
186 if (err)
187 return err;
189 switch (imsg.hdr.type) {
190 case GOT_IMSG_PATCH_DONE:
191 goto done;
192 case GOT_IMSG_PATCH_HUNK:
193 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
194 if (datalen != sizeof(hdr)) {
195 err = got_error(GOT_ERR_PRIVSEP_LEN);
196 goto done;
198 memcpy(&hdr, imsg.data, sizeof(hdr));
199 if ((h = calloc(1, sizeof(*h))) == NULL) {
200 err = got_error_from_errno("calloc");
201 goto done;
203 h->old_from = hdr.oldfrom;
204 h->old_lines = hdr.oldlines;
205 h->new_from = hdr.newfrom;
206 h->new_lines = hdr.newlines;
207 STAILQ_INSERT_TAIL(&p->head, h, entries);
208 break;
209 case GOT_IMSG_PATCH_LINE:
210 if (h == NULL) {
211 err = got_error(GOT_ERR_PRIVSEP_MSG);
212 goto done;
214 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
215 t = imsg.data;
216 /* at least one char plus newline */
217 if (datalen < 2 || t[datalen-1] != '\0') {
218 err = got_error(GOT_ERR_PRIVSEP_MSG);
219 goto done;
221 if (*t != ' ' && *t != '-' && *t != '+') {
222 err = got_error(GOT_ERR_PRIVSEP_MSG);
223 goto done;
225 err = pushline(h, t);
226 if (err)
227 goto done;
228 break;
229 default:
230 err = got_error(GOT_ERR_PRIVSEP_MSG);
231 goto done;
234 imsg_free(&imsg);
237 done:
238 imsg_free(&imsg);
239 return err;
242 /*
243 * Copy data from orig starting at copypos until pos into tmp.
244 * If pos is -1, copy until EOF.
245 */
246 static const struct got_error *
247 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
249 char buf[BUFSIZ];
250 size_t len, r, w;
252 if (fseek(orig, copypos, SEEK_SET) == -1)
253 return got_error_from_errno("fseek");
255 while (pos == -1 || copypos < pos) {
256 len = sizeof(buf);
257 if (pos > 0)
258 len = MIN(len, (size_t)pos - copypos);
259 r = fread(buf, 1, len, orig);
260 if (r != len && ferror(orig))
261 return got_error_from_errno("fread");
262 w = fwrite(buf, 1, r, tmp);
263 if (w != r)
264 return got_error_from_errno("fwrite");
265 copypos += len;
266 if (r != len && feof(orig)) {
267 if (pos == -1)
268 return NULL;
269 return got_error(GOT_ERR_PATCH_DONT_APPLY);
272 return NULL;
275 static const struct got_error *
276 locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
278 const struct got_error *err = NULL;
279 char *line = NULL;
280 char mode = *h->lines[0];
281 size_t linesize = 0;
282 ssize_t linelen;
283 off_t match = -1;
284 long match_lineno = -1;
286 for (;;) {
287 linelen = getline(&line, &linesize, orig);
288 if (linelen == -1) {
289 if (ferror(orig))
290 err = got_error_from_errno("getline");
291 else if (match == -1)
292 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
293 break;
295 (*lineno)++;
297 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
298 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
299 (mode == '+' && *lineno == h->old_from)) {
300 match = ftello(orig);
301 if (match == -1) {
302 err = got_error_from_errno("ftello");
303 break;
305 match -= linelen;
306 match_lineno = (*lineno)-1;
309 if (*lineno >= h->old_from && match != -1)
310 break;
313 if (err == NULL) {
314 *lineno = match_lineno;
315 if (fseek(orig, match, SEEK_SET) == -1)
316 err = got_error_from_errno("fseek");
319 free(line);
320 return err;
323 static const struct got_error *
324 test_hunk(FILE *orig, struct got_patch_hunk *h)
326 const struct got_error *err = NULL;
327 char *line = NULL;
328 size_t linesize = 0, i = 0;
329 ssize_t linelen;
331 for (i = 0; i < h->len; ++i) {
332 switch (*h->lines[i]) {
333 case '+':
334 continue;
335 case ' ':
336 case '-':
337 linelen = getline(&line, &linesize, orig);
338 if (linelen == -1) {
339 if (ferror(orig))
340 err = got_error_from_errno("getline");
341 else
342 err = got_error(
343 GOT_ERR_PATCH_DONT_APPLY);
344 goto done;
346 if (strcmp(h->lines[i]+1, line)) {
347 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
348 goto done;
350 break;
354 done:
355 free(line);
356 return err;
359 static const struct got_error *
360 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
362 size_t i = 0;
364 for (i = 0; i < h->len; ++i) {
365 switch (*h->lines[i]) {
366 case ' ':
367 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
368 return got_error_from_errno("fprintf");
369 /* fallthrough */
370 case '-':
371 (*lineno)++;
372 break;
373 case '+':
374 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
375 return got_error_from_errno("fprintf");
376 break;
379 return NULL;
382 static const struct got_error *
383 patch_file(struct got_patch *p, const char *path, FILE *tmp)
385 const struct got_error *err = NULL;
386 struct got_patch_hunk *h;
387 size_t i;
388 long lineno = 0;
389 FILE *orig;
390 off_t copypos, pos;
391 char *line = NULL;
392 size_t linesize = 0;
393 ssize_t linelen;
395 if (p->old == NULL) { /* create */
396 h = STAILQ_FIRST(&p->head);
397 if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
398 return got_error(GOT_ERR_PATCH_MALFORMED);
399 for (i = 0; i < h->len; ++i) {
400 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
401 return got_error_from_errno("fprintf");
403 return err;
406 if ((orig = fopen(path, "r")) == NULL) {
407 err = got_error_from_errno2("fopen", path);
408 goto done;
411 copypos = 0;
412 STAILQ_FOREACH(h, &p->head, entries) {
413 if (h->lines == NULL)
414 break;
416 tryagain:
417 err = locate_hunk(orig, h, &lineno);
418 if (err != NULL)
419 goto done;
420 if ((pos = ftello(orig)) == -1) {
421 err = got_error_from_errno("ftello");
422 goto done;
424 err = copy(tmp, orig, copypos, pos);
425 if (err != NULL)
426 goto done;
427 copypos = pos;
429 err = test_hunk(orig, h);
430 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
431 /*
432 * try to apply the hunk again starting the search
433 * after the previous partial match.
434 */
435 if (fseek(orig, pos, SEEK_SET) == -1) {
436 err = got_error_from_errno("fseek");
437 goto done;
439 linelen = getline(&line, &linesize, orig);
440 if (linelen == -1) {
441 err = got_error_from_errno("getline");
442 goto done;
444 lineno++;
445 goto tryagain;
447 if (err != NULL)
448 goto done;
450 err = apply_hunk(tmp, h, &lineno);
451 if (err != NULL)
452 goto done;
454 copypos = ftello(orig);
455 if (copypos == -1) {
456 err = got_error_from_errno("ftello");
457 goto done;
461 if (!feof(orig))
462 err = copy(tmp, orig, copypos, -1);
464 done:
465 if (orig != NULL)
466 fclose(orig);
467 return err;
470 static const struct got_error *
471 build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
472 struct got_worktree *worktree)
474 const struct got_error *err;
475 struct got_pathlist_entry *pe;
477 err = got_worktree_resolve_path(path, worktree, p);
478 if (err == NULL)
479 err = got_pathlist_insert(&pe, head, *path, NULL);
480 return err;
483 static const struct got_error *
484 can_rm(void *arg, unsigned char status, unsigned char staged_status,
485 const char *path, struct got_object_id *blob_id,
486 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
487 int dirfd, const char *de_name)
489 if (status == GOT_STATUS_NONEXISTENT)
490 return got_error_set_errno(ENOENT, path);
491 if (status != GOT_STATUS_NO_CHANGE &&
492 status != GOT_STATUS_ADD &&
493 status != GOT_STATUS_MODIFY &&
494 status != GOT_STATUS_MODE_CHANGE)
495 return got_error_path(path, GOT_ERR_FILE_STATUS);
496 if (staged_status == GOT_STATUS_DELETE)
497 return got_error_path(path, GOT_ERR_FILE_STATUS);
498 return NULL;
501 static const struct got_error *
502 can_add(void *arg, unsigned char status, unsigned char staged_status,
503 const char *path, struct got_object_id *blob_id,
504 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
505 int dirfd, const char *de_name)
507 if (status != GOT_STATUS_NONEXISTENT)
508 return got_error_path(path, GOT_ERR_FILE_STATUS);
509 return NULL;
512 static const struct got_error *
513 can_edit(void *arg, unsigned char status, unsigned char staged_status,
514 const char *path, struct got_object_id *blob_id,
515 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
516 int dirfd, const char *de_name)
518 if (status == GOT_STATUS_NONEXISTENT)
519 return got_error_set_errno(ENOENT, path);
520 if (status != GOT_STATUS_NO_CHANGE &&
521 status != GOT_STATUS_ADD &&
522 status != GOT_STATUS_MODIFY)
523 return got_error_path(path, GOT_ERR_FILE_STATUS);
524 if (staged_status == GOT_STATUS_DELETE)
525 return got_error_path(path, GOT_ERR_FILE_STATUS);
526 return NULL;
529 static const struct got_error *
530 check_file_status(struct got_patch *p, int file_renamed,
531 struct got_worktree *worktree, struct got_repository *repo,
532 struct got_pathlist_head *old, struct got_pathlist_head *new,
533 got_cancel_cb cancel_cb, void *cancel_arg)
535 static const struct got_error *err;
537 if (p->old != NULL && p->new == NULL)
538 return got_worktree_status(worktree, old, repo, 0,
539 can_rm, NULL, cancel_cb, cancel_arg);
540 else if (file_renamed) {
541 err = got_worktree_status(worktree, old, repo, 0,
542 can_rm, NULL, cancel_cb, cancel_arg);
543 if (err)
544 return err;
545 return got_worktree_status(worktree, new, repo, 0,
546 can_add, NULL, cancel_cb, cancel_arg);
547 } else if (p->old == NULL)
548 return got_worktree_status(worktree, new, repo, 0,
549 can_add, NULL, cancel_cb, cancel_arg);
550 else
551 return got_worktree_status(worktree, new, repo, 0,
552 can_edit, NULL, cancel_cb, cancel_arg);
555 static const struct got_error *
556 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
557 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
558 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
559 void *cancel_arg)
561 const struct got_error *err = NULL;
562 struct got_pathlist_head oldpaths, newpaths;
563 int file_renamed = 0;
564 char *oldpath = NULL, *newpath = NULL;
565 char *tmppath = NULL, *template = NULL;
566 FILE *tmp = NULL;
568 TAILQ_INIT(&oldpaths);
569 TAILQ_INIT(&newpaths);
571 err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
572 &oldpaths, worktree);
573 if (err)
574 goto done;
576 err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
577 &newpaths, worktree);
578 if (err)
579 goto done;
581 if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
582 file_renamed = 1;
584 err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
585 &newpaths, cancel_cb, cancel_arg);
586 if (err)
587 goto done;
589 if (p->old != NULL && p->new == NULL) {
590 /*
591 * special case: delete a file. don't try to match
592 * the lines but just schedule the removal.
593 */
594 err = got_worktree_schedule_delete(worktree, &oldpaths,
595 0, NULL, delete_cb, delete_arg, repo, 0, 0);
596 goto done;
599 if (asprintf(&template, "%s/got-patch",
600 got_worktree_get_root_path(worktree)) == -1) {
601 err = got_error_from_errno(template);
602 goto done;
605 err = got_opentemp_named(&tmppath, &tmp, template);
606 if (err)
607 goto done;
608 err = patch_file(p, oldpath, tmp);
609 if (err)
610 goto done;
612 if (rename(tmppath, newpath) == -1) {
613 err = got_error_from_errno3("rename", tmppath, newpath);
614 goto done;
617 if (file_renamed) {
618 err = got_worktree_schedule_delete(worktree, &oldpaths,
619 0, NULL, delete_cb, delete_arg, repo, 0, 0);
620 if (err == NULL)
621 err = got_worktree_schedule_add(worktree, &newpaths,
622 add_cb, add_arg, repo, 1);
623 } else if (p->old == NULL)
624 err = got_worktree_schedule_add(worktree, &newpaths,
625 add_cb, add_arg, repo, 1);
626 else
627 printf("M %s\n", oldpath); /* XXX */
629 done:
630 if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
631 unlink(newpath);
632 free(template);
633 if (tmppath != NULL)
634 unlink(tmppath);
635 free(tmppath);
636 got_pathlist_free(&oldpaths);
637 got_pathlist_free(&newpaths);
638 free(oldpath);
639 free(newpath);
640 return err;
643 const struct got_error *
644 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
645 got_worktree_delete_cb delete_cb, void *delete_arg,
646 got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
647 void *cancel_arg)
649 const struct got_error *err = NULL;
650 struct imsgbuf *ibuf;
651 int imsg_fds[2] = {-1, -1};
652 int done = 0;
653 pid_t pid;
655 ibuf = calloc(1, sizeof(*ibuf));
656 if (ibuf == NULL) {
657 err = got_error_from_errno("calloc");
658 goto done;
661 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
662 err = got_error_from_errno("socketpair");
663 goto done;
666 pid = fork();
667 if (pid == -1) {
668 err = got_error_from_errno("fork");
669 goto done;
670 } else if (pid == 0) {
671 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
672 NULL);
673 /* not reached */
676 if (close(imsg_fds[1]) == -1) {
677 err = got_error_from_errno("close");
678 goto done;
680 imsg_fds[1] = -1;
681 imsg_init(ibuf, imsg_fds[0]);
683 err = send_patch(ibuf, fd);
684 fd = -1;
685 if (err)
686 goto done;
688 while (!done && err == NULL) {
689 struct got_patch p;
691 err = recv_patch(ibuf, &done, &p);
692 if (err || done)
693 break;
695 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
696 add_cb, add_arg, cancel_cb, cancel_arg);
697 patch_free(&p);
698 if (err)
699 break;
702 done:
703 if (fd != -1 && close(fd) == -1 && err == NULL)
704 err = got_error_from_errno("close");
705 if (ibuf != NULL)
706 imsg_clear(ibuf);
707 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
708 err = got_error_from_errno("close");
709 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
710 err = got_error_from_errno("close");
711 return err;