Skip to content

Commit 9a42865

Browse files
jonathantanmygitster
authored andcommitted
pack: move add_packed_git()
Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 97de180 commit 9a42865

File tree

5 files changed

+55
-62
lines changed

5 files changed

+55
-62
lines changed

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern);
16401640
extern int odb_pack_keep(const char *name);
16411641

16421642
extern void clear_delta_base_cache(void);
1643-
extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
16441643

16451644
/*
16461645
* Make sure that a pointer access into an mmap'd index file is within bounds,

connected.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "sigchain.h"
44
#include "connected.h"
55
#include "transport.h"
6+
#include "pack.h"
67

78
/*
89
* If we feed all the commits we want to verify to this command

packfile.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,56 @@ void unuse_pack(struct pack_window **w_cursor)
605605
*w_cursor = NULL;
606606
}
607607
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t
4646
extern void close_pack_windows(struct packed_git *);
4747
extern void close_all_packs(void);
4848
extern void unuse_pack(struct pack_window **);
49+
extern struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
4950

5051
extern void release_pack_memory(size_t);
5152

sha1_file.c

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -719,67 +719,6 @@ void *xmmap(void *start, size_t length,
719719
return ret;
720720
}
721721

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-
783722
void install_packed_git(struct packed_git *pack)
784723
{
785724
if (pack->pack_fd != -1)

0 commit comments

Comments
 (0)