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