Blame


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