midx.c 38 KB

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