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