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