pack: move add_packed_git()

Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 18, 2017 at 15:20 UTC 9a4286537489f77b6e68f0ad0fc4398c4ec9846b
5 files changed +55 -62
cache.h
-1
@@ -1640,7 +1640,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern);
1640 extern int odb_pack_keep(const char *name);
1641
1642 extern void clear_delta_base_cache(void);
1643 -extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
1643
1644 /*
1645 * Make sure that a pointer access into an mmap'd index file is within bounds,
connected.c
+1
@@ -3,6 +3,7 @@
3 #include "sigchain.h"
4 #include "connected.h"
5 #include "transport.h"
6 +#include "pack.h"
7
8 /*
9 * If we feed all the commits we want to verify to this command
packfile.c
+53
@@ -605,3 +605,56 @@ void unuse_pack(struct pack_window **w_cursor)
605 *w_cursor = NULL;
606 }
607 }
608 +
609 +static void try_to_free_pack_memory(size_t size)
610 +{
611 + release_pack_memory(size);
612 +}
613 +
614 +struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
615 +{
616 + static int have_set_try_to_free_routine;
617 + struct stat st;
618 + size_t alloc;
619 + struct packed_git *p;
620 +
621 + if (!have_set_try_to_free_routine) {
622 + have_set_try_to_free_routine = 1;
623 + set_try_to_free_routine(try_to_free_pack_memory);
624 + }
625 +
626 + /*
627 + * Make sure a corresponding .pack file exists and that
628 + * the index looks sane.
629 + */
630 + if (!strip_suffix_mem(path, &path_len, ".idx"))
631 + return NULL;
632 +
633 + /*
634 + * ".pack" is long enough to hold any suffix we're adding (and
635 + * the use xsnprintf double-checks that)
636 + */
637 + alloc = st_add3(path_len, strlen(".pack"), 1);
638 + p = alloc_packed_git(alloc);
639 + memcpy(p->pack_name, path, path_len);
640 +
641 + xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
642 + if (!access(p->pack_name, F_OK))
643 + p->pack_keep = 1;
644 +
645 + xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
646 + if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
647 + free(p);
648 + return NULL;
649 + }
650 +
651 + /* ok, it looks sane as far as we can check without
652 + * actually mapping the pack file.
653 + */
654 + p->pack_size = st.st_size;
655 + p->pack_local = local;
656 + p->mtime = st.st_mtime;
657 + if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
658 + hashclr(p->sha1);
659 + return p;
660 +}
packfile.h
+1
@@ -46,6 +46,7 @@ extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t
46 extern void close_pack_windows(struct packed_git *);
47 extern void close_all_packs(void);
48 extern void unuse_pack(struct pack_window **);
49 +extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
50
51 extern void release_pack_memory(size_t);
52
sha1_file.c
-61
@@ -719,67 +719,6 @@ void *xmmap(void *start, size_t length,
719 return ret;
720 }
721
722 -static struct packed_git *alloc_packed_git(int extra)
723 -{
724 - struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
725 - memset(p, 0, sizeof(*p));
726 - p->pack_fd = -1;
727 - return p;
728 -}
729 -
730 -static void try_to_free_pack_memory(size_t size)
731 -{
732 - release_pack_memory(size);
733 -}
734 -
735 -struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
736 -{
737 - static int have_set_try_to_free_routine;
738 - struct stat st;
739 - size_t alloc;
740 - struct packed_git *p;
741 -
742 - if (!have_set_try_to_free_routine) {
743 - have_set_try_to_free_routine = 1;
744 - set_try_to_free_routine(try_to_free_pack_memory);
745 - }
746 -
747 - /*
748 - * Make sure a corresponding .pack file exists and that
749 - * the index looks sane.
750 - */
751 - if (!strip_suffix_mem(path, &path_len, ".idx"))
752 - return NULL;
753 -
754 - /*
755 - * ".pack" is long enough to hold any suffix we're adding (and
756 - * the use xsnprintf double-checks that)
757 - */
758 - alloc = st_add3(path_len, strlen(".pack"), 1);
759 - p = alloc_packed_git(alloc);
760 - memcpy(p->pack_name, path, path_len);
761 -
762 - xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
763 - if (!access(p->pack_name, F_OK))
764 - p->pack_keep = 1;
765 -
766 - xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
767 - if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
768 - free(p);
769 - return NULL;
770 - }
771 -
772 - /* ok, it looks sane as far as we can check without
773 - * actually mapping the pack file.
774 - */
775 - p->pack_size = st.st_size;
776 - p->pack_local = local;
777 - p->mtime = st.st_mtime;
778 - if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
779 - hashclr(p->sha1);
780 - return p;
781 -}
782 -
722 void install_packed_git(struct packed_git *pack)
723 {
724 if (pack->pack_fd != -1)