Blame


1 05118f5a 2021-06-22 stsp /*
2 05118f5a 2021-06-22 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 05118f5a 2021-06-22 stsp *
4 05118f5a 2021-06-22 stsp * Permission to use, copy, modify, and distribute this software for any
5 05118f5a 2021-06-22 stsp * purpose with or without fee is hereby granted, provided that the above
6 05118f5a 2021-06-22 stsp * copyright notice and this permission notice appear in all copies.
7 05118f5a 2021-06-22 stsp *
8 05118f5a 2021-06-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 05118f5a 2021-06-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 05118f5a 2021-06-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 05118f5a 2021-06-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 05118f5a 2021-06-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 05118f5a 2021-06-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 05118f5a 2021-06-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 05118f5a 2021-06-22 stsp */
16 05118f5a 2021-06-22 stsp
17 05118f5a 2021-06-22 stsp #include <sys/types.h>
18 05118f5a 2021-06-22 stsp #include <sys/queue.h>
19 05118f5a 2021-06-22 stsp #include <sys/uio.h>
20 05118f5a 2021-06-22 stsp #include <sys/stat.h>
21 05118f5a 2021-06-22 stsp #include <sys/socket.h>
22 05118f5a 2021-06-22 stsp #include <sys/wait.h>
23 05118f5a 2021-06-22 stsp
24 05118f5a 2021-06-22 stsp #include <dirent.h>
25 c8d1f14f 2021-06-23 naddy #include <endian.h>
26 05118f5a 2021-06-22 stsp #include <errno.h>
27 05118f5a 2021-06-22 stsp #include <fcntl.h>
28 05118f5a 2021-06-22 stsp #include <stdint.h>
29 05118f5a 2021-06-22 stsp #include <sha1.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 #include <imsg.h>
36 05118f5a 2021-06-22 stsp
37 05118f5a 2021-06-22 stsp #include "got_error.h"
38 05118f5a 2021-06-22 stsp #include "got_cancel.h"
39 05118f5a 2021-06-22 stsp #include "got_object.h"
40 05118f5a 2021-06-22 stsp #include "got_reference.h"
41 05118f5a 2021-06-22 stsp #include "got_repository.h"
42 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
43 05118f5a 2021-06-22 stsp #include "got_opentemp.h"
44 05118f5a 2021-06-22 stsp #include "got_path.h"
45 05118f5a 2021-06-22 stsp
46 05118f5a 2021-06-22 stsp #include "got_lib_delta.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_object.h"
48 b3d68e7f 2021-07-03 stsp #include "got_lib_object_idset.h"
49 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_pack.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
52 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
53 05118f5a 2021-06-22 stsp #include "got_lib_pack_create.h"
54 05118f5a 2021-06-22 stsp #include "got_lib_sha1.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 05118f5a 2021-06-22 stsp *ids = reallocarray(NULL, alloc_chunksz, sizeof(struct got_object_id *));
77 05118f5a 2021-06-22 stsp if (*ids == NULL)
78 05118f5a 2021-06-22 stsp return got_error_from_errno("reallocarray");
79 05118f5a 2021-06-22 stsp nalloc = alloc_chunksz;
80 05118f5a 2021-06-22 stsp
81 05118f5a 2021-06-22 stsp TAILQ_FOREACH(re, refs, entry) {
82 05118f5a 2021-06-22 stsp struct got_object_id *id;
83 05118f5a 2021-06-22 stsp
84 05118f5a 2021-06-22 stsp if (cancel_cb) {
85 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
86 05118f5a 2021-06-22 stsp if (err)
87 05118f5a 2021-06-22 stsp goto done;
88 05118f5a 2021-06-22 stsp }
89 05118f5a 2021-06-22 stsp
90 05118f5a 2021-06-22 stsp err = got_ref_resolve(&id, repo, re->ref);
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 if (wanted_obj_type_mask != GOT_OBJ_TYPE_ANY) {
95 05118f5a 2021-06-22 stsp int obj_type;
96 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
97 05118f5a 2021-06-22 stsp if (err)
98 05118f5a 2021-06-22 stsp goto done;
99 05118f5a 2021-06-22 stsp if ((wanted_obj_type_mask & (1 << obj_type)) == 0) {
100 05118f5a 2021-06-22 stsp free(id);
101 05118f5a 2021-06-22 stsp id = NULL;
102 05118f5a 2021-06-22 stsp continue;
103 05118f5a 2021-06-22 stsp }
104 05118f5a 2021-06-22 stsp }
105 05118f5a 2021-06-22 stsp
106 ae23ce34 2021-07-01 stsp if (nalloc <= *nobjects) {
107 05118f5a 2021-06-22 stsp struct got_object_id **new;
108 05118f5a 2021-06-22 stsp new = recallocarray(*ids, nalloc,
109 05118f5a 2021-06-22 stsp nalloc + alloc_chunksz,
110 05118f5a 2021-06-22 stsp sizeof(struct got_object_id *));
111 05118f5a 2021-06-22 stsp if (new == NULL) {
112 05118f5a 2021-06-22 stsp err = got_error_from_errno(
113 05118f5a 2021-06-22 stsp "recallocarray");
114 05118f5a 2021-06-22 stsp goto done;
115 05118f5a 2021-06-22 stsp }
116 05118f5a 2021-06-22 stsp *ids = new;
117 05118f5a 2021-06-22 stsp nalloc += alloc_chunksz;
118 05118f5a 2021-06-22 stsp }
119 05118f5a 2021-06-22 stsp (*ids)[*nobjects] = id;
120 05118f5a 2021-06-22 stsp if ((*ids)[*nobjects] == NULL) {
121 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
122 05118f5a 2021-06-22 stsp goto done;
123 05118f5a 2021-06-22 stsp }
124 05118f5a 2021-06-22 stsp (*nobjects)++;
125 05118f5a 2021-06-22 stsp }
126 05118f5a 2021-06-22 stsp done:
127 05118f5a 2021-06-22 stsp if (err) {
128 05118f5a 2021-06-22 stsp for (i = 0; i < *nobjects; i++)
129 05118f5a 2021-06-22 stsp free((*ids)[i]);
130 05118f5a 2021-06-22 stsp free(*ids);
131 05118f5a 2021-06-22 stsp *ids = NULL;
132 05118f5a 2021-06-22 stsp *nobjects = 0;
133 05118f5a 2021-06-22 stsp }
134 05118f5a 2021-06-22 stsp return err;
135 05118f5a 2021-06-22 stsp }
136 05118f5a 2021-06-22 stsp
137 05118f5a 2021-06-22 stsp const struct got_error *
138 05118f5a 2021-06-22 stsp got_repo_pack_objects(FILE **packfile, struct got_object_id **pack_hash,
139 05118f5a 2021-06-22 stsp struct got_reflist_head *include_refs,
140 05118f5a 2021-06-22 stsp struct got_reflist_head *exclude_refs, struct got_repository *repo,
141 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
142 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
143 05118f5a 2021-06-22 stsp {
144 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
145 05118f5a 2021-06-22 stsp struct got_object_id **ours = NULL, **theirs = NULL;
146 05118f5a 2021-06-22 stsp int nours = 0, ntheirs = 0, packfd = -1, i;
147 05118f5a 2021-06-22 stsp char *tmpfile_path = NULL, *path = NULL, *packfile_path = NULL;
148 05118f5a 2021-06-22 stsp char *sha1_str = NULL;
149 05118f5a 2021-06-22 stsp
150 05118f5a 2021-06-22 stsp *packfile = NULL;
151 05118f5a 2021-06-22 stsp *pack_hash = NULL;
152 05118f5a 2021-06-22 stsp
153 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/packing.pack",
154 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR) == -1) {
155 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
156 05118f5a 2021-06-22 stsp goto done;
157 05118f5a 2021-06-22 stsp }
158 05118f5a 2021-06-22 stsp err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
159 05118f5a 2021-06-22 stsp if (err)
160 05118f5a 2021-06-22 stsp goto done;
161 05118f5a 2021-06-22 stsp
162 05118f5a 2021-06-22 stsp if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
163 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpfile_path);
164 05118f5a 2021-06-22 stsp goto done;
165 05118f5a 2021-06-22 stsp }
166 05118f5a 2021-06-22 stsp
167 05118f5a 2021-06-22 stsp *packfile = fdopen(packfd, "w");
168 05118f5a 2021-06-22 stsp if (*packfile == NULL) {
169 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fdopen", tmpfile_path);
170 05118f5a 2021-06-22 stsp goto done;
171 05118f5a 2021-06-22 stsp }
172 05118f5a 2021-06-22 stsp packfd = -1;
173 05118f5a 2021-06-22 stsp
174 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&ours, &nours,
175 05118f5a 2021-06-22 stsp (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
176 05118f5a 2021-06-22 stsp include_refs, repo, cancel_cb, cancel_arg);
177 05118f5a 2021-06-22 stsp if (err)
178 05118f5a 2021-06-22 stsp goto done;
179 05118f5a 2021-06-22 stsp
180 05118f5a 2021-06-22 stsp if (nours == 0) {
181 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
182 05118f5a 2021-06-22 stsp goto done;
183 05118f5a 2021-06-22 stsp }
184 05118f5a 2021-06-22 stsp
185 05118f5a 2021-06-22 stsp if (!TAILQ_EMPTY(exclude_refs)) {
186 05118f5a 2021-06-22 stsp err = get_reflist_object_ids(&theirs, &ntheirs,
187 05118f5a 2021-06-22 stsp (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
188 05118f5a 2021-06-22 stsp exclude_refs, repo,
189 05118f5a 2021-06-22 stsp cancel_cb, cancel_arg);
190 05118f5a 2021-06-22 stsp if (err)
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 *pack_hash = calloc(1, sizeof(**pack_hash));
195 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
196 05118f5a 2021-06-22 stsp err = got_error_from_errno("calloc");
197 05118f5a 2021-06-22 stsp goto done;
198 05118f5a 2021-06-22 stsp }
199 05118f5a 2021-06-22 stsp
200 05118f5a 2021-06-22 stsp err = got_pack_create((*pack_hash)->sha1, *packfile, theirs, ntheirs,
201 05118f5a 2021-06-22 stsp ours, nours, repo, loose_obj_only, progress_cb, progress_arg,
202 05118f5a 2021-06-22 stsp cancel_cb, cancel_arg);
203 05118f5a 2021-06-22 stsp if (err)
204 05118f5a 2021-06-22 stsp goto done;
205 05118f5a 2021-06-22 stsp
206 05118f5a 2021-06-22 stsp err = got_object_id_str(&sha1_str, *pack_hash);
207 05118f5a 2021-06-22 stsp if (err)
208 05118f5a 2021-06-22 stsp goto done;
209 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
210 05118f5a 2021-06-22 stsp got_repo_get_path_git_dir(repo), GOT_OBJECTS_PACK_DIR,
211 05118f5a 2021-06-22 stsp sha1_str) == -1) {
212 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
213 05118f5a 2021-06-22 stsp goto done;
214 05118f5a 2021-06-22 stsp }
215 05118f5a 2021-06-22 stsp
216 05118f5a 2021-06-22 stsp if (fflush(*packfile) == -1) {
217 05118f5a 2021-06-22 stsp err = got_error_from_errno("fflush");
218 05118f5a 2021-06-22 stsp goto done;
219 05118f5a 2021-06-22 stsp }
220 05118f5a 2021-06-22 stsp if (fseek(*packfile, 0L, SEEK_SET) == -1) {
221 05118f5a 2021-06-22 stsp err = got_error_from_errno("fseek");
222 05118f5a 2021-06-22 stsp goto done;
223 05118f5a 2021-06-22 stsp }
224 05118f5a 2021-06-22 stsp if (rename(tmpfile_path, packfile_path) == -1) {
225 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpfile_path,
226 05118f5a 2021-06-22 stsp packfile_path);
227 05118f5a 2021-06-22 stsp goto done;
228 05118f5a 2021-06-22 stsp }
229 05118f5a 2021-06-22 stsp free(tmpfile_path);
230 05118f5a 2021-06-22 stsp tmpfile_path = NULL;
231 05118f5a 2021-06-22 stsp done:
232 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++)
233 05118f5a 2021-06-22 stsp free(ours[i]);
234 05118f5a 2021-06-22 stsp free(ours);
235 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++)
236 05118f5a 2021-06-22 stsp free(theirs[i]);
237 05118f5a 2021-06-22 stsp free(theirs);
238 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
239 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
240 05118f5a 2021-06-22 stsp if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)
241 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpfile_path);
242 05118f5a 2021-06-22 stsp free(tmpfile_path);
243 05118f5a 2021-06-22 stsp free(packfile_path);
244 05118f5a 2021-06-22 stsp free(sha1_str);
245 05118f5a 2021-06-22 stsp free(path);
246 05118f5a 2021-06-22 stsp if (err) {
247 05118f5a 2021-06-22 stsp free(*pack_hash);
248 05118f5a 2021-06-22 stsp *pack_hash = NULL;
249 05118f5a 2021-06-22 stsp if (*packfile)
250 05118f5a 2021-06-22 stsp fclose(*packfile);
251 05118f5a 2021-06-22 stsp *packfile = NULL;
252 05118f5a 2021-06-22 stsp }
253 05118f5a 2021-06-22 stsp return err;
254 05118f5a 2021-06-22 stsp }
255 05118f5a 2021-06-22 stsp
256 05118f5a 2021-06-22 stsp const struct got_error *
257 05118f5a 2021-06-22 stsp got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
258 05118f5a 2021-06-22 stsp struct got_repository *repo,
259 05118f5a 2021-06-22 stsp got_pack_index_progress_cb progress_cb, void *progress_arg,
260 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
261 05118f5a 2021-06-22 stsp {
262 05118f5a 2021-06-22 stsp size_t i;
263 05118f5a 2021-06-22 stsp char *path;
264 05118f5a 2021-06-22 stsp int imsg_idxfds[2];
265 05118f5a 2021-06-22 stsp int npackfd = -1, idxfd = -1, nidxfd = -1;
266 05118f5a 2021-06-22 stsp int tmpfds[3];
267 05118f5a 2021-06-22 stsp int idxstatus, done = 0;
268 05118f5a 2021-06-22 stsp const struct got_error *err;
269 05118f5a 2021-06-22 stsp struct imsgbuf idxibuf;
270 05118f5a 2021-06-22 stsp pid_t idxpid;
271 05118f5a 2021-06-22 stsp char *tmpidxpath = NULL;
272 05118f5a 2021-06-22 stsp char *packfile_path = NULL, *idxpath = NULL, *id_str = NULL;
273 05118f5a 2021-06-22 stsp const char *repo_path = got_repo_get_path_git_dir(repo);
274 05118f5a 2021-06-22 stsp struct stat sb;
275 05118f5a 2021-06-22 stsp
276 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++)
277 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
278 05118f5a 2021-06-22 stsp
279 05118f5a 2021-06-22 stsp if (asprintf(&path, "%s/%s/indexing.idx",
280 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR) == -1) {
281 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
282 05118f5a 2021-06-22 stsp goto done;
283 05118f5a 2021-06-22 stsp }
284 05118f5a 2021-06-22 stsp err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
285 05118f5a 2021-06-22 stsp free(path);
286 05118f5a 2021-06-22 stsp if (err)
287 05118f5a 2021-06-22 stsp goto done;
288 05118f5a 2021-06-22 stsp if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
289 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fchmod", tmpidxpath);
290 05118f5a 2021-06-22 stsp goto done;
291 05118f5a 2021-06-22 stsp }
292 05118f5a 2021-06-22 stsp
293 05118f5a 2021-06-22 stsp nidxfd = dup(idxfd);
294 05118f5a 2021-06-22 stsp if (nidxfd == -1) {
295 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
296 05118f5a 2021-06-22 stsp goto done;
297 05118f5a 2021-06-22 stsp }
298 05118f5a 2021-06-22 stsp
299 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
300 05118f5a 2021-06-22 stsp tmpfds[i] = got_opentempfd();
301 05118f5a 2021-06-22 stsp if (tmpfds[i] == -1) {
302 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_opentempfd");
303 05118f5a 2021-06-22 stsp goto done;
304 05118f5a 2021-06-22 stsp }
305 05118f5a 2021-06-22 stsp }
306 05118f5a 2021-06-22 stsp
307 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
308 05118f5a 2021-06-22 stsp if (err)
309 05118f5a 2021-06-22 stsp goto done;
310 05118f5a 2021-06-22 stsp
311 05118f5a 2021-06-22 stsp if (asprintf(&packfile_path, "%s/%s/pack-%s.pack",
312 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
313 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
314 05118f5a 2021-06-22 stsp goto done;
315 05118f5a 2021-06-22 stsp }
316 05118f5a 2021-06-22 stsp
317 05118f5a 2021-06-22 stsp if (fstat(fileno(packfile), &sb) == -1) {
318 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fstat", packfile_path);
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 if (asprintf(&idxpath, "%s/%s/pack-%s.idx",
323 05118f5a 2021-06-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, id_str) == -1) {
324 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
325 05118f5a 2021-06-22 stsp goto done;
326 05118f5a 2021-06-22 stsp }
327 05118f5a 2021-06-22 stsp
328 05118f5a 2021-06-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_idxfds) == -1) {
329 05118f5a 2021-06-22 stsp err = got_error_from_errno("socketpair");
330 05118f5a 2021-06-22 stsp goto done;
331 05118f5a 2021-06-22 stsp }
332 05118f5a 2021-06-22 stsp idxpid = fork();
333 05118f5a 2021-06-22 stsp if (idxpid == -1) {
334 05118f5a 2021-06-22 stsp err= got_error_from_errno("fork");
335 05118f5a 2021-06-22 stsp goto done;
336 05118f5a 2021-06-22 stsp } else if (idxpid == 0)
337 05118f5a 2021-06-22 stsp got_privsep_exec_child(imsg_idxfds,
338 05118f5a 2021-06-22 stsp GOT_PATH_PROG_INDEX_PACK, packfile_path);
339 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[1]) == -1) {
340 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
341 05118f5a 2021-06-22 stsp goto done;
342 05118f5a 2021-06-22 stsp }
343 05118f5a 2021-06-22 stsp imsg_init(&idxibuf, imsg_idxfds[0]);
344 05118f5a 2021-06-22 stsp
345 05118f5a 2021-06-22 stsp npackfd = dup(fileno(packfile));
346 05118f5a 2021-06-22 stsp if (npackfd == -1) {
347 05118f5a 2021-06-22 stsp err = got_error_from_errno("dup");
348 05118f5a 2021-06-22 stsp goto done;
349 05118f5a 2021-06-22 stsp }
350 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_req(&idxibuf, pack_hash->sha1,
351 05118f5a 2021-06-22 stsp npackfd);
352 05118f5a 2021-06-22 stsp if (err != NULL)
353 05118f5a 2021-06-22 stsp goto done;
354 05118f5a 2021-06-22 stsp npackfd = -1;
355 05118f5a 2021-06-22 stsp err = got_privsep_send_index_pack_outfd(&idxibuf, nidxfd);
356 05118f5a 2021-06-22 stsp if (err != NULL)
357 05118f5a 2021-06-22 stsp goto done;
358 05118f5a 2021-06-22 stsp nidxfd = -1;
359 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
360 05118f5a 2021-06-22 stsp err = got_privsep_send_tmpfd(&idxibuf, tmpfds[i]);
361 05118f5a 2021-06-22 stsp if (err != NULL)
362 05118f5a 2021-06-22 stsp goto done;
363 05118f5a 2021-06-22 stsp tmpfds[i] = -1;
364 05118f5a 2021-06-22 stsp }
365 05118f5a 2021-06-22 stsp done = 0;
366 05118f5a 2021-06-22 stsp while (!done) {
367 05118f5a 2021-06-22 stsp int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
368 05118f5a 2021-06-22 stsp
369 05118f5a 2021-06-22 stsp if (cancel_cb) {
370 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
371 05118f5a 2021-06-22 stsp if (err)
372 05118f5a 2021-06-22 stsp goto done;
373 05118f5a 2021-06-22 stsp }
374 05118f5a 2021-06-22 stsp
375 05118f5a 2021-06-22 stsp err = got_privsep_recv_index_progress(&done, &nobj_total,
376 05118f5a 2021-06-22 stsp &nobj_indexed, &nobj_loose, &nobj_resolved,
377 05118f5a 2021-06-22 stsp &idxibuf);
378 05118f5a 2021-06-22 stsp if (err != NULL)
379 05118f5a 2021-06-22 stsp goto done;
380 05118f5a 2021-06-22 stsp if (nobj_indexed != 0) {
381 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, sb.st_size,
382 05118f5a 2021-06-22 stsp nobj_total, nobj_indexed, nobj_loose,
383 05118f5a 2021-06-22 stsp nobj_resolved);
384 05118f5a 2021-06-22 stsp if (err)
385 05118f5a 2021-06-22 stsp break;
386 05118f5a 2021-06-22 stsp }
387 05118f5a 2021-06-22 stsp imsg_clear(&idxibuf);
388 05118f5a 2021-06-22 stsp }
389 05118f5a 2021-06-22 stsp if (close(imsg_idxfds[0]) == -1) {
390 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
391 05118f5a 2021-06-22 stsp goto done;
392 05118f5a 2021-06-22 stsp }
393 05118f5a 2021-06-22 stsp if (waitpid(idxpid, &idxstatus, 0) == -1) {
394 05118f5a 2021-06-22 stsp err = got_error_from_errno("waitpid");
395 05118f5a 2021-06-22 stsp goto done;
396 05118f5a 2021-06-22 stsp }
397 05118f5a 2021-06-22 stsp
398 05118f5a 2021-06-22 stsp if (rename(tmpidxpath, idxpath) == -1) {
399 05118f5a 2021-06-22 stsp err = got_error_from_errno3("rename", tmpidxpath, idxpath);
400 05118f5a 2021-06-22 stsp goto done;
401 05118f5a 2021-06-22 stsp }
402 05118f5a 2021-06-22 stsp free(tmpidxpath);
403 05118f5a 2021-06-22 stsp tmpidxpath = NULL;
404 05118f5a 2021-06-22 stsp
405 05118f5a 2021-06-22 stsp done:
406 05118f5a 2021-06-22 stsp if (tmpidxpath && unlink(tmpidxpath) == -1 && err == NULL)
407 05118f5a 2021-06-22 stsp err = got_error_from_errno2("unlink", tmpidxpath);
408 05118f5a 2021-06-22 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
409 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
410 05118f5a 2021-06-22 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
411 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
412 05118f5a 2021-06-22 stsp for (i = 0; i < nitems(tmpfds); i++) {
413 05118f5a 2021-06-22 stsp if (tmpfds[i] != -1 && close(tmpfds[i]) == -1 && err == NULL)
414 05118f5a 2021-06-22 stsp err = got_error_from_errno("close");
415 05118f5a 2021-06-22 stsp }
416 05118f5a 2021-06-22 stsp free(tmpidxpath);
417 05118f5a 2021-06-22 stsp free(idxpath);
418 05118f5a 2021-06-22 stsp free(packfile_path);
419 05118f5a 2021-06-22 stsp return err;
420 05118f5a 2021-06-22 stsp }
421 05118f5a 2021-06-22 stsp
422 05118f5a 2021-06-22 stsp const struct got_error *
423 05118f5a 2021-06-22 stsp got_repo_find_pack(FILE **packfile, struct got_object_id **pack_hash,
424 05118f5a 2021-06-22 stsp struct got_repository *repo, const char *packfile_path)
425 05118f5a 2021-06-22 stsp {
426 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
427 05118f5a 2021-06-22 stsp const char *packdir_path = NULL;
428 05118f5a 2021-06-22 stsp char *packfile_name = NULL, *p, *dot;
429 05118f5a 2021-06-22 stsp struct got_object_id id;
430 05118f5a 2021-06-22 stsp int packfd = -1;
431 05118f5a 2021-06-22 stsp
432 05118f5a 2021-06-22 stsp *packfile = NULL;
433 05118f5a 2021-06-22 stsp *pack_hash = NULL;
434 05118f5a 2021-06-22 stsp
435 05118f5a 2021-06-22 stsp packdir_path = got_repo_get_path_objects_pack(repo);
436 05118f5a 2021-06-22 stsp if (packdir_path == NULL)
437 05118f5a 2021-06-22 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
438 05118f5a 2021-06-22 stsp
439 05118f5a 2021-06-22 stsp if (!got_path_is_child(packfile_path, packdir_path,
440 05118f5a 2021-06-22 stsp strlen(packdir_path))) {
441 05118f5a 2021-06-22 stsp err = got_error_path(packfile_path, GOT_ERR_BAD_PATH);
442 05118f5a 2021-06-22 stsp goto done;
443 05118f5a 2021-06-22 stsp
444 05118f5a 2021-06-22 stsp }
445 05118f5a 2021-06-22 stsp
446 05118f5a 2021-06-22 stsp err = got_path_basename(&packfile_name, packfile_path);
447 05118f5a 2021-06-22 stsp if (err)
448 05118f5a 2021-06-22 stsp goto done;
449 05118f5a 2021-06-22 stsp p = packfile_name;
450 05118f5a 2021-06-22 stsp
451 05118f5a 2021-06-22 stsp if (strncmp(p, "pack-", 5) != 0) {
452 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
453 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
454 05118f5a 2021-06-22 stsp packfile_name);
455 05118f5a 2021-06-22 stsp goto done;
456 05118f5a 2021-06-22 stsp }
457 05118f5a 2021-06-22 stsp p += 5;
458 05118f5a 2021-06-22 stsp dot = strchr(p, '.');
459 05118f5a 2021-06-22 stsp if (dot == NULL) {
460 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
461 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
462 05118f5a 2021-06-22 stsp packfile_name);
463 05118f5a 2021-06-22 stsp goto done;
464 05118f5a 2021-06-22 stsp }
465 05118f5a 2021-06-22 stsp if (strcmp(dot + 1, "pack") != 0) {
466 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
467 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
468 05118f5a 2021-06-22 stsp packfile_name);
469 05118f5a 2021-06-22 stsp goto done;
470 05118f5a 2021-06-22 stsp }
471 05118f5a 2021-06-22 stsp *dot = '\0';
472 05118f5a 2021-06-22 stsp if (!got_parse_sha1_digest(id.sha1, p)) {
473 05118f5a 2021-06-22 stsp err = got_error_fmt(GOT_ERR_BAD_PATH,
474 05118f5a 2021-06-22 stsp "'%s' is not a valid pack file name",
475 05118f5a 2021-06-22 stsp packfile_name);
476 05118f5a 2021-06-22 stsp goto done;
477 05118f5a 2021-06-22 stsp }
478 05118f5a 2021-06-22 stsp
479 05118f5a 2021-06-22 stsp *pack_hash = got_object_id_dup(&id);
480 05118f5a 2021-06-22 stsp if (*pack_hash == NULL) {
481 05118f5a 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
482 05118f5a 2021-06-22 stsp goto done;
483 05118f5a 2021-06-22 stsp }
484 05118f5a 2021-06-22 stsp
485 05118f5a 2021-06-22 stsp packfd = open(packfile_path, O_RDONLY | O_NOFOLLOW);
486 05118f5a 2021-06-22 stsp if (packfd == -1) {
487 05118f5a 2021-06-22 stsp err = got_error_from_errno2("open", packfile_path);
488 05118f5a 2021-06-22 stsp goto done;
489 05118f5a 2021-06-22 stsp }
490 05118f5a 2021-06-22 stsp
491 05118f5a 2021-06-22 stsp *packfile = fdopen(packfd, "r");
492 05118f5a 2021-06-22 stsp if (*packfile == NULL) {
493 05118f5a 2021-06-22 stsp err = got_error_from_errno2("fdopen", packfile_path);
494 05118f5a 2021-06-22 stsp goto done;
495 05118f5a 2021-06-22 stsp }
496 05118f5a 2021-06-22 stsp packfd = -1;
497 05118f5a 2021-06-22 stsp done:
498 05118f5a 2021-06-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
499 05118f5a 2021-06-22 stsp err = got_error_from_errno2("close", packfile_path);
500 05118f5a 2021-06-22 stsp free(packfile_name);
501 05118f5a 2021-06-22 stsp if (err) {
502 05118f5a 2021-06-22 stsp free(*pack_hash);
503 05118f5a 2021-06-22 stsp *pack_hash = NULL;
504 05118f5a 2021-06-22 stsp }
505 05118f5a 2021-06-22 stsp return err;
506 05118f5a 2021-06-22 stsp }
507 05118f5a 2021-06-22 stsp
508 05118f5a 2021-06-22 stsp const struct got_error *
509 05118f5a 2021-06-22 stsp got_repo_list_pack(FILE *packfile, struct got_object_id *pack_hash,
510 05118f5a 2021-06-22 stsp struct got_repository *repo, got_pack_list_cb list_cb, void *list_arg,
511 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
512 05118f5a 2021-06-22 stsp {
513 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
514 05118f5a 2021-06-22 stsp char *id_str = NULL, *idxpath = NULL, *packpath = NULL;
515 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
516 05118f5a 2021-06-22 stsp struct got_pack *pack = NULL;
517 05118f5a 2021-06-22 stsp uint32_t nobj, i;
518 05118f5a 2021-06-22 stsp
519 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, pack_hash);
520 05118f5a 2021-06-22 stsp if (err)
521 05118f5a 2021-06-22 stsp goto done;
522 05118f5a 2021-06-22 stsp
523 05118f5a 2021-06-22 stsp if (asprintf(&packpath, "%s/pack-%s.pack",
524 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
525 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
526 05118f5a 2021-06-22 stsp goto done;
527 05118f5a 2021-06-22 stsp }
528 05118f5a 2021-06-22 stsp if (asprintf(&idxpath, "%s/pack-%s.idx",
529 05118f5a 2021-06-22 stsp GOT_OBJECTS_PACK_DIR, id_str) == -1) {
530 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
531 05118f5a 2021-06-22 stsp goto done;
532 05118f5a 2021-06-22 stsp }
533 05118f5a 2021-06-22 stsp
534 05118f5a 2021-06-22 stsp err = got_packidx_open(&packidx, got_repo_get_fd(repo), idxpath, 1);
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 err = got_repo_cache_pack(&pack, repo, packpath, packidx);
539 05118f5a 2021-06-22 stsp if (err)
540 05118f5a 2021-06-22 stsp goto done;
541 05118f5a 2021-06-22 stsp
542 05118f5a 2021-06-22 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
543 05118f5a 2021-06-22 stsp for (i = 0; i < nobj; i++) {
544 05118f5a 2021-06-22 stsp struct got_packidx_object_id *oid;
545 05118f5a 2021-06-22 stsp struct got_object_id id, base_id;
546 05118f5a 2021-06-22 stsp off_t offset, base_offset = 0;
547 05118f5a 2021-06-22 stsp uint8_t type;
548 05118f5a 2021-06-22 stsp uint64_t size;
549 05118f5a 2021-06-22 stsp size_t tslen, len;
550 05118f5a 2021-06-22 stsp
551 05118f5a 2021-06-22 stsp if (cancel_cb) {
552 05118f5a 2021-06-22 stsp err = cancel_cb(cancel_arg);
553 05118f5a 2021-06-22 stsp if (err)
554 05118f5a 2021-06-22 stsp break;
555 05118f5a 2021-06-22 stsp }
556 05118f5a 2021-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
557 05118f5a 2021-06-22 stsp memcpy(id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
558 05118f5a 2021-06-22 stsp
559 05118f5a 2021-06-22 stsp offset = got_packidx_get_object_offset(packidx, i);
560 05118f5a 2021-06-22 stsp if (offset == -1) {
561 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
562 05118f5a 2021-06-22 stsp goto done;
563 05118f5a 2021-06-22 stsp }
564 05118f5a 2021-06-22 stsp
565 05118f5a 2021-06-22 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
566 05118f5a 2021-06-22 stsp pack, offset);
567 05118f5a 2021-06-22 stsp if (err)
568 05118f5a 2021-06-22 stsp goto done;
569 05118f5a 2021-06-22 stsp
570 05118f5a 2021-06-22 stsp switch (type) {
571 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
572 05118f5a 2021-06-22 stsp err = got_pack_parse_offset_delta(&base_offset, &len,
573 05118f5a 2021-06-22 stsp pack, offset, tslen);
574 05118f5a 2021-06-22 stsp if (err)
575 05118f5a 2021-06-22 stsp goto done;
576 05118f5a 2021-06-22 stsp break;
577 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
578 05118f5a 2021-06-22 stsp err = got_pack_parse_ref_delta(&base_id,
579 05118f5a 2021-06-22 stsp pack, offset, tslen);
580 05118f5a 2021-06-22 stsp if (err)
581 05118f5a 2021-06-22 stsp goto done;
582 05118f5a 2021-06-22 stsp break;
583 05118f5a 2021-06-22 stsp }
584 05118f5a 2021-06-22 stsp err = (*list_cb)(list_arg, &id, type, offset, size,
585 05118f5a 2021-06-22 stsp base_offset, &base_id);
586 05118f5a 2021-06-22 stsp if (err)
587 05118f5a 2021-06-22 stsp goto done;
588 05118f5a 2021-06-22 stsp }
589 05118f5a 2021-06-22 stsp
590 05118f5a 2021-06-22 stsp done:
591 05118f5a 2021-06-22 stsp free(id_str);
592 05118f5a 2021-06-22 stsp free(idxpath);
593 05118f5a 2021-06-22 stsp free(packpath);
594 05118f5a 2021-06-22 stsp if (packidx)
595 05118f5a 2021-06-22 stsp got_packidx_close(packidx);
596 b3d68e7f 2021-07-03 stsp return err;
597 b3d68e7f 2021-07-03 stsp }
598 b3d68e7f 2021-07-03 stsp
599 b3d68e7f 2021-07-03 stsp static const struct got_error *
600 b3d68e7f 2021-07-03 stsp get_loose_object_ids(struct got_object_idset **loose_ids, off_t *ondisk_size,
601 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb, void *progress_arg,
602 b3d68e7f 2021-07-03 stsp struct got_repository *repo)
603 b3d68e7f 2021-07-03 stsp {
604 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
605 b3d68e7f 2021-07-03 stsp char *path_objects = NULL, *path = NULL;
606 b3d68e7f 2021-07-03 stsp DIR *dir = NULL;
607 b3d68e7f 2021-07-03 stsp struct got_object *obj = NULL;
608 b3d68e7f 2021-07-03 stsp struct got_object_id id;
609 b3d68e7f 2021-07-03 stsp int i, fd = -1;
610 b3d68e7f 2021-07-03 stsp struct stat sb;
611 b3d68e7f 2021-07-03 stsp
612 b3d68e7f 2021-07-03 stsp *ondisk_size = 0;
613 b3d68e7f 2021-07-03 stsp *loose_ids = got_object_idset_alloc();
614 b3d68e7f 2021-07-03 stsp if (*loose_ids == NULL)
615 b3d68e7f 2021-07-03 stsp return got_error_from_errno("got_object_idset_alloc");
616 b3d68e7f 2021-07-03 stsp
617 b3d68e7f 2021-07-03 stsp path_objects = got_repo_get_path_objects(repo);
618 b3d68e7f 2021-07-03 stsp if (path_objects == NULL) {
619 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("got_repo_get_path_objects");
620 b3d68e7f 2021-07-03 stsp goto done;
621 b3d68e7f 2021-07-03 stsp }
622 b3d68e7f 2021-07-03 stsp
623 b3d68e7f 2021-07-03 stsp for (i = 0; i <= 0xff; i++) {
624 b3d68e7f 2021-07-03 stsp struct dirent *dent;
625 b3d68e7f 2021-07-03 stsp
626 b3d68e7f 2021-07-03 stsp if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
627 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
628 b3d68e7f 2021-07-03 stsp break;
629 b3d68e7f 2021-07-03 stsp }
630 b3d68e7f 2021-07-03 stsp
631 b3d68e7f 2021-07-03 stsp dir = opendir(path);
632 b3d68e7f 2021-07-03 stsp if (dir == NULL) {
633 b3d68e7f 2021-07-03 stsp if (errno == ENOENT) {
634 b3d68e7f 2021-07-03 stsp err = NULL;
635 b3d68e7f 2021-07-03 stsp continue;
636 b3d68e7f 2021-07-03 stsp }
637 b3d68e7f 2021-07-03 stsp err = got_error_from_errno2("opendir", path);
638 b3d68e7f 2021-07-03 stsp break;
639 b3d68e7f 2021-07-03 stsp }
640 b3d68e7f 2021-07-03 stsp
641 b3d68e7f 2021-07-03 stsp while ((dent = readdir(dir)) != NULL) {
642 b3d68e7f 2021-07-03 stsp char *id_str;
643 b3d68e7f 2021-07-03 stsp
644 b3d68e7f 2021-07-03 stsp if (strcmp(dent->d_name, ".") == 0 ||
645 b3d68e7f 2021-07-03 stsp strcmp(dent->d_name, "..") == 0)
646 b3d68e7f 2021-07-03 stsp continue;
647 b3d68e7f 2021-07-03 stsp
648 b3d68e7f 2021-07-03 stsp if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
649 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
650 b3d68e7f 2021-07-03 stsp goto done;
651 b3d68e7f 2021-07-03 stsp }
652 b3d68e7f 2021-07-03 stsp
653 b3d68e7f 2021-07-03 stsp memset(&id, 0, sizeof(id));
654 b3d68e7f 2021-07-03 stsp if (!got_parse_sha1_digest(id.sha1, id_str)) {
655 b3d68e7f 2021-07-03 stsp free(id_str);
656 b3d68e7f 2021-07-03 stsp continue;
657 b3d68e7f 2021-07-03 stsp }
658 b3d68e7f 2021-07-03 stsp free(id_str);
659 b3d68e7f 2021-07-03 stsp
660 b3d68e7f 2021-07-03 stsp err = got_object_open_loose_fd(&fd, &id, repo);
661 b3d68e7f 2021-07-03 stsp if (err)
662 b3d68e7f 2021-07-03 stsp goto done;
663 b3d68e7f 2021-07-03 stsp if (fstat(fd, &sb) == -1) {
664 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("fstat");
665 b3d68e7f 2021-07-03 stsp goto done;
666 b3d68e7f 2021-07-03 stsp }
667 b3d68e7f 2021-07-03 stsp err = got_object_read_header_privsep(&obj, repo, fd);
668 b3d68e7f 2021-07-03 stsp if (err)
669 b3d68e7f 2021-07-03 stsp goto done;
670 b3d68e7f 2021-07-03 stsp fd = -1; /* already closed */
671 b3d68e7f 2021-07-03 stsp
672 b3d68e7f 2021-07-03 stsp switch (obj->type) {
673 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
674 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TREE:
675 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_BLOB:
676 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TAG:
677 b3d68e7f 2021-07-03 stsp break;
678 b3d68e7f 2021-07-03 stsp default:
679 b3d68e7f 2021-07-03 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
680 b3d68e7f 2021-07-03 stsp "%d", obj->type);
681 b3d68e7f 2021-07-03 stsp goto done;
682 b3d68e7f 2021-07-03 stsp }
683 b3d68e7f 2021-07-03 stsp got_object_close(obj);
684 b3d68e7f 2021-07-03 stsp obj = NULL;
685 b3d68e7f 2021-07-03 stsp (*ondisk_size) += sb.st_size;
686 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(*loose_ids, &id, NULL);
687 b3d68e7f 2021-07-03 stsp if (err)
688 b3d68e7f 2021-07-03 stsp goto done;
689 b3d68e7f 2021-07-03 stsp if (progress_cb) {
690 b3d68e7f 2021-07-03 stsp err = progress_cb(progress_arg,
691 b3d68e7f 2021-07-03 stsp got_object_idset_num_elements(*loose_ids),
692 b3d68e7f 2021-07-03 stsp -1, -1);
693 b3d68e7f 2021-07-03 stsp if (err)
694 b3d68e7f 2021-07-03 stsp goto done;
695 b3d68e7f 2021-07-03 stsp }
696 b3d68e7f 2021-07-03 stsp }
697 b3d68e7f 2021-07-03 stsp
698 b3d68e7f 2021-07-03 stsp if (closedir(dir) != 0) {
699 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("closedir");
700 b3d68e7f 2021-07-03 stsp goto done;
701 b3d68e7f 2021-07-03 stsp }
702 b3d68e7f 2021-07-03 stsp dir = NULL;
703 b3d68e7f 2021-07-03 stsp
704 b3d68e7f 2021-07-03 stsp free(path);
705 b3d68e7f 2021-07-03 stsp path = NULL;
706 b3d68e7f 2021-07-03 stsp }
707 b3d68e7f 2021-07-03 stsp done:
708 b3d68e7f 2021-07-03 stsp if (dir && closedir(dir) != 0 && err == NULL)
709 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("closedir");
710 b3d68e7f 2021-07-03 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
711 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("close");
712 b3d68e7f 2021-07-03 stsp if (err) {
713 b3d68e7f 2021-07-03 stsp got_object_idset_free(*loose_ids);
714 b3d68e7f 2021-07-03 stsp *loose_ids = NULL;
715 b3d68e7f 2021-07-03 stsp }
716 b3d68e7f 2021-07-03 stsp if (obj)
717 b3d68e7f 2021-07-03 stsp got_object_close(obj);
718 b3d68e7f 2021-07-03 stsp free(path_objects);
719 b3d68e7f 2021-07-03 stsp free(path);
720 05118f5a 2021-06-22 stsp return err;
721 05118f5a 2021-06-22 stsp }
722 b3d68e7f 2021-07-03 stsp
723 b3d68e7f 2021-07-03 stsp static const struct got_error *
724 b3d68e7f 2021-07-03 stsp search_packidx(int *found, struct got_object_id *id,
725 b3d68e7f 2021-07-03 stsp struct got_repository *repo)
726 b3d68e7f 2021-07-03 stsp {
727 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
728 b3d68e7f 2021-07-03 stsp struct got_packidx *packidx = NULL;
729 b3d68e7f 2021-07-03 stsp int idx;
730 b3d68e7f 2021-07-03 stsp
731 b3d68e7f 2021-07-03 stsp *found = 0;
732 b3d68e7f 2021-07-03 stsp
733 b3d68e7f 2021-07-03 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
734 b3d68e7f 2021-07-03 stsp if (err == NULL)
735 b3d68e7f 2021-07-03 stsp *found = 1; /* object is already packed */
736 b3d68e7f 2021-07-03 stsp else if (err->code == GOT_ERR_NO_OBJ)
737 b3d68e7f 2021-07-03 stsp err = NULL;
738 b3d68e7f 2021-07-03 stsp return err;
739 b3d68e7f 2021-07-03 stsp }
740 b3d68e7f 2021-07-03 stsp
741 b3d68e7f 2021-07-03 stsp static const struct got_error *
742 b3d68e7f 2021-07-03 stsp preserve_loose_object(struct got_object_idset *loose_ids,
743 b3d68e7f 2021-07-03 stsp struct got_object_id *id, struct got_repository *repo, int *npacked)
744 b3d68e7f 2021-07-03 stsp {
745 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
746 b3d68e7f 2021-07-03 stsp int is_packed;
747 b3d68e7f 2021-07-03 stsp
748 b3d68e7f 2021-07-03 stsp if (!got_object_idset_contains(loose_ids, id))
749 b3d68e7f 2021-07-03 stsp return NULL;
750 b3d68e7f 2021-07-03 stsp
751 b3d68e7f 2021-07-03 stsp err = search_packidx(&is_packed, id, repo);
752 b3d68e7f 2021-07-03 stsp if (err)
753 b3d68e7f 2021-07-03 stsp return err;
754 b3d68e7f 2021-07-03 stsp if (is_packed) {
755 b3d68e7f 2021-07-03 stsp struct got_object *obj;
756 b3d68e7f 2021-07-03 stsp
757 b3d68e7f 2021-07-03 stsp /*
758 b3d68e7f 2021-07-03 stsp * Sanity check: Open the packed object to prevent a
759 b3d68e7f 2021-07-03 stsp * corrupt pack index from misleading us.
760 b3d68e7f 2021-07-03 stsp */
761 b3d68e7f 2021-07-03 stsp err = got_object_open_packed(&obj, id, repo);
762 b3d68e7f 2021-07-03 stsp if (err == NULL) {
763 b3d68e7f 2021-07-03 stsp got_object_close(obj);
764 b3d68e7f 2021-07-03 stsp /*
765 b3d68e7f 2021-07-03 stsp * The object is referenced and packed.
766 b3d68e7f 2021-07-03 stsp * We can purge the redundantly stored loose object.
767 b3d68e7f 2021-07-03 stsp */
768 b3d68e7f 2021-07-03 stsp (*npacked)++;
769 b3d68e7f 2021-07-03 stsp return NULL;
770 b3d68e7f 2021-07-03 stsp } else if (err->code != GOT_ERR_NO_OBJ)
771 b3d68e7f 2021-07-03 stsp return err;
772 b3d68e7f 2021-07-03 stsp }
773 b3d68e7f 2021-07-03 stsp
774 b3d68e7f 2021-07-03 stsp /*
775 b3d68e7f 2021-07-03 stsp * This object is referenced and not packed.
776 b3d68e7f 2021-07-03 stsp * Remove it from our purge set.
777 b3d68e7f 2021-07-03 stsp */
778 b3d68e7f 2021-07-03 stsp return got_object_idset_remove(NULL, loose_ids, id);
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 *loose_ids,
784 b3d68e7f 2021-07-03 stsp struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
785 b3d68e7f 2021-07-03 stsp const char *dpath, struct got_repository *repo, int *npacked,
786 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
787 b3d68e7f 2021-07-03 stsp {
788 b3d68e7f 2021-07-03 stsp const struct got_error *err;
789 b3d68e7f 2021-07-03 stsp struct got_tree_object *tree;
790 b3d68e7f 2021-07-03 stsp char *p = NULL;
791 b3d68e7f 2021-07-03 stsp int i;
792 b3d68e7f 2021-07-03 stsp
793 b3d68e7f 2021-07-03 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
794 b3d68e7f 2021-07-03 stsp if (err)
795 b3d68e7f 2021-07-03 stsp return err;
796 b3d68e7f 2021-07-03 stsp
797 b3d68e7f 2021-07-03 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
798 b3d68e7f 2021-07-03 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
799 b3d68e7f 2021-07-03 stsp struct got_object_id *id = got_tree_entry_get_id(e);
800 b3d68e7f 2021-07-03 stsp mode_t mode = got_tree_entry_get_mode(e);
801 b3d68e7f 2021-07-03 stsp
802 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
803 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
804 b3d68e7f 2021-07-03 stsp if (err)
805 b3d68e7f 2021-07-03 stsp break;
806 b3d68e7f 2021-07-03 stsp }
807 b3d68e7f 2021-07-03 stsp
808 b3d68e7f 2021-07-03 stsp if (got_object_tree_entry_is_symlink(e) ||
809 b3d68e7f 2021-07-03 stsp got_object_tree_entry_is_submodule(e) ||
810 b3d68e7f 2021-07-03 stsp got_object_idset_contains(traversed_ids, id))
811 b3d68e7f 2021-07-03 stsp continue;
812 b3d68e7f 2021-07-03 stsp
813 b3d68e7f 2021-07-03 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
814 b3d68e7f 2021-07-03 stsp got_tree_entry_get_name(e)) == -1) {
815 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("asprintf");
816 b3d68e7f 2021-07-03 stsp break;
817 b3d68e7f 2021-07-03 stsp }
818 b3d68e7f 2021-07-03 stsp
819 b3d68e7f 2021-07-03 stsp if (S_ISDIR(mode)) {
820 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
821 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, id);
822 b3d68e7f 2021-07-03 stsp if (err)
823 b3d68e7f 2021-07-03 stsp break;
824 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
825 b3d68e7f 2021-07-03 stsp } else if (S_ISREG(mode)) {
826 b3d68e7f 2021-07-03 stsp /* This blob is referenced. */
827 b3d68e7f 2021-07-03 stsp err = preserve_loose_object(loose_ids, id, repo,
828 b3d68e7f 2021-07-03 stsp npacked);
829 b3d68e7f 2021-07-03 stsp if (err)
830 b3d68e7f 2021-07-03 stsp break;
831 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(traversed_ids, id, NULL);
832 b3d68e7f 2021-07-03 stsp if (err)
833 b3d68e7f 2021-07-03 stsp break;
834 b3d68e7f 2021-07-03 stsp
835 b3d68e7f 2021-07-03 stsp }
836 b3d68e7f 2021-07-03 stsp free(p);
837 b3d68e7f 2021-07-03 stsp p = NULL;
838 b3d68e7f 2021-07-03 stsp }
839 b3d68e7f 2021-07-03 stsp
840 b3d68e7f 2021-07-03 stsp got_object_tree_close(tree);
841 b3d68e7f 2021-07-03 stsp free(p);
842 b3d68e7f 2021-07-03 stsp return err;
843 b3d68e7f 2021-07-03 stsp }
844 b3d68e7f 2021-07-03 stsp
845 b3d68e7f 2021-07-03 stsp static const struct got_error *
846 b3d68e7f 2021-07-03 stsp load_tree(struct got_object_idset *loose_ids,
847 b3d68e7f 2021-07-03 stsp struct got_object_idset *traversed_ids, struct got_object_id *tree_id,
848 b3d68e7f 2021-07-03 stsp const char *dpath, struct got_repository *repo, int *npacked,
849 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
850 b3d68e7f 2021-07-03 stsp {
851 b3d68e7f 2021-07-03 stsp const struct got_error *err = NULL;
852 b3d68e7f 2021-07-03 stsp struct got_object_id_queue tree_ids;
853 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
854 b3d68e7f 2021-07-03 stsp
855 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, tree_id);
856 b3d68e7f 2021-07-03 stsp if (err)
857 b3d68e7f 2021-07-03 stsp return err;
858 b3d68e7f 2021-07-03 stsp
859 b3d68e7f 2021-07-03 stsp STAILQ_INIT(&tree_ids);
860 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
861 b3d68e7f 2021-07-03 stsp
862 b3d68e7f 2021-07-03 stsp while (!STAILQ_EMPTY(&tree_ids)) {
863 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
864 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
865 b3d68e7f 2021-07-03 stsp if (err)
866 b3d68e7f 2021-07-03 stsp break;
867 b3d68e7f 2021-07-03 stsp }
868 b3d68e7f 2021-07-03 stsp
869 b3d68e7f 2021-07-03 stsp qid = STAILQ_FIRST(&tree_ids);
870 b3d68e7f 2021-07-03 stsp STAILQ_REMOVE_HEAD(&tree_ids, entry);
871 b3d68e7f 2021-07-03 stsp
872 b3d68e7f 2021-07-03 stsp if (got_object_idset_contains(traversed_ids, qid->id)) {
873 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
874 b3d68e7f 2021-07-03 stsp continue;
875 b3d68e7f 2021-07-03 stsp }
876 b3d68e7f 2021-07-03 stsp
877 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(traversed_ids, qid->id, NULL);
878 b3d68e7f 2021-07-03 stsp if (err) {
879 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
880 b3d68e7f 2021-07-03 stsp break;
881 b3d68e7f 2021-07-03 stsp }
882 b3d68e7f 2021-07-03 stsp
883 b3d68e7f 2021-07-03 stsp /* This tree is referenced. */
884 b3d68e7f 2021-07-03 stsp err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
885 b3d68e7f 2021-07-03 stsp if (err)
886 b3d68e7f 2021-07-03 stsp break;
887 b3d68e7f 2021-07-03 stsp
888 b3d68e7f 2021-07-03 stsp err = load_tree_entries(&tree_ids, loose_ids, traversed_ids,
889 b3d68e7f 2021-07-03 stsp qid->id, dpath, repo, npacked, cancel_cb, cancel_arg);
890 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
891 b3d68e7f 2021-07-03 stsp if (err)
892 b3d68e7f 2021-07-03 stsp break;
893 b3d68e7f 2021-07-03 stsp }
894 b3d68e7f 2021-07-03 stsp
895 b3d68e7f 2021-07-03 stsp got_object_id_queue_free(&tree_ids);
896 b3d68e7f 2021-07-03 stsp return err;
897 b3d68e7f 2021-07-03 stsp }
898 b3d68e7f 2021-07-03 stsp
899 b3d68e7f 2021-07-03 stsp static const struct got_error *
900 b3d68e7f 2021-07-03 stsp load_commit_or_tag(struct got_object_idset *loose_ids, int *ncommits,
901 b3d68e7f 2021-07-03 stsp int *npacked, struct got_object_idset *traversed_ids,
902 b3d68e7f 2021-07-03 stsp struct got_object_id *id, struct got_repository *repo,
903 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb, void *progress_arg, int nloose,
904 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
905 b3d68e7f 2021-07-03 stsp {
906 b3d68e7f 2021-07-03 stsp const struct got_error *err;
907 b3d68e7f 2021-07-03 stsp struct got_commit_object *commit = NULL;
908 b3d68e7f 2021-07-03 stsp struct got_tag_object *tag = NULL;
909 b3d68e7f 2021-07-03 stsp struct got_object_id *tree_id = NULL;
910 b3d68e7f 2021-07-03 stsp struct got_object_id_queue ids;
911 b3d68e7f 2021-07-03 stsp struct got_object_qid *qid;
912 b3d68e7f 2021-07-03 stsp int obj_type;
913 b3d68e7f 2021-07-03 stsp
914 b3d68e7f 2021-07-03 stsp err = got_object_qid_alloc(&qid, id);
915 b3d68e7f 2021-07-03 stsp if (err)
916 b3d68e7f 2021-07-03 stsp return err;
917 b3d68e7f 2021-07-03 stsp
918 b3d68e7f 2021-07-03 stsp STAILQ_INIT(&ids);
919 b3d68e7f 2021-07-03 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
920 b3d68e7f 2021-07-03 stsp
921 b3d68e7f 2021-07-03 stsp while (!STAILQ_EMPTY(&ids)) {
922 b3d68e7f 2021-07-03 stsp if (cancel_cb) {
923 b3d68e7f 2021-07-03 stsp err = (*cancel_cb)(cancel_arg);
924 b3d68e7f 2021-07-03 stsp if (err)
925 b3d68e7f 2021-07-03 stsp break;
926 b3d68e7f 2021-07-03 stsp }
927 b3d68e7f 2021-07-03 stsp
928 b3d68e7f 2021-07-03 stsp qid = STAILQ_FIRST(&ids);
929 b3d68e7f 2021-07-03 stsp STAILQ_REMOVE_HEAD(&ids, entry);
930 b3d68e7f 2021-07-03 stsp
931 b3d68e7f 2021-07-03 stsp if (got_object_idset_contains(traversed_ids, qid->id)) {
932 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
933 b3d68e7f 2021-07-03 stsp qid = NULL;
934 b3d68e7f 2021-07-03 stsp continue;
935 b3d68e7f 2021-07-03 stsp }
936 b3d68e7f 2021-07-03 stsp
937 b3d68e7f 2021-07-03 stsp err = got_object_idset_add(traversed_ids, qid->id, NULL);
938 b3d68e7f 2021-07-03 stsp if (err)
939 b3d68e7f 2021-07-03 stsp break;
940 b3d68e7f 2021-07-03 stsp
941 b3d68e7f 2021-07-03 stsp /* This commit or tag is referenced. */
942 b3d68e7f 2021-07-03 stsp err = preserve_loose_object(loose_ids, qid->id, repo, npacked);
943 b3d68e7f 2021-07-03 stsp if (err)
944 b3d68e7f 2021-07-03 stsp break;
945 b3d68e7f 2021-07-03 stsp
946 b3d68e7f 2021-07-03 stsp err = got_object_get_type(&obj_type, repo, qid->id);
947 b3d68e7f 2021-07-03 stsp if (err)
948 b3d68e7f 2021-07-03 stsp break;
949 b3d68e7f 2021-07-03 stsp switch (obj_type) {
950 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
951 b3d68e7f 2021-07-03 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
952 b3d68e7f 2021-07-03 stsp if (err)
953 b3d68e7f 2021-07-03 stsp goto done;
954 b3d68e7f 2021-07-03 stsp break;
955 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TAG:
956 b3d68e7f 2021-07-03 stsp err = got_object_open_as_tag(&tag, repo, qid->id);
957 b3d68e7f 2021-07-03 stsp if (err)
958 b3d68e7f 2021-07-03 stsp goto done;
959 b3d68e7f 2021-07-03 stsp break;
960 b3d68e7f 2021-07-03 stsp default:
961 b3d68e7f 2021-07-03 stsp /* should not happen */
962 b3d68e7f 2021-07-03 stsp err = got_error(GOT_ERR_OBJ_TYPE);
963 b3d68e7f 2021-07-03 stsp goto done;
964 b3d68e7f 2021-07-03 stsp }
965 b3d68e7f 2021-07-03 stsp
966 b3d68e7f 2021-07-03 stsp /* Find a tree object to scan. */
967 b3d68e7f 2021-07-03 stsp if (commit) {
968 b3d68e7f 2021-07-03 stsp tree_id = got_object_commit_get_tree_id(commit);
969 b3d68e7f 2021-07-03 stsp } else if (tag) {
970 b3d68e7f 2021-07-03 stsp obj_type = got_object_tag_get_object_type(tag);
971 b3d68e7f 2021-07-03 stsp switch (obj_type) {
972 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_COMMIT:
973 b3d68e7f 2021-07-03 stsp err = got_object_open_as_commit(&commit, repo,
974 b3d68e7f 2021-07-03 stsp got_object_tag_get_object_id(tag));
975 b3d68e7f 2021-07-03 stsp if (err)
976 b3d68e7f 2021-07-03 stsp goto done;
977 b3d68e7f 2021-07-03 stsp tree_id = got_object_commit_get_tree_id(commit);
978 b3d68e7f 2021-07-03 stsp break;
979 b3d68e7f 2021-07-03 stsp case GOT_OBJ_TYPE_TREE:
980 b3d68e7f 2021-07-03 stsp tree_id = got_object_tag_get_object_id(tag);
981 b3d68e7f 2021-07-03 stsp break;
982 b3d68e7f 2021-07-03 stsp default:
983 b3d68e7f 2021-07-03 stsp /*
984 b3d68e7f 2021-07-03 stsp * Tag points at something other than a
985 b3d68e7f 2021-07-03 stsp * commit or tree. Leave this weird tag object
986 b3d68e7f 2021-07-03 stsp * and the object it points to on disk.
987 b3d68e7f 2021-07-03 stsp */
988 b3d68e7f 2021-07-03 stsp err = got_object_idset_remove(NULL, loose_ids,
989 b3d68e7f 2021-07-03 stsp qid->id);
990 b3d68e7f 2021-07-03 stsp if (err && err->code != GOT_ERR_NO_OBJ)
991 b3d68e7f 2021-07-03 stsp goto done;
992 b3d68e7f 2021-07-03 stsp err = got_object_idset_remove(NULL, loose_ids,
993 b3d68e7f 2021-07-03 stsp got_object_tag_get_object_id(tag));
994 b3d68e7f 2021-07-03 stsp if (err && err->code != GOT_ERR_NO_OBJ)
995 b3d68e7f 2021-07-03 stsp goto done;
996 b3d68e7f 2021-07-03 stsp err = NULL;
997 b3d68e7f 2021-07-03 stsp break;
998 b3d68e7f 2021-07-03 stsp }
999 b3d68e7f 2021-07-03 stsp }
1000 b3d68e7f 2021-07-03 stsp
1001 b3d68e7f 2021-07-03 stsp if (tree_id) {
1002 b3d68e7f 2021-07-03 stsp err = load_tree(loose_ids, traversed_ids, tree_id, "",
1003 b3d68e7f 2021-07-03 stsp repo, npacked, cancel_cb, cancel_arg);
1004 b3d68e7f 2021-07-03 stsp if (err)
1005 b3d68e7f 2021-07-03 stsp break;
1006 b3d68e7f 2021-07-03 stsp }
1007 b3d68e7f 2021-07-03 stsp
1008 b3d68e7f 2021-07-03 stsp if (commit || tag)
1009 b3d68e7f 2021-07-03 stsp (*ncommits)++; /* scanned tags are counted as commits */
1010 b3d68e7f 2021-07-03 stsp
1011 b3d68e7f 2021-07-03 stsp if (progress_cb) {
1012 b3d68e7f 2021-07-03 stsp err = progress_cb(progress_arg, nloose, *ncommits, -1);
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) {
1018 b3d68e7f 2021-07-03 stsp /* Find parent commits to scan. */
1019 b3d68e7f 2021-07-03 stsp const struct got_object_id_queue *parent_ids;
1020 b3d68e7f 2021-07-03 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1021 b3d68e7f 2021-07-03 stsp err = got_object_id_queue_copy(parent_ids, &ids);
1022 b3d68e7f 2021-07-03 stsp if (err)
1023 b3d68e7f 2021-07-03 stsp break;
1024 b3d68e7f 2021-07-03 stsp got_object_commit_close(commit);
1025 b3d68e7f 2021-07-03 stsp commit = NULL;
1026 b3d68e7f 2021-07-03 stsp }
1027 b3d68e7f 2021-07-03 stsp if (tag) {
1028 b3d68e7f 2021-07-03 stsp got_object_tag_close(tag);
1029 b3d68e7f 2021-07-03 stsp tag = NULL;
1030 b3d68e7f 2021-07-03 stsp }
1031 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
1032 b3d68e7f 2021-07-03 stsp qid = NULL;
1033 b3d68e7f 2021-07-03 stsp }
1034 b3d68e7f 2021-07-03 stsp done:
1035 b3d68e7f 2021-07-03 stsp if (qid)
1036 b3d68e7f 2021-07-03 stsp got_object_qid_free(qid);
1037 b3d68e7f 2021-07-03 stsp if (commit)
1038 b3d68e7f 2021-07-03 stsp got_object_commit_close(commit);
1039 b3d68e7f 2021-07-03 stsp if (tag)
1040 b3d68e7f 2021-07-03 stsp got_object_tag_close(tag);
1041 4b2e47fb 2021-07-03 stsp got_object_id_queue_free(&ids);
1042 b3d68e7f 2021-07-03 stsp return err;
1043 b3d68e7f 2021-07-03 stsp }
1044 b3d68e7f 2021-07-03 stsp
1045 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg {
1046 b3d68e7f 2021-07-03 stsp struct got_repository *repo;
1047 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb;
1048 b3d68e7f 2021-07-03 stsp void *progress_arg;
1049 b3d68e7f 2021-07-03 stsp int nloose;
1050 b3d68e7f 2021-07-03 stsp int ncommits;
1051 b3d68e7f 2021-07-03 stsp int npurged;
1052 b3d68e7f 2021-07-03 stsp off_t size_purged;
1053 b3d68e7f 2021-07-03 stsp int dry_run;
1054 b3d68e7f 2021-07-03 stsp };
1055 b3d68e7f 2021-07-03 stsp
1056 b3d68e7f 2021-07-03 stsp static const struct got_error *
1057 b3d68e7f 2021-07-03 stsp purge_loose_object(struct got_object_id *id, void *data, void *arg)
1058 b3d68e7f 2021-07-03 stsp {
1059 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg *a = arg;
1060 b3d68e7f 2021-07-03 stsp const struct got_error *err, *unlock_err = NULL;
1061 b3d68e7f 2021-07-03 stsp char *path = NULL;
1062 b3d68e7f 2021-07-03 stsp int fd = -1;
1063 b3d68e7f 2021-07-03 stsp struct stat sb;
1064 b3d68e7f 2021-07-03 stsp struct got_lockfile *lf = NULL;
1065 b3d68e7f 2021-07-03 stsp
1066 b3d68e7f 2021-07-03 stsp err = got_object_get_path(&path, id, a->repo);
1067 b3d68e7f 2021-07-03 stsp if (err)
1068 b3d68e7f 2021-07-03 stsp return err;
1069 b3d68e7f 2021-07-03 stsp
1070 b3d68e7f 2021-07-03 stsp err = got_object_open_loose_fd(&fd, id, a->repo);
1071 b3d68e7f 2021-07-03 stsp if (err)
1072 b3d68e7f 2021-07-03 stsp goto done;
1073 b3d68e7f 2021-07-03 stsp
1074 b3d68e7f 2021-07-03 stsp if (fstat(fd, &sb) == -1) {
1075 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("fstat");
1076 b3d68e7f 2021-07-03 stsp goto done;
1077 b3d68e7f 2021-07-03 stsp }
1078 b3d68e7f 2021-07-03 stsp
1079 b3d68e7f 2021-07-03 stsp if (!a->dry_run) {
1080 b3d68e7f 2021-07-03 stsp err = got_lockfile_lock(&lf, path);
1081 b3d68e7f 2021-07-03 stsp if (err)
1082 b3d68e7f 2021-07-03 stsp goto done;
1083 b3d68e7f 2021-07-03 stsp if (unlink(path) == -1) {
1084 b3d68e7f 2021-07-03 stsp err = got_error_from_errno2("unlink", path);
1085 b3d68e7f 2021-07-03 stsp goto done;
1086 b3d68e7f 2021-07-03 stsp }
1087 b3d68e7f 2021-07-03 stsp }
1088 b3d68e7f 2021-07-03 stsp
1089 b3d68e7f 2021-07-03 stsp a->npurged++;
1090 b3d68e7f 2021-07-03 stsp a->size_purged += sb.st_size;
1091 b3d68e7f 2021-07-03 stsp if (a->progress_cb) {
1092 b3d68e7f 2021-07-03 stsp err = a->progress_cb(a->progress_arg, a->nloose,
1093 b3d68e7f 2021-07-03 stsp a->ncommits, a->npurged);
1094 b3d68e7f 2021-07-03 stsp }
1095 b3d68e7f 2021-07-03 stsp done:
1096 b3d68e7f 2021-07-03 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1097 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("close");
1098 b3d68e7f 2021-07-03 stsp free(path);
1099 b3d68e7f 2021-07-03 stsp if (lf)
1100 b3d68e7f 2021-07-03 stsp unlock_err = got_lockfile_unlock(lf);
1101 b3d68e7f 2021-07-03 stsp return err ? err : unlock_err;
1102 b3d68e7f 2021-07-03 stsp }
1103 b3d68e7f 2021-07-03 stsp
1104 b3d68e7f 2021-07-03 stsp const struct got_error *
1105 b3d68e7f 2021-07-03 stsp got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
1106 b3d68e7f 2021-07-03 stsp off_t *size_before, off_t *size_after, int *npacked, int dry_run,
1107 b3d68e7f 2021-07-03 stsp got_cleanup_progress_cb progress_cb, void *progress_arg,
1108 b3d68e7f 2021-07-03 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1109 b3d68e7f 2021-07-03 stsp {
1110 b3d68e7f 2021-07-03 stsp const struct got_error *err;
1111 b3d68e7f 2021-07-03 stsp struct got_object_idset *loose_ids;
1112 b3d68e7f 2021-07-03 stsp struct got_object_idset *traversed_ids;
1113 b3d68e7f 2021-07-03 stsp struct got_object_id **referenced_ids;
1114 b3d68e7f 2021-07-03 stsp int i, nreferenced, nloose, ncommits = 0;
1115 b3d68e7f 2021-07-03 stsp struct got_reflist_head refs;
1116 b3d68e7f 2021-07-03 stsp struct purge_loose_object_arg arg;
1117 b3d68e7f 2021-07-03 stsp
1118 b3d68e7f 2021-07-03 stsp TAILQ_INIT(&refs);
1119 b3d68e7f 2021-07-03 stsp
1120 b3d68e7f 2021-07-03 stsp *size_before = 0;
1121 b3d68e7f 2021-07-03 stsp *size_after = 0;
1122 b3d68e7f 2021-07-03 stsp *npacked = 0;
1123 b3d68e7f 2021-07-03 stsp
1124 b3d68e7f 2021-07-03 stsp err = get_loose_object_ids(&loose_ids, size_before,
1125 b3d68e7f 2021-07-03 stsp progress_cb, progress_arg, repo);
1126 b3d68e7f 2021-07-03 stsp if (err)
1127 b3d68e7f 2021-07-03 stsp return err;
1128 b3d68e7f 2021-07-03 stsp nloose = got_object_idset_num_elements(loose_ids);
1129 b3d68e7f 2021-07-03 stsp if (nloose == 0) {
1130 b3d68e7f 2021-07-03 stsp got_object_idset_free(loose_ids);
1131 b3d68e7f 2021-07-03 stsp return NULL;
1132 b3d68e7f 2021-07-03 stsp }
1133 b3d68e7f 2021-07-03 stsp
1134 b3d68e7f 2021-07-03 stsp traversed_ids = got_object_idset_alloc();
1135 b3d68e7f 2021-07-03 stsp if (traversed_ids == NULL) {
1136 b3d68e7f 2021-07-03 stsp err = got_error_from_errno("got_object_idset_alloc");
1137 b3d68e7f 2021-07-03 stsp goto done;
1138 b3d68e7f 2021-07-03 stsp }
1139 b3d68e7f 2021-07-03 stsp
1140 b3d68e7f 2021-07-03 stsp err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
1141 b3d68e7f 2021-07-03 stsp if (err)
1142 b3d68e7f 2021-07-03 stsp goto done;
1143 b3d68e7f 2021-07-03 stsp
1144 b3d68e7f 2021-07-03 stsp err = get_reflist_object_ids(&referenced_ids, &nreferenced,
1145 b3d68e7f 2021-07-03 stsp (1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
1146 b3d68e7f 2021-07-03 stsp &refs, repo, cancel_cb, cancel_arg);
1147 b3d68e7f 2021-07-03 stsp if (err)
1148 b3d68e7f 2021-07-03 stsp goto done;
1149 b3d68e7f 2021-07-03 stsp
1150 b3d68e7f 2021-07-03 stsp for (i = 0; i < nreferenced; i++) {
1151 b3d68e7f 2021-07-03 stsp struct got_object_id *id = referenced_ids[i];
1152 b3d68e7f 2021-07-03 stsp err = load_commit_or_tag(loose_ids, &ncommits, npacked,
1153 b3d68e7f 2021-07-03 stsp traversed_ids, id, repo, progress_cb, progress_arg, nloose,
1154 b3d68e7f 2021-07-03 stsp cancel_cb, cancel_arg);
1155 b3d68e7f 2021-07-03 stsp if (err)
1156 b3d68e7f 2021-07-03 stsp goto done;
1157 b3d68e7f 2021-07-03 stsp }
1158 b3d68e7f 2021-07-03 stsp
1159 b3d68e7f 2021-07-03 stsp /* Produce a final progress report in case no objects can be purged. */
1160 b3d68e7f 2021-07-03 stsp if (got_object_idset_num_elements(loose_ids) == 0 && progress_cb) {
1161 b3d68e7f 2021-07-03 stsp err = progress_cb(progress_arg, nloose, ncommits, 0);
1162 b3d68e7f 2021-07-03 stsp if (err)
1163 b3d68e7f 2021-07-03 stsp goto done;
1164 b3d68e7f 2021-07-03 stsp }
1165 b3d68e7f 2021-07-03 stsp
1166 b3d68e7f 2021-07-03 stsp /* Any remaining loose objects are unreferenced and can be purged. */
1167 b3d68e7f 2021-07-03 stsp arg.repo = repo;
1168 b3d68e7f 2021-07-03 stsp arg.progress_arg = progress_arg;
1169 b3d68e7f 2021-07-03 stsp arg.progress_cb = progress_cb;
1170 b3d68e7f 2021-07-03 stsp arg.nloose = nloose;
1171 b3d68e7f 2021-07-03 stsp arg.npurged = 0;
1172 b3d68e7f 2021-07-03 stsp arg.size_purged = 0;
1173 b3d68e7f 2021-07-03 stsp arg.ncommits = ncommits;
1174 b3d68e7f 2021-07-03 stsp arg.dry_run = dry_run;
1175 b3d68e7f 2021-07-03 stsp err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
1176 b3d68e7f 2021-07-03 stsp if (err)
1177 b3d68e7f 2021-07-03 stsp goto done;
1178 b3d68e7f 2021-07-03 stsp *size_after = *size_before - arg.size_purged;
1179 b3d68e7f 2021-07-03 stsp done:
1180 b3d68e7f 2021-07-03 stsp got_object_idset_free(loose_ids);
1181 b3d68e7f 2021-07-03 stsp got_object_idset_free(traversed_ids);
1182 b3d68e7f 2021-07-03 stsp return err;
1183 b3d68e7f 2021-07-03 stsp }