You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DfsPackFile.java 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*
  2. * Copyright (C) 2008-2011, Google Inc.
  3. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.internal.storage.dfs;
  46. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.UNREACHABLE_GARBAGE;
  47. import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
  48. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  49. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  50. import java.io.BufferedInputStream;
  51. import java.io.EOFException;
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54. import java.nio.ByteBuffer;
  55. import java.nio.channels.Channels;
  56. import java.text.MessageFormat;
  57. import java.util.Set;
  58. import java.util.zip.CRC32;
  59. import java.util.zip.DataFormatException;
  60. import java.util.zip.Inflater;
  61. import org.eclipse.jgit.errors.CorruptObjectException;
  62. import org.eclipse.jgit.errors.LargeObjectException;
  63. import org.eclipse.jgit.errors.MissingObjectException;
  64. import org.eclipse.jgit.errors.PackInvalidException;
  65. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  66. import org.eclipse.jgit.internal.JGitText;
  67. import org.eclipse.jgit.internal.storage.file.PackBitmapIndex;
  68. import org.eclipse.jgit.internal.storage.file.PackIndex;
  69. import org.eclipse.jgit.internal.storage.file.PackReverseIndex;
  70. import org.eclipse.jgit.internal.storage.pack.BinaryDelta;
  71. import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
  72. import org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation;
  73. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  74. import org.eclipse.jgit.lib.AnyObjectId;
  75. import org.eclipse.jgit.lib.Constants;
  76. import org.eclipse.jgit.lib.ObjectId;
  77. import org.eclipse.jgit.lib.ObjectLoader;
  78. import org.eclipse.jgit.lib.Repository;
  79. import org.eclipse.jgit.util.LongList;
  80. /**
  81. * A Git version 2 pack file representation. A pack file contains Git objects in
  82. * delta packed format yielding high compression of lots of object where some
  83. * objects are similar.
  84. */
  85. public final class DfsPackFile extends BlockBasedFile {
  86. private static final int REC_SIZE = Constants.OBJECT_ID_LENGTH + 8;
  87. /**
  88. * Lock for initialization of {@link #index} and {@link #corruptObjects}.
  89. * <p>
  90. * This lock ensures only one thread can perform the initialization work.
  91. */
  92. private final Object initLock = new Object();
  93. /** Index mapping {@link ObjectId} to position within the pack stream. */
  94. private volatile PackIndex index;
  95. /** Reverse version of {@link #index} mapping position to {@link ObjectId}. */
  96. private volatile PackReverseIndex reverseIndex;
  97. /** Index of compressed bitmap mapping entire object graph. */
  98. private volatile PackBitmapIndex bitmapIndex;
  99. /**
  100. * Objects we have tried to read, and discovered to be corrupt.
  101. * <p>
  102. * The list is allocated after the first corruption is found, and filled in
  103. * as more entries are discovered. Typically this list is never used, as
  104. * pack files do not usually contain corrupt objects.
  105. */
  106. private volatile LongList corruptObjects;
  107. /**
  108. * Construct a reader for an existing, packfile.
  109. *
  110. * @param cache
  111. * cache that owns the pack data.
  112. * @param desc
  113. * description of the pack within the DFS.
  114. */
  115. DfsPackFile(DfsBlockCache cache, DfsPackDescription desc) {
  116. super(cache, desc, PACK);
  117. int bs = desc.getBlockSize(PACK);
  118. if (bs > 0) {
  119. setBlockSize(bs);
  120. }
  121. long sz = desc.getFileSize(PACK);
  122. length = sz > 0 ? sz : -1;
  123. }
  124. /**
  125. * Get description that was originally used to configure this pack file.
  126. *
  127. * @return description that was originally used to configure this pack file.
  128. */
  129. public DfsPackDescription getPackDescription() {
  130. return desc;
  131. }
  132. /**
  133. * Whether the pack index file is loaded and cached in memory.
  134. *
  135. * @return whether the pack index file is loaded and cached in memory.
  136. */
  137. public boolean isIndexLoaded() {
  138. return index != null;
  139. }
  140. void setPackIndex(PackIndex idx) {
  141. long objCnt = idx.getObjectCount();
  142. int recSize = Constants.OBJECT_ID_LENGTH + 8;
  143. long sz = objCnt * recSize;
  144. cache.putRef(desc.getStreamKey(INDEX), sz, idx);
  145. index = idx;
  146. }
  147. /**
  148. * Get the PackIndex for this PackFile.
  149. *
  150. * @param ctx
  151. * reader context to support reading from the backing store if
  152. * the index is not already loaded in memory.
  153. * @return the PackIndex.
  154. * @throws java.io.IOException
  155. * the pack index is not available, or is corrupt.
  156. */
  157. public PackIndex getPackIndex(DfsReader ctx) throws IOException {
  158. return idx(ctx);
  159. }
  160. private PackIndex idx(DfsReader ctx) throws IOException {
  161. if (index != null) {
  162. return index;
  163. }
  164. if (invalid) {
  165. throw new PackInvalidException(getFileName(), invalidatingCause);
  166. }
  167. Repository.getGlobalListenerList()
  168. .dispatch(new BeforeDfsPackIndexLoadedEvent(this));
  169. synchronized (initLock) {
  170. if (index != null) {
  171. return index;
  172. }
  173. try {
  174. DfsStreamKey idxKey = desc.getStreamKey(INDEX);
  175. DfsBlockCache.Ref<PackIndex> idxref = cache.getOrLoadRef(idxKey,
  176. () -> {
  177. try {
  178. ctx.stats.readIdx++;
  179. long start = System.nanoTime();
  180. try (ReadableChannel rc = ctx.db.openFile(desc,
  181. INDEX)) {
  182. InputStream in = Channels
  183. .newInputStream(rc);
  184. int wantSize = 8192;
  185. int bs = rc.blockSize();
  186. if (0 < bs && bs < wantSize) {
  187. bs = (wantSize / bs) * bs;
  188. } else if (bs <= 0) {
  189. bs = wantSize;
  190. }
  191. PackIndex idx = PackIndex.read(
  192. new BufferedInputStream(in, bs));
  193. int sz = (int) Math.min(
  194. idx.getObjectCount() * REC_SIZE,
  195. Integer.MAX_VALUE);
  196. ctx.stats.readIdxBytes += rc.position();
  197. index = idx;
  198. return new DfsBlockCache.Ref<>(idxKey, 0,
  199. sz, idx);
  200. } finally {
  201. ctx.stats.readIdxMicros += elapsedMicros(
  202. start);
  203. }
  204. } catch (EOFException e) {
  205. throw new IOException(MessageFormat.format(
  206. DfsText.get().shortReadOfIndex,
  207. desc.getFileName(INDEX)), e);
  208. } catch (IOException e) {
  209. throw new IOException(MessageFormat.format(
  210. DfsText.get().cannotReadIndex,
  211. desc.getFileName(INDEX)), e);
  212. }
  213. });
  214. PackIndex idx = idxref.get();
  215. if (index == null && idx != null) {
  216. index = idx;
  217. }
  218. return index;
  219. } catch (IOException e) {
  220. invalid = true;
  221. invalidatingCause = e;
  222. throw e;
  223. }
  224. }
  225. }
  226. final boolean isGarbage() {
  227. return desc.getPackSource() == UNREACHABLE_GARBAGE;
  228. }
  229. PackBitmapIndex getBitmapIndex(DfsReader ctx) throws IOException {
  230. if (invalid || isGarbage() || !desc.hasFileExt(BITMAP_INDEX)) {
  231. return null;
  232. }
  233. if (bitmapIndex != null) {
  234. return bitmapIndex;
  235. }
  236. synchronized (initLock) {
  237. if (bitmapIndex != null) {
  238. return bitmapIndex;
  239. }
  240. PackIndex idx = idx(ctx);
  241. PackReverseIndex revidx = getReverseIdx(ctx);
  242. DfsStreamKey bitmapKey = desc.getStreamKey(BITMAP_INDEX);
  243. DfsBlockCache.Ref<PackBitmapIndex> idxref = cache
  244. .getOrLoadRef(bitmapKey, () -> {
  245. ctx.stats.readBitmap++;
  246. long start = System.nanoTime();
  247. try (ReadableChannel rc = ctx.db.openFile(desc,
  248. BITMAP_INDEX)) {
  249. long size;
  250. PackBitmapIndex bmidx;
  251. try {
  252. InputStream in = Channels.newInputStream(rc);
  253. int wantSize = 8192;
  254. int bs = rc.blockSize();
  255. if (0 < bs && bs < wantSize) {
  256. bs = (wantSize / bs) * bs;
  257. } else if (bs <= 0) {
  258. bs = wantSize;
  259. }
  260. in = new BufferedInputStream(in, bs);
  261. bmidx = PackBitmapIndex.read(in, idx, revidx);
  262. } finally {
  263. size = rc.position();
  264. ctx.stats.readIdxBytes += size;
  265. ctx.stats.readIdxMicros += elapsedMicros(start);
  266. }
  267. int sz = (int) Math.min(size, Integer.MAX_VALUE);
  268. bitmapIndex = bmidx;
  269. return new DfsBlockCache.Ref<>(bitmapKey, 0, sz,
  270. bmidx);
  271. } catch (EOFException e) {
  272. throw new IOException(MessageFormat.format(
  273. DfsText.get().shortReadOfIndex,
  274. desc.getFileName(BITMAP_INDEX)), e);
  275. } catch (IOException e) {
  276. throw new IOException(MessageFormat.format(
  277. DfsText.get().cannotReadIndex,
  278. desc.getFileName(BITMAP_INDEX)), e);
  279. }
  280. });
  281. PackBitmapIndex bmidx = idxref.get();
  282. if (bitmapIndex == null && bmidx != null) {
  283. bitmapIndex = bmidx;
  284. }
  285. return bitmapIndex;
  286. }
  287. }
  288. PackReverseIndex getReverseIdx(DfsReader ctx) throws IOException {
  289. if (reverseIndex != null) {
  290. return reverseIndex;
  291. }
  292. synchronized (initLock) {
  293. if (reverseIndex != null) {
  294. return reverseIndex;
  295. }
  296. PackIndex idx = idx(ctx);
  297. DfsStreamKey revKey = new DfsStreamKey.ForReverseIndex(
  298. desc.getStreamKey(INDEX));
  299. DfsBlockCache.Ref<PackReverseIndex> revref = cache
  300. .getOrLoadRef(revKey, () -> {
  301. PackReverseIndex revidx = new PackReverseIndex(idx);
  302. int sz = (int) Math.min(idx.getObjectCount() * 8,
  303. Integer.MAX_VALUE);
  304. reverseIndex = revidx;
  305. return new DfsBlockCache.Ref<>(revKey, 0, sz, revidx);
  306. });
  307. PackReverseIndex revidx = revref.get();
  308. if (reverseIndex == null && revidx != null) {
  309. reverseIndex = revidx;
  310. }
  311. return reverseIndex;
  312. }
  313. }
  314. /**
  315. * Check if an object is stored within this pack.
  316. *
  317. * @param ctx
  318. * reader context to support reading from the backing store if
  319. * the index is not already loaded in memory.
  320. * @param id
  321. * object to be located.
  322. * @return true if the object exists in this pack; false if it does not.
  323. * @throws java.io.IOException
  324. * the pack index is not available, or is corrupt.
  325. */
  326. public boolean hasObject(DfsReader ctx, AnyObjectId id) throws IOException {
  327. final long offset = idx(ctx).findOffset(id);
  328. return 0 < offset && !isCorrupt(offset);
  329. }
  330. /**
  331. * Get an object from this pack.
  332. *
  333. * @param ctx
  334. * temporary working space associated with the calling thread.
  335. * @param id
  336. * the object to obtain from the pack. Must not be null.
  337. * @return the object loader for the requested object if it is contained in
  338. * this pack; null if the object was not found.
  339. * @throws IOException
  340. * the pack file or the index could not be read.
  341. */
  342. ObjectLoader get(DfsReader ctx, AnyObjectId id)
  343. throws IOException {
  344. long offset = idx(ctx).findOffset(id);
  345. return 0 < offset && !isCorrupt(offset) ? load(ctx, offset) : null;
  346. }
  347. long findOffset(DfsReader ctx, AnyObjectId id) throws IOException {
  348. return idx(ctx).findOffset(id);
  349. }
  350. void resolve(DfsReader ctx, Set<ObjectId> matches, AbbreviatedObjectId id,
  351. int matchLimit) throws IOException {
  352. idx(ctx).resolve(matches, id, matchLimit);
  353. }
  354. /**
  355. * Obtain the total number of objects available in this pack. This method
  356. * relies on pack index, giving number of effectively available objects.
  357. *
  358. * @param ctx
  359. * current reader for the calling thread.
  360. * @return number of objects in index of this pack, likewise in this pack
  361. * @throws IOException
  362. * the index file cannot be loaded into memory.
  363. */
  364. long getObjectCount(DfsReader ctx) throws IOException {
  365. return idx(ctx).getObjectCount();
  366. }
  367. private byte[] decompress(long position, int sz, DfsReader ctx)
  368. throws IOException, DataFormatException {
  369. byte[] dstbuf;
  370. try {
  371. dstbuf = new byte[sz];
  372. } catch (OutOfMemoryError noMemory) {
  373. // The size may be larger than our heap allows, return null to
  374. // let the caller know allocation isn't possible and it should
  375. // use the large object streaming approach instead.
  376. //
  377. // For example, this can occur when sz is 640 MB, and JRE
  378. // maximum heap size is only 256 MB. Even if the JRE has
  379. // 200 MB free, it cannot allocate a 640 MB byte array.
  380. return null;
  381. }
  382. if (ctx.inflate(this, position, dstbuf, false) != sz) {
  383. throw new EOFException(MessageFormat.format(
  384. JGitText.get().shortCompressedStreamAt,
  385. Long.valueOf(position)));
  386. }
  387. return dstbuf;
  388. }
  389. void copyPackAsIs(PackOutputStream out, DfsReader ctx) throws IOException {
  390. // If the length hasn't been determined yet, pin to set it.
  391. if (length == -1) {
  392. ctx.pin(this, 0);
  393. ctx.unpin();
  394. }
  395. try (ReadableChannel rc = ctx.db.openFile(desc, PACK)) {
  396. int sz = ctx.getOptions().getStreamPackBufferSize();
  397. if (sz > 0) {
  398. rc.setReadAheadBytes(sz);
  399. }
  400. if (cache.shouldCopyThroughCache(length)) {
  401. copyPackThroughCache(out, ctx, rc);
  402. } else {
  403. copyPackBypassCache(out, rc);
  404. }
  405. }
  406. }
  407. private void copyPackThroughCache(PackOutputStream out, DfsReader ctx,
  408. ReadableChannel rc) throws IOException {
  409. long position = 12;
  410. long remaining = length - (12 + 20);
  411. while (0 < remaining) {
  412. DfsBlock b = cache.getOrLoad(this, position, ctx, () -> rc);
  413. int ptr = (int) (position - b.start);
  414. if (b.size() <= ptr) {
  415. throw packfileIsTruncated();
  416. }
  417. int n = (int) Math.min(b.size() - ptr, remaining);
  418. b.write(out, position, n);
  419. position += n;
  420. remaining -= n;
  421. }
  422. }
  423. private long copyPackBypassCache(PackOutputStream out, ReadableChannel rc)
  424. throws IOException {
  425. ByteBuffer buf = newCopyBuffer(out, rc);
  426. long position = 12;
  427. long remaining = length - (12 + 20);
  428. boolean packHeadSkipped = false;
  429. while (0 < remaining) {
  430. DfsBlock b = cache.get(key, alignToBlock(position));
  431. if (b != null) {
  432. int ptr = (int) (position - b.start);
  433. if (b.size() <= ptr) {
  434. throw packfileIsTruncated();
  435. }
  436. int n = (int) Math.min(b.size() - ptr, remaining);
  437. b.write(out, position, n);
  438. position += n;
  439. remaining -= n;
  440. rc.position(position);
  441. packHeadSkipped = true;
  442. continue;
  443. }
  444. // Need to skip the 'PACK' header for the first read
  445. int ptr = packHeadSkipped ? 0 : 12;
  446. buf.position(0);
  447. int bufLen = read(rc, buf);
  448. if (bufLen <= ptr) {
  449. throw packfileIsTruncated();
  450. }
  451. int n = (int) Math.min(bufLen - ptr, remaining);
  452. out.write(buf.array(), ptr, n);
  453. position += n;
  454. remaining -= n;
  455. packHeadSkipped = true;
  456. }
  457. return position;
  458. }
  459. private ByteBuffer newCopyBuffer(PackOutputStream out, ReadableChannel rc) {
  460. int bs = blockSize(rc);
  461. byte[] copyBuf = out.getCopyBuffer();
  462. if (bs > copyBuf.length) {
  463. copyBuf = new byte[bs];
  464. }
  465. return ByteBuffer.wrap(copyBuf, 0, bs);
  466. }
  467. void copyAsIs(PackOutputStream out, DfsObjectToPack src,
  468. boolean validate, DfsReader ctx) throws IOException,
  469. StoredObjectRepresentationNotAvailableException {
  470. final CRC32 crc1 = validate ? new CRC32() : null;
  471. final CRC32 crc2 = validate ? new CRC32() : null;
  472. final byte[] buf = out.getCopyBuffer();
  473. // Rip apart the header so we can discover the size.
  474. //
  475. try {
  476. readFully(src.offset, buf, 0, 20, ctx);
  477. } catch (IOException ioError) {
  478. throw new StoredObjectRepresentationNotAvailableException(src,
  479. ioError);
  480. }
  481. int c = buf[0] & 0xff;
  482. final int typeCode = (c >> 4) & 7;
  483. long inflatedLength = c & 15;
  484. int shift = 4;
  485. int headerCnt = 1;
  486. while ((c & 0x80) != 0) {
  487. c = buf[headerCnt++] & 0xff;
  488. inflatedLength += ((long) (c & 0x7f)) << shift;
  489. shift += 7;
  490. }
  491. if (typeCode == Constants.OBJ_OFS_DELTA) {
  492. do {
  493. c = buf[headerCnt++] & 0xff;
  494. } while ((c & 128) != 0);
  495. if (validate) {
  496. assert(crc1 != null && crc2 != null);
  497. crc1.update(buf, 0, headerCnt);
  498. crc2.update(buf, 0, headerCnt);
  499. }
  500. } else if (typeCode == Constants.OBJ_REF_DELTA) {
  501. if (validate) {
  502. assert(crc1 != null && crc2 != null);
  503. crc1.update(buf, 0, headerCnt);
  504. crc2.update(buf, 0, headerCnt);
  505. }
  506. readFully(src.offset + headerCnt, buf, 0, 20, ctx);
  507. if (validate) {
  508. assert(crc1 != null && crc2 != null);
  509. crc1.update(buf, 0, 20);
  510. crc2.update(buf, 0, 20);
  511. }
  512. headerCnt += 20;
  513. } else if (validate) {
  514. assert(crc1 != null && crc2 != null);
  515. crc1.update(buf, 0, headerCnt);
  516. crc2.update(buf, 0, headerCnt);
  517. }
  518. final long dataOffset = src.offset + headerCnt;
  519. final long dataLength = src.length;
  520. final long expectedCRC;
  521. final DfsBlock quickCopy;
  522. // Verify the object isn't corrupt before sending. If it is,
  523. // we report it missing instead.
  524. //
  525. try {
  526. quickCopy = ctx.quickCopy(this, dataOffset, dataLength);
  527. if (validate && idx(ctx).hasCRC32Support()) {
  528. assert(crc1 != null);
  529. // Index has the CRC32 code cached, validate the object.
  530. //
  531. expectedCRC = idx(ctx).findCRC32(src);
  532. if (quickCopy != null) {
  533. quickCopy.crc32(crc1, dataOffset, (int) dataLength);
  534. } else {
  535. long pos = dataOffset;
  536. long cnt = dataLength;
  537. while (cnt > 0) {
  538. final int n = (int) Math.min(cnt, buf.length);
  539. readFully(pos, buf, 0, n, ctx);
  540. crc1.update(buf, 0, n);
  541. pos += n;
  542. cnt -= n;
  543. }
  544. }
  545. if (crc1.getValue() != expectedCRC) {
  546. setCorrupt(src.offset);
  547. throw new CorruptObjectException(MessageFormat.format(
  548. JGitText.get().objectAtHasBadZlibStream,
  549. Long.valueOf(src.offset), getFileName()));
  550. }
  551. } else if (validate) {
  552. assert(crc1 != null);
  553. // We don't have a CRC32 code in the index, so compute it
  554. // now while inflating the raw data to get zlib to tell us
  555. // whether or not the data is safe.
  556. //
  557. Inflater inf = ctx.inflater();
  558. byte[] tmp = new byte[1024];
  559. if (quickCopy != null) {
  560. quickCopy.check(inf, tmp, dataOffset, (int) dataLength);
  561. } else {
  562. long pos = dataOffset;
  563. long cnt = dataLength;
  564. while (cnt > 0) {
  565. final int n = (int) Math.min(cnt, buf.length);
  566. readFully(pos, buf, 0, n, ctx);
  567. crc1.update(buf, 0, n);
  568. inf.setInput(buf, 0, n);
  569. while (inf.inflate(tmp, 0, tmp.length) > 0) {
  570. continue;
  571. }
  572. pos += n;
  573. cnt -= n;
  574. }
  575. }
  576. if (!inf.finished() || inf.getBytesRead() != dataLength) {
  577. setCorrupt(src.offset);
  578. throw new EOFException(MessageFormat.format(
  579. JGitText.get().shortCompressedStreamAt,
  580. Long.valueOf(src.offset)));
  581. }
  582. expectedCRC = crc1.getValue();
  583. } else {
  584. expectedCRC = -1;
  585. }
  586. } catch (DataFormatException dataFormat) {
  587. setCorrupt(src.offset);
  588. CorruptObjectException corruptObject = new CorruptObjectException(
  589. MessageFormat.format(
  590. JGitText.get().objectAtHasBadZlibStream,
  591. Long.valueOf(src.offset), getFileName()),
  592. dataFormat);
  593. throw new StoredObjectRepresentationNotAvailableException(src,
  594. corruptObject);
  595. } catch (IOException ioError) {
  596. throw new StoredObjectRepresentationNotAvailableException(src,
  597. ioError);
  598. }
  599. if (quickCopy != null) {
  600. // The entire object fits into a single byte array window slice,
  601. // and we have it pinned. Write this out without copying.
  602. //
  603. out.writeHeader(src, inflatedLength);
  604. quickCopy.write(out, dataOffset, (int) dataLength);
  605. } else if (dataLength <= buf.length) {
  606. // Tiny optimization: Lots of objects are very small deltas or
  607. // deflated commits that are likely to fit in the copy buffer.
  608. //
  609. if (!validate) {
  610. long pos = dataOffset;
  611. long cnt = dataLength;
  612. while (cnt > 0) {
  613. final int n = (int) Math.min(cnt, buf.length);
  614. readFully(pos, buf, 0, n, ctx);
  615. pos += n;
  616. cnt -= n;
  617. }
  618. }
  619. out.writeHeader(src, inflatedLength);
  620. out.write(buf, 0, (int) dataLength);
  621. } else {
  622. // Now we are committed to sending the object. As we spool it out,
  623. // check its CRC32 code to make sure there wasn't corruption between
  624. // the verification we did above, and us actually outputting it.
  625. //
  626. out.writeHeader(src, inflatedLength);
  627. long pos = dataOffset;
  628. long cnt = dataLength;
  629. while (cnt > 0) {
  630. final int n = (int) Math.min(cnt, buf.length);
  631. readFully(pos, buf, 0, n, ctx);
  632. if (validate) {
  633. assert(crc2 != null);
  634. crc2.update(buf, 0, n);
  635. }
  636. out.write(buf, 0, n);
  637. pos += n;
  638. cnt -= n;
  639. }
  640. if (validate) {
  641. assert(crc2 != null);
  642. if (crc2.getValue() != expectedCRC) {
  643. throw new CorruptObjectException(MessageFormat.format(
  644. JGitText.get().objectAtHasBadZlibStream,
  645. Long.valueOf(src.offset), getFileName()));
  646. }
  647. }
  648. }
  649. }
  650. private IOException packfileIsTruncated() {
  651. invalid = true;
  652. IOException exc = new IOException(MessageFormat.format(
  653. JGitText.get().packfileIsTruncated, getFileName()));
  654. invalidatingCause = exc;
  655. return exc;
  656. }
  657. private void readFully(long position, byte[] dstbuf, int dstoff, int cnt,
  658. DfsReader ctx) throws IOException {
  659. if (ctx.copy(this, position, dstbuf, dstoff, cnt) != cnt)
  660. throw new EOFException();
  661. }
  662. ObjectLoader load(DfsReader ctx, long pos)
  663. throws IOException {
  664. try {
  665. final byte[] ib = ctx.tempId;
  666. Delta delta = null;
  667. byte[] data = null;
  668. int type = Constants.OBJ_BAD;
  669. boolean cached = false;
  670. SEARCH: for (;;) {
  671. readFully(pos, ib, 0, 20, ctx);
  672. int c = ib[0] & 0xff;
  673. final int typeCode = (c >> 4) & 7;
  674. long sz = c & 15;
  675. int shift = 4;
  676. int p = 1;
  677. while ((c & 0x80) != 0) {
  678. c = ib[p++] & 0xff;
  679. sz += ((long) (c & 0x7f)) << shift;
  680. shift += 7;
  681. }
  682. switch (typeCode) {
  683. case Constants.OBJ_COMMIT:
  684. case Constants.OBJ_TREE:
  685. case Constants.OBJ_BLOB:
  686. case Constants.OBJ_TAG: {
  687. if (delta != null) {
  688. data = decompress(pos + p, (int) sz, ctx);
  689. type = typeCode;
  690. break SEARCH;
  691. }
  692. if (sz < ctx.getStreamFileThreshold()) {
  693. data = decompress(pos + p, (int) sz, ctx);
  694. if (data != null) {
  695. return new ObjectLoader.SmallObject(typeCode, data);
  696. }
  697. }
  698. return new LargePackedWholeObject(typeCode, sz, pos, p, this, ctx.db);
  699. }
  700. case Constants.OBJ_OFS_DELTA: {
  701. c = ib[p++] & 0xff;
  702. long base = c & 127;
  703. while ((c & 128) != 0) {
  704. base += 1;
  705. c = ib[p++] & 0xff;
  706. base <<= 7;
  707. base += (c & 127);
  708. }
  709. base = pos - base;
  710. delta = new Delta(delta, pos, (int) sz, p, base);
  711. if (sz != delta.deltaSize) {
  712. break SEARCH;
  713. }
  714. DeltaBaseCache.Entry e = ctx.getDeltaBaseCache().get(key, base);
  715. if (e != null) {
  716. type = e.type;
  717. data = e.data;
  718. cached = true;
  719. break SEARCH;
  720. }
  721. pos = base;
  722. continue SEARCH;
  723. }
  724. case Constants.OBJ_REF_DELTA: {
  725. readFully(pos + p, ib, 0, 20, ctx);
  726. long base = findDeltaBase(ctx, ObjectId.fromRaw(ib));
  727. delta = new Delta(delta, pos, (int) sz, p + 20, base);
  728. if (sz != delta.deltaSize) {
  729. break SEARCH;
  730. }
  731. DeltaBaseCache.Entry e = ctx.getDeltaBaseCache().get(key, base);
  732. if (e != null) {
  733. type = e.type;
  734. data = e.data;
  735. cached = true;
  736. break SEARCH;
  737. }
  738. pos = base;
  739. continue SEARCH;
  740. }
  741. default:
  742. throw new IOException(MessageFormat.format(
  743. JGitText.get().unknownObjectType, Integer.valueOf(typeCode)));
  744. }
  745. }
  746. // At this point there is at least one delta to apply to data.
  747. // (Whole objects with no deltas to apply return early above.)
  748. if (data == null)
  749. throw new LargeObjectException();
  750. assert(delta != null);
  751. do {
  752. // Cache only the base immediately before desired object.
  753. if (cached) {
  754. cached = false;
  755. } else if (delta.next == null) {
  756. ctx.getDeltaBaseCache().put(key, delta.basePos, type, data);
  757. }
  758. pos = delta.deltaPos;
  759. byte[] cmds = decompress(pos + delta.hdrLen, delta.deltaSize, ctx);
  760. if (cmds == null) {
  761. data = null; // Discard base in case of OutOfMemoryError
  762. throw new LargeObjectException();
  763. }
  764. final long sz = BinaryDelta.getResultSize(cmds);
  765. if (Integer.MAX_VALUE <= sz) {
  766. throw new LargeObjectException.ExceedsByteArrayLimit();
  767. }
  768. final byte[] result;
  769. try {
  770. result = new byte[(int) sz];
  771. } catch (OutOfMemoryError tooBig) {
  772. data = null; // Discard base in case of OutOfMemoryError
  773. cmds = null;
  774. throw new LargeObjectException.OutOfMemory(tooBig);
  775. }
  776. BinaryDelta.apply(data, cmds, result);
  777. data = result;
  778. delta = delta.next;
  779. } while (delta != null);
  780. return new ObjectLoader.SmallObject(type, data);
  781. } catch (DataFormatException dfe) {
  782. throw new CorruptObjectException(
  783. MessageFormat.format(
  784. JGitText.get().objectAtHasBadZlibStream, Long.valueOf(pos),
  785. getFileName()),
  786. dfe);
  787. }
  788. }
  789. private long findDeltaBase(DfsReader ctx, ObjectId baseId)
  790. throws IOException, MissingObjectException {
  791. long ofs = idx(ctx).findOffset(baseId);
  792. if (ofs < 0) {
  793. throw new MissingObjectException(baseId,
  794. JGitText.get().missingDeltaBase);
  795. }
  796. return ofs;
  797. }
  798. private static class Delta {
  799. /** Child that applies onto this object. */
  800. final Delta next;
  801. /** Offset of the delta object. */
  802. final long deltaPos;
  803. /** Size of the inflated delta stream. */
  804. final int deltaSize;
  805. /** Total size of the delta's pack entry header (including base). */
  806. final int hdrLen;
  807. /** Offset of the base object this delta applies onto. */
  808. final long basePos;
  809. Delta(Delta next, long ofs, int sz, int hdrLen, long baseOffset) {
  810. this.next = next;
  811. this.deltaPos = ofs;
  812. this.deltaSize = sz;
  813. this.hdrLen = hdrLen;
  814. this.basePos = baseOffset;
  815. }
  816. }
  817. byte[] getDeltaHeader(DfsReader wc, long pos)
  818. throws IOException, DataFormatException {
  819. // The delta stream starts as two variable length integers. If we
  820. // assume they are 64 bits each, we need 16 bytes to encode them,
  821. // plus 2 extra bytes for the variable length overhead. So 18 is
  822. // the longest delta instruction header.
  823. //
  824. final byte[] hdr = new byte[32];
  825. wc.inflate(this, pos, hdr, true /* header only */);
  826. return hdr;
  827. }
  828. int getObjectType(DfsReader ctx, long pos) throws IOException {
  829. final byte[] ib = ctx.tempId;
  830. for (;;) {
  831. readFully(pos, ib, 0, 20, ctx);
  832. int c = ib[0] & 0xff;
  833. final int type = (c >> 4) & 7;
  834. switch (type) {
  835. case Constants.OBJ_COMMIT:
  836. case Constants.OBJ_TREE:
  837. case Constants.OBJ_BLOB:
  838. case Constants.OBJ_TAG:
  839. return type;
  840. case Constants.OBJ_OFS_DELTA: {
  841. int p = 1;
  842. while ((c & 0x80) != 0) {
  843. c = ib[p++] & 0xff;
  844. }
  845. c = ib[p++] & 0xff;
  846. long ofs = c & 127;
  847. while ((c & 128) != 0) {
  848. ofs += 1;
  849. c = ib[p++] & 0xff;
  850. ofs <<= 7;
  851. ofs += (c & 127);
  852. }
  853. pos = pos - ofs;
  854. continue;
  855. }
  856. case Constants.OBJ_REF_DELTA: {
  857. int p = 1;
  858. while ((c & 0x80) != 0) {
  859. c = ib[p++] & 0xff;
  860. }
  861. readFully(pos + p, ib, 0, 20, ctx);
  862. pos = findDeltaBase(ctx, ObjectId.fromRaw(ib));
  863. continue;
  864. }
  865. default:
  866. throw new IOException(MessageFormat.format(
  867. JGitText.get().unknownObjectType, Integer.valueOf(type)));
  868. }
  869. }
  870. }
  871. long getObjectSize(DfsReader ctx, AnyObjectId id) throws IOException {
  872. final long offset = idx(ctx).findOffset(id);
  873. return 0 < offset ? getObjectSize(ctx, offset) : -1;
  874. }
  875. long getObjectSize(DfsReader ctx, long pos)
  876. throws IOException {
  877. final byte[] ib = ctx.tempId;
  878. readFully(pos, ib, 0, 20, ctx);
  879. int c = ib[0] & 0xff;
  880. final int type = (c >> 4) & 7;
  881. long sz = c & 15;
  882. int shift = 4;
  883. int p = 1;
  884. while ((c & 0x80) != 0) {
  885. c = ib[p++] & 0xff;
  886. sz += ((long) (c & 0x7f)) << shift;
  887. shift += 7;
  888. }
  889. long deltaAt;
  890. switch (type) {
  891. case Constants.OBJ_COMMIT:
  892. case Constants.OBJ_TREE:
  893. case Constants.OBJ_BLOB:
  894. case Constants.OBJ_TAG:
  895. return sz;
  896. case Constants.OBJ_OFS_DELTA:
  897. c = ib[p++] & 0xff;
  898. while ((c & 128) != 0) {
  899. c = ib[p++] & 0xff;
  900. }
  901. deltaAt = pos + p;
  902. break;
  903. case Constants.OBJ_REF_DELTA:
  904. deltaAt = pos + p + 20;
  905. break;
  906. default:
  907. throw new IOException(MessageFormat.format(
  908. JGitText.get().unknownObjectType, Integer.valueOf(type)));
  909. }
  910. try {
  911. return BinaryDelta.getResultSize(getDeltaHeader(ctx, deltaAt));
  912. } catch (DataFormatException dfe) {
  913. throw new CorruptObjectException(
  914. MessageFormat.format(
  915. JGitText.get().objectAtHasBadZlibStream, Long.valueOf(pos),
  916. getFileName()),
  917. dfe);
  918. }
  919. }
  920. void representation(DfsObjectRepresentation r, final long pos,
  921. DfsReader ctx, PackReverseIndex rev)
  922. throws IOException {
  923. r.offset = pos;
  924. final byte[] ib = ctx.tempId;
  925. readFully(pos, ib, 0, 20, ctx);
  926. int c = ib[0] & 0xff;
  927. int p = 1;
  928. final int typeCode = (c >> 4) & 7;
  929. while ((c & 0x80) != 0) {
  930. c = ib[p++] & 0xff;
  931. }
  932. long len = rev.findNextOffset(pos, length - 20) - pos;
  933. switch (typeCode) {
  934. case Constants.OBJ_COMMIT:
  935. case Constants.OBJ_TREE:
  936. case Constants.OBJ_BLOB:
  937. case Constants.OBJ_TAG:
  938. r.format = StoredObjectRepresentation.PACK_WHOLE;
  939. r.baseId = null;
  940. r.length = len - p;
  941. return;
  942. case Constants.OBJ_OFS_DELTA: {
  943. c = ib[p++] & 0xff;
  944. long ofs = c & 127;
  945. while ((c & 128) != 0) {
  946. ofs += 1;
  947. c = ib[p++] & 0xff;
  948. ofs <<= 7;
  949. ofs += (c & 127);
  950. }
  951. r.format = StoredObjectRepresentation.PACK_DELTA;
  952. r.baseId = rev.findObject(pos - ofs);
  953. r.length = len - p;
  954. return;
  955. }
  956. case Constants.OBJ_REF_DELTA: {
  957. readFully(pos + p, ib, 0, 20, ctx);
  958. r.format = StoredObjectRepresentation.PACK_DELTA;
  959. r.baseId = ObjectId.fromRaw(ib);
  960. r.length = len - p - 20;
  961. return;
  962. }
  963. default:
  964. throw new IOException(MessageFormat.format(
  965. JGitText.get().unknownObjectType, Integer.valueOf(typeCode)));
  966. }
  967. }
  968. boolean isCorrupt(long offset) {
  969. LongList list = corruptObjects;
  970. if (list == null) {
  971. return false;
  972. }
  973. synchronized (list) {
  974. return list.contains(offset);
  975. }
  976. }
  977. private void setCorrupt(long offset) {
  978. LongList list = corruptObjects;
  979. if (list == null) {
  980. synchronized (initLock) {
  981. list = corruptObjects;
  982. if (list == null) {
  983. list = new LongList();
  984. corruptObjects = list;
  985. }
  986. }
  987. }
  988. synchronized (list) {
  989. list.add(offset);
  990. }
  991. }
  992. }