Skip to content

Commit eb5abbb

Browse files
pks-tgitster
authored andcommitted
streaming: move zlib stream into backends
While all backend-specific data is now contained in a backend-specific structure, we still share the zlib stream across the loose and packed objects. Refactor the code and move it into the specific structures so that we fully detangle the different backends from one another. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1154b2d commit eb5abbb

1 file changed

Lines changed: 52 additions & 52 deletions

File tree

streaming.c

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,8 @@ struct odb_read_stream {
2525

2626
enum object_type type;
2727
unsigned long size; /* inflated size of full object */
28-
git_zstream z;
29-
enum { z_unused, z_used, z_done, z_error } z_state;
3028
};
3129

32-
/*****************************************************************
33-
*
34-
* Common helpers
35-
*
36-
*****************************************************************/
37-
38-
static void close_deflated_stream(struct odb_read_stream *st)
39-
{
40-
if (st->z_state == z_used)
41-
git_inflate_end(&st->z);
42-
}
43-
44-
4530
/*****************************************************************
4631
*
4732
* Filtered stream
@@ -150,6 +135,12 @@ static struct odb_read_stream *attach_stream_filter(struct odb_read_stream *st,
150135

151136
struct odb_loose_read_stream {
152137
struct odb_read_stream base;
138+
git_zstream z;
139+
enum {
140+
ODB_LOOSE_READ_STREAM_INUSE,
141+
ODB_LOOSE_READ_STREAM_DONE,
142+
ODB_LOOSE_READ_STREAM_ERROR,
143+
} z_state;
153144
void *mapped;
154145
unsigned long mapsize;
155146
char hdr[32];
@@ -162,10 +153,10 @@ static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t
162153
struct odb_loose_read_stream *st = (struct odb_loose_read_stream *)_st;
163154
size_t total_read = 0;
164155

165-
switch (st->base.z_state) {
166-
case z_done:
156+
switch (st->z_state) {
157+
case ODB_LOOSE_READ_STREAM_DONE:
167158
return 0;
168-
case z_error:
159+
case ODB_LOOSE_READ_STREAM_ERROR:
169160
return -1;
170161
default:
171162
break;
@@ -183,20 +174,20 @@ static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t
183174
while (total_read < sz) {
184175
int status;
185176

186-
st->base.z.next_out = (unsigned char *)buf + total_read;
187-
st->base.z.avail_out = sz - total_read;
188-
status = git_inflate(&st->base.z, Z_FINISH);
177+
st->z.next_out = (unsigned char *)buf + total_read;
178+
st->z.avail_out = sz - total_read;
179+
status = git_inflate(&st->z, Z_FINISH);
189180

190-
total_read = st->base.z.next_out - (unsigned char *)buf;
181+
total_read = st->z.next_out - (unsigned char *)buf;
191182

192183
if (status == Z_STREAM_END) {
193-
git_inflate_end(&st->base.z);
194-
st->base.z_state = z_done;
184+
git_inflate_end(&st->z);
185+
st->z_state = ODB_LOOSE_READ_STREAM_DONE;
195186
break;
196187
}
197188
if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) {
198-
git_inflate_end(&st->base.z);
199-
st->base.z_state = z_error;
189+
git_inflate_end(&st->z);
190+
st->z_state = ODB_LOOSE_READ_STREAM_ERROR;
200191
return -1;
201192
}
202193
}
@@ -206,7 +197,8 @@ static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t
206197
static int close_istream_loose(struct odb_read_stream *_st)
207198
{
208199
struct odb_loose_read_stream *st = (struct odb_loose_read_stream *)_st;
209-
close_deflated_stream(&st->base);
200+
if (st->z_state == ODB_LOOSE_READ_STREAM_INUSE)
201+
git_inflate_end(&st->z);
210202
munmap(st->mapped, st->mapsize);
211203
return 0;
212204
}
@@ -238,7 +230,7 @@ static int open_istream_loose(struct odb_read_stream **out,
238230
*/
239231
CALLOC_ARRAY(st, 1);
240232

241-
switch (unpack_loose_header(&st->base.z, mapped, mapsize, st->hdr,
233+
switch (unpack_loose_header(&st->z, mapped, mapsize, st->hdr,
242234
sizeof(st->hdr))) {
243235
case ULHR_OK:
244236
break;
@@ -256,16 +248,16 @@ static int open_istream_loose(struct odb_read_stream **out,
256248
st->mapped = mapped;
257249
st->mapsize = mapsize;
258250
st->hdr_used = strlen(st->hdr) + 1;
259-
st->hdr_avail = st->base.z.total_out;
260-
st->base.z_state = z_used;
251+
st->hdr_avail = st->z.total_out;
252+
st->z_state = ODB_LOOSE_READ_STREAM_INUSE;
261253
st->base.close = close_istream_loose;
262254
st->base.read = read_istream_loose;
263255

264256
*out = &st->base;
265257

266258
return 0;
267259
error:
268-
git_inflate_end(&st->base.z);
260+
git_inflate_end(&st->z);
269261
munmap(st->mapped, st->mapsize);
270262
free(st);
271263
return -1;
@@ -281,6 +273,13 @@ static int open_istream_loose(struct odb_read_stream **out,
281273
struct odb_packed_read_stream {
282274
struct odb_read_stream base;
283275
struct packed_git *pack;
276+
git_zstream z;
277+
enum {
278+
ODB_PACKED_READ_STREAM_UNINITIALIZED,
279+
ODB_PACKED_READ_STREAM_INUSE,
280+
ODB_PACKED_READ_STREAM_DONE,
281+
ODB_PACKED_READ_STREAM_ERROR,
282+
} z_state;
284283
off_t pos;
285284
};
286285

@@ -290,17 +289,17 @@ static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *bu
290289
struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
291290
size_t total_read = 0;
292291

293-
switch (st->base.z_state) {
294-
case z_unused:
295-
memset(&st->base.z, 0, sizeof(st->base.z));
296-
git_inflate_init(&st->base.z);
297-
st->base.z_state = z_used;
292+
switch (st->z_state) {
293+
case ODB_PACKED_READ_STREAM_UNINITIALIZED:
294+
memset(&st->z, 0, sizeof(st->z));
295+
git_inflate_init(&st->z);
296+
st->z_state = ODB_PACKED_READ_STREAM_INUSE;
298297
break;
299-
case z_done:
298+
case ODB_PACKED_READ_STREAM_DONE:
300299
return 0;
301-
case z_error:
300+
case ODB_PACKED_READ_STREAM_ERROR:
302301
return -1;
303-
case z_used:
302+
case ODB_PACKED_READ_STREAM_INUSE:
304303
break;
305304
}
306305

@@ -310,20 +309,20 @@ static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *bu
310309
unsigned char *mapped;
311310

312311
mapped = use_pack(st->pack, &window,
313-
st->pos, &st->base.z.avail_in);
312+
st->pos, &st->z.avail_in);
314313

315-
st->base.z.next_out = (unsigned char *)buf + total_read;
316-
st->base.z.avail_out = sz - total_read;
317-
st->base.z.next_in = mapped;
318-
status = git_inflate(&st->base.z, Z_FINISH);
314+
st->z.next_out = (unsigned char *)buf + total_read;
315+
st->z.avail_out = sz - total_read;
316+
st->z.next_in = mapped;
317+
status = git_inflate(&st->z, Z_FINISH);
319318

320-
st->pos += st->base.z.next_in - mapped;
321-
total_read = st->base.z.next_out - (unsigned char *)buf;
319+
st->pos += st->z.next_in - mapped;
320+
total_read = st->z.next_out - (unsigned char *)buf;
322321
unuse_pack(&window);
323322

324323
if (status == Z_STREAM_END) {
325-
git_inflate_end(&st->base.z);
326-
st->base.z_state = z_done;
324+
git_inflate_end(&st->z);
325+
st->z_state = ODB_PACKED_READ_STREAM_DONE;
327326
break;
328327
}
329328

@@ -336,8 +335,8 @@ static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *bu
336335
* or truncated), then use_pack() catches that and will die().
337336
*/
338337
if (status != Z_OK && status != Z_BUF_ERROR) {
339-
git_inflate_end(&st->base.z);
340-
st->base.z_state = z_error;
338+
git_inflate_end(&st->z);
339+
st->z_state = ODB_PACKED_READ_STREAM_ERROR;
341340
return -1;
342341
}
343342
}
@@ -347,7 +346,8 @@ static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *bu
347346
static int close_istream_pack_non_delta(struct odb_read_stream *_st)
348347
{
349348
struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
350-
close_deflated_stream(&st->base);
349+
if (st->z_state == ODB_PACKED_READ_STREAM_INUSE)
350+
git_inflate_end(&st->z);
351351
return 0;
352352
}
353353

@@ -384,7 +384,7 @@ static int open_istream_pack_non_delta(struct odb_read_stream **out,
384384
stream->base.read = read_istream_pack_non_delta;
385385
stream->base.type = in_pack_type;
386386
stream->base.size = size;
387-
stream->base.z_state = z_unused;
387+
stream->z_state = ODB_PACKED_READ_STREAM_UNINITIALIZED;
388388
stream->pack = pack;
389389
stream->pos = offset;
390390

0 commit comments

Comments
 (0)