Skip to content

Commit a4e0a8b

Browse files
jltoblergitster
authored andcommitted
object-file: generalize packfile writes to use odb_write_stream
The `index_blob_packfile_transaction()` function streams blob data directly from an fd. This makes it difficult to reuse as part of a generic transactional object writing interface. Refactor the packfile write path to operate on a `struct odb_write_stream`, allowing callers to supply data from arbitrary sources. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b247256 commit a4e0a8b

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

object-file.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,18 +1446,19 @@ static int hash_blob_stream(struct odb_write_stream *stream,
14461446
}
14471447

14481448
/*
1449-
* Read the contents from fd for size bytes, streaming it to the
1449+
* Read the contents from the stream provided, streaming it to the
14501450
* packfile in state while updating the hash in ctx.
14511451
*/
14521452
static void stream_blob_to_pack(struct transaction_packfile *state,
1453-
struct git_hash_ctx *ctx, int fd, size_t size,
1454-
const char *path)
1453+
struct git_hash_ctx *ctx, size_t size,
1454+
struct odb_write_stream *stream)
14551455
{
14561456
git_zstream s;
14571457
unsigned char ibuf[16384];
14581458
unsigned char obuf[16384];
14591459
unsigned hdrlen;
14601460
int status = Z_OK;
1461+
size_t bytes_read = 0;
14611462

14621463
git_deflate_init(&s, pack_compression_level);
14631464

@@ -1466,23 +1467,21 @@ static void stream_blob_to_pack(struct transaction_packfile *state,
14661467
s.avail_out = sizeof(obuf) - hdrlen;
14671468

14681469
while (status != Z_STREAM_END) {
1469-
if (size && !s.avail_in) {
1470-
size_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
1471-
ssize_t read_result = read_in_full(fd, ibuf, rsize);
1472-
if (read_result < 0)
1473-
die_errno("failed to read from '%s'", path);
1474-
if ((size_t)read_result != rsize)
1475-
die("failed to read %u bytes from '%s'",
1476-
(unsigned)rsize, path);
1470+
if (!stream->is_finished && !s.avail_in) {
1471+
ssize_t rsize = odb_write_stream_read(stream, ibuf,
1472+
sizeof(ibuf));
1473+
1474+
if (rsize < 0)
1475+
die("failed to read blob data");
14771476

14781477
git_hash_update(ctx, ibuf, rsize);
14791478

14801479
s.next_in = ibuf;
14811480
s.avail_in = rsize;
1482-
size -= rsize;
1481+
bytes_read += rsize;
14831482
}
14841483

1485-
status = git_deflate(&s, size ? 0 : Z_FINISH);
1484+
status = git_deflate(&s, stream->is_finished ? Z_FINISH : 0);
14861485

14871486
if (!s.avail_out || status == Z_STREAM_END) {
14881487
size_t written = s.next_out - obuf;
@@ -1502,6 +1501,11 @@ static void stream_blob_to_pack(struct transaction_packfile *state,
15021501
die("unexpected deflate failure: %d", status);
15031502
}
15041503
}
1504+
1505+
if (bytes_read != size)
1506+
die("read %" PRIuMAX " bytes of blob data, but expected %" PRIuMAX " bytes",
1507+
(uintmax_t)bytes_read, (uintmax_t)size);
1508+
15051509
git_deflate_end(&s);
15061510
}
15071511

@@ -1573,10 +1577,13 @@ static void flush_packfile_transaction(struct odb_transaction_files *transaction
15731577
* binary blobs, they generally do not want to get any conversion, and
15741578
* callers should avoid this code path when filters are requested.
15751579
*/
1576-
static int index_blob_packfile_transaction(struct odb_transaction_files *transaction,
1577-
struct object_id *result_oid, int fd,
1578-
size_t size, const char *path)
1580+
static int index_blob_packfile_transaction(struct odb_transaction *base,
1581+
struct odb_write_stream *stream,
1582+
size_t size, struct object_id *result_oid)
15791583
{
1584+
struct odb_transaction_files *transaction = container_of(base,
1585+
struct odb_transaction_files,
1586+
base);
15801587
struct transaction_packfile *state = &transaction->packfile;
15811588
struct git_hash_ctx ctx;
15821589
unsigned char obuf[16384];
@@ -1610,7 +1617,7 @@ static int index_blob_packfile_transaction(struct odb_transaction_files *transac
16101617
hashfile_checkpoint(state->f, &checkpoint);
16111618
idx->offset = state->offset;
16121619
crc32_begin(state->f);
1613-
stream_blob_to_pack(state, &ctx, fd, size, path);
1620+
stream_blob_to_pack(state, &ctx, size, stream);
16141621
git_hash_final_oid(result_oid, &ctx);
16151622

16161623
idx->crc32 = crc32_end(state->f);
@@ -1654,15 +1661,12 @@ int index_fd(struct index_state *istate, struct object_id *oid,
16541661

16551662
if (flags & INDEX_WRITE_OBJECT) {
16561663
struct object_database *odb = the_repository->objects;
1657-
struct odb_transaction_files *files_transaction;
1658-
struct odb_transaction *transaction;
1659-
1660-
transaction = odb_transaction_begin(odb);
1661-
files_transaction = container_of(odb->transaction,
1662-
struct odb_transaction_files,
1663-
base);
1664-
ret = index_blob_packfile_transaction(files_transaction, oid, fd,
1665-
xsize_t(st->st_size), path);
1664+
struct odb_transaction *transaction = odb_transaction_begin(odb);
1665+
1666+
ret = index_blob_packfile_transaction(odb->transaction,
1667+
&stream,
1668+
xsize_t(st->st_size),
1669+
oid);
16661670
odb_transaction_commit(transaction);
16671671
} else {
16681672
ret = hash_blob_stream(&stream,

0 commit comments

Comments
 (0)