2 * Copyright (c) 2011, Google Inc.
14 typedef int (*open_istream_fn)(struct git_istream *,
16 const unsigned char *,
18 typedef int (*close_istream_fn)(struct git_istream *);
19 typedef ssize_t (*read_istream_fn)(struct git_istream *, char *, size_t);
22 close_istream_fn close;
26 #define open_method_decl(name) \
27 int open_istream_ ##name \
28 (struct git_istream *st, struct object_info *oi, \
29 const unsigned char *sha1, \
30 enum object_type *type)
32 #define close_method_decl(name) \
33 int close_istream_ ##name \
34 (struct git_istream *st)
36 #define read_method_decl(name) \
37 ssize_t read_istream_ ##name \
38 (struct git_istream *st, char *buf, size_t sz)
40 /* forward declaration */
41 static open_method_decl(incore);
42 static open_method_decl(loose);
43 static open_method_decl(pack_non_delta);
44 static struct git_istream *attach_stream_filter(struct git_istream *st,
45 struct stream_filter *filter);
48 static open_istream_fn open_istream_tbl[] = {
51 open_istream_pack_non_delta,
54 #define FILTER_BUFFER (1024*16)
56 struct filtered_istream {
57 struct git_istream *upstream;
58 struct stream_filter *filter;
59 char ibuf[FILTER_BUFFER];
60 char obuf[FILTER_BUFFER];
66 const struct stream_vtbl *vtbl;
67 unsigned long size; /* inflated size of full object */
69 enum { z_unused, z_used, z_done, z_error } z_state;
73 char *buf; /* from read_object() */
74 unsigned long read_ptr;
79 unsigned long mapsize;
86 struct packed_git *pack;
90 struct filtered_istream filtered;
94 int close_istream(struct git_istream *st)
96 return st->vtbl->close(st);
99 ssize_t read_istream(struct git_istream *st, char *buf, size_t sz)
101 return st->vtbl->read(st, buf, sz);
104 static enum input_source istream_source(const unsigned char *sha1,
105 enum object_type *type,
106 struct object_info *oi)
112 status = sha1_object_info_extended(sha1, oi);
117 switch (oi->whence) {
121 if (!oi->u.packed.is_delta && big_file_threshold <= size)
122 return pack_non_delta;
129 struct git_istream *open_istream(const unsigned char *sha1,
130 enum object_type *type,
132 struct stream_filter *filter)
134 struct git_istream *st;
135 struct object_info oi;
136 const unsigned char *real = lookup_replace_object(sha1);
137 enum input_source src = istream_source(real, type, &oi);
142 st = xmalloc(sizeof(*st));
143 if (open_istream_tbl[src](st, &oi, real, type)) {
144 if (open_istream_incore(st, &oi, real, type)) {
150 /* Add "&& !is_null_stream_filter(filter)" for performance */
151 struct git_istream *nst = attach_stream_filter(st, filter);
162 /*****************************************************************
166 *****************************************************************/
168 static void close_deflated_stream(struct git_istream *st)
170 if (st->z_state == z_used)
171 git_inflate_end(&st->z);
175 /*****************************************************************
179 *****************************************************************/
181 static close_method_decl(filtered)
183 free_stream_filter(st->u.filtered.filter);
184 return close_istream(st->u.filtered.upstream);
187 static read_method_decl(filtered)
189 struct filtered_istream *fs = &(st->u.filtered);
193 /* do we already have filtered output? */
194 if (fs->o_ptr < fs->o_end) {
195 size_t to_move = fs->o_end - fs->o_ptr;
198 memcpy(buf + filled, fs->obuf + fs->o_ptr, to_move);
199 fs->o_ptr += to_move;
204 fs->o_end = fs->o_ptr = 0;
206 /* do we have anything to feed the filter with? */
207 if (fs->i_ptr < fs->i_end) {
208 size_t to_feed = fs->i_end - fs->i_ptr;
209 size_t to_receive = FILTER_BUFFER;
210 if (stream_filter(fs->filter,
211 fs->ibuf + fs->i_ptr, &to_feed,
212 fs->obuf, &to_receive))
214 fs->i_ptr = fs->i_end - to_feed;
215 fs->o_end = FILTER_BUFFER - to_receive;
218 fs->i_end = fs->i_ptr = 0;
220 /* refill the input from the upstream */
221 fs->i_end = read_istream(fs->upstream, fs->ibuf, FILTER_BUFFER);
228 static struct stream_vtbl filtered_vtbl = {
229 close_istream_filtered,
230 read_istream_filtered,
233 static struct git_istream *attach_stream_filter(struct git_istream *st,
234 struct stream_filter *filter)
236 struct git_istream *ifs = xmalloc(sizeof(*ifs));
237 struct filtered_istream *fs = &(ifs->u.filtered);
239 ifs->vtbl = &filtered_vtbl;
242 fs->i_end = fs->i_ptr = 0;
243 fs->o_end = fs->o_ptr = 0;
244 ifs->size = -1; /* unknown */
248 /*****************************************************************
250 * Loose object stream
252 *****************************************************************/
254 static read_method_decl(loose)
256 size_t total_read = 0;
258 switch (st->z_state) {
267 if (st->u.loose.hdr_used < st->u.loose.hdr_avail) {
268 size_t to_copy = st->u.loose.hdr_avail - st->u.loose.hdr_used;
271 memcpy(buf, st->u.loose.hdr + st->u.loose.hdr_used, to_copy);
272 st->u.loose.hdr_used += to_copy;
273 total_read += to_copy;
276 while (total_read < sz) {
279 st->z.next_out = (unsigned char *)buf + total_read;
280 st->z.avail_out = sz - total_read;
281 status = git_inflate(&st->z, Z_FINISH);
283 total_read = st->z.next_out - (unsigned char *)buf;
285 if (status == Z_STREAM_END) {
286 git_inflate_end(&st->z);
287 st->z_state = z_done;
290 if (status != Z_OK && status != Z_BUF_ERROR) {
291 git_inflate_end(&st->z);
292 st->z_state = z_error;
299 static close_method_decl(loose)
301 close_deflated_stream(st);
302 munmap(st->u.loose.mapped, st->u.loose.mapsize);
306 static struct stream_vtbl loose_vtbl = {
311 static open_method_decl(loose)
313 st->u.loose.mapped = map_sha1_file(sha1, &st->u.loose.mapsize);
314 if (!st->u.loose.mapped)
316 if (unpack_sha1_header(&st->z,
320 sizeof(st->u.loose.hdr)) < 0) {
321 git_inflate_end(&st->z);
322 munmap(st->u.loose.mapped, st->u.loose.mapsize);
326 parse_sha1_header(st->u.loose.hdr, &st->size);
327 st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
328 st->u.loose.hdr_avail = st->z.total_out;
329 st->z_state = z_used;
331 st->vtbl = &loose_vtbl;
336 /*****************************************************************
338 * Non-delta packed object stream
340 *****************************************************************/
342 static read_method_decl(pack_non_delta)
344 size_t total_read = 0;
346 switch (st->z_state) {
348 memset(&st->z, 0, sizeof(st->z));
349 git_inflate_init(&st->z);
350 st->z_state = z_used;
360 while (total_read < sz) {
362 struct pack_window *window = NULL;
363 unsigned char *mapped;
365 mapped = use_pack(st->u.in_pack.pack, &window,
366 st->u.in_pack.pos, &st->z.avail_in);
368 st->z.next_out = (unsigned char *)buf + total_read;
369 st->z.avail_out = sz - total_read;
370 st->z.next_in = mapped;
371 status = git_inflate(&st->z, Z_FINISH);
373 st->u.in_pack.pos += st->z.next_in - mapped;
374 total_read = st->z.next_out - (unsigned char *)buf;
377 if (status == Z_STREAM_END) {
378 git_inflate_end(&st->z);
379 st->z_state = z_done;
382 if (status != Z_OK && status != Z_BUF_ERROR) {
383 git_inflate_end(&st->z);
384 st->z_state = z_error;
391 static close_method_decl(pack_non_delta)
393 close_deflated_stream(st);
397 static struct stream_vtbl pack_non_delta_vtbl = {
398 close_istream_pack_non_delta,
399 read_istream_pack_non_delta,
402 static open_method_decl(pack_non_delta)
404 struct pack_window *window;
405 enum object_type in_pack_type;
407 st->u.in_pack.pack = oi->u.packed.pack;
408 st->u.in_pack.pos = oi->u.packed.offset;
411 in_pack_type = unpack_object_header(st->u.in_pack.pack,
416 switch (in_pack_type) {
418 return -1; /* we do not do deltas for now */
425 st->z_state = z_unused;
426 st->vtbl = &pack_non_delta_vtbl;
431 /*****************************************************************
435 *****************************************************************/
437 static close_method_decl(incore)
439 free(st->u.incore.buf);
443 static read_method_decl(incore)
445 size_t read_size = sz;
446 size_t remainder = st->size - st->u.incore.read_ptr;
448 if (remainder <= read_size)
449 read_size = remainder;
451 memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
452 st->u.incore.read_ptr += read_size;
457 static struct stream_vtbl incore_vtbl = {
458 close_istream_incore,
462 static open_method_decl(incore)
464 st->u.incore.buf = read_sha1_file_extended(sha1, type, &st->size, 0);
465 st->u.incore.read_ptr = 0;
466 st->vtbl = &incore_vtbl;
468 return st->u.incore.buf ? 0 : -1;