2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ad242220 2018-09-08 stsp #include <sys/types.h>
18 79b11c62 2018-03-09 stsp #include <sys/queue.h>
19 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
20 ad242220 2018-09-08 stsp #include <sys/uio.h>
21 aba9c984 2019-09-08 stsp #include <sys/socket.h>
22 deeca238 2018-03-12 stsp #include <sys/stat.h>
23 1510f469 2018-09-09 stsp #include <sys/mman.h>
24 6c414261 2021-03-30 stsp #include <sys/resource.h>
26 e09a504c 2019-06-28 stsp #include <ctype.h>
27 78fb0967 2020-09-09 naddy #include <endian.h>
28 1510f469 2018-09-09 stsp #include <fcntl.h>
29 3ce1b845 2019-07-15 stsp #include <fnmatch.h>
30 4027f31a 2017-11-04 stsp #include <limits.h>
31 1510f469 2018-09-09 stsp #include <dirent.h>
32 4027f31a 2017-11-04 stsp #include <stdlib.h>
33 4027f31a 2017-11-04 stsp #include <stdio.h>
34 4027f31a 2017-11-04 stsp #include <sha1.h>
35 4027f31a 2017-11-04 stsp #include <string.h>
36 303e14b5 2019-09-23 stsp #include <time.h>
37 81a12da5 2020-09-09 naddy #include <unistd.h>
38 79b11c62 2018-03-09 stsp #include <zlib.h>
39 85f51bba 2018-07-16 stsp #include <errno.h>
40 85f51bba 2018-07-16 stsp #include <libgen.h>
41 ad242220 2018-09-08 stsp #include <stdint.h>
42 ad242220 2018-09-08 stsp #include <imsg.h>
43 c442a90d 2019-03-10 stsp #include <uuid.h>
45 b343c297 2021-10-11 stsp #include "bloom.h"
47 4027f31a 2017-11-04 stsp #include "got_error.h"
48 5261c201 2018-04-01 stsp #include "got_reference.h"
49 4027f31a 2017-11-04 stsp #include "got_repository.h"
50 1dd54920 2019-05-11 stsp #include "got_path.h"
51 e6209546 2019-08-22 stsp #include "got_cancel.h"
52 7bb0daa1 2018-06-21 stsp #include "got_object.h"
53 57160834 2022-05-31 stsp #include "got_opentemp.h"
55 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
56 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
57 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
58 3ce1b845 2019-07-15 stsp #include "got_lib_object_parse.h"
59 3ce1b845 2019-07-15 stsp #include "got_lib_object_create.h"
60 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
61 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
62 e09a504c 2019-06-28 stsp #include "got_lib_sha1.h"
63 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
64 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
65 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
67 79b11c62 2018-03-09 stsp #ifndef nitems
68 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
71 0ae84acc 2022-06-15 tracey #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
73 f8b19efd 2021-10-13 stsp RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
74 f8b19efd 2021-10-13 stsp got_packidx_bloom_filter_cmp);
76 7839bc15 2019-01-06 stsp const char *
77 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
79 7839bc15 2019-01-06 stsp return repo->path;
82 6e9da951 2019-01-06 stsp const char *
83 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
85 6e9da951 2019-01-06 stsp return repo->path_git_dir;
89 6d5a9006 2020-12-16 yzhong got_repo_get_fd(struct got_repository *repo)
91 6d5a9006 2020-12-16 yzhong return repo->gitdir_fd;
94 aba9c984 2019-09-08 stsp const char *
95 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_name(struct got_repository *repo)
97 aba9c984 2019-09-08 stsp return repo->gitconfig_author_name;
100 aba9c984 2019-09-08 stsp const char *
101 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_email(struct got_repository *repo)
103 aba9c984 2019-09-08 stsp return repo->gitconfig_author_email;
106 c9956ddf 2019-09-08 stsp const char *
107 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
109 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_name;
112 c9956ddf 2019-09-08 stsp const char *
113 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
115 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_email;
118 9a1cc63f 2020-02-03 stsp const char *
119 9a1cc63f 2020-02-03 stsp got_repo_get_gitconfig_owner(struct got_repository *repo)
121 9a1cc63f 2020-02-03 stsp return repo->gitconfig_owner;
125 9188bd78 2021-07-03 stsp got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
126 9188bd78 2021-07-03 stsp struct got_repository *repo)
128 9188bd78 2021-07-03 stsp *extensions = repo->extensions;
129 9188bd78 2021-07-03 stsp *nextensions = repo->nextensions;
133 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
135 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
138 4027f31a 2017-11-04 stsp static char *
139 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
141 4027f31a 2017-11-04 stsp char *path_child;
143 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
144 4027f31a 2017-11-04 stsp basename) == -1)
145 4027f31a 2017-11-04 stsp return NULL;
147 4027f31a 2017-11-04 stsp return path_child;
151 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
153 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
157 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
159 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
163 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
165 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
169 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
171 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
174 4027f31a 2017-11-04 stsp static char *
175 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
177 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
181 b46f3e71 2020-03-18 stsp got_repo_get_path_gitconfig(struct got_repository *repo)
183 b46f3e71 2020-03-18 stsp return get_path_git_child(repo, GOT_GITCONFIG);
187 257add31 2020-09-09 stsp got_repo_get_path_gotconfig(struct got_repository *repo)
189 50b0790e 2020-09-11 stsp return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
192 50b0790e 2020-09-11 stsp const struct got_gotconfig *
193 50b0790e 2020-09-11 stsp got_repo_get_gotconfig(struct got_repository *repo)
195 50b0790e 2020-09-11 stsp return repo->gotconfig;
199 50b0790e 2020-09-11 stsp got_repo_get_gitconfig_remotes(int *nremotes,
200 50b0790e 2020-09-11 stsp const struct got_remote_repo **remotes, struct got_repository *repo)
202 50b0790e 2020-09-11 stsp *nremotes = repo->ngitconfig_remotes;
203 50b0790e 2020-09-11 stsp *remotes = repo->gitconfig_remotes;
207 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
209 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
210 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
211 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
212 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
213 deeca238 2018-03-12 stsp int ret = 0;
214 deeca238 2018-03-12 stsp struct stat sb;
215 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
217 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
219 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
222 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
224 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
227 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
229 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
232 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
234 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
237 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
238 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
240 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
244 4027f31a 2017-11-04 stsp free(path_objects);
245 4027f31a 2017-11-04 stsp free(path_refs);
246 4027f31a 2017-11-04 stsp free(path_head);
247 4027f31a 2017-11-04 stsp return ret;
251 f6be5c30 2018-06-22 stsp const struct got_error *
252 0ae84acc 2022-06-15 tracey got_repo_pack_fds_open(int **pack_fds)
254 0ae84acc 2022-06-15 tracey const struct got_error *err = NULL;
255 f97edf0c 2022-07-21 florian int i, *pack_fds_tmp;
257 f97edf0c 2022-07-21 florian pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int));
258 f97edf0c 2022-07-21 florian if (pack_fds_tmp == NULL)
259 f97edf0c 2022-07-21 florian return got_error_from_errno("calloc");
260 0ae84acc 2022-06-15 tracey *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds));
261 f97edf0c 2022-07-21 florian if (*pack_fds == NULL) {
262 f97edf0c 2022-07-21 florian free(pack_fds_tmp);
263 0ae84acc 2022-06-15 tracey return got_error_from_errno("calloc");
267 0c411343 2022-07-21 stsp * got_repo_pack_fds_close will try to close all of the
268 0c411343 2022-07-21 stsp * GOT_PACK_NUM_TEMPFILES fds, even the ones that didn't manage to get
269 6f748833 2022-07-21 stsp * a value from got_opentempfd(), which would result in a close(0) if
270 6f748833 2022-07-21 stsp * we do not initialize to -1 here.
272 0c411343 2022-07-21 stsp for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++)
273 0c411343 2022-07-21 stsp pack_fds_tmp[i] = -1;
275 0ae84acc 2022-06-15 tracey for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
276 0ae84acc 2022-06-15 tracey pack_fds_tmp[i] = got_opentempfd();
277 0ae84acc 2022-06-15 tracey if (pack_fds_tmp[i] == -1) {
278 bb711527 2022-06-15 stsp err = got_error_from_errno("got_opentempfd");
279 bb711527 2022-06-15 stsp got_repo_pack_fds_close(pack_fds_tmp);
280 bb711527 2022-06-15 stsp return err;
283 f97edf0c 2022-07-21 florian memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int));
284 0ae84acc 2022-06-15 tracey return err;
287 0ae84acc 2022-06-15 tracey const struct got_error *
288 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(int *pack_fds)
290 0ae84acc 2022-06-15 tracey const struct got_error *err = NULL;
293 0ae84acc 2022-06-15 tracey for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) {
294 0ae84acc 2022-06-15 tracey if (pack_fds[i] == -1)
295 0ae84acc 2022-06-15 tracey continue;
296 0ae84acc 2022-06-15 tracey if (close(pack_fds[i]) == -1) {
297 0ae84acc 2022-06-15 tracey err = got_error_from_errno("close");
301 0ae84acc 2022-06-15 tracey free(pack_fds);
302 0ae84acc 2022-06-15 tracey return err;
305 0ae84acc 2022-06-15 tracey const struct got_error *
306 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
307 f6be5c30 2018-06-22 stsp struct got_object *obj)
309 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
310 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
311 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
313 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
314 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
315 79c99a64 2019-05-23 stsp err = NULL;
316 f6be5c30 2018-06-22 stsp return err;
318 f6be5c30 2018-06-22 stsp obj->refcnt++;
320 f6be5c30 2018-06-22 stsp return NULL;
323 7bb0daa1 2018-06-21 stsp struct got_object *
324 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
325 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
327 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
330 4027f31a 2017-11-04 stsp const struct got_error *
331 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
332 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
334 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
335 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
336 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
338 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
339 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
340 79c99a64 2019-05-23 stsp err = NULL;
341 f6be5c30 2018-06-22 stsp return err;
343 f6be5c30 2018-06-22 stsp tree->refcnt++;
345 f6be5c30 2018-06-22 stsp return NULL;
348 f6be5c30 2018-06-22 stsp struct got_tree_object *
349 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
350 f6be5c30 2018-06-22 stsp struct got_object_id *id)
352 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
353 6bef87be 2018-09-11 stsp &repo->treecache, id);
356 1943de01 2018-06-22 stsp const struct got_error *
357 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
358 1943de01 2018-06-22 stsp struct got_commit_object *commit)
360 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
361 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
362 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
364 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
365 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
366 79c99a64 2019-05-23 stsp err = NULL;
367 1943de01 2018-06-22 stsp return err;
369 1943de01 2018-06-22 stsp commit->refcnt++;
371 f6be5c30 2018-06-22 stsp return NULL;
374 1943de01 2018-06-22 stsp struct got_commit_object *
375 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
376 1943de01 2018-06-22 stsp struct got_object_id *id)
378 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
379 6bef87be 2018-09-11 stsp &repo->commitcache, id);
382 f4a881ce 2018-11-17 stsp const struct got_error *
383 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
384 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
386 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
387 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
388 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
390 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
391 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
392 79c99a64 2019-05-23 stsp err = NULL;
393 f4a881ce 2018-11-17 stsp return err;
395 f4a881ce 2018-11-17 stsp tag->refcnt++;
397 f4a881ce 2018-11-17 stsp return NULL;
400 f4a881ce 2018-11-17 stsp struct got_tag_object *
401 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
403 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
404 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
407 d3c116bf 2021-10-15 stsp const struct got_error *
408 d3c116bf 2021-10-15 stsp got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
409 d3c116bf 2021-10-15 stsp struct got_raw_object *raw)
411 d3c116bf 2021-10-15 stsp #ifndef GOT_NO_OBJ_CACHE
412 d3c116bf 2021-10-15 stsp const struct got_error *err = NULL;
413 d3c116bf 2021-10-15 stsp err = got_object_cache_add(&repo->rawcache, id, raw);
415 d3c116bf 2021-10-15 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
416 d3c116bf 2021-10-15 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
417 d3c116bf 2021-10-15 stsp err = NULL;
418 d3c116bf 2021-10-15 stsp return err;
420 d3c116bf 2021-10-15 stsp raw->refcnt++;
422 d3c116bf 2021-10-15 stsp return NULL;
426 d3c116bf 2021-10-15 stsp struct got_raw_object *
427 d3c116bf 2021-10-15 stsp got_repo_get_cached_raw_object(struct got_repository *repo,
428 d3c116bf 2021-10-15 stsp struct got_object_id *id)
430 d3c116bf 2021-10-15 stsp return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
434 991ff1aa 2021-06-15 tracey static const struct got_error *
435 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
437 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
439 6d5a9006 2020-12-16 yzhong repo->gitdir_fd = -1;
441 85f51bba 2018-07-16 stsp /* bare git repository? */
442 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
443 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
444 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
445 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
446 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
447 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
448 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
449 6d5a9006 2020-12-16 yzhong goto done;
451 8bd0cdad 2021-12-31 stsp repo->gitdir_fd = open(repo->path_git_dir,
452 8bd0cdad 2021-12-31 stsp O_DIRECTORY | O_CLOEXEC);
453 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd == -1) {
454 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("open",
455 6d5a9006 2020-12-16 yzhong repo->path_git_dir);
458 85f51bba 2018-07-16 stsp return NULL;
461 85f51bba 2018-07-16 stsp /* git repository with working tree? */
462 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
463 6b68ccd6 2019-09-01 stsp repo->path_git_dir = NULL;
464 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
465 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
468 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
469 85f51bba 2018-07-16 stsp repo->path = strdup(path);
470 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
471 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
474 8bd0cdad 2021-12-31 stsp repo->gitdir_fd = open(repo->path_git_dir,
475 8bd0cdad 2021-12-31 stsp O_DIRECTORY | O_CLOEXEC);
476 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd == -1) {
477 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("open",
478 6d5a9006 2020-12-16 yzhong repo->path_git_dir);
479 6d5a9006 2020-12-16 yzhong goto done;
481 85f51bba 2018-07-16 stsp return NULL;
484 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
487 ee645855 2019-02-05 stsp free(repo->path);
488 ee645855 2019-02-05 stsp repo->path = NULL;
489 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
490 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
491 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd != -1)
492 6d5a9006 2020-12-16 yzhong close(repo->gitdir_fd);
493 6d5a9006 2020-12-16 yzhong repo->gitdir_fd = -1;
496 aba9c984 2019-09-08 stsp return err;
499 aba9c984 2019-09-08 stsp static const struct got_error *
500 c9956ddf 2019-09-08 stsp parse_gitconfig_file(int *gitconfig_repository_format_version,
501 c9956ddf 2019-09-08 stsp char **gitconfig_author_name, char **gitconfig_author_email,
502 cd95becd 2019-11-29 stsp struct got_remote_repo **remotes, int *nremotes,
503 20b7abb3 2020-10-22 stsp char **gitconfig_owner, char ***extensions, int *nextensions,
504 c9956ddf 2019-09-08 stsp const char *gitconfig_path)
506 aba9c984 2019-09-08 stsp const struct got_error *err = NULL, *child_err = NULL;
507 aba9c984 2019-09-08 stsp int fd = -1;
508 aba9c984 2019-09-08 stsp int imsg_fds[2] = { -1, -1 };
510 aba9c984 2019-09-08 stsp struct imsgbuf *ibuf;
512 c9956ddf 2019-09-08 stsp *gitconfig_repository_format_version = 0;
513 20b7abb3 2020-10-22 stsp if (extensions)
514 20b7abb3 2020-10-22 stsp *extensions = NULL;
515 20b7abb3 2020-10-22 stsp if (nextensions)
516 20b7abb3 2020-10-22 stsp *nextensions = 0;
517 c9956ddf 2019-09-08 stsp *gitconfig_author_name = NULL;
518 c9956ddf 2019-09-08 stsp *gitconfig_author_email = NULL;
519 2fb669fb 2020-03-20 stsp if (remotes)
520 2fb669fb 2020-03-20 stsp *remotes = NULL;
521 2fb669fb 2020-03-20 stsp if (nremotes)
522 2fb669fb 2020-03-20 stsp *nremotes = 0;
523 2fb669fb 2020-03-20 stsp if (gitconfig_owner)
524 2fb669fb 2020-03-20 stsp *gitconfig_owner = NULL;
526 8bd0cdad 2021-12-31 stsp fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
527 aba9c984 2019-09-08 stsp if (fd == -1) {
528 c9956ddf 2019-09-08 stsp if (errno == ENOENT)
529 aba9c984 2019-09-08 stsp return NULL;
530 c9956ddf 2019-09-08 stsp return got_error_from_errno2("open", gitconfig_path);
533 aba9c984 2019-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
534 aba9c984 2019-09-08 stsp if (ibuf == NULL) {
535 aba9c984 2019-09-08 stsp err = got_error_from_errno("calloc");
539 aba9c984 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
540 aba9c984 2019-09-08 stsp err = got_error_from_errno("socketpair");
544 aba9c984 2019-09-08 stsp pid = fork();
545 aba9c984 2019-09-08 stsp if (pid == -1) {
546 aba9c984 2019-09-08 stsp err = got_error_from_errno("fork");
548 aba9c984 2019-09-08 stsp } else if (pid == 0) {
549 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
550 c9956ddf 2019-09-08 stsp gitconfig_path);
551 aba9c984 2019-09-08 stsp /* not reached */
554 aba9c984 2019-09-08 stsp if (close(imsg_fds[1]) == -1) {
555 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
558 aba9c984 2019-09-08 stsp imsg_fds[1] = -1;
559 aba9c984 2019-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
561 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
566 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
570 aba9c984 2019-09-08 stsp err = got_privsep_recv_gitconfig_int(
571 c9956ddf 2019-09-08 stsp gitconfig_repository_format_version, ibuf);
575 20b7abb3 2020-10-22 stsp if (extensions && nextensions) {
576 20b7abb3 2020-10-22 stsp err = got_privsep_send_gitconfig_repository_extensions_req(
580 20b7abb3 2020-10-22 stsp err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
583 20b7abb3 2020-10-22 stsp if (*nextensions > 0) {
585 20b7abb3 2020-10-22 stsp *extensions = calloc(*nextensions, sizeof(char *));
586 20b7abb3 2020-10-22 stsp if (*extensions == NULL) {
587 20b7abb3 2020-10-22 stsp err = got_error_from_errno("calloc");
590 20b7abb3 2020-10-22 stsp for (i = 0; i < *nextensions; i++) {
592 20b7abb3 2020-10-22 stsp err = got_privsep_recv_gitconfig_str(&ext,
596 20b7abb3 2020-10-22 stsp (*extensions)[i] = ext;
601 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_name_req(ibuf);
605 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
609 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_email_req(ibuf);
613 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
617 cd95becd 2019-11-29 stsp if (remotes && nremotes) {
618 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes_req(ibuf);
622 cd95becd 2019-11-29 stsp err = got_privsep_recv_gitconfig_remotes(remotes,
623 cd95becd 2019-11-29 stsp nremotes, ibuf);
628 9a1cc63f 2020-02-03 stsp if (gitconfig_owner) {
629 9a1cc63f 2020-02-03 stsp err = got_privsep_send_gitconfig_owner_req(ibuf);
632 9a1cc63f 2020-02-03 stsp err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
637 aba9c984 2019-09-08 stsp err = got_privsep_send_stop(imsg_fds[0]);
638 aba9c984 2019-09-08 stsp child_err = got_privsep_wait_for_child(pid);
639 aba9c984 2019-09-08 stsp if (child_err && err == NULL)
640 aba9c984 2019-09-08 stsp err = child_err;
642 aba9c984 2019-09-08 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
643 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
644 aba9c984 2019-09-08 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
645 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
646 aba9c984 2019-09-08 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
647 aba9c984 2019-09-08 stsp err = got_error_from_errno2("close", gitconfig_path);
648 aba9c984 2019-09-08 stsp free(ibuf);
649 c9956ddf 2019-09-08 stsp return err;
652 c9956ddf 2019-09-08 stsp static const struct got_error *
653 c9956ddf 2019-09-08 stsp read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
655 c9956ddf 2019-09-08 stsp const struct got_error *err = NULL;
656 c9956ddf 2019-09-08 stsp char *repo_gitconfig_path = NULL;
658 c9956ddf 2019-09-08 stsp if (global_gitconfig_path) {
659 c9956ddf 2019-09-08 stsp /* Read settings from ~/.gitconfig. */
660 c9956ddf 2019-09-08 stsp int dummy_repo_version;
661 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&dummy_repo_version,
662 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_name,
663 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_email,
664 20b7abb3 2020-10-22 stsp NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
666 c9956ddf 2019-09-08 stsp return err;
669 c9956ddf 2019-09-08 stsp /* Read repository's .git/config file. */
670 b46f3e71 2020-03-18 stsp repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
671 b46f3e71 2020-03-18 stsp if (repo_gitconfig_path == NULL)
672 b46f3e71 2020-03-18 stsp return got_error_from_errno("got_repo_get_path_gitconfig");
674 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
675 c9956ddf 2019-09-08 stsp &repo->gitconfig_author_name, &repo->gitconfig_author_email,
676 cd95becd 2019-11-29 stsp &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
677 20b7abb3 2020-10-22 stsp &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
678 20b7abb3 2020-10-22 stsp repo_gitconfig_path);
682 3e0f95fc 2022-07-23 stsp if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
685 3e0f95fc 2022-07-23 stsp for (i = 0; i < repo->ngitconfig_remotes; i++) {
686 3e0f95fc 2022-07-23 stsp got_repo_free_remote_repo_data(
687 3e0f95fc 2022-07-23 stsp &repo->gitconfig_remotes[i]);
689 3e0f95fc 2022-07-23 stsp free(repo->gitconfig_remotes);
690 4fa16b39 2022-07-24 op repo->gitconfig_remotes = NULL;
691 3e0f95fc 2022-07-23 stsp repo->ngitconfig_remotes = 0;
693 3e0f95fc 2022-07-23 stsp free(repo->gitconfig_author_name);
694 3e0f95fc 2022-07-23 stsp repo->gitconfig_author_name = NULL;
695 3e0f95fc 2022-07-23 stsp free(repo->gitconfig_author_email);
696 3e0f95fc 2022-07-23 stsp repo->gitconfig_author_email = NULL;
698 4fa16b39 2022-07-24 op free(repo->global_gitconfig_author_name);
699 4fa16b39 2022-07-24 op repo->global_gitconfig_author_name = NULL;
700 4fa16b39 2022-07-24 op free(repo->global_gitconfig_author_email);
701 4fa16b39 2022-07-24 op repo->global_gitconfig_author_email = NULL;
705 c9956ddf 2019-09-08 stsp free(repo_gitconfig_path);
706 257add31 2020-09-09 stsp return err;
709 257add31 2020-09-09 stsp static const struct got_error *
710 257add31 2020-09-09 stsp read_gotconfig(struct got_repository *repo)
712 257add31 2020-09-09 stsp const struct got_error *err = NULL;
713 257add31 2020-09-09 stsp char *gotconfig_path;
715 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
716 257add31 2020-09-09 stsp if (gotconfig_path == NULL)
717 257add31 2020-09-09 stsp return got_error_from_errno("got_repo_get_path_gotconfig");
719 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
720 257add31 2020-09-09 stsp free(gotconfig_path);
721 85f51bba 2018-07-16 stsp return err;
724 20b7abb3 2020-10-22 stsp /* Supported repository format extensions. */
725 f134ecfd 2022-03-19 naddy static const char *const repo_extensions[] = {
726 20b7abb3 2020-10-22 stsp "noop", /* Got supports repository format version 1. */
727 2252c019 2021-07-03 stsp "preciousObjects", /* Supported by gotadmin cleanup. */
728 20b7abb3 2020-10-22 stsp "worktreeConfig", /* Got does not care about Git work trees. */
731 85f51bba 2018-07-16 stsp const struct got_error *
732 c9956ddf 2019-09-08 stsp got_repo_open(struct got_repository **repop, const char *path,
733 0ae84acc 2022-06-15 tracey const char *global_gitconfig_path, int *pack_fds)
735 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
736 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
737 dbb02f4d 2020-12-04 stsp char *repo_path = NULL;
738 0ae84acc 2022-06-15 tracey size_t i, j = 0;
739 6c414261 2021-03-30 stsp struct rlimit rl;
741 85f51bba 2018-07-16 stsp *repop = NULL;
743 6c414261 2021-03-30 stsp if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
744 6c414261 2021-03-30 stsp return got_error_from_errno("getrlimit");
746 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
747 c560c1f5 2022-03-14 op if (repo == NULL)
748 c560c1f5 2022-03-14 op return got_error_from_errno("calloc");
750 f8b19efd 2021-10-13 stsp RB_INIT(&repo->packidx_bloom_filters);
751 9b576444 2022-03-14 stsp TAILQ_INIT(&repo->packidx_paths);
753 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
754 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
755 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
756 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
759 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
760 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
763 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
764 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
767 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
768 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
771 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
772 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
775 d3c116bf 2021-10-15 stsp err = got_object_cache_init(&repo->rawcache,
776 d3c116bf 2021-10-15 stsp GOT_OBJECT_CACHE_TYPE_RAW);
780 6c414261 2021-03-30 stsp repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
781 6c414261 2021-03-30 stsp if (repo->pack_cache_size > rl.rlim_cur / 8)
782 6c414261 2021-03-30 stsp repo->pack_cache_size = rl.rlim_cur / 8;
783 57160834 2022-05-31 stsp for (i = 0; i < nitems(repo->packs); i++) {
784 57160834 2022-05-31 stsp if (i < repo->pack_cache_size) {
785 0ae84acc 2022-06-15 tracey repo->packs[i].basefd = pack_fds[j++];
786 0ae84acc 2022-06-15 tracey repo->packs[i].accumfd = pack_fds[j++];
788 57160834 2022-05-31 stsp repo->packs[i].basefd = -1;
789 57160834 2022-05-31 stsp repo->packs[i].accumfd = -1;
792 61af9b21 2022-06-28 stsp repo->pinned_pack = -1;
793 61af9b21 2022-06-28 stsp repo->pinned_packidx = -1;
794 61af9b21 2022-06-28 stsp repo->pinned_pid = 0;
796 dbb02f4d 2020-12-04 stsp repo_path = realpath(path, NULL);
797 aff6eea4 2020-10-20 stsp if (repo_path == NULL) {
798 dbb02f4d 2020-12-04 stsp err = got_error_from_errno2("realpath", path);
803 aff6eea4 2020-10-20 stsp char *parent_path;
805 aff6eea4 2020-10-20 stsp err = open_repo(repo, repo_path);
806 85f51bba 2018-07-16 stsp if (err == NULL)
808 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
810 aff6eea4 2020-10-20 stsp if (repo_path[0] == '/' && repo_path[1] == '\0') {
811 0c93d204 2020-10-20 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
814 aff6eea4 2020-10-20 stsp err = got_path_dirname(&parent_path, repo_path);
817 aff6eea4 2020-10-20 stsp free(repo_path);
818 aff6eea4 2020-10-20 stsp repo_path = parent_path;
821 257add31 2020-09-09 stsp err = read_gotconfig(repo);
825 c9956ddf 2019-09-08 stsp err = read_gitconfig(repo, global_gitconfig_path);
828 b5d22c38 2022-07-21 florian if (repo->gitconfig_repository_format_version != 0) {
829 aba9c984 2019-09-08 stsp err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
830 b5d22c38 2022-07-21 florian goto done;
832 20b7abb3 2020-10-22 stsp for (i = 0; i < repo->nextensions; i++) {
833 20b7abb3 2020-10-22 stsp char *ext = repo->extensions[i];
834 20b7abb3 2020-10-22 stsp int j, supported = 0;
835 20b7abb3 2020-10-22 stsp for (j = 0; j < nitems(repo_extensions); j++) {
836 20b7abb3 2020-10-22 stsp if (strcmp(ext, repo_extensions[j]) == 0) {
837 20b7abb3 2020-10-22 stsp supported = 1;
841 20b7abb3 2020-10-22 stsp if (!supported) {
842 20b7abb3 2020-10-22 stsp err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
847 9b576444 2022-03-14 stsp err = got_repo_list_packidx(&repo->packidx_paths, repo);
850 5c2f5761 2018-09-19 stsp got_repo_close(repo);
852 85f51bba 2018-07-16 stsp *repop = repo;
853 aff6eea4 2020-10-20 stsp free(repo_path);
854 92af5469 2017-11-05 stsp return err;
857 ad242220 2018-09-08 stsp const struct got_error *
858 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
860 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
861 f8b19efd 2021-10-13 stsp struct got_packidx_bloom_filter *bf;
862 9b576444 2022-03-14 stsp struct got_pathlist_entry *pe;
865 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
866 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
868 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
871 f8b19efd 2021-10-13 stsp while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
872 f8b19efd 2021-10-13 stsp &repo->packidx_bloom_filters))) {
873 f8b19efd 2021-10-13 stsp RB_REMOVE(got_packidx_bloom_filter_tree,
874 f8b19efd 2021-10-13 stsp &repo->packidx_bloom_filters, bf);
875 794662a4 2022-08-31 stsp bloom_free(bf->bloom);
876 b343c297 2021-10-11 stsp free(bf->bloom);
880 0ae84acc 2022-06-15 tracey for (i = 0; i < repo->pack_cache_size; i++)
881 13242195 2022-05-31 stsp if (repo->packs[i].path_packfile)
882 0ae84acc 2022-06-15 tracey if (repo->packs[i].path_packfile)
883 0ae84acc 2022-06-15 tracey got_pack_close(&repo->packs[i]);
885 4027f31a 2017-11-04 stsp free(repo->path);
886 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
888 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
889 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
890 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
891 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
892 d3c116bf 2021-10-15 stsp got_object_cache_close(&repo->rawcache);
894 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
895 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
897 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
898 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
899 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
900 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
901 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
902 ad242220 2018-09-08 stsp if (child_err && err == NULL)
903 ad242220 2018-09-08 stsp err = child_err;
904 08578a35 2021-01-22 stsp if (close(repo->privsep_children[i].imsg_fd) == -1 &&
905 3a6ce05a 2019-02-11 stsp err == NULL)
906 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
909 1d0f4054 2021-06-17 stsp if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
910 1d0f4054 2021-06-17 stsp err == NULL)
911 1d0f4054 2021-06-17 stsp err = got_error_from_errno("close");
913 50b0790e 2020-09-11 stsp if (repo->gotconfig)
914 50b0790e 2020-09-11 stsp got_gotconfig_free(repo->gotconfig);
915 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_name);
916 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_email);
917 b8adfa55 2020-09-25 stsp for (i = 0; i < repo->ngitconfig_remotes; i++)
918 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
919 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes);
920 20b7abb3 2020-10-22 stsp for (i = 0; i < repo->nextensions; i++)
921 20b7abb3 2020-10-22 stsp free(repo->extensions[i]);
922 20b7abb3 2020-10-22 stsp free(repo->extensions);
924 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry)
925 9b576444 2022-03-14 stsp free((void *)pe->path);
926 9b576444 2022-03-14 stsp got_pathlist_free(&repo->packidx_paths);
927 4027f31a 2017-11-04 stsp free(repo);
929 ad242220 2018-09-08 stsp return err;
933 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(struct got_remote_repo *repo)
937 b8adfa55 2020-09-25 stsp free(repo->name);
938 b8adfa55 2020-09-25 stsp repo->name = NULL;
939 6480c871 2021-08-30 stsp free(repo->fetch_url);
940 6480c871 2021-08-30 stsp repo->fetch_url = NULL;
941 6480c871 2021-08-30 stsp free(repo->send_url);
942 6480c871 2021-08-30 stsp repo->send_url = NULL;
943 6480c871 2021-08-30 stsp for (i = 0; i < repo->nfetch_branches; i++)
944 6480c871 2021-08-30 stsp free(repo->fetch_branches[i]);
945 6480c871 2021-08-30 stsp free(repo->fetch_branches);
946 6480c871 2021-08-30 stsp repo->fetch_branches = NULL;
947 6480c871 2021-08-30 stsp repo->nfetch_branches = 0;
948 6480c871 2021-08-30 stsp for (i = 0; i < repo->nsend_branches; i++)
949 6480c871 2021-08-30 stsp free(repo->send_branches[i]);
950 6480c871 2021-08-30 stsp free(repo->send_branches);
951 6480c871 2021-08-30 stsp repo->send_branches = NULL;
952 6480c871 2021-08-30 stsp repo->nsend_branches = 0;
955 04ca23f4 2018-07-16 stsp const struct got_error *
956 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
957 8fa913ec 2020-11-14 stsp const char *input_path)
959 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
960 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
961 e83c0634 2020-01-27 stsp size_t repolen, len;
962 e83c0634 2020-01-27 stsp char *canonpath, *path = NULL;
964 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
966 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
967 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
968 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
971 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
975 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
977 8fa913ec 2020-11-14 stsp if (canonpath[0] == '\0') {
978 23721109 2018-10-22 stsp path = strdup(canonpath);
979 b70703ad 2019-03-18 stsp if (path == NULL) {
980 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
984 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
985 04ca23f4 2018-07-16 stsp if (path == NULL) {
986 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
987 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
992 b70703ad 2019-03-18 stsp * Path is not on disk.
993 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
995 b70703ad 2019-03-18 stsp path = strdup(canonpath);
996 b70703ad 2019-03-18 stsp if (path == NULL) {
997 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1002 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
1003 04ca23f4 2018-07-16 stsp len = strlen(path);
1006 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
1007 04ca23f4 2018-07-16 stsp free(path);
1008 04ca23f4 2018-07-16 stsp path = strdup("");
1009 04ca23f4 2018-07-16 stsp if (path == NULL) {
1010 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1011 04ca23f4 2018-07-16 stsp goto done;
1013 65aa7d1c 2020-01-28 stsp } else if (len > repolen &&
1014 65aa7d1c 2020-01-28 stsp got_path_is_child(path, repo_abspath, repolen)) {
1015 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
1016 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
1018 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
1019 eef9542c 2020-10-22 stsp * database. Treat input as repository-relative.
1021 abc59930 2021-09-05 naddy free(path);
1022 abc59930 2021-09-05 naddy path = canonpath;
1023 abc59930 2021-09-05 naddy canonpath = NULL;
1025 04ca23f4 2018-07-16 stsp char *child;
1026 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
1027 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
1028 04ca23f4 2018-07-16 stsp repo_abspath, path);
1030 04ca23f4 2018-07-16 stsp goto done;
1031 04ca23f4 2018-07-16 stsp free(path);
1032 04ca23f4 2018-07-16 stsp path = child;
1036 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
1037 eef9542c 2020-10-22 stsp * Treat input as repository-relative.
1039 abc59930 2021-09-05 naddy free(path);
1040 abc59930 2021-09-05 naddy path = canonpath;
1041 abc59930 2021-09-05 naddy canonpath = NULL;
1045 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
1046 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
1047 04ca23f4 2018-07-16 stsp char *abspath;
1048 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
1049 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1050 04ca23f4 2018-07-16 stsp goto done;
1052 04ca23f4 2018-07-16 stsp free(path);
1053 04ca23f4 2018-07-16 stsp path = abspath;
1057 04ca23f4 2018-07-16 stsp free(canonpath);
1059 04ca23f4 2018-07-16 stsp free(path);
1061 04ca23f4 2018-07-16 stsp *in_repo_path = path;
1062 1510f469 2018-09-09 stsp return err;
1065 e1a68182 2020-01-07 stsp static const struct got_error *
1066 e1a68182 2020-01-07 stsp cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1067 e1a68182 2020-01-07 stsp const char *path_packidx)
1069 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1072 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1073 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
1075 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
1076 e1a68182 2020-01-07 stsp path_packidx) == 0) {
1077 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1080 6c414261 2021-03-30 stsp if (i == repo->pack_cache_size) {
1083 61af9b21 2022-06-28 stsp } while (i > 0 && repo->pinned_packidx >= 0 &&
1084 61af9b21 2022-06-28 stsp i == repo->pinned_packidx);
1085 7a0b1068 2021-10-11 stsp err = got_packidx_close(repo->packidx_cache[i]);
1087 1510f469 2018-09-09 stsp return err;
1090 7a0b1068 2021-10-11 stsp repo->packidx_cache[i] = packidx;
1092 1510f469 2018-09-09 stsp return NULL;
1096 1124fe40 2021-07-07 stsp got_repo_is_packidx_filename(const char *name, size_t len)
1098 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
1101 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1104 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1105 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1111 f8b19efd 2021-10-13 stsp static struct got_packidx_bloom_filter *
1112 f8b19efd 2021-10-13 stsp get_packidx_bloom_filter(struct got_repository *repo,
1113 f8b19efd 2021-10-13 stsp const char *path, size_t path_len)
1115 f8b19efd 2021-10-13 stsp struct got_packidx_bloom_filter key;
1117 f8b19efd 2021-10-13 stsp if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1118 f8b19efd 2021-10-13 stsp return NULL; /* XXX */
1119 f8b19efd 2021-10-13 stsp key.path_len = path_len;
1121 f8b19efd 2021-10-13 stsp return RB_FIND(got_packidx_bloom_filter_tree,
1122 f8b19efd 2021-10-13 stsp &repo->packidx_bloom_filters, &key);
1126 67fd6849 2022-02-13 stsp got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1127 b343c297 2021-10-11 stsp const char *path_packidx, struct got_object_id *id)
1129 b343c297 2021-10-11 stsp struct got_packidx_bloom_filter *bf;
1131 f8b19efd 2021-10-13 stsp bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1133 f8b19efd 2021-10-13 stsp return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1135 b343c297 2021-10-11 stsp /* No bloom filter means this pack index must be searched. */
1139 b343c297 2021-10-11 stsp static const struct got_error *
1140 b343c297 2021-10-11 stsp add_packidx_bloom_filter(struct got_repository *repo,
1141 b343c297 2021-10-11 stsp struct got_packidx *packidx, const char *path_packidx)
1143 b343c297 2021-10-11 stsp int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1144 b343c297 2021-10-11 stsp struct got_packidx_bloom_filter *bf;
1145 b343c297 2021-10-11 stsp size_t len;
1148 b343c297 2021-10-11 stsp * Don't use bloom filters for very large pack index files.
1149 b343c297 2021-10-11 stsp * Large pack files will contain a relatively large fraction
1150 b343c297 2021-10-11 stsp * of our objects so we will likely need to visit them anyway.
1151 b343c297 2021-10-11 stsp * The more objects a pack file contains the higher the probability
1152 b343c297 2021-10-11 stsp * of a false-positive match from the bloom filter. And reading
1153 b343c297 2021-10-11 stsp * all object IDs from a large pack index file can be expensive.
1155 b343c297 2021-10-11 stsp if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1156 b343c297 2021-10-11 stsp return NULL;
1158 b343c297 2021-10-11 stsp /* Do we already have a filter for this pack index? */
1159 f8b19efd 2021-10-13 stsp if (get_packidx_bloom_filter(repo, path_packidx,
1160 f8b19efd 2021-10-13 stsp strlen(path_packidx)) != NULL)
1161 f8b19efd 2021-10-13 stsp return NULL;
1163 b343c297 2021-10-11 stsp bf = calloc(1, sizeof(*bf));
1164 b343c297 2021-10-11 stsp if (bf == NULL)
1165 b343c297 2021-10-11 stsp return got_error_from_errno("calloc");
1166 b343c297 2021-10-11 stsp bf->bloom = calloc(1, sizeof(*bf->bloom));
1167 b343c297 2021-10-11 stsp if (bf->bloom == NULL) {
1169 b343c297 2021-10-11 stsp return got_error_from_errno("calloc");
1172 f8b19efd 2021-10-13 stsp len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1173 f8b19efd 2021-10-13 stsp if (len >= sizeof(bf->path)) {
1174 b343c297 2021-10-11 stsp free(bf->bloom);
1176 b343c297 2021-10-11 stsp return got_error(GOT_ERR_NO_SPACE);
1178 f8b19efd 2021-10-13 stsp bf->path_len = len;
1180 b343c297 2021-10-11 stsp /* Minimum size supported by our bloom filter is 1000 entries. */
1181 b343c297 2021-10-11 stsp bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1182 b343c297 2021-10-11 stsp for (i = 0; i < nobjects; i++) {
1183 b343c297 2021-10-11 stsp struct got_packidx_object_id *id;
1184 b343c297 2021-10-11 stsp id = &packidx->hdr.sorted_ids[i];
1185 b343c297 2021-10-11 stsp bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1188 f8b19efd 2021-10-13 stsp RB_INSERT(got_packidx_bloom_filter_tree,
1189 f8b19efd 2021-10-13 stsp &repo->packidx_bloom_filters, bf);
1190 b343c297 2021-10-11 stsp return NULL;
1193 1510f469 2018-09-09 stsp const struct got_error *
1194 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1195 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
1197 1510f469 2018-09-09 stsp const struct got_error *err;
1198 9b576444 2022-03-14 stsp struct got_pathlist_entry *pe;
1201 1510f469 2018-09-09 stsp /* Search pack index cache. */
1202 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1203 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
1205 67fd6849 2022-02-13 stsp if (!got_repo_check_packidx_bloom_filter(repo,
1206 b343c297 2021-10-11 stsp repo->packidx_cache[i]->path_packidx, id))
1207 b343c297 2021-10-11 stsp continue; /* object will not be found in this index */
1208 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1209 1510f469 2018-09-09 stsp if (*idx != -1) {
1210 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
1212 87c1ed2b 2020-01-07 stsp * Move this cache entry to the front. Repeatedly
1213 87c1ed2b 2020-01-07 stsp * searching a wrong pack index can be expensive.
1215 87c1ed2b 2020-01-07 stsp if (i > 0) {
1216 7a0b1068 2021-10-11 stsp memmove(&repo->packidx_cache[1],
1217 7a0b1068 2021-10-11 stsp &repo->packidx_cache[0],
1218 7a0b1068 2021-10-11 stsp i * sizeof(repo->packidx_cache[0]));
1219 87c1ed2b 2020-01-07 stsp repo->packidx_cache[0] = *packidx;
1220 61af9b21 2022-06-28 stsp if (repo->pinned_packidx >= 0 &&
1221 61af9b21 2022-06-28 stsp repo->pinned_packidx < i)
1222 61af9b21 2022-06-28 stsp repo->pinned_packidx++;
1223 61af9b21 2022-06-28 stsp else if (repo->pinned_packidx == i)
1224 61af9b21 2022-06-28 stsp repo->pinned_packidx = 0;
1226 1510f469 2018-09-09 stsp return NULL;
1229 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
1231 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1232 9b576444 2022-03-14 stsp const char *path_packidx = pe->path;
1233 e1a68182 2020-01-07 stsp int is_cached = 0;
1235 67fd6849 2022-02-13 stsp if (!got_repo_check_packidx_bloom_filter(repo,
1236 9b576444 2022-03-14 stsp pe->path, id))
1237 b343c297 2021-10-11 stsp continue; /* object will not be found in this index */
1239 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1240 e1a68182 2020-01-07 stsp if (repo->packidx_cache[i] == NULL)
1242 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
1243 e1a68182 2020-01-07 stsp path_packidx) == 0) {
1244 e1a68182 2020-01-07 stsp is_cached = 1;
1248 9b576444 2022-03-14 stsp if (is_cached)
1249 e1a68182 2020-01-07 stsp continue; /* already searched */
1251 6d5a9006 2020-12-16 yzhong err = got_packidx_open(packidx, got_repo_get_fd(repo),
1252 6d5a9006 2020-12-16 yzhong path_packidx, 0);
1254 e1a68182 2020-01-07 stsp goto done;
1256 b343c297 2021-10-11 stsp err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1258 b343c297 2021-10-11 stsp goto done;
1260 e1a68182 2020-01-07 stsp err = cache_packidx(repo, *packidx, path_packidx);
1262 1510f469 2018-09-09 stsp goto done;
1264 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
1265 1510f469 2018-09-09 stsp if (*idx != -1) {
1266 1510f469 2018-09-09 stsp err = NULL; /* found the object */
1267 1510f469 2018-09-09 stsp goto done;
1271 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
1273 04ca23f4 2018-07-16 stsp return err;
1276 67fd6849 2022-02-13 stsp const struct got_error *
1277 67fd6849 2022-02-13 stsp got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1278 67fd6849 2022-02-13 stsp struct got_repository *repo)
1280 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1281 67fd6849 2022-02-13 stsp DIR *packdir = NULL;
1282 67fd6849 2022-02-13 stsp struct dirent *dent;
1283 67fd6849 2022-02-13 stsp char *path_packidx = NULL;
1284 67fd6849 2022-02-13 stsp int packdir_fd;
1286 67fd6849 2022-02-13 stsp packdir_fd = openat(got_repo_get_fd(repo),
1287 67fd6849 2022-02-13 stsp GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1288 67fd6849 2022-02-13 stsp if (packdir_fd == -1) {
1289 67fd6849 2022-02-13 stsp return got_error_from_errno_fmt("openat: %s/%s",
1290 67fd6849 2022-02-13 stsp got_repo_get_path_git_dir(repo),
1291 67fd6849 2022-02-13 stsp GOT_OBJECTS_PACK_DIR);
1294 67fd6849 2022-02-13 stsp packdir = fdopendir(packdir_fd);
1295 67fd6849 2022-02-13 stsp if (packdir == NULL) {
1296 67fd6849 2022-02-13 stsp err = got_error_from_errno("fdopendir");
1297 67fd6849 2022-02-13 stsp goto done;
1300 67fd6849 2022-02-13 stsp while ((dent = readdir(packdir)) != NULL) {
1301 67fd6849 2022-02-13 stsp if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1304 67fd6849 2022-02-13 stsp if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1305 67fd6849 2022-02-13 stsp dent->d_name) == -1) {
1306 67fd6849 2022-02-13 stsp err = got_error_from_errno("asprintf");
1307 67fd6849 2022-02-13 stsp path_packidx = NULL;
1311 67fd6849 2022-02-13 stsp err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1317 67fd6849 2022-02-13 stsp free(path_packidx);
1318 67fd6849 2022-02-13 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
1319 67fd6849 2022-02-13 stsp err = got_error_from_errno("closedir");
1320 67fd6849 2022-02-13 stsp return err;
1323 67fd6849 2022-02-13 stsp const struct got_error *
1324 67fd6849 2022-02-13 stsp got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1325 67fd6849 2022-02-13 stsp struct got_repository *repo)
1327 67fd6849 2022-02-13 stsp const struct got_error *err;
1330 67fd6849 2022-02-13 stsp *packidx = NULL;
1332 67fd6849 2022-02-13 stsp /* Search pack index cache. */
1333 67fd6849 2022-02-13 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1334 67fd6849 2022-02-13 stsp if (repo->packidx_cache[i] == NULL)
1336 67fd6849 2022-02-13 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
1337 67fd6849 2022-02-13 stsp path_packidx) == 0) {
1338 67fd6849 2022-02-13 stsp *packidx = repo->packidx_cache[i];
1339 67fd6849 2022-02-13 stsp return NULL;
1342 67fd6849 2022-02-13 stsp /* No luck. Search the filesystem. */
1344 67fd6849 2022-02-13 stsp err = got_packidx_open(packidx, got_repo_get_fd(repo),
1345 67fd6849 2022-02-13 stsp path_packidx, 0);
1347 67fd6849 2022-02-13 stsp return err;
1349 67fd6849 2022-02-13 stsp err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1351 67fd6849 2022-02-13 stsp goto done;
1353 67fd6849 2022-02-13 stsp err = cache_packidx(repo, *packidx, path_packidx);
1355 67fd6849 2022-02-13 stsp if (err) {
1356 67fd6849 2022-02-13 stsp got_packidx_close(*packidx);
1357 67fd6849 2022-02-13 stsp *packidx = NULL;
1359 67fd6849 2022-02-13 stsp return err;
1362 1510f469 2018-09-09 stsp static const struct got_error *
1363 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
1365 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1366 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1367 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
1368 1510f469 2018-09-09 stsp ssize_t n;
1370 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
1371 1510f469 2018-09-09 stsp if (n < 0)
1372 638f9024 2019-05-13 stsp return got_error_from_errno("read");
1373 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
1374 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1376 78fb0967 2020-09-09 naddy if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1377 78fb0967 2020-09-09 naddy be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1378 78fb0967 2020-09-09 naddy be32toh(hdr.nobjects) != totobj)
1379 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
1381 1510f469 2018-09-09 stsp return err;
1384 1510f469 2018-09-09 stsp static const struct got_error *
1385 6d5a9006 2020-12-16 yzhong open_packfile(int *fd, struct got_repository *repo,
1386 6d5a9006 2020-12-16 yzhong const char *relpath, struct got_packidx *packidx)
1388 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1390 e7ae0baf 2021-12-31 stsp *fd = openat(got_repo_get_fd(repo), relpath,
1391 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1392 1510f469 2018-09-09 stsp if (*fd == -1)
1393 6d5a9006 2020-12-16 yzhong return got_error_from_errno_fmt("openat: %s/%s",
1394 6d5a9006 2020-12-16 yzhong got_repo_get_path_git_dir(repo), relpath);
1396 1510f469 2018-09-09 stsp if (packidx) {
1397 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
1398 1510f469 2018-09-09 stsp if (err) {
1399 1510f469 2018-09-09 stsp close(*fd);
1404 1510f469 2018-09-09 stsp return err;
1407 1510f469 2018-09-09 stsp const struct got_error *
1408 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1409 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
1411 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1412 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1413 ff563a3d 2019-05-23 stsp struct stat sb;
1416 1510f469 2018-09-09 stsp if (packp)
1417 1510f469 2018-09-09 stsp *packp = NULL;
1419 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1420 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1421 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1423 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1424 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1427 6c414261 2021-03-30 stsp if (i == repo->pack_cache_size) {
1428 6a881297 2022-06-02 stsp struct got_pack tmp;
1431 61af9b21 2022-06-28 stsp } while (i > 0 && repo->pinned_pack >= 0 &&
1432 61af9b21 2022-06-28 stsp i == repo->pinned_pack);
1433 61af9b21 2022-06-28 stsp err = got_pack_close(&repo->packs[i]);
1435 1510f469 2018-09-09 stsp return err;
1436 61af9b21 2022-06-28 stsp if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1437 57160834 2022-05-31 stsp return got_error_from_errno("ftruncate");
1438 61af9b21 2022-06-28 stsp if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1439 61af9b21 2022-06-28 stsp return got_error_from_errno("ftruncate");
1440 5e91dae4 2022-08-30 stsp memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1441 61af9b21 2022-06-28 stsp memcpy(&repo->packs[i], &repo->packs[0],
1442 61af9b21 2022-06-28 stsp sizeof(repo->packs[i]));
1443 6a881297 2022-06-02 stsp memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1444 61af9b21 2022-06-28 stsp if (repo->pinned_pack == 0)
1445 61af9b21 2022-06-28 stsp repo->pinned_pack = i;
1446 61af9b21 2022-06-28 stsp else if (repo->pinned_pack == i)
1447 61af9b21 2022-06-28 stsp repo->pinned_pack = 0;
1451 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1453 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
1454 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
1455 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1456 1510f469 2018-09-09 stsp goto done;
1459 6d5a9006 2020-12-16 yzhong err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1461 1510f469 2018-09-09 stsp goto done;
1463 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
1464 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
1465 1510f469 2018-09-09 stsp goto done;
1467 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
1469 90636195 2018-09-11 stsp pack->privsep_child = NULL;
1471 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
1472 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1473 1510f469 2018-09-09 stsp pack->fd, 0);
1474 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
1475 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
1477 3a11398b 2019-02-21 stsp goto done;
1479 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
1483 1510f469 2018-09-09 stsp if (err) {
1484 1510f469 2018-09-09 stsp if (pack) {
1485 1510f469 2018-09-09 stsp free(pack->path_packfile);
1486 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
1488 1510f469 2018-09-09 stsp } else if (packp)
1489 1510f469 2018-09-09 stsp *packp = pack;
1490 1510f469 2018-09-09 stsp return err;
1493 1510f469 2018-09-09 stsp struct got_pack *
1494 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1496 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1499 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1500 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1501 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1503 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1504 1510f469 2018-09-09 stsp return pack;
1507 61af9b21 2022-06-28 stsp return NULL;
1510 61af9b21 2022-06-28 stsp const struct got_error *
1511 61af9b21 2022-06-28 stsp got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1512 61af9b21 2022-06-28 stsp struct got_pack *pack)
1515 61af9b21 2022-06-28 stsp int pinned_pack = -1, pinned_packidx = -1;
1517 61af9b21 2022-06-28 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1518 61af9b21 2022-06-28 stsp if (repo->packidx_cache[i] &&
1519 61af9b21 2022-06-28 stsp strcmp(repo->packidx_cache[i]->path_packidx,
1520 61af9b21 2022-06-28 stsp packidx->path_packidx) == 0)
1521 61af9b21 2022-06-28 stsp pinned_packidx = i;
1522 61af9b21 2022-06-28 stsp if (repo->packs[i].path_packfile &&
1523 61af9b21 2022-06-28 stsp strcmp(repo->packs[i].path_packfile,
1524 61af9b21 2022-06-28 stsp pack->path_packfile) == 0)
1525 61af9b21 2022-06-28 stsp pinned_pack = i;
1528 61af9b21 2022-06-28 stsp if (pinned_packidx == -1 || pinned_pack == -1)
1529 61af9b21 2022-06-28 stsp return got_error(GOT_ERR_PIN_PACK);
1531 61af9b21 2022-06-28 stsp repo->pinned_pack = pinned_pack;
1532 61af9b21 2022-06-28 stsp repo->pinned_packidx = pinned_packidx;
1533 61af9b21 2022-06-28 stsp repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1534 61af9b21 2022-06-28 stsp return NULL;
1537 61af9b21 2022-06-28 stsp struct got_pack *
1538 61af9b21 2022-06-28 stsp got_repo_get_pinned_pack(struct got_repository *repo)
1540 61af9b21 2022-06-28 stsp if (repo->pinned_pack >= 0 &&
1541 61af9b21 2022-06-28 stsp repo->pinned_pack < repo->pack_cache_size)
1542 61af9b21 2022-06-28 stsp return &repo->packs[repo->pinned_pack];
1544 2c7829a4 2019-06-17 stsp return NULL;
1548 61af9b21 2022-06-28 stsp got_repo_unpin_pack(struct got_repository *repo)
1550 61af9b21 2022-06-28 stsp repo->pinned_packidx = -1;
1551 61af9b21 2022-06-28 stsp repo->pinned_pack = -1;
1552 61af9b21 2022-06-28 stsp repo->pinned_pid = 0;
1555 2c7829a4 2019-06-17 stsp const struct got_error *
1556 2c7829a4 2019-06-17 stsp got_repo_init(const char *repo_path)
1558 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
1559 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
1560 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
1561 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
1562 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
1564 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
1565 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
1566 5d67f40d 2019-11-08 stsp const char *headref_str = "ref: refs/heads/main";
1567 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
1568 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
1569 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
1570 2c7829a4 2019-06-17 stsp "\tbare = true\n";
1571 2c7829a4 2019-06-17 stsp char *path;
1574 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
1575 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
1577 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
1578 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1579 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1581 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
1582 2c7829a4 2019-06-17 stsp free(path);
1584 2c7829a4 2019-06-17 stsp return err;
1587 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1588 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1589 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
1590 2c7829a4 2019-06-17 stsp free(path);
1592 2c7829a4 2019-06-17 stsp return err;
1594 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1595 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1596 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
1597 2c7829a4 2019-06-17 stsp free(path);
1599 2c7829a4 2019-06-17 stsp return err;
1601 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1602 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1603 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
1604 2c7829a4 2019-06-17 stsp free(path);
1606 2c7829a4 2019-06-17 stsp return err;
1608 1510f469 2018-09-09 stsp return NULL;
1611 e09a504c 2019-06-28 stsp static const struct got_error *
1612 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
1613 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
1615 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1616 9b576444 2022-03-14 stsp struct got_object_id_queue matched_ids;
1617 9b576444 2022-03-14 stsp struct got_pathlist_entry *pe;
1619 dbdddfee 2021-06-23 naddy STAILQ_INIT(&matched_ids);
1621 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1622 9b576444 2022-03-14 stsp const char *path_packidx = pe->path;
1623 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
1624 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
1626 6d5a9006 2020-12-16 yzhong err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1627 6d5a9006 2020-12-16 yzhong path_packidx, 0);
1631 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
1632 4277420a 2019-06-29 stsp packidx, id_str_prefix);
1633 4277420a 2019-06-29 stsp if (err) {
1634 4277420a 2019-06-29 stsp got_packidx_close(packidx);
1637 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
1641 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &matched_ids, entry) {
1642 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1643 dd88155e 2019-06-29 stsp int matched_type;
1644 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1645 d7b5a0e8 2022-04-20 stsp &qid->id);
1647 dd88155e 2019-06-29 stsp goto done;
1648 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1651 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
1652 d7b5a0e8 2022-04-20 stsp *unique_id = got_object_id_dup(&qid->id);
1653 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
1654 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
1655 dd88155e 2019-06-29 stsp goto done;
1658 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(*unique_id,
1659 d7b5a0e8 2022-04-20 stsp &qid->id) == 0)
1660 1accf02b 2020-01-05 stsp continue; /* packed multiple times */
1661 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1662 561c3678 2019-07-02 stsp goto done;
1667 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
1668 e09a504c 2019-06-28 stsp if (err) {
1669 e09a504c 2019-06-28 stsp free(*unique_id);
1670 e09a504c 2019-06-28 stsp *unique_id = NULL;
1672 e09a504c 2019-06-28 stsp return err;
1675 e09a504c 2019-06-28 stsp static const struct got_error *
1676 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1677 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
1678 e09a504c 2019-06-28 stsp struct got_repository *repo)
1680 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1681 e09a504c 2019-06-28 stsp char *path;
1682 e09a504c 2019-06-28 stsp DIR *dir = NULL;
1683 e09a504c 2019-06-28 stsp struct dirent *dent;
1684 e09a504c 2019-06-28 stsp struct got_object_id id;
1686 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1687 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1688 e09a504c 2019-06-28 stsp goto done;
1691 e09a504c 2019-06-28 stsp dir = opendir(path);
1692 e09a504c 2019-06-28 stsp if (dir == NULL) {
1693 4277420a 2019-06-29 stsp if (errno == ENOENT) {
1694 4277420a 2019-06-29 stsp err = NULL;
1695 4277420a 2019-06-29 stsp goto done;
1697 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
1698 e09a504c 2019-06-28 stsp goto done;
1700 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
1701 e09a504c 2019-06-28 stsp char *id_str;
1704 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
1705 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
1708 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1709 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1710 e09a504c 2019-06-28 stsp goto done;
1713 e09a504c 2019-06-28 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
1717 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1718 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1720 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1721 52d1d0d9 2019-07-07 stsp if (cmp != 0) {
1722 e09a504c 2019-06-28 stsp free(id_str);
1726 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1727 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1728 dd88155e 2019-06-29 stsp int matched_type;
1729 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1732 dd88155e 2019-06-29 stsp goto done;
1733 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1736 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1737 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1738 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1739 e09a504c 2019-06-28 stsp free(id_str);
1740 e09a504c 2019-06-28 stsp goto done;
1743 1accf02b 2020-01-05 stsp if (got_object_id_cmp(*unique_id, &id) == 0)
1744 1accf02b 2020-01-05 stsp continue; /* both packed and loose */
1745 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1746 e09a504c 2019-06-28 stsp free(id_str);
1747 e09a504c 2019-06-28 stsp goto done;
1751 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1752 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1753 e09a504c 2019-06-28 stsp if (err) {
1754 e09a504c 2019-06-28 stsp free(*unique_id);
1755 e09a504c 2019-06-28 stsp *unique_id = NULL;
1757 e09a504c 2019-06-28 stsp free(path);
1758 e09a504c 2019-06-28 stsp return err;
1761 e09a504c 2019-06-28 stsp const struct got_error *
1762 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1763 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1765 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1766 e09a504c 2019-06-28 stsp char *path_objects = got_repo_get_path_objects(repo);
1767 e09a504c 2019-06-28 stsp char *object_dir = NULL;
1768 e09a504c 2019-06-28 stsp size_t len;
1771 4277420a 2019-06-29 stsp *id = NULL;
1773 0e6ab4b2 2022-03-10 stsp len = strlen(id_str_prefix);
1774 0e6ab4b2 2022-03-10 stsp if (len > SHA1_DIGEST_STRING_LENGTH - 1)
1775 0e6ab4b2 2022-03-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1777 0e6ab4b2 2022-03-10 stsp for (i = 0; i < len; i++) {
1778 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1780 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1783 e09a504c 2019-06-28 stsp if (len >= 2) {
1784 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1786 83c8b3b8 2019-06-29 stsp goto done;
1787 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1788 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1789 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1790 83c8b3b8 2019-06-29 stsp goto done;
1792 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1793 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1794 e09a504c 2019-06-28 stsp } else if (len == 1) {
1796 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1797 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1799 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1800 83c8b3b8 2019-06-29 stsp goto done;
1802 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1803 dd88155e 2019-06-29 stsp obj_type);
1805 83c8b3b8 2019-06-29 stsp goto done;
1806 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1807 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1809 83c8b3b8 2019-06-29 stsp goto done;
1812 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1813 83c8b3b8 2019-06-29 stsp goto done;
1816 e09a504c 2019-06-28 stsp free(object_dir);
1817 4277420a 2019-06-29 stsp if (err) {
1818 4277420a 2019-06-29 stsp free(*id);
1819 4277420a 2019-06-29 stsp *id = NULL;
1820 138e4f47 2021-10-09 stsp } else if (*id == NULL) {
1821 138e4f47 2021-10-09 stsp switch (obj_type) {
1822 138e4f47 2021-10-09 stsp case GOT_OBJ_TYPE_BLOB:
1823 138e4f47 2021-10-09 stsp err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1824 138e4f47 2021-10-09 stsp GOT_OBJ_LABEL_BLOB, id_str_prefix);
1826 138e4f47 2021-10-09 stsp case GOT_OBJ_TYPE_TREE:
1827 138e4f47 2021-10-09 stsp err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1828 138e4f47 2021-10-09 stsp GOT_OBJ_LABEL_TREE, id_str_prefix);
1830 138e4f47 2021-10-09 stsp case GOT_OBJ_TYPE_COMMIT:
1831 138e4f47 2021-10-09 stsp err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1832 138e4f47 2021-10-09 stsp GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1834 138e4f47 2021-10-09 stsp case GOT_OBJ_TYPE_TAG:
1835 138e4f47 2021-10-09 stsp err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1836 138e4f47 2021-10-09 stsp GOT_OBJ_LABEL_TAG, id_str_prefix);
1839 138e4f47 2021-10-09 stsp err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1844 303e2782 2019-08-09 stsp return err;
1847 303e2782 2019-08-09 stsp const struct got_error *
1848 71a27632 2020-01-15 stsp got_repo_match_object_id(struct got_object_id **id, char **label,
1849 84de9106 2020-12-26 stsp const char *id_str, int obj_type, struct got_reflist_head *refs,
1850 71a27632 2020-01-15 stsp struct got_repository *repo)
1852 71a27632 2020-01-15 stsp const struct got_error *err;
1853 71a27632 2020-01-15 stsp struct got_tag_object *tag;
1854 71a27632 2020-01-15 stsp struct got_reference *ref = NULL;
1856 71a27632 2020-01-15 stsp *id = NULL;
1857 71a27632 2020-01-15 stsp if (label)
1858 71a27632 2020-01-15 stsp *label = NULL;
1860 84de9106 2020-12-26 stsp if (refs) {
1861 237cfda1 2021-10-08 stsp err = got_repo_object_match_tag(&tag, id_str, obj_type,
1862 84de9106 2020-12-26 stsp refs, repo);
1863 71a27632 2020-01-15 stsp if (err == NULL) {
1864 71a27632 2020-01-15 stsp *id = got_object_id_dup(
1865 71a27632 2020-01-15 stsp got_object_tag_get_object_id(tag));
1866 71a27632 2020-01-15 stsp if (*id == NULL)
1867 71a27632 2020-01-15 stsp err = got_error_from_errno("got_object_id_dup");
1868 71a27632 2020-01-15 stsp else if (label && asprintf(label, "refs/tags/%s",
1869 71a27632 2020-01-15 stsp got_object_tag_get_name(tag)) == -1) {
1870 71a27632 2020-01-15 stsp err = got_error_from_errno("asprintf");
1871 71a27632 2020-01-15 stsp free(*id);
1872 71a27632 2020-01-15 stsp *id = NULL;
1874 71a27632 2020-01-15 stsp got_object_tag_close(tag);
1875 71a27632 2020-01-15 stsp return err;
1876 71a27632 2020-01-15 stsp } else if (err->code != GOT_ERR_OBJ_TYPE &&
1877 71a27632 2020-01-15 stsp err->code != GOT_ERR_NO_OBJ)
1878 71a27632 2020-01-15 stsp return err;
1881 0ed2285b 2022-03-09 stsp err = got_ref_open(&ref, repo, id_str, 0);
1882 0ed2285b 2022-03-09 stsp if (err == NULL) {
1883 0ed2285b 2022-03-09 stsp err = got_ref_resolve(id, repo, ref);
1885 71a27632 2020-01-15 stsp goto done;
1886 71a27632 2020-01-15 stsp if (label) {
1887 71a27632 2020-01-15 stsp *label = strdup(got_ref_get_name(ref));
1888 71a27632 2020-01-15 stsp if (*label == NULL) {
1889 71a27632 2020-01-15 stsp err = got_error_from_errno("strdup");
1890 71a27632 2020-01-15 stsp goto done;
1894 0ed2285b 2022-03-09 stsp if (err->code != GOT_ERR_NOT_REF &&
1895 0ed2285b 2022-03-09 stsp err->code != GOT_ERR_BAD_REF_NAME)
1896 71a27632 2020-01-15 stsp goto done;
1897 0ed2285b 2022-03-09 stsp err = got_repo_match_object_id_prefix(id, id_str,
1898 0ed2285b 2022-03-09 stsp obj_type, repo);
1899 0ed2285b 2022-03-09 stsp if (err) {
1900 0ed2285b 2022-03-09 stsp if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1901 0ed2285b 2022-03-09 stsp err = got_error_not_ref(id_str);
1902 0ed2285b 2022-03-09 stsp goto done;
1904 0ed2285b 2022-03-09 stsp if (label) {
1905 0ed2285b 2022-03-09 stsp err = got_object_id_str(label, *id);
1906 0ed2285b 2022-03-09 stsp if (*label == NULL) {
1907 0ed2285b 2022-03-09 stsp err = got_error_from_errno("strdup");
1908 0ed2285b 2022-03-09 stsp goto done;
1914 71a27632 2020-01-15 stsp got_ref_close(ref);
1915 71a27632 2020-01-15 stsp return err;
1918 71a27632 2020-01-15 stsp const struct got_error *
1919 303e2782 2019-08-09 stsp got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1920 84de9106 2020-12-26 stsp int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1922 84de9106 2020-12-26 stsp const struct got_error *err = NULL;
1923 303e2782 2019-08-09 stsp struct got_reflist_entry *re;
1924 303e2782 2019-08-09 stsp struct got_object_id *tag_id;
1925 785d65a4 2020-12-05 stsp int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1927 303e2782 2019-08-09 stsp *tag = NULL;
1929 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1930 303e2782 2019-08-09 stsp const char *refname;
1931 303e2782 2019-08-09 stsp refname = got_ref_get_name(re->ref);
1932 29606af7 2019-08-23 stsp if (got_ref_is_symbolic(re->ref))
1934 84de9106 2020-12-26 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
1936 785d65a4 2020-12-05 stsp if (!name_is_absolute)
1937 785d65a4 2020-12-05 stsp refname += strlen("refs/tags/");
1938 303e2782 2019-08-09 stsp if (strcmp(refname, name) != 0)
1940 303e2782 2019-08-09 stsp err = got_ref_resolve(&tag_id, repo, re->ref);
1943 303e2782 2019-08-09 stsp err = got_object_open_as_tag(tag, repo, tag_id);
1944 303e2782 2019-08-09 stsp free(tag_id);
1947 d24820bf 2019-08-11 stsp if (obj_type == GOT_OBJ_TYPE_ANY ||
1948 d24820bf 2019-08-11 stsp got_object_tag_get_object_type(*tag) == obj_type)
1950 303e2782 2019-08-09 stsp got_object_tag_close(*tag);
1951 303e2782 2019-08-09 stsp *tag = NULL;
1954 303e2782 2019-08-09 stsp if (err == NULL && *tag == NULL)
1955 138e4f47 2021-10-09 stsp err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1956 138e4f47 2021-10-09 stsp GOT_OBJ_LABEL_TAG, name);
1957 e09a504c 2019-06-28 stsp return err;
1960 3ce1b845 2019-07-15 stsp static const struct got_error *
1961 3ce1b845 2019-07-15 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1962 3ce1b845 2019-07-15 stsp const char *name, mode_t mode, struct got_object_id *blob_id)
1964 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1966 3ce1b845 2019-07-15 stsp *new_te = NULL;
1968 3ce1b845 2019-07-15 stsp *new_te = calloc(1, sizeof(**new_te));
1969 3ce1b845 2019-07-15 stsp if (*new_te == NULL)
1970 3ce1b845 2019-07-15 stsp return got_error_from_errno("calloc");
1972 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1973 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
1974 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1975 3ce1b845 2019-07-15 stsp goto done;
1978 e8863bdc 2020-07-23 stsp if (S_ISLNK(mode)) {
1979 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFLNK;
1981 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFREG;
1982 e8863bdc 2020-07-23 stsp (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1984 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1986 3ce1b845 2019-07-15 stsp if (err && *new_te) {
1987 56e0773d 2019-11-28 stsp free(*new_te);
1988 3ce1b845 2019-07-15 stsp *new_te = NULL;
1990 3ce1b845 2019-07-15 stsp return err;
1993 3ce1b845 2019-07-15 stsp static const struct got_error *
1994 3ce1b845 2019-07-15 stsp import_file(struct got_tree_entry **new_te, struct dirent *de,
1995 3ce1b845 2019-07-15 stsp const char *path, struct got_repository *repo)
1997 3ce1b845 2019-07-15 stsp const struct got_error *err;
1998 3ce1b845 2019-07-15 stsp struct got_object_id *blob_id = NULL;
1999 3ce1b845 2019-07-15 stsp char *filepath;
2000 3ce1b845 2019-07-15 stsp struct stat sb;
2002 3ce1b845 2019-07-15 stsp if (asprintf(&filepath, "%s%s%s", path,
2003 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
2004 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
2006 3ce1b845 2019-07-15 stsp if (lstat(filepath, &sb) != 0) {
2007 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("lstat", path);
2008 3ce1b845 2019-07-15 stsp goto done;
2011 3ce1b845 2019-07-15 stsp err = got_object_blob_create(&blob_id, filepath, repo);
2013 3ce1b845 2019-07-15 stsp goto done;
2015 3ce1b845 2019-07-15 stsp err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2018 3ce1b845 2019-07-15 stsp free(filepath);
2020 3ce1b845 2019-07-15 stsp free(blob_id);
2021 3ce1b845 2019-07-15 stsp return err;
2024 3ce1b845 2019-07-15 stsp static const struct got_error *
2025 3ce1b845 2019-07-15 stsp insert_tree_entry(struct got_tree_entry *new_te,
2026 3ce1b845 2019-07-15 stsp struct got_pathlist_head *paths)
2028 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2029 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *new_pe;
2031 3ce1b845 2019-07-15 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2033 3ce1b845 2019-07-15 stsp return err;
2034 3ce1b845 2019-07-15 stsp if (new_pe == NULL)
2035 3ce1b845 2019-07-15 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2036 3ce1b845 2019-07-15 stsp return NULL;
2039 3ce1b845 2019-07-15 stsp static const struct got_error *write_tree(struct got_object_id **,
2040 3ce1b845 2019-07-15 stsp const char *, struct got_pathlist_head *, struct got_repository *,
2041 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg);
2043 3ce1b845 2019-07-15 stsp static const struct got_error *
2044 3ce1b845 2019-07-15 stsp import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2045 3ce1b845 2019-07-15 stsp const char *path, struct got_pathlist_head *ignores,
2046 3ce1b845 2019-07-15 stsp struct got_repository *repo,
2047 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
2049 3ce1b845 2019-07-15 stsp const struct got_error *err;
2050 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
2051 3ce1b845 2019-07-15 stsp char *subdirpath;
2053 3ce1b845 2019-07-15 stsp if (asprintf(&subdirpath, "%s%s%s", path,
2054 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
2055 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
2057 3ce1b845 2019-07-15 stsp (*new_te) = calloc(1, sizeof(**new_te));
2058 d6fca0ba 2019-09-15 hiltjo if (*new_te == NULL)
2059 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
2060 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFDIR;
2061 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2062 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
2063 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
2064 3ce1b845 2019-07-15 stsp goto done;
2066 56e0773d 2019-11-28 stsp err = write_tree(&id, subdirpath, ignores, repo,
2067 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
2069 56e0773d 2019-11-28 stsp goto done;
2070 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2074 3ce1b845 2019-07-15 stsp free(subdirpath);
2075 3ce1b845 2019-07-15 stsp if (err) {
2076 56e0773d 2019-11-28 stsp free(*new_te);
2077 3ce1b845 2019-07-15 stsp *new_te = NULL;
2079 3ce1b845 2019-07-15 stsp return err;
2082 3ce1b845 2019-07-15 stsp static const struct got_error *
2083 3ce1b845 2019-07-15 stsp write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2084 3ce1b845 2019-07-15 stsp struct got_pathlist_head *ignores, struct got_repository *repo,
2085 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
2087 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2089 3ce1b845 2019-07-15 stsp struct dirent *de;
2090 56e0773d 2019-11-28 stsp int nentries;
2091 3ce1b845 2019-07-15 stsp struct got_tree_entry *new_te = NULL;
2092 3ce1b845 2019-07-15 stsp struct got_pathlist_head paths;
2093 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
2095 3ce1b845 2019-07-15 stsp *new_tree_id = NULL;
2097 3ce1b845 2019-07-15 stsp TAILQ_INIT(&paths);
2099 3ce1b845 2019-07-15 stsp dir = opendir(path_dir);
2100 3ce1b845 2019-07-15 stsp if (dir == NULL) {
2101 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("opendir", path_dir);
2102 3ce1b845 2019-07-15 stsp goto done;
2105 56e0773d 2019-11-28 stsp nentries = 0;
2106 3ce1b845 2019-07-15 stsp while ((de = readdir(dir)) != NULL) {
2107 3ce1b845 2019-07-15 stsp int ignore = 0;
2110 3ce1b845 2019-07-15 stsp if (strcmp(de->d_name, ".") == 0 ||
2111 3ce1b845 2019-07-15 stsp strcmp(de->d_name, "..") == 0)
2114 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, ignores, entry) {
2115 3ce1b845 2019-07-15 stsp if (fnmatch(pe->path, de->d_name, 0) == 0) {
2116 3ce1b845 2019-07-15 stsp ignore = 1;
2120 3ce1b845 2019-07-15 stsp if (ignore)
2123 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_dir, de);
2125 20ccae39 2020-07-21 stsp goto done;
2127 20ccae39 2020-07-21 stsp if (type == DT_DIR) {
2128 3ce1b845 2019-07-15 stsp err = import_subdir(&new_te, de, path_dir,
2129 3ce1b845 2019-07-15 stsp ignores, repo, progress_cb, progress_arg);
2130 db1d3576 2019-10-04 stsp if (err) {
2131 db1d3576 2019-10-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
2132 db1d3576 2019-10-04 stsp goto done;
2133 db1d3576 2019-10-04 stsp err = NULL;
2136 e8863bdc 2020-07-23 stsp } else if (type == DT_REG || type == DT_LNK) {
2137 3ce1b845 2019-07-15 stsp err = import_file(&new_te, de, path_dir, repo);
2139 3ce1b845 2019-07-15 stsp goto done;
2143 3ce1b845 2019-07-15 stsp err = insert_tree_entry(new_te, &paths);
2145 3ce1b845 2019-07-15 stsp goto done;
2146 56e0773d 2019-11-28 stsp nentries++;
2149 db1d3576 2019-10-04 stsp if (TAILQ_EMPTY(&paths)) {
2150 b66cd6f3 2020-07-31 stsp err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2151 b66cd6f3 2020-07-31 stsp "cannot create tree without any entries");
2152 db1d3576 2019-10-04 stsp goto done;
2155 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, &paths, entry) {
2156 3ce1b845 2019-07-15 stsp struct got_tree_entry *te = pe->data;
2157 3ce1b845 2019-07-15 stsp char *path;
2158 e8863bdc 2020-07-23 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2160 3ce1b845 2019-07-15 stsp if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2161 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
2162 3ce1b845 2019-07-15 stsp goto done;
2164 3ce1b845 2019-07-15 stsp err = (*progress_cb)(progress_arg, path);
2165 3ce1b845 2019-07-15 stsp free(path);
2167 3ce1b845 2019-07-15 stsp goto done;
2170 56e0773d 2019-11-28 stsp err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2173 3ce1b845 2019-07-15 stsp closedir(dir);
2174 3ce1b845 2019-07-15 stsp got_pathlist_free(&paths);
2175 3ce1b845 2019-07-15 stsp return err;
2178 3ce1b845 2019-07-15 stsp const struct got_error *
2179 3ce1b845 2019-07-15 stsp got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2180 3ce1b845 2019-07-15 stsp const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2181 3ce1b845 2019-07-15 stsp struct got_repository *repo, got_repo_import_cb progress_cb,
2182 3ce1b845 2019-07-15 stsp void *progress_arg)
2184 3ce1b845 2019-07-15 stsp const struct got_error *err;
2185 3ce1b845 2019-07-15 stsp struct got_object_id *new_tree_id;
2187 3ce1b845 2019-07-15 stsp err = write_tree(&new_tree_id, path_dir, ignores, repo,
2188 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
2190 3ce1b845 2019-07-15 stsp return err;
2192 3ce1b845 2019-07-15 stsp err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2193 3ce1b845 2019-07-15 stsp author, time(NULL), author, time(NULL), logmsg, repo);
2194 3ce1b845 2019-07-15 stsp free(new_tree_id);
2195 20662ea0 2021-04-10 stsp return err;
2198 20662ea0 2021-04-10 stsp const struct got_error *
2199 20662ea0 2021-04-10 stsp got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2200 20662ea0 2021-04-10 stsp struct got_repository *repo)
2202 20662ea0 2021-04-10 stsp const struct got_error *err = NULL;
2203 20662ea0 2021-04-10 stsp char *path_objects = NULL, *path = NULL;
2204 20662ea0 2021-04-10 stsp DIR *dir = NULL;
2205 20662ea0 2021-04-10 stsp struct got_object_id id;
2208 20662ea0 2021-04-10 stsp *nobjects = 0;
2209 20662ea0 2021-04-10 stsp *ondisk_size = 0;
2211 20662ea0 2021-04-10 stsp path_objects = got_repo_get_path_objects(repo);
2212 20662ea0 2021-04-10 stsp if (path_objects == NULL)
2213 20662ea0 2021-04-10 stsp return got_error_from_errno("got_repo_get_path_objects");
2215 20662ea0 2021-04-10 stsp for (i = 0; i <= 0xff; i++) {
2216 20662ea0 2021-04-10 stsp struct dirent *dent;
2218 20662ea0 2021-04-10 stsp if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2219 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2223 20662ea0 2021-04-10 stsp dir = opendir(path);
2224 20662ea0 2021-04-10 stsp if (dir == NULL) {
2225 20662ea0 2021-04-10 stsp if (errno == ENOENT) {
2226 20662ea0 2021-04-10 stsp err = NULL;
2229 20662ea0 2021-04-10 stsp err = got_error_from_errno2("opendir", path);
2233 20662ea0 2021-04-10 stsp while ((dent = readdir(dir)) != NULL) {
2234 20662ea0 2021-04-10 stsp char *id_str;
2236 20662ea0 2021-04-10 stsp struct stat sb;
2238 20662ea0 2021-04-10 stsp if (strcmp(dent->d_name, ".") == 0 ||
2239 20662ea0 2021-04-10 stsp strcmp(dent->d_name, "..") == 0)
2242 20662ea0 2021-04-10 stsp if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2243 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2244 20662ea0 2021-04-10 stsp goto done;
2247 20662ea0 2021-04-10 stsp if (!got_parse_sha1_digest(id.sha1, id_str)) {
2248 20662ea0 2021-04-10 stsp free(id_str);
2251 20662ea0 2021-04-10 stsp free(id_str);
2253 20662ea0 2021-04-10 stsp err = got_object_open_loose_fd(&fd, &id, repo);
2255 20662ea0 2021-04-10 stsp goto done;
2257 20662ea0 2021-04-10 stsp if (fstat(fd, &sb) == -1) {
2258 20662ea0 2021-04-10 stsp err = got_error_from_errno("fstat");
2259 20662ea0 2021-04-10 stsp close(fd);
2260 20662ea0 2021-04-10 stsp goto done;
2262 20662ea0 2021-04-10 stsp (*nobjects)++;
2263 20662ea0 2021-04-10 stsp (*ondisk_size) += sb.st_size;
2265 20662ea0 2021-04-10 stsp if (close(fd) == -1) {
2266 20662ea0 2021-04-10 stsp err = got_error_from_errno("close");
2267 20662ea0 2021-04-10 stsp goto done;
2271 20662ea0 2021-04-10 stsp if (closedir(dir) != 0) {
2272 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2273 20662ea0 2021-04-10 stsp goto done;
2275 20662ea0 2021-04-10 stsp dir = NULL;
2277 20662ea0 2021-04-10 stsp free(path);
2278 20662ea0 2021-04-10 stsp path = NULL;
2281 20662ea0 2021-04-10 stsp if (dir && closedir(dir) != 0 && err == NULL)
2282 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2284 20662ea0 2021-04-10 stsp if (err) {
2285 20662ea0 2021-04-10 stsp *nobjects = 0;
2286 20662ea0 2021-04-10 stsp *ondisk_size = 0;
2288 20662ea0 2021-04-10 stsp free(path_objects);
2289 20662ea0 2021-04-10 stsp free(path);
2290 3ce1b845 2019-07-15 stsp return err;
2293 20662ea0 2021-04-10 stsp const struct got_error *
2294 20662ea0 2021-04-10 stsp got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2295 20662ea0 2021-04-10 stsp off_t *total_packsize, struct got_repository *repo)
2297 0c9eeee2 2021-06-05 stsp const struct got_error *err = NULL;
2298 20662ea0 2021-04-10 stsp DIR *packdir = NULL;
2299 20662ea0 2021-04-10 stsp struct dirent *dent;
2300 20662ea0 2021-04-10 stsp struct got_packidx *packidx = NULL;
2301 20662ea0 2021-04-10 stsp char *path_packidx;
2302 20662ea0 2021-04-10 stsp char *path_packfile;
2303 20662ea0 2021-04-10 stsp int packdir_fd;
2304 20662ea0 2021-04-10 stsp struct stat sb;
2306 20662ea0 2021-04-10 stsp *npackfiles = 0;
2307 20662ea0 2021-04-10 stsp *nobjects = 0;
2308 20662ea0 2021-04-10 stsp *total_packsize = 0;
2310 20662ea0 2021-04-10 stsp packdir_fd = openat(got_repo_get_fd(repo),
2311 20662ea0 2021-04-10 stsp GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2312 20662ea0 2021-04-10 stsp if (packdir_fd == -1) {
2313 20662ea0 2021-04-10 stsp return got_error_from_errno_fmt("openat: %s/%s",
2314 20662ea0 2021-04-10 stsp got_repo_get_path_git_dir(repo),
2315 20662ea0 2021-04-10 stsp GOT_OBJECTS_PACK_DIR);
2318 20662ea0 2021-04-10 stsp packdir = fdopendir(packdir_fd);
2319 20662ea0 2021-04-10 stsp if (packdir == NULL) {
2320 20662ea0 2021-04-10 stsp err = got_error_from_errno("fdopendir");
2321 20662ea0 2021-04-10 stsp goto done;
2324 20662ea0 2021-04-10 stsp while ((dent = readdir(packdir)) != NULL) {
2325 1124fe40 2021-07-07 stsp if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
2328 20662ea0 2021-04-10 stsp if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2329 20662ea0 2021-04-10 stsp dent->d_name) == -1) {
2330 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2331 20662ea0 2021-04-10 stsp goto done;
2334 20662ea0 2021-04-10 stsp err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2335 20662ea0 2021-04-10 stsp path_packidx, 0);
2336 20662ea0 2021-04-10 stsp free(path_packidx);
2338 20662ea0 2021-04-10 stsp goto done;
2340 20662ea0 2021-04-10 stsp if (fstat(packidx->fd, &sb) == -1)
2341 20662ea0 2021-04-10 stsp goto done;
2342 20662ea0 2021-04-10 stsp *total_packsize += sb.st_size;
2344 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2345 aea75d87 2021-07-06 stsp packidx->path_packidx);
2347 20662ea0 2021-04-10 stsp goto done;
2349 20662ea0 2021-04-10 stsp if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2350 20662ea0 2021-04-10 stsp 0) == -1) {
2351 20662ea0 2021-04-10 stsp free(path_packfile);
2352 20662ea0 2021-04-10 stsp goto done;
2354 20662ea0 2021-04-10 stsp free(path_packfile);
2355 20662ea0 2021-04-10 stsp *total_packsize += sb.st_size;
2357 20662ea0 2021-04-10 stsp *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2359 20662ea0 2021-04-10 stsp (*npackfiles)++;
2361 20662ea0 2021-04-10 stsp got_packidx_close(packidx);
2362 20662ea0 2021-04-10 stsp packidx = NULL;
2365 20662ea0 2021-04-10 stsp if (packidx)
2366 20662ea0 2021-04-10 stsp got_packidx_close(packidx);
2367 20662ea0 2021-04-10 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
2368 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2369 20662ea0 2021-04-10 stsp if (err) {
2370 20662ea0 2021-04-10 stsp *npackfiles = 0;
2371 20662ea0 2021-04-10 stsp *nobjects = 0;
2372 20662ea0 2021-04-10 stsp *total_packsize = 0;
2374 20662ea0 2021-04-10 stsp return err;
2377 f8b19efd 2021-10-13 stsp RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2378 f8b19efd 2021-10-13 stsp got_packidx_bloom_filter_cmp);