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 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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.PackExt;
  72. import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
  73. import org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation;
  74. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  75. import org.eclipse.jgit.lib.AnyObjectId;
  76. import org.eclipse.jgit.lib.Constants;
  77. import org.eclipse.jgit.lib.ObjectId;
  78. import org.eclipse.jgit.lib.ObjectLoader;
  79. import org.eclipse.jgit.lib.Repository;
  80. import org.eclipse.jgit.util.LongList;
  81. /**
  82. * A Git version 2 pack file representation. A pack file contains Git objects in
  83. * delta packed format yielding high compression of lots of object where some
  84. * objects are similar.
  85. */
  86. public final class DfsPackFile {
  87. /**
  88. * File offset used to cache {@link #index} in {@link DfsBlockCache}.
  89. * <p>
  90. * To better manage memory, the forward index is stored as a single block in
  91. * the block cache under this file position. A negative value is used
  92. * because it cannot occur in a normal pack file, and it is less likely to
  93. * collide with a valid data block from the file as the high bits will all
  94. * be set when treated as an unsigned long by the cache code.
  95. */
  96. private static final long POS_INDEX = -1;
  97. /** Offset used to cache {@link #reverseIndex}. See {@link #POS_INDEX}. */
  98. private static final long POS_REVERSE_INDEX = -2;
  99. /** Offset used to cache {@link #bitmapIndex}. See {@link #POS_INDEX}. */
  100. private static final long POS_BITMAP_INDEX = -3;
  101. /** Cache that owns this pack file and its data. */
  102. private final DfsBlockCache cache;
  103. /** Description of the pack file's storage. */
  104. private final DfsPackDescription packDesc;
  105. /** Unique identity of this pack while in-memory. */
  106. final DfsPackKey key;
  107. /**
  108. * Total number of bytes in this pack file.
  109. * <p>
  110. * This field initializes to -1 and gets populated when a block is loaded.
  111. */
  112. volatile long length;
  113. /**
  114. * Preferred alignment for loading blocks from the backing file.
  115. * <p>
  116. * It is initialized to 0 and filled in on the first read made from the
  117. * file. Block sizes may be odd, e.g. 4091, caused by the underling DFS
  118. * storing 4091 user bytes and 5 bytes block metadata into a lower level
  119. * 4096 byte block on disk.
  120. */
  121. private volatile int blockSize;
  122. /** True once corruption has been detected that cannot be worked around. */
  123. private volatile boolean invalid;
  124. /**
  125. * Lock for initialization of {@link #index} and {@link #corruptObjects}.
  126. * <p>
  127. * This lock ensures only one thread can perform the initialization work.
  128. */
  129. private final Object initLock = new Object();
  130. /** Index mapping {@link ObjectId} to position within the pack stream. */
  131. private volatile DfsBlockCache.Ref<PackIndex> index;
  132. /** Reverse version of {@link #index} mapping position to {@link ObjectId}. */
  133. private volatile DfsBlockCache.Ref<PackReverseIndex> reverseIndex;
  134. /** Index of compressed bitmap mapping entire object graph. */
  135. private volatile DfsBlockCache.Ref<PackBitmapIndex> bitmapIndex;
  136. /**
  137. * Objects we have tried to read, and discovered to be corrupt.
  138. * <p>
  139. * The list is allocated after the first corruption is found, and filled in
  140. * as more entries are discovered. Typically this list is never used, as
  141. * pack files do not usually contain corrupt objects.
  142. */
  143. private volatile LongList corruptObjects;
  144. /**
  145. * Construct a reader for an existing, packfile.
  146. *
  147. * @param cache
  148. * cache that owns the pack data.
  149. * @param desc
  150. * description of the pack within the DFS.
  151. * @param key
  152. * interned key used to identify blocks in the block cache.
  153. */
  154. DfsPackFile(DfsBlockCache cache, DfsPackDescription desc, DfsPackKey key) {
  155. this.cache = cache;
  156. this.packDesc = desc;
  157. this.key = key;
  158. length = desc.getFileSize(PACK);
  159. if (length <= 0)
  160. length = -1;
  161. }
  162. /** @return description that was originally used to configure this pack file. */
  163. public DfsPackDescription getPackDescription() {
  164. return packDesc;
  165. }
  166. /**
  167. * @return whether the pack index file is loaded and cached in memory.
  168. * @since 2.2
  169. */
  170. public boolean isIndexLoaded() {
  171. DfsBlockCache.Ref<PackIndex> idxref = index;
  172. return idxref != null && idxref.has();
  173. }
  174. /** @return bytes cached in memory for this pack, excluding the index. */
  175. public long getCachedSize() {
  176. return key.cachedSize.get();
  177. }
  178. String getPackName() {
  179. return packDesc.getFileName(PACK);
  180. }
  181. void setBlockSize(int newSize) {
  182. blockSize = newSize;
  183. }
  184. void setPackIndex(PackIndex idx) {
  185. long objCnt = idx.getObjectCount();
  186. int recSize = Constants.OBJECT_ID_LENGTH + 8;
  187. int sz = (int) Math.min(objCnt * recSize, Integer.MAX_VALUE);
  188. index = cache.put(key, POS_INDEX, sz, idx);
  189. }
  190. /**
  191. * Get the PackIndex for this PackFile.
  192. *
  193. * @param ctx
  194. * reader context to support reading from the backing store if
  195. * the index is not already loaded in memory.
  196. * @return the PackIndex.
  197. * @throws IOException
  198. * the pack index is not available, or is corrupt.
  199. */
  200. public PackIndex getPackIndex(DfsReader ctx) throws IOException {
  201. return idx(ctx);
  202. }
  203. private PackIndex idx(DfsReader ctx) throws IOException {
  204. DfsBlockCache.Ref<PackIndex> idxref = index;
  205. if (idxref != null) {
  206. PackIndex idx = idxref.get();
  207. if (idx != null)
  208. return idx;
  209. }
  210. if (invalid)
  211. throw new PackInvalidException(getPackName());
  212. Repository.getGlobalListenerList()
  213. .dispatch(new BeforeDfsPackIndexLoadedEvent(this));
  214. synchronized (initLock) {
  215. idxref = index;
  216. if (idxref != null) {
  217. PackIndex idx = idxref.get();
  218. if (idx != null)
  219. return idx;
  220. }
  221. PackIndex idx;
  222. try {
  223. ReadableChannel rc = ctx.db.openFile(packDesc, INDEX);
  224. try {
  225. InputStream in = Channels.newInputStream(rc);
  226. int wantSize = 8192;
  227. int bs = rc.blockSize();
  228. if (0 < bs && bs < wantSize)
  229. bs = (wantSize / bs) * bs;
  230. else if (bs <= 0)
  231. bs = wantSize;
  232. in = new BufferedInputStream(in, bs);
  233. idx = PackIndex.read(in);
  234. } finally {
  235. rc.close();
  236. }
  237. } catch (EOFException e) {
  238. invalid = true;
  239. IOException e2 = new IOException(MessageFormat.format(
  240. DfsText.get().shortReadOfIndex,
  241. packDesc.getFileName(INDEX)));
  242. e2.initCause(e);
  243. throw e2;
  244. } catch (IOException e) {
  245. invalid = true;
  246. IOException e2 = new IOException(MessageFormat.format(
  247. DfsText.get().cannotReadIndex,
  248. packDesc.getFileName(INDEX)));
  249. e2.initCause(e);
  250. throw e2;
  251. }
  252. setPackIndex(idx);
  253. return idx;
  254. }
  255. }
  256. final boolean isGarbage() {
  257. return packDesc.getPackSource() == UNREACHABLE_GARBAGE;
  258. }
  259. PackBitmapIndex getBitmapIndex(DfsReader ctx) throws IOException {
  260. if (invalid || isGarbage())
  261. return null;
  262. DfsBlockCache.Ref<PackBitmapIndex> idxref = bitmapIndex;
  263. if (idxref != null) {
  264. PackBitmapIndex idx = idxref.get();
  265. if (idx != null)
  266. return idx;
  267. }
  268. if (!packDesc.hasFileExt(PackExt.BITMAP_INDEX))
  269. return null;
  270. synchronized (initLock) {
  271. idxref = bitmapIndex;
  272. if (idxref != null) {
  273. PackBitmapIndex idx = idxref.get();
  274. if (idx != null)
  275. return idx;
  276. }
  277. long size;
  278. PackBitmapIndex idx;
  279. try {
  280. ReadableChannel rc = ctx.db.openFile(packDesc, BITMAP_INDEX);
  281. try {
  282. InputStream in = Channels.newInputStream(rc);
  283. int wantSize = 8192;
  284. int bs = rc.blockSize();
  285. if (0 < bs && bs < wantSize)
  286. bs = (wantSize / bs) * bs;
  287. else if (bs <= 0)
  288. bs = wantSize;
  289. in = new BufferedInputStream(in, bs);
  290. idx = PackBitmapIndex.read(
  291. in, idx(ctx), getReverseIdx(ctx));
  292. } finally {
  293. size = rc.position();
  294. rc.close();
  295. }
  296. } catch (EOFException e) {
  297. IOException e2 = new IOException(MessageFormat.format(
  298. DfsText.get().shortReadOfIndex,
  299. packDesc.getFileName(BITMAP_INDEX)));
  300. e2.initCause(e);
  301. throw e2;
  302. } catch (IOException e) {
  303. IOException e2 = new IOException(MessageFormat.format(
  304. DfsText.get().cannotReadIndex,
  305. packDesc.getFileName(BITMAP_INDEX)));
  306. e2.initCause(e);
  307. throw e2;
  308. }
  309. bitmapIndex = cache.put(key, POS_BITMAP_INDEX,
  310. (int) Math.min(size, Integer.MAX_VALUE), idx);
  311. return idx;
  312. }
  313. }
  314. PackReverseIndex getReverseIdx(DfsReader ctx) throws IOException {
  315. DfsBlockCache.Ref<PackReverseIndex> revref = reverseIndex;
  316. if (revref != null) {
  317. PackReverseIndex revidx = revref.get();
  318. if (revidx != null)
  319. return revidx;
  320. }
  321. synchronized (initLock) {
  322. revref = reverseIndex;
  323. if (revref != null) {
  324. PackReverseIndex revidx = revref.get();
  325. if (revidx != null)
  326. return revidx;
  327. }
  328. PackIndex idx = idx(ctx);
  329. PackReverseIndex revidx = new PackReverseIndex(idx);
  330. int sz = (int) Math.min(
  331. idx.getObjectCount() * 8, Integer.MAX_VALUE);
  332. reverseIndex = cache.put(key, POS_REVERSE_INDEX, sz, revidx);
  333. return revidx;
  334. }
  335. }
  336. /**
  337. * Check if an object is stored within this pack.
  338. *
  339. * @param ctx
  340. * reader context to support reading from the backing store if
  341. * the index is not already loaded in memory.
  342. * @param id
  343. * object to be located.
  344. * @return true if the object exists in this pack; false if it does not.
  345. * @throws IOException
  346. * the pack index is not available, or is corrupt.
  347. */
  348. public boolean hasObject(DfsReader ctx, AnyObjectId id) throws IOException {
  349. final long offset = idx(ctx).findOffset(id);
  350. return 0 < offset && !isCorrupt(offset);
  351. }
  352. /**
  353. * Get an object from this pack.
  354. *
  355. * @param ctx
  356. * temporary working space associated with the calling thread.
  357. * @param id
  358. * the object to obtain from the pack. Must not be null.
  359. * @return the object loader for the requested object if it is contained in
  360. * this pack; null if the object was not found.
  361. * @throws IOException
  362. * the pack file or the index could not be read.
  363. */
  364. ObjectLoader get(DfsReader ctx, AnyObjectId id)
  365. throws IOException {
  366. long offset = idx(ctx).findOffset(id);
  367. return 0 < offset && !isCorrupt(offset) ? load(ctx, offset) : null;
  368. }
  369. long findOffset(DfsReader ctx, AnyObjectId id) throws IOException {
  370. return idx(ctx).findOffset(id);
  371. }
  372. void resolve(DfsReader ctx, Set<ObjectId> matches, AbbreviatedObjectId id,
  373. int matchLimit) throws IOException {
  374. idx(ctx).resolve(matches, id, matchLimit);
  375. }
  376. /** Release all memory used by this DfsPackFile instance. */
  377. public void close() {
  378. cache.remove(this);
  379. index = null;
  380. reverseIndex = null;
  381. }
  382. /**
  383. * Obtain the total number of objects available in this pack. This method
  384. * relies on pack index, giving number of effectively available objects.
  385. *
  386. * @param ctx
  387. * current reader for the calling thread.
  388. * @return number of objects in index of this pack, likewise in this pack
  389. * @throws IOException
  390. * the index file cannot be loaded into memory.
  391. */
  392. long getObjectCount(DfsReader ctx) throws IOException {
  393. return idx(ctx).getObjectCount();
  394. }
  395. private byte[] decompress(long position, int sz, DfsReader ctx)
  396. throws IOException, DataFormatException {
  397. byte[] dstbuf;
  398. try {
  399. dstbuf = new byte[sz];
  400. } catch (OutOfMemoryError noMemory) {
  401. // The size may be larger than our heap allows, return null to
  402. // let the caller know allocation isn't possible and it should
  403. // use the large object streaming approach instead.
  404. //
  405. // For example, this can occur when sz is 640 MB, and JRE
  406. // maximum heap size is only 256 MB. Even if the JRE has
  407. // 200 MB free, it cannot allocate a 640 MB byte array.
  408. return null;
  409. }
  410. if (ctx.inflate(this, position, dstbuf, false) != sz)
  411. throw new EOFException(MessageFormat.format(
  412. JGitText.get().shortCompressedStreamAt,
  413. Long.valueOf(position)));
  414. return dstbuf;
  415. }
  416. void copyPackAsIs(PackOutputStream out, DfsReader ctx)
  417. throws IOException {
  418. // If the length hasn't been determined yet, pin to set it.
  419. if (length == -1) {
  420. ctx.pin(this, 0);
  421. ctx.unpin();
  422. }
  423. if (cache.shouldCopyThroughCache(length))
  424. copyPackThroughCache(out, ctx);
  425. else
  426. copyPackBypassCache(out, ctx);
  427. }
  428. private void copyPackThroughCache(PackOutputStream out, DfsReader ctx)
  429. throws IOException {
  430. long position = 12;
  431. long remaining = length - (12 + 20);
  432. while (0 < remaining) {
  433. DfsBlock b = cache.getOrLoad(this, position, ctx);
  434. int ptr = (int) (position - b.start);
  435. int n = (int) Math.min(b.size() - ptr, remaining);
  436. b.write(out, position, n);
  437. position += n;
  438. remaining -= n;
  439. }
  440. }
  441. private long copyPackBypassCache(PackOutputStream out, DfsReader ctx)
  442. throws IOException {
  443. try (ReadableChannel rc = ctx.db.openFile(packDesc, PACK)) {
  444. ByteBuffer buf = newCopyBuffer(out, rc);
  445. if (ctx.getOptions().getStreamPackBufferSize() > 0)
  446. rc.setReadAheadBytes(ctx.getOptions().getStreamPackBufferSize());
  447. long position = 12;
  448. long remaining = length - (12 + 20);
  449. while (0 < remaining) {
  450. DfsBlock b = cache.get(key, alignToBlock(position));
  451. if (b != null) {
  452. int ptr = (int) (position - b.start);
  453. int n = (int) Math.min(b.size() - ptr, remaining);
  454. b.write(out, position, n);
  455. position += n;
  456. remaining -= n;
  457. rc.position(position);
  458. continue;
  459. }
  460. buf.position(0);
  461. int n = read(rc, buf);
  462. if (n <= 0)
  463. throw packfileIsTruncated();
  464. else if (n > remaining)
  465. n = (int) remaining;
  466. out.write(buf.array(), 0, n);
  467. position += n;
  468. remaining -= n;
  469. }
  470. return position;
  471. }
  472. }
  473. private ByteBuffer newCopyBuffer(PackOutputStream out, ReadableChannel rc) {
  474. int bs = blockSize(rc);
  475. byte[] copyBuf = out.getCopyBuffer();
  476. if (bs > copyBuf.length)
  477. copyBuf = new byte[bs];
  478. return ByteBuffer.wrap(copyBuf, 0, bs);
  479. }
  480. void copyAsIs(PackOutputStream out, DfsObjectToPack src,
  481. boolean validate, DfsReader ctx) throws IOException,
  482. StoredObjectRepresentationNotAvailableException {
  483. final CRC32 crc1 = validate ? new CRC32() : null;
  484. final CRC32 crc2 = validate ? new CRC32() : null;
  485. final byte[] buf = out.getCopyBuffer();
  486. // Rip apart the header so we can discover the size.
  487. //
  488. try {
  489. readFully(src.offset, buf, 0, 20, ctx);
  490. } catch (IOException ioError) {
  491. StoredObjectRepresentationNotAvailableException gone;
  492. gone = new StoredObjectRepresentationNotAvailableException(src);
  493. gone.initCause(ioError);
  494. throw gone;
  495. }
  496. int c = buf[0] & 0xff;
  497. final int typeCode = (c >> 4) & 7;
  498. long inflatedLength = c & 15;
  499. int shift = 4;
  500. int headerCnt = 1;
  501. while ((c & 0x80) != 0) {
  502. c = buf[headerCnt++] & 0xff;
  503. inflatedLength += ((long) (c & 0x7f)) << shift;
  504. shift += 7;
  505. }
  506. if (typeCode == Constants.OBJ_OFS_DELTA) {
  507. do {
  508. c = buf[headerCnt++] & 0xff;
  509. } while ((c & 128) != 0);
  510. if (validate) {
  511. assert(crc1 != null && crc2 != null);
  512. crc1.update(buf, 0, headerCnt);
  513. crc2.update(buf, 0, headerCnt);
  514. }
  515. } else if (typeCode == Constants.OBJ_REF_DELTA) {
  516. if (validate) {
  517. assert(crc1 != null && crc2 != null);
  518. crc1.update(buf, 0, headerCnt);
  519. crc2.update(buf, 0, headerCnt);
  520. }
  521. readFully(src.offset + headerCnt, buf, 0, 20, ctx);
  522. if (validate) {
  523. assert(crc1 != null && crc2 != null);
  524. crc1.update(buf, 0, 20);
  525. crc2.update(buf, 0, 20);
  526. }
  527. headerCnt += 20;
  528. } else if (validate) {
  529. assert(crc1 != null && crc2 != null);
  530. crc1.update(buf, 0, headerCnt);
  531. crc2.update(buf, 0, headerCnt);
  532. }
  533. final long dataOffset = src.offset + headerCnt;
  534. final long dataLength = src.length;
  535. final long expectedCRC;
  536. final DfsBlock quickCopy;
  537. // Verify the object isn't corrupt before sending. If it is,
  538. // we report it missing instead.
  539. //
  540. try {
  541. quickCopy = ctx.quickCopy(this, dataOffset, dataLength);
  542. if (validate && idx(ctx).hasCRC32Support()) {
  543. assert(crc1 != null);
  544. // Index has the CRC32 code cached, validate the object.
  545. //
  546. expectedCRC = idx(ctx).findCRC32(src);
  547. if (quickCopy != null) {
  548. quickCopy.crc32(crc1, dataOffset, (int) dataLength);
  549. } else {
  550. long pos = dataOffset;
  551. long cnt = dataLength;
  552. while (cnt > 0) {
  553. final int n = (int) Math.min(cnt, buf.length);
  554. readFully(pos, buf, 0, n, ctx);
  555. crc1.update(buf, 0, n);
  556. pos += n;
  557. cnt -= n;
  558. }
  559. }
  560. if (crc1.getValue() != expectedCRC) {
  561. setCorrupt(src.offset);
  562. throw new CorruptObjectException(MessageFormat.format(
  563. JGitText.get().objectAtHasBadZlibStream,
  564. Long.valueOf(src.offset), getPackName()));
  565. }
  566. } else if (validate) {
  567. assert(crc1 != null);
  568. // We don't have a CRC32 code in the index, so compute it
  569. // now while inflating the raw data to get zlib to tell us
  570. // whether or not the data is safe.
  571. //
  572. Inflater inf = ctx.inflater();
  573. byte[] tmp = new byte[1024];
  574. if (quickCopy != null) {
  575. quickCopy.check(inf, tmp, dataOffset, (int) dataLength);
  576. } else {
  577. long pos = dataOffset;
  578. long cnt = dataLength;
  579. while (cnt > 0) {
  580. final int n = (int) Math.min(cnt, buf.length);
  581. readFully(pos, buf, 0, n, ctx);
  582. crc1.update(buf, 0, n);
  583. inf.setInput(buf, 0, n);
  584. while (inf.inflate(tmp, 0, tmp.length) > 0)
  585. continue;
  586. pos += n;
  587. cnt -= n;
  588. }
  589. }
  590. if (!inf.finished() || inf.getBytesRead() != dataLength) {
  591. setCorrupt(src.offset);
  592. throw new EOFException(MessageFormat.format(
  593. JGitText.get().shortCompressedStreamAt,
  594. Long.valueOf(src.offset)));
  595. }
  596. expectedCRC = crc1.getValue();
  597. } else {
  598. expectedCRC = -1;
  599. }
  600. } catch (DataFormatException dataFormat) {
  601. setCorrupt(src.offset);
  602. CorruptObjectException corruptObject = new CorruptObjectException(
  603. MessageFormat.format(
  604. JGitText.get().objectAtHasBadZlibStream,
  605. Long.valueOf(src.offset), getPackName()));
  606. corruptObject.initCause(dataFormat);
  607. StoredObjectRepresentationNotAvailableException gone;
  608. gone = new StoredObjectRepresentationNotAvailableException(src);
  609. gone.initCause(corruptObject);
  610. throw gone;
  611. } catch (IOException ioError) {
  612. StoredObjectRepresentationNotAvailableException gone;
  613. gone = new StoredObjectRepresentationNotAvailableException(src);
  614. gone.initCause(ioError);
  615. throw gone;
  616. }
  617. if (quickCopy != null) {
  618. // The entire object fits into a single byte array window slice,
  619. // and we have it pinned. Write this out without copying.
  620. //
  621. out.writeHeader(src, inflatedLength);
  622. quickCopy.write(out, dataOffset, (int) dataLength);
  623. } else if (dataLength <= buf.length) {
  624. // Tiny optimization: Lots of objects are very small deltas or
  625. // deflated commits that are likely to fit in the copy buffer.
  626. //
  627. if (!validate) {
  628. long pos = dataOffset;
  629. long cnt = dataLength;
  630. while (cnt > 0) {
  631. final int n = (int) Math.min(cnt, buf.length);
  632. readFully(pos, buf, 0, n, ctx);
  633. pos += n;
  634. cnt -= n;
  635. }
  636. }
  637. out.writeHeader(src, inflatedLength);
  638. out.write(buf, 0, (int) dataLength);
  639. } else {
  640. // Now we are committed to sending the object. As we spool it out,
  641. // check its CRC32 code to make sure there wasn't corruption between
  642. // the verification we did above, and us actually outputting it.
  643. //
  644. out.writeHeader(src, inflatedLength);
  645. long pos = dataOffset;
  646. long cnt = dataLength;
  647. while (cnt > 0) {
  648. final int n = (int) Math.min(cnt, buf.length);
  649. readFully(pos, buf, 0, n, ctx);
  650. if (validate) {
  651. assert(crc2 != null);
  652. crc2.update(buf, 0, n);
  653. }
  654. out.write(buf, 0, n);
  655. pos += n;
  656. cnt -= n;
  657. }
  658. if (validate) {
  659. assert(crc2 != null);
  660. if (crc2.getValue() != expectedCRC) {
  661. throw new CorruptObjectException(MessageFormat.format(
  662. JGitText.get().objectAtHasBadZlibStream,
  663. Long.valueOf(src.offset), getPackName()));
  664. }
  665. }
  666. }
  667. }
  668. boolean invalid() {
  669. return invalid;
  670. }
  671. void setInvalid() {
  672. invalid = true;
  673. }
  674. private IOException packfileIsTruncated() {
  675. invalid = true;
  676. return new IOException(MessageFormat.format(
  677. JGitText.get().packfileIsTruncated, getPackName()));
  678. }
  679. private void readFully(long position, byte[] dstbuf, int dstoff, int cnt,
  680. DfsReader ctx) throws IOException {
  681. if (ctx.copy(this, position, dstbuf, dstoff, cnt) != cnt)
  682. throw new EOFException();
  683. }
  684. long alignToBlock(long pos) {
  685. int size = blockSize;
  686. if (size == 0)
  687. size = cache.getBlockSize();
  688. return (pos / size) * size;
  689. }
  690. DfsBlock getOrLoadBlock(long pos, DfsReader ctx) throws IOException {
  691. return cache.getOrLoad(this, pos, ctx);
  692. }
  693. DfsBlock readOneBlock(long pos, DfsReader ctx)
  694. throws IOException {
  695. if (invalid)
  696. throw new PackInvalidException(getPackName());
  697. ReadableChannel rc = ctx.db.openFile(packDesc, PACK);
  698. try {
  699. int size = blockSize(rc);
  700. pos = (pos / size) * size;
  701. // If the size of the file is not yet known, try to discover it.
  702. // Channels may choose to return -1 to indicate they don't
  703. // know the length yet, in this case read up to the size unit
  704. // given by the caller, then recheck the length.
  705. long len = length;
  706. if (len < 0) {
  707. len = rc.size();
  708. if (0 <= len)
  709. length = len;
  710. }
  711. if (0 <= len && len < pos + size)
  712. size = (int) (len - pos);
  713. if (size <= 0)
  714. throw new EOFException(MessageFormat.format(
  715. DfsText.get().shortReadOfBlock, Long.valueOf(pos),
  716. getPackName(), Long.valueOf(0), Long.valueOf(0)));
  717. byte[] buf = new byte[size];
  718. rc.position(pos);
  719. int cnt = read(rc, ByteBuffer.wrap(buf, 0, size));
  720. if (cnt != size) {
  721. if (0 <= len) {
  722. throw new EOFException(MessageFormat.format(
  723. DfsText.get().shortReadOfBlock,
  724. Long.valueOf(pos),
  725. getPackName(),
  726. Integer.valueOf(size),
  727. Integer.valueOf(cnt)));
  728. }
  729. // Assume the entire thing was read in a single shot, compact
  730. // the buffer to only the space required.
  731. byte[] n = new byte[cnt];
  732. System.arraycopy(buf, 0, n, 0, n.length);
  733. buf = n;
  734. } else if (len < 0) {
  735. // With no length at the start of the read, the channel should
  736. // have the length available at the end.
  737. length = len = rc.size();
  738. }
  739. DfsBlock v = new DfsBlock(key, pos, buf);
  740. return v;
  741. } finally {
  742. rc.close();
  743. }
  744. }
  745. private int blockSize(ReadableChannel rc) {
  746. // If the block alignment is not yet known, discover it. Prefer the
  747. // larger size from either the cache or the file itself.
  748. int size = blockSize;
  749. if (size == 0) {
  750. size = rc.blockSize();
  751. if (size <= 0)
  752. size = cache.getBlockSize();
  753. else if (size < cache.getBlockSize())
  754. size = (cache.getBlockSize() / size) * size;
  755. blockSize = size;
  756. }
  757. return size;
  758. }
  759. private static int read(ReadableChannel rc, ByteBuffer buf)
  760. throws IOException {
  761. int n;
  762. do {
  763. n = rc.read(buf);
  764. } while (0 < n && buf.hasRemaining());
  765. return buf.position();
  766. }
  767. ObjectLoader load(DfsReader ctx, long pos)
  768. throws IOException {
  769. try {
  770. final byte[] ib = ctx.tempId;
  771. Delta delta = null;
  772. byte[] data = null;
  773. int type = Constants.OBJ_BAD;
  774. boolean cached = false;
  775. SEARCH: for (;;) {
  776. readFully(pos, ib, 0, 20, ctx);
  777. int c = ib[0] & 0xff;
  778. final int typeCode = (c >> 4) & 7;
  779. long sz = c & 15;
  780. int shift = 4;
  781. int p = 1;
  782. while ((c & 0x80) != 0) {
  783. c = ib[p++] & 0xff;
  784. sz += ((long) (c & 0x7f)) << shift;
  785. shift += 7;
  786. }
  787. switch (typeCode) {
  788. case Constants.OBJ_COMMIT:
  789. case Constants.OBJ_TREE:
  790. case Constants.OBJ_BLOB:
  791. case Constants.OBJ_TAG: {
  792. if (delta != null) {
  793. data = decompress(pos + p, (int) sz, ctx);
  794. type = typeCode;
  795. break SEARCH;
  796. }
  797. if (sz < ctx.getStreamFileThreshold()) {
  798. data = decompress(pos + p, (int) sz, ctx);
  799. if (data != null)
  800. return new ObjectLoader.SmallObject(typeCode, data);
  801. }
  802. return new LargePackedWholeObject(typeCode, sz, pos, p, this, ctx.db);
  803. }
  804. case Constants.OBJ_OFS_DELTA: {
  805. c = ib[p++] & 0xff;
  806. long base = c & 127;
  807. while ((c & 128) != 0) {
  808. base += 1;
  809. c = ib[p++] & 0xff;
  810. base <<= 7;
  811. base += (c & 127);
  812. }
  813. base = pos - base;
  814. delta = new Delta(delta, pos, (int) sz, p, base);
  815. if (sz != delta.deltaSize)
  816. break SEARCH;
  817. DeltaBaseCache.Entry e = ctx.getDeltaBaseCache().get(key, base);
  818. if (e != null) {
  819. type = e.type;
  820. data = e.data;
  821. cached = true;
  822. break SEARCH;
  823. }
  824. pos = base;
  825. continue SEARCH;
  826. }
  827. case Constants.OBJ_REF_DELTA: {
  828. readFully(pos + p, ib, 0, 20, ctx);
  829. long base = findDeltaBase(ctx, ObjectId.fromRaw(ib));
  830. delta = new Delta(delta, pos, (int) sz, p + 20, base);
  831. if (sz != delta.deltaSize)
  832. break SEARCH;
  833. DeltaBaseCache.Entry e = ctx.getDeltaBaseCache().get(key, base);
  834. if (e != null) {
  835. type = e.type;
  836. data = e.data;
  837. cached = true;
  838. break SEARCH;
  839. }
  840. pos = base;
  841. continue SEARCH;
  842. }
  843. default:
  844. throw new IOException(MessageFormat.format(
  845. JGitText.get().unknownObjectType, Integer.valueOf(typeCode)));
  846. }
  847. }
  848. // At this point there is at least one delta to apply to data.
  849. // (Whole objects with no deltas to apply return early above.)
  850. if (data == null)
  851. throw new LargeObjectException();
  852. assert(delta != null);
  853. do {
  854. // Cache only the base immediately before desired object.
  855. if (cached)
  856. cached = false;
  857. else if (delta.next == null)
  858. ctx.getDeltaBaseCache().put(key, delta.basePos, type, data);
  859. pos = delta.deltaPos;
  860. byte[] cmds = decompress(pos + delta.hdrLen, delta.deltaSize, ctx);
  861. if (cmds == null) {
  862. data = null; // Discard base in case of OutOfMemoryError
  863. throw new LargeObjectException();
  864. }
  865. final long sz = BinaryDelta.getResultSize(cmds);
  866. if (Integer.MAX_VALUE <= sz)
  867. throw new LargeObjectException.ExceedsByteArrayLimit();
  868. final byte[] result;
  869. try {
  870. result = new byte[(int) sz];
  871. } catch (OutOfMemoryError tooBig) {
  872. data = null; // Discard base in case of OutOfMemoryError
  873. cmds = null;
  874. throw new LargeObjectException.OutOfMemory(tooBig);
  875. }
  876. BinaryDelta.apply(data, cmds, result);
  877. data = result;
  878. delta = delta.next;
  879. } while (delta != null);
  880. return new ObjectLoader.SmallObject(type, data);
  881. } catch (DataFormatException dfe) {
  882. CorruptObjectException coe = new CorruptObjectException(
  883. MessageFormat.format(
  884. JGitText.get().objectAtHasBadZlibStream, Long.valueOf(pos),
  885. getPackName()));
  886. coe.initCause(dfe);
  887. throw coe;
  888. }
  889. }
  890. private long findDeltaBase(DfsReader ctx, ObjectId baseId)
  891. throws IOException, MissingObjectException {
  892. long ofs = idx(ctx).findOffset(baseId);
  893. if (ofs < 0)
  894. throw new MissingObjectException(baseId,
  895. JGitText.get().missingDeltaBase);
  896. return ofs;
  897. }
  898. private static class Delta {
  899. /** Child that applies onto this object. */
  900. final Delta next;
  901. /** Offset of the delta object. */
  902. final long deltaPos;
  903. /** Size of the inflated delta stream. */
  904. final int deltaSize;
  905. /** Total size of the delta's pack entry header (including base). */
  906. final int hdrLen;
  907. /** Offset of the base object this delta applies onto. */
  908. final long basePos;
  909. Delta(Delta next, long ofs, int sz, int hdrLen, long baseOffset) {
  910. this.next = next;
  911. this.deltaPos = ofs;
  912. this.deltaSize = sz;
  913. this.hdrLen = hdrLen;
  914. this.basePos = baseOffset;
  915. }
  916. }
  917. byte[] getDeltaHeader(DfsReader wc, long pos)
  918. throws IOException, DataFormatException {
  919. // The delta stream starts as two variable length integers. If we
  920. // assume they are 64 bits each, we need 16 bytes to encode them,
  921. // plus 2 extra bytes for the variable length overhead. So 18 is
  922. // the longest delta instruction header.
  923. //
  924. final byte[] hdr = new byte[32];
  925. wc.inflate(this, pos, hdr, true /* header only */);
  926. return hdr;
  927. }
  928. int getObjectType(DfsReader ctx, long pos) throws IOException {
  929. final byte[] ib = ctx.tempId;
  930. for (;;) {
  931. readFully(pos, ib, 0, 20, ctx);
  932. int c = ib[0] & 0xff;
  933. final int type = (c >> 4) & 7;
  934. switch (type) {
  935. case Constants.OBJ_COMMIT:
  936. case Constants.OBJ_TREE:
  937. case Constants.OBJ_BLOB:
  938. case Constants.OBJ_TAG:
  939. return type;
  940. case Constants.OBJ_OFS_DELTA: {
  941. int p = 1;
  942. while ((c & 0x80) != 0)
  943. c = ib[p++] & 0xff;
  944. c = ib[p++] & 0xff;
  945. long ofs = c & 127;
  946. while ((c & 128) != 0) {
  947. ofs += 1;
  948. c = ib[p++] & 0xff;
  949. ofs <<= 7;
  950. ofs += (c & 127);
  951. }
  952. pos = pos - ofs;
  953. continue;
  954. }
  955. case Constants.OBJ_REF_DELTA: {
  956. int p = 1;
  957. while ((c & 0x80) != 0)
  958. c = ib[p++] & 0xff;
  959. readFully(pos + p, ib, 0, 20, ctx);
  960. pos = findDeltaBase(ctx, ObjectId.fromRaw(ib));
  961. continue;
  962. }
  963. default:
  964. throw new IOException(MessageFormat.format(
  965. JGitText.get().unknownObjectType, Integer.valueOf(type)));
  966. }
  967. }
  968. }
  969. long getObjectSize(DfsReader ctx, AnyObjectId id) throws IOException {
  970. final long offset = idx(ctx).findOffset(id);
  971. return 0 < offset ? getObjectSize(ctx, offset) : -1;
  972. }
  973. long getObjectSize(DfsReader ctx, long pos)
  974. throws IOException {
  975. final byte[] ib = ctx.tempId;
  976. readFully(pos, ib, 0, 20, ctx);
  977. int c = ib[0] & 0xff;
  978. final int type = (c >> 4) & 7;
  979. long sz = c & 15;
  980. int shift = 4;
  981. int p = 1;
  982. while ((c & 0x80) != 0) {
  983. c = ib[p++] & 0xff;
  984. sz += ((long) (c & 0x7f)) << shift;
  985. shift += 7;
  986. }
  987. long deltaAt;
  988. switch (type) {
  989. case Constants.OBJ_COMMIT:
  990. case Constants.OBJ_TREE:
  991. case Constants.OBJ_BLOB:
  992. case Constants.OBJ_TAG:
  993. return sz;
  994. case Constants.OBJ_OFS_DELTA:
  995. c = ib[p++] & 0xff;
  996. while ((c & 128) != 0)
  997. c = ib[p++] & 0xff;
  998. deltaAt = pos + p;
  999. break;
  1000. case Constants.OBJ_REF_DELTA:
  1001. deltaAt = pos + p + 20;
  1002. break;
  1003. default:
  1004. throw new IOException(MessageFormat.format(
  1005. JGitText.get().unknownObjectType, Integer.valueOf(type)));
  1006. }
  1007. try {
  1008. return BinaryDelta.getResultSize(getDeltaHeader(ctx, deltaAt));
  1009. } catch (DataFormatException dfe) {
  1010. CorruptObjectException coe = new CorruptObjectException(
  1011. MessageFormat.format(
  1012. JGitText.get().objectAtHasBadZlibStream, Long.valueOf(pos),
  1013. getPackName()));
  1014. coe.initCause(dfe);
  1015. throw coe;
  1016. }
  1017. }
  1018. void representation(DfsObjectRepresentation r, final long pos,
  1019. DfsReader ctx, PackReverseIndex rev)
  1020. throws IOException {
  1021. r.offset = pos;
  1022. final byte[] ib = ctx.tempId;
  1023. readFully(pos, ib, 0, 20, ctx);
  1024. int c = ib[0] & 0xff;
  1025. int p = 1;
  1026. final int typeCode = (c >> 4) & 7;
  1027. while ((c & 0x80) != 0)
  1028. c = ib[p++] & 0xff;
  1029. long len = rev.findNextOffset(pos, length - 20) - pos;
  1030. switch (typeCode) {
  1031. case Constants.OBJ_COMMIT:
  1032. case Constants.OBJ_TREE:
  1033. case Constants.OBJ_BLOB:
  1034. case Constants.OBJ_TAG:
  1035. r.format = StoredObjectRepresentation.PACK_WHOLE;
  1036. r.baseId = null;
  1037. r.length = len - p;
  1038. return;
  1039. case Constants.OBJ_OFS_DELTA: {
  1040. c = ib[p++] & 0xff;
  1041. long ofs = c & 127;
  1042. while ((c & 128) != 0) {
  1043. ofs += 1;
  1044. c = ib[p++] & 0xff;
  1045. ofs <<= 7;
  1046. ofs += (c & 127);
  1047. }
  1048. r.format = StoredObjectRepresentation.PACK_DELTA;
  1049. r.baseId = rev.findObject(pos - ofs);
  1050. r.length = len - p;
  1051. return;
  1052. }
  1053. case Constants.OBJ_REF_DELTA: {
  1054. readFully(pos + p, ib, 0, 20, ctx);
  1055. r.format = StoredObjectRepresentation.PACK_DELTA;
  1056. r.baseId = ObjectId.fromRaw(ib);
  1057. r.length = len - p - 20;
  1058. return;
  1059. }
  1060. default:
  1061. throw new IOException(MessageFormat.format(
  1062. JGitText.get().unknownObjectType, Integer.valueOf(typeCode)));
  1063. }
  1064. }
  1065. private boolean isCorrupt(long offset) {
  1066. LongList list = corruptObjects;
  1067. if (list == null)
  1068. return false;
  1069. synchronized (list) {
  1070. return list.contains(offset);
  1071. }
  1072. }
  1073. private void setCorrupt(long offset) {
  1074. LongList list = corruptObjects;
  1075. if (list == null) {
  1076. synchronized (initLock) {
  1077. list = corruptObjects;
  1078. if (list == null) {
  1079. list = new LongList();
  1080. corruptObjects = list;
  1081. }
  1082. }
  1083. }
  1084. synchronized (list) {
  1085. list.add(offset);
  1086. }
  1087. }
  1088. }