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