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/mman.h>
22 #include <sys/syslimits.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_privsep.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_worktree.h"
51 #include "got_lib_object_idcache.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 const struct got_error *
535 got_repo_close(struct got_repository *repo)
537 const struct got_error *err = NULL, *child_err;
538 int i;
540 for (i = 0; i < nitems(repo->packidx_cache); i++) {
541 if (repo->packidx_cache[i] == NULL)
542 break;
543 got_packidx_close(repo->packidx_cache[i]);
546 for (i = 0; i < nitems(repo->packs); i++) {
547 if (repo->packs[i].path_packfile == NULL)
548 break;
549 got_pack_close(&repo->packs[i]);
552 free(repo->path);
553 free(repo->path_git_dir);
555 #if 0
556 print_cache_stats(&repo->objcache, "object");
557 print_cache_stats(&repo->treecache, "tree");
558 print_cache_stats(&repo->commitcache, "commit");
559 got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
560 &repo->objcache);
561 got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
562 &repo->treecache);
563 got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
564 &repo->commitcache);
565 #endif
567 if (repo->objcache.idcache)
568 got_object_idcache_free(repo->objcache.idcache);
569 if (repo->treecache.idcache)
570 got_object_idcache_free(repo->treecache.idcache);
571 if (repo->commitcache.idcache)
572 got_object_idcache_free(repo->commitcache.idcache);
574 for (i = 0; i < nitems(repo->privsep_children); i++) {
575 if (repo->privsep_children[i].imsg_fd == -1)
576 continue;
577 imsg_clear(repo->privsep_children[i].ibuf);
578 free(repo->privsep_children[i].ibuf);
579 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
580 child_err = got_privsep_wait_for_child(
581 repo->privsep_children[i].pid);
582 if (child_err && err == NULL)
583 err = child_err;
584 close(repo->privsep_children[i].imsg_fd);
586 free(repo);
588 return err;
591 const struct got_error *
592 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
593 const char *input_path)
595 const struct got_error *err = NULL;
596 char *repo_abspath = NULL, *cwd = NULL;
597 struct stat sb;
598 size_t repolen, cwdlen, len;
599 char *canonpath, *path;
601 *in_repo_path = NULL;
603 cwd = getcwd(NULL, 0);
604 if (cwd == NULL)
605 return got_error_from_errno();
607 canonpath = strdup(input_path);
608 if (canonpath == NULL) {
609 err = got_error_from_errno();
610 goto done;
612 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
613 if (err)
614 goto done;
616 repo_abspath = got_repo_get_path(repo);
617 if (repo_abspath == NULL) {
618 err = got_error_from_errno();
619 goto done;
622 /* TODO: Call "get in-repository path of work-tree node" API. */
624 if (lstat(canonpath, &sb) != 0) {
625 if (errno != ENOENT) {
626 err = got_error_from_errno();
627 goto done;
629 /*
630 * Path is not on disk.
631 * Assume it is already relative to repository root.
632 */
633 path = strdup(canonpath);
634 } else {
635 int is_repo_child = 0, is_cwd_child = 0;
637 path = realpath(canonpath, NULL);
638 if (path == NULL) {
639 err = got_error_from_errno();
640 goto done;
643 repolen = strlen(repo_abspath);
644 cwdlen = strlen(cwd);
645 len = strlen(path);
647 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
648 is_repo_child = 1;
649 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
650 is_cwd_child = 1;
652 if (strcmp(path, repo_abspath) == 0) {
653 free(path);
654 path = strdup("");
655 if (path == NULL) {
656 err = got_error_from_errno();
657 goto done;
659 } else if (is_repo_child && is_cwd_child) {
660 char *child;
661 /* TODO: Is path inside a got worktree? */
662 /* Strip common prefix with repository path. */
663 err = got_path_skip_common_ancestor(&child,
664 repo_abspath, path);
665 if (err)
666 goto done;
667 free(path);
668 path = child;
669 } else if (is_repo_child) {
670 /* Matched an on-disk path inside repository. */
671 if (got_repo_is_bare(repo)) {
672 /*
673 * Matched an on-disk path inside repository
674 * database. Treat as repository-relative.
675 */
676 } else {
677 char *child;
678 /* Strip common prefix with repository path. */
679 err = got_path_skip_common_ancestor(&child,
680 repo_abspath, path);
681 if (err)
682 goto done;
683 free(path);
684 path = child;
686 } else if (is_cwd_child) {
687 char *child;
688 /* TODO: Is path inside a got worktree? */
689 /* Strip common prefix with cwd. */
690 err = got_path_skip_common_ancestor(&child, cwd,
691 path);
692 if (err)
693 goto done;
694 free(path);
695 path = child;
696 } else {
697 /*
698 * Matched unrelated on-disk path.
699 * Treat it as repository-relative.
700 */
704 /* Make in-repository path absolute */
705 if (path[0] != '/') {
706 char *abspath;
707 if (asprintf(&abspath, "/%s", path) == -1) {
708 err = got_error_from_errno();
709 goto done;
711 free(path);
712 path = abspath;
715 done:
716 free(repo_abspath);
717 free(cwd);
718 free(canonpath);
719 if (err)
720 free(path);
721 else
722 *in_repo_path = path;
723 return err;
726 const struct got_error *
727 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
730 int i;
732 for (i = 0; i < nitems(repo->packidx_cache); i++) {
733 if (repo->packidx_cache[i] == NULL)
734 break;
737 if (i == nitems(repo->packidx_cache)) {
738 err = got_packidx_close(repo->packidx_cache[i - 1]);
739 if (err)
740 return err;
741 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
742 sizeof(repo->packidx_cache) -
743 sizeof(repo->packidx_cache[0]));
744 i = 0;
747 repo->packidx_cache[i] = packidx;
748 return NULL;
751 static int
752 is_packidx_filename(const char *name, size_t len)
754 if (len != GOT_PACKIDX_NAMELEN)
755 return 0;
757 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
758 return 0;
760 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
761 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
762 return 0;
764 return 1;
767 const struct got_error *
768 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
769 struct got_repository *repo, struct got_object_id *id)
771 const struct got_error *err;
772 char *path_packdir;
773 DIR *packdir;
774 struct dirent *dent;
775 char *path_packidx;
776 int i;
778 /* Search pack index cache. */
779 for (i = 0; i < nitems(repo->packidx_cache); i++) {
780 if (repo->packidx_cache[i] == NULL)
781 break;
782 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
783 if (*idx != -1) {
784 *packidx = repo->packidx_cache[i];
785 return NULL;
788 /* No luck. Search the filesystem. */
790 path_packdir = got_repo_get_path_objects_pack(repo);
791 if (path_packdir == NULL)
792 return got_error_from_errno();
794 packdir = opendir(path_packdir);
795 if (packdir == NULL) {
796 err = got_error_from_errno();
797 goto done;
800 while ((dent = readdir(packdir)) != NULL) {
801 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
802 continue;
804 if (asprintf(&path_packidx, "%s/%s", path_packdir,
805 dent->d_name) == -1) {
806 err = got_error_from_errno();
807 goto done;
810 err = got_packidx_open(packidx, path_packidx, 0);
811 free(path_packidx);
812 if (err)
813 goto done;
815 *idx = got_packidx_get_object_idx(*packidx, id);
816 if (*idx != -1) {
817 err = NULL; /* found the object */
818 err = got_repo_cache_packidx(repo, *packidx);
819 goto done;
822 err = got_packidx_close(*packidx);
823 *packidx = NULL;
824 if (err)
825 goto done;
828 err = got_error(GOT_ERR_NO_OBJ);
829 done:
830 free(path_packdir);
831 if (packdir && closedir(packdir) != 0 && err == 0)
832 err = got_error_from_errno();
833 return err;
836 static const struct got_error *
837 read_packfile_hdr(int fd, struct got_packidx *packidx)
839 const struct got_error *err = NULL;
840 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
841 struct got_packfile_hdr hdr;
842 ssize_t n;
844 n = read(fd, &hdr, sizeof(hdr));
845 if (n < 0)
846 return got_error_from_errno();
847 if (n != sizeof(hdr))
848 return got_error(GOT_ERR_BAD_PACKFILE);
850 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
851 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
852 betoh32(hdr.nobjects) != totobj)
853 err = got_error(GOT_ERR_BAD_PACKFILE);
855 return err;
858 static const struct got_error *
859 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
861 const struct got_error *err = NULL;
863 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
864 if (*fd == -1)
865 return got_error_from_errno();
867 if (packidx) {
868 err = read_packfile_hdr(*fd, packidx);
869 if (err) {
870 close(*fd);
871 *fd = -1;
875 return err;
878 const struct got_error *
879 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
880 const char *path_packfile, struct got_packidx *packidx)
882 const struct got_error *err = NULL;
883 struct got_pack *pack = NULL;
884 int i;
886 if (packp)
887 *packp = NULL;
889 for (i = 0; i < nitems(repo->packs); i++) {
890 pack = &repo->packs[i];
891 if (pack->path_packfile == NULL)
892 break;
893 if (strcmp(pack->path_packfile, path_packfile) == 0)
894 return NULL;
897 if (i == nitems(repo->packs) - 1) {
898 err = got_pack_close(&repo->packs[i - 1]);
899 if (err)
900 return err;
901 memmove(&repo->packs[1], &repo->packs[0],
902 sizeof(repo->packs) - sizeof(repo->packs[0]));
903 i = 0;
906 pack = &repo->packs[i];
908 pack->path_packfile = strdup(path_packfile);
909 if (pack->path_packfile == NULL) {
910 err = got_error_from_errno();
911 goto done;
914 err = open_packfile(&pack->fd, path_packfile, packidx);
915 if (err)
916 goto done;
918 err = got_pack_get_packfile_size(&pack->filesize, path_packfile);
919 if (err)
920 goto done;
922 #ifndef GOT_PACK_NO_MMAP
923 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
924 pack->fd, 0);
925 if (pack->map == MAP_FAILED)
926 pack->map = NULL; /* fall back to read(2) */
927 #endif
928 done:
929 if (err) {
930 if (pack) {
931 free(pack->path_packfile);
932 memset(pack, 0, sizeof(*pack));
934 } else if (packp)
935 *packp = pack;
936 return err;
939 struct got_pack *
940 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
942 struct got_pack *pack = NULL;
943 int i;
945 for (i = 0; i < nitems(repo->packs); i++) {
946 pack = &repo->packs[i];
947 if (pack->path_packfile == NULL)
948 break;
949 if (strcmp(pack->path_packfile, path_packfile) == 0)
950 return pack;
953 return NULL;