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.

1421 lines
36KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "csum-file.h"
  4. #include "dir.h"
  5. #include "lockfile.h"
  6. #include "packfile.h"
  7. #include "object-store.h"
  8. #include "sha1-lookup.h"
  9. #include "midx.h"
  10. #include "progress.h"
  11. #include "trace2.h"
  12. #include "run-command.h"
  13. #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
  14. #define MIDX_VERSION 1
  15. #define MIDX_BYTE_FILE_VERSION 4
  16. #define MIDX_BYTE_HASH_VERSION 5
  17. #define MIDX_BYTE_NUM_CHUNKS 6
  18. #define MIDX_BYTE_NUM_PACKS 8
  19. #define MIDX_HASH_VERSION 1
  20. #define MIDX_HEADER_SIZE 12
  21. #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
  22. #define MIDX_MAX_CHUNKS 5
  23. #define MIDX_CHUNK_ALIGNMENT 4
  24. #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
  25. #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
  26. #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
  27. #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
  28. #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
  29. #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
  30. #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
  31. #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
  32. #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
  33. #define MIDX_LARGE_OFFSET_NEEDED 0x80000000
  34. #define PACK_EXPIRED UINT_MAX
  35. static char *get_midx_filename(const char *object_dir)
  36. {
  37. return xstrfmt("%s/pack/multi-pack-index", object_dir);
  38. }
  39. struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
  40. {
  41. struct multi_pack_index *m = NULL;
  42. int fd;
  43. struct stat st;
  44. size_t midx_size;
  45. void *midx_map = NULL;
  46. uint32_t hash_version;
  47. char *midx_name = get_midx_filename(object_dir);
  48. uint32_t i;
  49. const char *cur_pack_name;
  50. fd = git_open(midx_name);
  51. if (fd < 0)
  52. goto cleanup_fail;
  53. if (fstat(fd, &st)) {
  54. error_errno(_("failed to read %s"), midx_name);
  55. goto cleanup_fail;
  56. }
  57. midx_size = xsize_t(st.st_size);
  58. if (midx_size < MIDX_MIN_SIZE) {
  59. error(_("multi-pack-index file %s is too small"), midx_name);
  60. goto cleanup_fail;
  61. }
  62. FREE_AND_NULL(midx_name);
  63. midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
  64. FLEX_ALLOC_STR(m, object_dir, object_dir);
  65. m->fd = fd;
  66. m->data = midx_map;
  67. m->data_len = midx_size;
  68. m->local = local;
  69. m->signature = get_be32(m->data);
  70. if (m->signature != MIDX_SIGNATURE)
  71. die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
  72. m->signature, MIDX_SIGNATURE);
  73. m->version = m->data[MIDX_BYTE_FILE_VERSION];
  74. if (m->version != MIDX_VERSION)
  75. die(_("multi-pack-index version %d not recognized"),
  76. m->version);
  77. hash_version = m->data[MIDX_BYTE_HASH_VERSION];
  78. if (hash_version != MIDX_HASH_VERSION)
  79. die(_("hash version %u does not match"), hash_version);
  80. m->hash_len = the_hash_algo->rawsz;
  81. m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
  82. m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
  83. for (i = 0; i < m->num_chunks; i++) {
  84. uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE +
  85. MIDX_CHUNKLOOKUP_WIDTH * i);
  86. uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 +
  87. MIDX_CHUNKLOOKUP_WIDTH * i);
  88. if (chunk_offset >= m->data_len)
  89. die(_("invalid chunk offset (too large)"));
  90. switch (chunk_id) {
  91. case MIDX_CHUNKID_PACKNAMES:
  92. m->chunk_pack_names = m->data + chunk_offset;
  93. break;
  94. case MIDX_CHUNKID_OIDFANOUT:
  95. m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset);
  96. break;
  97. case MIDX_CHUNKID_OIDLOOKUP:
  98. m->chunk_oid_lookup = m->data + chunk_offset;
  99. break;
  100. case MIDX_CHUNKID_OBJECTOFFSETS:
  101. m->chunk_object_offsets = m->data + chunk_offset;
  102. break;
  103. case MIDX_CHUNKID_LARGEOFFSETS:
  104. m->chunk_large_offsets = m->data + chunk_offset;
  105. break;
  106. case 0:
  107. die(_("terminating multi-pack-index chunk id appears earlier than expected"));
  108. break;
  109. default:
  110. /*
  111. * Do nothing on unrecognized chunks, allowing future
  112. * extensions to add optional chunks.
  113. */
  114. break;
  115. }
  116. }
  117. if (!m->chunk_pack_names)
  118. die(_("multi-pack-index missing required pack-name chunk"));
  119. if (!m->chunk_oid_fanout)
  120. die(_("multi-pack-index missing required OID fanout chunk"));
  121. if (!m->chunk_oid_lookup)
  122. die(_("multi-pack-index missing required OID lookup chunk"));
  123. if (!m->chunk_object_offsets)
  124. die(_("multi-pack-index missing required object offsets chunk"));
  125. m->num_objects = ntohl(m->chunk_oid_fanout[255]);
  126. m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
  127. m->packs = xcalloc(m->num_packs, sizeof(*m->packs));
  128. cur_pack_name = (const char *)m->chunk_pack_names;
  129. for (i = 0; i < m->num_packs; i++) {
  130. m->pack_names[i] = cur_pack_name;
  131. cur_pack_name += strlen(cur_pack_name) + 1;
  132. if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
  133. die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
  134. m->pack_names[i - 1],
  135. m->pack_names[i]);
  136. }
  137. trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
  138. trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
  139. return m;
  140. cleanup_fail:
  141. free(m);
  142. free(midx_name);
  143. if (midx_map)
  144. munmap(midx_map, midx_size);
  145. if (0 <= fd)
  146. close(fd);
  147. return NULL;
  148. }
  149. void close_midx(struct multi_pack_index *m)
  150. {
  151. uint32_t i;
  152. if (!m)
  153. return;
  154. munmap((unsigned char *)m->data, m->data_len);
  155. close(m->fd);
  156. m->fd = -1;
  157. for (i = 0; i < m->num_packs; i++) {
  158. if (m->packs[i])
  159. m->packs[i]->multi_pack_index = 0;
  160. }
  161. FREE_AND_NULL(m->packs);
  162. FREE_AND_NULL(m->pack_names);
  163. }
  164. int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
  165. {
  166. struct strbuf pack_name = STRBUF_INIT;
  167. struct packed_git *p;
  168. if (pack_int_id >= m->num_packs)
  169. die(_("bad pack-int-id: %u (%u total packs)"),
  170. pack_int_id, m->num_packs);
  171. if (m->packs[pack_int_id])
  172. return 0;
  173. strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
  174. m->pack_names[pack_int_id]);
  175. p = add_packed_git(pack_name.buf, pack_name.len, m->local);
  176. strbuf_release(&pack_name);
  177. if (!p)
  178. return 1;
  179. p->multi_pack_index = 1;
  180. m->packs[pack_int_id] = p;
  181. install_packed_git(r, p);
  182. list_add_tail(&p->mru, &r->objects->packed_git_mru);
  183. return 0;
  184. }
  185. int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
  186. {
  187. return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
  188. the_hash_algo->rawsz, result);
  189. }
  190. struct object_id *nth_midxed_object_oid(struct object_id *oid,
  191. struct multi_pack_index *m,
  192. uint32_t n)
  193. {
  194. if (n >= m->num_objects)
  195. return NULL;
  196. hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n);
  197. return oid;
  198. }
  199. static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
  200. {
  201. const unsigned char *offset_data;
  202. uint32_t offset32;
  203. offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH;
  204. offset32 = get_be32(offset_data + sizeof(uint32_t));
  205. if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
  206. if (sizeof(off_t) < sizeof(uint64_t))
  207. die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
  208. offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
  209. return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
  210. }
  211. return offset32;
  212. }
  213. static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
  214. {
  215. return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH);
  216. }
  217. static int nth_midxed_pack_entry(struct repository *r,
  218. struct multi_pack_index *m,
  219. struct pack_entry *e,
  220. uint32_t pos)
  221. {
  222. uint32_t pack_int_id;
  223. struct packed_git *p;
  224. if (pos >= m->num_objects)
  225. return 0;
  226. pack_int_id = nth_midxed_pack_int_id(m, pos);
  227. if (prepare_midx_pack(r, m, pack_int_id))
  228. die(_("error preparing packfile from multi-pack-index"));
  229. p = m->packs[pack_int_id];
  230. /*
  231. * We are about to tell the caller where they can locate the
  232. * requested object. We better make sure the packfile is
  233. * still here and can be accessed before supplying that
  234. * answer, as it may have been deleted since the MIDX was
  235. * loaded!
  236. */
  237. if (!is_pack_valid(p))
  238. return 0;
  239. if (p->num_bad_objects) {
  240. uint32_t i;
  241. struct object_id oid;
  242. nth_midxed_object_oid(&oid, m, pos);
  243. for (i = 0; i < p->num_bad_objects; i++)
  244. if (hasheq(oid.hash,
  245. p->bad_object_sha1 + the_hash_algo->rawsz * i))
  246. return 0;
  247. }
  248. e->offset = nth_midxed_offset(m, pos);
  249. e->p = p;
  250. return 1;
  251. }
  252. int fill_midx_entry(struct repository * r,
  253. const struct object_id *oid,
  254. struct pack_entry *e,
  255. struct multi_pack_index *m)
  256. {
  257. uint32_t pos;
  258. if (!bsearch_midx(oid, m, &pos))
  259. return 0;
  260. return nth_midxed_pack_entry(r, m, e, pos);
  261. }
  262. /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
  263. static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
  264. const char *idx_name)
  265. {
  266. /* Skip past any initial matching prefix. */
  267. while (*idx_name && *idx_name == *idx_or_pack_name) {
  268. idx_name++;
  269. idx_or_pack_name++;
  270. }
  271. /*
  272. * If we didn't match completely, we may have matched "pack-1234." and
  273. * be left with "idx" and "pack" respectively, which is also OK. We do
  274. * not have to check for "idx" and "idx", because that would have been
  275. * a complete match (and in that case these strcmps will be false, but
  276. * we'll correctly return 0 from the final strcmp() below.
  277. *
  278. * Technically this matches "fooidx" and "foopack", but we'd never have
  279. * such names in the first place.
  280. */
  281. if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
  282. return 0;
  283. /*
  284. * This not only checks for a complete match, but also orders based on
  285. * the first non-identical character, which means our ordering will
  286. * match a raw strcmp(). That makes it OK to use this to binary search
  287. * a naively-sorted list.
  288. */
  289. return strcmp(idx_or_pack_name, idx_name);
  290. }
  291. int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
  292. {
  293. uint32_t first = 0, last = m->num_packs;
  294. while (first < last) {
  295. uint32_t mid = first + (last - first) / 2;
  296. const char *current;
  297. int cmp;
  298. current = m->pack_names[mid];
  299. cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
  300. if (!cmp)
  301. return 1;
  302. if (cmp > 0) {
  303. first = mid + 1;
  304. continue;
  305. }
  306. last = mid;
  307. }
  308. return 0;
  309. }
  310. int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
  311. {
  312. struct multi_pack_index *m;
  313. struct multi_pack_index *m_search;
  314. int config_value;
  315. static int env_value = -1;
  316. if (env_value < 0)
  317. env_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);
  318. if (!env_value &&
  319. (repo_config_get_bool(r, "core.multipackindex", &config_value) ||
  320. !config_value))
  321. return 0;
  322. for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
  323. if (!strcmp(object_dir, m_search->object_dir))
  324. return 1;
  325. m = load_multi_pack_index(object_dir, local);
  326. if (m) {
  327. m->next = r->objects->multi_pack_index;
  328. r->objects->multi_pack_index = m;
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static size_t write_midx_header(struct hashfile *f,
  334. unsigned char num_chunks,
  335. uint32_t num_packs)
  336. {
  337. unsigned char byte_values[4];
  338. hashwrite_be32(f, MIDX_SIGNATURE);
  339. byte_values[0] = MIDX_VERSION;
  340. byte_values[1] = MIDX_HASH_VERSION;
  341. byte_values[2] = num_chunks;
  342. byte_values[3] = 0; /* unused */
  343. hashwrite(f, byte_values, sizeof(byte_values));
  344. hashwrite_be32(f, num_packs);
  345. return MIDX_HEADER_SIZE;
  346. }
  347. struct pack_info {
  348. uint32_t orig_pack_int_id;
  349. char *pack_name;
  350. struct packed_git *p;
  351. unsigned expired : 1;
  352. };
  353. static int pack_info_compare(const void *_a, const void *_b)
  354. {
  355. struct pack_info *a = (struct pack_info *)_a;
  356. struct pack_info *b = (struct pack_info *)_b;
  357. return strcmp(a->pack_name, b->pack_name);
  358. }
  359. struct pack_list {
  360. struct pack_info *info;
  361. uint32_t nr;
  362. uint32_t alloc;
  363. struct multi_pack_index *m;
  364. struct progress *progress;
  365. unsigned pack_paths_checked;
  366. };
  367. static void add_pack_to_midx(const char *full_path, size_t full_path_len,
  368. const char *file_name, void *data)
  369. {
  370. struct pack_list *packs = (struct pack_list *)data;
  371. if (ends_with(file_name, ".idx")) {
  372. display_progress(packs->progress, ++packs->pack_paths_checked);
  373. if (packs->m && midx_contains_pack(packs->m, file_name))
  374. return;
  375. ALLOC_GROW(packs->info, packs->nr + 1, packs->alloc);
  376. packs->info[packs->nr].p = add_packed_git(full_path,
  377. full_path_len,
  378. 0);
  379. if (!packs->info[packs->nr].p) {
  380. warning(_("failed to add packfile '%s'"),
  381. full_path);
  382. return;
  383. }
  384. if (open_pack_index(packs->info[packs->nr].p)) {
  385. warning(_("failed to open pack-index '%s'"),
  386. full_path);
  387. close_pack(packs->info[packs->nr].p);
  388. FREE_AND_NULL(packs->info[packs->nr].p);
  389. return;
  390. }
  391. packs->info[packs->nr].pack_name = xstrdup(file_name);
  392. packs->info[packs->nr].orig_pack_int_id = packs->nr;
  393. packs->info[packs->nr].expired = 0;
  394. packs->nr++;
  395. }
  396. }
  397. struct pack_midx_entry {
  398. struct object_id oid;
  399. uint32_t pack_int_id;
  400. time_t pack_mtime;
  401. uint64_t offset;
  402. };
  403. static int midx_oid_compare(const void *_a, const void *_b)
  404. {
  405. const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
  406. const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
  407. int cmp = oidcmp(&a->oid, &b->oid);
  408. if (cmp)
  409. return cmp;
  410. if (a->pack_mtime > b->pack_mtime)
  411. return -1;
  412. else if (a->pack_mtime < b->pack_mtime)
  413. return 1;
  414. return a->pack_int_id - b->pack_int_id;
  415. }
  416. static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
  417. struct pack_midx_entry *e,
  418. uint32_t pos)
  419. {
  420. if (pos >= m->num_objects)
  421. return 1;
  422. nth_midxed_object_oid(&e->oid, m, pos);
  423. e->pack_int_id = nth_midxed_pack_int_id(m, pos);
  424. e->offset = nth_midxed_offset(m, pos);
  425. /* consider objects in midx to be from "old" packs */
  426. e->pack_mtime = 0;
  427. return 0;
  428. }
  429. static void fill_pack_entry(uint32_t pack_int_id,
  430. struct packed_git *p,
  431. uint32_t cur_object,
  432. struct pack_midx_entry *entry)
  433. {
  434. if (!nth_packed_object_oid(&entry->oid, p, cur_object))
  435. die(_("failed to locate object %d in packfile"), cur_object);
  436. entry->pack_int_id = pack_int_id;
  437. entry->pack_mtime = p->mtime;
  438. entry->offset = nth_packed_object_offset(p, cur_object);
  439. }
  440. /*
  441. * It is possible to artificially get into a state where there are many
  442. * duplicate copies of objects. That can create high memory pressure if
  443. * we are to create a list of all objects before de-duplication. To reduce
  444. * this memory pressure without a significant performance drop, automatically
  445. * group objects by the first byte of their object id. Use the IDX fanout
  446. * tables to group the data, copy to a local array, then sort.
  447. *
  448. * Copy only the de-duplicated entries (selected by most-recent modified time
  449. * of a packfile containing the object).
  450. */
  451. static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
  452. struct pack_info *info,
  453. uint32_t nr_packs,
  454. uint32_t *nr_objects)
  455. {
  456. uint32_t cur_fanout, cur_pack, cur_object;
  457. uint32_t alloc_fanout, alloc_objects, total_objects = 0;
  458. struct pack_midx_entry *entries_by_fanout = NULL;
  459. struct pack_midx_entry *deduplicated_entries = NULL;
  460. uint32_t start_pack = m ? m->num_packs : 0;
  461. for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
  462. total_objects += info[cur_pack].p->num_objects;
  463. /*
  464. * As we de-duplicate by fanout value, we expect the fanout
  465. * slices to be evenly distributed, with some noise. Hence,
  466. * allocate slightly more than one 256th.
  467. */
  468. alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
  469. ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
  470. ALLOC_ARRAY(deduplicated_entries, alloc_objects);
  471. *nr_objects = 0;
  472. for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
  473. uint32_t nr_fanout = 0;
  474. if (m) {
  475. uint32_t start = 0, end;
  476. if (cur_fanout)
  477. start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
  478. end = ntohl(m->chunk_oid_fanout[cur_fanout]);
  479. for (cur_object = start; cur_object < end; cur_object++) {
  480. ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
  481. nth_midxed_pack_midx_entry(m,
  482. &entries_by_fanout[nr_fanout],
  483. cur_object);
  484. nr_fanout++;
  485. }
  486. }
  487. for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
  488. uint32_t start = 0, end;
  489. if (cur_fanout)
  490. start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1);
  491. end = get_pack_fanout(info[cur_pack].p, cur_fanout);
  492. for (cur_object = start; cur_object < end; cur_object++) {
  493. ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
  494. fill_pack_entry(cur_pack, info[cur_pack].p, cur_object, &entries_by_fanout[nr_fanout]);
  495. nr_fanout++;
  496. }
  497. }
  498. QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
  499. /*
  500. * The batch is now sorted by OID and then mtime (descending).
  501. * Take only the first duplicate.
  502. */
  503. for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
  504. if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid,
  505. &entries_by_fanout[cur_object].oid))
  506. continue;
  507. ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
  508. memcpy(&deduplicated_entries[*nr_objects],
  509. &entries_by_fanout[cur_object],
  510. sizeof(struct pack_midx_entry));
  511. (*nr_objects)++;
  512. }
  513. }
  514. free(entries_by_fanout);
  515. return deduplicated_entries;
  516. }
  517. static size_t write_midx_pack_names(struct hashfile *f,
  518. struct pack_info *info,
  519. uint32_t num_packs)
  520. {
  521. uint32_t i;
  522. unsigned char padding[MIDX_CHUNK_ALIGNMENT];
  523. size_t written = 0;
  524. for (i = 0; i < num_packs; i++) {
  525. size_t writelen;
  526. if (info[i].expired)
  527. continue;
  528. if (i && strcmp(info[i].pack_name, info[i - 1].pack_name) <= 0)
  529. BUG("incorrect pack-file order: %s before %s",
  530. info[i - 1].pack_name,
  531. info[i].pack_name);
  532. writelen = strlen(info[i].pack_name) + 1;
  533. hashwrite(f, info[i].pack_name, writelen);
  534. written += writelen;
  535. }
  536. /* add padding to be aligned */
  537. i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
  538. if (i < MIDX_CHUNK_ALIGNMENT) {
  539. memset(padding, 0, sizeof(padding));
  540. hashwrite(f, padding, i);
  541. written += i;
  542. }
  543. return written;
  544. }
  545. static size_t write_midx_oid_fanout(struct hashfile *f,
  546. struct pack_midx_entry *objects,
  547. uint32_t nr_objects)
  548. {
  549. struct pack_midx_entry *list = objects;
  550. struct pack_midx_entry *last = objects + nr_objects;
  551. uint32_t count = 0;
  552. uint32_t i;
  553. /*
  554. * Write the first-level table (the list is sorted,
  555. * but we use a 256-entry lookup to be able to avoid
  556. * having to do eight extra binary search iterations).
  557. */
  558. for (i = 0; i < 256; i++) {
  559. struct pack_midx_entry *next = list;
  560. while (next < last && next->oid.hash[0] == i) {
  561. count++;
  562. next++;
  563. }
  564. hashwrite_be32(f, count);
  565. list = next;
  566. }
  567. return MIDX_CHUNK_FANOUT_SIZE;
  568. }
  569. static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
  570. struct pack_midx_entry *objects,
  571. uint32_t nr_objects)
  572. {
  573. struct pack_midx_entry *list = objects;
  574. uint32_t i;
  575. size_t written = 0;
  576. for (i = 0; i < nr_objects; i++) {
  577. struct pack_midx_entry *obj = list++;
  578. if (i < nr_objects - 1) {
  579. struct pack_midx_entry *next = list;
  580. if (oidcmp(&obj->oid, &next->oid) >= 0)
  581. BUG("OIDs not in order: %s >= %s",
  582. oid_to_hex(&obj->oid),
  583. oid_to_hex(&next->oid));
  584. }
  585. hashwrite(f, obj->oid.hash, (int)hash_len);
  586. written += hash_len;
  587. }
  588. return written;
  589. }
  590. static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
  591. uint32_t *perm,
  592. struct pack_midx_entry *objects, uint32_t nr_objects)
  593. {
  594. struct pack_midx_entry *list = objects;
  595. uint32_t i, nr_large_offset = 0;
  596. size_t written = 0;
  597. for (i = 0; i < nr_objects; i++) {
  598. struct pack_midx_entry *obj = list++;
  599. if (perm[obj->pack_int_id] == PACK_EXPIRED)
  600. BUG("object %s is in an expired pack with int-id %d",
  601. oid_to_hex(&obj->oid),
  602. obj->pack_int_id);
  603. hashwrite_be32(f, perm[obj->pack_int_id]);
  604. if (large_offset_needed && obj->offset >> 31)
  605. hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
  606. else if (!large_offset_needed && obj->offset >> 32)
  607. BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
  608. oid_to_hex(&obj->oid),
  609. obj->offset);
  610. else
  611. hashwrite_be32(f, (uint32_t)obj->offset);
  612. written += MIDX_CHUNK_OFFSET_WIDTH;
  613. }
  614. return written;
  615. }
  616. static size_t write_midx_large_offsets(struct hashfile *f, uint32_t nr_large_offset,
  617. struct pack_midx_entry *objects, uint32_t nr_objects)
  618. {
  619. struct pack_midx_entry *list = objects, *end = objects + nr_objects;
  620. size_t written = 0;
  621. while (nr_large_offset) {
  622. struct pack_midx_entry *obj;
  623. uint64_t offset;
  624. if (list >= end)
  625. BUG("too many large-offset objects");
  626. obj = list++;
  627. offset = obj->offset;
  628. if (!(offset >> 31))
  629. continue;
  630. hashwrite_be32(f, offset >> 32);
  631. hashwrite_be32(f, offset & 0xffffffffUL);
  632. written += 2 * sizeof(uint32_t);
  633. nr_large_offset--;
  634. }
  635. return written;
  636. }
  637. static int write_midx_internal(const char *object_dir, struct multi_pack_index *m,
  638. struct string_list *packs_to_drop, unsigned flags)
  639. {
  640. unsigned char cur_chunk, num_chunks = 0;
  641. char *midx_name;
  642. uint32_t i;
  643. struct hashfile *f = NULL;
  644. struct lock_file lk;
  645. struct pack_list packs;
  646. uint32_t *pack_perm = NULL;
  647. uint64_t written = 0;
  648. uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
  649. uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
  650. uint32_t nr_entries, num_large_offsets = 0;
  651. struct pack_midx_entry *entries = NULL;
  652. struct progress *progress = NULL;
  653. int large_offsets_needed = 0;
  654. int pack_name_concat_len = 0;
  655. int dropped_packs = 0;
  656. int result = 0;
  657. midx_name = get_midx_filename(object_dir);
  658. if (safe_create_leading_directories(midx_name)) {
  659. UNLEAK(midx_name);
  660. die_errno(_("unable to create leading directories of %s"),
  661. midx_name);
  662. }
  663. if (m)
  664. packs.m = m;
  665. else
  666. packs.m = load_multi_pack_index(object_dir, 1);
  667. packs.nr = 0;
  668. packs.alloc = packs.m ? packs.m->num_packs : 16;
  669. packs.info = NULL;
  670. ALLOC_ARRAY(packs.info, packs.alloc);
  671. if (packs.m) {
  672. for (i = 0; i < packs.m->num_packs; i++) {
  673. ALLOC_GROW(packs.info, packs.nr + 1, packs.alloc);
  674. packs.info[packs.nr].orig_pack_int_id = i;
  675. packs.info[packs.nr].pack_name = xstrdup(packs.m->pack_names[i]);
  676. packs.info[packs.nr].p = NULL;
  677. packs.info[packs.nr].expired = 0;
  678. packs.nr++;
  679. }
  680. }
  681. packs.pack_paths_checked = 0;
  682. if (flags & MIDX_PROGRESS)
  683. packs.progress = start_progress(_("Adding packfiles to multi-pack-index"), 0);
  684. else
  685. packs.progress = NULL;
  686. for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
  687. stop_progress(&packs.progress);
  688. if (packs.m && packs.nr == packs.m->num_packs && !packs_to_drop)
  689. goto cleanup;
  690. entries = get_sorted_entries(packs.m, packs.info, packs.nr, &nr_entries);
  691. for (i = 0; i < nr_entries; i++) {
  692. if (entries[i].offset > 0x7fffffff)
  693. num_large_offsets++;
  694. if (entries[i].offset > 0xffffffff)
  695. large_offsets_needed = 1;
  696. }
  697. QSORT(packs.info, packs.nr, pack_info_compare);
  698. if (packs_to_drop && packs_to_drop->nr) {
  699. int drop_index = 0;
  700. int missing_drops = 0;
  701. for (i = 0; i < packs.nr && drop_index < packs_to_drop->nr; i++) {
  702. int cmp = strcmp(packs.info[i].pack_name,
  703. packs_to_drop->items[drop_index].string);
  704. if (!cmp) {
  705. drop_index++;
  706. packs.info[i].expired = 1;
  707. } else if (cmp > 0) {
  708. error(_("did not see pack-file %s to drop"),
  709. packs_to_drop->items[drop_index].string);
  710. drop_index++;
  711. missing_drops++;
  712. i--;
  713. } else {
  714. packs.info[i].expired = 0;
  715. }
  716. }
  717. if (missing_drops) {
  718. result = 1;
  719. goto cleanup;
  720. }
  721. }
  722. /*
  723. * pack_perm stores a permutation between pack-int-ids from the
  724. * previous multi-pack-index to the new one we are writing:
  725. *
  726. * pack_perm[old_id] = new_id
  727. */
  728. ALLOC_ARRAY(pack_perm, packs.nr);
  729. for (i = 0; i < packs.nr; i++) {
  730. if (packs.info[i].expired) {
  731. dropped_packs++;
  732. pack_perm[packs.info[i].orig_pack_int_id] = PACK_EXPIRED;
  733. } else {
  734. pack_perm[packs.info[i].orig_pack_int_id] = i - dropped_packs;
  735. }
  736. }
  737. for (i = 0; i < packs.nr; i++) {
  738. if (!packs.info[i].expired)
  739. pack_name_concat_len += strlen(packs.info[i].pack_name) + 1;
  740. }
  741. if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
  742. pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
  743. (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
  744. hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
  745. f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
  746. FREE_AND_NULL(midx_name);
  747. if (packs.m)
  748. close_midx(packs.m);
  749. cur_chunk = 0;
  750. num_chunks = large_offsets_needed ? 5 : 4;
  751. written = write_midx_header(f, num_chunks, packs.nr - dropped_packs);
  752. chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
  753. chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
  754. cur_chunk++;
  755. chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
  756. chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + pack_name_concat_len;
  757. cur_chunk++;
  758. chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
  759. chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE;
  760. cur_chunk++;
  761. chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS;
  762. chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * the_hash_algo->rawsz;
  763. cur_chunk++;
  764. chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH;
  765. if (large_offsets_needed) {
  766. chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
  767. cur_chunk++;
  768. chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] +
  769. num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
  770. }
  771. chunk_ids[cur_chunk] = 0;
  772. for (i = 0; i <= num_chunks; i++) {
  773. if (i && chunk_offsets[i] < chunk_offsets[i - 1])
  774. BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
  775. chunk_offsets[i - 1],
  776. chunk_offsets[i]);
  777. if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT)
  778. BUG("chunk offset %"PRIu64" is not properly aligned",
  779. chunk_offsets[i]);
  780. hashwrite_be32(f, chunk_ids[i]);
  781. hashwrite_be32(f, chunk_offsets[i] >> 32);
  782. hashwrite_be32(f, chunk_offsets[i]);
  783. written += MIDX_CHUNKLOOKUP_WIDTH;
  784. }
  785. if (flags & MIDX_PROGRESS)
  786. progress = start_progress(_("Writing chunks to multi-pack-index"),
  787. num_chunks);
  788. for (i = 0; i < num_chunks; i++) {
  789. if (written != chunk_offsets[i])
  790. BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
  791. chunk_offsets[i],
  792. written,
  793. chunk_ids[i]);
  794. switch (chunk_ids[i]) {
  795. case MIDX_CHUNKID_PACKNAMES:
  796. written += write_midx_pack_names(f, packs.info, packs.nr);
  797. break;
  798. case MIDX_CHUNKID_OIDFANOUT:
  799. written += write_midx_oid_fanout(f, entries, nr_entries);
  800. break;
  801. case MIDX_CHUNKID_OIDLOOKUP:
  802. written += write_midx_oid_lookup(f, the_hash_algo->rawsz, entries, nr_entries);
  803. break;
  804. case MIDX_CHUNKID_OBJECTOFFSETS:
  805. written += write_midx_object_offsets(f, large_offsets_needed, pack_perm, entries, nr_entries);
  806. break;
  807. case MIDX_CHUNKID_LARGEOFFSETS:
  808. written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries);
  809. break;
  810. default:
  811. BUG("trying to write unknown chunk id %"PRIx32,
  812. chunk_ids[i]);
  813. }
  814. display_progress(progress, i + 1);
  815. }
  816. stop_progress(&progress);
  817. if (written != chunk_offsets[num_chunks])
  818. BUG("incorrect final offset %"PRIu64" != %"PRIu64,
  819. written,
  820. chunk_offsets[num_chunks]);
  821. finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
  822. commit_lock_file(&lk);
  823. cleanup:
  824. for (i = 0; i < packs.nr; i++) {
  825. if (packs.info[i].p) {
  826. close_pack(packs.info[i].p);
  827. free(packs.info[i].p);
  828. }
  829. free(packs.info[i].pack_name);
  830. }
  831. free(packs.info);
  832. free(entries);
  833. free(pack_perm);
  834. free(midx_name);
  835. return result;
  836. }
  837. int write_midx_file(const char *object_dir, unsigned flags)
  838. {
  839. return write_midx_internal(object_dir, NULL, NULL, flags);
  840. }
  841. void clear_midx_file(struct repository *r)
  842. {
  843. char *midx = get_midx_filename(r->objects->odb->path);
  844. if (r->objects && r->objects->multi_pack_index) {
  845. close_midx(r->objects->multi_pack_index);
  846. r->objects->multi_pack_index = NULL;
  847. }
  848. if (remove_path(midx)) {
  849. UNLEAK(midx);
  850. die(_("failed to clear multi-pack-index at %s"), midx);
  851. }
  852. free(midx);
  853. }
  854. static int verify_midx_error;
  855. static void midx_report(const char *fmt, ...)
  856. {
  857. va_list ap;
  858. verify_midx_error = 1;
  859. va_start(ap, fmt);
  860. vfprintf(stderr, fmt, ap);
  861. fprintf(stderr, "\n");
  862. va_end(ap);
  863. }
  864. struct pair_pos_vs_id
  865. {
  866. uint32_t pos;
  867. uint32_t pack_int_id;
  868. };
  869. static int compare_pair_pos_vs_id(const void *_a, const void *_b)
  870. {
  871. struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
  872. struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
  873. return b->pack_int_id - a->pack_int_id;
  874. }
  875. /*
  876. * Limit calls to display_progress() for performance reasons.
  877. * The interval here was arbitrarily chosen.
  878. */
  879. #define SPARSE_PROGRESS_INTERVAL (1 << 12)
  880. #define midx_display_sparse_progress(progress, n) \
  881. do { \
  882. uint64_t _n = (n); \
  883. if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
  884. display_progress(progress, _n); \
  885. } while (0)
  886. int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
  887. {
  888. struct pair_pos_vs_id *pairs = NULL;
  889. uint32_t i;
  890. struct progress *progress = NULL;
  891. struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
  892. verify_midx_error = 0;
  893. if (!m)
  894. return 0;
  895. if (flags & MIDX_PROGRESS)
  896. progress = start_progress(_("Looking for referenced packfiles"),
  897. m->num_packs);
  898. for (i = 0; i < m->num_packs; i++) {
  899. if (prepare_midx_pack(r, m, i))
  900. midx_report("failed to load pack in position %d", i);
  901. display_progress(progress, i + 1);
  902. }
  903. stop_progress(&progress);
  904. for (i = 0; i < 255; i++) {
  905. uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
  906. uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]);
  907. if (oid_fanout1 > oid_fanout2)
  908. midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
  909. i, oid_fanout1, oid_fanout2, i + 1);
  910. }
  911. if (flags & MIDX_PROGRESS)
  912. progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
  913. m->num_objects - 1);
  914. for (i = 0; i < m->num_objects - 1; i++) {
  915. struct object_id oid1, oid2;
  916. nth_midxed_object_oid(&oid1, m, i);
  917. nth_midxed_object_oid(&oid2, m, i + 1);
  918. if (oidcmp(&oid1, &oid2) >= 0)
  919. midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
  920. i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
  921. midx_display_sparse_progress(progress, i + 1);
  922. }
  923. stop_progress(&progress);
  924. /*
  925. * Create an array mapping each object to its packfile id. Sort it
  926. * to group the objects by packfile. Use this permutation to visit
  927. * each of the objects and only require 1 packfile to be open at a
  928. * time.
  929. */
  930. ALLOC_ARRAY(pairs, m->num_objects);
  931. for (i = 0; i < m->num_objects; i++) {
  932. pairs[i].pos = i;
  933. pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
  934. }
  935. if (flags & MIDX_PROGRESS)
  936. progress = start_sparse_progress(_("Sorting objects by packfile"),
  937. m->num_objects);
  938. display_progress(progress, 0); /* TODO: Measure QSORT() progress */
  939. QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
  940. stop_progress(&progress);
  941. if (flags & MIDX_PROGRESS)
  942. progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
  943. for (i = 0; i < m->num_objects; i++) {
  944. struct object_id oid;
  945. struct pack_entry e;
  946. off_t m_offset, p_offset;
  947. if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
  948. m->packs[pairs[i-1].pack_int_id])
  949. {
  950. close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
  951. close_pack_index(m->packs[pairs[i-1].pack_int_id]);
  952. }
  953. nth_midxed_object_oid(&oid, m, pairs[i].pos);
  954. if (!fill_midx_entry(r, &oid, &e, m)) {
  955. midx_report(_("failed to load pack entry for oid[%d] = %s"),
  956. pairs[i].pos, oid_to_hex(&oid));
  957. continue;
  958. }
  959. if (open_pack_index(e.p)) {
  960. midx_report(_("failed to load pack-index for packfile %s"),
  961. e.p->pack_name);
  962. break;
  963. }
  964. m_offset = e.offset;
  965. p_offset = find_pack_entry_one(oid.hash, e.p);
  966. if (m_offset != p_offset)
  967. midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
  968. pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
  969. midx_display_sparse_progress(progress, i + 1);
  970. }
  971. stop_progress(&progress);
  972. free(pairs);
  973. return verify_midx_error;
  974. }
  975. int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
  976. {
  977. uint32_t i, *count, result = 0;
  978. struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
  979. struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
  980. struct progress *progress = NULL;
  981. if (!m)
  982. return 0;
  983. count = xcalloc(m->num_packs, sizeof(uint32_t));
  984. if (flags & MIDX_PROGRESS)
  985. progress = start_progress(_("Counting referenced objects"),
  986. m->num_objects);
  987. for (i = 0; i < m->num_objects; i++) {
  988. int pack_int_id = nth_midxed_pack_int_id(m, i);
  989. count[pack_int_id]++;
  990. display_progress(progress, i + 1);
  991. }
  992. stop_progress(&progress);
  993. if (flags & MIDX_PROGRESS)
  994. progress = start_progress(_("Finding and deleting unreferenced packfiles"),
  995. m->num_packs);
  996. for (i = 0; i < m->num_packs; i++) {
  997. char *pack_name;
  998. display_progress(progress, i + 1);
  999. if (count[i])
  1000. continue;
  1001. if (prepare_midx_pack(r, m, i))
  1002. continue;
  1003. if (m->packs[i]->pack_keep)
  1004. continue;
  1005. pack_name = xstrdup(m->packs[i]->pack_name);
  1006. close_pack(m->packs[i]);
  1007. string_list_insert(&packs_to_drop, m->pack_names[i]);
  1008. unlink_pack_path(pack_name, 0);
  1009. free(pack_name);
  1010. }
  1011. stop_progress(&progress);
  1012. free(count);
  1013. if (packs_to_drop.nr)
  1014. result = write_midx_internal(object_dir, m, &packs_to_drop, flags);
  1015. string_list_clear(&packs_to_drop, 0);
  1016. return result;
  1017. }
  1018. struct repack_info {
  1019. timestamp_t mtime;
  1020. uint32_t referenced_objects;
  1021. uint32_t pack_int_id;
  1022. };
  1023. static int compare_by_mtime(const void *a_, const void *b_)
  1024. {
  1025. const struct repack_info *a, *b;
  1026. a = (const struct repack_info *)a_;
  1027. b = (const struct repack_info *)b_;
  1028. if (a->mtime < b->mtime)
  1029. return -1;
  1030. if (a->mtime > b->mtime)
  1031. return 1;
  1032. return 0;
  1033. }
  1034. static int fill_included_packs_all(struct multi_pack_index *m,
  1035. unsigned char *include_pack)
  1036. {
  1037. uint32_t i;
  1038. for (i = 0; i < m->num_packs; i++)
  1039. include_pack[i] = 1;
  1040. return m->num_packs < 2;
  1041. }
  1042. static int fill_included_packs_batch(struct repository *r,
  1043. struct multi_pack_index *m,
  1044. unsigned char *include_pack,
  1045. size_t batch_size)
  1046. {
  1047. uint32_t i, packs_to_repack;
  1048. size_t total_size;
  1049. struct repack_info *pack_info = xcalloc(m->num_packs, sizeof(struct repack_info));
  1050. for (i = 0; i < m->num_packs; i++) {
  1051. pack_info[i].pack_int_id = i;
  1052. if (prepare_midx_pack(r, m, i))
  1053. continue;
  1054. pack_info[i].mtime = m->packs[i]->mtime;
  1055. }
  1056. for (i = 0; batch_size && i < m->num_objects; i++) {
  1057. uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
  1058. pack_info[pack_int_id].referenced_objects++;
  1059. }
  1060. QSORT(pack_info, m->num_packs, compare_by_mtime);
  1061. total_size = 0;
  1062. packs_to_repack = 0;
  1063. for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
  1064. int pack_int_id = pack_info[i].pack_int_id;
  1065. struct packed_git *p = m->packs[pack_int_id];
  1066. size_t expected_size;
  1067. if (!p)
  1068. continue;
  1069. if (open_pack_index(p) || !p->num_objects)
  1070. continue;
  1071. expected_size = (size_t)(p->pack_size
  1072. * pack_info[i].referenced_objects);
  1073. expected_size /= p->num_objects;
  1074. if (expected_size >= batch_size)
  1075. continue;
  1076. packs_to_repack++;
  1077. total_size += expected_size;
  1078. include_pack[pack_int_id] = 1;
  1079. }
  1080. free(pack_info);
  1081. if (total_size < batch_size || packs_to_repack < 2)
  1082. return 1;
  1083. return 0;
  1084. }
  1085. int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
  1086. {
  1087. int result = 0;
  1088. uint32_t i;
  1089. unsigned char *include_pack;
  1090. struct child_process cmd = CHILD_PROCESS_INIT;
  1091. struct strbuf base_name = STRBUF_INIT;
  1092. struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
  1093. if (!m)
  1094. return 0;
  1095. include_pack = xcalloc(m->num_packs, sizeof(unsigned char));
  1096. if (batch_size) {
  1097. if (fill_included_packs_batch(r, m, include_pack, batch_size))
  1098. goto cleanup;
  1099. } else if (fill_included_packs_all(m, include_pack))
  1100. goto cleanup;
  1101. argv_array_push(&cmd.args, "pack-objects");
  1102. strbuf_addstr(&base_name, object_dir);
  1103. strbuf_addstr(&base_name, "/pack/pack");
  1104. argv_array_push(&cmd.args, base_name.buf);
  1105. if (flags & MIDX_PROGRESS)
  1106. argv_array_push(&cmd.args, "--progress");
  1107. else
  1108. argv_array_push(&cmd.args, "-q");
  1109. strbuf_release(&base_name);
  1110. cmd.git_cmd = 1;
  1111. cmd.in = cmd.out = -1;
  1112. if (start_command(&cmd)) {
  1113. error(_("could not start pack-objects"));
  1114. result = 1;
  1115. goto cleanup;
  1116. }
  1117. for (i = 0; i < m->num_objects; i++) {
  1118. struct object_id oid;
  1119. uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
  1120. if (!include_pack[pack_int_id])
  1121. continue;
  1122. nth_midxed_object_oid(&oid, m, i);
  1123. xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
  1124. xwrite(cmd.in, "\n", 1);
  1125. }
  1126. close(cmd.in);
  1127. if (finish_command(&cmd)) {
  1128. error(_("could not finish pack-objects"));
  1129. result = 1;
  1130. goto cleanup;
  1131. }
  1132. result = write_midx_internal(object_dir, m, NULL, flags);
  1133. m = NULL;
  1134. cleanup:
  1135. if (m)
  1136. close_midx(m);
  1137. free(include_pack);
  1138. return result;
  1139. }