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