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 069bbb86 2022-03-07 thomas recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
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 069bbb86 2022-03-07 thomas if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
174 069bbb86 2022-03-07 thomas err = got_error_from_errno("strdup");
175 069bbb86 2022-03-07 thomas goto done;
176 069bbb86 2022-03-07 thomas }
177 069bbb86 2022-03-07 thomas if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
178 069bbb86 2022-03-07 thomas err = got_error_from_errno("strdup");
179 069bbb86 2022-03-07 thomas goto done;
180 069bbb86 2022-03-07 thomas }
181 64c9e565 2022-03-13 thomas if (p->old == NULL && p->new == NULL) {
182 64c9e565 2022-03-13 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
183 64c9e565 2022-03-13 thomas goto done;
184 64c9e565 2022-03-13 thomas }
185 069bbb86 2022-03-07 thomas
186 069bbb86 2022-03-07 thomas imsg_free(&imsg);
187 069bbb86 2022-03-07 thomas
188 069bbb86 2022-03-07 thomas for (;;) {
189 069bbb86 2022-03-07 thomas char *t;
190 069bbb86 2022-03-07 thomas
191 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
192 069bbb86 2022-03-07 thomas if (err)
193 069bbb86 2022-03-07 thomas return err;
194 069bbb86 2022-03-07 thomas
195 069bbb86 2022-03-07 thomas switch (imsg.hdr.type) {
196 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_DONE:
197 069bbb86 2022-03-07 thomas goto done;
198 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_HUNK:
199 ff7f34d3 2022-03-22 thomas if (h != NULL && h->nonl) {
200 ff7f34d3 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
201 ff7f34d3 2022-03-22 thomas goto done;
202 ff7f34d3 2022-03-22 thomas }
203 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
204 069bbb86 2022-03-07 thomas if (datalen != sizeof(hdr)) {
205 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
206 069bbb86 2022-03-07 thomas goto done;
207 069bbb86 2022-03-07 thomas }
208 069bbb86 2022-03-07 thomas memcpy(&hdr, imsg.data, sizeof(hdr));
209 069bbb86 2022-03-07 thomas if ((h = calloc(1, sizeof(*h))) == NULL) {
210 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
211 069bbb86 2022-03-07 thomas goto done;
212 069bbb86 2022-03-07 thomas }
213 069bbb86 2022-03-07 thomas h->old_from = hdr.oldfrom;
214 069bbb86 2022-03-07 thomas h->old_lines = hdr.oldlines;
215 069bbb86 2022-03-07 thomas h->new_from = hdr.newfrom;
216 069bbb86 2022-03-07 thomas h->new_lines = hdr.newlines;
217 069bbb86 2022-03-07 thomas STAILQ_INSERT_TAIL(&p->head, h, entries);
218 069bbb86 2022-03-07 thomas break;
219 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_LINE:
220 069bbb86 2022-03-07 thomas if (h == NULL) {
221 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
222 069bbb86 2022-03-07 thomas goto done;
223 069bbb86 2022-03-07 thomas }
224 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
225 069bbb86 2022-03-07 thomas t = imsg.data;
226 ff7f34d3 2022-03-22 thomas /* at least one char */
227 069bbb86 2022-03-07 thomas if (datalen < 2 || t[datalen-1] != '\0') {
228 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
229 069bbb86 2022-03-07 thomas goto done;
230 069bbb86 2022-03-07 thomas }
231 ff7f34d3 2022-03-22 thomas if (*t != ' ' && *t != '-' && *t != '+' &&
232 ff7f34d3 2022-03-22 thomas *t != '\\') {
233 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
234 069bbb86 2022-03-07 thomas goto done;
235 069bbb86 2022-03-07 thomas }
236 ff7f34d3 2022-03-22 thomas if (h->nonl)
237 ff7f34d3 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
238 ff7f34d3 2022-03-22 thomas if (*t == '\\')
239 ff7f34d3 2022-03-22 thomas h->nonl = 1;
240 ff7f34d3 2022-03-22 thomas else
241 ff7f34d3 2022-03-22 thomas err = pushline(h, t);
242 069bbb86 2022-03-07 thomas if (err)
243 069bbb86 2022-03-07 thomas goto done;
244 069bbb86 2022-03-07 thomas break;
245 069bbb86 2022-03-07 thomas default:
246 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
247 069bbb86 2022-03-07 thomas goto done;
248 069bbb86 2022-03-07 thomas }
249 069bbb86 2022-03-07 thomas
250 069bbb86 2022-03-07 thomas imsg_free(&imsg);
251 069bbb86 2022-03-07 thomas }
252 069bbb86 2022-03-07 thomas
253 069bbb86 2022-03-07 thomas done:
254 069bbb86 2022-03-07 thomas imsg_free(&imsg);
255 069bbb86 2022-03-07 thomas return err;
256 069bbb86 2022-03-07 thomas }
257 069bbb86 2022-03-07 thomas
258 069bbb86 2022-03-07 thomas /*
259 069bbb86 2022-03-07 thomas * Copy data from orig starting at copypos until pos into tmp.
260 069bbb86 2022-03-07 thomas * If pos is -1, copy until EOF.
261 069bbb86 2022-03-07 thomas */
262 069bbb86 2022-03-07 thomas static const struct got_error *
263 069bbb86 2022-03-07 thomas copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
264 069bbb86 2022-03-07 thomas {
265 069bbb86 2022-03-07 thomas char buf[BUFSIZ];
266 069bbb86 2022-03-07 thomas size_t len, r, w;
267 069bbb86 2022-03-07 thomas
268 069bbb86 2022-03-07 thomas if (fseek(orig, copypos, SEEK_SET) == -1)
269 069bbb86 2022-03-07 thomas return got_error_from_errno("fseek");
270 069bbb86 2022-03-07 thomas
271 069bbb86 2022-03-07 thomas while (pos == -1 || copypos < pos) {
272 069bbb86 2022-03-07 thomas len = sizeof(buf);
273 069bbb86 2022-03-07 thomas if (pos > 0)
274 069bbb86 2022-03-07 thomas len = MIN(len, (size_t)pos - copypos);
275 069bbb86 2022-03-07 thomas r = fread(buf, 1, len, orig);
276 069bbb86 2022-03-07 thomas if (r != len && ferror(orig))
277 069bbb86 2022-03-07 thomas return got_error_from_errno("fread");
278 069bbb86 2022-03-07 thomas w = fwrite(buf, 1, r, tmp);
279 069bbb86 2022-03-07 thomas if (w != r)
280 069bbb86 2022-03-07 thomas return got_error_from_errno("fwrite");
281 069bbb86 2022-03-07 thomas copypos += len;
282 069bbb86 2022-03-07 thomas if (r != len && feof(orig)) {
283 069bbb86 2022-03-07 thomas if (pos == -1)
284 069bbb86 2022-03-07 thomas return NULL;
285 49114f01 2022-03-22 thomas return got_error(GOT_ERR_HUNK_FAILED);
286 069bbb86 2022-03-07 thomas }
287 069bbb86 2022-03-07 thomas }
288 069bbb86 2022-03-07 thomas return NULL;
289 069bbb86 2022-03-07 thomas }
290 069bbb86 2022-03-07 thomas
291 069bbb86 2022-03-07 thomas static const struct got_error *
292 122e1611 2022-03-22 thomas locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
293 069bbb86 2022-03-07 thomas {
294 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
295 069bbb86 2022-03-07 thomas char *line = NULL;
296 069bbb86 2022-03-07 thomas char mode = *h->lines[0];
297 069bbb86 2022-03-07 thomas size_t linesize = 0;
298 069bbb86 2022-03-07 thomas ssize_t linelen;
299 069bbb86 2022-03-07 thomas off_t match = -1;
300 069bbb86 2022-03-07 thomas long match_lineno = -1;
301 069bbb86 2022-03-07 thomas
302 069bbb86 2022-03-07 thomas for (;;) {
303 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
304 069bbb86 2022-03-07 thomas if (linelen == -1) {
305 069bbb86 2022-03-07 thomas if (ferror(orig))
306 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
307 069bbb86 2022-03-07 thomas else if (match == -1)
308 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
309 069bbb86 2022-03-07 thomas break;
310 069bbb86 2022-03-07 thomas }
311 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
312 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
313 069bbb86 2022-03-07 thomas (*lineno)++;
314 069bbb86 2022-03-07 thomas
315 17d6446a 2022-03-22 thomas if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
316 17d6446a 2022-03-22 thomas (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
317 069bbb86 2022-03-07 thomas (mode == '+' && *lineno == h->old_from)) {
318 069bbb86 2022-03-07 thomas match = ftello(orig);
319 069bbb86 2022-03-07 thomas if (match == -1) {
320 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
321 069bbb86 2022-03-07 thomas break;
322 069bbb86 2022-03-07 thomas }
323 069bbb86 2022-03-07 thomas match -= linelen;
324 069bbb86 2022-03-07 thomas match_lineno = (*lineno)-1;
325 069bbb86 2022-03-07 thomas }
326 069bbb86 2022-03-07 thomas
327 069bbb86 2022-03-07 thomas if (*lineno >= h->old_from && match != -1)
328 069bbb86 2022-03-07 thomas break;
329 069bbb86 2022-03-07 thomas }
330 069bbb86 2022-03-07 thomas
331 069bbb86 2022-03-07 thomas if (err == NULL) {
332 122e1611 2022-03-22 thomas *pos = match;
333 069bbb86 2022-03-07 thomas *lineno = match_lineno;
334 069bbb86 2022-03-07 thomas if (fseek(orig, match, SEEK_SET) == -1)
335 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
336 069bbb86 2022-03-07 thomas }
337 069bbb86 2022-03-07 thomas
338 069bbb86 2022-03-07 thomas free(line);
339 069bbb86 2022-03-07 thomas return err;
340 069bbb86 2022-03-07 thomas }
341 069bbb86 2022-03-07 thomas
342 069bbb86 2022-03-07 thomas static const struct got_error *
343 069bbb86 2022-03-07 thomas test_hunk(FILE *orig, struct got_patch_hunk *h)
344 069bbb86 2022-03-07 thomas {
345 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
346 069bbb86 2022-03-07 thomas char *line = NULL;
347 069bbb86 2022-03-07 thomas size_t linesize = 0, i = 0;
348 069bbb86 2022-03-07 thomas ssize_t linelen;
349 069bbb86 2022-03-07 thomas
350 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
351 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
352 069bbb86 2022-03-07 thomas case '+':
353 069bbb86 2022-03-07 thomas continue;
354 069bbb86 2022-03-07 thomas case ' ':
355 069bbb86 2022-03-07 thomas case '-':
356 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
357 069bbb86 2022-03-07 thomas if (linelen == -1) {
358 069bbb86 2022-03-07 thomas if (ferror(orig))
359 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
360 069bbb86 2022-03-07 thomas else
361 069bbb86 2022-03-07 thomas err = got_error(
362 49114f01 2022-03-22 thomas GOT_ERR_HUNK_FAILED);
363 069bbb86 2022-03-07 thomas goto done;
364 069bbb86 2022-03-07 thomas }
365 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
366 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
367 17d6446a 2022-03-22 thomas if (strcmp(h->lines[i] + 1, line)) {
368 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_HUNK_FAILED);
369 069bbb86 2022-03-07 thomas goto done;
370 069bbb86 2022-03-07 thomas }
371 069bbb86 2022-03-07 thomas break;
372 069bbb86 2022-03-07 thomas }
373 069bbb86 2022-03-07 thomas }
374 069bbb86 2022-03-07 thomas
375 069bbb86 2022-03-07 thomas done:
376 069bbb86 2022-03-07 thomas free(line);
377 069bbb86 2022-03-07 thomas return err;
378 069bbb86 2022-03-07 thomas }
379 069bbb86 2022-03-07 thomas
380 069bbb86 2022-03-07 thomas static const struct got_error *
381 069bbb86 2022-03-07 thomas apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
382 069bbb86 2022-03-07 thomas {
383 069bbb86 2022-03-07 thomas size_t i = 0;
384 069bbb86 2022-03-07 thomas
385 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
386 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
387 069bbb86 2022-03-07 thomas case ' ':
388 ff7f34d3 2022-03-22 thomas if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
389 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
390 069bbb86 2022-03-07 thomas /* fallthrough */
391 069bbb86 2022-03-07 thomas case '-':
392 069bbb86 2022-03-07 thomas (*lineno)++;
393 069bbb86 2022-03-07 thomas break;
394 069bbb86 2022-03-07 thomas case '+':
395 17d6446a 2022-03-22 thomas if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
396 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
397 ff7f34d3 2022-03-22 thomas if (i != h->len - 1 || !h->nonl) {
398 ff7f34d3 2022-03-22 thomas if (fprintf(tmp, "\n") < 0)
399 ff7f34d3 2022-03-22 thomas return got_error_from_errno(
400 ff7f34d3 2022-03-22 thomas "fprintf");
401 ff7f34d3 2022-03-22 thomas }
402 069bbb86 2022-03-07 thomas break;
403 069bbb86 2022-03-07 thomas }
404 069bbb86 2022-03-07 thomas }
405 069bbb86 2022-03-07 thomas return NULL;
406 46e6bdd4 2022-03-22 thomas }
407 46e6bdd4 2022-03-22 thomas
408 46e6bdd4 2022-03-22 thomas static const struct got_error *
409 da09d8ed 2022-03-22 thomas patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
410 da09d8ed 2022-03-22 thomas mode_t *mode)
411 bb2ad8ff 2022-03-13 thomas {
412 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
413 bb2ad8ff 2022-03-13 thomas struct got_patch_hunk *h;
414 da09d8ed 2022-03-22 thomas struct stat sb;
415 bb2ad8ff 2022-03-13 thomas long lineno = 0;
416 bb2ad8ff 2022-03-13 thomas FILE *orig;
417 bb2ad8ff 2022-03-13 thomas off_t copypos, pos;
418 bb2ad8ff 2022-03-13 thomas char *line = NULL;
419 bb2ad8ff 2022-03-13 thomas size_t linesize = 0;
420 bb2ad8ff 2022-03-13 thomas ssize_t linelen;
421 94af5a06 2022-03-08 thomas
422 069bbb86 2022-03-07 thomas if (p->old == NULL) { /* create */
423 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
424 bb2ad8ff 2022-03-13 thomas if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
425 bb2ad8ff 2022-03-13 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
426 c71da4f7 2022-03-22 thomas if (nop)
427 eaf99875 2022-03-22 thomas return NULL;
428 afbf14b3 2022-03-22 thomas return apply_hunk(tmp, h, &lineno);
429 069bbb86 2022-03-07 thomas }
430 069bbb86 2022-03-07 thomas
431 069bbb86 2022-03-07 thomas if ((orig = fopen(path, "r")) == NULL) {
432 069bbb86 2022-03-07 thomas err = got_error_from_errno2("fopen", path);
433 069bbb86 2022-03-07 thomas goto done;
434 069bbb86 2022-03-07 thomas }
435 069bbb86 2022-03-07 thomas
436 da09d8ed 2022-03-22 thomas if (fstat(fileno(orig), &sb) == -1) {
437 da09d8ed 2022-03-22 thomas err = got_error_from_errno("fstat");
438 da09d8ed 2022-03-22 thomas goto done;
439 da09d8ed 2022-03-22 thomas }
440 da09d8ed 2022-03-22 thomas *mode = sb.st_mode;
441 da09d8ed 2022-03-22 thomas
442 069bbb86 2022-03-07 thomas copypos = 0;
443 069bbb86 2022-03-07 thomas STAILQ_FOREACH(h, &p->head, entries) {
444 bb2ad8ff 2022-03-13 thomas if (h->lines == NULL)
445 bb2ad8ff 2022-03-13 thomas break;
446 bb2ad8ff 2022-03-13 thomas
447 069bbb86 2022-03-07 thomas tryagain:
448 122e1611 2022-03-22 thomas err = locate_hunk(orig, h, &pos, &lineno);
449 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
450 49114f01 2022-03-22 thomas h->err = err;
451 069bbb86 2022-03-07 thomas if (err != NULL)
452 069bbb86 2022-03-07 thomas goto done;
453 c71da4f7 2022-03-22 thomas if (!nop)
454 eaf99875 2022-03-22 thomas err = copy(tmp, orig, copypos, pos);
455 069bbb86 2022-03-07 thomas if (err != NULL)
456 069bbb86 2022-03-07 thomas goto done;
457 069bbb86 2022-03-07 thomas copypos = pos;
458 069bbb86 2022-03-07 thomas
459 069bbb86 2022-03-07 thomas err = test_hunk(orig, h);
460 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
461 069bbb86 2022-03-07 thomas /*
462 069bbb86 2022-03-07 thomas * try to apply the hunk again starting the search
463 069bbb86 2022-03-07 thomas * after the previous partial match.
464 069bbb86 2022-03-07 thomas */
465 069bbb86 2022-03-07 thomas if (fseek(orig, pos, SEEK_SET) == -1) {
466 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
467 069bbb86 2022-03-07 thomas goto done;
468 069bbb86 2022-03-07 thomas }
469 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
470 069bbb86 2022-03-07 thomas if (linelen == -1) {
471 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
472 069bbb86 2022-03-07 thomas goto done;
473 069bbb86 2022-03-07 thomas }
474 069bbb86 2022-03-07 thomas lineno++;
475 069bbb86 2022-03-07 thomas goto tryagain;
476 069bbb86 2022-03-07 thomas }
477 069bbb86 2022-03-07 thomas if (err != NULL)
478 069bbb86 2022-03-07 thomas goto done;
479 069bbb86 2022-03-07 thomas
480 49114f01 2022-03-22 thomas if (lineno + 1 != h->old_from)
481 49114f01 2022-03-22 thomas h->offset = lineno + 1 - h->old_from;
482 49114f01 2022-03-22 thomas
483 c71da4f7 2022-03-22 thomas if (!nop)
484 bfc91212 2022-03-13 thomas err = apply_hunk(tmp, h, &lineno);
485 069bbb86 2022-03-07 thomas if (err != NULL)
486 069bbb86 2022-03-07 thomas goto done;
487 069bbb86 2022-03-07 thomas
488 069bbb86 2022-03-07 thomas copypos = ftello(orig);
489 069bbb86 2022-03-07 thomas if (copypos == -1) {
490 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
491 069bbb86 2022-03-07 thomas goto done;
492 069bbb86 2022-03-07 thomas }
493 069bbb86 2022-03-07 thomas }
494 069bbb86 2022-03-07 thomas
495 49114f01 2022-03-22 thomas if (p->new == NULL && sb.st_size != copypos) {
496 49114f01 2022-03-22 thomas h = STAILQ_FIRST(&p->head);
497 49114f01 2022-03-22 thomas h->err = got_error(GOT_ERR_HUNK_FAILED);
498 49114f01 2022-03-22 thomas err = h->err;
499 49114f01 2022-03-22 thomas } else if (!nop && !feof(orig))
500 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, -1);
501 bb2ad8ff 2022-03-13 thomas
502 bb2ad8ff 2022-03-13 thomas done:
503 bb2ad8ff 2022-03-13 thomas if (orig != NULL)
504 bb2ad8ff 2022-03-13 thomas fclose(orig);
505 10e55613 2022-03-22 thomas return err;
506 10e55613 2022-03-22 thomas }
507 10e55613 2022-03-22 thomas
508 10e55613 2022-03-22 thomas static const struct got_error *
509 49114f01 2022-03-22 thomas report_progress(struct patch_args *pa, const char *old, const char *new,
510 49114f01 2022-03-22 thomas unsigned char status, const struct got_error *orig_error)
511 49114f01 2022-03-22 thomas {
512 49114f01 2022-03-22 thomas const struct got_error *err;
513 49114f01 2022-03-22 thomas struct got_patch_hunk *h;
514 49114f01 2022-03-22 thomas
515 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, status,
516 49114f01 2022-03-22 thomas orig_error, 0, 0, 0, 0, 0, NULL);
517 49114f01 2022-03-22 thomas if (err)
518 49114f01 2022-03-22 thomas return err;
519 49114f01 2022-03-22 thomas
520 49114f01 2022-03-22 thomas STAILQ_FOREACH(h, pa->head, entries) {
521 49114f01 2022-03-22 thomas if (h->offset == 0 && h->err == NULL)
522 49114f01 2022-03-22 thomas continue;
523 49114f01 2022-03-22 thomas
524 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
525 49114f01 2022-03-22 thomas h->old_from, h->old_lines, h->new_from, h->new_lines,
526 49114f01 2022-03-22 thomas h->offset, h->err);
527 49114f01 2022-03-22 thomas if (err)
528 49114f01 2022-03-22 thomas return err;
529 49114f01 2022-03-22 thomas }
530 49114f01 2022-03-22 thomas
531 49114f01 2022-03-22 thomas return NULL;
532 49114f01 2022-03-22 thomas }
533 49114f01 2022-03-22 thomas
534 49114f01 2022-03-22 thomas static const struct got_error *
535 c71da4f7 2022-03-22 thomas patch_delete(void *arg, unsigned char status, unsigned char staged_status,
536 c71da4f7 2022-03-22 thomas const char *path)
537 c71da4f7 2022-03-22 thomas {
538 49114f01 2022-03-22 thomas return report_progress(arg, path, NULL, status, NULL);
539 c71da4f7 2022-03-22 thomas }
540 c71da4f7 2022-03-22 thomas
541 c71da4f7 2022-03-22 thomas static const struct got_error *
542 c71da4f7 2022-03-22 thomas patch_add(void *arg, unsigned char status, const char *path)
543 c71da4f7 2022-03-22 thomas {
544 49114f01 2022-03-22 thomas return report_progress(arg, NULL, path, status, NULL);
545 c71da4f7 2022-03-22 thomas }
546 c71da4f7 2022-03-22 thomas
547 c71da4f7 2022-03-22 thomas static const struct got_error *
548 bb2ad8ff 2022-03-13 thomas apply_patch(struct got_worktree *worktree, struct got_repository *repo,
549 49114f01 2022-03-22 thomas const char *oldpath, const char *newpath, struct got_patch *p,
550 49114f01 2022-03-22 thomas int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
551 bb2ad8ff 2022-03-13 thomas {
552 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
553 814624e7 2022-03-22 thomas struct got_pathlist_head oldpaths, newpaths;
554 814624e7 2022-03-22 thomas struct got_pathlist_entry *pe;
555 bb2ad8ff 2022-03-13 thomas int file_renamed = 0;
556 49114f01 2022-03-22 thomas char *tmppath = NULL, *template = NULL, *parent = NULL;;
557 bb2ad8ff 2022-03-13 thomas FILE *tmp = NULL;
558 da09d8ed 2022-03-22 thomas mode_t mode = GOT_DEFAULT_FILE_MODE;
559 bb2ad8ff 2022-03-13 thomas
560 814624e7 2022-03-22 thomas TAILQ_INIT(&oldpaths);
561 814624e7 2022-03-22 thomas TAILQ_INIT(&newpaths);
562 10e55613 2022-03-22 thomas
563 814624e7 2022-03-22 thomas err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
564 10e55613 2022-03-22 thomas if (err)
565 10e55613 2022-03-22 thomas goto done;
566 814624e7 2022-03-22 thomas err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
567 814624e7 2022-03-22 thomas if (err)
568 814624e7 2022-03-22 thomas goto done;
569 10e55613 2022-03-22 thomas
570 814624e7 2022-03-22 thomas file_renamed = strcmp(oldpath, newpath);
571 814624e7 2022-03-22 thomas
572 46e6bdd4 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
573 46e6bdd4 2022-03-22 thomas /*
574 46e6bdd4 2022-03-22 thomas * special case: delete a file. don't try to match
575 46e6bdd4 2022-03-22 thomas * the lines but just schedule the removal.
576 46e6bdd4 2022-03-22 thomas */
577 10e55613 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
578 569e9b0b 2022-03-22 thomas 0, NULL, patch_delete, pa, repo, 0, 0);
579 42d9d68e 2022-03-13 thomas goto done;
580 46e6bdd4 2022-03-22 thomas }
581 42d9d68e 2022-03-13 thomas
582 bb2ad8ff 2022-03-13 thomas if (asprintf(&template, "%s/got-patch",
583 bb2ad8ff 2022-03-13 thomas got_worktree_get_root_path(worktree)) == -1) {
584 bb2ad8ff 2022-03-13 thomas err = got_error_from_errno(template);
585 069bbb86 2022-03-07 thomas goto done;
586 069bbb86 2022-03-07 thomas }
587 069bbb86 2022-03-07 thomas
588 c71da4f7 2022-03-22 thomas if (!nop)
589 eaf99875 2022-03-22 thomas err = got_opentemp_named(&tmppath, &tmp, template);
590 bb2ad8ff 2022-03-13 thomas if (err)
591 bb2ad8ff 2022-03-13 thomas goto done;
592 da09d8ed 2022-03-22 thomas err = patch_file(p, oldpath, tmp, nop, &mode);
593 bb2ad8ff 2022-03-13 thomas if (err)
594 bb2ad8ff 2022-03-13 thomas goto done;
595 bb2ad8ff 2022-03-13 thomas
596 c71da4f7 2022-03-22 thomas if (nop)
597 eaf99875 2022-03-22 thomas goto done;
598 eaf99875 2022-03-22 thomas
599 eaf99875 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
600 814624e7 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
601 c71da4f7 2022-03-22 thomas 0, NULL, patch_delete, pa, repo, 0, 0);
602 eaf99875 2022-03-22 thomas goto done;
603 eaf99875 2022-03-22 thomas }
604 eaf99875 2022-03-22 thomas
605 da09d8ed 2022-03-22 thomas if (fchmod(fileno(tmp), mode) == -1) {
606 da09d8ed 2022-03-22 thomas err = got_error_from_errno2("chmod", newpath);
607 da09d8ed 2022-03-22 thomas goto done;
608 da09d8ed 2022-03-22 thomas }
609 da09d8ed 2022-03-22 thomas
610 bb2ad8ff 2022-03-13 thomas if (rename(tmppath, newpath) == -1) {
611 e0c1f81c 2022-03-22 thomas if (errno != ENOENT) {
612 e0c1f81c 2022-03-22 thomas err = got_error_from_errno3("rename", tmppath,
613 e0c1f81c 2022-03-22 thomas newpath);
614 e0c1f81c 2022-03-22 thomas goto done;
615 e0c1f81c 2022-03-22 thomas }
616 e0c1f81c 2022-03-22 thomas
617 e0c1f81c 2022-03-22 thomas err = got_path_dirname(&parent, newpath);
618 e0c1f81c 2022-03-22 thomas if (err != NULL)
619 e0c1f81c 2022-03-22 thomas goto done;
620 e0c1f81c 2022-03-22 thomas err = got_path_mkdir(parent);
621 e0c1f81c 2022-03-22 thomas if (err != NULL)
622 e0c1f81c 2022-03-22 thomas goto done;
623 e0c1f81c 2022-03-22 thomas if (rename(tmppath, newpath) == -1) {
624 e0c1f81c 2022-03-22 thomas err = got_error_from_errno3("rename", tmppath,
625 e0c1f81c 2022-03-22 thomas newpath);
626 e0c1f81c 2022-03-22 thomas goto done;
627 e0c1f81c 2022-03-22 thomas }
628 bb2ad8ff 2022-03-13 thomas }
629 bb2ad8ff 2022-03-13 thomas
630 bb2ad8ff 2022-03-13 thomas if (file_renamed) {
631 814624e7 2022-03-22 thomas err = got_worktree_schedule_delete(worktree, &oldpaths,
632 c71da4f7 2022-03-22 thomas 0, NULL, patch_delete, pa, repo, 0, 0);
633 bb2ad8ff 2022-03-13 thomas if (err == NULL)
634 814624e7 2022-03-22 thomas err = got_worktree_schedule_add(worktree, &newpaths,
635 c71da4f7 2022-03-22 thomas patch_add, pa, repo, 1);
636 814624e7 2022-03-22 thomas if (err)
637 814624e7 2022-03-22 thomas unlink(newpath);
638 814624e7 2022-03-22 thomas } else if (p->old == NULL) {
639 814624e7 2022-03-22 thomas err = got_worktree_schedule_add(worktree, &newpaths,
640 c71da4f7 2022-03-22 thomas patch_add, pa, repo, 1);
641 814624e7 2022-03-22 thomas if (err)
642 814624e7 2022-03-22 thomas unlink(newpath);
643 814624e7 2022-03-22 thomas } else
644 49114f01 2022-03-22 thomas err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
645 49114f01 2022-03-22 thomas NULL);
646 bb2ad8ff 2022-03-13 thomas
647 069bbb86 2022-03-07 thomas done:
648 814624e7 2022-03-22 thomas got_pathlist_free(&oldpaths);
649 814624e7 2022-03-22 thomas got_pathlist_free(&newpaths);
650 e0c1f81c 2022-03-22 thomas free(parent);
651 94af5a06 2022-03-08 thomas free(template);
652 069bbb86 2022-03-07 thomas if (tmppath != NULL)
653 069bbb86 2022-03-07 thomas unlink(tmppath);
654 069bbb86 2022-03-07 thomas free(tmppath);
655 069bbb86 2022-03-07 thomas return err;
656 069bbb86 2022-03-07 thomas }
657 069bbb86 2022-03-07 thomas
658 069bbb86 2022-03-07 thomas const struct got_error *
659 069bbb86 2022-03-07 thomas got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
660 c71da4f7 2022-03-22 thomas int nop, got_patch_progress_cb progress_cb, void *progress_arg,
661 c71da4f7 2022-03-22 thomas got_cancel_cb cancel_cb, void *cancel_arg)
662 069bbb86 2022-03-07 thomas {
663 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
664 814624e7 2022-03-22 thomas struct got_fileindex *fileindex = NULL;
665 49114f01 2022-03-22 thomas char *oldpath, *newpath;
666 069bbb86 2022-03-07 thomas struct imsgbuf *ibuf;
667 069bbb86 2022-03-07 thomas int imsg_fds[2] = {-1, -1};
668 49114f01 2022-03-22 thomas int done = 0, failed = 0;
669 069bbb86 2022-03-07 thomas pid_t pid;
670 069bbb86 2022-03-07 thomas
671 069bbb86 2022-03-07 thomas ibuf = calloc(1, sizeof(*ibuf));
672 069bbb86 2022-03-07 thomas if (ibuf == NULL) {
673 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
674 069bbb86 2022-03-07 thomas goto done;
675 069bbb86 2022-03-07 thomas }
676 069bbb86 2022-03-07 thomas
677 069bbb86 2022-03-07 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
678 069bbb86 2022-03-07 thomas err = got_error_from_errno("socketpair");
679 069bbb86 2022-03-07 thomas goto done;
680 069bbb86 2022-03-07 thomas }
681 069bbb86 2022-03-07 thomas
682 069bbb86 2022-03-07 thomas pid = fork();
683 069bbb86 2022-03-07 thomas if (pid == -1) {
684 069bbb86 2022-03-07 thomas err = got_error_from_errno("fork");
685 069bbb86 2022-03-07 thomas goto done;
686 069bbb86 2022-03-07 thomas } else if (pid == 0) {
687 069bbb86 2022-03-07 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
688 069bbb86 2022-03-07 thomas NULL);
689 069bbb86 2022-03-07 thomas /* not reached */
690 069bbb86 2022-03-07 thomas }
691 069bbb86 2022-03-07 thomas
692 069bbb86 2022-03-07 thomas if (close(imsg_fds[1]) == -1) {
693 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
694 069bbb86 2022-03-07 thomas goto done;
695 069bbb86 2022-03-07 thomas }
696 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
697 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
698 069bbb86 2022-03-07 thomas
699 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
700 069bbb86 2022-03-07 thomas fd = -1;
701 069bbb86 2022-03-07 thomas if (err)
702 069bbb86 2022-03-07 thomas goto done;
703 069bbb86 2022-03-07 thomas
704 814624e7 2022-03-22 thomas err = got_worktree_patch_prepare(&fileindex, worktree);
705 814624e7 2022-03-22 thomas if (err)
706 814624e7 2022-03-22 thomas goto done;
707 814624e7 2022-03-22 thomas
708 069bbb86 2022-03-07 thomas while (!done && err == NULL) {
709 069bbb86 2022-03-07 thomas struct got_patch p;
710 49114f01 2022-03-22 thomas struct patch_args pa;
711 069bbb86 2022-03-07 thomas
712 49114f01 2022-03-22 thomas pa.progress_cb = progress_cb;
713 49114f01 2022-03-22 thomas pa.progress_arg = progress_arg;
714 49114f01 2022-03-22 thomas pa.head = &p.head;
715 49114f01 2022-03-22 thomas
716 069bbb86 2022-03-07 thomas err = recv_patch(ibuf, &done, &p);
717 069bbb86 2022-03-07 thomas if (err || done)
718 069bbb86 2022-03-07 thomas break;
719 069bbb86 2022-03-07 thomas
720 814624e7 2022-03-22 thomas err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
721 814624e7 2022-03-22 thomas &newpath, worktree, repo, fileindex);
722 814624e7 2022-03-22 thomas if (err == NULL)
723 814624e7 2022-03-22 thomas err = apply_patch(worktree, repo, oldpath, newpath,
724 814624e7 2022-03-22 thomas &p, nop, &pa, cancel_cb, cancel_arg);
725 49114f01 2022-03-22 thomas if (err != NULL) {
726 49114f01 2022-03-22 thomas failed = 1;
727 49114f01 2022-03-22 thomas /* recoverable errors */
728 49114f01 2022-03-22 thomas if (err->code == GOT_ERR_FILE_STATUS ||
729 49114f01 2022-03-22 thomas (err->code == GOT_ERR_ERRNO && errno == ENOENT))
730 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
731 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, err);
732 49114f01 2022-03-22 thomas else if (err->code == GOT_ERR_HUNK_FAILED)
733 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
734 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, NULL);
735 49114f01 2022-03-22 thomas }
736 49114f01 2022-03-22 thomas
737 49114f01 2022-03-22 thomas free(oldpath);
738 49114f01 2022-03-22 thomas free(newpath);
739 069bbb86 2022-03-07 thomas patch_free(&p);
740 49114f01 2022-03-22 thomas
741 069bbb86 2022-03-07 thomas if (err)
742 069bbb86 2022-03-07 thomas break;
743 069bbb86 2022-03-07 thomas }
744 069bbb86 2022-03-07 thomas
745 069bbb86 2022-03-07 thomas done:
746 814624e7 2022-03-22 thomas if (fileindex)
747 814624e7 2022-03-22 thomas got_worktree_patch_complete(fileindex);
748 069bbb86 2022-03-07 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
749 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
750 069bbb86 2022-03-07 thomas if (ibuf != NULL)
751 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
752 069bbb86 2022-03-07 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
753 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
754 069bbb86 2022-03-07 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
755 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
756 49114f01 2022-03-22 thomas if (err == NULL && failed)
757 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_FAILED);
758 069bbb86 2022-03-07 thomas return err;
759 069bbb86 2022-03-07 thomas }