Skip to content

Commit 3fad664

Browse files
jltoblergitster
authored andcommitted
odb: update struct odb_write_stream read() callback
The `read()` callback used by `struct odb_write_stream` currently returns a pointer to an internal buffer along with the number of bytes read. This makes buffer ownership unclear and provides no way to report errors. Update the interface to instead require the caller to provide a buffer, and have the callback return the number of bytes written to it or a negative value on error. Call sites are updated accordingly. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ad1a0aa commit 3fad664

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

builtin/unpack-objects.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -360,34 +360,29 @@ static void unpack_non_delta_entry(enum object_type type, unsigned long size,
360360

361361
struct input_zstream_data {
362362
git_zstream *zstream;
363-
unsigned char buf[8192];
364363
int status;
365364
};
366365

367-
static const void *feed_input_zstream(struct odb_write_stream *in_stream,
368-
unsigned long *readlen)
366+
static ssize_t feed_input_zstream(struct odb_write_stream *in_stream,
367+
unsigned char *buf, size_t buf_len)
369368
{
370369
struct input_zstream_data *data = in_stream->data;
371370
git_zstream *zstream = data->zstream;
372371
void *in = fill(1);
373372

374-
if (in_stream->is_finished) {
375-
*readlen = 0;
376-
return NULL;
377-
}
373+
if (in_stream->is_finished)
374+
return 0;
378375

379-
zstream->next_out = data->buf;
380-
zstream->avail_out = sizeof(data->buf);
376+
zstream->next_out = buf;
377+
zstream->avail_out = buf_len;
381378
zstream->next_in = in;
382379
zstream->avail_in = len;
383380

384381
data->status = git_inflate(zstream, 0);
385382

386383
in_stream->is_finished = data->status != Z_OK;
387384
use(len - zstream->avail_in);
388-
*readlen = sizeof(data->buf) - zstream->avail_out;
389-
390-
return data->buf;
385+
return buf_len - zstream->avail_out;
391386
}
392387

393388
static void stream_blob(unsigned long size, unsigned nr)

object-file.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,7 @@ int odb_source_loose_write_stream(struct odb_source *source,
10661066
struct git_hash_ctx c, compat_c;
10671067
struct strbuf tmp_file = STRBUF_INIT;
10681068
struct strbuf filename = STRBUF_INIT;
1069+
unsigned char buf[8192];
10691070
int dirlen;
10701071
char hdr[MAX_HEADER_LEN];
10711072
int hdrlen;
@@ -1098,9 +1099,15 @@ int odb_source_loose_write_stream(struct odb_source *source,
10981099
unsigned char *in0 = stream.next_in;
10991100

11001101
if (!stream.avail_in && !in_stream->is_finished) {
1101-
const void *in = in_stream->read(in_stream, &stream.avail_in);
1102-
stream.next_in = (void *)in;
1103-
in0 = (unsigned char *)in;
1102+
ssize_t read_len = in_stream->read(in_stream, buf, sizeof(buf));
1103+
if (read_len < 0) {
1104+
err = -1;
1105+
goto cleanup;
1106+
}
1107+
1108+
stream.avail_in = read_len;
1109+
stream.next_in = buf;
1110+
in0 = buf;
11041111
/* All data has been read. */
11051112
if (in_stream->is_finished)
11061113
flush = 1;

odb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ static inline int odb_write_object(struct object_database *odb,
530530
}
531531

532532
struct odb_write_stream {
533-
const void *(*read)(struct odb_write_stream *, unsigned long *len);
533+
ssize_t (*read)(struct odb_write_stream *, unsigned char *, size_t len);
534534
void *data;
535535
int is_finished;
536536
};

0 commit comments

Comments
 (0)