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

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