Blame


1 069bbb86 2022-03-07 thomas /*
2 069bbb86 2022-03-07 thomas * Copyright 1986, Larry Wall
3 069bbb86 2022-03-07 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 069bbb86 2022-03-07 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/queue.h>
40 069bbb86 2022-03-07 thomas #include <sys/uio.h>
41 069bbb86 2022-03-07 thomas
42 069bbb86 2022-03-07 thomas #include <ctype.h>
43 069bbb86 2022-03-07 thomas #include <limits.h>
44 069bbb86 2022-03-07 thomas #include <paths.h>
45 069bbb86 2022-03-07 thomas #include <sha1.h>
46 069bbb86 2022-03-07 thomas #include <stdint.h>
47 069bbb86 2022-03-07 thomas #include <stdio.h>
48 069bbb86 2022-03-07 thomas #include <stdlib.h>
49 069bbb86 2022-03-07 thomas #include <string.h>
50 069bbb86 2022-03-07 thomas #include <unistd.h>
51 069bbb86 2022-03-07 thomas #include <imsg.h>
52 069bbb86 2022-03-07 thomas
53 069bbb86 2022-03-07 thomas #include "got_error.h"
54 069bbb86 2022-03-07 thomas #include "got_object.h"
55 069bbb86 2022-03-07 thomas
56 069bbb86 2022-03-07 thomas #include "got_lib_delta.h"
57 069bbb86 2022-03-07 thomas #include "got_lib_object.h"
58 069bbb86 2022-03-07 thomas #include "got_lib_privsep.h"
59 069bbb86 2022-03-07 thomas
60 069bbb86 2022-03-07 thomas struct imsgbuf ibuf;
61 069bbb86 2022-03-07 thomas
62 069bbb86 2022-03-07 thomas static const struct got_error *
63 069bbb86 2022-03-07 thomas send_patch(const char *oldname, const char *newname)
64 069bbb86 2022-03-07 thomas {
65 069bbb86 2022-03-07 thomas struct got_imsg_patch p;
66 069bbb86 2022-03-07 thomas
67 069bbb86 2022-03-07 thomas memset(&p, 0, sizeof(p));
68 069bbb86 2022-03-07 thomas
69 069bbb86 2022-03-07 thomas if (oldname != NULL)
70 069bbb86 2022-03-07 thomas strlcpy(p.old, oldname, sizeof(p.old));
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 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
75 069bbb86 2022-03-07 thomas &p, sizeof(p)) == -1)
76 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
77 069bbb86 2022-03-07 thomas return NULL;
78 069bbb86 2022-03-07 thomas }
79 069bbb86 2022-03-07 thomas
80 069bbb86 2022-03-07 thomas static const struct got_error *
81 069bbb86 2022-03-07 thomas send_patch_done(void)
82 069bbb86 2022-03-07 thomas {
83 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
84 069bbb86 2022-03-07 thomas NULL, 0) == -1)
85 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
86 069bbb86 2022-03-07 thomas if (imsg_flush(&ibuf) == -1)
87 069bbb86 2022-03-07 thomas return got_error_from_errno("imsg_flush");
88 069bbb86 2022-03-07 thomas return NULL;
89 069bbb86 2022-03-07 thomas }
90 069bbb86 2022-03-07 thomas
91 069bbb86 2022-03-07 thomas /* based on fetchname from usr.bin/patch/util.c */
92 069bbb86 2022-03-07 thomas static const struct got_error *
93 069bbb86 2022-03-07 thomas filename(const char *at, char **name, int strip)
94 069bbb86 2022-03-07 thomas {
95 069bbb86 2022-03-07 thomas char *fullname, *t;
96 069bbb86 2022-03-07 thomas int l, tab;
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 069bbb86 2022-03-07 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 069bbb86 2022-03-07 thomas t = strdup(at);
110 069bbb86 2022-03-07 thomas if (t == NULL)
111 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
112 069bbb86 2022-03-07 thomas *name = fullname = t;
113 069bbb86 2022-03-07 thomas tab = strchr(t, '\t') != NULL;
114 069bbb86 2022-03-07 thomas
115 069bbb86 2022-03-07 thomas /* strip off path components and NUL-terminate */
116 069bbb86 2022-03-07 thomas for (l = strip;
117 069bbb86 2022-03-07 thomas *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
118 069bbb86 2022-03-07 thomas ++t) {
119 069bbb86 2022-03-07 thomas if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
120 069bbb86 2022-03-07 thomas if (--l >= 0)
121 069bbb86 2022-03-07 thomas *name = t+1;
122 069bbb86 2022-03-07 thomas }
123 069bbb86 2022-03-07 thomas *t = '\0';
124 069bbb86 2022-03-07 thomas
125 069bbb86 2022-03-07 thomas *name = strdup(*name);
126 069bbb86 2022-03-07 thomas free(fullname);
127 069bbb86 2022-03-07 thomas if (*name == NULL)
128 069bbb86 2022-03-07 thomas return got_error_from_errno("strdup");
129 069bbb86 2022-03-07 thomas return NULL;
130 069bbb86 2022-03-07 thomas }
131 069bbb86 2022-03-07 thomas
132 069bbb86 2022-03-07 thomas static const struct got_error *
133 069bbb86 2022-03-07 thomas find_patch(FILE *fp)
134 069bbb86 2022-03-07 thomas {
135 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
136 069bbb86 2022-03-07 thomas char *old = NULL, *new = NULL;
137 069bbb86 2022-03-07 thomas char *line = NULL;
138 069bbb86 2022-03-07 thomas size_t linesize = 0;
139 069bbb86 2022-03-07 thomas ssize_t linelen;
140 069bbb86 2022-03-07 thomas int create, git = 0;
141 069bbb86 2022-03-07 thomas
142 069bbb86 2022-03-07 thomas while ((linelen = getline(&line, &linesize, fp)) != -1) {
143 069bbb86 2022-03-07 thomas /*
144 069bbb86 2022-03-07 thomas * Ignore the Index name like GNU and larry' patch,
145 069bbb86 2022-03-07 thomas * we don't have to follow POSIX.
146 069bbb86 2022-03-07 thomas */
147 069bbb86 2022-03-07 thomas
148 069bbb86 2022-03-07 thomas if (git && !strncmp(line, "--- a/", 6)) {
149 069bbb86 2022-03-07 thomas free(old);
150 069bbb86 2022-03-07 thomas err = filename(line+6, &old, 0);
151 069bbb86 2022-03-07 thomas } else if (!strncmp(line, "--- ", 4)) {
152 069bbb86 2022-03-07 thomas free(old);
153 069bbb86 2022-03-07 thomas err = filename(line+4, &old, 0);
154 069bbb86 2022-03-07 thomas } else if (git && !strncmp(line, "+++ b/", 6)) {
155 069bbb86 2022-03-07 thomas free(new);
156 069bbb86 2022-03-07 thomas err = filename(line+6, &new, 0);
157 069bbb86 2022-03-07 thomas } else if (!strncmp(line, "+++ ", 4)) {
158 069bbb86 2022-03-07 thomas free(new);
159 069bbb86 2022-03-07 thomas err = filename(line+4, &new, 0);
160 069bbb86 2022-03-07 thomas } else if (!strncmp(line, "diff --git a/", 13))
161 069bbb86 2022-03-07 thomas git = 1;
162 069bbb86 2022-03-07 thomas
163 069bbb86 2022-03-07 thomas if (err)
164 069bbb86 2022-03-07 thomas break;
165 069bbb86 2022-03-07 thomas
166 069bbb86 2022-03-07 thomas if (!strncmp(line, "@@ -", 4)) {
167 069bbb86 2022-03-07 thomas create = !strncmp(line+4, "0,0", 3);
168 069bbb86 2022-03-07 thomas if ((old == NULL && new == NULL) ||
169 069bbb86 2022-03-07 thomas (!create && old == NULL))
170 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
171 069bbb86 2022-03-07 thomas else
172 069bbb86 2022-03-07 thomas err = send_patch(old, new);
173 069bbb86 2022-03-07 thomas
174 069bbb86 2022-03-07 thomas free(old);
175 069bbb86 2022-03-07 thomas free(new);
176 069bbb86 2022-03-07 thomas
177 069bbb86 2022-03-07 thomas if (err)
178 069bbb86 2022-03-07 thomas break;
179 069bbb86 2022-03-07 thomas
180 069bbb86 2022-03-07 thomas /* rewind to previous line */
181 069bbb86 2022-03-07 thomas if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
182 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
183 069bbb86 2022-03-07 thomas break;
184 069bbb86 2022-03-07 thomas }
185 069bbb86 2022-03-07 thomas }
186 069bbb86 2022-03-07 thomas
187 069bbb86 2022-03-07 thomas free(line);
188 069bbb86 2022-03-07 thomas if (ferror(fp) && err == NULL)
189 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
190 069bbb86 2022-03-07 thomas if (feof(fp) && err == NULL)
191 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_NO_PATCH);
192 069bbb86 2022-03-07 thomas return err;
193 069bbb86 2022-03-07 thomas }
194 069bbb86 2022-03-07 thomas
195 069bbb86 2022-03-07 thomas static const struct got_error *
196 069bbb86 2022-03-07 thomas strtolnum(char **str, long *n)
197 069bbb86 2022-03-07 thomas {
198 069bbb86 2022-03-07 thomas char *p, c;
199 069bbb86 2022-03-07 thomas const char *errstr;
200 069bbb86 2022-03-07 thomas
201 069bbb86 2022-03-07 thomas for (p = *str; isdigit((unsigned char)*p); ++p)
202 069bbb86 2022-03-07 thomas /* nop */;
203 069bbb86 2022-03-07 thomas
204 069bbb86 2022-03-07 thomas c = *p;
205 069bbb86 2022-03-07 thomas *p = '\0';
206 069bbb86 2022-03-07 thomas
207 069bbb86 2022-03-07 thomas *n = strtonum(*str, 0, LONG_MAX, &errstr);
208 069bbb86 2022-03-07 thomas if (errstr != NULL)
209 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
210 069bbb86 2022-03-07 thomas
211 069bbb86 2022-03-07 thomas *p = c;
212 069bbb86 2022-03-07 thomas *str = p;
213 069bbb86 2022-03-07 thomas return NULL;
214 069bbb86 2022-03-07 thomas }
215 069bbb86 2022-03-07 thomas
216 069bbb86 2022-03-07 thomas static const struct got_error *
217 069bbb86 2022-03-07 thomas parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
218 069bbb86 2022-03-07 thomas {
219 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
220 069bbb86 2022-03-07 thomas
221 069bbb86 2022-03-07 thomas *ok = 1;
222 069bbb86 2022-03-07 thomas if (strncmp(s, "@@ -", 4)) {
223 069bbb86 2022-03-07 thomas *ok = 0;
224 069bbb86 2022-03-07 thomas return NULL;
225 069bbb86 2022-03-07 thomas }
226 069bbb86 2022-03-07 thomas
227 069bbb86 2022-03-07 thomas s += 4;
228 069bbb86 2022-03-07 thomas if (!*s)
229 069bbb86 2022-03-07 thomas return NULL;
230 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldfrom);
231 069bbb86 2022-03-07 thomas if (err)
232 069bbb86 2022-03-07 thomas return err;
233 069bbb86 2022-03-07 thomas if (*s == ',') {
234 069bbb86 2022-03-07 thomas s++;
235 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->oldlines);
236 069bbb86 2022-03-07 thomas if (err)
237 069bbb86 2022-03-07 thomas return err;
238 069bbb86 2022-03-07 thomas } else
239 069bbb86 2022-03-07 thomas hdr->oldlines = 1;
240 069bbb86 2022-03-07 thomas
241 069bbb86 2022-03-07 thomas if (*s == ' ')
242 069bbb86 2022-03-07 thomas s++;
243 069bbb86 2022-03-07 thomas
244 069bbb86 2022-03-07 thomas if (*s != '+' || !*++s)
245 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
246 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->newfrom);
247 069bbb86 2022-03-07 thomas if (err)
248 069bbb86 2022-03-07 thomas return err;
249 069bbb86 2022-03-07 thomas if (*s == ',') {
250 069bbb86 2022-03-07 thomas s++;
251 069bbb86 2022-03-07 thomas err = strtolnum(&s, &hdr->newlines);
252 069bbb86 2022-03-07 thomas if (err)
253 069bbb86 2022-03-07 thomas return err;
254 069bbb86 2022-03-07 thomas } else
255 069bbb86 2022-03-07 thomas hdr->newlines = 1;
256 069bbb86 2022-03-07 thomas
257 069bbb86 2022-03-07 thomas if (*s == ' ')
258 069bbb86 2022-03-07 thomas s++;
259 069bbb86 2022-03-07 thomas
260 069bbb86 2022-03-07 thomas if (*s != '@')
261 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
262 069bbb86 2022-03-07 thomas
263 069bbb86 2022-03-07 thomas if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
264 069bbb86 2022-03-07 thomas hdr->newfrom >= LONG_MAX - hdr->newlines ||
265 069bbb86 2022-03-07 thomas /* not so sure about this one */
266 069bbb86 2022-03-07 thomas hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
267 069bbb86 2022-03-07 thomas return got_error(GOT_ERR_PATCH_MALFORMED);
268 069bbb86 2022-03-07 thomas
269 069bbb86 2022-03-07 thomas if (hdr->oldlines == 0) {
270 069bbb86 2022-03-07 thomas /* larry says to "do append rather than insert"; I don't
271 069bbb86 2022-03-07 thomas * quite get it, but i trust him.
272 069bbb86 2022-03-07 thomas */
273 069bbb86 2022-03-07 thomas hdr->oldfrom++;
274 069bbb86 2022-03-07 thomas }
275 069bbb86 2022-03-07 thomas
276 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
277 069bbb86 2022-03-07 thomas hdr, sizeof(*hdr)) == -1)
278 069bbb86 2022-03-07 thomas return got_error_from_errno(
279 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_HUNK");
280 069bbb86 2022-03-07 thomas return NULL;
281 069bbb86 2022-03-07 thomas }
282 069bbb86 2022-03-07 thomas
283 069bbb86 2022-03-07 thomas static const struct got_error *
284 069bbb86 2022-03-07 thomas send_line(const char *line)
285 069bbb86 2022-03-07 thomas {
286 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
287 069bbb86 2022-03-07 thomas char *p = NULL;
288 069bbb86 2022-03-07 thomas
289 069bbb86 2022-03-07 thomas if (*line != '+' && *line != '-' && *line != ' ') {
290 069bbb86 2022-03-07 thomas if (asprintf(&p, " %s", line) == -1)
291 069bbb86 2022-03-07 thomas return got_error_from_errno("asprintf");
292 069bbb86 2022-03-07 thomas line = p;
293 069bbb86 2022-03-07 thomas }
294 069bbb86 2022-03-07 thomas
295 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
296 069bbb86 2022-03-07 thomas line, strlen(line)+1) == -1)
297 069bbb86 2022-03-07 thomas err = got_error_from_errno(
298 069bbb86 2022-03-07 thomas "imsg_compose GOT_IMSG_PATCH_LINE");
299 069bbb86 2022-03-07 thomas
300 069bbb86 2022-03-07 thomas free(p);
301 069bbb86 2022-03-07 thomas return err;
302 069bbb86 2022-03-07 thomas }
303 069bbb86 2022-03-07 thomas
304 069bbb86 2022-03-07 thomas static const struct got_error *
305 069bbb86 2022-03-07 thomas parse_hunk(FILE *fp, int *ok)
306 069bbb86 2022-03-07 thomas {
307 069bbb86 2022-03-07 thomas static const struct got_error *err = NULL;
308 069bbb86 2022-03-07 thomas struct got_imsg_patch_hunk hdr;
309 069bbb86 2022-03-07 thomas char *line = NULL, ch;
310 069bbb86 2022-03-07 thomas size_t linesize = 0;
311 069bbb86 2022-03-07 thomas ssize_t linelen;
312 069bbb86 2022-03-07 thomas long leftold, leftnew;
313 069bbb86 2022-03-07 thomas
314 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, fp);
315 069bbb86 2022-03-07 thomas if (linelen == -1) {
316 069bbb86 2022-03-07 thomas *ok = 0;
317 069bbb86 2022-03-07 thomas goto done;
318 069bbb86 2022-03-07 thomas }
319 069bbb86 2022-03-07 thomas
320 069bbb86 2022-03-07 thomas err = parse_hdr(line, ok, &hdr);
321 069bbb86 2022-03-07 thomas if (err)
322 069bbb86 2022-03-07 thomas goto done;
323 069bbb86 2022-03-07 thomas if (!*ok) {
324 069bbb86 2022-03-07 thomas if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
325 069bbb86 2022-03-07 thomas err = got_error_from_errno("fseek");
326 069bbb86 2022-03-07 thomas goto done;
327 069bbb86 2022-03-07 thomas }
328 069bbb86 2022-03-07 thomas
329 069bbb86 2022-03-07 thomas leftold = hdr.oldlines;
330 069bbb86 2022-03-07 thomas leftnew = hdr.newlines;
331 069bbb86 2022-03-07 thomas
332 069bbb86 2022-03-07 thomas while (leftold > 0 || leftnew > 0) {
333 069bbb86 2022-03-07 thomas linelen = getline(&line, &linesize, fp);
334 069bbb86 2022-03-07 thomas if (linelen == -1) {
335 069bbb86 2022-03-07 thomas if (ferror(fp)) {
336 069bbb86 2022-03-07 thomas err = got_error_from_errno("getline");
337 069bbb86 2022-03-07 thomas goto done;
338 069bbb86 2022-03-07 thomas }
339 069bbb86 2022-03-07 thomas
340 069bbb86 2022-03-07 thomas /* trailing newlines may be chopped */
341 069bbb86 2022-03-07 thomas if (leftold < 3 && leftnew < 3) {
342 069bbb86 2022-03-07 thomas *ok = 0;
343 069bbb86 2022-03-07 thomas break;
344 069bbb86 2022-03-07 thomas }
345 069bbb86 2022-03-07 thomas
346 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_TRUNCATED);
347 069bbb86 2022-03-07 thomas goto done;
348 069bbb86 2022-03-07 thomas }
349 069bbb86 2022-03-07 thomas
350 069bbb86 2022-03-07 thomas /* usr.bin/patch allows '=' as context char */
351 069bbb86 2022-03-07 thomas if (*line == '=')
352 069bbb86 2022-03-07 thomas *line = ' ';
353 069bbb86 2022-03-07 thomas
354 069bbb86 2022-03-07 thomas ch = *line;
355 069bbb86 2022-03-07 thomas if (ch == '\t' || ch == '\n')
356 069bbb86 2022-03-07 thomas ch = ' '; /* the space got eaten */
357 069bbb86 2022-03-07 thomas
358 069bbb86 2022-03-07 thomas switch (ch) {
359 069bbb86 2022-03-07 thomas case '-':
360 069bbb86 2022-03-07 thomas leftold--;
361 069bbb86 2022-03-07 thomas break;
362 069bbb86 2022-03-07 thomas case ' ':
363 069bbb86 2022-03-07 thomas leftold--;
364 069bbb86 2022-03-07 thomas leftnew--;
365 069bbb86 2022-03-07 thomas break;
366 069bbb86 2022-03-07 thomas case '+':
367 069bbb86 2022-03-07 thomas leftnew--;
368 069bbb86 2022-03-07 thomas break;
369 069bbb86 2022-03-07 thomas default:
370 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
371 069bbb86 2022-03-07 thomas goto done;
372 069bbb86 2022-03-07 thomas }
373 069bbb86 2022-03-07 thomas
374 069bbb86 2022-03-07 thomas if (leftold < 0 || leftnew < 0) {
375 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PATCH_MALFORMED);
376 069bbb86 2022-03-07 thomas goto done;
377 069bbb86 2022-03-07 thomas }
378 069bbb86 2022-03-07 thomas
379 069bbb86 2022-03-07 thomas err = send_line(line);
380 069bbb86 2022-03-07 thomas if (err)
381 069bbb86 2022-03-07 thomas goto done;
382 069bbb86 2022-03-07 thomas }
383 069bbb86 2022-03-07 thomas
384 069bbb86 2022-03-07 thomas done:
385 069bbb86 2022-03-07 thomas free(line);
386 069bbb86 2022-03-07 thomas return err;
387 069bbb86 2022-03-07 thomas }
388 069bbb86 2022-03-07 thomas
389 069bbb86 2022-03-07 thomas static const struct got_error *
390 069bbb86 2022-03-07 thomas read_patch(struct imsgbuf *ibuf, int fd)
391 069bbb86 2022-03-07 thomas {
392 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
393 069bbb86 2022-03-07 thomas FILE *fp;
394 069bbb86 2022-03-07 thomas int ok, patch_found = 0;
395 069bbb86 2022-03-07 thomas
396 069bbb86 2022-03-07 thomas if ((fp = fdopen(fd, "r")) == NULL) {
397 069bbb86 2022-03-07 thomas err = got_error_from_errno("fdopen");
398 069bbb86 2022-03-07 thomas close(fd);
399 069bbb86 2022-03-07 thomas return err;
400 069bbb86 2022-03-07 thomas }
401 069bbb86 2022-03-07 thomas
402 069bbb86 2022-03-07 thomas while (!feof(fp)) {
403 069bbb86 2022-03-07 thomas err = find_patch(fp);
404 069bbb86 2022-03-07 thomas if (err)
405 069bbb86 2022-03-07 thomas goto done;
406 069bbb86 2022-03-07 thomas
407 069bbb86 2022-03-07 thomas patch_found = 1;
408 069bbb86 2022-03-07 thomas for (;;) {
409 069bbb86 2022-03-07 thomas err = parse_hunk(fp, &ok);
410 069bbb86 2022-03-07 thomas if (err)
411 069bbb86 2022-03-07 thomas goto done;
412 069bbb86 2022-03-07 thomas if (!ok) {
413 069bbb86 2022-03-07 thomas err = send_patch_done();
414 069bbb86 2022-03-07 thomas if (err)
415 069bbb86 2022-03-07 thomas goto done;
416 069bbb86 2022-03-07 thomas break;
417 069bbb86 2022-03-07 thomas }
418 069bbb86 2022-03-07 thomas }
419 069bbb86 2022-03-07 thomas }
420 069bbb86 2022-03-07 thomas
421 069bbb86 2022-03-07 thomas done:
422 069bbb86 2022-03-07 thomas fclose(fp);
423 069bbb86 2022-03-07 thomas
424 069bbb86 2022-03-07 thomas /* ignore trailing gibberish */
425 069bbb86 2022-03-07 thomas if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
426 069bbb86 2022-03-07 thomas err = NULL;
427 069bbb86 2022-03-07 thomas
428 069bbb86 2022-03-07 thomas return err;
429 069bbb86 2022-03-07 thomas }
430 069bbb86 2022-03-07 thomas
431 069bbb86 2022-03-07 thomas int
432 069bbb86 2022-03-07 thomas main(int argc, char **argv)
433 069bbb86 2022-03-07 thomas {
434 069bbb86 2022-03-07 thomas const struct got_error *err = NULL;
435 069bbb86 2022-03-07 thomas struct imsg imsg;
436 069bbb86 2022-03-07 thomas #if 0
437 069bbb86 2022-03-07 thomas static int attached;
438 069bbb86 2022-03-07 thomas while (!attached)
439 069bbb86 2022-03-07 thomas sleep(1);
440 069bbb86 2022-03-07 thomas #endif
441 069bbb86 2022-03-07 thomas
442 069bbb86 2022-03-07 thomas imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
443 069bbb86 2022-03-07 thomas #ifndef PROFILE
444 069bbb86 2022-03-07 thomas /* revoke access to most system calls */
445 069bbb86 2022-03-07 thomas if (pledge("stdio recvfd", NULL) == -1) {
446 069bbb86 2022-03-07 thomas err = got_error_from_errno("pledge");
447 069bbb86 2022-03-07 thomas got_privsep_send_error(&ibuf, err);
448 069bbb86 2022-03-07 thomas return 1;
449 069bbb86 2022-03-07 thomas }
450 069bbb86 2022-03-07 thomas #endif
451 069bbb86 2022-03-07 thomas
452 069bbb86 2022-03-07 thomas err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
453 069bbb86 2022-03-07 thomas if (err)
454 069bbb86 2022-03-07 thomas goto done;
455 069bbb86 2022-03-07 thomas if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
456 069bbb86 2022-03-07 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
457 069bbb86 2022-03-07 thomas goto done;
458 069bbb86 2022-03-07 thomas }
459 069bbb86 2022-03-07 thomas
460 069bbb86 2022-03-07 thomas err = read_patch(&ibuf, imsg.fd);
461 069bbb86 2022-03-07 thomas if (err)
462 069bbb86 2022-03-07 thomas goto done;
463 069bbb86 2022-03-07 thomas if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
464 069bbb86 2022-03-07 thomas NULL, 0) == -1) {
465 069bbb86 2022-03-07 thomas err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
466 069bbb86 2022-03-07 thomas goto done;
467 069bbb86 2022-03-07 thomas }
468 069bbb86 2022-03-07 thomas err = got_privsep_flush_imsg(&ibuf);
469 069bbb86 2022-03-07 thomas done:
470 069bbb86 2022-03-07 thomas imsg_free(&imsg);
471 069bbb86 2022-03-07 thomas if (err != NULL) {
472 069bbb86 2022-03-07 thomas got_privsep_send_error(&ibuf, err);
473 069bbb86 2022-03-07 thomas err = NULL;
474 069bbb86 2022-03-07 thomas }
475 069bbb86 2022-03-07 thomas if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
476 069bbb86 2022-03-07 thomas err = got_error_from_errno("close");
477 069bbb86 2022-03-07 thomas if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
478 069bbb86 2022-03-07 thomas fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
479 069bbb86 2022-03-07 thomas return err ? 1 : 0;
480 069bbb86 2022-03-07 thomas }