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 2d9b6837 2022-06-23 thomas patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
456 bb2ad8ff 2022-03-13 thomas {
457 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
458 bb2ad8ff 2022-03-13 thomas struct got_patch_hunk *h;
459 da09d8ed 2022-03-22 thomas struct stat sb;
460 8ebb3daa 2022-06-23 thomas int lineno = 0;
461 bb2ad8ff 2022-03-13 thomas off_t copypos, pos;
462 bb2ad8ff 2022-03-13 thomas char *line = NULL;
463 bb2ad8ff 2022-03-13 thomas size_t linesize = 0;
464 bb2ad8ff 2022-03-13 thomas ssize_t linelen;
465 94af5a06 2022-03-08 thomas
466 069bbb86 2022-03-07 thomas if (p->old == NULL) { /* create */
467 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
468 bb2ad8ff 2022-03-13 thomas if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
469 bb2ad8ff 2022-03-13 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
470 afbf14b3 2022-03-22 thomas return apply_hunk(tmp, h, &lineno);
471 069bbb86 2022-03-07 thomas }
472 069bbb86 2022-03-07 thomas
473 2249fadd 2022-06-23 thomas if (fstat(fileno(orig), &sb) == -1)
474 2249fadd 2022-06-23 thomas return got_error_from_errno("fstat");
475 da09d8ed 2022-03-22 thomas *mode = sb.st_mode;
476 da09d8ed 2022-03-22 thomas
477 069bbb86 2022-03-07 thomas copypos = 0;
478 069bbb86 2022-03-07 thomas STAILQ_FOREACH(h, &p->head, entries) {
479 069bbb86 2022-03-07 thomas tryagain:
480 122e1611 2022-03-22 thomas err = locate_hunk(orig, h, &pos, &lineno);
481 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
482 49114f01 2022-03-22 thomas h->err = err;
483 069bbb86 2022-03-07 thomas if (err != NULL)
484 2249fadd 2022-06-23 thomas return err;
485 2d9b6837 2022-06-23 thomas err = copy(tmp, orig, copypos, pos);
486 069bbb86 2022-03-07 thomas if (err != NULL)
487 2249fadd 2022-06-23 thomas return err;
488 069bbb86 2022-03-07 thomas copypos = pos;
489 069bbb86 2022-03-07 thomas
490 069bbb86 2022-03-07 thomas err = test_hunk(orig, h);
491 49114f01 2022-03-22 thomas if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
492 069bbb86 2022-03-07 thomas /*
493 069bbb86 2022-03-07 thomas * try to apply the hunk again starting the search
494 069bbb86 2022-03-07 thomas * after the previous partial match.
495 069bbb86 2022-03-07 thomas */
496 2249fadd 2022-06-23 thomas if (fseeko(orig, pos, SEEK_SET) == -1)
497 2249fadd 2022-06-23 thomas return got_error_from_errno("fseeko");
498 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
499 2249fadd 2022-06-23 thomas if (linelen == -1)
500 2249fadd 2022-06-23 thomas return got_error_from_errno("getline");
501 069bbb86 2022-03-07 thomas lineno++;
502 069bbb86 2022-03-07 thomas goto tryagain;
503 069bbb86 2022-03-07 thomas }
504 069bbb86 2022-03-07 thomas if (err != NULL)
505 2249fadd 2022-06-23 thomas return err;
506 069bbb86 2022-03-07 thomas
507 49114f01 2022-03-22 thomas if (lineno + 1 != h->old_from)
508 49114f01 2022-03-22 thomas h->offset = lineno + 1 - h->old_from;
509 49114f01 2022-03-22 thomas
510 2d9b6837 2022-06-23 thomas err = apply_hunk(tmp, h, &lineno);
511 069bbb86 2022-03-07 thomas if (err != NULL)
512 2249fadd 2022-06-23 thomas return err;
513 f5f21873 2022-04-16 thomas
514 069bbb86 2022-03-07 thomas copypos = ftello(orig);
515 2249fadd 2022-06-23 thomas if (copypos == -1)
516 2249fadd 2022-06-23 thomas return got_error_from_errno("ftello");
517 069bbb86 2022-03-07 thomas }
518 069bbb86 2022-03-07 thomas
519 49114f01 2022-03-22 thomas if (p->new == NULL && sb.st_size != copypos) {
520 49114f01 2022-03-22 thomas h = STAILQ_FIRST(&p->head);
521 49114f01 2022-03-22 thomas h->err = got_error(GOT_ERR_HUNK_FAILED);
522 49114f01 2022-03-22 thomas err = h->err;
523 2d9b6837 2022-06-23 thomas } else if (!feof(orig))
524 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, -1);
525 bb2ad8ff 2022-03-13 thomas
526 10e55613 2022-03-22 thomas return err;
527 10e55613 2022-03-22 thomas }
528 10e55613 2022-03-22 thomas
529 10e55613 2022-03-22 thomas static const struct got_error *
530 49114f01 2022-03-22 thomas report_progress(struct patch_args *pa, const char *old, const char *new,
531 49114f01 2022-03-22 thomas unsigned char status, const struct got_error *orig_error)
532 49114f01 2022-03-22 thomas {
533 49114f01 2022-03-22 thomas const struct got_error *err;
534 49114f01 2022-03-22 thomas struct got_patch_hunk *h;
535 49114f01 2022-03-22 thomas
536 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, status,
537 49114f01 2022-03-22 thomas orig_error, 0, 0, 0, 0, 0, NULL);
538 49114f01 2022-03-22 thomas if (err)
539 49114f01 2022-03-22 thomas return err;
540 49114f01 2022-03-22 thomas
541 49114f01 2022-03-22 thomas STAILQ_FOREACH(h, pa->head, entries) {
542 49114f01 2022-03-22 thomas if (h->offset == 0 && h->err == NULL)
543 49114f01 2022-03-22 thomas continue;
544 49114f01 2022-03-22 thomas
545 49114f01 2022-03-22 thomas err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
546 49114f01 2022-03-22 thomas h->old_from, h->old_lines, h->new_from, h->new_lines,
547 49114f01 2022-03-22 thomas h->offset, h->err);
548 49114f01 2022-03-22 thomas if (err)
549 49114f01 2022-03-22 thomas return err;
550 49114f01 2022-03-22 thomas }
551 49114f01 2022-03-22 thomas
552 49114f01 2022-03-22 thomas return NULL;
553 49114f01 2022-03-22 thomas }
554 49114f01 2022-03-22 thomas
555 49114f01 2022-03-22 thomas static const struct got_error *
556 c71da4f7 2022-03-22 thomas patch_delete(void *arg, unsigned char status, unsigned char staged_status,
557 c71da4f7 2022-03-22 thomas const char *path)
558 c71da4f7 2022-03-22 thomas {
559 49114f01 2022-03-22 thomas return report_progress(arg, path, NULL, status, NULL);
560 c71da4f7 2022-03-22 thomas }
561 c71da4f7 2022-03-22 thomas
562 c71da4f7 2022-03-22 thomas static const struct got_error *
563 c71da4f7 2022-03-22 thomas patch_add(void *arg, unsigned char status, const char *path)
564 c71da4f7 2022-03-22 thomas {
565 49114f01 2022-03-22 thomas return report_progress(arg, NULL, path, status, NULL);
566 c71da4f7 2022-03-22 thomas }
567 c71da4f7 2022-03-22 thomas
568 c71da4f7 2022-03-22 thomas static const struct got_error *
569 bb2ad8ff 2022-03-13 thomas apply_patch(struct got_worktree *worktree, struct got_repository *repo,
570 5e22b737 2022-05-31 thomas struct got_fileindex *fileindex, const char *old, const char *new,
571 5e22b737 2022-05-31 thomas struct got_patch *p, int nop, struct patch_args *pa,
572 5e22b737 2022-05-31 thomas got_cancel_cb cancel_cb, void *cancel_arg)
573 bb2ad8ff 2022-03-13 thomas {
574 bb2ad8ff 2022-03-13 thomas const struct got_error *err = NULL;
575 bb2ad8ff 2022-03-13 thomas int file_renamed = 0;
576 72f46891 2022-04-23 thomas char *oldpath = NULL, *newpath = NULL;
577 3b5e1554 2022-06-23 thomas char *tmppath = NULL, *template = NULL, *parent = NULL;
578 2249fadd 2022-06-23 thomas FILE *oldfile = NULL, *tmp = NULL;
579 da09d8ed 2022-03-22 thomas mode_t mode = GOT_DEFAULT_FILE_MODE;
580 72f46891 2022-04-23 thomas
581 72f46891 2022-04-23 thomas if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
582 72f46891 2022-04-23 thomas old) == -1) {
583 72f46891 2022-04-23 thomas err = got_error_from_errno("asprintf");
584 814624e7 2022-03-22 thomas goto done;
585 72f46891 2022-04-23 thomas }
586 10e55613 2022-03-22 thomas
587 72f46891 2022-04-23 thomas if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
588 72f46891 2022-04-23 thomas new) == -1) {
589 72f46891 2022-04-23 thomas err = got_error_from_errno("asprintf");
590 72f46891 2022-04-23 thomas goto done;
591 72f46891 2022-04-23 thomas }
592 72f46891 2022-04-23 thomas
593 814624e7 2022-03-22 thomas file_renamed = strcmp(oldpath, newpath);
594 42d9d68e 2022-03-13 thomas
595 bb2ad8ff 2022-03-13 thomas if (asprintf(&template, "%s/got-patch",
596 bb2ad8ff 2022-03-13 thomas got_worktree_get_root_path(worktree)) == -1) {
597 bb2ad8ff 2022-03-13 thomas err = got_error_from_errno(template);
598 069bbb86 2022-03-07 thomas goto done;
599 069bbb86 2022-03-07 thomas }
600 069bbb86 2022-03-07 thomas
601 2249fadd 2022-06-23 thomas if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
602 2249fadd 2022-06-23 thomas err = got_error_from_errno2("open", oldpath);
603 2249fadd 2022-06-23 thomas goto done;
604 2249fadd 2022-06-23 thomas }
605 2249fadd 2022-06-23 thomas
606 2d9b6837 2022-06-23 thomas err = got_opentemp_named(&tmppath, &tmp, template);
607 bb2ad8ff 2022-03-13 thomas if (err)
608 bb2ad8ff 2022-03-13 thomas goto done;
609 2d9b6837 2022-06-23 thomas err = patch_file(p, oldfile, tmp, &mode);
610 bb2ad8ff 2022-03-13 thomas if (err)
611 bb2ad8ff 2022-03-13 thomas goto done;
612 bb2ad8ff 2022-03-13 thomas
613 c71da4f7 2022-03-22 thomas if (nop)
614 eaf99875 2022-03-22 thomas goto done;
615 eaf99875 2022-03-22 thomas
616 eaf99875 2022-03-22 thomas if (p->old != NULL && p->new == NULL) {
617 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_rm(old, repo, worktree,
618 5e22b737 2022-05-31 thomas fileindex, patch_delete, pa);
619 eaf99875 2022-03-22 thomas goto done;
620 eaf99875 2022-03-22 thomas }
621 eaf99875 2022-03-22 thomas
622 da09d8ed 2022-03-22 thomas if (fchmod(fileno(tmp), mode) == -1) {
623 8260acc8 2022-05-01 thomas err = got_error_from_errno2("chmod", tmppath);
624 da09d8ed 2022-03-22 thomas goto done;
625 da09d8ed 2022-03-22 thomas }
626 da09d8ed 2022-03-22 thomas
627 bb2ad8ff 2022-03-13 thomas if (rename(tmppath, newpath) == -1) {
628 e0c1f81c 2022-03-22 thomas if (errno != ENOENT) {
629 e0c1f81c 2022-03-22 thomas err = got_error_from_errno3("rename", tmppath,
630 e0c1f81c 2022-03-22 thomas newpath);
631 e0c1f81c 2022-03-22 thomas goto done;
632 e0c1f81c 2022-03-22 thomas }
633 e0c1f81c 2022-03-22 thomas
634 e0c1f81c 2022-03-22 thomas err = got_path_dirname(&parent, newpath);
635 e0c1f81c 2022-03-22 thomas if (err != NULL)
636 e0c1f81c 2022-03-22 thomas goto done;
637 e0c1f81c 2022-03-22 thomas err = got_path_mkdir(parent);
638 e0c1f81c 2022-03-22 thomas if (err != NULL)
639 e0c1f81c 2022-03-22 thomas goto done;
640 e0c1f81c 2022-03-22 thomas if (rename(tmppath, newpath) == -1) {
641 e0c1f81c 2022-03-22 thomas err = got_error_from_errno3("rename", tmppath,
642 e0c1f81c 2022-03-22 thomas newpath);
643 e0c1f81c 2022-03-22 thomas goto done;
644 e0c1f81c 2022-03-22 thomas }
645 bb2ad8ff 2022-03-13 thomas }
646 bb2ad8ff 2022-03-13 thomas
647 bb2ad8ff 2022-03-13 thomas if (file_renamed) {
648 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_rm(old, repo, worktree,
649 5e22b737 2022-05-31 thomas fileindex, patch_delete, pa);
650 bb2ad8ff 2022-03-13 thomas if (err == NULL)
651 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_add(new, repo,
652 5e22b737 2022-05-31 thomas worktree, fileindex, patch_add,
653 5e22b737 2022-05-31 thomas pa);
654 814624e7 2022-03-22 thomas if (err)
655 814624e7 2022-03-22 thomas unlink(newpath);
656 814624e7 2022-03-22 thomas } else if (p->old == NULL) {
657 5e22b737 2022-05-31 thomas err = got_worktree_patch_schedule_add(new, repo, worktree,
658 5e22b737 2022-05-31 thomas fileindex, patch_add, pa);
659 814624e7 2022-03-22 thomas if (err)
660 814624e7 2022-03-22 thomas unlink(newpath);
661 814624e7 2022-03-22 thomas } else
662 72f46891 2022-04-23 thomas err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
663 bb2ad8ff 2022-03-13 thomas
664 069bbb86 2022-03-07 thomas done:
665 e0c1f81c 2022-03-22 thomas free(parent);
666 94af5a06 2022-03-08 thomas free(template);
667 069bbb86 2022-03-07 thomas if (tmppath != NULL)
668 069bbb86 2022-03-07 thomas unlink(tmppath);
669 04cdf6ac 2022-06-23 thomas if (tmp != NULL && fclose(tmp) == EOF && err == NULL)
670 04cdf6ac 2022-06-23 thomas err = got_error_from_errno("fclose");
671 069bbb86 2022-03-07 thomas free(tmppath);
672 72f46891 2022-04-23 thomas free(oldpath);
673 2249fadd 2022-06-23 thomas if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
674 2249fadd 2022-06-23 thomas err = got_error_from_errno("fclose");
675 72f46891 2022-04-23 thomas free(newpath);
676 069bbb86 2022-03-07 thomas return err;
677 eaef698f 2022-04-23 thomas }
678 eaef698f 2022-04-23 thomas
679 eaef698f 2022-04-23 thomas static void
680 eaef698f 2022-04-23 thomas reverse_patch(struct got_patch *p)
681 eaef698f 2022-04-23 thomas {
682 eaef698f 2022-04-23 thomas struct got_patch_hunk *h;
683 eaef698f 2022-04-23 thomas size_t i;
684 8ebb3daa 2022-06-23 thomas int tmp;
685 eaef698f 2022-04-23 thomas
686 eaef698f 2022-04-23 thomas STAILQ_FOREACH(h, &p->head, entries) {
687 eaef698f 2022-04-23 thomas tmp = h->old_from;
688 eaef698f 2022-04-23 thomas h->old_from = h->new_from;
689 eaef698f 2022-04-23 thomas h->new_from = tmp;
690 eaef698f 2022-04-23 thomas
691 eaef698f 2022-04-23 thomas tmp = h->old_lines;
692 eaef698f 2022-04-23 thomas h->old_lines = h->new_lines;
693 eaef698f 2022-04-23 thomas h->new_lines = tmp;
694 eaef698f 2022-04-23 thomas
695 eaef698f 2022-04-23 thomas tmp = h->old_nonl;
696 eaef698f 2022-04-23 thomas h->old_nonl = h->new_nonl;
697 eaef698f 2022-04-23 thomas h->new_nonl = tmp;
698 eaef698f 2022-04-23 thomas
699 eaef698f 2022-04-23 thomas for (i = 0; i < h->len; ++i) {
700 eaef698f 2022-04-23 thomas if (*h->lines[i] == '+')
701 eaef698f 2022-04-23 thomas *h->lines[i] = '-';
702 eaef698f 2022-04-23 thomas else if (*h->lines[i] == '-')
703 eaef698f 2022-04-23 thomas *h->lines[i] = '+';
704 eaef698f 2022-04-23 thomas }
705 eaef698f 2022-04-23 thomas }
706 069bbb86 2022-03-07 thomas }
707 069bbb86 2022-03-07 thomas
708 069bbb86 2022-03-07 thomas const struct got_error *
709 069bbb86 2022-03-07 thomas got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
710 eaef698f 2022-04-23 thomas int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
711 eaef698f 2022-04-23 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
712 069bbb86 2022-03-07 thomas {
713 36832a8e 2022-05-31 thomas const struct got_error *err = NULL, *complete_err = NULL;
714 814624e7 2022-03-22 thomas struct got_fileindex *fileindex = NULL;
715 5e22b737 2022-05-31 thomas char *fileindex_path = NULL;
716 49114f01 2022-03-22 thomas char *oldpath, *newpath;
717 069bbb86 2022-03-07 thomas struct imsgbuf *ibuf;
718 069bbb86 2022-03-07 thomas int imsg_fds[2] = {-1, -1};
719 49114f01 2022-03-22 thomas int done = 0, failed = 0;
720 069bbb86 2022-03-07 thomas pid_t pid;
721 069bbb86 2022-03-07 thomas
722 069bbb86 2022-03-07 thomas ibuf = calloc(1, sizeof(*ibuf));
723 069bbb86 2022-03-07 thomas if (ibuf == NULL) {
724 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
725 069bbb86 2022-03-07 thomas goto done;
726 069bbb86 2022-03-07 thomas }
727 069bbb86 2022-03-07 thomas
728 069bbb86 2022-03-07 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
729 069bbb86 2022-03-07 thomas err = got_error_from_errno("socketpair");
730 069bbb86 2022-03-07 thomas goto done;
731 069bbb86 2022-03-07 thomas }
732 069bbb86 2022-03-07 thomas
733 069bbb86 2022-03-07 thomas pid = fork();
734 069bbb86 2022-03-07 thomas if (pid == -1) {
735 069bbb86 2022-03-07 thomas err = got_error_from_errno("fork");
736 069bbb86 2022-03-07 thomas goto done;
737 069bbb86 2022-03-07 thomas } else if (pid == 0) {
738 069bbb86 2022-03-07 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
739 069bbb86 2022-03-07 thomas NULL);
740 069bbb86 2022-03-07 thomas /* not reached */
741 069bbb86 2022-03-07 thomas }
742 069bbb86 2022-03-07 thomas
743 069bbb86 2022-03-07 thomas if (close(imsg_fds[1]) == -1) {
744 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
745 069bbb86 2022-03-07 thomas goto done;
746 069bbb86 2022-03-07 thomas }
747 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
748 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
749 069bbb86 2022-03-07 thomas
750 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
751 069bbb86 2022-03-07 thomas fd = -1;
752 069bbb86 2022-03-07 thomas if (err)
753 069bbb86 2022-03-07 thomas goto done;
754 069bbb86 2022-03-07 thomas
755 5e22b737 2022-05-31 thomas err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
756 5e22b737 2022-05-31 thomas worktree);
757 814624e7 2022-03-22 thomas if (err)
758 814624e7 2022-03-22 thomas goto done;
759 814624e7 2022-03-22 thomas
760 069bbb86 2022-03-07 thomas while (!done && err == NULL) {
761 069bbb86 2022-03-07 thomas struct got_patch p;
762 49114f01 2022-03-22 thomas struct patch_args pa;
763 069bbb86 2022-03-07 thomas
764 49114f01 2022-03-22 thomas pa.progress_cb = progress_cb;
765 49114f01 2022-03-22 thomas pa.progress_arg = progress_arg;
766 49114f01 2022-03-22 thomas pa.head = &p.head;
767 49114f01 2022-03-22 thomas
768 d9db2ff9 2022-04-16 thomas err = recv_patch(ibuf, &done, &p, strip);
769 069bbb86 2022-03-07 thomas if (err || done)
770 069bbb86 2022-03-07 thomas break;
771 069bbb86 2022-03-07 thomas
772 eaef698f 2022-04-23 thomas if (reverse)
773 eaef698f 2022-04-23 thomas reverse_patch(&p);
774 eaef698f 2022-04-23 thomas
775 814624e7 2022-03-22 thomas err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
776 814624e7 2022-03-22 thomas &newpath, worktree, repo, fileindex);
777 814624e7 2022-03-22 thomas if (err == NULL)
778 5e22b737 2022-05-31 thomas err = apply_patch(worktree, repo, fileindex, oldpath,
779 5e22b737 2022-05-31 thomas newpath, &p, nop, &pa, cancel_cb, cancel_arg);
780 49114f01 2022-03-22 thomas if (err != NULL) {
781 49114f01 2022-03-22 thomas failed = 1;
782 49114f01 2022-03-22 thomas /* recoverable errors */
783 49114f01 2022-03-22 thomas if (err->code == GOT_ERR_FILE_STATUS ||
784 49114f01 2022-03-22 thomas (err->code == GOT_ERR_ERRNO && errno == ENOENT))
785 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
786 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, err);
787 49114f01 2022-03-22 thomas else if (err->code == GOT_ERR_HUNK_FAILED)
788 49114f01 2022-03-22 thomas err = report_progress(&pa, p.old, p.new,
789 49114f01 2022-03-22 thomas GOT_STATUS_CANNOT_UPDATE, NULL);
790 49114f01 2022-03-22 thomas }
791 49114f01 2022-03-22 thomas
792 49114f01 2022-03-22 thomas free(oldpath);
793 49114f01 2022-03-22 thomas free(newpath);
794 069bbb86 2022-03-07 thomas patch_free(&p);
795 49114f01 2022-03-22 thomas
796 069bbb86 2022-03-07 thomas if (err)
797 069bbb86 2022-03-07 thomas break;
798 069bbb86 2022-03-07 thomas }
799 069bbb86 2022-03-07 thomas
800 069bbb86 2022-03-07 thomas done:
801 36832a8e 2022-05-31 thomas if (fileindex != NULL)
802 36832a8e 2022-05-31 thomas complete_err = got_worktree_patch_complete(fileindex,
803 36832a8e 2022-05-31 thomas fileindex_path);
804 5e22b737 2022-05-31 thomas if (complete_err && err == NULL)
805 5e22b737 2022-05-31 thomas err = complete_err;
806 36832a8e 2022-05-31 thomas free(fileindex_path);
807 069bbb86 2022-03-07 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
808 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
809 069bbb86 2022-03-07 thomas if (ibuf != NULL)
810 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
811 069bbb86 2022-03-07 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
812 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
813 069bbb86 2022-03-07 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
814 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
815 49114f01 2022-03-22 thomas if (err == NULL && failed)
816 49114f01 2022-03-22 thomas err = got_error(GOT_ERR_PATCH_FAILED);
817 069bbb86 2022-03-07 thomas return err;
818 069bbb86 2022-03-07 thomas }