Skip to content

Commit 0edc26c

Browse files
committed
pack: refactor streams to use git_zstream
While we do have a `git_zstream` abstraction that encapsulates all the calls to zlib as well as its error handling, we do not use it in our pack file code. Refactor it to make the code a lot easier to understand.
1 parent d8f6fee commit 0edc26c

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

src/pack.c

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#include "odb.h"
1414
#include "oid.h"
1515
#include "sha1_lookup.h"
16-
#include "zstream.h"
17-
18-
#include <zlib.h>
1916

2017
/* Option to bypass checking existence of '.keep' files */
2118
bool git_disable_pack_keep_file_checks = false;
@@ -766,31 +763,13 @@ int git_packfile_unpack(
766763
return error;
767764
}
768765

769-
static void *use_git_alloc(void *opaq, unsigned int count, unsigned int size)
770-
{
771-
GIT_UNUSED(opaq);
772-
return git__calloc(count, size);
773-
}
774-
775-
static void use_git_free(void *opaq, void *ptr)
776-
{
777-
GIT_UNUSED(opaq);
778-
git__free(ptr);
779-
}
780-
781766
int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, off64_t curpos)
782767
{
783-
int st;
784-
785768
memset(obj, 0, sizeof(git_packfile_stream));
786769
obj->curpos = curpos;
787770
obj->p = p;
788-
obj->zstream.zalloc = use_git_alloc;
789-
obj->zstream.zfree = use_git_free;
790-
obj->zstream.next_in = Z_NULL;
791-
obj->zstream.next_out = Z_NULL;
792-
st = inflateInit(&obj->zstream);
793-
if (st != Z_OK) {
771+
772+
if (git_zstream_init(&obj->zstream, GIT_ZSTREAM_INFLATE) < 0) {
794773
git_error_set(GIT_ERROR_ZLIB, "failed to init packfile stream");
795774
return -1;
796775
}
@@ -800,47 +779,41 @@ int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p,
800779

801780
ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len)
802781
{
782+
unsigned int window_len;
803783
unsigned char *in;
804-
size_t written;
805-
int st;
784+
int error;
806785

807786
if (obj->done)
808787
return 0;
809788

810-
in = pack_window_open(obj->p, &obj->mw, obj->curpos, &obj->zstream.avail_in);
811-
if (in == NULL)
789+
if ((in = pack_window_open(obj->p, &obj->mw, obj->curpos, &window_len)) == NULL)
812790
return GIT_EBUFS;
813791

814-
obj->zstream.next_out = buffer;
815-
obj->zstream.avail_out = (unsigned int)len;
816-
obj->zstream.next_in = in;
817-
818-
st = inflate(&obj->zstream, Z_SYNC_FLUSH);
819-
git_mwindow_close(&obj->mw);
820-
821-
obj->curpos += obj->zstream.next_in - in;
822-
written = len - obj->zstream.avail_out;
823-
824-
if (st != Z_OK && st != Z_STREAM_END) {
792+
if ((error = git_zstream_set_input(&obj->zstream, in, window_len)) < 0 ||
793+
(error = git_zstream_get_output_chunk(buffer, &len, &obj->zstream)) < 0) {
794+
git_mwindow_close(&obj->mw);
825795
git_error_set(GIT_ERROR_ZLIB, "error reading from the zlib stream");
826796
return -1;
827797
}
828798

829-
if (st == Z_STREAM_END)
830-
obj->done = 1;
799+
git_mwindow_close(&obj->mw);
831800

801+
obj->curpos += window_len - obj->zstream.in_len;
802+
803+
if (git_zstream_eos(&obj->zstream))
804+
obj->done = 1;
832805

833806
/* If we didn't write anything out but we're not done, we need more data */
834-
if (!written && st != Z_STREAM_END)
807+
if (!len && !git_zstream_eos(&obj->zstream))
835808
return GIT_EBUFS;
836809

837-
return written;
810+
return len;
838811

839812
}
840813

841814
void git_packfile_stream_dispose(git_packfile_stream *obj)
842815
{
843-
inflateEnd(&obj->zstream);
816+
git_zstream_free(&obj->zstream);
844817
}
845818

846819
static int packfile_unpack_compressed(

src/pack.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010

1111
#include "common.h"
1212

13-
#include <zlib.h>
14-
1513
#include "git2/oid.h"
1614

15+
#include "array.h"
1716
#include "map.h"
1817
#include "mwindow.h"
1918
#include "odb.h"
2019
#include "offmap.h"
2120
#include "oidmap.h"
22-
#include "array.h"
21+
#include "zstream.h"
2322

2423
#define GIT_PACK_FILE_MODE 0444
2524

@@ -116,7 +115,7 @@ struct git_pack_entry {
116115
typedef struct git_packfile_stream {
117116
off64_t curpos;
118117
int done;
119-
z_stream zstream;
118+
git_zstream zstream;
120119
struct git_pack_file *p;
121120
git_mwindow *mw;
122121
} git_packfile_stream;

0 commit comments

Comments
 (0)