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