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