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