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