]>
git.proxmox.com Git - libgit2.git/blob - src/odb_mempack.c
6d20b39baedf49e9be3e922123dc721c83ca7312
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
10 #include "git2/object.h"
11 #include "git2/sys/odb_backend.h"
12 #include "git2/sys/mempack.h"
19 #include "git2/odb_backend.h"
20 #include "git2/types.h"
21 #include "git2/pack.h"
27 char data
[GIT_FLEX_ARRAY
];
30 struct memory_packer_db
{
31 git_odb_backend parent
;
33 git_array_t(struct memobject
*) commits
;
36 static int impl__write(git_odb_backend
*_backend
, const git_oid
*oid
, const void *data
, size_t len
, git_object_t type
)
38 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
39 struct memobject
*obj
= NULL
;
42 if (git_oidmap_exists(db
->objects
, oid
))
45 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len
, sizeof(struct memobject
), len
);
46 obj
= git__malloc(alloc_len
);
47 GIT_ERROR_CHECK_ALLOC(obj
);
49 memcpy(obj
->data
, data
, len
);
50 git_oid_cpy(&obj
->oid
, oid
);
54 if (git_oidmap_set(db
->objects
, &obj
->oid
, obj
) < 0)
57 if (type
== GIT_OBJECT_COMMIT
) {
58 struct memobject
**store
= git_array_alloc(db
->commits
);
59 GIT_ERROR_CHECK_ALLOC(store
);
66 static int impl__exists(git_odb_backend
*backend
, const git_oid
*oid
)
68 struct memory_packer_db
*db
= (struct memory_packer_db
*)backend
;
70 return git_oidmap_exists(db
->objects
, oid
);
73 static int impl__read(void **buffer_p
, size_t *len_p
, git_object_t
*type_p
, git_odb_backend
*backend
, const git_oid
*oid
)
75 struct memory_packer_db
*db
= (struct memory_packer_db
*)backend
;
76 struct memobject
*obj
;
78 if ((obj
= git_oidmap_get(db
->objects
, oid
)) == NULL
)
83 *buffer_p
= git__malloc(obj
->len
);
84 GIT_ERROR_CHECK_ALLOC(*buffer_p
);
86 memcpy(*buffer_p
, obj
->data
, obj
->len
);
90 static int impl__read_header(size_t *len_p
, git_object_t
*type_p
, git_odb_backend
*backend
, const git_oid
*oid
)
92 struct memory_packer_db
*db
= (struct memory_packer_db
*)backend
;
93 struct memobject
*obj
;
95 if ((obj
= git_oidmap_get(db
->objects
, oid
)) == NULL
)
103 int git_mempack_dump(git_buf
*pack
, git_repository
*repo
, git_odb_backend
*_backend
)
105 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
106 git_packbuilder
*packbuilder
;
110 if (git_packbuilder_new(&packbuilder
, repo
) < 0)
113 git_packbuilder_set_threads(packbuilder
, 0);
115 for (i
= 0; i
< db
->commits
.size
; ++i
) {
116 struct memobject
*commit
= db
->commits
.ptr
[i
];
118 err
= git_packbuilder_insert_commit(packbuilder
, &commit
->oid
);
123 err
= git_packbuilder_write_buf(pack
, packbuilder
);
126 git_packbuilder_free(packbuilder
);
130 int git_mempack_reset(git_odb_backend
*_backend
)
132 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
133 struct memobject
*object
= NULL
;
135 git_oidmap_foreach_value(db
->objects
, object
, {
139 git_array_clear(db
->commits
);
141 git_oidmap_clear(db
->objects
);
146 static void impl__free(git_odb_backend
*_backend
)
148 struct memory_packer_db
*db
= (struct memory_packer_db
*)_backend
;
150 git_mempack_reset(_backend
);
151 git_oidmap_free(db
->objects
);
155 int git_mempack_new(git_odb_backend
**out
)
157 struct memory_packer_db
*db
;
161 db
= git__calloc(1, sizeof(struct memory_packer_db
));
162 GIT_ERROR_CHECK_ALLOC(db
);
164 if (git_oidmap_new(&db
->objects
) < 0)
167 db
->parent
.version
= GIT_ODB_BACKEND_VERSION
;
168 db
->parent
.read
= &impl__read
;
169 db
->parent
.write
= &impl__write
;
170 db
->parent
.read_header
= &impl__read_header
;
171 db
->parent
.exists
= &impl__exists
;
172 db
->parent
.free
= &impl__free
;
174 *out
= (git_odb_backend
*)db
;