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