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