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);
45 static open_istream_fn open_istream_tbl[] = {
48 open_istream_pack_non_delta,
52 const struct stream_vtbl *vtbl;
53 unsigned long size; /* inflated size of full object */
55 enum { z_unused, z_used, z_done, z_error } z_state;
59 char *buf; /* from read_object() */
60 unsigned long read_ptr;
64 int fd; /* open for reading */
65 /* NEEDSWORK: what else? */
69 struct packed_git *pack;
75 int close_istream(struct git_istream *st)
77 return st->vtbl->close(st);
80 ssize_t read_istream(struct git_istream *st, char *buf, size_t sz)
82 return st->vtbl->read(st, buf, sz);
85 static enum input_source istream_source(const unsigned char *sha1,
86 enum object_type *type,
87 struct object_info *oi)
93 status = sha1_object_info_extended(sha1, oi);
102 if (!oi->u.packed.is_delta && big_file_threshold <= size)
103 return pack_non_delta;
110 struct git_istream *open_istream(const unsigned char *sha1,
111 enum object_type *type,
114 struct git_istream *st;
115 struct object_info oi;
116 const unsigned char *real = lookup_replace_object(sha1);
117 enum input_source src = istream_source(real, type, &oi);
122 st = xmalloc(sizeof(*st));
123 if (open_istream_tbl[src](st, &oi, real, type)) {
124 if (open_istream_incore(st, &oi, real, type)) {
134 /*****************************************************************
138 *****************************************************************/
140 static void close_deflated_stream(struct git_istream *st)
142 if (st->z_state == z_used)
143 git_inflate_end(&st->z);
147 /*****************************************************************
149 * Loose object stream
151 *****************************************************************/
153 static open_method_decl(loose)
155 return -1; /* for now */
159 /*****************************************************************
161 * Non-delta packed object stream
163 *****************************************************************/
165 static read_method_decl(pack_non_delta)
167 size_t total_read = 0;
169 switch (st->z_state) {
171 memset(&st->z, 0, sizeof(st->z));
172 git_inflate_init(&st->z);
173 st->z_state = z_used;
183 while (total_read < sz) {
185 struct pack_window *window = NULL;
186 unsigned char *mapped;
188 mapped = use_pack(st->u.in_pack.pack, &window,
189 st->u.in_pack.pos, &st->z.avail_in);
191 st->z.next_out = (unsigned char *)buf + total_read;
192 st->z.avail_out = sz - total_read;
193 st->z.next_in = mapped;
194 status = git_inflate(&st->z, Z_FINISH);
196 st->u.in_pack.pos += st->z.next_in - mapped;
197 total_read = st->z.next_out - (unsigned char *)buf;
200 if (status == Z_STREAM_END) {
201 git_inflate_end(&st->z);
202 st->z_state = z_done;
205 if (status != Z_OK && status != Z_BUF_ERROR) {
206 git_inflate_end(&st->z);
207 st->z_state = z_error;
214 static close_method_decl(pack_non_delta)
216 close_deflated_stream(st);
220 static struct stream_vtbl pack_non_delta_vtbl = {
221 close_istream_pack_non_delta,
222 read_istream_pack_non_delta,
225 static open_method_decl(pack_non_delta)
227 struct pack_window *window;
228 enum object_type in_pack_type;
230 st->u.in_pack.pack = oi->u.packed.pack;
231 st->u.in_pack.pos = oi->u.packed.offset;
234 in_pack_type = unpack_object_header(st->u.in_pack.pack,
239 switch (in_pack_type) {
241 return -1; /* we do not do deltas for now */
248 st->z_state = z_unused;
249 st->vtbl = &pack_non_delta_vtbl;
254 /*****************************************************************
258 *****************************************************************/
260 static close_method_decl(incore)
262 free(st->u.incore.buf);
266 static read_method_decl(incore)
268 size_t read_size = sz;
269 size_t remainder = st->size - st->u.incore.read_ptr;
271 if (remainder <= read_size)
272 read_size = remainder;
274 memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
275 st->u.incore.read_ptr += read_size;
280 static struct stream_vtbl incore_vtbl = {
281 close_istream_incore,
285 static open_method_decl(incore)
287 st->u.incore.buf = read_sha1_file_extended(sha1, type, &st->size, 0);
288 st->u.incore.read_ptr = 0;
289 st->vtbl = &incore_vtbl;
291 return st->u.incore.buf ? 0 : -1;