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 <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <imsg.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_reference.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_patch.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 struct got_patch_hunk {
55 STAILQ_ENTRY(got_patch_hunk) entries;
56 long old_from;
57 long old_lines;
58 long new_from;
59 long new_lines;
60 size_t len;
61 size_t cap;
62 char **lines;
63 };
65 struct got_patch {
66 char *old;
67 char *new;
68 STAILQ_HEAD(, got_patch_hunk) head;
69 };
71 static const struct got_error *
72 send_patch(struct imsgbuf *ibuf, int fd)
73 {
74 const struct got_error *err = NULL;
76 if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
77 NULL, 0) == -1) {
78 err = got_error_from_errno(
79 "imsg_compose GOT_IMSG_PATCH_FILE");
80 close(fd);
81 return err;
82 }
84 if (imsg_flush(ibuf) == -1) {
85 err = got_error_from_errno("imsg_flush");
86 imsg_clear(ibuf);
87 }
89 return err;
90 }
92 static void
93 patch_free(struct got_patch *p)
94 {
95 struct got_patch_hunk *h;
96 size_t i;
98 while (!STAILQ_EMPTY(&p->head)) {
99 h = STAILQ_FIRST(&p->head);
100 STAILQ_REMOVE_HEAD(&p->head, entries);
102 for (i = 0; i < h->len; ++i)
103 free(h->lines[i]);
104 free(h->lines);
105 free(h);
108 free(p->new);
109 free(p->old);
112 static const struct got_error *
113 pushline(struct got_patch_hunk *h, const char *line)
115 void *t;
116 size_t newcap;
118 if (h->len == h->cap) {
119 if ((newcap = h->cap * 1.5) == 0)
120 newcap = 16;
121 t = recallocarray(h->lines, h->cap, newcap,
122 sizeof(h->lines[0]));
123 if (t == NULL)
124 return got_error_from_errno("recallocarray");
125 h->lines = t;
126 h->cap = newcap;
129 if ((t = strdup(line)) == NULL)
130 return got_error_from_errno("strdup");
132 h->lines[h->len++] = t;
133 return NULL;
136 static const struct got_error *
137 recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
139 const struct got_error *err = NULL;
140 struct imsg imsg;
141 struct got_imsg_patch_hunk hdr;
142 struct got_imsg_patch patch;
143 struct got_patch_hunk *h = NULL;
144 size_t datalen;
146 memset(p, 0, sizeof(*p));
147 STAILQ_INIT(&p->head);
149 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
150 if (err)
151 return err;
152 if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
153 *done = 1;
154 goto done;
156 if (imsg.hdr.type != GOT_IMSG_PATCH) {
157 err = got_error(GOT_ERR_PRIVSEP_MSG);
158 goto done;
160 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
161 if (datalen != sizeof(patch)) {
162 err = got_error(GOT_ERR_PRIVSEP_LEN);
163 goto done;
165 memcpy(&patch, imsg.data, sizeof(patch));
166 if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
167 err = got_error_from_errno("strdup");
168 goto done;
170 if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
171 err = got_error_from_errno("strdup");
172 goto done;
175 imsg_free(&imsg);
177 for (;;) {
178 char *t;
180 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
181 if (err)
182 return err;
184 switch (imsg.hdr.type) {
185 case GOT_IMSG_PATCH_DONE:
186 goto done;
187 case GOT_IMSG_PATCH_HUNK:
188 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
189 if (datalen != sizeof(hdr)) {
190 err = got_error(GOT_ERR_PRIVSEP_LEN);
191 goto done;
193 memcpy(&hdr, imsg.data, sizeof(hdr));
194 if ((h = calloc(1, sizeof(*h))) == NULL) {
195 err = got_error_from_errno("calloc");
196 goto done;
198 h->old_from = hdr.oldfrom;
199 h->old_lines = hdr.oldlines;
200 h->new_from = hdr.newfrom;
201 h->new_lines = hdr.newlines;
202 STAILQ_INSERT_TAIL(&p->head, h, entries);
203 break;
204 case GOT_IMSG_PATCH_LINE:
205 if (h == NULL) {
206 err = got_error(GOT_ERR_PRIVSEP_MSG);
207 goto done;
209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
210 t = imsg.data;
211 /* at least one char plus newline */
212 if (datalen < 2 || t[datalen-1] != '\0') {
213 err = got_error(GOT_ERR_PRIVSEP_MSG);
214 goto done;
216 if (*t != ' ' && *t != '-' && *t != '+') {
217 err = got_error(GOT_ERR_PRIVSEP_MSG);
218 goto done;
220 err = pushline(h, t);
221 if (err)
222 goto done;
223 break;
224 default:
225 err = got_error(GOT_ERR_PRIVSEP_MSG);
226 goto done;
229 imsg_free(&imsg);
232 done:
233 imsg_free(&imsg);
234 return err;
237 /*
238 * Copy data from orig starting at copypos until pos into tmp.
239 * If pos is -1, copy until EOF.
240 */
241 static const struct got_error *
242 copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
244 char buf[BUFSIZ];
245 size_t len, r, w;
247 if (fseek(orig, copypos, SEEK_SET) == -1)
248 return got_error_from_errno("fseek");
250 while (pos == -1 || copypos < pos) {
251 len = sizeof(buf);
252 if (pos > 0)
253 len = MIN(len, (size_t)pos - copypos);
254 r = fread(buf, 1, len, orig);
255 if (r != len && ferror(orig))
256 return got_error_from_errno("fread");
257 w = fwrite(buf, 1, r, tmp);
258 if (w != r)
259 return got_error_from_errno("fwrite");
260 copypos += len;
261 if (r != len && feof(orig)) {
262 if (pos == -1)
263 return NULL;
264 return got_error(GOT_ERR_PATCH_DONT_APPLY);
267 return NULL;
270 static const struct got_error *
271 locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
273 const struct got_error *err = NULL;
274 char *line = NULL;
275 char mode = *h->lines[0];
276 size_t linesize = 0;
277 ssize_t linelen;
278 off_t match = -1;
279 long match_lineno = -1;
281 for (;;) {
282 linelen = getline(&line, &linesize, orig);
283 if (linelen == -1) {
284 if (ferror(orig))
285 err = got_error_from_errno("getline");
286 else if (match == -1)
287 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
288 break;
290 (*lineno)++;
292 if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
293 (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
294 (mode == '+' && *lineno == h->old_from)) {
295 match = ftello(orig);
296 if (match == -1) {
297 err = got_error_from_errno("ftello");
298 break;
300 match -= linelen;
301 match_lineno = (*lineno)-1;
304 if (*lineno >= h->old_from && match != -1)
305 break;
308 if (err == NULL) {
309 *lineno = match_lineno;
310 if (fseek(orig, match, SEEK_SET) == -1)
311 err = got_error_from_errno("fseek");
314 free(line);
315 return err;
318 static const struct got_error *
319 test_hunk(FILE *orig, struct got_patch_hunk *h)
321 const struct got_error *err = NULL;
322 char *line = NULL;
323 size_t linesize = 0, i = 0;
324 ssize_t linelen;
326 for (i = 0; i < h->len; ++i) {
327 switch (*h->lines[i]) {
328 case '+':
329 continue;
330 case ' ':
331 case '-':
332 linelen = getline(&line, &linesize, orig);
333 if (linelen == -1) {
334 if (ferror(orig))
335 err = got_error_from_errno("getline");
336 else
337 err = got_error(
338 GOT_ERR_PATCH_DONT_APPLY);
339 goto done;
341 if (strcmp(h->lines[i]+1, line)) {
342 err = got_error(GOT_ERR_PATCH_DONT_APPLY);
343 goto done;
345 break;
349 done:
350 free(line);
351 return err;
354 static const struct got_error *
355 apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
357 size_t i = 0;
359 for (i = 0; i < h->len; ++i) {
360 switch (*h->lines[i]) {
361 case ' ':
362 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
363 return got_error_from_errno("fprintf");
364 /* fallthrough */
365 case '-':
366 (*lineno)++;
367 break;
368 case '+':
369 if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
370 return got_error_from_errno("fprintf");
371 break;
374 return NULL;
377 static const struct got_error *
378 apply_patch(struct got_worktree *worktree, struct got_repository *repo,
379 struct got_patch *p, got_worktree_delete_cb delete_cb,
380 got_worktree_checkout_cb add_cb)
382 const struct got_error *err = NULL;
383 struct got_pathlist_head paths;
384 struct got_pathlist_entry *pe;
385 char *path = NULL, *tmppath = NULL;
386 FILE *orig = NULL, *tmp = NULL;
387 struct got_patch_hunk *h;
388 size_t i;
389 long lineno = 0;
390 off_t copypos, pos;
391 char *line = NULL;
392 size_t linesize = 0;
393 ssize_t linelen;
395 TAILQ_INIT(&paths);
397 if (p->old == NULL && p->new == NULL)
398 return got_error(GOT_ERR_PATCH_MALFORMED);
400 err = got_worktree_resolve_path(&path, worktree,
401 p->new != NULL ? p->new : p->old);
402 if (err)
403 return err;
404 err = got_pathlist_insert(&pe, &paths, path, NULL);
405 if (err)
406 goto done;
408 if (p->old != NULL && p->new == NULL) {
409 /*
410 * special case: delete a file. don't try to match
411 * the lines but just schedule the removal.
412 */
413 err = got_worktree_schedule_delete(worktree, &paths,
414 0, NULL, delete_cb, NULL, repo, 0, 0);
415 goto done;
416 } else if (p->old != NULL && strcmp(p->old, p->new)) {
417 err = got_error(GOT_ERR_PATCH_PATHS_DIFFER);
418 goto done;
421 err = got_opentemp_named(&tmppath, &tmp,
422 got_worktree_get_root_path(worktree));
423 if (err)
424 goto done;
426 if (p->old == NULL) { /* create */
427 h = STAILQ_FIRST(&p->head);
428 if (h == NULL || STAILQ_NEXT(h, entries) != NULL) {
429 err = got_error(GOT_ERR_PATCH_MALFORMED);
430 goto done;
432 for (i = 0; i < h->len; ++i) {
433 if (fprintf(tmp, "%s", h->lines[i]+1) < 0) {
434 err = got_error_from_errno("fprintf");
435 goto done;
438 goto rename;
441 if ((orig = fopen(path, "r")) == NULL) {
442 err = got_error_from_errno2("fopen", path);
443 goto done;
446 copypos = 0;
447 STAILQ_FOREACH(h, &p->head, entries) {
448 tryagain:
449 err = locate_hunk(orig, h, &lineno);
450 if (err != NULL)
451 goto done;
452 if ((pos = ftello(orig)) == -1) {
453 err = got_error_from_errno("ftello");
454 goto done;
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_PATCH_DONT_APPLY) {
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 err = apply_hunk(tmp, h, &lineno);
483 if (err != NULL)
484 goto done;
486 copypos = ftello(orig);
487 if (copypos == -1) {
488 err = got_error_from_errno("ftello");
489 goto done;
493 if (!feof(orig)) {
494 err = copy(tmp, orig, copypos, -1);
495 if (err)
496 goto done;
499 rename:
500 if (rename(tmppath, path) == -1) {
501 err = got_error_from_errno3("rename", tmppath, path);
502 goto done;
505 if (p->old == NULL)
506 err = got_worktree_schedule_add(worktree, &paths,
507 add_cb, NULL, repo, 1);
508 else
509 printf("M %s\n", path); /* XXX */
510 done:
511 if (err != NULL && p->old == NULL && path != NULL)
512 unlink(path);
513 if (tmp != NULL)
514 fclose(tmp);
515 if (tmppath != NULL)
516 unlink(tmppath);
517 free(tmppath);
518 if (orig != NULL) {
519 if (p->old == NULL && err != NULL)
520 unlink(path);
521 fclose(orig);
523 free(path);
524 free(line);
525 got_pathlist_free(&paths);
526 return err;
529 const struct got_error *
530 got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
531 got_worktree_delete_cb delete_cb, got_worktree_checkout_cb add_cb)
533 const struct got_error *err = NULL;
534 struct imsgbuf *ibuf;
535 int imsg_fds[2] = {-1, -1};
536 int done = 0;
537 pid_t pid;
539 ibuf = calloc(1, sizeof(*ibuf));
540 if (ibuf == NULL) {
541 err = got_error_from_errno("calloc");
542 goto done;
545 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
546 err = got_error_from_errno("socketpair");
547 goto done;
550 pid = fork();
551 if (pid == -1) {
552 err = got_error_from_errno("fork");
553 goto done;
554 } else if (pid == 0) {
555 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
556 NULL);
557 /* not reached */
560 if (close(imsg_fds[1]) == -1) {
561 err = got_error_from_errno("close");
562 goto done;
564 imsg_fds[1] = -1;
565 imsg_init(ibuf, imsg_fds[0]);
567 err = send_patch(ibuf, fd);
568 fd = -1;
569 if (err)
570 goto done;
572 while (!done && err == NULL) {
573 struct got_patch p;
575 err = recv_patch(ibuf, &done, &p);
576 if (err || done)
577 break;
579 err = apply_patch(worktree, repo, &p, delete_cb, add_cb);
580 patch_free(&p);
581 if (err)
582 break;
585 done:
586 if (fd != -1 && close(fd) == -1 && err == NULL)
587 err = got_error_from_errno("close");
588 if (ibuf != NULL)
589 imsg_clear(ibuf);
590 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
591 err = got_error_from_errno("close");
592 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
593 err = got_error_from_errno("close");
594 return err;