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
];
67 const struct stream_vtbl
*vtbl
;
68 unsigned long size
; /* inflated size of full object */
70 enum { z_unused
, z_used
, z_done
, z_error
} z_state
;
74 char *buf
; /* from read_object() */
75 unsigned long read_ptr
;
80 unsigned long mapsize
;
87 struct packed_git
*pack
;
91 struct filtered_istream filtered
;
95 int close_istream(struct git_istream
*st
)
97 return st
->vtbl
->close(st
);
100 ssize_t
read_istream(struct git_istream
*st
, char *buf
, size_t sz
)
102 return st
->vtbl
->read(st
, buf
, sz
);
105 static enum input_source
istream_source(const unsigned char *sha1
,
106 enum object_type
*type
,
107 struct object_info
*oi
)
113 status
= sha1_object_info_extended(sha1
, oi
);
118 switch (oi
->whence
) {
122 if (!oi
->u
.packed
.is_delta
&& big_file_threshold
<= size
)
123 return pack_non_delta
;
130 struct git_istream
*open_istream(const unsigned char *sha1
,
131 enum object_type
*type
,
133 struct stream_filter
*filter
)
135 struct git_istream
*st
;
136 struct object_info oi
;
137 const unsigned char *real
= lookup_replace_object(sha1
);
138 enum input_source src
= istream_source(real
, type
, &oi
);
143 st
= xmalloc(sizeof(*st
));
144 if (open_istream_tbl
[src
](st
, &oi
, real
, type
)) {
145 if (open_istream_incore(st
, &oi
, real
, type
)) {
151 /* Add "&& !is_null_stream_filter(filter)" for performance */
152 struct git_istream
*nst
= attach_stream_filter(st
, filter
);
163 /*****************************************************************
167 *****************************************************************/
169 static void close_deflated_stream(struct git_istream
*st
)
171 if (st
->z_state
== z_used
)
172 git_inflate_end(&st
->z
);
176 /*****************************************************************
180 *****************************************************************/
182 static close_method_decl(filtered
)
184 free_stream_filter(st
->u
.filtered
.filter
);
185 return close_istream(st
->u
.filtered
.upstream
);
188 static read_method_decl(filtered
)
190 struct filtered_istream
*fs
= &(st
->u
.filtered
);
194 /* do we already have filtered output? */
195 if (fs
->o_ptr
< fs
->o_end
) {
196 size_t to_move
= fs
->o_end
- fs
->o_ptr
;
199 memcpy(buf
+ filled
, fs
->obuf
+ fs
->o_ptr
, to_move
);
200 fs
->o_ptr
+= to_move
;
205 fs
->o_end
= fs
->o_ptr
= 0;
207 /* do we have anything to feed the filter with? */
208 if (fs
->i_ptr
< fs
->i_end
) {
209 size_t to_feed
= fs
->i_end
- fs
->i_ptr
;
210 size_t to_receive
= FILTER_BUFFER
;
211 if (stream_filter(fs
->filter
,
212 fs
->ibuf
+ fs
->i_ptr
, &to_feed
,
213 fs
->obuf
, &to_receive
))
215 fs
->i_ptr
= fs
->i_end
- to_feed
;
216 fs
->o_end
= FILTER_BUFFER
- to_receive
;
220 /* tell the filter to drain upon no more input */
221 if (fs
->input_finished
) {
222 size_t to_receive
= FILTER_BUFFER
;
223 if (stream_filter(fs
->filter
,
225 fs
->obuf
, &to_receive
))
227 fs
->o_end
= FILTER_BUFFER
- to_receive
;
232 fs
->i_end
= fs
->i_ptr
= 0;
234 /* refill the input from the upstream */
235 if (!fs
->input_finished
) {
236 fs
->i_end
= read_istream(fs
->upstream
, fs
->ibuf
, FILTER_BUFFER
);
242 fs
->input_finished
= 1;
247 static struct stream_vtbl filtered_vtbl
= {
248 close_istream_filtered
,
249 read_istream_filtered
,
252 static struct git_istream
*attach_stream_filter(struct git_istream
*st
,
253 struct stream_filter
*filter
)
255 struct git_istream
*ifs
= xmalloc(sizeof(*ifs
));
256 struct filtered_istream
*fs
= &(ifs
->u
.filtered
);
258 ifs
->vtbl
= &filtered_vtbl
;
261 fs
->i_end
= fs
->i_ptr
= 0;
262 fs
->o_end
= fs
->o_ptr
= 0;
263 fs
->input_finished
= 0;
264 ifs
->size
= -1; /* unknown */
268 /*****************************************************************
270 * Loose object stream
272 *****************************************************************/
274 static read_method_decl(loose
)
276 size_t total_read
= 0;
278 switch (st
->z_state
) {
287 if (st
->u
.loose
.hdr_used
< st
->u
.loose
.hdr_avail
) {
288 size_t to_copy
= st
->u
.loose
.hdr_avail
- st
->u
.loose
.hdr_used
;
291 memcpy(buf
, st
->u
.loose
.hdr
+ st
->u
.loose
.hdr_used
, to_copy
);
292 st
->u
.loose
.hdr_used
+= to_copy
;
293 total_read
+= to_copy
;
296 while (total_read
< sz
) {
299 st
->z
.next_out
= (unsigned char *)buf
+ total_read
;
300 st
->z
.avail_out
= sz
- total_read
;
301 status
= git_inflate(&st
->z
, Z_FINISH
);
303 total_read
= st
->z
.next_out
- (unsigned char *)buf
;
305 if (status
== Z_STREAM_END
) {
306 git_inflate_end(&st
->z
);
307 st
->z_state
= z_done
;
310 if (status
!= Z_OK
&& status
!= Z_BUF_ERROR
) {
311 git_inflate_end(&st
->z
);
312 st
->z_state
= z_error
;
319 static close_method_decl(loose
)
321 close_deflated_stream(st
);
322 munmap(st
->u
.loose
.mapped
, st
->u
.loose
.mapsize
);
326 static struct stream_vtbl loose_vtbl
= {
331 static open_method_decl(loose
)
333 st
->u
.loose
.mapped
= map_sha1_file(sha1
, &st
->u
.loose
.mapsize
);
334 if (!st
->u
.loose
.mapped
)
336 if (unpack_sha1_header(&st
->z
,
340 sizeof(st
->u
.loose
.hdr
)) < 0) {
341 git_inflate_end(&st
->z
);
342 munmap(st
->u
.loose
.mapped
, st
->u
.loose
.mapsize
);
346 parse_sha1_header(st
->u
.loose
.hdr
, &st
->size
);
347 st
->u
.loose
.hdr_used
= strlen(st
->u
.loose
.hdr
) + 1;
348 st
->u
.loose
.hdr_avail
= st
->z
.total_out
;
349 st
->z_state
= z_used
;
351 st
->vtbl
= &loose_vtbl
;
356 /*****************************************************************
358 * Non-delta packed object stream
360 *****************************************************************/
362 static read_method_decl(pack_non_delta
)
364 size_t total_read
= 0;
366 switch (st
->z_state
) {
368 memset(&st
->z
, 0, sizeof(st
->z
));
369 git_inflate_init(&st
->z
);
370 st
->z_state
= z_used
;
380 while (total_read
< sz
) {
382 struct pack_window
*window
= NULL
;
383 unsigned char *mapped
;
385 mapped
= use_pack(st
->u
.in_pack
.pack
, &window
,
386 st
->u
.in_pack
.pos
, &st
->z
.avail_in
);
388 st
->z
.next_out
= (unsigned char *)buf
+ total_read
;
389 st
->z
.avail_out
= sz
- total_read
;
390 st
->z
.next_in
= mapped
;
391 status
= git_inflate(&st
->z
, Z_FINISH
);
393 st
->u
.in_pack
.pos
+= st
->z
.next_in
- mapped
;
394 total_read
= st
->z
.next_out
- (unsigned char *)buf
;
397 if (status
== Z_STREAM_END
) {
398 git_inflate_end(&st
->z
);
399 st
->z_state
= z_done
;
402 if (status
!= Z_OK
&& status
!= Z_BUF_ERROR
) {
403 git_inflate_end(&st
->z
);
404 st
->z_state
= z_error
;
411 static close_method_decl(pack_non_delta
)
413 close_deflated_stream(st
);
417 static struct stream_vtbl pack_non_delta_vtbl
= {
418 close_istream_pack_non_delta
,
419 read_istream_pack_non_delta
,
422 static open_method_decl(pack_non_delta
)
424 struct pack_window
*window
;
425 enum object_type in_pack_type
;
427 st
->u
.in_pack
.pack
= oi
->u
.packed
.pack
;
428 st
->u
.in_pack
.pos
= oi
->u
.packed
.offset
;
431 in_pack_type
= unpack_object_header(st
->u
.in_pack
.pack
,
436 switch (in_pack_type
) {
438 return -1; /* we do not do deltas for now */
445 st
->z_state
= z_unused
;
446 st
->vtbl
= &pack_non_delta_vtbl
;
451 /*****************************************************************
455 *****************************************************************/
457 static close_method_decl(incore
)
459 free(st
->u
.incore
.buf
);
463 static read_method_decl(incore
)
465 size_t read_size
= sz
;
466 size_t remainder
= st
->size
- st
->u
.incore
.read_ptr
;
468 if (remainder
<= read_size
)
469 read_size
= remainder
;
471 memcpy(buf
, st
->u
.incore
.buf
+ st
->u
.incore
.read_ptr
, read_size
);
472 st
->u
.incore
.read_ptr
+= read_size
;
477 static struct stream_vtbl incore_vtbl
= {
478 close_istream_incore
,
482 static open_method_decl(incore
)
484 st
->u
.incore
.buf
= read_sha1_file_extended(sha1
, type
, &st
->size
, 0);
485 st
->u
.incore
.read_ptr
= 0;
486 st
->vtbl
= &incore_vtbl
;
488 return st
->u
.incore
.buf
? 0 : -1;