sha1_file: convert sha1_object_info* to object_id
[alt-git.git] / streaming.c
blob344678e95f21246a615916dfd946a3b095a9dd8a
1 /*
2 * Copyright (c) 2011, Google Inc.
3 */
4 #include "cache.h"
5 #include "streaming.h"
6 #include "packfile.h"
8 enum input_source {
9 stream_error = -1,
10 incore = 0,
11 loose = 1,
12 pack_non_delta = 2
15 typedef int (*open_istream_fn)(struct git_istream *,
16 struct object_info *,
17 const unsigned char *,
18 enum object_type *);
19 typedef int (*close_istream_fn)(struct git_istream *);
20 typedef ssize_t (*read_istream_fn)(struct git_istream *, char *, size_t);
22 struct stream_vtbl {
23 close_istream_fn close;
24 read_istream_fn read;
27 #define open_method_decl(name) \
28 int open_istream_ ##name \
29 (struct git_istream *st, struct object_info *oi, \
30 const unsigned char *sha1, \
31 enum object_type *type)
33 #define close_method_decl(name) \
34 int close_istream_ ##name \
35 (struct git_istream *st)
37 #define read_method_decl(name) \
38 ssize_t read_istream_ ##name \
39 (struct git_istream *st, char *buf, size_t sz)
41 /* forward declaration */
42 static open_method_decl(incore);
43 static open_method_decl(loose);
44 static open_method_decl(pack_non_delta);
45 static struct git_istream *attach_stream_filter(struct git_istream *st,
46 struct stream_filter *filter);
49 static open_istream_fn open_istream_tbl[] = {
50 open_istream_incore,
51 open_istream_loose,
52 open_istream_pack_non_delta,
55 #define FILTER_BUFFER (1024*16)
57 struct filtered_istream {
58 struct git_istream *upstream;
59 struct stream_filter *filter;
60 char ibuf[FILTER_BUFFER];
61 char obuf[FILTER_BUFFER];
62 int i_end, i_ptr;
63 int o_end, o_ptr;
64 int input_finished;
67 struct git_istream {
68 const struct stream_vtbl *vtbl;
69 unsigned long size; /* inflated size of full object */
70 git_zstream z;
71 enum { z_unused, z_used, z_done, z_error } z_state;
73 union {
74 struct {
75 char *buf; /* from read_object() */
76 unsigned long read_ptr;
77 } incore;
79 struct {
80 void *mapped;
81 unsigned long mapsize;
82 char hdr[32];
83 int hdr_avail;
84 int hdr_used;
85 } loose;
87 struct {
88 struct packed_git *pack;
89 off_t pos;
90 } in_pack;
92 struct filtered_istream filtered;
93 } u;
96 int close_istream(struct git_istream *st)
98 int r = st->vtbl->close(st);
99 free(st);
100 return r;
103 ssize_t read_istream(struct git_istream *st, void *buf, size_t sz)
105 return st->vtbl->read(st, buf, sz);
108 static enum input_source istream_source(const unsigned char *sha1,
109 enum object_type *type,
110 struct object_info *oi)
112 unsigned long size;
113 int status;
114 struct object_id oid;
116 hashcpy(oid.hash, sha1);
118 oi->typep = type;
119 oi->sizep = &size;
120 status = oid_object_info_extended(&oid, oi, 0);
121 if (status < 0)
122 return stream_error;
124 switch (oi->whence) {
125 case OI_LOOSE:
126 return loose;
127 case OI_PACKED:
128 if (!oi->u.packed.is_delta && big_file_threshold < size)
129 return pack_non_delta;
130 /* fallthru */
131 default:
132 return incore;
136 struct git_istream *open_istream(const struct object_id *oid,
137 enum object_type *type,
138 unsigned long *size,
139 struct stream_filter *filter)
141 struct git_istream *st;
142 struct object_info oi = OBJECT_INFO_INIT;
143 const unsigned char *real = lookup_replace_object(oid->hash);
144 enum input_source src = istream_source(real, type, &oi);
146 if (src < 0)
147 return NULL;
149 st = xmalloc(sizeof(*st));
150 if (open_istream_tbl[src](st, &oi, real, type)) {
151 if (open_istream_incore(st, &oi, real, type)) {
152 free(st);
153 return NULL;
156 if (filter) {
157 /* Add "&& !is_null_stream_filter(filter)" for performance */
158 struct git_istream *nst = attach_stream_filter(st, filter);
159 if (!nst) {
160 close_istream(st);
161 return NULL;
163 st = nst;
166 *size = st->size;
167 return st;
171 /*****************************************************************
173 * Common helpers
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 /*****************************************************************
186 * Filtered stream
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);
199 size_t filled = 0;
201 while (sz) {
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;
205 if (sz < to_move)
206 to_move = sz;
207 memcpy(buf + filled, fs->obuf + fs->o_ptr, to_move);
208 fs->o_ptr += to_move;
209 sz -= to_move;
210 filled += to_move;
211 continue;
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))
222 return -1;
223 fs->i_ptr = fs->i_end - to_feed;
224 fs->o_end = FILTER_BUFFER - to_receive;
225 continue;
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,
232 NULL, NULL,
233 fs->obuf, &to_receive))
234 return -1;
235 fs->o_end = FILTER_BUFFER - to_receive;
236 if (!fs->o_end)
237 break;
238 continue;
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);
245 if (fs->i_end < 0)
246 return -1;
247 if (fs->i_end)
248 continue;
250 fs->input_finished = 1;
252 return filled;
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;
267 fs->upstream = st;
268 fs->filter = filter;
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 */
273 return ifs;
276 /*****************************************************************
278 * Loose object stream
280 *****************************************************************/
282 static read_method_decl(loose)
284 size_t total_read = 0;
286 switch (st->z_state) {
287 case z_done:
288 return 0;
289 case z_error:
290 return -1;
291 default:
292 break;
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;
297 if (sz < to_copy)
298 to_copy = sz;
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) {
305 int status;
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;
316 break;
318 if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) {
319 git_inflate_end(&st->z);
320 st->z_state = z_error;
321 return -1;
324 return total_read;
327 static close_method_decl(loose)
329 close_deflated_stream(st);
330 munmap(st->u.loose.mapped, st->u.loose.mapsize);
331 return 0;
334 static struct stream_vtbl loose_vtbl = {
335 close_istream_loose,
336 read_istream_loose,
339 static open_method_decl(loose)
341 st->u.loose.mapped = map_sha1_file(sha1, &st->u.loose.mapsize);
342 if (!st->u.loose.mapped)
343 return -1;
344 if ((unpack_sha1_header(&st->z,
345 st->u.loose.mapped,
346 st->u.loose.mapsize,
347 st->u.loose.hdr,
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);
352 return -1;
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;
360 return 0;
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) {
375 case z_unused:
376 memset(&st->z, 0, sizeof(st->z));
377 git_inflate_init(&st->z);
378 st->z_state = z_used;
379 break;
380 case z_done:
381 return 0;
382 case z_error:
383 return -1;
384 case z_used:
385 break;
388 while (total_read < sz) {
389 int status;
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;
403 unuse_pack(&window);
405 if (status == Z_STREAM_END) {
406 git_inflate_end(&st->z);
407 st->z_state = z_done;
408 break;
410 if (status != Z_OK && status != Z_BUF_ERROR) {
411 git_inflate_end(&st->z);
412 st->z_state = z_error;
413 return -1;
416 return total_read;
419 static close_method_decl(pack_non_delta)
421 close_deflated_stream(st);
422 return 0;
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;
437 window = NULL;
439 in_pack_type = unpack_object_header(st->u.in_pack.pack,
440 &window,
441 &st->u.in_pack.pos,
442 &st->size);
443 unuse_pack(&window);
444 switch (in_pack_type) {
445 default:
446 return -1; /* we do not do deltas for now */
447 case OBJ_COMMIT:
448 case OBJ_TREE:
449 case OBJ_BLOB:
450 case OBJ_TAG:
451 break;
453 st->z_state = z_unused;
454 st->vtbl = &pack_non_delta_vtbl;
455 return 0;
459 /*****************************************************************
461 * In-core stream
463 *****************************************************************/
465 static close_method_decl(incore)
467 free(st->u.incore.buf);
468 return 0;
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;
478 if (read_size) {
479 memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
480 st->u.incore.read_ptr += read_size;
482 return read_size;
485 static struct stream_vtbl incore_vtbl = {
486 close_istream_incore,
487 read_istream_incore,
490 static open_method_decl(incore)
492 st->u.incore.buf = read_sha1_file_extended(sha1, 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,
505 int can_seek)
507 struct git_istream *st;
508 enum object_type type;
509 unsigned long sz;
510 ssize_t kept = 0;
511 int result = -1;
513 st = open_istream(oid, &type, &sz, filter);
514 if (!st) {
515 if (filter)
516 free_stream_filter(filter);
517 return result;
519 if (type != OBJ_BLOB)
520 goto close_and_exit;
521 for (;;) {
522 char buf[1024 * 16];
523 ssize_t wrote, holeto;
524 ssize_t readlen = read_istream(st, buf, sizeof(buf));
526 if (readlen < 0)
527 goto close_and_exit;
528 if (!readlen)
529 break;
530 if (can_seek && sizeof(buf) == readlen) {
531 for (holeto = 0; holeto < readlen; holeto++)
532 if (buf[holeto])
533 break;
534 if (readlen == holeto) {
535 kept += holeto;
536 continue;
540 if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1)
541 goto close_and_exit;
542 else
543 kept = 0;
544 wrote = write_in_full(fd, buf, readlen);
546 if (wrote < 0)
547 goto close_and_exit;
549 if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
550 xwrite(fd, "", 1) != 1))
551 goto close_and_exit;
552 result = 0;
554 close_and_exit:
555 close_istream(st);
556 return result;