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 <limits.h>
32 #include <sha1.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;
176 imsg_free(&imsg);
178 for (;;) {
179 char *t;
181 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
182 if (err)
183 return err;
185 switch (imsg.hdr.type) {
186 case GOT_IMSG_PATCH_DONE:
187 goto done;
188 case GOT_IMSG_PATCH_HUNK:
189 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
190 if (datalen != sizeof(hdr)) {
191 err = got_error(GOT_ERR_PRIVSEP_LEN);
192 goto done;
194 memcpy(&hdr, imsg.data, sizeof(hdr));
195 if ((h = calloc(1, sizeof(*h))) == NULL) {
196 err = got_error_from_errno("calloc");
197 goto done;
199 h->old_from = hdr.oldfrom;
200 h->old_lines = hdr.oldlines;
201 h->new_from = hdr.newfrom;
202 h->new_lines = hdr.newlines;
203 STAILQ_INSERT_TAIL(&p->head, h, entries);
204 break;
205 case GOT_IMSG_PATCH_LINE:
206 if (h == NULL) {
207 err = got_error(GOT_ERR_PRIVSEP_MSG);
208 goto done;
210 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
211 t = imsg.data;
212 /* at least one char plus newline */
213 if (datalen < 2 || t[datalen-1] != '\0') {
214 err = got_error(GOT_ERR_PRIVSEP_MSG);
215 goto done;
217 if (*t != ' ' && *t != '-' && *t != '+') {
218 err = got_error(GOT_ERR_PRIVSEP_MSG);
219 goto done;
221 err = pushline(h, t);
222 if (err)
223 goto done;
224 break;
225 default:
226 err = got_error(GOT_ERR_PRIVSEP_MSG);
227 goto done;
230 imsg_free(&imsg);
233 done:
234 imsg_free(&imsg);
235 return err;
238 /*
239 * Copy data from orig starting at copypos until pos into tmp.
240 * If pos is -1, copy until EOF.
241 */
242 static const struct got_error *
243 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
245 char buf[BUFSIZ];
246 size_t len, r, w;
248 if (fseek(orig, copypos, SEEK_SET) == -1)
249 return got_error_from_errno("fseek");
251 while (pos == -1 || copypos < pos) {
252 len = sizeof(buf);
253 if (pos > 0)
254 len = MIN(len, (size_t)pos - copypos);
255 r = fread(buf, 1, len, orig);
256 if (r != len && ferror(orig))
257 return got_error_from_errno("fread");
258 w = fwrite(buf, 1, r, tmp);
259 if (w != r)
260 return got_error_from_errno("fwrite");
261 copypos += len;
262 if (r != len && feof(orig)) {
263 if (pos == -1)
264 return NULL;
265 return got_error(GOT_ERR_PATCH_DONT_APPLY);
268 return NULL;
271 static const struct got_error *
272 locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
274 const struct got_error *err = NULL;
275 char *line = NULL;
276 char mode = *h->lines[0];
277 size_t linesize = 0;
278 ssize_t linelen;
279 off_t match = -1;
280 long match_lineno = -1;
282 for (;;) {
283 linelen = getline(&line, &linesize, orig);
284 if (linelen == -1) {
285 if (ferror(orig))
286 err = got_error_from_errno("getline");
287 else if (match == -1)
288 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
289 break;
291 (*lineno)++;
293 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
294 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
295 (mode == '+' && *lineno == h->old_from)) {
296 match = ftello(orig);
297 if (match == -1) {
298 err = got_error_from_errno("ftello");
299 break;
301 match -= linelen;
302 match_lineno = (*lineno)-1;
305 if (*lineno >= h->old_from && match != -1)
306 break;
309 if (err == NULL) {
310 *lineno = match_lineno;
311 if (fseek(orig, match, SEEK_SET) == -1)
312 err = got_error_from_errno("fseek");
315 free(line);
316 return err;
319 static const struct got_error *
320 test_hunk(FILE *orig, struct got_patch_hunk *h)
322 const struct got_error *err = NULL;
323 char *line = NULL;
324 size_t linesize = 0, i = 0;
325 ssize_t linelen;
327 for (i = 0; i < h->len; ++i) {
328 switch (*h->lines[i]) {
329 case '+':
330 continue;
331 case ' ':
332 case '-':
333 linelen = getline(&line, &linesize, orig);
334 if (linelen == -1) {
335 if (ferror(orig))
336 err = got_error_from_errno("getline");
337 else
338 err = got_error(
339 GOT_ERR_PATCH_DONT_APPLY);
340 goto done;
342 if (strcmp(h->lines[i]+1, line)) {
343 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
344 goto done;
346 break;
350 done:
351 free(line);
352 return err;
355 static const struct got_error *
356 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
358 size_t i = 0;
360 for (i = 0; i < h->len; ++i) {
361 switch (*h->lines[i]) {
362 case ' ':
363 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
364 return got_error_from_errno("fprintf");
365 /* fallthrough */
366 case '-':
367 (*lineno)++;
368 break;
369 case '+':
370 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
371 return got_error_from_errno("fprintf");
372 break;
375 return NULL;
378 static const struct got_error *
379 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
380 struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
381 got_worktree_checkout_cb add_cb, void *add_arg)
383 const struct got_error *err = NULL;
384 struct got_pathlist_head paths;
385 struct got_pathlist_entry *pe;
386 char *path = NULL, *tmppath = NULL, *template = NULL;
387 FILE *orig = NULL, *tmp = NULL;
388 struct got_patch_hunk *h;
389 size_t i;
390 long lineno = 0;
391 off_t copypos, pos;
392 char *line = NULL;
393 size_t linesize = 0;
394 ssize_t linelen;
396 TAILQ_INIT(&paths);
398 if (p->old == NULL && p->new == NULL)
399 return got_error(GOT_ERR_PATCH_MALFORMED);
401 err = got_worktree_resolve_path(&path, worktree,
402 p->new != NULL ? p->new : p->old);
403 if (err)
404 return err;
405 err = got_pathlist_insert(&pe, &paths, path, NULL);
406 if (err)
407 goto done;
409 if (p->old != NULL && p->new == NULL) {
410 /*
411 * special case: delete a file. don't try to match
412 * the lines but just schedule the removal.
413 */
414 err = got_worktree_schedule_delete(worktree, &paths,
415 0, NULL, delete_cb, delete_arg, repo, 0, 0);
416 goto done;
417 } else if (p->old != NULL && strcmp(p->old, p->new)) {
418 err = got_error(GOT_ERR_PATCH_PATHS_DIFFER);
419 goto done;
422 if (asprintf(&template, "%s/got-patch",
423 got_worktree_get_root_path(worktree)) == -1) {
424 err = got_error_from_errno(template);
425 goto done;
428 err = got_opentemp_named(&tmppath, &tmp, template);
429 if (err)
430 goto done;
432 if (p->old == NULL) { /* create */
433 h = STAILQ_FIRST(&p->head);
434 if (h == NULL || STAILQ_NEXT(h, entries) != NULL) {
435 err = got_error(GOT_ERR_PATCH_MALFORMED);
436 goto done;
438 for (i = 0; i < h->len; ++i) {
439 if (fprintf(tmp, "%s", h->lines[i]+1) < 0) {
440 err = got_error_from_errno("fprintf");
441 goto done;
444 goto rename;
447 if ((orig = fopen(path, "r")) == NULL) {
448 err = got_error_from_errno2("fopen", path);
449 goto done;
452 copypos = 0;
453 STAILQ_FOREACH(h, &p->head, entries) {
454 tryagain:
455 err = locate_hunk(orig, h, &lineno);
456 if (err != NULL)
457 goto done;
458 if ((pos = ftello(orig)) == -1) {
459 err = got_error_from_errno("ftello");
460 goto done;
462 err = copy(tmp, orig, copypos, pos);
463 if (err != NULL)
464 goto done;
465 copypos = pos;
467 err = test_hunk(orig, h);
468 if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
469 /*
470 * try to apply the hunk again starting the search
471 * after the previous partial match.
472 */
473 if (fseek(orig, pos, SEEK_SET) == -1) {
474 err = got_error_from_errno("fseek");
475 goto done;
477 linelen = getline(&line, &linesize, orig);
478 if (linelen == -1) {
479 err = got_error_from_errno("getline");
480 goto done;
482 lineno++;
483 goto tryagain;
485 if (err != NULL)
486 goto done;
488 err = apply_hunk(tmp, h, &lineno);
489 if (err != NULL)
490 goto done;
492 copypos = ftello(orig);
493 if (copypos == -1) {
494 err = got_error_from_errno("ftello");
495 goto done;
499 if (!feof(orig)) {
500 err = copy(tmp, orig, copypos, -1);
501 if (err)
502 goto done;
505 rename:
506 if (rename(tmppath, path) == -1) {
507 err = got_error_from_errno3("rename", tmppath, path);
508 goto done;
511 if (p->old == NULL)
512 err = got_worktree_schedule_add(worktree, &paths,
513 add_cb, add_arg, repo, 1);
514 else
515 printf("M %s\n", path); /* XXX */
516 done:
517 free(template);
518 if (err != NULL && p->old == NULL && path != NULL)
519 unlink(path);
520 if (tmp != NULL)
521 fclose(tmp);
522 if (tmppath != NULL)
523 unlink(tmppath);
524 free(tmppath);
525 if (orig != NULL) {
526 if (p->old == NULL && err != NULL)
527 unlink(path);
528 fclose(orig);
530 free(path);
531 free(line);
532 got_pathlist_free(&paths);
533 return err;
536 const struct got_error *
537 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
538 got_worktree_delete_cb delete_cb, void *delete_arg,
539 got_worktree_checkout_cb add_cb, void *add_arg)
541 const struct got_error *err = NULL;
542 struct imsgbuf *ibuf;
543 int imsg_fds[2] = {-1, -1};
544 int done = 0;
545 pid_t pid;
547 ibuf = calloc(1, sizeof(*ibuf));
548 if (ibuf == NULL) {
549 err = got_error_from_errno("calloc");
550 goto done;
553 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
554 err = got_error_from_errno("socketpair");
555 goto done;
558 pid = fork();
559 if (pid == -1) {
560 err = got_error_from_errno("fork");
561 goto done;
562 } else if (pid == 0) {
563 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
564 NULL);
565 /* not reached */
568 if (close(imsg_fds[1]) == -1) {
569 err = got_error_from_errno("close");
570 goto done;
572 imsg_fds[1] = -1;
573 imsg_init(ibuf, imsg_fds[0]);
575 err = send_patch(ibuf, fd);
576 fd = -1;
577 if (err)
578 goto done;
580 while (!done && err == NULL) {
581 struct got_patch p;
583 err = recv_patch(ibuf, &done, &p);
584 if (err || done)
585 break;
587 err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
588 add_cb, add_arg);
589 patch_free(&p);
590 if (err)
591 break;
594 done:
595 if (fd != -1 && close(fd) == -1 && err == NULL)
596 err = got_error_from_errno("close");
597 if (ibuf != NULL)
598 imsg_clear(ibuf);
599 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
600 err = got_error_from_errno("close");
601 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
602 err = got_error_from_errno("close");
603 return err;