Deploy versioned git_odb_backend structure
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
diff --git a/src/odb.c b/src/odb.c
index 63b6828..e35e4ca 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -363,12 +363,27 @@ int git_odb_new(git_odb **out)
return 0;
}
+static bool backend_has_valid_version(git_odb_backend *backend)
+{
+ if (!backend)
+ return true;
+
+ if (backend->version > 0 && backend->version <= GIT_ODB_BACKEND_VERSION)
+ return true;
+
+ giterr_set(GITERR_INVALID, "Invalid version %d on git_odb_backend", backend->version);
+ return false;
+}
+
static int add_backend_internal(git_odb *odb, git_odb_backend *backend, int priority, int is_alternate)
{
backend_internal *internal;
assert(odb && backend);
+ if (!backend_has_valid_version(backend))
+ return -1;
+
/* Check if the backend is already owned by another ODB */
assert(!backend->odb || backend->odb == odb);
diff --git a/src/odb_loose.c b/src/odb_loose.c
index e2f1aec..df86d90 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -915,6 +915,7 @@ int git_odb_backend_loose(
backend = git__calloc(1, sizeof(loose_backend));
GITERR_CHECK_ALLOC(backend);
+ backend->parent.version = GIT_ODB_BACKEND_VERSION;
backend->objects_dir = git__strdup(objects_dir);
GITERR_CHECK_ALLOC(backend->objects_dir);
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 35bf158..41789e4 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -570,6 +570,7 @@ int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx)
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;
@@ -602,6 +603,7 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
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, 8, packfile_sort__cb) < 0 ||
git_buf_joinpath(&path, objects_dir, "pack") < 0)
diff --git a/tests-clar/odb/sorting.c b/tests-clar/odb/sorting.c
index bf64f6a..b4f9e44 100644
--- a/tests-clar/odb/sorting.c
+++ b/tests-clar/odb/sorting.c
@@ -11,11 +11,11 @@ static git_odb_backend *new_backend(int position)
{
fake_backend *b;
- b = git__malloc(sizeof(fake_backend));
+ b = git__calloc(1, sizeof(fake_backend));
if (b == NULL)
return NULL;
- memset(b, 0x0, sizeof(fake_backend));
+ b->base.version = GIT_ODB_BACKEND_VERSION;
b->position = position;
return (git_odb_backend *)b;
}