Re: [PATCH 07/16] odb/source-inmemory: implement `write_object_stream()` callback
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Apr 3, 2026, 22:11 UTC
- Message-ID
- <xmqqzf3jlmnv.fsf@gitster.g>
- In-Reply-To
- <20260403-b4-pks-odb-source-inmemory-v1-7-8b8d1abaa25e@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 7 quoted lines
> Implement the `write_object_stream()` callback function for the inmemory > source. > > Signed-off-by: Patrick Steinhardt <ps@pks.im> > --- > odb/source-inmemory.c | 40 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 40 insertions(+)
As the signature of the .read() method drastically changes in another topic in flight,
https://lore.kernel.org/git/20260402213220.2651523-4-jltobler@gmail.com/
this needs a bit of inter-topic coordination.
Show 58 quoted lines
> diff --git a/odb/source-inmemory.c b/odb/source-inmemory.c
> index d2fc4c4054..890e2a8c7c 100644
> --- a/odb/source-inmemory.c
> +++ b/odb/source-inmemory.c
> @@ -123,6 +123,45 @@ static int odb_source_inmemory_write_object(struct odb_source *source,
> return 0;
> }
>
> +static int odb_source_inmemory_write_object_stream(struct odb_source *source,
> + struct odb_write_stream *stream,
> + size_t len,
> + struct object_id *oid)
> +{
> + size_t total_read = 0;
> + char *data;
> + int ret;
> +
> + CALLOC_ARRAY(data, len);
> + while (!stream->is_finished) {
> + unsigned long bytes_read;
> + const void *in;
> +
> + in = stream->read(stream, &bytes_read);
> + if (total_read + bytes_read > len) {
> + ret = error("object stream yielded more bytes than expected");
> + goto out;
> + }
> +
> + memcpy(data, in, bytes_read);
> + total_read += bytes_read;
> + }
> +
> + if (total_read != len) {
> + ret = error("object stream yielded less bytes than expected");
> + goto out;
> + }
> +
> + ret = odb_source_inmemory_write_object(source, data, len, OBJ_BLOB, oid,
> + NULL, 0);
> + if (ret < 0)
> + goto out;
> +
> +out:
> + free(data);
> + return ret;
> +}
> +
> static void odb_source_inmemory_free(struct odb_source *source)
> {
> struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
> @@ -144,6 +183,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
> source->base.read_object_info = odb_source_inmemory_read_object_info;
> source->base.read_object_stream = odb_source_inmemory_read_object_stream;
> source->base.write_object = odb_source_inmemory_write_object;
> + source->base.write_object_stream = odb_source_inmemory_write_object_stream;
>
> return source;
> }