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.

PackFile.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Copyright (C) 2008-2009, 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.storage.file;
  46. import java.io.EOFException;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.io.RandomAccessFile;
  50. import java.nio.MappedByteBuffer;
  51. import java.nio.channels.FileChannel.MapMode;
  52. import java.text.MessageFormat;
  53. import java.util.Arrays;
  54. import java.util.Collections;
  55. import java.util.Comparator;
  56. import java.util.Iterator;
  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.JGitText;
  62. import org.eclipse.jgit.errors.CorruptObjectException;
  63. import org.eclipse.jgit.errors.LargeObjectException;
  64. import org.eclipse.jgit.errors.MissingObjectException;
  65. import org.eclipse.jgit.errors.PackInvalidException;
  66. import org.eclipse.jgit.errors.PackMismatchException;
  67. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  68. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  69. import org.eclipse.jgit.lib.AnyObjectId;
  70. import org.eclipse.jgit.lib.Constants;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectLoader;
  73. import org.eclipse.jgit.storage.pack.BinaryDelta;
  74. import org.eclipse.jgit.storage.pack.ObjectToPack;
  75. import org.eclipse.jgit.storage.pack.PackOutputStream;
  76. import org.eclipse.jgit.util.LongList;
  77. import org.eclipse.jgit.util.NB;
  78. import org.eclipse.jgit.util.RawParseUtils;
  79. /**
  80. * A Git version 2 pack file representation. A pack file contains Git objects in
  81. * delta packed format yielding high compression of lots of object where some
  82. * objects are similar.
  83. */
  84. public class PackFile implements Iterable<PackIndex.MutableEntry> {
  85. /** Sorts PackFiles to be most recently created to least recently created. */
  86. public static final Comparator<PackFile> SORT = new Comparator<PackFile>() {
  87. public int compare(final PackFile a, final PackFile b) {
  88. return b.packLastModified - a.packLastModified;
  89. }
  90. };
  91. private final File idxFile;
  92. private final File packFile;
  93. final int hash;
  94. private RandomAccessFile fd;
  95. /** Serializes reads performed against {@link #fd}. */
  96. private final Object readLock = new Object();
  97. long length;
  98. private int activeWindows;
  99. private int activeCopyRawData;
  100. private int packLastModified;
  101. private volatile boolean invalid;
  102. private byte[] packChecksum;
  103. private PackIndex loadedIdx;
  104. private PackReverseIndex reverseIdx;
  105. /**
  106. * Objects we have tried to read, and discovered to be corrupt.
  107. * <p>
  108. * The list is allocated after the first corruption is found, and filled in
  109. * as more entries are discovered. Typically this list is never used, as
  110. * pack files do not usually contain corrupt objects.
  111. */
  112. private volatile LongList corruptObjects;
  113. /**
  114. * Construct a reader for an existing, pre-indexed packfile.
  115. *
  116. * @param idxFile
  117. * path of the <code>.idx</code> file listing the contents.
  118. * @param packFile
  119. * path of the <code>.pack</code> file holding the data.
  120. */
  121. public PackFile(final File idxFile, final File packFile) {
  122. this.idxFile = idxFile;
  123. this.packFile = packFile;
  124. this.packLastModified = (int) (packFile.lastModified() >> 10);
  125. // Multiply by 31 here so we can more directly combine with another
  126. // value in WindowCache.hash(), without doing the multiply there.
  127. //
  128. hash = System.identityHashCode(this) * 31;
  129. length = Long.MAX_VALUE;
  130. }
  131. private synchronized PackIndex idx() throws IOException {
  132. if (loadedIdx == null) {
  133. if (invalid)
  134. throw new PackInvalidException(packFile);
  135. try {
  136. final PackIndex idx = PackIndex.open(idxFile);
  137. if (packChecksum == null)
  138. packChecksum = idx.packChecksum;
  139. else if (!Arrays.equals(packChecksum, idx.packChecksum))
  140. throw new PackMismatchException(JGitText.get().packChecksumMismatch);
  141. loadedIdx = idx;
  142. } catch (IOException e) {
  143. invalid = true;
  144. throw e;
  145. }
  146. }
  147. return loadedIdx;
  148. }
  149. /** @return the File object which locates this pack on disk. */
  150. public File getPackFile() {
  151. return packFile;
  152. }
  153. /**
  154. * Determine if an object is contained within the pack file.
  155. * <p>
  156. * For performance reasons only the index file is searched; the main pack
  157. * content is ignored entirely.
  158. * </p>
  159. *
  160. * @param id
  161. * the object to look for. Must not be null.
  162. * @return true if the object is in this pack; false otherwise.
  163. * @throws IOException
  164. * the index file cannot be loaded into memory.
  165. */
  166. public boolean hasObject(final AnyObjectId id) throws IOException {
  167. final long offset = idx().findOffset(id);
  168. return 0 < offset && !isCorrupt(offset);
  169. }
  170. /**
  171. * Get an object from this pack.
  172. *
  173. * @param curs
  174. * temporary working space associated with the calling thread.
  175. * @param id
  176. * the object to obtain from the pack. Must not be null.
  177. * @return the object loader for the requested object if it is contained in
  178. * this pack; null if the object was not found.
  179. * @throws IOException
  180. * the pack file or the index could not be read.
  181. */
  182. ObjectLoader get(final WindowCursor curs, final AnyObjectId id)
  183. throws IOException {
  184. final long offset = idx().findOffset(id);
  185. return 0 < offset && !isCorrupt(offset) ? load(curs, offset) : null;
  186. }
  187. void resolve(Set<ObjectId> matches, AbbreviatedObjectId id, int matchLimit)
  188. throws IOException {
  189. idx().resolve(matches, id, matchLimit);
  190. }
  191. /**
  192. * Close the resources utilized by this repository
  193. */
  194. public void close() {
  195. DeltaBaseCache.purge(this);
  196. WindowCache.purge(this);
  197. synchronized (this) {
  198. loadedIdx = null;
  199. reverseIdx = null;
  200. }
  201. }
  202. /**
  203. * Provide iterator over entries in associated pack index, that should also
  204. * exist in this pack file. Objects returned by such iterator are mutable
  205. * during iteration.
  206. * <p>
  207. * Iterator returns objects in SHA-1 lexicographical order.
  208. * </p>
  209. *
  210. * @return iterator over entries of associated pack index
  211. *
  212. * @see PackIndex#iterator()
  213. */
  214. public Iterator<PackIndex.MutableEntry> iterator() {
  215. try {
  216. return idx().iterator();
  217. } catch (IOException e) {
  218. return Collections.<PackIndex.MutableEntry> emptyList().iterator();
  219. }
  220. }
  221. /**
  222. * Obtain the total number of objects available in this pack. This method
  223. * relies on pack index, giving number of effectively available objects.
  224. *
  225. * @return number of objects in index of this pack, likewise in this pack
  226. * @throws IOException
  227. * the index file cannot be loaded into memory.
  228. */
  229. long getObjectCount() throws IOException {
  230. return idx().getObjectCount();
  231. }
  232. /**
  233. * Search for object id with the specified start offset in associated pack
  234. * (reverse) index.
  235. *
  236. * @param offset
  237. * start offset of object to find
  238. * @return object id for this offset, or null if no object was found
  239. * @throws IOException
  240. * the index file cannot be loaded into memory.
  241. */
  242. ObjectId findObjectForOffset(final long offset) throws IOException {
  243. return getReverseIdx().findObject(offset);
  244. }
  245. private final void decompress(final long position, final WindowCursor curs,
  246. final byte[] dstbuf, final int dstoff, final int dstsz)
  247. throws IOException, DataFormatException {
  248. if (curs.inflate(this, position, dstbuf, dstoff) != dstsz)
  249. throw new EOFException(MessageFormat.format(JGitText.get().shortCompressedStreamAt, position));
  250. }
  251. final void copyAsIs(PackOutputStream out, LocalObjectToPack src,
  252. WindowCursor curs) throws IOException,
  253. StoredObjectRepresentationNotAvailableException {
  254. beginCopyAsIs(src);
  255. try {
  256. copyAsIs2(out, src, curs);
  257. } finally {
  258. endCopyAsIs();
  259. }
  260. }
  261. private void copyAsIs2(PackOutputStream out, LocalObjectToPack src,
  262. WindowCursor curs) throws IOException,
  263. StoredObjectRepresentationNotAvailableException {
  264. final CRC32 crc1 = new CRC32();
  265. final CRC32 crc2 = new CRC32();
  266. final byte[] buf = out.getCopyBuffer();
  267. // Rip apart the header so we can discover the size.
  268. //
  269. readFully(src.offset, buf, 0, 20, curs);
  270. int c = buf[0] & 0xff;
  271. final int typeCode = (c >> 4) & 7;
  272. long inflatedLength = c & 15;
  273. int shift = 4;
  274. int headerCnt = 1;
  275. while ((c & 0x80) != 0) {
  276. c = buf[headerCnt++] & 0xff;
  277. inflatedLength += (c & 0x7f) << shift;
  278. shift += 7;
  279. }
  280. if (typeCode == Constants.OBJ_OFS_DELTA) {
  281. do {
  282. c = buf[headerCnt++] & 0xff;
  283. } while ((c & 128) != 0);
  284. crc1.update(buf, 0, headerCnt);
  285. crc2.update(buf, 0, headerCnt);
  286. } else if (typeCode == Constants.OBJ_REF_DELTA) {
  287. crc1.update(buf, 0, headerCnt);
  288. crc2.update(buf, 0, headerCnt);
  289. readFully(src.offset + headerCnt, buf, 0, 20, curs);
  290. crc1.update(buf, 0, 20);
  291. crc2.update(buf, 0, 20);
  292. headerCnt += 20;
  293. } else {
  294. crc1.update(buf, 0, headerCnt);
  295. crc2.update(buf, 0, headerCnt);
  296. }
  297. final long dataOffset = src.offset + headerCnt;
  298. final long dataLength = src.length;
  299. final long expectedCRC;
  300. final ByteArrayWindow quickCopy;
  301. // Verify the object isn't corrupt before sending. If it is,
  302. // we report it missing instead.
  303. //
  304. try {
  305. quickCopy = curs.quickCopy(this, dataOffset, dataLength);
  306. if (idx().hasCRC32Support()) {
  307. // Index has the CRC32 code cached, validate the object.
  308. //
  309. expectedCRC = idx().findCRC32(src);
  310. if (quickCopy != null) {
  311. quickCopy.crc32(crc1, dataOffset, (int) dataLength);
  312. } else {
  313. long pos = dataOffset;
  314. long cnt = dataLength;
  315. while (cnt > 0) {
  316. final int n = (int) Math.min(cnt, buf.length);
  317. readFully(pos, buf, 0, n, curs);
  318. crc1.update(buf, 0, n);
  319. pos += n;
  320. cnt -= n;
  321. }
  322. }
  323. if (crc1.getValue() != expectedCRC) {
  324. setCorrupt(src.offset);
  325. throw new CorruptObjectException(MessageFormat.format(
  326. JGitText.get().objectAtHasBadZlibStream,
  327. src.offset, getPackFile()));
  328. }
  329. } else {
  330. // We don't have a CRC32 code in the index, so compute it
  331. // now while inflating the raw data to get zlib to tell us
  332. // whether or not the data is safe.
  333. //
  334. Inflater inf = curs.inflater();
  335. byte[] tmp = new byte[1024];
  336. if (quickCopy != null) {
  337. quickCopy.check(inf, tmp, dataOffset, (int) dataLength);
  338. } else {
  339. long pos = dataOffset;
  340. long cnt = dataLength;
  341. while (cnt > 0) {
  342. final int n = (int) Math.min(cnt, buf.length);
  343. readFully(pos, buf, 0, n, curs);
  344. crc1.update(buf, 0, n);
  345. inf.setInput(buf, 0, n);
  346. while (inf.inflate(tmp, 0, tmp.length) > 0)
  347. continue;
  348. pos += n;
  349. cnt -= n;
  350. }
  351. }
  352. if (!inf.finished() || inf.getBytesRead() != dataLength) {
  353. setCorrupt(src.offset);
  354. throw new EOFException(MessageFormat.format(
  355. JGitText.get().shortCompressedStreamAt,
  356. src.offset));
  357. }
  358. expectedCRC = crc1.getValue();
  359. }
  360. } catch (DataFormatException dataFormat) {
  361. setCorrupt(src.offset);
  362. CorruptObjectException corruptObject = new CorruptObjectException(
  363. MessageFormat.format(
  364. JGitText.get().objectAtHasBadZlibStream,
  365. src.offset, getPackFile()));
  366. corruptObject.initCause(dataFormat);
  367. StoredObjectRepresentationNotAvailableException gone;
  368. gone = new StoredObjectRepresentationNotAvailableException(src);
  369. gone.initCause(corruptObject);
  370. throw gone;
  371. } catch (IOException ioError) {
  372. StoredObjectRepresentationNotAvailableException gone;
  373. gone = new StoredObjectRepresentationNotAvailableException(src);
  374. gone.initCause(ioError);
  375. throw gone;
  376. }
  377. if (quickCopy != null) {
  378. // The entire object fits into a single byte array window slice,
  379. // and we have it pinned. Write this out without copying.
  380. //
  381. out.writeHeader(src, inflatedLength);
  382. quickCopy.write(out, dataOffset, (int) dataLength);
  383. } else if (dataLength <= buf.length) {
  384. // Tiny optimization: Lots of objects are very small deltas or
  385. // deflated commits that are likely to fit in the copy buffer.
  386. //
  387. out.writeHeader(src, inflatedLength);
  388. out.write(buf, 0, (int) dataLength);
  389. } else {
  390. // Now we are committed to sending the object. As we spool it out,
  391. // check its CRC32 code to make sure there wasn't corruption between
  392. // the verification we did above, and us actually outputting it.
  393. //
  394. out.writeHeader(src, inflatedLength);
  395. long pos = dataOffset;
  396. long cnt = dataLength;
  397. while (cnt > 0) {
  398. final int n = (int) Math.min(cnt, buf.length);
  399. readFully(pos, buf, 0, n, curs);
  400. crc2.update(buf, 0, n);
  401. out.write(buf, 0, n);
  402. pos += n;
  403. cnt -= n;
  404. }
  405. if (crc2.getValue() != expectedCRC) {
  406. throw new CorruptObjectException(MessageFormat.format(JGitText
  407. .get().objectAtHasBadZlibStream, src.offset,
  408. getPackFile()));
  409. }
  410. }
  411. }
  412. boolean invalid() {
  413. return invalid;
  414. }
  415. private void readFully(final long position, final byte[] dstbuf,
  416. int dstoff, final int cnt, final WindowCursor curs)
  417. throws IOException {
  418. if (curs.copy(this, position, dstbuf, dstoff, cnt) != cnt)
  419. throw new EOFException();
  420. }
  421. private synchronized void beginCopyAsIs(ObjectToPack otp)
  422. throws StoredObjectRepresentationNotAvailableException {
  423. if (++activeCopyRawData == 1 && activeWindows == 0) {
  424. try {
  425. doOpen();
  426. } catch (IOException thisPackNotValid) {
  427. StoredObjectRepresentationNotAvailableException gone;
  428. gone = new StoredObjectRepresentationNotAvailableException(otp);
  429. gone.initCause(thisPackNotValid);
  430. throw gone;
  431. }
  432. }
  433. }
  434. private synchronized void endCopyAsIs() {
  435. if (--activeCopyRawData == 0 && activeWindows == 0)
  436. doClose();
  437. }
  438. synchronized boolean beginWindowCache() throws IOException {
  439. if (++activeWindows == 1) {
  440. if (activeCopyRawData == 0)
  441. doOpen();
  442. return true;
  443. }
  444. return false;
  445. }
  446. synchronized boolean endWindowCache() {
  447. final boolean r = --activeWindows == 0;
  448. if (r && activeCopyRawData == 0)
  449. doClose();
  450. return r;
  451. }
  452. private void doOpen() throws IOException {
  453. try {
  454. if (invalid)
  455. throw new PackInvalidException(packFile);
  456. synchronized (readLock) {
  457. fd = new RandomAccessFile(packFile, "r");
  458. length = fd.length();
  459. onOpenPack();
  460. }
  461. } catch (IOException ioe) {
  462. openFail();
  463. throw ioe;
  464. } catch (RuntimeException re) {
  465. openFail();
  466. throw re;
  467. } catch (Error re) {
  468. openFail();
  469. throw re;
  470. }
  471. }
  472. private void openFail() {
  473. activeWindows = 0;
  474. activeCopyRawData = 0;
  475. invalid = true;
  476. doClose();
  477. }
  478. private void doClose() {
  479. synchronized (readLock) {
  480. if (fd != null) {
  481. try {
  482. fd.close();
  483. } catch (IOException err) {
  484. // Ignore a close event. We had it open only for reading.
  485. // There should not be errors related to network buffers
  486. // not flushed, etc.
  487. }
  488. fd = null;
  489. }
  490. }
  491. }
  492. ByteArrayWindow read(final long pos, int size) throws IOException {
  493. synchronized (readLock) {
  494. if (length < pos + size)
  495. size = (int) (length - pos);
  496. final byte[] buf = new byte[size];
  497. fd.seek(pos);
  498. fd.readFully(buf, 0, size);
  499. return new ByteArrayWindow(this, pos, buf);
  500. }
  501. }
  502. ByteWindow mmap(final long pos, int size) throws IOException {
  503. synchronized (readLock) {
  504. if (length < pos + size)
  505. size = (int) (length - pos);
  506. MappedByteBuffer map;
  507. try {
  508. map = fd.getChannel().map(MapMode.READ_ONLY, pos, size);
  509. } catch (IOException ioe1) {
  510. // The most likely reason this failed is the JVM has run out
  511. // of virtual memory. We need to discard quickly, and try to
  512. // force the GC to finalize and release any existing mappings.
  513. //
  514. System.gc();
  515. System.runFinalization();
  516. map = fd.getChannel().map(MapMode.READ_ONLY, pos, size);
  517. }
  518. if (map.hasArray())
  519. return new ByteArrayWindow(this, pos, map.array());
  520. return new ByteBufferWindow(this, pos, map);
  521. }
  522. }
  523. private void onOpenPack() throws IOException {
  524. final PackIndex idx = idx();
  525. final byte[] buf = new byte[20];
  526. fd.seek(0);
  527. fd.readFully(buf, 0, 12);
  528. if (RawParseUtils.match(buf, 0, Constants.PACK_SIGNATURE) != 4)
  529. throw new IOException(JGitText.get().notAPACKFile);
  530. final long vers = NB.decodeUInt32(buf, 4);
  531. final long packCnt = NB.decodeUInt32(buf, 8);
  532. if (vers != 2 && vers != 3)
  533. throw new IOException(MessageFormat.format(JGitText.get().unsupportedPackVersion, vers));
  534. if (packCnt != idx.getObjectCount())
  535. throw new PackMismatchException(MessageFormat.format(
  536. JGitText.get().packObjectCountMismatch, packCnt, idx.getObjectCount(), getPackFile()));
  537. fd.seek(length - 20);
  538. fd.read(buf, 0, 20);
  539. if (!Arrays.equals(buf, packChecksum))
  540. throw new PackMismatchException(MessageFormat.format(
  541. JGitText.get().packObjectCountMismatch
  542. , ObjectId.fromRaw(buf).name()
  543. , ObjectId.fromRaw(idx.packChecksum).name()
  544. , getPackFile()));
  545. }
  546. ObjectLoader load(final WindowCursor curs, final long pos)
  547. throws IOException {
  548. final byte[] ib = curs.tempId;
  549. readFully(pos, ib, 0, 20, curs);
  550. int c = ib[0] & 0xff;
  551. final int type = (c >> 4) & 7;
  552. long sz = c & 15;
  553. int shift = 4;
  554. int p = 1;
  555. while ((c & 0x80) != 0) {
  556. c = ib[p++] & 0xff;
  557. sz += (c & 0x7f) << shift;
  558. shift += 7;
  559. }
  560. try {
  561. switch (type) {
  562. case Constants.OBJ_COMMIT:
  563. case Constants.OBJ_TREE:
  564. case Constants.OBJ_BLOB:
  565. case Constants.OBJ_TAG: {
  566. if (sz < curs.getStreamFileThreshold()) {
  567. byte[] data;
  568. try {
  569. data = new byte[(int) sz];
  570. } catch (OutOfMemoryError tooBig) {
  571. return largeWhole(curs, pos, type, sz, p);
  572. }
  573. decompress(pos + p, curs, data, 0, data.length);
  574. return new ObjectLoader.SmallObject(type, data);
  575. }
  576. return largeWhole(curs, pos, type, sz, p);
  577. }
  578. case Constants.OBJ_OFS_DELTA: {
  579. c = ib[p++] & 0xff;
  580. long ofs = c & 127;
  581. while ((c & 128) != 0) {
  582. ofs += 1;
  583. c = ib[p++] & 0xff;
  584. ofs <<= 7;
  585. ofs += (c & 127);
  586. }
  587. return loadDelta(pos, p, sz, pos - ofs, curs);
  588. }
  589. case Constants.OBJ_REF_DELTA: {
  590. readFully(pos + p, ib, 0, 20, curs);
  591. long ofs = findDeltaBase(ObjectId.fromRaw(ib));
  592. return loadDelta(pos, p + 20, sz, ofs, curs);
  593. }
  594. default:
  595. throw new IOException(MessageFormat.format(
  596. JGitText.get().unknownObjectType, type));
  597. }
  598. } catch (DataFormatException dfe) {
  599. CorruptObjectException coe = new CorruptObjectException(
  600. MessageFormat.format(
  601. JGitText.get().objectAtHasBadZlibStream, pos,
  602. getPackFile()));
  603. coe.initCause(dfe);
  604. throw coe;
  605. }
  606. }
  607. private long findDeltaBase(ObjectId baseId) throws IOException,
  608. MissingObjectException {
  609. long ofs = idx().findOffset(baseId);
  610. if (ofs < 0)
  611. throw new MissingObjectException(baseId,
  612. JGitText.get().missingDeltaBase);
  613. return ofs;
  614. }
  615. private ObjectLoader loadDelta(long posSelf, int hdrLen, long sz,
  616. long posBase, WindowCursor curs) throws IOException,
  617. DataFormatException {
  618. if (Integer.MAX_VALUE <= sz)
  619. return largeDelta(posSelf, hdrLen, posBase, curs);
  620. byte[] base;
  621. int type;
  622. DeltaBaseCache.Entry e = DeltaBaseCache.get(this, posBase);
  623. if (e != null) {
  624. base = e.data;
  625. type = e.type;
  626. } else {
  627. ObjectLoader p = load(curs, posBase);
  628. try {
  629. base = p.getCachedBytes(curs.getStreamFileThreshold());
  630. } catch (LargeObjectException tooBig) {
  631. return largeDelta(posSelf, hdrLen, posBase, curs);
  632. }
  633. type = p.getType();
  634. DeltaBaseCache.store(this, posBase, base, type);
  635. }
  636. final byte[] delta;
  637. try {
  638. delta = new byte[(int) sz];
  639. } catch (OutOfMemoryError tooBig) {
  640. return largeDelta(posSelf, hdrLen, posBase, curs);
  641. }
  642. decompress(posSelf + hdrLen, curs, delta, 0, delta.length);
  643. sz = BinaryDelta.getResultSize(delta);
  644. if (Integer.MAX_VALUE <= sz)
  645. return largeDelta(posSelf, hdrLen, posBase, curs);
  646. final byte[] result;
  647. try {
  648. result = new byte[(int) sz];
  649. } catch (OutOfMemoryError tooBig) {
  650. return largeDelta(posSelf, hdrLen, posBase, curs);
  651. }
  652. BinaryDelta.apply(base, delta, result);
  653. return new ObjectLoader.SmallObject(type, result);
  654. }
  655. private LargePackedWholeObject largeWhole(final WindowCursor curs,
  656. final long pos, final int type, long sz, int p) {
  657. return new LargePackedWholeObject(type, sz, pos, p, this, curs.db);
  658. }
  659. private LargePackedDeltaObject largeDelta(long posObj, int hdrLen,
  660. long posBase, WindowCursor wc) {
  661. return new LargePackedDeltaObject(posObj, posBase, hdrLen, this, wc.db);
  662. }
  663. byte[] getDeltaHeader(WindowCursor wc, long pos)
  664. throws IOException, DataFormatException {
  665. // The delta stream starts as two variable length integers. If we
  666. // assume they are 64 bits each, we need 16 bytes to encode them,
  667. // plus 2 extra bytes for the variable length overhead. So 18 is
  668. // the longest delta instruction header.
  669. //
  670. final byte[] hdr = new byte[18];
  671. wc.inflate(this, pos, hdr, 0);
  672. return hdr;
  673. }
  674. int getObjectType(final WindowCursor curs, long pos) throws IOException {
  675. final byte[] ib = curs.tempId;
  676. for (;;) {
  677. readFully(pos, ib, 0, 20, curs);
  678. int c = ib[0] & 0xff;
  679. final int type = (c >> 4) & 7;
  680. int shift = 4;
  681. int p = 1;
  682. while ((c & 0x80) != 0) {
  683. c = ib[p++] & 0xff;
  684. shift += 7;
  685. }
  686. switch (type) {
  687. case Constants.OBJ_COMMIT:
  688. case Constants.OBJ_TREE:
  689. case Constants.OBJ_BLOB:
  690. case Constants.OBJ_TAG:
  691. return type;
  692. case Constants.OBJ_OFS_DELTA: {
  693. c = ib[p++] & 0xff;
  694. long ofs = c & 127;
  695. while ((c & 128) != 0) {
  696. ofs += 1;
  697. c = ib[p++] & 0xff;
  698. ofs <<= 7;
  699. ofs += (c & 127);
  700. }
  701. pos = pos - ofs;
  702. continue;
  703. }
  704. case Constants.OBJ_REF_DELTA: {
  705. readFully(pos + p, ib, 0, 20, curs);
  706. pos = findDeltaBase(ObjectId.fromRaw(ib));
  707. continue;
  708. }
  709. default:
  710. throw new IOException(MessageFormat.format(
  711. JGitText.get().unknownObjectType, type));
  712. }
  713. }
  714. }
  715. long getObjectSize(final WindowCursor curs, final AnyObjectId id)
  716. throws IOException {
  717. final long offset = idx().findOffset(id);
  718. return 0 < offset ? getObjectSize(curs, offset) : -1;
  719. }
  720. long getObjectSize(final WindowCursor curs, final long pos)
  721. throws IOException {
  722. final byte[] ib = curs.tempId;
  723. readFully(pos, ib, 0, 20, curs);
  724. int c = ib[0] & 0xff;
  725. final int type = (c >> 4) & 7;
  726. long sz = c & 15;
  727. int shift = 4;
  728. int p = 1;
  729. while ((c & 0x80) != 0) {
  730. c = ib[p++] & 0xff;
  731. sz += (c & 0x7f) << shift;
  732. shift += 7;
  733. }
  734. long deltaAt;
  735. switch (type) {
  736. case Constants.OBJ_COMMIT:
  737. case Constants.OBJ_TREE:
  738. case Constants.OBJ_BLOB:
  739. case Constants.OBJ_TAG:
  740. return sz;
  741. case Constants.OBJ_OFS_DELTA:
  742. c = ib[p++] & 0xff;
  743. while ((c & 128) != 0)
  744. c = ib[p++] & 0xff;
  745. deltaAt = pos + p;
  746. break;
  747. case Constants.OBJ_REF_DELTA:
  748. deltaAt = pos + p + 20;
  749. break;
  750. default:
  751. throw new IOException(MessageFormat.format(
  752. JGitText.get().unknownObjectType, type));
  753. }
  754. try {
  755. return BinaryDelta.getResultSize(getDeltaHeader(curs, deltaAt));
  756. } catch (DataFormatException e) {
  757. throw new CorruptObjectException(MessageFormat.format(JGitText
  758. .get().objectAtHasBadZlibStream, pos, getPackFile()));
  759. }
  760. }
  761. LocalObjectRepresentation representation(final WindowCursor curs,
  762. final AnyObjectId objectId) throws IOException {
  763. final long pos = idx().findOffset(objectId);
  764. if (pos < 0)
  765. return null;
  766. final byte[] ib = curs.tempId;
  767. readFully(pos, ib, 0, 20, curs);
  768. int c = ib[0] & 0xff;
  769. int p = 1;
  770. final int typeCode = (c >> 4) & 7;
  771. while ((c & 0x80) != 0)
  772. c = ib[p++] & 0xff;
  773. long len = (findEndOffset(pos) - pos);
  774. switch (typeCode) {
  775. case Constants.OBJ_COMMIT:
  776. case Constants.OBJ_TREE:
  777. case Constants.OBJ_BLOB:
  778. case Constants.OBJ_TAG:
  779. return LocalObjectRepresentation.newWhole(this, pos, len - p);
  780. case Constants.OBJ_OFS_DELTA: {
  781. c = ib[p++] & 0xff;
  782. long ofs = c & 127;
  783. while ((c & 128) != 0) {
  784. ofs += 1;
  785. c = ib[p++] & 0xff;
  786. ofs <<= 7;
  787. ofs += (c & 127);
  788. }
  789. ofs = pos - ofs;
  790. return LocalObjectRepresentation.newDelta(this, pos, len - p, ofs);
  791. }
  792. case Constants.OBJ_REF_DELTA: {
  793. len -= p;
  794. len -= Constants.OBJECT_ID_LENGTH;
  795. readFully(pos + p, ib, 0, 20, curs);
  796. ObjectId id = ObjectId.fromRaw(ib);
  797. return LocalObjectRepresentation.newDelta(this, pos, len, id);
  798. }
  799. default:
  800. throw new IOException(MessageFormat.format(
  801. JGitText.get().unknownObjectType, typeCode));
  802. }
  803. }
  804. private long findEndOffset(final long startOffset)
  805. throws IOException, CorruptObjectException {
  806. final long maxOffset = length - 20;
  807. return getReverseIdx().findNextOffset(startOffset, maxOffset);
  808. }
  809. private synchronized PackReverseIndex getReverseIdx() throws IOException {
  810. if (reverseIdx == null)
  811. reverseIdx = new PackReverseIndex(idx());
  812. return reverseIdx;
  813. }
  814. private boolean isCorrupt(long offset) {
  815. LongList list = corruptObjects;
  816. if (list == null)
  817. return false;
  818. synchronized (list) {
  819. return list.contains(offset);
  820. }
  821. }
  822. private void setCorrupt(long offset) {
  823. LongList list = corruptObjects;
  824. if (list == null) {
  825. synchronized (readLock) {
  826. list = corruptObjects;
  827. if (list == null) {
  828. list = new LongList();
  829. corruptObjects = list;
  830. }
  831. }
  832. }
  833. synchronized (list) {
  834. list.add(offset);
  835. }
  836. }
  837. }