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