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