summaryrefslogtreecommitdiff
path: root/src/pack.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2012-11-19 22:22:33 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2012-11-30 15:55:23 +0100
commit46635339e935ce59e5a3525e18228f259d141aa3 (patch)
tree663c88e144983eb84f2a5ea4b875060d4767a4ce /src/pack.c
parentda820437368aae2088e992e7ce8944253693aa16 (diff)
downloadlibgit2-46635339e935ce59e5a3525e18228f259d141aa3.tar.gz
pack: introduce a streaming API for raw objects
This allows us to take objects from the packfile as a stream instead of having to keep it all in memory.
Diffstat (limited to 'src/pack.c')
-rw-r--r--src/pack.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/pack.c b/src/pack.c
index 6cb46d37b..01531d631 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -391,6 +391,72 @@ static void use_git_free(void *opaq, void *ptr)
git__free(ptr);
}
+int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos)
+{
+ int st;
+
+ memset(obj, 0, sizeof(git_packfile_stream));
+ obj->curpos = curpos;
+ obj->p = p;
+ obj->zstream.zalloc = use_git_alloc;
+ obj->zstream.zfree = use_git_free;
+ obj->zstream.next_in = Z_NULL;
+ obj->zstream.next_out = Z_NULL;
+ st = inflateInit(&obj->zstream);
+ if (st != Z_OK) {
+ git__free(obj);
+ giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
+ return -1;
+ }
+
+ return 0;
+}
+
+ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len)
+{
+ unsigned char *in;
+ size_t written;
+ int st;
+
+ if (obj->done)
+ return 0;
+
+ in = pack_window_open(obj->p, &obj->mw, obj->curpos, &obj->zstream.avail_in);
+ if (in == NULL)
+ return GIT_EBUFS;
+
+ obj->zstream.next_out = buffer;
+ obj->zstream.avail_out = len;
+ obj->zstream.next_in = in;
+
+ st = inflate(&obj->zstream, Z_SYNC_FLUSH);
+ git_mwindow_close(&obj->mw);
+
+ obj->curpos += obj->zstream.next_in - in;
+ written = len - obj->zstream.avail_out;
+
+ if (st != Z_OK && st != Z_STREAM_END) {
+ giterr_set(GITERR_ZLIB, "Failed to inflate packfile");
+ return -1;
+ }
+
+ if (st == Z_STREAM_END)
+ obj->done = 1;
+
+
+ /* If we didn't write anything out but we're not done, we need more data */
+ if (!written && st != Z_STREAM_END)
+ return GIT_EBUFS;
+
+ return written;
+
+}
+
+void git_packfile_stream_free(git_packfile_stream *obj)
+{
+ inflateEnd(&obj->zstream);
+}
+
int packfile_unpack_compressed(
git_rawobj *obj,
struct git_pack_file *p,