Blame


1 7b19e0f1 2017-11-05 stsp /*
2 3b339b2f 2018-02-12 stsp * Copyright (c) 2018 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 1510f469 2018-09-09 stsp #include <fcntl.h>
25 4027f31a 2017-11-04 stsp #include <limits.h>
26 1510f469 2018-09-09 stsp #include <dirent.h>
27 4027f31a 2017-11-04 stsp #include <stdlib.h>
28 4027f31a 2017-11-04 stsp #include <stdio.h>
29 4027f31a 2017-11-04 stsp #include <sha1.h>
30 4027f31a 2017-11-04 stsp #include <string.h>
31 79b11c62 2018-03-09 stsp #include <zlib.h>
32 85f51bba 2018-07-16 stsp #include <errno.h>
33 85f51bba 2018-07-16 stsp #include <libgen.h>
34 ad242220 2018-09-08 stsp #include <stdint.h>
35 ad242220 2018-09-08 stsp #include <imsg.h>
36 4027f31a 2017-11-04 stsp
37 4027f31a 2017-11-04 stsp #include "got_error.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 4027f31a 2017-11-04 stsp #include "got_repository.h"
40 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
41 7bb0daa1 2018-06-21 stsp #include "got_object.h"
42 4027f31a 2017-11-04 stsp
43 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
48 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
50 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
51 eb77ee11 2018-07-08 stsp #include "got_lib_object_idcache.h"
52 c3f94f68 2017-11-05 stsp
53 79b11c62 2018-03-09 stsp #ifndef nitems
54 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 79b11c62 2018-03-09 stsp #endif
56 3b339b2f 2018-02-12 stsp
57 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
58 4027f31a 2017-11-04 stsp
59 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
60 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
61 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
62 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
63 4027f31a 2017-11-04 stsp
64 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
65 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
66 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
67 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
68 4df642d9 2017-11-05 stsp
69 11995603 2017-11-05 stsp char *
70 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
71 86c3caaf 2018-03-09 stsp {
72 86c3caaf 2018-03-09 stsp return strdup(repo->path);
73 86c3caaf 2018-03-09 stsp }
74 86c3caaf 2018-03-09 stsp
75 86c3caaf 2018-03-09 stsp char *
76 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
77 4027f31a 2017-11-04 stsp {
78 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
79 04ca23f4 2018-07-16 stsp }
80 04ca23f4 2018-07-16 stsp
81 04ca23f4 2018-07-16 stsp int
82 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
83 04ca23f4 2018-07-16 stsp {
84 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
85 4027f31a 2017-11-04 stsp }
86 4027f31a 2017-11-04 stsp
87 4027f31a 2017-11-04 stsp static char *
88 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
89 4027f31a 2017-11-04 stsp {
90 4027f31a 2017-11-04 stsp char *path_child;
91 4027f31a 2017-11-04 stsp
92 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
93 4027f31a 2017-11-04 stsp basename) == -1)
94 4027f31a 2017-11-04 stsp return NULL;
95 4027f31a 2017-11-04 stsp
96 4027f31a 2017-11-04 stsp return path_child;
97 4027f31a 2017-11-04 stsp }
98 4027f31a 2017-11-04 stsp
99 11995603 2017-11-05 stsp char *
100 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
101 4027f31a 2017-11-04 stsp {
102 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
103 4027f31a 2017-11-04 stsp }
104 4027f31a 2017-11-04 stsp
105 11995603 2017-11-05 stsp char *
106 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
107 a1fd68d8 2018-01-12 stsp {
108 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
109 a1fd68d8 2018-01-12 stsp }
110 a1fd68d8 2018-01-12 stsp
111 a1fd68d8 2018-01-12 stsp char *
112 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
113 4027f31a 2017-11-04 stsp {
114 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
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_head(struct got_repository *repo)
119 4027f31a 2017-11-04 stsp {
120 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
121 4027f31a 2017-11-04 stsp }
122 4027f31a 2017-11-04 stsp
123 4027f31a 2017-11-04 stsp static int
124 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
125 4027f31a 2017-11-04 stsp {
126 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
127 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
128 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
129 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
130 deeca238 2018-03-12 stsp int ret = 0;
131 deeca238 2018-03-12 stsp struct stat sb;
132 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
133 4027f31a 2017-11-04 stsp
134 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
135 deeca238 2018-03-12 stsp goto done;
136 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
137 deeca238 2018-03-12 stsp goto done;
138 4027f31a 2017-11-04 stsp
139 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
140 deeca238 2018-03-12 stsp goto done;
141 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
142 deeca238 2018-03-12 stsp goto done;
143 deeca238 2018-03-12 stsp
144 deeca238 2018-03-12 stsp if (lstat(path_refs, &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 deeca238 2018-03-12 stsp
149 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
150 deeca238 2018-03-12 stsp goto done;
151 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
152 deeca238 2018-03-12 stsp goto done;
153 4847cca1 2018-03-12 stsp
154 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
155 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
156 4847cca1 2018-03-12 stsp goto done;
157 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
158 4847cca1 2018-03-12 stsp
159 deeca238 2018-03-12 stsp ret = 1;
160 deeca238 2018-03-12 stsp done:
161 4027f31a 2017-11-04 stsp free(path_git);
162 4027f31a 2017-11-04 stsp free(path_objects);
163 4027f31a 2017-11-04 stsp free(path_refs);
164 4027f31a 2017-11-04 stsp free(path_head);
165 4027f31a 2017-11-04 stsp return ret;
166 4027f31a 2017-11-04 stsp
167 4027f31a 2017-11-04 stsp }
168 4027f31a 2017-11-04 stsp
169 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
170 f6be5c30 2018-06-22 stsp static const struct got_error *
171 f6be5c30 2018-06-22 stsp cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
172 7bb0daa1 2018-06-21 stsp {
173 50bc349d 2018-06-22 stsp const struct got_error *err = NULL;
174 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
175 a9bf0c2c 2018-06-22 stsp int nelem;
176 7bb0daa1 2018-06-21 stsp
177 eb77ee11 2018-07-08 stsp nelem = got_object_idcache_num_elements(cache->idcache);
178 4307e577 2018-06-22 stsp if (nelem >= cache->size) {
179 eb77ee11 2018-07-08 stsp err = got_object_idcache_remove_least_used((void **)&ce,
180 eb77ee11 2018-07-08 stsp cache->idcache);
181 50bc349d 2018-06-22 stsp if (err)
182 50bc349d 2018-06-22 stsp return err;
183 f6be5c30 2018-06-22 stsp switch (cache->type) {
184 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
185 f6be5c30 2018-06-22 stsp got_object_close(ce->data.obj);
186 f6be5c30 2018-06-22 stsp break;
187 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
188 f6be5c30 2018-06-22 stsp got_object_tree_close(ce->data.tree);
189 f6be5c30 2018-06-22 stsp break;
190 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
191 1943de01 2018-06-22 stsp got_object_commit_close(ce->data.commit);
192 1943de01 2018-06-22 stsp break;
193 f6be5c30 2018-06-22 stsp }
194 7bb0daa1 2018-06-21 stsp free(ce);
195 7bb0daa1 2018-06-21 stsp }
196 7bb0daa1 2018-06-21 stsp
197 7bb0daa1 2018-06-21 stsp ce = calloc(1, sizeof(*ce));
198 7bb0daa1 2018-06-21 stsp if (ce == NULL)
199 7bb0daa1 2018-06-21 stsp return got_error_from_errno();
200 7bb0daa1 2018-06-21 stsp memcpy(&ce->id, id, sizeof(ce->id));
201 f6be5c30 2018-06-22 stsp switch (cache->type) {
202 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
203 f6be5c30 2018-06-22 stsp ce->data.obj = (struct got_object *)item;
204 f6be5c30 2018-06-22 stsp break;
205 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
206 f6be5c30 2018-06-22 stsp ce->data.tree = (struct got_tree_object *)item;
207 f6be5c30 2018-06-22 stsp break;
208 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
209 1943de01 2018-06-22 stsp ce->data.commit = (struct got_commit_object *)item;
210 1943de01 2018-06-22 stsp break;
211 f6be5c30 2018-06-22 stsp }
212 ccfe88e6 2018-07-12 stsp
213 eb77ee11 2018-07-08 stsp err = got_object_idcache_add(cache->idcache, id, ce);
214 50bc349d 2018-06-22 stsp if (err) {
215 50bc349d 2018-06-22 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
216 50bc349d 2018-06-22 stsp free(ce);
217 50bc349d 2018-06-22 stsp err = NULL;
218 50bc349d 2018-06-22 stsp }
219 a9bf0c2c 2018-06-22 stsp }
220 50bc349d 2018-06-22 stsp return err;
221 7bb0daa1 2018-06-21 stsp }
222 ccfe88e6 2018-07-12 stsp #endif
223 7bb0daa1 2018-06-21 stsp
224 f6be5c30 2018-06-22 stsp const struct got_error *
225 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
226 f6be5c30 2018-06-22 stsp struct got_object *obj)
227 f6be5c30 2018-06-22 stsp {
228 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
229 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
230 f6be5c30 2018-06-22 stsp err = cache_add(&repo->objcache, id, obj);
231 f6be5c30 2018-06-22 stsp if (err)
232 f6be5c30 2018-06-22 stsp return err;
233 f6be5c30 2018-06-22 stsp obj->refcnt++;
234 ccfe88e6 2018-07-12 stsp #endif
235 f6be5c30 2018-06-22 stsp return NULL;
236 f6be5c30 2018-06-22 stsp }
237 f6be5c30 2018-06-22 stsp
238 7bb0daa1 2018-06-21 stsp struct got_object *
239 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
240 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
241 7bb0daa1 2018-06-21 stsp {
242 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
243 54f20211 2018-06-22 stsp
244 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->objcache.idcache, id);
245 50bc349d 2018-06-22 stsp if (ce) {
246 54f20211 2018-06-22 stsp repo->objcache.cache_hit++;
247 f6be5c30 2018-06-22 stsp return ce->data.obj;
248 7bb0daa1 2018-06-21 stsp }
249 f6be5c30 2018-06-22 stsp
250 54f20211 2018-06-22 stsp repo->objcache.cache_miss++;
251 7bb0daa1 2018-06-21 stsp return NULL;
252 7bb0daa1 2018-06-21 stsp }
253 7bb0daa1 2018-06-21 stsp
254 4027f31a 2017-11-04 stsp const struct got_error *
255 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
256 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
257 f6be5c30 2018-06-22 stsp {
258 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
259 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
260 f6be5c30 2018-06-22 stsp err = cache_add(&repo->treecache, id, tree);
261 f6be5c30 2018-06-22 stsp if (err)
262 f6be5c30 2018-06-22 stsp return err;
263 f6be5c30 2018-06-22 stsp tree->refcnt++;
264 ccfe88e6 2018-07-12 stsp #endif
265 f6be5c30 2018-06-22 stsp return NULL;
266 f6be5c30 2018-06-22 stsp }
267 f6be5c30 2018-06-22 stsp
268 f6be5c30 2018-06-22 stsp struct got_tree_object *
269 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
270 f6be5c30 2018-06-22 stsp struct got_object_id *id)
271 f6be5c30 2018-06-22 stsp {
272 f6be5c30 2018-06-22 stsp struct got_object_cache_entry *ce;
273 f6be5c30 2018-06-22 stsp
274 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->treecache.idcache, id);
275 f6be5c30 2018-06-22 stsp if (ce) {
276 f6be5c30 2018-06-22 stsp repo->treecache.cache_hit++;
277 f6be5c30 2018-06-22 stsp return ce->data.tree;
278 f6be5c30 2018-06-22 stsp }
279 f6be5c30 2018-06-22 stsp
280 f6be5c30 2018-06-22 stsp repo->treecache.cache_miss++;
281 1943de01 2018-06-22 stsp return NULL;
282 1943de01 2018-06-22 stsp }
283 1943de01 2018-06-22 stsp
284 1943de01 2018-06-22 stsp const struct got_error *
285 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
286 1943de01 2018-06-22 stsp struct got_commit_object *commit)
287 1943de01 2018-06-22 stsp {
288 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
289 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
290 1943de01 2018-06-22 stsp err = cache_add(&repo->commitcache, id, commit);
291 1943de01 2018-06-22 stsp if (err)
292 1943de01 2018-06-22 stsp return err;
293 1943de01 2018-06-22 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 1943de01 2018-06-22 stsp struct got_object_cache_entry *ce;
304 f6be5c30 2018-06-22 stsp
305 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->commitcache.idcache, id);
306 1943de01 2018-06-22 stsp if (ce) {
307 1943de01 2018-06-22 stsp repo->commitcache.cache_hit++;
308 1943de01 2018-06-22 stsp return ce->data.commit;
309 1943de01 2018-06-22 stsp }
310 1943de01 2018-06-22 stsp
311 1943de01 2018-06-22 stsp repo->commitcache.cache_miss++;
312 1943de01 2018-06-22 stsp return NULL;
313 1943de01 2018-06-22 stsp }
314 1943de01 2018-06-22 stsp
315 f6be5c30 2018-06-22 stsp const struct got_error *
316 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
317 4027f31a 2017-11-04 stsp {
318 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
319 85f51bba 2018-07-16 stsp struct got_worktree *worktree = NULL;
320 85f51bba 2018-07-16 stsp
321 85f51bba 2018-07-16 stsp /* bare git repository? */
322 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
323 85f51bba 2018-07-16 stsp if (repo->path_git_dir == NULL) {
324 85f51bba 2018-07-16 stsp err = got_error_from_errno();
325 85f51bba 2018-07-16 stsp goto done;
326 85f51bba 2018-07-16 stsp }
327 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
328 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
329 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
330 85f51bba 2018-07-16 stsp err = got_error_from_errno();
331 85f51bba 2018-07-16 stsp goto done;
332 85f51bba 2018-07-16 stsp }
333 85f51bba 2018-07-16 stsp return NULL;
334 85f51bba 2018-07-16 stsp }
335 85f51bba 2018-07-16 stsp
336 85f51bba 2018-07-16 stsp /* git repository with working tree? */
337 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
338 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
339 85f51bba 2018-07-16 stsp err = got_error_from_errno();
340 85f51bba 2018-07-16 stsp goto done;
341 85f51bba 2018-07-16 stsp }
342 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
343 85f51bba 2018-07-16 stsp repo->path = strdup(path);
344 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
345 85f51bba 2018-07-16 stsp err = got_error_from_errno();
346 85f51bba 2018-07-16 stsp goto done;
347 85f51bba 2018-07-16 stsp }
348 85f51bba 2018-07-16 stsp return NULL;
349 85f51bba 2018-07-16 stsp }
350 85f51bba 2018-07-16 stsp
351 85f51bba 2018-07-16 stsp /* got work tree checked out from bare git repository? */
352 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
353 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
354 85f51bba 2018-07-16 stsp err = got_worktree_open(&worktree, path);
355 85f51bba 2018-07-16 stsp if (err) {
356 85f51bba 2018-07-16 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
357 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
358 85f51bba 2018-07-16 stsp goto done;
359 85f51bba 2018-07-16 stsp }
360 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(worktree->repo_path);
361 85f51bba 2018-07-16 stsp if (repo->path_git_dir == NULL) {
362 85f51bba 2018-07-16 stsp err = got_error_from_errno();
363 85f51bba 2018-07-16 stsp goto done;
364 85f51bba 2018-07-16 stsp }
365 85f51bba 2018-07-16 stsp
366 85f51bba 2018-07-16 stsp /* got work tree checked out from git repository with working tree? */
367 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
368 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
369 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
370 85f51bba 2018-07-16 stsp GOT_GIT_DIR) == -1) {
371 85f51bba 2018-07-16 stsp err = got_error_from_errno();
372 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
373 85f51bba 2018-07-16 stsp goto done;
374 85f51bba 2018-07-16 stsp }
375 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
376 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
377 85f51bba 2018-07-16 stsp goto done;
378 85f51bba 2018-07-16 stsp }
379 85f51bba 2018-07-16 stsp repo->path = strdup(worktree->repo_path);
380 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
381 85f51bba 2018-07-16 stsp err = got_error_from_errno();
382 85f51bba 2018-07-16 stsp goto done;
383 85f51bba 2018-07-16 stsp }
384 85f51bba 2018-07-16 stsp } else {
385 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
386 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
387 85f51bba 2018-07-16 stsp err = got_error_from_errno();
388 85f51bba 2018-07-16 stsp goto done;
389 85f51bba 2018-07-16 stsp }
390 85f51bba 2018-07-16 stsp }
391 85f51bba 2018-07-16 stsp done:
392 85f51bba 2018-07-16 stsp if (worktree)
393 85f51bba 2018-07-16 stsp got_worktree_close(worktree);
394 85f51bba 2018-07-16 stsp return err;
395 85f51bba 2018-07-16 stsp }
396 85f51bba 2018-07-16 stsp
397 85f51bba 2018-07-16 stsp const struct got_error *
398 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
399 85f51bba 2018-07-16 stsp {
400 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
401 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
402 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
403 ad242220 2018-09-08 stsp int i, tried_root = 0;
404 4027f31a 2017-11-04 stsp
405 85f51bba 2018-07-16 stsp *repop = NULL;
406 85f51bba 2018-07-16 stsp
407 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
408 2393f13b 2018-03-09 stsp abspath = strdup(path);
409 2393f13b 2018-03-09 stsp else
410 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
411 92af5469 2017-11-05 stsp if (abspath == NULL)
412 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
413 4027f31a 2017-11-04 stsp
414 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
415 92af5469 2017-11-05 stsp if (repo == NULL) {
416 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
417 92af5469 2017-11-05 stsp goto done;
418 92af5469 2017-11-05 stsp }
419 4027f31a 2017-11-04 stsp
420 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
421 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
422 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
423 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
424 ad242220 2018-09-08 stsp }
425 ad242220 2018-09-08 stsp
426 f6be5c30 2018-06-22 stsp repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
427 4307e577 2018-06-22 stsp repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
428 eb77ee11 2018-07-08 stsp repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
429 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache == NULL) {
430 f6be5c30 2018-06-22 stsp err = got_error_from_errno();
431 f6be5c30 2018-06-22 stsp goto done;
432 f6be5c30 2018-06-22 stsp }
433 eb77ee11 2018-07-08 stsp
434 f6be5c30 2018-06-22 stsp repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
435 4307e577 2018-06-22 stsp repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
436 eb77ee11 2018-07-08 stsp repo->treecache.idcache =
437 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->treecache.size);
438 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache == NULL) {
439 1943de01 2018-06-22 stsp err = got_error_from_errno();
440 1943de01 2018-06-22 stsp goto done;
441 1943de01 2018-06-22 stsp }
442 eb77ee11 2018-07-08 stsp
443 1943de01 2018-06-22 stsp repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
444 4307e577 2018-06-22 stsp repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
445 eb77ee11 2018-07-08 stsp repo->commitcache.idcache =
446 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->commitcache.size);
447 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache == NULL) {
448 eb77ee11 2018-07-08 stsp err = got_error_from_errno();
449 eb77ee11 2018-07-08 stsp goto done;
450 eb77ee11 2018-07-08 stsp }
451 1943de01 2018-06-22 stsp
452 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
453 85f51bba 2018-07-16 stsp if (normpath == NULL) {
454 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
455 92af5469 2017-11-05 stsp goto done;
456 92af5469 2017-11-05 stsp }
457 4027f31a 2017-11-04 stsp
458 85f51bba 2018-07-16 stsp path = normpath;
459 85f51bba 2018-07-16 stsp do {
460 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
461 85f51bba 2018-07-16 stsp if (err == NULL)
462 85f51bba 2018-07-16 stsp break;
463 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
464 85f51bba 2018-07-16 stsp break;
465 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
466 85f51bba 2018-07-16 stsp if (tried_root) {
467 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
468 85f51bba 2018-07-16 stsp break;
469 442a3ddc 2018-04-23 stsp }
470 85f51bba 2018-07-16 stsp tried_root = 1;
471 442a3ddc 2018-04-23 stsp }
472 85f51bba 2018-07-16 stsp path = dirname(path);
473 85f51bba 2018-07-16 stsp if (path == NULL)
474 85f51bba 2018-07-16 stsp err = got_error_from_errno();
475 85f51bba 2018-07-16 stsp } while (path);
476 92af5469 2017-11-05 stsp done:
477 92af5469 2017-11-05 stsp if (err)
478 ad242220 2018-09-08 stsp err = got_repo_close(repo);
479 85f51bba 2018-07-16 stsp else
480 85f51bba 2018-07-16 stsp *repop = repo;
481 92af5469 2017-11-05 stsp free(abspath);
482 85f51bba 2018-07-16 stsp free(normpath);
483 92af5469 2017-11-05 stsp return err;
484 4027f31a 2017-11-04 stsp }
485 4027f31a 2017-11-04 stsp
486 cd717821 2018-06-22 stsp #if 0
487 1943de01 2018-06-22 stsp static void
488 1943de01 2018-06-22 stsp print_cache_stats(struct got_object_cache *cache, const char *name)
489 1943de01 2018-06-22 stsp {
490 1943de01 2018-06-22 stsp fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
491 eb77ee11 2018-07-08 stsp name, got_object_idcache_num_elements(cache->idcache),
492 eb77ee11 2018-07-08 stsp cache->cache_hit, cache->cache_miss);
493 cd717821 2018-06-22 stsp }
494 cd717821 2018-06-22 stsp
495 cd717821 2018-06-22 stsp void check_refcount(struct got_object_id *id, void *data, void *arg)
496 cd717821 2018-06-22 stsp {
497 cd717821 2018-06-22 stsp struct got_object_cache *cache = arg;
498 cd717821 2018-06-22 stsp struct got_object_cache_entry *ce = data;
499 cd717821 2018-06-22 stsp struct got_object *obj;
500 cd717821 2018-06-22 stsp struct got_tree_object *tree;
501 cd717821 2018-06-22 stsp struct got_commit_object *commit;
502 cd717821 2018-06-22 stsp char *id_str;
503 cd717821 2018-06-22 stsp
504 cd717821 2018-06-22 stsp if (got_object_id_str(&id_str, id) != NULL)
505 cd717821 2018-06-22 stsp return;
506 cd717821 2018-06-22 stsp
507 cd717821 2018-06-22 stsp switch (cache->type) {
508 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
509 cd717821 2018-06-22 stsp obj = ce->data.obj;
510 cd717821 2018-06-22 stsp if (obj->refcnt == 1)
511 cd717821 2018-06-22 stsp break;
512 cd717821 2018-06-22 stsp fprintf(stderr, "object %s has %d unclaimed references\n",
513 cd717821 2018-06-22 stsp id_str, obj->refcnt - 1);
514 cd717821 2018-06-22 stsp break;
515 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
516 cd717821 2018-06-22 stsp tree = ce->data.tree;
517 cd717821 2018-06-22 stsp if (tree->refcnt == 1)
518 cd717821 2018-06-22 stsp break;
519 cd717821 2018-06-22 stsp fprintf(stderr, "tree %s has %d unclaimed references\n",
520 cd717821 2018-06-22 stsp id_str, tree->refcnt - 1);
521 cd717821 2018-06-22 stsp break;
522 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
523 cd717821 2018-06-22 stsp commit = ce->data.commit;
524 cd717821 2018-06-22 stsp if (commit->refcnt == 1)
525 cd717821 2018-06-22 stsp break;
526 cd717821 2018-06-22 stsp fprintf(stderr, "commit %s has %d unclaimed references\n",
527 cd717821 2018-06-22 stsp id_str, commit->refcnt);
528 cd717821 2018-06-22 stsp break;
529 cd717821 2018-06-22 stsp }
530 cd717821 2018-06-22 stsp free(id_str);
531 1943de01 2018-06-22 stsp }
532 cd717821 2018-06-22 stsp #endif
533 1943de01 2018-06-22 stsp
534 ad242220 2018-09-08 stsp const struct got_error *
535 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
536 4027f31a 2017-11-04 stsp {
537 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
538 79b11c62 2018-03-09 stsp int i;
539 79b11c62 2018-03-09 stsp
540 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
541 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
542 79b11c62 2018-03-09 stsp break;
543 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
544 79b11c62 2018-03-09 stsp }
545 bd1223b9 2018-03-14 stsp
546 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
547 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
548 7e656b93 2018-03-17 stsp break;
549 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
550 7e656b93 2018-03-17 stsp }
551 7e656b93 2018-03-17 stsp
552 4027f31a 2017-11-04 stsp free(repo->path);
553 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
554 cd717821 2018-06-22 stsp
555 cd717821 2018-06-22 stsp #if 0
556 1943de01 2018-06-22 stsp print_cache_stats(&repo->objcache, "object");
557 1943de01 2018-06-22 stsp print_cache_stats(&repo->treecache, "tree");
558 1943de01 2018-06-22 stsp print_cache_stats(&repo->commitcache, "commit");
559 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
560 cd717821 2018-06-22 stsp &repo->objcache);
561 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
562 cd717821 2018-06-22 stsp &repo->treecache);
563 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
564 cd717821 2018-06-22 stsp &repo->commitcache);
565 cd717821 2018-06-22 stsp #endif
566 cd717821 2018-06-22 stsp
567 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache)
568 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->objcache.idcache);
569 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache)
570 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->treecache.idcache);
571 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache)
572 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->commitcache.idcache);
573 ad242220 2018-09-08 stsp
574 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
575 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
576 ad242220 2018-09-08 stsp continue;
577 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
578 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
579 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
580 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
581 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
582 ad242220 2018-09-08 stsp if (child_err && err == NULL)
583 ad242220 2018-09-08 stsp err = child_err;
584 ad242220 2018-09-08 stsp close(repo->privsep_children[i].imsg_fd);
585 ad242220 2018-09-08 stsp }
586 4027f31a 2017-11-04 stsp free(repo);
587 ad242220 2018-09-08 stsp
588 ad242220 2018-09-08 stsp return err;
589 4027f31a 2017-11-04 stsp }
590 04ca23f4 2018-07-16 stsp
591 04ca23f4 2018-07-16 stsp const struct got_error *
592 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
593 04ca23f4 2018-07-16 stsp const char *input_path)
594 04ca23f4 2018-07-16 stsp {
595 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
596 04ca23f4 2018-07-16 stsp char *repo_abspath = NULL, *cwd = NULL;
597 04ca23f4 2018-07-16 stsp struct stat sb;
598 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
599 04ca23f4 2018-07-16 stsp char *canonpath, *path;
600 04ca23f4 2018-07-16 stsp
601 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
602 04ca23f4 2018-07-16 stsp
603 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
604 04ca23f4 2018-07-16 stsp if (cwd == NULL)
605 04ca23f4 2018-07-16 stsp return got_error_from_errno();
606 04ca23f4 2018-07-16 stsp
607 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
608 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
609 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
610 04ca23f4 2018-07-16 stsp goto done;
611 04ca23f4 2018-07-16 stsp }
612 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
613 04ca23f4 2018-07-16 stsp if (err)
614 04ca23f4 2018-07-16 stsp goto done;
615 04ca23f4 2018-07-16 stsp
616 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
617 04ca23f4 2018-07-16 stsp if (repo_abspath == NULL) {
618 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
619 04ca23f4 2018-07-16 stsp goto done;
620 04ca23f4 2018-07-16 stsp }
621 04ca23f4 2018-07-16 stsp
622 04ca23f4 2018-07-16 stsp /* TODO: Call "get in-repository path of work-tree node" API. */
623 04ca23f4 2018-07-16 stsp
624 04ca23f4 2018-07-16 stsp if (lstat(canonpath, &sb) != 0) {
625 04ca23f4 2018-07-16 stsp if (errno != ENOENT) {
626 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
627 04ca23f4 2018-07-16 stsp goto done;
628 04ca23f4 2018-07-16 stsp }
629 04ca23f4 2018-07-16 stsp /*
630 04ca23f4 2018-07-16 stsp * Path is not on disk.
631 04ca23f4 2018-07-16 stsp * Assume it is already relative to repository root.
632 04ca23f4 2018-07-16 stsp */
633 04ca23f4 2018-07-16 stsp path = strdup(canonpath);
634 04ca23f4 2018-07-16 stsp } else {
635 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
636 04ca23f4 2018-07-16 stsp
637 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
638 04ca23f4 2018-07-16 stsp if (path == NULL) {
639 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
640 04ca23f4 2018-07-16 stsp goto done;
641 04ca23f4 2018-07-16 stsp }
642 04ca23f4 2018-07-16 stsp
643 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
644 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
645 04ca23f4 2018-07-16 stsp len = strlen(path);
646 04ca23f4 2018-07-16 stsp
647 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
648 04ca23f4 2018-07-16 stsp is_repo_child = 1;
649 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
650 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
651 04ca23f4 2018-07-16 stsp
652 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
653 04ca23f4 2018-07-16 stsp free(path);
654 04ca23f4 2018-07-16 stsp path = strdup("");
655 04ca23f4 2018-07-16 stsp if (path == NULL) {
656 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
657 04ca23f4 2018-07-16 stsp goto done;
658 04ca23f4 2018-07-16 stsp }
659 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
660 04ca23f4 2018-07-16 stsp char *child;
661 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
662 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
663 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
664 04ca23f4 2018-07-16 stsp repo_abspath, path);
665 04ca23f4 2018-07-16 stsp if (err)
666 04ca23f4 2018-07-16 stsp goto done;
667 04ca23f4 2018-07-16 stsp free(path);
668 04ca23f4 2018-07-16 stsp path = child;
669 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
670 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
671 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
672 04ca23f4 2018-07-16 stsp /*
673 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
674 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
675 04ca23f4 2018-07-16 stsp */
676 04ca23f4 2018-07-16 stsp } else {
677 04ca23f4 2018-07-16 stsp char *child;
678 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
679 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
680 04ca23f4 2018-07-16 stsp repo_abspath, path);
681 04ca23f4 2018-07-16 stsp if (err)
682 04ca23f4 2018-07-16 stsp goto done;
683 04ca23f4 2018-07-16 stsp free(path);
684 04ca23f4 2018-07-16 stsp path = child;
685 04ca23f4 2018-07-16 stsp }
686 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
687 04ca23f4 2018-07-16 stsp char *child;
688 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
689 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
690 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
691 04ca23f4 2018-07-16 stsp path);
692 04ca23f4 2018-07-16 stsp if (err)
693 04ca23f4 2018-07-16 stsp goto done;
694 04ca23f4 2018-07-16 stsp free(path);
695 04ca23f4 2018-07-16 stsp path = child;
696 04ca23f4 2018-07-16 stsp } else {
697 04ca23f4 2018-07-16 stsp /*
698 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
699 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
700 04ca23f4 2018-07-16 stsp */
701 04ca23f4 2018-07-16 stsp }
702 04ca23f4 2018-07-16 stsp }
703 04ca23f4 2018-07-16 stsp
704 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
705 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
706 04ca23f4 2018-07-16 stsp char *abspath;
707 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
708 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
709 04ca23f4 2018-07-16 stsp goto done;
710 04ca23f4 2018-07-16 stsp }
711 04ca23f4 2018-07-16 stsp free(path);
712 04ca23f4 2018-07-16 stsp path = abspath;
713 04ca23f4 2018-07-16 stsp }
714 04ca23f4 2018-07-16 stsp
715 04ca23f4 2018-07-16 stsp done:
716 04ca23f4 2018-07-16 stsp free(repo_abspath);
717 04ca23f4 2018-07-16 stsp free(cwd);
718 04ca23f4 2018-07-16 stsp free(canonpath);
719 04ca23f4 2018-07-16 stsp if (err)
720 04ca23f4 2018-07-16 stsp free(path);
721 04ca23f4 2018-07-16 stsp else
722 04ca23f4 2018-07-16 stsp *in_repo_path = path;
723 1510f469 2018-09-09 stsp return err;
724 1510f469 2018-09-09 stsp }
725 1510f469 2018-09-09 stsp
726 1510f469 2018-09-09 stsp const struct got_error *
727 1510f469 2018-09-09 stsp got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
728 1510f469 2018-09-09 stsp {
729 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
730 1510f469 2018-09-09 stsp int i;
731 1510f469 2018-09-09 stsp
732 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
733 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
734 1510f469 2018-09-09 stsp break;
735 1510f469 2018-09-09 stsp }
736 1510f469 2018-09-09 stsp
737 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
738 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
739 1510f469 2018-09-09 stsp if (err)
740 1510f469 2018-09-09 stsp return err;
741 1510f469 2018-09-09 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
742 1510f469 2018-09-09 stsp sizeof(repo->packidx_cache) -
743 1510f469 2018-09-09 stsp sizeof(repo->packidx_cache[0]));
744 1510f469 2018-09-09 stsp i = 0;
745 1510f469 2018-09-09 stsp }
746 1510f469 2018-09-09 stsp
747 1510f469 2018-09-09 stsp repo->packidx_cache[i] = packidx;
748 1510f469 2018-09-09 stsp return NULL;
749 1510f469 2018-09-09 stsp }
750 1510f469 2018-09-09 stsp
751 1510f469 2018-09-09 stsp static int
752 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
753 1510f469 2018-09-09 stsp {
754 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
755 1510f469 2018-09-09 stsp return 0;
756 1510f469 2018-09-09 stsp
757 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
758 1510f469 2018-09-09 stsp return 0;
759 1510f469 2018-09-09 stsp
760 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
761 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
762 1510f469 2018-09-09 stsp return 0;
763 1510f469 2018-09-09 stsp
764 1510f469 2018-09-09 stsp return 1;
765 1510f469 2018-09-09 stsp }
766 1510f469 2018-09-09 stsp
767 1510f469 2018-09-09 stsp const struct got_error *
768 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
769 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
770 1510f469 2018-09-09 stsp {
771 1510f469 2018-09-09 stsp const struct got_error *err;
772 1510f469 2018-09-09 stsp char *path_packdir;
773 1510f469 2018-09-09 stsp DIR *packdir;
774 1510f469 2018-09-09 stsp struct dirent *dent;
775 1510f469 2018-09-09 stsp char *path_packidx;
776 1510f469 2018-09-09 stsp int i;
777 1510f469 2018-09-09 stsp
778 1510f469 2018-09-09 stsp /* Search pack index cache. */
779 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
780 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
781 1510f469 2018-09-09 stsp break;
782 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
783 1510f469 2018-09-09 stsp if (*idx != -1) {
784 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
785 1510f469 2018-09-09 stsp return NULL;
786 1510f469 2018-09-09 stsp }
787 1510f469 2018-09-09 stsp }
788 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
789 1510f469 2018-09-09 stsp
790 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
791 1510f469 2018-09-09 stsp if (path_packdir == NULL)
792 1510f469 2018-09-09 stsp return got_error_from_errno();
793 1510f469 2018-09-09 stsp
794 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
795 1510f469 2018-09-09 stsp if (packdir == NULL) {
796 1510f469 2018-09-09 stsp err = got_error_from_errno();
797 1510f469 2018-09-09 stsp goto done;
798 1510f469 2018-09-09 stsp }
799 1510f469 2018-09-09 stsp
800 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
801 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
802 1510f469 2018-09-09 stsp continue;
803 1510f469 2018-09-09 stsp
804 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
805 1510f469 2018-09-09 stsp dent->d_name) == -1) {
806 1510f469 2018-09-09 stsp err = got_error_from_errno();
807 1510f469 2018-09-09 stsp goto done;
808 1510f469 2018-09-09 stsp }
809 1510f469 2018-09-09 stsp
810 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
811 1510f469 2018-09-09 stsp free(path_packidx);
812 1510f469 2018-09-09 stsp if (err)
813 1510f469 2018-09-09 stsp goto done;
814 1510f469 2018-09-09 stsp
815 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
816 1510f469 2018-09-09 stsp if (*idx != -1) {
817 1510f469 2018-09-09 stsp err = NULL; /* found the object */
818 1510f469 2018-09-09 stsp err = got_repo_cache_packidx(repo, *packidx);
819 1510f469 2018-09-09 stsp goto done;
820 1510f469 2018-09-09 stsp }
821 1510f469 2018-09-09 stsp
822 1510f469 2018-09-09 stsp err = got_packidx_close(*packidx);
823 1510f469 2018-09-09 stsp *packidx = NULL;
824 1510f469 2018-09-09 stsp if (err)
825 1510f469 2018-09-09 stsp goto done;
826 1510f469 2018-09-09 stsp }
827 1510f469 2018-09-09 stsp
828 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_NO_OBJ);
829 1510f469 2018-09-09 stsp done:
830 1510f469 2018-09-09 stsp free(path_packdir);
831 1510f469 2018-09-09 stsp if (packdir && closedir(packdir) != 0 && err == 0)
832 1510f469 2018-09-09 stsp err = got_error_from_errno();
833 04ca23f4 2018-07-16 stsp return err;
834 04ca23f4 2018-07-16 stsp }
835 1510f469 2018-09-09 stsp
836 1510f469 2018-09-09 stsp static const struct got_error *
837 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
838 1510f469 2018-09-09 stsp {
839 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
840 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
841 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
842 1510f469 2018-09-09 stsp ssize_t n;
843 1510f469 2018-09-09 stsp
844 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
845 1510f469 2018-09-09 stsp if (n < 0)
846 1510f469 2018-09-09 stsp return got_error_from_errno();
847 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
848 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
849 1510f469 2018-09-09 stsp
850 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
851 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
852 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
853 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
854 1510f469 2018-09-09 stsp
855 1510f469 2018-09-09 stsp return err;
856 1510f469 2018-09-09 stsp }
857 1510f469 2018-09-09 stsp
858 1510f469 2018-09-09 stsp static const struct got_error *
859 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
860 1510f469 2018-09-09 stsp {
861 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
862 1510f469 2018-09-09 stsp
863 1510f469 2018-09-09 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
864 1510f469 2018-09-09 stsp if (*fd == -1)
865 1510f469 2018-09-09 stsp return got_error_from_errno();
866 1510f469 2018-09-09 stsp
867 1510f469 2018-09-09 stsp if (packidx) {
868 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
869 1510f469 2018-09-09 stsp if (err) {
870 1510f469 2018-09-09 stsp close(*fd);
871 1510f469 2018-09-09 stsp *fd = -1;
872 1510f469 2018-09-09 stsp }
873 1510f469 2018-09-09 stsp }
874 1510f469 2018-09-09 stsp
875 1510f469 2018-09-09 stsp return err;
876 1510f469 2018-09-09 stsp }
877 1510f469 2018-09-09 stsp
878 1510f469 2018-09-09 stsp const struct got_error *
879 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
880 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
881 1510f469 2018-09-09 stsp {
882 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
883 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
884 1510f469 2018-09-09 stsp int i;
885 1510f469 2018-09-09 stsp
886 1510f469 2018-09-09 stsp if (packp)
887 1510f469 2018-09-09 stsp *packp = NULL;
888 1510f469 2018-09-09 stsp
889 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
890 1510f469 2018-09-09 stsp pack = &repo->packs[i];
891 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
892 1510f469 2018-09-09 stsp break;
893 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
894 1510f469 2018-09-09 stsp return NULL;
895 1510f469 2018-09-09 stsp }
896 1510f469 2018-09-09 stsp
897 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
898 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
899 1510f469 2018-09-09 stsp if (err)
900 1510f469 2018-09-09 stsp return err;
901 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
902 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
903 1510f469 2018-09-09 stsp i = 0;
904 1510f469 2018-09-09 stsp }
905 1510f469 2018-09-09 stsp
906 1510f469 2018-09-09 stsp pack = &repo->packs[i];
907 1510f469 2018-09-09 stsp
908 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
909 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
910 1510f469 2018-09-09 stsp err = got_error_from_errno();
911 1510f469 2018-09-09 stsp goto done;
912 1510f469 2018-09-09 stsp }
913 1510f469 2018-09-09 stsp
914 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
915 1510f469 2018-09-09 stsp if (err)
916 1510f469 2018-09-09 stsp goto done;
917 1510f469 2018-09-09 stsp
918 1510f469 2018-09-09 stsp err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
919 1510f469 2018-09-09 stsp if (err)
920 1510f469 2018-09-09 stsp goto done;
921 1510f469 2018-09-09 stsp
922 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
923 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
924 1510f469 2018-09-09 stsp pack->fd, 0);
925 1510f469 2018-09-09 stsp if (pack->map == MAP_FAILED)
926 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
927 1510f469 2018-09-09 stsp #endif
928 1510f469 2018-09-09 stsp done:
929 1510f469 2018-09-09 stsp if (err) {
930 1510f469 2018-09-09 stsp if (pack) {
931 1510f469 2018-09-09 stsp free(pack->path_packfile);
932 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
933 1510f469 2018-09-09 stsp }
934 1510f469 2018-09-09 stsp } else if (packp)
935 1510f469 2018-09-09 stsp *packp = pack;
936 1510f469 2018-09-09 stsp return err;
937 1510f469 2018-09-09 stsp }
938 1510f469 2018-09-09 stsp
939 1510f469 2018-09-09 stsp struct got_pack *
940 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
941 1510f469 2018-09-09 stsp {
942 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
943 1510f469 2018-09-09 stsp int i;
944 1510f469 2018-09-09 stsp
945 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
946 1510f469 2018-09-09 stsp pack = &repo->packs[i];
947 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
948 1510f469 2018-09-09 stsp break;
949 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
950 1510f469 2018-09-09 stsp return pack;
951 1510f469 2018-09-09 stsp }
952 1510f469 2018-09-09 stsp
953 1510f469 2018-09-09 stsp return NULL;
954 1510f469 2018-09-09 stsp }