Blame


1 069bbb86 2022-03-07 thomas /*
2 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 069bbb86 2022-03-07 thomas *
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.
7 069bbb86 2022-03-07 thomas *
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.
15 069bbb86 2022-03-07 thomas *
16 069bbb86 2022-03-07 thomas * Apply patches.
17 069bbb86 2022-03-07 thomas *
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?
21 069bbb86 2022-03-07 thomas */
22 069bbb86 2022-03-07 thomas
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>
27 069bbb86 2022-03-07 thomas
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>
35 069bbb86 2022-03-07 thomas
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"
44 069bbb86 2022-03-07 thomas
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"
48 069bbb86 2022-03-07 thomas
49 069bbb86 2022-03-07 thomas #define MIN(a, b) ((a) < (b) ? (a) : (b))
50 069bbb86 2022-03-07 thomas
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;
55 ff7f34d3 2022-03-22 thomas int nonl;
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;
63 069bbb86 2022-03-07 thomas };
64 069bbb86 2022-03-07 thomas
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;
70 c71da4f7 2022-03-22 thomas };
71 c71da4f7 2022-03-22 thomas
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;
76 069bbb86 2022-03-07 thomas };
77 069bbb86 2022-03-07 thomas
78 069bbb86 2022-03-07 thomas static const struct got_error *
79 069bbb86 2022-03-07 thomas send_patch(struct imsgbuf *ibuf, int fd)
80 069bbb86 2022-03-07 thomas {
81 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
82 069bbb86 2022-03-07 thomas
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;
89 069bbb86 2022-03-07 thomas }
90 069bbb86 2022-03-07 thomas
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);
94 069bbb86 2022-03-07 thomas }
95 069bbb86 2022-03-07 thomas
96 069bbb86 2022-03-07 thomas return err;
97 069bbb86 2022-03-07 thomas }
98 069bbb86 2022-03-07 thomas
99 069bbb86 2022-03-07 thomas static void
100 069bbb86 2022-03-07 thomas patch_free(struct got_patch *p)
101 069bbb86 2022-03-07 thomas {
102 069bbb86 2022-03-07 thomas struct got_patch_hunk *h;
103 069bbb86 2022-03-07 thomas size_t i;
104 069bbb86 2022-03-07 thomas
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);
108 069bbb86 2022-03-07 thomas
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);
112 069bbb86 2022-03-07 thomas free(h);
113 069bbb86 2022-03-07 thomas }
114 069bbb86 2022-03-07 thomas
115 069bbb86 2022-03-07 thomas free(p->new);
116 069bbb86 2022-03-07 thomas free(p->old);
117 069bbb86 2022-03-07 thomas }
118 069bbb86 2022-03-07 thomas
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)
121 069bbb86 2022-03-07 thomas {
122 069bbb86 2022-03-07 thomas void *t;
123 069bbb86 2022-03-07 thomas size_t newcap;
124 069bbb86 2022-03-07 thomas
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;
134 069bbb86 2022-03-07 thomas }
135 069bbb86 2022-03-07 thomas
136 069bbb86 2022-03-07 thomas if ((t = strdup(line)) == NULL)
137 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
138 069bbb86 2022-03-07 thomas
139 069bbb86 2022-03-07 thomas h->lines[h->len++] = t;
140 069bbb86 2022-03-07 thomas return NULL;
141 069bbb86 2022-03-07 thomas }
142 069bbb86 2022-03-07 thomas
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)
145 069bbb86 2022-03-07 thomas {
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;
152 069bbb86 2022-03-07 thomas
153 069bbb86 2022-03-07 thomas memset(p, 0, sizeof(*p));
154 069bbb86 2022-03-07 thomas STAILQ_INIT(&p->head);
155 069bbb86 2022-03-07 thomas
156 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
157 069bbb86 2022-03-07 thomas if (err)
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;
162 069bbb86 2022-03-07 thomas }
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;
166 069bbb86 2022-03-07 thomas }
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;
171 069bbb86 2022-03-07 thomas }
172 069bbb86 2022-03-07 thomas memcpy(&patch, imsg.data, sizeof(patch));
173 d9db2ff9 2022-04-16 thomas
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;
179 d9db2ff9 2022-04-16 thomas
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);
183 d9db2ff9 2022-04-16 thomas if (err)
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);
187 d9db2ff9 2022-04-16 thomas if (err)
188 d9db2ff9 2022-04-16 thomas goto done;
189 069bbb86 2022-03-07 thomas }
190 d9db2ff9 2022-04-16 thomas
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);
193 d9db2ff9 2022-04-16 thomas if (err)
194 d9db2ff9 2022-04-16 thomas goto done;
195 d9db2ff9 2022-04-16 thomas }
196 d9db2ff9 2022-04-16 thomas
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;
200 64c9e565 2022-03-13 thomas }
201 069bbb86 2022-03-07 thomas
202 069bbb86 2022-03-07 thomas imsg_free(&imsg);
203 069bbb86 2022-03-07 thomas
204 069bbb86 2022-03-07 thomas for (;;) {
205 069bbb86 2022-03-07 thomas char *t;
206 069bbb86 2022-03-07 thomas
207 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
208 069bbb86 2022-03-07 thomas if (err)
209 069bbb86 2022-03-07 thomas return err;
210 069bbb86 2022-03-07 thomas
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;
218 ff7f34d3 2022-03-22 thomas }
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;
223 069bbb86 2022-03-07 thomas }
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;
228 069bbb86 2022-03-07 thomas }
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);
234 069bbb86 2022-03-07 thomas break;
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;
239 069bbb86 2022-03-07 thomas }
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;
246 069bbb86 2022-03-07 thomas }
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;
251 069bbb86 2022-03-07 thomas }
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;
256 ff7f34d3 2022-03-22 thomas else
257 ff7f34d3 2022-03-22 thomas err = pushline(h, t);
258 069bbb86 2022-03-07 thomas if (err)
259 069bbb86 2022-03-07 thomas goto done;
260 069bbb86 2022-03-07 thomas break;
261 069bbb86 2022-03-07 thomas default:
262 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
263 069bbb86 2022-03-07 thomas goto done;
264 069bbb86 2022-03-07 thomas }
265 069bbb86 2022-03-07 thomas
266 069bbb86 2022-03-07 thomas imsg_free(&imsg);
267 069bbb86 2022-03-07 thomas }
268 069bbb86 2022-03-07 thomas
269 069bbb86 2022-03-07 thomas done:
270 069bbb86 2022-03-07 thomas imsg_free(&imsg);
271 069bbb86 2022-03-07 thomas return err;
272 069bbb86 2022-03-07 thomas }
273 069bbb86 2022-03-07 thomas
274 069bbb86 2022-03-07 thomas /*
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.
277 069bbb86 2022-03-07 thomas */
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)
280 069bbb86 2022-03-07 thomas {
281 069bbb86 2022-03-07 thomas char buf[BUFSIZ];
282 069bbb86 2022-03-07 thomas size_t len, r, w;
283 069bbb86 2022-03-07 thomas
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");
286 069bbb86 2022-03-07 thomas
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);
302 069bbb86 2022-03-07 thomas }
303 069bbb86 2022-03-07 thomas }
304 069bbb86 2022-03-07 thomas return NULL;
305 069bbb86 2022-03-07 thomas }
306 069bbb86 2022-03-07 thomas
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)
309 069bbb86 2022-03-07 thomas {
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;
317 069bbb86 2022-03-07 thomas
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);
325 069bbb86 2022-03-07 thomas break;
326 069bbb86 2022-03-07 thomas }
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)++;
330 069bbb86 2022-03-07 thomas
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");
337 069bbb86 2022-03-07 thomas break;
338 069bbb86 2022-03-07 thomas }
339 069bbb86 2022-03-07 thomas match -= linelen;
340 069bbb86 2022-03-07 thomas match_lineno = (*lineno)-1;
341 069bbb86 2022-03-07 thomas }
342 069bbb86 2022-03-07 thomas
343 069bbb86 2022-03-07 thomas if (*lineno >= h->old_from && match != -1)
344 069bbb86 2022-03-07 thomas break;
345 069bbb86 2022-03-07 thomas }
346 069bbb86 2022-03-07 thomas
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");
352 069bbb86 2022-03-07 thomas }
353 069bbb86 2022-03-07 thomas
354 069bbb86 2022-03-07 thomas free(line);
355 069bbb86 2022-03-07 thomas return err;
356 069bbb86 2022-03-07 thomas }
357 069bbb86 2022-03-07 thomas
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)
360 069bbb86 2022-03-07 thomas {
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;
365 069bbb86 2022-03-07 thomas
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");
376 069bbb86 2022-03-07 thomas else
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;
380 069bbb86 2022-03-07 thomas }
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;
386 069bbb86 2022-03-07 thomas }
387 069bbb86 2022-03-07 thomas break;
388 069bbb86 2022-03-07 thomas }
389 069bbb86 2022-03-07 thomas }
390 069bbb86 2022-03-07 thomas
391 069bbb86 2022-03-07 thomas done:
392 069bbb86 2022-03-07 thomas free(line);
393 069bbb86 2022-03-07 thomas return err;
394 069bbb86 2022-03-07 thomas }
395 069bbb86 2022-03-07 thomas
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)
398 069bbb86 2022-03-07 thomas {
399 069bbb86 2022-03-07 thomas size_t i = 0;
400 069bbb86 2022-03-07 thomas
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)++;
409 069bbb86 2022-03-07 thomas break;
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");
417 ff7f34d3 2022-03-22 thomas }
418 069bbb86 2022-03-07 thomas break;
419 069bbb86 2022-03-07 thomas }
420 069bbb86 2022-03-07 thomas }
421 069bbb86 2022-03-07 thomas return NULL;
422 46e6bdd4 2022-03-22 thomas }
423 46e6bdd4 2022-03-22 thomas
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)
427 bb2ad8ff 2022-03-13 thomas {
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;
437 94af5a06 2022-03-08 thomas
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);
442 c71da4f7 2022-03-22 thomas if (nop)
443 eaf99875 2022-03-22 thomas return NULL;
444 afbf14b3 2022-03-22 thomas return apply_hunk(tmp, h, &lineno);
445 069bbb86 2022-03-07 thomas }
446 069bbb86 2022-03-07 thomas
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;
450 069bbb86 2022-03-07 thomas }
451 069bbb86 2022-03-07 thomas
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;
455 da09d8ed 2022-03-22 thomas }
456 da09d8ed 2022-03-22 thomas *mode = sb.st_mode;
457 da09d8ed 2022-03-22 thomas
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)
461 bb2ad8ff 2022-03-13 thomas break;
462 bb2ad8ff 2022-03-13 thomas
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;
474 069bbb86 2022-03-07 thomas
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) {
477 069bbb86 2022-03-07 thomas /*
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.
480 069bbb86 2022-03-07 thomas */
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;
484 069bbb86 2022-03-07 thomas }
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;
489 069bbb86 2022-03-07 thomas }
490 069bbb86 2022-03-07 thomas lineno++;
491 069bbb86 2022-03-07 thomas goto tryagain;
492 069bbb86 2022-03-07 thomas }
493 069bbb86 2022-03-07 thomas if (err != NULL)
494 069bbb86 2022-03-07 thomas goto done;
495 069bbb86 2022-03-07 thomas
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;
498 49114f01 2022-03-22 thomas
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;
503 f5f21873 2022-04-16 thomas
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;
508 069bbb86 2022-03-07 thomas }
509 069bbb86 2022-03-07 thomas }
510 069bbb86 2022-03-07 thomas
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);
517 bb2ad8ff 2022-03-13 thomas
518 bb2ad8ff 2022-03-13 thomas done:
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;
522 10e55613 2022-03-22 thomas }
523 10e55613 2022-03-22 thomas
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)
527 49114f01 2022-03-22 thomas {
528 49114f01 2022-03-22 thomas const struct got_error *err;
529 49114f01 2022-03-22 thomas struct got_patch_hunk *h;
530 49114f01 2022-03-22 thomas
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);
533 49114f01 2022-03-22 thomas if (err)
534 49114f01 2022-03-22 thomas return err;
535 49114f01 2022-03-22 thomas
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;
539 49114f01 2022-03-22 thomas
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);
543 49114f01 2022-03-22 thomas if (err)
544 49114f01 2022-03-22 thomas return err;
545 49114f01 2022-03-22 thomas }
546 49114f01 2022-03-22 thomas
547 49114f01 2022-03-22 thomas return NULL;
548 49114f01 2022-03-22 thomas }
549 49114f01 2022-03-22 thomas
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)
553 c71da4f7 2022-03-22 thomas {
554 49114f01 2022-03-22 thomas return report_progress(arg, path, NULL, status, NULL);
555 c71da4f7 2022-03-22 thomas }
556 c71da4f7 2022-03-22 thomas
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)
559 c71da4f7 2022-03-22 thomas {
560 49114f01 2022-03-22 thomas return report_progress(arg, NULL, path, status, NULL);
561 c71da4f7 2022-03-22 thomas }
562 c71da4f7 2022-03-22 thomas
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)
567 bb2ad8ff 2022-03-13 thomas {
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;
575 bb2ad8ff 2022-03-13 thomas
576 814624e7 2022-03-22 thomas TAILQ_INIT(&oldpaths);
577 814624e7 2022-03-22 thomas TAILQ_INIT(&newpaths);
578 10e55613 2022-03-22 thomas
579 814624e7 2022-03-22 thomas err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
580 10e55613 2022-03-22 thomas if (err)
581 10e55613 2022-03-22 thomas goto done;
582 814624e7 2022-03-22 thomas err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
583 814624e7 2022-03-22 thomas if (err)
584 814624e7 2022-03-22 thomas goto done;
585 10e55613 2022-03-22 thomas
586 814624e7 2022-03-22 thomas file_renamed = strcmp(oldpath, newpath);
587 42d9d68e 2022-03-13 thomas
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;
592 069bbb86 2022-03-07 thomas }
593 069bbb86 2022-03-07 thomas
594 c71da4f7 2022-03-22 thomas if (!nop)
595 eaf99875 2022-03-22 thomas err = got_opentemp_named(&tmppath, &tmp, template);
596 bb2ad8ff 2022-03-13 thomas if (err)
597 bb2ad8ff 2022-03-13 thomas goto done;
598 da09d8ed 2022-03-22 thomas err = patch_file(p, oldpath, tmp, nop, &mode);
599 bb2ad8ff 2022-03-13 thomas if (err)
600 bb2ad8ff 2022-03-13 thomas goto done;
601 bb2ad8ff 2022-03-13 thomas
602 c71da4f7 2022-03-22 thomas if (nop)
603 eaf99875 2022-03-22 thomas goto done;
604 eaf99875 2022-03-22 thomas
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;
609 eaf99875 2022-03-22 thomas }
610 eaf99875 2022-03-22 thomas
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;
614 da09d8ed 2022-03-22 thomas }
615 da09d8ed 2022-03-22 thomas
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;
621 e0c1f81c 2022-03-22 thomas }
622 e0c1f81c 2022-03-22 thomas
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;
633 e0c1f81c 2022-03-22 thomas }
634 bb2ad8ff 2022-03-13 thomas }
635 bb2ad8ff 2022-03-13 thomas
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);
642 814624e7 2022-03-22 thomas if (err)
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);
647 814624e7 2022-03-22 thomas if (err)
648 814624e7 2022-03-22 thomas unlink(newpath);
649 814624e7 2022-03-22 thomas } else
650 49114f01 2022-03-22 thomas err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
651 49114f01 2022-03-22 thomas NULL);
652 bb2ad8ff 2022-03-13 thomas
653 069bbb86 2022-03-07 thomas done:
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;
662 069bbb86 2022-03-07 thomas }
663 069bbb86 2022-03-07 thomas
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)
668 069bbb86 2022-03-07 thomas {
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;
676 069bbb86 2022-03-07 thomas
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;
681 069bbb86 2022-03-07 thomas }
682 069bbb86 2022-03-07 thomas
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;
686 069bbb86 2022-03-07 thomas }
687 069bbb86 2022-03-07 thomas
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,
694 069bbb86 2022-03-07 thomas NULL);
695 069bbb86 2022-03-07 thomas /* not reached */
696 069bbb86 2022-03-07 thomas }
697 069bbb86 2022-03-07 thomas
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;
701 069bbb86 2022-03-07 thomas }
702 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
703 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
704 069bbb86 2022-03-07 thomas
705 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
706 069bbb86 2022-03-07 thomas fd = -1;
707 069bbb86 2022-03-07 thomas if (err)
708 069bbb86 2022-03-07 thomas goto done;
709 069bbb86 2022-03-07 thomas
710 814624e7 2022-03-22 thomas err = got_worktree_patch_prepare(&fileindex, worktree);
711 814624e7 2022-03-22 thomas if (err)
712 814624e7 2022-03-22 thomas goto done;
713 814624e7 2022-03-22 thomas
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;
717 069bbb86 2022-03-07 thomas
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;
721 49114f01 2022-03-22 thomas
722 d9db2ff9 2022-04-16 thomas err = recv_patch(ibuf, &done, &p, strip);
723 069bbb86 2022-03-07 thomas if (err || done)
724 069bbb86 2022-03-07 thomas break;
725 069bbb86 2022-03-07 thomas
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);
741 49114f01 2022-03-22 thomas }
742 49114f01 2022-03-22 thomas
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);
746 49114f01 2022-03-22 thomas
747 069bbb86 2022-03-07 thomas if (err)
748 069bbb86 2022-03-07 thomas break;
749 069bbb86 2022-03-07 thomas }
750 069bbb86 2022-03-07 thomas
751 069bbb86 2022-03-07 thomas done:
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;
765 069bbb86 2022-03-07 thomas }