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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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.lib;
  46. import java.io.EOFException;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.io.OutputStream;
  50. import java.io.RandomAccessFile;
  51. import java.nio.MappedByteBuffer;
  52. import java.nio.channels.FileChannel.MapMode;
  53. import java.text.MessageFormat;
  54. import java.util.Arrays;
  55. import java.util.Collections;
  56. import java.util.Comparator;
  57. import java.util.Iterator;
  58. import java.util.zip.CRC32;
  59. import java.util.zip.CheckedOutputStream;
  60. import java.util.zip.DataFormatException;
  61. import org.eclipse.jgit.JGitText;
  62. import org.eclipse.jgit.errors.CorruptObjectException;
  63. import org.eclipse.jgit.errors.PackInvalidException;
  64. import org.eclipse.jgit.errors.PackMismatchException;
  65. import org.eclipse.jgit.util.NB;
  66. import org.eclipse.jgit.util.RawParseUtils;
  67. /**
  68. * A Git version 2 pack file representation. A pack file contains Git objects in
  69. * delta packed format yielding high compression of lots of object where some
  70. * objects are similar.
  71. */
  72. public class PackFile implements Iterable<PackIndex.MutableEntry> {
  73. /** Sorts PackFiles to be most recently created to least recently created. */
  74. public static Comparator<PackFile> SORT = new Comparator<PackFile>() {
  75. public int compare(final PackFile a, final PackFile b) {
  76. return b.packLastModified - a.packLastModified;
  77. }
  78. };
  79. private final File idxFile;
  80. private final File packFile;
  81. final int hash;
  82. private RandomAccessFile fd;
  83. /** Serializes reads performed against {@link #fd}. */
  84. private final Object readLock = new Object();
  85. long length;
  86. private int activeWindows;
  87. private int activeCopyRawData;
  88. private int packLastModified;
  89. private volatile boolean invalid;
  90. private byte[] packChecksum;
  91. private PackIndex loadedIdx;
  92. private PackReverseIndex reverseIdx;
  93. /**
  94. * Construct a reader for an existing, pre-indexed packfile.
  95. *
  96. * @param idxFile
  97. * path of the <code>.idx</code> file listing the contents.
  98. * @param packFile
  99. * path of the <code>.pack</code> file holding the data.
  100. */
  101. public PackFile(final File idxFile, final File packFile) {
  102. this.idxFile = idxFile;
  103. this.packFile = packFile;
  104. this.packLastModified = (int) (packFile.lastModified() >> 10);
  105. // Multiply by 31 here so we can more directly combine with another
  106. // value in WindowCache.hash(), without doing the multiply there.
  107. //
  108. hash = System.identityHashCode(this) * 31;
  109. length = Long.MAX_VALUE;
  110. }
  111. private synchronized PackIndex idx() throws IOException {
  112. if (loadedIdx == null) {
  113. if (invalid)
  114. throw new PackInvalidException(packFile);
  115. try {
  116. final PackIndex idx = PackIndex.open(idxFile);
  117. if (packChecksum == null)
  118. packChecksum = idx.packChecksum;
  119. else if (!Arrays.equals(packChecksum, idx.packChecksum))
  120. throw new PackMismatchException(JGitText.get().packChecksumMismatch);
  121. loadedIdx = idx;
  122. } catch (IOException e) {
  123. invalid = true;
  124. throw e;
  125. }
  126. }
  127. return loadedIdx;
  128. }
  129. final PackedObjectLoader resolveBase(final WindowCursor curs, final long ofs)
  130. throws IOException {
  131. return reader(curs, ofs);
  132. }
  133. /** @return the File object which locates this pack on disk. */
  134. public File getPackFile() {
  135. return packFile;
  136. }
  137. /**
  138. * Determine if an object is contained within the pack file.
  139. * <p>
  140. * For performance reasons only the index file is searched; the main pack
  141. * content is ignored entirely.
  142. * </p>
  143. *
  144. * @param id
  145. * the object to look for. Must not be null.
  146. * @return true if the object is in this pack; false otherwise.
  147. * @throws IOException
  148. * the index file cannot be loaded into memory.
  149. */
  150. public boolean hasObject(final AnyObjectId id) throws IOException {
  151. return idx().hasObject(id);
  152. }
  153. /**
  154. * Get an object from this pack.
  155. *
  156. * @param curs
  157. * temporary working space associated with the calling thread.
  158. * @param id
  159. * the object to obtain from the pack. Must not be null.
  160. * @return the object loader for the requested object if it is contained in
  161. * this pack; null if the object was not found.
  162. * @throws IOException
  163. * the pack file or the index could not be read.
  164. */
  165. public PackedObjectLoader get(final WindowCursor curs, final AnyObjectId id)
  166. throws IOException {
  167. final long offset = idx().findOffset(id);
  168. return 0 < offset ? reader(curs, offset) : null;
  169. }
  170. /**
  171. * Close the resources utilized by this repository
  172. */
  173. public void close() {
  174. UnpackedObjectCache.purge(this);
  175. WindowCache.purge(this);
  176. synchronized (this) {
  177. loadedIdx = null;
  178. reverseIdx = null;
  179. }
  180. }
  181. /**
  182. * Provide iterator over entries in associated pack index, that should also
  183. * exist in this pack file. Objects returned by such iterator are mutable
  184. * during iteration.
  185. * <p>
  186. * Iterator returns objects in SHA-1 lexicographical order.
  187. * </p>
  188. *
  189. * @return iterator over entries of associated pack index
  190. *
  191. * @see PackIndex#iterator()
  192. */
  193. public Iterator<PackIndex.MutableEntry> iterator() {
  194. try {
  195. return idx().iterator();
  196. } catch (IOException e) {
  197. return Collections.<PackIndex.MutableEntry> emptyList().iterator();
  198. }
  199. }
  200. /**
  201. * Obtain the total number of objects available in this pack. This method
  202. * relies on pack index, giving number of effectively available objects.
  203. *
  204. * @return number of objects in index of this pack, likewise in this pack
  205. * @throws IOException
  206. * the index file cannot be loaded into memory.
  207. */
  208. long getObjectCount() throws IOException {
  209. return idx().getObjectCount();
  210. }
  211. /**
  212. * Search for object id with the specified start offset in associated pack
  213. * (reverse) index.
  214. *
  215. * @param offset
  216. * start offset of object to find
  217. * @return object id for this offset, or null if no object was found
  218. * @throws IOException
  219. * the index file cannot be loaded into memory.
  220. */
  221. ObjectId findObjectForOffset(final long offset) throws IOException {
  222. return getReverseIdx().findObject(offset);
  223. }
  224. final UnpackedObjectCache.Entry readCache(final long position) {
  225. return UnpackedObjectCache.get(this, position);
  226. }
  227. final void saveCache(final long position, final byte[] data, final int type) {
  228. UnpackedObjectCache.store(this, position, data, type);
  229. }
  230. final byte[] decompress(final long position, final int totalSize,
  231. final WindowCursor curs) throws DataFormatException, IOException {
  232. final byte[] dstbuf = new byte[totalSize];
  233. if (curs.inflate(this, position, dstbuf, 0) != totalSize)
  234. throw new EOFException(MessageFormat.format(JGitText.get().shortCompressedStreamAt, position));
  235. return dstbuf;
  236. }
  237. final void copyRawData(final PackedObjectLoader loader,
  238. final OutputStream out, final byte buf[], final WindowCursor curs)
  239. throws IOException {
  240. final long objectOffset = loader.objectOffset;
  241. final long dataOffset = objectOffset + loader.headerSize;
  242. final long sz = findEndOffset(objectOffset) - dataOffset;
  243. final PackIndex idx = idx();
  244. if (idx.hasCRC32Support()) {
  245. final CRC32 crc = new CRC32();
  246. int headerCnt = loader.headerSize;
  247. while (headerCnt > 0) {
  248. final int toRead = Math.min(headerCnt, buf.length);
  249. readFully(objectOffset, buf, 0, toRead, curs);
  250. crc.update(buf, 0, toRead);
  251. headerCnt -= toRead;
  252. }
  253. final CheckedOutputStream crcOut = new CheckedOutputStream(out, crc);
  254. copyToStream(dataOffset, buf, sz, crcOut, curs);
  255. final long computed = crc.getValue();
  256. final ObjectId id = findObjectForOffset(objectOffset);
  257. final long expected = idx.findCRC32(id);
  258. if (computed != expected)
  259. throw new CorruptObjectException(MessageFormat.format(
  260. JGitText.get().objectAtHasBadZlibStream, objectOffset, getPackFile()));
  261. } else {
  262. try {
  263. curs.inflateVerify(this, dataOffset);
  264. } catch (DataFormatException dfe) {
  265. final CorruptObjectException coe;
  266. coe = new CorruptObjectException(MessageFormat.format(
  267. JGitText.get().objectAtHasBadZlibStream, objectOffset, getPackFile()));
  268. coe.initCause(dfe);
  269. throw coe;
  270. }
  271. copyToStream(dataOffset, buf, sz, out, curs);
  272. }
  273. }
  274. boolean supportsFastCopyRawData() throws IOException {
  275. return idx().hasCRC32Support();
  276. }
  277. boolean invalid() {
  278. return invalid;
  279. }
  280. private void readFully(final long position, final byte[] dstbuf,
  281. int dstoff, final int cnt, final WindowCursor curs)
  282. throws IOException {
  283. if (curs.copy(this, position, dstbuf, dstoff, cnt) != cnt)
  284. throw new EOFException();
  285. }
  286. private void copyToStream(long position, final byte[] buf, long cnt,
  287. final OutputStream out, final WindowCursor curs)
  288. throws IOException, EOFException {
  289. while (cnt > 0) {
  290. final int toRead = (int) Math.min(cnt, buf.length);
  291. readFully(position, buf, 0, toRead, curs);
  292. position += toRead;
  293. cnt -= toRead;
  294. out.write(buf, 0, toRead);
  295. }
  296. }
  297. synchronized void beginCopyRawData() throws IOException {
  298. if (++activeCopyRawData == 1 && activeWindows == 0)
  299. doOpen();
  300. }
  301. synchronized void endCopyRawData() {
  302. if (--activeCopyRawData == 0 && activeWindows == 0)
  303. doClose();
  304. }
  305. synchronized boolean beginWindowCache() throws IOException {
  306. if (++activeWindows == 1) {
  307. if (activeCopyRawData == 0)
  308. doOpen();
  309. return true;
  310. }
  311. return false;
  312. }
  313. synchronized boolean endWindowCache() {
  314. final boolean r = --activeWindows == 0;
  315. if (r && activeCopyRawData == 0)
  316. doClose();
  317. return r;
  318. }
  319. private void doOpen() throws IOException {
  320. try {
  321. if (invalid)
  322. throw new PackInvalidException(packFile);
  323. synchronized (readLock) {
  324. fd = new RandomAccessFile(packFile, "r");
  325. length = fd.length();
  326. onOpenPack();
  327. }
  328. } catch (IOException ioe) {
  329. openFail();
  330. throw ioe;
  331. } catch (RuntimeException re) {
  332. openFail();
  333. throw re;
  334. } catch (Error re) {
  335. openFail();
  336. throw re;
  337. }
  338. }
  339. private void openFail() {
  340. activeWindows = 0;
  341. activeCopyRawData = 0;
  342. invalid = true;
  343. doClose();
  344. }
  345. private void doClose() {
  346. synchronized (readLock) {
  347. if (fd != null) {
  348. try {
  349. fd.close();
  350. } catch (IOException err) {
  351. // Ignore a close event. We had it open only for reading.
  352. // There should not be errors related to network buffers
  353. // not flushed, etc.
  354. }
  355. fd = null;
  356. }
  357. }
  358. }
  359. ByteArrayWindow read(final long pos, int size) throws IOException {
  360. synchronized (readLock) {
  361. if (length < pos + size)
  362. size = (int) (length - pos);
  363. final byte[] buf = new byte[size];
  364. fd.seek(pos);
  365. fd.readFully(buf, 0, size);
  366. return new ByteArrayWindow(this, pos, buf);
  367. }
  368. }
  369. ByteWindow mmap(final long pos, int size) throws IOException {
  370. synchronized (readLock) {
  371. if (length < pos + size)
  372. size = (int) (length - pos);
  373. MappedByteBuffer map;
  374. try {
  375. map = fd.getChannel().map(MapMode.READ_ONLY, pos, size);
  376. } catch (IOException ioe1) {
  377. // The most likely reason this failed is the JVM has run out
  378. // of virtual memory. We need to discard quickly, and try to
  379. // force the GC to finalize and release any existing mappings.
  380. //
  381. System.gc();
  382. System.runFinalization();
  383. map = fd.getChannel().map(MapMode.READ_ONLY, pos, size);
  384. }
  385. if (map.hasArray())
  386. return new ByteArrayWindow(this, pos, map.array());
  387. return new ByteBufferWindow(this, pos, map);
  388. }
  389. }
  390. private void onOpenPack() throws IOException {
  391. final PackIndex idx = idx();
  392. final byte[] buf = new byte[20];
  393. fd.seek(0);
  394. fd.readFully(buf, 0, 12);
  395. if (RawParseUtils.match(buf, 0, Constants.PACK_SIGNATURE) != 4)
  396. throw new IOException(JGitText.get().notAPACKFile);
  397. final long vers = NB.decodeUInt32(buf, 4);
  398. final long packCnt = NB.decodeUInt32(buf, 8);
  399. if (vers != 2 && vers != 3)
  400. throw new IOException(MessageFormat.format(JGitText.get().unsupportedPackVersion, vers));
  401. if (packCnt != idx.getObjectCount())
  402. throw new PackMismatchException(MessageFormat.format(
  403. JGitText.get().packObjectCountMismatch, packCnt, idx.getObjectCount(), getPackFile()));
  404. fd.seek(length - 20);
  405. fd.read(buf, 0, 20);
  406. if (!Arrays.equals(buf, packChecksum))
  407. throw new PackMismatchException(MessageFormat.format(
  408. JGitText.get().packObjectCountMismatch
  409. , ObjectId.fromRaw(buf).name()
  410. , ObjectId.fromRaw(idx.packChecksum).name()
  411. , getPackFile()));
  412. }
  413. private PackedObjectLoader reader(final WindowCursor curs,
  414. final long objOffset) throws IOException {
  415. int p = 0;
  416. final byte[] ib = curs.tempId;
  417. readFully(objOffset, ib, 0, 20, curs);
  418. int c = ib[p++] & 0xff;
  419. final int typeCode = (c >> 4) & 7;
  420. long dataSize = c & 15;
  421. int shift = 4;
  422. while ((c & 0x80) != 0) {
  423. c = ib[p++] & 0xff;
  424. dataSize += (c & 0x7f) << shift;
  425. shift += 7;
  426. }
  427. switch (typeCode) {
  428. case Constants.OBJ_COMMIT:
  429. case Constants.OBJ_TREE:
  430. case Constants.OBJ_BLOB:
  431. case Constants.OBJ_TAG:
  432. return new WholePackedObjectLoader(this, objOffset, p, typeCode,
  433. (int) dataSize);
  434. case Constants.OBJ_OFS_DELTA: {
  435. c = ib[p++] & 0xff;
  436. long ofs = c & 127;
  437. while ((c & 128) != 0) {
  438. ofs += 1;
  439. c = ib[p++] & 0xff;
  440. ofs <<= 7;
  441. ofs += (c & 127);
  442. }
  443. return new DeltaOfsPackedObjectLoader(this, objOffset, p,
  444. (int) dataSize, objOffset - ofs);
  445. }
  446. case Constants.OBJ_REF_DELTA: {
  447. readFully(objOffset + p, ib, 0, 20, curs);
  448. return new DeltaRefPackedObjectLoader(this, objOffset, p + 20,
  449. (int) dataSize, ObjectId.fromRaw(ib));
  450. }
  451. default:
  452. throw new IOException(MessageFormat.format(JGitText.get().unknownObjectType, typeCode));
  453. }
  454. }
  455. private long findEndOffset(final long startOffset)
  456. throws IOException, CorruptObjectException {
  457. final long maxOffset = length - 20;
  458. return getReverseIdx().findNextOffset(startOffset, maxOffset);
  459. }
  460. private synchronized PackReverseIndex getReverseIdx() throws IOException {
  461. if (reverseIdx == null)
  462. reverseIdx = new PackReverseIndex(idx());
  463. return reverseIdx;
  464. }
  465. }