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