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 are still missing:
19 e9ce266e 2022-03-07 op * + "No final newline" handling
21 e9ce266e 2022-03-07 op * Things that we may want to support:
22 e9ce266e 2022-03-07 op * + support indented patches?
23 e9ce266e 2022-03-07 op * + support other kinds of patches?
26 e9ce266e 2022-03-07 op #include <sys/types.h>
27 e9ce266e 2022-03-07 op #include <sys/queue.h>
28 e9ce266e 2022-03-07 op #include <sys/socket.h>
29 5b67f96e 2022-03-13 op #include <sys/stat.h>
30 e9ce266e 2022-03-07 op #include <sys/uio.h>
32 dbda770b 2022-03-13 op #include <errno.h>
33 e9ce266e 2022-03-07 op #include <limits.h>
34 e9ce266e 2022-03-07 op #include <sha1.h>
35 e9ce266e 2022-03-07 op #include <stdint.h>
36 e9ce266e 2022-03-07 op #include <stdio.h>
37 e9ce266e 2022-03-07 op #include <stdlib.h>
38 e9ce266e 2022-03-07 op #include <string.h>
39 e9ce266e 2022-03-07 op #include <unistd.h>
40 e9ce266e 2022-03-07 op #include <imsg.h>
42 e9ce266e 2022-03-07 op #include "got_error.h"
43 e9ce266e 2022-03-07 op #include "got_object.h"
44 e9ce266e 2022-03-07 op #include "got_path.h"
45 e9ce266e 2022-03-07 op #include "got_reference.h"
46 e9ce266e 2022-03-07 op #include "got_cancel.h"
47 e9ce266e 2022-03-07 op #include "got_worktree.h"
48 e9ce266e 2022-03-07 op #include "got_opentemp.h"
49 e9ce266e 2022-03-07 op #include "got_patch.h"
51 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
52 e9ce266e 2022-03-07 op #include "got_lib_object.h"
53 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
55 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 e9ce266e 2022-03-07 op struct got_patch_hunk {
58 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
59 60aa1fa0 2022-03-17 op const struct got_error *err;
61 e9ce266e 2022-03-07 op long old_from;
62 e9ce266e 2022-03-07 op long old_lines;
63 e9ce266e 2022-03-07 op long new_from;
64 e9ce266e 2022-03-07 op long new_lines;
70 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
71 e9ce266e 2022-03-07 op struct got_patch {
74 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
77 b22138f5 2022-03-16 op struct patch_args {
78 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
79 b22138f5 2022-03-16 op void *progress_arg;
80 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
83 e9ce266e 2022-03-07 op static const struct got_error *
84 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
86 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
88 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
89 e9ce266e 2022-03-07 op NULL, 0) == -1) {
90 e9ce266e 2022-03-07 op err = got_error_from_errno(
91 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
96 e9ce266e 2022-03-07 op if (imsg_flush(ibuf) == -1) {
97 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_flush");
98 e9ce266e 2022-03-07 op imsg_clear(ibuf);
105 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
107 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
110 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
111 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
112 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
114 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
115 e9ce266e 2022-03-07 op free(h->lines[i]);
116 e9ce266e 2022-03-07 op free(h->lines);
120 e9ce266e 2022-03-07 op free(p->new);
121 e9ce266e 2022-03-07 op free(p->old);
124 e9ce266e 2022-03-07 op static const struct got_error *
125 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
128 e9ce266e 2022-03-07 op size_t newcap;
130 e9ce266e 2022-03-07 op if (h->len == h->cap) {
131 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
133 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
134 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
135 e9ce266e 2022-03-07 op if (t == NULL)
136 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
137 e9ce266e 2022-03-07 op h->lines = t;
138 e9ce266e 2022-03-07 op h->cap = newcap;
141 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
142 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
144 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
148 e9ce266e 2022-03-07 op static const struct got_error *
149 e9ce266e 2022-03-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
151 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
152 e9ce266e 2022-03-07 op struct imsg imsg;
153 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
154 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
155 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
156 e9ce266e 2022-03-07 op size_t datalen;
158 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
159 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
161 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
164 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
168 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
169 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
172 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
173 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
174 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
177 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
178 e9ce266e 2022-03-07 op if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
179 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
182 e9ce266e 2022-03-07 op if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
183 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
186 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
187 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
191 e9ce266e 2022-03-07 op imsg_free(&imsg);
196 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
200 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
201 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
203 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
204 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
205 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
206 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
209 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
210 e9ce266e 2022-03-07 op if ((h = calloc(1, sizeof(*h))) == NULL) {
211 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
214 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
215 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
216 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
217 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
218 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
220 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
221 e9ce266e 2022-03-07 op if (h == NULL) {
222 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
225 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
226 e9ce266e 2022-03-07 op t = imsg.data;
227 e9ce266e 2022-03-07 op /* at least one char plus newline */
228 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
229 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
232 e9ce266e 2022-03-07 op if (*t != ' ' && *t != '-' && *t != '+') {
233 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
236 e9ce266e 2022-03-07 op err = pushline(h, t);
241 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
245 e9ce266e 2022-03-07 op imsg_free(&imsg);
249 e9ce266e 2022-03-07 op imsg_free(&imsg);
254 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
255 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
257 e9ce266e 2022-03-07 op static const struct got_error *
258 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
260 e9ce266e 2022-03-07 op char buf[BUFSIZ];
261 e9ce266e 2022-03-07 op size_t len, r, w;
263 e9ce266e 2022-03-07 op if (fseek(orig, copypos, SEEK_SET) == -1)
264 e9ce266e 2022-03-07 op return got_error_from_errno("fseek");
266 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
267 e9ce266e 2022-03-07 op len = sizeof(buf);
269 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
270 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
271 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
272 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
273 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
275 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
276 e9ce266e 2022-03-07 op copypos += len;
277 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
278 e9ce266e 2022-03-07 op if (pos == -1)
280 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
286 e9ce266e 2022-03-07 op static const struct got_error *
287 33df9995 2022-03-11 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
289 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
290 e9ce266e 2022-03-07 op char *line = NULL;
291 e9ce266e 2022-03-07 op char mode = *h->lines[0];
292 e9ce266e 2022-03-07 op size_t linesize = 0;
293 e9ce266e 2022-03-07 op ssize_t linelen;
294 e9ce266e 2022-03-07 op off_t match = -1;
295 e9ce266e 2022-03-07 op long match_lineno = -1;
298 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
299 e9ce266e 2022-03-07 op if (linelen == -1) {
300 e9ce266e 2022-03-07 op if (ferror(orig))
301 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
302 e9ce266e 2022-03-07 op else if (match == -1)
303 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
308 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
309 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
310 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
311 e9ce266e 2022-03-07 op match = ftello(orig);
312 e9ce266e 2022-03-07 op if (match == -1) {
313 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
316 e9ce266e 2022-03-07 op match -= linelen;
317 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
320 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
324 e9ce266e 2022-03-07 op if (err == NULL) {
325 33df9995 2022-03-11 op *pos = match;
326 e9ce266e 2022-03-07 op *lineno = match_lineno;
327 e9ce266e 2022-03-07 op if (fseek(orig, match, SEEK_SET) == -1)
328 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
335 e9ce266e 2022-03-07 op static const struct got_error *
336 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
338 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
339 e9ce266e 2022-03-07 op char *line = NULL;
340 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
341 e9ce266e 2022-03-07 op ssize_t linelen;
343 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
344 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
349 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
350 e9ce266e 2022-03-07 op if (linelen == -1) {
351 e9ce266e 2022-03-07 op if (ferror(orig))
352 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
354 e9ce266e 2022-03-07 op err = got_error(
355 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
358 46ebad13 2022-03-17 op if (strcmp(h->lines[i] + 1, line)) {
359 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
371 e9ce266e 2022-03-07 op static const struct got_error *
372 e9ce266e 2022-03-07 op apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
374 e9ce266e 2022-03-07 op size_t i = 0;
376 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
377 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
379 46ebad13 2022-03-17 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
380 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
381 e9ce266e 2022-03-07 op /* fallthrough */
386 46ebad13 2022-03-17 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
387 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
394 6e96b326 2022-03-12 op static const struct got_error *
395 2be5e1a2 2022-03-16 op patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
396 2be5e1a2 2022-03-16 op mode_t *mode)
398 6e96b326 2022-03-12 op const struct got_error *err = NULL;
399 6e96b326 2022-03-12 op struct got_patch_hunk *h;
400 2be5e1a2 2022-03-16 op struct stat sb;
402 6e96b326 2022-03-12 op long lineno = 0;
404 6e96b326 2022-03-12 op off_t copypos, pos;
405 6e96b326 2022-03-12 op char *line = NULL;
406 6e96b326 2022-03-12 op size_t linesize = 0;
407 6e96b326 2022-03-12 op ssize_t linelen;
409 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
410 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
411 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
412 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
415 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
416 46ebad13 2022-03-17 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
417 6e96b326 2022-03-12 op return got_error_from_errno("fprintf");
422 e9ce266e 2022-03-07 op if ((orig = fopen(path, "r")) == NULL) {
423 e9ce266e 2022-03-07 op err = got_error_from_errno2("fopen", path);
427 2be5e1a2 2022-03-16 op if (fstat(fileno(orig), &sb) == -1) {
428 2be5e1a2 2022-03-16 op err = got_error_from_errno("fstat");
431 2be5e1a2 2022-03-16 op *mode = sb.st_mode;
434 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
435 6e96b326 2022-03-12 op if (h->lines == NULL)
439 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
440 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
441 60aa1fa0 2022-03-17 op h->err = err;
442 e9ce266e 2022-03-07 op if (err != NULL)
445 899fcfdf 2022-03-13 op err = copy(tmp, orig, copypos, pos);
446 e9ce266e 2022-03-07 op if (err != NULL)
448 e9ce266e 2022-03-07 op copypos = pos;
450 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
451 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
453 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
454 e9ce266e 2022-03-07 op * after the previous partial match.
456 e9ce266e 2022-03-07 op if (fseek(orig, pos, SEEK_SET) == -1) {
457 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
460 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
461 e9ce266e 2022-03-07 op if (linelen == -1) {
462 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
466 e9ce266e 2022-03-07 op goto tryagain;
468 e9ce266e 2022-03-07 op if (err != NULL)
471 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
472 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
475 899fcfdf 2022-03-13 op err = apply_hunk(tmp, h, &lineno);
476 e9ce266e 2022-03-07 op if (err != NULL)
479 e9ce266e 2022-03-07 op copypos = ftello(orig);
480 e9ce266e 2022-03-07 op if (copypos == -1) {
481 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
486 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
487 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
488 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
489 60aa1fa0 2022-03-17 op err = h->err;
490 60aa1fa0 2022-03-17 op } else if (!nop && !feof(orig))
491 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
494 6e96b326 2022-03-12 op if (orig != NULL)
495 6e96b326 2022-03-12 op fclose(orig);
499 dbda770b 2022-03-13 op static const struct got_error *
500 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
501 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
503 60aa1fa0 2022-03-17 op const struct got_error *err;
504 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
506 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
507 60aa1fa0 2022-03-17 op orig_error, 0, 0, 0, 0, 0, NULL);
511 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
512 60aa1fa0 2022-03-17 op if (h->offset == 0 && h->err == NULL)
515 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
516 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
517 60aa1fa0 2022-03-17 op h->offset, h->err);
525 60aa1fa0 2022-03-17 op static const struct got_error *
526 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
527 b22138f5 2022-03-16 op const char *path)
529 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
532 b22138f5 2022-03-16 op static const struct got_error *
533 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
535 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
538 b22138f5 2022-03-16 op static const struct got_error *
539 6e96b326 2022-03-12 op apply_patch(struct got_worktree *worktree, struct got_repository *repo,
540 60aa1fa0 2022-03-17 op const char *oldpath, const char *newpath, struct got_patch *p,
541 60aa1fa0 2022-03-17 op int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
543 6e96b326 2022-03-12 op const struct got_error *err = NULL;
544 78f5ac24 2022-03-19 op struct got_pathlist_head oldpaths, newpaths;
545 78f5ac24 2022-03-19 op struct got_pathlist_entry *pe;
546 6e96b326 2022-03-12 op int file_renamed = 0;
547 60aa1fa0 2022-03-17 op char *tmppath = NULL, *template = NULL, *parent = NULL;;
548 6e96b326 2022-03-12 op FILE *tmp = NULL;
549 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
551 78f5ac24 2022-03-19 op TAILQ_INIT(&oldpaths);
552 78f5ac24 2022-03-19 op TAILQ_INIT(&newpaths);
554 78f5ac24 2022-03-19 op err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
557 78f5ac24 2022-03-19 op err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
561 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
563 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
564 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
565 6e96b326 2022-03-12 op err = got_error_from_errno(template);
570 899fcfdf 2022-03-13 op err = got_opentemp_named(&tmppath, &tmp, template);
573 2be5e1a2 2022-03-16 op err = patch_file(p, oldpath, tmp, nop, &mode);
580 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
581 78f5ac24 2022-03-19 op err = got_worktree_schedule_delete(worktree, &oldpaths,
582 b22138f5 2022-03-16 op 0, NULL, patch_delete, pa, repo, 0, 0);
586 2be5e1a2 2022-03-16 op if (fchmod(fileno(tmp), mode) == -1) {
587 2be5e1a2 2022-03-16 op err = got_error_from_errno2("chmod", newpath);
591 6e96b326 2022-03-12 op if (rename(tmppath, newpath) == -1) {
592 95d68340 2022-03-16 op if (errno != ENOENT) {
593 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
598 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
599 95d68340 2022-03-16 op if (err != NULL)
601 95d68340 2022-03-16 op err = got_path_mkdir(parent);
602 95d68340 2022-03-16 op if (err != NULL)
604 95d68340 2022-03-16 op if (rename(tmppath, newpath) == -1) {
605 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
611 6e96b326 2022-03-12 op if (file_renamed) {
612 78f5ac24 2022-03-19 op err = got_worktree_schedule_delete(worktree, &oldpaths,
613 b22138f5 2022-03-16 op 0, NULL, patch_delete, pa, repo, 0, 0);
614 6e96b326 2022-03-12 op if (err == NULL)
615 78f5ac24 2022-03-19 op err = got_worktree_schedule_add(worktree, &newpaths,
616 b22138f5 2022-03-16 op patch_add, pa, repo, 1);
618 78f5ac24 2022-03-19 op unlink(newpath);
619 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
620 78f5ac24 2022-03-19 op err = got_worktree_schedule_add(worktree, &newpaths,
621 b22138f5 2022-03-16 op patch_add, pa, repo, 1);
623 78f5ac24 2022-03-19 op unlink(newpath);
625 60aa1fa0 2022-03-17 op err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
629 78f5ac24 2022-03-19 op got_pathlist_free(&oldpaths);
630 78f5ac24 2022-03-19 op got_pathlist_free(&newpaths);
631 95d68340 2022-03-16 op free(parent);
632 6f5cb1bd 2022-03-08 op free(template);
633 e9ce266e 2022-03-07 op if (tmppath != NULL)
634 e9ce266e 2022-03-07 op unlink(tmppath);
635 e9ce266e 2022-03-07 op free(tmppath);
639 e9ce266e 2022-03-07 op const struct got_error *
640 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
641 b22138f5 2022-03-16 op int nop, got_patch_progress_cb progress_cb, void *progress_arg,
642 b22138f5 2022-03-16 op got_cancel_cb cancel_cb, void *cancel_arg)
644 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
645 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
646 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
647 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
648 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
649 60aa1fa0 2022-03-17 op int done = 0, failed = 0;
652 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
653 e9ce266e 2022-03-07 op if (ibuf == NULL) {
654 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
658 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
659 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
663 e9ce266e 2022-03-07 op pid = fork();
664 e9ce266e 2022-03-07 op if (pid == -1) {
665 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
667 e9ce266e 2022-03-07 op } else if (pid == 0) {
668 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
670 e9ce266e 2022-03-07 op /* not reached */
673 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
674 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
677 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
678 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
680 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
685 78f5ac24 2022-03-19 op err = got_worktree_patch_prepare(&fileindex, worktree);
689 e9ce266e 2022-03-07 op while (!done && err == NULL) {
690 e9ce266e 2022-03-07 op struct got_patch p;
691 60aa1fa0 2022-03-17 op struct patch_args pa;
693 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
694 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
695 60aa1fa0 2022-03-17 op pa.head = &p.head;
697 e9ce266e 2022-03-07 op err = recv_patch(ibuf, &done, &p);
698 e9ce266e 2022-03-07 op if (err || done)
701 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
702 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
703 78f5ac24 2022-03-19 op if (err == NULL)
704 78f5ac24 2022-03-19 op err = apply_patch(worktree, repo, oldpath, newpath,
705 78f5ac24 2022-03-19 op &p, nop, &pa, cancel_cb, cancel_arg);
706 60aa1fa0 2022-03-17 op if (err != NULL) {
708 60aa1fa0 2022-03-17 op /* recoverable errors */
709 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
710 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
711 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
712 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
713 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
714 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
715 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
718 60aa1fa0 2022-03-17 op free(oldpath);
719 60aa1fa0 2022-03-17 op free(newpath);
720 e9ce266e 2022-03-07 op patch_free(&p);
727 78f5ac24 2022-03-19 op if (fileindex)
728 78f5ac24 2022-03-19 op got_worktree_patch_complete(fileindex);
729 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
730 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
731 e9ce266e 2022-03-07 op if (ibuf != NULL)
732 e9ce266e 2022-03-07 op imsg_clear(ibuf);
733 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
734 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
735 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
736 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
737 60aa1fa0 2022-03-17 op if (err == NULL && failed)
738 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);