2 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 e9ce266e 2022-03-07 op * Permission to use, copy, modify, and distribute this software for any
5 e9ce266e 2022-03-07 op * purpose with or without fee is hereby granted, provided that the above
6 e9ce266e 2022-03-07 op * copyright notice and this permission notice appear in all copies.
8 e9ce266e 2022-03-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e9ce266e 2022-03-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e9ce266e 2022-03-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e9ce266e 2022-03-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e9ce266e 2022-03-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e9ce266e 2022-03-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e9ce266e 2022-03-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 e9ce266e 2022-03-07 op * Apply patches.
18 e9ce266e 2022-03-07 op * Things that we may want to support:
19 e9ce266e 2022-03-07 op * + support indented patches?
20 e9ce266e 2022-03-07 op * + support other kinds of patches?
23 e9ce266e 2022-03-07 op #include <sys/types.h>
24 e9ce266e 2022-03-07 op #include <sys/queue.h>
25 e9ce266e 2022-03-07 op #include <sys/socket.h>
26 5b67f96e 2022-03-13 op #include <sys/stat.h>
27 e9ce266e 2022-03-07 op #include <sys/uio.h>
29 dbda770b 2022-03-13 op #include <errno.h>
30 e9ce266e 2022-03-07 op #include <limits.h>
31 e9ce266e 2022-03-07 op #include <sha1.h>
32 e9ce266e 2022-03-07 op #include <stdint.h>
33 e9ce266e 2022-03-07 op #include <stdio.h>
34 e9ce266e 2022-03-07 op #include <stdlib.h>
35 e9ce266e 2022-03-07 op #include <string.h>
36 e9ce266e 2022-03-07 op #include <unistd.h>
37 e9ce266e 2022-03-07 op #include <imsg.h>
39 e9ce266e 2022-03-07 op #include "got_error.h"
40 e9ce266e 2022-03-07 op #include "got_object.h"
41 e9ce266e 2022-03-07 op #include "got_path.h"
42 e9ce266e 2022-03-07 op #include "got_reference.h"
43 e9ce266e 2022-03-07 op #include "got_cancel.h"
44 e9ce266e 2022-03-07 op #include "got_worktree.h"
45 e9ce266e 2022-03-07 op #include "got_opentemp.h"
46 e9ce266e 2022-03-07 op #include "got_patch.h"
48 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
49 e9ce266e 2022-03-07 op #include "got_lib_object.h"
50 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
52 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 e9ce266e 2022-03-07 op struct got_patch_hunk {
55 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
56 60aa1fa0 2022-03-17 op const struct got_error *err;
59 e9ce266e 2022-03-07 op long old_from;
60 e9ce266e 2022-03-07 op long old_lines;
61 e9ce266e 2022-03-07 op long new_from;
62 e9ce266e 2022-03-07 op long new_lines;
68 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
69 e9ce266e 2022-03-07 op struct got_patch {
72 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
75 b22138f5 2022-03-16 op struct patch_args {
76 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
77 b22138f5 2022-03-16 op void *progress_arg;
78 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
81 e9ce266e 2022-03-07 op static const struct got_error *
82 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
84 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
86 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
87 e9ce266e 2022-03-07 op NULL, 0) == -1) {
88 e9ce266e 2022-03-07 op err = got_error_from_errno(
89 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
94 e9ce266e 2022-03-07 op if (imsg_flush(ibuf) == -1) {
95 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_flush");
96 e9ce266e 2022-03-07 op imsg_clear(ibuf);
103 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
105 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
108 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
109 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
110 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
112 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
113 e9ce266e 2022-03-07 op free(h->lines[i]);
114 e9ce266e 2022-03-07 op free(h->lines);
118 e9ce266e 2022-03-07 op free(p->new);
119 e9ce266e 2022-03-07 op free(p->old);
122 e9ce266e 2022-03-07 op static const struct got_error *
123 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
126 e9ce266e 2022-03-07 op size_t newcap;
128 e9ce266e 2022-03-07 op if (h->len == h->cap) {
129 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
131 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
132 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
133 e9ce266e 2022-03-07 op if (t == NULL)
134 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
135 e9ce266e 2022-03-07 op h->lines = t;
136 e9ce266e 2022-03-07 op h->cap = newcap;
139 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
140 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
142 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
146 e9ce266e 2022-03-07 op static const struct got_error *
147 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
149 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
150 e9ce266e 2022-03-07 op struct imsg imsg;
151 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
152 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
153 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
154 e9ce266e 2022-03-07 op size_t datalen;
156 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
157 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
159 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
162 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
166 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
167 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
170 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
171 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
172 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
175 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
177 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
178 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
179 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
180 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
183 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
184 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
185 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
188 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
189 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
194 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
195 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
200 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
201 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
205 e9ce266e 2022-03-07 op imsg_free(&imsg);
210 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
214 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
215 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
217 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
218 b3c57ab2 2022-03-22 op if (h != NULL && h->nonl) {
219 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
222 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
223 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
224 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
227 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
228 e9ce266e 2022-03-07 op if ((h = calloc(1, sizeof(*h))) == NULL) {
229 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
232 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
233 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
234 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
235 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
236 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
238 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
239 e9ce266e 2022-03-07 op if (h == NULL) {
240 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
243 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
244 e9ce266e 2022-03-07 op t = imsg.data;
245 b3c57ab2 2022-03-22 op /* at least one char */
246 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
247 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
250 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
251 b3c57ab2 2022-03-22 op *t != '\\') {
252 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
256 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
257 b3c57ab2 2022-03-22 op if (*t == '\\')
260 b3c57ab2 2022-03-22 op err = pushline(h, t);
265 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
269 e9ce266e 2022-03-07 op imsg_free(&imsg);
273 e9ce266e 2022-03-07 op imsg_free(&imsg);
278 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
279 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
281 e9ce266e 2022-03-07 op static const struct got_error *
282 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
284 e9ce266e 2022-03-07 op char buf[BUFSIZ];
285 e9ce266e 2022-03-07 op size_t len, r, w;
287 e9ce266e 2022-03-07 op if (fseek(orig, copypos, SEEK_SET) == -1)
288 e9ce266e 2022-03-07 op return got_error_from_errno("fseek");
290 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
291 e9ce266e 2022-03-07 op len = sizeof(buf);
293 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
294 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
295 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
296 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
297 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
299 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
300 e9ce266e 2022-03-07 op copypos += len;
301 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
302 e9ce266e 2022-03-07 op if (pos == -1)
304 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
310 e9ce266e 2022-03-07 op static const struct got_error *
311 33df9995 2022-03-11 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
313 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
314 e9ce266e 2022-03-07 op char *line = NULL;
315 e9ce266e 2022-03-07 op char mode = *h->lines[0];
316 e9ce266e 2022-03-07 op size_t linesize = 0;
317 e9ce266e 2022-03-07 op ssize_t linelen;
318 e9ce266e 2022-03-07 op off_t match = -1;
319 e9ce266e 2022-03-07 op long match_lineno = -1;
322 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
323 e9ce266e 2022-03-07 op if (linelen == -1) {
324 e9ce266e 2022-03-07 op if (ferror(orig))
325 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
326 e9ce266e 2022-03-07 op else if (match == -1)
327 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
330 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
331 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
334 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
335 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
336 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
337 e9ce266e 2022-03-07 op match = ftello(orig);
338 e9ce266e 2022-03-07 op if (match == -1) {
339 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
342 e9ce266e 2022-03-07 op match -= linelen;
343 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
346 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
350 e9ce266e 2022-03-07 op if (err == NULL) {
351 33df9995 2022-03-11 op *pos = match;
352 e9ce266e 2022-03-07 op *lineno = match_lineno;
353 e9ce266e 2022-03-07 op if (fseek(orig, match, SEEK_SET) == -1)
354 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
361 e9ce266e 2022-03-07 op static const struct got_error *
362 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
364 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
365 e9ce266e 2022-03-07 op char *line = NULL;
366 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
367 e9ce266e 2022-03-07 op ssize_t linelen;
369 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
370 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
375 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
376 e9ce266e 2022-03-07 op if (linelen == -1) {
377 e9ce266e 2022-03-07 op if (ferror(orig))
378 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
380 e9ce266e 2022-03-07 op err = got_error(
381 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
384 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
385 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
386 46ebad13 2022-03-17 op if (strcmp(h->lines[i] + 1, line)) {
387 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
399 e9ce266e 2022-03-07 op static const struct got_error *
400 e9ce266e 2022-03-07 op apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
402 e9ce266e 2022-03-07 op size_t i = 0;
404 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
405 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
407 b3c57ab2 2022-03-22 op if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
408 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
409 e9ce266e 2022-03-07 op /* fallthrough */
414 46ebad13 2022-03-17 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
415 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
416 b3c57ab2 2022-03-22 op if (i != h->len - 1 || !h->nonl) {
417 b3c57ab2 2022-03-22 op if (fprintf(tmp, "\n") < 0)
418 b3c57ab2 2022-03-22 op return got_error_from_errno(
427 6e96b326 2022-03-12 op static const struct got_error *
428 2be5e1a2 2022-03-16 op patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
429 2be5e1a2 2022-03-16 op mode_t *mode)
431 6e96b326 2022-03-12 op const struct got_error *err = NULL;
432 6e96b326 2022-03-12 op struct got_patch_hunk *h;
433 2be5e1a2 2022-03-16 op struct stat sb;
434 6e96b326 2022-03-12 op long lineno = 0;
436 6e96b326 2022-03-12 op off_t copypos, pos;
437 6e96b326 2022-03-12 op char *line = NULL;
438 6e96b326 2022-03-12 op size_t linesize = 0;
439 6e96b326 2022-03-12 op ssize_t linelen;
441 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
442 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
443 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
444 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
447 4027dbc2 2022-03-22 op return apply_hunk(tmp, h, &lineno);
450 e9ce266e 2022-03-07 op if ((orig = fopen(path, "r")) == NULL) {
451 e9ce266e 2022-03-07 op err = got_error_from_errno2("fopen", path);
455 2be5e1a2 2022-03-16 op if (fstat(fileno(orig), &sb) == -1) {
456 2be5e1a2 2022-03-16 op err = got_error_from_errno("fstat");
459 2be5e1a2 2022-03-16 op *mode = sb.st_mode;
462 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
463 6e96b326 2022-03-12 op if (h->lines == NULL)
467 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
468 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
469 60aa1fa0 2022-03-17 op h->err = err;
470 e9ce266e 2022-03-07 op if (err != NULL)
473 899fcfdf 2022-03-13 op err = copy(tmp, orig, copypos, pos);
474 e9ce266e 2022-03-07 op if (err != NULL)
476 e9ce266e 2022-03-07 op copypos = pos;
478 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
479 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
481 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
482 e9ce266e 2022-03-07 op * after the previous partial match.
484 e9ce266e 2022-03-07 op if (fseek(orig, pos, SEEK_SET) == -1) {
485 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
488 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
489 e9ce266e 2022-03-07 op if (linelen == -1) {
490 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
494 e9ce266e 2022-03-07 op goto tryagain;
496 e9ce266e 2022-03-07 op if (err != NULL)
499 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
500 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
503 899fcfdf 2022-03-13 op err = apply_hunk(tmp, h, &lineno);
504 e9ce266e 2022-03-07 op if (err != NULL)
507 e9ce266e 2022-03-07 op copypos = ftello(orig);
508 e9ce266e 2022-03-07 op if (copypos == -1) {
509 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
514 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
515 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
516 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
517 60aa1fa0 2022-03-17 op err = h->err;
518 60aa1fa0 2022-03-17 op } else if (!nop && !feof(orig))
519 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
522 6e96b326 2022-03-12 op if (orig != NULL)
523 6e96b326 2022-03-12 op fclose(orig);
527 dbda770b 2022-03-13 op static const struct got_error *
528 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
529 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
531 60aa1fa0 2022-03-17 op const struct got_error *err;
532 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
534 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
535 60aa1fa0 2022-03-17 op orig_error, 0, 0, 0, 0, 0, NULL);
539 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
540 60aa1fa0 2022-03-17 op if (h->offset == 0 && h->err == NULL)
543 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
544 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
545 60aa1fa0 2022-03-17 op h->offset, h->err);
553 60aa1fa0 2022-03-17 op static const struct got_error *
554 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
555 b22138f5 2022-03-16 op const char *path)
557 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
560 b22138f5 2022-03-16 op static const struct got_error *
561 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
563 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
566 b22138f5 2022-03-16 op static const struct got_error *
567 6e96b326 2022-03-12 op apply_patch(struct got_worktree *worktree, struct got_repository *repo,
568 60aa1fa0 2022-03-17 op const char *oldpath, const char *newpath, struct got_patch *p,
569 60aa1fa0 2022-03-17 op int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
571 6e96b326 2022-03-12 op const struct got_error *err = NULL;
572 78f5ac24 2022-03-19 op struct got_pathlist_head oldpaths, newpaths;
573 78f5ac24 2022-03-19 op struct got_pathlist_entry *pe;
574 6e96b326 2022-03-12 op int file_renamed = 0;
575 60aa1fa0 2022-03-17 op char *tmppath = NULL, *template = NULL, *parent = NULL;;
576 6e96b326 2022-03-12 op FILE *tmp = NULL;
577 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
579 78f5ac24 2022-03-19 op TAILQ_INIT(&oldpaths);
580 78f5ac24 2022-03-19 op TAILQ_INIT(&newpaths);
582 78f5ac24 2022-03-19 op err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
585 78f5ac24 2022-03-19 op err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
589 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
591 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
592 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
593 6e96b326 2022-03-12 op err = got_error_from_errno(template);
598 899fcfdf 2022-03-13 op err = got_opentemp_named(&tmppath, &tmp, template);
601 2be5e1a2 2022-03-16 op err = patch_file(p, oldpath, tmp, nop, &mode);
608 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
609 78f5ac24 2022-03-19 op err = got_worktree_schedule_delete(worktree, &oldpaths,
610 b22138f5 2022-03-16 op 0, NULL, patch_delete, pa, repo, 0, 0);
614 2be5e1a2 2022-03-16 op if (fchmod(fileno(tmp), mode) == -1) {
615 2be5e1a2 2022-03-16 op err = got_error_from_errno2("chmod", newpath);
619 6e96b326 2022-03-12 op if (rename(tmppath, newpath) == -1) {
620 95d68340 2022-03-16 op if (errno != ENOENT) {
621 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
626 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
627 95d68340 2022-03-16 op if (err != NULL)
629 95d68340 2022-03-16 op err = got_path_mkdir(parent);
630 95d68340 2022-03-16 op if (err != NULL)
632 95d68340 2022-03-16 op if (rename(tmppath, newpath) == -1) {
633 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
639 6e96b326 2022-03-12 op if (file_renamed) {
640 78f5ac24 2022-03-19 op err = got_worktree_schedule_delete(worktree, &oldpaths,
641 b22138f5 2022-03-16 op 0, NULL, patch_delete, pa, repo, 0, 0);
642 6e96b326 2022-03-12 op if (err == NULL)
643 78f5ac24 2022-03-19 op err = got_worktree_schedule_add(worktree, &newpaths,
644 b22138f5 2022-03-16 op patch_add, pa, repo, 1);
646 78f5ac24 2022-03-19 op unlink(newpath);
647 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
648 78f5ac24 2022-03-19 op err = got_worktree_schedule_add(worktree, &newpaths,
649 b22138f5 2022-03-16 op patch_add, pa, repo, 1);
651 78f5ac24 2022-03-19 op unlink(newpath);
653 60aa1fa0 2022-03-17 op err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
657 78f5ac24 2022-03-19 op got_pathlist_free(&oldpaths);
658 78f5ac24 2022-03-19 op got_pathlist_free(&newpaths);
659 95d68340 2022-03-16 op free(parent);
660 6f5cb1bd 2022-03-08 op free(template);
661 e9ce266e 2022-03-07 op if (tmppath != NULL)
662 e9ce266e 2022-03-07 op unlink(tmppath);
663 e9ce266e 2022-03-07 op free(tmppath);
667 e9ce266e 2022-03-07 op const struct got_error *
668 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
669 9d6cabd5 2022-04-07 op int nop, int strip, got_patch_progress_cb progress_cb, void *progress_arg,
670 b22138f5 2022-03-16 op got_cancel_cb cancel_cb, void *cancel_arg)
672 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
673 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
674 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
675 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
676 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
677 60aa1fa0 2022-03-17 op int done = 0, failed = 0;
680 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
681 e9ce266e 2022-03-07 op if (ibuf == NULL) {
682 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
686 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
687 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
691 e9ce266e 2022-03-07 op pid = fork();
692 e9ce266e 2022-03-07 op if (pid == -1) {
693 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
695 e9ce266e 2022-03-07 op } else if (pid == 0) {
696 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
698 e9ce266e 2022-03-07 op /* not reached */
701 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
702 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
705 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
706 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
708 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
713 78f5ac24 2022-03-19 op err = got_worktree_patch_prepare(&fileindex, worktree);
717 e9ce266e 2022-03-07 op while (!done && err == NULL) {
718 e9ce266e 2022-03-07 op struct got_patch p;
719 60aa1fa0 2022-03-17 op struct patch_args pa;
721 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
722 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
723 60aa1fa0 2022-03-17 op pa.head = &p.head;
725 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
726 e9ce266e 2022-03-07 op if (err || done)
729 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
730 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
731 78f5ac24 2022-03-19 op if (err == NULL)
732 78f5ac24 2022-03-19 op err = apply_patch(worktree, repo, oldpath, newpath,
733 78f5ac24 2022-03-19 op &p, nop, &pa, cancel_cb, cancel_arg);
734 60aa1fa0 2022-03-17 op if (err != NULL) {
736 60aa1fa0 2022-03-17 op /* recoverable errors */
737 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
738 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
739 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
740 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
741 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
742 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
743 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
746 60aa1fa0 2022-03-17 op free(oldpath);
747 60aa1fa0 2022-03-17 op free(newpath);
748 e9ce266e 2022-03-07 op patch_free(&p);
755 78f5ac24 2022-03-19 op if (fileindex)
756 78f5ac24 2022-03-19 op got_worktree_patch_complete(fileindex);
757 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
758 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
759 e9ce266e 2022-03-07 op if (ibuf != NULL)
760 e9ce266e 2022-03-07 op imsg_clear(ibuf);
761 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
762 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
763 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
764 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
765 60aa1fa0 2022-03-17 op if (err == NULL && failed)
766 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);