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 */
57 char *buf; /* from read_object() */
58 unsigned long read_ptr;
62 int fd; /* open for reading */
63 /* NEEDSWORK: what else? */
67 int fd; /* open for reading */
68 /* NEEDSWORK: what else? */
73 int close_istream(struct git_istream *st)
75 return st->vtbl->close(st);
78 ssize_t read_istream(struct git_istream *st, char *buf, size_t sz)
80 return st->vtbl->read(st, buf, sz);
83 static enum input_source istream_source(const unsigned char *sha1,
84 enum object_type *type,
85 struct object_info *oi)
91 status = sha1_object_info_extended(sha1, oi);
100 if (!oi->u.packed.is_delta && big_file_threshold <= size)
101 return pack_non_delta;
108 struct git_istream *open_istream(const unsigned char *sha1,
109 enum object_type *type,
112 struct git_istream *st;
113 struct object_info oi;
114 const unsigned char *real = lookup_replace_object(sha1);
115 enum input_source src = istream_source(real, type, &oi);
120 st = xmalloc(sizeof(*st));
121 if (open_istream_tbl[src](st, &oi, real, type)) {
122 if (open_istream_incore(st, &oi, real, type)) {
131 /*****************************************************************
133 * Loose object stream
135 *****************************************************************/
137 static open_method_decl(loose)
139 return -1; /* for now */
143 /*****************************************************************
145 * Non-delta packed object stream
147 *****************************************************************/
149 static open_method_decl(pack_non_delta)
151 return -1; /* for now */
155 /*****************************************************************
159 *****************************************************************/
161 static close_method_decl(incore)
163 free(st->u.incore.buf);
167 static read_method_decl(incore)
169 size_t read_size = sz;
170 size_t remainder = st->size - st->u.incore.read_ptr;
172 if (remainder <= read_size)
173 read_size = remainder;
175 memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
176 st->u.incore.read_ptr += read_size;
181 static struct stream_vtbl incore_vtbl = {
182 close_istream_incore,
186 static open_method_decl(incore)
188 st->u.incore.buf = read_sha1_file_extended(sha1, type, &st->size, 0);
189 st->u.incore.read_ptr = 0;
190 st->vtbl = &incore_vtbl;
192 return st->u.incore.buf ? 0 : -1;