Blame


1 069bbb86 2022-03-07 thomas /*
2 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 069bbb86 2022-03-07 thomas *
4 069bbb86 2022-03-07 thomas * Permission to use, copy, modify, and distribute this software for any
5 069bbb86 2022-03-07 thomas * purpose with or without fee is hereby granted, provided that the above
6 069bbb86 2022-03-07 thomas * copyright notice and this permission notice appear in all copies.
7 069bbb86 2022-03-07 thomas *
8 069bbb86 2022-03-07 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 069bbb86 2022-03-07 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 069bbb86 2022-03-07 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 069bbb86 2022-03-07 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 069bbb86 2022-03-07 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 069bbb86 2022-03-07 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 069bbb86 2022-03-07 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 069bbb86 2022-03-07 thomas *
16 069bbb86 2022-03-07 thomas * Apply patches.
17 069bbb86 2022-03-07 thomas *
18 069bbb86 2022-03-07 thomas * Things that are still missing:
19 069bbb86 2022-03-07 thomas * + "No final newline" handling
20 069bbb86 2022-03-07 thomas *
21 069bbb86 2022-03-07 thomas * Things that we may want to support:
22 069bbb86 2022-03-07 thomas * + support indented patches?
23 069bbb86 2022-03-07 thomas * + support other kinds of patches?
24 069bbb86 2022-03-07 thomas */
25 069bbb86 2022-03-07 thomas
26 069bbb86 2022-03-07 thomas #include <sys/types.h>
27 069bbb86 2022-03-07 thomas #include <sys/queue.h>
28 069bbb86 2022-03-07 thomas #include <sys/socket.h>
29 069bbb86 2022-03-07 thomas #include <sys/uio.h>
30 069bbb86 2022-03-07 thomas
31 069bbb86 2022-03-07 thomas #include <limits.h>
32 069bbb86 2022-03-07 thomas #include <stdint.h>
33 069bbb86 2022-03-07 thomas #include <stdio.h>
34 069bbb86 2022-03-07 thomas #include <stdlib.h>
35 069bbb86 2022-03-07 thomas #include <string.h>
36 069bbb86 2022-03-07 thomas #include <unistd.h>
37 069bbb86 2022-03-07 thomas #include <imsg.h>
38 069bbb86 2022-03-07 thomas
39 069bbb86 2022-03-07 thomas #include "got_error.h"
40 069bbb86 2022-03-07 thomas #include "got_object.h"
41 069bbb86 2022-03-07 thomas #include "got_path.h"
42 069bbb86 2022-03-07 thomas #include "got_reference.h"
43 069bbb86 2022-03-07 thomas #include "got_cancel.h"
44 069bbb86 2022-03-07 thomas #include "got_worktree.h"
45 069bbb86 2022-03-07 thomas #include "got_opentemp.h"
46 069bbb86 2022-03-07 thomas #include "got_patch.h"
47 069bbb86 2022-03-07 thomas
48 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
49 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
50 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
51 069bbb86 2022-03-07 thomas
52 069bbb86 2022-03-07 thomas #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 069bbb86 2022-03-07 thomas
54 069bbb86 2022-03-07 thomas struct got_patch_hunk {
55 069bbb86 2022-03-07 thomas STAILQ_ENTRY(got_patch_hunk) entries;
56 069bbb86 2022-03-07 thomas long old_from;
57 069bbb86 2022-03-07 thomas long old_lines;
58 069bbb86 2022-03-07 thomas long new_from;
59 069bbb86 2022-03-07 thomas long new_lines;
60 069bbb86 2022-03-07 thomas size_t len;
61 069bbb86 2022-03-07 thomas size_t cap;
62 069bbb86 2022-03-07 thomas char **lines;
63 069bbb86 2022-03-07 thomas };
64 069bbb86 2022-03-07 thomas
65 069bbb86 2022-03-07 thomas struct got_patch {
66 069bbb86 2022-03-07 thomas char *old;
67 069bbb86 2022-03-07 thomas char *new;
68 069bbb86 2022-03-07 thomas STAILQ_HEAD(, got_patch_hunk) head;
69 069bbb86 2022-03-07 thomas };
70 069bbb86 2022-03-07 thomas
71 069bbb86 2022-03-07 thomas static const struct got_error *
72 069bbb86 2022-03-07 thomas send_patch(struct imsgbuf *ibuf, int fd)
73 069bbb86 2022-03-07 thomas {
74 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
75 069bbb86 2022-03-07 thomas
76 069bbb86 2022-03-07 thomas if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
77 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
78 069bbb86 2022-03-07 thomas err = got_error_from_errno(
79 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_FILE");
80 069bbb86 2022-03-07 thomas close(fd);
81 069bbb86 2022-03-07 thomas return err;
82 069bbb86 2022-03-07 thomas }
83 069bbb86 2022-03-07 thomas
84 069bbb86 2022-03-07 thomas if (imsg_flush(ibuf) == -1) {
85 069bbb86 2022-03-07 thomas err = got_error_from_errno("imsg_flush");
86 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
87 069bbb86 2022-03-07 thomas }
88 069bbb86 2022-03-07 thomas
89 069bbb86 2022-03-07 thomas return err;
90 069bbb86 2022-03-07 thomas }
91 069bbb86 2022-03-07 thomas
92 069bbb86 2022-03-07 thomas static void
93 069bbb86 2022-03-07 thomas patch_free(struct got_patch *p)
94 069bbb86 2022-03-07 thomas {
95 069bbb86 2022-03-07 thomas struct got_patch_hunk *h;
96 069bbb86 2022-03-07 thomas size_t i;
97 069bbb86 2022-03-07 thomas
98 069bbb86 2022-03-07 thomas while (!STAILQ_EMPTY(&p->head)) {
99 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
100 069bbb86 2022-03-07 thomas STAILQ_REMOVE_HEAD(&p->head, entries);
101 069bbb86 2022-03-07 thomas
102 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i)
103 069bbb86 2022-03-07 thomas free(h->lines[i]);
104 069bbb86 2022-03-07 thomas free(h->lines);
105 069bbb86 2022-03-07 thomas free(h);
106 069bbb86 2022-03-07 thomas }
107 069bbb86 2022-03-07 thomas
108 069bbb86 2022-03-07 thomas free(p->new);
109 069bbb86 2022-03-07 thomas free(p->old);
110 069bbb86 2022-03-07 thomas }
111 069bbb86 2022-03-07 thomas
112 069bbb86 2022-03-07 thomas static const struct got_error *
113 069bbb86 2022-03-07 thomas pushline(struct got_patch_hunk *h, const char *line)
114 069bbb86 2022-03-07 thomas {
115 069bbb86 2022-03-07 thomas void *t;
116 069bbb86 2022-03-07 thomas size_t newcap;
117 069bbb86 2022-03-07 thomas
118 069bbb86 2022-03-07 thomas if (h->len == h->cap) {
119 069bbb86 2022-03-07 thomas if ((newcap = h->cap * 1.5) == 0)
120 069bbb86 2022-03-07 thomas newcap = 16;
121 069bbb86 2022-03-07 thomas t = recallocarray(h->lines, h->cap, newcap,
122 069bbb86 2022-03-07 thomas sizeof(h->lines[0]));
123 069bbb86 2022-03-07 thomas if (t == NULL)
124 069bbb86 2022-03-07 thomas return got_error_from_errno("recallocarray");
125 069bbb86 2022-03-07 thomas h->lines = t;
126 069bbb86 2022-03-07 thomas h->cap = newcap;
127 069bbb86 2022-03-07 thomas }
128 069bbb86 2022-03-07 thomas
129 069bbb86 2022-03-07 thomas if ((t = strdup(line)) == NULL)
130 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
131 069bbb86 2022-03-07 thomas
132 069bbb86 2022-03-07 thomas h->lines[h->len++] = t;
133 069bbb86 2022-03-07 thomas return NULL;
134 069bbb86 2022-03-07 thomas }
135 069bbb86 2022-03-07 thomas
136 069bbb86 2022-03-07 thomas static const struct got_error *
137 069bbb86 2022-03-07 thomas recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
138 069bbb86 2022-03-07 thomas {
139 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
140 069bbb86 2022-03-07 thomas struct imsg imsg;
141 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
142 069bbb86 2022-03-07 thomas struct got_imsg_patch patch;
143 069bbb86 2022-03-07 thomas struct got_patch_hunk *h = NULL;
144 069bbb86 2022-03-07 thomas size_t datalen;
145 069bbb86 2022-03-07 thomas
146 069bbb86 2022-03-07 thomas memset(p, 0, sizeof(*p));
147 069bbb86 2022-03-07 thomas STAILQ_INIT(&p->head);
148 069bbb86 2022-03-07 thomas
149 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
150 069bbb86 2022-03-07 thomas if (err)
151 069bbb86 2022-03-07 thomas return err;
152 069bbb86 2022-03-07 thomas if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
153 069bbb86 2022-03-07 thomas *done = 1;
154 069bbb86 2022-03-07 thomas goto done;
155 069bbb86 2022-03-07 thomas }
156 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH) {
157 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
158 069bbb86 2022-03-07 thomas goto done;
159 069bbb86 2022-03-07 thomas }
160 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
161 069bbb86 2022-03-07 thomas if (datalen != sizeof(patch)) {
162 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
163 069bbb86 2022-03-07 thomas goto done;
164 069bbb86 2022-03-07 thomas }
165 069bbb86 2022-03-07 thomas memcpy(&patch, imsg.data, sizeof(patch));
166 069bbb86 2022-03-07 thomas if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
167 069bbb86 2022-03-07 thomas err = got_error_from_errno("strdup");
168 069bbb86 2022-03-07 thomas goto done;
169 069bbb86 2022-03-07 thomas }
170 069bbb86 2022-03-07 thomas if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
171 069bbb86 2022-03-07 thomas err = got_error_from_errno("strdup");
172 069bbb86 2022-03-07 thomas goto done;
173 069bbb86 2022-03-07 thomas }
174 069bbb86 2022-03-07 thomas
175 069bbb86 2022-03-07 thomas imsg_free(&imsg);
176 069bbb86 2022-03-07 thomas
177 069bbb86 2022-03-07 thomas for (;;) {
178 069bbb86 2022-03-07 thomas char *t;
179 069bbb86 2022-03-07 thomas
180 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, ibuf, 0);
181 069bbb86 2022-03-07 thomas if (err)
182 069bbb86 2022-03-07 thomas return err;
183 069bbb86 2022-03-07 thomas
184 069bbb86 2022-03-07 thomas switch (imsg.hdr.type) {
185 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_DONE:
186 069bbb86 2022-03-07 thomas goto done;
187 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_HUNK:
188 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
189 069bbb86 2022-03-07 thomas if (datalen != sizeof(hdr)) {
190 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
191 069bbb86 2022-03-07 thomas goto done;
192 069bbb86 2022-03-07 thomas }
193 069bbb86 2022-03-07 thomas memcpy(&hdr, imsg.data, sizeof(hdr));
194 069bbb86 2022-03-07 thomas if ((h = calloc(1, sizeof(*h))) == NULL) {
195 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
196 069bbb86 2022-03-07 thomas goto done;
197 069bbb86 2022-03-07 thomas }
198 069bbb86 2022-03-07 thomas h->old_from = hdr.oldfrom;
199 069bbb86 2022-03-07 thomas h->old_lines = hdr.oldlines;
200 069bbb86 2022-03-07 thomas h->new_from = hdr.newfrom;
201 069bbb86 2022-03-07 thomas h->new_lines = hdr.newlines;
202 069bbb86 2022-03-07 thomas STAILQ_INSERT_TAIL(&p->head, h, entries);
203 069bbb86 2022-03-07 thomas break;
204 069bbb86 2022-03-07 thomas case GOT_IMSG_PATCH_LINE:
205 069bbb86 2022-03-07 thomas if (h == NULL) {
206 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
207 069bbb86 2022-03-07 thomas goto done;
208 069bbb86 2022-03-07 thomas }
209 069bbb86 2022-03-07 thomas datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
210 069bbb86 2022-03-07 thomas t = imsg.data;
211 069bbb86 2022-03-07 thomas /* at least one char plus newline */
212 069bbb86 2022-03-07 thomas if (datalen < 2 || t[datalen-1] != '\0') {
213 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
214 069bbb86 2022-03-07 thomas goto done;
215 069bbb86 2022-03-07 thomas }
216 069bbb86 2022-03-07 thomas if (*t != ' ' && *t != '-' && *t != '+') {
217 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
218 069bbb86 2022-03-07 thomas goto done;
219 069bbb86 2022-03-07 thomas }
220 069bbb86 2022-03-07 thomas err = pushline(h, t);
221 069bbb86 2022-03-07 thomas if (err)
222 069bbb86 2022-03-07 thomas goto done;
223 069bbb86 2022-03-07 thomas break;
224 069bbb86 2022-03-07 thomas default:
225 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
226 069bbb86 2022-03-07 thomas goto done;
227 069bbb86 2022-03-07 thomas }
228 069bbb86 2022-03-07 thomas
229 069bbb86 2022-03-07 thomas imsg_free(&imsg);
230 069bbb86 2022-03-07 thomas }
231 069bbb86 2022-03-07 thomas
232 069bbb86 2022-03-07 thomas done:
233 069bbb86 2022-03-07 thomas imsg_free(&imsg);
234 069bbb86 2022-03-07 thomas return err;
235 069bbb86 2022-03-07 thomas }
236 069bbb86 2022-03-07 thomas
237 069bbb86 2022-03-07 thomas /*
238 069bbb86 2022-03-07 thomas * Copy data from orig starting at copypos until pos into tmp.
239 069bbb86 2022-03-07 thomas * If pos is -1, copy until EOF.
240 069bbb86 2022-03-07 thomas */
241 069bbb86 2022-03-07 thomas static const struct got_error *
242 069bbb86 2022-03-07 thomas copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
243 069bbb86 2022-03-07 thomas {
244 069bbb86 2022-03-07 thomas char buf[BUFSIZ];
245 069bbb86 2022-03-07 thomas size_t len, r, w;
246 069bbb86 2022-03-07 thomas
247 069bbb86 2022-03-07 thomas if (fseek(orig, copypos, SEEK_SET) == -1)
248 069bbb86 2022-03-07 thomas return got_error_from_errno("fseek");
249 069bbb86 2022-03-07 thomas
250 069bbb86 2022-03-07 thomas while (pos == -1 || copypos < pos) {
251 069bbb86 2022-03-07 thomas len = sizeof(buf);
252 069bbb86 2022-03-07 thomas if (pos > 0)
253 069bbb86 2022-03-07 thomas len = MIN(len, (size_t)pos - copypos);
254 069bbb86 2022-03-07 thomas r = fread(buf, 1, len, orig);
255 069bbb86 2022-03-07 thomas if (r != len && ferror(orig))
256 069bbb86 2022-03-07 thomas return got_error_from_errno("fread");
257 069bbb86 2022-03-07 thomas w = fwrite(buf, 1, r, tmp);
258 069bbb86 2022-03-07 thomas if (w != r)
259 069bbb86 2022-03-07 thomas return got_error_from_errno("fwrite");
260 069bbb86 2022-03-07 thomas copypos += len;
261 069bbb86 2022-03-07 thomas if (r != len && feof(orig)) {
262 069bbb86 2022-03-07 thomas if (pos == -1)
263 069bbb86 2022-03-07 thomas return NULL;
264 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_DONT_APPLY);
265 069bbb86 2022-03-07 thomas }
266 069bbb86 2022-03-07 thomas }
267 069bbb86 2022-03-07 thomas return NULL;
268 069bbb86 2022-03-07 thomas }
269 069bbb86 2022-03-07 thomas
270 069bbb86 2022-03-07 thomas static const struct got_error *
271 069bbb86 2022-03-07 thomas locate_hunk(FILE *orig, struct got_patch_hunk *h, long *lineno)
272 069bbb86 2022-03-07 thomas {
273 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
274 069bbb86 2022-03-07 thomas char *line = NULL;
275 069bbb86 2022-03-07 thomas char mode = *h->lines[0];
276 069bbb86 2022-03-07 thomas size_t linesize = 0;
277 069bbb86 2022-03-07 thomas ssize_t linelen;
278 069bbb86 2022-03-07 thomas off_t match = -1;
279 069bbb86 2022-03-07 thomas long match_lineno = -1;
280 069bbb86 2022-03-07 thomas
281 069bbb86 2022-03-07 thomas for (;;) {
282 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
283 069bbb86 2022-03-07 thomas if (linelen == -1) {
284 069bbb86 2022-03-07 thomas if (ferror(orig))
285 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
286 069bbb86 2022-03-07 thomas else if (match == -1)
287 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_DONT_APPLY);
288 069bbb86 2022-03-07 thomas break;
289 069bbb86 2022-03-07 thomas }
290 069bbb86 2022-03-07 thomas (*lineno)++;
291 069bbb86 2022-03-07 thomas
292 069bbb86 2022-03-07 thomas if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
293 069bbb86 2022-03-07 thomas (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
294 069bbb86 2022-03-07 thomas (mode == '+' && *lineno == h->old_from)) {
295 069bbb86 2022-03-07 thomas match = ftello(orig);
296 069bbb86 2022-03-07 thomas if (match == -1) {
297 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
298 069bbb86 2022-03-07 thomas break;
299 069bbb86 2022-03-07 thomas }
300 069bbb86 2022-03-07 thomas match -= linelen;
301 069bbb86 2022-03-07 thomas match_lineno = (*lineno)-1;
302 069bbb86 2022-03-07 thomas }
303 069bbb86 2022-03-07 thomas
304 069bbb86 2022-03-07 thomas if (*lineno >= h->old_from && match != -1)
305 069bbb86 2022-03-07 thomas break;
306 069bbb86 2022-03-07 thomas }
307 069bbb86 2022-03-07 thomas
308 069bbb86 2022-03-07 thomas if (err == NULL) {
309 069bbb86 2022-03-07 thomas *lineno = match_lineno;
310 069bbb86 2022-03-07 thomas if (fseek(orig, match, SEEK_SET) == -1)
311 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
312 069bbb86 2022-03-07 thomas }
313 069bbb86 2022-03-07 thomas
314 069bbb86 2022-03-07 thomas free(line);
315 069bbb86 2022-03-07 thomas return err;
316 069bbb86 2022-03-07 thomas }
317 069bbb86 2022-03-07 thomas
318 069bbb86 2022-03-07 thomas static const struct got_error *
319 069bbb86 2022-03-07 thomas test_hunk(FILE *orig, struct got_patch_hunk *h)
320 069bbb86 2022-03-07 thomas {
321 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
322 069bbb86 2022-03-07 thomas char *line = NULL;
323 069bbb86 2022-03-07 thomas size_t linesize = 0, i = 0;
324 069bbb86 2022-03-07 thomas ssize_t linelen;
325 069bbb86 2022-03-07 thomas
326 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
327 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
328 069bbb86 2022-03-07 thomas case '+':
329 069bbb86 2022-03-07 thomas continue;
330 069bbb86 2022-03-07 thomas case ' ':
331 069bbb86 2022-03-07 thomas case '-':
332 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
333 069bbb86 2022-03-07 thomas if (linelen == -1) {
334 069bbb86 2022-03-07 thomas if (ferror(orig))
335 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
336 069bbb86 2022-03-07 thomas else
337 069bbb86 2022-03-07 thomas err = got_error(
338 069bbb86 2022-03-07 thomas GOT_ERR_PATCH_DONT_APPLY);
339 069bbb86 2022-03-07 thomas goto done;
340 069bbb86 2022-03-07 thomas }
341 069bbb86 2022-03-07 thomas if (strcmp(h->lines[i]+1, line)) {
342 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_DONT_APPLY);
343 069bbb86 2022-03-07 thomas goto done;
344 069bbb86 2022-03-07 thomas }
345 069bbb86 2022-03-07 thomas break;
346 069bbb86 2022-03-07 thomas }
347 069bbb86 2022-03-07 thomas }
348 069bbb86 2022-03-07 thomas
349 069bbb86 2022-03-07 thomas done:
350 069bbb86 2022-03-07 thomas free(line);
351 069bbb86 2022-03-07 thomas return err;
352 069bbb86 2022-03-07 thomas }
353 069bbb86 2022-03-07 thomas
354 069bbb86 2022-03-07 thomas static const struct got_error *
355 069bbb86 2022-03-07 thomas apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
356 069bbb86 2022-03-07 thomas {
357 069bbb86 2022-03-07 thomas size_t i = 0;
358 069bbb86 2022-03-07 thomas
359 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
360 069bbb86 2022-03-07 thomas switch (*h->lines[i]) {
361 069bbb86 2022-03-07 thomas case ' ':
362 069bbb86 2022-03-07 thomas if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
363 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
364 069bbb86 2022-03-07 thomas /* fallthrough */
365 069bbb86 2022-03-07 thomas case '-':
366 069bbb86 2022-03-07 thomas (*lineno)++;
367 069bbb86 2022-03-07 thomas break;
368 069bbb86 2022-03-07 thomas case '+':
369 069bbb86 2022-03-07 thomas if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
370 069bbb86 2022-03-07 thomas return got_error_from_errno("fprintf");
371 069bbb86 2022-03-07 thomas break;
372 069bbb86 2022-03-07 thomas }
373 069bbb86 2022-03-07 thomas }
374 069bbb86 2022-03-07 thomas return NULL;
375 069bbb86 2022-03-07 thomas }
376 069bbb86 2022-03-07 thomas
377 069bbb86 2022-03-07 thomas static const struct got_error *
378 069bbb86 2022-03-07 thomas apply_patch(struct got_worktree *worktree, struct got_repository *repo,
379 de3c84b6 2022-03-08 thomas struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
380 de3c84b6 2022-03-08 thomas got_worktree_checkout_cb add_cb, void *add_arg)
381 069bbb86 2022-03-07 thomas {
382 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
383 069bbb86 2022-03-07 thomas struct got_pathlist_head paths;
384 069bbb86 2022-03-07 thomas struct got_pathlist_entry *pe;
385 94af5a06 2022-03-08 thomas char *path = NULL, *tmppath = NULL, *template = NULL;
386 069bbb86 2022-03-07 thomas FILE *orig = NULL, *tmp = NULL;
387 069bbb86 2022-03-07 thomas struct got_patch_hunk *h;
388 069bbb86 2022-03-07 thomas size_t i;
389 069bbb86 2022-03-07 thomas long lineno = 0;
390 069bbb86 2022-03-07 thomas off_t copypos, pos;
391 069bbb86 2022-03-07 thomas char *line = NULL;
392 069bbb86 2022-03-07 thomas size_t linesize = 0;
393 069bbb86 2022-03-07 thomas ssize_t linelen;
394 069bbb86 2022-03-07 thomas
395 069bbb86 2022-03-07 thomas TAILQ_INIT(&paths);
396 069bbb86 2022-03-07 thomas
397 069bbb86 2022-03-07 thomas if (p->old == NULL && p->new == NULL)
398 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
399 069bbb86 2022-03-07 thomas
400 069bbb86 2022-03-07 thomas err = got_worktree_resolve_path(&path, worktree,
401 069bbb86 2022-03-07 thomas p->new != NULL ? p->new : p->old);
402 069bbb86 2022-03-07 thomas if (err)
403 069bbb86 2022-03-07 thomas return err;
404 069bbb86 2022-03-07 thomas err = got_pathlist_insert(&pe, &paths, path, NULL);
405 069bbb86 2022-03-07 thomas if (err)
406 069bbb86 2022-03-07 thomas goto done;
407 069bbb86 2022-03-07 thomas
408 069bbb86 2022-03-07 thomas if (p->old != NULL && p->new == NULL) {
409 069bbb86 2022-03-07 thomas /*
410 069bbb86 2022-03-07 thomas * special case: delete a file. don't try to match
411 069bbb86 2022-03-07 thomas * the lines but just schedule the removal.
412 069bbb86 2022-03-07 thomas */
413 069bbb86 2022-03-07 thomas err = got_worktree_schedule_delete(worktree, &paths,
414 de3c84b6 2022-03-08 thomas 0, NULL, delete_cb, delete_arg, repo, 0, 0);
415 069bbb86 2022-03-07 thomas goto done;
416 069bbb86 2022-03-07 thomas } else if (p->old != NULL && strcmp(p->old, p->new)) {
417 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_PATHS_DIFFER);
418 069bbb86 2022-03-07 thomas goto done;
419 069bbb86 2022-03-07 thomas }
420 069bbb86 2022-03-07 thomas
421 94af5a06 2022-03-08 thomas if (asprintf(&template, "%s/got-patch",
422 94af5a06 2022-03-08 thomas got_worktree_get_root_path(worktree)) == -1) {
423 94af5a06 2022-03-08 thomas err = got_error_from_errno(template);
424 94af5a06 2022-03-08 thomas goto done;
425 94af5a06 2022-03-08 thomas }
426 94af5a06 2022-03-08 thomas
427 94af5a06 2022-03-08 thomas err = got_opentemp_named(&tmppath, &tmp, template);
428 069bbb86 2022-03-07 thomas if (err)
429 069bbb86 2022-03-07 thomas goto done;
430 069bbb86 2022-03-07 thomas
431 069bbb86 2022-03-07 thomas if (p->old == NULL) { /* create */
432 069bbb86 2022-03-07 thomas h = STAILQ_FIRST(&p->head);
433 069bbb86 2022-03-07 thomas if (h == NULL || STAILQ_NEXT(h, entries) != NULL) {
434 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
435 069bbb86 2022-03-07 thomas goto done;
436 069bbb86 2022-03-07 thomas }
437 069bbb86 2022-03-07 thomas for (i = 0; i < h->len; ++i) {
438 069bbb86 2022-03-07 thomas if (fprintf(tmp, "%s", h->lines[i]+1) < 0) {
439 069bbb86 2022-03-07 thomas err = got_error_from_errno("fprintf");
440 069bbb86 2022-03-07 thomas goto done;
441 069bbb86 2022-03-07 thomas }
442 069bbb86 2022-03-07 thomas }
443 069bbb86 2022-03-07 thomas goto rename;
444 069bbb86 2022-03-07 thomas }
445 069bbb86 2022-03-07 thomas
446 069bbb86 2022-03-07 thomas if ((orig = fopen(path, "r")) == NULL) {
447 069bbb86 2022-03-07 thomas err = got_error_from_errno2("fopen", path);
448 069bbb86 2022-03-07 thomas goto done;
449 069bbb86 2022-03-07 thomas }
450 069bbb86 2022-03-07 thomas
451 069bbb86 2022-03-07 thomas copypos = 0;
452 069bbb86 2022-03-07 thomas STAILQ_FOREACH(h, &p->head, entries) {
453 069bbb86 2022-03-07 thomas tryagain:
454 069bbb86 2022-03-07 thomas err = locate_hunk(orig, h, &lineno);
455 069bbb86 2022-03-07 thomas if (err != NULL)
456 069bbb86 2022-03-07 thomas goto done;
457 069bbb86 2022-03-07 thomas if ((pos = ftello(orig)) == -1) {
458 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
459 069bbb86 2022-03-07 thomas goto done;
460 069bbb86 2022-03-07 thomas }
461 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, pos);
462 069bbb86 2022-03-07 thomas if (err != NULL)
463 069bbb86 2022-03-07 thomas goto done;
464 069bbb86 2022-03-07 thomas copypos = pos;
465 069bbb86 2022-03-07 thomas
466 069bbb86 2022-03-07 thomas err = test_hunk(orig, h);
467 069bbb86 2022-03-07 thomas if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
468 069bbb86 2022-03-07 thomas /*
469 069bbb86 2022-03-07 thomas * try to apply the hunk again starting the search
470 069bbb86 2022-03-07 thomas * after the previous partial match.
471 069bbb86 2022-03-07 thomas */
472 069bbb86 2022-03-07 thomas if (fseek(orig, pos, SEEK_SET) == -1) {
473 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
474 069bbb86 2022-03-07 thomas goto done;
475 069bbb86 2022-03-07 thomas }
476 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, orig);
477 069bbb86 2022-03-07 thomas if (linelen == -1) {
478 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
479 069bbb86 2022-03-07 thomas goto done;
480 069bbb86 2022-03-07 thomas }
481 069bbb86 2022-03-07 thomas lineno++;
482 069bbb86 2022-03-07 thomas goto tryagain;
483 069bbb86 2022-03-07 thomas }
484 069bbb86 2022-03-07 thomas if (err != NULL)
485 069bbb86 2022-03-07 thomas goto done;
486 069bbb86 2022-03-07 thomas
487 069bbb86 2022-03-07 thomas err = apply_hunk(tmp, h, &lineno);
488 069bbb86 2022-03-07 thomas if (err != NULL)
489 069bbb86 2022-03-07 thomas goto done;
490 069bbb86 2022-03-07 thomas
491 069bbb86 2022-03-07 thomas copypos = ftello(orig);
492 069bbb86 2022-03-07 thomas if (copypos == -1) {
493 069bbb86 2022-03-07 thomas err = got_error_from_errno("ftello");
494 069bbb86 2022-03-07 thomas goto done;
495 069bbb86 2022-03-07 thomas }
496 069bbb86 2022-03-07 thomas }
497 069bbb86 2022-03-07 thomas
498 069bbb86 2022-03-07 thomas if (!feof(orig)) {
499 069bbb86 2022-03-07 thomas err = copy(tmp, orig, copypos, -1);
500 069bbb86 2022-03-07 thomas if (err)
501 069bbb86 2022-03-07 thomas goto done;
502 069bbb86 2022-03-07 thomas }
503 069bbb86 2022-03-07 thomas
504 069bbb86 2022-03-07 thomas rename:
505 069bbb86 2022-03-07 thomas if (rename(tmppath, path) == -1) {
506 069bbb86 2022-03-07 thomas err = got_error_from_errno3("rename", tmppath, path);
507 069bbb86 2022-03-07 thomas goto done;
508 069bbb86 2022-03-07 thomas }
509 069bbb86 2022-03-07 thomas
510 069bbb86 2022-03-07 thomas if (p->old == NULL)
511 069bbb86 2022-03-07 thomas err = got_worktree_schedule_add(worktree, &paths,
512 de3c84b6 2022-03-08 thomas add_cb, add_arg, repo, 1);
513 069bbb86 2022-03-07 thomas else
514 069bbb86 2022-03-07 thomas printf("M %s\n", path); /* XXX */
515 069bbb86 2022-03-07 thomas done:
516 94af5a06 2022-03-08 thomas free(template);
517 069bbb86 2022-03-07 thomas if (err != NULL && p->old == NULL && path != NULL)
518 069bbb86 2022-03-07 thomas unlink(path);
519 069bbb86 2022-03-07 thomas if (tmp != NULL)
520 069bbb86 2022-03-07 thomas fclose(tmp);
521 069bbb86 2022-03-07 thomas if (tmppath != NULL)
522 069bbb86 2022-03-07 thomas unlink(tmppath);
523 069bbb86 2022-03-07 thomas free(tmppath);
524 069bbb86 2022-03-07 thomas if (orig != NULL) {
525 069bbb86 2022-03-07 thomas if (p->old == NULL && err != NULL)
526 069bbb86 2022-03-07 thomas unlink(path);
527 069bbb86 2022-03-07 thomas fclose(orig);
528 069bbb86 2022-03-07 thomas }
529 069bbb86 2022-03-07 thomas free(path);
530 069bbb86 2022-03-07 thomas free(line);
531 069bbb86 2022-03-07 thomas got_pathlist_free(&paths);
532 069bbb86 2022-03-07 thomas return err;
533 069bbb86 2022-03-07 thomas }
534 069bbb86 2022-03-07 thomas
535 069bbb86 2022-03-07 thomas const struct got_error *
536 069bbb86 2022-03-07 thomas got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
537 de3c84b6 2022-03-08 thomas got_worktree_delete_cb delete_cb, void *delete_arg,
538 de3c84b6 2022-03-08 thomas got_worktree_checkout_cb add_cb, void *add_arg)
539 069bbb86 2022-03-07 thomas {
540 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
541 069bbb86 2022-03-07 thomas struct imsgbuf *ibuf;
542 069bbb86 2022-03-07 thomas int imsg_fds[2] = {-1, -1};
543 069bbb86 2022-03-07 thomas int done = 0;
544 069bbb86 2022-03-07 thomas pid_t pid;
545 069bbb86 2022-03-07 thomas
546 069bbb86 2022-03-07 thomas ibuf = calloc(1, sizeof(*ibuf));
547 069bbb86 2022-03-07 thomas if (ibuf == NULL) {
548 069bbb86 2022-03-07 thomas err = got_error_from_errno("calloc");
549 069bbb86 2022-03-07 thomas goto done;
550 069bbb86 2022-03-07 thomas }
551 069bbb86 2022-03-07 thomas
552 069bbb86 2022-03-07 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
553 069bbb86 2022-03-07 thomas err = got_error_from_errno("socketpair");
554 069bbb86 2022-03-07 thomas goto done;
555 069bbb86 2022-03-07 thomas }
556 069bbb86 2022-03-07 thomas
557 069bbb86 2022-03-07 thomas pid = fork();
558 069bbb86 2022-03-07 thomas if (pid == -1) {
559 069bbb86 2022-03-07 thomas err = got_error_from_errno("fork");
560 069bbb86 2022-03-07 thomas goto done;
561 069bbb86 2022-03-07 thomas } else if (pid == 0) {
562 069bbb86 2022-03-07 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
563 069bbb86 2022-03-07 thomas NULL);
564 069bbb86 2022-03-07 thomas /* not reached */
565 069bbb86 2022-03-07 thomas }
566 069bbb86 2022-03-07 thomas
567 069bbb86 2022-03-07 thomas if (close(imsg_fds[1]) == -1) {
568 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
569 069bbb86 2022-03-07 thomas goto done;
570 069bbb86 2022-03-07 thomas }
571 069bbb86 2022-03-07 thomas imsg_fds[1] = -1;
572 069bbb86 2022-03-07 thomas imsg_init(ibuf, imsg_fds[0]);
573 069bbb86 2022-03-07 thomas
574 069bbb86 2022-03-07 thomas err = send_patch(ibuf, fd);
575 069bbb86 2022-03-07 thomas fd = -1;
576 069bbb86 2022-03-07 thomas if (err)
577 069bbb86 2022-03-07 thomas goto done;
578 069bbb86 2022-03-07 thomas
579 069bbb86 2022-03-07 thomas while (!done && err == NULL) {
580 069bbb86 2022-03-07 thomas struct got_patch p;
581 069bbb86 2022-03-07 thomas
582 069bbb86 2022-03-07 thomas err = recv_patch(ibuf, &done, &p);
583 069bbb86 2022-03-07 thomas if (err || done)
584 069bbb86 2022-03-07 thomas break;
585 069bbb86 2022-03-07 thomas
586 de3c84b6 2022-03-08 thomas err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
587 de3c84b6 2022-03-08 thomas add_cb, add_arg);
588 069bbb86 2022-03-07 thomas patch_free(&p);
589 069bbb86 2022-03-07 thomas if (err)
590 069bbb86 2022-03-07 thomas break;
591 069bbb86 2022-03-07 thomas }
592 069bbb86 2022-03-07 thomas
593 069bbb86 2022-03-07 thomas done:
594 069bbb86 2022-03-07 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
595 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
596 069bbb86 2022-03-07 thomas if (ibuf != NULL)
597 069bbb86 2022-03-07 thomas imsg_clear(ibuf);
598 069bbb86 2022-03-07 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
599 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
600 069bbb86 2022-03-07 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
601 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
602 069bbb86 2022-03-07 thomas return err;
603 069bbb86 2022-03-07 thomas }