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