index-pack.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. #include "builtin.h"
  2. #include "config.h"
  3. #include "delta.h"
  4. #include "pack.h"
  5. #include "csum-file.h"
  6. #include "blob.h"
  7. #include "commit.h"
  8. #include "tag.h"
  9. #include "tree.h"
  10. #include "progress.h"
  11. #include "fsck.h"
  12. #include "exec-cmd.h"
  13. #include "streaming.h"
  14. #include "thread-utils.h"
  15. #include "packfile.h"
  16. #include "object-store.h"
  17. #include "promisor-remote.h"
  18. static const char index_pack_usage[] =
  19. "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
  20. struct object_entry {
  21. struct pack_idx_entry idx;
  22. unsigned long size;
  23. unsigned char hdr_size;
  24. signed char type;
  25. signed char real_type;
  26. };
  27. struct object_stat {
  28. unsigned delta_depth;
  29. int base_object_no;
  30. };
  31. struct base_data {
  32. /* Initialized by make_base(). */
  33. struct base_data *base;
  34. struct object_entry *obj;
  35. int ref_first, ref_last;
  36. int ofs_first, ofs_last;
  37. /*
  38. * Threads should increment retain_data if they are about to call
  39. * patch_delta() using this struct's data as a base, and decrement this
  40. * when they are done. While retain_data is nonzero, this struct's data
  41. * will not be freed even if the delta base cache limit is exceeded.
  42. */
  43. int retain_data;
  44. /*
  45. * The number of direct children that have not been fully processed
  46. * (entered work_head, entered done_head, left done_head). When this
  47. * number reaches zero, this struct base_data can be freed.
  48. */
  49. int children_remaining;
  50. /* Not initialized by make_base(). */
  51. struct list_head list;
  52. void *data;
  53. unsigned long size;
  54. };
  55. /*
  56. * Stack of struct base_data that have unprocessed children.
  57. * threaded_second_pass() uses this as a source of work (the other being the
  58. * objects array).
  59. *
  60. * Guarded by work_mutex.
  61. */
  62. static LIST_HEAD(work_head);
  63. /*
  64. * Stack of struct base_data that have children, all of whom have been
  65. * processed or are being processed, and at least one child is being processed.
  66. * These struct base_data must be kept around until the last child is
  67. * processed.
  68. *
  69. * Guarded by work_mutex.
  70. */
  71. static LIST_HEAD(done_head);
  72. /*
  73. * All threads share one delta base cache.
  74. *
  75. * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only
  76. * in a thread.
  77. */
  78. static size_t base_cache_used;
  79. static size_t base_cache_limit;
  80. struct thread_local {
  81. pthread_t thread;
  82. int pack_fd;
  83. };
  84. /* Remember to update object flag allocation in object.h */
  85. #define FLAG_LINK (1u<<20)
  86. #define FLAG_CHECKED (1u<<21)
  87. struct ofs_delta_entry {
  88. off_t offset;
  89. int obj_no;
  90. };
  91. struct ref_delta_entry {
  92. struct object_id oid;
  93. int obj_no;
  94. };
  95. static struct object_entry *objects;
  96. static struct object_stat *obj_stat;
  97. static struct ofs_delta_entry *ofs_deltas;
  98. static struct ref_delta_entry *ref_deltas;
  99. static struct thread_local nothread_data;
  100. static int nr_objects;
  101. static int nr_ofs_deltas;
  102. static int nr_ref_deltas;
  103. static int ref_deltas_alloc;
  104. static int nr_resolved_deltas;
  105. static int nr_threads;
  106. static int from_stdin;
  107. static int strict;
  108. static int do_fsck_object;
  109. static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
  110. static int verbose;
  111. static int show_resolving_progress;
  112. static int show_stat;
  113. static int check_self_contained_and_connected;
  114. static struct progress *progress;
  115. /* We always read in 4kB chunks. */
  116. static unsigned char input_buffer[4096];
  117. static unsigned int input_offset, input_len;
  118. static off_t consumed_bytes;
  119. static off_t max_input_size;
  120. static unsigned deepest_delta;
  121. static git_hash_ctx input_ctx;
  122. static uint32_t input_crc32;
  123. static int input_fd, output_fd;
  124. static const char *curr_pack;
  125. static struct thread_local *thread_data;
  126. static int nr_dispatched;
  127. static int threads_active;
  128. static pthread_mutex_t read_mutex;
  129. #define read_lock() lock_mutex(&read_mutex)
  130. #define read_unlock() unlock_mutex(&read_mutex)
  131. static pthread_mutex_t counter_mutex;
  132. #define counter_lock() lock_mutex(&counter_mutex)
  133. #define counter_unlock() unlock_mutex(&counter_mutex)
  134. static pthread_mutex_t work_mutex;
  135. #define work_lock() lock_mutex(&work_mutex)
  136. #define work_unlock() unlock_mutex(&work_mutex)
  137. static pthread_mutex_t deepest_delta_mutex;
  138. #define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
  139. #define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
  140. static pthread_mutex_t type_cas_mutex;
  141. #define type_cas_lock() lock_mutex(&type_cas_mutex)
  142. #define type_cas_unlock() unlock_mutex(&type_cas_mutex)
  143. static pthread_key_t key;
  144. static inline void lock_mutex(pthread_mutex_t *mutex)
  145. {
  146. if (threads_active)
  147. pthread_mutex_lock(mutex);
  148. }
  149. static inline void unlock_mutex(pthread_mutex_t *mutex)
  150. {
  151. if (threads_active)
  152. pthread_mutex_unlock(mutex);
  153. }
  154. /*
  155. * Mutex and conditional variable can't be statically-initialized on Windows.
  156. */
  157. static void init_thread(void)
  158. {
  159. int i;
  160. init_recursive_mutex(&read_mutex);
  161. pthread_mutex_init(&counter_mutex, NULL);
  162. pthread_mutex_init(&work_mutex, NULL);
  163. pthread_mutex_init(&type_cas_mutex, NULL);
  164. if (show_stat)
  165. pthread_mutex_init(&deepest_delta_mutex, NULL);
  166. pthread_key_create(&key, NULL);
  167. thread_data = xcalloc(nr_threads, sizeof(*thread_data));
  168. for (i = 0; i < nr_threads; i++) {
  169. thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
  170. if (thread_data[i].pack_fd == -1)
  171. die_errno(_("unable to open %s"), curr_pack);
  172. }
  173. threads_active = 1;
  174. }
  175. static void cleanup_thread(void)
  176. {
  177. int i;
  178. if (!threads_active)
  179. return;
  180. threads_active = 0;
  181. pthread_mutex_destroy(&read_mutex);
  182. pthread_mutex_destroy(&counter_mutex);
  183. pthread_mutex_destroy(&work_mutex);
  184. pthread_mutex_destroy(&type_cas_mutex);
  185. if (show_stat)
  186. pthread_mutex_destroy(&deepest_delta_mutex);
  187. for (i = 0; i < nr_threads; i++)
  188. close(thread_data[i].pack_fd);
  189. pthread_key_delete(key);
  190. free(thread_data);
  191. }
  192. static int mark_link(struct object *obj, int type, void *data, struct fsck_options *options)
  193. {
  194. if (!obj)
  195. return -1;
  196. if (type != OBJ_ANY && obj->type != type)
  197. die(_("object type mismatch at %s"), oid_to_hex(&obj->oid));
  198. obj->flags |= FLAG_LINK;
  199. return 0;
  200. }
  201. /* The content of each linked object must have been checked
  202. or it must be already present in the object database */
  203. static unsigned check_object(struct object *obj)
  204. {
  205. if (!obj)
  206. return 0;
  207. if (!(obj->flags & FLAG_LINK))
  208. return 0;
  209. if (!(obj->flags & FLAG_CHECKED)) {
  210. unsigned long size;
  211. int type = oid_object_info(the_repository, &obj->oid, &size);
  212. if (type <= 0)
  213. die(_("did not receive expected object %s"),
  214. oid_to_hex(&obj->oid));
  215. if (type != obj->type)
  216. die(_("object %s: expected type %s, found %s"),
  217. oid_to_hex(&obj->oid),
  218. type_name(obj->type), type_name(type));
  219. obj->flags |= FLAG_CHECKED;
  220. return 1;
  221. }
  222. return 0;
  223. }
  224. static unsigned check_objects(void)
  225. {
  226. unsigned i, max, foreign_nr = 0;
  227. max = get_max_object_index();
  228. if (verbose)
  229. progress = start_delayed_progress(_("Checking objects"), max);
  230. for (i = 0; i < max; i++) {
  231. foreign_nr += check_object(get_indexed_object(i));
  232. display_progress(progress, i + 1);
  233. }
  234. stop_progress(&progress);
  235. return foreign_nr;
  236. }
  237. /* Discard current buffer used content. */
  238. static void flush(void)
  239. {
  240. if (input_offset) {
  241. if (output_fd >= 0)
  242. write_or_die(output_fd, input_buffer, input_offset);
  243. the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
  244. memmove(input_buffer, input_buffer + input_offset, input_len);
  245. input_offset = 0;
  246. }
  247. }
  248. /*
  249. * Make sure at least "min" bytes are available in the buffer, and
  250. * return the pointer to the buffer.
  251. */
  252. static void *fill(int min)
  253. {
  254. if (min <= input_len)
  255. return input_buffer + input_offset;
  256. if (min > sizeof(input_buffer))
  257. die(Q_("cannot fill %d byte",
  258. "cannot fill %d bytes",
  259. min),
  260. min);
  261. flush();
  262. do {
  263. ssize_t ret = xread(input_fd, input_buffer + input_len,
  264. sizeof(input_buffer) - input_len);
  265. if (ret <= 0) {
  266. if (!ret)
  267. die(_("early EOF"));
  268. die_errno(_("read error on input"));
  269. }
  270. input_len += ret;
  271. if (from_stdin)
  272. display_throughput(progress, consumed_bytes + input_len);
  273. } while (input_len < min);
  274. return input_buffer;
  275. }
  276. static void use(int bytes)
  277. {
  278. if (bytes > input_len)
  279. die(_("used more bytes than were available"));
  280. input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
  281. input_len -= bytes;
  282. input_offset += bytes;
  283. /* make sure off_t is sufficiently large not to wrap */
  284. if (signed_add_overflows(consumed_bytes, bytes))
  285. die(_("pack too large for current definition of off_t"));
  286. consumed_bytes += bytes;
  287. if (max_input_size && consumed_bytes > max_input_size)
  288. die(_("pack exceeds maximum allowed size"));
  289. }
  290. static const char *open_pack_file(const char *pack_name)
  291. {
  292. if (from_stdin) {
  293. input_fd = 0;
  294. if (!pack_name) {
  295. struct strbuf tmp_file = STRBUF_INIT;
  296. output_fd = odb_mkstemp(&tmp_file,
  297. "pack/tmp_pack_XXXXXX");
  298. pack_name = strbuf_detach(&tmp_file, NULL);
  299. } else {
  300. output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
  301. if (output_fd < 0)
  302. die_errno(_("unable to create '%s'"), pack_name);
  303. }
  304. nothread_data.pack_fd = output_fd;
  305. } else {
  306. input_fd = open(pack_name, O_RDONLY);
  307. if (input_fd < 0)
  308. die_errno(_("cannot open packfile '%s'"), pack_name);
  309. output_fd = -1;
  310. nothread_data.pack_fd = input_fd;
  311. }
  312. the_hash_algo->init_fn(&input_ctx);
  313. return pack_name;
  314. }
  315. static void parse_pack_header(void)
  316. {
  317. struct pack_header *hdr = fill(sizeof(struct pack_header));
  318. /* Header consistency check */
  319. if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
  320. die(_("pack signature mismatch"));
  321. if (!pack_version_ok(hdr->hdr_version))
  322. die(_("pack version %"PRIu32" unsupported"),
  323. ntohl(hdr->hdr_version));
  324. nr_objects = ntohl(hdr->hdr_entries);
  325. use(sizeof(struct pack_header));
  326. }
  327. static NORETURN void bad_object(off_t offset, const char *format,
  328. ...) __attribute__((format (printf, 2, 3)));
  329. static NORETURN void bad_object(off_t offset, const char *format, ...)
  330. {
  331. va_list params;
  332. char buf[1024];
  333. va_start(params, format);
  334. vsnprintf(buf, sizeof(buf), format, params);
  335. va_end(params);
  336. die(_("pack has bad object at offset %"PRIuMAX": %s"),
  337. (uintmax_t)offset, buf);
  338. }
  339. static inline struct thread_local *get_thread_data(void)
  340. {
  341. if (HAVE_THREADS) {
  342. if (threads_active)
  343. return pthread_getspecific(key);
  344. assert(!threads_active &&
  345. "This should only be reached when all threads are gone");
  346. }
  347. return &nothread_data;
  348. }
  349. static void set_thread_data(struct thread_local *data)
  350. {
  351. if (threads_active)
  352. pthread_setspecific(key, data);
  353. }
  354. static void free_base_data(struct base_data *c)
  355. {
  356. if (c->data) {
  357. FREE_AND_NULL(c->data);
  358. base_cache_used -= c->size;
  359. }
  360. }
  361. static void prune_base_data(struct base_data *retain)
  362. {
  363. struct list_head *pos;
  364. if (base_cache_used <= base_cache_limit)
  365. return;
  366. list_for_each_prev(pos, &done_head) {
  367. struct base_data *b = list_entry(pos, struct base_data, list);
  368. if (b->retain_data || b == retain)
  369. continue;
  370. if (b->data) {
  371. free_base_data(b);
  372. if (base_cache_used <= base_cache_limit)
  373. return;
  374. }
  375. }
  376. list_for_each_prev(pos, &work_head) {
  377. struct base_data *b = list_entry(pos, struct base_data, list);
  378. if (b->retain_data || b == retain)
  379. continue;
  380. if (b->data) {
  381. free_base_data(b);
  382. if (base_cache_used <= base_cache_limit)
  383. return;
  384. }
  385. }
  386. }
  387. static int is_delta_type(enum object_type type)
  388. {
  389. return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
  390. }
  391. static void *unpack_entry_data(off_t offset, unsigned long size,
  392. enum object_type type, struct object_id *oid)
  393. {
  394. static char fixed_buf[8192];
  395. int status;
  396. git_zstream stream;
  397. void *buf;
  398. git_hash_ctx c;
  399. char hdr[32];
  400. int hdrlen;
  401. if (!is_delta_type(type)) {
  402. hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX,
  403. type_name(type),(uintmax_t)size) + 1;
  404. the_hash_algo->init_fn(&c);
  405. the_hash_algo->update_fn(&c, hdr, hdrlen);
  406. } else
  407. oid = NULL;
  408. if (type == OBJ_BLOB && size > big_file_threshold)
  409. buf = fixed_buf;
  410. else
  411. buf = xmallocz(size);
  412. memset(&stream, 0, sizeof(stream));
  413. git_inflate_init(&stream);
  414. stream.next_out = buf;
  415. stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size;
  416. do {
  417. unsigned char *last_out = stream.next_out;
  418. stream.next_in = fill(1);
  419. stream.avail_in = input_len;
  420. status = git_inflate(&stream, 0);
  421. use(input_len - stream.avail_in);
  422. if (oid)
  423. the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
  424. if (buf == fixed_buf) {
  425. stream.next_out = buf;
  426. stream.avail_out = sizeof(fixed_buf);
  427. }
  428. } while (status == Z_OK);
  429. if (stream.total_out != size || status != Z_STREAM_END)
  430. bad_object(offset, _("inflate returned %d"), status);
  431. git_inflate_end(&stream);
  432. if (oid)
  433. the_hash_algo->final_fn(oid->hash, &c);
  434. return buf == fixed_buf ? NULL : buf;
  435. }
  436. static void *unpack_raw_entry(struct object_entry *obj,
  437. off_t *ofs_offset,
  438. struct object_id *ref_oid,
  439. struct object_id *oid)
  440. {
  441. unsigned char *p;
  442. unsigned long size, c;
  443. off_t base_offset;
  444. unsigned shift;
  445. void *data;
  446. obj->idx.offset = consumed_bytes;
  447. input_crc32 = crc32(0, NULL, 0);
  448. p = fill(1);
  449. c = *p;
  450. use(1);
  451. obj->type = (c >> 4) & 7;
  452. size = (c & 15);
  453. shift = 4;
  454. while (c & 0x80) {
  455. p = fill(1);
  456. c = *p;
  457. use(1);
  458. size += (c & 0x7f) << shift;
  459. shift += 7;
  460. }
  461. obj->size = size;
  462. switch (obj->type) {
  463. case OBJ_REF_DELTA:
  464. hashcpy(ref_oid->hash, fill(the_hash_algo->rawsz));
  465. use(the_hash_algo->rawsz);
  466. break;
  467. case OBJ_OFS_DELTA:
  468. p = fill(1);
  469. c = *p;
  470. use(1);
  471. base_offset = c & 127;
  472. while (c & 128) {
  473. base_offset += 1;
  474. if (!base_offset || MSB(base_offset, 7))
  475. bad_object(obj->idx.offset, _("offset value overflow for delta base object"));
  476. p = fill(1);
  477. c = *p;
  478. use(1);
  479. base_offset = (base_offset << 7) + (c & 127);
  480. }
  481. *ofs_offset = obj->idx.offset - base_offset;
  482. if (*ofs_offset <= 0 || *ofs_offset >= obj->idx.offset)
  483. bad_object(obj->idx.offset, _("delta base offset is out of bound"));
  484. break;
  485. case OBJ_COMMIT:
  486. case OBJ_TREE:
  487. case OBJ_BLOB:
  488. case OBJ_TAG:
  489. break;
  490. default:
  491. bad_object(obj->idx.offset, _("unknown object type %d"), obj->type);
  492. }
  493. obj->hdr_size = consumed_bytes - obj->idx.offset;
  494. data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, oid);
  495. obj->idx.crc32 = input_crc32;
  496. return data;
  497. }
  498. static void *unpack_data(struct object_entry *obj,
  499. int (*consume)(const unsigned char *, unsigned long, void *),
  500. void *cb_data)
  501. {
  502. off_t from = obj[0].idx.offset + obj[0].hdr_size;
  503. off_t len = obj[1].idx.offset - from;
  504. unsigned char *data, *inbuf;
  505. git_zstream stream;
  506. int status;
  507. data = xmallocz(consume ? 64*1024 : obj->size);
  508. inbuf = xmalloc((len < 64*1024) ? (int)len : 64*1024);
  509. memset(&stream, 0, sizeof(stream));
  510. git_inflate_init(&stream);
  511. stream.next_out = data;
  512. stream.avail_out = consume ? 64*1024 : obj->size;
  513. do {
  514. ssize_t n = (len < 64*1024) ? (ssize_t)len : 64*1024;
  515. n = xpread(get_thread_data()->pack_fd, inbuf, n, from);
  516. if (n < 0)
  517. die_errno(_("cannot pread pack file"));
  518. if (!n)
  519. die(Q_("premature end of pack file, %"PRIuMAX" byte missing",
  520. "premature end of pack file, %"PRIuMAX" bytes missing",
  521. (unsigned int)len),
  522. (uintmax_t)len);
  523. from += n;
  524. len -= n;
  525. stream.next_in = inbuf;
  526. stream.avail_in = n;
  527. if (!consume)
  528. status = git_inflate(&stream, 0);
  529. else {
  530. do {
  531. status = git_inflate(&stream, 0);
  532. if (consume(data, stream.next_out - data, cb_data)) {
  533. free(inbuf);
  534. free(data);
  535. return NULL;
  536. }
  537. stream.next_out = data;
  538. stream.avail_out = 64*1024;
  539. } while (status == Z_OK && stream.avail_in);
  540. }
  541. } while (len && status == Z_OK && !stream.avail_in);
  542. /* This has been inflated OK when first encountered, so... */
  543. if (status != Z_STREAM_END || stream.total_out != obj->size)
  544. die(_("serious inflate inconsistency"));
  545. git_inflate_end(&stream);
  546. free(inbuf);
  547. if (consume) {
  548. FREE_AND_NULL(data);
  549. }
  550. return data;
  551. }
  552. static void *get_data_from_pack(struct object_entry *obj)
  553. {
  554. return unpack_data(obj, NULL, NULL);
  555. }
  556. static int compare_ofs_delta_bases(off_t offset1, off_t offset2,
  557. enum object_type type1,
  558. enum object_type type2)
  559. {
  560. int cmp = type1 - type2;
  561. if (cmp)
  562. return cmp;
  563. return offset1 < offset2 ? -1 :
  564. offset1 > offset2 ? 1 :
  565. 0;
  566. }
  567. static int find_ofs_delta(const off_t offset)
  568. {
  569. int first = 0, last = nr_ofs_deltas;
  570. while (first < last) {
  571. int next = first + (last - first) / 2;
  572. struct ofs_delta_entry *delta = &ofs_deltas[next];
  573. int cmp;
  574. cmp = compare_ofs_delta_bases(offset, delta->offset,
  575. OBJ_OFS_DELTA,
  576. objects[delta->obj_no].type);
  577. if (!cmp)
  578. return next;
  579. if (cmp < 0) {
  580. last = next;
  581. continue;
  582. }
  583. first = next+1;
  584. }
  585. return -first-1;
  586. }
  587. static void find_ofs_delta_children(off_t offset,
  588. int *first_index, int *last_index)
  589. {
  590. int first = find_ofs_delta(offset);
  591. int last = first;
  592. int end = nr_ofs_deltas - 1;
  593. if (first < 0) {
  594. *first_index = 0;
  595. *last_index = -1;
  596. return;
  597. }
  598. while (first > 0 && ofs_deltas[first - 1].offset == offset)
  599. --first;
  600. while (last < end && ofs_deltas[last + 1].offset == offset)
  601. ++last;
  602. *first_index = first;
  603. *last_index = last;
  604. }
  605. static int compare_ref_delta_bases(const struct object_id *oid1,
  606. const struct object_id *oid2,
  607. enum object_type type1,
  608. enum object_type type2)
  609. {
  610. int cmp = type1 - type2;
  611. if (cmp)
  612. return cmp;
  613. return oidcmp(oid1, oid2);
  614. }
  615. static int find_ref_delta(const struct object_id *oid)
  616. {
  617. int first = 0, last = nr_ref_deltas;
  618. while (first < last) {
  619. int next = first + (last - first) / 2;
  620. struct ref_delta_entry *delta = &ref_deltas[next];
  621. int cmp;
  622. cmp = compare_ref_delta_bases(oid, &delta->oid,
  623. OBJ_REF_DELTA,
  624. objects[delta->obj_no].type);
  625. if (!cmp)
  626. return next;
  627. if (cmp < 0) {
  628. last = next;
  629. continue;
  630. }
  631. first = next+1;
  632. }
  633. return -first-1;
  634. }
  635. static void find_ref_delta_children(const struct object_id *oid,
  636. int *first_index, int *last_index)
  637. {
  638. int first = find_ref_delta(oid);
  639. int last = first;
  640. int end = nr_ref_deltas - 1;
  641. if (first < 0) {
  642. *first_index = 0;
  643. *last_index = -1;
  644. return;
  645. }
  646. while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid))
  647. --first;
  648. while (last < end && oideq(&ref_deltas[last + 1].oid, oid))
  649. ++last;
  650. *first_index = first;
  651. *last_index = last;
  652. }
  653. struct compare_data {
  654. struct object_entry *entry;
  655. struct git_istream *st;
  656. unsigned char *buf;
  657. unsigned long buf_size;
  658. };
  659. static int compare_objects(const unsigned char *buf, unsigned long size,
  660. void *cb_data)
  661. {
  662. struct compare_data *data = cb_data;
  663. if (data->buf_size < size) {
  664. free(data->buf);
  665. data->buf = xmalloc(size);
  666. data->buf_size = size;
  667. }
  668. while (size) {
  669. ssize_t len = read_istream(data->st, data->buf, size);
  670. if (len == 0)
  671. die(_("SHA1 COLLISION FOUND WITH %s !"),
  672. oid_to_hex(&data->entry->idx.oid));
  673. if (len < 0)
  674. die(_("unable to read %s"),
  675. oid_to_hex(&data->entry->idx.oid));
  676. if (memcmp(buf, data->buf, len))
  677. die(_("SHA1 COLLISION FOUND WITH %s !"),
  678. oid_to_hex(&data->entry->idx.oid));
  679. size -= len;
  680. buf += len;
  681. }
  682. return 0;
  683. }
  684. static int check_collison(struct object_entry *entry)
  685. {
  686. struct compare_data data;
  687. enum object_type type;
  688. unsigned long size;
  689. if (entry->size <= big_file_threshold || entry->type != OBJ_BLOB)
  690. return -1;
  691. memset(&data, 0, sizeof(data));
  692. data.entry = entry;
  693. data.st = open_istream(the_repository, &entry->idx.oid, &type, &size,
  694. NULL);
  695. if (!data.st)
  696. return -1;
  697. if (size != entry->size || type != entry->type)
  698. die(_("SHA1 COLLISION FOUND WITH %s !"),
  699. oid_to_hex(&entry->idx.oid));
  700. unpack_data(entry, compare_objects, &data);
  701. close_istream(data.st);
  702. free(data.buf);
  703. return 0;
  704. }
  705. static void sha1_object(const void *data, struct object_entry *obj_entry,
  706. unsigned long size, enum object_type type,
  707. const struct object_id *oid)
  708. {
  709. void *new_data = NULL;
  710. int collision_test_needed = 0;
  711. assert(data || obj_entry);
  712. if (startup_info->have_repository) {
  713. read_lock();
  714. collision_test_needed =
  715. has_object_file_with_flags(oid, OBJECT_INFO_QUICK);
  716. read_unlock();
  717. }
  718. if (collision_test_needed && !data) {
  719. read_lock();
  720. if (!check_collison(obj_entry))
  721. collision_test_needed = 0;
  722. read_unlock();
  723. }
  724. if (collision_test_needed) {
  725. void *has_data;
  726. enum object_type has_type;
  727. unsigned long has_size;
  728. read_lock();
  729. has_type = oid_object_info(the_repository, oid, &has_size);
  730. if (has_type < 0)
  731. die(_("cannot read existing object info %s"), oid_to_hex(oid));
  732. if (has_type != type || has_size != size)
  733. die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
  734. has_data = read_object_file(oid, &has_type, &has_size);
  735. read_unlock();
  736. if (!data)
  737. data = new_data = get_data_from_pack(obj_entry);
  738. if (!has_data)
  739. die(_("cannot read existing object %s"), oid_to_hex(oid));
  740. if (size != has_size || type != has_type ||
  741. memcmp(data, has_data, size) != 0)
  742. die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
  743. free(has_data);
  744. }
  745. if (strict || do_fsck_object) {
  746. read_lock();
  747. if (type == OBJ_BLOB) {
  748. struct blob *blob = lookup_blob(the_repository, oid);
  749. if (blob)
  750. blob->object.flags |= FLAG_CHECKED;
  751. else
  752. die(_("invalid blob object %s"), oid_to_hex(oid));
  753. if (do_fsck_object &&
  754. fsck_object(&blob->object, (void *)data, size, &fsck_options))
  755. die(_("fsck error in packed object"));
  756. } else {
  757. struct object *obj;
  758. int eaten;
  759. void *buf = (void *) data;
  760. assert(data && "data can only be NULL for large _blobs_");
  761. /*
  762. * we do not need to free the memory here, as the
  763. * buf is deleted by the caller.
  764. */
  765. obj = parse_object_buffer(the_repository, oid, type,
  766. size, buf,
  767. &eaten);
  768. if (!obj)
  769. die(_("invalid %s"), type_name(type));
  770. if (do_fsck_object &&
  771. fsck_object(obj, buf, size, &fsck_options))
  772. die(_("fsck error in packed object"));
  773. if (strict && fsck_walk(obj, NULL, &fsck_options))
  774. die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
  775. if (obj->type == OBJ_TREE) {
  776. struct tree *item = (struct tree *) obj;
  777. item->buffer = NULL;
  778. obj->parsed = 0;
  779. }
  780. if (obj->type == OBJ_COMMIT) {
  781. struct commit *commit = (struct commit *) obj;
  782. if (detach_commit_buffer(commit, NULL) != data)
  783. BUG("parse_object_buffer transmogrified our buffer");
  784. }
  785. obj->flags |= FLAG_CHECKED;
  786. }
  787. read_unlock();
  788. }
  789. free(new_data);
  790. }
  791. /*
  792. * Walk from current node up
  793. * to top parent if necessary to deflate the node. In normal
  794. * situation, its parent node would be already deflated, so it just
  795. * needs to apply delta.
  796. *
  797. * In the worst case scenario, parent node is no longer deflated because
  798. * we're running out of delta_base_cache_limit; we need to re-deflate
  799. * parents, possibly up to the top base.
  800. *
  801. * All deflated objects here are subject to be freed if we exceed
  802. * delta_base_cache_limit, just like in find_unresolved_deltas(), we
  803. * just need to make sure the last node is not freed.
  804. */
  805. static void *get_base_data(struct base_data *c)
  806. {
  807. if (!c->data) {
  808. struct object_entry *obj = c->obj;
  809. struct base_data **delta = NULL;
  810. int delta_nr = 0, delta_alloc = 0;
  811. while (is_delta_type(c->obj->type) && !c->data) {
  812. ALLOC_GROW(delta, delta_nr + 1, delta_alloc);
  813. delta[delta_nr++] = c;
  814. c = c->base;
  815. }
  816. if (!delta_nr) {
  817. c->data = get_data_from_pack(obj);
  818. c->size = obj->size;
  819. base_cache_used += c->size;
  820. prune_base_data(c);
  821. }
  822. for (; delta_nr > 0; delta_nr--) {
  823. void *base, *raw;
  824. c = delta[delta_nr - 1];
  825. obj = c->obj;
  826. base = get_base_data(c->base);
  827. raw = get_data_from_pack(obj);
  828. c->data = patch_delta(
  829. base, c->base->size,
  830. raw, obj->size,
  831. &c->size);
  832. free(raw);
  833. if (!c->data)
  834. bad_object(obj->idx.offset, _("failed to apply delta"));
  835. base_cache_used += c->size;
  836. prune_base_data(c);
  837. }
  838. free(delta);
  839. }
  840. return c->data;
  841. }
  842. static struct base_data *make_base(struct object_entry *obj,
  843. struct base_data *parent)
  844. {
  845. struct base_data *base = xcalloc(1, sizeof(struct base_data));
  846. base->base = parent;
  847. base->obj = obj;
  848. find_ref_delta_children(&obj->idx.oid,
  849. &base->ref_first, &base->ref_last);
  850. find_ofs_delta_children(obj->idx.offset,
  851. &base->ofs_first, &base->ofs_last);
  852. base->children_remaining = base->ref_last - base->ref_first +
  853. base->ofs_last - base->ofs_first + 2;
  854. return base;
  855. }
  856. static struct base_data *resolve_delta(struct object_entry *delta_obj,
  857. struct base_data *base)
  858. {
  859. void *delta_data, *result_data;
  860. struct base_data *result;
  861. unsigned long result_size;
  862. if (show_stat) {
  863. int i = delta_obj - objects;
  864. int j = base->obj - objects;
  865. obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1;
  866. deepest_delta_lock();
  867. if (deepest_delta < obj_stat[i].delta_depth)
  868. deepest_delta = obj_stat[i].delta_depth;
  869. deepest_delta_unlock();
  870. obj_stat[i].base_object_no = j;
  871. }
  872. delta_data = get_data_from_pack(delta_obj);
  873. assert(base->data);
  874. result_data = patch_delta(base->data, base->size,
  875. delta_data, delta_obj->size, &result_size);
  876. free(delta_data);
  877. if (!result_data)
  878. bad_object(delta_obj->idx.offset, _("failed to apply delta"));
  879. hash_object_file(the_hash_algo, result_data, result_size,
  880. type_name(delta_obj->real_type), &delta_obj->idx.oid);
  881. sha1_object(result_data, NULL, result_size, delta_obj->real_type,
  882. &delta_obj->idx.oid);
  883. result = make_base(delta_obj, base);
  884. result->data = result_data;
  885. result->size = result_size;
  886. counter_lock();
  887. nr_resolved_deltas++;
  888. counter_unlock();
  889. return result;
  890. }
  891. static int compare_ofs_delta_entry(const void *a, const void *b)
  892. {
  893. const struct ofs_delta_entry *delta_a = a;
  894. const struct ofs_delta_entry *delta_b = b;
  895. return delta_a->offset < delta_b->offset ? -1 :
  896. delta_a->offset > delta_b->offset ? 1 :
  897. 0;
  898. }
  899. static int compare_ref_delta_entry(const void *a, const void *b)
  900. {
  901. const struct ref_delta_entry *delta_a = a;
  902. const struct ref_delta_entry *delta_b = b;
  903. return oidcmp(&delta_a->oid, &delta_b->oid);
  904. }
  905. static void *threaded_second_pass(void *data)
  906. {
  907. if (data)
  908. set_thread_data(data);
  909. for (;;) {
  910. struct base_data *parent = NULL;
  911. struct object_entry *child_obj;
  912. struct base_data *child;
  913. work_lock();
  914. if (list_empty(&work_head)) {
  915. /*
  916. * Take an object from the object array.
  917. */
  918. while (nr_dispatched < nr_objects &&
  919. is_delta_type(objects[nr_dispatched].type))
  920. nr_dispatched++;
  921. if (nr_dispatched >= nr_objects) {
  922. work_unlock();
  923. break;
  924. }
  925. child_obj = &objects[nr_dispatched++];
  926. } else {
  927. /*
  928. * Peek at the top of the stack, and take a child from
  929. * it.
  930. */
  931. parent = list_first_entry(&work_head, struct base_data,
  932. list);
  933. if (parent->ref_first <= parent->ref_last) {
  934. int offset = ref_deltas[parent->ref_first++].obj_no;
  935. child_obj = objects + offset;
  936. if (child_obj->real_type != OBJ_REF_DELTA)
  937. die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
  938. (uintmax_t) child_obj->idx.offset,
  939. oid_to_hex(&parent->obj->idx.oid));
  940. child_obj->real_type = parent->obj->real_type;
  941. } else {
  942. child_obj = objects +
  943. ofs_deltas[parent->ofs_first++].obj_no;
  944. assert(child_obj->real_type == OBJ_OFS_DELTA);
  945. child_obj->real_type = parent->obj->real_type;
  946. }
  947. if (parent->ref_first > parent->ref_last &&
  948. parent->ofs_first > parent->ofs_last) {
  949. /*
  950. * This parent has run out of children, so move
  951. * it to done_head.
  952. */
  953. list_del(&parent->list);
  954. list_add(&parent->list, &done_head);
  955. }
  956. /*
  957. * Ensure that the parent has data, since we will need
  958. * it later.
  959. *
  960. * NEEDSWORK: If parent data needs to be reloaded, this
  961. * prolongs the time that the current thread spends in
  962. * the mutex. A mitigating factor is that parent data
  963. * needs to be reloaded only if the delta base cache
  964. * limit is exceeded, so in the typical case, this does
  965. * not happen.
  966. */
  967. get_base_data(parent);
  968. parent->retain_data++;
  969. }
  970. work_unlock();
  971. if (parent) {
  972. child = resolve_delta(child_obj, parent);
  973. if (!child->children_remaining)
  974. FREE_AND_NULL(child->data);
  975. } else {
  976. child = make_base(child_obj, NULL);
  977. if (child->children_remaining) {
  978. /*
  979. * Since this child has its own delta children,
  980. * we will need this data in the future.
  981. * Inflate now so that future iterations will
  982. * have access to this object's data while
  983. * outside the work mutex.
  984. */
  985. child->data = get_data_from_pack(child_obj);
  986. child->size = child_obj->size;
  987. }
  988. }
  989. work_lock();
  990. if (parent)
  991. parent->retain_data--;
  992. if (child->data) {
  993. /*
  994. * This child has its own children, so add it to
  995. * work_head.
  996. */
  997. list_add(&child->list, &work_head);
  998. base_cache_used += child->size;
  999. prune_base_data(NULL);
  1000. } else {
  1001. /*
  1002. * This child does not have its own children. It may be
  1003. * the last descendant of its ancestors; free those
  1004. * that we can.
  1005. */
  1006. struct base_data *p = parent;
  1007. while (p) {
  1008. struct base_data *next_p;
  1009. p->children_remaining--;
  1010. if (p->children_remaining)
  1011. break;
  1012. next_p = p->base;
  1013. free_base_data(p);
  1014. list_del(&p->list);
  1015. free(p);
  1016. p = next_p;
  1017. }
  1018. }
  1019. work_unlock();
  1020. }
  1021. return NULL;
  1022. }
  1023. /*
  1024. * First pass:
  1025. * - find locations of all objects;
  1026. * - calculate SHA1 of all non-delta objects;
  1027. * - remember base (SHA1 or offset) for all deltas.
  1028. */
  1029. static void parse_pack_objects(unsigned char *hash)
  1030. {
  1031. int i, nr_delays = 0;
  1032. struct ofs_delta_entry *ofs_delta = ofs_deltas;
  1033. struct object_id ref_delta_oid;
  1034. struct stat st;
  1035. if (verbose)
  1036. progress = start_progress(
  1037. from_stdin ? _("Receiving objects") : _("Indexing objects"),
  1038. nr_objects);
  1039. for (i = 0; i < nr_objects; i++) {
  1040. struct object_entry *obj = &objects[i];
  1041. void *data = unpack_raw_entry(obj, &ofs_delta->offset,
  1042. &ref_delta_oid,
  1043. &obj->idx.oid);
  1044. obj->real_type = obj->type;
  1045. if (obj->type == OBJ_OFS_DELTA) {
  1046. nr_ofs_deltas++;
  1047. ofs_delta->obj_no = i;
  1048. ofs_delta++;
  1049. } else if (obj->type == OBJ_REF_DELTA) {
  1050. ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc);
  1051. oidcpy(&ref_deltas[nr_ref_deltas].oid, &ref_delta_oid);
  1052. ref_deltas[nr_ref_deltas].obj_no = i;
  1053. nr_ref_deltas++;
  1054. } else if (!data) {
  1055. /* large blobs, check later */
  1056. obj->real_type = OBJ_BAD;
  1057. nr_delays++;
  1058. } else
  1059. sha1_object(data, NULL, obj->size, obj->type,
  1060. &obj->idx.oid);
  1061. free(data);
  1062. display_progress(progress, i+1);
  1063. }
  1064. objects[i].idx.offset = consumed_bytes;
  1065. stop_progress(&progress);
  1066. /* Check pack integrity */
  1067. flush();
  1068. the_hash_algo->final_fn(hash, &input_ctx);
  1069. if (!hasheq(fill(the_hash_algo->rawsz), hash))
  1070. die(_("pack is corrupted (SHA1 mismatch)"));
  1071. use(the_hash_algo->rawsz);
  1072. /* If input_fd is a file, we should have reached its end now. */
  1073. if (fstat(input_fd, &st))
  1074. die_errno(_("cannot fstat packfile"));
  1075. if (S_ISREG(st.st_mode) &&
  1076. lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
  1077. die(_("pack has junk at the end"));
  1078. for (i = 0; i < nr_objects; i++) {
  1079. struct object_entry *obj = &objects[i];
  1080. if (obj->real_type != OBJ_BAD)
  1081. continue;
  1082. obj->real_type = obj->type;
  1083. sha1_object(NULL, obj, obj->size, obj->type,
  1084. &obj->idx.oid);
  1085. nr_delays--;
  1086. }
  1087. if (nr_delays)
  1088. die(_("confusion beyond insanity in parse_pack_objects()"));
  1089. }
  1090. /*
  1091. * Second pass:
  1092. * - for all non-delta objects, look if it is used as a base for
  1093. * deltas;
  1094. * - if used as a base, uncompress the object and apply all deltas,
  1095. * recursively checking if the resulting object is used as a base
  1096. * for some more deltas.
  1097. */
  1098. static void resolve_deltas(void)
  1099. {
  1100. int i;
  1101. if (!nr_ofs_deltas && !nr_ref_deltas)
  1102. return;
  1103. /* Sort deltas by base SHA1/offset for fast searching */
  1104. QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry);
  1105. QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry);
  1106. if (verbose || show_resolving_progress)
  1107. progress = start_progress(_("Resolving deltas"),
  1108. nr_ref_deltas + nr_ofs_deltas);
  1109. nr_dispatched = 0;
  1110. base_cache_limit = delta_base_cache_limit * nr_threads;
  1111. if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
  1112. init_thread();
  1113. for (i = 0; i < nr_threads; i++) {
  1114. int ret = pthread_create(&thread_data[i].thread, NULL,
  1115. threaded_second_pass, thread_data + i);
  1116. if (ret)
  1117. die(_("unable to create thread: %s"),
  1118. strerror(ret));
  1119. }
  1120. for (i = 0; i < nr_threads; i++)
  1121. pthread_join(thread_data[i].thread, NULL);
  1122. cleanup_thread();
  1123. return;
  1124. }
  1125. threaded_second_pass(&nothread_data);
  1126. }
  1127. /*
  1128. * Third pass:
  1129. * - append objects to convert thin pack to full pack if required
  1130. * - write the final pack hash
  1131. */
  1132. static void fix_unresolved_deltas(struct hashfile *f);
  1133. static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash)
  1134. {
  1135. if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
  1136. stop_progress(&progress);
  1137. /* Flush remaining pack final hash. */
  1138. flush();
  1139. return;
  1140. }
  1141. if (fix_thin_pack) {
  1142. struct hashfile *f;
  1143. unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ];
  1144. struct strbuf msg = STRBUF_INIT;
  1145. int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
  1146. int nr_objects_initial = nr_objects;
  1147. if (nr_unresolved <= 0)
  1148. die(_("confusion beyond insanity"));
  1149. REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
  1150. memset(objects + nr_objects + 1, 0,
  1151. nr_unresolved * sizeof(*objects));
  1152. f = hashfd(output_fd, curr_pack);
  1153. fix_unresolved_deltas(f);
  1154. strbuf_addf(&msg, Q_("completed with %d local object",
  1155. "completed with %d local objects",
  1156. nr_objects - nr_objects_initial),
  1157. nr_objects - nr_objects_initial);
  1158. stop_progress_msg(&progress, msg.buf);
  1159. strbuf_release(&msg);
  1160. finalize_hashfile(f, tail_hash, 0);
  1161. hashcpy(read_hash, pack_hash);
  1162. fixup_pack_header_footer(output_fd, pack_hash,
  1163. curr_pack, nr_objects,
  1164. read_hash, consumed_bytes-the_hash_algo->rawsz);
  1165. if (!hasheq(read_hash, tail_hash))
  1166. die(_("Unexpected tail checksum for %s "
  1167. "(disk corruption?)"), curr_pack);
  1168. }
  1169. if (nr_ofs_deltas + nr_ref_deltas != nr_resolved_deltas)
  1170. die(Q_("pack has %d unresolved delta",
  1171. "pack has %d unresolved deltas",
  1172. nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas),
  1173. nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
  1174. }
  1175. static int write_compressed(struct hashfile *f, void *in, unsigned int size)
  1176. {
  1177. git_zstream stream;
  1178. int status;
  1179. unsigned char outbuf[4096];
  1180. git_deflate_init(&stream, zlib_compression_level);
  1181. stream.next_in = in;
  1182. stream.avail_in = size;
  1183. do {
  1184. stream.next_out = outbuf;
  1185. stream.avail_out = sizeof(outbuf);
  1186. status = git_deflate(&stream, Z_FINISH);
  1187. hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out);
  1188. } while (status == Z_OK);
  1189. if (status != Z_STREAM_END)
  1190. die(_("unable to deflate appended object (%d)"), status);
  1191. size = stream.total_out;
  1192. git_deflate_end(&stream);
  1193. return size;
  1194. }
  1195. static struct object_entry *append_obj_to_pack(struct hashfile *f,
  1196. const unsigned char *sha1, void *buf,
  1197. unsigned long size, enum object_type type)
  1198. {
  1199. struct object_entry *obj = &objects[nr_objects++];
  1200. unsigned char header[10];
  1201. unsigned long s = size;
  1202. int n = 0;
  1203. unsigned char c = (type << 4) | (s & 15);
  1204. s >>= 4;
  1205. while (s) {
  1206. header[n++] = c | 0x80;
  1207. c = s & 0x7f;
  1208. s >>= 7;
  1209. }
  1210. header[n++] = c;
  1211. crc32_begin(f);
  1212. hashwrite(f, header, n);
  1213. obj[0].size = size;
  1214. obj[0].hdr_size = n;
  1215. obj[0].type = type;
  1216. obj[0].real_type = type;
  1217. obj[1].idx.offset = obj[0].idx.offset + n;
  1218. obj[1].idx.offset += write_compressed(f, buf, size);
  1219. obj[0].idx.crc32 = crc32_end(f);
  1220. hashflush(f);
  1221. hashcpy(obj->idx.oid.hash, sha1);
  1222. return obj;
  1223. }
  1224. static int delta_pos_compare(const void *_a, const void *_b)
  1225. {
  1226. struct ref_delta_entry *a = *(struct ref_delta_entry **)_a;
  1227. struct ref_delta_entry *b = *(struct ref_delta_entry **)_b;
  1228. return a->obj_no - b->obj_no;
  1229. }
  1230. static void fix_unresolved_deltas(struct hashfile *f)
  1231. {
  1232. struct ref_delta_entry **sorted_by_pos;
  1233. int i;
  1234. /*
  1235. * Since many unresolved deltas may well be themselves base objects
  1236. * for more unresolved deltas, we really want to include the
  1237. * smallest number of base objects that would cover as much delta
  1238. * as possible by picking the
  1239. * trunc deltas first, allowing for other deltas to resolve without
  1240. * additional base objects. Since most base objects are to be found
  1241. * before deltas depending on them, a good heuristic is to start
  1242. * resolving deltas in the same order as their position in the pack.
  1243. */
  1244. ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
  1245. for (i = 0; i < nr_ref_deltas; i++)
  1246. sorted_by_pos[i] = &ref_deltas[i];
  1247. QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare);
  1248. if (has_promisor_remote()) {
  1249. /*
  1250. * Prefetch the delta bases.
  1251. */
  1252. struct oid_array to_fetch = OID_ARRAY_INIT;
  1253. for (i = 0; i < nr_ref_deltas; i++) {
  1254. struct ref_delta_entry *d = sorted_by_pos[i];
  1255. if (!oid_object_info_extended(the_repository, &d->oid,
  1256. NULL,
  1257. OBJECT_INFO_FOR_PREFETCH))
  1258. continue;
  1259. oid_array_append(&to_fetch, &d->oid);
  1260. }
  1261. promisor_remote_get_direct(the_repository,
  1262. to_fetch.oid, to_fetch.nr);
  1263. oid_array_clear(&to_fetch);
  1264. }
  1265. for (i = 0; i < nr_ref_deltas; i++) {
  1266. struct ref_delta_entry *d = sorted_by_pos[i];
  1267. enum object_type type;
  1268. void *data;
  1269. unsigned long size;
  1270. if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
  1271. continue;
  1272. data = read_object_file(&d->oid, &type, &size);
  1273. if (!data)
  1274. continue;
  1275. if (check_object_signature(the_repository, &d->oid,
  1276. data, size,
  1277. type_name(type)))
  1278. die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
  1279. /*
  1280. * Add this as an object to the objects array and call
  1281. * threaded_second_pass() (which will pick up the added
  1282. * object).
  1283. */
  1284. append_obj_to_pack(f, d->oid.hash, data, size, type);
  1285. threaded_second_pass(NULL);
  1286. display_progress(progress, nr_resolved_deltas);
  1287. }
  1288. free(sorted_by_pos);
  1289. }
  1290. static const char *derive_filename(const char *pack_name, const char *suffix,
  1291. struct strbuf *buf)
  1292. {
  1293. size_t len;
  1294. if (!strip_suffix(pack_name, ".pack", &len))
  1295. die(_("packfile name '%s' does not end with '.pack'"),
  1296. pack_name);
  1297. strbuf_add(buf, pack_name, len);
  1298. strbuf_addch(buf, '.');
  1299. strbuf_addstr(buf, suffix);
  1300. return buf->buf;
  1301. }
  1302. static void write_special_file(const char *suffix, const char *msg,
  1303. const char *pack_name, const unsigned char *hash,
  1304. const char **report)
  1305. {
  1306. struct strbuf name_buf = STRBUF_INIT;
  1307. const char *filename;
  1308. int fd;
  1309. int msg_len = strlen(msg);
  1310. if (pack_name)
  1311. filename = derive_filename(pack_name, suffix, &name_buf);
  1312. else
  1313. filename = odb_pack_name(&name_buf, hash, suffix);
  1314. fd = odb_pack_keep(filename);
  1315. if (fd < 0) {
  1316. if (errno != EEXIST)
  1317. die_errno(_("cannot write %s file '%s'"),
  1318. suffix, filename);
  1319. } else {
  1320. if (msg_len > 0) {
  1321. write_or_die(fd, msg, msg_len);
  1322. write_or_die(fd, "\n", 1);
  1323. }
  1324. if (close(fd) != 0)
  1325. die_errno(_("cannot close written %s file '%s'"),
  1326. suffix, filename);
  1327. if (report)
  1328. *report = suffix;
  1329. }
  1330. strbuf_release(&name_buf);
  1331. }
  1332. static void final(const char *final_pack_name, const char *curr_pack_name,
  1333. const char *final_index_name, const char *curr_index_name,
  1334. const char *keep_msg, const char *promisor_msg,
  1335. unsigned char *hash)
  1336. {
  1337. const char *report = "pack";
  1338. struct strbuf pack_name = STRBUF_INIT;
  1339. struct strbuf index_name = STRBUF_INIT;
  1340. int err;
  1341. if (!from_stdin) {
  1342. close(input_fd);
  1343. } else {
  1344. fsync_or_die(output_fd, curr_pack_name);
  1345. err = close(output_fd);
  1346. if (err)
  1347. die_errno(_("error while closing pack file"));
  1348. }
  1349. if (keep_msg)
  1350. write_special_file("keep", keep_msg, final_pack_name, hash,
  1351. &report);
  1352. if (promisor_msg)
  1353. write_special_file("promisor", promisor_msg, final_pack_name,
  1354. hash, NULL);
  1355. if (final_pack_name != curr_pack_name) {
  1356. if (!final_pack_name)
  1357. final_pack_name = odb_pack_name(&pack_name, hash, "pack");
  1358. if (finalize_object_file(curr_pack_name, final_pack_name))
  1359. die(_("cannot store pack file"));
  1360. } else if (from_stdin)
  1361. chmod(final_pack_name, 0444);
  1362. if (final_index_name != curr_index_name) {
  1363. if (!final_index_name)
  1364. final_index_name = odb_pack_name(&index_name, hash, "idx");
  1365. if (finalize_object_file(curr_index_name, final_index_name))
  1366. die(_("cannot store index file"));
  1367. } else
  1368. chmod(final_index_name, 0444);
  1369. if (do_fsck_object) {
  1370. struct packed_git *p;
  1371. p = add_packed_git(final_index_name, strlen(final_index_name), 0);
  1372. if (p)
  1373. install_packed_git(the_repository, p);
  1374. }
  1375. if (!from_stdin) {
  1376. printf("%s\n", hash_to_hex(hash));
  1377. } else {
  1378. struct strbuf buf = STRBUF_INIT;
  1379. strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash));
  1380. write_or_die(1, buf.buf, buf.len);
  1381. strbuf_release(&buf);
  1382. /*
  1383. * Let's just mimic git-unpack-objects here and write
  1384. * the last part of the input buffer to stdout.
  1385. */
  1386. while (input_len) {
  1387. err = xwrite(1, input_buffer + input_offset, input_len);
  1388. if (err <= 0)
  1389. break;
  1390. input_len -= err;
  1391. input_offset += err;
  1392. }
  1393. }
  1394. strbuf_release(&index_name);
  1395. strbuf_release(&pack_name);
  1396. }
  1397. static int git_index_pack_config(const char *k, const char *v, void *cb)
  1398. {
  1399. struct pack_idx_option *opts = cb;
  1400. if (!strcmp(k, "pack.indexversion")) {
  1401. opts->version = git_config_int(k, v);
  1402. if (opts->version > 2)
  1403. die(_("bad pack.indexversion=%"PRIu32), opts->version);
  1404. return 0;
  1405. }
  1406. if (!strcmp(k, "pack.threads")) {
  1407. nr_threads = git_config_int(k, v);
  1408. if (nr_threads < 0)
  1409. die(_("invalid number of threads specified (%d)"),
  1410. nr_threads);
  1411. if (!HAVE_THREADS && nr_threads != 1) {
  1412. warning(_("no threads support, ignoring %s"), k);
  1413. nr_threads = 1;
  1414. }
  1415. return 0;
  1416. }
  1417. return git_default_config(k, v, cb);
  1418. }
  1419. static int cmp_uint32(const void *a_, const void *b_)
  1420. {
  1421. uint32_t a = *((uint32_t *)a_);
  1422. uint32_t b = *((uint32_t *)b_);
  1423. return (a < b) ? -1 : (a != b);
  1424. }
  1425. static void read_v2_anomalous_offsets(struct packed_git *p,
  1426. struct pack_idx_option *opts)
  1427. {
  1428. const uint32_t *idx1, *idx2;
  1429. uint32_t i;
  1430. /* The address of the 4-byte offset table */
  1431. idx1 = (((const uint32_t *)((const uint8_t *)p->index_data + p->crc_offset))
  1432. + p->num_objects /* CRC32 table */
  1433. );
  1434. /* The address of the 8-byte offset table */
  1435. idx2 = idx1 + p->num_objects;
  1436. for (i = 0; i < p->num_objects; i++) {
  1437. uint32_t off = ntohl(idx1[i]);
  1438. if (!(off & 0x80000000))
  1439. continue;
  1440. off = off & 0x7fffffff;
  1441. check_pack_index_ptr(p, &idx2[off * 2]);
  1442. if (idx2[off * 2])
  1443. continue;
  1444. /*
  1445. * The real offset is ntohl(idx2[off * 2]) in high 4
  1446. * octets, and ntohl(idx2[off * 2 + 1]) in low 4
  1447. * octets. But idx2[off * 2] is Zero!!!
  1448. */
  1449. ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
  1450. opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
  1451. }
  1452. QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32);
  1453. }
  1454. static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
  1455. {
  1456. struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
  1457. if (!p)
  1458. die(_("Cannot open existing pack file '%s'"), pack_name);
  1459. if (open_pack_index(p))
  1460. die(_("Cannot open existing pack idx file for '%s'"), pack_name);
  1461. /* Read the attributes from the existing idx file */
  1462. opts->version = p->index_version;
  1463. if (opts->version == 2)
  1464. read_v2_anomalous_offsets(p, opts);
  1465. /*
  1466. * Get rid of the idx file as we do not need it anymore.
  1467. * NEEDSWORK: extract this bit from free_pack_by_name() in
  1468. * sha1-file.c, perhaps? It shouldn't matter very much as we
  1469. * know we haven't installed this pack (hence we never have
  1470. * read anything from it).
  1471. */
  1472. close_pack_index(p);
  1473. free(p);
  1474. }
  1475. static void show_pack_info(int stat_only)
  1476. {
  1477. int i, baseobjects = nr_objects - nr_ref_deltas - nr_ofs_deltas;
  1478. unsigned long *chain_histogram = NULL;
  1479. if (deepest_delta)
  1480. chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long));
  1481. for (i = 0; i < nr_objects; i++) {
  1482. struct object_entry *obj = &objects[i];
  1483. if (is_delta_type(obj->type))
  1484. chain_histogram[obj_stat[i].delta_depth - 1]++;
  1485. if (stat_only)
  1486. continue;
  1487. printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
  1488. oid_to_hex(&obj->idx.oid),
  1489. type_name(obj->real_type), (uintmax_t)obj->size,
  1490. (uintmax_t)(obj[1].idx.offset - obj->idx.offset),
  1491. (uintmax_t)obj->idx.offset);
  1492. if (is_delta_type(obj->type)) {
  1493. struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
  1494. printf(" %u %s", obj_stat[i].delta_depth,
  1495. oid_to_hex(&bobj->idx.oid));
  1496. }
  1497. putchar('\n');
  1498. }
  1499. if (baseobjects)
  1500. printf_ln(Q_("non delta: %d object",
  1501. "non delta: %d objects",
  1502. baseobjects),
  1503. baseobjects);
  1504. for (i = 0; i < deepest_delta; i++) {
  1505. if (!chain_histogram[i])
  1506. continue;
  1507. printf_ln(Q_("chain length = %d: %lu object",
  1508. "chain length = %d: %lu objects",
  1509. chain_histogram[i]),
  1510. i + 1,
  1511. chain_histogram[i]);
  1512. }
  1513. }
  1514. int cmd_index_pack(int argc, const char **argv, const char *prefix)
  1515. {
  1516. int i, fix_thin_pack = 0, verify = 0, stat_only = 0;
  1517. const char *curr_index;
  1518. const char *index_name = NULL, *pack_name = NULL;
  1519. const char *keep_msg = NULL;
  1520. const char *promisor_msg = NULL;
  1521. struct strbuf index_name_buf = STRBUF_INIT;
  1522. struct pack_idx_entry **idx_objects;
  1523. struct pack_idx_option opts;
  1524. unsigned char pack_hash[GIT_MAX_RAWSZ];
  1525. unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
  1526. int report_end_of_input = 0;
  1527. int hash_algo = 0;
  1528. /*
  1529. * index-pack never needs to fetch missing objects except when
  1530. * REF_DELTA bases are missing (which are explicitly handled). It only
  1531. * accesses the repo to do hash collision checks and to check which
  1532. * REF_DELTA bases need to be fetched.
  1533. */
  1534. fetch_if_missing = 0;
  1535. if (argc == 2 && !strcmp(argv[1], "-h"))
  1536. usage(index_pack_usage);
  1537. read_replace_refs = 0;
  1538. fsck_options.walk = mark_link;
  1539. reset_pack_idx_option(&opts);
  1540. git_config(git_index_pack_config, &opts);
  1541. if (prefix && chdir(prefix))
  1542. die(_("Cannot come back to cwd"));
  1543. for (i = 1; i < argc; i++) {
  1544. const char *arg = argv[i];
  1545. if (*arg == '-') {
  1546. if (!strcmp(arg, "--stdin")) {
  1547. from_stdin = 1;
  1548. } else if (!strcmp(arg, "--fix-thin")) {
  1549. fix_thin_pack = 1;
  1550. } else if (skip_to_optional_arg(arg, "--strict", &arg)) {
  1551. strict = 1;
  1552. do_fsck_object = 1;
  1553. fsck_set_msg_types(&fsck_options, arg);
  1554. } else if (!strcmp(arg, "--check-self-contained-and-connected")) {
  1555. strict = 1;
  1556. check_self_contained_and_connected = 1;
  1557. } else if (!strcmp(arg, "--fsck-objects")) {
  1558. do_fsck_object = 1;
  1559. } else if (!strcmp(arg, "--verify")) {
  1560. verify = 1;
  1561. } else if (!strcmp(arg, "--verify-stat")) {
  1562. verify = 1;
  1563. show_stat = 1;
  1564. } else if (!strcmp(arg, "--verify-stat-only")) {
  1565. verify = 1;
  1566. show_stat = 1;
  1567. stat_only = 1;
  1568. } else if (skip_to_optional_arg(arg, "--keep", &keep_msg)) {
  1569. ; /* nothing to do */
  1570. } else if (skip_to_optional_arg(arg, "--promisor", &promisor_msg)) {
  1571. ; /* already parsed */
  1572. } else if (starts_with(arg, "--threads=")) {
  1573. char *end;
  1574. nr_threads = strtoul(arg+10, &end, 0);
  1575. if (!arg[10] || *end || nr_threads < 0)
  1576. usage(index_pack_usage);
  1577. if (!HAVE_THREADS && nr_threads != 1) {
  1578. warning(_("no threads support, ignoring %s"), arg);
  1579. nr_threads = 1;
  1580. }
  1581. } else if (starts_with(arg, "--pack_header=")) {
  1582. struct pack_header *hdr;
  1583. char *c;
  1584. hdr = (struct pack_header *)input_buffer;
  1585. hdr->hdr_signature = htonl(PACK_SIGNATURE);
  1586. hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
  1587. if (*c != ',')
  1588. die(_("bad %s"), arg);
  1589. hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
  1590. if (*c)
  1591. die(_("bad %s"), arg);
  1592. input_len = sizeof(*hdr);
  1593. } else if (!strcmp(arg, "-v")) {
  1594. verbose = 1;
  1595. } else if (!strcmp(arg, "--show-resolving-progress")) {
  1596. show_resolving_progress = 1;
  1597. } else if (!strcmp(arg, "--report-end-of-input")) {
  1598. report_end_of_input = 1;
  1599. } else if (!strcmp(arg, "-o")) {
  1600. if (index_name || (i+1) >= argc)
  1601. usage(index_pack_usage);
  1602. index_name = argv[++i];
  1603. } else if (starts_with(arg, "--index-version=")) {
  1604. char *c;
  1605. opts.version = strtoul(arg + 16, &c, 10);
  1606. if (opts.version > 2)
  1607. die(_("bad %s"), arg);
  1608. if (*c == ',')
  1609. opts.off32_limit = strtoul(c+1, &c, 0);
  1610. if (*c || opts.off32_limit & 0x80000000)
  1611. die(_("bad %s"), arg);
  1612. } else if (skip_prefix(arg, "--max-input-size=", &arg)) {
  1613. max_input_size = strtoumax(arg, NULL, 10);
  1614. } else if (skip_prefix(arg, "--object-format=", &arg)) {
  1615. hash_algo = hash_algo_by_name(arg);
  1616. if (hash_algo == GIT_HASH_UNKNOWN)
  1617. die(_("unknown hash algorithm '%s'"), arg);
  1618. repo_set_hash_algo(the_repository, hash_algo);
  1619. } else
  1620. usage(index_pack_usage);
  1621. continue;
  1622. }
  1623. if (pack_name)
  1624. usage(index_pack_usage);
  1625. pack_name = arg;
  1626. }
  1627. if (!pack_name && !from_stdin)
  1628. usage(index_pack_usage);
  1629. if (fix_thin_pack && !from_stdin)
  1630. die(_("--fix-thin cannot be used without --stdin"));
  1631. if (from_stdin && !startup_info->have_repository)
  1632. die(_("--stdin requires a git repository"));
  1633. if (from_stdin && hash_algo)
  1634. die(_("--object-format cannot be used with --stdin"));
  1635. if (!index_name && pack_name)
  1636. index_name = derive_filename(pack_name, "idx", &index_name_buf);
  1637. if (verify) {
  1638. if (!index_name)
  1639. die(_("--verify with no packfile name given"));
  1640. read_idx_option(&opts, index_name);
  1641. opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT;
  1642. }
  1643. if (strict)
  1644. opts.flags |= WRITE_IDX_STRICT;
  1645. if (HAVE_THREADS && !nr_threads) {
  1646. nr_threads = online_cpus();
  1647. /*
  1648. * Experiments show that going above 20 threads doesn't help,
  1649. * no matter how many cores you have. Below that, we tend to
  1650. * max at half the number of online_cpus(), presumably because
  1651. * half of those are hyperthreads rather than full cores. We'll
  1652. * never reduce the level below "3", though, to match a
  1653. * historical value that nobody complained about.
  1654. */
  1655. if (nr_threads < 4)
  1656. ; /* too few cores to consider capping */
  1657. else if (nr_threads < 6)
  1658. nr_threads = 3; /* historic cap */
  1659. else if (nr_threads < 40)
  1660. nr_threads /= 2;
  1661. else
  1662. nr_threads = 20; /* hard cap */
  1663. }
  1664. curr_pack = open_pack_file(pack_name);
  1665. parse_pack_header();
  1666. objects = xcalloc(st_add(nr_objects, 1), sizeof(struct object_entry));
  1667. if (show_stat)
  1668. obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
  1669. ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
  1670. parse_pack_objects(pack_hash);
  1671. if (report_end_of_input)
  1672. write_in_full(2, "\0", 1);
  1673. resolve_deltas();
  1674. conclude_pack(fix_thin_pack, curr_pack, pack_hash);
  1675. free(ofs_deltas);
  1676. free(ref_deltas);
  1677. if (strict)
  1678. foreign_nr = check_objects();
  1679. if (show_stat)
  1680. show_pack_info(stat_only);
  1681. ALLOC_ARRAY(idx_objects, nr_objects);
  1682. for (i = 0; i < nr_objects; i++)
  1683. idx_objects[i] = &objects[i].idx;
  1684. curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
  1685. free(idx_objects);
  1686. if (!verify)
  1687. final(pack_name, curr_pack,
  1688. index_name, curr_index,
  1689. keep_msg, promisor_msg,
  1690. pack_hash);
  1691. else
  1692. close(input_fd);
  1693. if (do_fsck_object && fsck_finish(&fsck_options))
  1694. die(_("fsck error in pack objects"));
  1695. free(objects);
  1696. strbuf_release(&index_name_buf);
  1697. if (pack_name == NULL)
  1698. free((void *) curr_pack);
  1699. if (index_name == NULL)
  1700. free((void *) curr_index);
  1701. /*
  1702. * Let the caller know this pack is not self contained
  1703. */
  1704. if (check_self_contained_and_connected && foreign_nr)
  1705. return 1;
  1706. return 0;
  1707. }