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 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 c442a90d 2019-03-10 stsp #include <uuid.h>
37 4027f31a 2017-11-04 stsp
38 4027f31a 2017-11-04 stsp #include "got_error.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 4027f31a 2017-11-04 stsp #include "got_repository.h"
41 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
42 7bb0daa1 2018-06-21 stsp #include "got_object.h"
43 324d37e7 2019-05-11 stsp #include "got_path.h"
44 4027f31a 2017-11-04 stsp
45 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
49 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
50 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
53 c3f94f68 2017-11-05 stsp
54 79b11c62 2018-03-09 stsp #ifndef nitems
55 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 79b11c62 2018-03-09 stsp #endif
57 3b339b2f 2018-02-12 stsp
58 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
59 4027f31a 2017-11-04 stsp
60 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
61 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
62 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
63 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
64 4027f31a 2017-11-04 stsp
65 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
66 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
67 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
68 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
69 fb79db15 2019-02-01 stsp #define GOT_PACKED_REFS_FILE "packed-refs"
70 4df642d9 2017-11-05 stsp
71 7839bc15 2019-01-06 stsp const char *
72 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
73 86c3caaf 2018-03-09 stsp {
74 7839bc15 2019-01-06 stsp return repo->path;
75 86c3caaf 2018-03-09 stsp }
76 86c3caaf 2018-03-09 stsp
77 6e9da951 2019-01-06 stsp const char *
78 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
79 4027f31a 2017-11-04 stsp {
80 6e9da951 2019-01-06 stsp return repo->path_git_dir;
81 04ca23f4 2018-07-16 stsp }
82 04ca23f4 2018-07-16 stsp
83 04ca23f4 2018-07-16 stsp int
84 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
85 04ca23f4 2018-07-16 stsp {
86 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
87 4027f31a 2017-11-04 stsp }
88 4027f31a 2017-11-04 stsp
89 4027f31a 2017-11-04 stsp static char *
90 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
91 4027f31a 2017-11-04 stsp {
92 4027f31a 2017-11-04 stsp char *path_child;
93 4027f31a 2017-11-04 stsp
94 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
95 4027f31a 2017-11-04 stsp basename) == -1)
96 4027f31a 2017-11-04 stsp return NULL;
97 4027f31a 2017-11-04 stsp
98 4027f31a 2017-11-04 stsp return path_child;
99 4027f31a 2017-11-04 stsp }
100 4027f31a 2017-11-04 stsp
101 11995603 2017-11-05 stsp char *
102 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
103 4027f31a 2017-11-04 stsp {
104 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
105 4027f31a 2017-11-04 stsp }
106 4027f31a 2017-11-04 stsp
107 11995603 2017-11-05 stsp char *
108 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
109 a1fd68d8 2018-01-12 stsp {
110 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
111 a1fd68d8 2018-01-12 stsp }
112 a1fd68d8 2018-01-12 stsp
113 a1fd68d8 2018-01-12 stsp char *
114 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
115 4027f31a 2017-11-04 stsp {
116 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
117 4027f31a 2017-11-04 stsp }
118 4027f31a 2017-11-04 stsp
119 fb79db15 2019-02-01 stsp char *
120 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
121 fb79db15 2019-02-01 stsp {
122 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
123 fb79db15 2019-02-01 stsp }
124 fb79db15 2019-02-01 stsp
125 4027f31a 2017-11-04 stsp static char *
126 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
127 4027f31a 2017-11-04 stsp {
128 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
129 4027f31a 2017-11-04 stsp }
130 4027f31a 2017-11-04 stsp
131 4027f31a 2017-11-04 stsp static int
132 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
133 4027f31a 2017-11-04 stsp {
134 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
135 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
136 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
137 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
138 deeca238 2018-03-12 stsp int ret = 0;
139 deeca238 2018-03-12 stsp struct stat sb;
140 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
141 4027f31a 2017-11-04 stsp
142 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
143 deeca238 2018-03-12 stsp goto done;
144 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
145 deeca238 2018-03-12 stsp goto done;
146 4027f31a 2017-11-04 stsp
147 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
148 deeca238 2018-03-12 stsp goto done;
149 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
150 deeca238 2018-03-12 stsp goto done;
151 deeca238 2018-03-12 stsp
152 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
153 deeca238 2018-03-12 stsp goto done;
154 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
155 deeca238 2018-03-12 stsp goto done;
156 deeca238 2018-03-12 stsp
157 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
158 deeca238 2018-03-12 stsp goto done;
159 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
160 deeca238 2018-03-12 stsp goto done;
161 4847cca1 2018-03-12 stsp
162 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
163 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
164 4847cca1 2018-03-12 stsp goto done;
165 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
166 4847cca1 2018-03-12 stsp
167 deeca238 2018-03-12 stsp ret = 1;
168 deeca238 2018-03-12 stsp done:
169 4027f31a 2017-11-04 stsp free(path_objects);
170 4027f31a 2017-11-04 stsp free(path_refs);
171 4027f31a 2017-11-04 stsp free(path_head);
172 4027f31a 2017-11-04 stsp return ret;
173 4027f31a 2017-11-04 stsp
174 7bb0daa1 2018-06-21 stsp }
175 7bb0daa1 2018-06-21 stsp
176 f6be5c30 2018-06-22 stsp const struct got_error *
177 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
178 f6be5c30 2018-06-22 stsp struct got_object *obj)
179 f6be5c30 2018-06-22 stsp {
180 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
181 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
182 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
183 f6be5c30 2018-06-22 stsp if (err)
184 f6be5c30 2018-06-22 stsp return err;
185 f6be5c30 2018-06-22 stsp obj->refcnt++;
186 ccfe88e6 2018-07-12 stsp #endif
187 f6be5c30 2018-06-22 stsp return NULL;
188 f6be5c30 2018-06-22 stsp }
189 f6be5c30 2018-06-22 stsp
190 7bb0daa1 2018-06-21 stsp struct got_object *
191 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
192 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
193 7bb0daa1 2018-06-21 stsp {
194 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
195 7bb0daa1 2018-06-21 stsp }
196 7bb0daa1 2018-06-21 stsp
197 4027f31a 2017-11-04 stsp const struct got_error *
198 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
199 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
200 f6be5c30 2018-06-22 stsp {
201 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
202 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
203 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
204 f6be5c30 2018-06-22 stsp if (err)
205 f6be5c30 2018-06-22 stsp return err;
206 f6be5c30 2018-06-22 stsp tree->refcnt++;
207 ccfe88e6 2018-07-12 stsp #endif
208 f6be5c30 2018-06-22 stsp return NULL;
209 f6be5c30 2018-06-22 stsp }
210 f6be5c30 2018-06-22 stsp
211 f6be5c30 2018-06-22 stsp struct got_tree_object *
212 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
213 f6be5c30 2018-06-22 stsp struct got_object_id *id)
214 f6be5c30 2018-06-22 stsp {
215 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
216 6bef87be 2018-09-11 stsp &repo->treecache, id);
217 1943de01 2018-06-22 stsp }
218 1943de01 2018-06-22 stsp
219 1943de01 2018-06-22 stsp const struct got_error *
220 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
221 1943de01 2018-06-22 stsp struct got_commit_object *commit)
222 1943de01 2018-06-22 stsp {
223 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
224 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
225 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
226 1943de01 2018-06-22 stsp if (err)
227 1943de01 2018-06-22 stsp return err;
228 1943de01 2018-06-22 stsp commit->refcnt++;
229 ccfe88e6 2018-07-12 stsp #endif
230 f6be5c30 2018-06-22 stsp return NULL;
231 f6be5c30 2018-06-22 stsp }
232 f6be5c30 2018-06-22 stsp
233 1943de01 2018-06-22 stsp struct got_commit_object *
234 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
235 1943de01 2018-06-22 stsp struct got_object_id *id)
236 1943de01 2018-06-22 stsp {
237 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
238 6bef87be 2018-09-11 stsp &repo->commitcache, id);
239 f4a881ce 2018-11-17 stsp }
240 f4a881ce 2018-11-17 stsp
241 f4a881ce 2018-11-17 stsp const struct got_error *
242 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
243 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
244 f4a881ce 2018-11-17 stsp {
245 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
246 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
247 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
248 f4a881ce 2018-11-17 stsp if (err)
249 f4a881ce 2018-11-17 stsp return err;
250 f4a881ce 2018-11-17 stsp tag->refcnt++;
251 f4a881ce 2018-11-17 stsp #endif
252 f4a881ce 2018-11-17 stsp return NULL;
253 f4a881ce 2018-11-17 stsp }
254 f4a881ce 2018-11-17 stsp
255 f4a881ce 2018-11-17 stsp struct got_tag_object *
256 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
257 f4a881ce 2018-11-17 stsp {
258 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
259 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
260 1943de01 2018-06-22 stsp }
261 1943de01 2018-06-22 stsp
262 f6be5c30 2018-06-22 stsp const struct got_error *
263 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
264 4027f31a 2017-11-04 stsp {
265 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
266 85f51bba 2018-07-16 stsp
267 85f51bba 2018-07-16 stsp /* bare git repository? */
268 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
269 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
270 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
271 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
272 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
273 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
274 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
275 85f51bba 2018-07-16 stsp goto done;
276 85f51bba 2018-07-16 stsp }
277 85f51bba 2018-07-16 stsp return NULL;
278 85f51bba 2018-07-16 stsp }
279 85f51bba 2018-07-16 stsp
280 85f51bba 2018-07-16 stsp /* git repository with working tree? */
281 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
282 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
283 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
284 85f51bba 2018-07-16 stsp goto done;
285 85f51bba 2018-07-16 stsp }
286 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
287 85f51bba 2018-07-16 stsp repo->path = strdup(path);
288 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
289 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
290 85f51bba 2018-07-16 stsp goto done;
291 85f51bba 2018-07-16 stsp }
292 85f51bba 2018-07-16 stsp return NULL;
293 85f51bba 2018-07-16 stsp }
294 85f51bba 2018-07-16 stsp
295 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
296 ee645855 2019-02-05 stsp done:
297 85f51bba 2018-07-16 stsp if (err) {
298 ee645855 2019-02-05 stsp free(repo->path);
299 ee645855 2019-02-05 stsp repo->path = NULL;
300 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
301 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
302 85f51bba 2018-07-16 stsp }
303 85f51bba 2018-07-16 stsp return err;
304 85f51bba 2018-07-16 stsp }
305 85f51bba 2018-07-16 stsp
306 85f51bba 2018-07-16 stsp const struct got_error *
307 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
308 85f51bba 2018-07-16 stsp {
309 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
310 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
311 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
312 ad242220 2018-09-08 stsp int i, tried_root = 0;
313 4027f31a 2017-11-04 stsp
314 85f51bba 2018-07-16 stsp *repop = NULL;
315 85f51bba 2018-07-16 stsp
316 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
317 2393f13b 2018-03-09 stsp abspath = strdup(path);
318 2393f13b 2018-03-09 stsp else
319 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
320 92af5469 2017-11-05 stsp if (abspath == NULL)
321 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
322 4027f31a 2017-11-04 stsp
323 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
324 92af5469 2017-11-05 stsp if (repo == NULL) {
325 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("calloc");
326 92af5469 2017-11-05 stsp goto done;
327 92af5469 2017-11-05 stsp }
328 4027f31a 2017-11-04 stsp
329 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
330 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
331 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
332 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
333 ad242220 2018-09-08 stsp }
334 ad242220 2018-09-08 stsp
335 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
336 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
337 6bef87be 2018-09-11 stsp if (err)
338 f6be5c30 2018-06-22 stsp goto done;
339 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
340 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
341 6bef87be 2018-09-11 stsp if (err)
342 1943de01 2018-06-22 stsp goto done;
343 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
344 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
345 6bef87be 2018-09-11 stsp if (err)
346 eb77ee11 2018-07-08 stsp goto done;
347 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
348 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
349 f4a881ce 2018-11-17 stsp if (err)
350 f4a881ce 2018-11-17 stsp goto done;
351 1943de01 2018-06-22 stsp
352 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
353 85f51bba 2018-07-16 stsp if (normpath == NULL) {
354 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
355 92af5469 2017-11-05 stsp goto done;
356 92af5469 2017-11-05 stsp }
357 4027f31a 2017-11-04 stsp
358 85f51bba 2018-07-16 stsp path = normpath;
359 85f51bba 2018-07-16 stsp do {
360 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
361 85f51bba 2018-07-16 stsp if (err == NULL)
362 85f51bba 2018-07-16 stsp break;
363 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
364 85f51bba 2018-07-16 stsp break;
365 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
366 85f51bba 2018-07-16 stsp if (tried_root) {
367 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
368 85f51bba 2018-07-16 stsp break;
369 442a3ddc 2018-04-23 stsp }
370 85f51bba 2018-07-16 stsp tried_root = 1;
371 442a3ddc 2018-04-23 stsp }
372 85f51bba 2018-07-16 stsp path = dirname(path);
373 85f51bba 2018-07-16 stsp if (path == NULL)
374 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("dirname", path);
375 85f51bba 2018-07-16 stsp } while (path);
376 92af5469 2017-11-05 stsp done:
377 92af5469 2017-11-05 stsp if (err)
378 5c2f5761 2018-09-19 stsp got_repo_close(repo);
379 85f51bba 2018-07-16 stsp else
380 85f51bba 2018-07-16 stsp *repop = repo;
381 92af5469 2017-11-05 stsp free(abspath);
382 85f51bba 2018-07-16 stsp free(normpath);
383 92af5469 2017-11-05 stsp return err;
384 4027f31a 2017-11-04 stsp }
385 4027f31a 2017-11-04 stsp
386 ad242220 2018-09-08 stsp const struct got_error *
387 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
388 4027f31a 2017-11-04 stsp {
389 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
390 79b11c62 2018-03-09 stsp int i;
391 79b11c62 2018-03-09 stsp
392 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
393 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
394 79b11c62 2018-03-09 stsp break;
395 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
396 79b11c62 2018-03-09 stsp }
397 bd1223b9 2018-03-14 stsp
398 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
399 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
400 7e656b93 2018-03-17 stsp break;
401 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
402 7e656b93 2018-03-17 stsp }
403 7e656b93 2018-03-17 stsp
404 4027f31a 2017-11-04 stsp free(repo->path);
405 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
406 cd717821 2018-06-22 stsp
407 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
408 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
409 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
410 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
411 ad242220 2018-09-08 stsp
412 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
413 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
414 ad242220 2018-09-08 stsp continue;
415 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
416 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
417 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
418 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
419 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
420 ad242220 2018-09-08 stsp if (child_err && err == NULL)
421 ad242220 2018-09-08 stsp err = child_err;
422 3a6ce05a 2019-02-11 stsp if (close(repo->privsep_children[i].imsg_fd) != 0 &&
423 3a6ce05a 2019-02-11 stsp err == NULL)
424 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("close");
425 ad242220 2018-09-08 stsp }
426 4027f31a 2017-11-04 stsp free(repo);
427 ad242220 2018-09-08 stsp
428 ad242220 2018-09-08 stsp return err;
429 4027f31a 2017-11-04 stsp }
430 04ca23f4 2018-07-16 stsp
431 04ca23f4 2018-07-16 stsp const struct got_error *
432 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
433 23721109 2018-10-22 stsp const char *input_path, int check_disk)
434 04ca23f4 2018-07-16 stsp {
435 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
436 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
437 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
438 7839bc15 2019-01-06 stsp char *cwd, *canonpath, *path = NULL;
439 04ca23f4 2018-07-16 stsp
440 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
441 04ca23f4 2018-07-16 stsp
442 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
443 04ca23f4 2018-07-16 stsp if (cwd == NULL)
444 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
445 04ca23f4 2018-07-16 stsp
446 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
447 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
448 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
449 04ca23f4 2018-07-16 stsp goto done;
450 04ca23f4 2018-07-16 stsp }
451 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
452 04ca23f4 2018-07-16 stsp if (err)
453 04ca23f4 2018-07-16 stsp goto done;
454 04ca23f4 2018-07-16 stsp
455 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
456 04ca23f4 2018-07-16 stsp
457 b70703ad 2019-03-18 stsp if (!check_disk) {
458 23721109 2018-10-22 stsp path = strdup(canonpath);
459 b70703ad 2019-03-18 stsp if (path == NULL) {
460 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
461 04ca23f4 2018-07-16 stsp goto done;
462 04ca23f4 2018-07-16 stsp }
463 04ca23f4 2018-07-16 stsp } else {
464 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
465 04ca23f4 2018-07-16 stsp
466 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
467 04ca23f4 2018-07-16 stsp if (path == NULL) {
468 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
469 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("realpath",
470 230a42bd 2019-05-11 jcs canonpath);
471 b70703ad 2019-03-18 stsp goto done;
472 b70703ad 2019-03-18 stsp }
473 b70703ad 2019-03-18 stsp /*
474 b70703ad 2019-03-18 stsp * Path is not on disk.
475 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
476 b70703ad 2019-03-18 stsp */
477 b70703ad 2019-03-18 stsp path = strdup(canonpath);
478 b70703ad 2019-03-18 stsp if (path == NULL) {
479 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
480 b70703ad 2019-03-18 stsp goto done;
481 b70703ad 2019-03-18 stsp }
482 04ca23f4 2018-07-16 stsp }
483 04ca23f4 2018-07-16 stsp
484 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
485 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
486 04ca23f4 2018-07-16 stsp len = strlen(path);
487 04ca23f4 2018-07-16 stsp
488 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
489 04ca23f4 2018-07-16 stsp is_repo_child = 1;
490 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
491 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
492 04ca23f4 2018-07-16 stsp
493 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
494 04ca23f4 2018-07-16 stsp free(path);
495 04ca23f4 2018-07-16 stsp path = strdup("");
496 04ca23f4 2018-07-16 stsp if (path == NULL) {
497 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
498 04ca23f4 2018-07-16 stsp goto done;
499 04ca23f4 2018-07-16 stsp }
500 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
501 04ca23f4 2018-07-16 stsp char *child;
502 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
503 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
504 04ca23f4 2018-07-16 stsp repo_abspath, path);
505 04ca23f4 2018-07-16 stsp if (err)
506 04ca23f4 2018-07-16 stsp goto done;
507 04ca23f4 2018-07-16 stsp free(path);
508 04ca23f4 2018-07-16 stsp path = child;
509 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
510 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
511 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
512 04ca23f4 2018-07-16 stsp /*
513 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
514 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
515 04ca23f4 2018-07-16 stsp */
516 04ca23f4 2018-07-16 stsp } else {
517 04ca23f4 2018-07-16 stsp char *child;
518 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
519 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
520 04ca23f4 2018-07-16 stsp repo_abspath, path);
521 04ca23f4 2018-07-16 stsp if (err)
522 04ca23f4 2018-07-16 stsp goto done;
523 04ca23f4 2018-07-16 stsp free(path);
524 04ca23f4 2018-07-16 stsp path = child;
525 04ca23f4 2018-07-16 stsp }
526 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
527 04ca23f4 2018-07-16 stsp char *child;
528 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
529 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
530 04ca23f4 2018-07-16 stsp path);
531 04ca23f4 2018-07-16 stsp if (err)
532 04ca23f4 2018-07-16 stsp goto done;
533 04ca23f4 2018-07-16 stsp free(path);
534 04ca23f4 2018-07-16 stsp path = child;
535 04ca23f4 2018-07-16 stsp } else {
536 04ca23f4 2018-07-16 stsp /*
537 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
538 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
539 04ca23f4 2018-07-16 stsp */
540 04ca23f4 2018-07-16 stsp }
541 04ca23f4 2018-07-16 stsp }
542 04ca23f4 2018-07-16 stsp
543 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
544 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
545 04ca23f4 2018-07-16 stsp char *abspath;
546 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
547 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
548 04ca23f4 2018-07-16 stsp goto done;
549 04ca23f4 2018-07-16 stsp }
550 04ca23f4 2018-07-16 stsp free(path);
551 04ca23f4 2018-07-16 stsp path = abspath;
552 04ca23f4 2018-07-16 stsp }
553 04ca23f4 2018-07-16 stsp
554 04ca23f4 2018-07-16 stsp done:
555 04ca23f4 2018-07-16 stsp free(cwd);
556 04ca23f4 2018-07-16 stsp free(canonpath);
557 04ca23f4 2018-07-16 stsp if (err)
558 04ca23f4 2018-07-16 stsp free(path);
559 04ca23f4 2018-07-16 stsp else
560 04ca23f4 2018-07-16 stsp *in_repo_path = path;
561 1510f469 2018-09-09 stsp return err;
562 1510f469 2018-09-09 stsp }
563 1510f469 2018-09-09 stsp
564 1510f469 2018-09-09 stsp const struct got_error *
565 1510f469 2018-09-09 stsp got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
566 1510f469 2018-09-09 stsp {
567 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
568 1510f469 2018-09-09 stsp int i;
569 1510f469 2018-09-09 stsp
570 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
571 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
572 1510f469 2018-09-09 stsp break;
573 1510f469 2018-09-09 stsp }
574 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
575 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
576 1510f469 2018-09-09 stsp if (err)
577 1510f469 2018-09-09 stsp return err;
578 1510f469 2018-09-09 stsp }
579 1510f469 2018-09-09 stsp
580 15fe583f 2018-11-05 stsp /*
581 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
582 15fe583f 2018-11-05 stsp * be searched first in the future.
583 15fe583f 2018-11-05 stsp */
584 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
585 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
586 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
587 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
588 15fe583f 2018-11-05 stsp
589 1510f469 2018-09-09 stsp return NULL;
590 1510f469 2018-09-09 stsp }
591 1510f469 2018-09-09 stsp
592 1510f469 2018-09-09 stsp static int
593 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
594 1510f469 2018-09-09 stsp {
595 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
596 1510f469 2018-09-09 stsp return 0;
597 1510f469 2018-09-09 stsp
598 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
599 1510f469 2018-09-09 stsp return 0;
600 1510f469 2018-09-09 stsp
601 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
602 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
603 1510f469 2018-09-09 stsp return 0;
604 1510f469 2018-09-09 stsp
605 1510f469 2018-09-09 stsp return 1;
606 1510f469 2018-09-09 stsp }
607 1510f469 2018-09-09 stsp
608 1510f469 2018-09-09 stsp const struct got_error *
609 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
610 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
611 1510f469 2018-09-09 stsp {
612 1510f469 2018-09-09 stsp const struct got_error *err;
613 1510f469 2018-09-09 stsp char *path_packdir;
614 1510f469 2018-09-09 stsp DIR *packdir;
615 1510f469 2018-09-09 stsp struct dirent *dent;
616 1510f469 2018-09-09 stsp char *path_packidx;
617 1510f469 2018-09-09 stsp int i;
618 1510f469 2018-09-09 stsp
619 1510f469 2018-09-09 stsp /* Search pack index cache. */
620 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
621 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
622 1510f469 2018-09-09 stsp break;
623 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
624 1510f469 2018-09-09 stsp if (*idx != -1) {
625 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
626 1510f469 2018-09-09 stsp return NULL;
627 1510f469 2018-09-09 stsp }
628 1510f469 2018-09-09 stsp }
629 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
630 1510f469 2018-09-09 stsp
631 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
632 1510f469 2018-09-09 stsp if (path_packdir == NULL)
633 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_repo_get_path_objects_pack");
634 1510f469 2018-09-09 stsp
635 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
636 1510f469 2018-09-09 stsp if (packdir == NULL) {
637 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("opendir", path_packdir);
638 1510f469 2018-09-09 stsp goto done;
639 1510f469 2018-09-09 stsp }
640 1510f469 2018-09-09 stsp
641 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
642 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
643 1510f469 2018-09-09 stsp continue;
644 1510f469 2018-09-09 stsp
645 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
646 1510f469 2018-09-09 stsp dent->d_name) == -1) {
647 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
648 1510f469 2018-09-09 stsp goto done;
649 1510f469 2018-09-09 stsp }
650 1510f469 2018-09-09 stsp
651 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
652 1510f469 2018-09-09 stsp free(path_packidx);
653 1510f469 2018-09-09 stsp if (err)
654 1510f469 2018-09-09 stsp goto done;
655 1510f469 2018-09-09 stsp
656 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
657 1510f469 2018-09-09 stsp if (*idx != -1) {
658 1510f469 2018-09-09 stsp err = NULL; /* found the object */
659 1510f469 2018-09-09 stsp err = got_repo_cache_packidx(repo, *packidx);
660 1510f469 2018-09-09 stsp goto done;
661 1510f469 2018-09-09 stsp }
662 1510f469 2018-09-09 stsp
663 1510f469 2018-09-09 stsp err = got_packidx_close(*packidx);
664 1510f469 2018-09-09 stsp *packidx = NULL;
665 1510f469 2018-09-09 stsp if (err)
666 1510f469 2018-09-09 stsp goto done;
667 1510f469 2018-09-09 stsp }
668 1510f469 2018-09-09 stsp
669 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
670 1510f469 2018-09-09 stsp done:
671 1510f469 2018-09-09 stsp free(path_packdir);
672 1510f469 2018-09-09 stsp if (packdir && closedir(packdir) != 0 && err == 0)
673 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("closedir");
674 04ca23f4 2018-07-16 stsp return err;
675 04ca23f4 2018-07-16 stsp }
676 1510f469 2018-09-09 stsp
677 1510f469 2018-09-09 stsp static const struct got_error *
678 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
679 1510f469 2018-09-09 stsp {
680 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
681 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
682 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
683 1510f469 2018-09-09 stsp ssize_t n;
684 1510f469 2018-09-09 stsp
685 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
686 1510f469 2018-09-09 stsp if (n < 0)
687 230a42bd 2019-05-11 jcs return got_error_prefix_errno("read");
688 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
689 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
690 1510f469 2018-09-09 stsp
691 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
692 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
693 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
694 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
695 1510f469 2018-09-09 stsp
696 1510f469 2018-09-09 stsp return err;
697 1510f469 2018-09-09 stsp }
698 1510f469 2018-09-09 stsp
699 1510f469 2018-09-09 stsp static const struct got_error *
700 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
701 1510f469 2018-09-09 stsp {
702 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
703 1510f469 2018-09-09 stsp
704 a5b57ccf 2019-04-11 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
705 1510f469 2018-09-09 stsp if (*fd == -1)
706 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("open", path_packfile);
707 1510f469 2018-09-09 stsp
708 1510f469 2018-09-09 stsp if (packidx) {
709 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
710 1510f469 2018-09-09 stsp if (err) {
711 1510f469 2018-09-09 stsp close(*fd);
712 1510f469 2018-09-09 stsp *fd = -1;
713 1510f469 2018-09-09 stsp }
714 1510f469 2018-09-09 stsp }
715 1510f469 2018-09-09 stsp
716 1510f469 2018-09-09 stsp return err;
717 1510f469 2018-09-09 stsp }
718 1510f469 2018-09-09 stsp
719 1510f469 2018-09-09 stsp const struct got_error *
720 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
721 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
722 1510f469 2018-09-09 stsp {
723 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
724 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
725 1510f469 2018-09-09 stsp int i;
726 1510f469 2018-09-09 stsp
727 1510f469 2018-09-09 stsp if (packp)
728 1510f469 2018-09-09 stsp *packp = NULL;
729 1510f469 2018-09-09 stsp
730 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
731 1510f469 2018-09-09 stsp pack = &repo->packs[i];
732 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
733 1510f469 2018-09-09 stsp break;
734 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
735 1510f469 2018-09-09 stsp return NULL;
736 1510f469 2018-09-09 stsp }
737 1510f469 2018-09-09 stsp
738 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
739 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
740 1510f469 2018-09-09 stsp if (err)
741 1510f469 2018-09-09 stsp return err;
742 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
743 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[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 pack = &repo->packs[i];
748 1510f469 2018-09-09 stsp
749 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
750 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
751 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
752 1510f469 2018-09-09 stsp goto done;
753 1510f469 2018-09-09 stsp }
754 1510f469 2018-09-09 stsp
755 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
756 1510f469 2018-09-09 stsp if (err)
757 1510f469 2018-09-09 stsp goto done;
758 1510f469 2018-09-09 stsp
759 1510f469 2018-09-09 stsp err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
760 1510f469 2018-09-09 stsp if (err)
761 1510f469 2018-09-09 stsp goto done;
762 90636195 2018-09-11 stsp
763 90636195 2018-09-11 stsp pack->privsep_child = NULL;
764 1510f469 2018-09-09 stsp
765 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
766 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
767 1510f469 2018-09-09 stsp pack->fd, 0);
768 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
769 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
770 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("mmap");
771 3a11398b 2019-02-21 stsp goto done;
772 3a11398b 2019-02-21 stsp }
773 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
774 3a11398b 2019-02-21 stsp }
775 1510f469 2018-09-09 stsp #endif
776 1510f469 2018-09-09 stsp done:
777 1510f469 2018-09-09 stsp if (err) {
778 1510f469 2018-09-09 stsp if (pack) {
779 1510f469 2018-09-09 stsp free(pack->path_packfile);
780 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
781 1510f469 2018-09-09 stsp }
782 1510f469 2018-09-09 stsp } else if (packp)
783 1510f469 2018-09-09 stsp *packp = pack;
784 1510f469 2018-09-09 stsp return err;
785 1510f469 2018-09-09 stsp }
786 1510f469 2018-09-09 stsp
787 1510f469 2018-09-09 stsp struct got_pack *
788 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
789 1510f469 2018-09-09 stsp {
790 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
791 1510f469 2018-09-09 stsp int i;
792 1510f469 2018-09-09 stsp
793 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
794 1510f469 2018-09-09 stsp pack = &repo->packs[i];
795 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
796 1510f469 2018-09-09 stsp break;
797 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
798 1510f469 2018-09-09 stsp return pack;
799 1510f469 2018-09-09 stsp }
800 1510f469 2018-09-09 stsp
801 1510f469 2018-09-09 stsp return NULL;
802 1510f469 2018-09-09 stsp }