2 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 069bbb86 2022-03-07 thomas * Permission to use, copy, modify, and distribute this software for any
5 069bbb86 2022-03-07 thomas * purpose with or without fee is hereby granted, provided that the above
6 069bbb86 2022-03-07 thomas * copyright notice and this permission notice appear in all copies.
8 069bbb86 2022-03-07 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 069bbb86 2022-03-07 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 069bbb86 2022-03-07 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 069bbb86 2022-03-07 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 069bbb86 2022-03-07 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 069bbb86 2022-03-07 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 069bbb86 2022-03-07 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 069bbb86 2022-03-07 thomas * Apply patches.
18 069bbb86 2022-03-07 thomas * Things that we may want to support:
19 069bbb86 2022-03-07 thomas * + support indented patches?
20 069bbb86 2022-03-07 thomas * + support other kinds of patches?
23 069bbb86 2022-03-07 thomas #include <sys/types.h>
24 069bbb86 2022-03-07 thomas #include <sys/socket.h>
25 7dd42450 2022-03-13 thomas #include <sys/stat.h>
26 069bbb86 2022-03-07 thomas #include <sys/uio.h>
28 42d9d68e 2022-03-13 thomas #include <errno.h>
29 069bbb86 2022-03-07 thomas #include <limits.h>
30 069bbb86 2022-03-07 thomas #include <stdint.h>
31 069bbb86 2022-03-07 thomas #include <stdio.h>
32 069bbb86 2022-03-07 thomas #include <stdlib.h>
33 069bbb86 2022-03-07 thomas #include <string.h>
34 069bbb86 2022-03-07 thomas #include <unistd.h>
36 069bbb86 2022-03-07 thomas #include "got_error.h"
37 069bbb86 2022-03-07 thomas #include "got_object.h"
38 069bbb86 2022-03-07 thomas #include "got_path.h"
39 069bbb86 2022-03-07 thomas #include "got_reference.h"
40 069bbb86 2022-03-07 thomas #include "got_cancel.h"
41 069bbb86 2022-03-07 thomas #include "got_worktree.h"
42 069bbb86 2022-03-07 thomas #include "got_opentemp.h"
43 069bbb86 2022-03-07 thomas #include "got_patch.h"
45 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
46 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
47 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
49 069bbb86 2022-03-07 thomas #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 069bbb86 2022-03-07 thomas struct got_patch_hunk {
52 069bbb86 2022-03-07 thomas STAILQ_ENTRY(got_patch_hunk) entries;
53 49114f01 2022-03-22 thomas const struct got_error *err;
54 49114f01 2022-03-22 thomas long offset;
56 069bbb86 2022-03-07 thomas long old_from;
57 069bbb86 2022-03-07 thomas long old_lines;
58 069bbb86 2022-03-07 thomas long new_from;
59 069bbb86 2022-03-07 thomas long new_lines;
60 069bbb86 2022-03-07 thomas size_t len;
61 069bbb86 2022-03-07 thomas size_t cap;
62 069bbb86 2022-03-07 thomas char **lines;
65 49114f01 2022-03-22 thomas STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
66 069bbb86 2022-03-07 thomas struct got_patch {
67 069bbb86 2022-03-07 thomas char *old;
68 069bbb86 2022-03-07 thomas char *new;
69 49114f01 2022-03-22 thomas struct got_patch_hunk_head head;
72 c71da4f7 2022-03-22 thomas struct patch_args {
73 c71da4f7 2022-03-22 thomas got_patch_progress_cb progress_cb;
74 c71da4f7 2022-03-22 thomas void *progress_arg;
75 49114f01 2022-03-22 thomas struct got_patch_hunk_head *head;
78 069bbb86 2022-03-07 thomas static const struct got_error *
79 069bbb86 2022-03-07 thomas send_patch(struct imsgbuf *ibuf, int fd)
81 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
83 069bbb86 2022-03-07 thomas if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
84 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
85 069bbb86 2022-03-07 thomas err = got_error_from_errno(
86 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_FILE");
87 069bbb86 2022-03-07 thomas close(fd);
88 069bbb86 2022-03-07 thomas return err;
91 069bbb86 2022-03-07 thomas if (imsg_flush(ibuf) == -1) {
92 069bbb86 2022-03-07 thomas err = got_error_from_errno("imsg_flush");
93 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
96 069bbb86 2022-03-07 thomas return err;
99 069bbb86 2022-03-07 thomas static void
100 069bbb86 2022-03-07 thomas patch_free(struct got_patch *p)
102 069bbb86 2022-03-07 thomas struct got_patch_hunk *h;
103 069bbb86 2022-03-07 thomas size_t i;
105 069bbb86 2022-03-07 thomas while (!STAILQ_EMPTY(&p->head)) {
106 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
107 069bbb86 2022-03-07 thomas STAILQ_REMOVE_HEAD(&p->head, entries);
109 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i)
110 069bbb86 2022-03-07 thomas free(h->lines[i]);
111 069bbb86 2022-03-07 thomas free(h->lines);
115 069bbb86 2022-03-07 thomas free(p->new);
116 069bbb86 2022-03-07 thomas free(p->old);
119 069bbb86 2022-03-07 thomas static const struct got_error *
120 069bbb86 2022-03-07 thomas pushline(struct got_patch_hunk *h, const char *line)
123 069bbb86 2022-03-07 thomas size_t newcap;
125 069bbb86 2022-03-07 thomas if (h->len == h->cap) {
126 069bbb86 2022-03-07 thomas if ((newcap = h->cap * 1.5) == 0)
127 069bbb86 2022-03-07 thomas newcap = 16;
128 069bbb86 2022-03-07 thomas t = recallocarray(h->lines, h->cap, newcap,
129 069bbb86 2022-03-07 thomas sizeof(h->lines[0]));
130 069bbb86 2022-03-07 thomas if (t == NULL)
131 069bbb86 2022-03-07 thomas return got_error_from_errno("recallocarray");
132 069bbb86 2022-03-07 thomas h->lines = t;
133 069bbb86 2022-03-07 thomas h->cap = newcap;
136 069bbb86 2022-03-07 thomas if ((t = strdup(line)) == NULL)
137 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
139 069bbb86 2022-03-07 thomas h->lines[h->len++] = t;
140 069bbb86 2022-03-07 thomas return NULL;
143 069bbb86 2022-03-07 thomas static const struct got_error *
144 d9db2ff9 2022-04-16 thomas recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
146 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
147 069bbb86 2022-03-07 thomas struct imsg imsg;
148 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
149 069bbb86 2022-03-07 thomas struct got_imsg_patch patch;
150 069bbb86 2022-03-07 thomas struct got_patch_hunk *h = NULL;
151 069bbb86 2022-03-07 thomas size_t datalen;
153 069bbb86 2022-03-07 thomas memset(p, 0, sizeof(*p));
154 069bbb86 2022-03-07 thomas STAILQ_INIT(&p->head);
156 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
158 069bbb86 2022-03-07 thomas return err;
159 069bbb86 2022-03-07 thomas if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
160 069bbb86 2022-03-07 thomas *done = 1;
161 069bbb86 2022-03-07 thomas goto done;
163 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH) {
164 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
165 069bbb86 2022-03-07 thomas goto done;
167 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
168 069bbb86 2022-03-07 thomas if (datalen != sizeof(patch)) {
169 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
170 069bbb86 2022-03-07 thomas goto done;
172 069bbb86 2022-03-07 thomas memcpy(&patch, imsg.data, sizeof(patch));
174 d9db2ff9 2022-04-16 thomas /* automatically set strip=1 for git-style diffs */
175 d9db2ff9 2022-04-16 thomas if (strip == -1 && patch.git &&
176 d9db2ff9 2022-04-16 thomas (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
177 d9db2ff9 2022-04-16 thomas (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
178 d9db2ff9 2022-04-16 thomas strip = 1;
180 d9db2ff9 2022-04-16 thomas /* prefer the new name if not /dev/null for not git-style diffs */
181 d9db2ff9 2022-04-16 thomas if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
182 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->old, patch.new, strip);
184 d9db2ff9 2022-04-16 thomas goto done;
185 d9db2ff9 2022-04-16 thomas } else if (*patch.old != '\0') {
186 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->old, patch.old, strip);
188 d9db2ff9 2022-04-16 thomas goto done;
191 d9db2ff9 2022-04-16 thomas if (*patch.new != '\0') {
192 d9db2ff9 2022-04-16 thomas err = got_path_strip(&p->new, patch.new, strip);
194 d9db2ff9 2022-04-16 thomas goto done;
197 64c9e565 2022-03-13 thomas if (p->old == NULL && p->new == NULL) {
198 64c9e565 2022-03-13 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
199 64c9e565 2022-03-13 thomas goto done;
202 069bbb86 2022-03-07 thomas imsg_free(&imsg);
204 069bbb86 2022-03-07 thomas for (;;) {
207 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
209 069bbb86 2022-03-07 thomas return err;
211 069bbb86 2022-03-07 thomas switch (imsg.hdr.type) {
212 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_DONE:
213 069bbb86 2022-03-07 thomas goto done;
214 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_HUNK:
215 ff7f34d3 2022-03-22 thomas if (h != NULL && h->nonl) {
216 ff7f34d3 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
217 ff7f34d3 2022-03-22 thomas goto done;
219 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 069bbb86 2022-03-07 thomas if (datalen != sizeof(hdr)) {
221 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
222 069bbb86 2022-03-07 thomas goto done;
224 069bbb86 2022-03-07 thomas memcpy(&hdr, imsg.data, sizeof(hdr));
225 069bbb86 2022-03-07 thomas if ((h = calloc(1, sizeof(*h))) == NULL) {
226 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
227 069bbb86 2022-03-07 thomas goto done;
229 069bbb86 2022-03-07 thomas h->old_from = hdr.oldfrom;
230 069bbb86 2022-03-07 thomas h->old_lines = hdr.oldlines;
231 069bbb86 2022-03-07 thomas h->new_from = hdr.newfrom;
232 069bbb86 2022-03-07 thomas h->new_lines = hdr.newlines;
233 069bbb86 2022-03-07 thomas STAILQ_INSERT_TAIL(&p->head, h, entries);
235 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_LINE:
236 069bbb86 2022-03-07 thomas if (h == NULL) {
237 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
238 069bbb86 2022-03-07 thomas goto done;
240 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
241 069bbb86 2022-03-07 thomas t = imsg.data;
242 ff7f34d3 2022-03-22 thomas /* at least one char */
243 069bbb86 2022-03-07 thomas if (datalen < 2 || t[datalen-1] != '\0') {
244 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
245 069bbb86 2022-03-07 thomas goto done;
247 ff7f34d3 2022-03-22 thomas if (*t != ' ' && *t != '-' && *t != '+' &&
248 ff7f34d3 2022-03-22 thomas *t != '\\') {
249 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
250 069bbb86 2022-03-07 thomas goto done;
252 ff7f34d3 2022-03-22 thomas if (h->nonl)
253 ff7f34d3 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
254 ff7f34d3 2022-03-22 thomas if (*t == '\\')
255 ff7f34d3 2022-03-22 thomas h->nonl = 1;
257 ff7f34d3 2022-03-22 thomas err = pushline(h, t);
259 069bbb86 2022-03-07 thomas goto done;
262 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
263 069bbb86 2022-03-07 thomas goto done;
266 069bbb86 2022-03-07 thomas imsg_free(&imsg);
270 069bbb86 2022-03-07 thomas imsg_free(&imsg);
271 069bbb86 2022-03-07 thomas return err;
275 069bbb86 2022-03-07 thomas * Copy data from orig starting at copypos until pos into tmp.
276 069bbb86 2022-03-07 thomas * If pos is -1, copy until EOF.
278 069bbb86 2022-03-07 thomas static const struct got_error *
279 069bbb86 2022-03-07 thomas copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
281 069bbb86 2022-03-07 thomas char buf[BUFSIZ];
282 069bbb86 2022-03-07 thomas size_t len, r, w;
284 069bbb86 2022-03-07 thomas if (fseek(orig, copypos, SEEK_SET) == -1)
285 069bbb86 2022-03-07 thomas return got_error_from_errno("fseek");
287 069bbb86 2022-03-07 thomas while (pos == -1 || copypos < pos) {
288 069bbb86 2022-03-07 thomas len = sizeof(buf);
289 069bbb86 2022-03-07 thomas if (pos > 0)
290 069bbb86 2022-03-07 thomas len = MIN(len, (size_t)pos - copypos);
291 069bbb86 2022-03-07 thomas r = fread(buf, 1, len, orig);
292 069bbb86 2022-03-07 thomas if (r != len && ferror(orig))
293 069bbb86 2022-03-07 thomas return got_error_from_errno("fread");
294 069bbb86 2022-03-07 thomas w = fwrite(buf, 1, r, tmp);
295 069bbb86 2022-03-07 thomas if (w != r)
296 069bbb86 2022-03-07 thomas return got_error_from_errno("fwrite");
297 069bbb86 2022-03-07 thomas copypos += len;
298 069bbb86 2022-03-07 thomas if (r != len && feof(orig)) {
299 069bbb86 2022-03-07 thomas if (pos == -1)
300 069bbb86 2022-03-07 thomas return NULL;
301 49114f01 2022-03-22 thomas return got_error(GOT_ERR_HUNK_FAILED);
304 069bbb86 2022-03-07 thomas return NULL;
307 069bbb86 2022-03-07 thomas static const struct got_error *
308 122e1611 2022-03-22 thomas locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
310 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
311 069bbb86 2022-03-07 thomas char *line = NULL;
312 069bbb86 2022-03-07 thomas char mode = *h->lines[0];
313 069bbb86 2022-03-07 thomas size_t linesize = 0;
314 069bbb86 2022-03-07 thomas ssize_t linelen;
315 069bbb86 2022-03-07 thomas off_t match = -1;
316 069bbb86 2022-03-07 thomas long match_lineno = -1;
318 069bbb86 2022-03-07 thomas for (;;) {
319 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
320 069bbb86 2022-03-07 thomas if (linelen == -1) {
321 069bbb86 2022-03-07 thomas if (ferror(orig))
322 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
323 069bbb86 2022-03-07 thomas else if (match == -1)
324 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
327 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
328 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
329 069bbb86 2022-03-07 thomas (*lineno)++;
331 17d6446a 2022-03-22 thomas if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
332 17d6446a 2022-03-22 thomas (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
333 069bbb86 2022-03-07 thomas (mode == '+' && *lineno == h->old_from)) {
334 069bbb86 2022-03-07 thomas match = ftello(orig);
335 069bbb86 2022-03-07 thomas if (match == -1) {
336 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
339 069bbb86 2022-03-07 thomas match -= linelen;
340 069bbb86 2022-03-07 thomas match_lineno = (*lineno)-1;
343 069bbb86 2022-03-07 thomas if (*lineno >= h->old_from && match != -1)
347 069bbb86 2022-03-07 thomas if (err == NULL) {
348 122e1611 2022-03-22 thomas *pos = match;
349 069bbb86 2022-03-07 thomas *lineno = match_lineno;
350 069bbb86 2022-03-07 thomas if (fseek(orig, match, SEEK_SET) == -1)
351 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
354 069bbb86 2022-03-07 thomas free(line);
355 069bbb86 2022-03-07 thomas return err;
358 069bbb86 2022-03-07 thomas static const struct got_error *
359 069bbb86 2022-03-07 thomas test_hunk(FILE *orig, struct got_patch_hunk *h)
361 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
362 069bbb86 2022-03-07 thomas char *line = NULL;
363 069bbb86 2022-03-07 thomas size_t linesize = 0, i = 0;
364 069bbb86 2022-03-07 thomas ssize_t linelen;
366 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
367 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
368 069bbb86 2022-03-07 thomas case '+':
369 069bbb86 2022-03-07 thomas continue;
370 069bbb86 2022-03-07 thomas case ' ':
371 069bbb86 2022-03-07 thomas case '-':
372 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
373 069bbb86 2022-03-07 thomas if (linelen == -1) {
374 069bbb86 2022-03-07 thomas if (ferror(orig))
375 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
377 069bbb86 2022-03-07 thomas err = got_error(
378 49114f01 2022-03-22 thomas GOT_ERR_HUNK_FAILED);
379 069bbb86 2022-03-07 thomas goto done;
381 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
382 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
383 17d6446a 2022-03-22 thomas if (strcmp(h->lines[i] + 1, line)) {
384 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
385 069bbb86 2022-03-07 thomas goto done;
392 069bbb86 2022-03-07 thomas free(line);
393 069bbb86 2022-03-07 thomas return err;
396 069bbb86 2022-03-07 thomas static const struct got_error *
397 069bbb86 2022-03-07 thomas apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
399 069bbb86 2022-03-07 thomas size_t i = 0;
401 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
402 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
403 069bbb86 2022-03-07 thomas case ' ':
404 ff7f34d3 2022-03-22 thomas if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
405 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
406 069bbb86 2022-03-07 thomas /* fallthrough */
407 069bbb86 2022-03-07 thomas case '-':
408 069bbb86 2022-03-07 thomas (*lineno)++;
410 069bbb86 2022-03-07 thomas case '+':
411 17d6446a 2022-03-22 thomas if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
412 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
413 ff7f34d3 2022-03-22 thomas if (i != h->len - 1 || !h->nonl) {
414 ff7f34d3 2022-03-22 thomas if (fprintf(tmp, "\n") < 0)
415 ff7f34d3 2022-03-22 thomas return got_error_from_errno(
416 ff7f34d3 2022-03-22 thomas "fprintf");
421 069bbb86 2022-03-07 thomas return NULL;
424 46e6bdd4 2022-03-22 thomas static const struct got_error *
425 da09d8ed 2022-03-22 thomas patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
426 da09d8ed 2022-03-22 thomas mode_t *mode)
428 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
429 bb2ad8ff 2022-03-13 thomas struct got_patch_hunk *h;
430 da09d8ed 2022-03-22 thomas struct stat sb;
431 bb2ad8ff 2022-03-13 thomas long lineno = 0;
432 bb2ad8ff 2022-03-13 thomas FILE *orig;
433 bb2ad8ff 2022-03-13 thomas off_t copypos, pos;
434 bb2ad8ff 2022-03-13 thomas char *line = NULL;
435 bb2ad8ff 2022-03-13 thomas size_t linesize = 0;
436 bb2ad8ff 2022-03-13 thomas ssize_t linelen;
438 069bbb86 2022-03-07 thomas if (p->old == NULL) { /* create */
439 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
440 bb2ad8ff 2022-03-13 thomas if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
441 bb2ad8ff 2022-03-13 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
443 eaf99875 2022-03-22 thomas return NULL;
444 afbf14b3 2022-03-22 thomas return apply_hunk(tmp, h, &lineno);
447 069bbb86 2022-03-07 thomas if ((orig = fopen(path, "r")) == NULL) {
448 069bbb86 2022-03-07 thomas err = got_error_from_errno2("fopen", path);
449 069bbb86 2022-03-07 thomas goto done;
452 da09d8ed 2022-03-22 thomas if (fstat(fileno(orig), &sb) == -1) {
453 da09d8ed 2022-03-22 thomas err = got_error_from_errno("fstat");
454 da09d8ed 2022-03-22 thomas goto done;
456 da09d8ed 2022-03-22 thomas *mode = sb.st_mode;
458 069bbb86 2022-03-07 thomas copypos = 0;
459 069bbb86 2022-03-07 thomas STAILQ_FOREACH(h, &p->head, entries) {
460 bb2ad8ff 2022-03-13 thomas if (h->lines == NULL)
463 069bbb86 2022-03-07 thomas tryagain:
464 122e1611 2022-03-22 thomas err = locate_hunk(orig, h, &pos, &lineno);
465 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
466 49114f01 2022-03-22 thomas h->err = err;
467 069bbb86 2022-03-07 thomas if (err != NULL)
468 069bbb86 2022-03-07 thomas goto done;
469 c71da4f7 2022-03-22 thomas if (!nop)
470 eaf99875 2022-03-22 thomas err = copy(tmp, orig, copypos, pos);
471 069bbb86 2022-03-07 thomas if (err != NULL)
472 069bbb86 2022-03-07 thomas goto done;
473 069bbb86 2022-03-07 thomas copypos = pos;
475 069bbb86 2022-03-07 thomas err = test_hunk(orig, h);
476 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
478 069bbb86 2022-03-07 thomas * try to apply the hunk again starting the search
479 069bbb86 2022-03-07 thomas * after the previous partial match.
481 069bbb86 2022-03-07 thomas if (fseek(orig, pos, SEEK_SET) == -1) {
482 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
483 069bbb86 2022-03-07 thomas goto done;
485 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
486 069bbb86 2022-03-07 thomas if (linelen == -1) {
487 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
488 069bbb86 2022-03-07 thomas goto done;
490 069bbb86 2022-03-07 thomas lineno++;
491 069bbb86 2022-03-07 thomas goto tryagain;
493 069bbb86 2022-03-07 thomas if (err != NULL)
494 069bbb86 2022-03-07 thomas goto done;
496 49114f01 2022-03-22 thomas if (lineno + 1 != h->old_from)
497 49114f01 2022-03-22 thomas h->offset = lineno + 1 - h->old_from;
499 c71da4f7 2022-03-22 thomas if (!nop)
500 bfc91212 2022-03-13 thomas err = apply_hunk(tmp, h, &lineno);
501 069bbb86 2022-03-07 thomas if (err != NULL)
502 069bbb86 2022-03-07 thomas goto done;
504 069bbb86 2022-03-07 thomas copypos = ftello(orig);
505 069bbb86 2022-03-07 thomas if (copypos == -1) {
506 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
507 069bbb86 2022-03-07 thomas goto done;
511 49114f01 2022-03-22 thomas if (p->new == NULL && sb.st_size != copypos) {
512 49114f01 2022-03-22 thomas h = STAILQ_FIRST(&p->head);
513 49114f01 2022-03-22 thomas h->err = got_error(GOT_ERR_HUNK_FAILED);
514 49114f01 2022-03-22 thomas err = h->err;
515 49114f01 2022-03-22 thomas } else if (!nop && !feof(orig))
516 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, -1);
519 bb2ad8ff 2022-03-13 thomas if (orig != NULL)
520 bb2ad8ff 2022-03-13 thomas fclose(orig);
521 10e55613 2022-03-22 thomas return err;
524 10e55613 2022-03-22 thomas static const struct got_error *
525 49114f01 2022-03-22 thomas report_progress(struct patch_args *pa, const char *old, const char *new,
526 49114f01 2022-03-22 thomas unsigned char status, const struct got_error *orig_error)
528 49114f01 2022-03-22 thomas const struct got_error *err;
529 49114f01 2022-03-22 thomas struct got_patch_hunk *h;
531 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, status,
532 49114f01 2022-03-22 thomas orig_error, 0, 0, 0, 0, 0, NULL);
534 49114f01 2022-03-22 thomas return err;
536 49114f01 2022-03-22 thomas STAILQ_FOREACH(h, pa->head, entries) {
537 49114f01 2022-03-22 thomas if (h->offset == 0 && h->err == NULL)
538 49114f01 2022-03-22 thomas continue;
540 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
541 49114f01 2022-03-22 thomas h->old_from, h->old_lines, h->new_from, h->new_lines,
542 49114f01 2022-03-22 thomas h->offset, h->err);
544 49114f01 2022-03-22 thomas return err;
547 49114f01 2022-03-22 thomas return NULL;
550 49114f01 2022-03-22 thomas static const struct got_error *
551 c71da4f7 2022-03-22 thomas patch_delete(void *arg, unsigned char status, unsigned char staged_status,
552 c71da4f7 2022-03-22 thomas const char *path)
554 49114f01 2022-03-22 thomas return report_progress(arg, path, NULL, status, NULL);
557 c71da4f7 2022-03-22 thomas static const struct got_error *
558 c71da4f7 2022-03-22 thomas patch_add(void *arg, unsigned char status, const char *path)
560 49114f01 2022-03-22 thomas return report_progress(arg, NULL, path, status, NULL);
563 c71da4f7 2022-03-22 thomas static const struct got_error *
564 bb2ad8ff 2022-03-13 thomas apply_patch(struct got_worktree *worktree, struct got_repository *repo,
565 49114f01 2022-03-22 thomas const char *oldpath, const char *newpath, struct got_patch *p,
566 49114f01 2022-03-22 thomas int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
568 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
569 814624e7 2022-03-22 thomas struct got_pathlist_head oldpaths, newpaths;
570 814624e7 2022-03-22 thomas struct got_pathlist_entry *pe;
571 bb2ad8ff 2022-03-13 thomas int file_renamed = 0;
572 49114f01 2022-03-22 thomas char *tmppath = NULL, *template = NULL, *parent = NULL;;
573 bb2ad8ff 2022-03-13 thomas FILE *tmp = NULL;
574 da09d8ed 2022-03-22 thomas mode_t mode = GOT_DEFAULT_FILE_MODE;
576 814624e7 2022-03-22 thomas TAILQ_INIT(&oldpaths);
577 814624e7 2022-03-22 thomas TAILQ_INIT(&newpaths);
579 814624e7 2022-03-22 thomas err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
581 10e55613 2022-03-22 thomas goto done;
582 814624e7 2022-03-22 thomas err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
584 814624e7 2022-03-22 thomas goto done;
586 814624e7 2022-03-22 thomas file_renamed = strcmp(oldpath, newpath);
588 bb2ad8ff 2022-03-13 thomas if (asprintf(&template, "%s/got-patch",
589 bb2ad8ff 2022-03-13 thomas got_worktree_get_root_path(worktree)) == -1) {
590 bb2ad8ff 2022-03-13 thomas err = got_error_from_errno(template);
591 069bbb86 2022-03-07 thomas goto done;
594 c71da4f7 2022-03-22 thomas if (!nop)
595 eaf99875 2022-03-22 thomas err = got_opentemp_named(&tmppath, &tmp, template);
597 bb2ad8ff 2022-03-13 thomas goto done;
598 da09d8ed 2022-03-22 thomas err = patch_file(p, oldpath, tmp, nop, &mode);
600 bb2ad8ff 2022-03-13 thomas goto done;
603 eaf99875 2022-03-22 thomas goto done;
605 eaf99875 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
606 814624e7 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
607 c71da4f7 2022-03-22 thomas 0, NULL, patch_delete, pa, repo, 0, 0);
608 eaf99875 2022-03-22 thomas goto done;
611 da09d8ed 2022-03-22 thomas if (fchmod(fileno(tmp), mode) == -1) {
612 da09d8ed 2022-03-22 thomas err = got_error_from_errno2("chmod", newpath);
613 da09d8ed 2022-03-22 thomas goto done;
616 bb2ad8ff 2022-03-13 thomas if (rename(tmppath, newpath) == -1) {
617 e0c1f81c 2022-03-22 thomas if (errno != ENOENT) {
618 e0c1f81c 2022-03-22 thomas err = got_error_from_errno3("rename", tmppath,
619 e0c1f81c 2022-03-22 thomas newpath);
620 e0c1f81c 2022-03-22 thomas goto done;
623 e0c1f81c 2022-03-22 thomas err = got_path_dirname(&parent, newpath);
624 e0c1f81c 2022-03-22 thomas if (err != NULL)
625 e0c1f81c 2022-03-22 thomas goto done;
626 e0c1f81c 2022-03-22 thomas err = got_path_mkdir(parent);
627 e0c1f81c 2022-03-22 thomas if (err != NULL)
628 e0c1f81c 2022-03-22 thomas goto done;
629 e0c1f81c 2022-03-22 thomas if (rename(tmppath, newpath) == -1) {
630 e0c1f81c 2022-03-22 thomas err = got_error_from_errno3("rename", tmppath,
631 e0c1f81c 2022-03-22 thomas newpath);
632 e0c1f81c 2022-03-22 thomas goto done;
636 bb2ad8ff 2022-03-13 thomas if (file_renamed) {
637 814624e7 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
638 c71da4f7 2022-03-22 thomas 0, NULL, patch_delete, pa, repo, 0, 0);
639 bb2ad8ff 2022-03-13 thomas if (err == NULL)
640 814624e7 2022-03-22 thomas err = got_worktree_schedule_add(worktree, &newpaths,
641 c71da4f7 2022-03-22 thomas patch_add, pa, repo, 1);
643 814624e7 2022-03-22 thomas unlink(newpath);
644 814624e7 2022-03-22 thomas } else if (p->old == NULL) {
645 814624e7 2022-03-22 thomas err = got_worktree_schedule_add(worktree, &newpaths,
646 c71da4f7 2022-03-22 thomas patch_add, pa, repo, 1);
648 814624e7 2022-03-22 thomas unlink(newpath);
650 49114f01 2022-03-22 thomas err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
654 814624e7 2022-03-22 thomas got_pathlist_free(&oldpaths);
655 814624e7 2022-03-22 thomas got_pathlist_free(&newpaths);
656 e0c1f81c 2022-03-22 thomas free(parent);
657 94af5a06 2022-03-08 thomas free(template);
658 069bbb86 2022-03-07 thomas if (tmppath != NULL)
659 069bbb86 2022-03-07 thomas unlink(tmppath);
660 069bbb86 2022-03-07 thomas free(tmppath);
661 069bbb86 2022-03-07 thomas return err;
664 069bbb86 2022-03-07 thomas const struct got_error *
665 069bbb86 2022-03-07 thomas got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
666 d9db2ff9 2022-04-16 thomas int nop, int strip, got_patch_progress_cb progress_cb, void *progress_arg,
667 c71da4f7 2022-03-22 thomas got_cancel_cb cancel_cb, void *cancel_arg)
669 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
670 814624e7 2022-03-22 thomas struct got_fileindex *fileindex = NULL;
671 49114f01 2022-03-22 thomas char *oldpath, *newpath;
672 069bbb86 2022-03-07 thomas struct imsgbuf *ibuf;
673 069bbb86 2022-03-07 thomas int imsg_fds[2] = {-1, -1};
674 49114f01 2022-03-22 thomas int done = 0, failed = 0;
675 069bbb86 2022-03-07 thomas pid_t pid;
677 069bbb86 2022-03-07 thomas ibuf = calloc(1, sizeof(*ibuf));
678 069bbb86 2022-03-07 thomas if (ibuf == NULL) {
679 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
680 069bbb86 2022-03-07 thomas goto done;
683 069bbb86 2022-03-07 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
684 069bbb86 2022-03-07 thomas err = got_error_from_errno("socketpair");
685 069bbb86 2022-03-07 thomas goto done;
688 069bbb86 2022-03-07 thomas pid = fork();
689 069bbb86 2022-03-07 thomas if (pid == -1) {
690 069bbb86 2022-03-07 thomas err = got_error_from_errno("fork");
691 069bbb86 2022-03-07 thomas goto done;
692 069bbb86 2022-03-07 thomas } else if (pid == 0) {
693 069bbb86 2022-03-07 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
695 069bbb86 2022-03-07 thomas /* not reached */
698 069bbb86 2022-03-07 thomas if (close(imsg_fds[1]) == -1) {
699 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
700 069bbb86 2022-03-07 thomas goto done;
702 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
703 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
705 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
708 069bbb86 2022-03-07 thomas goto done;
710 814624e7 2022-03-22 thomas err = got_worktree_patch_prepare(&fileindex, worktree);
712 814624e7 2022-03-22 thomas goto done;
714 069bbb86 2022-03-07 thomas while (!done && err == NULL) {
715 069bbb86 2022-03-07 thomas struct got_patch p;
716 49114f01 2022-03-22 thomas struct patch_args pa;
718 49114f01 2022-03-22 thomas pa.progress_cb = progress_cb;
719 49114f01 2022-03-22 thomas pa.progress_arg = progress_arg;
720 49114f01 2022-03-22 thomas pa.head = &p.head;
722 d9db2ff9 2022-04-16 thomas err = recv_patch(ibuf, &done, &p, strip);
723 069bbb86 2022-03-07 thomas if (err || done)
726 814624e7 2022-03-22 thomas err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
727 814624e7 2022-03-22 thomas &newpath, worktree, repo, fileindex);
728 814624e7 2022-03-22 thomas if (err == NULL)
729 814624e7 2022-03-22 thomas err = apply_patch(worktree, repo, oldpath, newpath,
730 814624e7 2022-03-22 thomas &p, nop, &pa, cancel_cb, cancel_arg);
731 49114f01 2022-03-22 thomas if (err != NULL) {
732 49114f01 2022-03-22 thomas failed = 1;
733 49114f01 2022-03-22 thomas /* recoverable errors */
734 49114f01 2022-03-22 thomas if (err->code == GOT_ERR_FILE_STATUS ||
735 49114f01 2022-03-22 thomas (err->code == GOT_ERR_ERRNO && errno == ENOENT))
736 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
737 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, err);
738 49114f01 2022-03-22 thomas else if (err->code == GOT_ERR_HUNK_FAILED)
739 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
740 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, NULL);
743 49114f01 2022-03-22 thomas free(oldpath);
744 49114f01 2022-03-22 thomas free(newpath);
745 069bbb86 2022-03-07 thomas patch_free(&p);
752 814624e7 2022-03-22 thomas if (fileindex)
753 814624e7 2022-03-22 thomas got_worktree_patch_complete(fileindex);
754 069bbb86 2022-03-07 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
755 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
756 069bbb86 2022-03-07 thomas if (ibuf != NULL)
757 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
758 069bbb86 2022-03-07 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
759 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
760 069bbb86 2022-03-07 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
761 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
762 49114f01 2022-03-22 thomas if (err == NULL && failed)
763 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_FAILED);
764 069bbb86 2022-03-07 thomas return err;