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