packfile.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  1. #include "cache.h"
  2. #include "list.h"
  3. #include "pack.h"
  4. #include "repository.h"
  5. #include "dir.h"
  6. #include "mergesort.h"
  7. #include "packfile.h"
  8. #include "delta.h"
  9. #include "streaming.h"
  10. #include "sha1-lookup.h"
  11. #include "commit.h"
  12. #include "object.h"
  13. #include "tag.h"
  14. #include "tree-walk.h"
  15. #include "tree.h"
  16. #include "object-store.h"
  17. #include "midx.h"
  18. #include "commit-graph.h"
  19. #include "promisor-remote.h"
  20. char *odb_pack_name(struct strbuf *buf,
  21. const unsigned char *hash,
  22. const char *ext)
  23. {
  24. strbuf_reset(buf);
  25. strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
  26. hash_to_hex(hash), ext);
  27. return buf->buf;
  28. }
  29. char *sha1_pack_name(const unsigned char *sha1)
  30. {
  31. static struct strbuf buf = STRBUF_INIT;
  32. return odb_pack_name(&buf, sha1, "pack");
  33. }
  34. char *sha1_pack_index_name(const unsigned char *sha1)
  35. {
  36. static struct strbuf buf = STRBUF_INIT;
  37. return odb_pack_name(&buf, sha1, "idx");
  38. }
  39. static unsigned int pack_used_ctr;
  40. static unsigned int pack_mmap_calls;
  41. static unsigned int peak_pack_open_windows;
  42. static unsigned int pack_open_windows;
  43. static unsigned int pack_open_fds;
  44. static unsigned int pack_max_fds;
  45. static size_t peak_pack_mapped;
  46. static size_t pack_mapped;
  47. #define SZ_FMT PRIuMAX
  48. static inline uintmax_t sz_fmt(size_t s) { return s; }
  49. void pack_report(void)
  50. {
  51. fprintf(stderr,
  52. "pack_report: getpagesize() = %10" SZ_FMT "\n"
  53. "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
  54. "pack_report: core.packedGitLimit = %10" SZ_FMT "\n",
  55. sz_fmt(getpagesize()),
  56. sz_fmt(packed_git_window_size),
  57. sz_fmt(packed_git_limit));
  58. fprintf(stderr,
  59. "pack_report: pack_used_ctr = %10u\n"
  60. "pack_report: pack_mmap_calls = %10u\n"
  61. "pack_report: pack_open_windows = %10u / %10u\n"
  62. "pack_report: pack_mapped = "
  63. "%10" SZ_FMT " / %10" SZ_FMT "\n",
  64. pack_used_ctr,
  65. pack_mmap_calls,
  66. pack_open_windows, peak_pack_open_windows,
  67. sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
  68. }
  69. /*
  70. * Open and mmap the index file at path, perform a couple of
  71. * consistency checks, then record its information to p. Return 0 on
  72. * success.
  73. */
  74. static int check_packed_git_idx(const char *path, struct packed_git *p)
  75. {
  76. void *idx_map;
  77. size_t idx_size;
  78. int fd = git_open(path), ret;
  79. struct stat st;
  80. const unsigned int hashsz = the_hash_algo->rawsz;
  81. if (fd < 0)
  82. return -1;
  83. if (fstat(fd, &st)) {
  84. close(fd);
  85. return -1;
  86. }
  87. idx_size = xsize_t(st.st_size);
  88. if (idx_size < 4 * 256 + hashsz + hashsz) {
  89. close(fd);
  90. return error("index file %s is too small", path);
  91. }
  92. idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
  93. close(fd);
  94. ret = load_idx(path, hashsz, idx_map, idx_size, p);
  95. if (ret)
  96. munmap(idx_map, idx_size);
  97. return ret;
  98. }
  99. int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
  100. size_t idx_size, struct packed_git *p)
  101. {
  102. struct pack_idx_header *hdr = idx_map;
  103. uint32_t version, nr, i, *index;
  104. if (idx_size < 4 * 256 + hashsz + hashsz)
  105. return error("index file %s is too small", path);
  106. if (idx_map == NULL)
  107. return error("empty data");
  108. if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
  109. version = ntohl(hdr->idx_version);
  110. if (version < 2 || version > 2)
  111. return error("index file %s is version %"PRIu32
  112. " and is not supported by this binary"
  113. " (try upgrading GIT to a newer version)",
  114. path, version);
  115. } else
  116. version = 1;
  117. nr = 0;
  118. index = idx_map;
  119. if (version > 1)
  120. index += 2; /* skip index header */
  121. for (i = 0; i < 256; i++) {
  122. uint32_t n = ntohl(index[i]);
  123. if (n < nr)
  124. return error("non-monotonic index %s", path);
  125. nr = n;
  126. }
  127. if (version == 1) {
  128. /*
  129. * Total size:
  130. * - 256 index entries 4 bytes each
  131. * - 24-byte entries * nr (object ID + 4-byte offset)
  132. * - hash of the packfile
  133. * - file checksum
  134. */
  135. if (idx_size != 4 * 256 + nr * (hashsz + 4) + hashsz + hashsz)
  136. return error("wrong index v1 file size in %s", path);
  137. } else if (version == 2) {
  138. /*
  139. * Minimum size:
  140. * - 8 bytes of header
  141. * - 256 index entries 4 bytes each
  142. * - object ID entry * nr
  143. * - 4-byte crc entry * nr
  144. * - 4-byte offset entry * nr
  145. * - hash of the packfile
  146. * - file checksum
  147. * And after the 4-byte offset table might be a
  148. * variable sized table containing 8-byte entries
  149. * for offsets larger than 2^31.
  150. */
  151. unsigned long min_size = 8 + 4*256 + nr*(hashsz + 4 + 4) + hashsz + hashsz;
  152. unsigned long max_size = min_size;
  153. if (nr)
  154. max_size += (nr - 1)*8;
  155. if (idx_size < min_size || idx_size > max_size)
  156. return error("wrong index v2 file size in %s", path);
  157. if (idx_size != min_size &&
  158. /*
  159. * make sure we can deal with large pack offsets.
  160. * 31-bit signed offset won't be enough, neither
  161. * 32-bit unsigned one will be.
  162. */
  163. (sizeof(off_t) <= 4))
  164. return error("pack too large for current definition of off_t in %s", path);
  165. p->crc_offset = 8 + 4 * 256 + nr * hashsz;
  166. }
  167. p->index_version = version;
  168. p->index_data = idx_map;
  169. p->index_size = idx_size;
  170. p->num_objects = nr;
  171. return 0;
  172. }
  173. int open_pack_index(struct packed_git *p)
  174. {
  175. char *idx_name;
  176. size_t len;
  177. int ret;
  178. if (p->index_data)
  179. return 0;
  180. if (!strip_suffix(p->pack_name, ".pack", &len))
  181. BUG("pack_name does not end in .pack");
  182. idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
  183. ret = check_packed_git_idx(idx_name, p);
  184. free(idx_name);
  185. return ret;
  186. }
  187. uint32_t get_pack_fanout(struct packed_git *p, uint32_t value)
  188. {
  189. const uint32_t *level1_ofs = p->index_data;
  190. if (!level1_ofs) {
  191. if (open_pack_index(p))
  192. return 0;
  193. level1_ofs = p->index_data;
  194. }
  195. if (p->index_version > 1) {
  196. level1_ofs += 2;
  197. }
  198. return ntohl(level1_ofs[value]);
  199. }
  200. static struct packed_git *alloc_packed_git(int extra)
  201. {
  202. struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
  203. memset(p, 0, sizeof(*p));
  204. p->pack_fd = -1;
  205. return p;
  206. }
  207. struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
  208. {
  209. const char *path = sha1_pack_name(sha1);
  210. size_t alloc = st_add(strlen(path), 1);
  211. struct packed_git *p = alloc_packed_git(alloc);
  212. memcpy(p->pack_name, path, alloc); /* includes NUL */
  213. hashcpy(p->hash, sha1);
  214. if (check_packed_git_idx(idx_path, p)) {
  215. free(p);
  216. return NULL;
  217. }
  218. return p;
  219. }
  220. static void scan_windows(struct packed_git *p,
  221. struct packed_git **lru_p,
  222. struct pack_window **lru_w,
  223. struct pack_window **lru_l)
  224. {
  225. struct pack_window *w, *w_l;
  226. for (w_l = NULL, w = p->windows; w; w = w->next) {
  227. if (!w->inuse_cnt) {
  228. if (!*lru_w || w->last_used < (*lru_w)->last_used) {
  229. *lru_p = p;
  230. *lru_w = w;
  231. *lru_l = w_l;
  232. }
  233. }
  234. w_l = w;
  235. }
  236. }
  237. static int unuse_one_window(struct packed_git *current)
  238. {
  239. struct packed_git *p, *lru_p = NULL;
  240. struct pack_window *lru_w = NULL, *lru_l = NULL;
  241. if (current)
  242. scan_windows(current, &lru_p, &lru_w, &lru_l);
  243. for (p = the_repository->objects->packed_git; p; p = p->next)
  244. scan_windows(p, &lru_p, &lru_w, &lru_l);
  245. if (lru_p) {
  246. munmap(lru_w->base, lru_w->len);
  247. pack_mapped -= lru_w->len;
  248. if (lru_l)
  249. lru_l->next = lru_w->next;
  250. else
  251. lru_p->windows = lru_w->next;
  252. free(lru_w);
  253. pack_open_windows--;
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. void close_pack_windows(struct packed_git *p)
  259. {
  260. while (p->windows) {
  261. struct pack_window *w = p->windows;
  262. if (w->inuse_cnt)
  263. die("pack '%s' still has open windows to it",
  264. p->pack_name);
  265. munmap(w->base, w->len);
  266. pack_mapped -= w->len;
  267. pack_open_windows--;
  268. p->windows = w->next;
  269. free(w);
  270. }
  271. }
  272. int close_pack_fd(struct packed_git *p)
  273. {
  274. if (p->pack_fd < 0)
  275. return 0;
  276. close(p->pack_fd);
  277. pack_open_fds--;
  278. p->pack_fd = -1;
  279. return 1;
  280. }
  281. void close_pack_index(struct packed_git *p)
  282. {
  283. if (p->index_data) {
  284. munmap((void *)p->index_data, p->index_size);
  285. p->index_data = NULL;
  286. }
  287. }
  288. void close_pack(struct packed_git *p)
  289. {
  290. close_pack_windows(p);
  291. close_pack_fd(p);
  292. close_pack_index(p);
  293. }
  294. void close_object_store(struct raw_object_store *o)
  295. {
  296. struct packed_git *p;
  297. for (p = o->packed_git; p; p = p->next)
  298. if (p->do_not_close)
  299. BUG("want to close pack marked 'do-not-close'");
  300. else
  301. close_pack(p);
  302. if (o->multi_pack_index) {
  303. close_midx(o->multi_pack_index);
  304. o->multi_pack_index = NULL;
  305. }
  306. close_commit_graph(o);
  307. }
  308. void unlink_pack_path(const char *pack_name, int force_delete)
  309. {
  310. static const char *exts[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
  311. int i;
  312. struct strbuf buf = STRBUF_INIT;
  313. size_t plen;
  314. strbuf_addstr(&buf, pack_name);
  315. strip_suffix_mem(buf.buf, &buf.len, ".pack");
  316. plen = buf.len;
  317. if (!force_delete) {
  318. strbuf_addstr(&buf, ".keep");
  319. if (!access(buf.buf, F_OK)) {
  320. strbuf_release(&buf);
  321. return;
  322. }
  323. }
  324. for (i = 0; i < ARRAY_SIZE(exts); i++) {
  325. strbuf_setlen(&buf, plen);
  326. strbuf_addstr(&buf, exts[i]);
  327. unlink(buf.buf);
  328. }
  329. strbuf_release(&buf);
  330. }
  331. /*
  332. * The LRU pack is the one with the oldest MRU window, preferring packs
  333. * with no used windows, or the oldest mtime if it has no windows allocated.
  334. */
  335. static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
  336. {
  337. struct pack_window *w, *this_mru_w;
  338. int has_windows_inuse = 0;
  339. /*
  340. * Reject this pack if it has windows and the previously selected
  341. * one does not. If this pack does not have windows, reject
  342. * it if the pack file is newer than the previously selected one.
  343. */
  344. if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
  345. return;
  346. for (w = this_mru_w = p->windows; w; w = w->next) {
  347. /*
  348. * Reject this pack if any of its windows are in use,
  349. * but the previously selected pack did not have any
  350. * inuse windows. Otherwise, record that this pack
  351. * has windows in use.
  352. */
  353. if (w->inuse_cnt) {
  354. if (*accept_windows_inuse)
  355. has_windows_inuse = 1;
  356. else
  357. return;
  358. }
  359. if (w->last_used > this_mru_w->last_used)
  360. this_mru_w = w;
  361. /*
  362. * Reject this pack if it has windows that have been
  363. * used more recently than the previously selected pack.
  364. * If the previously selected pack had windows inuse and
  365. * we have not encountered a window in this pack that is
  366. * inuse, skip this check since we prefer a pack with no
  367. * inuse windows to one that has inuse windows.
  368. */
  369. if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
  370. this_mru_w->last_used > (*mru_w)->last_used)
  371. return;
  372. }
  373. /*
  374. * Select this pack.
  375. */
  376. *mru_w = this_mru_w;
  377. *lru_p = p;
  378. *accept_windows_inuse = has_windows_inuse;
  379. }
  380. static int close_one_pack(void)
  381. {
  382. struct packed_git *p, *lru_p = NULL;
  383. struct pack_window *mru_w = NULL;
  384. int accept_windows_inuse = 1;
  385. for (p = the_repository->objects->packed_git; p; p = p->next) {
  386. if (p->pack_fd == -1)
  387. continue;
  388. find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
  389. }
  390. if (lru_p)
  391. return close_pack_fd(lru_p);
  392. return 0;
  393. }
  394. static unsigned int get_max_fd_limit(void)
  395. {
  396. #ifdef RLIMIT_NOFILE
  397. {
  398. struct rlimit lim;
  399. if (!getrlimit(RLIMIT_NOFILE, &lim))
  400. return lim.rlim_cur;
  401. }
  402. #endif
  403. #ifdef _SC_OPEN_MAX
  404. {
  405. long open_max = sysconf(_SC_OPEN_MAX);
  406. if (0 < open_max)
  407. return open_max;
  408. /*
  409. * Otherwise, we got -1 for one of the two
  410. * reasons:
  411. *
  412. * (1) sysconf() did not understand _SC_OPEN_MAX
  413. * and signaled an error with -1; or
  414. * (2) sysconf() said there is no limit.
  415. *
  416. * We _could_ clear errno before calling sysconf() to
  417. * tell these two cases apart and return a huge number
  418. * in the latter case to let the caller cap it to a
  419. * value that is not so selfish, but letting the
  420. * fallback OPEN_MAX codepath take care of these cases
  421. * is a lot simpler.
  422. */
  423. }
  424. #endif
  425. #ifdef OPEN_MAX
  426. return OPEN_MAX;
  427. #else
  428. return 1; /* see the caller ;-) */
  429. #endif
  430. }
  431. const char *pack_basename(struct packed_git *p)
  432. {
  433. const char *ret = strrchr(p->pack_name, '/');
  434. if (ret)
  435. ret = ret + 1; /* skip past slash */
  436. else
  437. ret = p->pack_name; /* we only have a base */
  438. return ret;
  439. }
  440. /*
  441. * Do not call this directly as this leaks p->pack_fd on error return;
  442. * call open_packed_git() instead.
  443. */
  444. static int open_packed_git_1(struct packed_git *p)
  445. {
  446. struct stat st;
  447. struct pack_header hdr;
  448. unsigned char hash[GIT_MAX_RAWSZ];
  449. unsigned char *idx_hash;
  450. ssize_t read_result;
  451. const unsigned hashsz = the_hash_algo->rawsz;
  452. if (!p->index_data) {
  453. struct multi_pack_index *m;
  454. const char *pack_name = pack_basename(p);
  455. for (m = the_repository->objects->multi_pack_index;
  456. m; m = m->next) {
  457. if (midx_contains_pack(m, pack_name))
  458. break;
  459. }
  460. if (!m && open_pack_index(p))
  461. return error("packfile %s index unavailable", p->pack_name);
  462. }
  463. if (!pack_max_fds) {
  464. unsigned int max_fds = get_max_fd_limit();
  465. /* Save 3 for stdin/stdout/stderr, 22 for work */
  466. if (25 < max_fds)
  467. pack_max_fds = max_fds - 25;
  468. else
  469. pack_max_fds = 1;
  470. }
  471. while (pack_max_fds <= pack_open_fds && close_one_pack())
  472. ; /* nothing */
  473. p->pack_fd = git_open(p->pack_name);
  474. if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
  475. return -1;
  476. pack_open_fds++;
  477. /* If we created the struct before we had the pack we lack size. */
  478. if (!p->pack_size) {
  479. if (!S_ISREG(st.st_mode))
  480. return error("packfile %s not a regular file", p->pack_name);
  481. p->pack_size = st.st_size;
  482. } else if (p->pack_size != st.st_size)
  483. return error("packfile %s size changed", p->pack_name);
  484. /* Verify we recognize this pack file format. */
  485. read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr));
  486. if (read_result < 0)
  487. return error_errno("error reading from %s", p->pack_name);
  488. if (read_result != sizeof(hdr))
  489. return error("file %s is far too short to be a packfile", p->pack_name);
  490. if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
  491. return error("file %s is not a GIT packfile", p->pack_name);
  492. if (!pack_version_ok(hdr.hdr_version))
  493. return error("packfile %s is version %"PRIu32" and not"
  494. " supported (try upgrading GIT to a newer version)",
  495. p->pack_name, ntohl(hdr.hdr_version));
  496. /* Skip index checking if in multi-pack-index */
  497. if (!p->index_data)
  498. return 0;
  499. /* Verify the pack matches its index. */
  500. if (p->num_objects != ntohl(hdr.hdr_entries))
  501. return error("packfile %s claims to have %"PRIu32" objects"
  502. " while index indicates %"PRIu32" objects",
  503. p->pack_name, ntohl(hdr.hdr_entries),
  504. p->num_objects);
  505. read_result = pread_in_full(p->pack_fd, hash, hashsz,
  506. p->pack_size - hashsz);
  507. if (read_result < 0)
  508. return error_errno("error reading from %s", p->pack_name);
  509. if (read_result != hashsz)
  510. return error("packfile %s signature is unavailable", p->pack_name);
  511. idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2;
  512. if (!hasheq(hash, idx_hash))
  513. return error("packfile %s does not match index", p->pack_name);
  514. return 0;
  515. }
  516. static int open_packed_git(struct packed_git *p)
  517. {
  518. if (!open_packed_git_1(p))
  519. return 0;
  520. close_pack_fd(p);
  521. return -1;
  522. }
  523. static int in_window(struct pack_window *win, off_t offset)
  524. {
  525. /* We must promise at least one full hash after the
  526. * offset is available from this window, otherwise the offset
  527. * is not actually in this window and a different window (which
  528. * has that one hash excess) must be used. This is to support
  529. * the object header and delta base parsing routines below.
  530. */
  531. off_t win_off = win->offset;
  532. return win_off <= offset
  533. && (offset + the_hash_algo->rawsz) <= (win_off + win->len);
  534. }
  535. unsigned char *use_pack(struct packed_git *p,
  536. struct pack_window **w_cursor,
  537. off_t offset,
  538. unsigned long *left)
  539. {
  540. struct pack_window *win = *w_cursor;
  541. /* Since packfiles end in a hash of their content and it's
  542. * pointless to ask for an offset into the middle of that
  543. * hash, and the in_window function above wouldn't match
  544. * don't allow an offset too close to the end of the file.
  545. */
  546. if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
  547. die("packfile %s cannot be accessed", p->pack_name);
  548. if (offset > (p->pack_size - the_hash_algo->rawsz))
  549. die("offset beyond end of packfile (truncated pack?)");
  550. if (offset < 0)
  551. die(_("offset before end of packfile (broken .idx?)"));
  552. if (!win || !in_window(win, offset)) {
  553. if (win)
  554. win->inuse_cnt--;
  555. for (win = p->windows; win; win = win->next) {
  556. if (in_window(win, offset))
  557. break;
  558. }
  559. if (!win) {
  560. size_t window_align = packed_git_window_size / 2;
  561. off_t len;
  562. if (p->pack_fd == -1 && open_packed_git(p))
  563. die("packfile %s cannot be accessed", p->pack_name);
  564. win = xcalloc(1, sizeof(*win));
  565. win->offset = (offset / window_align) * window_align;
  566. len = p->pack_size - win->offset;
  567. if (len > packed_git_window_size)
  568. len = packed_git_window_size;
  569. win->len = (size_t)len;
  570. pack_mapped += win->len;
  571. while (packed_git_limit < pack_mapped
  572. && unuse_one_window(p))
  573. ; /* nothing */
  574. win->base = xmmap_gently(NULL, win->len,
  575. PROT_READ, MAP_PRIVATE,
  576. p->pack_fd, win->offset);
  577. if (win->base == MAP_FAILED)
  578. die_errno("packfile %s cannot be mapped",
  579. p->pack_name);
  580. if (!win->offset && win->len == p->pack_size
  581. && !p->do_not_close)
  582. close_pack_fd(p);
  583. pack_mmap_calls++;
  584. pack_open_windows++;
  585. if (pack_mapped > peak_pack_mapped)
  586. peak_pack_mapped = pack_mapped;
  587. if (pack_open_windows > peak_pack_open_windows)
  588. peak_pack_open_windows = pack_open_windows;
  589. win->next = p->windows;
  590. p->windows = win;
  591. }
  592. }
  593. if (win != *w_cursor) {
  594. win->last_used = pack_used_ctr++;
  595. win->inuse_cnt++;
  596. *w_cursor = win;
  597. }
  598. offset -= win->offset;
  599. if (left)
  600. *left = win->len - xsize_t(offset);
  601. return win->base + offset;
  602. }
  603. void unuse_pack(struct pack_window **w_cursor)
  604. {
  605. struct pack_window *w = *w_cursor;
  606. if (w) {
  607. w->inuse_cnt--;
  608. *w_cursor = NULL;
  609. }
  610. }
  611. struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
  612. {
  613. struct stat st;
  614. size_t alloc;
  615. struct packed_git *p;
  616. /*
  617. * Make sure a corresponding .pack file exists and that
  618. * the index looks sane.
  619. */
  620. if (!strip_suffix_mem(path, &path_len, ".idx"))
  621. return NULL;
  622. /*
  623. * ".promisor" is long enough to hold any suffix we're adding (and
  624. * the use xsnprintf double-checks that)
  625. */
  626. alloc = st_add3(path_len, strlen(".promisor"), 1);
  627. p = alloc_packed_git(alloc);
  628. memcpy(p->pack_name, path, path_len);
  629. xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
  630. if (!access(p->pack_name, F_OK))
  631. p->pack_keep = 1;
  632. xsnprintf(p->pack_name + path_len, alloc - path_len, ".promisor");
  633. if (!access(p->pack_name, F_OK))
  634. p->pack_promisor = 1;
  635. xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
  636. if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
  637. free(p);
  638. return NULL;
  639. }
  640. /* ok, it looks sane as far as we can check without
  641. * actually mapping the pack file.
  642. */
  643. p->pack_size = st.st_size;
  644. p->pack_local = local;
  645. p->mtime = st.st_mtime;
  646. if (path_len < the_hash_algo->hexsz ||
  647. get_sha1_hex(path + path_len - the_hash_algo->hexsz, p->hash))
  648. hashclr(p->hash);
  649. return p;
  650. }
  651. void install_packed_git(struct repository *r, struct packed_git *pack)
  652. {
  653. if (pack->pack_fd != -1)
  654. pack_open_fds++;
  655. pack->next = r->objects->packed_git;
  656. r->objects->packed_git = pack;
  657. hashmap_entry_init(&pack->packmap_ent, strhash(pack->pack_name));
  658. hashmap_add(&r->objects->pack_map, &pack->packmap_ent);
  659. }
  660. void (*report_garbage)(unsigned seen_bits, const char *path);
  661. static void report_helper(const struct string_list *list,
  662. int seen_bits, int first, int last)
  663. {
  664. if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
  665. return;
  666. for (; first < last; first++)
  667. report_garbage(seen_bits, list->items[first].string);
  668. }
  669. static void report_pack_garbage(struct string_list *list)
  670. {
  671. int i, baselen = -1, first = 0, seen_bits = 0;
  672. if (!report_garbage)
  673. return;
  674. string_list_sort(list);
  675. for (i = 0; i < list->nr; i++) {
  676. const char *path = list->items[i].string;
  677. if (baselen != -1 &&
  678. strncmp(path, list->items[first].string, baselen)) {
  679. report_helper(list, seen_bits, first, i);
  680. baselen = -1;
  681. seen_bits = 0;
  682. }
  683. if (baselen == -1) {
  684. const char *dot = strrchr(path, '.');
  685. if (!dot) {
  686. report_garbage(PACKDIR_FILE_GARBAGE, path);
  687. continue;
  688. }
  689. baselen = dot - path + 1;
  690. first = i;
  691. }
  692. if (!strcmp(path + baselen, "pack"))
  693. seen_bits |= 1;
  694. else if (!strcmp(path + baselen, "idx"))
  695. seen_bits |= 2;
  696. }
  697. report_helper(list, seen_bits, first, list->nr);
  698. }
  699. void for_each_file_in_pack_dir(const char *objdir,
  700. each_file_in_pack_dir_fn fn,
  701. void *data)
  702. {
  703. struct strbuf path = STRBUF_INIT;
  704. size_t dirnamelen;
  705. DIR *dir;
  706. struct dirent *de;
  707. strbuf_addstr(&path, objdir);
  708. strbuf_addstr(&path, "/pack");
  709. dir = opendir(path.buf);
  710. if (!dir) {
  711. if (errno != ENOENT)
  712. error_errno("unable to open object pack directory: %s",
  713. path.buf);
  714. strbuf_release(&path);
  715. return;
  716. }
  717. strbuf_addch(&path, '/');
  718. dirnamelen = path.len;
  719. while ((de = readdir(dir)) != NULL) {
  720. if (is_dot_or_dotdot(de->d_name))
  721. continue;
  722. strbuf_setlen(&path, dirnamelen);
  723. strbuf_addstr(&path, de->d_name);
  724. fn(path.buf, path.len, de->d_name, data);
  725. }
  726. closedir(dir);
  727. strbuf_release(&path);
  728. }
  729. struct prepare_pack_data {
  730. struct repository *r;
  731. struct string_list *garbage;
  732. int local;
  733. struct multi_pack_index *m;
  734. };
  735. static void prepare_pack(const char *full_name, size_t full_name_len,
  736. const char *file_name, void *_data)
  737. {
  738. struct prepare_pack_data *data = (struct prepare_pack_data *)_data;
  739. struct packed_git *p;
  740. size_t base_len = full_name_len;
  741. if (strip_suffix_mem(full_name, &base_len, ".idx") &&
  742. !(data->m && midx_contains_pack(data->m, file_name))) {
  743. struct hashmap_entry hent;
  744. char *pack_name = xstrfmt("%.*s.pack", (int)base_len, full_name);
  745. unsigned int hash = strhash(pack_name);
  746. hashmap_entry_init(&hent, hash);
  747. /* Don't reopen a pack we already have. */
  748. if (!hashmap_get(&data->r->objects->pack_map, &hent, pack_name)) {
  749. p = add_packed_git(full_name, full_name_len, data->local);
  750. if (p)
  751. install_packed_git(data->r, p);
  752. }
  753. free(pack_name);
  754. }
  755. if (!report_garbage)
  756. return;
  757. if (!strcmp(file_name, "multi-pack-index"))
  758. return;
  759. if (ends_with(file_name, ".idx") ||
  760. ends_with(file_name, ".pack") ||
  761. ends_with(file_name, ".bitmap") ||
  762. ends_with(file_name, ".keep") ||
  763. ends_with(file_name, ".promisor"))
  764. string_list_append(data->garbage, full_name);
  765. else
  766. report_garbage(PACKDIR_FILE_GARBAGE, full_name);
  767. }
  768. static void prepare_packed_git_one(struct repository *r, char *objdir, int local)
  769. {
  770. struct prepare_pack_data data;
  771. struct string_list garbage = STRING_LIST_INIT_DUP;
  772. data.m = r->objects->multi_pack_index;
  773. /* look for the multi-pack-index for this object directory */
  774. while (data.m && strcmp(data.m->object_dir, objdir))
  775. data.m = data.m->next;
  776. data.r = r;
  777. data.garbage = &garbage;
  778. data.local = local;
  779. for_each_file_in_pack_dir(objdir, prepare_pack, &data);
  780. report_pack_garbage(data.garbage);
  781. string_list_clear(data.garbage, 0);
  782. }
  783. static void prepare_packed_git(struct repository *r);
  784. /*
  785. * Give a fast, rough count of the number of objects in the repository. This
  786. * ignores loose objects completely. If you have a lot of them, then either
  787. * you should repack because your performance will be awful, or they are
  788. * all unreachable objects about to be pruned, in which case they're not really
  789. * interesting as a measure of repo size in the first place.
  790. */
  791. unsigned long repo_approximate_object_count(struct repository *r)
  792. {
  793. if (!r->objects->approximate_object_count_valid) {
  794. unsigned long count;
  795. struct multi_pack_index *m;
  796. struct packed_git *p;
  797. prepare_packed_git(r);
  798. count = 0;
  799. for (m = get_multi_pack_index(r); m; m = m->next)
  800. count += m->num_objects;
  801. for (p = r->objects->packed_git; p; p = p->next) {
  802. if (open_pack_index(p))
  803. continue;
  804. count += p->num_objects;
  805. }
  806. r->objects->approximate_object_count = count;
  807. r->objects->approximate_object_count_valid = 1;
  808. }
  809. return r->objects->approximate_object_count;
  810. }
  811. static void *get_next_packed_git(const void *p)
  812. {
  813. return ((const struct packed_git *)p)->next;
  814. }
  815. static void set_next_packed_git(void *p, void *next)
  816. {
  817. ((struct packed_git *)p)->next = next;
  818. }
  819. static int sort_pack(const void *a_, const void *b_)
  820. {
  821. const struct packed_git *a = a_;
  822. const struct packed_git *b = b_;
  823. int st;
  824. /*
  825. * Local packs tend to contain objects specific to our
  826. * variant of the project than remote ones. In addition,
  827. * remote ones could be on a network mounted filesystem.
  828. * Favor local ones for these reasons.
  829. */
  830. st = a->pack_local - b->pack_local;
  831. if (st)
  832. return -st;
  833. /*
  834. * Younger packs tend to contain more recent objects,
  835. * and more recent objects tend to get accessed more
  836. * often.
  837. */
  838. if (a->mtime < b->mtime)
  839. return 1;
  840. else if (a->mtime == b->mtime)
  841. return 0;
  842. return -1;
  843. }
  844. static void rearrange_packed_git(struct repository *r)
  845. {
  846. r->objects->packed_git = llist_mergesort(
  847. r->objects->packed_git, get_next_packed_git,
  848. set_next_packed_git, sort_pack);
  849. }
  850. static void prepare_packed_git_mru(struct repository *r)
  851. {
  852. struct packed_git *p;
  853. INIT_LIST_HEAD(&r->objects->packed_git_mru);
  854. for (p = r->objects->packed_git; p; p = p->next)
  855. list_add_tail(&p->mru, &r->objects->packed_git_mru);
  856. }
  857. static void prepare_packed_git(struct repository *r)
  858. {
  859. struct object_directory *odb;
  860. if (r->objects->packed_git_initialized)
  861. return;
  862. prepare_alt_odb(r);
  863. for (odb = r->objects->odb; odb; odb = odb->next) {
  864. int local = (odb == r->objects->odb);
  865. prepare_multi_pack_index_one(r, odb->path, local);
  866. prepare_packed_git_one(r, odb->path, local);
  867. }
  868. rearrange_packed_git(r);
  869. prepare_packed_git_mru(r);
  870. r->objects->packed_git_initialized = 1;
  871. }
  872. void reprepare_packed_git(struct repository *r)
  873. {
  874. struct object_directory *odb;
  875. obj_read_lock();
  876. for (odb = r->objects->odb; odb; odb = odb->next)
  877. odb_clear_loose_cache(odb);
  878. r->objects->approximate_object_count_valid = 0;
  879. r->objects->packed_git_initialized = 0;
  880. prepare_packed_git(r);
  881. obj_read_unlock();
  882. }
  883. struct packed_git *get_packed_git(struct repository *r)
  884. {
  885. prepare_packed_git(r);
  886. return r->objects->packed_git;
  887. }
  888. struct multi_pack_index *get_multi_pack_index(struct repository *r)
  889. {
  890. prepare_packed_git(r);
  891. return r->objects->multi_pack_index;
  892. }
  893. struct multi_pack_index *get_local_multi_pack_index(struct repository *r)
  894. {
  895. struct multi_pack_index *m = get_multi_pack_index(r);
  896. /* no need to iterate; we always put the local one first (if any) */
  897. if (m && m->local)
  898. return m;
  899. return NULL;
  900. }
  901. struct packed_git *get_all_packs(struct repository *r)
  902. {
  903. struct multi_pack_index *m;
  904. prepare_packed_git(r);
  905. for (m = r->objects->multi_pack_index; m; m = m->next) {
  906. uint32_t i;
  907. for (i = 0; i < m->num_packs; i++)
  908. prepare_midx_pack(r, m, i);
  909. }
  910. return r->objects->packed_git;
  911. }
  912. struct list_head *get_packed_git_mru(struct repository *r)
  913. {
  914. prepare_packed_git(r);
  915. return &r->objects->packed_git_mru;
  916. }
  917. unsigned long unpack_object_header_buffer(const unsigned char *buf,
  918. unsigned long len, enum object_type *type, unsigned long *sizep)
  919. {
  920. unsigned shift;
  921. unsigned long size, c;
  922. unsigned long used = 0;
  923. c = buf[used++];
  924. *type = (c >> 4) & 7;
  925. size = c & 15;
  926. shift = 4;
  927. while (c & 0x80) {
  928. if (len <= used || bitsizeof(long) <= shift) {
  929. error("bad object header");
  930. size = used = 0;
  931. break;
  932. }
  933. c = buf[used++];
  934. size += (c & 0x7f) << shift;
  935. shift += 7;
  936. }
  937. *sizep = size;
  938. return used;
  939. }
  940. unsigned long get_size_from_delta(struct packed_git *p,
  941. struct pack_window **w_curs,
  942. off_t curpos)
  943. {
  944. const unsigned char *data;
  945. unsigned char delta_head[20], *in;
  946. git_zstream stream;
  947. int st;
  948. memset(&stream, 0, sizeof(stream));
  949. stream.next_out = delta_head;
  950. stream.avail_out = sizeof(delta_head);
  951. git_inflate_init(&stream);
  952. do {
  953. in = use_pack(p, w_curs, curpos, &stream.avail_in);
  954. stream.next_in = in;
  955. /*
  956. * Note: the window section returned by use_pack() must be
  957. * available throughout git_inflate()'s unlocked execution. To
  958. * ensure no other thread will modify the window in the
  959. * meantime, we rely on the packed_window.inuse_cnt. This
  960. * counter is incremented before window reading and checked
  961. * before window disposal.
  962. *
  963. * Other worrying sections could be the call to close_pack_fd(),
  964. * which can close packs even with in-use windows, and to
  965. * reprepare_packed_git(). Regarding the former, mmap doc says:
  966. * "closing the file descriptor does not unmap the region". And
  967. * for the latter, it won't re-open already available packs.
  968. */
  969. obj_read_unlock();
  970. st = git_inflate(&stream, Z_FINISH);
  971. obj_read_lock();
  972. curpos += stream.next_in - in;
  973. } while ((st == Z_OK || st == Z_BUF_ERROR) &&
  974. stream.total_out < sizeof(delta_head));
  975. git_inflate_end(&stream);
  976. if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
  977. error("delta data unpack-initial failed");
  978. return 0;
  979. }
  980. /* Examine the initial part of the delta to figure out
  981. * the result size.
  982. */
  983. data = delta_head;
  984. /* ignore base size */
  985. get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
  986. /* Read the result size */
  987. return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
  988. }
  989. int unpack_object_header(struct packed_git *p,
  990. struct pack_window **w_curs,
  991. off_t *curpos,
  992. unsigned long *sizep)
  993. {
  994. unsigned char *base;
  995. unsigned long left;
  996. unsigned long used;
  997. enum object_type type;
  998. /* use_pack() assures us we have [base, base + 20) available
  999. * as a range that we can look at. (Its actually the hash
  1000. * size that is assured.) With our object header encoding
  1001. * the maximum deflated object size is 2^137, which is just
  1002. * insane, so we know won't exceed what we have been given.
  1003. */
  1004. base = use_pack(p, w_curs, *curpos, &left);
  1005. used = unpack_object_header_buffer(base, left, &type, sizep);
  1006. if (!used) {
  1007. type = OBJ_BAD;
  1008. } else
  1009. *curpos += used;
  1010. return type;
  1011. }
  1012. void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1)
  1013. {
  1014. unsigned i;
  1015. const unsigned hashsz = the_hash_algo->rawsz;
  1016. for (i = 0; i < p->num_bad_objects; i++)
  1017. if (hasheq(sha1, p->bad_object_sha1 + hashsz * i))
  1018. return;
  1019. p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
  1020. st_mult(GIT_MAX_RAWSZ,
  1021. st_add(p->num_bad_objects, 1)));
  1022. hashcpy(p->bad_object_sha1 + hashsz * p->num_bad_objects, sha1);
  1023. p->num_bad_objects++;
  1024. }
  1025. const struct packed_git *has_packed_and_bad(struct repository *r,
  1026. const unsigned char *sha1)
  1027. {
  1028. struct packed_git *p;
  1029. unsigned i;
  1030. for (p = r->objects->packed_git; p; p = p->next)
  1031. for (i = 0; i < p->num_bad_objects; i++)
  1032. if (hasheq(sha1,
  1033. p->bad_object_sha1 + the_hash_algo->rawsz * i))
  1034. return p;
  1035. return NULL;
  1036. }
  1037. off_t get_delta_base(struct packed_git *p,
  1038. struct pack_window **w_curs,
  1039. off_t *curpos,
  1040. enum object_type type,
  1041. off_t delta_obj_offset)
  1042. {
  1043. unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
  1044. off_t base_offset;
  1045. /* use_pack() assured us we have [base_info, base_info + 20)
  1046. * as a range that we can look at without walking off the
  1047. * end of the mapped window. Its actually the hash size
  1048. * that is assured. An OFS_DELTA longer than the hash size
  1049. * is stupid, as then a REF_DELTA would be smaller to store.
  1050. */
  1051. if (type == OBJ_OFS_DELTA) {
  1052. unsigned used = 0;
  1053. unsigned char c = base_info[used++];
  1054. base_offset = c & 127;
  1055. while (c & 128) {
  1056. base_offset += 1;
  1057. if (!base_offset || MSB(base_offset, 7))
  1058. return 0; /* overflow */
  1059. c = base_info[used++];
  1060. base_offset = (base_offset << 7) + (c & 127);
  1061. }
  1062. base_offset = delta_obj_offset - base_offset;
  1063. if (base_offset <= 0 || base_offset >= delta_obj_offset)
  1064. return 0; /* out of bound */
  1065. *curpos += used;
  1066. } else if (type == OBJ_REF_DELTA) {
  1067. /* The base entry _must_ be in the same pack */
  1068. base_offset = find_pack_entry_one(base_info, p);
  1069. *curpos += the_hash_algo->rawsz;
  1070. } else
  1071. die("I am totally screwed");
  1072. return base_offset;
  1073. }
  1074. /*
  1075. * Like get_delta_base above, but we return the sha1 instead of the pack
  1076. * offset. This means it is cheaper for REF deltas (we do not have to do
  1077. * the final object lookup), but more expensive for OFS deltas (we
  1078. * have to load the revidx to convert the offset back into a sha1).
  1079. */
  1080. static int get_delta_base_oid(struct packed_git *p,
  1081. struct pack_window **w_curs,
  1082. off_t curpos,
  1083. struct object_id *oid,
  1084. enum object_type type,
  1085. off_t delta_obj_offset)
  1086. {
  1087. if (type == OBJ_REF_DELTA) {
  1088. unsigned char *base = use_pack(p, w_curs, curpos, NULL);
  1089. oidread(oid, base);
  1090. return 0;
  1091. } else if (type == OBJ_OFS_DELTA) {
  1092. struct revindex_entry *revidx;
  1093. off_t base_offset = get_delta_base(p, w_curs, &curpos,
  1094. type, delta_obj_offset);
  1095. if (!base_offset)
  1096. return -1;
  1097. revidx = find_pack_revindex(p, base_offset);
  1098. if (!revidx)
  1099. return -1;
  1100. return nth_packed_object_id(oid, p, revidx->nr);
  1101. } else
  1102. return -1;
  1103. }
  1104. static int retry_bad_packed_offset(struct repository *r,
  1105. struct packed_git *p,
  1106. off_t obj_offset)
  1107. {
  1108. int type;
  1109. struct revindex_entry *revidx;
  1110. struct object_id oid;
  1111. revidx = find_pack_revindex(p, obj_offset);
  1112. if (!revidx)
  1113. return OBJ_BAD;
  1114. nth_packed_object_id(&oid, p, revidx->nr);
  1115. mark_bad_packed_object(p, oid.hash);
  1116. type = oid_object_info(r, &oid, NULL);
  1117. if (type <= OBJ_NONE)
  1118. return OBJ_BAD;
  1119. return type;
  1120. }
  1121. #define POI_STACK_PREALLOC 64
  1122. static enum object_type packed_to_object_type(struct repository *r,
  1123. struct packed_git *p,
  1124. off_t obj_offset,
  1125. enum object_type type,
  1126. struct pack_window **w_curs,
  1127. off_t curpos)
  1128. {
  1129. off_t small_poi_stack[POI_STACK_PREALLOC];
  1130. off_t *poi_stack = small_poi_stack;
  1131. int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
  1132. while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
  1133. off_t base_offset;
  1134. unsigned long size;
  1135. /* Push the object we're going to leave behind */
  1136. if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
  1137. poi_stack_alloc = alloc_nr(poi_stack_nr);
  1138. ALLOC_ARRAY(poi_stack, poi_stack_alloc);
  1139. COPY_ARRAY(poi_stack, small_poi_stack, poi_stack_nr);
  1140. } else {
  1141. ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
  1142. }
  1143. poi_stack[poi_stack_nr++] = obj_offset;
  1144. /* If parsing the base offset fails, just unwind */
  1145. base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
  1146. if (!base_offset)
  1147. goto unwind;
  1148. curpos = obj_offset = base_offset;
  1149. type = unpack_object_header(p, w_curs, &curpos, &size);
  1150. if (type <= OBJ_NONE) {
  1151. /* If getting the base itself fails, we first
  1152. * retry the base, otherwise unwind */
  1153. type = retry_bad_packed_offset(r, p, base_offset);
  1154. if (type > OBJ_NONE)
  1155. goto out;
  1156. goto unwind;
  1157. }
  1158. }
  1159. switch (type) {
  1160. case OBJ_BAD:
  1161. case OBJ_COMMIT:
  1162. case OBJ_TREE:
  1163. case OBJ_BLOB:
  1164. case OBJ_TAG:
  1165. break;
  1166. default:
  1167. error("unknown object type %i at offset %"PRIuMAX" in %s",
  1168. type, (uintmax_t)obj_offset, p->pack_name);
  1169. type = OBJ_BAD;
  1170. }
  1171. out:
  1172. if (poi_stack != small_poi_stack)
  1173. free(poi_stack);
  1174. return type;
  1175. unwind:
  1176. while (poi_stack_nr) {
  1177. obj_offset = poi_stack[--poi_stack_nr];
  1178. type = retry_bad_packed_offset(r, p, obj_offset);
  1179. if (type > OBJ_NONE)
  1180. goto out;
  1181. }
  1182. type = OBJ_BAD;
  1183. goto out;
  1184. }
  1185. static struct hashmap delta_base_cache;
  1186. static size_t delta_base_cached;
  1187. static LIST_HEAD(delta_base_cache_lru);
  1188. struct delta_base_cache_key {
  1189. struct packed_git *p;
  1190. off_t base_offset;
  1191. };
  1192. struct delta_base_cache_entry {
  1193. struct hashmap_entry ent;
  1194. struct delta_base_cache_key key;
  1195. struct list_head lru;
  1196. void *data;
  1197. unsigned long size;
  1198. enum object_type type;
  1199. };
  1200. static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
  1201. {
  1202. unsigned int hash;
  1203. hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
  1204. hash += (hash >> 8) + (hash >> 16);
  1205. return hash;
  1206. }
  1207. static struct delta_base_cache_entry *
  1208. get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
  1209. {
  1210. struct hashmap_entry entry, *e;
  1211. struct delta_base_cache_key key;
  1212. if (!delta_base_cache.cmpfn)
  1213. return NULL;
  1214. hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
  1215. key.p = p;
  1216. key.base_offset = base_offset;
  1217. e = hashmap_get(&delta_base_cache, &entry, &key);
  1218. return e ? container_of(e, struct delta_base_cache_entry, ent) : NULL;
  1219. }
  1220. static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
  1221. const struct delta_base_cache_key *b)
  1222. {
  1223. return a->p == b->p && a->base_offset == b->base_offset;
  1224. }
  1225. static int delta_base_cache_hash_cmp(const void *unused_cmp_data,
  1226. const struct hashmap_entry *va,
  1227. const struct hashmap_entry *vb,
  1228. const void *vkey)
  1229. {
  1230. const struct delta_base_cache_entry *a, *b;
  1231. const struct delta_base_cache_key *key = vkey;
  1232. a = container_of(va, const struct delta_base_cache_entry, ent);
  1233. b = container_of(vb, const struct delta_base_cache_entry, ent);
  1234. if (key)
  1235. return !delta_base_cache_key_eq(&a->key, key);
  1236. else
  1237. return !delta_base_cache_key_eq(&a->key, &b->key);
  1238. }
  1239. static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
  1240. {
  1241. return !!get_delta_base_cache_entry(p, base_offset);
  1242. }
  1243. /*
  1244. * Remove the entry from the cache, but do _not_ free the associated
  1245. * entry data. The caller takes ownership of the "data" buffer, and
  1246. * should copy out any fields it wants before detaching.
  1247. */
  1248. static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
  1249. {
  1250. hashmap_remove(&delta_base_cache, &ent->ent, &ent->key);
  1251. list_del(&ent->lru);
  1252. delta_base_cached -= ent->size;
  1253. free(ent);
  1254. }
  1255. static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p,
  1256. off_t base_offset, unsigned long *base_size,
  1257. enum object_type *type)
  1258. {
  1259. struct delta_base_cache_entry *ent;
  1260. ent = get_delta_base_cache_entry(p, base_offset);
  1261. if (!ent)
  1262. return unpack_entry(r, p, base_offset, type, base_size);
  1263. if (type)
  1264. *type = ent->type;
  1265. if (base_size)
  1266. *base_size = ent->size;
  1267. return xmemdupz(ent->data, ent->size);
  1268. }
  1269. static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
  1270. {
  1271. free(ent->data);
  1272. detach_delta_base_cache_entry(ent);
  1273. }
  1274. void clear_delta_base_cache(void)
  1275. {
  1276. struct list_head *lru, *tmp;
  1277. list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
  1278. struct delta_base_cache_entry *entry =
  1279. list_entry(lru, struct delta_base_cache_entry, lru);
  1280. release_delta_base_cache(entry);
  1281. }
  1282. }
  1283. static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
  1284. void *base, unsigned long base_size, enum object_type type)
  1285. {
  1286. struct delta_base_cache_entry *ent;
  1287. struct list_head *lru, *tmp;
  1288. /*
  1289. * Check required to avoid redundant entries when more than one thread
  1290. * is unpacking the same object, in unpack_entry() (since its phases I
  1291. * and III might run concurrently across multiple threads).
  1292. */
  1293. if (in_delta_base_cache(p, base_offset)) {
  1294. free(base);
  1295. return;
  1296. }
  1297. delta_base_cached += base_size;
  1298. list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
  1299. struct delta_base_cache_entry *f =
  1300. list_entry(lru, struct delta_base_cache_entry, lru);
  1301. if (delta_base_cached <= delta_base_cache_limit)
  1302. break;
  1303. release_delta_base_cache(f);
  1304. }
  1305. ent = xmalloc(sizeof(*ent));
  1306. ent->key.p = p;
  1307. ent->key.base_offset = base_offset;
  1308. ent->type = type;
  1309. ent->data = base;
  1310. ent->size = base_size;
  1311. list_add_tail(&ent->lru, &delta_base_cache_lru);
  1312. if (!delta_base_cache.cmpfn)
  1313. hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
  1314. hashmap_entry_init(&ent->ent, pack_entry_hash(p, base_offset));
  1315. hashmap_add(&delta_base_cache, &ent->ent);
  1316. }
  1317. int packed_object_info(struct repository *r, struct packed_git *p,
  1318. off_t obj_offset, struct object_info *oi)
  1319. {
  1320. struct pack_window *w_curs = NULL;
  1321. unsigned long size;
  1322. off_t curpos = obj_offset;
  1323. enum object_type type;
  1324. /*
  1325. * We always get the representation type, but only convert it to
  1326. * a "real" type later if the caller is interested.
  1327. */
  1328. if (oi->contentp) {
  1329. *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, oi->sizep,
  1330. &type);
  1331. if (!*oi->contentp)
  1332. type = OBJ_BAD;
  1333. } else {
  1334. type = unpack_object_header(p, &w_curs, &curpos, &size);
  1335. }
  1336. if (!oi->contentp && oi->sizep) {
  1337. if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
  1338. off_t tmp_pos = curpos;
  1339. off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
  1340. type, obj_offset);
  1341. if (!base_offset) {
  1342. type = OBJ_BAD;
  1343. goto out;
  1344. }
  1345. *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos);
  1346. if (*oi->sizep == 0) {
  1347. type = OBJ_BAD;
  1348. goto out;
  1349. }
  1350. } else {
  1351. *oi->sizep = size;
  1352. }
  1353. }
  1354. if (oi->disk_sizep) {
  1355. struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
  1356. *oi->disk_sizep = revidx[1].offset - obj_offset;
  1357. }
  1358. if (oi->typep || oi->type_name) {
  1359. enum object_type ptot;
  1360. ptot = packed_to_object_type(r, p, obj_offset,
  1361. type, &w_curs, curpos);
  1362. if (oi->typep)
  1363. *oi->typep = ptot;
  1364. if (oi->type_name) {
  1365. const char *tn = type_name(ptot);
  1366. if (tn)
  1367. strbuf_addstr(oi->type_name, tn);
  1368. }
  1369. if (ptot < 0) {
  1370. type = OBJ_BAD;
  1371. goto out;
  1372. }
  1373. }
  1374. if (oi->delta_base_oid) {
  1375. if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
  1376. if (get_delta_base_oid(p, &w_curs, curpos,
  1377. oi->delta_base_oid,
  1378. type, obj_offset) < 0) {
  1379. type = OBJ_BAD;
  1380. goto out;
  1381. }
  1382. } else
  1383. oidclr(oi->delta_base_oid);
  1384. }
  1385. oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED :
  1386. OI_PACKED;
  1387. out:
  1388. unuse_pack(&w_curs);
  1389. return type;
  1390. }
  1391. static void *unpack_compressed_entry(struct packed_git *p,
  1392. struct pack_window **w_curs,
  1393. off_t curpos,
  1394. unsigned long size)
  1395. {
  1396. int st;
  1397. git_zstream stream;
  1398. unsigned char *buffer, *in;
  1399. buffer = xmallocz_gently(size);
  1400. if (!buffer)
  1401. return NULL;
  1402. memset(&stream, 0, sizeof(stream));
  1403. stream.next_out = buffer;
  1404. stream.avail_out = size + 1;
  1405. git_inflate_init(&stream);
  1406. do {
  1407. in = use_pack(p, w_curs, curpos, &stream.avail_in);
  1408. stream.next_in = in;
  1409. /*
  1410. * Note: we must ensure the window section returned by
  1411. * use_pack() will be available throughout git_inflate()'s
  1412. * unlocked execution. Please refer to the comment at
  1413. * get_size_from_delta() to see how this is done.
  1414. */
  1415. obj_read_unlock();
  1416. st = git_inflate(&stream, Z_FINISH);
  1417. obj_read_lock();
  1418. if (!stream.avail_out)
  1419. break; /* the payload is larger than it should be */
  1420. curpos += stream.next_in - in;
  1421. } while (st == Z_OK || st == Z_BUF_ERROR);
  1422. git_inflate_end(&stream);
  1423. if ((st != Z_STREAM_END) || stream.total_out != size) {
  1424. free(buffer);
  1425. return NULL;
  1426. }
  1427. /* versions of zlib can clobber unconsumed portion of outbuf */
  1428. buffer[size] = '\0';
  1429. return buffer;
  1430. }
  1431. static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
  1432. {
  1433. static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
  1434. trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
  1435. p->pack_name, (uintmax_t)obj_offset);
  1436. }
  1437. int do_check_packed_object_crc;
  1438. #define UNPACK_ENTRY_STACK_PREALLOC 64
  1439. struct unpack_entry_stack_ent {
  1440. off_t obj_offset;
  1441. off_t curpos;
  1442. unsigned long size;
  1443. };
  1444. static void *read_object(struct repository *r,
  1445. const struct object_id *oid,
  1446. enum object_type *type,
  1447. unsigned long *size)
  1448. {
  1449. struct object_info oi = OBJECT_INFO_INIT;
  1450. void *content;
  1451. oi.typep = type;
  1452. oi.sizep = size;
  1453. oi.contentp = &content;
  1454. if (oid_object_info_extended(r, oid, &oi, 0) < 0)
  1455. return NULL;
  1456. return content;
  1457. }
  1458. void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
  1459. enum object_type *final_type, unsigned long *final_size)
  1460. {
  1461. struct pack_window *w_curs = NULL;
  1462. off_t curpos = obj_offset;
  1463. void *data = NULL;
  1464. unsigned long size;
  1465. enum object_type type;
  1466. struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
  1467. struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
  1468. int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
  1469. int base_from_cache = 0;
  1470. write_pack_access_log(p, obj_offset);
  1471. /* PHASE 1: drill down to the innermost base object */
  1472. for (;;) {
  1473. off_t base_offset;
  1474. int i;
  1475. struct delta_base_cache_entry *ent;
  1476. ent = get_delta_base_cache_entry(p, curpos);
  1477. if (ent) {
  1478. type = ent->type;
  1479. data = ent->data;
  1480. size = ent->size;
  1481. detach_delta_base_cache_entry(ent);
  1482. base_from_cache = 1;
  1483. break;
  1484. }
  1485. if (do_check_packed_object_crc && p->index_version > 1) {
  1486. struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
  1487. off_t len = revidx[1].offset - obj_offset;
  1488. if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
  1489. struct object_id oid;
  1490. nth_packed_object_id(&oid, p, revidx->nr);
  1491. error("bad packed object CRC for %s",
  1492. oid_to_hex(&oid));
  1493. mark_bad_packed_object(p, oid.hash);
  1494. data = NULL;
  1495. goto out;
  1496. }
  1497. }
  1498. type = unpack_object_header(p, &w_curs, &curpos, &size);
  1499. if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
  1500. break;
  1501. base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
  1502. if (!base_offset) {
  1503. error("failed to validate delta base reference "
  1504. "at offset %"PRIuMAX" from %s",
  1505. (uintmax_t)curpos, p->pack_name);
  1506. /* bail to phase 2, in hopes of recovery */
  1507. data = NULL;
  1508. break;
  1509. }
  1510. /* push object, proceed to base */
  1511. if (delta_stack_nr >= delta_stack_alloc
  1512. && delta_stack == small_delta_stack) {
  1513. delta_stack_alloc = alloc_nr(delta_stack_nr);
  1514. ALLOC_ARRAY(delta_stack, delta_stack_alloc);
  1515. COPY_ARRAY(delta_stack, small_delta_stack,
  1516. delta_stack_nr);
  1517. } else {
  1518. ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
  1519. }
  1520. i = delta_stack_nr++;
  1521. delta_stack[i].obj_offset = obj_offset;
  1522. delta_stack[i].curpos = curpos;
  1523. delta_stack[i].size = size;
  1524. curpos = obj_offset = base_offset;
  1525. }
  1526. /* PHASE 2: handle the base */
  1527. switch (type) {
  1528. case OBJ_OFS_DELTA:
  1529. case OBJ_REF_DELTA:
  1530. if (data)
  1531. BUG("unpack_entry: left loop at a valid delta");
  1532. break;
  1533. case OBJ_COMMIT:
  1534. case OBJ_TREE:
  1535. case OBJ_BLOB:
  1536. case OBJ_TAG:
  1537. if (!base_from_cache)
  1538. data = unpack_compressed_entry(p, &w_curs, curpos, size);
  1539. break;
  1540. default:
  1541. data = NULL;
  1542. error("unknown object type %i at offset %"PRIuMAX" in %s",
  1543. type, (uintmax_t)obj_offset, p->pack_name);
  1544. }
  1545. /* PHASE 3: apply deltas in order */
  1546. /* invariants:
  1547. * 'data' holds the base data, or NULL if there was corruption
  1548. */
  1549. while (delta_stack_nr) {
  1550. void *delta_data;
  1551. void *base = data;
  1552. void *external_base = NULL;
  1553. unsigned long delta_size, base_size = size;
  1554. int i;
  1555. off_t base_obj_offset = obj_offset;
  1556. data = NULL;
  1557. if (!base) {
  1558. /*
  1559. * We're probably in deep shit, but let's try to fetch
  1560. * the required base anyway from another pack or loose.
  1561. * This is costly but should happen only in the presence
  1562. * of a corrupted pack, and is better than failing outright.
  1563. */
  1564. struct revindex_entry *revidx;
  1565. struct object_id base_oid;
  1566. revidx = find_pack_revindex(p, obj_offset);
  1567. if (revidx) {
  1568. nth_packed_object_id(&base_oid, p, revidx->nr);
  1569. error("failed to read delta base object %s"
  1570. " at offset %"PRIuMAX" from %s",
  1571. oid_to_hex(&base_oid), (uintmax_t)obj_offset,
  1572. p->pack_name);
  1573. mark_bad_packed_object(p, base_oid.hash);
  1574. base = read_object(r, &base_oid, &type, &base_size);
  1575. external_base = base;
  1576. }
  1577. }
  1578. i = --delta_stack_nr;
  1579. obj_offset = delta_stack[i].obj_offset;
  1580. curpos = delta_stack[i].curpos;
  1581. delta_size = delta_stack[i].size;
  1582. if (!base)
  1583. continue;
  1584. delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
  1585. if (!delta_data) {
  1586. error("failed to unpack compressed delta "
  1587. "at offset %"PRIuMAX" from %s",
  1588. (uintmax_t)curpos, p->pack_name);
  1589. data = NULL;
  1590. } else {
  1591. data = patch_delta(base, base_size, delta_data,
  1592. delta_size, &size);
  1593. /*
  1594. * We could not apply the delta; warn the user, but
  1595. * keep going. Our failure will be noticed either in
  1596. * the next iteration of the loop, or if this is the
  1597. * final delta, in the caller when we return NULL.
  1598. * Those code paths will take care of making a more
  1599. * explicit warning and retrying with another copy of
  1600. * the object.
  1601. */
  1602. if (!data)
  1603. error("failed to apply delta");
  1604. }
  1605. /*
  1606. * We delay adding `base` to the cache until the end of the loop
  1607. * because unpack_compressed_entry() momentarily releases the
  1608. * obj_read_mutex, giving another thread the chance to access
  1609. * the cache. Therefore, if `base` was already there, this other
  1610. * thread could free() it (e.g. to make space for another entry)
  1611. * before we are done using it.
  1612. */
  1613. if (!external_base)
  1614. add_delta_base_cache(p, base_obj_offset, base, base_size, type);
  1615. free(delta_data);
  1616. free(external_base);
  1617. }
  1618. if (final_type)
  1619. *final_type = type;
  1620. if (final_size)
  1621. *final_size = size;
  1622. out:
  1623. unuse_pack(&w_curs);
  1624. if (delta_stack != small_delta_stack)
  1625. free(delta_stack);
  1626. return data;
  1627. }
  1628. int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result)
  1629. {
  1630. const unsigned char *index_fanout = p->index_data;
  1631. const unsigned char *index_lookup;
  1632. const unsigned int hashsz = the_hash_algo->rawsz;
  1633. int index_lookup_width;
  1634. if (!index_fanout)
  1635. BUG("bsearch_pack called without a valid pack-index");
  1636. index_lookup = index_fanout + 4 * 256;
  1637. if (p->index_version == 1) {
  1638. index_lookup_width = hashsz + 4;
  1639. index_lookup += 4;
  1640. } else {
  1641. index_lookup_width = hashsz;
  1642. index_fanout += 8;
  1643. index_lookup += 8;
  1644. }
  1645. return bsearch_hash(oid->hash, (const uint32_t*)index_fanout,
  1646. index_lookup, index_lookup_width, result);
  1647. }
  1648. int nth_packed_object_id(struct object_id *oid,
  1649. struct packed_git *p,
  1650. uint32_t n)
  1651. {
  1652. const unsigned char *index = p->index_data;
  1653. const unsigned int hashsz = the_hash_algo->rawsz;
  1654. if (!index) {
  1655. if (open_pack_index(p))
  1656. return -1;
  1657. index = p->index_data;
  1658. }
  1659. if (n >= p->num_objects)
  1660. return -1;
  1661. index += 4 * 256;
  1662. if (p->index_version == 1) {
  1663. oidread(oid, index + (hashsz + 4) * n + 4);
  1664. } else {
  1665. index += 8;
  1666. oidread(oid, index + hashsz * n);
  1667. }
  1668. return 0;
  1669. }
  1670. void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
  1671. {
  1672. const unsigned char *ptr = vptr;
  1673. const unsigned char *start = p->index_data;
  1674. const unsigned char *end = start + p->index_size;
  1675. if (ptr < start)
  1676. die(_("offset before start of pack index for %s (corrupt index?)"),
  1677. p->pack_name);
  1678. /* No need to check for underflow; .idx files must be at least 8 bytes */
  1679. if (ptr >= end - 8)
  1680. die(_("offset beyond end of pack index for %s (truncated index?)"),
  1681. p->pack_name);
  1682. }
  1683. off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
  1684. {
  1685. const unsigned char *index = p->index_data;
  1686. const unsigned int hashsz = the_hash_algo->rawsz;
  1687. index += 4 * 256;
  1688. if (p->index_version == 1) {
  1689. return ntohl(*((uint32_t *)(index + (hashsz + 4) * n)));
  1690. } else {
  1691. uint32_t off;
  1692. index += 8 + p->num_objects * (hashsz + 4);
  1693. off = ntohl(*((uint32_t *)(index + 4 * n)));
  1694. if (!(off & 0x80000000))
  1695. return off;
  1696. index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
  1697. check_pack_index_ptr(p, index);
  1698. return get_be64(index);
  1699. }
  1700. }
  1701. off_t find_pack_entry_one(const unsigned char *sha1,
  1702. struct packed_git *p)
  1703. {
  1704. const unsigned char *index = p->index_data;
  1705. struct object_id oid;
  1706. uint32_t result;
  1707. if (!index) {
  1708. if (open_pack_index(p))
  1709. return 0;
  1710. }
  1711. hashcpy(oid.hash, sha1);
  1712. if (bsearch_pack(&oid, p, &result))
  1713. return nth_packed_object_offset(p, result);
  1714. return 0;
  1715. }
  1716. int is_pack_valid(struct packed_git *p)
  1717. {
  1718. /* An already open pack is known to be valid. */
  1719. if (p->pack_fd != -1)
  1720. return 1;
  1721. /* If the pack has one window completely covering the
  1722. * file size, the pack is known to be valid even if
  1723. * the descriptor is not currently open.
  1724. */
  1725. if (p->windows) {
  1726. struct pack_window *w = p->windows;
  1727. if (!w->offset && w->len == p->pack_size)
  1728. return 1;
  1729. }
  1730. /* Force the pack to open to prove its valid. */
  1731. return !open_packed_git(p);
  1732. }
  1733. struct packed_git *find_sha1_pack(const unsigned char *sha1,
  1734. struct packed_git *packs)
  1735. {
  1736. struct packed_git *p;
  1737. for (p = packs; p; p = p->next) {
  1738. if (find_pack_entry_one(sha1, p))
  1739. return p;
  1740. }
  1741. return NULL;
  1742. }
  1743. static int fill_pack_entry(const struct object_id *oid,
  1744. struct pack_entry *e,
  1745. struct packed_git *p)
  1746. {
  1747. off_t offset;
  1748. if (p->num_bad_objects) {
  1749. unsigned i;
  1750. for (i = 0; i < p->num_bad_objects; i++)
  1751. if (hasheq(oid->hash,
  1752. p->bad_object_sha1 + the_hash_algo->rawsz * i))
  1753. return 0;
  1754. }
  1755. offset = find_pack_entry_one(oid->hash, p);
  1756. if (!offset)
  1757. return 0;
  1758. /*
  1759. * We are about to tell the caller where they can locate the
  1760. * requested object. We better make sure the packfile is
  1761. * still here and can be accessed before supplying that
  1762. * answer, as it may have been deleted since the index was
  1763. * loaded!
  1764. */
  1765. if (!is_pack_valid(p))
  1766. return 0;
  1767. e->offset = offset;
  1768. e->p = p;
  1769. return 1;
  1770. }
  1771. int find_pack_entry(struct repository *r, const struct object_id *oid, struct pack_entry *e)
  1772. {
  1773. struct list_head *pos;
  1774. struct multi_pack_index *m;
  1775. prepare_packed_git(r);
  1776. if (!r->objects->packed_git && !r->objects->multi_pack_index)
  1777. return 0;
  1778. for (m = r->objects->multi_pack_index; m; m = m->next) {
  1779. if (fill_midx_entry(r, oid, e, m))
  1780. return 1;
  1781. }
  1782. list_for_each(pos, &r->objects->packed_git_mru) {
  1783. struct packed_git *p = list_entry(pos, struct packed_git, mru);
  1784. if (!p->multi_pack_index && fill_pack_entry(oid, e, p)) {
  1785. list_move(&p->mru, &r->objects->packed_git_mru);
  1786. return 1;
  1787. }
  1788. }
  1789. return 0;
  1790. }
  1791. int has_object_pack(const struct object_id *oid)
  1792. {
  1793. struct pack_entry e;
  1794. return find_pack_entry(the_repository, oid, &e);
  1795. }
  1796. int has_pack_index(const unsigned char *sha1)
  1797. {
  1798. struct stat st;
  1799. if (stat(sha1_pack_index_name(sha1), &st))
  1800. return 0;
  1801. return 1;
  1802. }
  1803. int for_each_object_in_pack(struct packed_git *p,
  1804. each_packed_object_fn cb, void *data,
  1805. enum for_each_object_flags flags)
  1806. {
  1807. uint32_t i;
  1808. int r = 0;
  1809. if (flags & FOR_EACH_OBJECT_PACK_ORDER) {
  1810. if (load_pack_revindex(p))
  1811. return -1;
  1812. }
  1813. for (i = 0; i < p->num_objects; i++) {
  1814. uint32_t pos;
  1815. struct object_id oid;
  1816. if (flags & FOR_EACH_OBJECT_PACK_ORDER)
  1817. pos = p->revindex[i].nr;
  1818. else
  1819. pos = i;
  1820. if (nth_packed_object_id(&oid, p, pos) < 0)
  1821. return error("unable to get sha1 of object %u in %s",
  1822. pos, p->pack_name);
  1823. r = cb(&oid, p, pos, data);
  1824. if (r)
  1825. break;
  1826. }
  1827. return r;
  1828. }
  1829. int for_each_packed_object(each_packed_object_fn cb, void *data,
  1830. enum for_each_object_flags flags)
  1831. {
  1832. struct packed_git *p;
  1833. int r = 0;
  1834. int pack_errors = 0;
  1835. prepare_packed_git(the_repository);
  1836. for (p = get_all_packs(the_repository); p; p = p->next) {
  1837. if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
  1838. continue;
  1839. if ((flags & FOR_EACH_OBJECT_PROMISOR_ONLY) &&
  1840. !p->pack_promisor)
  1841. continue;
  1842. if (open_pack_index(p)) {
  1843. pack_errors = 1;
  1844. continue;
  1845. }
  1846. r = for_each_object_in_pack(p, cb, data, flags);
  1847. if (r)
  1848. break;
  1849. }
  1850. return r ? r : pack_errors;
  1851. }
  1852. static int add_promisor_object(const struct object_id *oid,
  1853. struct packed_git *pack,
  1854. uint32_t pos,
  1855. void *set_)
  1856. {
  1857. struct oidset *set = set_;
  1858. struct object *obj = parse_object(the_repository, oid);
  1859. if (!obj)
  1860. return 1;
  1861. oidset_insert(set, oid);
  1862. /*
  1863. * If this is a tree, commit, or tag, the objects it refers
  1864. * to are also promisor objects. (Blobs refer to no objects->)
  1865. */
  1866. if (obj->type == OBJ_TREE) {
  1867. struct tree *tree = (struct tree *)obj;
  1868. struct tree_desc desc;
  1869. struct name_entry entry;
  1870. if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
  1871. /*
  1872. * Error messages are given when packs are
  1873. * verified, so do not print any here.
  1874. */
  1875. return 0;
  1876. while (tree_entry_gently(&desc, &entry))
  1877. oidset_insert(set, &entry.oid);
  1878. } else if (obj->type == OBJ_COMMIT) {
  1879. struct commit *commit = (struct commit *) obj;
  1880. struct commit_list *parents = commit->parents;
  1881. oidset_insert(set, get_commit_tree_oid(commit));
  1882. for (; parents; parents = parents->next)
  1883. oidset_insert(set, &parents->item->object.oid);
  1884. } else if (obj->type == OBJ_TAG) {
  1885. struct tag *tag = (struct tag *) obj;
  1886. oidset_insert(set, get_tagged_oid(tag));
  1887. }
  1888. return 0;
  1889. }
  1890. int is_promisor_object(const struct object_id *oid)
  1891. {
  1892. static struct oidset promisor_objects;
  1893. static int promisor_objects_prepared;
  1894. if (!promisor_objects_prepared) {
  1895. if (has_promisor_remote()) {
  1896. for_each_packed_object(add_promisor_object,
  1897. &promisor_objects,
  1898. FOR_EACH_OBJECT_PROMISOR_ONLY);
  1899. }
  1900. promisor_objects_prepared = 1;
  1901. }
  1902. return oidset_contains(&promisor_objects, oid);
  1903. }