summaryrefslogtreecommitdiff
path: root/src/odb.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2013-08-15 14:29:39 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2013-08-15 14:29:39 +0200
commit8380b39a6761efa360398279083a65064d6770d3 (patch)
treeda94a1a19030319d68e211478b9fe75a0f4cc06e /src/odb.c
parent376e6c9f96ffcd572ba974c9cc4d13b4f1e31474 (diff)
downloadlibgit2-8380b39a6761efa360398279083a65064d6770d3.tar.gz
odb: perform the stream hashing in the frontend
Hash the data as it's coming into the stream and tell the backend what its name is when finalizing the write. This makes it consistent with the way a plain git_odb_write() performs the write.
Diffstat (limited to 'src/odb.c')
-rw-r--r--src/odb.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/odb.c b/src/odb.c
index 158159662..b7f64dfc9 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -871,11 +871,21 @@ int git_odb_write(
return error;
}
+static void hash_header(git_hash_ctx *ctx, size_t size, git_otype type)
+{
+ char header[64];
+ int hdrlen;
+
+ hdrlen = git_odb__format_object_header(header, sizeof(header), size, type);
+ git_hash_update(ctx, header, hdrlen);
+}
+
int git_odb_open_wstream(
git_odb_stream **stream, git_odb *db, size_t size, git_otype type)
{
size_t i, writes = 0;
int error = GIT_ERROR;
+ git_hash_ctx *ctx;
assert(stream && db);
@@ -901,16 +911,26 @@ int git_odb_open_wstream(
if (error < 0 && !writes)
error = git_odb__error_unsupported_in_backend("write object");
+ ctx = git__malloc(sizeof(git_hash_ctx));
+ GITERR_CHECK_ALLOC(ctx);
+
+
+ git_hash_ctx_init(ctx);
+ hash_header(ctx, size, type);
+ (*stream)->hash_ctx = ctx;
+
return error;
}
int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len)
{
+ git_hash_update(stream->hash_ctx, buffer, len);
return stream->write(stream, buffer, len);
}
int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
{
+ git_hash_final(out, stream->hash_ctx);
return stream->finalize_write(out, stream);
}
@@ -921,6 +941,7 @@ int git_odb_stream_read(git_odb_stream *stream, char *buffer, size_t len)
void git_odb_stream_free(git_odb_stream *stream)
{
+ git__free(stream->hash_ctx);
stream->free(stream);
}