mirror of
https://github.com/git/git.git
synced 2024-11-23 18:05:29 +08:00
zlib: wrap deflate side of the API
Wrap deflateInit, deflate, and deflateEnd for everybody, and the sole use of deflateInit2 in remote-curl.c to tell the library to use gzip header and trailer in git_deflate_init_gzip(). There is only one caller that cares about the status from deflateEnd(). Introduce git_deflate_end_gently() to let that sole caller retrieve the status and act on it (i.e. die) for now, but we would probably want to make inflate_end/deflate_end die when they ran out of memory and get rid of the _gently() kind. Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
5e86c1fb86
commit
55bb5c9147
@ -96,7 +96,7 @@ static void *zlib_deflate(void *data, unsigned long size,
|
||||
int result;
|
||||
|
||||
memset(&stream, 0, sizeof(stream));
|
||||
deflateInit(&stream, compression_level);
|
||||
git_deflate_init(&stream, compression_level);
|
||||
maxsize = deflateBound(&stream, size);
|
||||
buffer = xmalloc(maxsize);
|
||||
|
||||
@ -106,7 +106,7 @@ static void *zlib_deflate(void *data, unsigned long size,
|
||||
stream.avail_out = maxsize;
|
||||
|
||||
do {
|
||||
result = deflate(&stream, Z_FINISH);
|
||||
result = git_deflate(&stream, Z_FINISH);
|
||||
} while (result == Z_OK);
|
||||
|
||||
if (result != Z_STREAM_END) {
|
||||
@ -114,7 +114,7 @@ static void *zlib_deflate(void *data, unsigned long size,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
deflateEnd(&stream);
|
||||
git_deflate_end(&stream);
|
||||
*compressed_size = stream.total_out;
|
||||
|
||||
return buffer;
|
||||
|
@ -671,21 +671,21 @@ static int write_compressed(struct sha1file *f, void *in, unsigned int size)
|
||||
unsigned char outbuf[4096];
|
||||
|
||||
memset(&stream, 0, sizeof(stream));
|
||||
deflateInit(&stream, zlib_compression_level);
|
||||
git_deflate_init(&stream, zlib_compression_level);
|
||||
stream.next_in = in;
|
||||
stream.avail_in = size;
|
||||
|
||||
do {
|
||||
stream.next_out = outbuf;
|
||||
stream.avail_out = sizeof(outbuf);
|
||||
status = deflate(&stream, Z_FINISH);
|
||||
status = git_deflate(&stream, Z_FINISH);
|
||||
sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out);
|
||||
} while (status == Z_OK);
|
||||
|
||||
if (status != Z_STREAM_END)
|
||||
die("unable to deflate appended object (%d)", status);
|
||||
size = stream.total_out;
|
||||
deflateEnd(&stream);
|
||||
git_deflate_end(&stream);
|
||||
return size;
|
||||
}
|
||||
|
||||
|
@ -131,7 +131,7 @@ static unsigned long do_compress(void **pptr, unsigned long size)
|
||||
unsigned long maxsize;
|
||||
|
||||
memset(&stream, 0, sizeof(stream));
|
||||
deflateInit(&stream, pack_compression_level);
|
||||
git_deflate_init(&stream, pack_compression_level);
|
||||
maxsize = deflateBound(&stream, size);
|
||||
|
||||
in = *pptr;
|
||||
@ -142,9 +142,9 @@ static unsigned long do_compress(void **pptr, unsigned long size)
|
||||
stream.avail_in = size;
|
||||
stream.next_out = out;
|
||||
stream.avail_out = maxsize;
|
||||
while (deflate(&stream, Z_FINISH) == Z_OK)
|
||||
while (git_deflate(&stream, Z_FINISH) == Z_OK)
|
||||
; /* nothing */
|
||||
deflateEnd(&stream);
|
||||
git_deflate_end(&stream);
|
||||
|
||||
free(in);
|
||||
return stream.total_out;
|
||||
|
6
cache.h
6
cache.h
@ -25,6 +25,12 @@ void git_inflate_init_gzip_only(z_streamp strm);
|
||||
void git_inflate_end(z_streamp strm);
|
||||
int git_inflate(z_streamp strm, int flush);
|
||||
|
||||
void git_deflate_init(z_streamp strm, int level);
|
||||
void git_deflate_init_gzip(z_streamp strm, int level);
|
||||
void git_deflate_end(z_streamp strm);
|
||||
int git_deflate_end_gently(z_streamp strm);
|
||||
int git_deflate(z_streamp strm, int flush);
|
||||
|
||||
#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
|
||||
#define DTYPE(de) ((de)->d_type)
|
||||
#else
|
||||
|
6
diff.c
6
diff.c
@ -1732,7 +1732,7 @@ static unsigned char *deflate_it(char *data,
|
||||
z_stream stream;
|
||||
|
||||
memset(&stream, 0, sizeof(stream));
|
||||
deflateInit(&stream, zlib_compression_level);
|
||||
git_deflate_init(&stream, zlib_compression_level);
|
||||
bound = deflateBound(&stream, size);
|
||||
deflated = xmalloc(bound);
|
||||
stream.next_out = deflated;
|
||||
@ -1740,9 +1740,9 @@ static unsigned char *deflate_it(char *data,
|
||||
|
||||
stream.next_in = (unsigned char *)data;
|
||||
stream.avail_in = size;
|
||||
while (deflate(&stream, Z_FINISH) == Z_OK)
|
||||
while (git_deflate(&stream, Z_FINISH) == Z_OK)
|
||||
; /* nothing */
|
||||
deflateEnd(&stream);
|
||||
git_deflate_end(&stream);
|
||||
*result_size = stream.total_out;
|
||||
return deflated;
|
||||
}
|
||||
|
@ -1050,7 +1050,7 @@ static int store_object(
|
||||
delta = NULL;
|
||||
|
||||
memset(&s, 0, sizeof(s));
|
||||
deflateInit(&s, pack_compression_level);
|
||||
git_deflate_init(&s, pack_compression_level);
|
||||
if (delta) {
|
||||
s.next_in = delta;
|
||||
s.avail_in = deltalen;
|
||||
@ -1060,9 +1060,9 @@ static int store_object(
|
||||
}
|
||||
s.avail_out = deflateBound(&s, s.avail_in);
|
||||
s.next_out = out = xmalloc(s.avail_out);
|
||||
while (deflate(&s, Z_FINISH) == Z_OK)
|
||||
/* nothing */;
|
||||
deflateEnd(&s);
|
||||
while (git_deflate(&s, Z_FINISH) == Z_OK)
|
||||
; /* nothing */
|
||||
git_deflate_end(&s);
|
||||
|
||||
/* Determine if we should auto-checkpoint. */
|
||||
if ((max_packsize && (pack_size + 60 + s.total_out) > max_packsize)
|
||||
@ -1078,14 +1078,14 @@ static int store_object(
|
||||
delta = NULL;
|
||||
|
||||
memset(&s, 0, sizeof(s));
|
||||
deflateInit(&s, pack_compression_level);
|
||||
git_deflate_init(&s, pack_compression_level);
|
||||
s.next_in = (void *)dat->buf;
|
||||
s.avail_in = dat->len;
|
||||
s.avail_out = deflateBound(&s, s.avail_in);
|
||||
s.next_out = out = xrealloc(out, s.avail_out);
|
||||
while (deflate(&s, Z_FINISH) == Z_OK)
|
||||
/* nothing */;
|
||||
deflateEnd(&s);
|
||||
while (git_deflate(&s, Z_FINISH) == Z_OK)
|
||||
; /* nothing */
|
||||
git_deflate_end(&s);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1187,7 +1187,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
|
||||
crc32_begin(pack_file);
|
||||
|
||||
memset(&s, 0, sizeof(s));
|
||||
deflateInit(&s, pack_compression_level);
|
||||
git_deflate_init(&s, pack_compression_level);
|
||||
|
||||
hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
|
||||
if (out_sz <= hdrlen)
|
||||
@ -1209,7 +1209,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
|
||||
len -= n;
|
||||
}
|
||||
|
||||
status = deflate(&s, len ? 0 : Z_FINISH);
|
||||
status = git_deflate(&s, len ? 0 : Z_FINISH);
|
||||
|
||||
if (!s.avail_out || status == Z_STREAM_END) {
|
||||
size_t n = s.next_out - out_buf;
|
||||
@ -1228,7 +1228,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
|
||||
die("unexpected deflate failure: %d", status);
|
||||
}
|
||||
}
|
||||
deflateEnd(&s);
|
||||
git_deflate_end(&s);
|
||||
git_SHA1_Final(sha1, &c);
|
||||
|
||||
if (sha1out)
|
||||
|
12
http-push.c
12
http-push.c
@ -359,7 +359,7 @@ static void start_put(struct transfer_request *request)
|
||||
|
||||
/* Set it up */
|
||||
memset(&stream, 0, sizeof(stream));
|
||||
deflateInit(&stream, zlib_compression_level);
|
||||
git_deflate_init(&stream, zlib_compression_level);
|
||||
size = deflateBound(&stream, len + hdrlen);
|
||||
strbuf_init(&request->buffer.buf, size);
|
||||
request->buffer.posn = 0;
|
||||
@ -371,15 +371,15 @@ static void start_put(struct transfer_request *request)
|
||||
/* First header.. */
|
||||
stream.next_in = (void *)hdr;
|
||||
stream.avail_in = hdrlen;
|
||||
while (deflate(&stream, 0) == Z_OK)
|
||||
/* nothing */;
|
||||
while (git_deflate(&stream, 0) == Z_OK)
|
||||
; /* nothing */
|
||||
|
||||
/* Then the data itself.. */
|
||||
stream.next_in = unpacked;
|
||||
stream.avail_in = len;
|
||||
while (deflate(&stream, Z_FINISH) == Z_OK)
|
||||
/* nothing */;
|
||||
deflateEnd(&stream);
|
||||
while (git_deflate(&stream, Z_FINISH) == Z_OK)
|
||||
; /* nothing */
|
||||
git_deflate_end(&stream);
|
||||
free(unpacked);
|
||||
|
||||
request->buffer.buf.len = stream.total_out;
|
||||
|
@ -475,11 +475,7 @@ static int post_rpc(struct rpc_state *rpc)
|
||||
int ret;
|
||||
|
||||
memset(&stream, 0, sizeof(stream));
|
||||
ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
|
||||
Z_DEFLATED, (15 + 16),
|
||||
8, Z_DEFAULT_STRATEGY);
|
||||
if (ret != Z_OK)
|
||||
die("cannot deflate request; zlib init error %d", ret);
|
||||
git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
|
||||
size = deflateBound(&stream, rpc->len);
|
||||
gzip_body = xmalloc(size);
|
||||
|
||||
@ -488,11 +484,11 @@ static int post_rpc(struct rpc_state *rpc)
|
||||
stream.next_out = (unsigned char *)gzip_body;
|
||||
stream.avail_out = size;
|
||||
|
||||
ret = deflate(&stream, Z_FINISH);
|
||||
ret = git_deflate(&stream, Z_FINISH);
|
||||
if (ret != Z_STREAM_END)
|
||||
die("cannot deflate request; zlib deflate error %d", ret);
|
||||
|
||||
ret = deflateEnd(&stream);
|
||||
ret = git_deflate_end_gently(&stream);
|
||||
if (ret != Z_OK)
|
||||
die("cannot deflate request; zlib end error %d", ret);
|
||||
|
||||
|
10
sha1_file.c
10
sha1_file.c
@ -2442,7 +2442,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
||||
|
||||
/* Set it up */
|
||||
memset(&stream, 0, sizeof(stream));
|
||||
deflateInit(&stream, zlib_compression_level);
|
||||
git_deflate_init(&stream, zlib_compression_level);
|
||||
stream.next_out = compressed;
|
||||
stream.avail_out = sizeof(compressed);
|
||||
git_SHA1_Init(&c);
|
||||
@ -2450,8 +2450,8 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
||||
/* First header.. */
|
||||
stream.next_in = (unsigned char *)hdr;
|
||||
stream.avail_in = hdrlen;
|
||||
while (deflate(&stream, 0) == Z_OK)
|
||||
/* nothing */;
|
||||
while (git_deflate(&stream, 0) == Z_OK)
|
||||
; /* nothing */
|
||||
git_SHA1_Update(&c, hdr, hdrlen);
|
||||
|
||||
/* Then the data itself.. */
|
||||
@ -2459,7 +2459,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
||||
stream.avail_in = len;
|
||||
do {
|
||||
unsigned char *in0 = stream.next_in;
|
||||
ret = deflate(&stream, Z_FINISH);
|
||||
ret = git_deflate(&stream, Z_FINISH);
|
||||
git_SHA1_Update(&c, in0, stream.next_in - in0);
|
||||
if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
|
||||
die("unable to write sha1 file");
|
||||
@ -2469,7 +2469,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
|
||||
|
||||
if (ret != Z_STREAM_END)
|
||||
die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret);
|
||||
ret = deflateEnd(&stream);
|
||||
ret = git_deflate_end_gently(&stream);
|
||||
if (ret != Z_OK)
|
||||
die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
|
||||
git_SHA1_Final(parano_sha1, &c);
|
||||
|
62
zlib.c
62
zlib.c
@ -77,3 +77,65 @@ int git_inflate(z_streamp strm, int flush)
|
||||
strm->msg ? strm->msg : "no message");
|
||||
return status;
|
||||
}
|
||||
|
||||
void git_deflate_init(z_streamp strm, int level)
|
||||
{
|
||||
int status = deflateInit(strm, level);
|
||||
|
||||
if (status == Z_OK)
|
||||
return;
|
||||
die("deflateInit: %s (%s)", zerr_to_string(status),
|
||||
strm->msg ? strm->msg : "no message");
|
||||
}
|
||||
|
||||
void git_deflate_init_gzip(z_streamp strm, int level)
|
||||
{
|
||||
/*
|
||||
* Use default 15 bits, +16 is to generate gzip header/trailer
|
||||
* instead of the zlib wrapper.
|
||||
*/
|
||||
const int windowBits = 15 + 16;
|
||||
int status = deflateInit2(strm, level,
|
||||
Z_DEFLATED, windowBits,
|
||||
8, Z_DEFAULT_STRATEGY);
|
||||
if (status == Z_OK)
|
||||
return;
|
||||
die("deflateInit2: %s (%s)", zerr_to_string(status),
|
||||
strm->msg ? strm->msg : "no message");
|
||||
}
|
||||
|
||||
void git_deflate_end(z_streamp strm)
|
||||
{
|
||||
int status = deflateEnd(strm);
|
||||
|
||||
if (status == Z_OK)
|
||||
return;
|
||||
error("deflateEnd: %s (%s)", zerr_to_string(status),
|
||||
strm->msg ? strm->msg : "no message");
|
||||
}
|
||||
|
||||
int git_deflate_end_gently(z_streamp strm)
|
||||
{
|
||||
return deflateEnd(strm);
|
||||
}
|
||||
|
||||
int git_deflate(z_streamp strm, int flush)
|
||||
{
|
||||
int status = deflate(strm, flush);
|
||||
|
||||
switch (status) {
|
||||
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
|
||||
case Z_BUF_ERROR:
|
||||
case Z_OK:
|
||||
case Z_STREAM_END:
|
||||
return status;
|
||||
|
||||
case Z_MEM_ERROR:
|
||||
die("deflate: out of memory");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
error("deflate: %s (%s)", zerr_to_string(status),
|
||||
strm->msg ? strm->msg : "no message");
|
||||
return status;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user