odb/source-inmemory: implement `write_object_stream()` callback

Implement the `write_object_stream()` callback function for the in-memory source. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 10, 2026 at 14:12 UTC 197c8a85e37720e54afda1ed92bf8b393cca92f1
1 file changed +40
odb/source-inmemory.c
+40
@@ -128,6 +128,45 @@ static int odb_source_inmemory_write_object(struct odb_source *source,
128 return 0;
129 }
130
131 +static int odb_source_inmemory_write_object_stream(struct odb_source *source,
132 + struct odb_write_stream *stream,
133 + size_t len,
134 + struct object_id *oid)
135 +{
136 + char buf[16384];
137 + size_t total_read = 0;
138 + char *data;
139 + int ret;
140 +
141 + CALLOC_ARRAY(data, len);
142 + while (!stream->is_finished) {
143 + ssize_t bytes_read;
144 +
145 + bytes_read = odb_write_stream_read(stream, buf, sizeof(buf));
146 + if (total_read + bytes_read > len) {
147 + ret = error("object stream yielded more bytes than expected");
148 + goto out;
149 + }
150 +
151 + memcpy(data + total_read, buf, bytes_read);
152 + total_read += bytes_read;
153 + }
154 +
155 + if (total_read != len) {
156 + ret = error("object stream yielded less bytes than expected");
157 + goto out;
158 + }
159 +
160 + ret = odb_source_inmemory_write_object(source, data, len, OBJ_BLOB, oid,
161 + NULL, 0);
162 + if (ret < 0)
163 + goto out;
164 +
165 +out:
166 + free(data);
167 + return ret;
168 +}
169 +
170 static void odb_source_inmemory_free(struct odb_source *source)
171 {
172 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
@@ -149,6 +188,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
188 source->base.read_object_info = odb_source_inmemory_read_object_info;
189 source->base.read_object_stream = odb_source_inmemory_read_object_stream;
190 source->base.write_object = odb_source_inmemory_write_object;
191 + source->base.write_object_stream = odb_source_inmemory_write_object_stream;
192
193 return source;
194 }