summaryrefslogtreecommitdiff
path: root/src/pack.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-04-16 14:19:27 -0700
committerVicent Marti <tanoku@gmail.com>2013-04-22 16:52:07 +0200
commit38eef6113d8523abfe6746bf727cee2651398ad3 (patch)
tree3565c7cacc968d8ddf05a8c551e3202a7a08436c /src/pack.c
parentc628918625c7f779d2050a56998fb2b675f097fb (diff)
downloadlibgit2-38eef6113d8523abfe6746bf727cee2651398ad3.tar.gz
Make indexer use shared packfile open code
The indexer was creating a packfile object separately from the code in pack.c which was a problem since I put a call to git_mutex_init into just pack.c. This commit updates the pack function for creating a new pack object (i.e. git_packfile_check()) so that it can be used in both places and then makes indexer.c use the shared initialization routine. There are also a few minor formatting and warning message fixes.
Diffstat (limited to 'src/pack.c')
-rw-r--r--src/pack.c39
1 files changed, 17 insertions, 22 deletions
diff --git a/src/pack.c b/src/pack.c
index 3a2e7fb5a..8e8a01aa9 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -794,19 +794,6 @@ git_off_t get_delta_base(
*
***********************************************************/
-static struct git_pack_file *packfile_alloc(size_t extra)
-{
- struct git_pack_file *p = git__calloc(1, sizeof(*p) + extra);
- if (!p)
- return NULL;
-
- p->mwf.fd = -1;
- git_mutex_init(&p->lock);
-
- return p;
-}
-
-
void git_packfile_free(struct git_pack_file *p)
{
if (!p)
@@ -902,28 +889,33 @@ int git_packfile_check(struct git_pack_file **pack_out, const char *path)
{
struct stat st;
struct git_pack_file *p;
- size_t path_len;
+ size_t path_len = path ? strlen(path) : 0;
*pack_out = NULL;
- if (!path || (path_len = strlen(path)) <= strlen(".idx"))
+ if (path_len < strlen(".idx"))
return git_odb__error_notfound("invalid packfile path", NULL);
- p = packfile_alloc(path_len + 2);
+ p = git__calloc(1, sizeof(*p) + path_len + 2);
GITERR_CHECK_ALLOC(p);
+ memcpy(p->pack_name, path, path_len + 1);
+
/*
* Make sure a corresponding .pack file exists and that
* the index looks sane.
*/
- path_len -= strlen(".idx");
- memcpy(p->pack_name, path, path_len);
+ if (git__suffixcmp(path, ".idx") == 0) {
+ size_t root_len = path_len - strlen(".idx");
+
+ memcpy(p->pack_name + root_len, ".keep", sizeof(".keep"));
+ if (git_path_exists(p->pack_name) == true)
+ p->pack_keep = 1;
- memcpy(p->pack_name + path_len, ".keep", sizeof(".keep"));
- if (git_path_exists(p->pack_name) == true)
- p->pack_keep = 1;
+ memcpy(p->pack_name + root_len, ".pack", sizeof(".pack"));
+ path_len = path_len - strlen(".idx") + strlen(".pack");
+ }
- memcpy(p->pack_name + path_len, ".pack", sizeof(".pack"));
if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
git__free(p);
return git_odb__error_notfound("packfile not found", NULL);
@@ -932,10 +924,13 @@ int git_packfile_check(struct git_pack_file **pack_out, const char *path)
/* ok, it looks sane as far as we can check without
* actually mapping the pack file.
*/
+ p->mwf.fd = -1;
p->mwf.size = st.st_size;
p->pack_local = 1;
p->mtime = (git_time_t)st.st_mtime;
+ git_mutex_init(&p->lock);
+
/* see if we can parse the sha1 oid in the packfile name */
if (path_len < 40 ||
git_oid_fromstr(&p->sha1, path + path_len - GIT_OID_HEXSZ) < 0)