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.

282 lines
7.3KB

  1. /*
  2. * Copyright (c) 2011, Google Inc.
  3. */
  4. #include "cache.h"
  5. #include "bulk-checkin.h"
  6. #include "repository.h"
  7. #include "csum-file.h"
  8. #include "pack.h"
  9. #include "strbuf.h"
  10. #include "packfile.h"
  11. #include "object-store.h"
  12. static struct bulk_checkin_state {
  13. unsigned plugged:1;
  14. char *pack_tmp_name;
  15. struct hashfile *f;
  16. off_t offset;
  17. struct pack_idx_option pack_idx_opts;
  18. struct pack_idx_entry **written;
  19. uint32_t alloc_written;
  20. uint32_t nr_written;
  21. } state;
  22. static void finish_bulk_checkin(struct bulk_checkin_state *state)
  23. {
  24. struct object_id oid;
  25. struct strbuf packname = STRBUF_INIT;
  26. int i;
  27. if (!state->f)
  28. return;
  29. if (state->nr_written == 0) {
  30. close(state->f->fd);
  31. unlink(state->pack_tmp_name);
  32. goto clear_exit;
  33. } else if (state->nr_written == 1) {
  34. finalize_hashfile(state->f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
  35. } else {
  36. int fd = finalize_hashfile(state->f, oid.hash, 0);
  37. fixup_pack_header_footer(fd, oid.hash, state->pack_tmp_name,
  38. state->nr_written, oid.hash,
  39. state->offset);
  40. close(fd);
  41. }
  42. strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
  43. finish_tmp_packfile(&packname, state->pack_tmp_name,
  44. state->written, state->nr_written,
  45. &state->pack_idx_opts, oid.hash);
  46. for (i = 0; i < state->nr_written; i++)
  47. free(state->written[i]);
  48. clear_exit:
  49. free(state->written);
  50. memset(state, 0, sizeof(*state));
  51. strbuf_release(&packname);
  52. /* Make objects we just wrote available to ourselves */
  53. reprepare_packed_git(the_repository);
  54. }
  55. static int already_written(struct bulk_checkin_state *state, struct object_id *oid)
  56. {
  57. int i;
  58. /* The object may already exist in the repository */
  59. if (has_object_file(oid))
  60. return 1;
  61. /* Might want to keep the list sorted */
  62. for (i = 0; i < state->nr_written; i++)
  63. if (oideq(&state->written[i]->oid, oid))
  64. return 1;
  65. /* This is a new object we need to keep */
  66. return 0;
  67. }
  68. /*
  69. * Read the contents from fd for size bytes, streaming it to the
  70. * packfile in state while updating the hash in ctx. Signal a failure
  71. * by returning a negative value when the resulting pack would exceed
  72. * the pack size limit and this is not the first object in the pack,
  73. * so that the caller can discard what we wrote from the current pack
  74. * by truncating it and opening a new one. The caller will then call
  75. * us again after rewinding the input fd.
  76. *
  77. * The already_hashed_to pointer is kept untouched by the caller to
  78. * make sure we do not hash the same byte when we are called
  79. * again. This way, the caller does not have to checkpoint its hash
  80. * status before calling us just in case we ask it to call us again
  81. * with a new pack.
  82. */
  83. static int stream_to_pack(struct bulk_checkin_state *state,
  84. git_hash_ctx *ctx, off_t *already_hashed_to,
  85. int fd, size_t size, enum object_type type,
  86. const char *path, unsigned flags)
  87. {
  88. git_zstream s;
  89. unsigned char obuf[16384];
  90. unsigned hdrlen;
  91. int status = Z_OK;
  92. int write_object = (flags & HASH_WRITE_OBJECT);
  93. off_t offset = 0;
  94. git_deflate_init(&s, pack_compression_level);
  95. hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), type, size);
  96. s.next_out = obuf + hdrlen;
  97. s.avail_out = sizeof(obuf) - hdrlen;
  98. while (status != Z_STREAM_END) {
  99. unsigned char ibuf[16384];
  100. if (size && !s.avail_in) {
  101. ssize_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
  102. ssize_t read_result = read_in_full(fd, ibuf, rsize);
  103. if (read_result < 0)
  104. die_errno("failed to read from '%s'", path);
  105. if (read_result != rsize)
  106. die("failed to read %d bytes from '%s'",
  107. (int)rsize, path);
  108. offset += rsize;
  109. if (*already_hashed_to < offset) {
  110. size_t hsize = offset - *already_hashed_to;
  111. if (rsize < hsize)
  112. hsize = rsize;
  113. if (hsize)
  114. the_hash_algo->update_fn(ctx, ibuf, hsize);
  115. *already_hashed_to = offset;
  116. }
  117. s.next_in = ibuf;
  118. s.avail_in = rsize;
  119. size -= rsize;
  120. }
  121. status = git_deflate(&s, size ? 0 : Z_FINISH);
  122. if (!s.avail_out || status == Z_STREAM_END) {
  123. if (write_object) {
  124. size_t written = s.next_out - obuf;
  125. /* would we bust the size limit? */
  126. if (state->nr_written &&
  127. pack_size_limit_cfg &&
  128. pack_size_limit_cfg < state->offset + written) {
  129. git_deflate_abort(&s);
  130. return -1;
  131. }
  132. hashwrite(state->f, obuf, written);
  133. state->offset += written;
  134. }
  135. s.next_out = obuf;
  136. s.avail_out = sizeof(obuf);
  137. }
  138. switch (status) {
  139. case Z_OK:
  140. case Z_BUF_ERROR:
  141. case Z_STREAM_END:
  142. continue;
  143. default:
  144. die("unexpected deflate failure: %d", status);
  145. }
  146. }
  147. git_deflate_end(&s);
  148. return 0;
  149. }
  150. /* Lazily create backing packfile for the state */
  151. static void prepare_to_stream(struct bulk_checkin_state *state,
  152. unsigned flags)
  153. {
  154. if (!(flags & HASH_WRITE_OBJECT) || state->f)
  155. return;
  156. state->f = create_tmp_packfile(&state->pack_tmp_name);
  157. reset_pack_idx_option(&state->pack_idx_opts);
  158. /* Pretend we are going to write only one object */
  159. state->offset = write_pack_header(state->f, 1);
  160. if (!state->offset)
  161. die_errno("unable to write pack header");
  162. }
  163. static int deflate_to_pack(struct bulk_checkin_state *state,
  164. struct object_id *result_oid,
  165. int fd, size_t size,
  166. enum object_type type, const char *path,
  167. unsigned flags)
  168. {
  169. off_t seekback, already_hashed_to;
  170. git_hash_ctx ctx;
  171. unsigned char obuf[16384];
  172. unsigned header_len;
  173. struct hashfile_checkpoint checkpoint = {0};
  174. struct pack_idx_entry *idx = NULL;
  175. seekback = lseek(fd, 0, SEEK_CUR);
  176. if (seekback == (off_t) -1)
  177. return error("cannot find the current offset");
  178. header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
  179. type_name(type), (uintmax_t)size) + 1;
  180. the_hash_algo->init_fn(&ctx);
  181. the_hash_algo->update_fn(&ctx, obuf, header_len);
  182. /* Note: idx is non-NULL when we are writing */
  183. if ((flags & HASH_WRITE_OBJECT) != 0)
  184. idx = xcalloc(1, sizeof(*idx));
  185. already_hashed_to = 0;
  186. while (1) {
  187. prepare_to_stream(state, flags);
  188. if (idx) {
  189. hashfile_checkpoint(state->f, &checkpoint);
  190. idx->offset = state->offset;
  191. crc32_begin(state->f);
  192. }
  193. if (!stream_to_pack(state, &ctx, &already_hashed_to,
  194. fd, size, type, path, flags))
  195. break;
  196. /*
  197. * Writing this object to the current pack will make
  198. * it too big; we need to truncate it, start a new
  199. * pack, and write into it.
  200. */
  201. if (!idx)
  202. BUG("should not happen");
  203. hashfile_truncate(state->f, &checkpoint);
  204. state->offset = checkpoint.offset;
  205. finish_bulk_checkin(state);
  206. if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
  207. return error("cannot seek back");
  208. }
  209. the_hash_algo->final_fn(result_oid->hash, &ctx);
  210. if (!idx)
  211. return 0;
  212. idx->crc32 = crc32_end(state->f);
  213. if (already_written(state, result_oid)) {
  214. hashfile_truncate(state->f, &checkpoint);
  215. state->offset = checkpoint.offset;
  216. free(idx);
  217. } else {
  218. oidcpy(&idx->oid, result_oid);
  219. ALLOC_GROW(state->written,
  220. state->nr_written + 1,
  221. state->alloc_written);
  222. state->written[state->nr_written++] = idx;
  223. }
  224. return 0;
  225. }
  226. int index_bulk_checkin(struct object_id *oid,
  227. int fd, size_t size, enum object_type type,
  228. const char *path, unsigned flags)
  229. {
  230. int status = deflate_to_pack(&state, oid, fd, size, type,
  231. path, flags);
  232. if (!state.plugged)
  233. finish_bulk_checkin(&state);
  234. return status;
  235. }
  236. void plug_bulk_checkin(void)
  237. {
  238. state.plugged = 1;
  239. }
  240. void unplug_bulk_checkin(void)
  241. {
  242. state.plugged = 0;
  243. if (state.f)
  244. finish_bulk_checkin(&state);
  245. }