Blame


1 05118f5a 2021-06-22 stsp /*
2 05118f5a 2021-06-22 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 05118f5a 2021-06-22 stsp *
4 05118f5a 2021-06-22 stsp * Permission to use, copy, modify, and distribute this software for any
5 05118f5a 2021-06-22 stsp * purpose with or without fee is hereby granted, provided that the above
6 05118f5a 2021-06-22 stsp * copyright notice and this permission notice appear in all copies.
7 05118f5a 2021-06-22 stsp *
8 05118f5a 2021-06-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 05118f5a 2021-06-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 05118f5a 2021-06-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 05118f5a 2021-06-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 05118f5a 2021-06-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 05118f5a 2021-06-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 05118f5a 2021-06-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 05118f5a 2021-06-22 stsp */
16 05118f5a 2021-06-22 stsp
17 05118f5a 2021-06-22 stsp #include <sys/types.h>
18 05118f5a 2021-06-22 stsp #include <sys/queue.h>
19 05118f5a 2021-06-22 stsp #include <sys/uio.h>
20 05118f5a 2021-06-22 stsp #include <sys/stat.h>
21 05118f5a 2021-06-22 stsp #include <sys/socket.h>
22 05118f5a 2021-06-22 stsp #include <sys/wait.h>
23 05118f5a 2021-06-22 stsp
24 05118f5a 2021-06-22 stsp #include <dirent.h>
25 05118f5a 2021-06-22 stsp #include <errno.h>
26 05118f5a 2021-06-22 stsp #include <fcntl.h>
27 05118f5a 2021-06-22 stsp #include <stdint.h>
28 05118f5a 2021-06-22 stsp #include <sha1.h>
29 05118f5a 2021-06-22 stsp #include <stdio.h>
30 05118f5a 2021-06-22 stsp #include <stdlib.h>
31 05118f5a 2021-06-22 stsp #include <string.h>
32 05118f5a 2021-06-22 stsp #include <limits.h>
33 05118f5a 2021-06-22 stsp #include <unistd.h>
34 05118f5a 2021-06-22 stsp #include <imsg.h>
35 05118f5a 2021-06-22 stsp
36 05118f5a 2021-06-22 stsp #include "got_error.h"
37 05118f5a 2021-06-22 stsp #include "got_cancel.h"
38 05118f5a 2021-06-22 stsp #include "got_object.h"
39 05118f5a 2021-06-22 stsp #include "got_reference.h"
40 05118f5a 2021-06-22 stsp #include "got_repository.h"
41 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
42 05118f5a 2021-06-22 stsp #include "got_opentemp.h"
43 05118f5a 2021-06-22 stsp #include "got_path.h"
44 05118f5a 2021-06-22 stsp
45 05118f5a 2021-06-22 stsp #include "got_lib_delta.h"
46 05118f5a 2021-06-22 stsp #include "got_lib_object.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
48 05118f5a 2021-06-22 stsp #include "got_lib_pack.h"
49 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_pack_create.h"
52 05118f5a 2021-06-22 stsp #include "got_lib_sha1.h"
53 05118f5a 2021-06-22 stsp
54 05118f5a 2021-06-22 stsp #ifndef nitems
55 05118f5a 2021-06-22 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 05118f5a 2021-06-22 stsp #endif
57 05118f5a 2021-06-22 stsp
58 05118f5a 2021-06-22 stsp static const struct got_error *
59 05118f5a 2021-06-22 stsp get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
60 05118f5a 2021-06-22 stsp unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
61 05118f5a 2021-06-22 stsp struct got_repository *repo,
62 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
63 05118f5a 2021-06-22 stsp {
64 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
65 05118f5a 2021-06-22 stsp const size_t alloc_chunksz = 256;
66 05118f5a 2021-06-22 stsp size_t nalloc;
67 05118f5a 2021-06-22 stsp struct got_reflist_entry *re;
68 05118f5a 2021-06-22 stsp int i;
69 05118f5a 2021-06-22 stsp
70 05118f5a 2021-06-22 stsp *ids = NULL;
71 05118f5a 2021-06-22 stsp *nobjects = 0;
72 05118f5a 2021-06-22 stsp
73 05118f5a 2021-06-22 stsp *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
74 05118f5a 2021-06-22 stsp if (*ids == NULL)
75 05118f5a 2021-06-22 stsp return got_error_from_errno("reallocarray");
76 05118f5a 2021-06-22 stsp nalloc = alloc_chunksz;
77 05118f5a 2021-06-22 stsp
78 05118f5a 2021-06-22 stsp TAILQ_FOREACH(re, refs, entry) {
79 05118f5a 2021-06-22 stsp struct got_object_id *id;
80 05118f5a 2021-06-22 stsp
81 05118f5a 2021-06-22 stsp if (cancel_cb) {
82 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
83 05118f5a 2021-06-22 stsp if (err)
84 05118f5a 2021-06-22 stsp goto done;
85 05118f5a 2021-06-22 stsp }
86 05118f5a 2021-06-22 stsp
87 05118f5a 2021-06-22 stsp err = got_ref_resolve(&id, repo, re->ref);
88 05118f5a 2021-06-22 stsp if (err)
89 05118f5a 2021-06-22 stsp goto done;
90 05118f5a 2021-06-22 stsp
91 05118f5a 2021-06-22 stsp if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
92 05118f5a 2021-06-22 stsp int obj_type;
93 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
94 05118f5a 2021-06-22 stsp if (err)
95 05118f5a 2021-06-22 stsp goto done;
96 05118f5a 2021-06-22 stsp if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
97 05118f5a 2021-06-22 stsp free(id);
98 05118f5a 2021-06-22 stsp id = NULL;
99 05118f5a 2021-06-22 stsp continue;
100 05118f5a 2021-06-22 stsp }
101 05118f5a 2021-06-22 stsp }
102 05118f5a 2021-06-22 stsp
103 05118f5a 2021-06-22 stsp if (nalloc >= *nobjects) {
104 05118f5a 2021-06-22 stsp struct got_object_id **new;
105 05118f5a 2021-06-22 stsp new = recallocarray(*ids, nalloc,
106 05118f5a 2021-06-22 stsp nalloc + alloc_chunksz,
107 05118f5a 2021-06-22 stsp sizeof(struct got_object_id *));
108 05118f5a 2021-06-22 stsp if (new == NULL) {
109 05118f5a 2021-06-22 stsp err = got_error_from_errno(
110 05118f5a 2021-06-22 stsp "recallocarray");
111 05118f5a 2021-06-22 stsp goto done;
112 05118f5a 2021-06-22 stsp }
113 05118f5a 2021-06-22 stsp *ids = new;
114 05118f5a 2021-06-22 stsp nalloc += alloc_chunksz;
115 05118f5a 2021-06-22 stsp }
116 05118f5a 2021-06-22 stsp (*ids)[*nobjects] = id;
117 05118f5a 2021-06-22 stsp if ((*ids)[*nobjects] == NULL) {
118 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
119 05118f5a 2021-06-22 stsp goto done;
120 05118f5a 2021-06-22 stsp }
121 05118f5a 2021-06-22 stsp (*nobjects)++;
122 05118f5a 2021-06-22 stsp }
123 05118f5a 2021-06-22 stsp done:
124 05118f5a 2021-06-22 stsp if (err) {
125 05118f5a 2021-06-22 stsp for (i = 0; i < *nobjects; i++)
126 05118f5a 2021-06-22 stsp free((*ids)[i]);
127 05118f5a 2021-06-22 stsp free(*ids);
128 05118f5a 2021-06-22 stsp *ids = NULL;
129 05118f5a 2021-06-22 stsp *nobjects = 0;
130 05118f5a 2021-06-22 stsp }
131 05118f5a 2021-06-22 stsp return err;
132 05118f5a 2021-06-22 stsp }
133 05118f5a 2021-06-22 stsp
134 05118f5a 2021-06-22 stsp const struct got_error *
135 05118f5a 2021-06-22 stsp got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
136 05118f5a 2021-06-22 stsp struct got_reflist_head *include_refs,
137 05118f5a 2021-06-22 stsp struct got_reflist_head *exclude_refs, struct got_repository *repo,
138 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
139 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
140 05118f5a 2021-06-22 stsp {
141 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
142 05118f5a 2021-06-22 stsp struct got_object_id **ours = NULL, **theirs = NULL;
143 05118f5a 2021-06-22 stsp int nours = 0, ntheirs = 0, packfd = -1, i;
144 05118f5a 2021-06-22 stsp char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
145 05118f5a 2021-06-22 stsp char *sha1_str = NULL;
146 05118f5a 2021-06-22 stsp
147 05118f5a 2021-06-22 stsp *packfile = NULL;
148 05118f5a 2021-06-22 stsp *pack_hash = NULL;
149 05118f5a 2021-06-22 stsp
150 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/packing.pack",
151 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
152 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
153 05118f5a 2021-06-22 stsp goto done;
154 05118f5a 2021-06-22 stsp }
155 05118f5a 2021-06-22 stsp err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
156 05118f5a 2021-06-22 stsp if (err)
157 05118f5a 2021-06-22 stsp goto done;
158 05118f5a 2021-06-22 stsp
159 05118f5a 2021-06-22 stsp if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
160 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpfile_path);
161 05118f5a 2021-06-22 stsp goto done;
162 05118f5a 2021-06-22 stsp }
163 05118f5a 2021-06-22 stsp
164 05118f5a 2021-06-22 stsp *packfile = fdopen(packfd, "w");
165 05118f5a 2021-06-22 stsp if (*packfile == NULL) {
166 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fdopen", tmpfile_path);
167 05118f5a 2021-06-22 stsp goto done;
168 05118f5a 2021-06-22 stsp }
169 05118f5a 2021-06-22 stsp packfd = -1;
170 05118f5a 2021-06-22 stsp
171 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&ours, &nours,
172 05118f5a 2021-06-22 stsp (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
173 05118f5a 2021-06-22 stsp include_refs, repo, cancel_cb, cancel_arg);
174 05118f5a 2021-06-22 stsp if (err)
175 05118f5a 2021-06-22 stsp goto done;
176 05118f5a 2021-06-22 stsp
177 05118f5a 2021-06-22 stsp if (nours == 0) {
178 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
179 05118f5a 2021-06-22 stsp goto done;
180 05118f5a 2021-06-22 stsp }
181 05118f5a 2021-06-22 stsp
182 05118f5a 2021-06-22 stsp if (!TAILQ_EMPTY(exclude_refs)) {
183 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&theirs, &ntheirs,
184 05118f5a 2021-06-22 stsp (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
185 05118f5a 2021-06-22 stsp exclude_refs, repo,
186 05118f5a 2021-06-22 stsp cancel_cb, cancel_arg);
187 05118f5a 2021-06-22 stsp if (err)
188 05118f5a 2021-06-22 stsp goto done;
189 05118f5a 2021-06-22 stsp }
190 05118f5a 2021-06-22 stsp
191 05118f5a 2021-06-22 stsp *pack_hash = calloc(1, sizeof(**pack_hash));
192 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
193 05118f5a 2021-06-22 stsp err = got_error_from_errno("calloc");
194 05118f5a 2021-06-22 stsp goto done;
195 05118f5a 2021-06-22 stsp }
196 05118f5a 2021-06-22 stsp
197 05118f5a 2021-06-22 stsp err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
198 05118f5a 2021-06-22 stsp ours, nours, repo, loose_obj_only, progress_cb, progress_arg,
199 05118f5a 2021-06-22 stsp cancel_cb, cancel_arg);
200 05118f5a 2021-06-22 stsp if (err)
201 05118f5a 2021-06-22 stsp goto done;
202 05118f5a 2021-06-22 stsp
203 05118f5a 2021-06-22 stsp err = got_object_id_str(&sha1_str, *pack_hash);
204 05118f5a 2021-06-22 stsp if (err)
205 05118f5a 2021-06-22 stsp goto done;
206 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
207 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
208 05118f5a 2021-06-22 stsp sha1_str) == -1) {
209 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
210 05118f5a 2021-06-22 stsp goto done;
211 05118f5a 2021-06-22 stsp }
212 05118f5a 2021-06-22 stsp
213 05118f5a 2021-06-22 stsp if (fflush(*packfile) == -1) {
214 05118f5a 2021-06-22 stsp err = got_error_from_errno("fflush");
215 05118f5a 2021-06-22 stsp goto done;
216 05118f5a 2021-06-22 stsp }
217 05118f5a 2021-06-22 stsp if (fseek(*packfile, 0L, SEEK_SET) == -1) {
218 05118f5a 2021-06-22 stsp err = got_error_from_errno("fseek");
219 05118f5a 2021-06-22 stsp goto done;
220 05118f5a 2021-06-22 stsp }
221 05118f5a 2021-06-22 stsp if (rename(tmpfile_path, packfile_path) == -1) {
222 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpfile_path,
223 05118f5a 2021-06-22 stsp packfile_path);
224 05118f5a 2021-06-22 stsp goto done;
225 05118f5a 2021-06-22 stsp }
226 05118f5a 2021-06-22 stsp free(tmpfile_path);
227 05118f5a 2021-06-22 stsp tmpfile_path = NULL;
228 05118f5a 2021-06-22 stsp done:
229 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++)
230 05118f5a 2021-06-22 stsp free(ours[i]);
231 05118f5a 2021-06-22 stsp free(ours);
232 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++)
233 05118f5a 2021-06-22 stsp free(theirs[i]);
234 05118f5a 2021-06-22 stsp free(theirs);
235 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
236 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
237 05118f5a 2021-06-22 stsp if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
238 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpfile_path);
239 05118f5a 2021-06-22 stsp free(tmpfile_path);
240 05118f5a 2021-06-22 stsp free(packfile_path);
241 05118f5a 2021-06-22 stsp free(sha1_str);
242 05118f5a 2021-06-22 stsp free(path);
243 05118f5a 2021-06-22 stsp if (err) {
244 05118f5a 2021-06-22 stsp free(*pack_hash);
245 05118f5a 2021-06-22 stsp *pack_hash = NULL;
246 05118f5a 2021-06-22 stsp if (*packfile)
247 05118f5a 2021-06-22 stsp fclose(*packfile);
248 05118f5a 2021-06-22 stsp *packfile = NULL;
249 05118f5a 2021-06-22 stsp }
250 05118f5a 2021-06-22 stsp return err;
251 05118f5a 2021-06-22 stsp }
252 05118f5a 2021-06-22 stsp
253 05118f5a 2021-06-22 stsp const struct got_error *
254 05118f5a 2021-06-22 stsp got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
255 05118f5a 2021-06-22 stsp struct got_repository *repo,
256 05118f5a 2021-06-22 stsp got_pack_index_progress_cb progress_cb, void *progress_arg,
257 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
258 05118f5a 2021-06-22 stsp {
259 05118f5a 2021-06-22 stsp size_t i;
260 05118f5a 2021-06-22 stsp char *path;
261 05118f5a 2021-06-22 stsp int imsg_idxfds[2];
262 05118f5a 2021-06-22 stsp int npackfd = -1, idxfd = -1, nidxfd = -1;
263 05118f5a 2021-06-22 stsp int tmpfds[3];
264 05118f5a 2021-06-22 stsp int idxstatus, done = 0;
265 05118f5a 2021-06-22 stsp const struct got_error *err;
266 05118f5a 2021-06-22 stsp struct imsgbuf idxibuf;
267 05118f5a 2021-06-22 stsp pid_t idxpid;
268 05118f5a 2021-06-22 stsp char *tmpidxpath = NULL;
269 05118f5a 2021-06-22 stsp char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
270 05118f5a 2021-06-22 stsp const char *repo_path = got_repo_get_path_git_dir(repo);
271 05118f5a 2021-06-22 stsp struct stat sb;
272 05118f5a 2021-06-22 stsp
273 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++)
274 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
275 05118f5a 2021-06-22 stsp
276 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/indexing.idx",
277 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
278 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
279 05118f5a 2021-06-22 stsp goto done;
280 05118f5a 2021-06-22 stsp }
281 05118f5a 2021-06-22 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
282 05118f5a 2021-06-22 stsp free(path);
283 05118f5a 2021-06-22 stsp if (err)
284 05118f5a 2021-06-22 stsp goto done;
285 05118f5a 2021-06-22 stsp if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
286 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpidxpath);
287 05118f5a 2021-06-22 stsp goto done;
288 05118f5a 2021-06-22 stsp }
289 05118f5a 2021-06-22 stsp
290 05118f5a 2021-06-22 stsp nidxfd = dup(idxfd);
291 05118f5a 2021-06-22 stsp if (nidxfd == -1) {
292 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
293 05118f5a 2021-06-22 stsp goto done;
294 05118f5a 2021-06-22 stsp }
295 05118f5a 2021-06-22 stsp
296 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
297 05118f5a 2021-06-22 stsp tmpfds[i] = got_opentempfd();
298 05118f5a 2021-06-22 stsp if (tmpfds[i] == -1) {
299 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_opentempfd");
300 05118f5a 2021-06-22 stsp goto done;
301 05118f5a 2021-06-22 stsp }
302 05118f5a 2021-06-22 stsp }
303 05118f5a 2021-06-22 stsp
304 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
305 05118f5a 2021-06-22 stsp if (err)
306 05118f5a 2021-06-22 stsp goto done;
307 05118f5a 2021-06-22 stsp
308 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
309 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
310 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
311 05118f5a 2021-06-22 stsp goto done;
312 05118f5a 2021-06-22 stsp }
313 05118f5a 2021-06-22 stsp
314 05118f5a 2021-06-22 stsp if (fstat(fileno(packfile), &sb) == -1) {
315 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fstat", packfile_path);
316 05118f5a 2021-06-22 stsp goto done;
317 05118f5a 2021-06-22 stsp }
318 05118f5a 2021-06-22 stsp
319 05118f5a 2021-06-22 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
320 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
321 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
322 05118f5a 2021-06-22 stsp goto done;
323 05118f5a 2021-06-22 stsp }
324 05118f5a 2021-06-22 stsp
325 05118f5a 2021-06-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
326 05118f5a 2021-06-22 stsp err = got_error_from_errno("socketpair");
327 05118f5a 2021-06-22 stsp goto done;
328 05118f5a 2021-06-22 stsp }
329 05118f5a 2021-06-22 stsp idxpid = fork();
330 05118f5a 2021-06-22 stsp if (idxpid == -1) {
331 05118f5a 2021-06-22 stsp err= got_error_from_errno("fork");
332 05118f5a 2021-06-22 stsp goto done;
333 05118f5a 2021-06-22 stsp } else if (idxpid == 0)
334 05118f5a 2021-06-22 stsp got_privsep_exec_child(imsg_idxfds,
335 05118f5a 2021-06-22 stsp GOT_PATH_PROG_INDEX_PACK, packfile_path);
336 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[1]) == -1) {
337 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
338 05118f5a 2021-06-22 stsp goto done;
339 05118f5a 2021-06-22 stsp }
340 05118f5a 2021-06-22 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
341 05118f5a 2021-06-22 stsp
342 05118f5a 2021-06-22 stsp npackfd = dup(fileno(packfile));
343 05118f5a 2021-06-22 stsp if (npackfd == -1) {
344 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
345 05118f5a 2021-06-22 stsp goto done;
346 05118f5a 2021-06-22 stsp }
347 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
348 05118f5a 2021-06-22 stsp npackfd);
349 05118f5a 2021-06-22 stsp if (err != NULL)
350 05118f5a 2021-06-22 stsp goto done;
351 05118f5a 2021-06-22 stsp npackfd = -1;
352 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
353 05118f5a 2021-06-22 stsp if (err != NULL)
354 05118f5a 2021-06-22 stsp goto done;
355 05118f5a 2021-06-22 stsp nidxfd = -1;
356 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
357 05118f5a 2021-06-22 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
358 05118f5a 2021-06-22 stsp if (err != NULL)
359 05118f5a 2021-06-22 stsp goto done;
360 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
361 05118f5a 2021-06-22 stsp }
362 05118f5a 2021-06-22 stsp done = 0;
363 05118f5a 2021-06-22 stsp while (!done) {
364 05118f5a 2021-06-22 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
365 05118f5a 2021-06-22 stsp
366 05118f5a 2021-06-22 stsp if (cancel_cb) {
367 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
368 05118f5a 2021-06-22 stsp if (err)
369 05118f5a 2021-06-22 stsp goto done;
370 05118f5a 2021-06-22 stsp }
371 05118f5a 2021-06-22 stsp
372 05118f5a 2021-06-22 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
373 05118f5a 2021-06-22 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
374 05118f5a 2021-06-22 stsp &idxibuf);
375 05118f5a 2021-06-22 stsp if (err != NULL)
376 05118f5a 2021-06-22 stsp goto done;
377 05118f5a 2021-06-22 stsp if (nobj_indexed != 0) {
378 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, sb.st_size,
379 05118f5a 2021-06-22 stsp nobj_total, nobj_indexed, nobj_loose,
380 05118f5a 2021-06-22 stsp nobj_resolved);
381 05118f5a 2021-06-22 stsp if (err)
382 05118f5a 2021-06-22 stsp break;
383 05118f5a 2021-06-22 stsp }
384 05118f5a 2021-06-22 stsp imsg_clear(&idxibuf);
385 05118f5a 2021-06-22 stsp }
386 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[0]) == -1) {
387 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
388 05118f5a 2021-06-22 stsp goto done;
389 05118f5a 2021-06-22 stsp }
390 05118f5a 2021-06-22 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
391 05118f5a 2021-06-22 stsp err = got_error_from_errno("waitpid");
392 05118f5a 2021-06-22 stsp goto done;
393 05118f5a 2021-06-22 stsp }
394 05118f5a 2021-06-22 stsp
395 05118f5a 2021-06-22 stsp if (rename(tmpidxpath, idxpath) == -1) {
396 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
397 05118f5a 2021-06-22 stsp goto done;
398 05118f5a 2021-06-22 stsp }
399 05118f5a 2021-06-22 stsp free(tmpidxpath);
400 05118f5a 2021-06-22 stsp tmpidxpath = NULL;
401 05118f5a 2021-06-22 stsp
402 05118f5a 2021-06-22 stsp done:
403 05118f5a 2021-06-22 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
404 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpidxpath);
405 05118f5a 2021-06-22 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
406 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
407 05118f5a 2021-06-22 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
408 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
409 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
410 05118f5a 2021-06-22 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
411 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
412 05118f5a 2021-06-22 stsp }
413 05118f5a 2021-06-22 stsp free(tmpidxpath);
414 05118f5a 2021-06-22 stsp free(idxpath);
415 05118f5a 2021-06-22 stsp free(packfile_path);
416 05118f5a 2021-06-22 stsp return err;
417 05118f5a 2021-06-22 stsp }
418 05118f5a 2021-06-22 stsp
419 05118f5a 2021-06-22 stsp const struct got_error *
420 05118f5a 2021-06-22 stsp got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
421 05118f5a 2021-06-22 stsp struct got_repository *repo, const char *packfile_path)
422 05118f5a 2021-06-22 stsp {
423 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
424 05118f5a 2021-06-22 stsp const char *packdir_path = NULL;
425 05118f5a 2021-06-22 stsp char *packfile_name = NULL, *p, *dot;
426 05118f5a 2021-06-22 stsp struct got_object_id id;
427 05118f5a 2021-06-22 stsp int packfd = -1;
428 05118f5a 2021-06-22 stsp
429 05118f5a 2021-06-22 stsp *packfile = NULL;
430 05118f5a 2021-06-22 stsp *pack_hash = NULL;
431 05118f5a 2021-06-22 stsp
432 05118f5a 2021-06-22 stsp packdir_path = got_repo_get_path_objects_pack(repo);
433 05118f5a 2021-06-22 stsp if (packdir_path == NULL)
434 05118f5a 2021-06-22 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
435 05118f5a 2021-06-22 stsp
436 05118f5a 2021-06-22 stsp if (!got_path_is_child(packfile_path, packdir_path,
437 05118f5a 2021-06-22 stsp strlen(packdir_path))) {
438 05118f5a 2021-06-22 stsp err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
439 05118f5a 2021-06-22 stsp goto done;
440 05118f5a 2021-06-22 stsp
441 05118f5a 2021-06-22 stsp }
442 05118f5a 2021-06-22 stsp
443 05118f5a 2021-06-22 stsp err = got_path_basename(&packfile_name, packfile_path);
444 05118f5a 2021-06-22 stsp if (err)
445 05118f5a 2021-06-22 stsp goto done;
446 05118f5a 2021-06-22 stsp p = packfile_name;
447 05118f5a 2021-06-22 stsp
448 05118f5a 2021-06-22 stsp if (strncmp(p, "pack-", 5) != 0) {
449 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
450 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
451 05118f5a 2021-06-22 stsp packfile_name);
452 05118f5a 2021-06-22 stsp goto done;
453 05118f5a 2021-06-22 stsp }
454 05118f5a 2021-06-22 stsp p += 5;
455 05118f5a 2021-06-22 stsp dot = strchr(p, '.');
456 05118f5a 2021-06-22 stsp if (dot == NULL) {
457 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
458 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
459 05118f5a 2021-06-22 stsp packfile_name);
460 05118f5a 2021-06-22 stsp goto done;
461 05118f5a 2021-06-22 stsp }
462 05118f5a 2021-06-22 stsp if (strcmp(dot + 1, "pack") != 0) {
463 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
464 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
465 05118f5a 2021-06-22 stsp packfile_name);
466 05118f5a 2021-06-22 stsp goto done;
467 05118f5a 2021-06-22 stsp }
468 05118f5a 2021-06-22 stsp *dot = '\0';
469 05118f5a 2021-06-22 stsp if (!got_parse_sha1_digest(id.sha1, p)) {
470 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
471 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
472 05118f5a 2021-06-22 stsp packfile_name);
473 05118f5a 2021-06-22 stsp goto done;
474 05118f5a 2021-06-22 stsp }
475 05118f5a 2021-06-22 stsp
476 05118f5a 2021-06-22 stsp *pack_hash = got_object_id_dup(&id);
477 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
478 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
479 05118f5a 2021-06-22 stsp goto done;
480 05118f5a 2021-06-22 stsp }
481 05118f5a 2021-06-22 stsp
482 05118f5a 2021-06-22 stsp packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW);
483 05118f5a 2021-06-22 stsp if (packfd == -1) {
484 05118f5a 2021-06-22 stsp err = got_error_from_errno2("open", packfile_path);
485 05118f5a 2021-06-22 stsp goto done;
486 05118f5a 2021-06-22 stsp }
487 05118f5a 2021-06-22 stsp
488 05118f5a 2021-06-22 stsp *packfile = fdopen(packfd, "r");
489 05118f5a 2021-06-22 stsp if (*packfile == NULL) {
490 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fdopen", packfile_path);
491 05118f5a 2021-06-22 stsp goto done;
492 05118f5a 2021-06-22 stsp }
493 05118f5a 2021-06-22 stsp packfd = -1;
494 05118f5a 2021-06-22 stsp done:
495 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
496 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
497 05118f5a 2021-06-22 stsp free(packfile_name);
498 05118f5a 2021-06-22 stsp if (err) {
499 05118f5a 2021-06-22 stsp free(*pack_hash);
500 05118f5a 2021-06-22 stsp *pack_hash = NULL;
501 05118f5a 2021-06-22 stsp }
502 05118f5a 2021-06-22 stsp return err;
503 05118f5a 2021-06-22 stsp }
504 05118f5a 2021-06-22 stsp
505 05118f5a 2021-06-22 stsp const struct got_error *
506 05118f5a 2021-06-22 stsp got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
507 05118f5a 2021-06-22 stsp struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
508 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
509 05118f5a 2021-06-22 stsp {
510 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
511 05118f5a 2021-06-22 stsp char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
512 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
513 05118f5a 2021-06-22 stsp struct got_pack *pack = NULL;
514 05118f5a 2021-06-22 stsp uint32_t nobj, i;
515 05118f5a 2021-06-22 stsp
516 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
517 05118f5a 2021-06-22 stsp if (err)
518 05118f5a 2021-06-22 stsp goto done;
519 05118f5a 2021-06-22 stsp
520 05118f5a 2021-06-22 stsp if (asprintf(&packpath, "%s/pack-%s.pack",
521 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
522 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
523 05118f5a 2021-06-22 stsp goto done;
524 05118f5a 2021-06-22 stsp }
525 05118f5a 2021-06-22 stsp if (asprintf(&idxpath, "%s/pack-%s.idx",
526 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
527 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
528 05118f5a 2021-06-22 stsp goto done;
529 05118f5a 2021-06-22 stsp }
530 05118f5a 2021-06-22 stsp
531 05118f5a 2021-06-22 stsp err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
532 05118f5a 2021-06-22 stsp if (err)
533 05118f5a 2021-06-22 stsp goto done;
534 05118f5a 2021-06-22 stsp
535 05118f5a 2021-06-22 stsp err = got_repo_cache_pack(&pack, repo, packpath, packidx);
536 05118f5a 2021-06-22 stsp if (err)
537 05118f5a 2021-06-22 stsp goto done;
538 05118f5a 2021-06-22 stsp
539 05118f5a 2021-06-22 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
540 05118f5a 2021-06-22 stsp for (i = 0; i < nobj; i++) {
541 05118f5a 2021-06-22 stsp struct got_packidx_object_id *oid;
542 05118f5a 2021-06-22 stsp struct got_object_id id, base_id;
543 05118f5a 2021-06-22 stsp off_t offset, base_offset = 0;
544 05118f5a 2021-06-22 stsp uint8_t type;
545 05118f5a 2021-06-22 stsp uint64_t size;
546 05118f5a 2021-06-22 stsp size_t tslen, len;
547 05118f5a 2021-06-22 stsp
548 05118f5a 2021-06-22 stsp if (cancel_cb) {
549 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
550 05118f5a 2021-06-22 stsp if (err)
551 05118f5a 2021-06-22 stsp break;
552 05118f5a 2021-06-22 stsp }
553 05118f5a 2021-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
554 05118f5a 2021-06-22 stsp memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
555 05118f5a 2021-06-22 stsp
556 05118f5a 2021-06-22 stsp offset = got_packidx_get_object_offset(packidx, i);
557 05118f5a 2021-06-22 stsp if (offset == -1) {
558 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
559 05118f5a 2021-06-22 stsp goto done;
560 05118f5a 2021-06-22 stsp }
561 05118f5a 2021-06-22 stsp
562 05118f5a 2021-06-22 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
563 05118f5a 2021-06-22 stsp pack, offset);
564 05118f5a 2021-06-22 stsp if (err)
565 05118f5a 2021-06-22 stsp goto done;
566 05118f5a 2021-06-22 stsp
567 05118f5a 2021-06-22 stsp switch (type) {
568 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
569 05118f5a 2021-06-22 stsp err = got_pack_parse_offset_delta(&base_offset, &len,
570 05118f5a 2021-06-22 stsp pack, offset, tslen);
571 05118f5a 2021-06-22 stsp if (err)
572 05118f5a 2021-06-22 stsp goto done;
573 05118f5a 2021-06-22 stsp break;
574 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
575 05118f5a 2021-06-22 stsp err = got_pack_parse_ref_delta(&base_id,
576 05118f5a 2021-06-22 stsp pack, offset, tslen);
577 05118f5a 2021-06-22 stsp if (err)
578 05118f5a 2021-06-22 stsp goto done;
579 05118f5a 2021-06-22 stsp break;
580 05118f5a 2021-06-22 stsp }
581 05118f5a 2021-06-22 stsp err = (*list_cb)(list_arg, &id, type, offset, size,
582 05118f5a 2021-06-22 stsp base_offset, &base_id);
583 05118f5a 2021-06-22 stsp if (err)
584 05118f5a 2021-06-22 stsp goto done;
585 05118f5a 2021-06-22 stsp }
586 05118f5a 2021-06-22 stsp
587 05118f5a 2021-06-22 stsp done:
588 05118f5a 2021-06-22 stsp free(id_str);
589 05118f5a 2021-06-22 stsp free(idxpath);
590 05118f5a 2021-06-22 stsp free(packpath);
591 05118f5a 2021-06-22 stsp if (packidx)
592 05118f5a 2021-06-22 stsp got_packidx_close(packidx);
593 05118f5a 2021-06-22 stsp return err;
594 05118f5a 2021-06-22 stsp }