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