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