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