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;
61 35095610 2022-06-14 op int old_lines;
63 35095610 2022-06-14 op int new_lines;
69 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
70 e9ce266e 2022-03-07 op struct got_patch {
73 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
76 b22138f5 2022-03-16 op struct patch_args {
77 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
78 b22138f5 2022-03-16 op void *progress_arg;
79 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
82 e9ce266e 2022-03-07 op static const struct got_error *
83 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
85 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
87 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
88 e9ce266e 2022-03-07 op NULL, 0) == -1) {
89 e9ce266e 2022-03-07 op err = got_error_from_errno(
90 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
95 e9ce266e 2022-03-07 op if (imsg_flush(ibuf) == -1) {
96 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_flush");
97 e9ce266e 2022-03-07 op imsg_clear(ibuf);
104 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
106 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
109 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
110 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
111 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
113 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
114 e9ce266e 2022-03-07 op free(h->lines[i]);
115 e9ce266e 2022-03-07 op free(h->lines);
119 e9ce266e 2022-03-07 op free(p->new);
120 e9ce266e 2022-03-07 op free(p->old);
122 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
123 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
126 e9ce266e 2022-03-07 op static const struct got_error *
127 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
130 e9ce266e 2022-03-07 op size_t newcap;
132 e9ce266e 2022-03-07 op if (h->len == h->cap) {
133 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
135 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
136 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
137 e9ce266e 2022-03-07 op if (t == NULL)
138 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
139 e9ce266e 2022-03-07 op h->lines = t;
140 e9ce266e 2022-03-07 op h->cap = newcap;
143 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
144 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
146 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
150 e9ce266e 2022-03-07 op static const struct got_error *
151 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
153 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
154 e9ce266e 2022-03-07 op struct imsg imsg;
155 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
156 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
157 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
158 e9ce266e 2022-03-07 op size_t datalen;
159 b2832778 2022-04-23 op int lastmode = -1;
161 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
162 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
164 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
167 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
171 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
172 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
175 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
176 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
177 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
180 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
182 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
183 99b94ad7 2022-06-13 op patch.new[sizeof(patch.new)-1] != '\0') {
184 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
188 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
189 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
190 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
191 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
194 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
195 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
196 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
199 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
200 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
205 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
206 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
211 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
212 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
216 e9ce266e 2022-03-07 op imsg_free(&imsg);
221 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
223 0e07a2a1 2022-06-13 op patch_free(p);
227 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
228 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
229 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
230 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
231 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
233 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
234 be33dff7 2022-05-13 op if (h != NULL &&
235 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
236 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
239 b2832778 2022-04-23 op lastmode = -1;
240 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
241 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
244 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
245 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
246 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
249 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
250 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
253 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
254 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
255 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
256 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
257 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
259 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
260 e9ce266e 2022-03-07 op if (h == NULL) {
261 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
264 e9ce266e 2022-03-07 op t = imsg.data;
265 b3c57ab2 2022-03-22 op /* at least one char */
266 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
267 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
270 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
271 b3c57ab2 2022-03-22 op *t != '\\') {
272 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
276 b2832778 2022-04-23 op if (*t != '\\')
277 b3c57ab2 2022-03-22 op err = pushline(h, t);
278 b2832778 2022-04-23 op else if (lastmode == '-')
279 b2832778 2022-04-23 op h->old_nonl = 1;
280 b2832778 2022-04-23 op else if (lastmode == '+')
281 b2832778 2022-04-23 op h->new_nonl = 1;
283 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
288 b2832778 2022-04-23 op lastmode = *t;
291 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
295 e9ce266e 2022-03-07 op imsg_free(&imsg);
300 0e07a2a1 2022-06-13 op patch_free(p);
302 e9ce266e 2022-03-07 op imsg_free(&imsg);
307 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
308 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
310 e9ce266e 2022-03-07 op static const struct got_error *
311 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
313 e9ce266e 2022-03-07 op char buf[BUFSIZ];
314 e9ce266e 2022-03-07 op size_t len, r, w;
316 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
317 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
319 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
320 e9ce266e 2022-03-07 op len = sizeof(buf);
322 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
323 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
324 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
325 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
326 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
328 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
329 e9ce266e 2022-03-07 op copypos += len;
330 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
331 e9ce266e 2022-03-07 op if (pos == -1)
333 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
339 e9ce266e 2022-03-07 op static const struct got_error *
340 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
342 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
343 e9ce266e 2022-03-07 op char *line = NULL;
344 e9ce266e 2022-03-07 op char mode = *h->lines[0];
345 e9ce266e 2022-03-07 op size_t linesize = 0;
346 e9ce266e 2022-03-07 op ssize_t linelen;
347 e9ce266e 2022-03-07 op off_t match = -1;
348 35095610 2022-06-14 op int match_lineno = -1;
351 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
352 e9ce266e 2022-03-07 op if (linelen == -1) {
353 e9ce266e 2022-03-07 op if (ferror(orig))
354 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
355 e9ce266e 2022-03-07 op else if (match == -1)
356 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
359 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
360 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
363 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
364 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
365 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
366 e9ce266e 2022-03-07 op match = ftello(orig);
367 e9ce266e 2022-03-07 op if (match == -1) {
368 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
371 e9ce266e 2022-03-07 op match -= linelen;
372 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
375 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
379 e9ce266e 2022-03-07 op if (err == NULL) {
380 33df9995 2022-03-11 op *pos = match;
381 e9ce266e 2022-03-07 op *lineno = match_lineno;
382 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
383 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
390 e9ce266e 2022-03-07 op static const struct got_error *
391 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
393 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
394 e9ce266e 2022-03-07 op char *line = NULL;
395 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
396 e9ce266e 2022-03-07 op ssize_t linelen;
398 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
399 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
404 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
405 e9ce266e 2022-03-07 op if (linelen == -1) {
406 e9ce266e 2022-03-07 op if (ferror(orig))
407 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
409 e9ce266e 2022-03-07 op err = got_error(
410 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
413 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
414 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
415 46ebad13 2022-03-17 op if (strcmp(h->lines[i] + 1, line)) {
416 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
428 e9ce266e 2022-03-07 op static const struct got_error *
429 35095610 2022-06-14 op apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
431 b2832778 2022-04-23 op size_t i, new = 0;
433 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
434 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
436 b3c57ab2 2022-03-22 op if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
437 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
438 e9ce266e 2022-03-07 op /* fallthrough */
444 46ebad13 2022-03-17 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
445 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
446 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
447 b3c57ab2 2022-03-22 op if (fprintf(tmp, "\n") < 0)
448 b3c57ab2 2022-03-22 op return got_error_from_errno(
457 6e96b326 2022-03-12 op static const struct got_error *
458 2be5e1a2 2022-03-16 op patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
459 2be5e1a2 2022-03-16 op mode_t *mode)
461 6e96b326 2022-03-12 op const struct got_error *err = NULL;
462 6e96b326 2022-03-12 op struct got_patch_hunk *h;
463 2be5e1a2 2022-03-16 op struct stat sb;
464 35095610 2022-06-14 op int lineno = 0;
466 6e96b326 2022-03-12 op off_t copypos, pos;
467 6e96b326 2022-03-12 op char *line = NULL;
468 6e96b326 2022-03-12 op size_t linesize = 0;
469 6e96b326 2022-03-12 op ssize_t linelen;
471 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
472 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
473 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
474 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
477 4027dbc2 2022-03-22 op return apply_hunk(tmp, h, &lineno);
480 e9ce266e 2022-03-07 op if ((orig = fopen(path, "r")) == NULL) {
481 e9ce266e 2022-03-07 op err = got_error_from_errno2("fopen", path);
485 2be5e1a2 2022-03-16 op if (fstat(fileno(orig), &sb) == -1) {
486 2be5e1a2 2022-03-16 op err = got_error_from_errno("fstat");
489 2be5e1a2 2022-03-16 op *mode = sb.st_mode;
492 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
494 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
495 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
496 60aa1fa0 2022-03-17 op h->err = err;
497 e9ce266e 2022-03-07 op if (err != NULL)
500 899fcfdf 2022-03-13 op err = copy(tmp, orig, copypos, pos);
501 e9ce266e 2022-03-07 op if (err != NULL)
503 e9ce266e 2022-03-07 op copypos = pos;
505 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
506 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
508 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
509 e9ce266e 2022-03-07 op * after the previous partial match.
511 e45f7eba 2022-05-14 naddy if (fseeko(orig, pos, SEEK_SET) == -1) {
512 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
515 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
516 e9ce266e 2022-03-07 op if (linelen == -1) {
517 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
521 e9ce266e 2022-03-07 op goto tryagain;
523 e9ce266e 2022-03-07 op if (err != NULL)
526 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
527 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
530 899fcfdf 2022-03-13 op err = apply_hunk(tmp, h, &lineno);
531 e9ce266e 2022-03-07 op if (err != NULL)
534 e9ce266e 2022-03-07 op copypos = ftello(orig);
535 e9ce266e 2022-03-07 op if (copypos == -1) {
536 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
541 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
542 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
543 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
544 60aa1fa0 2022-03-17 op err = h->err;
545 60aa1fa0 2022-03-17 op } else if (!nop && !feof(orig))
546 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
549 06c44edc 2022-06-15 stsp if (orig != NULL && fclose(orig) == EOF && err == NULL)
550 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
554 dbda770b 2022-03-13 op static const struct got_error *
555 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
556 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
558 60aa1fa0 2022-03-17 op const struct got_error *err;
559 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
561 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
562 60aa1fa0 2022-03-17 op orig_error, 0, 0, 0, 0, 0, NULL);
566 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
567 60aa1fa0 2022-03-17 op if (h->offset == 0 && h->err == NULL)
570 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
571 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
572 60aa1fa0 2022-03-17 op h->offset, h->err);
580 60aa1fa0 2022-03-17 op static const struct got_error *
581 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
582 b22138f5 2022-03-16 op const char *path)
584 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
587 b22138f5 2022-03-16 op static const struct got_error *
588 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
590 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
593 b22138f5 2022-03-16 op static const struct got_error *
594 6e96b326 2022-03-12 op apply_patch(struct got_worktree *worktree, struct got_repository *repo,
595 f2dd7807 2022-05-17 op struct got_fileindex *fileindex, const char *old, const char *new,
596 f2dd7807 2022-05-17 op struct got_patch *p, int nop, struct patch_args *pa,
597 f2dd7807 2022-05-17 op got_cancel_cb cancel_cb, void *cancel_arg)
599 6e96b326 2022-03-12 op const struct got_error *err = NULL;
600 6e96b326 2022-03-12 op int file_renamed = 0;
601 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
602 1b1b91ab 2022-06-15 op char *tmppath = NULL, *template = NULL, *parent = NULL;
603 6e96b326 2022-03-12 op FILE *tmp = NULL;
604 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
606 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
607 ed3bff83 2022-04-23 op old) == -1) {
608 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
612 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
613 ed3bff83 2022-04-23 op new) == -1) {
614 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
618 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
620 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
621 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
622 6e96b326 2022-03-12 op err = got_error_from_errno(template);
627 899fcfdf 2022-03-13 op err = got_opentemp_named(&tmppath, &tmp, template);
630 2be5e1a2 2022-03-16 op err = patch_file(p, oldpath, tmp, nop, &mode);
637 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
638 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
639 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
643 2be5e1a2 2022-03-16 op if (fchmod(fileno(tmp), mode) == -1) {
644 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
648 6e96b326 2022-03-12 op if (rename(tmppath, newpath) == -1) {
649 95d68340 2022-03-16 op if (errno != ENOENT) {
650 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
655 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
656 95d68340 2022-03-16 op if (err != NULL)
658 95d68340 2022-03-16 op err = got_path_mkdir(parent);
659 95d68340 2022-03-16 op if (err != NULL)
661 95d68340 2022-03-16 op if (rename(tmppath, newpath) == -1) {
662 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
668 6e96b326 2022-03-12 op if (file_renamed) {
669 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
670 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
671 6e96b326 2022-03-12 op if (err == NULL)
672 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
673 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
676 78f5ac24 2022-03-19 op unlink(newpath);
677 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
678 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
679 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
681 78f5ac24 2022-03-19 op unlink(newpath);
683 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
686 95d68340 2022-03-16 op free(parent);
687 6f5cb1bd 2022-03-08 op free(template);
688 e9ce266e 2022-03-07 op if (tmppath != NULL)
689 e9ce266e 2022-03-07 op unlink(tmppath);
690 06c44edc 2022-06-15 stsp if (tmp != NULL && fclose(tmp) == EOF && err == NULL)
691 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
692 e9ce266e 2022-03-07 op free(tmppath);
693 ed3bff83 2022-04-23 op free(oldpath);
694 ed3bff83 2022-04-23 op free(newpath);
699 bad961bf 2022-04-23 op reverse_patch(struct got_patch *p)
701 bad961bf 2022-04-23 op struct got_patch_hunk *h;
705 bad961bf 2022-04-23 op STAILQ_FOREACH(h, &p->head, entries) {
706 bad961bf 2022-04-23 op tmp = h->old_from;
707 bad961bf 2022-04-23 op h->old_from = h->new_from;
708 bad961bf 2022-04-23 op h->new_from = tmp;
710 bad961bf 2022-04-23 op tmp = h->old_lines;
711 bad961bf 2022-04-23 op h->old_lines = h->new_lines;
712 bad961bf 2022-04-23 op h->new_lines = tmp;
714 bad961bf 2022-04-23 op tmp = h->old_nonl;
715 bad961bf 2022-04-23 op h->old_nonl = h->new_nonl;
716 bad961bf 2022-04-23 op h->new_nonl = tmp;
718 bad961bf 2022-04-23 op for (i = 0; i < h->len; ++i) {
719 bad961bf 2022-04-23 op if (*h->lines[i] == '+')
720 bad961bf 2022-04-23 op *h->lines[i] = '-';
721 bad961bf 2022-04-23 op else if (*h->lines[i] == '-')
722 bad961bf 2022-04-23 op *h->lines[i] = '+';
727 e9ce266e 2022-03-07 op const struct got_error *
728 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
729 bad961bf 2022-04-23 op int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
730 bad961bf 2022-04-23 op void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
732 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
733 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
734 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
735 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
736 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
737 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
738 60aa1fa0 2022-03-17 op int done = 0, failed = 0;
741 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
742 e9ce266e 2022-03-07 op if (ibuf == NULL) {
743 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
747 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
748 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
752 e9ce266e 2022-03-07 op pid = fork();
753 e9ce266e 2022-03-07 op if (pid == -1) {
754 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
756 e9ce266e 2022-03-07 op } else if (pid == 0) {
757 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
759 e9ce266e 2022-03-07 op /* not reached */
762 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
763 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
766 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
767 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
769 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
774 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
779 e9ce266e 2022-03-07 op while (!done && err == NULL) {
780 e9ce266e 2022-03-07 op struct got_patch p;
781 60aa1fa0 2022-03-17 op struct patch_args pa;
783 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
784 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
785 60aa1fa0 2022-03-17 op pa.head = &p.head;
787 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
788 e9ce266e 2022-03-07 op if (err || done)
792 bad961bf 2022-04-23 op reverse_patch(&p);
794 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
795 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
796 78f5ac24 2022-03-19 op if (err == NULL)
797 f2dd7807 2022-05-17 op err = apply_patch(worktree, repo, fileindex, oldpath,
798 f2dd7807 2022-05-17 op newpath, &p, nop, &pa, cancel_cb, cancel_arg);
799 60aa1fa0 2022-03-17 op if (err != NULL) {
801 60aa1fa0 2022-03-17 op /* recoverable errors */
802 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
803 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
804 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
805 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
806 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
807 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
808 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
811 60aa1fa0 2022-03-17 op free(oldpath);
812 60aa1fa0 2022-03-17 op free(newpath);
813 e9ce266e 2022-03-07 op patch_free(&p);
820 4bcdc895 2022-05-17 op if (fileindex != NULL)
821 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
822 4bcdc895 2022-05-17 op fileindex_path);
823 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
824 f2dd7807 2022-05-17 op err = complete_err;
825 4bcdc895 2022-05-17 op free(fileindex_path);
826 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
827 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
828 e9ce266e 2022-03-07 op if (ibuf != NULL)
829 e9ce266e 2022-03-07 op imsg_clear(ibuf);
830 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
831 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
832 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
833 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
834 60aa1fa0 2022-03-17 op if (err == NULL && failed)
835 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);