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 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_repository.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 7839bc15 2019-01-06 stsp const char *
70 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
71 86c3caaf 2018-03-09 stsp {
72 7839bc15 2019-01-06 stsp return repo->path;
73 86c3caaf 2018-03-09 stsp }
74 86c3caaf 2018-03-09 stsp
75 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 7bb0daa1 2018-06-21 stsp }
168 7bb0daa1 2018-06-21 stsp
169 f6be5c30 2018-06-22 stsp const struct got_error *
170 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
171 f6be5c30 2018-06-22 stsp struct got_object *obj)
172 f6be5c30 2018-06-22 stsp {
173 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
174 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
175 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
176 f6be5c30 2018-06-22 stsp if (err)
177 f6be5c30 2018-06-22 stsp return err;
178 f6be5c30 2018-06-22 stsp obj->refcnt++;
179 ccfe88e6 2018-07-12 stsp #endif
180 f6be5c30 2018-06-22 stsp return NULL;
181 f6be5c30 2018-06-22 stsp }
182 f6be5c30 2018-06-22 stsp
183 7bb0daa1 2018-06-21 stsp struct got_object *
184 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
185 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
186 7bb0daa1 2018-06-21 stsp {
187 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
188 7bb0daa1 2018-06-21 stsp }
189 7bb0daa1 2018-06-21 stsp
190 4027f31a 2017-11-04 stsp const struct got_error *
191 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
192 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
193 f6be5c30 2018-06-22 stsp {
194 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
195 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
196 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
197 f6be5c30 2018-06-22 stsp if (err)
198 f6be5c30 2018-06-22 stsp return err;
199 f6be5c30 2018-06-22 stsp tree->refcnt++;
200 ccfe88e6 2018-07-12 stsp #endif
201 f6be5c30 2018-06-22 stsp return NULL;
202 f6be5c30 2018-06-22 stsp }
203 f6be5c30 2018-06-22 stsp
204 f6be5c30 2018-06-22 stsp struct got_tree_object *
205 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
206 f6be5c30 2018-06-22 stsp struct got_object_id *id)
207 f6be5c30 2018-06-22 stsp {
208 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
209 6bef87be 2018-09-11 stsp &repo->treecache, id);
210 1943de01 2018-06-22 stsp }
211 1943de01 2018-06-22 stsp
212 1943de01 2018-06-22 stsp const struct got_error *
213 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
214 1943de01 2018-06-22 stsp struct got_commit_object *commit)
215 1943de01 2018-06-22 stsp {
216 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
217 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
218 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
219 1943de01 2018-06-22 stsp if (err)
220 1943de01 2018-06-22 stsp return err;
221 1943de01 2018-06-22 stsp commit->refcnt++;
222 ccfe88e6 2018-07-12 stsp #endif
223 f6be5c30 2018-06-22 stsp return NULL;
224 f6be5c30 2018-06-22 stsp }
225 f6be5c30 2018-06-22 stsp
226 1943de01 2018-06-22 stsp struct got_commit_object *
227 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
228 1943de01 2018-06-22 stsp struct got_object_id *id)
229 1943de01 2018-06-22 stsp {
230 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
231 6bef87be 2018-09-11 stsp &repo->commitcache, id);
232 f4a881ce 2018-11-17 stsp }
233 f4a881ce 2018-11-17 stsp
234 f4a881ce 2018-11-17 stsp const struct got_error *
235 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
236 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
237 f4a881ce 2018-11-17 stsp {
238 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
239 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
240 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
241 f4a881ce 2018-11-17 stsp if (err)
242 f4a881ce 2018-11-17 stsp return err;
243 f4a881ce 2018-11-17 stsp tag->refcnt++;
244 f4a881ce 2018-11-17 stsp #endif
245 f4a881ce 2018-11-17 stsp return NULL;
246 f4a881ce 2018-11-17 stsp }
247 f4a881ce 2018-11-17 stsp
248 f4a881ce 2018-11-17 stsp struct got_tag_object *
249 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
250 f4a881ce 2018-11-17 stsp {
251 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
252 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
253 1943de01 2018-06-22 stsp }
254 1943de01 2018-06-22 stsp
255 f6be5c30 2018-06-22 stsp const struct got_error *
256 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
257 4027f31a 2017-11-04 stsp {
258 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
259 85f51bba 2018-07-16 stsp struct got_worktree *worktree = NULL;
260 85f51bba 2018-07-16 stsp
261 85f51bba 2018-07-16 stsp /* bare git repository? */
262 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
263 85f51bba 2018-07-16 stsp if (repo->path_git_dir == NULL) {
264 85f51bba 2018-07-16 stsp err = got_error_from_errno();
265 85f51bba 2018-07-16 stsp goto done;
266 85f51bba 2018-07-16 stsp }
267 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
268 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
269 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
270 85f51bba 2018-07-16 stsp err = got_error_from_errno();
271 85f51bba 2018-07-16 stsp goto done;
272 85f51bba 2018-07-16 stsp }
273 85f51bba 2018-07-16 stsp return NULL;
274 85f51bba 2018-07-16 stsp }
275 85f51bba 2018-07-16 stsp
276 85f51bba 2018-07-16 stsp /* git repository with working tree? */
277 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
278 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
279 85f51bba 2018-07-16 stsp err = got_error_from_errno();
280 85f51bba 2018-07-16 stsp goto done;
281 85f51bba 2018-07-16 stsp }
282 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
283 85f51bba 2018-07-16 stsp repo->path = strdup(path);
284 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
285 85f51bba 2018-07-16 stsp err = got_error_from_errno();
286 85f51bba 2018-07-16 stsp goto done;
287 85f51bba 2018-07-16 stsp }
288 85f51bba 2018-07-16 stsp return NULL;
289 85f51bba 2018-07-16 stsp }
290 85f51bba 2018-07-16 stsp
291 85f51bba 2018-07-16 stsp /* got work tree checked out from bare git repository? */
292 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
293 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
294 85f51bba 2018-07-16 stsp err = got_worktree_open(&worktree, path);
295 85f51bba 2018-07-16 stsp if (err) {
296 85f51bba 2018-07-16 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
297 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
298 85f51bba 2018-07-16 stsp goto done;
299 85f51bba 2018-07-16 stsp }
300 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(worktree->repo_path);
301 85f51bba 2018-07-16 stsp if (repo->path_git_dir == NULL) {
302 85f51bba 2018-07-16 stsp err = got_error_from_errno();
303 85f51bba 2018-07-16 stsp goto done;
304 85f51bba 2018-07-16 stsp }
305 85f51bba 2018-07-16 stsp
306 85f51bba 2018-07-16 stsp /* got work tree checked out from git repository with working tree? */
307 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
308 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
309 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
310 85f51bba 2018-07-16 stsp GOT_GIT_DIR) == -1) {
311 85f51bba 2018-07-16 stsp err = got_error_from_errno();
312 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
313 85f51bba 2018-07-16 stsp goto done;
314 85f51bba 2018-07-16 stsp }
315 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
316 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
317 85f51bba 2018-07-16 stsp goto done;
318 85f51bba 2018-07-16 stsp }
319 85f51bba 2018-07-16 stsp repo->path = strdup(worktree->repo_path);
320 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
321 85f51bba 2018-07-16 stsp err = got_error_from_errno();
322 85f51bba 2018-07-16 stsp goto done;
323 85f51bba 2018-07-16 stsp }
324 85f51bba 2018-07-16 stsp } else {
325 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
326 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
327 85f51bba 2018-07-16 stsp err = got_error_from_errno();
328 85f51bba 2018-07-16 stsp goto done;
329 85f51bba 2018-07-16 stsp }
330 85f51bba 2018-07-16 stsp }
331 85f51bba 2018-07-16 stsp done:
332 85f51bba 2018-07-16 stsp if (worktree)
333 85f51bba 2018-07-16 stsp got_worktree_close(worktree);
334 85f51bba 2018-07-16 stsp return err;
335 85f51bba 2018-07-16 stsp }
336 85f51bba 2018-07-16 stsp
337 85f51bba 2018-07-16 stsp const struct got_error *
338 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
339 85f51bba 2018-07-16 stsp {
340 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
341 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
342 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
343 ad242220 2018-09-08 stsp int i, tried_root = 0;
344 4027f31a 2017-11-04 stsp
345 85f51bba 2018-07-16 stsp *repop = NULL;
346 85f51bba 2018-07-16 stsp
347 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
348 2393f13b 2018-03-09 stsp abspath = strdup(path);
349 2393f13b 2018-03-09 stsp else
350 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
351 92af5469 2017-11-05 stsp if (abspath == NULL)
352 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
353 4027f31a 2017-11-04 stsp
354 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
355 92af5469 2017-11-05 stsp if (repo == NULL) {
356 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
357 92af5469 2017-11-05 stsp goto done;
358 92af5469 2017-11-05 stsp }
359 4027f31a 2017-11-04 stsp
360 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
361 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
362 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
363 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
364 ad242220 2018-09-08 stsp }
365 ad242220 2018-09-08 stsp
366 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
367 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
368 6bef87be 2018-09-11 stsp if (err)
369 f6be5c30 2018-06-22 stsp goto done;
370 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
371 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
372 6bef87be 2018-09-11 stsp if (err)
373 1943de01 2018-06-22 stsp goto done;
374 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
375 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
376 6bef87be 2018-09-11 stsp if (err)
377 eb77ee11 2018-07-08 stsp goto done;
378 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
379 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
380 f4a881ce 2018-11-17 stsp if (err)
381 f4a881ce 2018-11-17 stsp goto done;
382 1943de01 2018-06-22 stsp
383 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
384 85f51bba 2018-07-16 stsp if (normpath == NULL) {
385 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
386 92af5469 2017-11-05 stsp goto done;
387 92af5469 2017-11-05 stsp }
388 4027f31a 2017-11-04 stsp
389 85f51bba 2018-07-16 stsp path = normpath;
390 85f51bba 2018-07-16 stsp do {
391 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
392 85f51bba 2018-07-16 stsp if (err == NULL)
393 85f51bba 2018-07-16 stsp break;
394 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
395 85f51bba 2018-07-16 stsp break;
396 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
397 85f51bba 2018-07-16 stsp if (tried_root) {
398 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
399 85f51bba 2018-07-16 stsp break;
400 442a3ddc 2018-04-23 stsp }
401 85f51bba 2018-07-16 stsp tried_root = 1;
402 442a3ddc 2018-04-23 stsp }
403 85f51bba 2018-07-16 stsp path = dirname(path);
404 85f51bba 2018-07-16 stsp if (path == NULL)
405 85f51bba 2018-07-16 stsp err = got_error_from_errno();
406 85f51bba 2018-07-16 stsp } while (path);
407 92af5469 2017-11-05 stsp done:
408 92af5469 2017-11-05 stsp if (err)
409 5c2f5761 2018-09-19 stsp got_repo_close(repo);
410 85f51bba 2018-07-16 stsp else
411 85f51bba 2018-07-16 stsp *repop = repo;
412 92af5469 2017-11-05 stsp free(abspath);
413 85f51bba 2018-07-16 stsp free(normpath);
414 92af5469 2017-11-05 stsp return err;
415 4027f31a 2017-11-04 stsp }
416 4027f31a 2017-11-04 stsp
417 ad242220 2018-09-08 stsp const struct got_error *
418 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
419 4027f31a 2017-11-04 stsp {
420 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
421 79b11c62 2018-03-09 stsp int i;
422 79b11c62 2018-03-09 stsp
423 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
424 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
425 79b11c62 2018-03-09 stsp break;
426 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
427 79b11c62 2018-03-09 stsp }
428 bd1223b9 2018-03-14 stsp
429 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
430 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
431 7e656b93 2018-03-17 stsp break;
432 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
433 7e656b93 2018-03-17 stsp }
434 7e656b93 2018-03-17 stsp
435 4027f31a 2017-11-04 stsp free(repo->path);
436 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
437 cd717821 2018-06-22 stsp
438 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
439 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
440 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
441 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
442 ad242220 2018-09-08 stsp
443 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
444 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
445 ad242220 2018-09-08 stsp continue;
446 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
447 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
448 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
449 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
450 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
451 ad242220 2018-09-08 stsp if (child_err && err == NULL)
452 ad242220 2018-09-08 stsp err = child_err;
453 ad242220 2018-09-08 stsp close(repo->privsep_children[i].imsg_fd);
454 ad242220 2018-09-08 stsp }
455 4027f31a 2017-11-04 stsp free(repo);
456 ad242220 2018-09-08 stsp
457 ad242220 2018-09-08 stsp return err;
458 4027f31a 2017-11-04 stsp }
459 04ca23f4 2018-07-16 stsp
460 04ca23f4 2018-07-16 stsp const struct got_error *
461 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
462 23721109 2018-10-22 stsp const char *input_path, int check_disk)
463 04ca23f4 2018-07-16 stsp {
464 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
465 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
466 04ca23f4 2018-07-16 stsp struct stat sb;
467 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
468 7839bc15 2019-01-06 stsp char *cwd, *canonpath, *path = NULL;
469 04ca23f4 2018-07-16 stsp
470 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
471 04ca23f4 2018-07-16 stsp
472 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
473 04ca23f4 2018-07-16 stsp if (cwd == NULL)
474 04ca23f4 2018-07-16 stsp return got_error_from_errno();
475 04ca23f4 2018-07-16 stsp
476 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
477 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
478 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
479 04ca23f4 2018-07-16 stsp goto done;
480 04ca23f4 2018-07-16 stsp }
481 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
482 04ca23f4 2018-07-16 stsp if (err)
483 04ca23f4 2018-07-16 stsp goto done;
484 04ca23f4 2018-07-16 stsp
485 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
486 04ca23f4 2018-07-16 stsp
487 04ca23f4 2018-07-16 stsp /* TODO: Call "get in-repository path of work-tree node" API. */
488 04ca23f4 2018-07-16 stsp
489 23721109 2018-10-22 stsp if (!check_disk)
490 23721109 2018-10-22 stsp path = strdup(canonpath);
491 23721109 2018-10-22 stsp else if (lstat(canonpath, &sb) != 0) {
492 04ca23f4 2018-07-16 stsp if (errno != ENOENT) {
493 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
494 04ca23f4 2018-07-16 stsp goto done;
495 04ca23f4 2018-07-16 stsp }
496 04ca23f4 2018-07-16 stsp /*
497 04ca23f4 2018-07-16 stsp * Path is not on disk.
498 04ca23f4 2018-07-16 stsp * Assume it is already relative to repository root.
499 04ca23f4 2018-07-16 stsp */
500 04ca23f4 2018-07-16 stsp path = strdup(canonpath);
501 04ca23f4 2018-07-16 stsp } else {
502 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
503 04ca23f4 2018-07-16 stsp
504 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
505 04ca23f4 2018-07-16 stsp if (path == NULL) {
506 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
507 04ca23f4 2018-07-16 stsp goto done;
508 04ca23f4 2018-07-16 stsp }
509 04ca23f4 2018-07-16 stsp
510 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
511 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
512 04ca23f4 2018-07-16 stsp len = strlen(path);
513 04ca23f4 2018-07-16 stsp
514 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
515 04ca23f4 2018-07-16 stsp is_repo_child = 1;
516 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
517 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
518 04ca23f4 2018-07-16 stsp
519 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
520 04ca23f4 2018-07-16 stsp free(path);
521 04ca23f4 2018-07-16 stsp path = strdup("");
522 04ca23f4 2018-07-16 stsp if (path == NULL) {
523 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
524 04ca23f4 2018-07-16 stsp goto done;
525 04ca23f4 2018-07-16 stsp }
526 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
527 04ca23f4 2018-07-16 stsp char *child;
528 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
529 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
530 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
531 04ca23f4 2018-07-16 stsp repo_abspath, path);
532 04ca23f4 2018-07-16 stsp if (err)
533 04ca23f4 2018-07-16 stsp goto done;
534 04ca23f4 2018-07-16 stsp free(path);
535 04ca23f4 2018-07-16 stsp path = child;
536 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
537 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
538 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
539 04ca23f4 2018-07-16 stsp /*
540 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
541 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
542 04ca23f4 2018-07-16 stsp */
543 04ca23f4 2018-07-16 stsp } else {
544 04ca23f4 2018-07-16 stsp char *child;
545 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
546 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
547 04ca23f4 2018-07-16 stsp repo_abspath, path);
548 04ca23f4 2018-07-16 stsp if (err)
549 04ca23f4 2018-07-16 stsp goto done;
550 04ca23f4 2018-07-16 stsp free(path);
551 04ca23f4 2018-07-16 stsp path = child;
552 04ca23f4 2018-07-16 stsp }
553 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
554 04ca23f4 2018-07-16 stsp char *child;
555 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
556 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
557 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
558 04ca23f4 2018-07-16 stsp path);
559 04ca23f4 2018-07-16 stsp if (err)
560 04ca23f4 2018-07-16 stsp goto done;
561 04ca23f4 2018-07-16 stsp free(path);
562 04ca23f4 2018-07-16 stsp path = child;
563 04ca23f4 2018-07-16 stsp } else {
564 04ca23f4 2018-07-16 stsp /*
565 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
566 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
567 04ca23f4 2018-07-16 stsp */
568 04ca23f4 2018-07-16 stsp }
569 04ca23f4 2018-07-16 stsp }
570 04ca23f4 2018-07-16 stsp
571 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
572 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
573 04ca23f4 2018-07-16 stsp char *abspath;
574 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
575 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
576 04ca23f4 2018-07-16 stsp goto done;
577 04ca23f4 2018-07-16 stsp }
578 04ca23f4 2018-07-16 stsp free(path);
579 04ca23f4 2018-07-16 stsp path = abspath;
580 04ca23f4 2018-07-16 stsp }
581 04ca23f4 2018-07-16 stsp
582 04ca23f4 2018-07-16 stsp done:
583 04ca23f4 2018-07-16 stsp free(cwd);
584 04ca23f4 2018-07-16 stsp free(canonpath);
585 04ca23f4 2018-07-16 stsp if (err)
586 04ca23f4 2018-07-16 stsp free(path);
587 04ca23f4 2018-07-16 stsp else
588 04ca23f4 2018-07-16 stsp *in_repo_path = path;
589 1510f469 2018-09-09 stsp return err;
590 1510f469 2018-09-09 stsp }
591 1510f469 2018-09-09 stsp
592 1510f469 2018-09-09 stsp const struct got_error *
593 1510f469 2018-09-09 stsp got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
594 1510f469 2018-09-09 stsp {
595 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
596 1510f469 2018-09-09 stsp int i;
597 1510f469 2018-09-09 stsp
598 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
599 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
600 1510f469 2018-09-09 stsp break;
601 1510f469 2018-09-09 stsp }
602 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
603 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
604 1510f469 2018-09-09 stsp if (err)
605 1510f469 2018-09-09 stsp return err;
606 1510f469 2018-09-09 stsp }
607 1510f469 2018-09-09 stsp
608 15fe583f 2018-11-05 stsp /*
609 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
610 15fe583f 2018-11-05 stsp * be searched first in the future.
611 15fe583f 2018-11-05 stsp */
612 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
613 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
614 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
615 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
616 15fe583f 2018-11-05 stsp
617 1510f469 2018-09-09 stsp return NULL;
618 1510f469 2018-09-09 stsp }
619 1510f469 2018-09-09 stsp
620 1510f469 2018-09-09 stsp static int
621 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
622 1510f469 2018-09-09 stsp {
623 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
624 1510f469 2018-09-09 stsp return 0;
625 1510f469 2018-09-09 stsp
626 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
627 1510f469 2018-09-09 stsp return 0;
628 1510f469 2018-09-09 stsp
629 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
630 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
631 1510f469 2018-09-09 stsp return 0;
632 1510f469 2018-09-09 stsp
633 1510f469 2018-09-09 stsp return 1;
634 1510f469 2018-09-09 stsp }
635 1510f469 2018-09-09 stsp
636 1510f469 2018-09-09 stsp const struct got_error *
637 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
638 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
639 1510f469 2018-09-09 stsp {
640 1510f469 2018-09-09 stsp const struct got_error *err;
641 1510f469 2018-09-09 stsp char *path_packdir;
642 1510f469 2018-09-09 stsp DIR *packdir;
643 1510f469 2018-09-09 stsp struct dirent *dent;
644 1510f469 2018-09-09 stsp char *path_packidx;
645 1510f469 2018-09-09 stsp int i;
646 1510f469 2018-09-09 stsp
647 1510f469 2018-09-09 stsp /* Search pack index cache. */
648 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
649 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
650 1510f469 2018-09-09 stsp break;
651 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
652 1510f469 2018-09-09 stsp if (*idx != -1) {
653 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
654 1510f469 2018-09-09 stsp return NULL;
655 1510f469 2018-09-09 stsp }
656 1510f469 2018-09-09 stsp }
657 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
658 1510f469 2018-09-09 stsp
659 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
660 1510f469 2018-09-09 stsp if (path_packdir == NULL)
661 1510f469 2018-09-09 stsp return got_error_from_errno();
662 1510f469 2018-09-09 stsp
663 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
664 1510f469 2018-09-09 stsp if (packdir == NULL) {
665 1510f469 2018-09-09 stsp err = got_error_from_errno();
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 while ((dent = readdir(packdir)) != NULL) {
670 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
671 1510f469 2018-09-09 stsp continue;
672 1510f469 2018-09-09 stsp
673 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
674 1510f469 2018-09-09 stsp dent->d_name) == -1) {
675 1510f469 2018-09-09 stsp err = got_error_from_errno();
676 1510f469 2018-09-09 stsp goto done;
677 1510f469 2018-09-09 stsp }
678 1510f469 2018-09-09 stsp
679 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
680 1510f469 2018-09-09 stsp free(path_packidx);
681 1510f469 2018-09-09 stsp if (err)
682 1510f469 2018-09-09 stsp goto done;
683 1510f469 2018-09-09 stsp
684 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
685 1510f469 2018-09-09 stsp if (*idx != -1) {
686 1510f469 2018-09-09 stsp err = NULL; /* found the object */
687 1510f469 2018-09-09 stsp err = got_repo_cache_packidx(repo, *packidx);
688 1510f469 2018-09-09 stsp goto done;
689 1510f469 2018-09-09 stsp }
690 1510f469 2018-09-09 stsp
691 1510f469 2018-09-09 stsp err = got_packidx_close(*packidx);
692 1510f469 2018-09-09 stsp *packidx = NULL;
693 1510f469 2018-09-09 stsp if (err)
694 1510f469 2018-09-09 stsp goto done;
695 1510f469 2018-09-09 stsp }
696 1510f469 2018-09-09 stsp
697 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
698 1510f469 2018-09-09 stsp done:
699 1510f469 2018-09-09 stsp free(path_packdir);
700 1510f469 2018-09-09 stsp if (packdir && closedir(packdir) != 0 && err == 0)
701 1510f469 2018-09-09 stsp err = got_error_from_errno();
702 04ca23f4 2018-07-16 stsp return err;
703 04ca23f4 2018-07-16 stsp }
704 1510f469 2018-09-09 stsp
705 1510f469 2018-09-09 stsp static const struct got_error *
706 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
707 1510f469 2018-09-09 stsp {
708 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
709 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
710 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
711 1510f469 2018-09-09 stsp ssize_t n;
712 1510f469 2018-09-09 stsp
713 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
714 1510f469 2018-09-09 stsp if (n < 0)
715 1510f469 2018-09-09 stsp return got_error_from_errno();
716 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
717 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
718 1510f469 2018-09-09 stsp
719 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
720 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
721 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
722 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
723 1510f469 2018-09-09 stsp
724 1510f469 2018-09-09 stsp return err;
725 1510f469 2018-09-09 stsp }
726 1510f469 2018-09-09 stsp
727 1510f469 2018-09-09 stsp static const struct got_error *
728 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
729 1510f469 2018-09-09 stsp {
730 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
731 1510f469 2018-09-09 stsp
732 1510f469 2018-09-09 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
733 1510f469 2018-09-09 stsp if (*fd == -1)
734 1510f469 2018-09-09 stsp return got_error_from_errno();
735 1510f469 2018-09-09 stsp
736 1510f469 2018-09-09 stsp if (packidx) {
737 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
738 1510f469 2018-09-09 stsp if (err) {
739 1510f469 2018-09-09 stsp close(*fd);
740 1510f469 2018-09-09 stsp *fd = -1;
741 1510f469 2018-09-09 stsp }
742 1510f469 2018-09-09 stsp }
743 1510f469 2018-09-09 stsp
744 1510f469 2018-09-09 stsp return err;
745 1510f469 2018-09-09 stsp }
746 1510f469 2018-09-09 stsp
747 1510f469 2018-09-09 stsp const struct got_error *
748 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
749 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
750 1510f469 2018-09-09 stsp {
751 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
752 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
753 1510f469 2018-09-09 stsp int i;
754 1510f469 2018-09-09 stsp
755 1510f469 2018-09-09 stsp if (packp)
756 1510f469 2018-09-09 stsp *packp = NULL;
757 1510f469 2018-09-09 stsp
758 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
759 1510f469 2018-09-09 stsp pack = &repo->packs[i];
760 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
761 1510f469 2018-09-09 stsp break;
762 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
763 1510f469 2018-09-09 stsp return NULL;
764 1510f469 2018-09-09 stsp }
765 1510f469 2018-09-09 stsp
766 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
767 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
768 1510f469 2018-09-09 stsp if (err)
769 1510f469 2018-09-09 stsp return err;
770 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
771 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
772 1510f469 2018-09-09 stsp i = 0;
773 1510f469 2018-09-09 stsp }
774 1510f469 2018-09-09 stsp
775 1510f469 2018-09-09 stsp pack = &repo->packs[i];
776 1510f469 2018-09-09 stsp
777 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
778 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
779 1510f469 2018-09-09 stsp err = got_error_from_errno();
780 1510f469 2018-09-09 stsp goto done;
781 1510f469 2018-09-09 stsp }
782 1510f469 2018-09-09 stsp
783 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
784 1510f469 2018-09-09 stsp if (err)
785 1510f469 2018-09-09 stsp goto done;
786 1510f469 2018-09-09 stsp
787 1510f469 2018-09-09 stsp err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
788 1510f469 2018-09-09 stsp if (err)
789 1510f469 2018-09-09 stsp goto done;
790 90636195 2018-09-11 stsp
791 90636195 2018-09-11 stsp pack->privsep_child = NULL;
792 1510f469 2018-09-09 stsp
793 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
794 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
795 1510f469 2018-09-09 stsp pack->fd, 0);
796 1510f469 2018-09-09 stsp if (pack->map == MAP_FAILED)
797 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
798 1510f469 2018-09-09 stsp #endif
799 1510f469 2018-09-09 stsp done:
800 1510f469 2018-09-09 stsp if (err) {
801 1510f469 2018-09-09 stsp if (pack) {
802 1510f469 2018-09-09 stsp free(pack->path_packfile);
803 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
804 1510f469 2018-09-09 stsp }
805 1510f469 2018-09-09 stsp } else if (packp)
806 1510f469 2018-09-09 stsp *packp = pack;
807 1510f469 2018-09-09 stsp return err;
808 1510f469 2018-09-09 stsp }
809 1510f469 2018-09-09 stsp
810 1510f469 2018-09-09 stsp struct got_pack *
811 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
812 1510f469 2018-09-09 stsp {
813 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
814 1510f469 2018-09-09 stsp int i;
815 1510f469 2018-09-09 stsp
816 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
817 1510f469 2018-09-09 stsp pack = &repo->packs[i];
818 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
819 1510f469 2018-09-09 stsp break;
820 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
821 1510f469 2018-09-09 stsp return pack;
822 1510f469 2018-09-09 stsp }
823 1510f469 2018-09-09 stsp
824 1510f469 2018-09-09 stsp return NULL;
825 1510f469 2018-09-09 stsp }