]> git.ipfire.org Git - thirdparty/git.git/blame - streaming.c
Merge branch 'ak/gcc46-profile-feedback'
[thirdparty/git.git] / streaming.c
CommitLineData
46bf0438
JH
1/*
2 * Copyright (c) 2011, Google Inc.
3 */
4#include "cache.h"
5#include "streaming.h"
6
7enum input_source {
8 stream_error = -1,
9 incore = 0,
10 loose = 1,
11 pack_non_delta = 2
12};
13
14typedef int (*open_istream_fn)(struct git_istream *,
15 struct object_info *,
16 const unsigned char *,
17 enum object_type *);
18typedef int (*close_istream_fn)(struct git_istream *);
19typedef ssize_t (*read_istream_fn)(struct git_istream *, char *, size_t);
20
21struct stream_vtbl {
22 close_istream_fn close;
23 read_istream_fn read;
24};
25
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)
31
32#define close_method_decl(name) \
33 int close_istream_ ##name \
34 (struct git_istream *st)
35
36#define read_method_decl(name) \
37 ssize_t read_istream_ ##name \
38 (struct git_istream *st, char *buf, size_t sz)
39
40/* forward declaration */
41static open_method_decl(incore);
42static open_method_decl(loose);
43static open_method_decl(pack_non_delta);
b6691092
JH
44static struct git_istream *attach_stream_filter(struct git_istream *st,
45 struct stream_filter *filter);
46
46bf0438
JH
47
48static open_istream_fn open_istream_tbl[] = {
49 open_istream_incore,
50 open_istream_loose,
51 open_istream_pack_non_delta,
52};
53
b6691092
JH
54#define FILTER_BUFFER (1024*16)
55
56struct filtered_istream {
57 struct git_istream *upstream;
58 struct stream_filter *filter;
59 char ibuf[FILTER_BUFFER];
60 char obuf[FILTER_BUFFER];
61 int i_end, i_ptr;
62 int o_end, o_ptr;
4ae66704 63 int input_finished;
b6691092
JH
64};
65
46bf0438
JH
66struct git_istream {
67 const struct stream_vtbl *vtbl;
68 unsigned long size; /* inflated size of full object */
7ef2d9a2
JH
69 z_stream z;
70 enum { z_unused, z_used, z_done, z_error } z_state;
46bf0438
JH
71
72 union {
73 struct {
74 char *buf; /* from read_object() */
75 unsigned long read_ptr;
76 } incore;
77
78 struct {
93aa7bd5
JH
79 void *mapped;
80 unsigned long mapsize;
81 char hdr[32];
82 int hdr_avail;
83 int hdr_used;
46bf0438
JH
84 } loose;
85
86 struct {
7ef2d9a2
JH
87 struct packed_git *pack;
88 off_t pos;
46bf0438 89 } in_pack;
b6691092
JH
90
91 struct filtered_istream filtered;
46bf0438
JH
92 } u;
93};
94
95int close_istream(struct git_istream *st)
96{
97 return st->vtbl->close(st);
98}
99
100ssize_t read_istream(struct git_istream *st, char *buf, size_t sz)
101{
102 return st->vtbl->read(st, buf, sz);
103}
104
105static enum input_source istream_source(const unsigned char *sha1,
106 enum object_type *type,
107 struct object_info *oi)
108{
109 unsigned long size;
110 int status;
111
112 oi->sizep = &size;
113 status = sha1_object_info_extended(sha1, oi);
114 if (status < 0)
115 return stream_error;
116 *type = status;
117
118 switch (oi->whence) {
119 case OI_LOOSE:
120 return loose;
121 case OI_PACKED:
122 if (!oi->u.packed.is_delta && big_file_threshold <= size)
123 return pack_non_delta;
124 /* fallthru */
125 default:
126 return incore;
127 }
128}
129
130struct git_istream *open_istream(const unsigned char *sha1,
131 enum object_type *type,
b6691092
JH
132 unsigned long *size,
133 struct stream_filter *filter)
46bf0438
JH
134{
135 struct git_istream *st;
136 struct object_info oi;
137 const unsigned char *real = lookup_replace_object(sha1);
138 enum input_source src = istream_source(real, type, &oi);
139
140 if (src < 0)
141 return NULL;
142
143 st = xmalloc(sizeof(*st));
144 if (open_istream_tbl[src](st, &oi, real, type)) {
145 if (open_istream_incore(st, &oi, real, type)) {
146 free(st);
147 return NULL;
148 }
149 }
b6691092
JH
150 if (st && filter) {
151 /* Add "&& !is_null_stream_filter(filter)" for performance */
152 struct git_istream *nst = attach_stream_filter(st, filter);
153 if (!nst)
154 close_istream(st);
155 st = nst;
156 }
157
46bf0438
JH
158 *size = st->size;
159 return st;
160}
161
7ef2d9a2
JH
162
163/*****************************************************************
164 *
165 * Common helpers
166 *
167 *****************************************************************/
168
169static void close_deflated_stream(struct git_istream *st)
170{
171 if (st->z_state == z_used)
172 git_inflate_end(&st->z);
173}
174
175
b6691092
JH
176/*****************************************************************
177 *
178 * Filtered stream
179 *
180 *****************************************************************/
181
182static close_method_decl(filtered)
183{
184 free_stream_filter(st->u.filtered.filter);
185 return close_istream(st->u.filtered.upstream);
186}
187
188static read_method_decl(filtered)
189{
190 struct filtered_istream *fs = &(st->u.filtered);
191 size_t filled = 0;
192
193 while (sz) {
194 /* do we already have filtered output? */
195 if (fs->o_ptr < fs->o_end) {
196 size_t to_move = fs->o_end - fs->o_ptr;
197 if (sz < to_move)
198 to_move = sz;
199 memcpy(buf + filled, fs->obuf + fs->o_ptr, to_move);
200 fs->o_ptr += to_move;
201 sz -= to_move;
202 filled += to_move;
203 continue;
204 }
205 fs->o_end = fs->o_ptr = 0;
206
207 /* do we have anything to feed the filter with? */
208 if (fs->i_ptr < fs->i_end) {
209 size_t to_feed = fs->i_end - fs->i_ptr;
210 size_t to_receive = FILTER_BUFFER;
211 if (stream_filter(fs->filter,
212 fs->ibuf + fs->i_ptr, &to_feed,
213 fs->obuf, &to_receive))
214 return -1;
215 fs->i_ptr = fs->i_end - to_feed;
216 fs->o_end = FILTER_BUFFER - to_receive;
217 continue;
218 }
4ae66704
JH
219
220 /* tell the filter to drain upon no more input */
221 if (fs->input_finished) {
222 size_t to_receive = FILTER_BUFFER;
223 if (stream_filter(fs->filter,
224 NULL, NULL,
225 fs->obuf, &to_receive))
226 return -1;
227 fs->o_end = FILTER_BUFFER - to_receive;
228 if (!fs->o_end)
229 break;
230 continue;
231 }
b6691092
JH
232 fs->i_end = fs->i_ptr = 0;
233
234 /* refill the input from the upstream */
4ae66704
JH
235 if (!fs->input_finished) {
236 fs->i_end = read_istream(fs->upstream, fs->ibuf, FILTER_BUFFER);
237 if (fs->i_end < 0)
238 break;
239 if (fs->i_end)
240 continue;
241 }
242 fs->input_finished = 1;
b6691092
JH
243 }
244 return filled;
245}
246
247static struct stream_vtbl filtered_vtbl = {
248 close_istream_filtered,
249 read_istream_filtered,
250};
251
252static struct git_istream *attach_stream_filter(struct git_istream *st,
253 struct stream_filter *filter)
254{
255 struct git_istream *ifs = xmalloc(sizeof(*ifs));
256 struct filtered_istream *fs = &(ifs->u.filtered);
257
258 ifs->vtbl = &filtered_vtbl;
259 fs->upstream = st;
260 fs->filter = filter;
261 fs->i_end = fs->i_ptr = 0;
262 fs->o_end = fs->o_ptr = 0;
4ae66704 263 fs->input_finished = 0;
b6691092
JH
264 ifs->size = -1; /* unknown */
265 return ifs;
266}
267
46bf0438
JH
268/*****************************************************************
269 *
270 * Loose object stream
271 *
272 *****************************************************************/
273
93aa7bd5
JH
274static read_method_decl(loose)
275{
276 size_t total_read = 0;
277
278 switch (st->z_state) {
279 case z_done:
280 return 0;
281 case z_error:
282 return -1;
283 default:
284 break;
285 }
286
287 if (st->u.loose.hdr_used < st->u.loose.hdr_avail) {
288 size_t to_copy = st->u.loose.hdr_avail - st->u.loose.hdr_used;
289 if (sz < to_copy)
290 to_copy = sz;
291 memcpy(buf, st->u.loose.hdr + st->u.loose.hdr_used, to_copy);
292 st->u.loose.hdr_used += to_copy;
293 total_read += to_copy;
294 }
295
296 while (total_read < sz) {
297 int status;
298
299 st->z.next_out = (unsigned char *)buf + total_read;
300 st->z.avail_out = sz - total_read;
301 status = git_inflate(&st->z, Z_FINISH);
302
303 total_read = st->z.next_out - (unsigned char *)buf;
304
305 if (status == Z_STREAM_END) {
306 git_inflate_end(&st->z);
307 st->z_state = z_done;
308 break;
309 }
310 if (status != Z_OK && status != Z_BUF_ERROR) {
311 git_inflate_end(&st->z);
312 st->z_state = z_error;
313 return -1;
314 }
315 }
316 return total_read;
317}
318
319static close_method_decl(loose)
320{
321 close_deflated_stream(st);
322 munmap(st->u.loose.mapped, st->u.loose.mapsize);
323 return 0;
324}
325
326static struct stream_vtbl loose_vtbl = {
327 close_istream_loose,
328 read_istream_loose,
329};
330
46bf0438
JH
331static open_method_decl(loose)
332{
93aa7bd5
JH
333 st->u.loose.mapped = map_sha1_file(sha1, &st->u.loose.mapsize);
334 if (!st->u.loose.mapped)
335 return -1;
336 if (unpack_sha1_header(&st->z,
337 st->u.loose.mapped,
338 st->u.loose.mapsize,
339 st->u.loose.hdr,
340 sizeof(st->u.loose.hdr)) < 0) {
341 git_inflate_end(&st->z);
342 munmap(st->u.loose.mapped, st->u.loose.mapsize);
343 return -1;
344 }
345
346 parse_sha1_header(st->u.loose.hdr, &st->size);
347 st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
348 st->u.loose.hdr_avail = st->z.total_out;
349 st->z_state = z_used;
350
351 st->vtbl = &loose_vtbl;
352 return 0;
46bf0438
JH
353}
354
355
356/*****************************************************************
357 *
358 * Non-delta packed object stream
359 *
360 *****************************************************************/
361
7ef2d9a2
JH
362static read_method_decl(pack_non_delta)
363{
364 size_t total_read = 0;
365
366 switch (st->z_state) {
367 case z_unused:
368 memset(&st->z, 0, sizeof(st->z));
369 git_inflate_init(&st->z);
370 st->z_state = z_used;
371 break;
372 case z_done:
373 return 0;
374 case z_error:
375 return -1;
376 case z_used:
377 break;
378 }
379
380 while (total_read < sz) {
381 int status;
382 struct pack_window *window = NULL;
383 unsigned char *mapped;
384
385 mapped = use_pack(st->u.in_pack.pack, &window,
386 st->u.in_pack.pos, &st->z.avail_in);
387
388 st->z.next_out = (unsigned char *)buf + total_read;
389 st->z.avail_out = sz - total_read;
390 st->z.next_in = mapped;
391 status = git_inflate(&st->z, Z_FINISH);
392
393 st->u.in_pack.pos += st->z.next_in - mapped;
394 total_read = st->z.next_out - (unsigned char *)buf;
395 unuse_pack(&window);
396
397 if (status == Z_STREAM_END) {
398 git_inflate_end(&st->z);
399 st->z_state = z_done;
400 break;
401 }
402 if (status != Z_OK && status != Z_BUF_ERROR) {
403 git_inflate_end(&st->z);
404 st->z_state = z_error;
405 return -1;
406 }
407 }
408 return total_read;
409}
410
411static close_method_decl(pack_non_delta)
412{
413 close_deflated_stream(st);
414 return 0;
415}
416
417static struct stream_vtbl pack_non_delta_vtbl = {
418 close_istream_pack_non_delta,
419 read_istream_pack_non_delta,
420};
421
46bf0438
JH
422static open_method_decl(pack_non_delta)
423{
7ef2d9a2
JH
424 struct pack_window *window;
425 enum object_type in_pack_type;
426
427 st->u.in_pack.pack = oi->u.packed.pack;
428 st->u.in_pack.pos = oi->u.packed.offset;
429 window = NULL;
430
431 in_pack_type = unpack_object_header(st->u.in_pack.pack,
432 &window,
433 &st->u.in_pack.pos,
434 &st->size);
435 unuse_pack(&window);
436 switch (in_pack_type) {
437 default:
438 return -1; /* we do not do deltas for now */
439 case OBJ_COMMIT:
440 case OBJ_TREE:
441 case OBJ_BLOB:
442 case OBJ_TAG:
443 break;
444 }
445 st->z_state = z_unused;
446 st->vtbl = &pack_non_delta_vtbl;
447 return 0;
46bf0438
JH
448}
449
450
451/*****************************************************************
452 *
453 * In-core stream
454 *
455 *****************************************************************/
456
457static close_method_decl(incore)
458{
459 free(st->u.incore.buf);
460 return 0;
461}
462
463static read_method_decl(incore)
464{
465 size_t read_size = sz;
466 size_t remainder = st->size - st->u.incore.read_ptr;
467
468 if (remainder <= read_size)
469 read_size = remainder;
470 if (read_size) {
471 memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
472 st->u.incore.read_ptr += read_size;
473 }
474 return read_size;
475}
476
477static struct stream_vtbl incore_vtbl = {
478 close_istream_incore,
479 read_istream_incore,
480};
481
482static open_method_decl(incore)
483{
484 st->u.incore.buf = read_sha1_file_extended(sha1, type, &st->size, 0);
485 st->u.incore.read_ptr = 0;
486 st->vtbl = &incore_vtbl;
487
488 return st->u.incore.buf ? 0 : -1;
489}