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