]>
Commit | Line | Data |
---|---|---|
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 JH |
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 object_info *, | |
575042a0 | 20 | const struct object_id *, |
46bf0438 JH |
21 | enum object_type *); |
22 | typedef int (*close_istream_fn)(struct git_istream *); | |
23 | typedef ssize_t (*read_istream_fn)(struct git_istream *, char *, size_t); | |
24 | ||
25 | struct stream_vtbl { | |
26 | close_istream_fn close; | |
27 | read_istream_fn read; | |
28 | }; | |
29 | ||
30 | #define open_method_decl(name) \ | |
31 | int open_istream_ ##name \ | |
32 | (struct git_istream *st, struct object_info *oi, \ | |
575042a0 | 33 | const struct object_id *oid, \ |
46bf0438 JH |
34 | enum object_type *type) |
35 | ||
36 | #define close_method_decl(name) \ | |
37 | int close_istream_ ##name \ | |
38 | (struct git_istream *st) | |
39 | ||
40 | #define read_method_decl(name) \ | |
41 | ssize_t read_istream_ ##name \ | |
42 | (struct git_istream *st, char *buf, size_t sz) | |
43 | ||
44 | /* forward declaration */ | |
45 | static open_method_decl(incore); | |
46 | static open_method_decl(loose); | |
47 | static open_method_decl(pack_non_delta); | |
b6691092 JH |
48 | static struct git_istream *attach_stream_filter(struct git_istream *st, |
49 | struct stream_filter *filter); | |
50 | ||
46bf0438 JH |
51 | |
52 | static open_istream_fn open_istream_tbl[] = { | |
53 | open_istream_incore, | |
54 | open_istream_loose, | |
55 | open_istream_pack_non_delta, | |
56 | }; | |
57 | ||
b6691092 JH |
58 | #define FILTER_BUFFER (1024*16) |
59 | ||
60 | struct filtered_istream { | |
61 | struct git_istream *upstream; | |
62 | struct stream_filter *filter; | |
63 | char ibuf[FILTER_BUFFER]; | |
64 | char obuf[FILTER_BUFFER]; | |
65 | int i_end, i_ptr; | |
66 | int o_end, o_ptr; | |
4ae66704 | 67 | int input_finished; |
b6691092 JH |
68 | }; |
69 | ||
46bf0438 JH |
70 | struct git_istream { |
71 | const struct stream_vtbl *vtbl; | |
72 | unsigned long size; /* inflated size of full object */ | |
eb4f4076 | 73 | git_zstream z; |
7ef2d9a2 | 74 | enum { z_unused, z_used, z_done, z_error } z_state; |
46bf0438 JH |
75 | |
76 | union { | |
77 | struct { | |
78 | char *buf; /* from read_object() */ | |
79 | unsigned long read_ptr; | |
80 | } incore; | |
81 | ||
82 | struct { | |
93aa7bd5 JH |
83 | void *mapped; |
84 | unsigned long mapsize; | |
85 | char hdr[32]; | |
86 | int hdr_avail; | |
87 | int hdr_used; | |
46bf0438 JH |
88 | } loose; |
89 | ||
90 | struct { | |
7ef2d9a2 JH |
91 | struct packed_git *pack; |
92 | off_t pos; | |
46bf0438 | 93 | } in_pack; |
b6691092 JH |
94 | |
95 | struct filtered_istream filtered; | |
46bf0438 JH |
96 | } u; |
97 | }; | |
98 | ||
99 | int close_istream(struct git_istream *st) | |
100 | { | |
95dea6eb JK |
101 | int r = st->vtbl->close(st); |
102 | free(st); | |
103 | return r; | |
46bf0438 JH |
104 | } |
105 | ||
6163cd8a | 106 | ssize_t read_istream(struct git_istream *st, void *buf, size_t sz) |
46bf0438 JH |
107 | { |
108 | return st->vtbl->read(st, buf, sz); | |
109 | } | |
110 | ||
b383a13c | 111 | static enum input_source istream_source(const struct object_id *oid, |
46bf0438 JH |
112 | enum object_type *type, |
113 | struct object_info *oi) | |
114 | { | |
115 | unsigned long size; | |
116 | int status; | |
117 | ||
5b086407 | 118 | oi->typep = type; |
46bf0438 | 119 | oi->sizep = &size; |
7ecd8690 | 120 | status = oid_object_info_extended(the_repository, oid, oi, 0); |
46bf0438 JH |
121 | if (status < 0) |
122 | return stream_error; | |
46bf0438 JH |
123 | |
124 | switch (oi->whence) { | |
125 | case OI_LOOSE: | |
126 | return loose; | |
127 | case OI_PACKED: | |
754980d0 | 128 | if (!oi->u.packed.is_delta && big_file_threshold < size) |
46bf0438 JH |
129 | return pack_non_delta; |
130 | /* fallthru */ | |
131 | default: | |
132 | return incore; | |
133 | } | |
134 | } | |
135 | ||
ef7b5195 | 136 | struct git_istream *open_istream(const struct object_id *oid, |
46bf0438 | 137 | enum object_type *type, |
b6691092 JH |
138 | unsigned long *size, |
139 | struct stream_filter *filter) | |
46bf0438 JH |
140 | { |
141 | struct git_istream *st; | |
27b5c1a0 | 142 | struct object_info oi = OBJECT_INFO_INIT; |
1f2e7cea | 143 | const struct object_id *real = lookup_replace_object(the_repository, oid); |
46bf0438 JH |
144 | enum input_source src = istream_source(real, type, &oi); |
145 | ||
146 | if (src < 0) | |
147 | return NULL; | |
148 | ||
149 | st = xmalloc(sizeof(*st)); | |
b383a13c | 150 | if (open_istream_tbl[src](st, &oi, real, type)) { |
151 | if (open_istream_incore(st, &oi, real, type)) { | |
46bf0438 JH |
152 | free(st); |
153 | return NULL; | |
154 | } | |
155 | } | |
6a907786 | 156 | if (filter) { |
b6691092 JH |
157 | /* Add "&& !is_null_stream_filter(filter)" for performance */ |
158 | struct git_istream *nst = attach_stream_filter(st, filter); | |
78368f2c | 159 | if (!nst) { |
b6691092 | 160 | close_istream(st); |
78368f2c JH |
161 | return NULL; |
162 | } | |
b6691092 JH |
163 | st = nst; |
164 | } | |
165 | ||
46bf0438 JH |
166 | *size = st->size; |
167 | return st; | |
168 | } | |
169 | ||
7ef2d9a2 JH |
170 | |
171 | /***************************************************************** | |
172 | * | |
173 | * Common helpers | |
174 | * | |
175 | *****************************************************************/ | |
176 | ||
177 | static void close_deflated_stream(struct git_istream *st) | |
178 | { | |
179 | if (st->z_state == z_used) | |
180 | git_inflate_end(&st->z); | |
181 | } | |
182 | ||
183 | ||
b6691092 JH |
184 | /***************************************************************** |
185 | * | |
186 | * Filtered stream | |
187 | * | |
188 | *****************************************************************/ | |
189 | ||
190 | static close_method_decl(filtered) | |
191 | { | |
192 | free_stream_filter(st->u.filtered.filter); | |
193 | return close_istream(st->u.filtered.upstream); | |
194 | } | |
195 | ||
196 | static read_method_decl(filtered) | |
197 | { | |
198 | struct filtered_istream *fs = &(st->u.filtered); | |
199 | size_t filled = 0; | |
200 | ||
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; | |
212 | } | |
213 | fs->o_end = fs->o_ptr = 0; | |
214 | ||
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; | |
226 | } | |
4ae66704 JH |
227 | |
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; | |
239 | } | |
b6691092 JH |
240 | fs->i_end = fs->i_ptr = 0; |
241 | ||
242 | /* refill the input from the upstream */ | |
4ae66704 JH |
243 | if (!fs->input_finished) { |
244 | fs->i_end = read_istream(fs->upstream, fs->ibuf, FILTER_BUFFER); | |
245 | if (fs->i_end < 0) | |
42e7e2a5 | 246 | return -1; |
4ae66704 JH |
247 | if (fs->i_end) |
248 | continue; | |
249 | } | |
250 | fs->input_finished = 1; | |
b6691092 JH |
251 | } |
252 | return filled; | |
253 | } | |
254 | ||
255 | static struct stream_vtbl filtered_vtbl = { | |
256 | close_istream_filtered, | |
257 | read_istream_filtered, | |
258 | }; | |
259 | ||
260 | static struct git_istream *attach_stream_filter(struct git_istream *st, | |
261 | struct stream_filter *filter) | |
262 | { | |
263 | struct git_istream *ifs = xmalloc(sizeof(*ifs)); | |
264 | struct filtered_istream *fs = &(ifs->u.filtered); | |
265 | ||
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; | |
4ae66704 | 271 | fs->input_finished = 0; |
b6691092 JH |
272 | ifs->size = -1; /* unknown */ |
273 | return ifs; | |
274 | } | |
275 | ||
46bf0438 JH |
276 | /***************************************************************** |
277 | * | |
278 | * Loose object stream | |
279 | * | |
280 | *****************************************************************/ | |
281 | ||
93aa7bd5 JH |
282 | static read_method_decl(loose) |
283 | { | |
284 | size_t total_read = 0; | |
285 | ||
286 | switch (st->z_state) { | |
287 | case z_done: | |
288 | return 0; | |
289 | case z_error: | |
290 | return -1; | |
291 | default: | |
292 | break; | |
293 | } | |
294 | ||
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; | |
302 | } | |
303 | ||
304 | while (total_read < sz) { | |
305 | int status; | |
306 | ||
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); | |
310 | ||
311 | total_read = st->z.next_out - (unsigned char *)buf; | |
312 | ||
313 | if (status == Z_STREAM_END) { | |
314 | git_inflate_end(&st->z); | |
315 | st->z_state = z_done; | |
316 | break; | |
317 | } | |
692f0bc7 | 318 | if (status != Z_OK && (status != Z_BUF_ERROR || total_read < sz)) { |
93aa7bd5 JH |
319 | git_inflate_end(&st->z); |
320 | st->z_state = z_error; | |
321 | return -1; | |
322 | } | |
323 | } | |
324 | return total_read; | |
325 | } | |
326 | ||
327 | static close_method_decl(loose) | |
328 | { | |
329 | close_deflated_stream(st); | |
330 | munmap(st->u.loose.mapped, st->u.loose.mapsize); | |
331 | return 0; | |
332 | } | |
333 | ||
334 | static struct stream_vtbl loose_vtbl = { | |
335 | close_istream_loose, | |
336 | read_istream_loose, | |
337 | }; | |
338 | ||
46bf0438 JH |
339 | static open_method_decl(loose) |
340 | { | |
e35454fa | 341 | st->u.loose.mapped = map_sha1_file(the_repository, |
cf0b1793 | 342 | oid->hash, &st->u.loose.mapsize); |
93aa7bd5 JH |
343 | if (!st->u.loose.mapped) |
344 | return -1; | |
97026fe9 JH |
345 | if ((unpack_sha1_header(&st->z, |
346 | st->u.loose.mapped, | |
347 | st->u.loose.mapsize, | |
348 | st->u.loose.hdr, | |
349 | sizeof(st->u.loose.hdr)) < 0) || | |
350 | (parse_sha1_header(st->u.loose.hdr, &st->size) < 0)) { | |
93aa7bd5 JH |
351 | git_inflate_end(&st->z); |
352 | munmap(st->u.loose.mapped, st->u.loose.mapsize); | |
353 | return -1; | |
354 | } | |
355 | ||
93aa7bd5 JH |
356 | st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1; |
357 | st->u.loose.hdr_avail = st->z.total_out; | |
358 | st->z_state = z_used; | |
359 | ||
360 | st->vtbl = &loose_vtbl; | |
361 | return 0; | |
46bf0438 JH |
362 | } |
363 | ||
364 | ||
365 | /***************************************************************** | |
366 | * | |
367 | * Non-delta packed object stream | |
368 | * | |
369 | *****************************************************************/ | |
370 | ||
7ef2d9a2 JH |
371 | static read_method_decl(pack_non_delta) |
372 | { | |
373 | size_t total_read = 0; | |
374 | ||
375 | switch (st->z_state) { | |
376 | case z_unused: | |
377 | memset(&st->z, 0, sizeof(st->z)); | |
378 | git_inflate_init(&st->z); | |
379 | st->z_state = z_used; | |
380 | break; | |
381 | case z_done: | |
382 | return 0; | |
383 | case z_error: | |
384 | return -1; | |
385 | case z_used: | |
386 | break; | |
387 | } | |
388 | ||
389 | while (total_read < sz) { | |
390 | int status; | |
391 | struct pack_window *window = NULL; | |
392 | unsigned char *mapped; | |
393 | ||
394 | mapped = use_pack(st->u.in_pack.pack, &window, | |
395 | st->u.in_pack.pos, &st->z.avail_in); | |
396 | ||
397 | st->z.next_out = (unsigned char *)buf + total_read; | |
398 | st->z.avail_out = sz - total_read; | |
399 | st->z.next_in = mapped; | |
400 | status = git_inflate(&st->z, Z_FINISH); | |
401 | ||
402 | st->u.in_pack.pos += st->z.next_in - mapped; | |
403 | total_read = st->z.next_out - (unsigned char *)buf; | |
404 | unuse_pack(&window); | |
405 | ||
406 | if (status == Z_STREAM_END) { | |
407 | git_inflate_end(&st->z); | |
408 | st->z_state = z_done; | |
409 | break; | |
410 | } | |
411 | if (status != Z_OK && status != Z_BUF_ERROR) { | |
412 | git_inflate_end(&st->z); | |
413 | st->z_state = z_error; | |
414 | return -1; | |
415 | } | |
416 | } | |
417 | return total_read; | |
418 | } | |
419 | ||
420 | static close_method_decl(pack_non_delta) | |
421 | { | |
422 | close_deflated_stream(st); | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static struct stream_vtbl pack_non_delta_vtbl = { | |
427 | close_istream_pack_non_delta, | |
428 | read_istream_pack_non_delta, | |
429 | }; | |
430 | ||
46bf0438 JH |
431 | static open_method_decl(pack_non_delta) |
432 | { | |
7ef2d9a2 JH |
433 | struct pack_window *window; |
434 | enum object_type in_pack_type; | |
435 | ||
436 | st->u.in_pack.pack = oi->u.packed.pack; | |
437 | st->u.in_pack.pos = oi->u.packed.offset; | |
438 | window = NULL; | |
439 | ||
440 | in_pack_type = unpack_object_header(st->u.in_pack.pack, | |
441 | &window, | |
442 | &st->u.in_pack.pos, | |
443 | &st->size); | |
444 | unuse_pack(&window); | |
445 | switch (in_pack_type) { | |
446 | default: | |
447 | return -1; /* we do not do deltas for now */ | |
448 | case OBJ_COMMIT: | |
449 | case OBJ_TREE: | |
450 | case OBJ_BLOB: | |
451 | case OBJ_TAG: | |
452 | break; | |
453 | } | |
454 | st->z_state = z_unused; | |
455 | st->vtbl = &pack_non_delta_vtbl; | |
456 | return 0; | |
46bf0438 JH |
457 | } |
458 | ||
459 | ||
460 | /***************************************************************** | |
461 | * | |
462 | * In-core stream | |
463 | * | |
464 | *****************************************************************/ | |
465 | ||
466 | static close_method_decl(incore) | |
467 | { | |
468 | free(st->u.incore.buf); | |
469 | return 0; | |
470 | } | |
471 | ||
472 | static read_method_decl(incore) | |
473 | { | |
474 | size_t read_size = sz; | |
475 | size_t remainder = st->size - st->u.incore.read_ptr; | |
476 | ||
477 | if (remainder <= read_size) | |
478 | read_size = remainder; | |
479 | if (read_size) { | |
480 | memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size); | |
481 | st->u.incore.read_ptr += read_size; | |
482 | } | |
483 | return read_size; | |
484 | } | |
485 | ||
486 | static struct stream_vtbl incore_vtbl = { | |
487 | close_istream_incore, | |
488 | read_istream_incore, | |
489 | }; | |
490 | ||
491 | static open_method_decl(incore) | |
492 | { | |
b4f5aca4 | 493 | st->u.incore.buf = read_object_file_extended(oid, type, &st->size, 0); |
46bf0438 JH |
494 | st->u.incore.read_ptr = 0; |
495 | st->vtbl = &incore_vtbl; | |
496 | ||
497 | return st->u.incore.buf ? 0 : -1; | |
498 | } | |
47a02ff2 JH |
499 | |
500 | ||
501 | /**************************************************************** | |
502 | * Users of streaming interface | |
503 | ****************************************************************/ | |
504 | ||
7eda0e4f | 505 | int stream_blob_to_fd(int fd, const struct object_id *oid, struct stream_filter *filter, |
47a02ff2 JH |
506 | int can_seek) |
507 | { | |
508 | struct git_istream *st; | |
509 | enum object_type type; | |
510 | unsigned long sz; | |
511 | ssize_t kept = 0; | |
512 | int result = -1; | |
513 | ||
ef7b5195 | 514 | st = open_istream(oid, &type, &sz, filter); |
9ce4ad3e JK |
515 | if (!st) { |
516 | if (filter) | |
517 | free_stream_filter(filter); | |
47a02ff2 | 518 | return result; |
9ce4ad3e | 519 | } |
47a02ff2 JH |
520 | if (type != OBJ_BLOB) |
521 | goto close_and_exit; | |
522 | for (;;) { | |
523 | char buf[1024 * 16]; | |
524 | ssize_t wrote, holeto; | |
525 | ssize_t readlen = read_istream(st, buf, sizeof(buf)); | |
526 | ||
45d4bdae JK |
527 | if (readlen < 0) |
528 | goto close_and_exit; | |
47a02ff2 JH |
529 | if (!readlen) |
530 | break; | |
531 | if (can_seek && sizeof(buf) == readlen) { | |
532 | for (holeto = 0; holeto < readlen; holeto++) | |
533 | if (buf[holeto]) | |
534 | break; | |
535 | if (readlen == holeto) { | |
536 | kept += holeto; | |
537 | continue; | |
538 | } | |
539 | } | |
540 | ||
541 | if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1) | |
542 | goto close_and_exit; | |
543 | else | |
544 | kept = 0; | |
545 | wrote = write_in_full(fd, buf, readlen); | |
546 | ||
564bde9a | 547 | if (wrote < 0) |
47a02ff2 JH |
548 | goto close_and_exit; |
549 | } | |
550 | if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 || | |
7edc02f4 | 551 | xwrite(fd, "", 1) != 1)) |
47a02ff2 JH |
552 | goto close_and_exit; |
553 | result = 0; | |
554 | ||
555 | close_and_exit: | |
556 | close_istream(st); | |
557 | return result; | |
558 | } |