Blame


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