Blame


1 7b19e0f1 2017-11-05 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
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.
7 7b19e0f1 2017-11-05 stsp *
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.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 ad242220 2018-09-08 stsp #include <sys/types.h>
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 ad242220 2018-09-08 stsp #include <sys/uio.h>
20 aba9c984 2019-09-08 stsp #include <sys/socket.h>
21 deeca238 2018-03-12 stsp #include <sys/stat.h>
22 1510f469 2018-09-09 stsp #include <sys/mman.h>
23 6c414261 2021-03-30 stsp #include <sys/resource.h>
24 79b11c62 2018-03-09 stsp
25 e09a504c 2019-06-28 stsp #include <ctype.h>
26 1510f469 2018-09-09 stsp #include <fcntl.h>
27 3ce1b845 2019-07-15 stsp #include <fnmatch.h>
28 4027f31a 2017-11-04 stsp #include <limits.h>
29 1510f469 2018-09-09 stsp #include <dirent.h>
30 4027f31a 2017-11-04 stsp #include <stdlib.h>
31 4027f31a 2017-11-04 stsp #include <stdio.h>
32 4027f31a 2017-11-04 stsp #include <string.h>
33 303e14b5 2019-09-23 stsp #include <time.h>
34 81a12da5 2020-09-09 naddy #include <unistd.h>
35 79b11c62 2018-03-09 stsp #include <zlib.h>
36 85f51bba 2018-07-16 stsp #include <errno.h>
37 85f51bba 2018-07-16 stsp #include <libgen.h>
38 ad242220 2018-09-08 stsp #include <stdint.h>
39 4027f31a 2017-11-04 stsp
40 4545b700 2021-10-15 thomas #include "bloom.h"
41 dd038bc6 2021-09-21 thomas.ad
42 4027f31a 2017-11-04 stsp #include "got_error.h"
43 5261c201 2018-04-01 stsp #include "got_reference.h"
44 4027f31a 2017-11-04 stsp #include "got_repository.h"
45 1dd54920 2019-05-11 stsp #include "got_path.h"
46 e6209546 2019-08-22 stsp #include "got_cancel.h"
47 7bb0daa1 2018-06-21 stsp #include "got_object.h"
48 bfb5ee0b 2022-05-31 thomas #include "got_opentemp.h"
49 4027f31a 2017-11-04 stsp
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 3efd8e31 2022-10-23 thomas #include "got_lib_delta_cache.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 3ce1b845 2019-07-15 stsp #include "got_lib_object_parse.h"
55 3ce1b845 2019-07-15 stsp #include "got_lib_object_create.h"
56 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
57 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
58 e09a504c 2019-06-28 stsp #include "got_lib_sha1.h"
59 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
60 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
61 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
62 c3f94f68 2017-11-05 stsp
63 79b11c62 2018-03-09 stsp #ifndef nitems
64 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 79b11c62 2018-03-09 stsp #endif
66 0be8fa4c 2021-10-15 thomas
67 7cd52833 2022-06-23 thomas #define GOT_PACK_NUM_TEMPFILES GOT_PACK_CACHE_SIZE * 2
68 7cd52833 2022-06-23 thomas
69 0be8fa4c 2021-10-15 thomas RB_PROTOTYPE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
70 0be8fa4c 2021-10-15 thomas got_packidx_bloom_filter_cmp);
71 b091c2cd 2023-02-07 thomas
72 b091c2cd 2023-02-07 thomas static inline int
73 b091c2cd 2023-02-07 thomas is_boolean_val(const char *val)
74 b091c2cd 2023-02-07 thomas {
75 b091c2cd 2023-02-07 thomas return (strcasecmp(val, "true") == 0 ||
76 b091c2cd 2023-02-07 thomas strcasecmp(val, "false") == 0 ||
77 b091c2cd 2023-02-07 thomas strcasecmp(val, "on") == 0 ||
78 b091c2cd 2023-02-07 thomas strcasecmp(val, "off") == 0 ||
79 b091c2cd 2023-02-07 thomas strcasecmp(val, "yes") == 0 ||
80 b091c2cd 2023-02-07 thomas strcasecmp(val, "no") == 0 ||
81 b091c2cd 2023-02-07 thomas strcasecmp(val, "1") == 0 ||
82 b091c2cd 2023-02-07 thomas strcasecmp(val, "0") == 0);
83 b091c2cd 2023-02-07 thomas }
84 b091c2cd 2023-02-07 thomas
85 b091c2cd 2023-02-07 thomas static inline int
86 b091c2cd 2023-02-07 thomas get_boolean_val(const char *val)
87 b091c2cd 2023-02-07 thomas {
88 b091c2cd 2023-02-07 thomas return (strcasecmp(val, "true") == 0 ||
89 b091c2cd 2023-02-07 thomas strcasecmp(val, "on") == 0 ||
90 b091c2cd 2023-02-07 thomas strcasecmp(val, "yes") == 0 ||
91 b091c2cd 2023-02-07 thomas strcasecmp(val, "1") == 0);
92 b091c2cd 2023-02-07 thomas }
93 4df642d9 2017-11-05 stsp
94 7839bc15 2019-01-06 stsp const char *
95 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
96 86c3caaf 2018-03-09 stsp {
97 7839bc15 2019-01-06 stsp return repo->path;
98 86c3caaf 2018-03-09 stsp }
99 86c3caaf 2018-03-09 stsp
100 6e9da951 2019-01-06 stsp const char *
101 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
102 4027f31a 2017-11-04 stsp {
103 6e9da951 2019-01-06 stsp return repo->path_git_dir;
104 6d5a9006 2020-12-16 yzhong }
105 6d5a9006 2020-12-16 yzhong
106 6d5a9006 2020-12-16 yzhong int
107 6d5a9006 2020-12-16 yzhong got_repo_get_fd(struct got_repository *repo)
108 6d5a9006 2020-12-16 yzhong {
109 6d5a9006 2020-12-16 yzhong return repo->gitdir_fd;
110 04ca23f4 2018-07-16 stsp }
111 04ca23f4 2018-07-16 stsp
112 aba9c984 2019-09-08 stsp const char *
113 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_name(struct got_repository *repo)
114 aba9c984 2019-09-08 stsp {
115 aba9c984 2019-09-08 stsp return repo->gitconfig_author_name;
116 aba9c984 2019-09-08 stsp }
117 aba9c984 2019-09-08 stsp
118 aba9c984 2019-09-08 stsp const char *
119 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_email(struct got_repository *repo)
120 aba9c984 2019-09-08 stsp {
121 aba9c984 2019-09-08 stsp return repo->gitconfig_author_email;
122 c9956ddf 2019-09-08 stsp }
123 c9956ddf 2019-09-08 stsp
124 c9956ddf 2019-09-08 stsp const char *
125 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
126 c9956ddf 2019-09-08 stsp {
127 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_name;
128 c9956ddf 2019-09-08 stsp }
129 c9956ddf 2019-09-08 stsp
130 c9956ddf 2019-09-08 stsp const char *
131 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
132 c9956ddf 2019-09-08 stsp {
133 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_email;
134 9a1cc63f 2020-02-03 stsp }
135 9a1cc63f 2020-02-03 stsp
136 9a1cc63f 2020-02-03 stsp const char *
137 9a1cc63f 2020-02-03 stsp got_repo_get_gitconfig_owner(struct got_repository *repo)
138 9a1cc63f 2020-02-03 stsp {
139 9a1cc63f 2020-02-03 stsp return repo->gitconfig_owner;
140 9188bd78 2021-07-03 stsp }
141 9188bd78 2021-07-03 stsp
142 2624d165 2023-02-07 thomas int
143 2624d165 2023-02-07 thomas got_repo_has_extension(struct got_repository *repo, const char *ext)
144 9188bd78 2021-07-03 stsp {
145 2624d165 2023-02-07 thomas int i;
146 2624d165 2023-02-07 thomas
147 2624d165 2023-02-07 thomas for (i = 0; i < repo->nextensions; ++i) {
148 b091c2cd 2023-02-07 thomas if (!strcasecmp(ext, repo->extnames[i]))
149 b091c2cd 2023-02-07 thomas return get_boolean_val(repo->extvals[i]);
150 2624d165 2023-02-07 thomas }
151 2624d165 2023-02-07 thomas
152 2624d165 2023-02-07 thomas return 0;
153 aba9c984 2019-09-08 stsp }
154 aba9c984 2019-09-08 stsp
155 04ca23f4 2018-07-16 stsp int
156 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
157 04ca23f4 2018-07-16 stsp {
158 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
159 4027f31a 2017-11-04 stsp }
160 4027f31a 2017-11-04 stsp
161 4027f31a 2017-11-04 stsp static char *
162 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
163 4027f31a 2017-11-04 stsp {
164 4027f31a 2017-11-04 stsp char *path_child;
165 3168e5da 2020-09-10 stsp
166 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
167 4027f31a 2017-11-04 stsp basename) == -1)
168 4027f31a 2017-11-04 stsp return NULL;
169 4027f31a 2017-11-04 stsp
170 4027f31a 2017-11-04 stsp return path_child;
171 4027f31a 2017-11-04 stsp }
172 4027f31a 2017-11-04 stsp
173 11995603 2017-11-05 stsp char *
174 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
175 4027f31a 2017-11-04 stsp {
176 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
177 4027f31a 2017-11-04 stsp }
178 4027f31a 2017-11-04 stsp
179 11995603 2017-11-05 stsp char *
180 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
181 a1fd68d8 2018-01-12 stsp {
182 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
183 a1fd68d8 2018-01-12 stsp }
184 a1fd68d8 2018-01-12 stsp
185 a1fd68d8 2018-01-12 stsp char *
186 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
187 4027f31a 2017-11-04 stsp {
188 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
189 4027f31a 2017-11-04 stsp }
190 4027f31a 2017-11-04 stsp
191 fb79db15 2019-02-01 stsp char *
192 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
193 fb79db15 2019-02-01 stsp {
194 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
195 fb79db15 2019-02-01 stsp }
196 fb79db15 2019-02-01 stsp
197 4027f31a 2017-11-04 stsp static char *
198 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
199 4027f31a 2017-11-04 stsp {
200 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
201 1d126e2d 2019-08-24 stsp }
202 1d126e2d 2019-08-24 stsp
203 b46f3e71 2020-03-18 stsp char *
204 b46f3e71 2020-03-18 stsp got_repo_get_path_gitconfig(struct got_repository *repo)
205 1d126e2d 2019-08-24 stsp {
206 b46f3e71 2020-03-18 stsp return get_path_git_child(repo, GOT_GITCONFIG);
207 cd95becd 2019-11-29 stsp }
208 cd95becd 2019-11-29 stsp
209 257add31 2020-09-09 stsp char *
210 257add31 2020-09-09 stsp got_repo_get_path_gotconfig(struct got_repository *repo)
211 257add31 2020-09-09 stsp {
212 50b0790e 2020-09-11 stsp return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
213 257add31 2020-09-09 stsp }
214 257add31 2020-09-09 stsp
215 50b0790e 2020-09-11 stsp const struct got_gotconfig *
216 50b0790e 2020-09-11 stsp got_repo_get_gotconfig(struct got_repository *repo)
217 cd95becd 2019-11-29 stsp {
218 50b0790e 2020-09-11 stsp return repo->gotconfig;
219 4027f31a 2017-11-04 stsp }
220 4027f31a 2017-11-04 stsp
221 257add31 2020-09-09 stsp void
222 50b0790e 2020-09-11 stsp got_repo_get_gitconfig_remotes(int *nremotes,
223 50b0790e 2020-09-11 stsp const struct got_remote_repo **remotes, struct got_repository *repo)
224 257add31 2020-09-09 stsp {
225 50b0790e 2020-09-11 stsp *nremotes = repo->ngitconfig_remotes;
226 50b0790e 2020-09-11 stsp *remotes = repo->gitconfig_remotes;
227 257add31 2020-09-09 stsp }
228 257add31 2020-09-09 stsp
229 4027f31a 2017-11-04 stsp static int
230 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
231 4027f31a 2017-11-04 stsp {
232 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
233 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
234 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
235 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
236 deeca238 2018-03-12 stsp int ret = 0;
237 deeca238 2018-03-12 stsp struct stat sb;
238 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
239 4027f31a 2017-11-04 stsp
240 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
241 deeca238 2018-03-12 stsp goto done;
242 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
243 deeca238 2018-03-12 stsp goto done;
244 4027f31a 2017-11-04 stsp
245 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
246 deeca238 2018-03-12 stsp goto done;
247 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
248 deeca238 2018-03-12 stsp goto done;
249 deeca238 2018-03-12 stsp
250 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
251 deeca238 2018-03-12 stsp goto done;
252 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
253 deeca238 2018-03-12 stsp goto done;
254 deeca238 2018-03-12 stsp
255 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
256 deeca238 2018-03-12 stsp goto done;
257 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
258 deeca238 2018-03-12 stsp goto done;
259 4847cca1 2018-03-12 stsp
260 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
261 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
262 4847cca1 2018-03-12 stsp goto done;
263 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
264 4847cca1 2018-03-12 stsp
265 deeca238 2018-03-12 stsp ret = 1;
266 deeca238 2018-03-12 stsp done:
267 4027f31a 2017-11-04 stsp free(path_objects);
268 4027f31a 2017-11-04 stsp free(path_refs);
269 4027f31a 2017-11-04 stsp free(path_head);
270 4027f31a 2017-11-04 stsp return ret;
271 4027f31a 2017-11-04 stsp
272 7bb0daa1 2018-06-21 stsp }
273 7bb0daa1 2018-06-21 stsp
274 3efd8e31 2022-10-23 thomas static const struct got_error *
275 3efd8e31 2022-10-23 thomas close_tempfiles(int *fds, size_t nfds)
276 7cd52833 2022-06-23 thomas {
277 7cd52833 2022-06-23 thomas const struct got_error *err = NULL;
278 a6fd182a 2022-09-03 thomas int i;
279 7cd52833 2022-06-23 thomas
280 3efd8e31 2022-10-23 thomas for (i = 0; i < nfds; i++) {
281 3efd8e31 2022-10-23 thomas if (fds[i] == -1)
282 3efd8e31 2022-10-23 thomas continue;
283 3efd8e31 2022-10-23 thomas if (close(fds[i]) == -1) {
284 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
285 3efd8e31 2022-10-23 thomas break;
286 3efd8e31 2022-10-23 thomas }
287 3efd8e31 2022-10-23 thomas }
288 3efd8e31 2022-10-23 thomas free(fds);
289 3efd8e31 2022-10-23 thomas return err;
290 3efd8e31 2022-10-23 thomas }
291 7cd52833 2022-06-23 thomas
292 3efd8e31 2022-10-23 thomas static const struct got_error *
293 e0220e74 2022-10-31 thomas open_tempfiles(int **fds, size_t array_size, size_t nfds)
294 3efd8e31 2022-10-23 thomas {
295 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
296 3efd8e31 2022-10-23 thomas int i;
297 3efd8e31 2022-10-23 thomas
298 e0220e74 2022-10-31 thomas *fds = calloc(array_size, sizeof(**fds));
299 3efd8e31 2022-10-23 thomas if (*fds == NULL)
300 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
301 3efd8e31 2022-10-23 thomas
302 e0220e74 2022-10-31 thomas for (i = 0; i < array_size; i++)
303 3efd8e31 2022-10-23 thomas (*fds)[i] = -1;
304 3efd8e31 2022-10-23 thomas
305 3efd8e31 2022-10-23 thomas for (i = 0; i < nfds; i++) {
306 3efd8e31 2022-10-23 thomas (*fds)[i] = got_opentempfd();
307 3efd8e31 2022-10-23 thomas if ((*fds)[i] == -1) {
308 7648b8ae 2022-06-23 thomas err = got_error_from_errno("got_opentempfd");
309 3efd8e31 2022-10-23 thomas close_tempfiles(*fds, nfds);
310 3efd8e31 2022-10-23 thomas *fds = NULL;
311 7648b8ae 2022-06-23 thomas return err;
312 7cd52833 2022-06-23 thomas }
313 7cd52833 2022-06-23 thomas }
314 a6fd182a 2022-09-03 thomas
315 a6fd182a 2022-09-03 thomas return NULL;
316 7cd52833 2022-06-23 thomas }
317 7cd52833 2022-06-23 thomas
318 e0220e74 2022-10-31 thomas static const struct got_error *
319 e0220e74 2022-10-31 thomas get_pack_cache_size(int *pack_cache_size)
320 e0220e74 2022-10-31 thomas {
321 e0220e74 2022-10-31 thomas struct rlimit rl;
322 e0220e74 2022-10-31 thomas
323 e0220e74 2022-10-31 thomas if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
324 e0220e74 2022-10-31 thomas return got_error_from_errno("getrlimit");
325 e0220e74 2022-10-31 thomas
326 e0220e74 2022-10-31 thomas *pack_cache_size = GOT_PACK_CACHE_SIZE;
327 e0220e74 2022-10-31 thomas if (*pack_cache_size > rl.rlim_cur / 8)
328 e0220e74 2022-10-31 thomas *pack_cache_size = rl.rlim_cur / 8;
329 e0220e74 2022-10-31 thomas
330 e0220e74 2022-10-31 thomas return NULL;
331 e0220e74 2022-10-31 thomas }
332 e0220e74 2022-10-31 thomas
333 7cd52833 2022-06-23 thomas const struct got_error *
334 3efd8e31 2022-10-23 thomas got_repo_pack_fds_open(int **pack_fds)
335 3efd8e31 2022-10-23 thomas {
336 e0220e74 2022-10-31 thomas const struct got_error *err;
337 e0220e74 2022-10-31 thomas int nfds;
338 e0220e74 2022-10-31 thomas
339 e0220e74 2022-10-31 thomas err = get_pack_cache_size(&nfds);
340 e0220e74 2022-10-31 thomas if (err)
341 e0220e74 2022-10-31 thomas return err;
342 e0220e74 2022-10-31 thomas
343 e0220e74 2022-10-31 thomas /*
344 e0220e74 2022-10-31 thomas * We need one basefd and one accumfd per cached pack.
345 e0220e74 2022-10-31 thomas * Our constants should be set up in a way such that
346 e0220e74 2022-10-31 thomas * this error never triggers.
347 e0220e74 2022-10-31 thomas */
348 e0220e74 2022-10-31 thomas if (nfds * 2 > GOT_PACK_NUM_TEMPFILES)
349 e0220e74 2022-10-31 thomas return got_error(GOT_ERR_NO_SPACE);
350 e0220e74 2022-10-31 thomas
351 e0220e74 2022-10-31 thomas return open_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES, nfds * 2);
352 3efd8e31 2022-10-23 thomas }
353 3efd8e31 2022-10-23 thomas
354 3efd8e31 2022-10-23 thomas const struct got_error *
355 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(int *pack_fds)
356 7cd52833 2022-06-23 thomas {
357 3efd8e31 2022-10-23 thomas return close_tempfiles(pack_fds, GOT_PACK_NUM_TEMPFILES);
358 3efd8e31 2022-10-23 thomas }
359 7cd52833 2022-06-23 thomas
360 3efd8e31 2022-10-23 thomas const struct got_error *
361 946e0798 2022-11-06 thomas got_repo_temp_fds_open(int **temp_fds)
362 3efd8e31 2022-10-23 thomas {
363 e0220e74 2022-10-31 thomas return open_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES,
364 e0220e74 2022-10-31 thomas GOT_REPO_NUM_TEMPFILES);
365 3efd8e31 2022-10-23 thomas }
366 3efd8e31 2022-10-23 thomas
367 3efd8e31 2022-10-23 thomas void
368 946e0798 2022-11-06 thomas got_repo_temp_fds_set(struct got_repository *repo, int *temp_fds)
369 3efd8e31 2022-10-23 thomas {
370 3efd8e31 2022-10-23 thomas int i;
371 3efd8e31 2022-10-23 thomas
372 3efd8e31 2022-10-23 thomas for (i = 0; i < GOT_REPO_NUM_TEMPFILES; i++)
373 3efd8e31 2022-10-23 thomas repo->tempfiles[i] = temp_fds[i];
374 3efd8e31 2022-10-23 thomas }
375 3efd8e31 2022-10-23 thomas
376 3efd8e31 2022-10-23 thomas const struct got_error *
377 3efd8e31 2022-10-23 thomas got_repo_temp_fds_get(int *fd, int *idx, struct got_repository *repo)
378 3efd8e31 2022-10-23 thomas {
379 3efd8e31 2022-10-23 thomas int i;
380 3efd8e31 2022-10-23 thomas
381 3efd8e31 2022-10-23 thomas *fd = -1;
382 3efd8e31 2022-10-23 thomas *idx = -1;
383 3efd8e31 2022-10-23 thomas
384 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo->tempfiles); i++) {
385 3efd8e31 2022-10-23 thomas if (repo->tempfile_use_mask & (1 << i))
386 7cd52833 2022-06-23 thomas continue;
387 3efd8e31 2022-10-23 thomas if (repo->tempfiles[i] != -1) {
388 3efd8e31 2022-10-23 thomas if (ftruncate(repo->tempfiles[i], 0L) == -1)
389 3efd8e31 2022-10-23 thomas return got_error_from_errno("ftruncate");
390 3efd8e31 2022-10-23 thomas *fd = repo->tempfiles[i];
391 3efd8e31 2022-10-23 thomas *idx = i;
392 3efd8e31 2022-10-23 thomas repo->tempfile_use_mask |= (1 << i);
393 3efd8e31 2022-10-23 thomas return NULL;
394 7cd52833 2022-06-23 thomas }
395 7cd52833 2022-06-23 thomas }
396 3efd8e31 2022-10-23 thomas
397 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_REPO_TEMPFILE);
398 3efd8e31 2022-10-23 thomas }
399 3efd8e31 2022-10-23 thomas
400 3efd8e31 2022-10-23 thomas void
401 3efd8e31 2022-10-23 thomas got_repo_temp_fds_put(int idx, struct got_repository *repo)
402 3efd8e31 2022-10-23 thomas {
403 3efd8e31 2022-10-23 thomas repo->tempfile_use_mask &= ~(1 << idx);
404 3efd8e31 2022-10-23 thomas }
405 3efd8e31 2022-10-23 thomas
406 3efd8e31 2022-10-23 thomas const struct got_error *
407 3efd8e31 2022-10-23 thomas got_repo_temp_fds_close(int *temp_fds)
408 3efd8e31 2022-10-23 thomas {
409 3efd8e31 2022-10-23 thomas return close_tempfiles(temp_fds, GOT_REPO_NUM_TEMPFILES);
410 7cd52833 2022-06-23 thomas }
411 7cd52833 2022-06-23 thomas
412 7cd52833 2022-06-23 thomas const struct got_error *
413 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
414 f6be5c30 2018-06-22 stsp struct got_object *obj)
415 f6be5c30 2018-06-22 stsp {
416 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
417 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
418 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
419 79c99a64 2019-05-23 stsp if (err) {
420 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
421 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
422 79c99a64 2019-05-23 stsp err = NULL;
423 f6be5c30 2018-06-22 stsp return err;
424 79c99a64 2019-05-23 stsp }
425 f6be5c30 2018-06-22 stsp obj->refcnt++;
426 ccfe88e6 2018-07-12 stsp #endif
427 f6be5c30 2018-06-22 stsp return NULL;
428 f6be5c30 2018-06-22 stsp }
429 f6be5c30 2018-06-22 stsp
430 7bb0daa1 2018-06-21 stsp struct got_object *
431 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
432 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
433 7bb0daa1 2018-06-21 stsp {
434 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
435 7bb0daa1 2018-06-21 stsp }
436 7bb0daa1 2018-06-21 stsp
437 4027f31a 2017-11-04 stsp const struct got_error *
438 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
439 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
440 f6be5c30 2018-06-22 stsp {
441 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
442 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
443 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
444 79c99a64 2019-05-23 stsp if (err) {
445 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
446 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
447 79c99a64 2019-05-23 stsp err = NULL;
448 f6be5c30 2018-06-22 stsp return err;
449 79c99a64 2019-05-23 stsp }
450 f6be5c30 2018-06-22 stsp tree->refcnt++;
451 ccfe88e6 2018-07-12 stsp #endif
452 f6be5c30 2018-06-22 stsp return NULL;
453 f6be5c30 2018-06-22 stsp }
454 f6be5c30 2018-06-22 stsp
455 f6be5c30 2018-06-22 stsp struct got_tree_object *
456 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
457 f6be5c30 2018-06-22 stsp struct got_object_id *id)
458 f6be5c30 2018-06-22 stsp {
459 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
460 6bef87be 2018-09-11 stsp &repo->treecache, id);
461 1943de01 2018-06-22 stsp }
462 1943de01 2018-06-22 stsp
463 1943de01 2018-06-22 stsp const struct got_error *
464 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
465 1943de01 2018-06-22 stsp struct got_commit_object *commit)
466 1943de01 2018-06-22 stsp {
467 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
468 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
469 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
470 79c99a64 2019-05-23 stsp if (err) {
471 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
472 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
473 79c99a64 2019-05-23 stsp err = NULL;
474 1943de01 2018-06-22 stsp return err;
475 79c99a64 2019-05-23 stsp }
476 1943de01 2018-06-22 stsp commit->refcnt++;
477 ccfe88e6 2018-07-12 stsp #endif
478 f6be5c30 2018-06-22 stsp return NULL;
479 f6be5c30 2018-06-22 stsp }
480 f6be5c30 2018-06-22 stsp
481 1943de01 2018-06-22 stsp struct got_commit_object *
482 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
483 1943de01 2018-06-22 stsp struct got_object_id *id)
484 1943de01 2018-06-22 stsp {
485 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
486 6bef87be 2018-09-11 stsp &repo->commitcache, id);
487 f4a881ce 2018-11-17 stsp }
488 f4a881ce 2018-11-17 stsp
489 f4a881ce 2018-11-17 stsp const struct got_error *
490 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
491 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
492 f4a881ce 2018-11-17 stsp {
493 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
494 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
495 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
496 79c99a64 2019-05-23 stsp if (err) {
497 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
498 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
499 79c99a64 2019-05-23 stsp err = NULL;
500 f4a881ce 2018-11-17 stsp return err;
501 79c99a64 2019-05-23 stsp }
502 f4a881ce 2018-11-17 stsp tag->refcnt++;
503 f4a881ce 2018-11-17 stsp #endif
504 f4a881ce 2018-11-17 stsp return NULL;
505 f4a881ce 2018-11-17 stsp }
506 f4a881ce 2018-11-17 stsp
507 f4a881ce 2018-11-17 stsp struct got_tag_object *
508 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
509 f4a881ce 2018-11-17 stsp {
510 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
511 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
512 8ab9215c 2021-10-15 thomas }
513 8ab9215c 2021-10-15 thomas
514 8ab9215c 2021-10-15 thomas const struct got_error *
515 8ab9215c 2021-10-15 thomas got_repo_cache_raw_object(struct got_repository *repo, struct got_object_id *id,
516 8ab9215c 2021-10-15 thomas struct got_raw_object *raw)
517 8ab9215c 2021-10-15 thomas {
518 8ab9215c 2021-10-15 thomas #ifndef GOT_NO_OBJ_CACHE
519 8ab9215c 2021-10-15 thomas const struct got_error *err = NULL;
520 8ab9215c 2021-10-15 thomas err = got_object_cache_add(&repo->rawcache, id, raw);
521 8ab9215c 2021-10-15 thomas if (err) {
522 8ab9215c 2021-10-15 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
523 8ab9215c 2021-10-15 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
524 8ab9215c 2021-10-15 thomas err = NULL;
525 8ab9215c 2021-10-15 thomas return err;
526 8ab9215c 2021-10-15 thomas }
527 8ab9215c 2021-10-15 thomas raw->refcnt++;
528 8ab9215c 2021-10-15 thomas #endif
529 8ab9215c 2021-10-15 thomas return NULL;
530 1943de01 2018-06-22 stsp }
531 1943de01 2018-06-22 stsp
532 8ab9215c 2021-10-15 thomas
533 8ab9215c 2021-10-15 thomas struct got_raw_object *
534 8ab9215c 2021-10-15 thomas got_repo_get_cached_raw_object(struct got_repository *repo,
535 8ab9215c 2021-10-15 thomas struct got_object_id *id)
536 8ab9215c 2021-10-15 thomas {
537 8ab9215c 2021-10-15 thomas return (struct got_raw_object *)got_object_cache_get(&repo->rawcache, id);
538 8ab9215c 2021-10-15 thomas }
539 8ab9215c 2021-10-15 thomas
540 8ab9215c 2021-10-15 thomas
541 991ff1aa 2021-06-15 tracey static const struct got_error *
542 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
543 4027f31a 2017-11-04 stsp {
544 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
545 85f51bba 2018-07-16 stsp
546 6d5a9006 2020-12-16 yzhong repo->gitdir_fd = -1;
547 6d5a9006 2020-12-16 yzhong
548 85f51bba 2018-07-16 stsp /* bare git repository? */
549 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
550 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
551 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
552 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
553 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
554 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
555 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
556 6d5a9006 2020-12-16 yzhong goto done;
557 6d5a9006 2020-12-16 yzhong }
558 06340621 2021-12-31 thomas repo->gitdir_fd = open(repo->path_git_dir,
559 06340621 2021-12-31 thomas O_DIRECTORY | O_CLOEXEC);
560 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd == -1) {
561 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("open",
562 6d5a9006 2020-12-16 yzhong repo->path_git_dir);
563 85f51bba 2018-07-16 stsp goto done;
564 85f51bba 2018-07-16 stsp }
565 85f51bba 2018-07-16 stsp return NULL;
566 85f51bba 2018-07-16 stsp }
567 85f51bba 2018-07-16 stsp
568 85f51bba 2018-07-16 stsp /* git repository with working tree? */
569 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
570 6b68ccd6 2019-09-01 stsp repo->path_git_dir = NULL;
571 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
572 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
573 85f51bba 2018-07-16 stsp goto done;
574 85f51bba 2018-07-16 stsp }
575 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
576 85f51bba 2018-07-16 stsp repo->path = strdup(path);
577 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
578 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
579 85f51bba 2018-07-16 stsp goto done;
580 85f51bba 2018-07-16 stsp }
581 06340621 2021-12-31 thomas repo->gitdir_fd = open(repo->path_git_dir,
582 06340621 2021-12-31 thomas O_DIRECTORY | O_CLOEXEC);
583 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd == -1) {
584 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("open",
585 6d5a9006 2020-12-16 yzhong repo->path_git_dir);
586 6d5a9006 2020-12-16 yzhong goto done;
587 6d5a9006 2020-12-16 yzhong }
588 85f51bba 2018-07-16 stsp return NULL;
589 85f51bba 2018-07-16 stsp }
590 85f51bba 2018-07-16 stsp
591 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
592 ee645855 2019-02-05 stsp done:
593 85f51bba 2018-07-16 stsp if (err) {
594 ee645855 2019-02-05 stsp free(repo->path);
595 ee645855 2019-02-05 stsp repo->path = NULL;
596 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
597 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
598 6d5a9006 2020-12-16 yzhong if (repo->gitdir_fd != -1)
599 6d5a9006 2020-12-16 yzhong close(repo->gitdir_fd);
600 6d5a9006 2020-12-16 yzhong repo->gitdir_fd = -1;
601 cd95becd 2019-11-29 stsp
602 cd95becd 2019-11-29 stsp }
603 c9956ddf 2019-09-08 stsp return err;
604 c9956ddf 2019-09-08 stsp }
605 c9956ddf 2019-09-08 stsp
606 c9956ddf 2019-09-08 stsp static const struct got_error *
607 c9956ddf 2019-09-08 stsp read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
608 c9956ddf 2019-09-08 stsp {
609 c9956ddf 2019-09-08 stsp const struct got_error *err = NULL;
610 c9956ddf 2019-09-08 stsp char *repo_gitconfig_path = NULL;
611 c9956ddf 2019-09-08 stsp
612 c9956ddf 2019-09-08 stsp if (global_gitconfig_path) {
613 c9956ddf 2019-09-08 stsp /* Read settings from ~/.gitconfig. */
614 c9956ddf 2019-09-08 stsp int dummy_repo_version;
615 d348087d 2022-10-13 thomas err = got_repo_read_gitconfig(&dummy_repo_version,
616 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_name,
617 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_email,
618 b091c2cd 2023-02-07 thomas NULL, NULL, NULL, NULL, NULL, NULL,
619 b091c2cd 2023-02-07 thomas global_gitconfig_path);
620 c9956ddf 2019-09-08 stsp if (err)
621 c9956ddf 2019-09-08 stsp return err;
622 c9956ddf 2019-09-08 stsp }
623 c9956ddf 2019-09-08 stsp
624 c9956ddf 2019-09-08 stsp /* Read repository's .git/config file. */
625 b46f3e71 2020-03-18 stsp repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
626 b46f3e71 2020-03-18 stsp if (repo_gitconfig_path == NULL)
627 b46f3e71 2020-03-18 stsp return got_error_from_errno("got_repo_get_path_gitconfig");
628 c9956ddf 2019-09-08 stsp
629 d348087d 2022-10-13 thomas err = got_repo_read_gitconfig(
630 d348087d 2022-10-13 thomas &repo->gitconfig_repository_format_version,
631 c9956ddf 2019-09-08 stsp &repo->gitconfig_author_name, &repo->gitconfig_author_email,
632 cd95becd 2019-11-29 stsp &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
633 b091c2cd 2023-02-07 thomas &repo->gitconfig_owner, &repo->extnames, &repo->extvals,
634 b091c2cd 2023-02-07 thomas &repo->nextensions, repo_gitconfig_path);
635 c9956ddf 2019-09-08 stsp if (err)
636 c9956ddf 2019-09-08 stsp goto done;
637 5daf5c53 2022-07-23 thomas
638 5daf5c53 2022-07-23 thomas if (getenv("GOT_IGNORE_GITCONFIG") != NULL) {
639 5daf5c53 2022-07-23 thomas int i;
640 5daf5c53 2022-07-23 thomas
641 5daf5c53 2022-07-23 thomas for (i = 0; i < repo->ngitconfig_remotes; i++) {
642 5daf5c53 2022-07-23 thomas got_repo_free_remote_repo_data(
643 5daf5c53 2022-07-23 thomas &repo->gitconfig_remotes[i]);
644 5daf5c53 2022-07-23 thomas }
645 5daf5c53 2022-07-23 thomas free(repo->gitconfig_remotes);
646 7e91f3d2 2022-07-24 thomas repo->gitconfig_remotes = NULL;
647 5daf5c53 2022-07-23 thomas repo->ngitconfig_remotes = 0;
648 5daf5c53 2022-07-23 thomas
649 5daf5c53 2022-07-23 thomas free(repo->gitconfig_author_name);
650 5daf5c53 2022-07-23 thomas repo->gitconfig_author_name = NULL;
651 5daf5c53 2022-07-23 thomas free(repo->gitconfig_author_email);
652 5daf5c53 2022-07-23 thomas repo->gitconfig_author_email = NULL;
653 7e91f3d2 2022-07-24 thomas
654 7e91f3d2 2022-07-24 thomas free(repo->global_gitconfig_author_name);
655 7e91f3d2 2022-07-24 thomas repo->global_gitconfig_author_name = NULL;
656 7e91f3d2 2022-07-24 thomas free(repo->global_gitconfig_author_email);
657 7e91f3d2 2022-07-24 thomas repo->global_gitconfig_author_email = NULL;
658 5daf5c53 2022-07-23 thomas }
659 5daf5c53 2022-07-23 thomas
660 c9956ddf 2019-09-08 stsp done:
661 c9956ddf 2019-09-08 stsp free(repo_gitconfig_path);
662 257add31 2020-09-09 stsp return err;
663 257add31 2020-09-09 stsp }
664 257add31 2020-09-09 stsp
665 257add31 2020-09-09 stsp static const struct got_error *
666 257add31 2020-09-09 stsp read_gotconfig(struct got_repository *repo)
667 257add31 2020-09-09 stsp {
668 257add31 2020-09-09 stsp const struct got_error *err = NULL;
669 257add31 2020-09-09 stsp char *gotconfig_path;
670 257add31 2020-09-09 stsp
671 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
672 257add31 2020-09-09 stsp if (gotconfig_path == NULL)
673 257add31 2020-09-09 stsp return got_error_from_errno("got_repo_get_path_gotconfig");
674 257add31 2020-09-09 stsp
675 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
676 257add31 2020-09-09 stsp free(gotconfig_path);
677 85f51bba 2018-07-16 stsp return err;
678 85f51bba 2018-07-16 stsp }
679 85f51bba 2018-07-16 stsp
680 20b7abb3 2020-10-22 stsp /* Supported repository format extensions. */
681 d224ad33 2022-03-22 thomas static const char *const repo_extensions[] = {
682 20b7abb3 2020-10-22 stsp "noop", /* Got supports repository format version 1. */
683 2252c019 2021-07-03 stsp "preciousObjects", /* Supported by gotadmin cleanup. */
684 20b7abb3 2020-10-22 stsp "worktreeConfig", /* Got does not care about Git work trees. */
685 20b7abb3 2020-10-22 stsp };
686 20b7abb3 2020-10-22 stsp
687 85f51bba 2018-07-16 stsp const struct got_error *
688 c9956ddf 2019-09-08 stsp got_repo_open(struct got_repository **repop, const char *path,
689 7cd52833 2022-06-23 thomas const char *global_gitconfig_path, int *pack_fds)
690 85f51bba 2018-07-16 stsp {
691 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
692 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
693 dbb02f4d 2020-12-04 stsp char *repo_path = NULL;
694 7cd52833 2022-06-23 thomas size_t i, j = 0;
695 4027f31a 2017-11-04 stsp
696 85f51bba 2018-07-16 stsp *repop = NULL;
697 4027f31a 2017-11-04 stsp
698 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
699 58cbcd83 2022-03-22 thomas if (repo == NULL)
700 58cbcd83 2022-03-22 thomas return got_error_from_errno("calloc");
701 4545b700 2021-10-15 thomas
702 cfe41121 2021-10-16 thomas RB_INIT(&repo->packidx_bloom_filters);
703 de47d040 2022-03-22 thomas TAILQ_INIT(&repo->packidx_paths);
704 4027f31a 2017-11-04 stsp
705 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
706 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
707 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
708 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
709 ad242220 2018-09-08 stsp }
710 ad242220 2018-09-08 stsp
711 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
712 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
713 6bef87be 2018-09-11 stsp if (err)
714 f6be5c30 2018-06-22 stsp goto done;
715 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
716 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
717 6bef87be 2018-09-11 stsp if (err)
718 1943de01 2018-06-22 stsp goto done;
719 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
720 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
721 6bef87be 2018-09-11 stsp if (err)
722 eb77ee11 2018-07-08 stsp goto done;
723 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
724 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
725 f4a881ce 2018-11-17 stsp if (err)
726 f4a881ce 2018-11-17 stsp goto done;
727 8ab9215c 2021-10-15 thomas err = got_object_cache_init(&repo->rawcache,
728 8ab9215c 2021-10-15 thomas GOT_OBJECT_CACHE_TYPE_RAW);
729 8ab9215c 2021-10-15 thomas if (err)
730 8ab9215c 2021-10-15 thomas goto done;
731 1943de01 2018-06-22 stsp
732 e0220e74 2022-10-31 thomas err = get_pack_cache_size(&repo->pack_cache_size);
733 e0220e74 2022-10-31 thomas if (err)
734 e0220e74 2022-10-31 thomas goto done;
735 bfb5ee0b 2022-05-31 thomas for (i = 0; i < nitems(repo->packs); i++) {
736 3efd8e31 2022-10-23 thomas if (pack_fds != NULL && i < repo->pack_cache_size) {
737 7cd52833 2022-06-23 thomas repo->packs[i].basefd = pack_fds[j++];
738 7cd52833 2022-06-23 thomas repo->packs[i].accumfd = pack_fds[j++];
739 bfb5ee0b 2022-05-31 thomas } else {
740 bfb5ee0b 2022-05-31 thomas repo->packs[i].basefd = -1;
741 bfb5ee0b 2022-05-31 thomas repo->packs[i].accumfd = -1;
742 bfb5ee0b 2022-05-31 thomas }
743 bfb5ee0b 2022-05-31 thomas }
744 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo->tempfiles); i++)
745 3efd8e31 2022-10-23 thomas repo->tempfiles[i] = -1;
746 ec2b23c5 2022-07-01 thomas repo->pinned_pack = -1;
747 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = -1;
748 ec2b23c5 2022-07-01 thomas repo->pinned_pid = 0;
749 6c414261 2021-03-30 stsp
750 dbb02f4d 2020-12-04 stsp repo_path = realpath(path, NULL);
751 aff6eea4 2020-10-20 stsp if (repo_path == NULL) {
752 dbb02f4d 2020-12-04 stsp err = got_error_from_errno2("realpath", path);
753 92af5469 2017-11-05 stsp goto done;
754 92af5469 2017-11-05 stsp }
755 4027f31a 2017-11-04 stsp
756 aff6eea4 2020-10-20 stsp for (;;) {
757 aff6eea4 2020-10-20 stsp char *parent_path;
758 aff6eea4 2020-10-20 stsp
759 aff6eea4 2020-10-20 stsp err = open_repo(repo, repo_path);
760 85f51bba 2018-07-16 stsp if (err == NULL)
761 85f51bba 2018-07-16 stsp break;
762 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
763 9aceaadf 2020-10-20 stsp goto done;
764 aff6eea4 2020-10-20 stsp if (repo_path[0] == '/' && repo_path[1] == '\0') {
765 0c93d204 2020-10-20 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
766 0c93d204 2020-10-20 stsp goto done;
767 442a3ddc 2018-04-23 stsp }
768 aff6eea4 2020-10-20 stsp err = got_path_dirname(&parent_path, repo_path);
769 aff6eea4 2020-10-20 stsp if (err)
770 f2db9c47 2019-08-24 stsp goto done;
771 aff6eea4 2020-10-20 stsp free(repo_path);
772 aff6eea4 2020-10-20 stsp repo_path = parent_path;
773 aff6eea4 2020-10-20 stsp }
774 1d126e2d 2019-08-24 stsp
775 257add31 2020-09-09 stsp err = read_gotconfig(repo);
776 257add31 2020-09-09 stsp if (err)
777 257add31 2020-09-09 stsp goto done;
778 257add31 2020-09-09 stsp
779 c9956ddf 2019-09-08 stsp err = read_gitconfig(repo, global_gitconfig_path);
780 1d126e2d 2019-08-24 stsp if (err)
781 1d126e2d 2019-08-24 stsp goto done;
782 ebb42948 2022-07-21 thomas if (repo->gitconfig_repository_format_version != 0) {
783 aba9c984 2019-09-08 stsp err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
784 ebb42948 2022-07-21 thomas goto done;
785 ebb42948 2022-07-21 thomas }
786 20b7abb3 2020-10-22 stsp for (i = 0; i < repo->nextensions; i++) {
787 b091c2cd 2023-02-07 thomas char *ext = repo->extnames[i];
788 b091c2cd 2023-02-07 thomas char *val = repo->extvals[i];
789 20b7abb3 2020-10-22 stsp int j, supported = 0;
790 b091c2cd 2023-02-07 thomas
791 b091c2cd 2023-02-07 thomas if (!is_boolean_val(val)) {
792 b091c2cd 2023-02-07 thomas err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
793 b091c2cd 2023-02-07 thomas goto done;
794 b091c2cd 2023-02-07 thomas }
795 b091c2cd 2023-02-07 thomas
796 b091c2cd 2023-02-07 thomas if (!get_boolean_val(val))
797 b091c2cd 2023-02-07 thomas continue;
798 b091c2cd 2023-02-07 thomas
799 20b7abb3 2020-10-22 stsp for (j = 0; j < nitems(repo_extensions); j++) {
800 20b7abb3 2020-10-22 stsp if (strcmp(ext, repo_extensions[j]) == 0) {
801 20b7abb3 2020-10-22 stsp supported = 1;
802 20b7abb3 2020-10-22 stsp break;
803 20b7abb3 2020-10-22 stsp }
804 20b7abb3 2020-10-22 stsp }
805 20b7abb3 2020-10-22 stsp if (!supported) {
806 20b7abb3 2020-10-22 stsp err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
807 20b7abb3 2020-10-22 stsp goto done;
808 20b7abb3 2020-10-22 stsp }
809 20b7abb3 2020-10-22 stsp }
810 de47d040 2022-03-22 thomas
811 de47d040 2022-03-22 thomas err = got_repo_list_packidx(&repo->packidx_paths, repo);
812 92af5469 2017-11-05 stsp done:
813 92af5469 2017-11-05 stsp if (err)
814 5c2f5761 2018-09-19 stsp got_repo_close(repo);
815 85f51bba 2018-07-16 stsp else
816 85f51bba 2018-07-16 stsp *repop = repo;
817 aff6eea4 2020-10-20 stsp free(repo_path);
818 92af5469 2017-11-05 stsp return err;
819 4027f31a 2017-11-04 stsp }
820 4027f31a 2017-11-04 stsp
821 ad242220 2018-09-08 stsp const struct got_error *
822 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
823 4027f31a 2017-11-04 stsp {
824 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
825 0be8fa4c 2021-10-15 thomas struct got_packidx_bloom_filter *bf;
826 16aeacf7 2020-11-26 stsp size_t i;
827 79b11c62 2018-03-09 stsp
828 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
829 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
830 79b11c62 2018-03-09 stsp break;
831 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
832 4545b700 2021-10-15 thomas }
833 4545b700 2021-10-15 thomas
834 cfe41121 2021-10-16 thomas while ((bf = RB_MIN(got_packidx_bloom_filter_tree,
835 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters))) {
836 cfe41121 2021-10-16 thomas RB_REMOVE(got_packidx_bloom_filter_tree,
837 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters, bf);
838 c4f29b53 2022-08-31 thomas bloom_free(bf->bloom);
839 4545b700 2021-10-15 thomas free(bf->bloom);
840 4545b700 2021-10-15 thomas free(bf);
841 79b11c62 2018-03-09 stsp }
842 bd1223b9 2018-03-14 stsp
843 7cd52833 2022-06-23 thomas for (i = 0; i < repo->pack_cache_size; i++)
844 cf032c44 2022-05-31 thomas if (repo->packs[i].path_packfile)
845 7cd52833 2022-06-23 thomas if (repo->packs[i].path_packfile)
846 7cd52833 2022-06-23 thomas got_pack_close(&repo->packs[i]);
847 7e656b93 2018-03-17 stsp
848 4027f31a 2017-11-04 stsp free(repo->path);
849 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
850 cd717821 2018-06-22 stsp
851 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
852 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
853 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
854 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
855 8ab9215c 2021-10-15 thomas got_object_cache_close(&repo->rawcache);
856 ad242220 2018-09-08 stsp
857 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
858 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
859 ad242220 2018-09-08 stsp continue;
860 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
861 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
862 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
863 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
864 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
865 ad242220 2018-09-08 stsp if (child_err && err == NULL)
866 ad242220 2018-09-08 stsp err = child_err;
867 08578a35 2021-01-22 stsp if (close(repo->privsep_children[i].imsg_fd) == -1 &&
868 3a6ce05a 2019-02-11 stsp err == NULL)
869 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
870 ad242220 2018-09-08 stsp }
871 aba9c984 2019-09-08 stsp
872 1d0f4054 2021-06-17 stsp if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
873 1d0f4054 2021-06-17 stsp err == NULL)
874 1d0f4054 2021-06-17 stsp err = got_error_from_errno("close");
875 991ff1aa 2021-06-15 tracey
876 50b0790e 2020-09-11 stsp if (repo->gotconfig)
877 50b0790e 2020-09-11 stsp got_gotconfig_free(repo->gotconfig);
878 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_name);
879 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_email);
880 b8adfa55 2020-09-25 stsp for (i = 0; i < repo->ngitconfig_remotes; i++)
881 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
882 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes);
883 b091c2cd 2023-02-07 thomas for (i = 0; i < repo->nextensions; i++) {
884 b091c2cd 2023-02-07 thomas free(repo->extnames[i]);
885 b091c2cd 2023-02-07 thomas free(repo->extvals[i]);
886 b091c2cd 2023-02-07 thomas }
887 b091c2cd 2023-02-07 thomas free(repo->extnames);
888 b091c2cd 2023-02-07 thomas free(repo->extvals);
889 de47d040 2022-03-22 thomas
890 21c2d8be 2023-01-10 thomas got_pathlist_free(&repo->packidx_paths, GOT_PATHLIST_FREE_PATH);
891 4027f31a 2017-11-04 stsp free(repo);
892 ad242220 2018-09-08 stsp
893 ad242220 2018-09-08 stsp return err;
894 b8adfa55 2020-09-25 stsp }
895 b8adfa55 2020-09-25 stsp
896 b8adfa55 2020-09-25 stsp void
897 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(struct got_remote_repo *repo)
898 b8adfa55 2020-09-25 stsp {
899 b8adfa55 2020-09-25 stsp int i;
900 b8adfa55 2020-09-25 stsp
901 b8adfa55 2020-09-25 stsp free(repo->name);
902 b8adfa55 2020-09-25 stsp repo->name = NULL;
903 6480c871 2021-08-30 stsp free(repo->fetch_url);
904 6480c871 2021-08-30 stsp repo->fetch_url = NULL;
905 6480c871 2021-08-30 stsp free(repo->send_url);
906 6480c871 2021-08-30 stsp repo->send_url = NULL;
907 6480c871 2021-08-30 stsp for (i = 0; i < repo->nfetch_branches; i++)
908 6480c871 2021-08-30 stsp free(repo->fetch_branches[i]);
909 6480c871 2021-08-30 stsp free(repo->fetch_branches);
910 6480c871 2021-08-30 stsp repo->fetch_branches = NULL;
911 6480c871 2021-08-30 stsp repo->nfetch_branches = 0;
912 6480c871 2021-08-30 stsp for (i = 0; i < repo->nsend_branches; i++)
913 6480c871 2021-08-30 stsp free(repo->send_branches[i]);
914 6480c871 2021-08-30 stsp free(repo->send_branches);
915 6480c871 2021-08-30 stsp repo->send_branches = NULL;
916 6480c871 2021-08-30 stsp repo->nsend_branches = 0;
917 4027f31a 2017-11-04 stsp }
918 04ca23f4 2018-07-16 stsp
919 04ca23f4 2018-07-16 stsp const struct got_error *
920 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
921 8fa913ec 2020-11-14 stsp const char *input_path)
922 04ca23f4 2018-07-16 stsp {
923 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
924 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
925 e83c0634 2020-01-27 stsp size_t repolen, len;
926 e83c0634 2020-01-27 stsp char *canonpath, *path = NULL;
927 04ca23f4 2018-07-16 stsp
928 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
929 04ca23f4 2018-07-16 stsp
930 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
931 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
932 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
933 04ca23f4 2018-07-16 stsp goto done;
934 04ca23f4 2018-07-16 stsp }
935 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
936 04ca23f4 2018-07-16 stsp if (err)
937 04ca23f4 2018-07-16 stsp goto done;
938 04ca23f4 2018-07-16 stsp
939 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
940 04ca23f4 2018-07-16 stsp
941 8fa913ec 2020-11-14 stsp if (canonpath[0] == '\0') {
942 23721109 2018-10-22 stsp path = strdup(canonpath);
943 b70703ad 2019-03-18 stsp if (path == NULL) {
944 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
945 04ca23f4 2018-07-16 stsp goto done;
946 04ca23f4 2018-07-16 stsp }
947 04ca23f4 2018-07-16 stsp } else {
948 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
949 04ca23f4 2018-07-16 stsp if (path == NULL) {
950 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
951 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
952 230a42bd 2019-05-11 jcs canonpath);
953 b70703ad 2019-03-18 stsp goto done;
954 b70703ad 2019-03-18 stsp }
955 b70703ad 2019-03-18 stsp /*
956 b70703ad 2019-03-18 stsp * Path is not on disk.
957 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
958 b70703ad 2019-03-18 stsp */
959 b70703ad 2019-03-18 stsp path = strdup(canonpath);
960 b70703ad 2019-03-18 stsp if (path == NULL) {
961 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
962 b70703ad 2019-03-18 stsp goto done;
963 b70703ad 2019-03-18 stsp }
964 04ca23f4 2018-07-16 stsp }
965 04ca23f4 2018-07-16 stsp
966 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
967 04ca23f4 2018-07-16 stsp len = strlen(path);
968 04ca23f4 2018-07-16 stsp
969 04ca23f4 2018-07-16 stsp
970 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
971 04ca23f4 2018-07-16 stsp free(path);
972 04ca23f4 2018-07-16 stsp path = strdup("");
973 04ca23f4 2018-07-16 stsp if (path == NULL) {
974 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
975 04ca23f4 2018-07-16 stsp goto done;
976 04ca23f4 2018-07-16 stsp }
977 65aa7d1c 2020-01-28 stsp } else if (len > repolen &&
978 65aa7d1c 2020-01-28 stsp got_path_is_child(path, repo_abspath, repolen)) {
979 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
980 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
981 04ca23f4 2018-07-16 stsp /*
982 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
983 eef9542c 2020-10-22 stsp * database. Treat input as repository-relative.
984 04ca23f4 2018-07-16 stsp */
985 abc59930 2021-09-05 naddy free(path);
986 abc59930 2021-09-05 naddy path = canonpath;
987 abc59930 2021-09-05 naddy canonpath = NULL;
988 04ca23f4 2018-07-16 stsp } else {
989 04ca23f4 2018-07-16 stsp char *child;
990 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
991 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
992 04ca23f4 2018-07-16 stsp repo_abspath, path);
993 04ca23f4 2018-07-16 stsp if (err)
994 04ca23f4 2018-07-16 stsp goto done;
995 04ca23f4 2018-07-16 stsp free(path);
996 04ca23f4 2018-07-16 stsp path = child;
997 04ca23f4 2018-07-16 stsp }
998 04ca23f4 2018-07-16 stsp } else {
999 04ca23f4 2018-07-16 stsp /*
1000 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
1001 eef9542c 2020-10-22 stsp * Treat input as repository-relative.
1002 04ca23f4 2018-07-16 stsp */
1003 abc59930 2021-09-05 naddy free(path);
1004 abc59930 2021-09-05 naddy path = canonpath;
1005 abc59930 2021-09-05 naddy canonpath = NULL;
1006 04ca23f4 2018-07-16 stsp }
1007 04ca23f4 2018-07-16 stsp }
1008 04ca23f4 2018-07-16 stsp
1009 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
1010 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
1011 04ca23f4 2018-07-16 stsp char *abspath;
1012 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
1013 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1014 04ca23f4 2018-07-16 stsp goto done;
1015 04ca23f4 2018-07-16 stsp }
1016 04ca23f4 2018-07-16 stsp free(path);
1017 04ca23f4 2018-07-16 stsp path = abspath;
1018 04ca23f4 2018-07-16 stsp }
1019 04ca23f4 2018-07-16 stsp
1020 04ca23f4 2018-07-16 stsp done:
1021 04ca23f4 2018-07-16 stsp free(canonpath);
1022 04ca23f4 2018-07-16 stsp if (err)
1023 04ca23f4 2018-07-16 stsp free(path);
1024 04ca23f4 2018-07-16 stsp else
1025 04ca23f4 2018-07-16 stsp *in_repo_path = path;
1026 1510f469 2018-09-09 stsp return err;
1027 1510f469 2018-09-09 stsp }
1028 1510f469 2018-09-09 stsp
1029 e1a68182 2020-01-07 stsp static const struct got_error *
1030 e1a68182 2020-01-07 stsp cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
1031 e1a68182 2020-01-07 stsp const char *path_packidx)
1032 1510f469 2018-09-09 stsp {
1033 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1034 16aeacf7 2020-11-26 stsp size_t i;
1035 1510f469 2018-09-09 stsp
1036 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1037 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
1038 1510f469 2018-09-09 stsp break;
1039 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
1040 e1a68182 2020-01-07 stsp path_packidx) == 0) {
1041 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1042 e1a68182 2020-01-07 stsp }
1043 1510f469 2018-09-09 stsp }
1044 6c414261 2021-03-30 stsp if (i == repo->pack_cache_size) {
1045 ec2b23c5 2022-07-01 thomas do {
1046 ec2b23c5 2022-07-01 thomas i--;
1047 ec2b23c5 2022-07-01 thomas } while (i > 0 && repo->pinned_packidx >= 0 &&
1048 ec2b23c5 2022-07-01 thomas i == repo->pinned_packidx);
1049 3333aec6 2021-10-15 thomas err = got_packidx_close(repo->packidx_cache[i]);
1050 1510f469 2018-09-09 stsp if (err)
1051 1510f469 2018-09-09 stsp return err;
1052 1510f469 2018-09-09 stsp }
1053 1510f469 2018-09-09 stsp
1054 3333aec6 2021-10-15 thomas repo->packidx_cache[i] = packidx;
1055 15fe583f 2018-11-05 stsp
1056 1510f469 2018-09-09 stsp return NULL;
1057 1510f469 2018-09-09 stsp }
1058 1510f469 2018-09-09 stsp
1059 1124fe40 2021-07-07 stsp int
1060 1124fe40 2021-07-07 stsp got_repo_is_packidx_filename(const char *name, size_t len)
1061 1510f469 2018-09-09 stsp {
1062 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
1063 1510f469 2018-09-09 stsp return 0;
1064 1510f469 2018-09-09 stsp
1065 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
1066 1510f469 2018-09-09 stsp return 0;
1067 1510f469 2018-09-09 stsp
1068 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
1069 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
1070 1510f469 2018-09-09 stsp return 0;
1071 4545b700 2021-10-15 thomas
1072 4545b700 2021-10-15 thomas return 1;
1073 0be8fa4c 2021-10-15 thomas }
1074 0be8fa4c 2021-10-15 thomas
1075 cfe41121 2021-10-16 thomas static struct got_packidx_bloom_filter *
1076 cfe41121 2021-10-16 thomas get_packidx_bloom_filter(struct got_repository *repo,
1077 cfe41121 2021-10-16 thomas const char *path, size_t path_len)
1078 cfe41121 2021-10-16 thomas {
1079 cfe41121 2021-10-16 thomas struct got_packidx_bloom_filter key;
1080 cfe41121 2021-10-16 thomas
1081 cfe41121 2021-10-16 thomas if (strlcpy(key.path, path, sizeof(key.path)) >= sizeof(key.path))
1082 cfe41121 2021-10-16 thomas return NULL; /* XXX */
1083 cfe41121 2021-10-16 thomas key.path_len = path_len;
1084 cfe41121 2021-10-16 thomas
1085 cfe41121 2021-10-16 thomas return RB_FIND(got_packidx_bloom_filter_tree,
1086 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters, &key);
1087 cfe41121 2021-10-16 thomas }
1088 cfe41121 2021-10-16 thomas
1089 f9c2e8e5 2022-02-13 thomas int
1090 f9c2e8e5 2022-02-13 thomas got_repo_check_packidx_bloom_filter(struct got_repository *repo,
1091 4545b700 2021-10-15 thomas const char *path_packidx, struct got_object_id *id)
1092 4545b700 2021-10-15 thomas {
1093 4545b700 2021-10-15 thomas struct got_packidx_bloom_filter *bf;
1094 4545b700 2021-10-15 thomas
1095 cfe41121 2021-10-16 thomas bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx));
1096 cfe41121 2021-10-16 thomas if (bf)
1097 cfe41121 2021-10-16 thomas return bloom_check(bf->bloom, id->sha1, sizeof(id->sha1));
1098 1510f469 2018-09-09 stsp
1099 4545b700 2021-10-15 thomas /* No bloom filter means this pack index must be searched. */
1100 1510f469 2018-09-09 stsp return 1;
1101 4545b700 2021-10-15 thomas }
1102 4545b700 2021-10-15 thomas
1103 4545b700 2021-10-15 thomas static const struct got_error *
1104 4545b700 2021-10-15 thomas add_packidx_bloom_filter(struct got_repository *repo,
1105 4545b700 2021-10-15 thomas struct got_packidx *packidx, const char *path_packidx)
1106 4545b700 2021-10-15 thomas {
1107 4545b700 2021-10-15 thomas int i, nobjects = be32toh(packidx->hdr.fanout_table[0xff]);
1108 4545b700 2021-10-15 thomas struct got_packidx_bloom_filter *bf;
1109 4545b700 2021-10-15 thomas size_t len;
1110 4545b700 2021-10-15 thomas
1111 4545b700 2021-10-15 thomas /*
1112 4545b700 2021-10-15 thomas * Don't use bloom filters for very large pack index files.
1113 4545b700 2021-10-15 thomas * Large pack files will contain a relatively large fraction
1114 4545b700 2021-10-15 thomas * of our objects so we will likely need to visit them anyway.
1115 4545b700 2021-10-15 thomas * The more objects a pack file contains the higher the probability
1116 4545b700 2021-10-15 thomas * of a false-positive match from the bloom filter. And reading
1117 4545b700 2021-10-15 thomas * all object IDs from a large pack index file can be expensive.
1118 4545b700 2021-10-15 thomas */
1119 4545b700 2021-10-15 thomas if (nobjects > 100000) /* cut-off at about 2MB, at 20 bytes per ID */
1120 4545b700 2021-10-15 thomas return NULL;
1121 4545b700 2021-10-15 thomas
1122 4545b700 2021-10-15 thomas /* Do we already have a filter for this pack index? */
1123 cfe41121 2021-10-16 thomas if (get_packidx_bloom_filter(repo, path_packidx,
1124 cfe41121 2021-10-16 thomas strlen(path_packidx)) != NULL)
1125 cfe41121 2021-10-16 thomas return NULL;
1126 4545b700 2021-10-15 thomas
1127 4545b700 2021-10-15 thomas bf = calloc(1, sizeof(*bf));
1128 4545b700 2021-10-15 thomas if (bf == NULL)
1129 4545b700 2021-10-15 thomas return got_error_from_errno("calloc");
1130 4545b700 2021-10-15 thomas bf->bloom = calloc(1, sizeof(*bf->bloom));
1131 4545b700 2021-10-15 thomas if (bf->bloom == NULL) {
1132 4545b700 2021-10-15 thomas free(bf);
1133 4545b700 2021-10-15 thomas return got_error_from_errno("calloc");
1134 4545b700 2021-10-15 thomas }
1135 b6b86fd1 2022-08-30 thomas
1136 cfe41121 2021-10-16 thomas len = strlcpy(bf->path, path_packidx, sizeof(bf->path));
1137 cfe41121 2021-10-16 thomas if (len >= sizeof(bf->path)) {
1138 4545b700 2021-10-15 thomas free(bf->bloom);
1139 4545b700 2021-10-15 thomas free(bf);
1140 4545b700 2021-10-15 thomas return got_error(GOT_ERR_NO_SPACE);
1141 4545b700 2021-10-15 thomas }
1142 cfe41121 2021-10-16 thomas bf->path_len = len;
1143 4545b700 2021-10-15 thomas
1144 4545b700 2021-10-15 thomas /* Minimum size supported by our bloom filter is 1000 entries. */
1145 4545b700 2021-10-15 thomas bloom_init(bf->bloom, nobjects < 1000 ? 1000 : nobjects, 0.1);
1146 4545b700 2021-10-15 thomas for (i = 0; i < nobjects; i++) {
1147 4545b700 2021-10-15 thomas struct got_packidx_object_id *id;
1148 4545b700 2021-10-15 thomas id = &packidx->hdr.sorted_ids[i];
1149 4545b700 2021-10-15 thomas bloom_add(bf->bloom, id->sha1, sizeof(id->sha1));
1150 4545b700 2021-10-15 thomas }
1151 4545b700 2021-10-15 thomas
1152 cfe41121 2021-10-16 thomas RB_INSERT(got_packidx_bloom_filter_tree,
1153 cfe41121 2021-10-16 thomas &repo->packidx_bloom_filters, bf);
1154 4545b700 2021-10-15 thomas return NULL;
1155 5b462462 2022-10-22 thomas }
1156 5b462462 2022-10-22 thomas
1157 5b462462 2022-10-22 thomas static void
1158 5b462462 2022-10-22 thomas purge_packidx_paths(struct got_pathlist_head *packidx_paths)
1159 5b462462 2022-10-22 thomas {
1160 5b462462 2022-10-22 thomas struct got_pathlist_entry *pe;
1161 5b462462 2022-10-22 thomas
1162 5b462462 2022-10-22 thomas while (!TAILQ_EMPTY(packidx_paths)) {
1163 5b462462 2022-10-22 thomas pe = TAILQ_FIRST(packidx_paths);
1164 5b462462 2022-10-22 thomas TAILQ_REMOVE(packidx_paths, pe, entry);
1165 5b462462 2022-10-22 thomas free((char *)pe->path);
1166 5b462462 2022-10-22 thomas free(pe);
1167 5b462462 2022-10-22 thomas }
1168 5b462462 2022-10-22 thomas }
1169 5b462462 2022-10-22 thomas
1170 5b462462 2022-10-22 thomas static const struct got_error *
1171 5b462462 2022-10-22 thomas refresh_packidx_paths(struct got_repository *repo)
1172 5b462462 2022-10-22 thomas {
1173 5b462462 2022-10-22 thomas const struct got_error *err = NULL;
1174 5b462462 2022-10-22 thomas char *objects_pack_dir = NULL;
1175 5b462462 2022-10-22 thomas struct stat sb;
1176 5b462462 2022-10-22 thomas
1177 5b462462 2022-10-22 thomas objects_pack_dir = got_repo_get_path_objects_pack(repo);
1178 5b462462 2022-10-22 thomas if (objects_pack_dir == NULL)
1179 5b462462 2022-10-22 thomas return got_error_from_errno("got_repo_get_path_objects_pack");
1180 5b462462 2022-10-22 thomas
1181 5b462462 2022-10-22 thomas if (stat(objects_pack_dir, &sb) == -1) {
1182 5b462462 2022-10-22 thomas if (errno != ENOENT) {
1183 5b462462 2022-10-22 thomas err = got_error_from_errno2("stat", objects_pack_dir);
1184 5b462462 2022-10-22 thomas goto done;
1185 5b462462 2022-10-22 thomas }
1186 9b251092 2022-11-08 thomas } else if (TAILQ_EMPTY(&repo->packidx_paths) ||
1187 9b251092 2022-11-08 thomas sb.st_mtim.tv_sec != repo->pack_path_mtime.tv_sec ||
1188 9b251092 2022-11-08 thomas sb.st_mtim.tv_nsec != repo->pack_path_mtime.tv_nsec) {
1189 5b462462 2022-10-22 thomas purge_packidx_paths(&repo->packidx_paths);
1190 5b462462 2022-10-22 thomas err = got_repo_list_packidx(&repo->packidx_paths, repo);
1191 5b462462 2022-10-22 thomas if (err)
1192 5b462462 2022-10-22 thomas goto done;
1193 5b462462 2022-10-22 thomas }
1194 5b462462 2022-10-22 thomas done:
1195 5b462462 2022-10-22 thomas free(objects_pack_dir);
1196 5b462462 2022-10-22 thomas return err;
1197 1510f469 2018-09-09 stsp }
1198 1510f469 2018-09-09 stsp
1199 1510f469 2018-09-09 stsp const struct got_error *
1200 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
1201 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
1202 1510f469 2018-09-09 stsp {
1203 1510f469 2018-09-09 stsp const struct got_error *err;
1204 de47d040 2022-03-22 thomas struct got_pathlist_entry *pe;
1205 16aeacf7 2020-11-26 stsp size_t i;
1206 1510f469 2018-09-09 stsp
1207 1510f469 2018-09-09 stsp /* Search pack index cache. */
1208 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1209 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
1210 1510f469 2018-09-09 stsp break;
1211 f9c2e8e5 2022-02-13 thomas if (!got_repo_check_packidx_bloom_filter(repo,
1212 4545b700 2021-10-15 thomas repo->packidx_cache[i]->path_packidx, id))
1213 4545b700 2021-10-15 thomas continue; /* object will not be found in this index */
1214 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
1215 1510f469 2018-09-09 stsp if (*idx != -1) {
1216 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
1217 87c1ed2b 2020-01-07 stsp /*
1218 87c1ed2b 2020-01-07 stsp * Move this cache entry to the front. Repeatedly
1219 87c1ed2b 2020-01-07 stsp * searching a wrong pack index can be expensive.
1220 87c1ed2b 2020-01-07 stsp */
1221 87c1ed2b 2020-01-07 stsp if (i > 0) {
1222 3333aec6 2021-10-15 thomas memmove(&repo->packidx_cache[1],
1223 3333aec6 2021-10-15 thomas &repo->packidx_cache[0],
1224 3333aec6 2021-10-15 thomas i * sizeof(repo->packidx_cache[0]));
1225 87c1ed2b 2020-01-07 stsp repo->packidx_cache[0] = *packidx;
1226 ec2b23c5 2022-07-01 thomas if (repo->pinned_packidx >= 0 &&
1227 ec2b23c5 2022-07-01 thomas repo->pinned_packidx < i)
1228 ec2b23c5 2022-07-01 thomas repo->pinned_packidx++;
1229 ec2b23c5 2022-07-01 thomas else if (repo->pinned_packidx == i)
1230 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = 0;
1231 87c1ed2b 2020-01-07 stsp }
1232 1510f469 2018-09-09 stsp return NULL;
1233 1510f469 2018-09-09 stsp }
1234 1510f469 2018-09-09 stsp }
1235 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
1236 5b462462 2022-10-22 thomas
1237 5b462462 2022-10-22 thomas err = refresh_packidx_paths(repo);
1238 5b462462 2022-10-22 thomas if (err)
1239 5b462462 2022-10-22 thomas return err;
1240 1510f469 2018-09-09 stsp
1241 de47d040 2022-03-22 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1242 de47d040 2022-03-22 thomas const char *path_packidx = pe->path;
1243 e1a68182 2020-01-07 stsp int is_cached = 0;
1244 e1a68182 2020-01-07 stsp
1245 f9c2e8e5 2022-02-13 thomas if (!got_repo_check_packidx_bloom_filter(repo,
1246 de47d040 2022-03-22 thomas pe->path, id))
1247 4545b700 2021-10-15 thomas continue; /* object will not be found in this index */
1248 4545b700 2021-10-15 thomas
1249 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1250 e1a68182 2020-01-07 stsp if (repo->packidx_cache[i] == NULL)
1251 e1a68182 2020-01-07 stsp break;
1252 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
1253 e1a68182 2020-01-07 stsp path_packidx) == 0) {
1254 e1a68182 2020-01-07 stsp is_cached = 1;
1255 e1a68182 2020-01-07 stsp break;
1256 e1a68182 2020-01-07 stsp }
1257 1510f469 2018-09-09 stsp }
1258 de47d040 2022-03-22 thomas if (is_cached)
1259 e1a68182 2020-01-07 stsp continue; /* already searched */
1260 1510f469 2018-09-09 stsp
1261 6d5a9006 2020-12-16 yzhong err = got_packidx_open(packidx, got_repo_get_fd(repo),
1262 6d5a9006 2020-12-16 yzhong path_packidx, 0);
1263 de47d040 2022-03-22 thomas if (err)
1264 e1a68182 2020-01-07 stsp goto done;
1265 e1a68182 2020-01-07 stsp
1266 4545b700 2021-10-15 thomas err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1267 de47d040 2022-03-22 thomas if (err)
1268 4545b700 2021-10-15 thomas goto done;
1269 4545b700 2021-10-15 thomas
1270 e1a68182 2020-01-07 stsp err = cache_packidx(repo, *packidx, path_packidx);
1271 1510f469 2018-09-09 stsp if (err)
1272 1510f469 2018-09-09 stsp goto done;
1273 1510f469 2018-09-09 stsp
1274 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
1275 1510f469 2018-09-09 stsp if (*idx != -1) {
1276 1510f469 2018-09-09 stsp err = NULL; /* found the object */
1277 1510f469 2018-09-09 stsp goto done;
1278 1510f469 2018-09-09 stsp }
1279 1510f469 2018-09-09 stsp }
1280 1510f469 2018-09-09 stsp
1281 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
1282 1510f469 2018-09-09 stsp done:
1283 04ca23f4 2018-07-16 stsp return err;
1284 04ca23f4 2018-07-16 stsp }
1285 1510f469 2018-09-09 stsp
1286 f9c2e8e5 2022-02-13 thomas const struct got_error *
1287 f9c2e8e5 2022-02-13 thomas got_repo_list_packidx(struct got_pathlist_head *packidx_paths,
1288 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
1289 f9c2e8e5 2022-02-13 thomas {
1290 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
1291 f9c2e8e5 2022-02-13 thomas DIR *packdir = NULL;
1292 f9c2e8e5 2022-02-13 thomas struct dirent *dent;
1293 f9c2e8e5 2022-02-13 thomas char *path_packidx = NULL;
1294 f9c2e8e5 2022-02-13 thomas int packdir_fd;
1295 04051e8b 2022-09-02 thomas struct stat sb;
1296 f9c2e8e5 2022-02-13 thomas
1297 f9c2e8e5 2022-02-13 thomas packdir_fd = openat(got_repo_get_fd(repo),
1298 f9c2e8e5 2022-02-13 thomas GOT_OBJECTS_PACK_DIR, O_DIRECTORY | O_CLOEXEC);
1299 f9c2e8e5 2022-02-13 thomas if (packdir_fd == -1) {
1300 f9c2e8e5 2022-02-13 thomas return got_error_from_errno_fmt("openat: %s/%s",
1301 f9c2e8e5 2022-02-13 thomas got_repo_get_path_git_dir(repo),
1302 f9c2e8e5 2022-02-13 thomas GOT_OBJECTS_PACK_DIR);
1303 f9c2e8e5 2022-02-13 thomas }
1304 f9c2e8e5 2022-02-13 thomas
1305 f9c2e8e5 2022-02-13 thomas packdir = fdopendir(packdir_fd);
1306 f9c2e8e5 2022-02-13 thomas if (packdir == NULL) {
1307 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fdopendir");
1308 04051e8b 2022-09-02 thomas goto done;
1309 04051e8b 2022-09-02 thomas }
1310 04051e8b 2022-09-02 thomas
1311 04051e8b 2022-09-02 thomas if (fstat(packdir_fd, &sb) == -1) {
1312 04051e8b 2022-09-02 thomas err = got_error_from_errno("fstat");
1313 f9c2e8e5 2022-02-13 thomas goto done;
1314 f9c2e8e5 2022-02-13 thomas }
1315 9b251092 2022-11-08 thomas repo->pack_path_mtime.tv_sec = sb.st_mtim.tv_sec;
1316 9b251092 2022-11-08 thomas repo->pack_path_mtime.tv_nsec = sb.st_mtim.tv_nsec;
1317 f9c2e8e5 2022-02-13 thomas
1318 f9c2e8e5 2022-02-13 thomas while ((dent = readdir(packdir)) != NULL) {
1319 9389bcf6 2022-02-13 thomas if (!got_repo_is_packidx_filename(dent->d_name,
1320 9389bcf6 2022-02-13 thomas strlen(dent->d_name)))
1321 f9c2e8e5 2022-02-13 thomas continue;
1322 f9c2e8e5 2022-02-13 thomas
1323 f9c2e8e5 2022-02-13 thomas if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1324 f9c2e8e5 2022-02-13 thomas dent->d_name) == -1) {
1325 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("asprintf");
1326 f9c2e8e5 2022-02-13 thomas path_packidx = NULL;
1327 f9c2e8e5 2022-02-13 thomas break;
1328 f9c2e8e5 2022-02-13 thomas }
1329 f9c2e8e5 2022-02-13 thomas
1330 f9c2e8e5 2022-02-13 thomas err = got_pathlist_append(packidx_paths, path_packidx, NULL);
1331 f9c2e8e5 2022-02-13 thomas if (err)
1332 f9c2e8e5 2022-02-13 thomas break;
1333 f9c2e8e5 2022-02-13 thomas }
1334 f9c2e8e5 2022-02-13 thomas done:
1335 f9c2e8e5 2022-02-13 thomas if (err)
1336 f9c2e8e5 2022-02-13 thomas free(path_packidx);
1337 f9c2e8e5 2022-02-13 thomas if (packdir && closedir(packdir) != 0 && err == NULL)
1338 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("closedir");
1339 f9c2e8e5 2022-02-13 thomas return err;
1340 f9c2e8e5 2022-02-13 thomas }
1341 f9c2e8e5 2022-02-13 thomas
1342 f9c2e8e5 2022-02-13 thomas const struct got_error *
1343 f9c2e8e5 2022-02-13 thomas got_repo_get_packidx(struct got_packidx **packidx, const char *path_packidx,
1344 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
1345 f9c2e8e5 2022-02-13 thomas {
1346 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
1347 f9c2e8e5 2022-02-13 thomas size_t i;
1348 f9c2e8e5 2022-02-13 thomas
1349 f9c2e8e5 2022-02-13 thomas *packidx = NULL;
1350 f9c2e8e5 2022-02-13 thomas
1351 f9c2e8e5 2022-02-13 thomas /* Search pack index cache. */
1352 f9c2e8e5 2022-02-13 thomas for (i = 0; i < repo->pack_cache_size; i++) {
1353 f9c2e8e5 2022-02-13 thomas if (repo->packidx_cache[i] == NULL)
1354 f9c2e8e5 2022-02-13 thomas break;
1355 f9c2e8e5 2022-02-13 thomas if (strcmp(repo->packidx_cache[i]->path_packidx,
1356 f9c2e8e5 2022-02-13 thomas path_packidx) == 0) {
1357 f9c2e8e5 2022-02-13 thomas *packidx = repo->packidx_cache[i];
1358 f9c2e8e5 2022-02-13 thomas return NULL;
1359 f9c2e8e5 2022-02-13 thomas }
1360 f9c2e8e5 2022-02-13 thomas }
1361 f9c2e8e5 2022-02-13 thomas /* No luck. Search the filesystem. */
1362 f9c2e8e5 2022-02-13 thomas
1363 f9c2e8e5 2022-02-13 thomas err = got_packidx_open(packidx, got_repo_get_fd(repo),
1364 f9c2e8e5 2022-02-13 thomas path_packidx, 0);
1365 f9c2e8e5 2022-02-13 thomas if (err)
1366 f9c2e8e5 2022-02-13 thomas return err;
1367 f9c2e8e5 2022-02-13 thomas
1368 f9c2e8e5 2022-02-13 thomas err = add_packidx_bloom_filter(repo, *packidx, path_packidx);
1369 f9c2e8e5 2022-02-13 thomas if (err)
1370 f9c2e8e5 2022-02-13 thomas goto done;
1371 f9c2e8e5 2022-02-13 thomas
1372 f9c2e8e5 2022-02-13 thomas err = cache_packidx(repo, *packidx, path_packidx);
1373 f9c2e8e5 2022-02-13 thomas done:
1374 f9c2e8e5 2022-02-13 thomas if (err) {
1375 f9c2e8e5 2022-02-13 thomas got_packidx_close(*packidx);
1376 f9c2e8e5 2022-02-13 thomas *packidx = NULL;
1377 f9c2e8e5 2022-02-13 thomas }
1378 f9c2e8e5 2022-02-13 thomas return err;
1379 f9c2e8e5 2022-02-13 thomas }
1380 f9c2e8e5 2022-02-13 thomas
1381 1510f469 2018-09-09 stsp static const struct got_error *
1382 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
1383 1510f469 2018-09-09 stsp {
1384 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1385 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1386 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
1387 1510f469 2018-09-09 stsp ssize_t n;
1388 1510f469 2018-09-09 stsp
1389 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
1390 1510f469 2018-09-09 stsp if (n < 0)
1391 638f9024 2019-05-13 stsp return got_error_from_errno("read");
1392 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
1393 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1394 1510f469 2018-09-09 stsp
1395 78fb0967 2020-09-09 naddy if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1396 78fb0967 2020-09-09 naddy be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1397 78fb0967 2020-09-09 naddy be32toh(hdr.nobjects) != totobj)
1398 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
1399 1510f469 2018-09-09 stsp
1400 1510f469 2018-09-09 stsp return err;
1401 1510f469 2018-09-09 stsp }
1402 1510f469 2018-09-09 stsp
1403 1510f469 2018-09-09 stsp static const struct got_error *
1404 6d5a9006 2020-12-16 yzhong open_packfile(int *fd, struct got_repository *repo,
1405 6d5a9006 2020-12-16 yzhong const char *relpath, struct got_packidx *packidx)
1406 1510f469 2018-09-09 stsp {
1407 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1408 1510f469 2018-09-09 stsp
1409 fc63f50d 2021-12-31 thomas *fd = openat(got_repo_get_fd(repo), relpath,
1410 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1411 1510f469 2018-09-09 stsp if (*fd == -1)
1412 6d5a9006 2020-12-16 yzhong return got_error_from_errno_fmt("openat: %s/%s",
1413 6d5a9006 2020-12-16 yzhong got_repo_get_path_git_dir(repo), relpath);
1414 1510f469 2018-09-09 stsp
1415 1510f469 2018-09-09 stsp if (packidx) {
1416 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
1417 1510f469 2018-09-09 stsp if (err) {
1418 1510f469 2018-09-09 stsp close(*fd);
1419 1510f469 2018-09-09 stsp *fd = -1;
1420 1510f469 2018-09-09 stsp }
1421 1510f469 2018-09-09 stsp }
1422 1510f469 2018-09-09 stsp
1423 1510f469 2018-09-09 stsp return err;
1424 1510f469 2018-09-09 stsp }
1425 1510f469 2018-09-09 stsp
1426 1510f469 2018-09-09 stsp const struct got_error *
1427 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1428 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
1429 1510f469 2018-09-09 stsp {
1430 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1431 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1432 ff563a3d 2019-05-23 stsp struct stat sb;
1433 16aeacf7 2020-11-26 stsp size_t i;
1434 1510f469 2018-09-09 stsp
1435 1510f469 2018-09-09 stsp if (packp)
1436 1510f469 2018-09-09 stsp *packp = NULL;
1437 1510f469 2018-09-09 stsp
1438 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1439 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1440 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1441 1510f469 2018-09-09 stsp break;
1442 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1443 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1444 1510f469 2018-09-09 stsp }
1445 1510f469 2018-09-09 stsp
1446 6c414261 2021-03-30 stsp if (i == repo->pack_cache_size) {
1447 93948435 2022-06-13 thomas struct got_pack tmp;
1448 ec2b23c5 2022-07-01 thomas do {
1449 ec2b23c5 2022-07-01 thomas i--;
1450 ec2b23c5 2022-07-01 thomas } while (i > 0 && repo->pinned_pack >= 0 &&
1451 ec2b23c5 2022-07-01 thomas i == repo->pinned_pack);
1452 ec2b23c5 2022-07-01 thomas err = got_pack_close(&repo->packs[i]);
1453 1510f469 2018-09-09 stsp if (err)
1454 1510f469 2018-09-09 stsp return err;
1455 ec2b23c5 2022-07-01 thomas if (ftruncate(repo->packs[i].basefd, 0L) == -1)
1456 bfb5ee0b 2022-05-31 thomas return got_error_from_errno("ftruncate");
1457 ec2b23c5 2022-07-01 thomas if (ftruncate(repo->packs[i].accumfd, 0L) == -1)
1458 ec2b23c5 2022-07-01 thomas return got_error_from_errno("ftruncate");
1459 b6b86fd1 2022-08-30 thomas memcpy(&tmp, &repo->packs[i], sizeof(tmp));
1460 ec2b23c5 2022-07-01 thomas memcpy(&repo->packs[i], &repo->packs[0],
1461 ec2b23c5 2022-07-01 thomas sizeof(repo->packs[i]));
1462 93948435 2022-06-13 thomas memcpy(&repo->packs[0], &tmp, sizeof(repo->packs[0]));
1463 ec2b23c5 2022-07-01 thomas if (repo->pinned_pack == 0)
1464 ec2b23c5 2022-07-01 thomas repo->pinned_pack = i;
1465 ec2b23c5 2022-07-01 thomas else if (repo->pinned_pack == i)
1466 ec2b23c5 2022-07-01 thomas repo->pinned_pack = 0;
1467 1510f469 2018-09-09 stsp i = 0;
1468 1510f469 2018-09-09 stsp }
1469 1510f469 2018-09-09 stsp
1470 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1471 1510f469 2018-09-09 stsp
1472 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
1473 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
1474 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1475 1510f469 2018-09-09 stsp goto done;
1476 1510f469 2018-09-09 stsp }
1477 1510f469 2018-09-09 stsp
1478 6d5a9006 2020-12-16 yzhong err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1479 1510f469 2018-09-09 stsp if (err)
1480 1510f469 2018-09-09 stsp goto done;
1481 1510f469 2018-09-09 stsp
1482 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
1483 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
1484 1510f469 2018-09-09 stsp goto done;
1485 ff563a3d 2019-05-23 stsp }
1486 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
1487 90636195 2018-09-11 stsp
1488 90636195 2018-09-11 stsp pack->privsep_child = NULL;
1489 3efd8e31 2022-10-23 thomas
1490 3efd8e31 2022-10-23 thomas err = got_delta_cache_alloc(&pack->delta_cache);
1491 3efd8e31 2022-10-23 thomas if (err)
1492 3efd8e31 2022-10-23 thomas goto done;
1493 1510f469 2018-09-09 stsp
1494 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
1495 aa75acde 2022-10-25 thomas if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1496 aa75acde 2022-10-25 thomas pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1497 aa75acde 2022-10-25 thomas pack->fd, 0);
1498 aa75acde 2022-10-25 thomas if (pack->map == MAP_FAILED) {
1499 aa75acde 2022-10-25 thomas if (errno != ENOMEM) {
1500 aa75acde 2022-10-25 thomas err = got_error_from_errno("mmap");
1501 aa75acde 2022-10-25 thomas goto done;
1502 aa75acde 2022-10-25 thomas }
1503 aa75acde 2022-10-25 thomas pack->map = NULL; /* fall back to read(2) */
1504 3a11398b 2019-02-21 stsp }
1505 3a11398b 2019-02-21 stsp }
1506 1510f469 2018-09-09 stsp #endif
1507 1510f469 2018-09-09 stsp done:
1508 1510f469 2018-09-09 stsp if (err) {
1509 3efd8e31 2022-10-23 thomas if (pack)
1510 3efd8e31 2022-10-23 thomas got_pack_close(pack);
1511 1510f469 2018-09-09 stsp } else if (packp)
1512 1510f469 2018-09-09 stsp *packp = pack;
1513 1510f469 2018-09-09 stsp return err;
1514 1510f469 2018-09-09 stsp }
1515 1510f469 2018-09-09 stsp
1516 1510f469 2018-09-09 stsp struct got_pack *
1517 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1518 1510f469 2018-09-09 stsp {
1519 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1520 16aeacf7 2020-11-26 stsp size_t i;
1521 1510f469 2018-09-09 stsp
1522 6c414261 2021-03-30 stsp for (i = 0; i < repo->pack_cache_size; i++) {
1523 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1524 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1525 1510f469 2018-09-09 stsp break;
1526 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1527 1510f469 2018-09-09 stsp return pack;
1528 ec2b23c5 2022-07-01 thomas }
1529 ec2b23c5 2022-07-01 thomas
1530 ec2b23c5 2022-07-01 thomas return NULL;
1531 ec2b23c5 2022-07-01 thomas }
1532 ec2b23c5 2022-07-01 thomas
1533 ec2b23c5 2022-07-01 thomas const struct got_error *
1534 ec2b23c5 2022-07-01 thomas got_repo_pin_pack(struct got_repository *repo, struct got_packidx *packidx,
1535 ec2b23c5 2022-07-01 thomas struct got_pack *pack)
1536 ec2b23c5 2022-07-01 thomas {
1537 ec2b23c5 2022-07-01 thomas size_t i;
1538 ec2b23c5 2022-07-01 thomas int pinned_pack = -1, pinned_packidx = -1;
1539 ec2b23c5 2022-07-01 thomas
1540 ec2b23c5 2022-07-01 thomas for (i = 0; i < repo->pack_cache_size; i++) {
1541 ec2b23c5 2022-07-01 thomas if (repo->packidx_cache[i] &&
1542 ec2b23c5 2022-07-01 thomas strcmp(repo->packidx_cache[i]->path_packidx,
1543 ec2b23c5 2022-07-01 thomas packidx->path_packidx) == 0)
1544 ec2b23c5 2022-07-01 thomas pinned_packidx = i;
1545 ec2b23c5 2022-07-01 thomas if (repo->packs[i].path_packfile &&
1546 ec2b23c5 2022-07-01 thomas strcmp(repo->packs[i].path_packfile,
1547 ec2b23c5 2022-07-01 thomas pack->path_packfile) == 0)
1548 ec2b23c5 2022-07-01 thomas pinned_pack = i;
1549 2c7829a4 2019-06-17 stsp }
1550 ec2b23c5 2022-07-01 thomas
1551 ec2b23c5 2022-07-01 thomas if (pinned_packidx == -1 || pinned_pack == -1)
1552 ec2b23c5 2022-07-01 thomas return got_error(GOT_ERR_PIN_PACK);
1553 ec2b23c5 2022-07-01 thomas
1554 ec2b23c5 2022-07-01 thomas repo->pinned_pack = pinned_pack;
1555 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = pinned_packidx;
1556 c44c7d6e 2022-12-04 thomas if (repo->packs[pinned_pack].privsep_child)
1557 c44c7d6e 2022-12-04 thomas repo->pinned_pid = repo->packs[pinned_pack].privsep_child->pid;
1558 ec2b23c5 2022-07-01 thomas return NULL;
1559 ec2b23c5 2022-07-01 thomas }
1560 ec2b23c5 2022-07-01 thomas
1561 ec2b23c5 2022-07-01 thomas struct got_pack *
1562 ec2b23c5 2022-07-01 thomas got_repo_get_pinned_pack(struct got_repository *repo)
1563 ec2b23c5 2022-07-01 thomas {
1564 ec2b23c5 2022-07-01 thomas if (repo->pinned_pack >= 0 &&
1565 ec2b23c5 2022-07-01 thomas repo->pinned_pack < repo->pack_cache_size)
1566 ec2b23c5 2022-07-01 thomas return &repo->packs[repo->pinned_pack];
1567 2c7829a4 2019-06-17 stsp
1568 2c7829a4 2019-06-17 stsp return NULL;
1569 ec2b23c5 2022-07-01 thomas }
1570 ec2b23c5 2022-07-01 thomas
1571 ec2b23c5 2022-07-01 thomas void
1572 ec2b23c5 2022-07-01 thomas got_repo_unpin_pack(struct got_repository *repo)
1573 ec2b23c5 2022-07-01 thomas {
1574 ec2b23c5 2022-07-01 thomas repo->pinned_packidx = -1;
1575 ec2b23c5 2022-07-01 thomas repo->pinned_pack = -1;
1576 ec2b23c5 2022-07-01 thomas repo->pinned_pid = 0;
1577 2c7829a4 2019-06-17 stsp }
1578 2c7829a4 2019-06-17 stsp
1579 2c7829a4 2019-06-17 stsp const struct got_error *
1580 e9424ba1 2022-09-20 thomas got_repo_init(const char *repo_path, const char *head_name)
1581 2c7829a4 2019-06-17 stsp {
1582 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
1583 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
1584 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
1585 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
1586 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
1587 2c7829a4 2019-06-17 stsp };
1588 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
1589 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
1590 e9424ba1 2022-09-20 thomas const char *headref = "ref: refs/heads/";
1591 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
1592 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
1593 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
1594 2c7829a4 2019-06-17 stsp "\tbare = true\n";
1595 e9424ba1 2022-09-20 thomas char *headref_str, *path;
1596 16aeacf7 2020-11-26 stsp size_t i;
1597 2c7829a4 2019-06-17 stsp
1598 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
1599 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
1600 2c7829a4 2019-06-17 stsp
1601 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
1602 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1603 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1604 2c7829a4 2019-06-17 stsp }
1605 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
1606 2c7829a4 2019-06-17 stsp free(path);
1607 2c7829a4 2019-06-17 stsp if (err)
1608 2c7829a4 2019-06-17 stsp return err;
1609 1510f469 2018-09-09 stsp }
1610 1510f469 2018-09-09 stsp
1611 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1612 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1613 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
1614 2c7829a4 2019-06-17 stsp free(path);
1615 2c7829a4 2019-06-17 stsp if (err)
1616 2c7829a4 2019-06-17 stsp return err;
1617 2c7829a4 2019-06-17 stsp
1618 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1619 e9424ba1 2022-09-20 thomas return got_error_from_errno("asprintf");
1620 e9424ba1 2022-09-20 thomas if (asprintf(&headref_str, "%s%s", headref,
1621 e9424ba1 2022-09-20 thomas head_name ? head_name : "main") == -1) {
1622 e9424ba1 2022-09-20 thomas free(path);
1623 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1624 e9424ba1 2022-09-20 thomas }
1625 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
1626 e9424ba1 2022-09-20 thomas free(headref_str);
1627 2c7829a4 2019-06-17 stsp free(path);
1628 2c7829a4 2019-06-17 stsp if (err)
1629 2c7829a4 2019-06-17 stsp return err;
1630 2c7829a4 2019-06-17 stsp
1631 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1632 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1633 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
1634 2c7829a4 2019-06-17 stsp free(path);
1635 2c7829a4 2019-06-17 stsp if (err)
1636 2c7829a4 2019-06-17 stsp return err;
1637 2c7829a4 2019-06-17 stsp
1638 1510f469 2018-09-09 stsp return NULL;
1639 04051e8b 2022-09-02 thomas }
1640 04051e8b 2022-09-02 thomas
1641 e09a504c 2019-06-28 stsp static const struct got_error *
1642 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
1643 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
1644 e09a504c 2019-06-28 stsp {
1645 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1646 de47d040 2022-03-22 thomas struct got_object_id_queue matched_ids;
1647 de47d040 2022-03-22 thomas struct got_pathlist_entry *pe;
1648 e09a504c 2019-06-28 stsp
1649 dbdddfee 2021-06-23 naddy STAILQ_INIT(&matched_ids);
1650 dd88155e 2019-06-29 stsp
1651 5b462462 2022-10-22 thomas err = refresh_packidx_paths(repo);
1652 5b462462 2022-10-22 thomas if (err)
1653 5b462462 2022-10-22 thomas return err;
1654 04051e8b 2022-09-02 thomas
1655 de47d040 2022-03-22 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1656 de47d040 2022-03-22 thomas const char *path_packidx = pe->path;
1657 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
1658 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
1659 e09a504c 2019-06-28 stsp
1660 6d5a9006 2020-12-16 yzhong err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1661 6d5a9006 2020-12-16 yzhong path_packidx, 0);
1662 e09a504c 2019-06-28 stsp if (err)
1663 4277420a 2019-06-29 stsp break;
1664 e09a504c 2019-06-28 stsp
1665 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
1666 4277420a 2019-06-29 stsp packidx, id_str_prefix);
1667 4277420a 2019-06-29 stsp if (err) {
1668 4277420a 2019-06-29 stsp got_packidx_close(packidx);
1669 4277420a 2019-06-29 stsp break;
1670 4277420a 2019-06-29 stsp }
1671 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
1672 dd88155e 2019-06-29 stsp if (err)
1673 e09a504c 2019-06-28 stsp break;
1674 e09a504c 2019-06-28 stsp
1675 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &matched_ids, entry) {
1676 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1677 dd88155e 2019-06-29 stsp int matched_type;
1678 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1679 ec242592 2022-04-22 thomas &qid->id);
1680 dd88155e 2019-06-29 stsp if (err)
1681 dd88155e 2019-06-29 stsp goto done;
1682 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1683 dd88155e 2019-06-29 stsp continue;
1684 dd88155e 2019-06-29 stsp }
1685 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
1686 ec242592 2022-04-22 thomas *unique_id = got_object_id_dup(&qid->id);
1687 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
1688 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
1689 dd88155e 2019-06-29 stsp goto done;
1690 dd88155e 2019-06-29 stsp }
1691 4277420a 2019-06-29 stsp } else {
1692 ec242592 2022-04-22 thomas if (got_object_id_cmp(*unique_id,
1693 ec242592 2022-04-22 thomas &qid->id) == 0)
1694 1accf02b 2020-01-05 stsp continue; /* packed multiple times */
1695 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1696 561c3678 2019-07-02 stsp goto done;
1697 e09a504c 2019-06-28 stsp }
1698 e09a504c 2019-06-28 stsp }
1699 e09a504c 2019-06-28 stsp }
1700 e09a504c 2019-06-28 stsp done:
1701 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
1702 e09a504c 2019-06-28 stsp if (err) {
1703 e09a504c 2019-06-28 stsp free(*unique_id);
1704 e09a504c 2019-06-28 stsp *unique_id = NULL;
1705 e09a504c 2019-06-28 stsp }
1706 e09a504c 2019-06-28 stsp return err;
1707 e09a504c 2019-06-28 stsp }
1708 e09a504c 2019-06-28 stsp
1709 e09a504c 2019-06-28 stsp static const struct got_error *
1710 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1711 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
1712 e09a504c 2019-06-28 stsp struct got_repository *repo)
1713 e09a504c 2019-06-28 stsp {
1714 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1715 5ef2cc1c 2022-09-02 thomas char *path, *id_str = NULL;
1716 e09a504c 2019-06-28 stsp DIR *dir = NULL;
1717 e09a504c 2019-06-28 stsp struct dirent *dent;
1718 e09a504c 2019-06-28 stsp struct got_object_id id;
1719 e09a504c 2019-06-28 stsp
1720 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1721 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1722 e09a504c 2019-06-28 stsp goto done;
1723 e09a504c 2019-06-28 stsp }
1724 e09a504c 2019-06-28 stsp
1725 e09a504c 2019-06-28 stsp dir = opendir(path);
1726 e09a504c 2019-06-28 stsp if (dir == NULL) {
1727 4277420a 2019-06-29 stsp if (errno == ENOENT) {
1728 4277420a 2019-06-29 stsp err = NULL;
1729 4277420a 2019-06-29 stsp goto done;
1730 4277420a 2019-06-29 stsp }
1731 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
1732 e09a504c 2019-06-28 stsp goto done;
1733 e09a504c 2019-06-28 stsp }
1734 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
1735 5903ff6e 2019-06-29 stsp int cmp;
1736 5903ff6e 2019-06-29 stsp
1737 5ef2cc1c 2022-09-02 thomas free(id_str);
1738 5ef2cc1c 2022-09-02 thomas id_str = NULL;
1739 5ef2cc1c 2022-09-02 thomas
1740 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
1741 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
1742 e09a504c 2019-06-28 stsp continue;
1743 e09a504c 2019-06-28 stsp
1744 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1745 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1746 e09a504c 2019-06-28 stsp goto done;
1747 e09a504c 2019-06-28 stsp }
1748 e09a504c 2019-06-28 stsp
1749 e09a504c 2019-06-28 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
1750 e09a504c 2019-06-28 stsp continue;
1751 e09a504c 2019-06-28 stsp
1752 52d1d0d9 2019-07-07 stsp /*
1753 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1754 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1755 52d1d0d9 2019-07-07 stsp */
1756 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1757 5ef2cc1c 2022-09-02 thomas if (cmp != 0)
1758 e09a504c 2019-06-28 stsp continue;
1759 e09a504c 2019-06-28 stsp
1760 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1761 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1762 dd88155e 2019-06-29 stsp int matched_type;
1763 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1764 dd88155e 2019-06-29 stsp &id);
1765 dd88155e 2019-06-29 stsp if (err)
1766 dd88155e 2019-06-29 stsp goto done;
1767 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1768 dd88155e 2019-06-29 stsp continue;
1769 dd88155e 2019-06-29 stsp }
1770 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1771 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1772 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1773 e09a504c 2019-06-28 stsp goto done;
1774 e09a504c 2019-06-28 stsp }
1775 e09a504c 2019-06-28 stsp } else {
1776 1accf02b 2020-01-05 stsp if (got_object_id_cmp(*unique_id, &id) == 0)
1777 1accf02b 2020-01-05 stsp continue; /* both packed and loose */
1778 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1779 e09a504c 2019-06-28 stsp goto done;
1780 e09a504c 2019-06-28 stsp }
1781 e09a504c 2019-06-28 stsp }
1782 e09a504c 2019-06-28 stsp done:
1783 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1784 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1785 e09a504c 2019-06-28 stsp if (err) {
1786 e09a504c 2019-06-28 stsp free(*unique_id);
1787 e09a504c 2019-06-28 stsp *unique_id = NULL;
1788 e09a504c 2019-06-28 stsp }
1789 5ef2cc1c 2022-09-02 thomas free(id_str);
1790 e09a504c 2019-06-28 stsp free(path);
1791 e09a504c 2019-06-28 stsp return err;
1792 1510f469 2018-09-09 stsp }
1793 e09a504c 2019-06-28 stsp
1794 e09a504c 2019-06-28 stsp const struct got_error *
1795 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1796 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1797 e09a504c 2019-06-28 stsp {
1798 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1799 66ca6940 2022-09-06 thomas char *path_objects = NULL, *object_dir = NULL;
1800 e09a504c 2019-06-28 stsp size_t len;
1801 4277420a 2019-06-29 stsp int i;
1802 e09a504c 2019-06-28 stsp
1803 4277420a 2019-06-29 stsp *id = NULL;
1804 4277420a 2019-06-29 stsp
1805 66ca6940 2022-09-06 thomas path_objects = got_repo_get_path_objects(repo);
1806 66ca6940 2022-09-06 thomas
1807 70e6418e 2022-03-10 thomas len = strlen(id_str_prefix);
1808 66ca6940 2022-09-06 thomas if (len > SHA1_DIGEST_STRING_LENGTH - 1) {
1809 66ca6940 2022-09-06 thomas err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1810 66ca6940 2022-09-06 thomas goto done;
1811 66ca6940 2022-09-06 thomas }
1812 70e6418e 2022-03-10 thomas
1813 70e6418e 2022-03-10 thomas for (i = 0; i < len; i++) {
1814 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1815 4277420a 2019-06-29 stsp continue;
1816 66ca6940 2022-09-06 thomas err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1817 66ca6940 2022-09-06 thomas goto done;
1818 4277420a 2019-06-29 stsp }
1819 4277420a 2019-06-29 stsp
1820 e09a504c 2019-06-28 stsp if (len >= 2) {
1821 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1822 4277420a 2019-06-29 stsp if (err)
1823 83c8b3b8 2019-06-29 stsp goto done;
1824 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1825 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1826 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1827 83c8b3b8 2019-06-29 stsp goto done;
1828 83c8b3b8 2019-06-29 stsp }
1829 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1830 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1831 e09a504c 2019-06-28 stsp } else if (len == 1) {
1832 e09a504c 2019-06-28 stsp int i;
1833 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1834 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1835 83c8b3b8 2019-06-29 stsp == -1) {
1836 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1837 83c8b3b8 2019-06-29 stsp goto done;
1838 83c8b3b8 2019-06-29 stsp }
1839 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1840 dd88155e 2019-06-29 stsp obj_type);
1841 4277420a 2019-06-29 stsp if (err)
1842 83c8b3b8 2019-06-29 stsp goto done;
1843 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1844 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1845 e09a504c 2019-06-28 stsp if (err)
1846 83c8b3b8 2019-06-29 stsp goto done;
1847 e09a504c 2019-06-28 stsp }
1848 83c8b3b8 2019-06-29 stsp } else {
1849 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1850 83c8b3b8 2019-06-29 stsp goto done;
1851 83c8b3b8 2019-06-29 stsp }
1852 83c8b3b8 2019-06-29 stsp done:
1853 66ca6940 2022-09-06 thomas free(path_objects);
1854 e09a504c 2019-06-28 stsp free(object_dir);
1855 4277420a 2019-06-29 stsp if (err) {
1856 4277420a 2019-06-29 stsp free(*id);
1857 4277420a 2019-06-29 stsp *id = NULL;
1858 a3599220 2021-10-10 thomas } else if (*id == NULL) {
1859 a3599220 2021-10-10 thomas switch (obj_type) {
1860 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_BLOB:
1861 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1862 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_BLOB, id_str_prefix);
1863 a3599220 2021-10-10 thomas break;
1864 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_TREE:
1865 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1866 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_TREE, id_str_prefix);
1867 a3599220 2021-10-10 thomas break;
1868 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_COMMIT:
1869 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1870 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_COMMIT, id_str_prefix);
1871 a3599220 2021-10-10 thomas break;
1872 a3599220 2021-10-10 thomas case GOT_OBJ_TYPE_TAG:
1873 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1874 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_TAG, id_str_prefix);
1875 a3599220 2021-10-10 thomas break;
1876 a3599220 2021-10-10 thomas default:
1877 a3599220 2021-10-10 thomas err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1878 a3599220 2021-10-10 thomas break;
1879 a3599220 2021-10-10 thomas }
1880 a3599220 2021-10-10 thomas }
1881 303e2782 2019-08-09 stsp
1882 303e2782 2019-08-09 stsp return err;
1883 303e2782 2019-08-09 stsp }
1884 303e2782 2019-08-09 stsp
1885 303e2782 2019-08-09 stsp const struct got_error *
1886 71a27632 2020-01-15 stsp got_repo_match_object_id(struct got_object_id **id, char **label,
1887 84de9106 2020-12-26 stsp const char *id_str, int obj_type, struct got_reflist_head *refs,
1888 71a27632 2020-01-15 stsp struct got_repository *repo)
1889 71a27632 2020-01-15 stsp {
1890 71a27632 2020-01-15 stsp const struct got_error *err;
1891 71a27632 2020-01-15 stsp struct got_tag_object *tag;
1892 71a27632 2020-01-15 stsp struct got_reference *ref = NULL;
1893 71a27632 2020-01-15 stsp
1894 71a27632 2020-01-15 stsp *id = NULL;
1895 71a27632 2020-01-15 stsp if (label)
1896 71a27632 2020-01-15 stsp *label = NULL;
1897 71a27632 2020-01-15 stsp
1898 84de9106 2020-12-26 stsp if (refs) {
1899 f1165c79 2021-10-10 thomas err = got_repo_object_match_tag(&tag, id_str, obj_type,
1900 84de9106 2020-12-26 stsp refs, repo);
1901 71a27632 2020-01-15 stsp if (err == NULL) {
1902 71a27632 2020-01-15 stsp *id = got_object_id_dup(
1903 71a27632 2020-01-15 stsp got_object_tag_get_object_id(tag));
1904 71a27632 2020-01-15 stsp if (*id == NULL)
1905 71a27632 2020-01-15 stsp err = got_error_from_errno("got_object_id_dup");
1906 71a27632 2020-01-15 stsp else if (label && asprintf(label, "refs/tags/%s",
1907 71a27632 2020-01-15 stsp got_object_tag_get_name(tag)) == -1) {
1908 71a27632 2020-01-15 stsp err = got_error_from_errno("asprintf");
1909 71a27632 2020-01-15 stsp free(*id);
1910 71a27632 2020-01-15 stsp *id = NULL;
1911 71a27632 2020-01-15 stsp }
1912 71a27632 2020-01-15 stsp got_object_tag_close(tag);
1913 71a27632 2020-01-15 stsp return err;
1914 71a27632 2020-01-15 stsp } else if (err->code != GOT_ERR_OBJ_TYPE &&
1915 71a27632 2020-01-15 stsp err->code != GOT_ERR_NO_OBJ)
1916 71a27632 2020-01-15 stsp return err;
1917 71a27632 2020-01-15 stsp }
1918 71a27632 2020-01-15 stsp
1919 65ad15fa 2022-07-16 thomas err = got_ref_open(&ref, repo, id_str, 0);
1920 65ad15fa 2022-07-16 thomas if (err == NULL) {
1921 65ad15fa 2022-07-16 thomas err = got_ref_resolve(id, repo, ref);
1922 65ad15fa 2022-07-16 thomas if (err)
1923 71a27632 2020-01-15 stsp goto done;
1924 71a27632 2020-01-15 stsp if (label) {
1925 71a27632 2020-01-15 stsp *label = strdup(got_ref_get_name(ref));
1926 71a27632 2020-01-15 stsp if (*label == NULL) {
1927 71a27632 2020-01-15 stsp err = got_error_from_errno("strdup");
1928 71a27632 2020-01-15 stsp goto done;
1929 71a27632 2020-01-15 stsp }
1930 71a27632 2020-01-15 stsp }
1931 65ad15fa 2022-07-16 thomas } else {
1932 65ad15fa 2022-07-16 thomas if (err->code != GOT_ERR_NOT_REF &&
1933 65ad15fa 2022-07-16 thomas err->code != GOT_ERR_BAD_REF_NAME)
1934 71a27632 2020-01-15 stsp goto done;
1935 65ad15fa 2022-07-16 thomas err = got_repo_match_object_id_prefix(id, id_str,
1936 65ad15fa 2022-07-16 thomas obj_type, repo);
1937 65ad15fa 2022-07-16 thomas if (err) {
1938 65ad15fa 2022-07-16 thomas if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
1939 65ad15fa 2022-07-16 thomas err = got_error_not_ref(id_str);
1940 65ad15fa 2022-07-16 thomas goto done;
1941 71a27632 2020-01-15 stsp }
1942 65ad15fa 2022-07-16 thomas if (label) {
1943 65ad15fa 2022-07-16 thomas err = got_object_id_str(label, *id);
1944 65ad15fa 2022-07-16 thomas if (*label == NULL) {
1945 65ad15fa 2022-07-16 thomas err = got_error_from_errno("strdup");
1946 65ad15fa 2022-07-16 thomas goto done;
1947 65ad15fa 2022-07-16 thomas }
1948 65ad15fa 2022-07-16 thomas }
1949 71a27632 2020-01-15 stsp }
1950 71a27632 2020-01-15 stsp done:
1951 71a27632 2020-01-15 stsp if (ref)
1952 71a27632 2020-01-15 stsp got_ref_close(ref);
1953 71a27632 2020-01-15 stsp return err;
1954 71a27632 2020-01-15 stsp }
1955 71a27632 2020-01-15 stsp
1956 71a27632 2020-01-15 stsp const struct got_error *
1957 303e2782 2019-08-09 stsp got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1958 84de9106 2020-12-26 stsp int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1959 303e2782 2019-08-09 stsp {
1960 84de9106 2020-12-26 stsp const struct got_error *err = NULL;
1961 303e2782 2019-08-09 stsp struct got_reflist_entry *re;
1962 303e2782 2019-08-09 stsp struct got_object_id *tag_id;
1963 785d65a4 2020-12-05 stsp int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1964 303e2782 2019-08-09 stsp
1965 303e2782 2019-08-09 stsp *tag = NULL;
1966 303e2782 2019-08-09 stsp
1967 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1968 303e2782 2019-08-09 stsp const char *refname;
1969 303e2782 2019-08-09 stsp refname = got_ref_get_name(re->ref);
1970 29606af7 2019-08-23 stsp if (got_ref_is_symbolic(re->ref))
1971 303e2782 2019-08-09 stsp continue;
1972 84de9106 2020-12-26 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
1973 84de9106 2020-12-26 stsp continue;
1974 785d65a4 2020-12-05 stsp if (!name_is_absolute)
1975 785d65a4 2020-12-05 stsp refname += strlen("refs/tags/");
1976 303e2782 2019-08-09 stsp if (strcmp(refname, name) != 0)
1977 303e2782 2019-08-09 stsp continue;
1978 303e2782 2019-08-09 stsp err = got_ref_resolve(&tag_id, repo, re->ref);
1979 303e2782 2019-08-09 stsp if (err)
1980 303e2782 2019-08-09 stsp break;
1981 303e2782 2019-08-09 stsp err = got_object_open_as_tag(tag, repo, tag_id);
1982 303e2782 2019-08-09 stsp free(tag_id);
1983 303e2782 2019-08-09 stsp if (err)
1984 303e2782 2019-08-09 stsp break;
1985 d24820bf 2019-08-11 stsp if (obj_type == GOT_OBJ_TYPE_ANY ||
1986 d24820bf 2019-08-11 stsp got_object_tag_get_object_type(*tag) == obj_type)
1987 303e2782 2019-08-09 stsp break;
1988 303e2782 2019-08-09 stsp got_object_tag_close(*tag);
1989 303e2782 2019-08-09 stsp *tag = NULL;
1990 303e2782 2019-08-09 stsp }
1991 4277420a 2019-06-29 stsp
1992 303e2782 2019-08-09 stsp if (err == NULL && *tag == NULL)
1993 a3599220 2021-10-10 thomas err = got_error_fmt(GOT_ERR_NO_OBJ, "%s %s",
1994 a3599220 2021-10-10 thomas GOT_OBJ_LABEL_TAG, name);
1995 e09a504c 2019-06-28 stsp return err;
1996 e09a504c 2019-06-28 stsp }
1997 7a1d6b72 2020-01-15 stsp
1998 3ce1b845 2019-07-15 stsp static const struct got_error *
1999 3ce1b845 2019-07-15 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2000 3ce1b845 2019-07-15 stsp const char *name, mode_t mode, struct got_object_id *blob_id)
2001 3ce1b845 2019-07-15 stsp {
2002 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2003 3ce1b845 2019-07-15 stsp
2004 3ce1b845 2019-07-15 stsp *new_te = NULL;
2005 3ce1b845 2019-07-15 stsp
2006 3ce1b845 2019-07-15 stsp *new_te = calloc(1, sizeof(**new_te));
2007 3ce1b845 2019-07-15 stsp if (*new_te == NULL)
2008 3ce1b845 2019-07-15 stsp return got_error_from_errno("calloc");
2009 3ce1b845 2019-07-15 stsp
2010 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
2011 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
2012 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
2013 3ce1b845 2019-07-15 stsp goto done;
2014 3ce1b845 2019-07-15 stsp }
2015 3ce1b845 2019-07-15 stsp
2016 e8863bdc 2020-07-23 stsp if (S_ISLNK(mode)) {
2017 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFLNK;
2018 e8863bdc 2020-07-23 stsp } else {
2019 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFREG;
2020 e8863bdc 2020-07-23 stsp (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
2021 e8863bdc 2020-07-23 stsp }
2022 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
2023 3ce1b845 2019-07-15 stsp done:
2024 3ce1b845 2019-07-15 stsp if (err && *new_te) {
2025 56e0773d 2019-11-28 stsp free(*new_te);
2026 3ce1b845 2019-07-15 stsp *new_te = NULL;
2027 3ce1b845 2019-07-15 stsp }
2028 3ce1b845 2019-07-15 stsp return err;
2029 3ce1b845 2019-07-15 stsp }
2030 3ce1b845 2019-07-15 stsp
2031 3ce1b845 2019-07-15 stsp static const struct got_error *
2032 3ce1b845 2019-07-15 stsp import_file(struct got_tree_entry **new_te, struct dirent *de,
2033 3ce1b845 2019-07-15 stsp const char *path, struct got_repository *repo)
2034 3ce1b845 2019-07-15 stsp {
2035 3ce1b845 2019-07-15 stsp const struct got_error *err;
2036 3ce1b845 2019-07-15 stsp struct got_object_id *blob_id = NULL;
2037 3ce1b845 2019-07-15 stsp char *filepath;
2038 3ce1b845 2019-07-15 stsp struct stat sb;
2039 3ce1b845 2019-07-15 stsp
2040 3ce1b845 2019-07-15 stsp if (asprintf(&filepath, "%s%s%s", path,
2041 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
2042 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
2043 3ce1b845 2019-07-15 stsp
2044 3ce1b845 2019-07-15 stsp if (lstat(filepath, &sb) != 0) {
2045 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("lstat", path);
2046 3ce1b845 2019-07-15 stsp goto done;
2047 3ce1b845 2019-07-15 stsp }
2048 3ce1b845 2019-07-15 stsp
2049 3ce1b845 2019-07-15 stsp err = got_object_blob_create(&blob_id, filepath, repo);
2050 3ce1b845 2019-07-15 stsp if (err)
2051 3ce1b845 2019-07-15 stsp goto done;
2052 3ce1b845 2019-07-15 stsp
2053 3ce1b845 2019-07-15 stsp err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
2054 3ce1b845 2019-07-15 stsp blob_id);
2055 3ce1b845 2019-07-15 stsp done:
2056 3ce1b845 2019-07-15 stsp free(filepath);
2057 3ce1b845 2019-07-15 stsp if (err)
2058 3ce1b845 2019-07-15 stsp free(blob_id);
2059 3ce1b845 2019-07-15 stsp return err;
2060 3ce1b845 2019-07-15 stsp }
2061 3ce1b845 2019-07-15 stsp
2062 3ce1b845 2019-07-15 stsp static const struct got_error *
2063 3ce1b845 2019-07-15 stsp insert_tree_entry(struct got_tree_entry *new_te,
2064 3ce1b845 2019-07-15 stsp struct got_pathlist_head *paths)
2065 3ce1b845 2019-07-15 stsp {
2066 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2067 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *new_pe;
2068 3ce1b845 2019-07-15 stsp
2069 3ce1b845 2019-07-15 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2070 3ce1b845 2019-07-15 stsp if (err)
2071 3ce1b845 2019-07-15 stsp return err;
2072 3ce1b845 2019-07-15 stsp if (new_pe == NULL)
2073 3ce1b845 2019-07-15 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2074 3ce1b845 2019-07-15 stsp return NULL;
2075 3ce1b845 2019-07-15 stsp }
2076 3ce1b845 2019-07-15 stsp
2077 3ce1b845 2019-07-15 stsp static const struct got_error *write_tree(struct got_object_id **,
2078 3ce1b845 2019-07-15 stsp const char *, struct got_pathlist_head *, struct got_repository *,
2079 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg);
2080 3ce1b845 2019-07-15 stsp
2081 3ce1b845 2019-07-15 stsp static const struct got_error *
2082 3ce1b845 2019-07-15 stsp import_subdir(struct got_tree_entry **new_te, struct dirent *de,
2083 3ce1b845 2019-07-15 stsp const char *path, struct got_pathlist_head *ignores,
2084 3ce1b845 2019-07-15 stsp struct got_repository *repo,
2085 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
2086 3ce1b845 2019-07-15 stsp {
2087 3ce1b845 2019-07-15 stsp const struct got_error *err;
2088 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
2089 3ce1b845 2019-07-15 stsp char *subdirpath;
2090 3ce1b845 2019-07-15 stsp
2091 3ce1b845 2019-07-15 stsp if (asprintf(&subdirpath, "%s%s%s", path,
2092 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
2093 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
2094 3ce1b845 2019-07-15 stsp
2095 3ce1b845 2019-07-15 stsp (*new_te) = calloc(1, sizeof(**new_te));
2096 d6fca0ba 2019-09-15 hiltjo if (*new_te == NULL)
2097 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
2098 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFDIR;
2099 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
2100 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
2101 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
2102 3ce1b845 2019-07-15 stsp goto done;
2103 3ce1b845 2019-07-15 stsp }
2104 56e0773d 2019-11-28 stsp err = write_tree(&id, subdirpath, ignores, repo,
2105 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
2106 56e0773d 2019-11-28 stsp if (err)
2107 56e0773d 2019-11-28 stsp goto done;
2108 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
2109 b6b86fd1 2022-08-30 thomas
2110 3ce1b845 2019-07-15 stsp done:
2111 56e0773d 2019-11-28 stsp free(id);
2112 3ce1b845 2019-07-15 stsp free(subdirpath);
2113 3ce1b845 2019-07-15 stsp if (err) {
2114 56e0773d 2019-11-28 stsp free(*new_te);
2115 3ce1b845 2019-07-15 stsp *new_te = NULL;
2116 3ce1b845 2019-07-15 stsp }
2117 3ce1b845 2019-07-15 stsp return err;
2118 3ce1b845 2019-07-15 stsp }
2119 3ce1b845 2019-07-15 stsp
2120 3ce1b845 2019-07-15 stsp static const struct got_error *
2121 3ce1b845 2019-07-15 stsp write_tree(struct got_object_id **new_tree_id, const char *path_dir,
2122 3ce1b845 2019-07-15 stsp struct got_pathlist_head *ignores, struct got_repository *repo,
2123 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
2124 3ce1b845 2019-07-15 stsp {
2125 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
2126 3ce1b845 2019-07-15 stsp DIR *dir;
2127 3ce1b845 2019-07-15 stsp struct dirent *de;
2128 56e0773d 2019-11-28 stsp int nentries;
2129 3ce1b845 2019-07-15 stsp struct got_tree_entry *new_te = NULL;
2130 3ce1b845 2019-07-15 stsp struct got_pathlist_head paths;
2131 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
2132 3ce1b845 2019-07-15 stsp
2133 3ce1b845 2019-07-15 stsp *new_tree_id = NULL;
2134 3ce1b845 2019-07-15 stsp
2135 3ce1b845 2019-07-15 stsp TAILQ_INIT(&paths);
2136 3ce1b845 2019-07-15 stsp
2137 3ce1b845 2019-07-15 stsp dir = opendir(path_dir);
2138 3ce1b845 2019-07-15 stsp if (dir == NULL) {
2139 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("opendir", path_dir);
2140 3ce1b845 2019-07-15 stsp goto done;
2141 3ce1b845 2019-07-15 stsp }
2142 3ce1b845 2019-07-15 stsp
2143 56e0773d 2019-11-28 stsp nentries = 0;
2144 3ce1b845 2019-07-15 stsp while ((de = readdir(dir)) != NULL) {
2145 3ce1b845 2019-07-15 stsp int ignore = 0;
2146 20ccae39 2020-07-21 stsp int type;
2147 3ce1b845 2019-07-15 stsp
2148 3ce1b845 2019-07-15 stsp if (strcmp(de->d_name, ".") == 0 ||
2149 3ce1b845 2019-07-15 stsp strcmp(de->d_name, "..") == 0)
2150 3ce1b845 2019-07-15 stsp continue;
2151 e012cf94 2023-02-07 thomas
2152 e012cf94 2023-02-07 thomas err = got_path_dirent_type(&type, path_dir, de);
2153 e012cf94 2023-02-07 thomas if (err)
2154 e012cf94 2023-02-07 thomas goto done;
2155 3ce1b845 2019-07-15 stsp
2156 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, ignores, entry) {
2157 e012cf94 2023-02-07 thomas if (type == DT_DIR && pe->path_len > 0 &&
2158 e012cf94 2023-02-07 thomas pe->path[pe->path_len - 1] == '/') {
2159 e012cf94 2023-02-07 thomas char stripped[PATH_MAX];
2160 e012cf94 2023-02-07 thomas
2161 e012cf94 2023-02-07 thomas if (strlcpy(stripped, pe->path,
2162 e012cf94 2023-02-07 thomas sizeof(stripped)) >= sizeof(stripped)) {
2163 e012cf94 2023-02-07 thomas err = got_error(GOT_ERR_NO_SPACE);
2164 e012cf94 2023-02-07 thomas goto done;
2165 e012cf94 2023-02-07 thomas }
2166 e012cf94 2023-02-07 thomas got_path_strip_trailing_slashes(stripped);
2167 e012cf94 2023-02-07 thomas if (fnmatch(stripped, de->d_name, 0) == 0) {
2168 e012cf94 2023-02-07 thomas ignore = 1;
2169 e012cf94 2023-02-07 thomas break;
2170 e012cf94 2023-02-07 thomas }
2171 e012cf94 2023-02-07 thomas } else if (fnmatch(pe->path, de->d_name, 0) == 0) {
2172 3ce1b845 2019-07-15 stsp ignore = 1;
2173 3ce1b845 2019-07-15 stsp break;
2174 3ce1b845 2019-07-15 stsp }
2175 3ce1b845 2019-07-15 stsp }
2176 3ce1b845 2019-07-15 stsp if (ignore)
2177 3ce1b845 2019-07-15 stsp continue;
2178 20ccae39 2020-07-21 stsp
2179 20ccae39 2020-07-21 stsp if (type == DT_DIR) {
2180 3ce1b845 2019-07-15 stsp err = import_subdir(&new_te, de, path_dir,
2181 3ce1b845 2019-07-15 stsp ignores, repo, progress_cb, progress_arg);
2182 db1d3576 2019-10-04 stsp if (err) {
2183 db1d3576 2019-10-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
2184 db1d3576 2019-10-04 stsp goto done;
2185 db1d3576 2019-10-04 stsp err = NULL;
2186 db1d3576 2019-10-04 stsp continue;
2187 db1d3576 2019-10-04 stsp }
2188 e8863bdc 2020-07-23 stsp } else if (type == DT_REG || type == DT_LNK) {
2189 3ce1b845 2019-07-15 stsp err = import_file(&new_te, de, path_dir, repo);
2190 3ce1b845 2019-07-15 stsp if (err)
2191 3ce1b845 2019-07-15 stsp goto done;
2192 3ce1b845 2019-07-15 stsp } else
2193 3ce1b845 2019-07-15 stsp continue;
2194 3ce1b845 2019-07-15 stsp
2195 3ce1b845 2019-07-15 stsp err = insert_tree_entry(new_te, &paths);
2196 3ce1b845 2019-07-15 stsp if (err)
2197 3ce1b845 2019-07-15 stsp goto done;
2198 56e0773d 2019-11-28 stsp nentries++;
2199 3ce1b845 2019-07-15 stsp }
2200 3ce1b845 2019-07-15 stsp
2201 db1d3576 2019-10-04 stsp if (TAILQ_EMPTY(&paths)) {
2202 b66cd6f3 2020-07-31 stsp err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
2203 b66cd6f3 2020-07-31 stsp "cannot create tree without any entries");
2204 db1d3576 2019-10-04 stsp goto done;
2205 db1d3576 2019-10-04 stsp }
2206 db1d3576 2019-10-04 stsp
2207 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, &paths, entry) {
2208 3ce1b845 2019-07-15 stsp struct got_tree_entry *te = pe->data;
2209 3ce1b845 2019-07-15 stsp char *path;
2210 e8863bdc 2020-07-23 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
2211 3ce1b845 2019-07-15 stsp continue;
2212 3ce1b845 2019-07-15 stsp if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
2213 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
2214 3ce1b845 2019-07-15 stsp goto done;
2215 3ce1b845 2019-07-15 stsp }
2216 3ce1b845 2019-07-15 stsp err = (*progress_cb)(progress_arg, path);
2217 3ce1b845 2019-07-15 stsp free(path);
2218 3ce1b845 2019-07-15 stsp if (err)
2219 3ce1b845 2019-07-15 stsp goto done;
2220 3ce1b845 2019-07-15 stsp }
2221 3ce1b845 2019-07-15 stsp
2222 56e0773d 2019-11-28 stsp err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
2223 3ce1b845 2019-07-15 stsp done:
2224 3ce1b845 2019-07-15 stsp if (dir)
2225 3ce1b845 2019-07-15 stsp closedir(dir);
2226 21c2d8be 2023-01-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2227 3ce1b845 2019-07-15 stsp return err;
2228 3ce1b845 2019-07-15 stsp }
2229 3ce1b845 2019-07-15 stsp
2230 3ce1b845 2019-07-15 stsp const struct got_error *
2231 3ce1b845 2019-07-15 stsp got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
2232 3ce1b845 2019-07-15 stsp const char *logmsg, const char *author, struct got_pathlist_head *ignores,
2233 3ce1b845 2019-07-15 stsp struct got_repository *repo, got_repo_import_cb progress_cb,
2234 3ce1b845 2019-07-15 stsp void *progress_arg)
2235 3ce1b845 2019-07-15 stsp {
2236 3ce1b845 2019-07-15 stsp const struct got_error *err;
2237 3ce1b845 2019-07-15 stsp struct got_object_id *new_tree_id;
2238 3ce1b845 2019-07-15 stsp
2239 3ce1b845 2019-07-15 stsp err = write_tree(&new_tree_id, path_dir, ignores, repo,
2240 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
2241 3ce1b845 2019-07-15 stsp if (err)
2242 3ce1b845 2019-07-15 stsp return err;
2243 3ce1b845 2019-07-15 stsp
2244 3ce1b845 2019-07-15 stsp err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
2245 3ce1b845 2019-07-15 stsp author, time(NULL), author, time(NULL), logmsg, repo);
2246 3ce1b845 2019-07-15 stsp free(new_tree_id);
2247 20662ea0 2021-04-10 stsp return err;
2248 20662ea0 2021-04-10 stsp }
2249 20662ea0 2021-04-10 stsp
2250 20662ea0 2021-04-10 stsp const struct got_error *
2251 20662ea0 2021-04-10 stsp got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
2252 20662ea0 2021-04-10 stsp struct got_repository *repo)
2253 20662ea0 2021-04-10 stsp {
2254 20662ea0 2021-04-10 stsp const struct got_error *err = NULL;
2255 20662ea0 2021-04-10 stsp char *path_objects = NULL, *path = NULL;
2256 20662ea0 2021-04-10 stsp DIR *dir = NULL;
2257 20662ea0 2021-04-10 stsp struct got_object_id id;
2258 20662ea0 2021-04-10 stsp int i;
2259 20662ea0 2021-04-10 stsp
2260 20662ea0 2021-04-10 stsp *nobjects = 0;
2261 20662ea0 2021-04-10 stsp *ondisk_size = 0;
2262 20662ea0 2021-04-10 stsp
2263 20662ea0 2021-04-10 stsp path_objects = got_repo_get_path_objects(repo);
2264 20662ea0 2021-04-10 stsp if (path_objects == NULL)
2265 20662ea0 2021-04-10 stsp return got_error_from_errno("got_repo_get_path_objects");
2266 20662ea0 2021-04-10 stsp
2267 20662ea0 2021-04-10 stsp for (i = 0; i <= 0xff; i++) {
2268 20662ea0 2021-04-10 stsp struct dirent *dent;
2269 20662ea0 2021-04-10 stsp
2270 20662ea0 2021-04-10 stsp if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
2271 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2272 20662ea0 2021-04-10 stsp break;
2273 20662ea0 2021-04-10 stsp }
2274 20662ea0 2021-04-10 stsp
2275 20662ea0 2021-04-10 stsp dir = opendir(path);
2276 20662ea0 2021-04-10 stsp if (dir == NULL) {
2277 20662ea0 2021-04-10 stsp if (errno == ENOENT) {
2278 20662ea0 2021-04-10 stsp err = NULL;
2279 20662ea0 2021-04-10 stsp continue;
2280 20662ea0 2021-04-10 stsp }
2281 20662ea0 2021-04-10 stsp err = got_error_from_errno2("opendir", path);
2282 20662ea0 2021-04-10 stsp break;
2283 20662ea0 2021-04-10 stsp }
2284 20662ea0 2021-04-10 stsp
2285 20662ea0 2021-04-10 stsp while ((dent = readdir(dir)) != NULL) {
2286 20662ea0 2021-04-10 stsp char *id_str;
2287 20662ea0 2021-04-10 stsp int fd;
2288 20662ea0 2021-04-10 stsp struct stat sb;
2289 20662ea0 2021-04-10 stsp
2290 20662ea0 2021-04-10 stsp if (strcmp(dent->d_name, ".") == 0 ||
2291 20662ea0 2021-04-10 stsp strcmp(dent->d_name, "..") == 0)
2292 20662ea0 2021-04-10 stsp continue;
2293 20662ea0 2021-04-10 stsp
2294 20662ea0 2021-04-10 stsp if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
2295 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2296 20662ea0 2021-04-10 stsp goto done;
2297 20662ea0 2021-04-10 stsp }
2298 20662ea0 2021-04-10 stsp
2299 20662ea0 2021-04-10 stsp if (!got_parse_sha1_digest(id.sha1, id_str)) {
2300 20662ea0 2021-04-10 stsp free(id_str);
2301 20662ea0 2021-04-10 stsp continue;
2302 20662ea0 2021-04-10 stsp }
2303 20662ea0 2021-04-10 stsp free(id_str);
2304 20662ea0 2021-04-10 stsp
2305 20662ea0 2021-04-10 stsp err = got_object_open_loose_fd(&fd, &id, repo);
2306 20662ea0 2021-04-10 stsp if (err)
2307 20662ea0 2021-04-10 stsp goto done;
2308 20662ea0 2021-04-10 stsp
2309 20662ea0 2021-04-10 stsp if (fstat(fd, &sb) == -1) {
2310 20662ea0 2021-04-10 stsp err = got_error_from_errno("fstat");
2311 20662ea0 2021-04-10 stsp close(fd);
2312 20662ea0 2021-04-10 stsp goto done;
2313 20662ea0 2021-04-10 stsp }
2314 20662ea0 2021-04-10 stsp (*nobjects)++;
2315 20662ea0 2021-04-10 stsp (*ondisk_size) += sb.st_size;
2316 20662ea0 2021-04-10 stsp
2317 20662ea0 2021-04-10 stsp if (close(fd) == -1) {
2318 20662ea0 2021-04-10 stsp err = got_error_from_errno("close");
2319 20662ea0 2021-04-10 stsp goto done;
2320 20662ea0 2021-04-10 stsp }
2321 20662ea0 2021-04-10 stsp }
2322 20662ea0 2021-04-10 stsp
2323 20662ea0 2021-04-10 stsp if (closedir(dir) != 0) {
2324 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2325 20662ea0 2021-04-10 stsp goto done;
2326 20662ea0 2021-04-10 stsp }
2327 20662ea0 2021-04-10 stsp dir = NULL;
2328 20662ea0 2021-04-10 stsp
2329 20662ea0 2021-04-10 stsp free(path);
2330 20662ea0 2021-04-10 stsp path = NULL;
2331 20662ea0 2021-04-10 stsp }
2332 20662ea0 2021-04-10 stsp done:
2333 20662ea0 2021-04-10 stsp if (dir && closedir(dir) != 0 && err == NULL)
2334 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2335 20662ea0 2021-04-10 stsp
2336 20662ea0 2021-04-10 stsp if (err) {
2337 20662ea0 2021-04-10 stsp *nobjects = 0;
2338 20662ea0 2021-04-10 stsp *ondisk_size = 0;
2339 20662ea0 2021-04-10 stsp }
2340 20662ea0 2021-04-10 stsp free(path_objects);
2341 20662ea0 2021-04-10 stsp free(path);
2342 3ce1b845 2019-07-15 stsp return err;
2343 3ce1b845 2019-07-15 stsp }
2344 20662ea0 2021-04-10 stsp
2345 20662ea0 2021-04-10 stsp const struct got_error *
2346 20662ea0 2021-04-10 stsp got_repo_get_packfile_info(int *npackfiles, int *nobjects,
2347 20662ea0 2021-04-10 stsp off_t *total_packsize, struct got_repository *repo)
2348 20662ea0 2021-04-10 stsp {
2349 0c9eeee2 2021-06-05 stsp const struct got_error *err = NULL;
2350 20662ea0 2021-04-10 stsp DIR *packdir = NULL;
2351 20662ea0 2021-04-10 stsp struct dirent *dent;
2352 20662ea0 2021-04-10 stsp struct got_packidx *packidx = NULL;
2353 20662ea0 2021-04-10 stsp char *path_packidx;
2354 20662ea0 2021-04-10 stsp char *path_packfile;
2355 20662ea0 2021-04-10 stsp int packdir_fd;
2356 20662ea0 2021-04-10 stsp struct stat sb;
2357 20662ea0 2021-04-10 stsp
2358 20662ea0 2021-04-10 stsp *npackfiles = 0;
2359 20662ea0 2021-04-10 stsp *nobjects = 0;
2360 20662ea0 2021-04-10 stsp *total_packsize = 0;
2361 20662ea0 2021-04-10 stsp
2362 20662ea0 2021-04-10 stsp packdir_fd = openat(got_repo_get_fd(repo),
2363 20662ea0 2021-04-10 stsp GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
2364 20662ea0 2021-04-10 stsp if (packdir_fd == -1) {
2365 20662ea0 2021-04-10 stsp return got_error_from_errno_fmt("openat: %s/%s",
2366 20662ea0 2021-04-10 stsp got_repo_get_path_git_dir(repo),
2367 20662ea0 2021-04-10 stsp GOT_OBJECTS_PACK_DIR);
2368 20662ea0 2021-04-10 stsp }
2369 20662ea0 2021-04-10 stsp
2370 20662ea0 2021-04-10 stsp packdir = fdopendir(packdir_fd);
2371 20662ea0 2021-04-10 stsp if (packdir == NULL) {
2372 20662ea0 2021-04-10 stsp err = got_error_from_errno("fdopendir");
2373 20662ea0 2021-04-10 stsp goto done;
2374 20662ea0 2021-04-10 stsp }
2375 20662ea0 2021-04-10 stsp
2376 20662ea0 2021-04-10 stsp while ((dent = readdir(packdir)) != NULL) {
2377 dd038bc6 2021-09-21 thomas.ad if (!got_repo_is_packidx_filename(dent->d_name,
2378 dd038bc6 2021-09-21 thomas.ad strlen(dent->d_name)))
2379 20662ea0 2021-04-10 stsp continue;
2380 20662ea0 2021-04-10 stsp
2381 20662ea0 2021-04-10 stsp if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
2382 20662ea0 2021-04-10 stsp dent->d_name) == -1) {
2383 20662ea0 2021-04-10 stsp err = got_error_from_errno("asprintf");
2384 20662ea0 2021-04-10 stsp goto done;
2385 20662ea0 2021-04-10 stsp }
2386 20662ea0 2021-04-10 stsp
2387 20662ea0 2021-04-10 stsp err = got_packidx_open(&packidx, got_repo_get_fd(repo),
2388 20662ea0 2021-04-10 stsp path_packidx, 0);
2389 20662ea0 2021-04-10 stsp free(path_packidx);
2390 20662ea0 2021-04-10 stsp if (err)
2391 20662ea0 2021-04-10 stsp goto done;
2392 20662ea0 2021-04-10 stsp
2393 20662ea0 2021-04-10 stsp if (fstat(packidx->fd, &sb) == -1)
2394 20662ea0 2021-04-10 stsp goto done;
2395 20662ea0 2021-04-10 stsp *total_packsize += sb.st_size;
2396 20662ea0 2021-04-10 stsp
2397 aea75d87 2021-07-06 stsp err = got_packidx_get_packfile_path(&path_packfile,
2398 aea75d87 2021-07-06 stsp packidx->path_packidx);
2399 20662ea0 2021-04-10 stsp if (err)
2400 20662ea0 2021-04-10 stsp goto done;
2401 20662ea0 2021-04-10 stsp
2402 20662ea0 2021-04-10 stsp if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
2403 20662ea0 2021-04-10 stsp 0) == -1) {
2404 20662ea0 2021-04-10 stsp free(path_packfile);
2405 20662ea0 2021-04-10 stsp goto done;
2406 20662ea0 2021-04-10 stsp }
2407 20662ea0 2021-04-10 stsp free(path_packfile);
2408 20662ea0 2021-04-10 stsp *total_packsize += sb.st_size;
2409 20662ea0 2021-04-10 stsp
2410 20662ea0 2021-04-10 stsp *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
2411 20662ea0 2021-04-10 stsp
2412 20662ea0 2021-04-10 stsp (*npackfiles)++;
2413 20662ea0 2021-04-10 stsp
2414 20662ea0 2021-04-10 stsp got_packidx_close(packidx);
2415 20662ea0 2021-04-10 stsp packidx = NULL;
2416 20662ea0 2021-04-10 stsp }
2417 20662ea0 2021-04-10 stsp done:
2418 20662ea0 2021-04-10 stsp if (packidx)
2419 20662ea0 2021-04-10 stsp got_packidx_close(packidx);
2420 20662ea0 2021-04-10 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
2421 20662ea0 2021-04-10 stsp err = got_error_from_errno("closedir");
2422 20662ea0 2021-04-10 stsp if (err) {
2423 20662ea0 2021-04-10 stsp *npackfiles = 0;
2424 20662ea0 2021-04-10 stsp *nobjects = 0;
2425 20662ea0 2021-04-10 stsp *total_packsize = 0;
2426 20662ea0 2021-04-10 stsp }
2427 20662ea0 2021-04-10 stsp return err;
2428 20662ea0 2021-04-10 stsp }
2429 0be8fa4c 2021-10-15 thomas
2430 0be8fa4c 2021-10-15 thomas RB_GENERATE(got_packidx_bloom_filter_tree, got_packidx_bloom_filter, entry,
2431 0be8fa4c 2021-10-15 thomas got_packidx_bloom_filter_cmp);