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 dbda770b 2022-03-13 op #include <errno.h>
30 e9ce266e 2022-03-07 op #include <limits.h>
31 e9ce266e 2022-03-07 op #include <sha1.h>
32 e9ce266e 2022-03-07 op #include <stdint.h>
33 e9ce266e 2022-03-07 op #include <stdio.h>
34 e9ce266e 2022-03-07 op #include <stdlib.h>
35 e9ce266e 2022-03-07 op #include <string.h>
36 e9ce266e 2022-03-07 op #include <unistd.h>
37 e9ce266e 2022-03-07 op #include <imsg.h>
38 e9ce266e 2022-03-07 op
39 e9ce266e 2022-03-07 op #include "got_error.h"
40 e9ce266e 2022-03-07 op #include "got_object.h"
41 e9ce266e 2022-03-07 op #include "got_path.h"
42 e9ce266e 2022-03-07 op #include "got_reference.h"
43 e9ce266e 2022-03-07 op #include "got_cancel.h"
44 e9ce266e 2022-03-07 op #include "got_worktree.h"
45 e9ce266e 2022-03-07 op #include "got_opentemp.h"
46 e9ce266e 2022-03-07 op #include "got_patch.h"
47 e9ce266e 2022-03-07 op
48 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
49 e9ce266e 2022-03-07 op #include "got_lib_object.h"
50 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
51 e9ce266e 2022-03-07 op
52 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 e9ce266e 2022-03-07 op
54 e9ce266e 2022-03-07 op struct got_patch_hunk {
55 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
56 60aa1fa0 2022-03-17 op const struct got_error *err;
57 60aa1fa0 2022-03-17 op long offset;
58 b3c57ab2 2022-03-22 op int nonl;
59 e9ce266e 2022-03-07 op long old_from;
60 e9ce266e 2022-03-07 op long old_lines;
61 e9ce266e 2022-03-07 op long new_from;
62 e9ce266e 2022-03-07 op long new_lines;
63 e9ce266e 2022-03-07 op size_t len;
64 e9ce266e 2022-03-07 op size_t cap;
65 e9ce266e 2022-03-07 op char **lines;
66 e9ce266e 2022-03-07 op };
67 e9ce266e 2022-03-07 op
68 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
69 e9ce266e 2022-03-07 op struct got_patch {
70 e9ce266e 2022-03-07 op char *old;
71 e9ce266e 2022-03-07 op char *new;
72 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
73 b22138f5 2022-03-16 op };
74 b22138f5 2022-03-16 op
75 b22138f5 2022-03-16 op struct patch_args {
76 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
77 b22138f5 2022-03-16 op void *progress_arg;
78 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
79 e9ce266e 2022-03-07 op };
80 e9ce266e 2022-03-07 op
81 e9ce266e 2022-03-07 op static const struct got_error *
82 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
83 e9ce266e 2022-03-07 op {
84 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
85 e9ce266e 2022-03-07 op
86 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
87 e9ce266e 2022-03-07 op NULL, 0) == -1) {
88 e9ce266e 2022-03-07 op err = got_error_from_errno(
89 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
90 e9ce266e 2022-03-07 op close(fd);
91 e9ce266e 2022-03-07 op return err;
92 e9ce266e 2022-03-07 op }
93 e9ce266e 2022-03-07 op
94 e9ce266e 2022-03-07 op if (imsg_flush(ibuf) == -1) {
95 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_flush");
96 e9ce266e 2022-03-07 op imsg_clear(ibuf);
97 e9ce266e 2022-03-07 op }
98 e9ce266e 2022-03-07 op
99 e9ce266e 2022-03-07 op return err;
100 e9ce266e 2022-03-07 op }
101 e9ce266e 2022-03-07 op
102 e9ce266e 2022-03-07 op static void
103 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
104 e9ce266e 2022-03-07 op {
105 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
106 e9ce266e 2022-03-07 op size_t i;
107 e9ce266e 2022-03-07 op
108 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
109 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
110 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
111 e9ce266e 2022-03-07 op
112 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
113 e9ce266e 2022-03-07 op free(h->lines[i]);
114 e9ce266e 2022-03-07 op free(h->lines);
115 e9ce266e 2022-03-07 op free(h);
116 e9ce266e 2022-03-07 op }
117 e9ce266e 2022-03-07 op
118 e9ce266e 2022-03-07 op free(p->new);
119 e9ce266e 2022-03-07 op free(p->old);
120 e9ce266e 2022-03-07 op }
121 e9ce266e 2022-03-07 op
122 e9ce266e 2022-03-07 op static const struct got_error *
123 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
124 e9ce266e 2022-03-07 op {
125 e9ce266e 2022-03-07 op void *t;
126 e9ce266e 2022-03-07 op size_t newcap;
127 e9ce266e 2022-03-07 op
128 e9ce266e 2022-03-07 op if (h->len == h->cap) {
129 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
130 e9ce266e 2022-03-07 op newcap = 16;
131 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
132 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
133 e9ce266e 2022-03-07 op if (t == NULL)
134 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
135 e9ce266e 2022-03-07 op h->lines = t;
136 e9ce266e 2022-03-07 op h->cap = newcap;
137 e9ce266e 2022-03-07 op }
138 e9ce266e 2022-03-07 op
139 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
140 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
141 e9ce266e 2022-03-07 op
142 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
143 e9ce266e 2022-03-07 op return NULL;
144 e9ce266e 2022-03-07 op }
145 e9ce266e 2022-03-07 op
146 e9ce266e 2022-03-07 op static const struct got_error *
147 e9ce266e 2022-03-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
148 e9ce266e 2022-03-07 op {
149 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
150 e9ce266e 2022-03-07 op struct imsg imsg;
151 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
152 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
153 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
154 e9ce266e 2022-03-07 op size_t datalen;
155 e9ce266e 2022-03-07 op
156 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
157 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
158 e9ce266e 2022-03-07 op
159 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
160 e9ce266e 2022-03-07 op if (err)
161 e9ce266e 2022-03-07 op return err;
162 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
163 e9ce266e 2022-03-07 op *done = 1;
164 e9ce266e 2022-03-07 op goto done;
165 e9ce266e 2022-03-07 op }
166 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
167 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
168 e9ce266e 2022-03-07 op goto done;
169 e9ce266e 2022-03-07 op }
170 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
171 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
172 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
173 e9ce266e 2022-03-07 op goto done;
174 e9ce266e 2022-03-07 op }
175 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
176 e9ce266e 2022-03-07 op if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
177 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
178 e9ce266e 2022-03-07 op goto done;
179 e9ce266e 2022-03-07 op }
180 e9ce266e 2022-03-07 op if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
181 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
182 e9ce266e 2022-03-07 op goto done;
183 e9ce266e 2022-03-07 op }
184 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
185 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
186 b95c53df 2022-03-12 op goto done;
187 b95c53df 2022-03-12 op }
188 e9ce266e 2022-03-07 op
189 e9ce266e 2022-03-07 op imsg_free(&imsg);
190 e9ce266e 2022-03-07 op
191 e9ce266e 2022-03-07 op for (;;) {
192 e9ce266e 2022-03-07 op char *t;
193 e9ce266e 2022-03-07 op
194 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
195 e9ce266e 2022-03-07 op if (err)
196 e9ce266e 2022-03-07 op return err;
197 e9ce266e 2022-03-07 op
198 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
199 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
200 e9ce266e 2022-03-07 op goto done;
201 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
202 b3c57ab2 2022-03-22 op if (h != NULL && h->nonl) {
203 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
204 b3c57ab2 2022-03-22 op goto done;
205 b3c57ab2 2022-03-22 op }
206 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
207 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
208 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
209 e9ce266e 2022-03-07 op goto done;
210 e9ce266e 2022-03-07 op }
211 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
212 e9ce266e 2022-03-07 op if ((h = calloc(1, sizeof(*h))) == NULL) {
213 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
214 e9ce266e 2022-03-07 op goto done;
215 e9ce266e 2022-03-07 op }
216 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
217 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
218 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
219 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
220 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
221 e9ce266e 2022-03-07 op break;
222 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
223 e9ce266e 2022-03-07 op if (h == NULL) {
224 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
225 e9ce266e 2022-03-07 op goto done;
226 e9ce266e 2022-03-07 op }
227 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
228 e9ce266e 2022-03-07 op t = imsg.data;
229 b3c57ab2 2022-03-22 op /* at least one char */
230 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
231 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
232 e9ce266e 2022-03-07 op goto done;
233 e9ce266e 2022-03-07 op }
234 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
235 b3c57ab2 2022-03-22 op *t != '\\') {
236 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
237 e9ce266e 2022-03-07 op goto done;
238 e9ce266e 2022-03-07 op }
239 b3c57ab2 2022-03-22 op if (h->nonl)
240 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
241 b3c57ab2 2022-03-22 op if (*t == '\\')
242 b3c57ab2 2022-03-22 op h->nonl = 1;
243 b3c57ab2 2022-03-22 op else
244 b3c57ab2 2022-03-22 op err = pushline(h, t);
245 e9ce266e 2022-03-07 op if (err)
246 e9ce266e 2022-03-07 op goto done;
247 e9ce266e 2022-03-07 op break;
248 e9ce266e 2022-03-07 op default:
249 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
250 e9ce266e 2022-03-07 op goto done;
251 e9ce266e 2022-03-07 op }
252 e9ce266e 2022-03-07 op
253 e9ce266e 2022-03-07 op imsg_free(&imsg);
254 e9ce266e 2022-03-07 op }
255 e9ce266e 2022-03-07 op
256 e9ce266e 2022-03-07 op done:
257 e9ce266e 2022-03-07 op imsg_free(&imsg);
258 e9ce266e 2022-03-07 op return err;
259 e9ce266e 2022-03-07 op }
260 e9ce266e 2022-03-07 op
261 e9ce266e 2022-03-07 op /*
262 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
263 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
264 e9ce266e 2022-03-07 op */
265 e9ce266e 2022-03-07 op static const struct got_error *
266 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
267 e9ce266e 2022-03-07 op {
268 e9ce266e 2022-03-07 op char buf[BUFSIZ];
269 e9ce266e 2022-03-07 op size_t len, r, w;
270 e9ce266e 2022-03-07 op
271 e9ce266e 2022-03-07 op if (fseek(orig, copypos, SEEK_SET) == -1)
272 e9ce266e 2022-03-07 op return got_error_from_errno("fseek");
273 e9ce266e 2022-03-07 op
274 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
275 e9ce266e 2022-03-07 op len = sizeof(buf);
276 e9ce266e 2022-03-07 op if (pos > 0)
277 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
278 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
279 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
280 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
281 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
282 e9ce266e 2022-03-07 op if (w != r)
283 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
284 e9ce266e 2022-03-07 op copypos += len;
285 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
286 e9ce266e 2022-03-07 op if (pos == -1)
287 e9ce266e 2022-03-07 op return NULL;
288 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
289 e9ce266e 2022-03-07 op }
290 e9ce266e 2022-03-07 op }
291 e9ce266e 2022-03-07 op return NULL;
292 e9ce266e 2022-03-07 op }
293 e9ce266e 2022-03-07 op
294 e9ce266e 2022-03-07 op static const struct got_error *
295 33df9995 2022-03-11 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
296 e9ce266e 2022-03-07 op {
297 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
298 e9ce266e 2022-03-07 op char *line = NULL;
299 e9ce266e 2022-03-07 op char mode = *h->lines[0];
300 e9ce266e 2022-03-07 op size_t linesize = 0;
301 e9ce266e 2022-03-07 op ssize_t linelen;
302 e9ce266e 2022-03-07 op off_t match = -1;
303 e9ce266e 2022-03-07 op long match_lineno = -1;
304 e9ce266e 2022-03-07 op
305 e9ce266e 2022-03-07 op for (;;) {
306 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
307 e9ce266e 2022-03-07 op if (linelen == -1) {
308 e9ce266e 2022-03-07 op if (ferror(orig))
309 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
310 e9ce266e 2022-03-07 op else if (match == -1)
311 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
312 e9ce266e 2022-03-07 op break;
313 e9ce266e 2022-03-07 op }
314 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
315 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
316 e9ce266e 2022-03-07 op (*lineno)++;
317 e9ce266e 2022-03-07 op
318 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
319 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
320 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
321 e9ce266e 2022-03-07 op match = ftello(orig);
322 e9ce266e 2022-03-07 op if (match == -1) {
323 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
324 e9ce266e 2022-03-07 op break;
325 e9ce266e 2022-03-07 op }
326 e9ce266e 2022-03-07 op match -= linelen;
327 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
328 e9ce266e 2022-03-07 op }
329 e9ce266e 2022-03-07 op
330 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
331 e9ce266e 2022-03-07 op break;
332 e9ce266e 2022-03-07 op }
333 e9ce266e 2022-03-07 op
334 e9ce266e 2022-03-07 op if (err == NULL) {
335 33df9995 2022-03-11 op *pos = match;
336 e9ce266e 2022-03-07 op *lineno = match_lineno;
337 e9ce266e 2022-03-07 op if (fseek(orig, match, SEEK_SET) == -1)
338 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
339 e9ce266e 2022-03-07 op }
340 e9ce266e 2022-03-07 op
341 e9ce266e 2022-03-07 op free(line);
342 e9ce266e 2022-03-07 op return err;
343 e9ce266e 2022-03-07 op }
344 e9ce266e 2022-03-07 op
345 e9ce266e 2022-03-07 op static const struct got_error *
346 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
347 e9ce266e 2022-03-07 op {
348 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
349 e9ce266e 2022-03-07 op char *line = NULL;
350 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
351 e9ce266e 2022-03-07 op ssize_t linelen;
352 e9ce266e 2022-03-07 op
353 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
354 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
355 e9ce266e 2022-03-07 op case '+':
356 e9ce266e 2022-03-07 op continue;
357 e9ce266e 2022-03-07 op case ' ':
358 e9ce266e 2022-03-07 op case '-':
359 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
360 e9ce266e 2022-03-07 op if (linelen == -1) {
361 e9ce266e 2022-03-07 op if (ferror(orig))
362 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
363 e9ce266e 2022-03-07 op else
364 e9ce266e 2022-03-07 op err = got_error(
365 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
366 e9ce266e 2022-03-07 op goto done;
367 e9ce266e 2022-03-07 op }
368 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
369 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
370 46ebad13 2022-03-17 op if (strcmp(h->lines[i] + 1, line)) {
371 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
372 e9ce266e 2022-03-07 op goto done;
373 e9ce266e 2022-03-07 op }
374 e9ce266e 2022-03-07 op break;
375 e9ce266e 2022-03-07 op }
376 e9ce266e 2022-03-07 op }
377 e9ce266e 2022-03-07 op
378 e9ce266e 2022-03-07 op done:
379 e9ce266e 2022-03-07 op free(line);
380 e9ce266e 2022-03-07 op return err;
381 e9ce266e 2022-03-07 op }
382 e9ce266e 2022-03-07 op
383 e9ce266e 2022-03-07 op static const struct got_error *
384 e9ce266e 2022-03-07 op apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
385 e9ce266e 2022-03-07 op {
386 e9ce266e 2022-03-07 op size_t i = 0;
387 e9ce266e 2022-03-07 op
388 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
389 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
390 e9ce266e 2022-03-07 op case ' ':
391 b3c57ab2 2022-03-22 op if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
392 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
393 e9ce266e 2022-03-07 op /* fallthrough */
394 e9ce266e 2022-03-07 op case '-':
395 e9ce266e 2022-03-07 op (*lineno)++;
396 e9ce266e 2022-03-07 op break;
397 e9ce266e 2022-03-07 op case '+':
398 46ebad13 2022-03-17 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
399 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
400 b3c57ab2 2022-03-22 op if (i != h->len - 1 || !h->nonl) {
401 b3c57ab2 2022-03-22 op if (fprintf(tmp, "\n") < 0)
402 b3c57ab2 2022-03-22 op return got_error_from_errno(
403 b3c57ab2 2022-03-22 op "fprintf");
404 b3c57ab2 2022-03-22 op }
405 e9ce266e 2022-03-07 op break;
406 e9ce266e 2022-03-07 op }
407 e9ce266e 2022-03-07 op }
408 e9ce266e 2022-03-07 op return NULL;
409 6e96b326 2022-03-12 op }
410 e9ce266e 2022-03-07 op
411 6e96b326 2022-03-12 op static const struct got_error *
412 2be5e1a2 2022-03-16 op patch_file(struct got_patch *p, const char *path, FILE *tmp, int nop,
413 2be5e1a2 2022-03-16 op mode_t *mode)
414 6e96b326 2022-03-12 op {
415 6e96b326 2022-03-12 op const struct got_error *err = NULL;
416 6e96b326 2022-03-12 op struct got_patch_hunk *h;
417 2be5e1a2 2022-03-16 op struct stat sb;
418 6e96b326 2022-03-12 op long lineno = 0;
419 6e96b326 2022-03-12 op FILE *orig;
420 6e96b326 2022-03-12 op off_t copypos, pos;
421 6e96b326 2022-03-12 op char *line = NULL;
422 6e96b326 2022-03-12 op size_t linesize = 0;
423 6e96b326 2022-03-12 op ssize_t linelen;
424 6f5cb1bd 2022-03-08 op
425 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
426 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
427 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
428 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
429 b22138f5 2022-03-16 op if (nop)
430 899fcfdf 2022-03-13 op return NULL;
431 4027dbc2 2022-03-22 op return apply_hunk(tmp, h, &lineno);
432 e9ce266e 2022-03-07 op }
433 e9ce266e 2022-03-07 op
434 e9ce266e 2022-03-07 op if ((orig = fopen(path, "r")) == NULL) {
435 e9ce266e 2022-03-07 op err = got_error_from_errno2("fopen", path);
436 e9ce266e 2022-03-07 op goto done;
437 e9ce266e 2022-03-07 op }
438 e9ce266e 2022-03-07 op
439 2be5e1a2 2022-03-16 op if (fstat(fileno(orig), &sb) == -1) {
440 2be5e1a2 2022-03-16 op err = got_error_from_errno("fstat");
441 2be5e1a2 2022-03-16 op goto done;
442 2be5e1a2 2022-03-16 op }
443 2be5e1a2 2022-03-16 op *mode = sb.st_mode;
444 2be5e1a2 2022-03-16 op
445 e9ce266e 2022-03-07 op copypos = 0;
446 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
447 6e96b326 2022-03-12 op if (h->lines == NULL)
448 6e96b326 2022-03-12 op break;
449 6e96b326 2022-03-12 op
450 e9ce266e 2022-03-07 op tryagain:
451 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
452 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
453 60aa1fa0 2022-03-17 op h->err = err;
454 e9ce266e 2022-03-07 op if (err != NULL)
455 e9ce266e 2022-03-07 op goto done;
456 b22138f5 2022-03-16 op if (!nop)
457 899fcfdf 2022-03-13 op err = copy(tmp, orig, copypos, pos);
458 e9ce266e 2022-03-07 op if (err != NULL)
459 e9ce266e 2022-03-07 op goto done;
460 e9ce266e 2022-03-07 op copypos = pos;
461 e9ce266e 2022-03-07 op
462 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
463 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
464 e9ce266e 2022-03-07 op /*
465 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
466 e9ce266e 2022-03-07 op * after the previous partial match.
467 e9ce266e 2022-03-07 op */
468 e9ce266e 2022-03-07 op if (fseek(orig, pos, SEEK_SET) == -1) {
469 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
470 e9ce266e 2022-03-07 op goto done;
471 e9ce266e 2022-03-07 op }
472 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
473 e9ce266e 2022-03-07 op if (linelen == -1) {
474 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
475 e9ce266e 2022-03-07 op goto done;
476 e9ce266e 2022-03-07 op }
477 e9ce266e 2022-03-07 op lineno++;
478 e9ce266e 2022-03-07 op goto tryagain;
479 e9ce266e 2022-03-07 op }
480 e9ce266e 2022-03-07 op if (err != NULL)
481 e9ce266e 2022-03-07 op goto done;
482 e9ce266e 2022-03-07 op
483 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
484 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
485 60aa1fa0 2022-03-17 op
486 b22138f5 2022-03-16 op if (!nop)
487 899fcfdf 2022-03-13 op err = apply_hunk(tmp, h, &lineno);
488 e9ce266e 2022-03-07 op if (err != NULL)
489 e9ce266e 2022-03-07 op goto done;
490 e9ce266e 2022-03-07 op
491 e9ce266e 2022-03-07 op copypos = ftello(orig);
492 e9ce266e 2022-03-07 op if (copypos == -1) {
493 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
494 e9ce266e 2022-03-07 op goto done;
495 e9ce266e 2022-03-07 op }
496 e9ce266e 2022-03-07 op }
497 e9ce266e 2022-03-07 op
498 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
499 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
500 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
501 60aa1fa0 2022-03-17 op err = h->err;
502 60aa1fa0 2022-03-17 op } else if (!nop && !feof(orig))
503 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
504 6e96b326 2022-03-12 op
505 6e96b326 2022-03-12 op done:
506 6e96b326 2022-03-12 op if (orig != NULL)
507 6e96b326 2022-03-12 op fclose(orig);
508 dbda770b 2022-03-13 op return err;
509 dbda770b 2022-03-13 op }
510 dbda770b 2022-03-13 op
511 dbda770b 2022-03-13 op static const struct got_error *
512 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
513 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
514 60aa1fa0 2022-03-17 op {
515 60aa1fa0 2022-03-17 op const struct got_error *err;
516 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
517 60aa1fa0 2022-03-17 op
518 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
519 60aa1fa0 2022-03-17 op orig_error, 0, 0, 0, 0, 0, NULL);
520 60aa1fa0 2022-03-17 op if (err)
521 60aa1fa0 2022-03-17 op return err;
522 60aa1fa0 2022-03-17 op
523 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
524 60aa1fa0 2022-03-17 op if (h->offset == 0 && h->err == NULL)
525 60aa1fa0 2022-03-17 op continue;
526 60aa1fa0 2022-03-17 op
527 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
528 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
529 60aa1fa0 2022-03-17 op h->offset, h->err);
530 60aa1fa0 2022-03-17 op if (err)
531 60aa1fa0 2022-03-17 op return err;
532 60aa1fa0 2022-03-17 op }
533 60aa1fa0 2022-03-17 op
534 60aa1fa0 2022-03-17 op return NULL;
535 60aa1fa0 2022-03-17 op }
536 60aa1fa0 2022-03-17 op
537 60aa1fa0 2022-03-17 op static const struct got_error *
538 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
539 b22138f5 2022-03-16 op const char *path)
540 b22138f5 2022-03-16 op {
541 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
542 b22138f5 2022-03-16 op }
543 b22138f5 2022-03-16 op
544 b22138f5 2022-03-16 op static const struct got_error *
545 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
546 b22138f5 2022-03-16 op {
547 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
548 b22138f5 2022-03-16 op }
549 b22138f5 2022-03-16 op
550 b22138f5 2022-03-16 op static const struct got_error *
551 6e96b326 2022-03-12 op apply_patch(struct got_worktree *worktree, struct got_repository *repo,
552 60aa1fa0 2022-03-17 op const char *oldpath, const char *newpath, struct got_patch *p,
553 60aa1fa0 2022-03-17 op int nop, struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
554 6e96b326 2022-03-12 op {
555 6e96b326 2022-03-12 op const struct got_error *err = NULL;
556 78f5ac24 2022-03-19 op struct got_pathlist_head oldpaths, newpaths;
557 78f5ac24 2022-03-19 op struct got_pathlist_entry *pe;
558 6e96b326 2022-03-12 op int file_renamed = 0;
559 60aa1fa0 2022-03-17 op char *tmppath = NULL, *template = NULL, *parent = NULL;;
560 6e96b326 2022-03-12 op FILE *tmp = NULL;
561 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
562 6e96b326 2022-03-12 op
563 78f5ac24 2022-03-19 op TAILQ_INIT(&oldpaths);
564 78f5ac24 2022-03-19 op TAILQ_INIT(&newpaths);
565 dbda770b 2022-03-13 op
566 78f5ac24 2022-03-19 op err = got_pathlist_insert(&pe, &oldpaths, oldpath, NULL);
567 dbda770b 2022-03-13 op if (err)
568 dbda770b 2022-03-13 op goto done;
569 78f5ac24 2022-03-19 op err = got_pathlist_insert(&pe, &newpaths, newpath, NULL);
570 78f5ac24 2022-03-19 op if (err)
571 78f5ac24 2022-03-19 op goto done;
572 dbda770b 2022-03-13 op
573 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
574 78f5ac24 2022-03-19 op
575 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
576 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
577 6e96b326 2022-03-12 op err = got_error_from_errno(template);
578 e9ce266e 2022-03-07 op goto done;
579 e9ce266e 2022-03-07 op }
580 e9ce266e 2022-03-07 op
581 b22138f5 2022-03-16 op if (!nop)
582 899fcfdf 2022-03-13 op err = got_opentemp_named(&tmppath, &tmp, template);
583 6e96b326 2022-03-12 op if (err)
584 6e96b326 2022-03-12 op goto done;
585 2be5e1a2 2022-03-16 op err = patch_file(p, oldpath, tmp, nop, &mode);
586 6e96b326 2022-03-12 op if (err)
587 6e96b326 2022-03-12 op goto done;
588 6e96b326 2022-03-12 op
589 b22138f5 2022-03-16 op if (nop)
590 899fcfdf 2022-03-13 op goto done;
591 899fcfdf 2022-03-13 op
592 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
593 78f5ac24 2022-03-19 op err = got_worktree_schedule_delete(worktree, &oldpaths,
594 b22138f5 2022-03-16 op 0, NULL, patch_delete, pa, repo, 0, 0);
595 5b67f96e 2022-03-13 op goto done;
596 5b67f96e 2022-03-13 op }
597 5b67f96e 2022-03-13 op
598 2be5e1a2 2022-03-16 op if (fchmod(fileno(tmp), mode) == -1) {
599 2be5e1a2 2022-03-16 op err = got_error_from_errno2("chmod", newpath);
600 2be5e1a2 2022-03-16 op goto done;
601 2be5e1a2 2022-03-16 op }
602 2be5e1a2 2022-03-16 op
603 6e96b326 2022-03-12 op if (rename(tmppath, newpath) == -1) {
604 95d68340 2022-03-16 op if (errno != ENOENT) {
605 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
606 95d68340 2022-03-16 op newpath);
607 95d68340 2022-03-16 op goto done;
608 95d68340 2022-03-16 op }
609 95d68340 2022-03-16 op
610 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
611 95d68340 2022-03-16 op if (err != NULL)
612 95d68340 2022-03-16 op goto done;
613 95d68340 2022-03-16 op err = got_path_mkdir(parent);
614 95d68340 2022-03-16 op if (err != NULL)
615 95d68340 2022-03-16 op goto done;
616 95d68340 2022-03-16 op if (rename(tmppath, newpath) == -1) {
617 95d68340 2022-03-16 op err = got_error_from_errno3("rename", tmppath,
618 95d68340 2022-03-16 op newpath);
619 95d68340 2022-03-16 op goto done;
620 95d68340 2022-03-16 op }
621 6e96b326 2022-03-12 op }
622 6e96b326 2022-03-12 op
623 6e96b326 2022-03-12 op if (file_renamed) {
624 78f5ac24 2022-03-19 op err = got_worktree_schedule_delete(worktree, &oldpaths,
625 b22138f5 2022-03-16 op 0, NULL, patch_delete, pa, repo, 0, 0);
626 6e96b326 2022-03-12 op if (err == NULL)
627 78f5ac24 2022-03-19 op err = got_worktree_schedule_add(worktree, &newpaths,
628 b22138f5 2022-03-16 op patch_add, pa, repo, 1);
629 78f5ac24 2022-03-19 op if (err)
630 78f5ac24 2022-03-19 op unlink(newpath);
631 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
632 78f5ac24 2022-03-19 op err = got_worktree_schedule_add(worktree, &newpaths,
633 b22138f5 2022-03-16 op patch_add, pa, repo, 1);
634 78f5ac24 2022-03-19 op if (err)
635 78f5ac24 2022-03-19 op unlink(newpath);
636 78f5ac24 2022-03-19 op } else
637 60aa1fa0 2022-03-17 op err = report_progress(pa, oldpath, newpath, GOT_STATUS_MODIFY,
638 60aa1fa0 2022-03-17 op NULL);
639 6e96b326 2022-03-12 op
640 e9ce266e 2022-03-07 op done:
641 78f5ac24 2022-03-19 op got_pathlist_free(&oldpaths);
642 78f5ac24 2022-03-19 op got_pathlist_free(&newpaths);
643 95d68340 2022-03-16 op free(parent);
644 6f5cb1bd 2022-03-08 op free(template);
645 e9ce266e 2022-03-07 op if (tmppath != NULL)
646 e9ce266e 2022-03-07 op unlink(tmppath);
647 e9ce266e 2022-03-07 op free(tmppath);
648 e9ce266e 2022-03-07 op return err;
649 e9ce266e 2022-03-07 op }
650 e9ce266e 2022-03-07 op
651 e9ce266e 2022-03-07 op const struct got_error *
652 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
653 b22138f5 2022-03-16 op int nop, got_patch_progress_cb progress_cb, void *progress_arg,
654 b22138f5 2022-03-16 op got_cancel_cb cancel_cb, void *cancel_arg)
655 e9ce266e 2022-03-07 op {
656 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
657 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
658 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
659 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
660 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
661 60aa1fa0 2022-03-17 op int done = 0, failed = 0;
662 e9ce266e 2022-03-07 op pid_t pid;
663 e9ce266e 2022-03-07 op
664 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
665 e9ce266e 2022-03-07 op if (ibuf == NULL) {
666 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
667 e9ce266e 2022-03-07 op goto done;
668 e9ce266e 2022-03-07 op }
669 e9ce266e 2022-03-07 op
670 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
671 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
672 e9ce266e 2022-03-07 op goto done;
673 e9ce266e 2022-03-07 op }
674 e9ce266e 2022-03-07 op
675 e9ce266e 2022-03-07 op pid = fork();
676 e9ce266e 2022-03-07 op if (pid == -1) {
677 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
678 e9ce266e 2022-03-07 op goto done;
679 e9ce266e 2022-03-07 op } else if (pid == 0) {
680 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
681 e9ce266e 2022-03-07 op NULL);
682 e9ce266e 2022-03-07 op /* not reached */
683 e9ce266e 2022-03-07 op }
684 e9ce266e 2022-03-07 op
685 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
686 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
687 e9ce266e 2022-03-07 op goto done;
688 e9ce266e 2022-03-07 op }
689 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
690 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
691 e9ce266e 2022-03-07 op
692 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
693 e9ce266e 2022-03-07 op fd = -1;
694 e9ce266e 2022-03-07 op if (err)
695 e9ce266e 2022-03-07 op goto done;
696 e9ce266e 2022-03-07 op
697 78f5ac24 2022-03-19 op err = got_worktree_patch_prepare(&fileindex, worktree);
698 78f5ac24 2022-03-19 op if (err)
699 78f5ac24 2022-03-19 op goto done;
700 78f5ac24 2022-03-19 op
701 e9ce266e 2022-03-07 op while (!done && err == NULL) {
702 e9ce266e 2022-03-07 op struct got_patch p;
703 60aa1fa0 2022-03-17 op struct patch_args pa;
704 e9ce266e 2022-03-07 op
705 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
706 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
707 60aa1fa0 2022-03-17 op pa.head = &p.head;
708 60aa1fa0 2022-03-17 op
709 e9ce266e 2022-03-07 op err = recv_patch(ibuf, &done, &p);
710 e9ce266e 2022-03-07 op if (err || done)
711 e9ce266e 2022-03-07 op break;
712 e9ce266e 2022-03-07 op
713 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
714 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
715 78f5ac24 2022-03-19 op if (err == NULL)
716 78f5ac24 2022-03-19 op err = apply_patch(worktree, repo, oldpath, newpath,
717 78f5ac24 2022-03-19 op &p, nop, &pa, cancel_cb, cancel_arg);
718 60aa1fa0 2022-03-17 op if (err != NULL) {
719 60aa1fa0 2022-03-17 op failed = 1;
720 60aa1fa0 2022-03-17 op /* recoverable errors */
721 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
722 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
723 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
724 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
725 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
726 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
727 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
728 60aa1fa0 2022-03-17 op }
729 60aa1fa0 2022-03-17 op
730 60aa1fa0 2022-03-17 op free(oldpath);
731 60aa1fa0 2022-03-17 op free(newpath);
732 e9ce266e 2022-03-07 op patch_free(&p);
733 60aa1fa0 2022-03-17 op
734 e9ce266e 2022-03-07 op if (err)
735 e9ce266e 2022-03-07 op break;
736 e9ce266e 2022-03-07 op }
737 e9ce266e 2022-03-07 op
738 e9ce266e 2022-03-07 op done:
739 78f5ac24 2022-03-19 op if (fileindex)
740 78f5ac24 2022-03-19 op got_worktree_patch_complete(fileindex);
741 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
742 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
743 e9ce266e 2022-03-07 op if (ibuf != NULL)
744 e9ce266e 2022-03-07 op imsg_clear(ibuf);
745 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
746 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
747 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
748 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
749 60aa1fa0 2022-03-17 op if (err == NULL && failed)
750 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);
751 e9ce266e 2022-03-07 op return err;
752 e9ce266e 2022-03-07 op }