mirror of
https://github.com/git-for-windows/git.git
synced 2026-06-28 06:35:27 -05:00
The Git project is not exactly the easiest project to get started in: it's written in C and POSIX shell, with bits of Perl, Rust and other languages sprinkled into it. On top of that, the project has grown somewhat organically over time, making the codebase hard to navigate. These are problems that we're aware of, and there have been and still are efforts to clean up some of the technical debt that is natural to exist an a project that is more than 20 years old. Furthermore, we provide resources to newcomers that help them out like our coding guidelines, code of conduct or "MyFirstContribution.adoc". But there is a rather practical problem: finding your way around in our project's tree is not easy. Doing a directory listing in the top-level directory will present you with more than 550 files, which makes it extremely hard for a newcomer to figure out what files they are even supposed to look at. This makes the onboarding experience somewhat harder than it really needs to be. This isn't only a problem for newcomers though, as I myself struggle to find the files I am looking for because of the sheer number of files. Besides the problem of discoverability it also creates a problem of structure. It is not obvious at all which files are part of "libgit.a" and which files are only linked into our final executables. So while we have this split in our build systems, that split is not evident at all in our tree. Introduce a new "lib/" directory and move all of our sources for "libgit.a" into it to fix these issues. It makes the split we have evident and reduces the number of files in our top-level tree from 550 files to ~80 files. This is still a lot of files, but it's significantly easier to navigate already. Furthermore, we can further iterate after this step and think about introducing a better structure for remaining files, as well. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
291 lines
6.9 KiB
C
291 lines
6.9 KiB
C
/*
|
|
* zlib wrappers to make sure we don't silently miss errors
|
|
* at init time.
|
|
*/
|
|
#include "git-compat-util.h"
|
|
#include "git-zlib.h"
|
|
|
|
static const char *zerr_to_string(int status)
|
|
{
|
|
switch (status) {
|
|
case Z_MEM_ERROR:
|
|
return "out of memory";
|
|
case Z_VERSION_ERROR:
|
|
return "wrong version";
|
|
case Z_NEED_DICT:
|
|
return "needs dictionary";
|
|
case Z_DATA_ERROR:
|
|
return "data stream error";
|
|
case Z_STREAM_ERROR:
|
|
return "stream consistency error";
|
|
default:
|
|
return "unknown error";
|
|
}
|
|
}
|
|
|
|
/*
|
|
* avail_in and avail_out in zlib are counted in uInt, which typically
|
|
* limits the size of the buffer we can use to 4GB when interacting
|
|
* with zlib in a single call to inflate/deflate.
|
|
*/
|
|
/* #define ZLIB_BUF_MAX ((uInt)-1) */
|
|
#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
|
|
|
|
/* uLong is 32-bit on Windows, even on 64-bit systems */
|
|
#define ULONG_MAX_VALUE maximum_unsigned_value_of_type(uLong)
|
|
static inline uInt zlib_buf_cap(unsigned long len)
|
|
{
|
|
return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
|
|
}
|
|
|
|
static inline uLong zlib_uLong_cap(size_t s)
|
|
{
|
|
return s < ULONG_MAX_VALUE ? (uLong)s : ULONG_MAX_VALUE;
|
|
}
|
|
|
|
static void zlib_pre_call(git_zstream *s)
|
|
{
|
|
s->z.next_in = s->next_in;
|
|
s->z.next_out = s->next_out;
|
|
s->z.total_in = zlib_uLong_cap(s->total_in);
|
|
s->z.total_out = zlib_uLong_cap(s->total_out);
|
|
s->z.avail_in = zlib_buf_cap(s->avail_in);
|
|
s->z.avail_out = zlib_buf_cap(s->avail_out);
|
|
}
|
|
|
|
static void zlib_post_call(git_zstream *s, int status)
|
|
{
|
|
size_t bytes_consumed;
|
|
size_t bytes_produced;
|
|
|
|
bytes_consumed = s->z.next_in - s->next_in;
|
|
bytes_produced = s->z.next_out - s->next_out;
|
|
/*
|
|
* zlib's total_out/total_in are uLong which may wrap for >4GB.
|
|
* We track our own totals and verify only the low bits match.
|
|
*/
|
|
if ((s->z.total_out & ULONG_MAX_VALUE) !=
|
|
((zlib_uLong_cap(s->total_out) + bytes_produced) & ULONG_MAX_VALUE))
|
|
BUG("total_out mismatch");
|
|
/*
|
|
* zlib does not update total_in when it returns Z_NEED_DICT,
|
|
* causing a mismatch here. Skip the sanity check in that case.
|
|
*/
|
|
if (status != Z_NEED_DICT &&
|
|
(s->z.total_in & ULONG_MAX_VALUE) !=
|
|
((zlib_uLong_cap(s->total_in) + bytes_consumed) & ULONG_MAX_VALUE))
|
|
BUG("total_in mismatch");
|
|
|
|
s->total_out += bytes_produced;
|
|
s->total_in += bytes_consumed;
|
|
/* zlib-ng marks `next_in` as `const`, so we have to cast it away. */
|
|
s->next_in = (unsigned char *) s->z.next_in;
|
|
s->next_out = s->z.next_out;
|
|
s->avail_in -= bytes_consumed;
|
|
s->avail_out -= bytes_produced;
|
|
}
|
|
|
|
void git_inflate_init(git_zstream *strm)
|
|
{
|
|
int status;
|
|
|
|
zlib_pre_call(strm);
|
|
status = inflateInit(&strm->z);
|
|
zlib_post_call(strm, status);
|
|
if (status == Z_OK)
|
|
return;
|
|
die("inflateInit: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
}
|
|
|
|
void git_inflate_init_gzip_only(git_zstream *strm)
|
|
{
|
|
/*
|
|
* Use default 15 bits, +16 is to accept only gzip and to
|
|
* yield Z_DATA_ERROR when fed zlib format.
|
|
*/
|
|
const int windowBits = 15 + 16;
|
|
int status;
|
|
|
|
zlib_pre_call(strm);
|
|
status = inflateInit2(&strm->z, windowBits);
|
|
zlib_post_call(strm, status);
|
|
if (status == Z_OK)
|
|
return;
|
|
die("inflateInit2: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
}
|
|
|
|
void git_inflate_end(git_zstream *strm)
|
|
{
|
|
int status;
|
|
|
|
zlib_pre_call(strm);
|
|
status = inflateEnd(&strm->z);
|
|
zlib_post_call(strm, status);
|
|
if (status == Z_OK)
|
|
return;
|
|
error("inflateEnd: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
}
|
|
|
|
int git_inflate(git_zstream *strm, int flush)
|
|
{
|
|
int status;
|
|
|
|
for (;;) {
|
|
zlib_pre_call(strm);
|
|
/* Never say Z_FINISH unless we are feeding everything */
|
|
status = inflate(&strm->z,
|
|
(strm->z.avail_in != strm->avail_in)
|
|
? 0 : flush);
|
|
if (status == Z_MEM_ERROR)
|
|
die("inflate: out of memory");
|
|
zlib_post_call(strm, status);
|
|
|
|
/*
|
|
* Let zlib work another round, while we can still
|
|
* make progress.
|
|
*/
|
|
if ((strm->avail_out && !strm->z.avail_out) &&
|
|
(status == Z_OK || status == Z_BUF_ERROR))
|
|
continue;
|
|
break;
|
|
}
|
|
|
|
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;
|
|
default:
|
|
break;
|
|
}
|
|
error("inflate: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
return status;
|
|
}
|
|
|
|
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
|
|
{
|
|
return deflateBound(&strm->z, size);
|
|
}
|
|
|
|
void git_deflate_init(git_zstream *strm, int level)
|
|
{
|
|
int status;
|
|
|
|
memset(strm, 0, sizeof(*strm));
|
|
zlib_pre_call(strm);
|
|
status = deflateInit(&strm->z, level);
|
|
zlib_post_call(strm, status);
|
|
if (status == Z_OK)
|
|
return;
|
|
die("deflateInit: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
}
|
|
|
|
static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
|
|
{
|
|
int status;
|
|
|
|
memset(strm, 0, sizeof(*strm));
|
|
zlib_pre_call(strm);
|
|
status = deflateInit2(&strm->z, level,
|
|
Z_DEFLATED, windowBits,
|
|
8, Z_DEFAULT_STRATEGY);
|
|
zlib_post_call(strm, status);
|
|
if (status == Z_OK)
|
|
return;
|
|
die("deflateInit2: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
}
|
|
|
|
void git_deflate_init_gzip(git_zstream *strm, int level)
|
|
{
|
|
/*
|
|
* Use default 15 bits, +16 is to generate gzip header/trailer
|
|
* instead of the zlib wrapper.
|
|
*/
|
|
do_git_deflate_init(strm, level, 15 + 16);
|
|
}
|
|
|
|
void git_deflate_init_raw(git_zstream *strm, int level)
|
|
{
|
|
/*
|
|
* Use default 15 bits, negate the value to get raw compressed
|
|
* data without zlib header and trailer.
|
|
*/
|
|
do_git_deflate_init(strm, level, -15);
|
|
}
|
|
|
|
int git_deflate_abort(git_zstream *strm)
|
|
{
|
|
int status;
|
|
|
|
zlib_pre_call(strm);
|
|
status = deflateEnd(&strm->z);
|
|
zlib_post_call(strm, status);
|
|
return status;
|
|
}
|
|
|
|
void git_deflate_end(git_zstream *strm)
|
|
{
|
|
int status = git_deflate_abort(strm);
|
|
|
|
if (status == Z_OK)
|
|
return;
|
|
error("deflateEnd: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
}
|
|
|
|
int git_deflate_end_gently(git_zstream *strm)
|
|
{
|
|
int status;
|
|
|
|
zlib_pre_call(strm);
|
|
status = deflateEnd(&strm->z);
|
|
zlib_post_call(strm, status);
|
|
return status;
|
|
}
|
|
|
|
int git_deflate(git_zstream *strm, int flush)
|
|
{
|
|
int status;
|
|
|
|
for (;;) {
|
|
zlib_pre_call(strm);
|
|
|
|
/* Never say Z_FINISH unless we are feeding everything */
|
|
status = deflate(&strm->z,
|
|
(strm->z.avail_in != strm->avail_in)
|
|
? 0 : flush);
|
|
if (status == Z_MEM_ERROR)
|
|
die("deflate: out of memory");
|
|
zlib_post_call(strm, status);
|
|
|
|
/*
|
|
* Let zlib work another round, while we can still
|
|
* make progress.
|
|
*/
|
|
if ((strm->avail_out && !strm->z.avail_out) &&
|
|
(status == Z_OK || status == Z_BUF_ERROR))
|
|
continue;
|
|
break;
|
|
}
|
|
|
|
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;
|
|
default:
|
|
break;
|
|
}
|
|
error("deflate: %s (%s)", zerr_to_string(status),
|
|
strm->z.msg ? strm->z.msg : "no message");
|
|
return status;
|
|
}
|