Blame


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