Blame


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