2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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 1dd54920 2019-05-11 stsp #include "got_path.h"
42 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
43 7bb0daa1 2018-06-21 stsp #include "got_object.h"
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"
54 79b11c62 2018-03-09 stsp #ifndef nitems
55 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
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"
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"
71 7839bc15 2019-01-06 stsp const char *
72 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
74 7839bc15 2019-01-06 stsp return repo->path;
77 6e9da951 2019-01-06 stsp const char *
78 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
80 6e9da951 2019-01-06 stsp return repo->path_git_dir;
84 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
86 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
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)
92 4027f31a 2017-11-04 stsp char *path_child;
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;
98 4027f31a 2017-11-04 stsp return path_child;
102 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
104 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
108 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
110 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
114 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
116 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
120 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
122 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
125 4027f31a 2017-11-04 stsp static char *
126 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
128 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
132 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
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;
142 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
144 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
147 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
149 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
152 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
154 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
157 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
159 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
162 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
163 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
165 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
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;
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)
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);
184 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
185 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
186 79c99a64 2019-05-23 stsp err = NULL;
187 f6be5c30 2018-06-22 stsp return err;
189 f6be5c30 2018-06-22 stsp obj->refcnt++;
191 f6be5c30 2018-06-22 stsp return NULL;
194 7bb0daa1 2018-06-21 stsp struct got_object *
195 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
196 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
198 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
201 4027f31a 2017-11-04 stsp const struct got_error *
202 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
203 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
205 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
206 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
207 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
209 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
210 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
211 79c99a64 2019-05-23 stsp err = NULL;
212 f6be5c30 2018-06-22 stsp return err;
214 f6be5c30 2018-06-22 stsp tree->refcnt++;
216 f6be5c30 2018-06-22 stsp return NULL;
219 f6be5c30 2018-06-22 stsp struct got_tree_object *
220 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
221 f6be5c30 2018-06-22 stsp struct got_object_id *id)
223 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
224 6bef87be 2018-09-11 stsp &repo->treecache, id);
227 1943de01 2018-06-22 stsp const struct got_error *
228 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
229 1943de01 2018-06-22 stsp struct got_commit_object *commit)
231 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
232 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
233 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
235 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
236 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
237 79c99a64 2019-05-23 stsp err = NULL;
238 1943de01 2018-06-22 stsp return err;
240 1943de01 2018-06-22 stsp commit->refcnt++;
242 f6be5c30 2018-06-22 stsp return NULL;
245 1943de01 2018-06-22 stsp struct got_commit_object *
246 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
247 1943de01 2018-06-22 stsp struct got_object_id *id)
249 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
250 6bef87be 2018-09-11 stsp &repo->commitcache, id);
253 f4a881ce 2018-11-17 stsp const struct got_error *
254 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
255 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
257 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
258 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
259 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
261 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
262 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
263 79c99a64 2019-05-23 stsp err = NULL;
264 f4a881ce 2018-11-17 stsp return err;
266 f4a881ce 2018-11-17 stsp tag->refcnt++;
268 f4a881ce 2018-11-17 stsp return NULL;
271 f4a881ce 2018-11-17 stsp struct got_tag_object *
272 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
274 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
275 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
278 f6be5c30 2018-06-22 stsp const struct got_error *
279 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
281 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
283 85f51bba 2018-07-16 stsp /* bare git repository? */
284 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
285 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
286 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
287 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
288 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
289 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
290 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
293 85f51bba 2018-07-16 stsp return NULL;
296 85f51bba 2018-07-16 stsp /* git repository with working tree? */
297 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
298 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
299 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
302 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
303 85f51bba 2018-07-16 stsp repo->path = strdup(path);
304 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
308 85f51bba 2018-07-16 stsp return NULL;
311 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
314 ee645855 2019-02-05 stsp free(repo->path);
315 ee645855 2019-02-05 stsp repo->path = NULL;
316 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
317 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
319 85f51bba 2018-07-16 stsp return err;
322 85f51bba 2018-07-16 stsp const struct got_error *
323 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
325 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
326 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
327 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
328 ad242220 2018-09-08 stsp int i, tried_root = 0;
330 85f51bba 2018-07-16 stsp *repop = NULL;
332 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
333 2393f13b 2018-03-09 stsp abspath = strdup(path);
335 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
336 92af5469 2017-11-05 stsp if (abspath == NULL)
337 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
339 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
340 92af5469 2017-11-05 stsp if (repo == NULL) {
341 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
345 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
346 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
347 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
348 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
351 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
352 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
355 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
356 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
359 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
360 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
363 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
364 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
368 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
369 85f51bba 2018-07-16 stsp if (normpath == NULL) {
370 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
374 85f51bba 2018-07-16 stsp path = normpath;
376 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
377 85f51bba 2018-07-16 stsp if (err == NULL)
379 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
381 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
382 85f51bba 2018-07-16 stsp if (tried_root) {
383 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
386 85f51bba 2018-07-16 stsp tried_root = 1;
388 85f51bba 2018-07-16 stsp path = dirname(path);
389 85f51bba 2018-07-16 stsp if (path == NULL)
390 638f9024 2019-05-13 stsp err = got_error_from_errno2("dirname", path);
391 85f51bba 2018-07-16 stsp } while (path);
394 5c2f5761 2018-09-19 stsp got_repo_close(repo);
396 85f51bba 2018-07-16 stsp *repop = repo;
397 92af5469 2017-11-05 stsp free(abspath);
398 85f51bba 2018-07-16 stsp free(normpath);
399 92af5469 2017-11-05 stsp return err;
402 ad242220 2018-09-08 stsp const struct got_error *
403 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
405 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
408 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
409 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
411 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
414 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
415 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
417 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
420 4027f31a 2017-11-04 stsp free(repo->path);
421 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
423 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
424 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
425 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
426 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
428 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
429 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
431 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
432 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
433 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
434 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
435 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
436 ad242220 2018-09-08 stsp if (child_err && err == NULL)
437 ad242220 2018-09-08 stsp err = child_err;
438 3a6ce05a 2019-02-11 stsp if (close(repo->privsep_children[i].imsg_fd) != 0 &&
439 3a6ce05a 2019-02-11 stsp err == NULL)
440 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
442 4027f31a 2017-11-04 stsp free(repo);
444 ad242220 2018-09-08 stsp return err;
447 04ca23f4 2018-07-16 stsp const struct got_error *
448 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
449 23721109 2018-10-22 stsp const char *input_path, int check_disk)
451 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
452 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
453 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
454 7839bc15 2019-01-06 stsp char *cwd, *canonpath, *path = NULL;
456 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
458 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
459 04ca23f4 2018-07-16 stsp if (cwd == NULL)
460 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
462 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
463 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
464 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
467 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
471 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
473 b70703ad 2019-03-18 stsp if (!check_disk) {
474 23721109 2018-10-22 stsp path = strdup(canonpath);
475 b70703ad 2019-03-18 stsp if (path == NULL) {
476 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
480 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
482 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
483 04ca23f4 2018-07-16 stsp if (path == NULL) {
484 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
485 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
490 b70703ad 2019-03-18 stsp * Path is not on disk.
491 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
493 b70703ad 2019-03-18 stsp path = strdup(canonpath);
494 b70703ad 2019-03-18 stsp if (path == NULL) {
495 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
500 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
501 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
502 04ca23f4 2018-07-16 stsp len = strlen(path);
504 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
505 04ca23f4 2018-07-16 stsp is_repo_child = 1;
506 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
507 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
509 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
510 04ca23f4 2018-07-16 stsp free(path);
511 04ca23f4 2018-07-16 stsp path = strdup("");
512 04ca23f4 2018-07-16 stsp if (path == NULL) {
513 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
516 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
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);
523 04ca23f4 2018-07-16 stsp free(path);
524 04ca23f4 2018-07-16 stsp path = child;
525 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
526 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
527 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
529 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
530 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
533 04ca23f4 2018-07-16 stsp char *child;
534 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
535 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
536 04ca23f4 2018-07-16 stsp repo_abspath, path);
539 04ca23f4 2018-07-16 stsp free(path);
540 04ca23f4 2018-07-16 stsp path = child;
542 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
543 04ca23f4 2018-07-16 stsp char *child;
544 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
545 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
549 04ca23f4 2018-07-16 stsp free(path);
550 04ca23f4 2018-07-16 stsp path = child;
553 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
554 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
559 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
560 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
561 04ca23f4 2018-07-16 stsp char *abspath;
562 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
563 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 04ca23f4 2018-07-16 stsp free(path);
567 04ca23f4 2018-07-16 stsp path = abspath;
572 04ca23f4 2018-07-16 stsp free(canonpath);
574 04ca23f4 2018-07-16 stsp free(path);
576 04ca23f4 2018-07-16 stsp *in_repo_path = path;
577 1510f469 2018-09-09 stsp return err;
580 1510f469 2018-09-09 stsp const struct got_error *
581 1510f469 2018-09-09 stsp got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
583 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
586 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
587 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
590 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
591 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
593 1510f469 2018-09-09 stsp return err;
597 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
598 15fe583f 2018-11-05 stsp * be searched first in the future.
600 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
601 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
602 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
603 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
605 1510f469 2018-09-09 stsp return NULL;
609 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
611 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
614 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
617 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
618 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
624 1510f469 2018-09-09 stsp const struct got_error *
625 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
626 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
628 1510f469 2018-09-09 stsp const struct got_error *err;
629 1510f469 2018-09-09 stsp char *path_packdir;
630 1510f469 2018-09-09 stsp DIR *packdir;
631 1510f469 2018-09-09 stsp struct dirent *dent;
632 1510f469 2018-09-09 stsp char *path_packidx;
635 1510f469 2018-09-09 stsp /* Search pack index cache. */
636 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
637 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
639 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
640 1510f469 2018-09-09 stsp if (*idx != -1) {
641 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
642 1510f469 2018-09-09 stsp return NULL;
645 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
647 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
648 1510f469 2018-09-09 stsp if (path_packdir == NULL)
649 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
651 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
652 1510f469 2018-09-09 stsp if (packdir == NULL) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", path_packdir);
657 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
658 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
661 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
662 1510f469 2018-09-09 stsp dent->d_name) == -1) {
663 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
667 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
668 1510f469 2018-09-09 stsp free(path_packidx);
672 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
673 1510f469 2018-09-09 stsp if (*idx != -1) {
674 1510f469 2018-09-09 stsp err = NULL; /* found the object */
675 1510f469 2018-09-09 stsp err = got_repo_cache_packidx(repo, *packidx);
679 1510f469 2018-09-09 stsp err = got_packidx_close(*packidx);
680 1510f469 2018-09-09 stsp *packidx = NULL;
685 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
687 1510f469 2018-09-09 stsp free(path_packdir);
688 1510f469 2018-09-09 stsp if (packdir && closedir(packdir) != 0 && err == 0)
689 638f9024 2019-05-13 stsp err = got_error_from_errno("closedir");
690 04ca23f4 2018-07-16 stsp return err;
693 1510f469 2018-09-09 stsp static const struct got_error *
694 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
696 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
697 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
698 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
701 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
703 638f9024 2019-05-13 stsp return got_error_from_errno("read");
704 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
705 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
707 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
708 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
709 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
710 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
712 1510f469 2018-09-09 stsp return err;
715 1510f469 2018-09-09 stsp static const struct got_error *
716 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
718 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
720 a5b57ccf 2019-04-11 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
721 1510f469 2018-09-09 stsp if (*fd == -1)
722 638f9024 2019-05-13 stsp return got_error_from_errno2("open", path_packfile);
724 1510f469 2018-09-09 stsp if (packidx) {
725 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
727 1510f469 2018-09-09 stsp close(*fd);
732 1510f469 2018-09-09 stsp return err;
735 1510f469 2018-09-09 stsp const struct got_error *
736 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
737 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
739 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
740 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
741 ff563a3d 2019-05-23 stsp struct stat sb;
745 1510f469 2018-09-09 stsp *packp = NULL;
747 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
748 1510f469 2018-09-09 stsp pack = &repo->packs[i];
749 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
751 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
752 1510f469 2018-09-09 stsp return NULL;
755 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
756 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
758 1510f469 2018-09-09 stsp return err;
759 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
760 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
764 1510f469 2018-09-09 stsp pack = &repo->packs[i];
766 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
767 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
768 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
772 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
776 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
777 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
780 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
782 90636195 2018-09-11 stsp pack->privsep_child = NULL;
784 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
785 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
786 1510f469 2018-09-09 stsp pack->fd, 0);
787 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
788 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
789 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
792 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
797 1510f469 2018-09-09 stsp if (pack) {
798 1510f469 2018-09-09 stsp free(pack->path_packfile);
799 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
801 1510f469 2018-09-09 stsp } else if (packp)
802 1510f469 2018-09-09 stsp *packp = pack;
803 1510f469 2018-09-09 stsp return err;
806 1510f469 2018-09-09 stsp struct got_pack *
807 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
809 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
812 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
813 1510f469 2018-09-09 stsp pack = &repo->packs[i];
814 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
816 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
817 1510f469 2018-09-09 stsp return pack;
820 2c7829a4 2019-06-17 stsp return NULL;
823 2c7829a4 2019-06-17 stsp const struct got_error *
824 2c7829a4 2019-06-17 stsp got_repo_init(const char *repo_path)
826 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
827 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
828 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
829 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
830 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
832 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
833 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
834 2c7829a4 2019-06-17 stsp const char *headref_str = "ref: refs/heads/master";
835 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
836 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
837 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
838 2c7829a4 2019-06-17 stsp "\tbare = true\n";
839 2c7829a4 2019-06-17 stsp char *path;
842 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
843 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
845 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
846 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
847 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
849 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
850 2c7829a4 2019-06-17 stsp free(path);
852 2c7829a4 2019-06-17 stsp return err;
855 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
856 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
857 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
858 2c7829a4 2019-06-17 stsp free(path);
860 2c7829a4 2019-06-17 stsp return err;
862 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
863 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
864 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
865 2c7829a4 2019-06-17 stsp free(path);
867 2c7829a4 2019-06-17 stsp return err;
869 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
870 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
871 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
872 2c7829a4 2019-06-17 stsp free(path);
874 2c7829a4 2019-06-17 stsp return err;
876 1510f469 2018-09-09 stsp return NULL;