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