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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
diff --git a/src/indexer.c b/src/indexer.c
index 50a9d3a..6067719 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -62,7 +62,7 @@ static int open_pack(struct git_pack_file **out, const char *filename)
{
struct git_pack_file *pack;
- if (git_packfile_check(&pack, filename) < 0)
+ if (git_packfile_alloc(&pack, filename) < 0)
return -1;
if ((pack->mwf.fd = p_open(pack->pack_name, O_RDONLY)) < 0) {
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 773e149..eec7925 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -207,7 +207,7 @@ static int packfile_load__cb(void *_data, git_buf *path)
return 0;
}
- error = git_packfile_check(&pack, path->ptr);
+ error = git_packfile_alloc(&pack, path->ptr);
if (error == GIT_ENOTFOUND)
/* ignore missing .pack file as git does */
return 0;
@@ -527,80 +527,75 @@ static void pack_backend__free(git_odb_backend *_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 git_pack_file *packfile = NULL;
+ struct pack_backend *backend = git__calloc(1, sizeof(struct pack_backend));
+ 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;
+ }
- backend = git__calloc(1, sizeof(struct pack_backend));
- GITERR_CHECK_ALLOC(backend);
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_prefix = &pack_backend__read_prefix;
backend->parent.read_header = &pack_backend__read_header;
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;
-
+ *out = backend;
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;
- git_buf path = GIT_BUF_INIT;
+ struct git_pack_file *packfile = NULL;
- backend = git__calloc(1, sizeof(struct pack_backend));
- GITERR_CHECK_ALLOC(backend);
- backend->parent.version = GIT_ODB_BACKEND_VERSION;
+ if (pack_backend__alloc(&backend, 1) < 0)
+ return -1;
- if (git_vector_init(&backend->packs, 8, packfile_sort__cb) < 0 ||
- git_buf_joinpath(&path, objects_dir, "pack") < 0)
+ if (git_packfile_alloc(&packfile, idx) < 0 ||
+ git_vector_insert(&backend->packs, packfile) < 0)
{
- git__free(backend);
+ pack_backend__free((git_odb_backend *)backend);
return -1;
}
- if (git_path_isdir(git_buf_cstr(&path)) == true) {
- int error;
+ *backend_out = (git_odb_backend *)backend;
+ 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);
+
error = pack_backend__refresh((git_odb_backend *)backend);
- if (error < 0)
- return error;
}
- backend->parent.read = &pack_backend__read;
- backend->parent.read_prefix = &pack_backend__read_prefix;
- backend->parent.read_header = &pack_backend__read_header;
- 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;
+ if (error < 0) {
+ pack_backend__free((git_odb_backend *)backend);
+ backend = NULL;
+ }
*backend_out = (git_odb_backend *)backend;
git_buf_free(&path);
- return 0;
+ return error;
}
diff --git a/src/pack.c b/src/pack.c
index 8e8a01a..33cdf76 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -885,7 +885,7 @@ cleanup:
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 git_pack_file *p;
diff --git a/src/pack.h b/src/pack.h
index b8014b1..aeeac9c 100644
--- a/src/pack.h
+++ b/src/pack.h
@@ -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);
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(
struct git_pack_entry *e,
struct git_pack_file *p,