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