[PATCH v3 3/5] object-file.c: read stream in a loop in write_loose_object()
- From
- Han Xin <chiyutianyi@gmail.com>
- Date
- Nov 22, 2021, 03:32 UTC
- Message-ID
- <20211122033220.32883-4-chiyutianyi@gmail.com>
- In-Reply-To
- <20211009082058.41138-1-chiyutianyi@gmail.com>
From: Han Xin <hanxin.hx@alibaba-inc.com>
In order to prepare the stream version of "write_loose_object()", read the input stream in a loop in "write_loose_object()", so that we can feed the contents of large blob object to "write_loose_object()" using a small fixed buffer.
Helped-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Han Xin <hanxin.hx@alibaba-inc.com> --- object-file.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/object-file.c b/object-file.c index 78fd2a5d39..93bcfaca50 100644 --- a/object-file.c +++ b/object-file.c @@ -1890,7 +1890,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr, static struct strbuf tmp_file = STRBUF_INIT; static struct strbuf filename = STRBUF_INIT; const void *buf; - unsigned long len; + int flush = 0; if (is_null_oid(oid)) { /* When oid is not determined, save tmp file to odb path. */ @@ -1925,12 +1925,18 @@ static int write_loose_object(const struct object_id *oid, char *hdr, the_hash_algo->update_fn(&c, hdr, hdrlen); /* Then the data itself.. */ - buf = in_stream->read(in_stream, &len); - stream.next_in = (void *)buf; - stream.avail_in = len; do { unsigned char *in0 = stream.next_in; - ret = git_deflate(&stream, Z_FINISH); + if (!stream.avail_in) { + buf = in_stream->read(in_stream, &stream.avail_in); + if (buf) { + stream.next_in = (void *)buf; + in0 = (unsigned char *)buf; + } else { + flush = Z_FINISH; + } + } + ret = git_deflate(&stream, flush); the_hash_algo->update_fn(&c, in0, stream.next_in - in0); if (write_buffer(fd, compressed, stream.next_out - compressed) < 0) die(_("unable to write loose object file"));
-- 2.34.0.6.g676eedc724