2 * Copyright (c) 2011, Google Inc.
6 #include "repository.h"
7 #include "object-store.h"
8 #include "replace-object.h"
18 typedef int (*open_istream_fn)(struct git_istream *,
20 const struct object_id *,
22 typedef int (*close_istream_fn)(struct git_istream *);
23 typedef ssize_t (*read_istream_fn)(struct git_istream *, char *, size_t);
26 close_istream_fn close;
30 #define open_method_decl(name) \
31 int open_istream_ ##name \
32 (struct git_istream *st, struct object_info *oi, \
33 const struct object_id *oid, \
34 enum object_type *type)
36 #define close_method_decl(name) \
37 int close_istream_ ##name \
38 (struct git_istream *st)
40 #define read_method_decl(name) \
41 ssize_t read_istream_ ##name \
42 (struct git_istream *st, char *buf, size_t sz)
44 /* forward declaration */
45 static open_method_decl(incore);
46 static open_method_decl(loose);
47 static open_method_decl(pack_non_delta);
48 static struct git_istream *attach_stream_filter(struct git_istream *st,
49 struct stream_filter *filter);
52 static open_istream_fn open_istream_tbl[] = {
55 open_istream_pack_non_delta,
58 #define FILTER_BUFFER (1024*16)
60 struct filtered_istream {
61 struct git_istream *upstream;
62 struct stream_filter *filter;
63 char ibuf[FILTER_BUFFER];
64 char obuf[FILTER_BUFFER];
71 const struct stream_vtbl *vtbl;
72 unsigned long size; /* inflated size of full object */
74 enum { z_unused, z_used, z_done, z_error } z_state;
78 char *buf; /* from read_object() */
79 unsigned long read_ptr;
84 unsigned long mapsize;
91 struct packed_git *pack;
95 struct filtered_istream filtered;
99 int close_istream(struct git_istream *st)
101 int r = st->vtbl->close(st);
106 ssize_t read_istream(struct git_istream *st, void *buf, size_t sz)
108 return st->vtbl->read(st, buf, sz);
111 static enum input_source istream_source(const struct object_id *oid,
112 enum object_type *type,
113 struct object_info *oi)
120 status = oid_object_info_extended(the_repository, oid, oi, 0);
124 switch (oi->whence) {
128 if (!oi->u.packed.is_delta && big_file_threshold < size)
129 return pack_non_delta;
136 struct git_istream *open_istream(const struct object_id *oid,
137 enum object_type *type,
139 struct stream_filter *filter)
141 struct git_istream *st;
142 struct object_info oi = OBJECT_INFO_INIT;
143 const struct object_id *real = lookup_replace_object(the_repository, oid);
144 enum input_source src = istream_source(real, type, &oi);
149 st = xmalloc(sizeof(*st));
150 if (open_istream_tbl[src](st, &oi, real, type)) {
151 if (open_istream_incore(st, &oi, real, type)) {
157 /* Add "&& !is_null_stream_filter(filter)" for performance */
158 struct git_istream *nst = attach_stream_filter(st, filter);
171 /*****************************************************************
175 *****************************************************************/
177 static void close_deflated_stream(struct git_istream *st)
179 if (st->z_state == z_used)
180 git_inflate_end(&st->z);
184 /*****************************************************************
188 *****************************************************************/
190 static close_method_decl(filtered)
192 free_stream_filter(st->u.filtered.filter);
193 return close_istream(st->u.filtered.upstream);
196 static read_method_decl(filtered)
198 struct filtered_istream *fs = &(st->u.filtered);
202 /* do we already have filtered output? */
203 if (fs->o_ptr < fs->o_end) {
204 size_t to_move = fs->o_end - fs->o_ptr;
207 memcpy(buf + filled, fs->obuf + fs->o_ptr, to_move);
208 fs->o_ptr += to_move;
213 fs->o_end = fs->o_ptr = 0;
215 /* do we have anything to feed the filter with? */
216 if (fs->i_ptr < fs->i_end) {
217 size_t to_feed = fs->i_end - fs->i_ptr;
218 size_t to_receive = FILTER_BUFFER;
219 if (stream_filter(fs->filter,
220 fs->ibuf + fs->i_ptr, &to_feed,
221 fs->obuf, &to_receive))
223 fs->i_ptr = fs->i_end - to_feed;
224 fs->o_end = FILTER_BUFFER - to_receive;
228 /* tell the filter to drain upon no more input */
229 if (fs->input_finished) {
230 size_t to_receive = FILTER_BUFFER;
231 if (stream_filter(fs->filter,
233 fs->obuf, &to_receive))
235 fs->o_end = FILTER_BUFFER - to_receive;
240 fs->i_end = fs->i_ptr = 0;
242 /* refill the input from the upstream */
243 if (!fs->input_finished) {
244 fs->i_end = read_istream(fs->upstream, fs->ibuf, FILTER_BUFFER);
250 fs->input_finished = 1;
255 static struct stream_vtbl filtered_vtbl = {
256 close_istream_filtered,
257 read_istream_filtered,
260 static struct git_istream *attach_stream_filter(struct git_istream *st,
261 struct stream_filter *filter)
263 struct git_istream *ifs = xmalloc(sizeof(*ifs));
264 struct filtered_istream *fs = &(ifs->u.filtered);
266 ifs->vtbl = &filtered_vtbl;
269 fs->i_end = fs->i_ptr = 0;
270 fs->o_end = fs->o_ptr = 0;
271 fs->input_finished = 0;
272 ifs->size = -1; /* unknown */
276 /*****************************************************************
278 * Loose object stream
280 *****************************************************************/
282 static read_method_decl(loose)
284 size_t total_read = 0;
286 switch (st->z_state) {
295 if (st->u.loose.hdr_used < st->u.loose.hdr_avail) {
296 size_t to_copy = st->u.loose.hdr_avail - st->u.loose.hdr_used;
299 memcpy(buf, st->u.loose.hdr + st->u.loose.hdr_used, to_copy);
300 st->u.loose.hdr_used += to_copy;
301 total_read += to_copy;
304 while (total_read < sz) {
307 st->z.next_out = (unsigned char *)buf + total_read;
308 st->z.avail_out = sz - total_read;
309 status = git_inflate(&st->z, Z_FINISH);
311 total_read = st->z.next_out - (unsigned char *)buf;
313 if (status == Z_STREAM_END) {
314 git_inflate_end(&st->z);
315 st->z_state = z_done;
318 if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) {
319 git_inflate_end(&st->z);
320 st->z_state = z_error;
327 static close_method_decl(loose)
329 close_deflated_stream(st);
330 munmap(st->u.loose.mapped, st->u.loose.mapsize);
334 static struct stream_vtbl loose_vtbl = {
339 static open_method_decl(loose)
341 st->u.loose.mapped = map_sha1_file(the_repository,
342 oid->hash, &st->u.loose.mapsize);
343 if (!st->u.loose.mapped)
345 if ((unpack_sha1_header(&st->z,
349 sizeof(st->u.loose.hdr)) < 0) ||
350 (parse_sha1_header(st->u.loose.hdr, &st->size) < 0)) {
351 git_inflate_end(&st->z);
352 munmap(st->u.loose.mapped, st->u.loose.mapsize);
356 st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
357 st->u.loose.hdr_avail = st->z.total_out;
358 st->z_state = z_used;
360 st->vtbl = &loose_vtbl;
365 /*****************************************************************
367 * Non-delta packed object stream
369 *****************************************************************/
371 static read_method_decl(pack_non_delta)
373 size_t total_read = 0;
375 switch (st->z_state) {
377 memset(&st->z, 0, sizeof(st->z));
378 git_inflate_init(&st->z);
379 st->z_state = z_used;
389 while (total_read < sz) {
391 struct pack_window *window = NULL;
392 unsigned char *mapped;
394 mapped = use_pack(st->u.in_pack.pack, &window,
395 st->u.in_pack.pos, &st->z.avail_in);
397 st->z.next_out = (unsigned char *)buf + total_read;
398 st->z.avail_out = sz - total_read;
399 st->z.next_in = mapped;
400 status = git_inflate(&st->z, Z_FINISH);
402 st->u.in_pack.pos += st->z.next_in - mapped;
403 total_read = st->z.next_out - (unsigned char *)buf;
406 if (status == Z_STREAM_END) {
407 git_inflate_end(&st->z);
408 st->z_state = z_done;
411 if (status != Z_OK && status != Z_BUF_ERROR) {
412 git_inflate_end(&st->z);
413 st->z_state = z_error;
420 static close_method_decl(pack_non_delta)
422 close_deflated_stream(st);
426 static struct stream_vtbl pack_non_delta_vtbl = {
427 close_istream_pack_non_delta,
428 read_istream_pack_non_delta,
431 static open_method_decl(pack_non_delta)
433 struct pack_window *window;
434 enum object_type in_pack_type;
436 st->u.in_pack.pack = oi->u.packed.pack;
437 st->u.in_pack.pos = oi->u.packed.offset;
440 in_pack_type = unpack_object_header(st->u.in_pack.pack,
445 switch (in_pack_type) {
447 return -1; /* we do not do deltas for now */
454 st->z_state = z_unused;
455 st->vtbl = &pack_non_delta_vtbl;
460 /*****************************************************************
464 *****************************************************************/
466 static close_method_decl(incore)
468 free(st->u.incore.buf);
472 static read_method_decl(incore)
474 size_t read_size = sz;
475 size_t remainder = st->size - st->u.incore.read_ptr;
477 if (remainder <= read_size)
478 read_size = remainder;
480 memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
481 st->u.incore.read_ptr += read_size;
486 static struct stream_vtbl incore_vtbl = {
487 close_istream_incore,
491 static open_method_decl(incore)
493 st->u.incore.buf = read_object_file_extended(oid, type, &st->size, 0);
494 st->u.incore.read_ptr = 0;
495 st->vtbl = &incore_vtbl;
497 return st->u.incore.buf ? 0 : -1;
501 /****************************************************************
502 * Users of streaming interface
503 ****************************************************************/
505 int stream_blob_to_fd(int fd, const struct object_id *oid, struct stream_filter *filter,
508 struct git_istream *st;
509 enum object_type type;
514 st = open_istream(oid, &type, &sz, filter);
517 free_stream_filter(filter);
520 if (type != OBJ_BLOB)
524 ssize_t wrote, holeto;
525 ssize_t readlen = read_istream(st, buf, sizeof(buf));
531 if (can_seek && sizeof(buf) == readlen) {
532 for (holeto = 0; holeto < readlen; holeto++)
535 if (readlen == holeto) {
541 if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1)
545 wrote = write_in_full(fd, buf, readlen);
550 if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
551 xwrite(fd, "", 1) != 1))