From 9a4286537489f77b6e68f0ad0fc4398c4ec9846b Mon Sep 17 00:00:00 2001 From: Jonathan Tan Date: Fri, 18 Aug 2017 15:20:24 -0700 Subject: pack: move add_packed_git() Signed-off-by: Jonathan Tan Signed-off-by: Junio C Hamano --- packfile.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'packfile.c') diff --git a/packfile.c b/packfile.c index 93526ea7b..efe0ed3e8 100644 --- a/packfile.c +++ b/packfile.c @@ -605,3 +605,56 @@ void unuse_pack(struct pack_window **w_cursor) *w_cursor = NULL; } } + +static void try_to_free_pack_memory(size_t size) +{ + release_pack_memory(size); +} + +struct packed_git *add_packed_git(const char *path, size_t path_len, int local) +{ + static int have_set_try_to_free_routine; + struct stat st; + size_t alloc; + struct packed_git *p; + + if (!have_set_try_to_free_routine) { + have_set_try_to_free_routine = 1; + set_try_to_free_routine(try_to_free_pack_memory); + } + + /* + * Make sure a corresponding .pack file exists and that + * the index looks sane. + */ + if (!strip_suffix_mem(path, &path_len, ".idx")) + return NULL; + + /* + * ".pack" is long enough to hold any suffix we're adding (and + * the use xsnprintf double-checks that) + */ + alloc = st_add3(path_len, strlen(".pack"), 1); + p = alloc_packed_git(alloc); + memcpy(p->pack_name, path, path_len); + + xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep"); + if (!access(p->pack_name, F_OK)) + p->pack_keep = 1; + + xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack"); + if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) { + free(p); + return NULL; + } + + /* ok, it looks sane as far as we can check without + * actually mapping the pack file. + */ + p->pack_size = st.st_size; + p->pack_local = local; + p->mtime = st.st_mtime; + if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1)) + hashclr(p->sha1); + return p; +} -- cgit v1.2.1