]> git.ipfire.org Git - thirdparty/git.git/blob - streaming.c
streaming.c: avoid forward declarations
[thirdparty/git.git] / streaming.c
1 /*
2 * Copyright (c) 2011, Google Inc.
3 */
4 #include "cache.h"
5 #include "streaming.h"
6 #include "repository.h"
7 #include "object-store.h"
8 #include "replace-object.h"
9 #include "packfile.h"
10
11 enum input_source {
12 stream_error = -1,
13 incore = 0,
14 loose = 1,
15 pack_non_delta = 2
16 };
17
18 typedef int (*open_istream_fn)(struct git_istream *,
19 struct repository *,
20 struct object_info *,
21 const struct object_id *,
22 enum object_type *);
23 typedef int (*close_istream_fn)(struct git_istream *);
24 typedef ssize_t (*read_istream_fn)(struct git_istream *, char *, size_t);
25
26 struct stream_vtbl {
27 close_istream_fn close;
28 read_istream_fn read;
29 };
30
31 #define open_method_decl(name) \
32 int open_istream_ ##name \
33 (struct git_istream *st, struct repository *r, \
34 struct object_info *oi, const struct object_id *oid, \
35 enum object_type *type)
36
37 #define close_method_decl(name) \
38 int close_istream_ ##name \
39 (struct git_istream *st)
40
41 #define read_method_decl(name) \
42 ssize_t read_istream_ ##name \
43 (struct git_istream *st, char *buf, size_t sz)
44
45 #define FILTER_BUFFER (1024*16)
46
47 struct filtered_istream {
48 struct git_istream *upstream;
49 struct stream_filter *filter;
50 char ibuf[FILTER_BUFFER];
51 char obuf[FILTER_BUFFER];
52 int i_end, i_ptr;
53 int o_end, o_ptr;
54 int input_finished;
55 };
56
57 struct git_istream {
58 const struct stream_vtbl *vtbl;
59 unsigned long size; /* inflated size of full object */
60 git_zstream z;
61 enum { z_unused, z_used, z_done, z_error } z_state;
62
63 union {
64 struct {
65 char *buf; /* from read_object() */
66 unsigned long read_ptr;
67 } incore;
68
69 struct {
70 void *mapped;
71 unsigned long mapsize;
72 char hdr[32];
73 int hdr_avail;
74 int hdr_used;
75 } loose;
76
77 struct {
78 struct packed_git *pack;
79 off_t pos;
80 } in_pack;
81
82 struct filtered_istream filtered;
83 } u;
84 };
85
86 /*****************************************************************
87 *
88 * Common helpers
89 *
90 *****************************************************************/
91
92 static void close_deflated_stream(struct git_istream *st)
93 {
94 if (st->z_state == z_used)
95 git_inflate_end(&st->z);
96 }
97
98
99 /*****************************************************************
100 *
101 * Filtered stream
102 *
103 *****************************************************************/
104
105 static close_method_decl(filtered)
106 {
107 free_stream_filter(st->u.filtered.filter);
108 return close_istream(st->u.filtered.upstream);
109 }
110
111 static read_method_decl(filtered)
112 {
113 struct filtered_istream *fs = &(st->u.filtered);
114 size_t filled = 0;
115
116 while (sz) {
117 /* do we already have filtered output? */
118 if (fs->o_ptr < fs->o_end) {
119 size_t to_move = fs->o_end - fs->o_ptr;
120 if (sz < to_move)
121 to_move = sz;
122 memcpy(buf + filled, fs->obuf + fs->o_ptr, to_move);
123 fs->o_ptr += to_move;
124 sz -= to_move;
125 filled += to_move;
126 continue;
127 }
128 fs->o_end = fs->o_ptr = 0;
129
130 /* do we have anything to feed the filter with? */
131 if (fs->i_ptr < fs->i_end) {
132 size_t to_feed = fs->i_end - fs->i_ptr;
133 size_t to_receive = FILTER_BUFFER;
134 if (stream_filter(fs->filter,
135 fs->ibuf + fs->i_ptr, &to_feed,
136 fs->obuf, &to_receive))
137 return -1;
138 fs->i_ptr = fs->i_end - to_feed;
139 fs->o_end = FILTER_BUFFER - to_receive;
140 continue;
141 }
142
143 /* tell the filter to drain upon no more input */
144 if (fs->input_finished) {
145 size_t to_receive = FILTER_BUFFER;
146 if (stream_filter(fs->filter,
147 NULL, NULL,
148 fs->obuf, &to_receive))
149 return -1;
150 fs->o_end = FILTER_BUFFER - to_receive;
151 if (!fs->o_end)
152 break;
153 continue;
154 }
155 fs->i_end = fs->i_ptr = 0;
156
157 /* refill the input from the upstream */
158 if (!fs->input_finished) {
159 fs->i_end = read_istream(fs->upstream, fs->ibuf, FILTER_BUFFER);
160 if (fs->i_end < 0)
161 return -1;
162 if (fs->i_end)
163 continue;
164 }
165 fs->input_finished = 1;
166 }
167 return filled;
168 }
169
170 static struct stream_vtbl filtered_vtbl = {
171 close_istream_filtered,
172 read_istream_filtered,
173 };
174
175 static struct git_istream *attach_stream_filter(struct git_istream *st,
176 struct stream_filter *filter)
177 {
178 struct git_istream *ifs = xmalloc(sizeof(*ifs));
179 struct filtered_istream *fs = &(ifs->u.filtered);
180
181 ifs->vtbl = &filtered_vtbl;
182 fs->upstream = st;
183 fs->filter = filter;
184 fs->i_end = fs->i_ptr = 0;
185 fs->o_end = fs->o_ptr = 0;
186 fs->input_finished = 0;
187 ifs->size = -1; /* unknown */
188 return ifs;
189 }
190
191 /*****************************************************************
192 *
193 * Loose object stream
194 *
195 *****************************************************************/
196
197 static read_method_decl(loose)
198 {
199 size_t total_read = 0;
200
201 switch (st->z_state) {
202 case z_done:
203 return 0;
204 case z_error:
205 return -1;
206 default:
207 break;
208 }
209
210 if (st->u.loose.hdr_used < st->u.loose.hdr_avail) {
211 size_t to_copy = st->u.loose.hdr_avail - st->u.loose.hdr_used;
212 if (sz < to_copy)
213 to_copy = sz;
214 memcpy(buf, st->u.loose.hdr + st->u.loose.hdr_used, to_copy);
215 st->u.loose.hdr_used += to_copy;
216 total_read += to_copy;
217 }
218
219 while (total_read < sz) {
220 int status;
221
222 st->z.next_out = (unsigned char *)buf + total_read;
223 st->z.avail_out = sz - total_read;
224 status = git_inflate(&st->z, Z_FINISH);
225
226 total_read = st->z.next_out - (unsigned char *)buf;
227
228 if (status == Z_STREAM_END) {
229 git_inflate_end(&st->z);
230 st->z_state = z_done;
231 break;
232 }
233 if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) {
234 git_inflate_end(&st->z);
235 st->z_state = z_error;
236 return -1;
237 }
238 }
239 return total_read;
240 }
241
242 static close_method_decl(loose)
243 {
244 close_deflated_stream(st);
245 munmap(st->u.loose.mapped, st->u.loose.mapsize);
246 return 0;
247 }
248
249 static struct stream_vtbl loose_vtbl = {
250 close_istream_loose,
251 read_istream_loose,
252 };
253
254 static open_method_decl(loose)
255 {
256 st->u.loose.mapped = map_loose_object(r, oid, &st->u.loose.mapsize);
257 if (!st->u.loose.mapped)
258 return -1;
259 if ((unpack_loose_header(&st->z,
260 st->u.loose.mapped,
261 st->u.loose.mapsize,
262 st->u.loose.hdr,
263 sizeof(st->u.loose.hdr)) < 0) ||
264 (parse_loose_header(st->u.loose.hdr, &st->size) < 0)) {
265 git_inflate_end(&st->z);
266 munmap(st->u.loose.mapped, st->u.loose.mapsize);
267 return -1;
268 }
269
270 st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
271 st->u.loose.hdr_avail = st->z.total_out;
272 st->z_state = z_used;
273
274 st->vtbl = &loose_vtbl;
275 return 0;
276 }
277
278
279 /*****************************************************************
280 *
281 * Non-delta packed object stream
282 *
283 *****************************************************************/
284
285 static read_method_decl(pack_non_delta)
286 {
287 size_t total_read = 0;
288
289 switch (st->z_state) {
290 case z_unused:
291 memset(&st->z, 0, sizeof(st->z));
292 git_inflate_init(&st->z);
293 st->z_state = z_used;
294 break;
295 case z_done:
296 return 0;
297 case z_error:
298 return -1;
299 case z_used:
300 break;
301 }
302
303 while (total_read < sz) {
304 int status;
305 struct pack_window *window = NULL;
306 unsigned char *mapped;
307
308 mapped = use_pack(st->u.in_pack.pack, &window,
309 st->u.in_pack.pos, &st->z.avail_in);
310
311 st->z.next_out = (unsigned char *)buf + total_read;
312 st->z.avail_out = sz - total_read;
313 st->z.next_in = mapped;
314 status = git_inflate(&st->z, Z_FINISH);
315
316 st->u.in_pack.pos += st->z.next_in - mapped;
317 total_read = st->z.next_out - (unsigned char *)buf;
318 unuse_pack(&window);
319
320 if (status == Z_STREAM_END) {
321 git_inflate_end(&st->z);
322 st->z_state = z_done;
323 break;
324 }
325
326 /*
327 * Unlike the loose object case, we do not have to worry here
328 * about running out of input bytes and spinning infinitely. If
329 * we get Z_BUF_ERROR due to too few input bytes, then we'll
330 * replenish them in the next use_pack() call when we loop. If
331 * we truly hit the end of the pack (i.e., because it's corrupt
332 * or truncated), then use_pack() catches that and will die().
333 */
334 if (status != Z_OK && status != Z_BUF_ERROR) {
335 git_inflate_end(&st->z);
336 st->z_state = z_error;
337 return -1;
338 }
339 }
340 return total_read;
341 }
342
343 static close_method_decl(pack_non_delta)
344 {
345 close_deflated_stream(st);
346 return 0;
347 }
348
349 static struct stream_vtbl pack_non_delta_vtbl = {
350 close_istream_pack_non_delta,
351 read_istream_pack_non_delta,
352 };
353
354 static open_method_decl(pack_non_delta)
355 {
356 struct pack_window *window;
357 enum object_type in_pack_type;
358
359 st->u.in_pack.pack = oi->u.packed.pack;
360 st->u.in_pack.pos = oi->u.packed.offset;
361 window = NULL;
362
363 in_pack_type = unpack_object_header(st->u.in_pack.pack,
364 &window,
365 &st->u.in_pack.pos,
366 &st->size);
367 unuse_pack(&window);
368 switch (in_pack_type) {
369 default:
370 return -1; /* we do not do deltas for now */
371 case OBJ_COMMIT:
372 case OBJ_TREE:
373 case OBJ_BLOB:
374 case OBJ_TAG:
375 break;
376 }
377 st->z_state = z_unused;
378 st->vtbl = &pack_non_delta_vtbl;
379 return 0;
380 }
381
382
383 /*****************************************************************
384 *
385 * In-core stream
386 *
387 *****************************************************************/
388
389 static close_method_decl(incore)
390 {
391 free(st->u.incore.buf);
392 return 0;
393 }
394
395 static read_method_decl(incore)
396 {
397 size_t read_size = sz;
398 size_t remainder = st->size - st->u.incore.read_ptr;
399
400 if (remainder <= read_size)
401 read_size = remainder;
402 if (read_size) {
403 memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
404 st->u.incore.read_ptr += read_size;
405 }
406 return read_size;
407 }
408
409 static struct stream_vtbl incore_vtbl = {
410 close_istream_incore,
411 read_istream_incore,
412 };
413
414 static open_method_decl(incore)
415 {
416 st->u.incore.buf = read_object_file_extended(r, oid, type, &st->size, 0);
417 st->u.incore.read_ptr = 0;
418 st->vtbl = &incore_vtbl;
419
420 return st->u.incore.buf ? 0 : -1;
421 }
422
423 /*****************************************************************************
424 * static helpers variables and functions for users of streaming interface
425 *****************************************************************************/
426
427 static open_istream_fn open_istream_tbl[] = {
428 open_istream_incore,
429 open_istream_loose,
430 open_istream_pack_non_delta,
431 };
432
433 static enum input_source istream_source(struct repository *r,
434 const struct object_id *oid,
435 enum object_type *type,
436 struct object_info *oi)
437 {
438 unsigned long size;
439 int status;
440
441 oi->typep = type;
442 oi->sizep = &size;
443 status = oid_object_info_extended(r, oid, oi, 0);
444 if (status < 0)
445 return stream_error;
446
447 switch (oi->whence) {
448 case OI_LOOSE:
449 return loose;
450 case OI_PACKED:
451 if (!oi->u.packed.is_delta && big_file_threshold < size)
452 return pack_non_delta;
453 /* fallthru */
454 default:
455 return incore;
456 }
457 }
458
459
460 /****************************************************************
461 * Users of streaming interface
462 ****************************************************************/
463
464 int close_istream(struct git_istream *st)
465 {
466 int r = st->vtbl->close(st);
467 free(st);
468 return r;
469 }
470
471 ssize_t read_istream(struct git_istream *st, void *buf, size_t sz)
472 {
473 return st->vtbl->read(st, buf, sz);
474 }
475
476 struct git_istream *open_istream(struct repository *r,
477 const struct object_id *oid,
478 enum object_type *type,
479 unsigned long *size,
480 struct stream_filter *filter)
481 {
482 struct git_istream *st;
483 struct object_info oi = OBJECT_INFO_INIT;
484 const struct object_id *real = lookup_replace_object(r, oid);
485 enum input_source src = istream_source(r, real, type, &oi);
486
487 if (src < 0)
488 return NULL;
489
490 st = xmalloc(sizeof(*st));
491 if (open_istream_tbl[src](st, r, &oi, real, type)) {
492 if (open_istream_incore(st, r, &oi, real, type)) {
493 free(st);
494 return NULL;
495 }
496 }
497 if (filter) {
498 /* Add "&& !is_null_stream_filter(filter)" for performance */
499 struct git_istream *nst = attach_stream_filter(st, filter);
500 if (!nst) {
501 close_istream(st);
502 return NULL;
503 }
504 st = nst;
505 }
506
507 *size = st->size;
508 return st;
509 }
510
511 int stream_blob_to_fd(int fd, const struct object_id *oid, struct stream_filter *filter,
512 int can_seek)
513 {
514 struct git_istream *st;
515 enum object_type type;
516 unsigned long sz;
517 ssize_t kept = 0;
518 int result = -1;
519
520 st = open_istream(the_repository, oid, &type, &sz, filter);
521 if (!st) {
522 if (filter)
523 free_stream_filter(filter);
524 return result;
525 }
526 if (type != OBJ_BLOB)
527 goto close_and_exit;
528 for (;;) {
529 char buf[1024 * 16];
530 ssize_t wrote, holeto;
531 ssize_t readlen = read_istream(st, buf, sizeof(buf));
532
533 if (readlen < 0)
534 goto close_and_exit;
535 if (!readlen)
536 break;
537 if (can_seek && sizeof(buf) == readlen) {
538 for (holeto = 0; holeto < readlen; holeto++)
539 if (buf[holeto])
540 break;
541 if (readlen == holeto) {
542 kept += holeto;
543 continue;
544 }
545 }
546
547 if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1)
548 goto close_and_exit;
549 else
550 kept = 0;
551 wrote = write_in_full(fd, buf, readlen);
552
553 if (wrote < 0)
554 goto close_and_exit;
555 }
556 if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
557 xwrite(fd, "", 1) != 1))
558 goto close_and_exit;
559 result = 0;
560
561 close_and_exit:
562 close_istream(st);
563 return result;
564 }