THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

568 lines
12KB

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