2 * Copyright (c) 2011, Google Inc.
6 #include "repository.h"
7 #include "object-store.h"
17 typedef int (*open_istream_fn
)(struct git_istream
*,
19 const struct object_id
*,
21 typedef int (*close_istream_fn
)(struct git_istream
*);
22 typedef ssize_t (*read_istream_fn
)(struct git_istream
*, char *, size_t);
25 close_istream_fn close
;
29 #define open_method_decl(name) \
30 int open_istream_ ##name \
31 (struct git_istream *st, struct object_info *oi, \
32 const struct object_id *oid, \
33 enum object_type *type)
35 #define close_method_decl(name) \
36 int close_istream_ ##name \
37 (struct git_istream *st)
39 #define read_method_decl(name) \
40 ssize_t read_istream_ ##name \
41 (struct git_istream *st, char *buf, size_t sz)
43 /* forward declaration */
44 static open_method_decl(incore
);
45 static open_method_decl(loose
);
46 static open_method_decl(pack_non_delta
);
47 static struct git_istream
*attach_stream_filter(struct git_istream
*st
,
48 struct stream_filter
*filter
);
51 static open_istream_fn open_istream_tbl
[] = {
54 open_istream_pack_non_delta
,
57 #define FILTER_BUFFER (1024*16)
59 struct filtered_istream
{
60 struct git_istream
*upstream
;
61 struct stream_filter
*filter
;
62 char ibuf
[FILTER_BUFFER
];
63 char obuf
[FILTER_BUFFER
];
70 const struct stream_vtbl
*vtbl
;
71 unsigned long size
; /* inflated size of full object */
73 enum { z_unused
, z_used
, z_done
, z_error
} z_state
;
77 char *buf
; /* from read_object() */
78 unsigned long read_ptr
;
83 unsigned long mapsize
;
90 struct packed_git
*pack
;
94 struct filtered_istream filtered
;
98 int close_istream(struct git_istream
*st
)
100 int r
= st
->vtbl
->close(st
);
105 ssize_t
read_istream(struct git_istream
*st
, void *buf
, size_t sz
)
107 return st
->vtbl
->read(st
, buf
, sz
);
110 static enum input_source
istream_source(const struct object_id
*oid
,
111 enum object_type
*type
,
112 struct object_info
*oi
)
119 status
= oid_object_info_extended(oid
, oi
, 0);
123 switch (oi
->whence
) {
127 if (!oi
->u
.packed
.is_delta
&& big_file_threshold
< size
)
128 return pack_non_delta
;
135 struct git_istream
*open_istream(const struct object_id
*oid
,
136 enum object_type
*type
,
138 struct stream_filter
*filter
)
140 struct git_istream
*st
;
141 struct object_info oi
= OBJECT_INFO_INIT
;
142 const struct object_id
*real
= lookup_replace_object(oid
);
143 enum input_source src
= istream_source(real
, type
, &oi
);
148 st
= xmalloc(sizeof(*st
));
149 if (open_istream_tbl
[src
](st
, &oi
, real
, type
)) {
150 if (open_istream_incore(st
, &oi
, real
, type
)) {
156 /* Add "&& !is_null_stream_filter(filter)" for performance */
157 struct git_istream
*nst
= attach_stream_filter(st
, filter
);
170 /*****************************************************************
174 *****************************************************************/
176 static void close_deflated_stream(struct git_istream
*st
)
178 if (st
->z_state
== z_used
)
179 git_inflate_end(&st
->z
);
183 /*****************************************************************
187 *****************************************************************/
189 static close_method_decl(filtered
)
191 free_stream_filter(st
->u
.filtered
.filter
);
192 return close_istream(st
->u
.filtered
.upstream
);
195 static read_method_decl(filtered
)
197 struct filtered_istream
*fs
= &(st
->u
.filtered
);
201 /* do we already have filtered output? */
202 if (fs
->o_ptr
< fs
->o_end
) {
203 size_t to_move
= fs
->o_end
- fs
->o_ptr
;
206 memcpy(buf
+ filled
, fs
->obuf
+ fs
->o_ptr
, to_move
);
207 fs
->o_ptr
+= to_move
;
212 fs
->o_end
= fs
->o_ptr
= 0;
214 /* do we have anything to feed the filter with? */
215 if (fs
->i_ptr
< fs
->i_end
) {
216 size_t to_feed
= fs
->i_end
- fs
->i_ptr
;
217 size_t to_receive
= FILTER_BUFFER
;
218 if (stream_filter(fs
->filter
,
219 fs
->ibuf
+ fs
->i_ptr
, &to_feed
,
220 fs
->obuf
, &to_receive
))
222 fs
->i_ptr
= fs
->i_end
- to_feed
;
223 fs
->o_end
= FILTER_BUFFER
- to_receive
;
227 /* tell the filter to drain upon no more input */
228 if (fs
->input_finished
) {
229 size_t to_receive
= FILTER_BUFFER
;
230 if (stream_filter(fs
->filter
,
232 fs
->obuf
, &to_receive
))
234 fs
->o_end
= FILTER_BUFFER
- to_receive
;
239 fs
->i_end
= fs
->i_ptr
= 0;
241 /* refill the input from the upstream */
242 if (!fs
->input_finished
) {
243 fs
->i_end
= read_istream(fs
->upstream
, fs
->ibuf
, FILTER_BUFFER
);
249 fs
->input_finished
= 1;
254 static struct stream_vtbl filtered_vtbl
= {
255 close_istream_filtered
,
256 read_istream_filtered
,
259 static struct git_istream
*attach_stream_filter(struct git_istream
*st
,
260 struct stream_filter
*filter
)
262 struct git_istream
*ifs
= xmalloc(sizeof(*ifs
));
263 struct filtered_istream
*fs
= &(ifs
->u
.filtered
);
265 ifs
->vtbl
= &filtered_vtbl
;
268 fs
->i_end
= fs
->i_ptr
= 0;
269 fs
->o_end
= fs
->o_ptr
= 0;
270 fs
->input_finished
= 0;
271 ifs
->size
= -1; /* unknown */
275 /*****************************************************************
277 * Loose object stream
279 *****************************************************************/
281 static read_method_decl(loose
)
283 size_t total_read
= 0;
285 switch (st
->z_state
) {
294 if (st
->u
.loose
.hdr_used
< st
->u
.loose
.hdr_avail
) {
295 size_t to_copy
= st
->u
.loose
.hdr_avail
- st
->u
.loose
.hdr_used
;
298 memcpy(buf
, st
->u
.loose
.hdr
+ st
->u
.loose
.hdr_used
, to_copy
);
299 st
->u
.loose
.hdr_used
+= to_copy
;
300 total_read
+= to_copy
;
303 while (total_read
< sz
) {
306 st
->z
.next_out
= (unsigned char *)buf
+ total_read
;
307 st
->z
.avail_out
= sz
- total_read
;
308 status
= git_inflate(&st
->z
, Z_FINISH
);
310 total_read
= st
->z
.next_out
- (unsigned char *)buf
;
312 if (status
== Z_STREAM_END
) {
313 git_inflate_end(&st
->z
);
314 st
->z_state
= z_done
;
317 if (status
!= Z_OK
&& (status
!= Z_BUF_ERROR
|| total_read
< sz
)) {
318 git_inflate_end(&st
->z
);
319 st
->z_state
= z_error
;
326 static close_method_decl(loose
)
328 close_deflated_stream(st
);
329 munmap(st
->u
.loose
.mapped
, st
->u
.loose
.mapsize
);
333 static struct stream_vtbl loose_vtbl
= {
338 static open_method_decl(loose
)
340 st
->u
.loose
.mapped
= map_sha1_file(the_repository
,
341 oid
->hash
, &st
->u
.loose
.mapsize
);
342 if (!st
->u
.loose
.mapped
)
344 if ((unpack_sha1_header(&st
->z
,
348 sizeof(st
->u
.loose
.hdr
)) < 0) ||
349 (parse_sha1_header(st
->u
.loose
.hdr
, &st
->size
) < 0)) {
350 git_inflate_end(&st
->z
);
351 munmap(st
->u
.loose
.mapped
, st
->u
.loose
.mapsize
);
355 st
->u
.loose
.hdr_used
= strlen(st
->u
.loose
.hdr
) + 1;
356 st
->u
.loose
.hdr_avail
= st
->z
.total_out
;
357 st
->z_state
= z_used
;
359 st
->vtbl
= &loose_vtbl
;
364 /*****************************************************************
366 * Non-delta packed object stream
368 *****************************************************************/
370 static read_method_decl(pack_non_delta
)
372 size_t total_read
= 0;
374 switch (st
->z_state
) {
376 memset(&st
->z
, 0, sizeof(st
->z
));
377 git_inflate_init(&st
->z
);
378 st
->z_state
= z_used
;
388 while (total_read
< sz
) {
390 struct pack_window
*window
= NULL
;
391 unsigned char *mapped
;
393 mapped
= use_pack(st
->u
.in_pack
.pack
, &window
,
394 st
->u
.in_pack
.pos
, &st
->z
.avail_in
);
396 st
->z
.next_out
= (unsigned char *)buf
+ total_read
;
397 st
->z
.avail_out
= sz
- total_read
;
398 st
->z
.next_in
= mapped
;
399 status
= git_inflate(&st
->z
, Z_FINISH
);
401 st
->u
.in_pack
.pos
+= st
->z
.next_in
- mapped
;
402 total_read
= st
->z
.next_out
- (unsigned char *)buf
;
405 if (status
== Z_STREAM_END
) {
406 git_inflate_end(&st
->z
);
407 st
->z_state
= z_done
;
410 if (status
!= Z_OK
&& status
!= Z_BUF_ERROR
) {
411 git_inflate_end(&st
->z
);
412 st
->z_state
= z_error
;
419 static close_method_decl(pack_non_delta
)
421 close_deflated_stream(st
);
425 static struct stream_vtbl pack_non_delta_vtbl
= {
426 close_istream_pack_non_delta
,
427 read_istream_pack_non_delta
,
430 static open_method_decl(pack_non_delta
)
432 struct pack_window
*window
;
433 enum object_type in_pack_type
;
435 st
->u
.in_pack
.pack
= oi
->u
.packed
.pack
;
436 st
->u
.in_pack
.pos
= oi
->u
.packed
.offset
;
439 in_pack_type
= unpack_object_header(st
->u
.in_pack
.pack
,
444 switch (in_pack_type
) {
446 return -1; /* we do not do deltas for now */
453 st
->z_state
= z_unused
;
454 st
->vtbl
= &pack_non_delta_vtbl
;
459 /*****************************************************************
463 *****************************************************************/
465 static close_method_decl(incore
)
467 free(st
->u
.incore
.buf
);
471 static read_method_decl(incore
)
473 size_t read_size
= sz
;
474 size_t remainder
= st
->size
- st
->u
.incore
.read_ptr
;
476 if (remainder
<= read_size
)
477 read_size
= remainder
;
479 memcpy(buf
, st
->u
.incore
.buf
+ st
->u
.incore
.read_ptr
, read_size
);
480 st
->u
.incore
.read_ptr
+= read_size
;
485 static struct stream_vtbl incore_vtbl
= {
486 close_istream_incore
,
490 static open_method_decl(incore
)
492 st
->u
.incore
.buf
= read_object_file_extended(oid
, type
, &st
->size
, 0);
493 st
->u
.incore
.read_ptr
= 0;
494 st
->vtbl
= &incore_vtbl
;
496 return st
->u
.incore
.buf
? 0 : -1;
500 /****************************************************************
501 * Users of streaming interface
502 ****************************************************************/
504 int stream_blob_to_fd(int fd
, const struct object_id
*oid
, struct stream_filter
*filter
,
507 struct git_istream
*st
;
508 enum object_type type
;
513 st
= open_istream(oid
, &type
, &sz
, filter
);
516 free_stream_filter(filter
);
519 if (type
!= OBJ_BLOB
)
523 ssize_t wrote
, holeto
;
524 ssize_t readlen
= read_istream(st
, buf
, sizeof(buf
));
530 if (can_seek
&& sizeof(buf
) == readlen
) {
531 for (holeto
= 0; holeto
< readlen
; holeto
++)
534 if (readlen
== holeto
) {
540 if (kept
&& lseek(fd
, kept
, SEEK_CUR
) == (off_t
) -1)
544 wrote
= write_in_full(fd
, buf
, readlen
);
549 if (kept
&& (lseek(fd
, kept
- 1, SEEK_CUR
) == (off_t
) -1 ||
550 xwrite(fd
, "", 1) != 1))