2 05118f5a 2021-06-22 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 05118f5a 2021-06-22 stsp #include <sys/types.h>
18 8b925c6c 2022-07-16 thomas #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>
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 <stdio.h>
29 05118f5a 2021-06-22 stsp #include <stdlib.h>
30 05118f5a 2021-06-22 stsp #include <string.h>
31 05118f5a 2021-06-22 stsp #include <limits.h>
32 05118f5a 2021-06-22 stsp #include <unistd.h>
34 05118f5a 2021-06-22 stsp #include "got_error.h"
35 05118f5a 2021-06-22 stsp #include "got_cancel.h"
36 05118f5a 2021-06-22 stsp #include "got_object.h"
37 05118f5a 2021-06-22 stsp #include "got_reference.h"
38 05118f5a 2021-06-22 stsp #include "got_repository.h"
39 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
40 05118f5a 2021-06-22 stsp #include "got_opentemp.h"
41 05118f5a 2021-06-22 stsp #include "got_path.h"
43 05118f5a 2021-06-22 stsp #include "got_lib_delta.h"
44 05118f5a 2021-06-22 stsp #include "got_lib_object.h"
45 b3d68e7f 2021-07-03 stsp #include "got_lib_object_idset.h"
46 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_pack.h"
48 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
49 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_pack_create.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_sha1.h"
52 b3d68e7f 2021-07-03 stsp #include "got_lib_lockfile.h"
53 31ba2236 2022-01-05 thomas #include "got_lib_ratelimit.h"
55 05118f5a 2021-06-22 stsp #ifndef nitems
56 05118f5a 2021-06-22 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 05118f5a 2021-06-22 stsp static const struct got_error *
60 05118f5a 2021-06-22 stsp get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
61 05118f5a 2021-06-22 stsp unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
62 05118f5a 2021-06-22 stsp struct got_repository *repo,
63 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
65 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
66 05118f5a 2021-06-22 stsp const size_t alloc_chunksz = 256;
67 05118f5a 2021-06-22 stsp size_t nalloc;
68 05118f5a 2021-06-22 stsp struct got_reflist_entry *re;
71 05118f5a 2021-06-22 stsp *ids = NULL;
72 05118f5a 2021-06-22 stsp *nobjects = 0;
74 bd00a07c 2022-04-16 thomas err = got_reflist_sort(refs,
75 bd00a07c 2022-04-16 thomas got_ref_cmp_by_commit_timestamp_descending, repo);
77 bd00a07c 2022-04-16 thomas return err;
79 05118f5a 2021-06-22 stsp *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
80 05118f5a 2021-06-22 stsp if (*ids == NULL)
81 05118f5a 2021-06-22 stsp return got_error_from_errno("reallocarray");
82 05118f5a 2021-06-22 stsp nalloc = alloc_chunksz;
84 05118f5a 2021-06-22 stsp TAILQ_FOREACH(re, refs, entry) {
85 05118f5a 2021-06-22 stsp struct got_object_id *id;
87 05118f5a 2021-06-22 stsp if (cancel_cb) {
88 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
93 05118f5a 2021-06-22 stsp err = got_ref_resolve(&id, repo, re->ref);
97 05118f5a 2021-06-22 stsp if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
98 05118f5a 2021-06-22 stsp int obj_type;
99 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
102 05118f5a 2021-06-22 stsp if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
109 ae23ce34 2021-07-01 stsp if (nalloc <= *nobjects) {
110 05118f5a 2021-06-22 stsp struct got_object_id **new;
111 05118f5a 2021-06-22 stsp new = recallocarray(*ids, nalloc,
112 05118f5a 2021-06-22 stsp nalloc + alloc_chunksz,
113 05118f5a 2021-06-22 stsp sizeof(struct got_object_id *));
114 05118f5a 2021-06-22 stsp if (new == NULL) {
115 05118f5a 2021-06-22 stsp err = got_error_from_errno(
116 05118f5a 2021-06-22 stsp "recallocarray");
119 05118f5a 2021-06-22 stsp *ids = new;
120 05118f5a 2021-06-22 stsp nalloc += alloc_chunksz;
122 05118f5a 2021-06-22 stsp (*ids)[*nobjects] = id;
123 05118f5a 2021-06-22 stsp if ((*ids)[*nobjects] == NULL) {
124 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
127 05118f5a 2021-06-22 stsp (*nobjects)++;
131 05118f5a 2021-06-22 stsp for (i = 0; i < *nobjects; i++)
132 05118f5a 2021-06-22 stsp free((*ids)[i]);
133 05118f5a 2021-06-22 stsp free(*ids);
134 05118f5a 2021-06-22 stsp *ids = NULL;
135 05118f5a 2021-06-22 stsp *nobjects = 0;
137 05118f5a 2021-06-22 stsp return err;
140 05118f5a 2021-06-22 stsp const struct got_error *
141 05118f5a 2021-06-22 stsp got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
142 05118f5a 2021-06-22 stsp struct got_reflist_head *include_refs,
143 05118f5a 2021-06-22 stsp struct got_reflist_head *exclude_refs, struct got_repository *repo,
144 f9c2e8e5 2022-02-13 thomas int loose_obj_only,
145 f9c2e8e5 2022-02-13 thomas got_pack_progress_cb progress_cb, void *progress_arg,
146 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
148 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
149 05118f5a 2021-06-22 stsp struct got_object_id **ours = NULL, **theirs = NULL;
150 05118f5a 2021-06-22 stsp int nours = 0, ntheirs = 0, packfd = -1, i;
151 05118f5a 2021-06-22 stsp char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
152 05118f5a 2021-06-22 stsp char *sha1_str = NULL;
154 05118f5a 2021-06-22 stsp *packfile = NULL;
155 05118f5a 2021-06-22 stsp *pack_hash = NULL;
157 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/packing.pack",
158 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
159 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
162 05118f5a 2021-06-22 stsp err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
166 05118f5a 2021-06-22 stsp if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
167 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpfile_path);
171 05118f5a 2021-06-22 stsp *packfile = fdopen(packfd, "w");
172 05118f5a 2021-06-22 stsp if (*packfile == NULL) {
173 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fdopen", tmpfile_path);
176 05118f5a 2021-06-22 stsp packfd = -1;
178 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&ours, &nours,
179 abc59930 2021-09-05 naddy (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
180 abc59930 2021-09-05 naddy include_refs, repo, cancel_cb, cancel_arg);
184 05118f5a 2021-06-22 stsp if (nours == 0) {
185 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
189 05118f5a 2021-06-22 stsp if (!TAILQ_EMPTY(exclude_refs)) {
190 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&theirs, &ntheirs,
191 abc59930 2021-09-05 naddy (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
192 abc59930 2021-09-05 naddy exclude_refs, repo,
193 abc59930 2021-09-05 naddy cancel_cb, cancel_arg);
198 05118f5a 2021-06-22 stsp *pack_hash = calloc(1, sizeof(**pack_hash));
199 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
200 05118f5a 2021-06-22 stsp err = got_error_from_errno("calloc");
204 05118f5a 2021-06-22 stsp err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
205 f8a36e22 2021-08-26 stsp ours, nours, repo, loose_obj_only, 0, progress_cb, progress_arg,
206 05118f5a 2021-06-22 stsp cancel_cb, cancel_arg);
210 05118f5a 2021-06-22 stsp err = got_object_id_str(&sha1_str, *pack_hash);
213 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
214 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
215 05118f5a 2021-06-22 stsp sha1_str) == -1) {
216 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
220 05118f5a 2021-06-22 stsp if (fflush(*packfile) == -1) {
221 05118f5a 2021-06-22 stsp err = got_error_from_errno("fflush");
224 05118f5a 2021-06-22 stsp if (fseek(*packfile, 0L, SEEK_SET) == -1) {
225 05118f5a 2021-06-22 stsp err = got_error_from_errno("fseek");
228 05118f5a 2021-06-22 stsp if (rename(tmpfile_path, packfile_path) == -1) {
229 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpfile_path,
230 05118f5a 2021-06-22 stsp packfile_path);
233 05118f5a 2021-06-22 stsp free(tmpfile_path);
234 05118f5a 2021-06-22 stsp tmpfile_path = NULL;
236 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++)
237 05118f5a 2021-06-22 stsp free(ours[i]);
238 05118f5a 2021-06-22 stsp free(ours);
239 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++)
240 05118f5a 2021-06-22 stsp free(theirs[i]);
241 05118f5a 2021-06-22 stsp free(theirs);
242 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
243 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
244 05118f5a 2021-06-22 stsp if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
245 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpfile_path);
246 05118f5a 2021-06-22 stsp free(tmpfile_path);
247 05118f5a 2021-06-22 stsp free(packfile_path);
248 05118f5a 2021-06-22 stsp free(sha1_str);
249 05118f5a 2021-06-22 stsp free(path);
251 05118f5a 2021-06-22 stsp free(*pack_hash);
252 05118f5a 2021-06-22 stsp *pack_hash = NULL;
253 05118f5a 2021-06-22 stsp if (*packfile)
254 05118f5a 2021-06-22 stsp fclose(*packfile);
255 05118f5a 2021-06-22 stsp *packfile = NULL;
257 05118f5a 2021-06-22 stsp return err;
260 05118f5a 2021-06-22 stsp const struct got_error *
261 05118f5a 2021-06-22 stsp got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
262 05118f5a 2021-06-22 stsp struct got_repository *repo,
263 05118f5a 2021-06-22 stsp got_pack_index_progress_cb progress_cb, void *progress_arg,
264 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
267 05118f5a 2021-06-22 stsp char *path;
268 05118f5a 2021-06-22 stsp int imsg_idxfds[2];
269 05118f5a 2021-06-22 stsp int npackfd = -1, idxfd = -1, nidxfd = -1;
270 05118f5a 2021-06-22 stsp int tmpfds[3];
271 05118f5a 2021-06-22 stsp int idxstatus, done = 0;
272 05118f5a 2021-06-22 stsp const struct got_error *err;
273 05118f5a 2021-06-22 stsp struct imsgbuf idxibuf;
274 05118f5a 2021-06-22 stsp pid_t idxpid;
275 05118f5a 2021-06-22 stsp char *tmpidxpath = NULL;
276 05118f5a 2021-06-22 stsp char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
277 05118f5a 2021-06-22 stsp const char *repo_path = got_repo_get_path_git_dir(repo);
278 05118f5a 2021-06-22 stsp struct stat sb;
280 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++)
281 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
283 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/indexing.idx",
284 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
285 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
288 05118f5a 2021-06-22 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
289 05118f5a 2021-06-22 stsp free(path);
292 05118f5a 2021-06-22 stsp if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
293 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpidxpath);
297 05118f5a 2021-06-22 stsp nidxfd = dup(idxfd);
298 05118f5a 2021-06-22 stsp if (nidxfd == -1) {
299 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
303 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
304 05118f5a 2021-06-22 stsp tmpfds[i] = got_opentempfd();
305 05118f5a 2021-06-22 stsp if (tmpfds[i] == -1) {
306 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_opentempfd");
311 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
315 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
316 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
317 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
321 05118f5a 2021-06-22 stsp if (fstat(fileno(packfile), &sb) == -1) {
322 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fstat", packfile_path);
326 05118f5a 2021-06-22 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
327 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
328 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
332 05118f5a 2021-06-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
333 05118f5a 2021-06-22 stsp err = got_error_from_errno("socketpair");
336 05118f5a 2021-06-22 stsp idxpid = fork();
337 05118f5a 2021-06-22 stsp if (idxpid == -1) {
338 05118f5a 2021-06-22 stsp err= got_error_from_errno("fork");
340 05118f5a 2021-06-22 stsp } else if (idxpid == 0)
341 05118f5a 2021-06-22 stsp got_privsep_exec_child(imsg_idxfds,
342 05118f5a 2021-06-22 stsp GOT_PATH_PROG_INDEX_PACK, packfile_path);
343 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[1]) == -1) {
344 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
347 05118f5a 2021-06-22 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
349 05118f5a 2021-06-22 stsp npackfd = dup(fileno(packfile));
350 05118f5a 2021-06-22 stsp if (npackfd == -1) {
351 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
354 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
356 05118f5a 2021-06-22 stsp if (err != NULL)
358 05118f5a 2021-06-22 stsp npackfd = -1;
359 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
360 05118f5a 2021-06-22 stsp if (err != NULL)
362 05118f5a 2021-06-22 stsp nidxfd = -1;
363 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
364 05118f5a 2021-06-22 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
365 05118f5a 2021-06-22 stsp if (err != NULL)
367 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
370 05118f5a 2021-06-22 stsp while (!done) {
371 05118f5a 2021-06-22 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
373 05118f5a 2021-06-22 stsp if (cancel_cb) {
374 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
379 05118f5a 2021-06-22 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
380 05118f5a 2021-06-22 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
382 05118f5a 2021-06-22 stsp if (err != NULL)
384 05118f5a 2021-06-22 stsp if (nobj_indexed != 0) {
385 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, sb.st_size,
386 05118f5a 2021-06-22 stsp nobj_total, nobj_indexed, nobj_loose,
387 05118f5a 2021-06-22 stsp nobj_resolved);
392 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[0]) == -1) {
393 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
396 05118f5a 2021-06-22 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
397 05118f5a 2021-06-22 stsp err = got_error_from_errno("waitpid");
401 05118f5a 2021-06-22 stsp if (rename(tmpidxpath, idxpath) == -1) {
402 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
405 05118f5a 2021-06-22 stsp free(tmpidxpath);
406 05118f5a 2021-06-22 stsp tmpidxpath = NULL;
409 05118f5a 2021-06-22 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
410 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpidxpath);
411 05118f5a 2021-06-22 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
412 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
413 05118f5a 2021-06-22 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
414 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
415 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
416 05118f5a 2021-06-22 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
417 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
419 05118f5a 2021-06-22 stsp free(tmpidxpath);
420 05118f5a 2021-06-22 stsp free(idxpath);
421 05118f5a 2021-06-22 stsp free(packfile_path);
422 05118f5a 2021-06-22 stsp return err;
425 05118f5a 2021-06-22 stsp const struct got_error *
426 05118f5a 2021-06-22 stsp got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
427 05118f5a 2021-06-22 stsp struct got_repository *repo, const char *packfile_path)
429 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
430 05118f5a 2021-06-22 stsp const char *packdir_path = NULL;
431 05118f5a 2021-06-22 stsp char *packfile_name = NULL, *p, *dot;
432 05118f5a 2021-06-22 stsp struct got_object_id id;
433 05118f5a 2021-06-22 stsp int packfd = -1;
435 05118f5a 2021-06-22 stsp *packfile = NULL;
436 05118f5a 2021-06-22 stsp *pack_hash = NULL;
438 05118f5a 2021-06-22 stsp packdir_path = got_repo_get_path_objects_pack(repo);
439 05118f5a 2021-06-22 stsp if (packdir_path == NULL)
440 05118f5a 2021-06-22 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
442 05118f5a 2021-06-22 stsp if (!got_path_is_child(packfile_path, packdir_path,
443 05118f5a 2021-06-22 stsp strlen(packdir_path))) {
444 05118f5a 2021-06-22 stsp err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
449 05118f5a 2021-06-22 stsp err = got_path_basename(&packfile_name, packfile_path);
452 05118f5a 2021-06-22 stsp p = packfile_name;
454 05118f5a 2021-06-22 stsp if (strncmp(p, "pack-", 5) != 0) {
455 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
456 abc59930 2021-09-05 naddy "'%s' is not a valid pack file name",
457 abc59930 2021-09-05 naddy packfile_name);
461 05118f5a 2021-06-22 stsp dot = strchr(p, '.');
462 05118f5a 2021-06-22 stsp if (dot == NULL) {
463 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
464 abc59930 2021-09-05 naddy "'%s' is not a valid pack file name",
465 abc59930 2021-09-05 naddy packfile_name);
468 05118f5a 2021-06-22 stsp if (strcmp(dot + 1, "pack") != 0) {
469 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
470 abc59930 2021-09-05 naddy "'%s' is not a valid pack file name",
471 abc59930 2021-09-05 naddy packfile_name);
474 05118f5a 2021-06-22 stsp *dot = '\0';
475 05118f5a 2021-06-22 stsp if (!got_parse_sha1_digest(id.sha1, p)) {
476 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
477 abc59930 2021-09-05 naddy "'%s' is not a valid pack file name",
478 abc59930 2021-09-05 naddy packfile_name);
482 05118f5a 2021-06-22 stsp *pack_hash = got_object_id_dup(&id);
483 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
484 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
488 06340621 2021-12-31 thomas packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
489 05118f5a 2021-06-22 stsp if (packfd == -1) {
490 05118f5a 2021-06-22 stsp err = got_error_from_errno2("open", packfile_path);
494 05118f5a 2021-06-22 stsp *packfile = fdopen(packfd, "r");
495 05118f5a 2021-06-22 stsp if (*packfile == NULL) {
496 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fdopen", packfile_path);
499 05118f5a 2021-06-22 stsp packfd = -1;
501 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
502 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
503 05118f5a 2021-06-22 stsp free(packfile_name);
505 05118f5a 2021-06-22 stsp free(*pack_hash);
506 05118f5a 2021-06-22 stsp *pack_hash = NULL;
508 05118f5a 2021-06-22 stsp return err;
511 05118f5a 2021-06-22 stsp const struct got_error *
512 05118f5a 2021-06-22 stsp got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
513 05118f5a 2021-06-22 stsp struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
514 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
516 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
517 05118f5a 2021-06-22 stsp char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
518 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
519 05118f5a 2021-06-22 stsp struct got_pack *pack = NULL;
520 05118f5a 2021-06-22 stsp uint32_t nobj, i;
522 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
526 05118f5a 2021-06-22 stsp if (asprintf(&packpath, "%s/pack-%s.pack",
527 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
528 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
531 05118f5a 2021-06-22 stsp if (asprintf(&idxpath, "%s/pack-%s.idx",
532 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
533 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
537 05118f5a 2021-06-22 stsp err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
541 05118f5a 2021-06-22 stsp err = got_repo_cache_pack(&pack, repo, packpath, packidx);
545 05118f5a 2021-06-22 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
546 05118f5a 2021-06-22 stsp for (i = 0; i < nobj; i++) {
547 05118f5a 2021-06-22 stsp struct got_packidx_object_id *oid;
548 05118f5a 2021-06-22 stsp struct got_object_id id, base_id;
549 05118f5a 2021-06-22 stsp off_t offset, base_offset = 0;
550 05118f5a 2021-06-22 stsp uint8_t type;
551 05118f5a 2021-06-22 stsp uint64_t size;
552 05118f5a 2021-06-22 stsp size_t tslen, len;
554 05118f5a 2021-06-22 stsp if (cancel_cb) {
555 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
559 05118f5a 2021-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
560 05118f5a 2021-06-22 stsp memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
562 05118f5a 2021-06-22 stsp offset = got_packidx_get_object_offset(packidx, i);
563 05118f5a 2021-06-22 stsp if (offset == -1) {
564 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
568 05118f5a 2021-06-22 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
569 05118f5a 2021-06-22 stsp pack, offset);
573 05118f5a 2021-06-22 stsp switch (type) {
574 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
575 05118f5a 2021-06-22 stsp err = got_pack_parse_offset_delta(&base_offset, &len,
576 05118f5a 2021-06-22 stsp pack, offset, tslen);
580 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
581 05118f5a 2021-06-22 stsp err = got_pack_parse_ref_delta(&base_id,
582 05118f5a 2021-06-22 stsp pack, offset, tslen);
587 05118f5a 2021-06-22 stsp err = (*list_cb)(list_arg, &id, type, offset, size,
588 05118f5a 2021-06-22 stsp base_offset, &base_id);
594 05118f5a 2021-06-22 stsp free(id_str);
595 05118f5a 2021-06-22 stsp free(idxpath);
596 05118f5a 2021-06-22 stsp free(packpath);
597 05118f5a 2021-06-22 stsp if (packidx)
598 05118f5a 2021-06-22 stsp got_packidx_close(packidx);
599 b3d68e7f 2021-07-03 stsp return err;
602 b3d68e7f 2021-07-03 stsp static const struct got_error *
603 31ba2236 2022-01-05 thomas report_cleanup_progress(got_cleanup_progress_cb progress_cb,
604 31ba2236 2022-01-05 thomas void *progress_arg, struct got_ratelimit *rl,
605 31ba2236 2022-01-05 thomas int nloose, int ncommits, int npurged)
607 31ba2236 2022-01-05 thomas const struct got_error *err;
608 31ba2236 2022-01-05 thomas int elapsed;
610 31ba2236 2022-01-05 thomas if (progress_cb == NULL)
611 31ba2236 2022-01-05 thomas return NULL;
613 31ba2236 2022-01-05 thomas err = got_ratelimit_check(&elapsed, rl);
614 31ba2236 2022-01-05 thomas if (err || !elapsed)
615 31ba2236 2022-01-05 thomas return err;
617 31ba2236 2022-01-05 thomas return progress_cb(progress_arg, nloose, ncommits, npurged);
620 31ba2236 2022-01-05 thomas static const struct got_error *
621 b3d68e7f 2021-07-03 stsp get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
622 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb, void *progress_arg,
623 31ba2236 2022-01-05 thomas struct got_ratelimit *rl, struct got_repository *repo)
625 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
626 b3d68e7f 2021-07-03 stsp char *path_objects = NULL, *path = NULL;
627 b3d68e7f 2021-07-03 stsp DIR *dir = NULL;
628 b3d68e7f 2021-07-03 stsp struct got_object *obj = NULL;
629 b3d68e7f 2021-07-03 stsp struct got_object_id id;
630 b3d68e7f 2021-07-03 stsp int i, fd = -1;
631 b3d68e7f 2021-07-03 stsp struct stat sb;
633 b3d68e7f 2021-07-03 stsp *ondisk_size = 0;
634 b3d68e7f 2021-07-03 stsp *loose_ids = got_object_idset_alloc();
635 b3d68e7f 2021-07-03 stsp if (*loose_ids == NULL)
636 b3d68e7f 2021-07-03 stsp return got_error_from_errno("got_object_idset_alloc");
638 b3d68e7f 2021-07-03 stsp path_objects = got_repo_get_path_objects(repo);
639 b3d68e7f 2021-07-03 stsp if (path_objects == NULL) {
640 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("got_repo_get_path_objects");
644 b3d68e7f 2021-07-03 stsp for (i = 0; i <= 0xff; i++) {
645 b3d68e7f 2021-07-03 stsp struct dirent *dent;
647 b3d68e7f 2021-07-03 stsp if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
648 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
652 b3d68e7f 2021-07-03 stsp dir = opendir(path);
653 b3d68e7f 2021-07-03 stsp if (dir == NULL) {
654 b3d68e7f 2021-07-03 stsp if (errno == ENOENT) {
655 b3d68e7f 2021-07-03 stsp err = NULL;
658 b3d68e7f 2021-07-03 stsp err = got_error_from_errno2("opendir", path);
662 b3d68e7f 2021-07-03 stsp while ((dent = readdir(dir)) != NULL) {
663 b3d68e7f 2021-07-03 stsp char *id_str;
665 b3d68e7f 2021-07-03 stsp if (strcmp(dent->d_name, ".") == 0 ||
666 b3d68e7f 2021-07-03 stsp strcmp(dent->d_name, "..") == 0)
669 b3d68e7f 2021-07-03 stsp if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
670 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
674 b3d68e7f 2021-07-03 stsp memset(&id, 0, sizeof(id));
675 b3d68e7f 2021-07-03 stsp if (!got_parse_sha1_digest(id.sha1, id_str)) {
676 b3d68e7f 2021-07-03 stsp free(id_str);
679 b3d68e7f 2021-07-03 stsp free(id_str);
681 b3d68e7f 2021-07-03 stsp err = got_object_open_loose_fd(&fd, &id, repo);
684 b3d68e7f 2021-07-03 stsp if (fstat(fd, &sb) == -1) {
685 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("fstat");
688 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, &id, repo,
692 b3d68e7f 2021-07-03 stsp fd = -1; /* already closed */
694 b3d68e7f 2021-07-03 stsp switch (obj->type) {
695 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
696 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TREE:
697 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_BLOB:
698 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TAG:
701 b3d68e7f 2021-07-03 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
702 b3d68e7f 2021-07-03 stsp "%d", obj->type);
705 b3d68e7f 2021-07-03 stsp got_object_close(obj);
706 b3d68e7f 2021-07-03 stsp obj = NULL;
707 b3d68e7f 2021-07-03 stsp (*ondisk_size) += sb.st_size;
708 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(*loose_ids, &id, NULL);
711 31ba2236 2022-01-05 thomas err = report_cleanup_progress(progress_cb,
712 31ba2236 2022-01-05 thomas progress_arg, rl,
713 31ba2236 2022-01-05 thomas got_object_idset_num_elements(*loose_ids), -1, -1);
715 31ba2236 2022-01-05 thomas goto done;
718 b3d68e7f 2021-07-03 stsp if (closedir(dir) != 0) {
719 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("closedir");
722 b3d68e7f 2021-07-03 stsp dir = NULL;
724 b3d68e7f 2021-07-03 stsp free(path);
725 b3d68e7f 2021-07-03 stsp path = NULL;
728 b3d68e7f 2021-07-03 stsp if (dir && closedir(dir) != 0 && err == NULL)
729 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("closedir");
730 b3d68e7f 2021-07-03 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
731 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("close");
733 b3d68e7f 2021-07-03 stsp got_object_idset_free(*loose_ids);
734 b3d68e7f 2021-07-03 stsp *loose_ids = NULL;
737 b3d68e7f 2021-07-03 stsp got_object_close(obj);
738 b3d68e7f 2021-07-03 stsp free(path_objects);
739 b3d68e7f 2021-07-03 stsp free(path);
740 b3d68e7f 2021-07-03 stsp return err;
743 b3d68e7f 2021-07-03 stsp static const struct got_error *
744 b3d68e7f 2021-07-03 stsp preserve_loose_object(struct got_object_idset *loose_ids,
745 b3d68e7f 2021-07-03 stsp struct got_object_id *id, struct got_repository *repo, int *npacked)
747 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
748 9614da0d 2021-07-04 stsp struct got_object *obj;
750 b3d68e7f 2021-07-03 stsp if (!got_object_idset_contains(loose_ids, id))
751 b3d68e7f 2021-07-03 stsp return NULL;
754 9614da0d 2021-07-04 stsp * Try to open this object from a pack file. This ensures that
755 9614da0d 2021-07-04 stsp * we do in fact have a valid packed copy of the object. Otherwise
756 9614da0d 2021-07-04 stsp * we should not delete the loose representation of this object.
758 abc59930 2021-09-05 naddy err = got_object_open_packed(&obj, id, repo);
759 abc59930 2021-09-05 naddy if (err == NULL) {
760 9614da0d 2021-07-04 stsp got_object_close(obj);
762 9614da0d 2021-07-04 stsp * The object is referenced and packed.
763 9614da0d 2021-07-04 stsp * We can purge the redundantly stored loose object.
765 9614da0d 2021-07-04 stsp (*npacked)++;
766 9614da0d 2021-07-04 stsp return NULL;
767 9614da0d 2021-07-04 stsp } else if (err->code != GOT_ERR_NO_OBJ)
768 9614da0d 2021-07-04 stsp return err;
771 b3d68e7f 2021-07-03 stsp * This object is referenced and not packed.
772 b3d68e7f 2021-07-03 stsp * Remove it from our purge set.
774 b3d68e7f 2021-07-03 stsp return got_object_idset_remove(NULL, loose_ids, id);
777 b3d68e7f 2021-07-03 stsp static const struct got_error *
778 b3d68e7f 2021-07-03 stsp load_tree_entries(struct got_object_id_queue *ids,
779 b3d68e7f 2021-07-03 stsp struct got_object_idset *loose_ids,
780 b3d68e7f 2021-07-03 stsp struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
781 b3d68e7f 2021-07-03 stsp const char *dpath, struct got_repository *repo, int *npacked,
782 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
784 b3d68e7f 2021-07-03 stsp const struct got_error *err;
785 b3d68e7f 2021-07-03 stsp struct got_tree_object *tree;
786 b3d68e7f 2021-07-03 stsp char *p = NULL;
789 b3d68e7f 2021-07-03 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
791 b3d68e7f 2021-07-03 stsp return err;
793 b3d68e7f 2021-07-03 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
794 b3d68e7f 2021-07-03 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
795 b3d68e7f 2021-07-03 stsp struct got_object_id *id = got_tree_entry_get_id(e);
796 b3d68e7f 2021-07-03 stsp mode_t mode = got_tree_entry_get_mode(e);
798 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
799 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
804 b3d68e7f 2021-07-03 stsp if (got_object_tree_entry_is_symlink(e) ||
805 b3d68e7f 2021-07-03 stsp got_object_tree_entry_is_submodule(e) ||
806 b3d68e7f 2021-07-03 stsp got_object_idset_contains(traversed_ids, id))
809 b3d68e7f 2021-07-03 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
810 b3d68e7f 2021-07-03 stsp got_tree_entry_get_name(e)) == -1) {
811 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
815 b3d68e7f 2021-07-03 stsp if (S_ISDIR(mode)) {
816 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
817 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, id);
820 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
821 b3d68e7f 2021-07-03 stsp } else if (S_ISREG(mode)) {
822 b3d68e7f 2021-07-03 stsp /* This blob is referenced. */
823 b3d68e7f 2021-07-03 stsp err = preserve_loose_object(loose_ids, id, repo,
827 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(traversed_ids, id, NULL);
836 b3d68e7f 2021-07-03 stsp got_object_tree_close(tree);
838 b3d68e7f 2021-07-03 stsp return err;
841 b3d68e7f 2021-07-03 stsp static const struct got_error *
842 b3d68e7f 2021-07-03 stsp load_tree(struct got_object_idset *loose_ids,
843 b3d68e7f 2021-07-03 stsp struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
844 b3d68e7f 2021-07-03 stsp const char *dpath, struct got_repository *repo, int *npacked,
845 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
847 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
848 b3d68e7f 2021-07-03 stsp struct got_object_id_queue tree_ids;
849 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
851 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, tree_id);
853 b3d68e7f 2021-07-03 stsp return err;
855 b3d68e7f 2021-07-03 stsp STAILQ_INIT(&tree_ids);
856 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
858 b3d68e7f 2021-07-03 stsp while (!STAILQ_EMPTY(&tree_ids)) {
859 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
860 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
865 b3d68e7f 2021-07-03 stsp qid = STAILQ_FIRST(&tree_ids);
866 b3d68e7f 2021-07-03 stsp STAILQ_REMOVE_HEAD(&tree_ids, entry);
868 ec242592 2022-04-22 thomas if (got_object_idset_contains(traversed_ids, &qid->id)) {
869 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
873 ec242592 2022-04-22 thomas err = got_object_idset_add(traversed_ids, &qid->id, NULL);
875 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
879 b3d68e7f 2021-07-03 stsp /* This tree is referenced. */
880 ec242592 2022-04-22 thomas err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
884 b3d68e7f 2021-07-03 stsp err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
885 ec242592 2022-04-22 thomas &qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
886 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
891 b3d68e7f 2021-07-03 stsp got_object_id_queue_free(&tree_ids);
892 b3d68e7f 2021-07-03 stsp return err;
895 b3d68e7f 2021-07-03 stsp static const struct got_error *
896 b3d68e7f 2021-07-03 stsp load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
897 b3d68e7f 2021-07-03 stsp int *npacked, struct got_object_idset *traversed_ids,
898 b3d68e7f 2021-07-03 stsp struct got_object_id *id, struct got_repository *repo,
899 31ba2236 2022-01-05 thomas got_cleanup_progress_cb progress_cb, void *progress_arg,
900 31ba2236 2022-01-05 thomas struct got_ratelimit *rl, int nloose,
901 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
903 b3d68e7f 2021-07-03 stsp const struct got_error *err;
904 b3d68e7f 2021-07-03 stsp struct got_commit_object *commit = NULL;
905 b3d68e7f 2021-07-03 stsp struct got_tag_object *tag = NULL;
906 b3d68e7f 2021-07-03 stsp struct got_object_id *tree_id = NULL;
907 b3d68e7f 2021-07-03 stsp struct got_object_id_queue ids;
908 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
909 b3d68e7f 2021-07-03 stsp int obj_type;
911 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, id);
913 b3d68e7f 2021-07-03 stsp return err;
915 b3d68e7f 2021-07-03 stsp STAILQ_INIT(&ids);
916 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
918 b3d68e7f 2021-07-03 stsp while (!STAILQ_EMPTY(&ids)) {
919 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
920 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
925 b3d68e7f 2021-07-03 stsp qid = STAILQ_FIRST(&ids);
926 b3d68e7f 2021-07-03 stsp STAILQ_REMOVE_HEAD(&ids, entry);
928 ec242592 2022-04-22 thomas if (got_object_idset_contains(traversed_ids, &qid->id)) {
929 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
930 b3d68e7f 2021-07-03 stsp qid = NULL;
934 ec242592 2022-04-22 thomas err = got_object_idset_add(traversed_ids, &qid->id, NULL);
938 b3d68e7f 2021-07-03 stsp /* This commit or tag is referenced. */
939 ec242592 2022-04-22 thomas err = preserve_loose_object(loose_ids, &qid->id, repo, npacked);
943 ec242592 2022-04-22 thomas err = got_object_get_type(&obj_type, repo, &qid->id);
946 b3d68e7f 2021-07-03 stsp switch (obj_type) {
947 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
948 ec242592 2022-04-22 thomas err = got_object_open_as_commit(&commit, repo,
949 ec242592 2022-04-22 thomas &qid->id);
953 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TAG:
954 ec242592 2022-04-22 thomas err = got_object_open_as_tag(&tag, repo, &qid->id);
959 b3d68e7f 2021-07-03 stsp /* should not happen */
960 b3d68e7f 2021-07-03 stsp err = got_error(GOT_ERR_OBJ_TYPE);
964 b3d68e7f 2021-07-03 stsp /* Find a tree object to scan. */
965 b3d68e7f 2021-07-03 stsp if (commit) {
966 b3d68e7f 2021-07-03 stsp tree_id = got_object_commit_get_tree_id(commit);
967 b3d68e7f 2021-07-03 stsp } else if (tag) {
968 b3d68e7f 2021-07-03 stsp obj_type = got_object_tag_get_object_type(tag);
969 b3d68e7f 2021-07-03 stsp switch (obj_type) {
970 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
971 b3d68e7f 2021-07-03 stsp err = got_object_open_as_commit(&commit, repo,
972 b3d68e7f 2021-07-03 stsp got_object_tag_get_object_id(tag));
975 b3d68e7f 2021-07-03 stsp tree_id = got_object_commit_get_tree_id(commit);
977 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TREE:
978 b3d68e7f 2021-07-03 stsp tree_id = got_object_tag_get_object_id(tag);
982 b3d68e7f 2021-07-03 stsp * Tag points at something other than a
983 b3d68e7f 2021-07-03 stsp * commit or tree. Leave this weird tag object
984 b3d68e7f 2021-07-03 stsp * and the object it points to on disk.
986 b3d68e7f 2021-07-03 stsp err = got_object_idset_remove(NULL, loose_ids,
987 ec242592 2022-04-22 thomas &qid->id);
988 b3d68e7f 2021-07-03 stsp if (err && err->code != GOT_ERR_NO_OBJ)
990 b3d68e7f 2021-07-03 stsp err = got_object_idset_remove(NULL, loose_ids,
991 b3d68e7f 2021-07-03 stsp got_object_tag_get_object_id(tag));
992 b3d68e7f 2021-07-03 stsp if (err && err->code != GOT_ERR_NO_OBJ)
994 b3d68e7f 2021-07-03 stsp err = NULL;
999 b3d68e7f 2021-07-03 stsp if (tree_id) {
1000 b3d68e7f 2021-07-03 stsp err = load_tree(loose_ids, traversed_ids, tree_id, "",
1001 b3d68e7f 2021-07-03 stsp repo, npacked, cancel_cb, cancel_arg);
1006 b3d68e7f 2021-07-03 stsp if (commit || tag)
1007 b3d68e7f 2021-07-03 stsp (*ncommits)++; /* scanned tags are counted as commits */
1009 31ba2236 2022-01-05 thomas err = report_cleanup_progress(progress_cb, progress_arg, rl,
1010 31ba2236 2022-01-05 thomas nloose, *ncommits, -1);
1011 31ba2236 2022-01-05 thomas if (err)
1014 b3d68e7f 2021-07-03 stsp if (commit) {
1015 b3d68e7f 2021-07-03 stsp /* Find parent commits to scan. */
1016 b3d68e7f 2021-07-03 stsp const struct got_object_id_queue *parent_ids;
1017 b3d68e7f 2021-07-03 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1018 b3d68e7f 2021-07-03 stsp err = got_object_id_queue_copy(parent_ids, &ids);
1021 b3d68e7f 2021-07-03 stsp got_object_commit_close(commit);
1022 b3d68e7f 2021-07-03 stsp commit = NULL;
1024 b3d68e7f 2021-07-03 stsp if (tag) {
1025 b3d68e7f 2021-07-03 stsp got_object_tag_close(tag);
1026 b3d68e7f 2021-07-03 stsp tag = NULL;
1028 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
1029 b3d68e7f 2021-07-03 stsp qid = NULL;
1033 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
1034 b3d68e7f 2021-07-03 stsp if (commit)
1035 b3d68e7f 2021-07-03 stsp got_object_commit_close(commit);
1037 b3d68e7f 2021-07-03 stsp got_object_tag_close(tag);
1038 4b2e47fb 2021-07-03 stsp got_object_id_queue_free(&ids);
1039 b3d68e7f 2021-07-03 stsp return err;
1042 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg {
1043 b3d68e7f 2021-07-03 stsp struct got_repository *repo;
1044 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb;
1045 b3d68e7f 2021-07-03 stsp void *progress_arg;
1046 31ba2236 2022-01-05 thomas struct got_ratelimit *rl;
1047 b3d68e7f 2021-07-03 stsp int nloose;
1048 b3d68e7f 2021-07-03 stsp int ncommits;
1049 b3d68e7f 2021-07-03 stsp int npurged;
1050 b3d68e7f 2021-07-03 stsp off_t size_purged;
1051 b3d68e7f 2021-07-03 stsp int dry_run;
1052 ef8ec606 2021-07-27 stsp time_t max_mtime;
1053 ef8ec606 2021-07-27 stsp int ignore_mtime;
1056 b3d68e7f 2021-07-03 stsp static const struct got_error *
1057 b3d68e7f 2021-07-03 stsp purge_loose_object(struct got_object_id *id, void *data, void *arg)
1059 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg *a = arg;
1060 b3d68e7f 2021-07-03 stsp const struct got_error *err, *unlock_err = NULL;
1061 b3d68e7f 2021-07-03 stsp char *path = NULL;
1062 b3d68e7f 2021-07-03 stsp int fd = -1;
1063 b3d68e7f 2021-07-03 stsp struct stat sb;
1064 b3d68e7f 2021-07-03 stsp struct got_lockfile *lf = NULL;
1066 b3d68e7f 2021-07-03 stsp err = got_object_get_path(&path, id, a->repo);
1068 b3d68e7f 2021-07-03 stsp return err;
1070 b3d68e7f 2021-07-03 stsp err = got_object_open_loose_fd(&fd, id, a->repo);
1072 b3d68e7f 2021-07-03 stsp goto done;
1074 b3d68e7f 2021-07-03 stsp if (fstat(fd, &sb) == -1) {
1075 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("fstat");
1076 b3d68e7f 2021-07-03 stsp goto done;
1080 ef8ec606 2021-07-27 stsp * Do not delete objects which are younger than our maximum
1081 ef8ec606 2021-07-27 stsp * modification time threshold. This prevents a race where
1082 ef8ec606 2021-07-27 stsp * new objects which are being added to the repository
1083 ef8ec606 2021-07-27 stsp * concurrently would be deleted.
1085 ef8ec606 2021-07-27 stsp if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1086 ef8ec606 2021-07-27 stsp if (!a->dry_run) {
1087 ef8ec606 2021-07-27 stsp err = got_lockfile_lock(&lf, path, -1);
1089 ef8ec606 2021-07-27 stsp goto done;
1090 ef8ec606 2021-07-27 stsp if (unlink(path) == -1) {
1091 ef8ec606 2021-07-27 stsp err = got_error_from_errno2("unlink", path);
1092 ef8ec606 2021-07-27 stsp goto done;
1096 ef8ec606 2021-07-27 stsp a->npurged++;
1097 ef8ec606 2021-07-27 stsp a->size_purged += sb.st_size;
1098 31ba2236 2022-01-05 thomas err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1099 31ba2236 2022-01-05 thomas a->rl, a->nloose, a->ncommits, a->npurged);
1100 31ba2236 2022-01-05 thomas if (err)
1101 31ba2236 2022-01-05 thomas goto done;
1104 b3d68e7f 2021-07-03 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1105 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("close");
1106 b3d68e7f 2021-07-03 stsp free(path);
1108 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1109 b3d68e7f 2021-07-03 stsp return err ? err : unlock_err;
1112 b3d68e7f 2021-07-03 stsp const struct got_error *
1113 b3d68e7f 2021-07-03 stsp got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1114 b3d68e7f 2021-07-03 stsp off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1115 ef8ec606 2021-07-27 stsp int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
1116 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1118 b3d68e7f 2021-07-03 stsp const struct got_error *err;
1119 b3d68e7f 2021-07-03 stsp struct got_object_idset *loose_ids;
1120 b3d68e7f 2021-07-03 stsp struct got_object_idset *traversed_ids;
1121 b3d68e7f 2021-07-03 stsp struct got_object_id **referenced_ids;
1122 b3d68e7f 2021-07-03 stsp int i, nreferenced, nloose, ncommits = 0;
1123 b3d68e7f 2021-07-03 stsp struct got_reflist_head refs;
1124 ef8ec606 2021-07-27 stsp struct got_reflist_entry *re;
1125 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg arg;
1126 ef8ec606 2021-07-27 stsp time_t max_mtime = 0;
1127 31ba2236 2022-01-05 thomas struct got_ratelimit rl;
1129 b3d68e7f 2021-07-03 stsp TAILQ_INIT(&refs);
1130 31ba2236 2022-01-05 thomas got_ratelimit_init(&rl, 0, 500);
1132 b3d68e7f 2021-07-03 stsp *size_before = 0;
1133 b3d68e7f 2021-07-03 stsp *size_after = 0;
1134 b3d68e7f 2021-07-03 stsp *npacked = 0;
1136 b3d68e7f 2021-07-03 stsp err = get_loose_object_ids(&loose_ids, size_before,
1137 31ba2236 2022-01-05 thomas progress_cb, progress_arg, &rl, repo);
1139 b3d68e7f 2021-07-03 stsp return err;
1140 b3d68e7f 2021-07-03 stsp nloose = got_object_idset_num_elements(loose_ids);
1141 b3d68e7f 2021-07-03 stsp if (nloose == 0) {
1142 b3d68e7f 2021-07-03 stsp got_object_idset_free(loose_ids);
1143 39a089ac 2022-01-05 thomas if (progress_cb) {
1144 39a089ac 2022-01-05 thomas err = progress_cb(progress_arg, 0, 0, 0);
1145 39a089ac 2022-01-05 thomas if (err)
1146 39a089ac 2022-01-05 thomas return err;
1148 b3d68e7f 2021-07-03 stsp return NULL;
1151 b3d68e7f 2021-07-03 stsp traversed_ids = got_object_idset_alloc();
1152 b3d68e7f 2021-07-03 stsp if (traversed_ids == NULL) {
1153 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("got_object_idset_alloc");
1154 b3d68e7f 2021-07-03 stsp goto done;
1157 b3d68e7f 2021-07-03 stsp err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1159 b3d68e7f 2021-07-03 stsp goto done;
1160 ef8ec606 2021-07-27 stsp if (!ignore_mtime) {
1161 ef8ec606 2021-07-27 stsp TAILQ_FOREACH(re, &refs, entry) {
1162 ef8ec606 2021-07-27 stsp time_t mtime = got_ref_get_mtime(re->ref);
1163 ef8ec606 2021-07-27 stsp if (mtime > max_mtime)
1164 ef8ec606 2021-07-27 stsp max_mtime = mtime;
1167 ef8ec606 2021-07-27 stsp * For safety, keep objects created within 10 minutes
1168 ef8ec606 2021-07-27 stsp * before the youngest reference was created.
1170 ef8ec606 2021-07-27 stsp if (max_mtime >= 600)
1171 ef8ec606 2021-07-27 stsp max_mtime -= 600;
1174 b3d68e7f 2021-07-03 stsp err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1175 b3d68e7f 2021-07-03 stsp (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1176 b3d68e7f 2021-07-03 stsp &refs, repo, cancel_cb, cancel_arg);
1178 b3d68e7f 2021-07-03 stsp goto done;
1180 b3d68e7f 2021-07-03 stsp for (i = 0; i < nreferenced; i++) {
1181 b3d68e7f 2021-07-03 stsp struct got_object_id *id = referenced_ids[i];
1182 b3d68e7f 2021-07-03 stsp err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1183 31ba2236 2022-01-05 thomas traversed_ids, id, repo, progress_cb, progress_arg, &rl,
1184 31ba2236 2022-01-05 thomas nloose, cancel_cb, cancel_arg);
1186 b3d68e7f 2021-07-03 stsp goto done;
1189 b3d68e7f 2021-07-03 stsp /* Any remaining loose objects are unreferenced and can be purged. */
1190 b3d68e7f 2021-07-03 stsp arg.repo = repo;
1191 b3d68e7f 2021-07-03 stsp arg.progress_arg = progress_arg;
1192 b3d68e7f 2021-07-03 stsp arg.progress_cb = progress_cb;
1193 31ba2236 2022-01-05 thomas arg.rl = &rl;
1194 b3d68e7f 2021-07-03 stsp arg.nloose = nloose;
1195 b3d68e7f 2021-07-03 stsp arg.npurged = 0;
1196 b3d68e7f 2021-07-03 stsp arg.size_purged = 0;
1197 b3d68e7f 2021-07-03 stsp arg.ncommits = ncommits;
1198 b3d68e7f 2021-07-03 stsp arg.dry_run = dry_run;
1199 ef8ec606 2021-07-27 stsp arg.max_mtime = max_mtime;
1200 ef8ec606 2021-07-27 stsp arg.ignore_mtime = ignore_mtime;
1201 b3d68e7f 2021-07-03 stsp err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1203 b3d68e7f 2021-07-03 stsp goto done;
1204 b3d68e7f 2021-07-03 stsp *size_after = *size_before - arg.size_purged;
1206 31ba2236 2022-01-05 thomas /* Produce a final progress report. */
1207 31ba2236 2022-01-05 thomas if (progress_cb) {
1208 654306ef 2022-01-05 thomas err = progress_cb(progress_arg, nloose, ncommits, arg.npurged);
1209 31ba2236 2022-01-05 thomas if (err)
1210 31ba2236 2022-01-05 thomas goto done;
1213 b3d68e7f 2021-07-03 stsp got_object_idset_free(loose_ids);
1214 b3d68e7f 2021-07-03 stsp got_object_idset_free(traversed_ids);
1215 1124fe40 2021-07-07 stsp return err;
1218 1124fe40 2021-07-07 stsp static const struct got_error *
1219 1124fe40 2021-07-07 stsp remove_packidx(int dir_fd, const char *relpath)
1221 1124fe40 2021-07-07 stsp const struct got_error *err, *unlock_err;
1222 1124fe40 2021-07-07 stsp struct got_lockfile *lf;
1224 1124fe40 2021-07-07 stsp err = got_lockfile_lock(&lf, relpath, dir_fd);
1226 1124fe40 2021-07-07 stsp return err;
1227 1124fe40 2021-07-07 stsp if (unlinkat(dir_fd, relpath, 0) == -1)
1228 1124fe40 2021-07-07 stsp err = got_error_from_errno("unlinkat");
1229 1124fe40 2021-07-07 stsp unlock_err = got_lockfile_unlock(lf, dir_fd);
1230 1124fe40 2021-07-07 stsp return err ? err : unlock_err;
1233 1124fe40 2021-07-07 stsp const struct got_error *
1234 1124fe40 2021-07-07 stsp got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1235 1124fe40 2021-07-07 stsp got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1236 1124fe40 2021-07-07 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1238 dee47c87 2022-07-21 thomas const struct got_error *err = NULL;
1239 1124fe40 2021-07-07 stsp DIR *packdir = NULL;
1240 1124fe40 2021-07-07 stsp struct dirent *dent;
1241 1124fe40 2021-07-07 stsp char *pack_relpath = NULL;
1242 1124fe40 2021-07-07 stsp int packdir_fd;
1243 1124fe40 2021-07-07 stsp struct stat sb;
1245 1124fe40 2021-07-07 stsp packdir_fd = openat(got_repo_get_fd(repo),
1246 fc63f50d 2021-12-31 thomas GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1247 1124fe40 2021-07-07 stsp if (packdir_fd == -1) {
1248 1124fe40 2021-07-07 stsp if (errno == ENOENT)
1249 1124fe40 2021-07-07 stsp return NULL;
1250 1124fe40 2021-07-07 stsp return got_error_from_errno_fmt("openat: %s/%s",
1251 1124fe40 2021-07-07 stsp got_repo_get_path_git_dir(repo),
1252 1124fe40 2021-07-07 stsp GOT_OBJECTS_PACK_DIR);
1255 1124fe40 2021-07-07 stsp packdir = fdopendir(packdir_fd);
1256 1124fe40 2021-07-07 stsp if (packdir == NULL) {
1257 1124fe40 2021-07-07 stsp err = got_error_from_errno("fdopendir");
1258 1124fe40 2021-07-07 stsp goto done;
1261 1124fe40 2021-07-07 stsp while ((dent = readdir(packdir)) != NULL) {
1262 1124fe40 2021-07-07 stsp if (cancel_cb) {
1263 1124fe40 2021-07-07 stsp err = cancel_cb(cancel_arg);
1265 1124fe40 2021-07-07 stsp goto done;
1268 dd038bc6 2021-09-21 thomas.ad if (!got_repo_is_packidx_filename(dent->d_name,
1269 dd038bc6 2021-09-21 thomas.ad strlen(dent->d_name)))
1272 1124fe40 2021-07-07 stsp err = got_packidx_get_packfile_path(&pack_relpath,
1273 1124fe40 2021-07-07 stsp dent->d_name);
1275 1124fe40 2021-07-07 stsp goto done;
1277 1124fe40 2021-07-07 stsp if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1278 1124fe40 2021-07-07 stsp free(pack_relpath);
1279 1124fe40 2021-07-07 stsp pack_relpath = NULL;
1282 1124fe40 2021-07-07 stsp if (errno != ENOENT) {
1283 1124fe40 2021-07-07 stsp err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1284 1124fe40 2021-07-07 stsp got_repo_get_path_git_dir(repo),
1285 1124fe40 2021-07-07 stsp GOT_OBJECTS_PACK_DIR,
1286 1124fe40 2021-07-07 stsp pack_relpath);
1287 1124fe40 2021-07-07 stsp goto done;
1290 1124fe40 2021-07-07 stsp if (!dry_run) {
1291 1124fe40 2021-07-07 stsp err = remove_packidx(packdir_fd, dent->d_name);
1293 1124fe40 2021-07-07 stsp goto done;
1295 1124fe40 2021-07-07 stsp if (progress_cb) {
1296 1124fe40 2021-07-07 stsp char *path;
1297 1124fe40 2021-07-07 stsp if (asprintf(&path, "%s/%s/%s",
1298 1124fe40 2021-07-07 stsp got_repo_get_path_git_dir(repo),
1299 1124fe40 2021-07-07 stsp GOT_OBJECTS_PACK_DIR,
1300 1124fe40 2021-07-07 stsp dent->d_name) == -1) {
1301 1124fe40 2021-07-07 stsp err = got_error_from_errno("asprintf");
1302 1124fe40 2021-07-07 stsp goto done;
1304 1124fe40 2021-07-07 stsp err = progress_cb(progress_arg, path);
1305 1124fe40 2021-07-07 stsp free(path);
1307 1124fe40 2021-07-07 stsp goto done;
1309 1124fe40 2021-07-07 stsp free(pack_relpath);
1310 1124fe40 2021-07-07 stsp pack_relpath = NULL;
1313 1124fe40 2021-07-07 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
1314 1124fe40 2021-07-07 stsp err = got_error_from_errno("closedir");
1315 1124fe40 2021-07-07 stsp free(pack_relpath);
1316 b3d68e7f 2021-07-03 stsp return err;