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