Git-Raw
view release on metacpan or search on metacpan
deps/libgit2/src/libgit2/odb.c view on Meta::CPAN
}
error = git_vector_dup(&backends, &db->backends, NULL);
git_mutex_unlock(&db->lock);
if (error < 0)
goto cleanup;
git_vector_foreach(&backends, i, internal) {
git_odb_backend *b = internal->backend;
error = b->foreach(b, cb, payload);
if (error != 0)
goto cleanup;
}
cleanup:
git_vector_free(&backends);
return error;
}
int git_odb_write(
git_oid *oid, git_odb *db, const void *data, size_t len, git_object_t type)
{
size_t i;
int error;
git_odb_stream *stream;
GIT_ASSERT_ARG(oid);
GIT_ASSERT_ARG(db);
if ((error = git_odb_hash(oid, data, len, type)) < 0)
return error;
if (git_oid_is_zero(oid))
return error_null_oid(GIT_EINVALID, "cannot write object");
if (git_odb__freshen(db, oid))
return 0;
if ((error = git_mutex_lock(&db->lock)) < 0) {
git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock");
return error;
}
for (i = 0, error = GIT_ERROR; i < db->backends.length && error < 0; ++i) {
backend_internal *internal = git_vector_get(&db->backends, i);
git_odb_backend *b = internal->backend;
/* we don't write in alternates! */
if (internal->is_alternate)
continue;
if (b->write != NULL)
error = b->write(b, oid, data, len, type);
}
git_mutex_unlock(&db->lock);
if (!error || error == GIT_PASSTHROUGH)
return 0;
/* if no backends were able to write the object directly, we try a
* streaming write to the backends; just write the whole object into the
* stream in one push
*/
if ((error = git_odb_open_wstream(&stream, db, len, type)) != 0)
return error;
if ((error = stream->write(stream, data, len)) == 0)
error = stream->finalize_write(stream, oid);
git_odb_stream_free(stream);
return error;
}
static int hash_header(git_hash_ctx *ctx, git_object_size_t size, git_object_t type)
{
char header[64];
size_t hdrlen;
int error;
if ((error = git_odb__format_object_header(&hdrlen,
header, sizeof(header), size, type)) < 0)
return error;
return git_hash_update(ctx, header, hdrlen);
}
int git_odb_open_wstream(
git_odb_stream **stream, git_odb *db, git_object_size_t size, git_object_t type)
{
size_t i, writes = 0;
int error = GIT_ERROR;
git_hash_ctx *ctx = NULL;
GIT_ASSERT_ARG(stream);
GIT_ASSERT_ARG(db);
if ((error = git_mutex_lock(&db->lock)) < 0) {
git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock");
return error;
}
error = GIT_ERROR;
for (i = 0; i < db->backends.length && error < 0; ++i) {
backend_internal *internal = git_vector_get(&db->backends, i);
git_odb_backend *b = internal->backend;
/* we don't write in alternates! */
if (internal->is_alternate)
continue;
if (b->writestream != NULL) {
++writes;
error = b->writestream(stream, b, size, type);
} else if (b->write != NULL) {
++writes;
error = init_fake_wstream(stream, b, size, type);
}
}
git_mutex_unlock(&db->lock);
if (error < 0) {
if (error == GIT_PASSTHROUGH)
( run in 0.572 second using v1.01-cache-2.11-cpan-99c4e6809bf )