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