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 55e9459f 2022-06-19 op #include "got_lib_sha1.h"
60 e9ce266e 2022-03-07 op
61 e9ce266e 2022-03-07 op struct imsgbuf ibuf;
62 e9ce266e 2022-03-07 op
63 e9ce266e 2022-03-07 op static const struct got_error *
64 d8b5af43 2022-06-19 op send_patch(const char *oldname, const char *newname, const char *commitid,
65 d8b5af43 2022-06-19 op const char *blob, int git)
66 e9ce266e 2022-03-07 op {
67 e9ce266e 2022-03-07 op struct got_imsg_patch p;
68 e9ce266e 2022-03-07 op
69 e9ce266e 2022-03-07 op memset(&p, 0, sizeof(p));
70 e9ce266e 2022-03-07 op
71 9d6cabd5 2022-04-07 op if (oldname != NULL)
72 e9ce266e 2022-03-07 op strlcpy(p.old, oldname, sizeof(p.old));
73 7a30b5cb 2022-03-20 op
74 e9ce266e 2022-03-07 op if (newname != NULL)
75 e9ce266e 2022-03-07 op strlcpy(p.new, newname, sizeof(p.new));
76 e9ce266e 2022-03-07 op
77 497a5915 2022-06-27 op if (commitid != NULL)
78 d8b5af43 2022-06-19 op strlcpy(p.cid, commitid, sizeof(p.cid));
79 497a5915 2022-06-27 op
80 497a5915 2022-06-27 op if (blob != NULL)
81 55e9459f 2022-06-19 op strlcpy(p.blob, blob, sizeof(p.blob));
82 55e9459f 2022-06-19 op
83 9d6cabd5 2022-04-07 op p.git = git;
84 55e9459f 2022-06-19 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
85 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
86 e9ce266e 2022-03-07 op return NULL;
87 e9ce266e 2022-03-07 op }
88 e9ce266e 2022-03-07 op
89 e9ce266e 2022-03-07 op static const struct got_error *
90 e9ce266e 2022-03-07 op send_patch_done(void)
91 e9ce266e 2022-03-07 op {
92 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
93 e9ce266e 2022-03-07 op NULL, 0) == -1)
94 e9ce266e 2022-03-07 op return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
95 08f44789 2022-07-07 op return got_privsep_flush_imsg(&ibuf);
96 e9ce266e 2022-03-07 op }
97 e9ce266e 2022-03-07 op
98 e9ce266e 2022-03-07 op /* based on fetchname from usr.bin/patch/util.c */
99 e9ce266e 2022-03-07 op static const struct got_error *
100 9d6cabd5 2022-04-07 op filename(const char *at, char **name)
101 e9ce266e 2022-03-07 op {
102 9d6cabd5 2022-04-07 op char *tmp, *t;
103 e9ce266e 2022-03-07 op
104 e9ce266e 2022-03-07 op *name = NULL;
105 e9ce266e 2022-03-07 op if (*at == '\0')
106 e9ce266e 2022-03-07 op return NULL;
107 e9ce266e 2022-03-07 op
108 e9ce266e 2022-03-07 op while (isspace((unsigned char)*at))
109 e9ce266e 2022-03-07 op at++;
110 e9ce266e 2022-03-07 op
111 e9ce266e 2022-03-07 op /* files can be created or removed by diffing against /dev/null */
112 46ebad13 2022-03-17 op if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
113 e9ce266e 2022-03-07 op return NULL;
114 e9ce266e 2022-03-07 op
115 9d6cabd5 2022-04-07 op tmp = strdup(at);
116 9d6cabd5 2022-04-07 op if (tmp == NULL)
117 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
118 9d6cabd5 2022-04-07 op if ((t = strchr(tmp, '\t')) != NULL)
119 9d6cabd5 2022-04-07 op *t = '\0';
120 9d6cabd5 2022-04-07 op if ((t = strchr(tmp, '\n')) != NULL)
121 9d6cabd5 2022-04-07 op *t = '\0';
122 e9ce266e 2022-03-07 op
123 9d6cabd5 2022-04-07 op *name = strdup(tmp);
124 9d6cabd5 2022-04-07 op free(tmp);
125 e9ce266e 2022-03-07 op if (*name == NULL)
126 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
127 e9ce266e 2022-03-07 op return NULL;
128 e9ce266e 2022-03-07 op }
129 e9ce266e 2022-03-07 op
130 e9ce266e 2022-03-07 op static const struct got_error *
131 db0dfdd7 2022-06-27 op blobid(const char *line, char **blob, int git)
132 55e9459f 2022-06-19 op {
133 55e9459f 2022-06-19 op uint8_t digest[SHA1_DIGEST_LENGTH];
134 55e9459f 2022-06-19 op size_t len;
135 55e9459f 2022-06-19 op
136 55e9459f 2022-06-19 op *blob = NULL;
137 55e9459f 2022-06-19 op
138 55e9459f 2022-06-19 op len = strspn(line, "0123456789abcdefABCDEF");
139 55e9459f 2022-06-19 op if ((*blob = strndup(line, len)) == NULL)
140 55e9459f 2022-06-19 op return got_error_from_errno("strndup");
141 55e9459f 2022-06-19 op
142 db0dfdd7 2022-06-27 op if (!git && !got_parse_sha1_digest(digest, *blob)) {
143 55e9459f 2022-06-19 op /* silently ignore invalid blob ids */
144 55e9459f 2022-06-19 op free(*blob);
145 55e9459f 2022-06-19 op *blob = NULL;
146 55e9459f 2022-06-19 op }
147 55e9459f 2022-06-19 op return NULL;
148 55e9459f 2022-06-19 op }
149 55e9459f 2022-06-19 op
150 55e9459f 2022-06-19 op static const struct got_error *
151 acf749fc 2022-07-02 op patch_start(int *git, char **cid, FILE *fp)
152 e9ce266e 2022-03-07 op {
153 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
154 e9ce266e 2022-03-07 op char *line = NULL;
155 e9ce266e 2022-03-07 op size_t linesize = 0;
156 e9ce266e 2022-03-07 op ssize_t linelen;
157 acf749fc 2022-07-02 op
158 acf749fc 2022-07-02 op *git = 0;
159 acf749fc 2022-07-02 op
160 acf749fc 2022-07-02 op while ((linelen = getline(&line, &linesize, fp)) != -1) {
161 acf749fc 2022-07-02 op if (!strncmp(line, "diff --git ", 11)) {
162 acf749fc 2022-07-02 op *git = 1;
163 acf749fc 2022-07-02 op free(*cid);
164 acf749fc 2022-07-02 op *cid = NULL;
165 acf749fc 2022-07-02 op break;
166 acf749fc 2022-07-02 op } else if (!strncmp(line, "diff ", 5)) {
167 acf749fc 2022-07-02 op *git = 0;
168 acf749fc 2022-07-02 op free(*cid);
169 acf749fc 2022-07-02 op *cid = NULL;
170 acf749fc 2022-07-02 op } else if (!strncmp(line, "commit - ", 9)) {
171 acf749fc 2022-07-02 op free(*cid);
172 acf749fc 2022-07-02 op err = blobid(line + 9, cid, *git);
173 acf749fc 2022-07-02 op if (err)
174 acf749fc 2022-07-02 op break;
175 acf749fc 2022-07-02 op } else if (!strncmp(line, "--- ", 4) ||
176 acf749fc 2022-07-02 op !strncmp(line, "+++ ", 4) ||
177 acf749fc 2022-07-02 op !strncmp(line, "blob - ", 7)) {
178 acf749fc 2022-07-02 op /* rewind to previous line */
179 acf749fc 2022-07-02 op if (fseeko(fp, -linelen, SEEK_CUR) == -1)
180 acf749fc 2022-07-02 op err = got_error_from_errno("fseeko");
181 acf749fc 2022-07-02 op break;
182 acf749fc 2022-07-02 op }
183 acf749fc 2022-07-02 op }
184 acf749fc 2022-07-02 op
185 acf749fc 2022-07-02 op free(line);
186 acf749fc 2022-07-02 op if (ferror(fp) && err == NULL)
187 acf749fc 2022-07-02 op err = got_error_from_errno("getline");
188 acf749fc 2022-07-02 op if (feof(fp) && err == NULL)
189 acf749fc 2022-07-02 op err = got_error(GOT_ERR_NO_PATCH);
190 acf749fc 2022-07-02 op return err;
191 acf749fc 2022-07-02 op }
192 acf749fc 2022-07-02 op
193 acf749fc 2022-07-02 op static const struct got_error *
194 acf749fc 2022-07-02 op find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
195 acf749fc 2022-07-02 op {
196 acf749fc 2022-07-02 op const struct got_error *err = NULL;
197 acf749fc 2022-07-02 op char *old = NULL, *new = NULL;
198 acf749fc 2022-07-02 op char *blob = NULL;
199 acf749fc 2022-07-02 op char *line = NULL;
200 acf749fc 2022-07-02 op size_t linesize = 0;
201 acf749fc 2022-07-02 op ssize_t linelen;
202 acf749fc 2022-07-02 op int create, rename = 0;
203 e9ce266e 2022-03-07 op
204 acf749fc 2022-07-02 op *done = 0;
205 acf749fc 2022-07-02 op *next = 0;
206 e9ce266e 2022-03-07 op while ((linelen = getline(&line, &linesize, fp)) != -1) {
207 e9ce266e 2022-03-07 op /*
208 e9ce266e 2022-03-07 op * Ignore the Index name like GNU and larry' patch,
209 e9ce266e 2022-03-07 op * we don't have to follow POSIX.
210 e9ce266e 2022-03-07 op */
211 e9ce266e 2022-03-07 op
212 9d6cabd5 2022-04-07 op if (!strncmp(line, "--- ", 4)) {
213 e9ce266e 2022-03-07 op free(old);
214 9d6cabd5 2022-04-07 op err = filename(line+4, &old);
215 4379a9aa 2022-05-02 op } else if (rename && !strncmp(line, "rename from ", 12)) {
216 4379a9aa 2022-05-02 op free(old);
217 4379a9aa 2022-05-02 op err = filename(line+12, &old);
218 e9ce266e 2022-03-07 op } else if (!strncmp(line, "+++ ", 4)) {
219 e9ce266e 2022-03-07 op free(new);
220 9d6cabd5 2022-04-07 op err = filename(line+4, &new);
221 55e9459f 2022-06-19 op } else if (!git && !strncmp(line, "blob - ", 7)) {
222 55e9459f 2022-06-19 op free(blob);
223 db0dfdd7 2022-06-27 op err = blobid(line + 7, &blob, git);
224 4379a9aa 2022-05-02 op } else if (rename && !strncmp(line, "rename to ", 10)) {
225 4379a9aa 2022-05-02 op free(new);
226 4379a9aa 2022-05-02 op err = filename(line + 10, &new);
227 4379a9aa 2022-05-02 op } else if (git && !strncmp(line, "similarity index 100%", 21))
228 4379a9aa 2022-05-02 op rename = 1;
229 db0dfdd7 2022-06-27 op else if (git && !strncmp(line, "index ", 6)) {
230 db0dfdd7 2022-06-27 op free(blob);
231 db0dfdd7 2022-06-27 op err = blobid(line + 6, &blob, git);
232 acf749fc 2022-07-02 op } else if (!strncmp(line, "diff ", 5)) {
233 acf749fc 2022-07-02 op /* rewind to previous line */
234 acf749fc 2022-07-02 op if (fseeko(fp, -linelen, SEEK_CUR) == -1)
235 acf749fc 2022-07-02 op err = got_error_from_errno("fseeko");
236 acf749fc 2022-07-02 op *next = 1;
237 acf749fc 2022-07-02 op break;
238 55e9459f 2022-06-19 op }
239 e9ce266e 2022-03-07 op
240 e9ce266e 2022-03-07 op if (err)
241 4379a9aa 2022-05-02 op break;
242 4379a9aa 2022-05-02 op
243 4379a9aa 2022-05-02 op /*
244 4379a9aa 2022-05-02 op * Git-style diffs with "similarity index 100%" don't
245 4379a9aa 2022-05-02 op * have any hunks and ends with the "rename to foobar"
246 4379a9aa 2022-05-02 op * line.
247 4379a9aa 2022-05-02 op */
248 4379a9aa 2022-05-02 op if (rename && old != NULL && new != NULL) {
249 6b7665ac 2022-05-02 op *done = 1;
250 d8b5af43 2022-06-19 op err = send_patch(old, new, commitid,
251 d8b5af43 2022-06-19 op blob, git);
252 e9ce266e 2022-03-07 op break;
253 4379a9aa 2022-05-02 op }
254 e9ce266e 2022-03-07 op
255 e9ce266e 2022-03-07 op if (!strncmp(line, "@@ -", 4)) {
256 e9ce266e 2022-03-07 op create = !strncmp(line+4, "0,0", 3);
257 e9ce266e 2022-03-07 op if ((old == NULL && new == NULL) ||
258 e9ce266e 2022-03-07 op (!create && old == NULL))
259 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
260 e9ce266e 2022-03-07 op else
261 d8b5af43 2022-06-19 op err = send_patch(old, new, commitid,
262 d8b5af43 2022-06-19 op blob, git);
263 e9ce266e 2022-03-07 op
264 e9ce266e 2022-03-07 op if (err)
265 e9ce266e 2022-03-07 op break;
266 e9ce266e 2022-03-07 op
267 e9ce266e 2022-03-07 op /* rewind to previous line */
268 e45f7eba 2022-05-14 naddy if (fseeko(fp, -linelen, SEEK_CUR) == -1)
269 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
270 e9ce266e 2022-03-07 op break;
271 e9ce266e 2022-03-07 op }
272 e9ce266e 2022-03-07 op }
273 e9ce266e 2022-03-07 op
274 423faaa6 2022-03-12 op free(old);
275 423faaa6 2022-03-12 op free(new);
276 55e9459f 2022-06-19 op free(blob);
277 e9ce266e 2022-03-07 op free(line);
278 e9ce266e 2022-03-07 op if (ferror(fp) && err == NULL)
279 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
280 e9ce266e 2022-03-07 op if (feof(fp) && err == NULL)
281 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_NO_PATCH);
282 e9ce266e 2022-03-07 op return err;
283 e9ce266e 2022-03-07 op }
284 e9ce266e 2022-03-07 op
285 e9ce266e 2022-03-07 op static const struct got_error *
286 35095610 2022-06-14 op strtolnum(char **str, int *n)
287 e9ce266e 2022-03-07 op {
288 e9ce266e 2022-03-07 op char *p, c;
289 e9ce266e 2022-03-07 op const char *errstr;
290 e9ce266e 2022-03-07 op
291 e9ce266e 2022-03-07 op for (p = *str; isdigit((unsigned char)*p); ++p)
292 e9ce266e 2022-03-07 op /* nop */;
293 e9ce266e 2022-03-07 op
294 e9ce266e 2022-03-07 op c = *p;
295 e9ce266e 2022-03-07 op *p = '\0';
296 e9ce266e 2022-03-07 op
297 35095610 2022-06-14 op *n = strtonum(*str, 0, INT_MAX, &errstr);
298 e9ce266e 2022-03-07 op if (errstr != NULL)
299 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
300 e9ce266e 2022-03-07 op
301 e9ce266e 2022-03-07 op *p = c;
302 e9ce266e 2022-03-07 op *str = p;
303 e9ce266e 2022-03-07 op return NULL;
304 e9ce266e 2022-03-07 op }
305 e9ce266e 2022-03-07 op
306 e9ce266e 2022-03-07 op static const struct got_error *
307 d75b9573 2022-05-02 op parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
308 e9ce266e 2022-03-07 op {
309 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
310 e9ce266e 2022-03-07 op
311 e9ce266e 2022-03-07 op if (strncmp(s, "@@ -", 4)) {
312 d75b9573 2022-05-02 op *done = 1;
313 e9ce266e 2022-03-07 op return NULL;
314 e9ce266e 2022-03-07 op }
315 e9ce266e 2022-03-07 op
316 e9ce266e 2022-03-07 op s += 4;
317 e9ce266e 2022-03-07 op if (!*s)
318 e9ce266e 2022-03-07 op return NULL;
319 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->oldfrom);
320 e9ce266e 2022-03-07 op if (err)
321 e9ce266e 2022-03-07 op return err;
322 e9ce266e 2022-03-07 op if (*s == ',') {
323 e9ce266e 2022-03-07 op s++;
324 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->oldlines);
325 e9ce266e 2022-03-07 op if (err)
326 e9ce266e 2022-03-07 op return err;
327 e9ce266e 2022-03-07 op } else
328 e9ce266e 2022-03-07 op hdr->oldlines = 1;
329 e9ce266e 2022-03-07 op
330 e9ce266e 2022-03-07 op if (*s == ' ')
331 e9ce266e 2022-03-07 op s++;
332 e9ce266e 2022-03-07 op
333 e9ce266e 2022-03-07 op if (*s != '+' || !*++s)
334 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
335 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->newfrom);
336 e9ce266e 2022-03-07 op if (err)
337 e9ce266e 2022-03-07 op return err;
338 e9ce266e 2022-03-07 op if (*s == ',') {
339 e9ce266e 2022-03-07 op s++;
340 e9ce266e 2022-03-07 op err = strtolnum(&s, &hdr->newlines);
341 e9ce266e 2022-03-07 op if (err)
342 e9ce266e 2022-03-07 op return err;
343 e9ce266e 2022-03-07 op } else
344 e9ce266e 2022-03-07 op hdr->newlines = 1;
345 e9ce266e 2022-03-07 op
346 e9ce266e 2022-03-07 op if (*s == ' ')
347 e9ce266e 2022-03-07 op s++;
348 e9ce266e 2022-03-07 op
349 e9ce266e 2022-03-07 op if (*s != '@')
350 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
351 e9ce266e 2022-03-07 op
352 35095610 2022-06-14 op if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
353 35095610 2022-06-14 op hdr->newfrom >= INT_MAX - hdr->newlines ||
354 e9ce266e 2022-03-07 op /* not so sure about this one */
355 35095610 2022-06-14 op hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
356 be33dff7 2022-05-13 op (hdr->oldlines == 0 && hdr->newlines == 0))
357 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_MALFORMED);
358 e9ce266e 2022-03-07 op
359 e9ce266e 2022-03-07 op if (hdr->oldlines == 0) {
360 e9ce266e 2022-03-07 op /* larry says to "do append rather than insert"; I don't
361 e9ce266e 2022-03-07 op * quite get it, but i trust him.
362 e9ce266e 2022-03-07 op */
363 e9ce266e 2022-03-07 op hdr->oldfrom++;
364 e9ce266e 2022-03-07 op }
365 e9ce266e 2022-03-07 op
366 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
367 e9ce266e 2022-03-07 op hdr, sizeof(*hdr)) == -1)
368 e9ce266e 2022-03-07 op return got_error_from_errno(
369 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_HUNK");
370 e9ce266e 2022-03-07 op return NULL;
371 e9ce266e 2022-03-07 op }
372 e9ce266e 2022-03-07 op
373 e9ce266e 2022-03-07 op static const struct got_error *
374 e9ce266e 2022-03-07 op send_line(const char *line)
375 e9ce266e 2022-03-07 op {
376 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
377 e9ce266e 2022-03-07 op char *p = NULL;
378 e9ce266e 2022-03-07 op
379 b3c57ab2 2022-03-22 op if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
380 e9ce266e 2022-03-07 op if (asprintf(&p, " %s", line) == -1)
381 e9ce266e 2022-03-07 op return got_error_from_errno("asprintf");
382 e9ce266e 2022-03-07 op line = p;
383 e9ce266e 2022-03-07 op }
384 e9ce266e 2022-03-07 op
385 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
386 46ebad13 2022-03-17 op line, strlen(line) + 1) == -1)
387 e9ce266e 2022-03-07 op err = got_error_from_errno(
388 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_LINE");
389 e9ce266e 2022-03-07 op
390 e9ce266e 2022-03-07 op free(p);
391 e9ce266e 2022-03-07 op return err;
392 e9ce266e 2022-03-07 op }
393 e9ce266e 2022-03-07 op
394 e9ce266e 2022-03-07 op static const struct got_error *
395 b2832778 2022-04-23 op peek_special_line(FILE *fp)
396 b3c57ab2 2022-03-22 op {
397 b3c57ab2 2022-03-22 op const struct got_error *err;
398 e260f8ae 2022-03-22 stsp int ch;
399 b3c57ab2 2022-03-22 op
400 b3c57ab2 2022-03-22 op ch = fgetc(fp);
401 b3c57ab2 2022-03-22 op if (ch != EOF && ch != '\\') {
402 b3c57ab2 2022-03-22 op ungetc(ch, fp);
403 b3c57ab2 2022-03-22 op return NULL;
404 b3c57ab2 2022-03-22 op }
405 b3c57ab2 2022-03-22 op
406 b2832778 2022-04-23 op if (ch == '\\') {
407 b3c57ab2 2022-03-22 op err = send_line("\\");
408 b3c57ab2 2022-03-22 op if (err)
409 b3c57ab2 2022-03-22 op return err;
410 b3c57ab2 2022-03-22 op }
411 b3c57ab2 2022-03-22 op
412 b3c57ab2 2022-03-22 op while (ch != EOF && ch != '\n')
413 b3c57ab2 2022-03-22 op ch = fgetc(fp);
414 b3c57ab2 2022-03-22 op
415 b3c57ab2 2022-03-22 op if (ch != EOF || feof(fp))
416 b3c57ab2 2022-03-22 op return NULL;
417 b3c57ab2 2022-03-22 op return got_error(GOT_ERR_IO);
418 b3c57ab2 2022-03-22 op }
419 b3c57ab2 2022-03-22 op
420 b3c57ab2 2022-03-22 op static const struct got_error *
421 d75b9573 2022-05-02 op parse_hunk(FILE *fp, int *done)
422 e9ce266e 2022-03-07 op {
423 e9ce266e 2022-03-07 op static const struct got_error *err = NULL;
424 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
425 e9ce266e 2022-03-07 op char *line = NULL, ch;
426 e9ce266e 2022-03-07 op size_t linesize = 0;
427 e9ce266e 2022-03-07 op ssize_t linelen;
428 35095610 2022-06-14 op int leftold, leftnew;
429 e9ce266e 2022-03-07 op
430 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, fp);
431 e9ce266e 2022-03-07 op if (linelen == -1) {
432 d75b9573 2022-05-02 op *done = 1;
433 e9ce266e 2022-03-07 op goto done;
434 e9ce266e 2022-03-07 op }
435 e9ce266e 2022-03-07 op
436 d75b9573 2022-05-02 op err = parse_hdr(line, done, &hdr);
437 e9ce266e 2022-03-07 op if (err)
438 e9ce266e 2022-03-07 op goto done;
439 d75b9573 2022-05-02 op if (*done) {
440 e45f7eba 2022-05-14 naddy if (fseeko(fp, -linelen, SEEK_CUR) == -1)
441 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
442 e9ce266e 2022-03-07 op goto done;
443 e9ce266e 2022-03-07 op }
444 e9ce266e 2022-03-07 op
445 e9ce266e 2022-03-07 op leftold = hdr.oldlines;
446 e9ce266e 2022-03-07 op leftnew = hdr.newlines;
447 e9ce266e 2022-03-07 op
448 e9ce266e 2022-03-07 op while (leftold > 0 || leftnew > 0) {
449 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, fp);
450 e9ce266e 2022-03-07 op if (linelen == -1) {
451 e9ce266e 2022-03-07 op if (ferror(fp)) {
452 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
453 e9ce266e 2022-03-07 op goto done;
454 e9ce266e 2022-03-07 op }
455 e9ce266e 2022-03-07 op
456 e9ce266e 2022-03-07 op /* trailing newlines may be chopped */
457 e9ce266e 2022-03-07 op if (leftold < 3 && leftnew < 3) {
458 d75b9573 2022-05-02 op *done = 1;
459 e9ce266e 2022-03-07 op break;
460 e9ce266e 2022-03-07 op }
461 e9ce266e 2022-03-07 op
462 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_TRUNCATED);
463 e9ce266e 2022-03-07 op goto done;
464 e9ce266e 2022-03-07 op }
465 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
466 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
467 e9ce266e 2022-03-07 op
468 e9ce266e 2022-03-07 op /* usr.bin/patch allows '=' as context char */
469 e9ce266e 2022-03-07 op if (*line == '=')
470 e9ce266e 2022-03-07 op *line = ' ';
471 e9ce266e 2022-03-07 op
472 e9ce266e 2022-03-07 op ch = *line;
473 b3c57ab2 2022-03-22 op if (ch == '\t' || ch == '\0')
474 e9ce266e 2022-03-07 op ch = ' '; /* the space got eaten */
475 e9ce266e 2022-03-07 op
476 e9ce266e 2022-03-07 op switch (ch) {
477 e9ce266e 2022-03-07 op case '-':
478 e9ce266e 2022-03-07 op leftold--;
479 e9ce266e 2022-03-07 op break;
480 e9ce266e 2022-03-07 op case ' ':
481 e9ce266e 2022-03-07 op leftold--;
482 e9ce266e 2022-03-07 op leftnew--;
483 e9ce266e 2022-03-07 op break;
484 e9ce266e 2022-03-07 op case '+':
485 e9ce266e 2022-03-07 op leftnew--;
486 e9ce266e 2022-03-07 op break;
487 e9ce266e 2022-03-07 op default:
488 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
489 e9ce266e 2022-03-07 op goto done;
490 e9ce266e 2022-03-07 op }
491 e9ce266e 2022-03-07 op
492 e9ce266e 2022-03-07 op if (leftold < 0 || leftnew < 0) {
493 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_MALFORMED);
494 e9ce266e 2022-03-07 op goto done;
495 e9ce266e 2022-03-07 op }
496 e9ce266e 2022-03-07 op
497 e9ce266e 2022-03-07 op err = send_line(line);
498 e9ce266e 2022-03-07 op if (err)
499 e9ce266e 2022-03-07 op goto done;
500 b3c57ab2 2022-03-22 op
501 b3c57ab2 2022-03-22 op if ((ch == '-' && leftold == 0) ||
502 b3c57ab2 2022-03-22 op (ch == '+' && leftnew == 0)) {
503 b2832778 2022-04-23 op err = peek_special_line(fp);
504 b3c57ab2 2022-03-22 op if (err)
505 b3c57ab2 2022-03-22 op goto done;
506 b3c57ab2 2022-03-22 op }
507 e9ce266e 2022-03-07 op }
508 e9ce266e 2022-03-07 op
509 e9ce266e 2022-03-07 op done:
510 e9ce266e 2022-03-07 op free(line);
511 e9ce266e 2022-03-07 op return err;
512 e9ce266e 2022-03-07 op }
513 e9ce266e 2022-03-07 op
514 e9ce266e 2022-03-07 op static const struct got_error *
515 e9ce266e 2022-03-07 op read_patch(struct imsgbuf *ibuf, int fd)
516 e9ce266e 2022-03-07 op {
517 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
518 e9ce266e 2022-03-07 op FILE *fp;
519 acf749fc 2022-07-02 op int git, patch_found = 0;
520 acf749fc 2022-07-02 op char *cid = NULL;
521 e9ce266e 2022-03-07 op
522 e9ce266e 2022-03-07 op if ((fp = fdopen(fd, "r")) == NULL) {
523 e9ce266e 2022-03-07 op err = got_error_from_errno("fdopen");
524 e9ce266e 2022-03-07 op close(fd);
525 e9ce266e 2022-03-07 op return err;
526 e9ce266e 2022-03-07 op }
527 e9ce266e 2022-03-07 op
528 acf749fc 2022-07-02 op while ((err = patch_start(&git, &cid, fp)) == NULL) {
529 acf749fc 2022-07-02 op int done, next;
530 4379a9aa 2022-05-02 op
531 acf749fc 2022-07-02 op err = find_diff(&done, &next, fp, git, cid);
532 e9ce266e 2022-03-07 op if (err)
533 e9ce266e 2022-03-07 op goto done;
534 acf749fc 2022-07-02 op if (next)
535 acf749fc 2022-07-02 op continue;
536 e9ce266e 2022-03-07 op
537 e9ce266e 2022-03-07 op patch_found = 1;
538 d75b9573 2022-05-02 op
539 d75b9573 2022-05-02 op while (!done) {
540 d75b9573 2022-05-02 op err = parse_hunk(fp, &done);
541 e9ce266e 2022-03-07 op if (err)
542 e9ce266e 2022-03-07 op goto done;
543 e9ce266e 2022-03-07 op }
544 d75b9573 2022-05-02 op
545 d75b9573 2022-05-02 op err = send_patch_done();
546 d75b9573 2022-05-02 op if (err)
547 d75b9573 2022-05-02 op goto done;
548 e9ce266e 2022-03-07 op }
549 e9ce266e 2022-03-07 op
550 e9ce266e 2022-03-07 op done:
551 e9ce266e 2022-03-07 op fclose(fp);
552 acf749fc 2022-07-02 op free(cid);
553 e9ce266e 2022-03-07 op
554 e9ce266e 2022-03-07 op /* ignore trailing gibberish */
555 e9ce266e 2022-03-07 op if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
556 e9ce266e 2022-03-07 op err = NULL;
557 e9ce266e 2022-03-07 op
558 e9ce266e 2022-03-07 op return err;
559 e9ce266e 2022-03-07 op }
560 e9ce266e 2022-03-07 op
561 e9ce266e 2022-03-07 op int
562 e9ce266e 2022-03-07 op main(int argc, char **argv)
563 e9ce266e 2022-03-07 op {
564 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
565 e9ce266e 2022-03-07 op struct imsg imsg;
566 e9ce266e 2022-03-07 op #if 0
567 e9ce266e 2022-03-07 op static int attached;
568 e9ce266e 2022-03-07 op while (!attached)
569 e9ce266e 2022-03-07 op sleep(1);
570 e9ce266e 2022-03-07 op #endif
571 e9ce266e 2022-03-07 op
572 e9ce266e 2022-03-07 op imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
573 e9ce266e 2022-03-07 op #ifndef PROFILE
574 e9ce266e 2022-03-07 op /* revoke access to most system calls */
575 e9ce266e 2022-03-07 op if (pledge("stdio recvfd", NULL) == -1) {
576 e9ce266e 2022-03-07 op err = got_error_from_errno("pledge");
577 e9ce266e 2022-03-07 op got_privsep_send_error(&ibuf, err);
578 e9ce266e 2022-03-07 op return 1;
579 e9ce266e 2022-03-07 op }
580 e9ce266e 2022-03-07 op #endif
581 e9ce266e 2022-03-07 op
582 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
583 e9ce266e 2022-03-07 op if (err)
584 e9ce266e 2022-03-07 op goto done;
585 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
586 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
587 e9ce266e 2022-03-07 op goto done;
588 e9ce266e 2022-03-07 op }
589 e9ce266e 2022-03-07 op
590 e9ce266e 2022-03-07 op err = read_patch(&ibuf, imsg.fd);
591 e9ce266e 2022-03-07 op if (err)
592 e9ce266e 2022-03-07 op goto done;
593 e9ce266e 2022-03-07 op if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
594 e9ce266e 2022-03-07 op NULL, 0) == -1) {
595 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
596 e9ce266e 2022-03-07 op goto done;
597 e9ce266e 2022-03-07 op }
598 e9ce266e 2022-03-07 op err = got_privsep_flush_imsg(&ibuf);
599 e9ce266e 2022-03-07 op done:
600 e9ce266e 2022-03-07 op imsg_free(&imsg);
601 e9ce266e 2022-03-07 op if (err != NULL) {
602 e9ce266e 2022-03-07 op got_privsep_send_error(&ibuf, err);
603 e9ce266e 2022-03-07 op err = NULL;
604 e9ce266e 2022-03-07 op }
605 e9ce266e 2022-03-07 op if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
606 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
607 e9ce266e 2022-03-07 op if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
608 e9ce266e 2022-03-07 op fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
609 e9ce266e 2022-03-07 op return err ? 1 : 0;
610 e9ce266e 2022-03-07 op }