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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 ad242220 2018-09-08 stsp #include <sys/types.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 ad242220 2018-09-08 stsp #include <sys/uio.h>
22 aba9c984 2019-09-08 stsp #include <sys/socket.h>
23 deeca238 2018-03-12 stsp #include <sys/stat.h>
24 1510f469 2018-09-09 stsp #include <sys/mman.h>
25 6c414261 2021-03-30 stsp #include <sys/resource.h>
27 e09a504c 2019-06-28 stsp #include <ctype.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 <string.h>
35 303e14b5 2019-09-23 stsp #include <time.h>
36 81a12da5 2020-09-09 naddy #include <unistd.h>
37 79b11c62 2018-03-09 stsp #include <zlib.h>
38 85f51bba 2018-07-16 stsp #include <errno.h>
39 85f51bba 2018-07-16 stsp #include <libgen.h>
40 ad242220 2018-09-08 stsp #include <stdint.h>
42 4545b700 2021-10-15 thomas #include "bloom.h"
44 4027f31a 2017-11-04 stsp #include "got_error.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 4027f31a 2017-11-04 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 7bb0daa1 2018-06-21 stsp #include "got_object.h"
50 bfb5ee0b 2022-05-31 thomas #include "got_opentemp.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 3efd8e31 2022-10-23 thomas #include "got_lib_delta_cache.h"
54 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
55 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
56 3ce1b845 2019-07-15 stsp #include "got_lib_object_parse.h"
57 3ce1b845 2019-07-15 stsp #include "got_lib_object_create.h"
58 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
59 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
60 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
61 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
62 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
63 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
65 79b11c62 2018-03-09 stsp #ifndef nitems
66 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 7cd52833 2022-06-23 thomas #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
71 0be8fa4c 2021-10-15 thomas RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
72 0be8fa4c 2021-10-15 thomas got_packidx_bloom_filter_cmp);
74 b091c2cd 2023-02-07 thomas static inline int
75 b091c2cd 2023-02-07 thomas is_boolean_val(const char *val)
77 b091c2cd 2023-02-07 thomas return (strcasecmp(val, "true") == 0 ||
78 b091c2cd 2023-02-07 thomas strcasecmp(val, "false") == 0 ||
79 b091c2cd 2023-02-07 thomas strcasecmp(val, "on") == 0 ||
80 b091c2cd 2023-02-07 thomas strcasecmp(val, "off") == 0 ||
81 b091c2cd 2023-02-07 thomas strcasecmp(val, "yes") == 0 ||
82 b091c2cd 2023-02-07 thomas strcasecmp(val, "no") == 0 ||
83 b091c2cd 2023-02-07 thomas strcasecmp(val, "1") == 0 ||
84 b091c2cd 2023-02-07 thomas strcasecmp(val, "0") == 0);
87 b091c2cd 2023-02-07 thomas static inline int
88 b091c2cd 2023-02-07 thomas get_boolean_val(const char *val)
90 b091c2cd 2023-02-07 thomas return (strcasecmp(val, "true") == 0 ||
91 b091c2cd 2023-02-07 thomas strcasecmp(val, "on") == 0 ||
92 b091c2cd 2023-02-07 thomas strcasecmp(val, "yes") == 0 ||
93 b091c2cd 2023-02-07 thomas strcasecmp(val, "1") == 0);
96 7839bc15 2019-01-06 stsp const char *
97 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
99 7839bc15 2019-01-06 stsp return repo->path;
102 6e9da951 2019-01-06 stsp const char *
103 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
105 6e9da951 2019-01-06 stsp return repo->path_git_dir;
109 6d5a9006 2020-12-16 yzhong got_repo_get_fd(struct got_repository *repo)
111 6d5a9006 2020-12-16 yzhong return repo->gitdir_fd;
114 f4204d57 2023-03-01 thomas enum got_hash_algorithm
115 f4204d57 2023-03-01 thomas got_repo_get_object_format(struct got_repository *repo)
117 f4204d57 2023-03-01 thomas return repo->algo;
120 aba9c984 2019-09-08 stsp const char *
121 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_name(struct got_repository *repo)
123 aba9c984 2019-09-08 stsp return repo->gitconfig_author_name;
126 aba9c984 2019-09-08 stsp const char *
127 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_email(struct got_repository *repo)
129 aba9c984 2019-09-08 stsp return repo->gitconfig_author_email;
132 c9956ddf 2019-09-08 stsp const char *
133 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
135 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_name;
138 c9956ddf 2019-09-08 stsp const char *
139 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
141 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_email;
144 9a1cc63f 2020-02-03 stsp const char *
145 9a1cc63f 2020-02-03 stsp got_repo_get_gitconfig_owner(struct got_repository *repo)
147 9a1cc63f 2020-02-03 stsp return repo->gitconfig_owner;
151 2624d165 2023-02-07 thomas got_repo_has_extension(struct got_repository *repo, const char *ext)
155 2624d165 2023-02-07 thomas for (i = 0; i < repo->nextensions; ++i) {
156 b091c2cd 2023-02-07 thomas if (!strcasecmp(ext, repo->extnames[i]))
157 b091c2cd 2023-02-07 thomas return get_boolean_val(repo->extvals[i]);
160 2624d165 2023-02-07 thomas return 0;
164 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
166 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
169 4027f31a 2017-11-04 stsp static char *
170 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
172 4027f31a 2017-11-04 stsp char *path_child;
174 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
175 4027f31a 2017-11-04 stsp basename) == -1)
176 4027f31a 2017-11-04 stsp return NULL;
178 4027f31a 2017-11-04 stsp return path_child;
182 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
184 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
188 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
190 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
194 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
196 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
200 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
202 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
205 4027f31a 2017-11-04 stsp static char *
206 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
208 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
212 b46f3e71 2020-03-18 stsp got_repo_get_path_gitconfig(struct got_repository *repo)
214 b46f3e71 2020-03-18 stsp return get_path_git_child(repo, GOT_GITCONFIG);
218 257add31 2020-09-09 stsp got_repo_get_path_gotconfig(struct got_repository *repo)
220 50b0790e 2020-09-11 stsp return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
223 50b0790e 2020-09-11 stsp const struct got_gotconfig *
224 50b0790e 2020-09-11 stsp got_repo_get_gotconfig(struct got_repository *repo)
226 50b0790e 2020-09-11 stsp return repo->gotconfig;
230 50b0790e 2020-09-11 stsp got_repo_get_gitconfig_remotes(int *nremotes,
231 50b0790e 2020-09-11 stsp const struct got_remote_repo **remotes, struct got_repository *repo)
233 50b0790e 2020-09-11 stsp *nremotes = repo->ngitconfig_remotes;
234 50b0790e 2020-09-11 stsp *remotes = repo->gitconfig_remotes;
238 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
240 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
241 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
242 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
243 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
244 deeca238 2018-03-12 stsp int ret = 0;
245 deeca238 2018-03-12 stsp struct stat sb;
246 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
248 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
250 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
253 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
255 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
258 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
260 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
263 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
265 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
268 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
269 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
271 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
275 4027f31a 2017-11-04 stsp free(path_objects);
276 4027f31a 2017-11-04 stsp free(path_refs);
277 4027f31a 2017-11-04 stsp free(path_head);
278 4027f31a 2017-11-04 stsp return ret;
282 3efd8e31 2022-10-23 thomas static const struct got_error *
283 3efd8e31 2022-10-23 thomas close_tempfiles(int *fds, size_t nfds)
285 7cd52833 2022-06-23 thomas const struct got_error *err = NULL;
288 3efd8e31 2022-10-23 thomas for (i = 0; i < nfds; i++) {
289 3efd8e31 2022-10-23 thomas if (fds[i] == -1)
290 3efd8e31 2022-10-23 thomas continue;
291 3efd8e31 2022-10-23 thomas if (close(fds[i]) == -1) {
292 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
296 3efd8e31 2022-10-23 thomas free(fds);
297 3efd8e31 2022-10-23 thomas return err;
300 3efd8e31 2022-10-23 thomas static const struct got_error *
301 e0220e74 2022-10-31 thomas open_tempfiles(int **fds, size_t array_size, size_t nfds)
303 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
306 e0220e74 2022-10-31 thomas *fds = calloc(array_size, sizeof(**fds));
307 3efd8e31 2022-10-23 thomas if (*fds == NULL)
308 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
310 e0220e74 2022-10-31 thomas for (i = 0; i < array_size; i++)
311 3efd8e31 2022-10-23 thomas (*fds)[i] = -1;
313 3efd8e31 2022-10-23 thomas for (i = 0; i < nfds; i++) {
314 3efd8e31 2022-10-23 thomas (*fds)[i] = got_opentempfd();
315 3efd8e31 2022-10-23 thomas if ((*fds)[i] == -1) {
316 7648b8ae 2022-06-23 thomas err = got_error_from_errno("got_opentempfd");
317 3efd8e31 2022-10-23 thomas close_tempfiles(*fds, nfds);
318 3efd8e31 2022-10-23 thomas *fds = NULL;
319 7648b8ae 2022-06-23 thomas return err;
323 a6fd182a 2022-09-03 thomas return NULL;
326 e0220e74 2022-10-31 thomas static const struct got_error *
327 e0220e74 2022-10-31 thomas get_pack_cache_size(int *pack_cache_size)
329 e0220e74 2022-10-31 thomas struct rlimit rl;
331 e0220e74 2022-10-31 thomas if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
332 e0220e74 2022-10-31 thomas return got_error_from_errno("getrlimit");
334 e0220e74 2022-10-31 thomas *pack_cache_size = GOT_PACK_CACHE_SIZE;
335 e0220e74 2022-10-31 thomas if (*pack_cache_size > rl.rlim_cur / 8)
336 e0220e74 2022-10-31 thomas *pack_cache_size = rl.rlim_cur / 8;
338 e0220e74 2022-10-31 thomas return NULL;
341 7cd52833 2022-06-23 thomas const struct got_error *
342 3efd8e31 2022-10-23 thomas got_repo_pack_fds_open(int **pack_fds)
344 e0220e74 2022-10-31 thomas const struct got_error *err;
345 e0220e74 2022-10-31 thomas int nfds;
347 e0220e74 2022-10-31 thomas err = get_pack_cache_size(&nfds);
349 e0220e74 2022-10-31 thomas return err;
352 e0220e74 2022-10-31 thomas * We need one basefd and one accumfd per cached pack.
353 e0220e74 2022-10-31 thomas * Our constants should be set up in a way such that
354 e0220e74 2022-10-31 thomas * this error never triggers.
356 e0220e74 2022-10-31 thomas if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
357 e0220e74 2022-10-31 thomas return got_error(GOT_ERR_NO_SPACE);
359 e0220e74 2022-10-31 thomas return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
362 3efd8e31 2022-10-23 thomas const struct got_error *
363 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(int *pack_fds)
365 3efd8e31 2022-10-23 thomas return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
368 3efd8e31 2022-10-23 thomas const struct got_error *
369 946e0798 2022-11-06 thomas got_repo_temp_fds_open(int **temp_fds)
371 e0220e74 2022-10-31 thomas return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
372 e0220e74 2022-10-31 thomas GOT_REPO_NUM_TEMPFILES);
376 946e0798 2022-11-06 thomas got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
380 3efd8e31 2022-10-23 thomas for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
381 3efd8e31 2022-10-23 thomas repo->tempfiles[i] = temp_fds[i];
384 3efd8e31 2022-10-23 thomas const struct got_error *
385 3efd8e31 2022-10-23 thomas got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
389 3efd8e31 2022-10-23 thomas *fd = -1;
390 3efd8e31 2022-10-23 thomas *idx = -1;
392 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo->tempfiles); i++) {
393 3efd8e31 2022-10-23 thomas if (repo->tempfile_use_mask & (1 << i))
394 7cd52833 2022-06-23 thomas continue;
395 3efd8e31 2022-10-23 thomas if (repo->tempfiles[i] != -1) {
396 3efd8e31 2022-10-23 thomas if (ftruncate(repo->tempfiles[i], 0L) == -1)
397 3efd8e31 2022-10-23 thomas return got_error_from_errno("ftruncate");
398 3efd8e31 2022-10-23 thomas *fd = repo->tempfiles[i];
399 3efd8e31 2022-10-23 thomas *idx = i;
400 3efd8e31 2022-10-23 thomas repo->tempfile_use_mask |= (1 << i);
401 3efd8e31 2022-10-23 thomas return NULL;
405 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_REPO_TEMPFILE);
409 3efd8e31 2022-10-23 thomas got_repo_temp_fds_put(int idx, struct got_repository *repo)
411 3efd8e31 2022-10-23 thomas repo->tempfile_use_mask &= ~(1 << idx);
414 3efd8e31 2022-10-23 thomas const struct got_error *
415 3efd8e31 2022-10-23 thomas got_repo_temp_fds_close(int *temp_fds)
417 3efd8e31 2022-10-23 thomas return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
420 7cd52833 2022-06-23 thomas const struct got_error *
421 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
422 f6be5c30 2018-06-22 stsp struct got_object *obj)
424 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
425 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
426 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
428 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
429 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
430 79c99a64 2019-05-23 stsp err = NULL;
431 f6be5c30 2018-06-22 stsp return err;
433 f6be5c30 2018-06-22 stsp obj->refcnt++;
435 f6be5c30 2018-06-22 stsp return NULL;
438 7bb0daa1 2018-06-21 stsp struct got_object *
439 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
440 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
442 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
445 4027f31a 2017-11-04 stsp const struct got_error *
446 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
447 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
449 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
450 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
451 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
453 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
454 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
455 79c99a64 2019-05-23 stsp err = NULL;
456 f6be5c30 2018-06-22 stsp return err;
458 f6be5c30 2018-06-22 stsp tree->refcnt++;
460 f6be5c30 2018-06-22 stsp return NULL;
463 f6be5c30 2018-06-22 stsp struct got_tree_object *
464 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
465 f6be5c30 2018-06-22 stsp struct got_object_id *id)
467 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
468 6bef87be 2018-09-11 stsp &repo->treecache, id);
471 1943de01 2018-06-22 stsp const struct got_error *
472 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
473 1943de01 2018-06-22 stsp struct got_commit_object *commit)
475 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
476 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
477 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
479 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
480 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
481 79c99a64 2019-05-23 stsp err = NULL;
482 1943de01 2018-06-22 stsp return err;
484 1943de01 2018-06-22 stsp commit->refcnt++;
486 f6be5c30 2018-06-22 stsp return NULL;
489 1943de01 2018-06-22 stsp struct got_commit_object *
490 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
491 1943de01 2018-06-22 stsp struct got_object_id *id)
493 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
494 6bef87be 2018-09-11 stsp &repo->commitcache, id);
497 f4a881ce 2018-11-17 stsp const struct got_error *
498 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
499 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
501 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
502 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
503 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
505 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
506 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
507 79c99a64 2019-05-23 stsp err = NULL;
508 f4a881ce 2018-11-17 stsp return err;
510 f4a881ce 2018-11-17 stsp tag->refcnt++;
512 f4a881ce 2018-11-17 stsp return NULL;
515 f4a881ce 2018-11-17 stsp struct got_tag_object *
516 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
518 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
519 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
522 8ab9215c 2021-10-15 thomas const struct got_error *
523 8ab9215c 2021-10-15 thomas got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
524 8ab9215c 2021-10-15 thomas struct got_raw_object *raw)
526 8ab9215c 2021-10-15 thomas #ifndef GOT_NO_OBJ_CACHE
527 8ab9215c 2021-10-15 thomas const struct got_error *err = NULL;
528 8ab9215c 2021-10-15 thomas err = got_object_cache_add(&repo->rawcache, id, raw);
529 8ab9215c 2021-10-15 thomas if (err) {
530 8ab9215c 2021-10-15 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
531 8ab9215c 2021-10-15 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
532 8ab9215c 2021-10-15 thomas err = NULL;
533 8ab9215c 2021-10-15 thomas return err;
535 8ab9215c 2021-10-15 thomas raw->refcnt++;
537 8ab9215c 2021-10-15 thomas return NULL;
541 8ab9215c 2021-10-15 thomas struct got_raw_object *
542 8ab9215c 2021-10-15 thomas got_repo_get_cached_raw_object(struct got_repository *repo,
543 8ab9215c 2021-10-15 thomas struct got_object_id *id)
545 8ab9215c 2021-10-15 thomas return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
549 991ff1aa 2021-06-15 tracey static const struct got_error *
550 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
552 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
554 6d5a9006 2020-12-16 yzhong repo->gitdir_fd = -1;
556 85f51bba 2018-07-16 stsp /* bare git repository? */
557 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
558 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
559 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
560 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
561 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
562 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
563 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
564 6d5a9006 2020-12-16 yzhong goto done;
566 06340621 2021-12-31 thomas repo->gitdir_fd = open(repo->path_git_dir,
567 06340621 2021-12-31 thomas O_DIRECTORY | O_CLOEXEC);
568 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd == -1) {
569 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("open",
570 6d5a9006 2020-12-16 yzhong repo->path_git_dir);
573 85f51bba 2018-07-16 stsp return NULL;
576 85f51bba 2018-07-16 stsp /* git repository with working tree? */
577 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
578 6b68ccd6 2019-09-01 stsp repo->path_git_dir = NULL;
579 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
580 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
583 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
584 85f51bba 2018-07-16 stsp repo->path = strdup(path);
585 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
586 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
589 06340621 2021-12-31 thomas repo->gitdir_fd = open(repo->path_git_dir,
590 06340621 2021-12-31 thomas O_DIRECTORY | O_CLOEXEC);
591 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd == -1) {
592 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("open",
593 6d5a9006 2020-12-16 yzhong repo->path_git_dir);
594 6d5a9006 2020-12-16 yzhong goto done;
596 85f51bba 2018-07-16 stsp return NULL;
599 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
602 ee645855 2019-02-05 stsp free(repo->path);
603 ee645855 2019-02-05 stsp repo->path = NULL;
604 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
605 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
606 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd != -1)
607 6d5a9006 2020-12-16 yzhong close(repo->gitdir_fd);
608 6d5a9006 2020-12-16 yzhong repo->gitdir_fd = -1;
611 c9956ddf 2019-09-08 stsp return err;
614 c9956ddf 2019-09-08 stsp static const struct got_error *
615 c9956ddf 2019-09-08 stsp read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
617 c9956ddf 2019-09-08 stsp const struct got_error *err = NULL;
618 c9956ddf 2019-09-08 stsp char *repo_gitconfig_path = NULL;
620 c9956ddf 2019-09-08 stsp if (global_gitconfig_path) {
621 c9956ddf 2019-09-08 stsp /* Read settings from ~/.gitconfig. */
622 c9956ddf 2019-09-08 stsp int dummy_repo_version;
623 d348087d 2022-10-13 thomas err = got_repo_read_gitconfig(&dummy_repo_version,
624 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_name,
625 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_email,
626 b091c2cd 2023-02-07 thomas NULL, NULL, NULL, NULL, NULL, NULL,
627 b091c2cd 2023-02-07 thomas global_gitconfig_path);
629 c9956ddf 2019-09-08 stsp return err;
632 c9956ddf 2019-09-08 stsp /* Read repository's .git/config file. */
633 b46f3e71 2020-03-18 stsp repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
634 b46f3e71 2020-03-18 stsp if (repo_gitconfig_path == NULL)
635 b46f3e71 2020-03-18 stsp return got_error_from_errno("got_repo_get_path_gitconfig");
637 d348087d 2022-10-13 thomas err = got_repo_read_gitconfig(
638 d348087d 2022-10-13 thomas &repo->gitconfig_repository_format_version,
639 c9956ddf 2019-09-08 stsp &repo->gitconfig_author_name, &repo->gitconfig_author_email,
640 cd95becd 2019-11-29 stsp &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
641 b091c2cd 2023-02-07 thomas &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
642 b091c2cd 2023-02-07 thomas &repo->nextensions, repo_gitconfig_path);
646 5daf5c53 2022-07-23 thomas if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
649 5daf5c53 2022-07-23 thomas for (i = 0; i < repo->ngitconfig_remotes; i++) {
650 5daf5c53 2022-07-23 thomas got_repo_free_remote_repo_data(
651 5daf5c53 2022-07-23 thomas &repo->gitconfig_remotes[i]);
653 5daf5c53 2022-07-23 thomas free(repo->gitconfig_remotes);
654 7e91f3d2 2022-07-24 thomas repo->gitconfig_remotes = NULL;
655 5daf5c53 2022-07-23 thomas repo->ngitconfig_remotes = 0;
657 5daf5c53 2022-07-23 thomas free(repo->gitconfig_author_name);
658 5daf5c53 2022-07-23 thomas repo->gitconfig_author_name = NULL;
659 5daf5c53 2022-07-23 thomas free(repo->gitconfig_author_email);
660 5daf5c53 2022-07-23 thomas repo->gitconfig_author_email = NULL;
662 7e91f3d2 2022-07-24 thomas free(repo->global_gitconfig_author_name);
663 7e91f3d2 2022-07-24 thomas repo->global_gitconfig_author_name = NULL;
664 7e91f3d2 2022-07-24 thomas free(repo->global_gitconfig_author_email);
665 7e91f3d2 2022-07-24 thomas repo->global_gitconfig_author_email = NULL;
669 c9956ddf 2019-09-08 stsp free(repo_gitconfig_path);
670 257add31 2020-09-09 stsp return err;
673 257add31 2020-09-09 stsp static const struct got_error *
674 257add31 2020-09-09 stsp read_gotconfig(struct got_repository *repo)
676 257add31 2020-09-09 stsp const struct got_error *err = NULL;
677 257add31 2020-09-09 stsp char *gotconfig_path;
679 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
680 257add31 2020-09-09 stsp if (gotconfig_path == NULL)
681 257add31 2020-09-09 stsp return got_error_from_errno("got_repo_get_path_gotconfig");
683 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
684 257add31 2020-09-09 stsp free(gotconfig_path);
685 85f51bba 2018-07-16 stsp return err;
688 20b7abb3 2020-10-22 stsp /* Supported repository format extensions. */
689 d224ad33 2022-03-22 thomas static const char *const repo_extensions[] = {
690 20b7abb3 2020-10-22 stsp "noop", /* Got supports repository format version 1. */
691 2252c019 2021-07-03 stsp "preciousObjects", /* Supported by gotadmin cleanup. */
692 20b7abb3 2020-10-22 stsp "worktreeConfig", /* Got does not care about Git work trees. */
695 85f51bba 2018-07-16 stsp const struct got_error *
696 c9956ddf 2019-09-08 stsp got_repo_open(struct got_repository **repop, const char *path,
697 7cd52833 2022-06-23 thomas const char *global_gitconfig_path, int *pack_fds)
699 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
700 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
701 dbb02f4d 2020-12-04 stsp char *repo_path = NULL;
702 7cd52833 2022-06-23 thomas size_t i, j = 0;
704 85f51bba 2018-07-16 stsp *repop = NULL;
706 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
707 58cbcd83 2022-03-22 thomas if (repo == NULL)
708 58cbcd83 2022-03-22 thomas return got_error_from_errno("calloc");
710 cfe41121 2021-10-16 thomas RB_INIT(&repo->packidx_bloom_filters);
711 de47d040 2022-03-22 thomas TAILQ_INIT(&repo->packidx_paths);
713 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
714 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
715 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
716 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
719 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
720 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
723 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
724 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
727 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
728 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
731 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
732 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
735 8ab9215c 2021-10-15 thomas err = got_object_cache_init(&repo->rawcache,
736 8ab9215c 2021-10-15 thomas GOT_OBJECT_CACHE_TYPE_RAW);
738 8ab9215c 2021-10-15 thomas goto done;
740 e0220e74 2022-10-31 thomas err = get_pack_cache_size(&repo->pack_cache_size);
742 e0220e74 2022-10-31 thomas goto done;
743 bfb5ee0b 2022-05-31 thomas for (i = 0; i < nitems(repo->packs); i++) {
744 3efd8e31 2022-10-23 thomas if (pack_fds != NULL && i < repo->pack_cache_size) {
745 7cd52833 2022-06-23 thomas repo->packs[i].basefd = pack_fds[j++];
746 7cd52833 2022-06-23 thomas repo->packs[i].accumfd = pack_fds[j++];
748 bfb5ee0b 2022-05-31 thomas repo->packs[i].basefd = -1;
749 bfb5ee0b 2022-05-31 thomas repo->packs[i].accumfd = -1;
752 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo->tempfiles); i++)
753 3efd8e31 2022-10-23 thomas repo->tempfiles[i] = -1;
754 ec2b23c5 2022-07-01 thomas repo->pinned_pack = -1;
755 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = -1;
756 ec2b23c5 2022-07-01 thomas repo->pinned_pid = 0;
758 dbb02f4d 2020-12-04 stsp repo_path = realpath(path, NULL);
759 aff6eea4 2020-10-20 stsp if (repo_path == NULL) {
760 dbb02f4d 2020-12-04 stsp err = got_error_from_errno2("realpath", path);
765 aff6eea4 2020-10-20 stsp char *parent_path;
767 aff6eea4 2020-10-20 stsp err = open_repo(repo, repo_path);
768 85f51bba 2018-07-16 stsp if (err == NULL)
770 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
772 aff6eea4 2020-10-20 stsp if (repo_path[0] == '/' && repo_path[1] == '\0') {
773 0c93d204 2020-10-20 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
776 aff6eea4 2020-10-20 stsp err = got_path_dirname(&parent_path, repo_path);
779 aff6eea4 2020-10-20 stsp free(repo_path);
780 aff6eea4 2020-10-20 stsp repo_path = parent_path;
783 257add31 2020-09-09 stsp err = read_gotconfig(repo);
787 c9956ddf 2019-09-08 stsp err = read_gitconfig(repo, global_gitconfig_path);
790 ebb42948 2022-07-21 thomas if (repo->gitconfig_repository_format_version != 0) {
791 aba9c984 2019-09-08 stsp err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
792 ebb42948 2022-07-21 thomas goto done;
794 20b7abb3 2020-10-22 stsp for (i = 0; i < repo->nextensions; i++) {
795 b091c2cd 2023-02-07 thomas char *ext = repo->extnames[i];
796 b091c2cd 2023-02-07 thomas char *val = repo->extvals[i];
797 20b7abb3 2020-10-22 stsp int j, supported = 0;
799 b091c2cd 2023-02-07 thomas if (!is_boolean_val(val)) {
800 b091c2cd 2023-02-07 thomas err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
801 b091c2cd 2023-02-07 thomas goto done;
804 b091c2cd 2023-02-07 thomas if (!get_boolean_val(val))
805 b091c2cd 2023-02-07 thomas continue;
807 20b7abb3 2020-10-22 stsp for (j = 0; j < nitems(repo_extensions); j++) {
808 20b7abb3 2020-10-22 stsp if (strcmp(ext, repo_extensions[j]) == 0) {
809 20b7abb3 2020-10-22 stsp supported = 1;
813 20b7abb3 2020-10-22 stsp if (!supported) {
814 20b7abb3 2020-10-22 stsp err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
819 de47d040 2022-03-22 thomas err = got_repo_list_packidx(&repo->packidx_paths, repo);
822 5c2f5761 2018-09-19 stsp got_repo_close(repo);
824 85f51bba 2018-07-16 stsp *repop = repo;
825 aff6eea4 2020-10-20 stsp free(repo_path);
826 92af5469 2017-11-05 stsp return err;
829 ad242220 2018-09-08 stsp const struct got_error *
830 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
832 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
833 0be8fa4c 2021-10-15 thomas struct got_packidx_bloom_filter *bf;
836 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
837 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
839 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
842 cfe41121 2021-10-16 thomas while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
843 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters))) {
844 cfe41121 2021-10-16 thomas RB_REMOVE(got_packidx_bloom_filter_tree,
845 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters, bf);
846 c4f29b53 2022-08-31 thomas bloom_free(bf->bloom);
847 4545b700 2021-10-15 thomas free(bf->bloom);
848 4545b700 2021-10-15 thomas free(bf);
851 7cd52833 2022-06-23 thomas for (i = 0; i < repo->pack_cache_size; i++)
852 cf032c44 2022-05-31 thomas if (repo->packs[i].path_packfile)
853 7cd52833 2022-06-23 thomas if (repo->packs[i].path_packfile)
854 7cd52833 2022-06-23 thomas got_pack_close(&repo->packs[i]);
856 4027f31a 2017-11-04 stsp free(repo->path);
857 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
859 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
860 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
861 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
862 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
863 8ab9215c 2021-10-15 thomas got_object_cache_close(&repo->rawcache);
865 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
866 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
868 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
869 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
870 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
871 8050095e 2023-06-22 thomas if (err && err->code == GOT_ERR_EOF)
872 8050095e 2023-06-22 thomas err = NULL;
873 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
874 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
875 ad242220 2018-09-08 stsp if (child_err && err == NULL)
876 ad242220 2018-09-08 stsp err = child_err;
877 08578a35 2021-01-22 stsp if (close(repo->privsep_children[i].imsg_fd) == -1 &&
878 3a6ce05a 2019-02-11 stsp err == NULL)
879 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
882 1d0f4054 2021-06-17 stsp if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
883 1d0f4054 2021-06-17 stsp err == NULL)
884 1d0f4054 2021-06-17 stsp err = got_error_from_errno("close");
886 50b0790e 2020-09-11 stsp if (repo->gotconfig)
887 50b0790e 2020-09-11 stsp got_gotconfig_free(repo->gotconfig);
888 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_name);
889 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_email);
890 b8adfa55 2020-09-25 stsp for (i = 0; i < repo->ngitconfig_remotes; i++)
891 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
892 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes);
893 b091c2cd 2023-02-07 thomas for (i = 0; i < repo->nextensions; i++) {
894 b091c2cd 2023-02-07 thomas free(repo->extnames[i]);
895 b091c2cd 2023-02-07 thomas free(repo->extvals[i]);
897 b091c2cd 2023-02-07 thomas free(repo->extnames);
898 b091c2cd 2023-02-07 thomas free(repo->extvals);
900 21c2d8be 2023-01-10 thomas got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
901 4027f31a 2017-11-04 stsp free(repo);
903 ad242220 2018-09-08 stsp return err;
906 11024cb9 2023-09-05 thomas const struct got_error *
907 11024cb9 2023-09-05 thomas got_repo_remote_repo_dup(struct got_remote_repo **newp,
908 11024cb9 2023-09-05 thomas const struct got_remote_repo *repo)
910 11024cb9 2023-09-05 thomas const struct got_error *err = NULL;
911 11024cb9 2023-09-05 thomas struct got_remote_repo *new;
914 11024cb9 2023-09-05 thomas new = calloc(1, sizeof(*new));
915 11024cb9 2023-09-05 thomas if (new == NULL)
916 11024cb9 2023-09-05 thomas return got_error_from_errno("calloc");
918 11024cb9 2023-09-05 thomas if (repo->name) {
919 11024cb9 2023-09-05 thomas new->name = strdup(repo->name);
920 11024cb9 2023-09-05 thomas if (new->name == NULL) {
921 11024cb9 2023-09-05 thomas err = got_error_from_errno("strdup");
922 11024cb9 2023-09-05 thomas goto done;
926 11024cb9 2023-09-05 thomas if (repo->fetch_url) {
927 11024cb9 2023-09-05 thomas new->fetch_url = strdup(repo->fetch_url);
928 11024cb9 2023-09-05 thomas if (new->fetch_url == NULL) {
929 11024cb9 2023-09-05 thomas err = got_error_from_errno("strdup");
930 11024cb9 2023-09-05 thomas goto done;
934 11024cb9 2023-09-05 thomas if (repo->send_url) {
935 11024cb9 2023-09-05 thomas new->send_url = strdup(repo->send_url);
936 11024cb9 2023-09-05 thomas if (new->send_url == NULL) {
937 11024cb9 2023-09-05 thomas err = got_error_from_errno("strdup");
938 11024cb9 2023-09-05 thomas goto done;
942 11024cb9 2023-09-05 thomas new->mirror_references = repo->mirror_references;
944 d7a689ea 2023-10-01 thomas new->fetch_all_branches = repo->fetch_all_branches;
946 11024cb9 2023-09-05 thomas new->nfetch_branches = repo->nfetch_branches;
947 11024cb9 2023-09-05 thomas if (repo->fetch_branches) {
948 11024cb9 2023-09-05 thomas new->fetch_branches = calloc(repo->nfetch_branches,
949 11024cb9 2023-09-05 thomas sizeof(char *));
950 11024cb9 2023-09-05 thomas if (new->fetch_branches == NULL) {
951 11024cb9 2023-09-05 thomas err = got_error_from_errno("calloc");
952 11024cb9 2023-09-05 thomas goto done;
954 11024cb9 2023-09-05 thomas for (i = 0; i < repo->nfetch_branches; i++) {
955 11024cb9 2023-09-05 thomas new->fetch_branches[i] = strdup(
956 11024cb9 2023-09-05 thomas repo->fetch_branches[i]);
957 11024cb9 2023-09-05 thomas if (new->fetch_branches[i] == NULL) {
958 11024cb9 2023-09-05 thomas err = got_error_from_errno("strdup");
959 11024cb9 2023-09-05 thomas goto done;
964 11024cb9 2023-09-05 thomas new->nsend_branches = repo->nsend_branches;
965 11024cb9 2023-09-05 thomas if (repo->send_branches) {
966 11024cb9 2023-09-05 thomas new->send_branches = calloc(repo->nsend_branches,
967 11024cb9 2023-09-05 thomas sizeof(char *));
968 11024cb9 2023-09-05 thomas if (new->send_branches == NULL) {
969 11024cb9 2023-09-05 thomas err = got_error_from_errno("calloc");
970 11024cb9 2023-09-05 thomas goto done;
972 11024cb9 2023-09-05 thomas for (i = 0; i < repo->nsend_branches; i++) {
973 11024cb9 2023-09-05 thomas new->send_branches[i] = strdup(
974 11024cb9 2023-09-05 thomas repo->send_branches[i]);
975 11024cb9 2023-09-05 thomas if (new->send_branches[i] == NULL) {
976 11024cb9 2023-09-05 thomas err = got_error_from_errno("strdup");
977 11024cb9 2023-09-05 thomas goto done;
982 11024cb9 2023-09-05 thomas new->nfetch_refs = repo->nfetch_refs;
983 11024cb9 2023-09-05 thomas if (repo->fetch_refs) {
984 11024cb9 2023-09-05 thomas new->fetch_refs = calloc(repo->nfetch_refs,
985 11024cb9 2023-09-05 thomas sizeof(char *));
986 11024cb9 2023-09-05 thomas if (new->fetch_refs == NULL) {
987 11024cb9 2023-09-05 thomas err = got_error_from_errno("calloc");
988 11024cb9 2023-09-05 thomas goto done;
990 11024cb9 2023-09-05 thomas for (i = 0; i < repo->nfetch_refs; i++) {
991 11024cb9 2023-09-05 thomas new->fetch_refs[i] = strdup(
992 11024cb9 2023-09-05 thomas repo->fetch_refs[i]);
993 11024cb9 2023-09-05 thomas if (new->fetch_refs[i] == NULL) {
994 11024cb9 2023-09-05 thomas err = got_error_from_errno("strdup");
995 11024cb9 2023-09-05 thomas goto done;
1000 11024cb9 2023-09-05 thomas if (err) {
1001 11024cb9 2023-09-05 thomas got_repo_free_remote_repo_data(new);
1002 11024cb9 2023-09-05 thomas free(new);
1004 11024cb9 2023-09-05 thomas *newp = new;
1006 11024cb9 2023-09-05 thomas return err;
1010 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(struct got_remote_repo *repo)
1014 11024cb9 2023-09-05 thomas if (repo == NULL)
1017 b8adfa55 2020-09-25 stsp free(repo->name);
1018 b8adfa55 2020-09-25 stsp repo->name = NULL;
1019 6480c871 2021-08-30 stsp free(repo->fetch_url);
1020 6480c871 2021-08-30 stsp repo->fetch_url = NULL;
1021 6480c871 2021-08-30 stsp free(repo->send_url);
1022 6480c871 2021-08-30 stsp repo->send_url = NULL;
1023 6480c871 2021-08-30 stsp for (i = 0; i < repo->nfetch_branches; i++)
1024 6480c871 2021-08-30 stsp free(repo->fetch_branches[i]);
1025 6480c871 2021-08-30 stsp free(repo->fetch_branches);
1026 6480c871 2021-08-30 stsp repo->fetch_branches = NULL;
1027 6480c871 2021-08-30 stsp repo->nfetch_branches = 0;
1028 6480c871 2021-08-30 stsp for (i = 0; i < repo->nsend_branches; i++)
1029 6480c871 2021-08-30 stsp free(repo->send_branches[i]);
1030 6480c871 2021-08-30 stsp free(repo->send_branches);
1031 6480c871 2021-08-30 stsp repo->send_branches = NULL;
1032 6480c871 2021-08-30 stsp repo->nsend_branches = 0;
1033 11024cb9 2023-09-05 thomas for (i = 0; i < repo->nfetch_refs; i++)
1034 11024cb9 2023-09-05 thomas free(repo->fetch_refs[i]);
1035 11024cb9 2023-09-05 thomas free(repo->fetch_refs);
1036 11024cb9 2023-09-05 thomas repo->fetch_refs = NULL;
1037 11024cb9 2023-09-05 thomas repo->nfetch_refs = 0;
1040 04ca23f4 2018-07-16 stsp const struct got_error *
1041 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
1042 8fa913ec 2020-11-14 stsp const char *input_path)
1044 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
1045 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
1046 e83c0634 2020-01-27 stsp size_t repolen, len;
1047 e83c0634 2020-01-27 stsp char *canonpath, *path = NULL;
1049 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
1051 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
1052 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
1053 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1054 04ca23f4 2018-07-16 stsp goto done;
1056 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
1058 04ca23f4 2018-07-16 stsp goto done;
1060 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
1062 8fa913ec 2020-11-14 stsp if (canonpath[0] == '\0') {
1063 23721109 2018-10-22 stsp path = strdup(canonpath);
1064 b70703ad 2019-03-18 stsp if (path == NULL) {
1065 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1066 04ca23f4 2018-07-16 stsp goto done;
1069 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
1070 04ca23f4 2018-07-16 stsp if (path == NULL) {
1071 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
1072 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
1073 230a42bd 2019-05-11 jcs canonpath);
1074 b70703ad 2019-03-18 stsp goto done;
1077 b70703ad 2019-03-18 stsp * Path is not on disk.
1078 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
1080 b70703ad 2019-03-18 stsp path = strdup(canonpath);
1081 b70703ad 2019-03-18 stsp if (path == NULL) {
1082 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1083 b70703ad 2019-03-18 stsp goto done;
1087 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
1088 04ca23f4 2018-07-16 stsp len = strlen(path);
1091 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
1092 04ca23f4 2018-07-16 stsp free(path);
1093 04ca23f4 2018-07-16 stsp path = strdup("");
1094 04ca23f4 2018-07-16 stsp if (path == NULL) {
1095 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1096 04ca23f4 2018-07-16 stsp goto done;
1098 65aa7d1c 2020-01-28 stsp } else if (len > repolen &&
1099 65aa7d1c 2020-01-28 stsp got_path_is_child(path, repo_abspath, repolen)) {
1100 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
1101 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
1103 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
1104 eef9542c 2020-10-22 stsp * database. Treat input as repository-relative.
1106 abc59930 2021-09-05 naddy free(path);
1107 abc59930 2021-09-05 naddy path = canonpath;
1108 abc59930 2021-09-05 naddy canonpath = NULL;
1110 04ca23f4 2018-07-16 stsp char *child;
1111 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
1112 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
1113 04ca23f4 2018-07-16 stsp repo_abspath, path);
1115 04ca23f4 2018-07-16 stsp goto done;
1116 04ca23f4 2018-07-16 stsp free(path);
1117 04ca23f4 2018-07-16 stsp path = child;
1121 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
1122 eef9542c 2020-10-22 stsp * Treat input as repository-relative.
1124 abc59930 2021-09-05 naddy free(path);
1125 abc59930 2021-09-05 naddy path = canonpath;
1126 abc59930 2021-09-05 naddy canonpath = NULL;
1130 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
1131 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
1132 04ca23f4 2018-07-16 stsp char *abspath;
1133 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
1134 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1135 04ca23f4 2018-07-16 stsp goto done;
1137 04ca23f4 2018-07-16 stsp free(path);
1138 04ca23f4 2018-07-16 stsp path = abspath;
1142 04ca23f4 2018-07-16 stsp free(canonpath);
1144 04ca23f4 2018-07-16 stsp free(path);
1146 04ca23f4 2018-07-16 stsp *in_repo_path = path;
1147 1510f469 2018-09-09 stsp return err;
1150 e1a68182 2020-01-07 stsp static const struct got_error *
1151 e1a68182 2020-01-07 stsp cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1152 e1a68182 2020-01-07 stsp const char *path_packidx)
1154 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1157 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1158 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
1160 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
1161 e1a68182 2020-01-07 stsp path_packidx) == 0) {
1162 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1165 6c414261 2021-03-30 stsp if (i == repo->pack_cache_size) {
1168 ec2b23c5 2022-07-01 thomas } while (i > 0 && repo->pinned_packidx >= 0 &&
1169 ec2b23c5 2022-07-01 thomas i == repo->pinned_packidx);
1170 3333aec6 2021-10-15 thomas err = got_packidx_close(repo->packidx_cache[i]);
1172 1510f469 2018-09-09 stsp return err;
1175 3333aec6 2021-10-15 thomas repo->packidx_cache[i] = packidx;
1177 1510f469 2018-09-09 stsp return NULL;
1181 1124fe40 2021-07-07 stsp got_repo_is_packidx_filename(const char *name, size_t len)
1183 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
1186 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1189 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1190 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1193 4545b700 2021-10-15 thomas return 1;
1196 cfe41121 2021-10-16 thomas static struct got_packidx_bloom_filter *
1197 cfe41121 2021-10-16 thomas get_packidx_bloom_filter(struct got_repository *repo,
1198 cfe41121 2021-10-16 thomas const char *path, size_t path_len)
1200 cfe41121 2021-10-16 thomas struct got_packidx_bloom_filter key;
1202 cfe41121 2021-10-16 thomas if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1203 cfe41121 2021-10-16 thomas return NULL; /* XXX */
1204 cfe41121 2021-10-16 thomas key.path_len = path_len;
1206 cfe41121 2021-10-16 thomas return RB_FIND(got_packidx_bloom_filter_tree,
1207 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters, &key);
1211 f9c2e8e5 2022-02-13 thomas got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1212 4545b700 2021-10-15 thomas const char *path_packidx, struct got_object_id *id)
1214 4545b700 2021-10-15 thomas struct got_packidx_bloom_filter *bf;
1216 cfe41121 2021-10-16 thomas bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1218 cfe41121 2021-10-16 thomas return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1220 4545b700 2021-10-15 thomas /* No bloom filter means this pack index must be searched. */
1224 4545b700 2021-10-15 thomas static const struct got_error *
1225 4545b700 2021-10-15 thomas add_packidx_bloom_filter(struct got_repository *repo,
1226 4545b700 2021-10-15 thomas struct got_packidx *packidx, const char *path_packidx)
1228 4545b700 2021-10-15 thomas int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1229 4545b700 2021-10-15 thomas struct got_packidx_bloom_filter *bf;
1230 4545b700 2021-10-15 thomas size_t len;
1233 4545b700 2021-10-15 thomas * Don't use bloom filters for very large pack index files.
1234 4545b700 2021-10-15 thomas * Large pack files will contain a relatively large fraction
1235 4545b700 2021-10-15 thomas * of our objects so we will likely need to visit them anyway.
1236 4545b700 2021-10-15 thomas * The more objects a pack file contains the higher the probability
1237 4545b700 2021-10-15 thomas * of a false-positive match from the bloom filter. And reading
1238 4545b700 2021-10-15 thomas * all object IDs from a large pack index file can be expensive.
1240 4545b700 2021-10-15 thomas if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1241 4545b700 2021-10-15 thomas return NULL;
1243 4545b700 2021-10-15 thomas /* Do we already have a filter for this pack index? */
1244 cfe41121 2021-10-16 thomas if (get_packidx_bloom_filter(repo, path_packidx,
1245 cfe41121 2021-10-16 thomas strlen(path_packidx)) != NULL)
1246 cfe41121 2021-10-16 thomas return NULL;
1248 4545b700 2021-10-15 thomas bf = calloc(1, sizeof(*bf));
1249 4545b700 2021-10-15 thomas if (bf == NULL)
1250 4545b700 2021-10-15 thomas return got_error_from_errno("calloc");
1251 4545b700 2021-10-15 thomas bf->bloom = calloc(1, sizeof(*bf->bloom));
1252 4545b700 2021-10-15 thomas if (bf->bloom == NULL) {
1253 4545b700 2021-10-15 thomas free(bf);
1254 4545b700 2021-10-15 thomas return got_error_from_errno("calloc");
1257 cfe41121 2021-10-16 thomas len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1258 cfe41121 2021-10-16 thomas if (len >= sizeof(bf->path)) {
1259 4545b700 2021-10-15 thomas free(bf->bloom);
1260 4545b700 2021-10-15 thomas free(bf);
1261 4545b700 2021-10-15 thomas return got_error(GOT_ERR_NO_SPACE);
1263 cfe41121 2021-10-16 thomas bf->path_len = len;
1265 4545b700 2021-10-15 thomas /* Minimum size supported by our bloom filter is 1000 entries. */
1266 4545b700 2021-10-15 thomas bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1267 4545b700 2021-10-15 thomas for (i = 0; i < nobjects; i++) {
1268 4545b700 2021-10-15 thomas struct got_packidx_object_id *id;
1269 4545b700 2021-10-15 thomas id = &packidx->hdr.sorted_ids[i];
1270 4545b700 2021-10-15 thomas bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1273 cfe41121 2021-10-16 thomas RB_INSERT(got_packidx_bloom_filter_tree,
1274 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters, bf);
1275 4545b700 2021-10-15 thomas return NULL;
1278 5b462462 2022-10-22 thomas static void
1279 5b462462 2022-10-22 thomas purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1281 5b462462 2022-10-22 thomas struct got_pathlist_entry *pe;
1283 5b462462 2022-10-22 thomas while (!TAILQ_EMPTY(packidx_paths)) {
1284 5b462462 2022-10-22 thomas pe = TAILQ_FIRST(packidx_paths);
1285 5b462462 2022-10-22 thomas TAILQ_REMOVE(packidx_paths, pe, entry);
1286 5b462462 2022-10-22 thomas free((char *)pe->path);
1287 5b462462 2022-10-22 thomas free(pe);
1291 5b462462 2022-10-22 thomas static const struct got_error *
1292 5b462462 2022-10-22 thomas refresh_packidx_paths(struct got_repository *repo)
1294 5b462462 2022-10-22 thomas const struct got_error *err = NULL;
1295 5b462462 2022-10-22 thomas char *objects_pack_dir = NULL;
1296 5b462462 2022-10-22 thomas struct stat sb;
1298 5b462462 2022-10-22 thomas objects_pack_dir = got_repo_get_path_objects_pack(repo);
1299 5b462462 2022-10-22 thomas if (objects_pack_dir == NULL)
1300 5b462462 2022-10-22 thomas return got_error_from_errno("got_repo_get_path_objects_pack");
1302 5b462462 2022-10-22 thomas if (stat(objects_pack_dir, &sb) == -1) {
1303 5b462462 2022-10-22 thomas if (errno != ENOENT) {
1304 5b462462 2022-10-22 thomas err = got_error_from_errno2("stat", objects_pack_dir);
1305 5b462462 2022-10-22 thomas goto done;
1307 9b251092 2022-11-08 thomas } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1308 9b251092 2022-11-08 thomas sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1309 9b251092 2022-11-08 thomas sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1310 5b462462 2022-10-22 thomas purge_packidx_paths(&repo->packidx_paths);
1311 5b462462 2022-10-22 thomas err = got_repo_list_packidx(&repo->packidx_paths, repo);
1312 5b462462 2022-10-22 thomas if (err)
1313 5b462462 2022-10-22 thomas goto done;
1316 5b462462 2022-10-22 thomas free(objects_pack_dir);
1317 5b462462 2022-10-22 thomas return err;
1320 1510f469 2018-09-09 stsp const struct got_error *
1321 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1322 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
1324 1510f469 2018-09-09 stsp const struct got_error *err;
1325 de47d040 2022-03-22 thomas struct got_pathlist_entry *pe;
1328 1510f469 2018-09-09 stsp /* Search pack index cache. */
1329 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1330 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
1332 f9c2e8e5 2022-02-13 thomas if (!got_repo_check_packidx_bloom_filter(repo,
1333 4545b700 2021-10-15 thomas repo->packidx_cache[i]->path_packidx, id))
1334 4545b700 2021-10-15 thomas continue; /* object will not be found in this index */
1335 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1336 1510f469 2018-09-09 stsp if (*idx != -1) {
1337 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
1339 87c1ed2b 2020-01-07 stsp * Move this cache entry to the front. Repeatedly
1340 87c1ed2b 2020-01-07 stsp * searching a wrong pack index can be expensive.
1342 87c1ed2b 2020-01-07 stsp if (i > 0) {
1343 3333aec6 2021-10-15 thomas memmove(&repo->packidx_cache[1],
1344 3333aec6 2021-10-15 thomas &repo->packidx_cache[0],
1345 3333aec6 2021-10-15 thomas i * sizeof(repo->packidx_cache[0]));
1346 87c1ed2b 2020-01-07 stsp repo->packidx_cache[0] = *packidx;
1347 ec2b23c5 2022-07-01 thomas if (repo->pinned_packidx >= 0 &&
1348 ec2b23c5 2022-07-01 thomas repo->pinned_packidx < i)
1349 ec2b23c5 2022-07-01 thomas repo->pinned_packidx++;
1350 ec2b23c5 2022-07-01 thomas else if (repo->pinned_packidx == i)
1351 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = 0;
1353 1510f469 2018-09-09 stsp return NULL;
1356 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
1358 5b462462 2022-10-22 thomas err = refresh_packidx_paths(repo);
1359 5b462462 2022-10-22 thomas if (err)
1360 5b462462 2022-10-22 thomas return err;
1362 de47d040 2022-03-22 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1363 de47d040 2022-03-22 thomas const char *path_packidx = pe->path;
1364 e1a68182 2020-01-07 stsp int is_cached = 0;
1366 f9c2e8e5 2022-02-13 thomas if (!got_repo_check_packidx_bloom_filter(repo,
1367 de47d040 2022-03-22 thomas pe->path, id))
1368 4545b700 2021-10-15 thomas continue; /* object will not be found in this index */
1370 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1371 e1a68182 2020-01-07 stsp if (repo->packidx_cache[i] == NULL)
1373 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
1374 e1a68182 2020-01-07 stsp path_packidx) == 0) {
1375 e1a68182 2020-01-07 stsp is_cached = 1;
1379 de47d040 2022-03-22 thomas if (is_cached)
1380 e1a68182 2020-01-07 stsp continue; /* already searched */
1382 6d5a9006 2020-12-16 yzhong err = got_packidx_open(packidx, got_repo_get_fd(repo),
1383 6d5a9006 2020-12-16 yzhong path_packidx, 0);
1384 de47d040 2022-03-22 thomas if (err)
1385 e1a68182 2020-01-07 stsp goto done;
1387 4545b700 2021-10-15 thomas err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1388 de47d040 2022-03-22 thomas if (err)
1389 4545b700 2021-10-15 thomas goto done;
1391 e1a68182 2020-01-07 stsp err = cache_packidx(repo, *packidx, path_packidx);
1393 1510f469 2018-09-09 stsp goto done;
1395 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
1396 1510f469 2018-09-09 stsp if (*idx != -1) {
1397 1510f469 2018-09-09 stsp err = NULL; /* found the object */
1398 1510f469 2018-09-09 stsp goto done;
1402 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
1404 04ca23f4 2018-07-16 stsp return err;
1407 f9c2e8e5 2022-02-13 thomas const struct got_error *
1408 f9c2e8e5 2022-02-13 thomas got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1409 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
1411 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
1412 f9c2e8e5 2022-02-13 thomas DIR *packdir = NULL;
1413 f9c2e8e5 2022-02-13 thomas struct dirent *dent;
1414 f9c2e8e5 2022-02-13 thomas char *path_packidx = NULL;
1415 f9c2e8e5 2022-02-13 thomas int packdir_fd;
1416 04051e8b 2022-09-02 thomas struct stat sb;
1418 f9c2e8e5 2022-02-13 thomas packdir_fd = openat(got_repo_get_fd(repo),
1419 f9c2e8e5 2022-02-13 thomas GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1420 f9c2e8e5 2022-02-13 thomas if (packdir_fd == -1) {
1421 f9c2e8e5 2022-02-13 thomas return got_error_from_errno_fmt("openat: %s/%s",
1422 f9c2e8e5 2022-02-13 thomas got_repo_get_path_git_dir(repo),
1423 f9c2e8e5 2022-02-13 thomas GOT_OBJECTS_PACK_DIR);
1426 f9c2e8e5 2022-02-13 thomas packdir = fdopendir(packdir_fd);
1427 f9c2e8e5 2022-02-13 thomas if (packdir == NULL) {
1428 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fdopendir");
1429 815470d4 2024-01-31 thomas close(packdir_fd);
1430 04051e8b 2022-09-02 thomas goto done;
1433 04051e8b 2022-09-02 thomas if (fstat(packdir_fd, &sb) == -1) {
1434 04051e8b 2022-09-02 thomas err = got_error_from_errno("fstat");
1435 f9c2e8e5 2022-02-13 thomas goto done;
1437 9b251092 2022-11-08 thomas repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1438 9b251092 2022-11-08 thomas repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1440 f9c2e8e5 2022-02-13 thomas while ((dent = readdir(packdir)) != NULL) {
1441 9389bcf6 2022-02-13 thomas if (!got_repo_is_packidx_filename(dent->d_name,
1442 9389bcf6 2022-02-13 thomas strlen(dent->d_name)))
1443 f9c2e8e5 2022-02-13 thomas continue;
1445 f9c2e8e5 2022-02-13 thomas if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1446 f9c2e8e5 2022-02-13 thomas dent->d_name) == -1) {
1447 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("asprintf");
1448 f9c2e8e5 2022-02-13 thomas path_packidx = NULL;
1452 f9c2e8e5 2022-02-13 thomas err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1453 f9c2e8e5 2022-02-13 thomas if (err)
1457 f9c2e8e5 2022-02-13 thomas if (err)
1458 f9c2e8e5 2022-02-13 thomas free(path_packidx);
1459 f9c2e8e5 2022-02-13 thomas if (packdir && closedir(packdir) != 0 && err == NULL)
1460 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("closedir");
1461 f9c2e8e5 2022-02-13 thomas return err;
1464 f9c2e8e5 2022-02-13 thomas const struct got_error *
1465 f9c2e8e5 2022-02-13 thomas got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1466 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
1468 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
1469 f9c2e8e5 2022-02-13 thomas size_t i;
1471 f9c2e8e5 2022-02-13 thomas *packidx = NULL;
1473 f9c2e8e5 2022-02-13 thomas /* Search pack index cache. */
1474 f9c2e8e5 2022-02-13 thomas for (i = 0; i < repo->pack_cache_size; i++) {
1475 f9c2e8e5 2022-02-13 thomas if (repo->packidx_cache[i] == NULL)
1477 f9c2e8e5 2022-02-13 thomas if (strcmp(repo->packidx_cache[i]->path_packidx,
1478 f9c2e8e5 2022-02-13 thomas path_packidx) == 0) {
1479 f9c2e8e5 2022-02-13 thomas *packidx = repo->packidx_cache[i];
1480 f9c2e8e5 2022-02-13 thomas return NULL;
1483 f9c2e8e5 2022-02-13 thomas /* No luck. Search the filesystem. */
1485 f9c2e8e5 2022-02-13 thomas err = got_packidx_open(packidx, got_repo_get_fd(repo),
1486 f9c2e8e5 2022-02-13 thomas path_packidx, 0);
1487 f9c2e8e5 2022-02-13 thomas if (err)
1488 f9c2e8e5 2022-02-13 thomas return err;
1490 f9c2e8e5 2022-02-13 thomas err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1491 f9c2e8e5 2022-02-13 thomas if (err)
1492 f9c2e8e5 2022-02-13 thomas goto done;
1494 f9c2e8e5 2022-02-13 thomas err = cache_packidx(repo, *packidx, path_packidx);
1496 f9c2e8e5 2022-02-13 thomas if (err) {
1497 f9c2e8e5 2022-02-13 thomas got_packidx_close(*packidx);
1498 f9c2e8e5 2022-02-13 thomas *packidx = NULL;
1500 f9c2e8e5 2022-02-13 thomas return err;
1503 1510f469 2018-09-09 stsp static const struct got_error *
1504 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
1506 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1507 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1508 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
1509 1510f469 2018-09-09 stsp ssize_t n;
1511 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
1512 1510f469 2018-09-09 stsp if (n < 0)
1513 638f9024 2019-05-13 stsp return got_error_from_errno("read");
1514 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
1515 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1517 78fb0967 2020-09-09 naddy if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1518 78fb0967 2020-09-09 naddy be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1519 78fb0967 2020-09-09 naddy be32toh(hdr.nobjects) != totobj)
1520 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
1522 1510f469 2018-09-09 stsp return err;
1525 1510f469 2018-09-09 stsp static const struct got_error *
1526 6d5a9006 2020-12-16 yzhong open_packfile(int *fd, struct got_repository *repo,
1527 6d5a9006 2020-12-16 yzhong const char *relpath, struct got_packidx *packidx)
1529 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1531 fc63f50d 2021-12-31 thomas *fd = openat(got_repo_get_fd(repo), relpath,
1532 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1533 1510f469 2018-09-09 stsp if (*fd == -1)
1534 6d5a9006 2020-12-16 yzhong return got_error_from_errno_fmt("openat: %s/%s",
1535 6d5a9006 2020-12-16 yzhong got_repo_get_path_git_dir(repo), relpath);
1537 1510f469 2018-09-09 stsp if (packidx) {
1538 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
1539 1510f469 2018-09-09 stsp if (err) {
1540 1510f469 2018-09-09 stsp close(*fd);
1545 1510f469 2018-09-09 stsp return err;
1548 1510f469 2018-09-09 stsp const struct got_error *
1549 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1550 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
1552 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1553 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1554 ff563a3d 2019-05-23 stsp struct stat sb;
1557 1510f469 2018-09-09 stsp if (packp)
1558 1510f469 2018-09-09 stsp *packp = NULL;
1560 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1561 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1562 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1564 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1565 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1568 6c414261 2021-03-30 stsp if (i == repo->pack_cache_size) {
1569 93948435 2022-06-13 thomas struct got_pack tmp;
1572 ec2b23c5 2022-07-01 thomas } while (i > 0 && repo->pinned_pack >= 0 &&
1573 ec2b23c5 2022-07-01 thomas i == repo->pinned_pack);
1574 ec2b23c5 2022-07-01 thomas err = got_pack_close(&repo->packs[i]);
1576 1510f469 2018-09-09 stsp return err;
1577 ec2b23c5 2022-07-01 thomas if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1578 bfb5ee0b 2022-05-31 thomas return got_error_from_errno("ftruncate");
1579 ec2b23c5 2022-07-01 thomas if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1580 ec2b23c5 2022-07-01 thomas return got_error_from_errno("ftruncate");
1581 b6b86fd1 2022-08-30 thomas memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1582 ec2b23c5 2022-07-01 thomas memcpy(&repo->packs[i], &repo->packs[0],
1583 ec2b23c5 2022-07-01 thomas sizeof(repo->packs[i]));
1584 93948435 2022-06-13 thomas memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1585 ec2b23c5 2022-07-01 thomas if (repo->pinned_pack == 0)
1586 ec2b23c5 2022-07-01 thomas repo->pinned_pack = i;
1587 ec2b23c5 2022-07-01 thomas else if (repo->pinned_pack == i)
1588 ec2b23c5 2022-07-01 thomas repo->pinned_pack = 0;
1592 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1594 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
1595 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1597 1510f469 2018-09-09 stsp goto done;
1600 6d5a9006 2020-12-16 yzhong err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1602 1510f469 2018-09-09 stsp goto done;
1604 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
1605 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
1606 1510f469 2018-09-09 stsp goto done;
1608 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
1610 90636195 2018-09-11 stsp pack->privsep_child = NULL;
1612 3efd8e31 2022-10-23 thomas err = got_delta_cache_alloc(&pack->delta_cache);
1613 3efd8e31 2022-10-23 thomas if (err)
1614 3efd8e31 2022-10-23 thomas goto done;
1616 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
1617 aa75acde 2022-10-25 thomas if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1618 aa75acde 2022-10-25 thomas pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1619 aa75acde 2022-10-25 thomas pack->fd, 0);
1620 aa75acde 2022-10-25 thomas if (pack->map == MAP_FAILED) {
1621 aa75acde 2022-10-25 thomas if (errno != ENOMEM) {
1622 aa75acde 2022-10-25 thomas err = got_error_from_errno("mmap");
1623 aa75acde 2022-10-25 thomas goto done;
1625 aa75acde 2022-10-25 thomas pack->map = NULL; /* fall back to read(2) */
1630 1510f469 2018-09-09 stsp if (err) {
1631 3efd8e31 2022-10-23 thomas if (pack)
1632 3efd8e31 2022-10-23 thomas got_pack_close(pack);
1633 1510f469 2018-09-09 stsp } else if (packp)
1634 1510f469 2018-09-09 stsp *packp = pack;
1635 1510f469 2018-09-09 stsp return err;
1638 1510f469 2018-09-09 stsp struct got_pack *
1639 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1641 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1644 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1645 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1646 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1648 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1649 1510f469 2018-09-09 stsp return pack;
1652 ec2b23c5 2022-07-01 thomas return NULL;
1655 ec2b23c5 2022-07-01 thomas const struct got_error *
1656 ec2b23c5 2022-07-01 thomas got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1657 ec2b23c5 2022-07-01 thomas struct got_pack *pack)
1659 ec2b23c5 2022-07-01 thomas size_t i;
1660 ec2b23c5 2022-07-01 thomas int pinned_pack = -1, pinned_packidx = -1;
1662 ec2b23c5 2022-07-01 thomas for (i = 0; i < repo->pack_cache_size; i++) {
1663 ec2b23c5 2022-07-01 thomas if (repo->packidx_cache[i] &&
1664 ec2b23c5 2022-07-01 thomas strcmp(repo->packidx_cache[i]->path_packidx,
1665 ec2b23c5 2022-07-01 thomas packidx->path_packidx) == 0)
1666 ec2b23c5 2022-07-01 thomas pinned_packidx = i;
1667 ec2b23c5 2022-07-01 thomas if (repo->packs[i].path_packfile &&
1668 ec2b23c5 2022-07-01 thomas strcmp(repo->packs[i].path_packfile,
1669 ec2b23c5 2022-07-01 thomas pack->path_packfile) == 0)
1670 ec2b23c5 2022-07-01 thomas pinned_pack = i;
1673 ec2b23c5 2022-07-01 thomas if (pinned_packidx == -1 || pinned_pack == -1)
1674 ec2b23c5 2022-07-01 thomas return got_error(GOT_ERR_PIN_PACK);
1676 ec2b23c5 2022-07-01 thomas repo->pinned_pack = pinned_pack;
1677 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = pinned_packidx;
1678 c44c7d6e 2022-12-04 thomas if (repo->packs[pinned_pack].privsep_child)
1679 c44c7d6e 2022-12-04 thomas repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1680 ec2b23c5 2022-07-01 thomas return NULL;
1683 ec2b23c5 2022-07-01 thomas struct got_pack *
1684 ec2b23c5 2022-07-01 thomas got_repo_get_pinned_pack(struct got_repository *repo)
1686 ec2b23c5 2022-07-01 thomas if (repo->pinned_pack >= 0 &&
1687 ec2b23c5 2022-07-01 thomas repo->pinned_pack < repo->pack_cache_size)
1688 ec2b23c5 2022-07-01 thomas return &repo->packs[repo->pinned_pack];
1690 2c7829a4 2019-06-17 stsp return NULL;
1694 ec2b23c5 2022-07-01 thomas got_repo_unpin_pack(struct got_repository *repo)
1696 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = -1;
1697 ec2b23c5 2022-07-01 thomas repo->pinned_pack = -1;
1698 ec2b23c5 2022-07-01 thomas repo->pinned_pid = 0;
1701 2c7829a4 2019-06-17 stsp const struct got_error *
1702 e9424ba1 2022-09-20 thomas got_repo_init(const char *repo_path, const char *head_name)
1704 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
1705 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
1706 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
1707 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
1708 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
1710 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
1711 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
1712 e9424ba1 2022-09-20 thomas const char *headref = "ref: refs/heads/";
1713 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
1714 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
1715 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
1716 2c7829a4 2019-06-17 stsp "\tbare = true\n";
1717 e9424ba1 2022-09-20 thomas char *headref_str, *path;
1720 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
1721 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
1723 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
1724 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1725 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1727 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
1728 2c7829a4 2019-06-17 stsp free(path);
1730 2c7829a4 2019-06-17 stsp return err;
1733 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1734 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1735 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
1736 2c7829a4 2019-06-17 stsp free(path);
1738 2c7829a4 2019-06-17 stsp return err;
1740 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1741 e9424ba1 2022-09-20 thomas return got_error_from_errno("asprintf");
1742 e9424ba1 2022-09-20 thomas if (asprintf(&headref_str, "%s%s", headref,
1743 e9424ba1 2022-09-20 thomas head_name ? head_name : "main") == -1) {
1744 e9424ba1 2022-09-20 thomas free(path);
1745 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1747 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
1748 e9424ba1 2022-09-20 thomas free(headref_str);
1749 2c7829a4 2019-06-17 stsp free(path);
1751 2c7829a4 2019-06-17 stsp return err;
1753 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1754 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1755 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
1756 2c7829a4 2019-06-17 stsp free(path);
1758 2c7829a4 2019-06-17 stsp return err;
1760 1510f469 2018-09-09 stsp return NULL;
1763 e09a504c 2019-06-28 stsp static const struct got_error *
1764 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
1765 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
1767 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1768 de47d040 2022-03-22 thomas struct got_object_id_queue matched_ids;
1769 de47d040 2022-03-22 thomas struct got_pathlist_entry *pe;
1770 405e082c 2023-11-26 thomas struct timespec tv;
1771 405e082c 2023-11-26 thomas int retries = 0;
1772 405e082c 2023-11-26 thomas const int max_retries = 10;
1774 dbdddfee 2021-06-23 naddy STAILQ_INIT(&matched_ids);
1776 5b462462 2022-10-22 thomas err = refresh_packidx_paths(repo);
1777 5b462462 2022-10-22 thomas if (err)
1778 5b462462 2022-10-22 thomas return err;
1781 405e082c 2023-11-26 thomas * Opening objects while iterating over the pack-index path
1782 405e082c 2023-11-26 thomas * list is racy. If the set of pack files in the repository
1783 405e082c 2023-11-26 thomas * changes during loop iteration, refresh_packidx_paths() will
1784 405e082c 2023-11-26 thomas * be called again, via got_object_get_type(), invalidating
1785 405e082c 2023-11-26 thomas * the packidx_paths list we are iterating over.
1786 405e082c 2023-11-26 thomas * To work around this we keep track of the current modification
1787 405e082c 2023-11-26 thomas * time and retry the entire loop if it changes.
1790 405e082c 2023-11-26 thomas tv.tv_sec = repo->pack_path_mtime.tv_sec;
1791 405e082c 2023-11-26 thomas tv.tv_nsec = repo->pack_path_mtime.tv_nsec;
1793 de47d040 2022-03-22 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1794 405e082c 2023-11-26 thomas const char *path_packidx;
1795 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
1796 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
1799 405e082c 2023-11-26 thomas * If the modification time of the 'objects/pack' directory
1800 405e082c 2023-11-26 thomas * has changed then 'pe' could now be an invalid pointer.
1802 405e082c 2023-11-26 thomas if (tv.tv_sec != repo->pack_path_mtime.tv_sec ||
1803 405e082c 2023-11-26 thomas tv.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1804 405e082c 2023-11-26 thomas if (++retries > max_retries) {
1805 405e082c 2023-11-26 thomas err = got_error_msg(GOT_ERR_TIMEOUT,
1806 405e082c 2023-11-26 thomas "too many concurrent pack file "
1807 405e082c 2023-11-26 thomas "modifications");
1808 405e082c 2023-11-26 thomas goto done;
1810 405e082c 2023-11-26 thomas goto retry;
1813 405e082c 2023-11-26 thomas path_packidx = pe->path;
1815 6d5a9006 2020-12-16 yzhong err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1816 6d5a9006 2020-12-16 yzhong path_packidx, 0);
1820 7438da3b 2023-12-19 thomas got_object_id_queue_free(&matched_ids);
1822 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
1823 4277420a 2019-06-29 stsp packidx, id_str_prefix);
1824 4277420a 2019-06-29 stsp if (err) {
1825 4277420a 2019-06-29 stsp got_packidx_close(packidx);
1828 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
1832 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &matched_ids, entry) {
1833 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1834 dd88155e 2019-06-29 stsp int matched_type;
1835 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1836 ec242592 2022-04-22 thomas &qid->id);
1838 dd88155e 2019-06-29 stsp goto done;
1839 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1842 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
1843 ec242592 2022-04-22 thomas *unique_id = got_object_id_dup(&qid->id);
1844 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
1845 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
1846 dd88155e 2019-06-29 stsp goto done;
1849 ec242592 2022-04-22 thomas if (got_object_id_cmp(*unique_id,
1850 ec242592 2022-04-22 thomas &qid->id) == 0)
1851 1accf02b 2020-01-05 stsp continue; /* packed multiple times */
1852 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1853 561c3678 2019-07-02 stsp goto done;
1858 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
1859 e09a504c 2019-06-28 stsp if (err) {
1860 e09a504c 2019-06-28 stsp free(*unique_id);
1861 e09a504c 2019-06-28 stsp *unique_id = NULL;
1863 e09a504c 2019-06-28 stsp return err;
1866 e09a504c 2019-06-28 stsp static const struct got_error *
1867 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1868 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
1869 e09a504c 2019-06-28 stsp struct got_repository *repo)
1871 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1872 5ef2cc1c 2022-09-02 thomas char *path, *id_str = NULL;
1873 e09a504c 2019-06-28 stsp DIR *dir = NULL;
1874 e09a504c 2019-06-28 stsp struct dirent *dent;
1875 e09a504c 2019-06-28 stsp struct got_object_id id;
1877 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1878 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1879 e09a504c 2019-06-28 stsp goto done;
1882 e09a504c 2019-06-28 stsp dir = opendir(path);
1883 e09a504c 2019-06-28 stsp if (dir == NULL) {
1884 4277420a 2019-06-29 stsp if (errno == ENOENT) {
1885 4277420a 2019-06-29 stsp err = NULL;
1886 4277420a 2019-06-29 stsp goto done;
1888 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
1889 e09a504c 2019-06-28 stsp goto done;
1891 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
1894 5ef2cc1c 2022-09-02 thomas free(id_str);
1895 5ef2cc1c 2022-09-02 thomas id_str = NULL;
1897 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
1898 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
1901 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1902 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1903 e09a504c 2019-06-28 stsp goto done;
1906 f4204d57 2023-03-01 thomas if (!got_parse_object_id(&id, id_str, repo->algo))
1910 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1911 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1913 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1914 5ef2cc1c 2022-09-02 thomas if (cmp != 0)
1917 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1918 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1919 dd88155e 2019-06-29 stsp int matched_type;
1920 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1923 dd88155e 2019-06-29 stsp goto done;
1924 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1927 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1928 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1929 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1930 e09a504c 2019-06-28 stsp goto done;
1933 1accf02b 2020-01-05 stsp if (got_object_id_cmp(*unique_id, &id) == 0)
1934 1accf02b 2020-01-05 stsp continue; /* both packed and loose */
1935 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1936 e09a504c 2019-06-28 stsp goto done;
1940 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1941 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1942 e09a504c 2019-06-28 stsp if (err) {
1943 e09a504c 2019-06-28 stsp free(*unique_id);
1944 e09a504c 2019-06-28 stsp *unique_id = NULL;
1946 5ef2cc1c 2022-09-02 thomas free(id_str);
1947 e09a504c 2019-06-28 stsp free(path);
1948 e09a504c 2019-06-28 stsp return err;
1951 e09a504c 2019-06-28 stsp const struct got_error *
1952 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1953 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1955 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1956 66ca6940 2022-09-06 thomas char *path_objects = NULL, *object_dir = NULL;
1957 e09a504c 2019-06-28 stsp size_t len;
1960 4277420a 2019-06-29 stsp *id = NULL;
1962 66ca6940 2022-09-06 thomas path_objects = got_repo_get_path_objects(repo);
1964 70e6418e 2022-03-10 thomas len = strlen(id_str_prefix);
1965 66ca6940 2022-09-06 thomas if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1966 66ca6940 2022-09-06 thomas err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1967 66ca6940 2022-09-06 thomas goto done;
1970 70e6418e 2022-03-10 thomas for (i = 0; i < len; i++) {
1971 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1973 66ca6940 2022-09-06 thomas err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1974 66ca6940 2022-09-06 thomas goto done;
1977 e09a504c 2019-06-28 stsp if (len >= 2) {
1978 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1980 83c8b3b8 2019-06-29 stsp goto done;
1981 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1982 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1983 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1984 83c8b3b8 2019-06-29 stsp goto done;
1986 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1987 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1988 e09a504c 2019-06-28 stsp } else if (len == 1) {
1990 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1991 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1993 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1994 83c8b3b8 2019-06-29 stsp goto done;
1996 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1997 dd88155e 2019-06-29 stsp obj_type);
1999 83c8b3b8 2019-06-29 stsp goto done;
2000 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
2001 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
2003 83c8b3b8 2019-06-29 stsp goto done;
2006 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
2007 83c8b3b8 2019-06-29 stsp goto done;
2010 66ca6940 2022-09-06 thomas free(path_objects);
2011 e09a504c 2019-06-28 stsp free(object_dir);
2012 4277420a 2019-06-29 stsp if (err) {
2013 4277420a 2019-06-29 stsp free(*id);
2014 4277420a 2019-06-29 stsp *id = NULL;
2015 a3599220 2021-10-10 thomas } else if (*id == NULL) {
2016 a3599220 2021-10-10 thomas switch (obj_type) {
2017 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_BLOB:
2018 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2019 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_BLOB, id_str_prefix);
2021 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_TREE:
2022 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2023 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_TREE, id_str_prefix);
2025 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_COMMIT:
2026 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2027 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_COMMIT, id_str_prefix);
2029 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_TAG:
2030 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2031 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_TAG, id_str_prefix);
2033 a3599220 2021-10-10 thomas default:
2034 a3599220 2021-10-10 thomas err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
2039 303e2782 2019-08-09 stsp return err;
2042 303e2782 2019-08-09 stsp const struct got_error *
2043 71a27632 2020-01-15 stsp got_repo_match_object_id(struct got_object_id **id, char **label,
2044 84de9106 2020-12-26 stsp const char *id_str, int obj_type, struct got_reflist_head *refs,
2045 71a27632 2020-01-15 stsp struct got_repository *repo)
2047 71a27632 2020-01-15 stsp const struct got_error *err;
2048 71a27632 2020-01-15 stsp struct got_tag_object *tag;
2049 71a27632 2020-01-15 stsp struct got_reference *ref = NULL;
2051 71a27632 2020-01-15 stsp *id = NULL;
2052 71a27632 2020-01-15 stsp if (label)
2053 71a27632 2020-01-15 stsp *label = NULL;
2055 84de9106 2020-12-26 stsp if (refs) {
2056 f1165c79 2021-10-10 thomas err = got_repo_object_match_tag(&tag, id_str, obj_type,
2057 84de9106 2020-12-26 stsp refs, repo);
2058 71a27632 2020-01-15 stsp if (err == NULL) {
2059 71a27632 2020-01-15 stsp *id = got_object_id_dup(
2060 71a27632 2020-01-15 stsp got_object_tag_get_object_id(tag));
2061 71a27632 2020-01-15 stsp if (*id == NULL)
2062 71a27632 2020-01-15 stsp err = got_error_from_errno("got_object_id_dup");
2063 71a27632 2020-01-15 stsp else if (label && asprintf(label, "refs/tags/%s",
2064 71a27632 2020-01-15 stsp got_object_tag_get_name(tag)) == -1) {
2065 71a27632 2020-01-15 stsp err = got_error_from_errno("asprintf");
2066 71a27632 2020-01-15 stsp free(*id);
2067 71a27632 2020-01-15 stsp *id = NULL;
2069 71a27632 2020-01-15 stsp got_object_tag_close(tag);
2070 71a27632 2020-01-15 stsp return err;
2071 71a27632 2020-01-15 stsp } else if (err->code != GOT_ERR_OBJ_TYPE &&
2072 71a27632 2020-01-15 stsp err->code != GOT_ERR_NO_OBJ)
2073 71a27632 2020-01-15 stsp return err;
2076 65ad15fa 2022-07-16 thomas err = got_ref_open(&ref, repo, id_str, 0);
2077 65ad15fa 2022-07-16 thomas if (err == NULL) {
2078 65ad15fa 2022-07-16 thomas err = got_ref_resolve(id, repo, ref);
2079 65ad15fa 2022-07-16 thomas if (err)
2080 71a27632 2020-01-15 stsp goto done;
2081 71a27632 2020-01-15 stsp if (label) {
2082 71a27632 2020-01-15 stsp *label = strdup(got_ref_get_name(ref));
2083 71a27632 2020-01-15 stsp if (*label == NULL) {
2084 71a27632 2020-01-15 stsp err = got_error_from_errno("strdup");
2085 71a27632 2020-01-15 stsp goto done;
2088 65ad15fa 2022-07-16 thomas } else {
2089 65ad15fa 2022-07-16 thomas if (err->code != GOT_ERR_NOT_REF &&
2090 65ad15fa 2022-07-16 thomas err->code != GOT_ERR_BAD_REF_NAME)
2091 71a27632 2020-01-15 stsp goto done;
2092 65ad15fa 2022-07-16 thomas err = got_repo_match_object_id_prefix(id, id_str,
2093 65ad15fa 2022-07-16 thomas obj_type, repo);
2094 65ad15fa 2022-07-16 thomas if (err) {
2095 65ad15fa 2022-07-16 thomas if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
2096 65ad15fa 2022-07-16 thomas err = got_error_not_ref(id_str);
2097 65ad15fa 2022-07-16 thomas goto done;
2099 65ad15fa 2022-07-16 thomas if (label) {
2100 65ad15fa 2022-07-16 thomas err = got_object_id_str(label, *id);
2101 65ad15fa 2022-07-16 thomas if (*label == NULL) {
2102 65ad15fa 2022-07-16 thomas err = got_error_from_errno("strdup");
2103 65ad15fa 2022-07-16 thomas goto done;
2109 71a27632 2020-01-15 stsp got_ref_close(ref);
2110 71a27632 2020-01-15 stsp return err;
2113 71a27632 2020-01-15 stsp const struct got_error *
2114 303e2782 2019-08-09 stsp got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
2115 84de9106 2020-12-26 stsp int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
2117 84de9106 2020-12-26 stsp const struct got_error *err = NULL;
2118 303e2782 2019-08-09 stsp struct got_reflist_entry *re;
2119 303e2782 2019-08-09 stsp struct got_object_id *tag_id;
2120 785d65a4 2020-12-05 stsp int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
2122 303e2782 2019-08-09 stsp *tag = NULL;
2124 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2125 303e2782 2019-08-09 stsp const char *refname;
2126 303e2782 2019-08-09 stsp refname = got_ref_get_name(re->ref);
2127 29606af7 2019-08-23 stsp if (got_ref_is_symbolic(re->ref))
2129 84de9106 2020-12-26 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
2131 785d65a4 2020-12-05 stsp if (!name_is_absolute)
2132 785d65a4 2020-12-05 stsp refname += strlen("refs/tags/");
2133 303e2782 2019-08-09 stsp if (strcmp(refname, name) != 0)
2135 303e2782 2019-08-09 stsp err = got_ref_resolve(&tag_id, repo, re->ref);
2138 303e2782 2019-08-09 stsp err = got_object_open_as_tag(tag, repo, tag_id);
2139 303e2782 2019-08-09 stsp free(tag_id);
2142 d24820bf 2019-08-11 stsp if (obj_type == GOT_OBJ_TYPE_ANY ||
2143 d24820bf 2019-08-11 stsp got_object_tag_get_object_type(*tag) == obj_type)
2145 303e2782 2019-08-09 stsp got_object_tag_close(*tag);
2146 303e2782 2019-08-09 stsp *tag = NULL;
2149 303e2782 2019-08-09 stsp if (err == NULL && *tag == NULL)
2150 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
2151 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_TAG, name);
2152 e09a504c 2019-06-28 stsp return err;
2155 44f875ad 2024-01-18 thomas const struct got_error *
2156 44f875ad 2024-01-18 thomas got_repo_find_object_id(struct got_object_id *id, struct got_repository *repo)
2158 44f875ad 2024-01-18 thomas const struct got_error *err;
2159 44f875ad 2024-01-18 thomas struct got_object_id *matched_id = NULL;
2160 44f875ad 2024-01-18 thomas char *id_str = NULL;
2162 44f875ad 2024-01-18 thomas err = got_object_id_str(&id_str, id);
2163 44f875ad 2024-01-18 thomas if (err)
2164 44f875ad 2024-01-18 thomas return err;
2166 44f875ad 2024-01-18 thomas err = got_repo_match_object_id_prefix(&matched_id, id_str,
2167 44f875ad 2024-01-18 thomas GOT_OBJ_TYPE_ANY, repo);
2168 44f875ad 2024-01-18 thomas free(id_str);
2169 44f875ad 2024-01-18 thomas return err;
2172 3ce1b845 2019-07-15 stsp static const struct got_error *
2173 3ce1b845 2019-07-15 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2174 3ce1b845 2019-07-15 stsp const char *name, mode_t mode, struct got_object_id *blob_id)
2176 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2178 9c57c4c6 2024-02-13 thomas *new_te = NULL;
2180 3ce1b845 2019-07-15 stsp *new_te = calloc(1, sizeof(**new_te));
2181 3ce1b845 2019-07-15 stsp if (*new_te == NULL)
2182 3ce1b845 2019-07-15 stsp return got_error_from_errno("calloc");
2184 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2185 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
2186 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
2187 3ce1b845 2019-07-15 stsp goto done;
2190 e8863bdc 2020-07-23 stsp if (S_ISLNK(mode)) {
2191 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFLNK;
2193 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFREG;
2194 e8863bdc 2020-07-23 stsp (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2196 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2198 3ce1b845 2019-07-15 stsp if (err && *new_te) {
2199 56e0773d 2019-11-28 stsp free(*new_te);
2200 3ce1b845 2019-07-15 stsp *new_te = NULL;
2202 3ce1b845 2019-07-15 stsp return err;
2205 3ce1b845 2019-07-15 stsp static const struct got_error *
2206 3ce1b845 2019-07-15 stsp import_file(struct got_tree_entry **new_te, struct dirent *de,
2207 3ce1b845 2019-07-15 stsp const char *path, struct got_repository *repo)
2209 3ce1b845 2019-07-15 stsp const struct got_error *err;
2210 3ce1b845 2019-07-15 stsp struct got_object_id *blob_id = NULL;
2211 3ce1b845 2019-07-15 stsp char *filepath;
2212 3ce1b845 2019-07-15 stsp struct stat sb;
2214 3ce1b845 2019-07-15 stsp if (asprintf(&filepath, "%s%s%s", path,
2215 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
2216 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
2218 3ce1b845 2019-07-15 stsp if (lstat(filepath, &sb) != 0) {
2219 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("lstat", path);
2220 3ce1b845 2019-07-15 stsp goto done;
2223 3ce1b845 2019-07-15 stsp err = got_object_blob_create(&blob_id, filepath, repo);
2225 3ce1b845 2019-07-15 stsp goto done;
2227 3ce1b845 2019-07-15 stsp err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2230 3ce1b845 2019-07-15 stsp free(filepath);
2232 3ce1b845 2019-07-15 stsp free(blob_id);
2233 3ce1b845 2019-07-15 stsp return err;
2236 3ce1b845 2019-07-15 stsp static const struct got_error *
2237 3ce1b845 2019-07-15 stsp insert_tree_entry(struct got_tree_entry *new_te,
2238 3ce1b845 2019-07-15 stsp struct got_pathlist_head *paths)
2240 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2241 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *new_pe;
2243 3ce1b845 2019-07-15 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2245 3ce1b845 2019-07-15 stsp return err;
2246 3ce1b845 2019-07-15 stsp if (new_pe == NULL)
2247 3ce1b845 2019-07-15 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2248 3ce1b845 2019-07-15 stsp return NULL;
2251 3ce1b845 2019-07-15 stsp static const struct got_error *write_tree(struct got_object_id **,
2252 3ce1b845 2019-07-15 stsp const char *, struct got_pathlist_head *, struct got_repository *,
2253 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg);
2255 3ce1b845 2019-07-15 stsp static const struct got_error *
2256 3ce1b845 2019-07-15 stsp import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2257 3ce1b845 2019-07-15 stsp const char *path, struct got_pathlist_head *ignores,
2258 3ce1b845 2019-07-15 stsp struct got_repository *repo,
2259 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
2261 3ce1b845 2019-07-15 stsp const struct got_error *err;
2262 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
2263 3ce1b845 2019-07-15 stsp char *subdirpath;
2265 3ce1b845 2019-07-15 stsp if (asprintf(&subdirpath, "%s%s%s", path,
2266 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
2267 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
2269 3ce1b845 2019-07-15 stsp (*new_te) = calloc(1, sizeof(**new_te));
2270 d6fca0ba 2019-09-15 hiltjo if (*new_te == NULL)
2271 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
2272 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFDIR;
2273 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2274 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
2275 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
2276 3ce1b845 2019-07-15 stsp goto done;
2278 56e0773d 2019-11-28 stsp err = write_tree(&id, subdirpath, ignores, repo,
2279 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
2281 56e0773d 2019-11-28 stsp goto done;
2282 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2286 3ce1b845 2019-07-15 stsp free(subdirpath);
2287 3ce1b845 2019-07-15 stsp if (err) {
2288 56e0773d 2019-11-28 stsp free(*new_te);
2289 3ce1b845 2019-07-15 stsp *new_te = NULL;
2291 3ce1b845 2019-07-15 stsp return err;
2294 3ce1b845 2019-07-15 stsp static const struct got_error *
2295 3ce1b845 2019-07-15 stsp write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2296 3ce1b845 2019-07-15 stsp struct got_pathlist_head *ignores, struct got_repository *repo,
2297 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
2299 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2301 3ce1b845 2019-07-15 stsp struct dirent *de;
2302 56e0773d 2019-11-28 stsp int nentries;
2303 3ce1b845 2019-07-15 stsp struct got_tree_entry *new_te = NULL;
2304 3ce1b845 2019-07-15 stsp struct got_pathlist_head paths;
2305 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
2307 3ce1b845 2019-07-15 stsp *new_tree_id = NULL;
2309 3ce1b845 2019-07-15 stsp TAILQ_INIT(&paths);
2311 3ce1b845 2019-07-15 stsp dir = opendir(path_dir);
2312 3ce1b845 2019-07-15 stsp if (dir == NULL) {
2313 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("opendir", path_dir);
2314 3ce1b845 2019-07-15 stsp goto done;
2317 56e0773d 2019-11-28 stsp nentries = 0;
2318 3ce1b845 2019-07-15 stsp while ((de = readdir(dir)) != NULL) {
2319 3ce1b845 2019-07-15 stsp int ignore = 0;
2322 3ce1b845 2019-07-15 stsp if (strcmp(de->d_name, ".") == 0 ||
2323 3ce1b845 2019-07-15 stsp strcmp(de->d_name, "..") == 0)
2326 e012cf94 2023-02-07 thomas err = got_path_dirent_type(&type, path_dir, de);
2327 e012cf94 2023-02-07 thomas if (err)
2328 e012cf94 2023-02-07 thomas goto done;
2330 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, ignores, entry) {
2331 e012cf94 2023-02-07 thomas if (type == DT_DIR && pe->path_len > 0 &&
2332 e012cf94 2023-02-07 thomas pe->path[pe->path_len - 1] == '/') {
2333 e012cf94 2023-02-07 thomas char stripped[PATH_MAX];
2335 e012cf94 2023-02-07 thomas if (strlcpy(stripped, pe->path,
2336 e012cf94 2023-02-07 thomas sizeof(stripped)) >= sizeof(stripped)) {
2337 e012cf94 2023-02-07 thomas err = got_error(GOT_ERR_NO_SPACE);
2338 e012cf94 2023-02-07 thomas goto done;
2340 e012cf94 2023-02-07 thomas got_path_strip_trailing_slashes(stripped);
2341 e012cf94 2023-02-07 thomas if (fnmatch(stripped, de->d_name, 0) == 0) {
2342 e012cf94 2023-02-07 thomas ignore = 1;
2345 e012cf94 2023-02-07 thomas } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2346 3ce1b845 2019-07-15 stsp ignore = 1;
2350 3ce1b845 2019-07-15 stsp if (ignore)
2353 20ccae39 2020-07-21 stsp if (type == DT_DIR) {
2354 3ce1b845 2019-07-15 stsp err = import_subdir(&new_te, de, path_dir,
2355 3ce1b845 2019-07-15 stsp ignores, repo, progress_cb, progress_arg);
2356 db1d3576 2019-10-04 stsp if (err) {
2357 db1d3576 2019-10-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
2358 db1d3576 2019-10-04 stsp goto done;
2359 db1d3576 2019-10-04 stsp err = NULL;
2362 e8863bdc 2020-07-23 stsp } else if (type == DT_REG || type == DT_LNK) {
2363 3ce1b845 2019-07-15 stsp err = import_file(&new_te, de, path_dir, repo);
2365 3ce1b845 2019-07-15 stsp goto done;
2369 3ce1b845 2019-07-15 stsp err = insert_tree_entry(new_te, &paths);
2371 3ce1b845 2019-07-15 stsp goto done;
2372 56e0773d 2019-11-28 stsp nentries++;
2375 db1d3576 2019-10-04 stsp if (TAILQ_EMPTY(&paths)) {
2376 b66cd6f3 2020-07-31 stsp err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2377 b66cd6f3 2020-07-31 stsp "cannot create tree without any entries");
2378 db1d3576 2019-10-04 stsp goto done;
2381 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, &paths, entry) {
2382 3ce1b845 2019-07-15 stsp struct got_tree_entry *te = pe->data;
2383 3ce1b845 2019-07-15 stsp char *path;
2384 e8863bdc 2020-07-23 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2386 3ce1b845 2019-07-15 stsp if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2387 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
2388 3ce1b845 2019-07-15 stsp goto done;
2390 3ce1b845 2019-07-15 stsp err = (*progress_cb)(progress_arg, path);
2391 3ce1b845 2019-07-15 stsp free(path);
2393 3ce1b845 2019-07-15 stsp goto done;
2396 56e0773d 2019-11-28 stsp err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2399 3ce1b845 2019-07-15 stsp closedir(dir);
2400 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2401 3ce1b845 2019-07-15 stsp return err;
2404 3ce1b845 2019-07-15 stsp const struct got_error *
2405 3ce1b845 2019-07-15 stsp got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2406 3ce1b845 2019-07-15 stsp const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2407 3ce1b845 2019-07-15 stsp struct got_repository *repo, got_repo_import_cb progress_cb,
2408 3ce1b845 2019-07-15 stsp void *progress_arg)
2410 3ce1b845 2019-07-15 stsp const struct got_error *err;
2411 3ce1b845 2019-07-15 stsp struct got_object_id *new_tree_id;
2413 3ce1b845 2019-07-15 stsp err = write_tree(&new_tree_id, path_dir, ignores, repo,
2414 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
2416 3ce1b845 2019-07-15 stsp return err;
2418 3ce1b845 2019-07-15 stsp err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2419 3ce1b845 2019-07-15 stsp author, time(NULL), author, time(NULL), logmsg, repo);
2420 3ce1b845 2019-07-15 stsp free(new_tree_id);
2421 20662ea0 2021-04-10 stsp return err;
2424 20662ea0 2021-04-10 stsp const struct got_error *
2425 20662ea0 2021-04-10 stsp got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2426 20662ea0 2021-04-10 stsp struct got_repository *repo)
2428 20662ea0 2021-04-10 stsp const struct got_error *err = NULL;
2429 20662ea0 2021-04-10 stsp char *path_objects = NULL, *path = NULL;
2430 20662ea0 2021-04-10 stsp DIR *dir = NULL;
2431 20662ea0 2021-04-10 stsp struct got_object_id id;
2434 20662ea0 2021-04-10 stsp *nobjects = 0;
2435 20662ea0 2021-04-10 stsp *ondisk_size = 0;
2437 20662ea0 2021-04-10 stsp path_objects = got_repo_get_path_objects(repo);
2438 20662ea0 2021-04-10 stsp if (path_objects == NULL)
2439 20662ea0 2021-04-10 stsp return got_error_from_errno("got_repo_get_path_objects");
2441 20662ea0 2021-04-10 stsp for (i = 0; i <= 0xff; i++) {
2442 20662ea0 2021-04-10 stsp struct dirent *dent;
2444 20662ea0 2021-04-10 stsp if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2445 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2449 20662ea0 2021-04-10 stsp dir = opendir(path);
2450 20662ea0 2021-04-10 stsp if (dir == NULL) {
2451 20662ea0 2021-04-10 stsp if (errno == ENOENT) {
2452 20662ea0 2021-04-10 stsp err = NULL;
2455 20662ea0 2021-04-10 stsp err = got_error_from_errno2("opendir", path);
2459 20662ea0 2021-04-10 stsp while ((dent = readdir(dir)) != NULL) {
2460 20662ea0 2021-04-10 stsp char *id_str;
2462 20662ea0 2021-04-10 stsp struct stat sb;
2464 20662ea0 2021-04-10 stsp if (strcmp(dent->d_name, ".") == 0 ||
2465 20662ea0 2021-04-10 stsp strcmp(dent->d_name, "..") == 0)
2468 20662ea0 2021-04-10 stsp if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2469 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2470 20662ea0 2021-04-10 stsp goto done;
2473 f4204d57 2023-03-01 thomas if (!got_parse_object_id(&id, id_str, repo->algo)) {
2474 20662ea0 2021-04-10 stsp free(id_str);
2477 20662ea0 2021-04-10 stsp free(id_str);
2479 20662ea0 2021-04-10 stsp err = got_object_open_loose_fd(&fd, &id, repo);
2481 20662ea0 2021-04-10 stsp goto done;
2483 20662ea0 2021-04-10 stsp if (fstat(fd, &sb) == -1) {
2484 20662ea0 2021-04-10 stsp err = got_error_from_errno("fstat");
2485 20662ea0 2021-04-10 stsp close(fd);
2486 20662ea0 2021-04-10 stsp goto done;
2488 20662ea0 2021-04-10 stsp (*nobjects)++;
2489 20662ea0 2021-04-10 stsp (*ondisk_size) += sb.st_size;
2491 20662ea0 2021-04-10 stsp if (close(fd) == -1) {
2492 20662ea0 2021-04-10 stsp err = got_error_from_errno("close");
2493 20662ea0 2021-04-10 stsp goto done;
2497 20662ea0 2021-04-10 stsp if (closedir(dir) != 0) {
2498 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2499 20662ea0 2021-04-10 stsp goto done;
2501 20662ea0 2021-04-10 stsp dir = NULL;
2503 20662ea0 2021-04-10 stsp free(path);
2504 20662ea0 2021-04-10 stsp path = NULL;
2507 20662ea0 2021-04-10 stsp if (dir && closedir(dir) != 0 && err == NULL)
2508 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2510 20662ea0 2021-04-10 stsp if (err) {
2511 20662ea0 2021-04-10 stsp *nobjects = 0;
2512 20662ea0 2021-04-10 stsp *ondisk_size = 0;
2514 20662ea0 2021-04-10 stsp free(path_objects);
2515 20662ea0 2021-04-10 stsp free(path);
2516 3ce1b845 2019-07-15 stsp return err;
2519 20662ea0 2021-04-10 stsp const struct got_error *
2520 20662ea0 2021-04-10 stsp got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2521 20662ea0 2021-04-10 stsp off_t *total_packsize, struct got_repository *repo)
2523 0c9eeee2 2021-06-05 stsp const struct got_error *err = NULL;
2524 20662ea0 2021-04-10 stsp DIR *packdir = NULL;
2525 20662ea0 2021-04-10 stsp struct dirent *dent;
2526 20662ea0 2021-04-10 stsp struct got_packidx *packidx = NULL;
2527 20662ea0 2021-04-10 stsp char *path_packidx;
2528 20662ea0 2021-04-10 stsp char *path_packfile;
2529 20662ea0 2021-04-10 stsp int packdir_fd;
2530 20662ea0 2021-04-10 stsp struct stat sb;
2532 20662ea0 2021-04-10 stsp *npackfiles = 0;
2533 20662ea0 2021-04-10 stsp *nobjects = 0;
2534 20662ea0 2021-04-10 stsp *total_packsize = 0;
2536 20662ea0 2021-04-10 stsp packdir_fd = openat(got_repo_get_fd(repo),
2537 20662ea0 2021-04-10 stsp GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2538 20662ea0 2021-04-10 stsp if (packdir_fd == -1) {
2539 20662ea0 2021-04-10 stsp return got_error_from_errno_fmt("openat: %s/%s",
2540 20662ea0 2021-04-10 stsp got_repo_get_path_git_dir(repo),
2541 20662ea0 2021-04-10 stsp GOT_OBJECTS_PACK_DIR);
2544 20662ea0 2021-04-10 stsp packdir = fdopendir(packdir_fd);
2545 20662ea0 2021-04-10 stsp if (packdir == NULL) {
2546 20662ea0 2021-04-10 stsp err = got_error_from_errno("fdopendir");
2547 815470d4 2024-01-31 thomas close(packdir_fd);
2548 20662ea0 2021-04-10 stsp goto done;
2551 20662ea0 2021-04-10 stsp while ((dent = readdir(packdir)) != NULL) {
2552 dd038bc6 2021-09-21 thomas.ad if (!got_repo_is_packidx_filename(dent->d_name,
2553 dd038bc6 2021-09-21 thomas.ad strlen(dent->d_name)))
2556 20662ea0 2021-04-10 stsp if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2557 20662ea0 2021-04-10 stsp dent->d_name) == -1) {
2558 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2559 20662ea0 2021-04-10 stsp goto done;
2562 20662ea0 2021-04-10 stsp err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2563 20662ea0 2021-04-10 stsp path_packidx, 0);
2564 20662ea0 2021-04-10 stsp free(path_packidx);
2566 20662ea0 2021-04-10 stsp goto done;
2568 20662ea0 2021-04-10 stsp if (fstat(packidx->fd, &sb) == -1)
2569 20662ea0 2021-04-10 stsp goto done;
2570 20662ea0 2021-04-10 stsp *total_packsize += sb.st_size;
2572 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2573 aea75d87 2021-07-06 stsp packidx->path_packidx);
2575 20662ea0 2021-04-10 stsp goto done;
2577 20662ea0 2021-04-10 stsp if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2578 20662ea0 2021-04-10 stsp 0) == -1) {
2579 20662ea0 2021-04-10 stsp free(path_packfile);
2580 20662ea0 2021-04-10 stsp goto done;
2582 20662ea0 2021-04-10 stsp free(path_packfile);
2583 20662ea0 2021-04-10 stsp *total_packsize += sb.st_size;
2585 20662ea0 2021-04-10 stsp *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2587 20662ea0 2021-04-10 stsp (*npackfiles)++;
2589 20662ea0 2021-04-10 stsp got_packidx_close(packidx);
2590 20662ea0 2021-04-10 stsp packidx = NULL;
2593 20662ea0 2021-04-10 stsp if (packidx)
2594 20662ea0 2021-04-10 stsp got_packidx_close(packidx);
2595 20662ea0 2021-04-10 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
2596 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2597 20662ea0 2021-04-10 stsp if (err) {
2598 20662ea0 2021-04-10 stsp *npackfiles = 0;
2599 20662ea0 2021-04-10 stsp *nobjects = 0;
2600 20662ea0 2021-04-10 stsp *total_packsize = 0;
2602 20662ea0 2021-04-10 stsp return err;
2605 0be8fa4c 2021-10-15 thomas RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2606 0be8fa4c 2021-10-15 thomas got_packidx_bloom_filter_cmp);