Blame


1 7b19e0f1 2017-11-05 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 deeca238 2018-03-12 stsp #include <sys/stat.h>
21 1510f469 2018-09-09 stsp #include <sys/mman.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 79b11c62 2018-03-09 stsp
24 e09a504c 2019-06-28 stsp #include <ctype.h>
25 1510f469 2018-09-09 stsp #include <fcntl.h>
26 4027f31a 2017-11-04 stsp #include <limits.h>
27 1510f469 2018-09-09 stsp #include <dirent.h>
28 4027f31a 2017-11-04 stsp #include <stdlib.h>
29 4027f31a 2017-11-04 stsp #include <stdio.h>
30 4027f31a 2017-11-04 stsp #include <sha1.h>
31 4027f31a 2017-11-04 stsp #include <string.h>
32 79b11c62 2018-03-09 stsp #include <zlib.h>
33 85f51bba 2018-07-16 stsp #include <errno.h>
34 85f51bba 2018-07-16 stsp #include <libgen.h>
35 ad242220 2018-09-08 stsp #include <stdint.h>
36 ad242220 2018-09-08 stsp #include <imsg.h>
37 c442a90d 2019-03-10 stsp #include <uuid.h>
38 4027f31a 2017-11-04 stsp
39 4027f31a 2017-11-04 stsp #include "got_error.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 4027f31a 2017-11-04 stsp #include "got_repository.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
44 7bb0daa1 2018-06-21 stsp #include "got_object.h"
45 4027f31a 2017-11-04 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
50 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
51 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
52 e09a504c 2019-06-28 stsp #include "got_lib_sha1.h"
53 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
54 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
55 c3f94f68 2017-11-05 stsp
56 79b11c62 2018-03-09 stsp #ifndef nitems
57 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 79b11c62 2018-03-09 stsp #endif
59 3b339b2f 2018-02-12 stsp
60 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
61 4027f31a 2017-11-04 stsp
62 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
63 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
64 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
65 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
66 4027f31a 2017-11-04 stsp
67 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
68 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
69 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
70 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
71 fb79db15 2019-02-01 stsp #define GOT_PACKED_REFS_FILE "packed-refs"
72 4df642d9 2017-11-05 stsp
73 7839bc15 2019-01-06 stsp const char *
74 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
75 86c3caaf 2018-03-09 stsp {
76 7839bc15 2019-01-06 stsp return repo->path;
77 86c3caaf 2018-03-09 stsp }
78 86c3caaf 2018-03-09 stsp
79 6e9da951 2019-01-06 stsp const char *
80 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
81 4027f31a 2017-11-04 stsp {
82 6e9da951 2019-01-06 stsp return repo->path_git_dir;
83 04ca23f4 2018-07-16 stsp }
84 04ca23f4 2018-07-16 stsp
85 04ca23f4 2018-07-16 stsp int
86 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
87 04ca23f4 2018-07-16 stsp {
88 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
89 4027f31a 2017-11-04 stsp }
90 4027f31a 2017-11-04 stsp
91 4027f31a 2017-11-04 stsp static char *
92 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
93 4027f31a 2017-11-04 stsp {
94 4027f31a 2017-11-04 stsp char *path_child;
95 4027f31a 2017-11-04 stsp
96 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
97 4027f31a 2017-11-04 stsp basename) == -1)
98 4027f31a 2017-11-04 stsp return NULL;
99 4027f31a 2017-11-04 stsp
100 4027f31a 2017-11-04 stsp return path_child;
101 4027f31a 2017-11-04 stsp }
102 4027f31a 2017-11-04 stsp
103 11995603 2017-11-05 stsp char *
104 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
105 4027f31a 2017-11-04 stsp {
106 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
107 4027f31a 2017-11-04 stsp }
108 4027f31a 2017-11-04 stsp
109 11995603 2017-11-05 stsp char *
110 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
111 a1fd68d8 2018-01-12 stsp {
112 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
113 a1fd68d8 2018-01-12 stsp }
114 a1fd68d8 2018-01-12 stsp
115 a1fd68d8 2018-01-12 stsp char *
116 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
117 4027f31a 2017-11-04 stsp {
118 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
119 4027f31a 2017-11-04 stsp }
120 4027f31a 2017-11-04 stsp
121 fb79db15 2019-02-01 stsp char *
122 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
123 fb79db15 2019-02-01 stsp {
124 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
125 fb79db15 2019-02-01 stsp }
126 fb79db15 2019-02-01 stsp
127 4027f31a 2017-11-04 stsp static char *
128 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
129 4027f31a 2017-11-04 stsp {
130 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
131 4027f31a 2017-11-04 stsp }
132 4027f31a 2017-11-04 stsp
133 4027f31a 2017-11-04 stsp static int
134 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
135 4027f31a 2017-11-04 stsp {
136 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
137 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
138 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
139 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
140 deeca238 2018-03-12 stsp int ret = 0;
141 deeca238 2018-03-12 stsp struct stat sb;
142 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
143 4027f31a 2017-11-04 stsp
144 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
145 deeca238 2018-03-12 stsp goto done;
146 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
147 deeca238 2018-03-12 stsp goto done;
148 4027f31a 2017-11-04 stsp
149 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
150 deeca238 2018-03-12 stsp goto done;
151 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
152 deeca238 2018-03-12 stsp goto done;
153 deeca238 2018-03-12 stsp
154 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
155 deeca238 2018-03-12 stsp goto done;
156 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
157 deeca238 2018-03-12 stsp goto done;
158 deeca238 2018-03-12 stsp
159 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
160 deeca238 2018-03-12 stsp goto done;
161 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
162 deeca238 2018-03-12 stsp goto done;
163 4847cca1 2018-03-12 stsp
164 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
165 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
166 4847cca1 2018-03-12 stsp goto done;
167 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
168 4847cca1 2018-03-12 stsp
169 deeca238 2018-03-12 stsp ret = 1;
170 deeca238 2018-03-12 stsp done:
171 4027f31a 2017-11-04 stsp free(path_objects);
172 4027f31a 2017-11-04 stsp free(path_refs);
173 4027f31a 2017-11-04 stsp free(path_head);
174 4027f31a 2017-11-04 stsp return ret;
175 4027f31a 2017-11-04 stsp
176 7bb0daa1 2018-06-21 stsp }
177 7bb0daa1 2018-06-21 stsp
178 f6be5c30 2018-06-22 stsp const struct got_error *
179 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
180 f6be5c30 2018-06-22 stsp struct got_object *obj)
181 f6be5c30 2018-06-22 stsp {
182 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
183 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
184 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
185 79c99a64 2019-05-23 stsp if (err) {
186 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
187 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
188 79c99a64 2019-05-23 stsp err = NULL;
189 f6be5c30 2018-06-22 stsp return err;
190 79c99a64 2019-05-23 stsp }
191 f6be5c30 2018-06-22 stsp obj->refcnt++;
192 ccfe88e6 2018-07-12 stsp #endif
193 f6be5c30 2018-06-22 stsp return NULL;
194 f6be5c30 2018-06-22 stsp }
195 f6be5c30 2018-06-22 stsp
196 7bb0daa1 2018-06-21 stsp struct got_object *
197 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
198 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
199 7bb0daa1 2018-06-21 stsp {
200 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
201 7bb0daa1 2018-06-21 stsp }
202 7bb0daa1 2018-06-21 stsp
203 4027f31a 2017-11-04 stsp const struct got_error *
204 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
205 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
206 f6be5c30 2018-06-22 stsp {
207 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
208 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
209 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
210 79c99a64 2019-05-23 stsp if (err) {
211 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
212 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
213 79c99a64 2019-05-23 stsp err = NULL;
214 f6be5c30 2018-06-22 stsp return err;
215 79c99a64 2019-05-23 stsp }
216 f6be5c30 2018-06-22 stsp tree->refcnt++;
217 ccfe88e6 2018-07-12 stsp #endif
218 f6be5c30 2018-06-22 stsp return NULL;
219 f6be5c30 2018-06-22 stsp }
220 f6be5c30 2018-06-22 stsp
221 f6be5c30 2018-06-22 stsp struct got_tree_object *
222 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
223 f6be5c30 2018-06-22 stsp struct got_object_id *id)
224 f6be5c30 2018-06-22 stsp {
225 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
226 6bef87be 2018-09-11 stsp &repo->treecache, id);
227 1943de01 2018-06-22 stsp }
228 1943de01 2018-06-22 stsp
229 1943de01 2018-06-22 stsp const struct got_error *
230 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
231 1943de01 2018-06-22 stsp struct got_commit_object *commit)
232 1943de01 2018-06-22 stsp {
233 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
234 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
235 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
236 79c99a64 2019-05-23 stsp if (err) {
237 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
238 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
239 79c99a64 2019-05-23 stsp err = NULL;
240 1943de01 2018-06-22 stsp return err;
241 79c99a64 2019-05-23 stsp }
242 1943de01 2018-06-22 stsp commit->refcnt++;
243 ccfe88e6 2018-07-12 stsp #endif
244 f6be5c30 2018-06-22 stsp return NULL;
245 f6be5c30 2018-06-22 stsp }
246 f6be5c30 2018-06-22 stsp
247 1943de01 2018-06-22 stsp struct got_commit_object *
248 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
249 1943de01 2018-06-22 stsp struct got_object_id *id)
250 1943de01 2018-06-22 stsp {
251 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
252 6bef87be 2018-09-11 stsp &repo->commitcache, id);
253 f4a881ce 2018-11-17 stsp }
254 f4a881ce 2018-11-17 stsp
255 f4a881ce 2018-11-17 stsp const struct got_error *
256 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
257 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
258 f4a881ce 2018-11-17 stsp {
259 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
260 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
261 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
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 f4a881ce 2018-11-17 stsp return err;
267 79c99a64 2019-05-23 stsp }
268 f4a881ce 2018-11-17 stsp tag->refcnt++;
269 f4a881ce 2018-11-17 stsp #endif
270 f4a881ce 2018-11-17 stsp return NULL;
271 f4a881ce 2018-11-17 stsp }
272 f4a881ce 2018-11-17 stsp
273 f4a881ce 2018-11-17 stsp struct got_tag_object *
274 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
275 f4a881ce 2018-11-17 stsp {
276 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
277 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
278 1943de01 2018-06-22 stsp }
279 1943de01 2018-06-22 stsp
280 f6be5c30 2018-06-22 stsp const struct got_error *
281 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
282 4027f31a 2017-11-04 stsp {
283 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
284 85f51bba 2018-07-16 stsp
285 85f51bba 2018-07-16 stsp /* bare git repository? */
286 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
287 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
288 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
289 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
290 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
291 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
292 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
293 85f51bba 2018-07-16 stsp goto done;
294 85f51bba 2018-07-16 stsp }
295 85f51bba 2018-07-16 stsp return NULL;
296 85f51bba 2018-07-16 stsp }
297 85f51bba 2018-07-16 stsp
298 85f51bba 2018-07-16 stsp /* git repository with working tree? */
299 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
300 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
301 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
302 85f51bba 2018-07-16 stsp goto done;
303 85f51bba 2018-07-16 stsp }
304 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
305 85f51bba 2018-07-16 stsp repo->path = strdup(path);
306 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
307 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
308 85f51bba 2018-07-16 stsp goto done;
309 85f51bba 2018-07-16 stsp }
310 85f51bba 2018-07-16 stsp return NULL;
311 85f51bba 2018-07-16 stsp }
312 85f51bba 2018-07-16 stsp
313 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
314 ee645855 2019-02-05 stsp done:
315 85f51bba 2018-07-16 stsp if (err) {
316 ee645855 2019-02-05 stsp free(repo->path);
317 ee645855 2019-02-05 stsp repo->path = NULL;
318 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
319 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
320 85f51bba 2018-07-16 stsp }
321 85f51bba 2018-07-16 stsp return err;
322 85f51bba 2018-07-16 stsp }
323 85f51bba 2018-07-16 stsp
324 85f51bba 2018-07-16 stsp const struct got_error *
325 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
326 85f51bba 2018-07-16 stsp {
327 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
328 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
329 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
330 ad242220 2018-09-08 stsp int i, tried_root = 0;
331 4027f31a 2017-11-04 stsp
332 85f51bba 2018-07-16 stsp *repop = NULL;
333 85f51bba 2018-07-16 stsp
334 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
335 2393f13b 2018-03-09 stsp abspath = strdup(path);
336 2393f13b 2018-03-09 stsp else
337 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
338 92af5469 2017-11-05 stsp if (abspath == NULL)
339 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
340 4027f31a 2017-11-04 stsp
341 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
342 92af5469 2017-11-05 stsp if (repo == NULL) {
343 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
344 92af5469 2017-11-05 stsp goto done;
345 92af5469 2017-11-05 stsp }
346 4027f31a 2017-11-04 stsp
347 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
348 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
349 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
350 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
351 ad242220 2018-09-08 stsp }
352 ad242220 2018-09-08 stsp
353 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
354 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
355 6bef87be 2018-09-11 stsp if (err)
356 f6be5c30 2018-06-22 stsp goto done;
357 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
358 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
359 6bef87be 2018-09-11 stsp if (err)
360 1943de01 2018-06-22 stsp goto done;
361 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
362 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
363 6bef87be 2018-09-11 stsp if (err)
364 eb77ee11 2018-07-08 stsp goto done;
365 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
366 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
367 f4a881ce 2018-11-17 stsp if (err)
368 f4a881ce 2018-11-17 stsp goto done;
369 1943de01 2018-06-22 stsp
370 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
371 85f51bba 2018-07-16 stsp if (normpath == NULL) {
372 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
373 92af5469 2017-11-05 stsp goto done;
374 92af5469 2017-11-05 stsp }
375 4027f31a 2017-11-04 stsp
376 85f51bba 2018-07-16 stsp path = normpath;
377 85f51bba 2018-07-16 stsp do {
378 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
379 85f51bba 2018-07-16 stsp if (err == NULL)
380 85f51bba 2018-07-16 stsp break;
381 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
382 85f51bba 2018-07-16 stsp break;
383 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
384 85f51bba 2018-07-16 stsp if (tried_root) {
385 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
386 85f51bba 2018-07-16 stsp break;
387 442a3ddc 2018-04-23 stsp }
388 85f51bba 2018-07-16 stsp tried_root = 1;
389 442a3ddc 2018-04-23 stsp }
390 85f51bba 2018-07-16 stsp path = dirname(path);
391 85f51bba 2018-07-16 stsp if (path == NULL)
392 638f9024 2019-05-13 stsp err = got_error_from_errno2("dirname", path);
393 85f51bba 2018-07-16 stsp } while (path);
394 92af5469 2017-11-05 stsp done:
395 92af5469 2017-11-05 stsp if (err)
396 5c2f5761 2018-09-19 stsp got_repo_close(repo);
397 85f51bba 2018-07-16 stsp else
398 85f51bba 2018-07-16 stsp *repop = repo;
399 92af5469 2017-11-05 stsp free(abspath);
400 85f51bba 2018-07-16 stsp free(normpath);
401 92af5469 2017-11-05 stsp return err;
402 4027f31a 2017-11-04 stsp }
403 4027f31a 2017-11-04 stsp
404 ad242220 2018-09-08 stsp const struct got_error *
405 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
406 4027f31a 2017-11-04 stsp {
407 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
408 79b11c62 2018-03-09 stsp int i;
409 79b11c62 2018-03-09 stsp
410 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
411 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
412 79b11c62 2018-03-09 stsp break;
413 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
414 79b11c62 2018-03-09 stsp }
415 bd1223b9 2018-03-14 stsp
416 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
417 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
418 7e656b93 2018-03-17 stsp break;
419 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
420 7e656b93 2018-03-17 stsp }
421 7e656b93 2018-03-17 stsp
422 4027f31a 2017-11-04 stsp free(repo->path);
423 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
424 cd717821 2018-06-22 stsp
425 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
426 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
427 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
428 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
429 ad242220 2018-09-08 stsp
430 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
431 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
432 ad242220 2018-09-08 stsp continue;
433 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
434 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
435 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
436 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
437 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
438 ad242220 2018-09-08 stsp if (child_err && err == NULL)
439 ad242220 2018-09-08 stsp err = child_err;
440 3a6ce05a 2019-02-11 stsp if (close(repo->privsep_children[i].imsg_fd) != 0 &&
441 3a6ce05a 2019-02-11 stsp err == NULL)
442 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
443 ad242220 2018-09-08 stsp }
444 4027f31a 2017-11-04 stsp free(repo);
445 ad242220 2018-09-08 stsp
446 ad242220 2018-09-08 stsp return err;
447 4027f31a 2017-11-04 stsp }
448 04ca23f4 2018-07-16 stsp
449 04ca23f4 2018-07-16 stsp const struct got_error *
450 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
451 23721109 2018-10-22 stsp const char *input_path, int check_disk)
452 04ca23f4 2018-07-16 stsp {
453 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
454 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
455 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
456 7839bc15 2019-01-06 stsp char *cwd, *canonpath, *path = NULL;
457 04ca23f4 2018-07-16 stsp
458 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
459 04ca23f4 2018-07-16 stsp
460 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
461 04ca23f4 2018-07-16 stsp if (cwd == NULL)
462 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
463 04ca23f4 2018-07-16 stsp
464 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
465 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
466 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
467 04ca23f4 2018-07-16 stsp goto done;
468 04ca23f4 2018-07-16 stsp }
469 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
470 04ca23f4 2018-07-16 stsp if (err)
471 04ca23f4 2018-07-16 stsp goto done;
472 04ca23f4 2018-07-16 stsp
473 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
474 04ca23f4 2018-07-16 stsp
475 2840f715 2019-07-11 stsp if (!check_disk || canonpath[0] == '\0') {
476 23721109 2018-10-22 stsp path = strdup(canonpath);
477 b70703ad 2019-03-18 stsp if (path == NULL) {
478 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
479 04ca23f4 2018-07-16 stsp goto done;
480 04ca23f4 2018-07-16 stsp }
481 04ca23f4 2018-07-16 stsp } else {
482 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
483 04ca23f4 2018-07-16 stsp
484 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
485 04ca23f4 2018-07-16 stsp if (path == NULL) {
486 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
487 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
488 230a42bd 2019-05-11 jcs canonpath);
489 b70703ad 2019-03-18 stsp goto done;
490 b70703ad 2019-03-18 stsp }
491 b70703ad 2019-03-18 stsp /*
492 b70703ad 2019-03-18 stsp * Path is not on disk.
493 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
494 b70703ad 2019-03-18 stsp */
495 b70703ad 2019-03-18 stsp path = strdup(canonpath);
496 b70703ad 2019-03-18 stsp if (path == NULL) {
497 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
498 b70703ad 2019-03-18 stsp goto done;
499 b70703ad 2019-03-18 stsp }
500 04ca23f4 2018-07-16 stsp }
501 04ca23f4 2018-07-16 stsp
502 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
503 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
504 04ca23f4 2018-07-16 stsp len = strlen(path);
505 04ca23f4 2018-07-16 stsp
506 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
507 04ca23f4 2018-07-16 stsp is_repo_child = 1;
508 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
509 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
510 04ca23f4 2018-07-16 stsp
511 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
512 04ca23f4 2018-07-16 stsp free(path);
513 04ca23f4 2018-07-16 stsp path = strdup("");
514 04ca23f4 2018-07-16 stsp if (path == NULL) {
515 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
516 04ca23f4 2018-07-16 stsp goto done;
517 04ca23f4 2018-07-16 stsp }
518 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
519 04ca23f4 2018-07-16 stsp char *child;
520 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
521 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
522 04ca23f4 2018-07-16 stsp repo_abspath, path);
523 04ca23f4 2018-07-16 stsp if (err)
524 04ca23f4 2018-07-16 stsp goto done;
525 04ca23f4 2018-07-16 stsp free(path);
526 04ca23f4 2018-07-16 stsp path = child;
527 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
528 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
529 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
530 04ca23f4 2018-07-16 stsp /*
531 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
532 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
533 04ca23f4 2018-07-16 stsp */
534 04ca23f4 2018-07-16 stsp } else {
535 04ca23f4 2018-07-16 stsp char *child;
536 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
537 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
538 04ca23f4 2018-07-16 stsp repo_abspath, path);
539 04ca23f4 2018-07-16 stsp if (err)
540 04ca23f4 2018-07-16 stsp goto done;
541 04ca23f4 2018-07-16 stsp free(path);
542 04ca23f4 2018-07-16 stsp path = child;
543 04ca23f4 2018-07-16 stsp }
544 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
545 04ca23f4 2018-07-16 stsp char *child;
546 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
547 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
548 04ca23f4 2018-07-16 stsp path);
549 04ca23f4 2018-07-16 stsp if (err)
550 04ca23f4 2018-07-16 stsp goto done;
551 04ca23f4 2018-07-16 stsp free(path);
552 04ca23f4 2018-07-16 stsp path = child;
553 04ca23f4 2018-07-16 stsp } else {
554 04ca23f4 2018-07-16 stsp /*
555 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
556 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
557 04ca23f4 2018-07-16 stsp */
558 04ca23f4 2018-07-16 stsp }
559 04ca23f4 2018-07-16 stsp }
560 04ca23f4 2018-07-16 stsp
561 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
562 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
563 04ca23f4 2018-07-16 stsp char *abspath;
564 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 04ca23f4 2018-07-16 stsp goto done;
567 04ca23f4 2018-07-16 stsp }
568 04ca23f4 2018-07-16 stsp free(path);
569 04ca23f4 2018-07-16 stsp path = abspath;
570 04ca23f4 2018-07-16 stsp }
571 04ca23f4 2018-07-16 stsp
572 04ca23f4 2018-07-16 stsp done:
573 04ca23f4 2018-07-16 stsp free(cwd);
574 04ca23f4 2018-07-16 stsp free(canonpath);
575 04ca23f4 2018-07-16 stsp if (err)
576 04ca23f4 2018-07-16 stsp free(path);
577 04ca23f4 2018-07-16 stsp else
578 04ca23f4 2018-07-16 stsp *in_repo_path = path;
579 1510f469 2018-09-09 stsp return err;
580 1510f469 2018-09-09 stsp }
581 1510f469 2018-09-09 stsp
582 1510f469 2018-09-09 stsp const struct got_error *
583 1510f469 2018-09-09 stsp got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
584 1510f469 2018-09-09 stsp {
585 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
586 1510f469 2018-09-09 stsp int i;
587 1510f469 2018-09-09 stsp
588 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
589 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
590 1510f469 2018-09-09 stsp break;
591 1510f469 2018-09-09 stsp }
592 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
593 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
594 1510f469 2018-09-09 stsp if (err)
595 1510f469 2018-09-09 stsp return err;
596 1510f469 2018-09-09 stsp }
597 1510f469 2018-09-09 stsp
598 15fe583f 2018-11-05 stsp /*
599 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
600 15fe583f 2018-11-05 stsp * be searched first in the future.
601 15fe583f 2018-11-05 stsp */
602 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
603 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
604 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
605 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
606 15fe583f 2018-11-05 stsp
607 1510f469 2018-09-09 stsp return NULL;
608 1510f469 2018-09-09 stsp }
609 1510f469 2018-09-09 stsp
610 1510f469 2018-09-09 stsp static int
611 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
612 1510f469 2018-09-09 stsp {
613 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
614 1510f469 2018-09-09 stsp return 0;
615 1510f469 2018-09-09 stsp
616 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
617 1510f469 2018-09-09 stsp return 0;
618 1510f469 2018-09-09 stsp
619 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
620 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
621 1510f469 2018-09-09 stsp return 0;
622 1510f469 2018-09-09 stsp
623 1510f469 2018-09-09 stsp return 1;
624 1510f469 2018-09-09 stsp }
625 1510f469 2018-09-09 stsp
626 1510f469 2018-09-09 stsp const struct got_error *
627 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
628 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
629 1510f469 2018-09-09 stsp {
630 1510f469 2018-09-09 stsp const struct got_error *err;
631 1510f469 2018-09-09 stsp char *path_packdir;
632 1510f469 2018-09-09 stsp DIR *packdir;
633 1510f469 2018-09-09 stsp struct dirent *dent;
634 1510f469 2018-09-09 stsp char *path_packidx;
635 1510f469 2018-09-09 stsp int i;
636 1510f469 2018-09-09 stsp
637 1510f469 2018-09-09 stsp /* Search pack index cache. */
638 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
639 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
640 1510f469 2018-09-09 stsp break;
641 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
642 1510f469 2018-09-09 stsp if (*idx != -1) {
643 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
644 1510f469 2018-09-09 stsp return NULL;
645 1510f469 2018-09-09 stsp }
646 1510f469 2018-09-09 stsp }
647 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
648 1510f469 2018-09-09 stsp
649 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
650 1510f469 2018-09-09 stsp if (path_packdir == NULL)
651 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
652 1510f469 2018-09-09 stsp
653 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
654 1510f469 2018-09-09 stsp if (packdir == NULL) {
655 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", path_packdir);
656 1510f469 2018-09-09 stsp goto done;
657 1510f469 2018-09-09 stsp }
658 1510f469 2018-09-09 stsp
659 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
660 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
661 1510f469 2018-09-09 stsp continue;
662 1510f469 2018-09-09 stsp
663 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
664 1510f469 2018-09-09 stsp dent->d_name) == -1) {
665 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
666 1510f469 2018-09-09 stsp goto done;
667 1510f469 2018-09-09 stsp }
668 1510f469 2018-09-09 stsp
669 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
670 1510f469 2018-09-09 stsp free(path_packidx);
671 1510f469 2018-09-09 stsp if (err)
672 1510f469 2018-09-09 stsp goto done;
673 1510f469 2018-09-09 stsp
674 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
675 1510f469 2018-09-09 stsp if (*idx != -1) {
676 1510f469 2018-09-09 stsp err = NULL; /* found the object */
677 1510f469 2018-09-09 stsp err = got_repo_cache_packidx(repo, *packidx);
678 1510f469 2018-09-09 stsp goto done;
679 1510f469 2018-09-09 stsp }
680 1510f469 2018-09-09 stsp
681 1510f469 2018-09-09 stsp err = got_packidx_close(*packidx);
682 1510f469 2018-09-09 stsp *packidx = NULL;
683 1510f469 2018-09-09 stsp if (err)
684 1510f469 2018-09-09 stsp goto done;
685 1510f469 2018-09-09 stsp }
686 1510f469 2018-09-09 stsp
687 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
688 1510f469 2018-09-09 stsp done:
689 1510f469 2018-09-09 stsp free(path_packdir);
690 d69bcdf7 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
691 638f9024 2019-05-13 stsp err = got_error_from_errno("closedir");
692 04ca23f4 2018-07-16 stsp return err;
693 04ca23f4 2018-07-16 stsp }
694 1510f469 2018-09-09 stsp
695 1510f469 2018-09-09 stsp static const struct got_error *
696 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
697 1510f469 2018-09-09 stsp {
698 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
699 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
700 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
701 1510f469 2018-09-09 stsp ssize_t n;
702 1510f469 2018-09-09 stsp
703 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
704 1510f469 2018-09-09 stsp if (n < 0)
705 638f9024 2019-05-13 stsp return got_error_from_errno("read");
706 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
707 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
708 1510f469 2018-09-09 stsp
709 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
710 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
711 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
712 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
713 1510f469 2018-09-09 stsp
714 1510f469 2018-09-09 stsp return err;
715 1510f469 2018-09-09 stsp }
716 1510f469 2018-09-09 stsp
717 1510f469 2018-09-09 stsp static const struct got_error *
718 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
719 1510f469 2018-09-09 stsp {
720 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
721 1510f469 2018-09-09 stsp
722 a5b57ccf 2019-04-11 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
723 1510f469 2018-09-09 stsp if (*fd == -1)
724 638f9024 2019-05-13 stsp return got_error_from_errno2("open", path_packfile);
725 1510f469 2018-09-09 stsp
726 1510f469 2018-09-09 stsp if (packidx) {
727 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
728 1510f469 2018-09-09 stsp if (err) {
729 1510f469 2018-09-09 stsp close(*fd);
730 1510f469 2018-09-09 stsp *fd = -1;
731 1510f469 2018-09-09 stsp }
732 1510f469 2018-09-09 stsp }
733 1510f469 2018-09-09 stsp
734 1510f469 2018-09-09 stsp return err;
735 1510f469 2018-09-09 stsp }
736 1510f469 2018-09-09 stsp
737 1510f469 2018-09-09 stsp const struct got_error *
738 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
739 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
740 1510f469 2018-09-09 stsp {
741 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
742 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
743 ff563a3d 2019-05-23 stsp struct stat sb;
744 1510f469 2018-09-09 stsp int i;
745 1510f469 2018-09-09 stsp
746 1510f469 2018-09-09 stsp if (packp)
747 1510f469 2018-09-09 stsp *packp = NULL;
748 1510f469 2018-09-09 stsp
749 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
750 1510f469 2018-09-09 stsp pack = &repo->packs[i];
751 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
752 1510f469 2018-09-09 stsp break;
753 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
754 1510f469 2018-09-09 stsp return NULL;
755 1510f469 2018-09-09 stsp }
756 1510f469 2018-09-09 stsp
757 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
758 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
759 1510f469 2018-09-09 stsp if (err)
760 1510f469 2018-09-09 stsp return err;
761 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
762 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
763 1510f469 2018-09-09 stsp i = 0;
764 1510f469 2018-09-09 stsp }
765 1510f469 2018-09-09 stsp
766 1510f469 2018-09-09 stsp pack = &repo->packs[i];
767 1510f469 2018-09-09 stsp
768 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
769 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
770 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
771 1510f469 2018-09-09 stsp goto done;
772 1510f469 2018-09-09 stsp }
773 1510f469 2018-09-09 stsp
774 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
775 1510f469 2018-09-09 stsp if (err)
776 1510f469 2018-09-09 stsp goto done;
777 1510f469 2018-09-09 stsp
778 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
779 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
780 1510f469 2018-09-09 stsp goto done;
781 ff563a3d 2019-05-23 stsp }
782 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
783 90636195 2018-09-11 stsp
784 90636195 2018-09-11 stsp pack->privsep_child = NULL;
785 1510f469 2018-09-09 stsp
786 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
787 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
788 1510f469 2018-09-09 stsp pack->fd, 0);
789 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
790 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
791 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
792 3a11398b 2019-02-21 stsp goto done;
793 3a11398b 2019-02-21 stsp }
794 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
795 3a11398b 2019-02-21 stsp }
796 1510f469 2018-09-09 stsp #endif
797 1510f469 2018-09-09 stsp done:
798 1510f469 2018-09-09 stsp if (err) {
799 1510f469 2018-09-09 stsp if (pack) {
800 1510f469 2018-09-09 stsp free(pack->path_packfile);
801 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
802 1510f469 2018-09-09 stsp }
803 1510f469 2018-09-09 stsp } else if (packp)
804 1510f469 2018-09-09 stsp *packp = pack;
805 1510f469 2018-09-09 stsp return err;
806 1510f469 2018-09-09 stsp }
807 1510f469 2018-09-09 stsp
808 1510f469 2018-09-09 stsp struct got_pack *
809 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
810 1510f469 2018-09-09 stsp {
811 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
812 1510f469 2018-09-09 stsp int i;
813 1510f469 2018-09-09 stsp
814 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
815 1510f469 2018-09-09 stsp pack = &repo->packs[i];
816 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
817 1510f469 2018-09-09 stsp break;
818 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
819 1510f469 2018-09-09 stsp return pack;
820 2c7829a4 2019-06-17 stsp }
821 2c7829a4 2019-06-17 stsp
822 2c7829a4 2019-06-17 stsp return NULL;
823 2c7829a4 2019-06-17 stsp }
824 2c7829a4 2019-06-17 stsp
825 2c7829a4 2019-06-17 stsp const struct got_error *
826 2c7829a4 2019-06-17 stsp got_repo_init(const char *repo_path)
827 2c7829a4 2019-06-17 stsp {
828 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
829 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
830 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
831 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
832 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
833 2c7829a4 2019-06-17 stsp };
834 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
835 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
836 2c7829a4 2019-06-17 stsp const char *headref_str = "ref: refs/heads/master";
837 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
838 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
839 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
840 2c7829a4 2019-06-17 stsp "\tbare = true\n";
841 2c7829a4 2019-06-17 stsp char *path;
842 2c7829a4 2019-06-17 stsp int i;
843 2c7829a4 2019-06-17 stsp
844 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
845 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
846 2c7829a4 2019-06-17 stsp
847 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
848 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
849 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
850 2c7829a4 2019-06-17 stsp }
851 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
852 2c7829a4 2019-06-17 stsp free(path);
853 2c7829a4 2019-06-17 stsp if (err)
854 2c7829a4 2019-06-17 stsp return err;
855 1510f469 2018-09-09 stsp }
856 1510f469 2018-09-09 stsp
857 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
858 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
859 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
860 2c7829a4 2019-06-17 stsp free(path);
861 2c7829a4 2019-06-17 stsp if (err)
862 2c7829a4 2019-06-17 stsp return err;
863 2c7829a4 2019-06-17 stsp
864 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
865 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
866 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
867 2c7829a4 2019-06-17 stsp free(path);
868 2c7829a4 2019-06-17 stsp if (err)
869 2c7829a4 2019-06-17 stsp return err;
870 2c7829a4 2019-06-17 stsp
871 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
872 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
873 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
874 2c7829a4 2019-06-17 stsp free(path);
875 2c7829a4 2019-06-17 stsp if (err)
876 2c7829a4 2019-06-17 stsp return err;
877 2c7829a4 2019-06-17 stsp
878 1510f469 2018-09-09 stsp return NULL;
879 e09a504c 2019-06-28 stsp }
880 e09a504c 2019-06-28 stsp
881 e09a504c 2019-06-28 stsp static const struct got_error *
882 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
883 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
884 e09a504c 2019-06-28 stsp {
885 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
886 e09a504c 2019-06-28 stsp char *path_packdir;
887 e09a504c 2019-06-28 stsp DIR *packdir;
888 e09a504c 2019-06-28 stsp struct dirent *dent;
889 e09a504c 2019-06-28 stsp char *path_packidx;
890 dd88155e 2019-06-29 stsp struct got_object_id_queue matched_ids;
891 e09a504c 2019-06-28 stsp
892 dd88155e 2019-06-29 stsp SIMPLEQ_INIT(&matched_ids);
893 dd88155e 2019-06-29 stsp
894 e09a504c 2019-06-28 stsp path_packdir = got_repo_get_path_objects_pack(repo);
895 e09a504c 2019-06-28 stsp if (path_packdir == NULL)
896 e09a504c 2019-06-28 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
897 e09a504c 2019-06-28 stsp
898 e09a504c 2019-06-28 stsp packdir = opendir(path_packdir);
899 e09a504c 2019-06-28 stsp if (packdir == NULL) {
900 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path_packdir);
901 e09a504c 2019-06-28 stsp goto done;
902 e09a504c 2019-06-28 stsp }
903 e09a504c 2019-06-28 stsp
904 e09a504c 2019-06-28 stsp while ((dent = readdir(packdir)) != NULL) {
905 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
906 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
907 dd88155e 2019-06-29 stsp
908 e09a504c 2019-06-28 stsp
909 e09a504c 2019-06-28 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
910 e09a504c 2019-06-28 stsp continue;
911 e09a504c 2019-06-28 stsp
912 e09a504c 2019-06-28 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
913 e09a504c 2019-06-28 stsp dent->d_name) == -1) {
914 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
915 4277420a 2019-06-29 stsp break;
916 e09a504c 2019-06-28 stsp }
917 e09a504c 2019-06-28 stsp
918 e09a504c 2019-06-28 stsp err = got_packidx_open(&packidx, path_packidx, 0);
919 e09a504c 2019-06-28 stsp free(path_packidx);
920 e09a504c 2019-06-28 stsp if (err)
921 4277420a 2019-06-29 stsp break;
922 e09a504c 2019-06-28 stsp
923 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
924 4277420a 2019-06-29 stsp packidx, id_str_prefix);
925 4277420a 2019-06-29 stsp if (err) {
926 4277420a 2019-06-29 stsp got_packidx_close(packidx);
927 4277420a 2019-06-29 stsp break;
928 4277420a 2019-06-29 stsp }
929 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
930 dd88155e 2019-06-29 stsp if (err)
931 e09a504c 2019-06-28 stsp break;
932 e09a504c 2019-06-28 stsp
933 dd88155e 2019-06-29 stsp SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
934 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
935 dd88155e 2019-06-29 stsp int matched_type;
936 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
937 dd88155e 2019-06-29 stsp qid->id);
938 dd88155e 2019-06-29 stsp if (err)
939 dd88155e 2019-06-29 stsp goto done;
940 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
941 dd88155e 2019-06-29 stsp continue;
942 dd88155e 2019-06-29 stsp }
943 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
944 dd88155e 2019-06-29 stsp *unique_id = got_object_id_dup(qid->id);
945 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
946 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
947 dd88155e 2019-06-29 stsp goto done;
948 dd88155e 2019-06-29 stsp }
949 4277420a 2019-06-29 stsp } else {
950 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
951 561c3678 2019-07-02 stsp goto done;
952 e09a504c 2019-06-28 stsp }
953 e09a504c 2019-06-28 stsp }
954 e09a504c 2019-06-28 stsp }
955 e09a504c 2019-06-28 stsp done:
956 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
957 e09a504c 2019-06-28 stsp free(path_packdir);
958 e09a504c 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
959 e09a504c 2019-06-28 stsp err = got_error_from_errno("closedir");
960 e09a504c 2019-06-28 stsp if (err) {
961 e09a504c 2019-06-28 stsp free(*unique_id);
962 e09a504c 2019-06-28 stsp *unique_id = NULL;
963 e09a504c 2019-06-28 stsp }
964 e09a504c 2019-06-28 stsp return err;
965 e09a504c 2019-06-28 stsp }
966 e09a504c 2019-06-28 stsp
967 e09a504c 2019-06-28 stsp static const struct got_error *
968 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
969 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
970 e09a504c 2019-06-28 stsp struct got_repository *repo)
971 e09a504c 2019-06-28 stsp {
972 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
973 e09a504c 2019-06-28 stsp char *path;
974 e09a504c 2019-06-28 stsp DIR *dir = NULL;
975 e09a504c 2019-06-28 stsp struct dirent *dent;
976 e09a504c 2019-06-28 stsp struct got_object_id id;
977 e09a504c 2019-06-28 stsp
978 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
979 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
980 e09a504c 2019-06-28 stsp goto done;
981 e09a504c 2019-06-28 stsp }
982 e09a504c 2019-06-28 stsp
983 e09a504c 2019-06-28 stsp dir = opendir(path);
984 e09a504c 2019-06-28 stsp if (dir == NULL) {
985 4277420a 2019-06-29 stsp if (errno == ENOENT) {
986 4277420a 2019-06-29 stsp err = NULL;
987 4277420a 2019-06-29 stsp goto done;
988 4277420a 2019-06-29 stsp }
989 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
990 e09a504c 2019-06-28 stsp goto done;
991 e09a504c 2019-06-28 stsp }
992 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
993 e09a504c 2019-06-28 stsp char *id_str;
994 5903ff6e 2019-06-29 stsp int cmp;
995 5903ff6e 2019-06-29 stsp
996 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
997 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
998 e09a504c 2019-06-28 stsp continue;
999 e09a504c 2019-06-28 stsp
1000 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1001 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1002 e09a504c 2019-06-28 stsp goto done;
1003 e09a504c 2019-06-28 stsp }
1004 e09a504c 2019-06-28 stsp
1005 e09a504c 2019-06-28 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
1006 e09a504c 2019-06-28 stsp continue;
1007 e09a504c 2019-06-28 stsp
1008 52d1d0d9 2019-07-07 stsp /*
1009 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1010 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1011 52d1d0d9 2019-07-07 stsp */
1012 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1013 52d1d0d9 2019-07-07 stsp if (cmp != 0) {
1014 e09a504c 2019-06-28 stsp free(id_str);
1015 e09a504c 2019-06-28 stsp continue;
1016 e09a504c 2019-06-28 stsp }
1017 e09a504c 2019-06-28 stsp
1018 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1019 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1020 dd88155e 2019-06-29 stsp int matched_type;
1021 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1022 dd88155e 2019-06-29 stsp &id);
1023 dd88155e 2019-06-29 stsp if (err)
1024 dd88155e 2019-06-29 stsp goto done;
1025 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1026 dd88155e 2019-06-29 stsp continue;
1027 dd88155e 2019-06-29 stsp }
1028 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1029 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1030 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1031 e09a504c 2019-06-28 stsp free(id_str);
1032 e09a504c 2019-06-28 stsp goto done;
1033 e09a504c 2019-06-28 stsp }
1034 e09a504c 2019-06-28 stsp } else {
1035 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1036 e09a504c 2019-06-28 stsp free(id_str);
1037 e09a504c 2019-06-28 stsp goto done;
1038 e09a504c 2019-06-28 stsp }
1039 e09a504c 2019-06-28 stsp }
1040 e09a504c 2019-06-28 stsp done:
1041 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1042 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1043 e09a504c 2019-06-28 stsp if (err) {
1044 e09a504c 2019-06-28 stsp free(*unique_id);
1045 e09a504c 2019-06-28 stsp *unique_id = NULL;
1046 e09a504c 2019-06-28 stsp }
1047 e09a504c 2019-06-28 stsp free(path);
1048 e09a504c 2019-06-28 stsp return err;
1049 1510f469 2018-09-09 stsp }
1050 e09a504c 2019-06-28 stsp
1051 e09a504c 2019-06-28 stsp const struct got_error *
1052 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1053 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1054 e09a504c 2019-06-28 stsp {
1055 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1056 e09a504c 2019-06-28 stsp char *path_objects = got_repo_get_path_objects(repo);
1057 e09a504c 2019-06-28 stsp char *object_dir = NULL;
1058 e09a504c 2019-06-28 stsp size_t len;
1059 4277420a 2019-06-29 stsp int i;
1060 e09a504c 2019-06-28 stsp
1061 4277420a 2019-06-29 stsp *id = NULL;
1062 4277420a 2019-06-29 stsp
1063 4277420a 2019-06-29 stsp for (i = 0; i < strlen(id_str_prefix); i++) {
1064 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1065 4277420a 2019-06-29 stsp continue;
1066 4277420a 2019-06-29 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1067 4277420a 2019-06-29 stsp }
1068 4277420a 2019-06-29 stsp
1069 e09a504c 2019-06-28 stsp len = strlen(id_str_prefix);
1070 e09a504c 2019-06-28 stsp if (len >= 2) {
1071 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1072 4277420a 2019-06-29 stsp if (err)
1073 83c8b3b8 2019-06-29 stsp goto done;
1074 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1075 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1076 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1077 83c8b3b8 2019-06-29 stsp goto done;
1078 83c8b3b8 2019-06-29 stsp }
1079 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1080 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1081 e09a504c 2019-06-28 stsp } else if (len == 1) {
1082 e09a504c 2019-06-28 stsp int i;
1083 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1084 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1085 83c8b3b8 2019-06-29 stsp == -1) {
1086 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1087 83c8b3b8 2019-06-29 stsp goto done;
1088 83c8b3b8 2019-06-29 stsp }
1089 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1090 dd88155e 2019-06-29 stsp obj_type);
1091 4277420a 2019-06-29 stsp if (err)
1092 83c8b3b8 2019-06-29 stsp goto done;
1093 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1094 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1095 e09a504c 2019-06-28 stsp if (err)
1096 83c8b3b8 2019-06-29 stsp goto done;
1097 e09a504c 2019-06-28 stsp }
1098 83c8b3b8 2019-06-29 stsp } else {
1099 83c8b3b8 2019-06-29 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1100 83c8b3b8 2019-06-29 stsp goto done;
1101 83c8b3b8 2019-06-29 stsp }
1102 83c8b3b8 2019-06-29 stsp done:
1103 e09a504c 2019-06-28 stsp free(object_dir);
1104 4277420a 2019-06-29 stsp if (err) {
1105 4277420a 2019-06-29 stsp free(*id);
1106 4277420a 2019-06-29 stsp *id = NULL;
1107 4277420a 2019-06-29 stsp } else if (*id == NULL)
1108 4277420a 2019-06-29 stsp err = got_error(GOT_ERR_NO_OBJ);
1109 4277420a 2019-06-29 stsp
1110 e09a504c 2019-06-28 stsp return err;
1111 e09a504c 2019-06-28 stsp }