Commit 5d2d21e5 by Russell Belfer Committed by Vicent Marti

Consolidate packfile allocation further

Rename git_packfile_check to git_packfile_alloc since it is now
being used more in that capacity.  Fix the various places that use
it.  Consolidate some repeated code in odb_pack.c related to the
allocation of a new pack_backend.
parent 38eef611
...@@ -62,7 +62,7 @@ static int open_pack(struct git_pack_file **out, const char *filename) ...@@ -62,7 +62,7 @@ static int open_pack(struct git_pack_file **out, const char *filename)
{ {
struct git_pack_file *pack; struct git_pack_file *pack;
if (git_packfile_check(&pack, filename) < 0) if (git_packfile_alloc(&pack, filename) < 0)
return -1; return -1;
if ((pack->mwf.fd = p_open(pack->pack_name, O_RDONLY)) < 0) { if ((pack->mwf.fd = p_open(pack->pack_name, O_RDONLY)) < 0) {
......
...@@ -207,7 +207,7 @@ static int packfile_load__cb(void *_data, git_buf *path) ...@@ -207,7 +207,7 @@ static int packfile_load__cb(void *_data, git_buf *path)
return 0; return 0;
} }
error = git_packfile_check(&pack, path->ptr); error = git_packfile_alloc(&pack, path->ptr);
if (error == GIT_ENOTFOUND) if (error == GIT_ENOTFOUND)
/* ignore missing .pack file as git does */ /* ignore missing .pack file as git does */
return 0; return 0;
...@@ -527,80 +527,75 @@ static void pack_backend__free(git_odb_backend *_backend) ...@@ -527,80 +527,75 @@ static void pack_backend__free(git_odb_backend *_backend)
git__free(backend); git__free(backend);
} }
int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx) static int pack_backend__alloc(struct pack_backend **out, size_t initial_size)
{ {
struct pack_backend *backend = NULL; struct pack_backend *backend = git__calloc(1, sizeof(struct pack_backend));
struct git_pack_file *packfile = NULL; GITERR_CHECK_ALLOC(backend);
if (git_packfile_check(&packfile, idx) < 0) if (git_vector_init(&backend->packs, initial_size, packfile_sort__cb) < 0) {
git__free(backend);
return -1; return -1;
}
backend = git__calloc(1, sizeof(struct pack_backend));
GITERR_CHECK_ALLOC(backend);
backend->parent.version = GIT_ODB_BACKEND_VERSION; backend->parent.version = GIT_ODB_BACKEND_VERSION;
if (git_vector_init(&backend->packs, 1, NULL) < 0)
goto on_error;
if (git_vector_insert(&backend->packs, packfile) < 0)
goto on_error;
backend->parent.read = &pack_backend__read; backend->parent.read = &pack_backend__read;
backend->parent.read_prefix = &pack_backend__read_prefix; backend->parent.read_prefix = &pack_backend__read_prefix;
backend->parent.read_header = &pack_backend__read_header; backend->parent.read_header = &pack_backend__read_header;
backend->parent.exists = &pack_backend__exists; backend->parent.exists = &pack_backend__exists;
backend->parent.refresh = &pack_backend__refresh; backend->parent.refresh = &pack_backend__refresh;
backend->parent.foreach = &pack_backend__foreach; backend->parent.foreach = &pack_backend__foreach;
backend->parent.writepack = &pack_backend__writepack;
backend->parent.free = &pack_backend__free; backend->parent.free = &pack_backend__free;
*backend_out = (git_odb_backend *)backend; *out = backend;
return 0; return 0;
on_error:
git_vector_free(&backend->packs);
git__free(backend);
git__free(packfile);
return -1;
} }
int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir) int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx)
{ {
struct pack_backend *backend = NULL; struct pack_backend *backend = NULL;
git_buf path = GIT_BUF_INIT; struct git_pack_file *packfile = NULL;
backend = git__calloc(1, sizeof(struct pack_backend)); if (pack_backend__alloc(&backend, 1) < 0)
GITERR_CHECK_ALLOC(backend); return -1;
backend->parent.version = GIT_ODB_BACKEND_VERSION;
if (git_vector_init(&backend->packs, 8, packfile_sort__cb) < 0 || if (git_packfile_alloc(&packfile, idx) < 0 ||
git_buf_joinpath(&path, objects_dir, "pack") < 0) git_vector_insert(&backend->packs, packfile) < 0)
{ {
git__free(backend); pack_backend__free((git_odb_backend *)backend);
return -1; return -1;
} }
if (git_path_isdir(git_buf_cstr(&path)) == true) { *backend_out = (git_odb_backend *)backend;
int error; return 0;
}
int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
{
int error = 0;
struct pack_backend *backend = NULL;
git_buf path = GIT_BUF_INIT;
if (pack_backend__alloc(&backend, 8) < 0)
return -1;
if (!(error = git_buf_joinpath(&path, objects_dir, "pack")) &&
git_path_isdir(git_buf_cstr(&path)))
{
backend->pack_folder = git_buf_detach(&path); backend->pack_folder = git_buf_detach(&path);
error = pack_backend__refresh((git_odb_backend *)backend); error = pack_backend__refresh((git_odb_backend *)backend);
if (error < 0)
return error;
} }
backend->parent.read = &pack_backend__read; if (error < 0) {
backend->parent.read_prefix = &pack_backend__read_prefix; pack_backend__free((git_odb_backend *)backend);
backend->parent.read_header = &pack_backend__read_header; backend = NULL;
backend->parent.exists = &pack_backend__exists; }
backend->parent.refresh = &pack_backend__refresh;
backend->parent.foreach = &pack_backend__foreach;
backend->parent.writepack = &pack_backend__writepack;
backend->parent.free = &pack_backend__free;
*backend_out = (git_odb_backend *)backend; *backend_out = (git_odb_backend *)backend;
git_buf_free(&path); git_buf_free(&path);
return 0; return error;
} }
...@@ -885,7 +885,7 @@ cleanup: ...@@ -885,7 +885,7 @@ cleanup:
return -1; return -1;
} }
int git_packfile_check(struct git_pack_file **pack_out, const char *path) int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
{ {
struct stat st; struct stat st;
struct git_pack_file *p; struct git_pack_file *p;
......
...@@ -143,7 +143,8 @@ git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs, ...@@ -143,7 +143,8 @@ git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
git_off_t delta_obj_offset); git_off_t delta_obj_offset);
void git_packfile_free(struct git_pack_file *p); void git_packfile_free(struct git_pack_file *p);
int git_packfile_check(struct git_pack_file **pack_out, const char *path); int git_packfile_alloc(struct git_pack_file **pack_out, const char *path);
int git_pack_entry_find( int git_pack_entry_find(
struct git_pack_entry *e, struct git_pack_entry *e,
struct git_pack_file *p, struct git_pack_file *p,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment