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 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
20 05118f5a 2021-06-22 stsp #include <sys/uio.h>
21 05118f5a 2021-06-22 stsp #include <sys/stat.h>
22 05118f5a 2021-06-22 stsp #include <sys/socket.h>
23 05118f5a 2021-06-22 stsp #include <sys/wait.h>
24 05118f5a 2021-06-22 stsp
25 05118f5a 2021-06-22 stsp #include <dirent.h>
26 c8d1f14f 2021-06-23 naddy #include <endian.h>
27 05118f5a 2021-06-22 stsp #include <errno.h>
28 05118f5a 2021-06-22 stsp #include <fcntl.h>
29 05118f5a 2021-06-22 stsp #include <stdint.h>
30 05118f5a 2021-06-22 stsp #include <sha1.h>
31 5822e79e 2023-02-23 op #include <sha2.h>
32 05118f5a 2021-06-22 stsp #include <stdio.h>
33 05118f5a 2021-06-22 stsp #include <stdlib.h>
34 05118f5a 2021-06-22 stsp #include <string.h>
35 05118f5a 2021-06-22 stsp #include <limits.h>
36 05118f5a 2021-06-22 stsp #include <unistd.h>
37 05118f5a 2021-06-22 stsp #include <imsg.h>
38 05118f5a 2021-06-22 stsp
39 05118f5a 2021-06-22 stsp #include "got_error.h"
40 05118f5a 2021-06-22 stsp #include "got_cancel.h"
41 05118f5a 2021-06-22 stsp #include "got_object.h"
42 05118f5a 2021-06-22 stsp #include "got_reference.h"
43 05118f5a 2021-06-22 stsp #include "got_repository.h"
44 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
45 05118f5a 2021-06-22 stsp #include "got_opentemp.h"
46 05118f5a 2021-06-22 stsp #include "got_path.h"
47 05118f5a 2021-06-22 stsp
48 05118f5a 2021-06-22 stsp #include "got_lib_delta.h"
49 05118f5a 2021-06-22 stsp #include "got_lib_object.h"
50 b3d68e7f 2021-07-03 stsp #include "got_lib_object_idset.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
52 05118f5a 2021-06-22 stsp #include "got_lib_pack.h"
53 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
54 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
55 cae60ab8 2022-10-18 stsp #include "got_lib_ratelimit.h"
56 05118f5a 2021-06-22 stsp #include "got_lib_pack_create.h"
57 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
58 b3d68e7f 2021-07-03 stsp #include "got_lib_lockfile.h"
59 05118f5a 2021-06-22 stsp
60 05118f5a 2021-06-22 stsp #ifndef nitems
61 05118f5a 2021-06-22 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 05118f5a 2021-06-22 stsp #endif
63 05118f5a 2021-06-22 stsp
64 05118f5a 2021-06-22 stsp static const struct got_error *
65 05118f5a 2021-06-22 stsp get_reflist_object_ids(struct got_object_id ***ids, int *nobjects,
66 05118f5a 2021-06-22 stsp unsigned int wanted_obj_type_mask, struct got_reflist_head *refs,
67 05118f5a 2021-06-22 stsp struct got_repository *repo,
68 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
69 05118f5a 2021-06-22 stsp {
70 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
71 05118f5a 2021-06-22 stsp const size_t alloc_chunksz = 256;
72 05118f5a 2021-06-22 stsp size_t nalloc;
73 05118f5a 2021-06-22 stsp struct got_reflist_entry *re;
74 05118f5a 2021-06-22 stsp int i;
75 05118f5a 2021-06-22 stsp
76 05118f5a 2021-06-22 stsp *ids = NULL;
77 05118f5a 2021-06-22 stsp *nobjects = 0;
78 05118f5a 2021-06-22 stsp
79 7e4f461f 2022-04-13 stsp err = got_reflist_sort(refs,
80 7e4f461f 2022-04-13 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
81 7e4f461f 2022-04-13 stsp if (err)
82 7e4f461f 2022-04-13 stsp return err;
83 7e4f461f 2022-04-13 stsp
84 05118f5a 2021-06-22 stsp *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
85 05118f5a 2021-06-22 stsp if (*ids == NULL)
86 05118f5a 2021-06-22 stsp return got_error_from_errno("reallocarray");
87 05118f5a 2021-06-22 stsp nalloc = alloc_chunksz;
88 05118f5a 2021-06-22 stsp
89 05118f5a 2021-06-22 stsp TAILQ_FOREACH(re, refs, entry) {
90 05118f5a 2021-06-22 stsp struct got_object_id *id;
91 05118f5a 2021-06-22 stsp
92 05118f5a 2021-06-22 stsp if (cancel_cb) {
93 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
94 05118f5a 2021-06-22 stsp if (err)
95 05118f5a 2021-06-22 stsp goto done;
96 05118f5a 2021-06-22 stsp }
97 05118f5a 2021-06-22 stsp
98 05118f5a 2021-06-22 stsp err = got_ref_resolve(&id, repo, re->ref);
99 05118f5a 2021-06-22 stsp if (err)
100 05118f5a 2021-06-22 stsp goto done;
101 05118f5a 2021-06-22 stsp
102 05118f5a 2021-06-22 stsp if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
103 05118f5a 2021-06-22 stsp int obj_type;
104 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
105 05118f5a 2021-06-22 stsp if (err)
106 05118f5a 2021-06-22 stsp goto done;
107 05118f5a 2021-06-22 stsp if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
108 05118f5a 2021-06-22 stsp free(id);
109 05118f5a 2021-06-22 stsp id = NULL;
110 05118f5a 2021-06-22 stsp continue;
111 05118f5a 2021-06-22 stsp }
112 05118f5a 2021-06-22 stsp }
113 05118f5a 2021-06-22 stsp
114 ae23ce34 2021-07-01 stsp if (nalloc <= *nobjects) {
115 05118f5a 2021-06-22 stsp struct got_object_id **new;
116 05118f5a 2021-06-22 stsp new = recallocarray(*ids, nalloc,
117 05118f5a 2021-06-22 stsp nalloc + alloc_chunksz,
118 05118f5a 2021-06-22 stsp sizeof(struct got_object_id *));
119 05118f5a 2021-06-22 stsp if (new == NULL) {
120 05118f5a 2021-06-22 stsp err = got_error_from_errno(
121 05118f5a 2021-06-22 stsp "recallocarray");
122 05118f5a 2021-06-22 stsp goto done;
123 05118f5a 2021-06-22 stsp }
124 05118f5a 2021-06-22 stsp *ids = new;
125 05118f5a 2021-06-22 stsp nalloc += alloc_chunksz;
126 05118f5a 2021-06-22 stsp }
127 05118f5a 2021-06-22 stsp (*ids)[*nobjects] = id;
128 05118f5a 2021-06-22 stsp if ((*ids)[*nobjects] == NULL) {
129 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
130 05118f5a 2021-06-22 stsp goto done;
131 05118f5a 2021-06-22 stsp }
132 05118f5a 2021-06-22 stsp (*nobjects)++;
133 05118f5a 2021-06-22 stsp }
134 05118f5a 2021-06-22 stsp done:
135 05118f5a 2021-06-22 stsp if (err) {
136 05118f5a 2021-06-22 stsp for (i = 0; i < *nobjects; i++)
137 05118f5a 2021-06-22 stsp free((*ids)[i]);
138 05118f5a 2021-06-22 stsp free(*ids);
139 05118f5a 2021-06-22 stsp *ids = NULL;
140 05118f5a 2021-06-22 stsp *nobjects = 0;
141 05118f5a 2021-06-22 stsp }
142 05118f5a 2021-06-22 stsp return err;
143 05118f5a 2021-06-22 stsp }
144 05118f5a 2021-06-22 stsp
145 05118f5a 2021-06-22 stsp const struct got_error *
146 05118f5a 2021-06-22 stsp got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
147 05118f5a 2021-06-22 stsp struct got_reflist_head *include_refs,
148 05118f5a 2021-06-22 stsp struct got_reflist_head *exclude_refs, struct got_repository *repo,
149 c7a4fcc8 2023-02-17 op int loose_obj_only, int force_refdelta,
150 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
151 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
152 05118f5a 2021-06-22 stsp {
153 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
154 05118f5a 2021-06-22 stsp struct got_object_id **ours = NULL, **theirs = NULL;
155 05118f5a 2021-06-22 stsp int nours = 0, ntheirs = 0, packfd = -1, i;
156 05118f5a 2021-06-22 stsp char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
157 05118f5a 2021-06-22 stsp char *sha1_str = NULL;
158 a32780aa 2022-10-15 stsp FILE *delta_cache = NULL;
159 cae60ab8 2022-10-18 stsp struct got_ratelimit rl;
160 05118f5a 2021-06-22 stsp
161 05118f5a 2021-06-22 stsp *packfile = NULL;
162 05118f5a 2021-06-22 stsp *pack_hash = NULL;
163 05118f5a 2021-06-22 stsp
164 cae60ab8 2022-10-18 stsp got_ratelimit_init(&rl, 0, 500);
165 cae60ab8 2022-10-18 stsp
166 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/packing.pack",
167 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
168 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
169 05118f5a 2021-06-22 stsp goto done;
170 05118f5a 2021-06-22 stsp }
171 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
172 05118f5a 2021-06-22 stsp if (err)
173 05118f5a 2021-06-22 stsp goto done;
174 05118f5a 2021-06-22 stsp
175 839bbaae 2023-02-01 op if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
176 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpfile_path);
177 05118f5a 2021-06-22 stsp goto done;
178 05118f5a 2021-06-22 stsp }
179 05118f5a 2021-06-22 stsp
180 a32780aa 2022-10-15 stsp delta_cache = got_opentemp();
181 a32780aa 2022-10-15 stsp if (delta_cache == NULL) {
182 a32780aa 2022-10-15 stsp err = got_error_from_errno("got_opentemp");
183 a32780aa 2022-10-15 stsp goto done;
184 a32780aa 2022-10-15 stsp }
185 a32780aa 2022-10-15 stsp
186 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&ours, &nours,
187 abc59930 2021-09-05 naddy (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
188 abc59930 2021-09-05 naddy include_refs, repo, cancel_cb, cancel_arg);
189 05118f5a 2021-06-22 stsp if (err)
190 05118f5a 2021-06-22 stsp goto done;
191 05118f5a 2021-06-22 stsp
192 05118f5a 2021-06-22 stsp if (nours == 0) {
193 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
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 if (!TAILQ_EMPTY(exclude_refs)) {
198 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&theirs, &ntheirs,
199 abc59930 2021-09-05 naddy (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
200 abc59930 2021-09-05 naddy exclude_refs, repo,
201 abc59930 2021-09-05 naddy cancel_cb, cancel_arg);
202 05118f5a 2021-06-22 stsp if (err)
203 05118f5a 2021-06-22 stsp goto done;
204 05118f5a 2021-06-22 stsp }
205 05118f5a 2021-06-22 stsp
206 05118f5a 2021-06-22 stsp *pack_hash = calloc(1, sizeof(**pack_hash));
207 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
208 05118f5a 2021-06-22 stsp err = got_error_from_errno("calloc");
209 05118f5a 2021-06-22 stsp goto done;
210 05118f5a 2021-06-22 stsp }
211 05118f5a 2021-06-22 stsp
212 a32780aa 2022-10-15 stsp err = got_pack_create((*pack_hash)->sha1, packfd, delta_cache,
213 c7a4fcc8 2023-02-17 op theirs, ntheirs, ours, nours, repo, loose_obj_only,
214 c7a4fcc8 2023-02-17 op 0, force_refdelta, progress_cb, progress_arg, &rl,
215 c7a4fcc8 2023-02-17 op cancel_cb, cancel_arg);
216 05118f5a 2021-06-22 stsp if (err)
217 05118f5a 2021-06-22 stsp goto done;
218 05118f5a 2021-06-22 stsp
219 05118f5a 2021-06-22 stsp err = got_object_id_str(&sha1_str, *pack_hash);
220 05118f5a 2021-06-22 stsp if (err)
221 05118f5a 2021-06-22 stsp goto done;
222 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
223 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
224 05118f5a 2021-06-22 stsp sha1_str) == -1) {
225 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
226 05118f5a 2021-06-22 stsp goto done;
227 05118f5a 2021-06-22 stsp }
228 05118f5a 2021-06-22 stsp
229 894e4711 2022-10-15 stsp if (lseek(packfd, 0L, SEEK_SET) == -1) {
230 894e4711 2022-10-15 stsp err = got_error_from_errno("lseek");
231 05118f5a 2021-06-22 stsp goto done;
232 05118f5a 2021-06-22 stsp }
233 05118f5a 2021-06-22 stsp if (rename(tmpfile_path, packfile_path) == -1) {
234 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpfile_path,
235 05118f5a 2021-06-22 stsp packfile_path);
236 05118f5a 2021-06-22 stsp goto done;
237 05118f5a 2021-06-22 stsp }
238 05118f5a 2021-06-22 stsp free(tmpfile_path);
239 05118f5a 2021-06-22 stsp tmpfile_path = NULL;
240 894e4711 2022-10-15 stsp
241 894e4711 2022-10-15 stsp *packfile = fdopen(packfd, "w");
242 894e4711 2022-10-15 stsp if (*packfile == NULL) {
243 894e4711 2022-10-15 stsp err = got_error_from_errno2("fdopen", tmpfile_path);
244 894e4711 2022-10-15 stsp goto done;
245 894e4711 2022-10-15 stsp }
246 894e4711 2022-10-15 stsp packfd = -1;
247 05118f5a 2021-06-22 stsp done:
248 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++)
249 05118f5a 2021-06-22 stsp free(ours[i]);
250 05118f5a 2021-06-22 stsp free(ours);
251 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++)
252 05118f5a 2021-06-22 stsp free(theirs[i]);
253 05118f5a 2021-06-22 stsp free(theirs);
254 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
255 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
256 a32780aa 2022-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
257 a32780aa 2022-10-15 stsp err = got_error_from_errno("fclose");
258 05118f5a 2021-06-22 stsp if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
259 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpfile_path);
260 05118f5a 2021-06-22 stsp free(tmpfile_path);
261 05118f5a 2021-06-22 stsp free(packfile_path);
262 05118f5a 2021-06-22 stsp free(sha1_str);
263 05118f5a 2021-06-22 stsp free(path);
264 05118f5a 2021-06-22 stsp if (err) {
265 05118f5a 2021-06-22 stsp free(*pack_hash);
266 05118f5a 2021-06-22 stsp *pack_hash = NULL;
267 05118f5a 2021-06-22 stsp if (*packfile)
268 05118f5a 2021-06-22 stsp fclose(*packfile);
269 05118f5a 2021-06-22 stsp *packfile = NULL;
270 05118f5a 2021-06-22 stsp }
271 05118f5a 2021-06-22 stsp return err;
272 05118f5a 2021-06-22 stsp }
273 05118f5a 2021-06-22 stsp
274 05118f5a 2021-06-22 stsp const struct got_error *
275 05118f5a 2021-06-22 stsp got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
276 05118f5a 2021-06-22 stsp struct got_repository *repo,
277 05118f5a 2021-06-22 stsp got_pack_index_progress_cb progress_cb, void *progress_arg,
278 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
279 05118f5a 2021-06-22 stsp {
280 05118f5a 2021-06-22 stsp size_t i;
281 05118f5a 2021-06-22 stsp char *path;
282 05118f5a 2021-06-22 stsp int imsg_idxfds[2];
283 05118f5a 2021-06-22 stsp int npackfd = -1, idxfd = -1, nidxfd = -1;
284 05118f5a 2021-06-22 stsp int tmpfds[3];
285 05118f5a 2021-06-22 stsp int idxstatus, done = 0;
286 05118f5a 2021-06-22 stsp const struct got_error *err;
287 05118f5a 2021-06-22 stsp struct imsgbuf idxibuf;
288 05118f5a 2021-06-22 stsp pid_t idxpid;
289 05118f5a 2021-06-22 stsp char *tmpidxpath = NULL;
290 05118f5a 2021-06-22 stsp char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
291 05118f5a 2021-06-22 stsp const char *repo_path = got_repo_get_path_git_dir(repo);
292 05118f5a 2021-06-22 stsp struct stat sb;
293 05118f5a 2021-06-22 stsp
294 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++)
295 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
296 05118f5a 2021-06-22 stsp
297 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/indexing.idx",
298 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
299 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
300 05118f5a 2021-06-22 stsp goto done;
301 05118f5a 2021-06-22 stsp }
302 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
303 05118f5a 2021-06-22 stsp free(path);
304 05118f5a 2021-06-22 stsp if (err)
305 05118f5a 2021-06-22 stsp goto done;
306 839bbaae 2023-02-01 op if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
307 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpidxpath);
308 05118f5a 2021-06-22 stsp goto done;
309 05118f5a 2021-06-22 stsp }
310 05118f5a 2021-06-22 stsp
311 05118f5a 2021-06-22 stsp nidxfd = dup(idxfd);
312 05118f5a 2021-06-22 stsp if (nidxfd == -1) {
313 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
314 05118f5a 2021-06-22 stsp goto done;
315 05118f5a 2021-06-22 stsp }
316 05118f5a 2021-06-22 stsp
317 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
318 05118f5a 2021-06-22 stsp tmpfds[i] = got_opentempfd();
319 05118f5a 2021-06-22 stsp if (tmpfds[i] == -1) {
320 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_opentempfd");
321 05118f5a 2021-06-22 stsp goto done;
322 05118f5a 2021-06-22 stsp }
323 05118f5a 2021-06-22 stsp }
324 05118f5a 2021-06-22 stsp
325 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
326 05118f5a 2021-06-22 stsp if (err)
327 05118f5a 2021-06-22 stsp goto done;
328 05118f5a 2021-06-22 stsp
329 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
330 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
331 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
332 05118f5a 2021-06-22 stsp goto done;
333 05118f5a 2021-06-22 stsp }
334 05118f5a 2021-06-22 stsp
335 05118f5a 2021-06-22 stsp if (fstat(fileno(packfile), &sb) == -1) {
336 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fstat", packfile_path);
337 05118f5a 2021-06-22 stsp goto done;
338 05118f5a 2021-06-22 stsp }
339 05118f5a 2021-06-22 stsp
340 05118f5a 2021-06-22 stsp if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
341 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
342 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
343 05118f5a 2021-06-22 stsp goto done;
344 05118f5a 2021-06-22 stsp }
345 05118f5a 2021-06-22 stsp
346 05118f5a 2021-06-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
347 05118f5a 2021-06-22 stsp err = got_error_from_errno("socketpair");
348 05118f5a 2021-06-22 stsp goto done;
349 05118f5a 2021-06-22 stsp }
350 05118f5a 2021-06-22 stsp idxpid = fork();
351 05118f5a 2021-06-22 stsp if (idxpid == -1) {
352 05118f5a 2021-06-22 stsp err= got_error_from_errno("fork");
353 05118f5a 2021-06-22 stsp goto done;
354 05118f5a 2021-06-22 stsp } else if (idxpid == 0)
355 05118f5a 2021-06-22 stsp got_privsep_exec_child(imsg_idxfds,
356 05118f5a 2021-06-22 stsp GOT_PATH_PROG_INDEX_PACK, packfile_path);
357 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[1]) == -1) {
358 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
359 05118f5a 2021-06-22 stsp goto done;
360 05118f5a 2021-06-22 stsp }
361 05118f5a 2021-06-22 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
362 05118f5a 2021-06-22 stsp
363 05118f5a 2021-06-22 stsp npackfd = dup(fileno(packfile));
364 05118f5a 2021-06-22 stsp if (npackfd == -1) {
365 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
366 05118f5a 2021-06-22 stsp goto done;
367 05118f5a 2021-06-22 stsp }
368 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
369 05118f5a 2021-06-22 stsp npackfd);
370 05118f5a 2021-06-22 stsp if (err != NULL)
371 05118f5a 2021-06-22 stsp goto done;
372 05118f5a 2021-06-22 stsp npackfd = -1;
373 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
374 05118f5a 2021-06-22 stsp if (err != NULL)
375 05118f5a 2021-06-22 stsp goto done;
376 05118f5a 2021-06-22 stsp nidxfd = -1;
377 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
378 05118f5a 2021-06-22 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
379 05118f5a 2021-06-22 stsp if (err != NULL)
380 05118f5a 2021-06-22 stsp goto done;
381 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
382 05118f5a 2021-06-22 stsp }
383 05118f5a 2021-06-22 stsp done = 0;
384 05118f5a 2021-06-22 stsp while (!done) {
385 05118f5a 2021-06-22 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
386 05118f5a 2021-06-22 stsp
387 05118f5a 2021-06-22 stsp if (cancel_cb) {
388 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
389 05118f5a 2021-06-22 stsp if (err)
390 05118f5a 2021-06-22 stsp goto done;
391 05118f5a 2021-06-22 stsp }
392 05118f5a 2021-06-22 stsp
393 05118f5a 2021-06-22 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
394 05118f5a 2021-06-22 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
395 05118f5a 2021-06-22 stsp &idxibuf);
396 05118f5a 2021-06-22 stsp if (err != NULL)
397 05118f5a 2021-06-22 stsp goto done;
398 05118f5a 2021-06-22 stsp if (nobj_indexed != 0) {
399 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, sb.st_size,
400 05118f5a 2021-06-22 stsp nobj_total, nobj_indexed, nobj_loose,
401 05118f5a 2021-06-22 stsp nobj_resolved);
402 05118f5a 2021-06-22 stsp if (err)
403 05118f5a 2021-06-22 stsp break;
404 05118f5a 2021-06-22 stsp }
405 05118f5a 2021-06-22 stsp }
406 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[0]) == -1) {
407 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
408 05118f5a 2021-06-22 stsp goto done;
409 05118f5a 2021-06-22 stsp }
410 05118f5a 2021-06-22 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
411 05118f5a 2021-06-22 stsp err = got_error_from_errno("waitpid");
412 05118f5a 2021-06-22 stsp goto done;
413 05118f5a 2021-06-22 stsp }
414 05118f5a 2021-06-22 stsp
415 05118f5a 2021-06-22 stsp if (rename(tmpidxpath, idxpath) == -1) {
416 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
417 05118f5a 2021-06-22 stsp goto done;
418 05118f5a 2021-06-22 stsp }
419 05118f5a 2021-06-22 stsp free(tmpidxpath);
420 05118f5a 2021-06-22 stsp tmpidxpath = NULL;
421 05118f5a 2021-06-22 stsp
422 05118f5a 2021-06-22 stsp done:
423 05118f5a 2021-06-22 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
424 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpidxpath);
425 05118f5a 2021-06-22 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
426 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
427 05118f5a 2021-06-22 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
428 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
429 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
430 05118f5a 2021-06-22 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
431 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
432 05118f5a 2021-06-22 stsp }
433 05118f5a 2021-06-22 stsp free(tmpidxpath);
434 05118f5a 2021-06-22 stsp free(idxpath);
435 05118f5a 2021-06-22 stsp free(packfile_path);
436 05118f5a 2021-06-22 stsp return err;
437 05118f5a 2021-06-22 stsp }
438 05118f5a 2021-06-22 stsp
439 05118f5a 2021-06-22 stsp const struct got_error *
440 05118f5a 2021-06-22 stsp got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
441 05118f5a 2021-06-22 stsp struct got_repository *repo, const char *packfile_path)
442 05118f5a 2021-06-22 stsp {
443 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
444 05118f5a 2021-06-22 stsp const char *packdir_path = NULL;
445 05118f5a 2021-06-22 stsp char *packfile_name = NULL, *p, *dot;
446 05118f5a 2021-06-22 stsp struct got_object_id id;
447 05118f5a 2021-06-22 stsp int packfd = -1;
448 05118f5a 2021-06-22 stsp
449 05118f5a 2021-06-22 stsp *packfile = NULL;
450 05118f5a 2021-06-22 stsp *pack_hash = NULL;
451 05118f5a 2021-06-22 stsp
452 05118f5a 2021-06-22 stsp packdir_path = got_repo_get_path_objects_pack(repo);
453 05118f5a 2021-06-22 stsp if (packdir_path == NULL)
454 05118f5a 2021-06-22 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
455 05118f5a 2021-06-22 stsp
456 05118f5a 2021-06-22 stsp if (!got_path_is_child(packfile_path, packdir_path,
457 05118f5a 2021-06-22 stsp strlen(packdir_path))) {
458 05118f5a 2021-06-22 stsp err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
459 05118f5a 2021-06-22 stsp goto done;
460 05118f5a 2021-06-22 stsp
461 05118f5a 2021-06-22 stsp }
462 05118f5a 2021-06-22 stsp
463 05118f5a 2021-06-22 stsp err = got_path_basename(&packfile_name, packfile_path);
464 05118f5a 2021-06-22 stsp if (err)
465 05118f5a 2021-06-22 stsp goto done;
466 05118f5a 2021-06-22 stsp p = packfile_name;
467 05118f5a 2021-06-22 stsp
468 05118f5a 2021-06-22 stsp if (strncmp(p, "pack-", 5) != 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);
472 05118f5a 2021-06-22 stsp goto done;
473 05118f5a 2021-06-22 stsp }
474 05118f5a 2021-06-22 stsp p += 5;
475 05118f5a 2021-06-22 stsp dot = strchr(p, '.');
476 05118f5a 2021-06-22 stsp if (dot == NULL) {
477 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
478 abc59930 2021-09-05 naddy "'%s' is not a valid pack file name",
479 abc59930 2021-09-05 naddy packfile_name);
480 05118f5a 2021-06-22 stsp goto done;
481 05118f5a 2021-06-22 stsp }
482 05118f5a 2021-06-22 stsp if (strcmp(dot + 1, "pack") != 0) {
483 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
484 abc59930 2021-09-05 naddy "'%s' is not a valid pack file name",
485 abc59930 2021-09-05 naddy packfile_name);
486 05118f5a 2021-06-22 stsp goto done;
487 05118f5a 2021-06-22 stsp }
488 05118f5a 2021-06-22 stsp *dot = '\0';
489 b54930d5 2023-02-28 op if (!got_parse_object_id(&id, p, repo->algo)) {
490 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
491 abc59930 2021-09-05 naddy "'%s' is not a valid pack file name",
492 abc59930 2021-09-05 naddy packfile_name);
493 05118f5a 2021-06-22 stsp goto done;
494 05118f5a 2021-06-22 stsp }
495 05118f5a 2021-06-22 stsp
496 05118f5a 2021-06-22 stsp *pack_hash = got_object_id_dup(&id);
497 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
498 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
499 05118f5a 2021-06-22 stsp goto done;
500 05118f5a 2021-06-22 stsp }
501 05118f5a 2021-06-22 stsp
502 8bd0cdad 2021-12-31 stsp packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
503 05118f5a 2021-06-22 stsp if (packfd == -1) {
504 05118f5a 2021-06-22 stsp err = got_error_from_errno2("open", packfile_path);
505 05118f5a 2021-06-22 stsp goto done;
506 05118f5a 2021-06-22 stsp }
507 05118f5a 2021-06-22 stsp
508 05118f5a 2021-06-22 stsp *packfile = fdopen(packfd, "r");
509 05118f5a 2021-06-22 stsp if (*packfile == NULL) {
510 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fdopen", packfile_path);
511 05118f5a 2021-06-22 stsp goto done;
512 05118f5a 2021-06-22 stsp }
513 05118f5a 2021-06-22 stsp packfd = -1;
514 05118f5a 2021-06-22 stsp done:
515 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
516 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
517 05118f5a 2021-06-22 stsp free(packfile_name);
518 05118f5a 2021-06-22 stsp if (err) {
519 05118f5a 2021-06-22 stsp free(*pack_hash);
520 05118f5a 2021-06-22 stsp *pack_hash = NULL;
521 05118f5a 2021-06-22 stsp }
522 05118f5a 2021-06-22 stsp return err;
523 05118f5a 2021-06-22 stsp }
524 05118f5a 2021-06-22 stsp
525 05118f5a 2021-06-22 stsp const struct got_error *
526 05118f5a 2021-06-22 stsp got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
527 05118f5a 2021-06-22 stsp struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
528 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
529 05118f5a 2021-06-22 stsp {
530 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
531 05118f5a 2021-06-22 stsp char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
532 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
533 05118f5a 2021-06-22 stsp struct got_pack *pack = NULL;
534 05118f5a 2021-06-22 stsp uint32_t nobj, i;
535 05118f5a 2021-06-22 stsp
536 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
537 05118f5a 2021-06-22 stsp if (err)
538 05118f5a 2021-06-22 stsp goto done;
539 05118f5a 2021-06-22 stsp
540 05118f5a 2021-06-22 stsp if (asprintf(&packpath, "%s/pack-%s.pack",
541 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
542 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
543 05118f5a 2021-06-22 stsp goto done;
544 05118f5a 2021-06-22 stsp }
545 05118f5a 2021-06-22 stsp if (asprintf(&idxpath, "%s/pack-%s.idx",
546 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
547 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
548 05118f5a 2021-06-22 stsp goto done;
549 05118f5a 2021-06-22 stsp }
550 05118f5a 2021-06-22 stsp
551 05118f5a 2021-06-22 stsp err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
552 05118f5a 2021-06-22 stsp if (err)
553 05118f5a 2021-06-22 stsp goto done;
554 05118f5a 2021-06-22 stsp
555 05118f5a 2021-06-22 stsp err = got_repo_cache_pack(&pack, repo, packpath, packidx);
556 05118f5a 2021-06-22 stsp if (err)
557 05118f5a 2021-06-22 stsp goto done;
558 05118f5a 2021-06-22 stsp
559 05118f5a 2021-06-22 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
560 05118f5a 2021-06-22 stsp for (i = 0; i < nobj; i++) {
561 05118f5a 2021-06-22 stsp struct got_packidx_object_id *oid;
562 05118f5a 2021-06-22 stsp struct got_object_id id, base_id;
563 05118f5a 2021-06-22 stsp off_t offset, base_offset = 0;
564 05118f5a 2021-06-22 stsp uint8_t type;
565 05118f5a 2021-06-22 stsp uint64_t size;
566 05118f5a 2021-06-22 stsp size_t tslen, len;
567 05118f5a 2021-06-22 stsp
568 05118f5a 2021-06-22 stsp if (cancel_cb) {
569 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
570 05118f5a 2021-06-22 stsp if (err)
571 05118f5a 2021-06-22 stsp break;
572 05118f5a 2021-06-22 stsp }
573 05118f5a 2021-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
574 05118f5a 2021-06-22 stsp memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
575 05118f5a 2021-06-22 stsp
576 05118f5a 2021-06-22 stsp offset = got_packidx_get_object_offset(packidx, i);
577 05118f5a 2021-06-22 stsp if (offset == -1) {
578 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
579 05118f5a 2021-06-22 stsp goto done;
580 05118f5a 2021-06-22 stsp }
581 05118f5a 2021-06-22 stsp
582 05118f5a 2021-06-22 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
583 05118f5a 2021-06-22 stsp pack, offset);
584 05118f5a 2021-06-22 stsp if (err)
585 05118f5a 2021-06-22 stsp goto done;
586 05118f5a 2021-06-22 stsp
587 05118f5a 2021-06-22 stsp switch (type) {
588 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
589 05118f5a 2021-06-22 stsp err = got_pack_parse_offset_delta(&base_offset, &len,
590 05118f5a 2021-06-22 stsp pack, offset, tslen);
591 05118f5a 2021-06-22 stsp if (err)
592 05118f5a 2021-06-22 stsp goto done;
593 05118f5a 2021-06-22 stsp break;
594 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
595 05118f5a 2021-06-22 stsp err = got_pack_parse_ref_delta(&base_id,
596 05118f5a 2021-06-22 stsp pack, offset, tslen);
597 05118f5a 2021-06-22 stsp if (err)
598 05118f5a 2021-06-22 stsp goto done;
599 05118f5a 2021-06-22 stsp break;
600 05118f5a 2021-06-22 stsp }
601 05118f5a 2021-06-22 stsp err = (*list_cb)(list_arg, &id, type, offset, size,
602 05118f5a 2021-06-22 stsp base_offset, &base_id);
603 05118f5a 2021-06-22 stsp if (err)
604 05118f5a 2021-06-22 stsp goto done;
605 05118f5a 2021-06-22 stsp }
606 05118f5a 2021-06-22 stsp
607 05118f5a 2021-06-22 stsp done:
608 05118f5a 2021-06-22 stsp free(id_str);
609 05118f5a 2021-06-22 stsp free(idxpath);
610 05118f5a 2021-06-22 stsp free(packpath);
611 05118f5a 2021-06-22 stsp if (packidx)
612 05118f5a 2021-06-22 stsp got_packidx_close(packidx);
613 b3d68e7f 2021-07-03 stsp return err;
614 3bf0e21f 2023-06-19 op }
615 3bf0e21f 2023-06-19 op
616 0317ab6c 2023-07-11 op static const struct got_error *
617 0317ab6c 2023-07-11 op repo_cleanup_lock(struct got_repository *repo, struct got_lockfile **lk)
618 3bf0e21f 2023-06-19 op {
619 3bf0e21f 2023-06-19 op const struct got_error *err;
620 f528b33e 2023-06-25 naddy char myname[_POSIX_HOST_NAME_MAX + 1];
621 3bf0e21f 2023-06-19 op
622 3bf0e21f 2023-06-19 op if (gethostname(myname, sizeof(myname)) == -1)
623 3bf0e21f 2023-06-19 op return got_error_from_errno("gethostname");
624 3bf0e21f 2023-06-19 op
625 3bf0e21f 2023-06-19 op err = got_lockfile_lock(lk, "gc.pid", got_repo_get_fd(repo));
626 3bf0e21f 2023-06-19 op if (err)
627 3bf0e21f 2023-06-19 op return err;
628 3bf0e21f 2023-06-19 op
629 3bf0e21f 2023-06-19 op /*
630 3bf0e21f 2023-06-19 op * Git uses these info to provide some verbiage when finds a
631 3bf0e21f 2023-06-19 op * lock during `git gc --force' so don't try too hard to avoid
632 3bf0e21f 2023-06-19 op * short writes and don't care if a race happens between the
633 3bf0e21f 2023-06-19 op * lockfile creation and the write itself.
634 3bf0e21f 2023-06-19 op */
635 3bf0e21f 2023-06-19 op if (dprintf((*lk)->fd, "%d %s", getpid(), myname) < 0)
636 3bf0e21f 2023-06-19 op return got_error_from_errno("dprintf");
637 3bf0e21f 2023-06-19 op
638 3bf0e21f 2023-06-19 op return NULL;
639 b3d68e7f 2021-07-03 stsp }
640 b3d68e7f 2021-07-03 stsp
641 b3d68e7f 2021-07-03 stsp static const struct got_error *
642 211cfef0 2022-01-05 stsp report_cleanup_progress(got_cleanup_progress_cb progress_cb,
643 211cfef0 2022-01-05 stsp void *progress_arg, struct got_ratelimit *rl,
644 0317ab6c 2023-07-11 op int ncommits, int nloose, int npurged, int nredundant)
645 211cfef0 2022-01-05 stsp {
646 211cfef0 2022-01-05 stsp const struct got_error *err;
647 211cfef0 2022-01-05 stsp int elapsed;
648 211cfef0 2022-01-05 stsp
649 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
650 211cfef0 2022-01-05 stsp return NULL;
651 211cfef0 2022-01-05 stsp
652 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
653 211cfef0 2022-01-05 stsp if (err || !elapsed)
654 211cfef0 2022-01-05 stsp return err;
655 211cfef0 2022-01-05 stsp
656 0317ab6c 2023-07-11 op return progress_cb(progress_arg, ncommits, nloose, npurged,
657 0317ab6c 2023-07-11 op nredundant);
658 211cfef0 2022-01-05 stsp }
659 211cfef0 2022-01-05 stsp
660 211cfef0 2022-01-05 stsp static const struct got_error *
661 0317ab6c 2023-07-11 op get_loose_object_ids(struct got_object_idset **loose_ids,
662 0317ab6c 2023-07-11 op off_t *ondisk_size, int ncommits,
663 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb, void *progress_arg,
664 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, struct got_repository *repo)
665 b3d68e7f 2021-07-03 stsp {
666 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
667 b3d68e7f 2021-07-03 stsp char *path_objects = NULL, *path = NULL;
668 b3d68e7f 2021-07-03 stsp DIR *dir = NULL;
669 b3d68e7f 2021-07-03 stsp struct got_object *obj = NULL;
670 b3d68e7f 2021-07-03 stsp struct got_object_id id;
671 b3d68e7f 2021-07-03 stsp int i, fd = -1;
672 b3d68e7f 2021-07-03 stsp struct stat sb;
673 b3d68e7f 2021-07-03 stsp
674 b3d68e7f 2021-07-03 stsp *ondisk_size = 0;
675 b3d68e7f 2021-07-03 stsp *loose_ids = got_object_idset_alloc();
676 b3d68e7f 2021-07-03 stsp if (*loose_ids == NULL)
677 b3d68e7f 2021-07-03 stsp return got_error_from_errno("got_object_idset_alloc");
678 b3d68e7f 2021-07-03 stsp
679 b3d68e7f 2021-07-03 stsp path_objects = got_repo_get_path_objects(repo);
680 b3d68e7f 2021-07-03 stsp if (path_objects == NULL) {
681 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("got_repo_get_path_objects");
682 b3d68e7f 2021-07-03 stsp goto done;
683 b3d68e7f 2021-07-03 stsp }
684 b3d68e7f 2021-07-03 stsp
685 b3d68e7f 2021-07-03 stsp for (i = 0; i <= 0xff; i++) {
686 b3d68e7f 2021-07-03 stsp struct dirent *dent;
687 b3d68e7f 2021-07-03 stsp
688 b3d68e7f 2021-07-03 stsp if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
689 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
690 b3d68e7f 2021-07-03 stsp break;
691 b3d68e7f 2021-07-03 stsp }
692 b3d68e7f 2021-07-03 stsp
693 b3d68e7f 2021-07-03 stsp dir = opendir(path);
694 b3d68e7f 2021-07-03 stsp if (dir == NULL) {
695 b3d68e7f 2021-07-03 stsp if (errno == ENOENT) {
696 b3d68e7f 2021-07-03 stsp err = NULL;
697 b3d68e7f 2021-07-03 stsp continue;
698 b3d68e7f 2021-07-03 stsp }
699 b3d68e7f 2021-07-03 stsp err = got_error_from_errno2("opendir", path);
700 b3d68e7f 2021-07-03 stsp break;
701 b3d68e7f 2021-07-03 stsp }
702 b3d68e7f 2021-07-03 stsp
703 b3d68e7f 2021-07-03 stsp while ((dent = readdir(dir)) != NULL) {
704 b3d68e7f 2021-07-03 stsp char *id_str;
705 b3d68e7f 2021-07-03 stsp
706 b3d68e7f 2021-07-03 stsp if (strcmp(dent->d_name, ".") == 0 ||
707 b3d68e7f 2021-07-03 stsp strcmp(dent->d_name, "..") == 0)
708 b3d68e7f 2021-07-03 stsp continue;
709 b3d68e7f 2021-07-03 stsp
710 b3d68e7f 2021-07-03 stsp if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
711 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
712 b3d68e7f 2021-07-03 stsp goto done;
713 b3d68e7f 2021-07-03 stsp }
714 b3d68e7f 2021-07-03 stsp
715 b54930d5 2023-02-28 op if (!got_parse_object_id(&id, id_str, repo->algo)) {
716 b3d68e7f 2021-07-03 stsp free(id_str);
717 b3d68e7f 2021-07-03 stsp continue;
718 b3d68e7f 2021-07-03 stsp }
719 b3d68e7f 2021-07-03 stsp free(id_str);
720 b3d68e7f 2021-07-03 stsp
721 b3d68e7f 2021-07-03 stsp err = got_object_open_loose_fd(&fd, &id, repo);
722 b3d68e7f 2021-07-03 stsp if (err)
723 b3d68e7f 2021-07-03 stsp goto done;
724 b3d68e7f 2021-07-03 stsp if (fstat(fd, &sb) == -1) {
725 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("fstat");
726 b3d68e7f 2021-07-03 stsp goto done;
727 b3d68e7f 2021-07-03 stsp }
728 d5c81d44 2021-07-08 stsp err = got_object_read_header_privsep(&obj, &id, repo,
729 d5c81d44 2021-07-08 stsp fd);
730 b3d68e7f 2021-07-03 stsp if (err)
731 b3d68e7f 2021-07-03 stsp goto done;
732 b3d68e7f 2021-07-03 stsp fd = -1; /* already closed */
733 b3d68e7f 2021-07-03 stsp
734 b3d68e7f 2021-07-03 stsp switch (obj->type) {
735 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
736 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TREE:
737 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_BLOB:
738 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TAG:
739 b3d68e7f 2021-07-03 stsp break;
740 b3d68e7f 2021-07-03 stsp default:
741 b3d68e7f 2021-07-03 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
742 b3d68e7f 2021-07-03 stsp "%d", obj->type);
743 b3d68e7f 2021-07-03 stsp goto done;
744 b3d68e7f 2021-07-03 stsp }
745 b3d68e7f 2021-07-03 stsp got_object_close(obj);
746 b3d68e7f 2021-07-03 stsp obj = NULL;
747 b3d68e7f 2021-07-03 stsp (*ondisk_size) += sb.st_size;
748 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(*loose_ids, &id, NULL);
749 b3d68e7f 2021-07-03 stsp if (err)
750 b3d68e7f 2021-07-03 stsp goto done;
751 211cfef0 2022-01-05 stsp err = report_cleanup_progress(progress_cb,
752 0317ab6c 2023-07-11 op progress_arg, rl, ncommits,
753 5852c834 2023-06-23 stsp got_object_idset_num_elements(*loose_ids),
754 0317ab6c 2023-07-11 op -1, -1);
755 211cfef0 2022-01-05 stsp if (err)
756 211cfef0 2022-01-05 stsp goto done;
757 b3d68e7f 2021-07-03 stsp }
758 b3d68e7f 2021-07-03 stsp
759 b3d68e7f 2021-07-03 stsp if (closedir(dir) != 0) {
760 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("closedir");
761 b3d68e7f 2021-07-03 stsp goto done;
762 b3d68e7f 2021-07-03 stsp }
763 b3d68e7f 2021-07-03 stsp dir = NULL;
764 b3d68e7f 2021-07-03 stsp
765 b3d68e7f 2021-07-03 stsp free(path);
766 b3d68e7f 2021-07-03 stsp path = NULL;
767 b3d68e7f 2021-07-03 stsp }
768 b3d68e7f 2021-07-03 stsp done:
769 b3d68e7f 2021-07-03 stsp if (dir && closedir(dir) != 0 && err == NULL)
770 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("closedir");
771 b3d68e7f 2021-07-03 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
772 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("close");
773 b3d68e7f 2021-07-03 stsp if (err) {
774 b3d68e7f 2021-07-03 stsp got_object_idset_free(*loose_ids);
775 b3d68e7f 2021-07-03 stsp *loose_ids = NULL;
776 b3d68e7f 2021-07-03 stsp }
777 b3d68e7f 2021-07-03 stsp if (obj)
778 b3d68e7f 2021-07-03 stsp got_object_close(obj);
779 b3d68e7f 2021-07-03 stsp free(path_objects);
780 b3d68e7f 2021-07-03 stsp free(path);
781 b3d68e7f 2021-07-03 stsp return err;
782 b3d68e7f 2021-07-03 stsp }
783 b3d68e7f 2021-07-03 stsp
784 b3d68e7f 2021-07-03 stsp static const struct got_error *
785 b3d68e7f 2021-07-03 stsp load_tree_entries(struct got_object_id_queue *ids,
786 b3d68e7f 2021-07-03 stsp struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
787 0317ab6c 2023-07-11 op const char *dpath, struct got_repository *repo,
788 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
789 b3d68e7f 2021-07-03 stsp {
790 b3d68e7f 2021-07-03 stsp const struct got_error *err;
791 b3d68e7f 2021-07-03 stsp struct got_tree_object *tree;
792 b3d68e7f 2021-07-03 stsp char *p = NULL;
793 b3d68e7f 2021-07-03 stsp int i;
794 b3d68e7f 2021-07-03 stsp
795 b3d68e7f 2021-07-03 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
796 b3d68e7f 2021-07-03 stsp if (err)
797 b3d68e7f 2021-07-03 stsp return err;
798 b3d68e7f 2021-07-03 stsp
799 b3d68e7f 2021-07-03 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
800 b3d68e7f 2021-07-03 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
801 b3d68e7f 2021-07-03 stsp struct got_object_id *id = got_tree_entry_get_id(e);
802 b3d68e7f 2021-07-03 stsp mode_t mode = got_tree_entry_get_mode(e);
803 b3d68e7f 2021-07-03 stsp
804 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
805 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
806 b3d68e7f 2021-07-03 stsp if (err)
807 b3d68e7f 2021-07-03 stsp break;
808 b3d68e7f 2021-07-03 stsp }
809 b3d68e7f 2021-07-03 stsp
810 b3d68e7f 2021-07-03 stsp if (got_object_tree_entry_is_symlink(e) ||
811 b3d68e7f 2021-07-03 stsp got_object_tree_entry_is_submodule(e) ||
812 b3d68e7f 2021-07-03 stsp got_object_idset_contains(traversed_ids, id))
813 b3d68e7f 2021-07-03 stsp continue;
814 b3d68e7f 2021-07-03 stsp
815 b3d68e7f 2021-07-03 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
816 b3d68e7f 2021-07-03 stsp got_tree_entry_get_name(e)) == -1) {
817 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
818 b3d68e7f 2021-07-03 stsp break;
819 b3d68e7f 2021-07-03 stsp }
820 b3d68e7f 2021-07-03 stsp
821 b3d68e7f 2021-07-03 stsp if (S_ISDIR(mode)) {
822 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
823 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, id);
824 b3d68e7f 2021-07-03 stsp if (err)
825 b3d68e7f 2021-07-03 stsp break;
826 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
827 b3d68e7f 2021-07-03 stsp } else if (S_ISREG(mode)) {
828 b3d68e7f 2021-07-03 stsp /* This blob is referenced. */
829 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(traversed_ids, id, NULL);
830 b3d68e7f 2021-07-03 stsp if (err)
831 b3d68e7f 2021-07-03 stsp break;
832 b3d68e7f 2021-07-03 stsp }
833 b3d68e7f 2021-07-03 stsp free(p);
834 b3d68e7f 2021-07-03 stsp p = NULL;
835 b3d68e7f 2021-07-03 stsp }
836 b3d68e7f 2021-07-03 stsp
837 b3d68e7f 2021-07-03 stsp got_object_tree_close(tree);
838 b3d68e7f 2021-07-03 stsp free(p);
839 b3d68e7f 2021-07-03 stsp return err;
840 b3d68e7f 2021-07-03 stsp }
841 b3d68e7f 2021-07-03 stsp
842 b3d68e7f 2021-07-03 stsp static const struct got_error *
843 0317ab6c 2023-07-11 op load_tree(struct got_object_idset *traversed_ids,
844 0317ab6c 2023-07-11 op struct got_object_id *tree_id,
845 0317ab6c 2023-07-11 op const char *dpath, struct got_repository *repo,
846 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
847 b3d68e7f 2021-07-03 stsp {
848 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
849 b3d68e7f 2021-07-03 stsp struct got_object_id_queue tree_ids;
850 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
851 b3d68e7f 2021-07-03 stsp
852 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, tree_id);
853 b3d68e7f 2021-07-03 stsp if (err)
854 b3d68e7f 2021-07-03 stsp return err;
855 b3d68e7f 2021-07-03 stsp
856 b3d68e7f 2021-07-03 stsp STAILQ_INIT(&tree_ids);
857 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
858 b3d68e7f 2021-07-03 stsp
859 b3d68e7f 2021-07-03 stsp while (!STAILQ_EMPTY(&tree_ids)) {
860 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
861 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
862 b3d68e7f 2021-07-03 stsp if (err)
863 b3d68e7f 2021-07-03 stsp break;
864 b3d68e7f 2021-07-03 stsp }
865 b3d68e7f 2021-07-03 stsp
866 b3d68e7f 2021-07-03 stsp qid = STAILQ_FIRST(&tree_ids);
867 b3d68e7f 2021-07-03 stsp STAILQ_REMOVE_HEAD(&tree_ids, entry);
868 b3d68e7f 2021-07-03 stsp
869 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(traversed_ids, &qid->id)) {
870 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
871 b3d68e7f 2021-07-03 stsp continue;
872 b3d68e7f 2021-07-03 stsp }
873 b3d68e7f 2021-07-03 stsp
874 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(traversed_ids, &qid->id, NULL);
875 b3d68e7f 2021-07-03 stsp if (err) {
876 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
877 b3d68e7f 2021-07-03 stsp break;
878 b3d68e7f 2021-07-03 stsp }
879 b3d68e7f 2021-07-03 stsp
880 0317ab6c 2023-07-11 op err = load_tree_entries(&tree_ids, traversed_ids,
881 0317ab6c 2023-07-11 op &qid->id, dpath, repo, cancel_cb, cancel_arg);
882 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
883 b3d68e7f 2021-07-03 stsp if (err)
884 b3d68e7f 2021-07-03 stsp break;
885 b3d68e7f 2021-07-03 stsp }
886 b3d68e7f 2021-07-03 stsp
887 b3d68e7f 2021-07-03 stsp got_object_id_queue_free(&tree_ids);
888 b3d68e7f 2021-07-03 stsp return err;
889 b3d68e7f 2021-07-03 stsp }
890 b3d68e7f 2021-07-03 stsp
891 b3d68e7f 2021-07-03 stsp static const struct got_error *
892 0317ab6c 2023-07-11 op load_commit_or_tag(int *ncommits, struct got_object_idset *traversed_ids,
893 b3d68e7f 2021-07-03 stsp struct got_object_id *id, struct got_repository *repo,
894 211cfef0 2022-01-05 stsp got_cleanup_progress_cb progress_cb, void *progress_arg,
895 0317ab6c 2023-07-11 op struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
896 b3d68e7f 2021-07-03 stsp {
897 b3d68e7f 2021-07-03 stsp const struct got_error *err;
898 b3d68e7f 2021-07-03 stsp struct got_commit_object *commit = NULL;
899 b3d68e7f 2021-07-03 stsp struct got_tag_object *tag = NULL;
900 b3d68e7f 2021-07-03 stsp struct got_object_id *tree_id = NULL;
901 b3d68e7f 2021-07-03 stsp struct got_object_id_queue ids;
902 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
903 b3d68e7f 2021-07-03 stsp int obj_type;
904 b3d68e7f 2021-07-03 stsp
905 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, id);
906 b3d68e7f 2021-07-03 stsp if (err)
907 b3d68e7f 2021-07-03 stsp return err;
908 b3d68e7f 2021-07-03 stsp
909 b3d68e7f 2021-07-03 stsp STAILQ_INIT(&ids);
910 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
911 b3d68e7f 2021-07-03 stsp
912 b3d68e7f 2021-07-03 stsp while (!STAILQ_EMPTY(&ids)) {
913 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
914 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
915 b3d68e7f 2021-07-03 stsp if (err)
916 b3d68e7f 2021-07-03 stsp break;
917 b3d68e7f 2021-07-03 stsp }
918 b3d68e7f 2021-07-03 stsp
919 b3d68e7f 2021-07-03 stsp qid = STAILQ_FIRST(&ids);
920 b3d68e7f 2021-07-03 stsp STAILQ_REMOVE_HEAD(&ids, entry);
921 b3d68e7f 2021-07-03 stsp
922 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(traversed_ids, &qid->id)) {
923 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
924 b3d68e7f 2021-07-03 stsp qid = NULL;
925 b3d68e7f 2021-07-03 stsp continue;
926 b3d68e7f 2021-07-03 stsp }
927 b3d68e7f 2021-07-03 stsp
928 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(traversed_ids, &qid->id, NULL);
929 b3d68e7f 2021-07-03 stsp if (err)
930 b3d68e7f 2021-07-03 stsp break;
931 b3d68e7f 2021-07-03 stsp
932 d7b5a0e8 2022-04-20 stsp err = got_object_get_type(&obj_type, repo, &qid->id);
933 b3d68e7f 2021-07-03 stsp if (err)
934 b3d68e7f 2021-07-03 stsp break;
935 b3d68e7f 2021-07-03 stsp switch (obj_type) {
936 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
937 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&commit, repo,
938 d7b5a0e8 2022-04-20 stsp &qid->id);
939 b3d68e7f 2021-07-03 stsp if (err)
940 b3d68e7f 2021-07-03 stsp goto done;
941 b3d68e7f 2021-07-03 stsp break;
942 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TAG:
943 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_tag(&tag, repo, &qid->id);
944 b3d68e7f 2021-07-03 stsp if (err)
945 b3d68e7f 2021-07-03 stsp goto done;
946 b3d68e7f 2021-07-03 stsp break;
947 b3d68e7f 2021-07-03 stsp default:
948 b3d68e7f 2021-07-03 stsp /* should not happen */
949 b3d68e7f 2021-07-03 stsp err = got_error(GOT_ERR_OBJ_TYPE);
950 b3d68e7f 2021-07-03 stsp goto done;
951 b3d68e7f 2021-07-03 stsp }
952 b3d68e7f 2021-07-03 stsp
953 b3d68e7f 2021-07-03 stsp /* Find a tree object to scan. */
954 b3d68e7f 2021-07-03 stsp if (commit) {
955 b3d68e7f 2021-07-03 stsp tree_id = got_object_commit_get_tree_id(commit);
956 b3d68e7f 2021-07-03 stsp } else if (tag) {
957 b3d68e7f 2021-07-03 stsp obj_type = got_object_tag_get_object_type(tag);
958 b3d68e7f 2021-07-03 stsp switch (obj_type) {
959 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
960 b3d68e7f 2021-07-03 stsp err = got_object_open_as_commit(&commit, repo,
961 b3d68e7f 2021-07-03 stsp got_object_tag_get_object_id(tag));
962 b3d68e7f 2021-07-03 stsp if (err)
963 b3d68e7f 2021-07-03 stsp goto done;
964 b3d68e7f 2021-07-03 stsp tree_id = got_object_commit_get_tree_id(commit);
965 b3d68e7f 2021-07-03 stsp break;
966 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TREE:
967 b3d68e7f 2021-07-03 stsp tree_id = got_object_tag_get_object_id(tag);
968 b3d68e7f 2021-07-03 stsp break;
969 b3d68e7f 2021-07-03 stsp default:
970 b3d68e7f 2021-07-03 stsp /*
971 b3d68e7f 2021-07-03 stsp * Tag points at something other than a
972 b3d68e7f 2021-07-03 stsp * commit or tree. Leave this weird tag object
973 0317ab6c 2023-07-11 op * and the object it points to.
974 b3d68e7f 2021-07-03 stsp */
975 0317ab6c 2023-07-11 op if (got_object_idset_contains(traversed_ids,
976 0317ab6c 2023-07-11 op got_object_tag_get_object_id(tag)))
977 0317ab6c 2023-07-11 op break;
978 0317ab6c 2023-07-11 op err = got_object_idset_add(traversed_ids,
979 0317ab6c 2023-07-11 op got_object_tag_get_object_id(tag), NULL);
980 0317ab6c 2023-07-11 op if (err)
981 b3d68e7f 2021-07-03 stsp goto done;
982 b3d68e7f 2021-07-03 stsp break;
983 b3d68e7f 2021-07-03 stsp }
984 b3d68e7f 2021-07-03 stsp }
985 b3d68e7f 2021-07-03 stsp
986 b3d68e7f 2021-07-03 stsp if (tree_id) {
987 0317ab6c 2023-07-11 op err = load_tree(traversed_ids, tree_id, "",
988 0317ab6c 2023-07-11 op repo, cancel_cb, cancel_arg);
989 b3d68e7f 2021-07-03 stsp if (err)
990 b3d68e7f 2021-07-03 stsp break;
991 b3d68e7f 2021-07-03 stsp }
992 b3d68e7f 2021-07-03 stsp
993 b3d68e7f 2021-07-03 stsp if (commit || tag)
994 b3d68e7f 2021-07-03 stsp (*ncommits)++; /* scanned tags are counted as commits */
995 b3d68e7f 2021-07-03 stsp
996 211cfef0 2022-01-05 stsp err = report_cleanup_progress(progress_cb, progress_arg, rl,
997 0317ab6c 2023-07-11 op *ncommits, -1, -1, -1);
998 211cfef0 2022-01-05 stsp if (err)
999 211cfef0 2022-01-05 stsp break;
1000 5e91dae4 2022-08-30 stsp
1001 b3d68e7f 2021-07-03 stsp if (commit) {
1002 b3d68e7f 2021-07-03 stsp /* Find parent commits to scan. */
1003 b3d68e7f 2021-07-03 stsp const struct got_object_id_queue *parent_ids;
1004 b3d68e7f 2021-07-03 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1005 b3d68e7f 2021-07-03 stsp err = got_object_id_queue_copy(parent_ids, &ids);
1006 b3d68e7f 2021-07-03 stsp if (err)
1007 b3d68e7f 2021-07-03 stsp break;
1008 b3d68e7f 2021-07-03 stsp got_object_commit_close(commit);
1009 b3d68e7f 2021-07-03 stsp commit = NULL;
1010 b3d68e7f 2021-07-03 stsp }
1011 b3d68e7f 2021-07-03 stsp if (tag) {
1012 b3d68e7f 2021-07-03 stsp got_object_tag_close(tag);
1013 b3d68e7f 2021-07-03 stsp tag = NULL;
1014 b3d68e7f 2021-07-03 stsp }
1015 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
1016 b3d68e7f 2021-07-03 stsp qid = NULL;
1017 b3d68e7f 2021-07-03 stsp }
1018 b3d68e7f 2021-07-03 stsp done:
1019 b3d68e7f 2021-07-03 stsp if (qid)
1020 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
1021 b3d68e7f 2021-07-03 stsp if (commit)
1022 b3d68e7f 2021-07-03 stsp got_object_commit_close(commit);
1023 b3d68e7f 2021-07-03 stsp if (tag)
1024 b3d68e7f 2021-07-03 stsp got_object_tag_close(tag);
1025 4b2e47fb 2021-07-03 stsp got_object_id_queue_free(&ids);
1026 b3d68e7f 2021-07-03 stsp return err;
1027 b3d68e7f 2021-07-03 stsp }
1028 b3d68e7f 2021-07-03 stsp
1029 0317ab6c 2023-07-11 op static const struct got_error *
1030 0317ab6c 2023-07-11 op is_object_packed(int *packed, struct got_repository *repo,
1031 0317ab6c 2023-07-11 op struct got_object_id *id)
1032 0317ab6c 2023-07-11 op {
1033 0317ab6c 2023-07-11 op const struct got_error *err;
1034 0317ab6c 2023-07-11 op struct got_object *obj;
1035 0317ab6c 2023-07-11 op
1036 0317ab6c 2023-07-11 op *packed = 0;
1037 0317ab6c 2023-07-11 op
1038 0317ab6c 2023-07-11 op err = got_object_open_packed(&obj, id, repo);
1039 0317ab6c 2023-07-11 op if (err) {
1040 0317ab6c 2023-07-11 op if (err->code == GOT_ERR_NO_OBJ)
1041 0317ab6c 2023-07-11 op err = NULL;
1042 0317ab6c 2023-07-11 op return err;
1043 0317ab6c 2023-07-11 op }
1044 0317ab6c 2023-07-11 op got_object_close(obj);
1045 0317ab6c 2023-07-11 op *packed = 1;
1046 0317ab6c 2023-07-11 op return NULL;
1047 0317ab6c 2023-07-11 op }
1048 0317ab6c 2023-07-11 op
1049 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg {
1050 b3d68e7f 2021-07-03 stsp struct got_repository *repo;
1051 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb;
1052 b3d68e7f 2021-07-03 stsp void *progress_arg;
1053 211cfef0 2022-01-05 stsp struct got_ratelimit *rl;
1054 0317ab6c 2023-07-11 op struct got_object_idset *traversed_ids;
1055 b3d68e7f 2021-07-03 stsp int nloose;
1056 b3d68e7f 2021-07-03 stsp int ncommits;
1057 0317ab6c 2023-07-11 op int npacked;
1058 b3d68e7f 2021-07-03 stsp int npurged;
1059 b3d68e7f 2021-07-03 stsp off_t size_purged;
1060 b3d68e7f 2021-07-03 stsp int dry_run;
1061 ef8ec606 2021-07-27 stsp time_t max_mtime;
1062 ef8ec606 2021-07-27 stsp int ignore_mtime;
1063 b3d68e7f 2021-07-03 stsp };
1064 b3d68e7f 2021-07-03 stsp
1065 b3d68e7f 2021-07-03 stsp static const struct got_error *
1066 b3d68e7f 2021-07-03 stsp purge_loose_object(struct got_object_id *id, void *data, void *arg)
1067 b3d68e7f 2021-07-03 stsp {
1068 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg *a = arg;
1069 b3d68e7f 2021-07-03 stsp const struct got_error *err, *unlock_err = NULL;
1070 b3d68e7f 2021-07-03 stsp char *path = NULL;
1071 0317ab6c 2023-07-11 op int packed, fd = -1;
1072 b3d68e7f 2021-07-03 stsp struct stat sb;
1073 b3d68e7f 2021-07-03 stsp struct got_lockfile *lf = NULL;
1074 b3d68e7f 2021-07-03 stsp
1075 0317ab6c 2023-07-11 op err = is_object_packed(&packed, a->repo, id);
1076 0317ab6c 2023-07-11 op if (err)
1077 0317ab6c 2023-07-11 op return err;
1078 0317ab6c 2023-07-11 op
1079 0317ab6c 2023-07-11 op if (!packed && got_object_idset_contains(a->traversed_ids, id))
1080 0317ab6c 2023-07-11 op return NULL;
1081 0317ab6c 2023-07-11 op
1082 0317ab6c 2023-07-11 op if (packed)
1083 0317ab6c 2023-07-11 op a->npacked++;
1084 0317ab6c 2023-07-11 op
1085 b3d68e7f 2021-07-03 stsp err = got_object_get_path(&path, id, a->repo);
1086 b3d68e7f 2021-07-03 stsp if (err)
1087 b3d68e7f 2021-07-03 stsp return err;
1088 b3d68e7f 2021-07-03 stsp
1089 b3d68e7f 2021-07-03 stsp err = got_object_open_loose_fd(&fd, id, a->repo);
1090 b3d68e7f 2021-07-03 stsp if (err)
1091 b3d68e7f 2021-07-03 stsp goto done;
1092 b3d68e7f 2021-07-03 stsp
1093 b3d68e7f 2021-07-03 stsp if (fstat(fd, &sb) == -1) {
1094 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("fstat");
1095 b3d68e7f 2021-07-03 stsp goto done;
1096 b3d68e7f 2021-07-03 stsp }
1097 b3d68e7f 2021-07-03 stsp
1098 ef8ec606 2021-07-27 stsp /*
1099 ef8ec606 2021-07-27 stsp * Do not delete objects which are younger than our maximum
1100 ef8ec606 2021-07-27 stsp * modification time threshold. This prevents a race where
1101 ef8ec606 2021-07-27 stsp * new objects which are being added to the repository
1102 ef8ec606 2021-07-27 stsp * concurrently would be deleted.
1103 ef8ec606 2021-07-27 stsp */
1104 ef8ec606 2021-07-27 stsp if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
1105 ef8ec606 2021-07-27 stsp if (!a->dry_run) {
1106 ef8ec606 2021-07-27 stsp err = got_lockfile_lock(&lf, path, -1);
1107 ef8ec606 2021-07-27 stsp if (err)
1108 ef8ec606 2021-07-27 stsp goto done;
1109 ef8ec606 2021-07-27 stsp if (unlink(path) == -1) {
1110 ef8ec606 2021-07-27 stsp err = got_error_from_errno2("unlink", path);
1111 ef8ec606 2021-07-27 stsp goto done;
1112 ef8ec606 2021-07-27 stsp }
1113 b3d68e7f 2021-07-03 stsp }
1114 b3d68e7f 2021-07-03 stsp
1115 ef8ec606 2021-07-27 stsp a->npurged++;
1116 ef8ec606 2021-07-27 stsp a->size_purged += sb.st_size;
1117 211cfef0 2022-01-05 stsp err = report_cleanup_progress(a->progress_cb, a->progress_arg,
1118 0317ab6c 2023-07-11 op a->rl, a->ncommits, a->nloose, a->npurged, -1);
1119 211cfef0 2022-01-05 stsp if (err)
1120 211cfef0 2022-01-05 stsp goto done;
1121 b3d68e7f 2021-07-03 stsp }
1122 b3d68e7f 2021-07-03 stsp done:
1123 b3d68e7f 2021-07-03 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1124 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("close");
1125 b3d68e7f 2021-07-03 stsp free(path);
1126 b3d68e7f 2021-07-03 stsp if (lf)
1127 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1128 b3d68e7f 2021-07-03 stsp return err ? err : unlock_err;
1129 b3d68e7f 2021-07-03 stsp }
1130 b3d68e7f 2021-07-03 stsp
1131 0317ab6c 2023-07-11 op static const struct got_error *
1132 0317ab6c 2023-07-11 op repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1133 0317ab6c 2023-07-11 op struct got_object_idset *traversed_ids,
1134 0317ab6c 2023-07-11 op off_t *size_before, off_t *size_after, int ncommits, int *nloose,
1135 0317ab6c 2023-07-11 op int *npacked, int *npurged, int dry_run, int ignore_mtime,
1136 0317ab6c 2023-07-11 op time_t max_mtime, struct got_ratelimit *rl,
1137 900499fd 2023-06-23 stsp got_cleanup_progress_cb progress_cb, void *progress_arg,
1138 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1139 b3d68e7f 2021-07-03 stsp {
1140 b3d68e7f 2021-07-03 stsp const struct got_error *err;
1141 b3d68e7f 2021-07-03 stsp struct got_object_idset *loose_ids;
1142 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg arg;
1143 b3d68e7f 2021-07-03 stsp
1144 0317ab6c 2023-07-11 op err = get_loose_object_ids(&loose_ids, size_before, ncommits,
1145 0317ab6c 2023-07-11 op progress_cb, progress_arg, rl, repo);
1146 b3d68e7f 2021-07-03 stsp if (err)
1147 b3d68e7f 2021-07-03 stsp return err;
1148 900499fd 2023-06-23 stsp *nloose = got_object_idset_num_elements(loose_ids);
1149 900499fd 2023-06-23 stsp if (*nloose == 0) {
1150 b3d68e7f 2021-07-03 stsp got_object_idset_free(loose_ids);
1151 dbe266a4 2022-01-05 stsp if (progress_cb) {
1152 9a7c12cf 2023-06-18 op err = progress_cb(progress_arg, 0, 0, 0, -1);
1153 dbe266a4 2022-01-05 stsp if (err)
1154 dbe266a4 2022-01-05 stsp return err;
1155 dbe266a4 2022-01-05 stsp }
1156 b3d68e7f 2021-07-03 stsp return NULL;
1157 b3d68e7f 2021-07-03 stsp }
1158 b3d68e7f 2021-07-03 stsp
1159 0317ab6c 2023-07-11 op memset(&arg, 0, sizeof(arg));
1160 b3d68e7f 2021-07-03 stsp arg.repo = repo;
1161 b3d68e7f 2021-07-03 stsp arg.progress_arg = progress_arg;
1162 b3d68e7f 2021-07-03 stsp arg.progress_cb = progress_cb;
1163 0317ab6c 2023-07-11 op arg.rl = rl;
1164 0317ab6c 2023-07-11 op arg.traversed_ids = traversed_ids;
1165 900499fd 2023-06-23 stsp arg.nloose = *nloose;
1166 0317ab6c 2023-07-11 op arg.npacked = 0;
1167 b3d68e7f 2021-07-03 stsp arg.npurged = 0;
1168 b3d68e7f 2021-07-03 stsp arg.size_purged = 0;
1169 b3d68e7f 2021-07-03 stsp arg.dry_run = dry_run;
1170 ef8ec606 2021-07-27 stsp arg.max_mtime = max_mtime;
1171 ef8ec606 2021-07-27 stsp arg.ignore_mtime = ignore_mtime;
1172 b3d68e7f 2021-07-03 stsp err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1173 b3d68e7f 2021-07-03 stsp if (err)
1174 b3d68e7f 2021-07-03 stsp goto done;
1175 0317ab6c 2023-07-11 op
1176 b3d68e7f 2021-07-03 stsp *size_after = *size_before - arg.size_purged;
1177 0317ab6c 2023-07-11 op *npacked = arg.npacked;
1178 0317ab6c 2023-07-11 op *npurged = arg.npurged;
1179 211cfef0 2022-01-05 stsp
1180 211cfef0 2022-01-05 stsp /* Produce a final progress report. */
1181 211cfef0 2022-01-05 stsp if (progress_cb) {
1182 0317ab6c 2023-07-11 op err = progress_cb(progress_arg, ncommits, *nloose,
1183 0317ab6c 2023-07-11 op arg.npurged, -1);
1184 211cfef0 2022-01-05 stsp if (err)
1185 211cfef0 2022-01-05 stsp goto done;
1186 211cfef0 2022-01-05 stsp }
1187 b3d68e7f 2021-07-03 stsp done:
1188 b3d68e7f 2021-07-03 stsp got_object_idset_free(loose_ids);
1189 9a7c12cf 2023-06-18 op return err;
1190 9a7c12cf 2023-06-18 op }
1191 9a7c12cf 2023-06-18 op
1192 9a7c12cf 2023-06-18 op static const struct got_error *
1193 9a7c12cf 2023-06-18 op purge_redundant_pack(struct got_repository *repo, const char *packidx_path,
1194 ad6dd0bb 2023-07-11 op int dry_run, int ignore_mtime, time_t max_mtime,
1195 ad6dd0bb 2023-07-11 op int *remove, off_t *size_before, off_t *size_after)
1196 9a7c12cf 2023-06-18 op {
1197 9a7c12cf 2023-06-18 op static const char *ext[] = {".idx", ".pack", ".rev", ".bitmap",
1198 9a7c12cf 2023-06-18 op ".promisor", ".mtimes"};
1199 9a7c12cf 2023-06-18 op struct stat sb;
1200 9a7c12cf 2023-06-18 op char *dot, path[PATH_MAX];
1201 9a7c12cf 2023-06-18 op size_t i;
1202 9a7c12cf 2023-06-18 op
1203 0d23c640 2023-07-09 op if (strlcpy(path, packidx_path, sizeof(path)) >= sizeof(path))
1204 9a7c12cf 2023-06-18 op return got_error(GOT_ERR_NO_SPACE);
1205 9a7c12cf 2023-06-18 op
1206 9a7c12cf 2023-06-18 op /*
1207 ad6dd0bb 2023-07-11 op * Do not delete pack files which are younger than our maximum
1208 ad6dd0bb 2023-07-11 op * modification time threshold. This prevents a race where a
1209 ad6dd0bb 2023-07-11 op * new pack file which is being added to the repository
1210 ad6dd0bb 2023-07-11 op * concurrently would be deleted.
1211 ad6dd0bb 2023-07-11 op */
1212 ad6dd0bb 2023-07-11 op if (fstatat(got_repo_get_fd(repo), path, &sb, 0) == -1) {
1213 ad6dd0bb 2023-07-11 op if (errno == ENOENT)
1214 ad6dd0bb 2023-07-11 op return NULL;
1215 ad6dd0bb 2023-07-11 op return got_error_from_errno2("fstatat", path);
1216 ad6dd0bb 2023-07-11 op }
1217 ad6dd0bb 2023-07-11 op if (!ignore_mtime && sb.st_mtime > max_mtime)
1218 ad6dd0bb 2023-07-11 op *remove = 0;
1219 ad6dd0bb 2023-07-11 op
1220 ad6dd0bb 2023-07-11 op /*
1221 9a7c12cf 2023-06-18 op * For compatibility with Git, if a matching .keep file exist
1222 9a7c12cf 2023-06-18 op * don't delete the packfile.
1223 9a7c12cf 2023-06-18 op */
1224 9a7c12cf 2023-06-18 op dot = strrchr(path, '.');
1225 9a7c12cf 2023-06-18 op *dot = '\0';
1226 9a7c12cf 2023-06-18 op if (strlcat(path, ".keep", sizeof(path)) >= sizeof(path))
1227 9a7c12cf 2023-06-18 op return got_error(GOT_ERR_NO_SPACE);
1228 9a7c12cf 2023-06-18 op if (faccessat(got_repo_get_fd(repo), path, F_OK, 0) == 0)
1229 9a7c12cf 2023-06-18 op *remove = 0;
1230 9a7c12cf 2023-06-18 op
1231 9a7c12cf 2023-06-18 op for (i = 0; i < nitems(ext); ++i) {
1232 9a7c12cf 2023-06-18 op *dot = '\0';
1233 9a7c12cf 2023-06-18 op
1234 9a7c12cf 2023-06-18 op if (strlcat(path, ext[i], sizeof(path)) >=
1235 9a7c12cf 2023-06-18 op sizeof(path))
1236 9a7c12cf 2023-06-18 op return got_error(GOT_ERR_NO_SPACE);
1237 9a7c12cf 2023-06-18 op
1238 9a7c12cf 2023-06-18 op if (fstatat(got_repo_get_fd(repo), path, &sb, 0) ==
1239 9a7c12cf 2023-06-18 op -1) {
1240 77c65f86 2023-07-11 op if (errno == ENOENT)
1241 9a7c12cf 2023-06-18 op continue;
1242 9a7c12cf 2023-06-18 op return got_error_from_errno2("fstatat", path);
1243 9a7c12cf 2023-06-18 op }
1244 9a7c12cf 2023-06-18 op
1245 9a7c12cf 2023-06-18 op *size_before += sb.st_size;
1246 9a7c12cf 2023-06-18 op if (!*remove) {
1247 9a7c12cf 2023-06-18 op *size_after += sb.st_size;
1248 9a7c12cf 2023-06-18 op continue;
1249 9a7c12cf 2023-06-18 op }
1250 9a7c12cf 2023-06-18 op
1251 9a7c12cf 2023-06-18 op if (dry_run)
1252 9a7c12cf 2023-06-18 op continue;
1253 9a7c12cf 2023-06-18 op
1254 9a7c12cf 2023-06-18 op if (unlinkat(got_repo_get_fd(repo), path, 0) == -1) {
1255 9a7c12cf 2023-06-18 op if (errno == ENOENT)
1256 9a7c12cf 2023-06-18 op continue;
1257 9a7c12cf 2023-06-18 op return got_error_from_errno2("unlinkat",
1258 9a7c12cf 2023-06-18 op path);
1259 9a7c12cf 2023-06-18 op }
1260 9a7c12cf 2023-06-18 op }
1261 9a7c12cf 2023-06-18 op
1262 9a7c12cf 2023-06-18 op return NULL;
1263 9a7c12cf 2023-06-18 op }
1264 9a7c12cf 2023-06-18 op
1265 9a7c12cf 2023-06-18 op static const struct got_error *
1266 9a7c12cf 2023-06-18 op pack_is_redundant(int *redundant, struct got_repository *repo,
1267 0317ab6c 2023-07-11 op struct got_object_idset *traversed_ids,
1268 9a7c12cf 2023-06-18 op const char *packidx_path, struct got_object_idset *idset)
1269 9a7c12cf 2023-06-18 op {
1270 9a7c12cf 2023-06-18 op const struct got_error *err;
1271 9a7c12cf 2023-06-18 op struct got_packidx *packidx;
1272 9a7c12cf 2023-06-18 op struct got_packidx_object_id *pid;
1273 9a7c12cf 2023-06-18 op struct got_object_id id;
1274 9a7c12cf 2023-06-18 op size_t i, nobjects;
1275 9a7c12cf 2023-06-18 op
1276 9a7c12cf 2023-06-18 op *redundant = 1;
1277 9a7c12cf 2023-06-18 op
1278 9a7c12cf 2023-06-18 op err = got_repo_get_packidx(&packidx, packidx_path, repo);
1279 9a7c12cf 2023-06-18 op if (err)
1280 9a7c12cf 2023-06-18 op return err;
1281 9a7c12cf 2023-06-18 op
1282 9a7c12cf 2023-06-18 op nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1283 9a7c12cf 2023-06-18 op for (i = 0; i < nobjects; ++i) {
1284 9a7c12cf 2023-06-18 op pid = &packidx->hdr.sorted_ids[i];
1285 9a7c12cf 2023-06-18 op
1286 9a7c12cf 2023-06-18 op memset(&id, 0, sizeof(id));
1287 9a7c12cf 2023-06-18 op memcpy(&id.sha1, pid->sha1, sizeof(id.sha1));
1288 9a7c12cf 2023-06-18 op
1289 9a7c12cf 2023-06-18 op if (got_object_idset_contains(idset, &id))
1290 9a7c12cf 2023-06-18 op continue;
1291 9a7c12cf 2023-06-18 op
1292 0317ab6c 2023-07-11 op if (!got_object_idset_contains(traversed_ids, &id))
1293 0317ab6c 2023-07-11 op continue;
1294 0317ab6c 2023-07-11 op
1295 9a7c12cf 2023-06-18 op *redundant = 0;
1296 9a7c12cf 2023-06-18 op err = got_object_idset_add(idset, &id, NULL);
1297 9a7c12cf 2023-06-18 op if (err)
1298 9a7c12cf 2023-06-18 op return err;
1299 9a7c12cf 2023-06-18 op }
1300 9a7c12cf 2023-06-18 op
1301 9a7c12cf 2023-06-18 op return NULL;
1302 9a7c12cf 2023-06-18 op }
1303 9a7c12cf 2023-06-18 op
1304 9a7c12cf 2023-06-18 op struct pack_info {
1305 9a7c12cf 2023-06-18 op const char *path;
1306 9a7c12cf 2023-06-18 op size_t nobjects;
1307 9a7c12cf 2023-06-18 op };
1308 9a7c12cf 2023-06-18 op
1309 9a7c12cf 2023-06-18 op static int
1310 9a7c12cf 2023-06-18 op pack_info_cmp(const void *a, const void *b)
1311 9a7c12cf 2023-06-18 op {
1312 9a7c12cf 2023-06-18 op const struct pack_info *pa, *pb;
1313 9a7c12cf 2023-06-18 op
1314 9a7c12cf 2023-06-18 op pa = a;
1315 9a7c12cf 2023-06-18 op pb = b;
1316 9a7c12cf 2023-06-18 op if (pa->nobjects == pb->nobjects)
1317 9a7c12cf 2023-06-18 op return strcmp(pa->path, pb->path);
1318 9a7c12cf 2023-06-18 op if (pa->nobjects > pb->nobjects)
1319 9a7c12cf 2023-06-18 op return -1;
1320 9a7c12cf 2023-06-18 op return 1;
1321 9a7c12cf 2023-06-18 op }
1322 9a7c12cf 2023-06-18 op
1323 0317ab6c 2023-07-11 op static const struct got_error *
1324 0317ab6c 2023-07-11 op repo_purge_redundant_packfiles(struct got_repository *repo,
1325 0317ab6c 2023-07-11 op struct got_object_idset *traversed_ids,
1326 ad6dd0bb 2023-07-11 op off_t *size_before, off_t *size_after, int dry_run, int ignore_mtime,
1327 ad6dd0bb 2023-07-11 op time_t max_mtime, int nloose, int ncommits, int npurged,
1328 ad6dd0bb 2023-07-11 op struct got_ratelimit *rl,
1329 9a7c12cf 2023-06-18 op got_cleanup_progress_cb progress_cb, void *progress_arg,
1330 9a7c12cf 2023-06-18 op got_cancel_cb cancel_cb, void *cancel_arg)
1331 9a7c12cf 2023-06-18 op {
1332 9a7c12cf 2023-06-18 op const struct got_error *err;
1333 9a7c12cf 2023-06-18 op struct pack_info *pinfo, *sorted = NULL;
1334 9a7c12cf 2023-06-18 op struct got_packidx *packidx;
1335 9a7c12cf 2023-06-18 op struct got_object_idset *idset = NULL;
1336 9a7c12cf 2023-06-18 op struct got_pathlist_entry *pe;
1337 9a7c12cf 2023-06-18 op size_t i, npacks;
1338 9a7c12cf 2023-06-18 op int remove, redundant_packs = 0;
1339 5852c834 2023-06-23 stsp
1340 9a7c12cf 2023-06-18 op npacks = 0;
1341 9a7c12cf 2023-06-18 op TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
1342 9a7c12cf 2023-06-18 op npacks++;
1343 9a7c12cf 2023-06-18 op
1344 9a7c12cf 2023-06-18 op if (npacks == 0)
1345 9a7c12cf 2023-06-18 op return NULL;
1346 9a7c12cf 2023-06-18 op
1347 9a7c12cf 2023-06-18 op sorted = calloc(npacks, sizeof(*sorted));
1348 9a7c12cf 2023-06-18 op if (sorted == NULL)
1349 9a7c12cf 2023-06-18 op return got_error_from_errno("calloc");
1350 9a7c12cf 2023-06-18 op
1351 9a7c12cf 2023-06-18 op i = 0;
1352 9a7c12cf 2023-06-18 op TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1353 9a7c12cf 2023-06-18 op err = got_repo_get_packidx(&packidx, pe->path, repo);
1354 9a7c12cf 2023-06-18 op if (err)
1355 9a7c12cf 2023-06-18 op goto done;
1356 9a7c12cf 2023-06-18 op
1357 9a7c12cf 2023-06-18 op pinfo = &sorted[i++];
1358 9a7c12cf 2023-06-18 op pinfo->path = pe->path;
1359 9a7c12cf 2023-06-18 op pinfo->nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1360 9a7c12cf 2023-06-18 op }
1361 9a7c12cf 2023-06-18 op qsort(sorted, npacks, sizeof(*sorted), pack_info_cmp);
1362 9a7c12cf 2023-06-18 op
1363 9a7c12cf 2023-06-18 op idset = got_object_idset_alloc();
1364 9a7c12cf 2023-06-18 op if (idset == NULL) {
1365 9a7c12cf 2023-06-18 op err = got_error_from_errno("got_object_idset_alloc");
1366 9a7c12cf 2023-06-18 op goto done;
1367 9a7c12cf 2023-06-18 op }
1368 9a7c12cf 2023-06-18 op
1369 9a7c12cf 2023-06-18 op for (i = 0; i < npacks; ++i) {
1370 9a7c12cf 2023-06-18 op if (cancel_cb) {
1371 9a7c12cf 2023-06-18 op err = (*cancel_cb)(cancel_arg);
1372 9a7c12cf 2023-06-18 op if (err)
1373 9a7c12cf 2023-06-18 op break;
1374 9a7c12cf 2023-06-18 op }
1375 9a7c12cf 2023-06-18 op
1376 0317ab6c 2023-07-11 op err = pack_is_redundant(&remove, repo, traversed_ids,
1377 0317ab6c 2023-07-11 op sorted[i].path, idset);
1378 9a7c12cf 2023-06-18 op if (err)
1379 9a7c12cf 2023-06-18 op goto done;
1380 9a7c12cf 2023-06-18 op err = purge_redundant_pack(repo, sorted[i].path, dry_run,
1381 ad6dd0bb 2023-07-11 op ignore_mtime, max_mtime, &remove, size_before, size_after);
1382 9a7c12cf 2023-06-18 op if (err)
1383 9a7c12cf 2023-06-18 op goto done;
1384 9a7c12cf 2023-06-18 op if (!remove)
1385 9a7c12cf 2023-06-18 op continue;
1386 5852c834 2023-06-23 stsp err = report_cleanup_progress(progress_cb, progress_arg,
1387 0317ab6c 2023-07-11 op rl, ncommits, nloose, npurged, ++redundant_packs);
1388 5852c834 2023-06-23 stsp if (err)
1389 5852c834 2023-06-23 stsp goto done;
1390 900499fd 2023-06-23 stsp }
1391 900499fd 2023-06-23 stsp
1392 900499fd 2023-06-23 stsp /* Produce a final progress report. */
1393 900499fd 2023-06-23 stsp if (progress_cb) {
1394 0317ab6c 2023-07-11 op err = progress_cb(progress_arg, ncommits, nloose, npurged,
1395 900499fd 2023-06-23 stsp redundant_packs);
1396 9a7c12cf 2023-06-18 op if (err)
1397 9a7c12cf 2023-06-18 op goto done;
1398 9a7c12cf 2023-06-18 op }
1399 9a7c12cf 2023-06-18 op done:
1400 9a7c12cf 2023-06-18 op free(sorted);
1401 9a7c12cf 2023-06-18 op if (idset)
1402 9a7c12cf 2023-06-18 op got_object_idset_free(idset);
1403 0317ab6c 2023-07-11 op return err;
1404 0317ab6c 2023-07-11 op }
1405 0317ab6c 2023-07-11 op
1406 0317ab6c 2023-07-11 op const struct got_error *
1407 0317ab6c 2023-07-11 op got_repo_cleanup(struct got_repository *repo,
1408 0317ab6c 2023-07-11 op off_t *loose_before, off_t *loose_after,
1409 0317ab6c 2023-07-11 op off_t *pack_before, off_t *pack_after,
1410 0317ab6c 2023-07-11 op int *ncommits, int *nloose, int *npacked, int dry_run, int ignore_mtime,
1411 0317ab6c 2023-07-11 op got_cleanup_progress_cb progress_cb, void *progress_arg,
1412 0317ab6c 2023-07-11 op got_cancel_cb cancel_cb, void *cancel_arg)
1413 0317ab6c 2023-07-11 op {
1414 0317ab6c 2023-07-11 op const struct got_error *unlock_err, *err = NULL;
1415 0317ab6c 2023-07-11 op struct got_lockfile *lk = NULL;
1416 0317ab6c 2023-07-11 op struct got_ratelimit rl;
1417 0317ab6c 2023-07-11 op struct got_reflist_head refs;
1418 0317ab6c 2023-07-11 op struct got_object_idset *traversed_ids = NULL;
1419 0317ab6c 2023-07-11 op struct got_reflist_entry *re;
1420 0317ab6c 2023-07-11 op struct got_object_id **referenced_ids;
1421 0317ab6c 2023-07-11 op int i, nreferenced;
1422 0317ab6c 2023-07-11 op int npurged = 0;
1423 0317ab6c 2023-07-11 op time_t max_mtime = 0;
1424 0317ab6c 2023-07-11 op
1425 0317ab6c 2023-07-11 op TAILQ_INIT(&refs);
1426 0317ab6c 2023-07-11 op got_ratelimit_init(&rl, 0, 500);
1427 0317ab6c 2023-07-11 op
1428 0317ab6c 2023-07-11 op *loose_before = 0;
1429 0317ab6c 2023-07-11 op *loose_after = 0;
1430 0317ab6c 2023-07-11 op *pack_before = 0;
1431 0317ab6c 2023-07-11 op *pack_after = 0;
1432 0317ab6c 2023-07-11 op *ncommits = 0;
1433 0317ab6c 2023-07-11 op *nloose = 0;
1434 0317ab6c 2023-07-11 op *npacked = 0;
1435 0317ab6c 2023-07-11 op
1436 0317ab6c 2023-07-11 op err = repo_cleanup_lock(repo, &lk);
1437 0317ab6c 2023-07-11 op if (err)
1438 0317ab6c 2023-07-11 op return err;
1439 0317ab6c 2023-07-11 op
1440 0317ab6c 2023-07-11 op traversed_ids = got_object_idset_alloc();
1441 0317ab6c 2023-07-11 op if (traversed_ids == NULL) {
1442 0317ab6c 2023-07-11 op err = got_error_from_errno("got_object_idset_alloc");
1443 0317ab6c 2023-07-11 op goto done;
1444 0317ab6c 2023-07-11 op }
1445 0317ab6c 2023-07-11 op
1446 0317ab6c 2023-07-11 op err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1447 0317ab6c 2023-07-11 op if (err)
1448 0317ab6c 2023-07-11 op goto done;
1449 0317ab6c 2023-07-11 op if (!ignore_mtime) {
1450 0317ab6c 2023-07-11 op TAILQ_FOREACH(re, &refs, entry) {
1451 0317ab6c 2023-07-11 op time_t mtime = got_ref_get_mtime(re->ref);
1452 0317ab6c 2023-07-11 op if (mtime > max_mtime)
1453 0317ab6c 2023-07-11 op max_mtime = mtime;
1454 0317ab6c 2023-07-11 op }
1455 0317ab6c 2023-07-11 op /*
1456 0317ab6c 2023-07-11 op * For safety, keep objects created within 10 minutes
1457 0317ab6c 2023-07-11 op * before the youngest reference was created.
1458 0317ab6c 2023-07-11 op */
1459 0317ab6c 2023-07-11 op if (max_mtime >= 600)
1460 0317ab6c 2023-07-11 op max_mtime -= 600;
1461 0317ab6c 2023-07-11 op }
1462 0317ab6c 2023-07-11 op
1463 0317ab6c 2023-07-11 op err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1464 0317ab6c 2023-07-11 op (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1465 0317ab6c 2023-07-11 op &refs, repo, cancel_cb, cancel_arg);
1466 0317ab6c 2023-07-11 op if (err)
1467 0317ab6c 2023-07-11 op goto done;
1468 0317ab6c 2023-07-11 op
1469 0317ab6c 2023-07-11 op for (i = 0; i < nreferenced; i++) {
1470 0317ab6c 2023-07-11 op struct got_object_id *id = referenced_ids[i];
1471 0317ab6c 2023-07-11 op err = load_commit_or_tag(ncommits, traversed_ids,
1472 0317ab6c 2023-07-11 op id, repo, progress_cb, progress_arg, &rl,
1473 0317ab6c 2023-07-11 op cancel_cb, cancel_arg);
1474 0317ab6c 2023-07-11 op if (err)
1475 0317ab6c 2023-07-11 op goto done;
1476 0317ab6c 2023-07-11 op }
1477 0317ab6c 2023-07-11 op
1478 0317ab6c 2023-07-11 op err = repo_purge_unreferenced_loose_objects(repo, traversed_ids,
1479 0317ab6c 2023-07-11 op loose_before, loose_after, *ncommits, nloose, npacked, &npurged,
1480 0317ab6c 2023-07-11 op dry_run, ignore_mtime, max_mtime, &rl, progress_cb, progress_arg,
1481 0317ab6c 2023-07-11 op cancel_cb, cancel_arg);
1482 0317ab6c 2023-07-11 op if (err)
1483 0317ab6c 2023-07-11 op goto done;
1484 0317ab6c 2023-07-11 op
1485 0317ab6c 2023-07-11 op err = repo_purge_redundant_packfiles(repo, traversed_ids,
1486 ad6dd0bb 2023-07-11 op pack_before, pack_after, dry_run, ignore_mtime, max_mtime,
1487 ad6dd0bb 2023-07-11 op *nloose, *ncommits, npurged, &rl, progress_cb, progress_arg,
1488 ad6dd0bb 2023-07-11 op cancel_cb, cancel_arg);
1489 0317ab6c 2023-07-11 op if (err)
1490 0317ab6c 2023-07-11 op goto done;
1491 0317ab6c 2023-07-11 op
1492 0317ab6c 2023-07-11 op done:
1493 0317ab6c 2023-07-11 op if (lk) {
1494 0317ab6c 2023-07-11 op unlock_err = got_lockfile_unlock(lk, got_repo_get_fd(repo));
1495 0317ab6c 2023-07-11 op if (err == NULL)
1496 0317ab6c 2023-07-11 op err = unlock_err;
1497 0317ab6c 2023-07-11 op }
1498 0317ab6c 2023-07-11 op if (traversed_ids)
1499 0317ab6c 2023-07-11 op got_object_idset_free(traversed_ids);
1500 1124fe40 2021-07-07 stsp return err;
1501 1124fe40 2021-07-07 stsp }
1502 1124fe40 2021-07-07 stsp
1503 1124fe40 2021-07-07 stsp const struct got_error *
1504 1124fe40 2021-07-07 stsp got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
1505 1124fe40 2021-07-07 stsp got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
1506 1124fe40 2021-07-07 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1507 1124fe40 2021-07-07 stsp {
1508 2edc2f9d 2022-07-21 florian const struct got_error *err = NULL;
1509 1124fe40 2021-07-07 stsp DIR *packdir = NULL;
1510 1124fe40 2021-07-07 stsp struct dirent *dent;
1511 1124fe40 2021-07-07 stsp char *pack_relpath = NULL;
1512 1124fe40 2021-07-07 stsp int packdir_fd;
1513 1124fe40 2021-07-07 stsp struct stat sb;
1514 1124fe40 2021-07-07 stsp
1515 1124fe40 2021-07-07 stsp packdir_fd = openat(got_repo_get_fd(repo),
1516 e7ae0baf 2021-12-31 stsp GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1517 1124fe40 2021-07-07 stsp if (packdir_fd == -1) {
1518 1124fe40 2021-07-07 stsp if (errno == ENOENT)
1519 1124fe40 2021-07-07 stsp return NULL;
1520 1124fe40 2021-07-07 stsp return got_error_from_errno_fmt("openat: %s/%s",
1521 1124fe40 2021-07-07 stsp got_repo_get_path_git_dir(repo),
1522 1124fe40 2021-07-07 stsp GOT_OBJECTS_PACK_DIR);
1523 1124fe40 2021-07-07 stsp }
1524 1124fe40 2021-07-07 stsp
1525 1124fe40 2021-07-07 stsp packdir = fdopendir(packdir_fd);
1526 1124fe40 2021-07-07 stsp if (packdir == NULL) {
1527 1124fe40 2021-07-07 stsp err = got_error_from_errno("fdopendir");
1528 fc9b745f 2024-01-30 op close(packdir_fd);
1529 1124fe40 2021-07-07 stsp goto done;
1530 1124fe40 2021-07-07 stsp }
1531 1124fe40 2021-07-07 stsp
1532 1124fe40 2021-07-07 stsp while ((dent = readdir(packdir)) != NULL) {
1533 1124fe40 2021-07-07 stsp if (cancel_cb) {
1534 1124fe40 2021-07-07 stsp err = cancel_cb(cancel_arg);
1535 1124fe40 2021-07-07 stsp if (err)
1536 1124fe40 2021-07-07 stsp goto done;
1537 1124fe40 2021-07-07 stsp }
1538 1124fe40 2021-07-07 stsp
1539 1124fe40 2021-07-07 stsp if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1540 1124fe40 2021-07-07 stsp continue;
1541 1124fe40 2021-07-07 stsp
1542 1124fe40 2021-07-07 stsp err = got_packidx_get_packfile_path(&pack_relpath,
1543 1124fe40 2021-07-07 stsp dent->d_name);
1544 1124fe40 2021-07-07 stsp if (err)
1545 1124fe40 2021-07-07 stsp goto done;
1546 1124fe40 2021-07-07 stsp
1547 1124fe40 2021-07-07 stsp if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
1548 1124fe40 2021-07-07 stsp free(pack_relpath);
1549 1124fe40 2021-07-07 stsp pack_relpath = NULL;
1550 1124fe40 2021-07-07 stsp continue;
1551 1124fe40 2021-07-07 stsp }
1552 1124fe40 2021-07-07 stsp if (errno != ENOENT) {
1553 1124fe40 2021-07-07 stsp err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
1554 1124fe40 2021-07-07 stsp got_repo_get_path_git_dir(repo),
1555 1124fe40 2021-07-07 stsp GOT_OBJECTS_PACK_DIR,
1556 1124fe40 2021-07-07 stsp pack_relpath);
1557 1124fe40 2021-07-07 stsp goto done;
1558 1124fe40 2021-07-07 stsp }
1559 1124fe40 2021-07-07 stsp
1560 1124fe40 2021-07-07 stsp if (!dry_run) {
1561 3bf0e21f 2023-06-19 op if (unlinkat(packdir_fd, dent->d_name, 0) == -1) {
1562 3bf0e21f 2023-06-19 op err = got_error_from_errno("unlinkat");
1563 1124fe40 2021-07-07 stsp goto done;
1564 3bf0e21f 2023-06-19 op }
1565 1124fe40 2021-07-07 stsp }
1566 1124fe40 2021-07-07 stsp if (progress_cb) {
1567 1124fe40 2021-07-07 stsp char *path;
1568 1124fe40 2021-07-07 stsp if (asprintf(&path, "%s/%s/%s",
1569 1124fe40 2021-07-07 stsp got_repo_get_path_git_dir(repo),
1570 1124fe40 2021-07-07 stsp GOT_OBJECTS_PACK_DIR,
1571 1124fe40 2021-07-07 stsp dent->d_name) == -1) {
1572 1124fe40 2021-07-07 stsp err = got_error_from_errno("asprintf");
1573 1124fe40 2021-07-07 stsp goto done;
1574 1124fe40 2021-07-07 stsp }
1575 1124fe40 2021-07-07 stsp err = progress_cb(progress_arg, path);
1576 1124fe40 2021-07-07 stsp free(path);
1577 1124fe40 2021-07-07 stsp if (err)
1578 1124fe40 2021-07-07 stsp goto done;
1579 1124fe40 2021-07-07 stsp }
1580 1124fe40 2021-07-07 stsp free(pack_relpath);
1581 1124fe40 2021-07-07 stsp pack_relpath = NULL;
1582 1124fe40 2021-07-07 stsp }
1583 1124fe40 2021-07-07 stsp done:
1584 1124fe40 2021-07-07 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
1585 1124fe40 2021-07-07 stsp err = got_error_from_errno("closedir");
1586 1124fe40 2021-07-07 stsp free(pack_relpath);
1587 b3d68e7f 2021-07-03 stsp return err;
1588 b3d68e7f 2021-07-03 stsp }