Blame


1 f7d653fc 2023-07-09 op /*
2 f7d653fc 2023-07-09 op * Copyright (c) 2023 Omar Polo <op@openbsd.org>
3 f7d653fc 2023-07-09 op *
4 f7d653fc 2023-07-09 op * Permission to use, copy, modify, and distribute this software for any
5 f7d653fc 2023-07-09 op * purpose with or without fee is hereby granted, provided that the above
6 f7d653fc 2023-07-09 op * copyright notice and this permission notice appear in all copies.
7 f7d653fc 2023-07-09 op *
8 f7d653fc 2023-07-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 f7d653fc 2023-07-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 f7d653fc 2023-07-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 f7d653fc 2023-07-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 f7d653fc 2023-07-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 f7d653fc 2023-07-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 f7d653fc 2023-07-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 f7d653fc 2023-07-09 op */
16 f7d653fc 2023-07-09 op
17 f7d653fc 2023-07-09 op #include <sys/queue.h>
18 f7d653fc 2023-07-09 op #include <sys/socket.h>
19 f7d653fc 2023-07-09 op #include <sys/stat.h>
20 f7d653fc 2023-07-09 op #include <sys/time.h>
21 f7d653fc 2023-07-09 op #include <sys/tree.h>
22 f7d653fc 2023-07-09 op #include <sys/types.h>
23 f7d653fc 2023-07-09 op #include <sys/uio.h>
24 f7d653fc 2023-07-09 op #include <sys/wait.h>
25 f7d653fc 2023-07-09 op
26 f7d653fc 2023-07-09 op #include <endian.h>
27 f7d653fc 2023-07-09 op #include <limits.h>
28 f7d653fc 2023-07-09 op #include <sha1.h>
29 f7d653fc 2023-07-09 op #include <sha2.h>
30 f7d653fc 2023-07-09 op #include <stdint.h>
31 f7d653fc 2023-07-09 op #include <stdio.h>
32 f7d653fc 2023-07-09 op #include <stdlib.h>
33 f7d653fc 2023-07-09 op #include <string.h>
34 f7d653fc 2023-07-09 op #include <unistd.h>
35 f7d653fc 2023-07-09 op #include <imsg.h>
36 f7d653fc 2023-07-09 op
37 f7d653fc 2023-07-09 op #include "got_error.h"
38 f7d653fc 2023-07-09 op #include "got_cancel.h"
39 f7d653fc 2023-07-09 op #include "got_object.h"
40 f7d653fc 2023-07-09 op #include "got_opentemp.h"
41 f7d653fc 2023-07-09 op #include "got_path.h"
42 f7d653fc 2023-07-09 op #include "got_reference.h"
43 f7d653fc 2023-07-09 op #include "got_repository.h"
44 f7d653fc 2023-07-09 op #include "got_repository_load.h"
45 f7d653fc 2023-07-09 op
46 f7d653fc 2023-07-09 op #include "got_lib_delta.h"
47 f7d653fc 2023-07-09 op #include "got_lib_hash.h"
48 f7d653fc 2023-07-09 op #include "got_lib_object.h"
49 f7d653fc 2023-07-09 op #include "got_lib_object_cache.h"
50 f7d653fc 2023-07-09 op #include "got_lib_pack.h"
51 f7d653fc 2023-07-09 op #include "got_lib_ratelimit.h"
52 f7d653fc 2023-07-09 op #include "got_lib_repository.h"
53 f7d653fc 2023-07-09 op #include "got_lib_privsep.h"
54 f7d653fc 2023-07-09 op
55 f7d653fc 2023-07-09 op #define GIT_BUNDLE_SIGNATURE_V2 "# v2 git bundle\n"
56 f7d653fc 2023-07-09 op
57 f7d653fc 2023-07-09 op #ifndef nitems
58 f7d653fc 2023-07-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 f7d653fc 2023-07-09 op #endif
60 f7d653fc 2023-07-09 op
61 f7d653fc 2023-07-09 op #ifndef ssizeof
62 f7d653fc 2023-07-09 op #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
63 f7d653fc 2023-07-09 op #endif
64 f7d653fc 2023-07-09 op
65 f7d653fc 2023-07-09 op static const struct got_error *
66 f7d653fc 2023-07-09 op temp_file(int *fd, char **path, const char *ext, struct got_repository *repo)
67 f7d653fc 2023-07-09 op {
68 f7d653fc 2023-07-09 op const struct got_error *err;
69 f7d653fc 2023-07-09 op char p[PATH_MAX];
70 f7d653fc 2023-07-09 op int r;
71 f7d653fc 2023-07-09 op
72 f7d653fc 2023-07-09 op *path = NULL;
73 f7d653fc 2023-07-09 op
74 f7d653fc 2023-07-09 op r = snprintf(p, sizeof(p), "%s/%s/loading",
75 f7d653fc 2023-07-09 op got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR);
76 f7d653fc 2023-07-09 op if (r < 0 || (size_t)r >= sizeof(p))
77 f7d653fc 2023-07-09 op return got_error_from_errno("snprintf");
78 f7d653fc 2023-07-09 op
79 f7d653fc 2023-07-09 op err = got_opentemp_named_fd(path, fd, p, ext);
80 f7d653fc 2023-07-09 op if (err)
81 f7d653fc 2023-07-09 op return err;
82 f7d653fc 2023-07-09 op
83 f7d653fc 2023-07-09 op if (fchmod(*fd, GOT_DEFAULT_FILE_MODE) == -1)
84 f7d653fc 2023-07-09 op return got_error_from_errno("fchmod");
85 f7d653fc 2023-07-09 op
86 f7d653fc 2023-07-09 op return NULL;
87 f7d653fc 2023-07-09 op }
88 f7d653fc 2023-07-09 op
89 f7d653fc 2023-07-09 op static const struct got_error *
90 f7d653fc 2023-07-09 op load_report_progress(got_load_progress_cb progress_cb, void *progress_arg,
91 f7d653fc 2023-07-09 op struct got_ratelimit *rl, off_t packsiz, int nobj_total,
92 f7d653fc 2023-07-09 op int nobj_indexed, int nobj_loose, int nobj_resolved)
93 f7d653fc 2023-07-09 op {
94 f7d653fc 2023-07-09 op const struct got_error *err;
95 f7d653fc 2023-07-09 op int elapsed;
96 f7d653fc 2023-07-09 op
97 f7d653fc 2023-07-09 op if (progress_cb == NULL)
98 f7d653fc 2023-07-09 op return NULL;
99 f7d653fc 2023-07-09 op
100 f7d653fc 2023-07-09 op err = got_ratelimit_check(&elapsed, rl);
101 f7d653fc 2023-07-09 op if (err || !elapsed)
102 f7d653fc 2023-07-09 op return err;
103 f7d653fc 2023-07-09 op
104 f7d653fc 2023-07-09 op return progress_cb(progress_arg, packsiz, nobj_total, nobj_indexed,
105 f7d653fc 2023-07-09 op nobj_loose, nobj_resolved);
106 f7d653fc 2023-07-09 op }
107 f7d653fc 2023-07-09 op
108 f7d653fc 2023-07-09 op static const struct got_error *
109 f7d653fc 2023-07-09 op copypack(FILE *in, int outfd, off_t *tot,
110 f7d653fc 2023-07-09 op struct got_object_id *id, struct got_ratelimit *rl,
111 f7d653fc 2023-07-09 op got_load_progress_cb progress_cb, void *progress_arg,
112 f7d653fc 2023-07-09 op got_cancel_cb cancel_cb, void *cancel_arg)
113 f7d653fc 2023-07-09 op {
114 f7d653fc 2023-07-09 op const struct got_error *err;
115 f7d653fc 2023-07-09 op struct got_hash hash;
116 f7d653fc 2023-07-09 op struct got_object_id expected_id;
117 f7d653fc 2023-07-09 op char buf[BUFSIZ], sha1buf[SHA1_DIGEST_LENGTH];
118 f7d653fc 2023-07-09 op size_t r, sha1buflen = 0;
119 f7d653fc 2023-07-09 op
120 f7d653fc 2023-07-09 op *tot = 0;
121 f7d653fc 2023-07-09 op got_hash_init(&hash, GOT_HASH_SHA1);
122 f7d653fc 2023-07-09 op
123 f7d653fc 2023-07-09 op for (;;) {
124 f7d653fc 2023-07-09 op err = cancel_cb(cancel_arg);
125 f7d653fc 2023-07-09 op if (err)
126 f7d653fc 2023-07-09 op return err;
127 f7d653fc 2023-07-09 op
128 f7d653fc 2023-07-09 op r = fread(buf, 1, sizeof(buf), in);
129 f7d653fc 2023-07-09 op if (r == 0)
130 f7d653fc 2023-07-09 op break;
131 f7d653fc 2023-07-09 op
132 f7d653fc 2023-07-09 op /*
133 f7d653fc 2023-07-09 op * An expected SHA1 checksum sits at the end of the
134 f7d653fc 2023-07-09 op * pack file. Since we don't know the file size ahead
135 f7d653fc 2023-07-09 op * of time we have to keep SHA1_DIGEST_LENGTH bytes
136 f7d653fc 2023-07-09 op * buffered and avoid mixing those bytes int our hash
137 f7d653fc 2023-07-09 op * computation until we know for sure that additional
138 f7d653fc 2023-07-09 op * pack file data bytes follow.
139 f7d653fc 2023-07-09 op *
140 f7d653fc 2023-07-09 op * We can assume that BUFSIZE is greater than
141 f7d653fc 2023-07-09 op * SHA1_DIGEST_LENGTH and that a short read means that
142 f7d653fc 2023-07-09 op * we've reached EOF.
143 f7d653fc 2023-07-09 op */
144 f7d653fc 2023-07-09 op
145 f7d653fc 2023-07-09 op if (r >= sizeof(sha1buf)) {
146 f7d653fc 2023-07-09 op *tot += sha1buflen;
147 f7d653fc 2023-07-09 op got_hash_update(&hash, sha1buf, sha1buflen);
148 f7d653fc 2023-07-09 op if (write(outfd, sha1buf, sha1buflen) == -1)
149 f7d653fc 2023-07-09 op return got_error_from_errno("write");
150 f7d653fc 2023-07-09 op
151 f7d653fc 2023-07-09 op r -= sizeof(sha1buf);
152 f7d653fc 2023-07-09 op memcpy(sha1buf, &buf[r], sizeof(sha1buf));
153 f7d653fc 2023-07-09 op sha1buflen = sizeof(sha1buf);
154 f7d653fc 2023-07-09 op
155 f7d653fc 2023-07-09 op *tot += r;
156 f7d653fc 2023-07-09 op got_hash_update(&hash, buf, r);
157 f7d653fc 2023-07-09 op if (write(outfd, buf, r) == -1)
158 f7d653fc 2023-07-09 op return got_error_from_errno("write");
159 f7d653fc 2023-07-09 op
160 f7d653fc 2023-07-09 op err = load_report_progress(progress_cb, progress_arg,
161 f7d653fc 2023-07-09 op rl, *tot, 0, 0, 0, 0);
162 f7d653fc 2023-07-09 op if (err)
163 f7d653fc 2023-07-09 op return err;
164 f7d653fc 2023-07-09 op
165 f7d653fc 2023-07-09 op continue;
166 f7d653fc 2023-07-09 op }
167 f7d653fc 2023-07-09 op
168 f7d653fc 2023-07-09 op if (sha1buflen == 0)
169 f7d653fc 2023-07-09 op return got_error(GOT_ERR_BAD_PACKFILE);
170 f7d653fc 2023-07-09 op
171 f7d653fc 2023-07-09 op /* short read, we've reached EOF */
172 f7d653fc 2023-07-09 op *tot += r;
173 f7d653fc 2023-07-09 op got_hash_update(&hash, sha1buf, r);
174 f7d653fc 2023-07-09 op if (write(outfd, sha1buf, r) == -1)
175 f7d653fc 2023-07-09 op return got_error_from_errno("write");
176 f7d653fc 2023-07-09 op
177 f7d653fc 2023-07-09 op memmove(&sha1buf[0], &sha1buf[r], sizeof(sha1buf) - r);
178 f7d653fc 2023-07-09 op memcpy(&sha1buf[sizeof(sha1buf) - r], buf, r);
179 f7d653fc 2023-07-09 op break;
180 f7d653fc 2023-07-09 op }
181 f7d653fc 2023-07-09 op
182 f7d653fc 2023-07-09 op if (sha1buflen == 0)
183 f7d653fc 2023-07-09 op return got_error(GOT_ERR_BAD_PACKFILE);
184 f7d653fc 2023-07-09 op
185 f7d653fc 2023-07-09 op got_hash_final_object_id(&hash, id);
186 f7d653fc 2023-07-09 op
187 f7d653fc 2023-07-09 op /* XXX SHA256 */
188 f7d653fc 2023-07-09 op memset(&expected_id, 0, sizeof(expected_id));
189 f7d653fc 2023-07-09 op memcpy(&expected_id.sha1, sha1buf, sizeof(expected_id.sha1));
190 f7d653fc 2023-07-09 op
191 f7d653fc 2023-07-09 op if (got_object_id_cmp(id, &expected_id) != 0)
192 f7d653fc 2023-07-09 op return got_error(GOT_ERR_PACKIDX_CSUM);
193 f7d653fc 2023-07-09 op
194 f7d653fc 2023-07-09 op /* re-add the expected hash at the end of the pack */
195 f7d653fc 2023-07-09 op if (write(outfd, sha1buf, sizeof(sha1buf)) == -1)
196 f7d653fc 2023-07-09 op return got_error_from_errno("write");
197 f7d653fc 2023-07-09 op
198 f7d653fc 2023-07-09 op *tot += sizeof(sha1buf);
199 f7d653fc 2023-07-09 op err = progress_cb(progress_arg, *tot, 0, 0, 0, 0);
200 f7d653fc 2023-07-09 op if (err)
201 f7d653fc 2023-07-09 op return err;
202 f7d653fc 2023-07-09 op
203 f7d653fc 2023-07-09 op return NULL;
204 f7d653fc 2023-07-09 op }
205 f7d653fc 2023-07-09 op
206 f7d653fc 2023-07-09 op const struct got_error *
207 f7d653fc 2023-07-09 op got_repo_load(FILE *in, struct got_pathlist_head *refs_found,
208 f7d653fc 2023-07-09 op struct got_repository *repo, int list_refs_only, int noop,
209 f7d653fc 2023-07-09 op got_load_progress_cb progress_cb, void *progress_arg,
210 f7d653fc 2023-07-09 op got_cancel_cb cancel_cb, void *cancel_arg)
211 f7d653fc 2023-07-09 op {
212 f7d653fc 2023-07-09 op const struct got_error *err = NULL;
213 f7d653fc 2023-07-09 op struct got_object_id id;
214 f7d653fc 2023-07-09 op struct got_object *obj;
215 f7d653fc 2023-07-09 op struct got_packfile_hdr pack_hdr;
216 f7d653fc 2023-07-09 op struct got_ratelimit rl;
217 f7d653fc 2023-07-09 op struct imsgbuf idxibuf;
218 f7d653fc 2023-07-09 op const char *repo_path;
219 f7d653fc 2023-07-09 op char *packpath = NULL, *idxpath = NULL;
220 f7d653fc 2023-07-09 op char *tmppackpath = NULL, *tmpidxpath = NULL;
221 f7d653fc 2023-07-09 op int packfd = -1, idxfd = -1;
222 f7d653fc 2023-07-09 op char *spc, *refname, *id_str = NULL;
223 f7d653fc 2023-07-09 op char *line = NULL;
224 f7d653fc 2023-07-09 op size_t linesize = 0;
225 f7d653fc 2023-07-09 op ssize_t linelen;
226 f7d653fc 2023-07-09 op size_t i;
227 f7d653fc 2023-07-09 op ssize_t n;
228 f7d653fc 2023-07-09 op off_t packsiz;
229 f7d653fc 2023-07-09 op int tmpfds[3] = {-1, -1, -1};
230 f7d653fc 2023-07-09 op int imsg_idxfds[2] = {-1, -1};
231 f7d653fc 2023-07-09 op int ch, done, nobj, idxstatus;
232 f7d653fc 2023-07-09 op pid_t idxpid;
233 f7d653fc 2023-07-09 op
234 f7d653fc 2023-07-09 op got_ratelimit_init(&rl, 0, 500);
235 f7d653fc 2023-07-09 op
236 f7d653fc 2023-07-09 op repo_path = got_repo_get_path_git_dir(repo);
237 f7d653fc 2023-07-09 op
238 f7d653fc 2023-07-09 op linelen = getline(&line, &linesize, in);
239 f7d653fc 2023-07-09 op if (linelen == -1) {
240 f7d653fc 2023-07-09 op err = got_ferror(in, GOT_ERR_IO);
241 f7d653fc 2023-07-09 op goto done;
242 f7d653fc 2023-07-09 op }
243 f7d653fc 2023-07-09 op
244 f7d653fc 2023-07-09 op if (strcmp(line, GIT_BUNDLE_SIGNATURE_V2) != 0) {
245 f7d653fc 2023-07-09 op err = got_error(GOT_ERR_BUNDLE_FORMAT);
246 f7d653fc 2023-07-09 op goto done;
247 f7d653fc 2023-07-09 op }
248 f7d653fc 2023-07-09 op
249 f7d653fc 2023-07-09 op /* Parse the prerequisite */
250 f7d653fc 2023-07-09 op for (;;) {
251 f7d653fc 2023-07-09 op ch = fgetc(in);
252 f7d653fc 2023-07-09 op if (ch != '-') {
253 f7d653fc 2023-07-09 op if (ch != EOF)
254 f7d653fc 2023-07-09 op ungetc(ch, in);
255 f7d653fc 2023-07-09 op break;
256 f7d653fc 2023-07-09 op }
257 f7d653fc 2023-07-09 op
258 f7d653fc 2023-07-09 op linelen = getline(&line, &linesize, in);
259 f7d653fc 2023-07-09 op if (linelen == -1) {
260 f7d653fc 2023-07-09 op err = got_ferror(in, GOT_ERR_IO);
261 f7d653fc 2023-07-09 op goto done;
262 f7d653fc 2023-07-09 op }
263 f7d653fc 2023-07-09 op
264 f7d653fc 2023-07-09 op if (line[linelen - 1] == '\n')
265 f7d653fc 2023-07-09 op line[linelen - 1] = '\0';
266 f7d653fc 2023-07-09 op
267 f7d653fc 2023-07-09 op if (!got_parse_object_id(&id, line, GOT_HASH_SHA1)) {
268 f7d653fc 2023-07-09 op err = got_error_path(line, GOT_ERR_BAD_OBJ_ID_STR);
269 f7d653fc 2023-07-09 op goto done;
270 f7d653fc 2023-07-09 op }
271 f7d653fc 2023-07-09 op
272 f7d653fc 2023-07-09 op err = got_object_open(&obj, repo, &id);
273 f7d653fc 2023-07-09 op if (err)
274 f7d653fc 2023-07-09 op goto done;
275 f7d653fc 2023-07-09 op got_object_close(obj);
276 f7d653fc 2023-07-09 op }
277 f7d653fc 2023-07-09 op
278 f7d653fc 2023-07-09 op /* Read references */
279 f7d653fc 2023-07-09 op for (;;) {
280 f7d653fc 2023-07-09 op struct got_object_id *id;
281 f7d653fc 2023-07-09 op char *dup;
282 f7d653fc 2023-07-09 op
283 f7d653fc 2023-07-09 op linelen = getline(&line, &linesize, in);
284 f7d653fc 2023-07-09 op if (linelen == -1) {
285 f7d653fc 2023-07-09 op err = got_ferror(in, GOT_ERR_IO);
286 f7d653fc 2023-07-09 op goto done;
287 f7d653fc 2023-07-09 op }
288 f7d653fc 2023-07-09 op if (line[linelen - 1] == '\n')
289 f7d653fc 2023-07-09 op line[linelen - 1] = '\0';
290 f7d653fc 2023-07-09 op if (*line == '\0')
291 f7d653fc 2023-07-09 op break;
292 f7d653fc 2023-07-09 op
293 f7d653fc 2023-07-09 op spc = strchr(line, ' ');
294 f7d653fc 2023-07-09 op if (spc == NULL) {
295 f7d653fc 2023-07-09 op err = got_error(GOT_ERR_IO);
296 f7d653fc 2023-07-09 op goto done;
297 f7d653fc 2023-07-09 op }
298 f7d653fc 2023-07-09 op *spc = '\0';
299 f7d653fc 2023-07-09 op
300 f7d653fc 2023-07-09 op refname = spc + 1;
301 f7d653fc 2023-07-09 op if (!got_ref_name_is_valid(refname)) {
302 f7d653fc 2023-07-09 op err = got_error(GOT_ERR_BAD_REF_DATA);
303 f7d653fc 2023-07-09 op goto done;
304 f7d653fc 2023-07-09 op }
305 f7d653fc 2023-07-09 op
306 f7d653fc 2023-07-09 op id = malloc(sizeof(*id));
307 f7d653fc 2023-07-09 op if (id == NULL) {
308 f7d653fc 2023-07-09 op err = got_error_from_errno("malloc");
309 f7d653fc 2023-07-09 op goto done;
310 f7d653fc 2023-07-09 op }
311 f7d653fc 2023-07-09 op
312 f7d653fc 2023-07-09 op if (!got_parse_object_id(id, line, GOT_HASH_SHA1)) {
313 f7d653fc 2023-07-09 op free(id);
314 f7d653fc 2023-07-09 op err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
315 f7d653fc 2023-07-09 op goto done;
316 f7d653fc 2023-07-09 op }
317 f7d653fc 2023-07-09 op
318 f7d653fc 2023-07-09 op dup = strdup(refname);
319 f7d653fc 2023-07-09 op if (dup == NULL) {
320 f7d653fc 2023-07-09 op free(id);
321 f7d653fc 2023-07-09 op err = got_error_from_errno("strdup");
322 f7d653fc 2023-07-09 op goto done;
323 f7d653fc 2023-07-09 op }
324 f7d653fc 2023-07-09 op
325 f7d653fc 2023-07-09 op err = got_pathlist_append(refs_found, dup, id);
326 f7d653fc 2023-07-09 op if (err) {
327 f7d653fc 2023-07-09 op free(id);
328 f7d653fc 2023-07-09 op free(dup);
329 f7d653fc 2023-07-09 op goto done;
330 f7d653fc 2023-07-09 op }
331 f7d653fc 2023-07-09 op }
332 f7d653fc 2023-07-09 op
333 f7d653fc 2023-07-09 op if (list_refs_only)
334 f7d653fc 2023-07-09 op goto done;
335 f7d653fc 2023-07-09 op
336 f7d653fc 2023-07-09 op err = temp_file(&packfd, &tmppackpath, ".pack", repo);
337 f7d653fc 2023-07-09 op if (err)
338 f7d653fc 2023-07-09 op goto done;
339 f7d653fc 2023-07-09 op
340 f7d653fc 2023-07-09 op err = temp_file(&idxfd, &tmpidxpath, ".idx", repo);
341 f7d653fc 2023-07-09 op if (err)
342 f7d653fc 2023-07-09 op goto done;
343 f7d653fc 2023-07-09 op
344 f7d653fc 2023-07-09 op err = copypack(in, packfd, &packsiz, &id, &rl,
345 f7d653fc 2023-07-09 op progress_cb, progress_arg, cancel_cb, cancel_arg);
346 f7d653fc 2023-07-09 op if (err)
347 f7d653fc 2023-07-09 op goto done;
348 f7d653fc 2023-07-09 op
349 f7d653fc 2023-07-09 op if (lseek(packfd, 0, SEEK_SET) == -1) {
350 f7d653fc 2023-07-09 op err = got_error_from_errno("lseek");
351 f7d653fc 2023-07-09 op goto done;
352 f7d653fc 2023-07-09 op }
353 f7d653fc 2023-07-09 op
354 f7d653fc 2023-07-09 op /* Safety checks on the pack' content. */
355 f7d653fc 2023-07-09 op if (packsiz <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
356 f7d653fc 2023-07-09 op err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
357 f7d653fc 2023-07-09 op goto done;
358 f7d653fc 2023-07-09 op }
359 f7d653fc 2023-07-09 op
360 f7d653fc 2023-07-09 op n = read(packfd, &pack_hdr, ssizeof(pack_hdr));
361 f7d653fc 2023-07-09 op if (n == -1) {
362 f7d653fc 2023-07-09 op err = got_error_from_errno("read");
363 f7d653fc 2023-07-09 op goto done;
364 f7d653fc 2023-07-09 op }
365 f7d653fc 2023-07-09 op if (n != ssizeof(pack_hdr)) {
366 f7d653fc 2023-07-09 op err = got_error(GOT_ERR_IO);
367 f7d653fc 2023-07-09 op goto done;
368 f7d653fc 2023-07-09 op }
369 f7d653fc 2023-07-09 op if (pack_hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)) {
370 f7d653fc 2023-07-09 op err = got_error_msg(GOT_ERR_BAD_PACKFILE,
371 f7d653fc 2023-07-09 op "bad pack file signature");
372 f7d653fc 2023-07-09 op goto done;
373 f7d653fc 2023-07-09 op }
374 f7d653fc 2023-07-09 op if (pack_hdr.version != htobe32(GOT_PACKFILE_VERSION)) {
375 f7d653fc 2023-07-09 op err = got_error_msg(GOT_ERR_BAD_PACKFILE,
376 f7d653fc 2023-07-09 op "bad pack file version");
377 f7d653fc 2023-07-09 op goto done;
378 f7d653fc 2023-07-09 op }
379 f7d653fc 2023-07-09 op nobj = be32toh(pack_hdr.nobjects);
380 f7d653fc 2023-07-09 op if (nobj == 0 &&
381 f7d653fc 2023-07-09 op packsiz > ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
382 f7d653fc 2023-07-09 op err = got_error_msg(GOT_ERR_BAD_PACKFILE,
383 f7d653fc 2023-07-09 op "bad pack file with zero objects");
384 f7d653fc 2023-07-09 op goto done;
385 f7d653fc 2023-07-09 op }
386 f7d653fc 2023-07-09 op if (nobj != 0 &&
387 f7d653fc 2023-07-09 op packsiz <= ssizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
388 f7d653fc 2023-07-09 op err = got_error_msg(GOT_ERR_BAD_PACKFILE,
389 f7d653fc 2023-07-09 op "empty pack file with non-zero object count");
390 f7d653fc 2023-07-09 op goto done;
391 f7d653fc 2023-07-09 op }
392 f7d653fc 2023-07-09 op
393 f7d653fc 2023-07-09 op /* nothing to do if there are no objects. */
394 f7d653fc 2023-07-09 op if (nobj == 0)
395 f7d653fc 2023-07-09 op goto done;
396 f7d653fc 2023-07-09 op
397 f7d653fc 2023-07-09 op for (i = 0; i < nitems(tmpfds); i++) {
398 f7d653fc 2023-07-09 op tmpfds[i] = got_opentempfd();
399 f7d653fc 2023-07-09 op if (tmpfds[i] == -1) {
400 f7d653fc 2023-07-09 op err = got_error_from_errno("got_opentempfd");
401 f7d653fc 2023-07-09 op goto done;
402 f7d653fc 2023-07-09 op }
403 f7d653fc 2023-07-09 op }
404 f7d653fc 2023-07-09 op
405 f7d653fc 2023-07-09 op if (lseek(packfd, 0, SEEK_SET) == -1) {
406 f7d653fc 2023-07-09 op err = got_error_from_errno("lseek");
407 f7d653fc 2023-07-09 op goto done;
408 f7d653fc 2023-07-09 op }
409 f7d653fc 2023-07-09 op
410 f7d653fc 2023-07-09 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
411 f7d653fc 2023-07-09 op err = got_error_from_errno("socketpair");
412 f7d653fc 2023-07-09 op goto done;
413 f7d653fc 2023-07-09 op }
414 f7d653fc 2023-07-09 op idxpid = fork();
415 f7d653fc 2023-07-09 op if (idxpid == -1) {
416 f7d653fc 2023-07-09 op err= got_error_from_errno("fork");
417 f7d653fc 2023-07-09 op goto done;
418 f7d653fc 2023-07-09 op } else if (idxpid == 0)
419 f7d653fc 2023-07-09 op got_privsep_exec_child(imsg_idxfds,
420 f7d653fc 2023-07-09 op GOT_PATH_PROG_INDEX_PACK, tmppackpath);
421 f7d653fc 2023-07-09 op if (close(imsg_idxfds[1]) == -1) {
422 f7d653fc 2023-07-09 op err = got_error_from_errno("close");
423 f7d653fc 2023-07-09 op goto done;
424 f7d653fc 2023-07-09 op }
425 f7d653fc 2023-07-09 op imsg_idxfds[1] = -1;
426 f7d653fc 2023-07-09 op imsg_init(&idxibuf, imsg_idxfds[0]);
427 f7d653fc 2023-07-09 op
428 f7d653fc 2023-07-09 op err = got_privsep_send_index_pack_req(&idxibuf, id.sha1, packfd);
429 f7d653fc 2023-07-09 op if (err)
430 f7d653fc 2023-07-09 op goto done;
431 f7d653fc 2023-07-09 op packfd = -1;
432 f7d653fc 2023-07-09 op
433 f7d653fc 2023-07-09 op err = got_privsep_send_index_pack_outfd(&idxibuf, idxfd);
434 f7d653fc 2023-07-09 op if (err)
435 f7d653fc 2023-07-09 op goto done;
436 f7d653fc 2023-07-09 op idxfd = -1;
437 f7d653fc 2023-07-09 op
438 f7d653fc 2023-07-09 op for (i = 0; i < nitems(tmpfds); i++) {
439 f7d653fc 2023-07-09 op err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
440 f7d653fc 2023-07-09 op if (err != NULL)
441 f7d653fc 2023-07-09 op goto done;
442 f7d653fc 2023-07-09 op tmpfds[i] = -1;
443 f7d653fc 2023-07-09 op }
444 f7d653fc 2023-07-09 op
445 f7d653fc 2023-07-09 op done = 0;
446 f7d653fc 2023-07-09 op while (!done) {
447 f7d653fc 2023-07-09 op int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
448 f7d653fc 2023-07-09 op
449 f7d653fc 2023-07-09 op err = got_privsep_recv_index_progress(&done, &nobj_total,
450 f7d653fc 2023-07-09 op &nobj_indexed, &nobj_loose, &nobj_resolved, &idxibuf);
451 f7d653fc 2023-07-09 op if (err)
452 f7d653fc 2023-07-09 op goto done;
453 f7d653fc 2023-07-09 op if (nobj_indexed != 0) {
454 f7d653fc 2023-07-09 op err = load_report_progress(progress_cb, progress_arg,
455 f7d653fc 2023-07-09 op &rl, packsiz, nobj_total, nobj_indexed,
456 f7d653fc 2023-07-09 op nobj_loose, nobj_resolved);
457 f7d653fc 2023-07-09 op if (err)
458 f7d653fc 2023-07-09 op goto done;
459 f7d653fc 2023-07-09 op }
460 f7d653fc 2023-07-09 op }
461 f7d653fc 2023-07-09 op if (close(imsg_idxfds[0]) == -1) {
462 f7d653fc 2023-07-09 op err = got_error_from_errno("close");
463 f7d653fc 2023-07-09 op goto done;
464 f7d653fc 2023-07-09 op }
465 f7d653fc 2023-07-09 op imsg_idxfds[0] = -1;
466 f7d653fc 2023-07-09 op if (waitpid(idxpid, &idxstatus, 0) == -1) {
467 f7d653fc 2023-07-09 op err = got_error_from_errno("waitpid");
468 f7d653fc 2023-07-09 op goto done;
469 f7d653fc 2023-07-09 op }
470 f7d653fc 2023-07-09 op
471 f7d653fc 2023-07-09 op if (noop)
472 f7d653fc 2023-07-09 op goto done;
473 f7d653fc 2023-07-09 op
474 f7d653fc 2023-07-09 op err = got_object_id_str(&id_str, &id);
475 f7d653fc 2023-07-09 op if (err)
476 f7d653fc 2023-07-09 op goto done;
477 f7d653fc 2023-07-09 op
478 f7d653fc 2023-07-09 op if (asprintf(&packpath, "%s/%s/pack-%s.pack", repo_path,
479 f7d653fc 2023-07-09 op GOT_OBJECTS_PACK_DIR, id_str) == -1) {
480 f7d653fc 2023-07-09 op err = got_error_from_errno("asprintf");
481 f7d653fc 2023-07-09 op goto done;
482 f7d653fc 2023-07-09 op }
483 f7d653fc 2023-07-09 op
484 f7d653fc 2023-07-09 op if (asprintf(&idxpath, "%s/%s/pack-%s.idx", repo_path,
485 f7d653fc 2023-07-09 op GOT_OBJECTS_PACK_DIR, id_str) == -1) {
486 f7d653fc 2023-07-09 op err = got_error_from_errno("asprintf");
487 f7d653fc 2023-07-09 op goto done;
488 f7d653fc 2023-07-09 op }
489 f7d653fc 2023-07-09 op
490 f7d653fc 2023-07-09 op if (rename(tmppackpath, packpath) == -1) {
491 f7d653fc 2023-07-09 op err = got_error_from_errno3("rename", tmppackpath, packpath);
492 f7d653fc 2023-07-09 op goto done;
493 f7d653fc 2023-07-09 op }
494 f7d653fc 2023-07-09 op free(tmppackpath);
495 f7d653fc 2023-07-09 op tmppackpath = NULL;
496 f7d653fc 2023-07-09 op
497 f7d653fc 2023-07-09 op if (rename(tmpidxpath, idxpath) == -1) {
498 f7d653fc 2023-07-09 op err = got_error_from_errno3("rename", tmpidxpath, idxpath);
499 f7d653fc 2023-07-09 op goto done;
500 f7d653fc 2023-07-09 op }
501 f7d653fc 2023-07-09 op free(tmpidxpath);
502 f7d653fc 2023-07-09 op tmpidxpath = NULL;
503 f7d653fc 2023-07-09 op
504 f7d653fc 2023-07-09 op done:
505 f7d653fc 2023-07-09 op free(line);
506 f7d653fc 2023-07-09 op free(packpath);
507 f7d653fc 2023-07-09 op free(idxpath);
508 f7d653fc 2023-07-09 op free(id_str);
509 f7d653fc 2023-07-09 op
510 f7d653fc 2023-07-09 op if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
511 f7d653fc 2023-07-09 op err = got_error_from_errno2("unlink", tmppackpath);
512 f7d653fc 2023-07-09 op if (packfd != -1 && close(packfd) == -1 && err == NULL)
513 f7d653fc 2023-07-09 op err = got_error_from_errno("close");
514 f7d653fc 2023-07-09 op free(tmppackpath);
515 f7d653fc 2023-07-09 op
516 f7d653fc 2023-07-09 op if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
517 f7d653fc 2023-07-09 op err = got_error_from_errno2("unlink", tmpidxpath);
518 f7d653fc 2023-07-09 op if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
519 f7d653fc 2023-07-09 op err = got_error_from_errno("close");
520 f7d653fc 2023-07-09 op free(tmpidxpath);
521 f7d653fc 2023-07-09 op
522 f7d653fc 2023-07-09 op if (imsg_idxfds[0] != -1 && close(imsg_idxfds[0]) == -1 && err == NULL)
523 f7d653fc 2023-07-09 op err = got_error_from_errno("close");
524 f7d653fc 2023-07-09 op if (imsg_idxfds[1] != -1 && close(imsg_idxfds[1]) == -1 && err == NULL)
525 f7d653fc 2023-07-09 op err = got_error_from_errno("close");
526 f7d653fc 2023-07-09 op
527 f7d653fc 2023-07-09 op for (i = 0; i < nitems(tmpfds); ++i)
528 f7d653fc 2023-07-09 op if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
529 f7d653fc 2023-07-09 op err = got_error_from_errno("close");
530 f7d653fc 2023-07-09 op
531 f7d653fc 2023-07-09 op return err;
532 f7d653fc 2023-07-09 op }