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