]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source-inmemory: implement `write_object_stream()` callback
authorPatrick Steinhardt <ps@pks.im>
Fri, 10 Apr 2026 12:12:37 +0000 (14:12 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 May 2026 19:50:45 +0000 (04:50 +0900)
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>
odb/source-inmemory.c

index 4848011df5189cff1a2a3b89d0294637b662a177..d05a13df45ea5f6ce884d100d257e6f354e582ea 100644 (file)
@@ -128,6 +128,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)
+{
+       char buf[16384];
+       size_t total_read = 0;
+       char *data;
+       int ret;
+
+       CALLOC_ARRAY(data, len);
+       while (!stream->is_finished) {
+               ssize_t bytes_read;
+
+               bytes_read = odb_write_stream_read(stream, buf, sizeof(buf));
+               if (total_read + bytes_read > len) {
+                       ret = error("object stream yielded more bytes than expected");
+                       goto out;
+               }
+
+               memcpy(data + total_read, buf, 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);
@@ -149,6 +188,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;
 }