Blame


1 069bbb86 2022-03-07 thomas /*
2 069bbb86 2022-03-07 thomas * Copyright 1986, Larry Wall
3 39807ab2 2022-05-12 thomas *
4 069bbb86 2022-03-07 thomas * Redistribution and use in source and binary forms, with or without
5 069bbb86 2022-03-07 thomas * modification, are permitted provided that the following condition is met:
6 069bbb86 2022-03-07 thomas * 1. Redistributions of source code must retain the above copyright notice,
7 069bbb86 2022-03-07 thomas * this condition and the following disclaimer.
8 39807ab2 2022-05-12 thomas *
9 069bbb86 2022-03-07 thomas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 069bbb86 2022-03-07 thomas * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 069bbb86 2022-03-07 thomas * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 069bbb86 2022-03-07 thomas * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 069bbb86 2022-03-07 thomas * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 069bbb86 2022-03-07 thomas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 069bbb86 2022-03-07 thomas * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 069bbb86 2022-03-07 thomas * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 069bbb86 2022-03-07 thomas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 069bbb86 2022-03-07 thomas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19 069bbb86 2022-03-07 thomas * SUCH DAMAGE.
20 069bbb86 2022-03-07 thomas */
21 069bbb86 2022-03-07 thomas
22 069bbb86 2022-03-07 thomas /*
23 069bbb86 2022-03-07 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
24 069bbb86 2022-03-07 thomas *
25 069bbb86 2022-03-07 thomas * Permission to use, copy, modify, and distribute this software for any
26 069bbb86 2022-03-07 thomas * purpose with or without fee is hereby granted, provided that the above
27 069bbb86 2022-03-07 thomas * copyright notice and this permission notice appear in all copies.
28 069bbb86 2022-03-07 thomas *
29 069bbb86 2022-03-07 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
30 069bbb86 2022-03-07 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
31 069bbb86 2022-03-07 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
32 069bbb86 2022-03-07 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
33 069bbb86 2022-03-07 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
34 069bbb86 2022-03-07 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
35 069bbb86 2022-03-07 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 069bbb86 2022-03-07 thomas */
37 069bbb86 2022-03-07 thomas
38 069bbb86 2022-03-07 thomas #include <sys/types.h>
39 069bbb86 2022-03-07 thomas #include <sys/uio.h>
40 069bbb86 2022-03-07 thomas
41 069bbb86 2022-03-07 thomas #include <ctype.h>
42 069bbb86 2022-03-07 thomas #include <limits.h>
43 069bbb86 2022-03-07 thomas #include <paths.h>
44 069bbb86 2022-03-07 thomas #include <stdint.h>
45 069bbb86 2022-03-07 thomas #include <stdio.h>
46 069bbb86 2022-03-07 thomas #include <stdlib.h>
47 069bbb86 2022-03-07 thomas #include <string.h>
48 069bbb86 2022-03-07 thomas #include <unistd.h>
49 069bbb86 2022-03-07 thomas
50 069bbb86 2022-03-07 thomas #include "got_error.h"
51 069bbb86 2022-03-07 thomas #include "got_object.h"
52 069bbb86 2022-03-07 thomas
53 973f3f6e 2022-03-07 thomas #include "got_compat.h"
54 973f3f6e 2022-03-07 thomas
55 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
56 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
57 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
58 069bbb86 2022-03-07 thomas
59 069bbb86 2022-03-07 thomas struct imsgbuf ibuf;
60 069bbb86 2022-03-07 thomas
61 069bbb86 2022-03-07 thomas static const struct got_error *
62 be53ddb1 2022-03-22 thomas send_patch(const char *oldname, const char *newname, int git)
63 069bbb86 2022-03-07 thomas {
64 069bbb86 2022-03-07 thomas struct got_imsg_patch p;
65 069bbb86 2022-03-07 thomas
66 069bbb86 2022-03-07 thomas memset(&p, 0, sizeof(p));
67 069bbb86 2022-03-07 thomas
68 d9db2ff9 2022-04-16 thomas if (oldname != NULL)
69 069bbb86 2022-03-07 thomas strlcpy(p.old, oldname, sizeof(p.old));
70 be53ddb1 2022-03-22 thomas
71 069bbb86 2022-03-07 thomas if (newname != NULL)
72 069bbb86 2022-03-07 thomas strlcpy(p.new, newname, sizeof(p.new));
73 069bbb86 2022-03-07 thomas
74 d9db2ff9 2022-04-16 thomas p.git = git;
75 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
76 069bbb86 2022-03-07 thomas &p, sizeof(p)) == -1)
77 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
78 069bbb86 2022-03-07 thomas return NULL;
79 069bbb86 2022-03-07 thomas }
80 069bbb86 2022-03-07 thomas
81 069bbb86 2022-03-07 thomas static const struct got_error *
82 069bbb86 2022-03-07 thomas send_patch_done(void)
83 069bbb86 2022-03-07 thomas {
84 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
85 069bbb86 2022-03-07 thomas NULL, 0) == -1)
86 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
87 069bbb86 2022-03-07 thomas if (imsg_flush(&ibuf) == -1)
88 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_flush");
89 069bbb86 2022-03-07 thomas return NULL;
90 069bbb86 2022-03-07 thomas }
91 069bbb86 2022-03-07 thomas
92 069bbb86 2022-03-07 thomas /* based on fetchname from usr.bin/patch/util.c */
93 069bbb86 2022-03-07 thomas static const struct got_error *
94 d9db2ff9 2022-04-16 thomas filename(const char *at, char **name)
95 069bbb86 2022-03-07 thomas {
96 d9db2ff9 2022-04-16 thomas char *tmp, *t;
97 069bbb86 2022-03-07 thomas
98 069bbb86 2022-03-07 thomas *name = NULL;
99 069bbb86 2022-03-07 thomas if (*at == '\0')
100 069bbb86 2022-03-07 thomas return NULL;
101 069bbb86 2022-03-07 thomas
102 069bbb86 2022-03-07 thomas while (isspace((unsigned char)*at))
103 069bbb86 2022-03-07 thomas at++;
104 069bbb86 2022-03-07 thomas
105 069bbb86 2022-03-07 thomas /* files can be created or removed by diffing against /dev/null */
106 17d6446a 2022-03-22 thomas if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
107 069bbb86 2022-03-07 thomas return NULL;
108 069bbb86 2022-03-07 thomas
109 d9db2ff9 2022-04-16 thomas tmp = strdup(at);
110 d9db2ff9 2022-04-16 thomas if (tmp == NULL)
111 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
112 d9db2ff9 2022-04-16 thomas if ((t = strchr(tmp, '\t')) != NULL)
113 d9db2ff9 2022-04-16 thomas *t = '\0';
114 d9db2ff9 2022-04-16 thomas if ((t = strchr(tmp, '\n')) != NULL)
115 d9db2ff9 2022-04-16 thomas *t = '\0';
116 069bbb86 2022-03-07 thomas
117 d9db2ff9 2022-04-16 thomas *name = strdup(tmp);
118 d9db2ff9 2022-04-16 thomas free(tmp);
119 069bbb86 2022-03-07 thomas if (*name == NULL)
120 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
121 069bbb86 2022-03-07 thomas return NULL;
122 069bbb86 2022-03-07 thomas }
123 069bbb86 2022-03-07 thomas
124 069bbb86 2022-03-07 thomas static const struct got_error *
125 3b488fde 2022-05-12 thomas find_patch(int *done, FILE *fp)
126 069bbb86 2022-03-07 thomas {
127 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
128 069bbb86 2022-03-07 thomas char *old = NULL, *new = NULL;
129 069bbb86 2022-03-07 thomas char *line = NULL;
130 069bbb86 2022-03-07 thomas size_t linesize = 0;
131 069bbb86 2022-03-07 thomas ssize_t linelen;
132 8afe1f71 2022-05-12 thomas int create, rename = 0, git = 0;
133 069bbb86 2022-03-07 thomas
134 069bbb86 2022-03-07 thomas while ((linelen = getline(&line, &linesize, fp)) != -1) {
135 069bbb86 2022-03-07 thomas /*
136 069bbb86 2022-03-07 thomas * Ignore the Index name like GNU and larry' patch,
137 069bbb86 2022-03-07 thomas * we don't have to follow POSIX.
138 069bbb86 2022-03-07 thomas */
139 069bbb86 2022-03-07 thomas
140 d9db2ff9 2022-04-16 thomas if (!strncmp(line, "--- ", 4)) {
141 069bbb86 2022-03-07 thomas free(old);
142 d9db2ff9 2022-04-16 thomas err = filename(line+4, &old);
143 8afe1f71 2022-05-12 thomas } else if (rename && !strncmp(line, "rename from ", 12)) {
144 8afe1f71 2022-05-12 thomas free(old);
145 8afe1f71 2022-05-12 thomas err = filename(line+12, &old);
146 069bbb86 2022-03-07 thomas } else if (!strncmp(line, "+++ ", 4)) {
147 069bbb86 2022-03-07 thomas free(new);
148 d9db2ff9 2022-04-16 thomas err = filename(line+4, &new);
149 8afe1f71 2022-05-12 thomas } else if (rename && !strncmp(line, "rename to ", 10)) {
150 8afe1f71 2022-05-12 thomas free(new);
151 8afe1f71 2022-05-12 thomas err = filename(line + 10, &new);
152 8afe1f71 2022-05-12 thomas } else if (git && !strncmp(line, "similarity index 100%", 21))
153 8afe1f71 2022-05-12 thomas rename = 1;
154 8afe1f71 2022-05-12 thomas else if (!strncmp(line, "diff --git a/", 13))
155 069bbb86 2022-03-07 thomas git = 1;
156 069bbb86 2022-03-07 thomas
157 069bbb86 2022-03-07 thomas if (err)
158 8afe1f71 2022-05-12 thomas break;
159 8afe1f71 2022-05-12 thomas
160 8afe1f71 2022-05-12 thomas /*
161 8afe1f71 2022-05-12 thomas * Git-style diffs with "similarity index 100%" don't
162 8afe1f71 2022-05-12 thomas * have any hunks and ends with the "rename to foobar"
163 8afe1f71 2022-05-12 thomas * line.
164 8afe1f71 2022-05-12 thomas */
165 8afe1f71 2022-05-12 thomas if (rename && old != NULL && new != NULL) {
166 3b488fde 2022-05-12 thomas *done = 1;
167 8afe1f71 2022-05-12 thomas err = send_patch(old, new, git);
168 069bbb86 2022-03-07 thomas break;
169 8afe1f71 2022-05-12 thomas }
170 069bbb86 2022-03-07 thomas
171 069bbb86 2022-03-07 thomas if (!strncmp(line, "@@ -", 4)) {
172 069bbb86 2022-03-07 thomas create = !strncmp(line+4, "0,0", 3);
173 069bbb86 2022-03-07 thomas if ((old == NULL && new == NULL) ||
174 069bbb86 2022-03-07 thomas (!create && old == NULL))
175 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
176 069bbb86 2022-03-07 thomas else
177 be53ddb1 2022-03-22 thomas err = send_patch(old, new, git);
178 069bbb86 2022-03-07 thomas
179 069bbb86 2022-03-07 thomas if (err)
180 069bbb86 2022-03-07 thomas break;
181 069bbb86 2022-03-07 thomas
182 069bbb86 2022-03-07 thomas /* rewind to previous line */
183 069bbb86 2022-03-07 thomas if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
184 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
185 069bbb86 2022-03-07 thomas break;
186 069bbb86 2022-03-07 thomas }
187 069bbb86 2022-03-07 thomas }
188 069bbb86 2022-03-07 thomas
189 19f1a2ac 2022-03-13 thomas free(old);
190 19f1a2ac 2022-03-13 thomas free(new);
191 069bbb86 2022-03-07 thomas free(line);
192 069bbb86 2022-03-07 thomas if (ferror(fp) && err == NULL)
193 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
194 069bbb86 2022-03-07 thomas if (feof(fp) && err == NULL)
195 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_NO_PATCH);
196 069bbb86 2022-03-07 thomas return err;
197 069bbb86 2022-03-07 thomas }
198 069bbb86 2022-03-07 thomas
199 069bbb86 2022-03-07 thomas static const struct got_error *
200 069bbb86 2022-03-07 thomas strtolnum(char **str, long *n)
201 069bbb86 2022-03-07 thomas {
202 069bbb86 2022-03-07 thomas char *p, c;
203 069bbb86 2022-03-07 thomas const char *errstr;
204 069bbb86 2022-03-07 thomas
205 069bbb86 2022-03-07 thomas for (p = *str; isdigit((unsigned char)*p); ++p)
206 069bbb86 2022-03-07 thomas /* nop */;
207 069bbb86 2022-03-07 thomas
208 069bbb86 2022-03-07 thomas c = *p;
209 069bbb86 2022-03-07 thomas *p = '\0';
210 069bbb86 2022-03-07 thomas
211 069bbb86 2022-03-07 thomas *n = strtonum(*str, 0, LONG_MAX, &errstr);
212 069bbb86 2022-03-07 thomas if (errstr != NULL)
213 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
214 069bbb86 2022-03-07 thomas
215 069bbb86 2022-03-07 thomas *p = c;
216 069bbb86 2022-03-07 thomas *str = p;
217 069bbb86 2022-03-07 thomas return NULL;
218 069bbb86 2022-03-07 thomas }
219 069bbb86 2022-03-07 thomas
220 069bbb86 2022-03-07 thomas static const struct got_error *
221 1992b6cb 2022-05-12 thomas parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
222 069bbb86 2022-03-07 thomas {
223 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
224 069bbb86 2022-03-07 thomas
225 069bbb86 2022-03-07 thomas if (strncmp(s, "@@ -", 4)) {
226 1992b6cb 2022-05-12 thomas *done = 1;
227 069bbb86 2022-03-07 thomas return NULL;
228 069bbb86 2022-03-07 thomas }
229 069bbb86 2022-03-07 thomas
230 069bbb86 2022-03-07 thomas s += 4;
231 069bbb86 2022-03-07 thomas if (!*s)
232 069bbb86 2022-03-07 thomas return NULL;
233 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldfrom);
234 069bbb86 2022-03-07 thomas if (err)
235 069bbb86 2022-03-07 thomas return err;
236 069bbb86 2022-03-07 thomas if (*s == ',') {
237 069bbb86 2022-03-07 thomas s++;
238 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldlines);
239 069bbb86 2022-03-07 thomas if (err)
240 069bbb86 2022-03-07 thomas return err;
241 069bbb86 2022-03-07 thomas } else
242 069bbb86 2022-03-07 thomas hdr->oldlines = 1;
243 069bbb86 2022-03-07 thomas
244 069bbb86 2022-03-07 thomas if (*s == ' ')
245 069bbb86 2022-03-07 thomas s++;
246 069bbb86 2022-03-07 thomas
247 069bbb86 2022-03-07 thomas if (*s != '+' || !*++s)
248 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
249 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->newfrom);
250 069bbb86 2022-03-07 thomas if (err)
251 069bbb86 2022-03-07 thomas return err;
252 069bbb86 2022-03-07 thomas if (*s == ',') {
253 069bbb86 2022-03-07 thomas s++;
254 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->newlines);
255 069bbb86 2022-03-07 thomas if (err)
256 069bbb86 2022-03-07 thomas return err;
257 069bbb86 2022-03-07 thomas } else
258 069bbb86 2022-03-07 thomas hdr->newlines = 1;
259 069bbb86 2022-03-07 thomas
260 069bbb86 2022-03-07 thomas if (*s == ' ')
261 069bbb86 2022-03-07 thomas s++;
262 069bbb86 2022-03-07 thomas
263 069bbb86 2022-03-07 thomas if (*s != '@')
264 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
265 069bbb86 2022-03-07 thomas
266 069bbb86 2022-03-07 thomas if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
267 069bbb86 2022-03-07 thomas hdr->newfrom >= LONG_MAX - hdr->newlines ||
268 069bbb86 2022-03-07 thomas /* not so sure about this one */
269 069bbb86 2022-03-07 thomas hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
270 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
271 069bbb86 2022-03-07 thomas
272 069bbb86 2022-03-07 thomas if (hdr->oldlines == 0) {
273 069bbb86 2022-03-07 thomas /* larry says to "do append rather than insert"; I don't
274 069bbb86 2022-03-07 thomas * quite get it, but i trust him.
275 069bbb86 2022-03-07 thomas */
276 069bbb86 2022-03-07 thomas hdr->oldfrom++;
277 069bbb86 2022-03-07 thomas }
278 069bbb86 2022-03-07 thomas
279 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
280 069bbb86 2022-03-07 thomas hdr, sizeof(*hdr)) == -1)
281 069bbb86 2022-03-07 thomas return got_error_from_errno(
282 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_HUNK");
283 069bbb86 2022-03-07 thomas return NULL;
284 069bbb86 2022-03-07 thomas }
285 069bbb86 2022-03-07 thomas
286 069bbb86 2022-03-07 thomas static const struct got_error *
287 069bbb86 2022-03-07 thomas send_line(const char *line)
288 069bbb86 2022-03-07 thomas {
289 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
290 069bbb86 2022-03-07 thomas char *p = NULL;
291 069bbb86 2022-03-07 thomas
292 ff7f34d3 2022-03-22 thomas if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
293 069bbb86 2022-03-07 thomas if (asprintf(&p, " %s", line) == -1)
294 069bbb86 2022-03-07 thomas return got_error_from_errno("asprintf");
295 069bbb86 2022-03-07 thomas line = p;
296 069bbb86 2022-03-07 thomas }
297 069bbb86 2022-03-07 thomas
298 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
299 17d6446a 2022-03-22 thomas line, strlen(line) + 1) == -1)
300 069bbb86 2022-03-07 thomas err = got_error_from_errno(
301 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_LINE");
302 069bbb86 2022-03-07 thomas
303 069bbb86 2022-03-07 thomas free(p);
304 069bbb86 2022-03-07 thomas return err;
305 069bbb86 2022-03-07 thomas }
306 069bbb86 2022-03-07 thomas
307 069bbb86 2022-03-07 thomas static const struct got_error *
308 656c2baa 2022-04-23 thomas peek_special_line(FILE *fp)
309 ff7f34d3 2022-03-22 thomas {
310 ff7f34d3 2022-03-22 thomas const struct got_error *err;
311 1f954382 2022-03-22 thomas int ch;
312 ff7f34d3 2022-03-22 thomas
313 ff7f34d3 2022-03-22 thomas ch = fgetc(fp);
314 ff7f34d3 2022-03-22 thomas if (ch != EOF && ch != '\\') {
315 ff7f34d3 2022-03-22 thomas ungetc(ch, fp);
316 ff7f34d3 2022-03-22 thomas return NULL;
317 ff7f34d3 2022-03-22 thomas }
318 ff7f34d3 2022-03-22 thomas
319 656c2baa 2022-04-23 thomas if (ch == '\\') {
320 ff7f34d3 2022-03-22 thomas err = send_line("\\");
321 ff7f34d3 2022-03-22 thomas if (err)
322 ff7f34d3 2022-03-22 thomas return err;
323 ff7f34d3 2022-03-22 thomas }
324 ff7f34d3 2022-03-22 thomas
325 ff7f34d3 2022-03-22 thomas while (ch != EOF && ch != '\n')
326 ff7f34d3 2022-03-22 thomas ch = fgetc(fp);
327 ff7f34d3 2022-03-22 thomas
328 ff7f34d3 2022-03-22 thomas if (ch != EOF || feof(fp))
329 ff7f34d3 2022-03-22 thomas return NULL;
330 ff7f34d3 2022-03-22 thomas return got_error(GOT_ERR_IO);
331 ff7f34d3 2022-03-22 thomas }
332 ff7f34d3 2022-03-22 thomas
333 ff7f34d3 2022-03-22 thomas static const struct got_error *
334 1992b6cb 2022-05-12 thomas parse_hunk(FILE *fp, int *done)
335 069bbb86 2022-03-07 thomas {
336 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
337 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
338 069bbb86 2022-03-07 thomas char *line = NULL, ch;
339 069bbb86 2022-03-07 thomas size_t linesize = 0;
340 069bbb86 2022-03-07 thomas ssize_t linelen;
341 069bbb86 2022-03-07 thomas long leftold, leftnew;
342 069bbb86 2022-03-07 thomas
343 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, fp);
344 069bbb86 2022-03-07 thomas if (linelen == -1) {
345 1992b6cb 2022-05-12 thomas *done = 1;
346 069bbb86 2022-03-07 thomas goto done;
347 069bbb86 2022-03-07 thomas }
348 069bbb86 2022-03-07 thomas
349 1992b6cb 2022-05-12 thomas err = parse_hdr(line, done, &hdr);
350 069bbb86 2022-03-07 thomas if (err)
351 069bbb86 2022-03-07 thomas goto done;
352 1992b6cb 2022-05-12 thomas if (*done) {
353 069bbb86 2022-03-07 thomas if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
354 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
355 069bbb86 2022-03-07 thomas goto done;
356 069bbb86 2022-03-07 thomas }
357 069bbb86 2022-03-07 thomas
358 069bbb86 2022-03-07 thomas leftold = hdr.oldlines;
359 069bbb86 2022-03-07 thomas leftnew = hdr.newlines;
360 069bbb86 2022-03-07 thomas
361 069bbb86 2022-03-07 thomas while (leftold > 0 || leftnew > 0) {
362 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, fp);
363 069bbb86 2022-03-07 thomas if (linelen == -1) {
364 069bbb86 2022-03-07 thomas if (ferror(fp)) {
365 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
366 069bbb86 2022-03-07 thomas goto done;
367 069bbb86 2022-03-07 thomas }
368 069bbb86 2022-03-07 thomas
369 069bbb86 2022-03-07 thomas /* trailing newlines may be chopped */
370 069bbb86 2022-03-07 thomas if (leftold < 3 && leftnew < 3) {
371 1992b6cb 2022-05-12 thomas *done = 1;
372 069bbb86 2022-03-07 thomas break;
373 069bbb86 2022-03-07 thomas }
374 069bbb86 2022-03-07 thomas
375 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_TRUNCATED);
376 069bbb86 2022-03-07 thomas goto done;
377 069bbb86 2022-03-07 thomas }
378 ff7f34d3 2022-03-22 thomas if (line[linelen - 1] == '\n')
379 ff7f34d3 2022-03-22 thomas line[linelen - 1] = '\0';
380 069bbb86 2022-03-07 thomas
381 069bbb86 2022-03-07 thomas /* usr.bin/patch allows '=' as context char */
382 069bbb86 2022-03-07 thomas if (*line == '=')
383 069bbb86 2022-03-07 thomas *line = ' ';
384 069bbb86 2022-03-07 thomas
385 069bbb86 2022-03-07 thomas ch = *line;
386 ff7f34d3 2022-03-22 thomas if (ch == '\t' || ch == '\0')
387 069bbb86 2022-03-07 thomas ch = ' '; /* the space got eaten */
388 069bbb86 2022-03-07 thomas
389 069bbb86 2022-03-07 thomas switch (ch) {
390 069bbb86 2022-03-07 thomas case '-':
391 069bbb86 2022-03-07 thomas leftold--;
392 069bbb86 2022-03-07 thomas break;
393 069bbb86 2022-03-07 thomas case ' ':
394 069bbb86 2022-03-07 thomas leftold--;
395 069bbb86 2022-03-07 thomas leftnew--;
396 069bbb86 2022-03-07 thomas break;
397 069bbb86 2022-03-07 thomas case '+':
398 069bbb86 2022-03-07 thomas leftnew--;
399 069bbb86 2022-03-07 thomas break;
400 069bbb86 2022-03-07 thomas default:
401 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
402 069bbb86 2022-03-07 thomas goto done;
403 069bbb86 2022-03-07 thomas }
404 069bbb86 2022-03-07 thomas
405 069bbb86 2022-03-07 thomas if (leftold < 0 || leftnew < 0) {
406 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
407 069bbb86 2022-03-07 thomas goto done;
408 069bbb86 2022-03-07 thomas }
409 069bbb86 2022-03-07 thomas
410 069bbb86 2022-03-07 thomas err = send_line(line);
411 069bbb86 2022-03-07 thomas if (err)
412 069bbb86 2022-03-07 thomas goto done;
413 ff7f34d3 2022-03-22 thomas
414 ff7f34d3 2022-03-22 thomas if ((ch == '-' && leftold == 0) ||
415 ff7f34d3 2022-03-22 thomas (ch == '+' && leftnew == 0)) {
416 656c2baa 2022-04-23 thomas err = peek_special_line(fp);
417 ff7f34d3 2022-03-22 thomas if (err)
418 ff7f34d3 2022-03-22 thomas goto done;
419 ff7f34d3 2022-03-22 thomas }
420 069bbb86 2022-03-07 thomas }
421 069bbb86 2022-03-07 thomas
422 069bbb86 2022-03-07 thomas done:
423 069bbb86 2022-03-07 thomas free(line);
424 069bbb86 2022-03-07 thomas return err;
425 069bbb86 2022-03-07 thomas }
426 069bbb86 2022-03-07 thomas
427 069bbb86 2022-03-07 thomas static const struct got_error *
428 069bbb86 2022-03-07 thomas read_patch(struct imsgbuf *ibuf, int fd)
429 069bbb86 2022-03-07 thomas {
430 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
431 069bbb86 2022-03-07 thomas FILE *fp;
432 8afe1f71 2022-05-12 thomas int patch_found = 0;
433 069bbb86 2022-03-07 thomas
434 069bbb86 2022-03-07 thomas if ((fp = fdopen(fd, "r")) == NULL) {
435 069bbb86 2022-03-07 thomas err = got_error_from_errno("fdopen");
436 069bbb86 2022-03-07 thomas close(fd);
437 069bbb86 2022-03-07 thomas return err;
438 069bbb86 2022-03-07 thomas }
439 069bbb86 2022-03-07 thomas
440 069bbb86 2022-03-07 thomas while (!feof(fp)) {
441 1992b6cb 2022-05-12 thomas int done = 0;
442 8afe1f71 2022-05-12 thomas
443 1992b6cb 2022-05-12 thomas err = find_patch(&done, fp);
444 069bbb86 2022-03-07 thomas if (err)
445 069bbb86 2022-03-07 thomas goto done;
446 069bbb86 2022-03-07 thomas
447 069bbb86 2022-03-07 thomas patch_found = 1;
448 1992b6cb 2022-05-12 thomas
449 1992b6cb 2022-05-12 thomas while (!done) {
450 1992b6cb 2022-05-12 thomas err = parse_hunk(fp, &done);
451 069bbb86 2022-03-07 thomas if (err)
452 069bbb86 2022-03-07 thomas goto done;
453 069bbb86 2022-03-07 thomas }
454 1992b6cb 2022-05-12 thomas
455 1992b6cb 2022-05-12 thomas err = send_patch_done();
456 1992b6cb 2022-05-12 thomas if (err)
457 1992b6cb 2022-05-12 thomas goto done;
458 069bbb86 2022-03-07 thomas }
459 069bbb86 2022-03-07 thomas
460 069bbb86 2022-03-07 thomas done:
461 069bbb86 2022-03-07 thomas fclose(fp);
462 069bbb86 2022-03-07 thomas
463 069bbb86 2022-03-07 thomas /* ignore trailing gibberish */
464 069bbb86 2022-03-07 thomas if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
465 069bbb86 2022-03-07 thomas err = NULL;
466 069bbb86 2022-03-07 thomas
467 069bbb86 2022-03-07 thomas return err;
468 069bbb86 2022-03-07 thomas }
469 069bbb86 2022-03-07 thomas
470 069bbb86 2022-03-07 thomas int
471 069bbb86 2022-03-07 thomas main(int argc, char **argv)
472 069bbb86 2022-03-07 thomas {
473 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
474 069bbb86 2022-03-07 thomas struct imsg imsg;
475 069bbb86 2022-03-07 thomas #if 0
476 069bbb86 2022-03-07 thomas static int attached;
477 069bbb86 2022-03-07 thomas while (!attached)
478 069bbb86 2022-03-07 thomas sleep(1);
479 069bbb86 2022-03-07 thomas #endif
480 069bbb86 2022-03-07 thomas
481 069bbb86 2022-03-07 thomas imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
482 069bbb86 2022-03-07 thomas #ifndef PROFILE
483 069bbb86 2022-03-07 thomas /* revoke access to most system calls */
484 069bbb86 2022-03-07 thomas if (pledge("stdio recvfd", NULL) == -1) {
485 069bbb86 2022-03-07 thomas err = got_error_from_errno("pledge");
486 069bbb86 2022-03-07 thomas got_privsep_send_error(&ibuf, err);
487 069bbb86 2022-03-07 thomas return 1;
488 069bbb86 2022-03-07 thomas }
489 762ddcd8 2022-03-09 thomas
490 762ddcd8 2022-03-09 thomas /* revoke fs access */
491 762ddcd8 2022-03-09 thomas if (landlock_no_fs() == -1) {
492 762ddcd8 2022-03-09 thomas err = got_error_from_errno("landlock_no_fs");
493 762ddcd8 2022-03-09 thomas got_privsep_send_error(&ibuf, err);
494 762ddcd8 2022-03-09 thomas return 1;
495 762ddcd8 2022-03-09 thomas }
496 069bbb86 2022-03-07 thomas #endif
497 069bbb86 2022-03-07 thomas
498 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
499 069bbb86 2022-03-07 thomas if (err)
500 069bbb86 2022-03-07 thomas goto done;
501 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
502 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
503 069bbb86 2022-03-07 thomas goto done;
504 069bbb86 2022-03-07 thomas }
505 069bbb86 2022-03-07 thomas
506 069bbb86 2022-03-07 thomas err = read_patch(&ibuf, imsg.fd);
507 069bbb86 2022-03-07 thomas if (err)
508 069bbb86 2022-03-07 thomas goto done;
509 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
510 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
511 069bbb86 2022-03-07 thomas err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
512 069bbb86 2022-03-07 thomas goto done;
513 069bbb86 2022-03-07 thomas }
514 069bbb86 2022-03-07 thomas err = got_privsep_flush_imsg(&ibuf);
515 069bbb86 2022-03-07 thomas done:
516 069bbb86 2022-03-07 thomas imsg_free(&imsg);
517 069bbb86 2022-03-07 thomas if (err != NULL) {
518 069bbb86 2022-03-07 thomas got_privsep_send_error(&ibuf, err);
519 069bbb86 2022-03-07 thomas err = NULL;
520 069bbb86 2022-03-07 thomas }
521 069bbb86 2022-03-07 thomas if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
522 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
523 069bbb86 2022-03-07 thomas if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
524 069bbb86 2022-03-07 thomas fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
525 069bbb86 2022-03-07 thomas return err ? 1 : 0;
526 069bbb86 2022-03-07 thomas }