Blame


1 e9ce266e 2022-03-07 op /*
2 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 e9ce266e 2022-03-07 op *
4 e9ce266e 2022-03-07 op * Permission to use, copy, modify, and distribute this software for any
5 e9ce266e 2022-03-07 op * purpose with or without fee is hereby granted, provided that the above
6 e9ce266e 2022-03-07 op * copyright notice and this permission notice appear in all copies.
7 e9ce266e 2022-03-07 op *
8 e9ce266e 2022-03-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e9ce266e 2022-03-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e9ce266e 2022-03-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e9ce266e 2022-03-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e9ce266e 2022-03-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e9ce266e 2022-03-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e9ce266e 2022-03-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 e9ce266e 2022-03-07 op *
16 e9ce266e 2022-03-07 op * Apply patches.
17 e9ce266e 2022-03-07 op *
18 e9ce266e 2022-03-07 op * Things that we may want to support:
19 e9ce266e 2022-03-07 op * + support indented patches?
20 e9ce266e 2022-03-07 op * + support other kinds of patches?
21 e9ce266e 2022-03-07 op */
22 e9ce266e 2022-03-07 op
23 e9ce266e 2022-03-07 op #include <sys/types.h>
24 e9ce266e 2022-03-07 op #include <sys/queue.h>
25 e9ce266e 2022-03-07 op #include <sys/socket.h>
26 5b67f96e 2022-03-13 op #include <sys/stat.h>
27 e9ce266e 2022-03-07 op #include <sys/uio.h>
28 e9ce266e 2022-03-07 op
29 a92a2042 2022-07-02 op #include <ctype.h>
30 dbda770b 2022-03-13 op #include <errno.h>
31 e9ce266e 2022-03-07 op #include <limits.h>
32 e9ce266e 2022-03-07 op #include <sha1.h>
33 e9ce266e 2022-03-07 op #include <stdint.h>
34 e9ce266e 2022-03-07 op #include <stdio.h>
35 e9ce266e 2022-03-07 op #include <stdlib.h>
36 e9ce266e 2022-03-07 op #include <string.h>
37 e9ce266e 2022-03-07 op #include <unistd.h>
38 e9ce266e 2022-03-07 op #include <imsg.h>
39 e9ce266e 2022-03-07 op
40 e9ce266e 2022-03-07 op #include "got_error.h"
41 e9ce266e 2022-03-07 op #include "got_object.h"
42 e9ce266e 2022-03-07 op #include "got_path.h"
43 e9ce266e 2022-03-07 op #include "got_reference.h"
44 e9ce266e 2022-03-07 op #include "got_cancel.h"
45 e9ce266e 2022-03-07 op #include "got_worktree.h"
46 55e9459f 2022-06-19 op #include "got_repository.h"
47 e9ce266e 2022-03-07 op #include "got_opentemp.h"
48 e9ce266e 2022-03-07 op #include "got_patch.h"
49 4b752015 2022-06-30 stsp #include "got_diff.h"
50 e9ce266e 2022-03-07 op
51 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
52 55e9459f 2022-06-19 op #include "got_lib_diff.h"
53 e9ce266e 2022-03-07 op #include "got_lib_object.h"
54 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
55 55e9459f 2022-06-19 op #include "got_lib_sha1.h"
56 e9ce266e 2022-03-07 op
57 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 e9ce266e 2022-03-07 op
59 e9ce266e 2022-03-07 op struct got_patch_hunk {
60 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
61 60aa1fa0 2022-03-17 op const struct got_error *err;
62 a92a2042 2022-07-02 op int ws_mangled;
63 35095610 2022-06-14 op int offset;
64 b2832778 2022-04-23 op int old_nonl;
65 b2832778 2022-04-23 op int new_nonl;
66 35095610 2022-06-14 op int old_from;
67 35095610 2022-06-14 op int old_lines;
68 35095610 2022-06-14 op int new_from;
69 35095610 2022-06-14 op int new_lines;
70 e9ce266e 2022-03-07 op size_t len;
71 e9ce266e 2022-03-07 op size_t cap;
72 e9ce266e 2022-03-07 op char **lines;
73 e9ce266e 2022-03-07 op };
74 e9ce266e 2022-03-07 op
75 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
76 e9ce266e 2022-03-07 op struct got_patch {
77 e9ce266e 2022-03-07 op char *old;
78 e9ce266e 2022-03-07 op char *new;
79 d8b5af43 2022-06-19 op char cid[41];
80 55e9459f 2022-06-19 op char blob[41];
81 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
82 b22138f5 2022-03-16 op };
83 b22138f5 2022-03-16 op
84 b22138f5 2022-03-16 op struct patch_args {
85 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
86 b22138f5 2022-03-16 op void *progress_arg;
87 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
88 e9ce266e 2022-03-07 op };
89 e9ce266e 2022-03-07 op
90 e9ce266e 2022-03-07 op static const struct got_error *
91 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
92 e9ce266e 2022-03-07 op {
93 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
94 e9ce266e 2022-03-07 op
95 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
96 e9ce266e 2022-03-07 op NULL, 0) == -1) {
97 e9ce266e 2022-03-07 op err = got_error_from_errno(
98 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
99 e9ce266e 2022-03-07 op close(fd);
100 e9ce266e 2022-03-07 op return err;
101 e9ce266e 2022-03-07 op }
102 e9ce266e 2022-03-07 op
103 08f44789 2022-07-07 op return got_privsep_flush_imsg(ibuf);
104 e9ce266e 2022-03-07 op }
105 e9ce266e 2022-03-07 op
106 e9ce266e 2022-03-07 op static void
107 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
108 e9ce266e 2022-03-07 op {
109 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
110 e9ce266e 2022-03-07 op size_t i;
111 e9ce266e 2022-03-07 op
112 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
113 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
114 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
115 e9ce266e 2022-03-07 op
116 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
117 e9ce266e 2022-03-07 op free(h->lines[i]);
118 e9ce266e 2022-03-07 op free(h->lines);
119 e9ce266e 2022-03-07 op free(h);
120 e9ce266e 2022-03-07 op }
121 e9ce266e 2022-03-07 op
122 e9ce266e 2022-03-07 op free(p->new);
123 e9ce266e 2022-03-07 op free(p->old);
124 0e07a2a1 2022-06-13 op
125 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
126 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
127 e9ce266e 2022-03-07 op }
128 e9ce266e 2022-03-07 op
129 e9ce266e 2022-03-07 op static const struct got_error *
130 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
131 e9ce266e 2022-03-07 op {
132 e9ce266e 2022-03-07 op void *t;
133 e9ce266e 2022-03-07 op size_t newcap;
134 e9ce266e 2022-03-07 op
135 e9ce266e 2022-03-07 op if (h->len == h->cap) {
136 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
137 e9ce266e 2022-03-07 op newcap = 16;
138 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
139 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
140 e9ce266e 2022-03-07 op if (t == NULL)
141 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
142 e9ce266e 2022-03-07 op h->lines = t;
143 e9ce266e 2022-03-07 op h->cap = newcap;
144 e9ce266e 2022-03-07 op }
145 e9ce266e 2022-03-07 op
146 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
147 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
148 e9ce266e 2022-03-07 op
149 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
150 e9ce266e 2022-03-07 op return NULL;
151 e9ce266e 2022-03-07 op }
152 e9ce266e 2022-03-07 op
153 e9ce266e 2022-03-07 op static const struct got_error *
154 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
155 e9ce266e 2022-03-07 op {
156 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
157 e9ce266e 2022-03-07 op struct imsg imsg;
158 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
159 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
160 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
161 e9ce266e 2022-03-07 op size_t datalen;
162 b2832778 2022-04-23 op int lastmode = -1;
163 e9ce266e 2022-03-07 op
164 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
165 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
166 e9ce266e 2022-03-07 op
167 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
168 e9ce266e 2022-03-07 op if (err)
169 e9ce266e 2022-03-07 op return err;
170 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
171 e9ce266e 2022-03-07 op *done = 1;
172 e9ce266e 2022-03-07 op goto done;
173 e9ce266e 2022-03-07 op }
174 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
175 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
176 e9ce266e 2022-03-07 op goto done;
177 e9ce266e 2022-03-07 op }
178 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
179 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
180 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
181 e9ce266e 2022-03-07 op goto done;
182 e9ce266e 2022-03-07 op }
183 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
184 99b94ad7 2022-06-13 op
185 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
186 55e9459f 2022-06-19 op patch.new[sizeof(patch.new)-1] != '\0' ||
187 d8b5af43 2022-06-19 op patch.cid[sizeof(patch.cid)-1] != '\0' ||
188 55e9459f 2022-06-19 op patch.blob[sizeof(patch.blob)-1] != '\0') {
189 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
190 99b94ad7 2022-06-13 op goto done;
191 99b94ad7 2022-06-13 op }
192 55e9459f 2022-06-19 op
193 497a5915 2022-06-27 op if (*patch.cid != '\0')
194 d8b5af43 2022-06-19 op strlcpy(p->cid, patch.cid, sizeof(p->cid));
195 497a5915 2022-06-27 op
196 497a5915 2022-06-27 op if (*patch.blob != '\0')
197 d8b5af43 2022-06-19 op strlcpy(p->blob, patch.blob, sizeof(p->blob));
198 9d6cabd5 2022-04-07 op
199 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
200 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
201 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
202 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
203 9d6cabd5 2022-04-07 op strip = 1;
204 9d6cabd5 2022-04-07 op
205 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
206 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
207 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
208 9d6cabd5 2022-04-07 op if (err)
209 9d6cabd5 2022-04-07 op goto done;
210 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
211 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
212 9d6cabd5 2022-04-07 op if (err)
213 9d6cabd5 2022-04-07 op goto done;
214 e9ce266e 2022-03-07 op }
215 9d6cabd5 2022-04-07 op
216 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
217 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
218 9d6cabd5 2022-04-07 op if (err)
219 9d6cabd5 2022-04-07 op goto done;
220 9d6cabd5 2022-04-07 op }
221 9d6cabd5 2022-04-07 op
222 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
223 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
224 b95c53df 2022-03-12 op goto done;
225 b95c53df 2022-03-12 op }
226 e9ce266e 2022-03-07 op
227 e9ce266e 2022-03-07 op imsg_free(&imsg);
228 e9ce266e 2022-03-07 op
229 e9ce266e 2022-03-07 op for (;;) {
230 e9ce266e 2022-03-07 op char *t;
231 e9ce266e 2022-03-07 op
232 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
233 0e07a2a1 2022-06-13 op if (err) {
234 0e07a2a1 2022-06-13 op patch_free(p);
235 e9ce266e 2022-03-07 op return err;
236 0e07a2a1 2022-06-13 op }
237 e9ce266e 2022-03-07 op
238 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
239 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
240 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
241 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
242 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
243 e9ce266e 2022-03-07 op goto done;
244 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
245 be33dff7 2022-05-13 op if (h != NULL &&
246 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
247 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
248 b3c57ab2 2022-03-22 op goto done;
249 b3c57ab2 2022-03-22 op }
250 b2832778 2022-04-23 op lastmode = -1;
251 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
252 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
253 e9ce266e 2022-03-07 op goto done;
254 e9ce266e 2022-03-07 op }
255 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
256 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
257 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
258 71393b5c 2022-06-14 op goto done;
259 71393b5c 2022-06-14 op }
260 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
261 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
262 e9ce266e 2022-03-07 op goto done;
263 e9ce266e 2022-03-07 op }
264 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
265 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
266 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
267 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
268 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
269 e9ce266e 2022-03-07 op break;
270 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
271 e9ce266e 2022-03-07 op if (h == NULL) {
272 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
273 e9ce266e 2022-03-07 op goto done;
274 e9ce266e 2022-03-07 op }
275 e9ce266e 2022-03-07 op t = imsg.data;
276 b3c57ab2 2022-03-22 op /* at least one char */
277 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
278 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
279 e9ce266e 2022-03-07 op goto done;
280 e9ce266e 2022-03-07 op }
281 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
282 b3c57ab2 2022-03-22 op *t != '\\') {
283 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
284 e9ce266e 2022-03-07 op goto done;
285 e9ce266e 2022-03-07 op }
286 b2832778 2022-04-23 op
287 b2832778 2022-04-23 op if (*t != '\\')
288 b3c57ab2 2022-03-22 op err = pushline(h, t);
289 b2832778 2022-04-23 op else if (lastmode == '-')
290 b2832778 2022-04-23 op h->old_nonl = 1;
291 b2832778 2022-04-23 op else if (lastmode == '+')
292 b2832778 2022-04-23 op h->new_nonl = 1;
293 b2832778 2022-04-23 op else
294 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
295 b2832778 2022-04-23 op
296 e9ce266e 2022-03-07 op if (err)
297 e9ce266e 2022-03-07 op goto done;
298 b2832778 2022-04-23 op
299 b2832778 2022-04-23 op lastmode = *t;
300 e9ce266e 2022-03-07 op break;
301 e9ce266e 2022-03-07 op default:
302 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
303 e9ce266e 2022-03-07 op goto done;
304 e9ce266e 2022-03-07 op }
305 e9ce266e 2022-03-07 op
306 e9ce266e 2022-03-07 op imsg_free(&imsg);
307 e9ce266e 2022-03-07 op }
308 e9ce266e 2022-03-07 op
309 e9ce266e 2022-03-07 op done:
310 0e07a2a1 2022-06-13 op if (err)
311 0e07a2a1 2022-06-13 op patch_free(p);
312 0e07a2a1 2022-06-13 op
313 e9ce266e 2022-03-07 op imsg_free(&imsg);
314 e9ce266e 2022-03-07 op return err;
315 e9ce266e 2022-03-07 op }
316 38d61ead 2022-07-23 op
317 38d61ead 2022-07-23 op static void
318 38d61ead 2022-07-23 op reverse_patch(struct got_patch *p)
319 38d61ead 2022-07-23 op {
320 38d61ead 2022-07-23 op struct got_patch_hunk *h;
321 38d61ead 2022-07-23 op size_t i;
322 38d61ead 2022-07-23 op int tmp;
323 38d61ead 2022-07-23 op
324 38d61ead 2022-07-23 op STAILQ_FOREACH(h, &p->head, entries) {
325 38d61ead 2022-07-23 op tmp = h->old_from;
326 38d61ead 2022-07-23 op h->old_from = h->new_from;
327 38d61ead 2022-07-23 op h->new_from = tmp;
328 e9ce266e 2022-03-07 op
329 38d61ead 2022-07-23 op tmp = h->old_lines;
330 38d61ead 2022-07-23 op h->old_lines = h->new_lines;
331 38d61ead 2022-07-23 op h->new_lines = tmp;
332 38d61ead 2022-07-23 op
333 38d61ead 2022-07-23 op tmp = h->old_nonl;
334 38d61ead 2022-07-23 op h->old_nonl = h->new_nonl;
335 38d61ead 2022-07-23 op h->new_nonl = tmp;
336 38d61ead 2022-07-23 op
337 38d61ead 2022-07-23 op for (i = 0; i < h->len; ++i) {
338 38d61ead 2022-07-23 op if (*h->lines[i] == '+')
339 38d61ead 2022-07-23 op *h->lines[i] = '-';
340 38d61ead 2022-07-23 op else if (*h->lines[i] == '-')
341 38d61ead 2022-07-23 op *h->lines[i] = '+';
342 38d61ead 2022-07-23 op }
343 38d61ead 2022-07-23 op }
344 38d61ead 2022-07-23 op }
345 38d61ead 2022-07-23 op
346 e9ce266e 2022-03-07 op /*
347 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
348 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
349 e9ce266e 2022-03-07 op */
350 e9ce266e 2022-03-07 op static const struct got_error *
351 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
352 e9ce266e 2022-03-07 op {
353 e9ce266e 2022-03-07 op char buf[BUFSIZ];
354 e9ce266e 2022-03-07 op size_t len, r, w;
355 e9ce266e 2022-03-07 op
356 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
357 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
358 e9ce266e 2022-03-07 op
359 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
360 e9ce266e 2022-03-07 op len = sizeof(buf);
361 e9ce266e 2022-03-07 op if (pos > 0)
362 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
363 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
364 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
365 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
366 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
367 e9ce266e 2022-03-07 op if (w != r)
368 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
369 e9ce266e 2022-03-07 op copypos += len;
370 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
371 e9ce266e 2022-03-07 op if (pos == -1)
372 e9ce266e 2022-03-07 op return NULL;
373 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
374 e9ce266e 2022-03-07 op }
375 e9ce266e 2022-03-07 op }
376 e9ce266e 2022-03-07 op return NULL;
377 e9ce266e 2022-03-07 op }
378 e9ce266e 2022-03-07 op
379 e9ce266e 2022-03-07 op static const struct got_error *
380 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
381 e9ce266e 2022-03-07 op {
382 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
383 e9ce266e 2022-03-07 op char *line = NULL;
384 e9ce266e 2022-03-07 op char mode = *h->lines[0];
385 e9ce266e 2022-03-07 op size_t linesize = 0;
386 e9ce266e 2022-03-07 op ssize_t linelen;
387 e9ce266e 2022-03-07 op off_t match = -1;
388 35095610 2022-06-14 op int match_lineno = -1;
389 e9ce266e 2022-03-07 op
390 e9ce266e 2022-03-07 op for (;;) {
391 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
392 e9ce266e 2022-03-07 op if (linelen == -1) {
393 e9ce266e 2022-03-07 op if (ferror(orig))
394 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
395 e9ce266e 2022-03-07 op else if (match == -1)
396 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
397 e9ce266e 2022-03-07 op break;
398 e9ce266e 2022-03-07 op }
399 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
400 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
401 e9ce266e 2022-03-07 op (*lineno)++;
402 e9ce266e 2022-03-07 op
403 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
404 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
405 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
406 e9ce266e 2022-03-07 op match = ftello(orig);
407 e9ce266e 2022-03-07 op if (match == -1) {
408 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
409 e9ce266e 2022-03-07 op break;
410 e9ce266e 2022-03-07 op }
411 e9ce266e 2022-03-07 op match -= linelen;
412 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
413 e9ce266e 2022-03-07 op }
414 e9ce266e 2022-03-07 op
415 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
416 e9ce266e 2022-03-07 op break;
417 e9ce266e 2022-03-07 op }
418 e9ce266e 2022-03-07 op
419 e9ce266e 2022-03-07 op if (err == NULL) {
420 33df9995 2022-03-11 op *pos = match;
421 e9ce266e 2022-03-07 op *lineno = match_lineno;
422 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
423 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
424 e9ce266e 2022-03-07 op }
425 e9ce266e 2022-03-07 op
426 e9ce266e 2022-03-07 op free(line);
427 e9ce266e 2022-03-07 op return err;
428 a92a2042 2022-07-02 op }
429 a92a2042 2022-07-02 op
430 a92a2042 2022-07-02 op static int
431 a92a2042 2022-07-02 op linecmp(const char *a, const char *b, int *mangled)
432 a92a2042 2022-07-02 op {
433 a92a2042 2022-07-02 op int c;
434 a92a2042 2022-07-02 op
435 a92a2042 2022-07-02 op *mangled = 0;
436 a92a2042 2022-07-02 op c = strcmp(a, b);
437 a92a2042 2022-07-02 op if (c == 0)
438 a92a2042 2022-07-02 op return c;
439 a92a2042 2022-07-02 op
440 a92a2042 2022-07-02 op *mangled = 1;
441 a92a2042 2022-07-02 op for (;;) {
442 2c986b8f 2022-07-04 op while (*a == '\t' || *a == ' ' || *a == '\f')
443 a92a2042 2022-07-02 op a++;
444 2c986b8f 2022-07-04 op while (*b == '\t' || *b == ' ' || *b == '\f')
445 a92a2042 2022-07-02 op b++;
446 2c986b8f 2022-07-04 op if (*a == '\0' || *a != *b)
447 a92a2042 2022-07-02 op break;
448 a92a2042 2022-07-02 op a++, b++;
449 a92a2042 2022-07-02 op }
450 a92a2042 2022-07-02 op
451 a92a2042 2022-07-02 op return *a - *b;
452 e9ce266e 2022-03-07 op }
453 e9ce266e 2022-03-07 op
454 e9ce266e 2022-03-07 op static const struct got_error *
455 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
456 e9ce266e 2022-03-07 op {
457 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
458 e9ce266e 2022-03-07 op char *line = NULL;
459 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
460 e9ce266e 2022-03-07 op ssize_t linelen;
461 a92a2042 2022-07-02 op int mangled;
462 e9ce266e 2022-03-07 op
463 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
464 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
465 e9ce266e 2022-03-07 op case '+':
466 e9ce266e 2022-03-07 op continue;
467 e9ce266e 2022-03-07 op case ' ':
468 e9ce266e 2022-03-07 op case '-':
469 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
470 e9ce266e 2022-03-07 op if (linelen == -1) {
471 e9ce266e 2022-03-07 op if (ferror(orig))
472 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
473 e9ce266e 2022-03-07 op else
474 e9ce266e 2022-03-07 op err = got_error(
475 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
476 e9ce266e 2022-03-07 op goto done;
477 e9ce266e 2022-03-07 op }
478 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
479 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
480 a92a2042 2022-07-02 op if (linecmp(h->lines[i] + 1, line, &mangled)) {
481 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
482 e9ce266e 2022-03-07 op goto done;
483 e9ce266e 2022-03-07 op }
484 a92a2042 2022-07-02 op if (mangled)
485 a92a2042 2022-07-02 op h->ws_mangled = 1;
486 e9ce266e 2022-03-07 op break;
487 e9ce266e 2022-03-07 op }
488 e9ce266e 2022-03-07 op }
489 e9ce266e 2022-03-07 op
490 e9ce266e 2022-03-07 op done:
491 e9ce266e 2022-03-07 op free(line);
492 e9ce266e 2022-03-07 op return err;
493 e9ce266e 2022-03-07 op }
494 e9ce266e 2022-03-07 op
495 e9ce266e 2022-03-07 op static const struct got_error *
496 a92a2042 2022-07-02 op apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
497 a92a2042 2022-07-02 op off_t from)
498 e9ce266e 2022-03-07 op {
499 a92a2042 2022-07-02 op const struct got_error *err = NULL;
500 a92a2042 2022-07-02 op const char *t;
501 a92a2042 2022-07-02 op size_t linesize = 0, i, new = 0;
502 a92a2042 2022-07-02 op char *line = NULL;
503 a92a2042 2022-07-02 op char mode;
504 a92a2042 2022-07-02 op ssize_t linelen;
505 e9ce266e 2022-03-07 op
506 a92a2042 2022-07-02 op if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
507 a92a2042 2022-07-02 op return got_error_from_errno("fseeko");
508 a92a2042 2022-07-02 op
509 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
510 a92a2042 2022-07-02 op switch (mode = *h->lines[i]) {
511 e9ce266e 2022-03-07 op case '-':
512 a92a2042 2022-07-02 op case ' ':
513 e9ce266e 2022-03-07 op (*lineno)++;
514 a92a2042 2022-07-02 op if (orig != NULL) {
515 a92a2042 2022-07-02 op linelen = getline(&line, &linesize, orig);
516 a92a2042 2022-07-02 op if (linelen == -1) {
517 a92a2042 2022-07-02 op err = got_error_from_errno("getline");
518 a92a2042 2022-07-02 op goto done;
519 a92a2042 2022-07-02 op }
520 a92a2042 2022-07-02 op if (line[linelen - 1] == '\n')
521 a92a2042 2022-07-02 op line[linelen - 1] = '\0';
522 a92a2042 2022-07-02 op t = line;
523 a92a2042 2022-07-02 op } else
524 a92a2042 2022-07-02 op t = h->lines[i] + 1;
525 a92a2042 2022-07-02 op if (mode == '-')
526 a92a2042 2022-07-02 op continue;
527 a92a2042 2022-07-02 op if (fprintf(tmp, "%s\n", t) < 0) {
528 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
529 a92a2042 2022-07-02 op goto done;
530 a92a2042 2022-07-02 op }
531 e9ce266e 2022-03-07 op break;
532 e9ce266e 2022-03-07 op case '+':
533 b2832778 2022-04-23 op new++;
534 a92a2042 2022-07-02 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
535 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
536 a92a2042 2022-07-02 op goto done;
537 a92a2042 2022-07-02 op }
538 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
539 a92a2042 2022-07-02 op if (fprintf(tmp, "\n") < 0) {
540 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
541 a92a2042 2022-07-02 op goto done;
542 a92a2042 2022-07-02 op }
543 b3c57ab2 2022-03-22 op }
544 e9ce266e 2022-03-07 op break;
545 e9ce266e 2022-03-07 op }
546 e9ce266e 2022-03-07 op }
547 a92a2042 2022-07-02 op
548 a92a2042 2022-07-02 op done:
549 a92a2042 2022-07-02 op free(line);
550 a92a2042 2022-07-02 op return err;
551 6e96b326 2022-03-12 op }
552 e9ce266e 2022-03-07 op
553 6e96b326 2022-03-12 op static const struct got_error *
554 5dffb1a1 2022-07-02 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
555 6e96b326 2022-03-12 op {
556 6e96b326 2022-03-12 op const struct got_error *err = NULL;
557 6e96b326 2022-03-12 op struct got_patch_hunk *h;
558 2be5e1a2 2022-03-16 op struct stat sb;
559 35095610 2022-06-14 op int lineno = 0;
560 6e96b326 2022-03-12 op off_t copypos, pos;
561 6e96b326 2022-03-12 op char *line = NULL;
562 6e96b326 2022-03-12 op size_t linesize = 0;
563 6e96b326 2022-03-12 op ssize_t linelen;
564 6f5cb1bd 2022-03-08 op
565 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
566 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
567 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
568 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
569 a92a2042 2022-07-02 op return apply_hunk(orig, tmp, h, &lineno, 0);
570 e9ce266e 2022-03-07 op }
571 e9ce266e 2022-03-07 op
572 827bcd6c 2022-06-19 op if (fstat(fileno(orig), &sb) == -1)
573 827bcd6c 2022-06-19 op return got_error_from_errno("fstat");
574 2be5e1a2 2022-03-16 op
575 e9ce266e 2022-03-07 op copypos = 0;
576 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
577 e9ce266e 2022-03-07 op tryagain:
578 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
579 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
580 60aa1fa0 2022-03-17 op h->err = err;
581 e9ce266e 2022-03-07 op if (err != NULL)
582 827bcd6c 2022-06-19 op return err;
583 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
584 e9ce266e 2022-03-07 op if (err != NULL)
585 827bcd6c 2022-06-19 op return err;
586 e9ce266e 2022-03-07 op copypos = pos;
587 e9ce266e 2022-03-07 op
588 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
589 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
590 e9ce266e 2022-03-07 op /*
591 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
592 e9ce266e 2022-03-07 op * after the previous partial match.
593 e9ce266e 2022-03-07 op */
594 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
595 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
596 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
597 827bcd6c 2022-06-19 op if (linelen == -1)
598 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
599 e9ce266e 2022-03-07 op lineno++;
600 e9ce266e 2022-03-07 op goto tryagain;
601 e9ce266e 2022-03-07 op }
602 e9ce266e 2022-03-07 op if (err != NULL)
603 827bcd6c 2022-06-19 op return err;
604 e9ce266e 2022-03-07 op
605 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
606 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
607 60aa1fa0 2022-03-17 op
608 a92a2042 2022-07-02 op err = apply_hunk(orig, tmp, h, &lineno, pos);
609 e9ce266e 2022-03-07 op if (err != NULL)
610 827bcd6c 2022-06-19 op return err;
611 fbbb53b9 2022-03-25 op
612 e9ce266e 2022-03-07 op copypos = ftello(orig);
613 827bcd6c 2022-06-19 op if (copypos == -1)
614 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
615 e9ce266e 2022-03-07 op }
616 e9ce266e 2022-03-07 op
617 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
618 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
619 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
620 60aa1fa0 2022-03-17 op err = h->err;
621 05737d49 2022-06-19 op } else if (!feof(orig))
622 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
623 6e96b326 2022-03-12 op
624 dbda770b 2022-03-13 op return err;
625 dbda770b 2022-03-13 op }
626 dbda770b 2022-03-13 op
627 dbda770b 2022-03-13 op static const struct got_error *
628 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
629 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
630 60aa1fa0 2022-03-17 op {
631 60aa1fa0 2022-03-17 op const struct got_error *err;
632 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
633 60aa1fa0 2022-03-17 op
634 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
635 a92a2042 2022-07-02 op orig_error, 0, 0, 0, 0, 0, 0, NULL);
636 60aa1fa0 2022-03-17 op if (err)
637 60aa1fa0 2022-03-17 op return err;
638 60aa1fa0 2022-03-17 op
639 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
640 a92a2042 2022-07-02 op if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
641 60aa1fa0 2022-03-17 op continue;
642 60aa1fa0 2022-03-17 op
643 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
644 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
645 a92a2042 2022-07-02 op h->offset, h->ws_mangled, h->err);
646 60aa1fa0 2022-03-17 op if (err)
647 60aa1fa0 2022-03-17 op return err;
648 60aa1fa0 2022-03-17 op }
649 60aa1fa0 2022-03-17 op
650 60aa1fa0 2022-03-17 op return NULL;
651 60aa1fa0 2022-03-17 op }
652 60aa1fa0 2022-03-17 op
653 60aa1fa0 2022-03-17 op static const struct got_error *
654 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
655 b22138f5 2022-03-16 op const char *path)
656 b22138f5 2022-03-16 op {
657 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
658 b22138f5 2022-03-16 op }
659 b22138f5 2022-03-16 op
660 b22138f5 2022-03-16 op static const struct got_error *
661 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
662 b22138f5 2022-03-16 op {
663 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
664 b22138f5 2022-03-16 op }
665 b22138f5 2022-03-16 op
666 b22138f5 2022-03-16 op static const struct got_error *
667 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
668 55e9459f 2022-06-19 op struct got_repository *repo)
669 6e96b326 2022-03-12 op {
670 6e96b326 2022-03-12 op const struct got_error *err = NULL;
671 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
672 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
673 eb81bc23 2022-06-28 tracey int fd = -1;
674 55e9459f 2022-06-19 op
675 55e9459f 2022-06-19 op *fp = NULL;
676 55e9459f 2022-06-19 op *path = NULL;
677 55e9459f 2022-06-19 op
678 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
679 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
680 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
681 db0dfdd7 2022-06-27 op repo);
682 db0dfdd7 2022-06-27 op if (err)
683 db0dfdd7 2022-06-27 op return err;
684 db0dfdd7 2022-06-27 op idptr = matched_id;
685 db0dfdd7 2022-06-27 op } else {
686 db0dfdd7 2022-06-27 op if (!got_parse_sha1_digest(id.sha1, blobid))
687 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
688 db0dfdd7 2022-06-27 op idptr = &id;
689 db0dfdd7 2022-06-27 op }
690 55e9459f 2022-06-19 op
691 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
692 eb81bc23 2022-06-28 tracey if (fd == -1) {
693 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
694 eb81bc23 2022-06-28 tracey goto done;
695 eb81bc23 2022-06-28 tracey }
696 eb81bc23 2022-06-28 tracey
697 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
698 55e9459f 2022-06-19 op if (err)
699 55e9459f 2022-06-19 op goto done;
700 55e9459f 2022-06-19 op
701 55e9459f 2022-06-19 op err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
702 55e9459f 2022-06-19 op if (err)
703 55e9459f 2022-06-19 op goto done;
704 55e9459f 2022-06-19 op
705 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
706 55e9459f 2022-06-19 op if (err)
707 55e9459f 2022-06-19 op goto done;
708 55e9459f 2022-06-19 op
709 55e9459f 2022-06-19 op done:
710 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
711 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
712 55e9459f 2022-06-19 op if (blob)
713 55e9459f 2022-06-19 op got_object_blob_close(blob);
714 db0dfdd7 2022-06-27 op if (matched_id != NULL)
715 db0dfdd7 2022-06-27 op free(matched_id);
716 55e9459f 2022-06-19 op if (err) {
717 55e9459f 2022-06-19 op if (*fp != NULL)
718 55e9459f 2022-06-19 op fclose(*fp);
719 55e9459f 2022-06-19 op if (*path != NULL)
720 55e9459f 2022-06-19 op unlink(*path);
721 55e9459f 2022-06-19 op free(*path);
722 55e9459f 2022-06-19 op *fp = NULL;
723 55e9459f 2022-06-19 op *path = NULL;
724 55e9459f 2022-06-19 op }
725 55e9459f 2022-06-19 op return err;
726 55e9459f 2022-06-19 op }
727 55e9459f 2022-06-19 op
728 55e9459f 2022-06-19 op static const struct got_error *
729 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
730 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
731 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
732 38d61ead 2022-07-23 op int reverse, struct patch_args *pa,
733 38d61ead 2022-07-23 op got_cancel_cb cancel_cb, void *cancel_arg)
734 55e9459f 2022-06-19 op {
735 55e9459f 2022-06-19 op const struct got_error *err = NULL;
736 5dffb1a1 2022-07-02 op struct stat sb;
737 55e9459f 2022-06-19 op int do_merge = 0, file_renamed = 0;
738 d8b5af43 2022-06-19 op char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
739 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
740 1b1b91ab 2022-06-15 op char *tmppath = NULL, *template = NULL, *parent = NULL;
741 55e9459f 2022-06-19 op char *apath = NULL, *mergepath = NULL;
742 55e9459f 2022-06-19 op FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
743 55e9459f 2022-06-19 op int outfd;
744 55e9459f 2022-06-19 op const char *outpath;
745 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
746 ed3bff83 2022-04-23 op
747 55e9459f 2022-06-19 op *overlapcnt = 0;
748 55e9459f 2022-06-19 op
749 55e9459f 2022-06-19 op /* don't run the diff3 merge on creations/deletions */
750 55e9459f 2022-06-19 op if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
751 55e9459f 2022-06-19 op err = open_blob(&apath, &afile, p->blob, repo);
752 dbc68eed 2022-06-21 op /*
753 dbc68eed 2022-06-21 op * ignore failures to open this blob, we might have
754 dbc68eed 2022-06-21 op * parsed gibberish.
755 dbc68eed 2022-06-21 op */
756 db0dfdd7 2022-06-27 op if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
757 db0dfdd7 2022-06-27 op err->code != GOT_ERR_NO_OBJ)
758 55e9459f 2022-06-19 op return err;
759 55e9459f 2022-06-19 op else if (err == NULL)
760 55e9459f 2022-06-19 op do_merge = 1;
761 38d61ead 2022-07-23 op else if (reverse)
762 38d61ead 2022-07-23 op reverse_patch(p);
763 55e9459f 2022-06-19 op err = NULL;
764 55e9459f 2022-06-19 op }
765 55e9459f 2022-06-19 op
766 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
767 ed3bff83 2022-04-23 op old) == -1) {
768 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
769 78f5ac24 2022-03-19 op goto done;
770 ed3bff83 2022-04-23 op }
771 dbda770b 2022-03-13 op
772 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
773 ed3bff83 2022-04-23 op new) == -1) {
774 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
775 ed3bff83 2022-04-23 op goto done;
776 ed3bff83 2022-04-23 op }
777 ed3bff83 2022-04-23 op
778 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
779 78f5ac24 2022-03-19 op
780 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
781 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
782 6e96b326 2022-03-12 op err = got_error_from_errno(template);
783 e9ce266e 2022-03-07 op goto done;
784 e9ce266e 2022-03-07 op }
785 e9ce266e 2022-03-07 op
786 5dffb1a1 2022-07-02 op if (p->old != NULL) {
787 5dffb1a1 2022-07-02 op if ((oldfile = fopen(oldpath, "r")) == NULL) {
788 5dffb1a1 2022-07-02 op err = got_error_from_errno2("open", oldpath);
789 5dffb1a1 2022-07-02 op goto done;
790 5dffb1a1 2022-07-02 op }
791 5dffb1a1 2022-07-02 op if (fstat(fileno(oldfile), &sb) == -1) {
792 5dffb1a1 2022-07-02 op err = got_error_from_errno2("fstat", oldpath);
793 5dffb1a1 2022-07-02 op goto done;
794 5dffb1a1 2022-07-02 op }
795 5dffb1a1 2022-07-02 op mode = sb.st_mode;
796 827bcd6c 2022-06-19 op }
797 827bcd6c 2022-06-19 op
798 55e9459f 2022-06-19 op err = got_opentemp_named(&tmppath, &tmpfile, template);
799 6e96b326 2022-03-12 op if (err)
800 6e96b326 2022-03-12 op goto done;
801 55e9459f 2022-06-19 op outpath = tmppath;
802 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
803 5dffb1a1 2022-07-02 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
804 6e96b326 2022-03-12 op if (err)
805 6e96b326 2022-03-12 op goto done;
806 6e96b326 2022-03-12 op
807 55e9459f 2022-06-19 op if (do_merge) {
808 497a5915 2022-06-27 op const char *type, *id;
809 497a5915 2022-06-27 op
810 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
811 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
812 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
813 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
814 55e9459f 2022-06-19 op goto done;
815 55e9459f 2022-06-19 op }
816 55e9459f 2022-06-19 op
817 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
818 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
819 55e9459f 2022-06-19 op oldlabel = NULL;
820 55e9459f 2022-06-19 op goto done;
821 55e9459f 2022-06-19 op }
822 55e9459f 2022-06-19 op
823 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
824 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
825 55e9459f 2022-06-19 op newlabel = NULL;
826 55e9459f 2022-06-19 op goto done;
827 55e9459f 2022-06-19 op }
828 55e9459f 2022-06-19 op
829 497a5915 2022-06-27 op if (*p->cid != '\0') {
830 497a5915 2022-06-27 op type = "commit";
831 497a5915 2022-06-27 op id = p->cid;
832 497a5915 2022-06-27 op } else {
833 497a5915 2022-06-27 op type = "blob";
834 497a5915 2022-06-27 op id = p->blob;
835 497a5915 2022-06-27 op }
836 497a5915 2022-06-27 op
837 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
838 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
839 d8b5af43 2022-06-19 op anclabel = NULL;
840 d8b5af43 2022-06-19 op goto done;
841 d8b5af43 2022-06-19 op }
842 d8b5af43 2022-06-19 op
843 38d61ead 2022-07-23 op if (reverse) {
844 38d61ead 2022-07-23 op char *s;
845 38d61ead 2022-07-23 op FILE *t;
846 38d61ead 2022-07-23 op
847 38d61ead 2022-07-23 op s = anclabel;
848 38d61ead 2022-07-23 op anclabel = newlabel;
849 38d61ead 2022-07-23 op newlabel = s;
850 38d61ead 2022-07-23 op
851 38d61ead 2022-07-23 op t = afile;
852 38d61ead 2022-07-23 op afile = tmpfile;
853 38d61ead 2022-07-23 op tmpfile = t;
854 38d61ead 2022-07-23 op }
855 38d61ead 2022-07-23 op
856 55e9459f 2022-06-19 op err = got_opentemp_named(&mergepath, &mergefile, template);
857 55e9459f 2022-06-19 op if (err)
858 55e9459f 2022-06-19 op goto done;
859 55e9459f 2022-06-19 op outpath = mergepath;
860 55e9459f 2022-06-19 op outfd = fileno(mergefile);
861 55e9459f 2022-06-19 op
862 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
863 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
864 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
865 55e9459f 2022-06-19 op if (err)
866 55e9459f 2022-06-19 op goto done;
867 55e9459f 2022-06-19 op }
868 55e9459f 2022-06-19 op
869 b22138f5 2022-03-16 op if (nop)
870 899fcfdf 2022-03-13 op goto done;
871 899fcfdf 2022-03-13 op
872 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
873 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
874 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
875 5b67f96e 2022-03-13 op goto done;
876 5b67f96e 2022-03-13 op }
877 5b67f96e 2022-03-13 op
878 55e9459f 2022-06-19 op if (fchmod(outfd, mode) == -1) {
879 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
880 2be5e1a2 2022-03-16 op goto done;
881 2be5e1a2 2022-03-16 op }
882 2be5e1a2 2022-03-16 op
883 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
884 95d68340 2022-03-16 op if (errno != ENOENT) {
885 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
886 95d68340 2022-03-16 op newpath);
887 95d68340 2022-03-16 op goto done;
888 95d68340 2022-03-16 op }
889 95d68340 2022-03-16 op
890 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
891 95d68340 2022-03-16 op if (err != NULL)
892 95d68340 2022-03-16 op goto done;
893 95d68340 2022-03-16 op err = got_path_mkdir(parent);
894 95d68340 2022-03-16 op if (err != NULL)
895 95d68340 2022-03-16 op goto done;
896 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
897 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
898 95d68340 2022-03-16 op newpath);
899 95d68340 2022-03-16 op goto done;
900 95d68340 2022-03-16 op }
901 6e96b326 2022-03-12 op }
902 6e96b326 2022-03-12 op
903 6e96b326 2022-03-12 op if (file_renamed) {
904 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
905 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
906 6e96b326 2022-03-12 op if (err == NULL)
907 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
908 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
909 f2dd7807 2022-05-17 op pa);
910 78f5ac24 2022-03-19 op if (err)
911 78f5ac24 2022-03-19 op unlink(newpath);
912 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
913 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
914 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
915 78f5ac24 2022-03-19 op if (err)
916 78f5ac24 2022-03-19 op unlink(newpath);
917 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
918 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
919 9802c41c 2022-06-21 op else if (do_merge)
920 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
921 55e9459f 2022-06-19 op else
922 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
923 6e96b326 2022-03-12 op
924 e9ce266e 2022-03-07 op done:
925 95d68340 2022-03-16 op free(parent);
926 6f5cb1bd 2022-03-08 op free(template);
927 55e9459f 2022-06-19 op
928 e9ce266e 2022-03-07 op if (tmppath != NULL)
929 e9ce266e 2022-03-07 op unlink(tmppath);
930 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
931 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
932 e9ce266e 2022-03-07 op free(tmppath);
933 55e9459f 2022-06-19 op
934 ed3bff83 2022-04-23 op free(oldpath);
935 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
936 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
937 55e9459f 2022-06-19 op
938 55e9459f 2022-06-19 op if (apath != NULL)
939 55e9459f 2022-06-19 op unlink(apath);
940 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
941 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
942 55e9459f 2022-06-19 op free(apath);
943 55e9459f 2022-06-19 op
944 55e9459f 2022-06-19 op if (mergepath != NULL)
945 55e9459f 2022-06-19 op unlink(mergepath);
946 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
947 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
948 55e9459f 2022-06-19 op free(mergepath);
949 55e9459f 2022-06-19 op
950 ed3bff83 2022-04-23 op free(newpath);
951 55e9459f 2022-06-19 op free(oldlabel);
952 55e9459f 2022-06-19 op free(newlabel);
953 d8b5af43 2022-06-19 op free(anclabel);
954 e9ce266e 2022-03-07 op return err;
955 bad961bf 2022-04-23 op }
956 bad961bf 2022-04-23 op
957 e9ce266e 2022-03-07 op const struct got_error *
958 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
959 bad961bf 2022-04-23 op int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
960 bad961bf 2022-04-23 op void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
961 e9ce266e 2022-03-07 op {
962 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
963 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
964 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
965 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
966 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
967 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
968 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
969 e9ce266e 2022-03-07 op pid_t pid;
970 e9ce266e 2022-03-07 op
971 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
972 e9ce266e 2022-03-07 op if (ibuf == NULL) {
973 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
974 e9ce266e 2022-03-07 op goto done;
975 e9ce266e 2022-03-07 op }
976 e9ce266e 2022-03-07 op
977 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
978 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
979 e9ce266e 2022-03-07 op goto done;
980 e9ce266e 2022-03-07 op }
981 e9ce266e 2022-03-07 op
982 e9ce266e 2022-03-07 op pid = fork();
983 e9ce266e 2022-03-07 op if (pid == -1) {
984 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
985 e9ce266e 2022-03-07 op goto done;
986 e9ce266e 2022-03-07 op } else if (pid == 0) {
987 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
988 e9ce266e 2022-03-07 op NULL);
989 e9ce266e 2022-03-07 op /* not reached */
990 e9ce266e 2022-03-07 op }
991 e9ce266e 2022-03-07 op
992 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
993 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
994 e9ce266e 2022-03-07 op goto done;
995 e9ce266e 2022-03-07 op }
996 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
997 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
998 e9ce266e 2022-03-07 op
999 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
1000 e9ce266e 2022-03-07 op fd = -1;
1001 e9ce266e 2022-03-07 op if (err)
1002 e9ce266e 2022-03-07 op goto done;
1003 e9ce266e 2022-03-07 op
1004 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1005 f2dd7807 2022-05-17 op worktree);
1006 78f5ac24 2022-03-19 op if (err)
1007 78f5ac24 2022-03-19 op goto done;
1008 78f5ac24 2022-03-19 op
1009 e9ce266e 2022-03-07 op while (!done && err == NULL) {
1010 e9ce266e 2022-03-07 op struct got_patch p;
1011 60aa1fa0 2022-03-17 op struct patch_args pa;
1012 e9ce266e 2022-03-07 op
1013 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
1014 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
1015 60aa1fa0 2022-03-17 op pa.head = &p.head;
1016 60aa1fa0 2022-03-17 op
1017 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
1018 e9ce266e 2022-03-07 op if (err || done)
1019 e9ce266e 2022-03-07 op break;
1020 e9ce266e 2022-03-07 op
1021 38d61ead 2022-07-23 op /* reversal application with merge base is done differently */
1022 38d61ead 2022-07-23 op if (reverse && *p.blob == '\0')
1023 bad961bf 2022-04-23 op reverse_patch(&p);
1024 bad961bf 2022-04-23 op
1025 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1026 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
1027 78f5ac24 2022-03-19 op if (err == NULL)
1028 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
1029 38d61ead 2022-07-23 op fileindex, oldpath, newpath, &p, nop, reverse,
1030 38d61ead 2022-07-23 op &pa, cancel_cb, cancel_arg);
1031 60aa1fa0 2022-03-17 op if (err != NULL) {
1032 60aa1fa0 2022-03-17 op failed = 1;
1033 60aa1fa0 2022-03-17 op /* recoverable errors */
1034 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
1035 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1036 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1037 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
1038 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
1039 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1040 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
1041 60aa1fa0 2022-03-17 op }
1042 55e9459f 2022-06-19 op if (overlapcnt != 0)
1043 55e9459f 2022-06-19 op failed = 1;
1044 60aa1fa0 2022-03-17 op
1045 60aa1fa0 2022-03-17 op free(oldpath);
1046 60aa1fa0 2022-03-17 op free(newpath);
1047 e9ce266e 2022-03-07 op patch_free(&p);
1048 60aa1fa0 2022-03-17 op
1049 e9ce266e 2022-03-07 op if (err)
1050 e9ce266e 2022-03-07 op break;
1051 e9ce266e 2022-03-07 op }
1052 e9ce266e 2022-03-07 op
1053 e9ce266e 2022-03-07 op done:
1054 4bcdc895 2022-05-17 op if (fileindex != NULL)
1055 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
1056 4bcdc895 2022-05-17 op fileindex_path);
1057 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
1058 f2dd7807 2022-05-17 op err = complete_err;
1059 4bcdc895 2022-05-17 op free(fileindex_path);
1060 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
1061 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1062 e9ce266e 2022-03-07 op if (ibuf != NULL)
1063 e9ce266e 2022-03-07 op imsg_clear(ibuf);
1064 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1065 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1066 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1067 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1068 60aa1fa0 2022-03-17 op if (err == NULL && failed)
1069 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);
1070 e9ce266e 2022-03-07 op return err;
1071 e9ce266e 2022-03-07 op }