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