Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22 #include <sys/mman.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <dirent.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sha1.h>
30 #include <string.h>
31 #include <zlib.h>
32 #include <errno.h>
33 #include <libgen.h>
34 #include <stdint.h>
35 #include <imsg.h>
37 #include "got_error.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_worktree.h"
41 #include "got_object.h"
43 #include "got_lib_path.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_repository.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_privsep.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 #endif
57 #define GOT_GIT_DIR ".git"
59 /* Mandatory files and directories inside the git directory. */
60 #define GOT_OBJECTS_DIR "objects"
61 #define GOT_REFS_DIR "refs"
62 #define GOT_HEAD_FILE "HEAD"
64 /* Other files and directories inside the git directory. */
65 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
66 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
67 #define GOT_OBJECTS_PACK_DIR "objects/pack"
69 char *
70 got_repo_get_path(struct got_repository *repo)
71 {
72 return strdup(repo->path);
73 }
75 char *
76 got_repo_get_path_git_dir(struct got_repository *repo)
77 {
78 return strdup(repo->path_git_dir);
79 }
81 int
82 got_repo_is_bare(struct got_repository *repo)
83 {
84 return (strcmp(repo->path, repo->path_git_dir) == 0);
85 }
87 static char *
88 get_path_git_child(struct got_repository *repo, const char *basename)
89 {
90 char *path_child;
92 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
93 basename) == -1)
94 return NULL;
96 return path_child;
97 }
99 char *
100 got_repo_get_path_objects(struct got_repository *repo)
102 return get_path_git_child(repo, GOT_OBJECTS_DIR);
105 char *
106 got_repo_get_path_objects_pack(struct got_repository *repo)
108 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
111 char *
112 got_repo_get_path_refs(struct got_repository *repo)
114 return get_path_git_child(repo, GOT_REFS_DIR);
117 static char *
118 get_path_head(struct got_repository *repo)
120 return get_path_git_child(repo, GOT_HEAD_FILE);
123 static int
124 is_git_repo(struct got_repository *repo)
126 char *path_git = got_repo_get_path_git_dir(repo);
127 char *path_objects = got_repo_get_path_objects(repo);
128 char *path_refs = got_repo_get_path_refs(repo);
129 char *path_head = get_path_head(repo);
130 int ret = 0;
131 struct stat sb;
132 struct got_reference *head_ref;
134 if (lstat(path_git, &sb) == -1)
135 goto done;
136 if (!S_ISDIR(sb.st_mode))
137 goto done;
139 if (lstat(path_objects, &sb) == -1)
140 goto done;
141 if (!S_ISDIR(sb.st_mode))
142 goto done;
144 if (lstat(path_refs, &sb) == -1)
145 goto done;
146 if (!S_ISDIR(sb.st_mode))
147 goto done;
149 if (lstat(path_head, &sb) == -1)
150 goto done;
151 if (!S_ISREG(sb.st_mode))
152 goto done;
154 /* Check if the HEAD reference can be opened. */
155 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
156 goto done;
157 got_ref_close(head_ref);
159 ret = 1;
160 done:
161 free(path_git);
162 free(path_objects);
163 free(path_refs);
164 free(path_head);
165 return ret;
169 #ifndef GOT_NO_OBJ_CACHE
170 static const struct got_error *
171 cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
173 const struct got_error *err = NULL;
174 struct got_object_cache_entry *ce;
175 int nelem;
177 nelem = got_object_idcache_num_elements(cache->idcache);
178 if (nelem >= cache->size) {
179 err = got_object_idcache_remove_least_used((void **)&ce,
180 cache->idcache);
181 if (err)
182 return err;
183 switch (cache->type) {
184 case GOT_OBJECT_CACHE_TYPE_OBJ:
185 got_object_close(ce->data.obj);
186 break;
187 case GOT_OBJECT_CACHE_TYPE_TREE:
188 got_object_tree_close(ce->data.tree);
189 break;
190 case GOT_OBJECT_CACHE_TYPE_COMMIT:
191 got_object_commit_close(ce->data.commit);
192 break;
194 free(ce);
197 ce = calloc(1, sizeof(*ce));
198 if (ce == NULL)
199 return got_error_from_errno();
200 memcpy(&ce->id, id, sizeof(ce->id));
201 switch (cache->type) {
202 case GOT_OBJECT_CACHE_TYPE_OBJ:
203 ce->data.obj = (struct got_object *)item;
204 break;
205 case GOT_OBJECT_CACHE_TYPE_TREE:
206 ce->data.tree = (struct got_tree_object *)item;
207 break;
208 case GOT_OBJECT_CACHE_TYPE_COMMIT:
209 ce->data.commit = (struct got_commit_object *)item;
210 break;
213 err = got_object_idcache_add(cache->idcache, id, ce);
214 if (err) {
215 if (err->code == GOT_ERR_OBJ_EXISTS) {
216 free(ce);
217 err = NULL;
220 return err;
222 #endif
224 const struct got_error *
225 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
226 struct got_object *obj)
228 #ifndef GOT_NO_OBJ_CACHE
229 const struct got_error *err = NULL;
230 err = cache_add(&repo->objcache, id, obj);
231 if (err)
232 return err;
233 obj->refcnt++;
234 #endif
235 return NULL;
238 struct got_object *
239 got_repo_get_cached_object(struct got_repository *repo,
240 struct got_object_id *id)
242 struct got_object_cache_entry *ce;
244 ce = got_object_idcache_get(repo->objcache.idcache, id);
245 if (ce) {
246 repo->objcache.cache_hit++;
247 return ce->data.obj;
250 repo->objcache.cache_miss++;
251 return NULL;
254 const struct got_error *
255 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
256 struct got_tree_object *tree)
258 #ifndef GOT_NO_OBJ_CACHE
259 const struct got_error *err = NULL;
260 err = cache_add(&repo->treecache, id, tree);
261 if (err)
262 return err;
263 tree->refcnt++;
264 #endif
265 return NULL;
268 struct got_tree_object *
269 got_repo_get_cached_tree(struct got_repository *repo,
270 struct got_object_id *id)
272 struct got_object_cache_entry *ce;
274 ce = got_object_idcache_get(repo->treecache.idcache, id);
275 if (ce) {
276 repo->treecache.cache_hit++;
277 return ce->data.tree;
280 repo->treecache.cache_miss++;
281 return NULL;
284 const struct got_error *
285 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
286 struct got_commit_object *commit)
288 #ifndef GOT_NO_OBJ_CACHE
289 const struct got_error *err = NULL;
290 err = cache_add(&repo->commitcache, id, commit);
291 if (err)
292 return err;
294 commit->refcnt++;
295 #endif
296 return NULL;
299 struct got_commit_object *
300 got_repo_get_cached_commit(struct got_repository *repo,
301 struct got_object_id *id)
303 struct got_object_cache_entry *ce;
305 ce = got_object_idcache_get(repo->commitcache.idcache, id);
306 if (ce) {
307 repo->commitcache.cache_hit++;
308 return ce->data.commit;
311 repo->commitcache.cache_miss++;
312 return NULL;
315 const struct got_error *
316 open_repo(struct got_repository *repo, const char *path)
318 const struct got_error *err = NULL;
319 struct got_worktree *worktree = NULL;
321 /* bare git repository? */
322 repo->path_git_dir = strdup(path);
323 if (repo->path_git_dir == NULL) {
324 err = got_error_from_errno();
325 goto done;
327 if (is_git_repo(repo)) {
328 repo->path = strdup(repo->path_git_dir);
329 if (repo->path == NULL) {
330 err = got_error_from_errno();
331 goto done;
333 return NULL;
336 /* git repository with working tree? */
337 free(repo->path_git_dir);
338 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
339 err = got_error_from_errno();
340 goto done;
342 if (is_git_repo(repo)) {
343 repo->path = strdup(path);
344 if (repo->path == NULL) {
345 err = got_error_from_errno();
346 goto done;
348 return NULL;
351 /* got work tree checked out from bare git repository? */
352 free(repo->path_git_dir);
353 repo->path_git_dir = NULL;
354 err = got_worktree_open(&worktree, path);
355 if (err) {
356 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
357 err = got_error(GOT_ERR_NOT_GIT_REPO);
358 goto done;
360 repo->path_git_dir = strdup(worktree->repo_path);
361 if (repo->path_git_dir == NULL) {
362 err = got_error_from_errno();
363 goto done;
366 /* got work tree checked out from git repository with working tree? */
367 if (!is_git_repo(repo)) {
368 free(repo->path_git_dir);
369 if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
370 GOT_GIT_DIR) == -1) {
371 err = got_error_from_errno();
372 repo->path_git_dir = NULL;
373 goto done;
375 if (!is_git_repo(repo)) {
376 err = got_error(GOT_ERR_NOT_GIT_REPO);
377 goto done;
379 repo->path = strdup(worktree->repo_path);
380 if (repo->path == NULL) {
381 err = got_error_from_errno();
382 goto done;
384 } else {
385 repo->path = strdup(repo->path_git_dir);
386 if (repo->path == NULL) {
387 err = got_error_from_errno();
388 goto done;
391 done:
392 if (worktree)
393 got_worktree_close(worktree);
394 return err;
397 const struct got_error *
398 got_repo_open(struct got_repository **repop, const char *path)
400 struct got_repository *repo = NULL;
401 const struct got_error *err = NULL;
402 char *abspath, *normpath = NULL;
403 int i, tried_root = 0;
405 *repop = NULL;
407 if (got_path_is_absolute(path))
408 abspath = strdup(path);
409 else
410 abspath = got_path_get_absolute(path);
411 if (abspath == NULL)
412 return got_error(GOT_ERR_BAD_PATH);
414 repo = calloc(1, sizeof(*repo));
415 if (repo == NULL) {
416 err = got_error_from_errno();
417 goto done;
420 for (i = 0; i < nitems(repo->privsep_children); i++) {
421 memset(&repo->privsep_children[i], 0,
422 sizeof(repo->privsep_children[0]));
423 repo->privsep_children[i].imsg_fd = -1;
426 repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
427 repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
428 repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
429 if (repo->objcache.idcache == NULL) {
430 err = got_error_from_errno();
431 goto done;
434 repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
435 repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
436 repo->treecache.idcache =
437 got_object_idcache_alloc(repo->treecache.size);
438 if (repo->treecache.idcache == NULL) {
439 err = got_error_from_errno();
440 goto done;
443 repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
444 repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
445 repo->commitcache.idcache =
446 got_object_idcache_alloc(repo->commitcache.size);
447 if (repo->commitcache.idcache == NULL) {
448 err = got_error_from_errno();
449 goto done;
452 normpath = got_path_normalize(abspath);
453 if (normpath == NULL) {
454 err = got_error(GOT_ERR_BAD_PATH);
455 goto done;
458 path = normpath;
459 do {
460 err = open_repo(repo, path);
461 if (err == NULL)
462 break;
463 if (err->code != GOT_ERR_NOT_GIT_REPO)
464 break;
465 if (path[0] == '/' && path[1] == '\0') {
466 if (tried_root) {
467 err = got_error(GOT_ERR_NOT_GIT_REPO);
468 break;
470 tried_root = 1;
472 path = dirname(path);
473 if (path == NULL)
474 err = got_error_from_errno();
475 } while (path);
476 done:
477 if (err)
478 err = got_repo_close(repo);
479 else
480 *repop = repo;
481 free(abspath);
482 free(normpath);
483 return err;
486 #if 0
487 static void
488 print_cache_stats(struct got_object_cache *cache, const char *name)
490 fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
491 name, got_object_idcache_num_elements(cache->idcache),
492 cache->cache_hit, cache->cache_miss);
495 void check_refcount(struct got_object_id *id, void *data, void *arg)
497 struct got_object_cache *cache = arg;
498 struct got_object_cache_entry *ce = data;
499 struct got_object *obj;
500 struct got_tree_object *tree;
501 struct got_commit_object *commit;
502 char *id_str;
504 if (got_object_id_str(&id_str, id) != NULL)
505 return;
507 switch (cache->type) {
508 case GOT_OBJECT_CACHE_TYPE_OBJ:
509 obj = ce->data.obj;
510 if (obj->refcnt == 1)
511 break;
512 fprintf(stderr, "object %s has %d unclaimed references\n",
513 id_str, obj->refcnt - 1);
514 break;
515 case GOT_OBJECT_CACHE_TYPE_TREE:
516 tree = ce->data.tree;
517 if (tree->refcnt == 1)
518 break;
519 fprintf(stderr, "tree %s has %d unclaimed references\n",
520 id_str, tree->refcnt - 1);
521 break;
522 case GOT_OBJECT_CACHE_TYPE_COMMIT:
523 commit = ce->data.commit;
524 if (commit->refcnt == 1)
525 break;
526 fprintf(stderr, "commit %s has %d unclaimed references\n",
527 id_str, commit->refcnt);
528 break;
530 free(id_str);
532 #endif
534 static const struct got_error *
535 wait_for_child(pid_t pid)
537 int child_status;
539 waitpid(pid, &child_status, 0);
541 if (!WIFEXITED(child_status))
542 return got_error(GOT_ERR_PRIVSEP_DIED);
544 if (WEXITSTATUS(child_status) != 0)
545 return got_error(GOT_ERR_PRIVSEP_EXIT);
547 return NULL;
550 const struct got_error *
551 got_repo_close(struct got_repository *repo)
553 const struct got_error *err = NULL, *child_err;
554 int i;
556 for (i = 0; i < nitems(repo->packidx_cache); i++) {
557 if (repo->packidx_cache[i] == NULL)
558 break;
559 got_packidx_close(repo->packidx_cache[i]);
562 for (i = 0; i < nitems(repo->packs); i++) {
563 if (repo->packs[i].path_packfile == NULL)
564 break;
565 got_pack_close(&repo->packs[i]);
568 free(repo->path);
569 free(repo->path_git_dir);
571 #if 0
572 print_cache_stats(&repo->objcache, "object");
573 print_cache_stats(&repo->treecache, "tree");
574 print_cache_stats(&repo->commitcache, "commit");
575 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
576 &repo->objcache);
577 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
578 &repo->treecache);
579 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
580 &repo->commitcache);
581 #endif
583 if (repo->objcache.idcache)
584 got_object_idcache_free(repo->objcache.idcache);
585 if (repo->treecache.idcache)
586 got_object_idcache_free(repo->treecache.idcache);
587 if (repo->commitcache.idcache)
588 got_object_idcache_free(repo->commitcache.idcache);
590 for (i = 0; i < nitems(repo->privsep_children); i++) {
591 if (repo->privsep_children[i].imsg_fd == -1)
592 continue;
593 imsg_clear(repo->privsep_children[i].ibuf);
594 free(repo->privsep_children[i].ibuf);
595 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
596 child_err = wait_for_child(repo->privsep_children[i].pid);
597 if (child_err && err == NULL)
598 err = child_err;
599 close(repo->privsep_children[i].imsg_fd);
601 free(repo);
603 return err;
606 const struct got_error *
607 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
608 const char *input_path)
610 const struct got_error *err = NULL;
611 char *repo_abspath = NULL, *cwd = NULL;
612 struct stat sb;
613 size_t repolen, cwdlen, len;
614 char *canonpath, *path;
616 *in_repo_path = NULL;
618 cwd = getcwd(NULL, 0);
619 if (cwd == NULL)
620 return got_error_from_errno();
622 canonpath = strdup(input_path);
623 if (canonpath == NULL) {
624 err = got_error_from_errno();
625 goto done;
627 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
628 if (err)
629 goto done;
631 repo_abspath = got_repo_get_path(repo);
632 if (repo_abspath == NULL) {
633 err = got_error_from_errno();
634 goto done;
637 /* TODO: Call "get in-repository path of work-tree node" API. */
639 if (lstat(canonpath, &sb) != 0) {
640 if (errno != ENOENT) {
641 err = got_error_from_errno();
642 goto done;
644 /*
645 * Path is not on disk.
646 * Assume it is already relative to repository root.
647 */
648 path = strdup(canonpath);
649 } else {
650 int is_repo_child = 0, is_cwd_child = 0;
652 path = realpath(canonpath, NULL);
653 if (path == NULL) {
654 err = got_error_from_errno();
655 goto done;
658 repolen = strlen(repo_abspath);
659 cwdlen = strlen(cwd);
660 len = strlen(path);
662 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
663 is_repo_child = 1;
664 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
665 is_cwd_child = 1;
667 if (strcmp(path, repo_abspath) == 0) {
668 free(path);
669 path = strdup("");
670 if (path == NULL) {
671 err = got_error_from_errno();
672 goto done;
674 } else if (is_repo_child && is_cwd_child) {
675 char *child;
676 /* TODO: Is path inside a got worktree? */
677 /* Strip common prefix with repository path. */
678 err = got_path_skip_common_ancestor(&child,
679 repo_abspath, path);
680 if (err)
681 goto done;
682 free(path);
683 path = child;
684 } else if (is_repo_child) {
685 /* Matched an on-disk path inside repository. */
686 if (got_repo_is_bare(repo)) {
687 /*
688 * Matched an on-disk path inside repository
689 * database. Treat as repository-relative.
690 */
691 } else {
692 char *child;
693 /* Strip common prefix with repository path. */
694 err = got_path_skip_common_ancestor(&child,
695 repo_abspath, path);
696 if (err)
697 goto done;
698 free(path);
699 path = child;
701 } else if (is_cwd_child) {
702 char *child;
703 /* TODO: Is path inside a got worktree? */
704 /* Strip common prefix with cwd. */
705 err = got_path_skip_common_ancestor(&child, cwd,
706 path);
707 if (err)
708 goto done;
709 free(path);
710 path = child;
711 } else {
712 /*
713 * Matched unrelated on-disk path.
714 * Treat it as repository-relative.
715 */
719 /* Make in-repository path absolute */
720 if (path[0] != '/') {
721 char *abspath;
722 if (asprintf(&abspath, "/%s", path) == -1) {
723 err = got_error_from_errno();
724 goto done;
726 free(path);
727 path = abspath;
730 done:
731 free(repo_abspath);
732 free(cwd);
733 free(canonpath);
734 if (err)
735 free(path);
736 else
737 *in_repo_path = path;
738 return err;
741 const struct got_error *
742 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
744 const struct got_error *err = NULL;
745 int i;
747 for (i = 0; i < nitems(repo->packidx_cache); i++) {
748 if (repo->packidx_cache[i] == NULL)
749 break;
752 if (i == nitems(repo->packidx_cache)) {
753 err = got_packidx_close(repo->packidx_cache[i - 1]);
754 if (err)
755 return err;
756 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
757 sizeof(repo->packidx_cache) -
758 sizeof(repo->packidx_cache[0]));
759 i = 0;
762 repo->packidx_cache[i] = packidx;
763 return NULL;
766 static int
767 is_packidx_filename(const char *name, size_t len)
769 if (len != GOT_PACKIDX_NAMELEN)
770 return 0;
772 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
773 return 0;
775 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
776 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
777 return 0;
779 return 1;
782 const struct got_error *
783 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
784 struct got_repository *repo, struct got_object_id *id)
786 const struct got_error *err;
787 char *path_packdir;
788 DIR *packdir;
789 struct dirent *dent;
790 char *path_packidx;
791 int i;
793 /* Search pack index cache. */
794 for (i = 0; i < nitems(repo->packidx_cache); i++) {
795 if (repo->packidx_cache[i] == NULL)
796 break;
797 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
798 if (*idx != -1) {
799 *packidx = repo->packidx_cache[i];
800 return NULL;
803 /* No luck. Search the filesystem. */
805 path_packdir = got_repo_get_path_objects_pack(repo);
806 if (path_packdir == NULL)
807 return got_error_from_errno();
809 packdir = opendir(path_packdir);
810 if (packdir == NULL) {
811 err = got_error_from_errno();
812 goto done;
815 while ((dent = readdir(packdir)) != NULL) {
816 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
817 continue;
819 if (asprintf(&path_packidx, "%s/%s", path_packdir,
820 dent->d_name) == -1) {
821 err = got_error_from_errno();
822 goto done;
825 err = got_packidx_open(packidx, path_packidx, 0);
826 free(path_packidx);
827 if (err)
828 goto done;
830 *idx = got_packidx_get_object_idx(*packidx, id);
831 if (*idx != -1) {
832 err = NULL; /* found the object */
833 err = got_repo_cache_packidx(repo, *packidx);
834 goto done;
837 err = got_packidx_close(*packidx);
838 *packidx = NULL;
839 if (err)
840 goto done;
843 err = got_error(GOT_ERR_NO_OBJ);
844 done:
845 free(path_packdir);
846 if (packdir && closedir(packdir) != 0 && err == 0)
847 err = got_error_from_errno();
848 return err;
851 static const struct got_error *
852 read_packfile_hdr(int fd, struct got_packidx *packidx)
854 const struct got_error *err = NULL;
855 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
856 struct got_packfile_hdr hdr;
857 ssize_t n;
859 n = read(fd, &hdr, sizeof(hdr));
860 if (n < 0)
861 return got_error_from_errno();
862 if (n != sizeof(hdr))
863 return got_error(GOT_ERR_BAD_PACKFILE);
865 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
866 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
867 betoh32(hdr.nobjects) != totobj)
868 err = got_error(GOT_ERR_BAD_PACKFILE);
870 return err;
873 static const struct got_error *
874 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
876 const struct got_error *err = NULL;
878 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
879 if (*fd == -1)
880 return got_error_from_errno();
882 if (packidx) {
883 err = read_packfile_hdr(*fd, packidx);
884 if (err) {
885 close(*fd);
886 *fd = -1;
890 return err;
893 const struct got_error *
894 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
895 const char *path_packfile, struct got_packidx *packidx)
897 const struct got_error *err = NULL;
898 struct got_pack *pack = NULL;
899 int i;
901 if (packp)
902 *packp = NULL;
904 for (i = 0; i < nitems(repo->packs); i++) {
905 pack = &repo->packs[i];
906 if (pack->path_packfile == NULL)
907 break;
908 if (strcmp(pack->path_packfile, path_packfile) == 0)
909 return NULL;
912 if (i == nitems(repo->packs) - 1) {
913 err = got_pack_close(&repo->packs[i - 1]);
914 if (err)
915 return err;
916 memmove(&repo->packs[1], &repo->packs[0],
917 sizeof(repo->packs) - sizeof(repo->packs[0]));
918 i = 0;
921 pack = &repo->packs[i];
923 pack->path_packfile = strdup(path_packfile);
924 if (pack->path_packfile == NULL) {
925 err = got_error_from_errno();
926 goto done;
929 err = open_packfile(&pack->fd, path_packfile, packidx);
930 if (err)
931 goto done;
933 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
934 if (err)
935 goto done;
937 #ifndef GOT_PACK_NO_MMAP
938 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
939 pack->fd, 0);
940 if (pack->map == MAP_FAILED)
941 pack->map = NULL; /* fall back to read(2) */
942 #endif
943 done:
944 if (err) {
945 if (pack) {
946 free(pack->path_packfile);
947 memset(pack, 0, sizeof(*pack));
949 } else if (packp)
950 *packp = pack;
951 return err;
954 struct got_pack *
955 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
957 struct got_pack *pack = NULL;
958 int i;
960 for (i = 0; i < nitems(repo->packs); i++) {
961 pack = &repo->packs[i];
962 if (pack->path_packfile == NULL)
963 break;
964 if (strcmp(pack->path_packfile, path_packfile) == 0)
965 return pack;
968 return NULL;