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