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.

IndexPack.java 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 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.transport;
  46. import java.io.EOFException;
  47. import java.io.File;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.RandomAccessFile;
  52. import java.security.MessageDigest;
  53. import java.text.MessageFormat;
  54. import java.util.ArrayList;
  55. import java.util.Arrays;
  56. import java.util.List;
  57. import java.util.zip.CRC32;
  58. import java.util.zip.DataFormatException;
  59. import java.util.zip.Deflater;
  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.MissingObjectException;
  64. import org.eclipse.jgit.lib.AnyObjectId;
  65. import org.eclipse.jgit.lib.Constants;
  66. import org.eclipse.jgit.lib.CoreConfig;
  67. import org.eclipse.jgit.lib.InflaterCache;
  68. import org.eclipse.jgit.lib.MutableObjectId;
  69. import org.eclipse.jgit.lib.ObjectChecker;
  70. import org.eclipse.jgit.lib.ObjectDatabase;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  73. import org.eclipse.jgit.lib.ObjectLoader;
  74. import org.eclipse.jgit.lib.ProgressMonitor;
  75. import org.eclipse.jgit.lib.Repository;
  76. import org.eclipse.jgit.lib.ObjectReader;
  77. import org.eclipse.jgit.storage.file.PackIndexWriter;
  78. import org.eclipse.jgit.storage.file.PackLock;
  79. import org.eclipse.jgit.storage.pack.BinaryDelta;
  80. import org.eclipse.jgit.util.NB;
  81. /** Indexes Git pack files for local use. */
  82. public class IndexPack {
  83. /** Progress message when reading raw data from the pack. */
  84. public static final String PROGRESS_DOWNLOAD = JGitText.get().receivingObjects;
  85. /** Progress message when computing names of delta compressed objects. */
  86. public static final String PROGRESS_RESOLVE_DELTA = JGitText.get().resolvingDeltas;
  87. /**
  88. * Size of the internal stream buffer.
  89. * <p>
  90. * If callers are going to be supplying IndexPack a BufferedInputStream they
  91. * should use this buffer size as the size of the buffer for that
  92. * BufferedInputStream, and any other its may be wrapping. This way the
  93. * buffers will cascade efficiently and only the IndexPack buffer will be
  94. * receiving the bulk of the data stream.
  95. */
  96. public static final int BUFFER_SIZE = 8192;
  97. /**
  98. * Create an index pack instance to load a new pack into a repository.
  99. * <p>
  100. * The received pack data and generated index will be saved to temporary
  101. * files within the repository's <code>objects</code> directory. To use the
  102. * data contained within them call {@link #renameAndOpenPack()} once the
  103. * indexing is complete.
  104. *
  105. * @param db
  106. * the repository that will receive the new pack.
  107. * @param is
  108. * stream to read the pack data from. If the stream is buffered
  109. * use {@link #BUFFER_SIZE} as the buffer size for the stream.
  110. * @return a new index pack instance.
  111. * @throws IOException
  112. * a temporary file could not be created.
  113. */
  114. public static IndexPack create(final Repository db, final InputStream is)
  115. throws IOException {
  116. final String suffix = ".pack";
  117. final File objdir = db.getObjectsDirectory();
  118. final File tmp = File.createTempFile("incoming_", suffix, objdir);
  119. final String n = tmp.getName();
  120. final File base;
  121. base = new File(objdir, n.substring(0, n.length() - suffix.length()));
  122. final IndexPack ip = new IndexPack(db, is, base);
  123. ip.setIndexVersion(db.getConfig().get(CoreConfig.KEY)
  124. .getPackIndexVersion());
  125. return ip;
  126. }
  127. private static enum Source {
  128. /** Data is read from the incoming stream. */
  129. INPUT,
  130. /**
  131. * Data is read from the spooled pack file.
  132. * <p>
  133. * During streaming, some (or all) data might be saved into the spooled
  134. * pack file so it can be randomly accessed later.
  135. */
  136. FILE;
  137. }
  138. private final Repository repo;
  139. /**
  140. * Object database used for loading existing objects
  141. */
  142. private final ObjectDatabase objectDatabase;
  143. private Inflater inflater;
  144. private final MessageDigest objectDigest;
  145. private final MutableObjectId tempObjectId;
  146. private InputStream in;
  147. private byte[] buf;
  148. private long bBase;
  149. private int bOffset;
  150. private int bAvail;
  151. private ObjectChecker objCheck;
  152. private boolean fixThin;
  153. private boolean keepEmpty;
  154. private boolean needBaseObjectIds;
  155. private int outputVersion;
  156. private final File dstPack;
  157. private final File dstIdx;
  158. private long objectCount;
  159. private PackedObjectInfo[] entries;
  160. /**
  161. * Every object contained within the incoming pack.
  162. * <p>
  163. * This is a subset of {@link #entries}, as thin packs can add additional
  164. * objects to {@code entries} by copying already existing objects from the
  165. * repository onto the end of the thin pack to make it self-contained.
  166. */
  167. private ObjectIdSubclassMap<ObjectId> newObjectIds;
  168. private int deltaCount;
  169. private int entryCount;
  170. private final CRC32 crc = new CRC32();
  171. private ObjectIdSubclassMap<DeltaChain> baseById;
  172. /**
  173. * Objects referenced by their name from deltas, that aren't in this pack.
  174. * <p>
  175. * This is the set of objects that were copied onto the end of this pack to
  176. * make it complete. These objects were not transmitted by the remote peer,
  177. * but instead were assumed to already exist in the local repository.
  178. */
  179. private ObjectIdSubclassMap<ObjectId> baseObjectIds;
  180. private LongMap<UnresolvedDelta> baseByPos;
  181. private byte[] skipBuffer;
  182. private MessageDigest packDigest;
  183. private RandomAccessFile packOut;
  184. private byte[] packcsum;
  185. /** If {@link #fixThin} this is the last byte of the original checksum. */
  186. private long originalEOF;
  187. private ObjectReader readCurs;
  188. /**
  189. * Create a new pack indexer utility.
  190. *
  191. * @param db
  192. * @param src
  193. * stream to read the pack data from. If the stream is buffered
  194. * use {@link #BUFFER_SIZE} as the buffer size for the stream.
  195. * @param dstBase
  196. * @throws IOException
  197. * the output packfile could not be created.
  198. */
  199. public IndexPack(final Repository db, final InputStream src,
  200. final File dstBase) throws IOException {
  201. repo = db;
  202. objectDatabase = db.getObjectDatabase().newCachedDatabase();
  203. in = src;
  204. inflater = InflaterCache.get();
  205. readCurs = objectDatabase.newReader();
  206. buf = new byte[BUFFER_SIZE];
  207. skipBuffer = new byte[512];
  208. objectDigest = Constants.newMessageDigest();
  209. tempObjectId = new MutableObjectId();
  210. packDigest = Constants.newMessageDigest();
  211. if (dstBase != null) {
  212. final File dir = dstBase.getParentFile();
  213. final String nam = dstBase.getName();
  214. dstPack = new File(dir, nam + ".pack");
  215. dstIdx = new File(dir, nam + ".idx");
  216. packOut = new RandomAccessFile(dstPack, "rw");
  217. packOut.setLength(0);
  218. } else {
  219. dstPack = null;
  220. dstIdx = null;
  221. }
  222. }
  223. /**
  224. * Set the pack index file format version this instance will create.
  225. *
  226. * @param version
  227. * the version to write. The special version 0 designates the
  228. * oldest (most compatible) format available for the objects.
  229. * @see PackIndexWriter
  230. */
  231. public void setIndexVersion(final int version) {
  232. outputVersion = version;
  233. }
  234. /**
  235. * Configure this index pack instance to make a thin pack complete.
  236. * <p>
  237. * Thin packs are sometimes used during network transfers to allow a delta
  238. * to be sent without a base object. Such packs are not permitted on disk.
  239. * They can be fixed by copying the base object onto the end of the pack.
  240. *
  241. * @param fix
  242. * true to enable fixing a thin pack.
  243. */
  244. public void setFixThin(final boolean fix) {
  245. fixThin = fix;
  246. }
  247. /**
  248. * Configure this index pack instance to keep an empty pack.
  249. * <p>
  250. * By default an empty pack (a pack with no objects) is not kept, as doing
  251. * so is completely pointless. With no objects in the pack there is no data
  252. * stored by it, so the pack is unnecessary.
  253. *
  254. * @param empty true to enable keeping an empty pack.
  255. */
  256. public void setKeepEmpty(final boolean empty) {
  257. keepEmpty = empty;
  258. }
  259. /**
  260. * Configure this index pack instance to keep track of new objects.
  261. * <p>
  262. * By default an index pack doesn't save the new objects that were created
  263. * when it was instantiated. Setting this flag to {@code true} allows the
  264. * caller to use {@link #getNewObjectIds()} to retrieve that list.
  265. *
  266. * @param b {@code true} to enable keeping track of new objects.
  267. */
  268. public void setNeedNewObjectIds(boolean b) {
  269. if (b)
  270. newObjectIds = new ObjectIdSubclassMap<ObjectId>();
  271. else
  272. newObjectIds = null;
  273. }
  274. private boolean needNewObjectIds() {
  275. return newObjectIds != null;
  276. }
  277. /**
  278. * Configure this index pack instance to keep track of the objects assumed
  279. * for delta bases.
  280. * <p>
  281. * By default an index pack doesn't save the objects that were used as delta
  282. * bases. Setting this flag to {@code true} will allow the caller to
  283. * use {@link #getBaseObjectIds()} to retrieve that list.
  284. *
  285. * @param b {@code true} to enable keeping track of delta bases.
  286. */
  287. public void setNeedBaseObjectIds(boolean b) {
  288. this.needBaseObjectIds = b;
  289. }
  290. /** @return the new objects that were sent by the user */
  291. public ObjectIdSubclassMap<ObjectId> getNewObjectIds() {
  292. if (newObjectIds != null)
  293. return newObjectIds;
  294. return new ObjectIdSubclassMap<ObjectId>();
  295. }
  296. /** @return set of objects the incoming pack assumed for delta purposes */
  297. public ObjectIdSubclassMap<ObjectId> getBaseObjectIds() {
  298. if (baseObjectIds != null)
  299. return baseObjectIds;
  300. return new ObjectIdSubclassMap<ObjectId>();
  301. }
  302. /**
  303. * Configure the checker used to validate received objects.
  304. * <p>
  305. * Usually object checking isn't necessary, as Git implementations only
  306. * create valid objects in pack files. However, additional checking may be
  307. * useful if processing data from an untrusted source.
  308. *
  309. * @param oc
  310. * the checker instance; null to disable object checking.
  311. */
  312. public void setObjectChecker(final ObjectChecker oc) {
  313. objCheck = oc;
  314. }
  315. /**
  316. * Configure the checker used to validate received objects.
  317. * <p>
  318. * Usually object checking isn't necessary, as Git implementations only
  319. * create valid objects in pack files. However, additional checking may be
  320. * useful if processing data from an untrusted source.
  321. * <p>
  322. * This is shorthand for:
  323. *
  324. * <pre>
  325. * setObjectChecker(on ? new ObjectChecker() : null);
  326. * </pre>
  327. *
  328. * @param on
  329. * true to enable the default checker; false to disable it.
  330. */
  331. public void setObjectChecking(final boolean on) {
  332. setObjectChecker(on ? new ObjectChecker() : null);
  333. }
  334. /**
  335. * Consume data from the input stream until the packfile is indexed.
  336. *
  337. * @param progress
  338. * progress feedback
  339. *
  340. * @throws IOException
  341. */
  342. public void index(final ProgressMonitor progress) throws IOException {
  343. progress.start(2 /* tasks */);
  344. try {
  345. try {
  346. readPackHeader();
  347. entries = new PackedObjectInfo[(int) objectCount];
  348. baseById = new ObjectIdSubclassMap<DeltaChain>();
  349. baseByPos = new LongMap<UnresolvedDelta>();
  350. progress.beginTask(PROGRESS_DOWNLOAD, (int) objectCount);
  351. for (int done = 0; done < objectCount; done++) {
  352. indexOneObject();
  353. progress.update(1);
  354. if (progress.isCancelled())
  355. throw new IOException(JGitText.get().downloadCancelled);
  356. }
  357. readPackFooter();
  358. endInput();
  359. progress.endTask();
  360. if (deltaCount > 0) {
  361. if (packOut == null)
  362. throw new IOException(JGitText.get().needPackOut);
  363. resolveDeltas(progress);
  364. if (entryCount < objectCount) {
  365. if (!fixThin) {
  366. throw new IOException(MessageFormat.format(
  367. JGitText.get().packHasUnresolvedDeltas, (objectCount - entryCount)));
  368. }
  369. fixThinPack(progress);
  370. }
  371. }
  372. if (packOut != null && (keepEmpty || entryCount > 0))
  373. packOut.getChannel().force(true);
  374. packDigest = null;
  375. baseById = null;
  376. baseByPos = null;
  377. if (dstIdx != null && (keepEmpty || entryCount > 0))
  378. writeIdx();
  379. } finally {
  380. try {
  381. if (readCurs != null)
  382. readCurs.release();
  383. } finally {
  384. readCurs = null;
  385. }
  386. try {
  387. InflaterCache.release(inflater);
  388. } finally {
  389. inflater = null;
  390. objectDatabase.close();
  391. }
  392. progress.endTask();
  393. if (packOut != null)
  394. packOut.close();
  395. }
  396. if (keepEmpty || entryCount > 0) {
  397. if (dstPack != null)
  398. dstPack.setReadOnly();
  399. if (dstIdx != null)
  400. dstIdx.setReadOnly();
  401. }
  402. } catch (IOException err) {
  403. if (dstPack != null)
  404. dstPack.delete();
  405. if (dstIdx != null)
  406. dstIdx.delete();
  407. throw err;
  408. }
  409. }
  410. private void resolveDeltas(final ProgressMonitor progress)
  411. throws IOException {
  412. progress.beginTask(PROGRESS_RESOLVE_DELTA, deltaCount);
  413. final int last = entryCount;
  414. for (int i = 0; i < last; i++) {
  415. final int before = entryCount;
  416. resolveDeltas(entries[i]);
  417. progress.update(entryCount - before);
  418. if (progress.isCancelled())
  419. throw new IOException(JGitText.get().downloadCancelledDuringIndexing);
  420. }
  421. progress.endTask();
  422. }
  423. private void resolveDeltas(final PackedObjectInfo oe) throws IOException {
  424. final int oldCRC = oe.getCRC();
  425. if (baseById.get(oe) != null || baseByPos.containsKey(oe.getOffset()))
  426. resolveDeltas(oe.getOffset(), oldCRC, Constants.OBJ_BAD, null, oe);
  427. }
  428. private void resolveDeltas(final long pos, final int oldCRC, int type,
  429. byte[] data, PackedObjectInfo oe) throws IOException {
  430. crc.reset();
  431. position(pos);
  432. int c = readFrom(Source.FILE);
  433. final int typeCode = (c >> 4) & 7;
  434. long sz = c & 15;
  435. int shift = 4;
  436. while ((c & 0x80) != 0) {
  437. c = readFrom(Source.FILE);
  438. sz += (c & 0x7f) << shift;
  439. shift += 7;
  440. }
  441. switch (typeCode) {
  442. case Constants.OBJ_COMMIT:
  443. case Constants.OBJ_TREE:
  444. case Constants.OBJ_BLOB:
  445. case Constants.OBJ_TAG:
  446. type = typeCode;
  447. data = inflateAndReturn(Source.FILE, sz);
  448. break;
  449. case Constants.OBJ_OFS_DELTA: {
  450. c = readFrom(Source.FILE) & 0xff;
  451. while ((c & 128) != 0)
  452. c = readFrom(Source.FILE) & 0xff;
  453. data = BinaryDelta.apply(data, inflateAndReturn(Source.FILE, sz));
  454. break;
  455. }
  456. case Constants.OBJ_REF_DELTA: {
  457. crc.update(buf, fill(Source.FILE, 20), 20);
  458. use(20);
  459. data = BinaryDelta.apply(data, inflateAndReturn(Source.FILE, sz));
  460. break;
  461. }
  462. default:
  463. throw new IOException(MessageFormat.format(JGitText.get().unknownObjectType, typeCode));
  464. }
  465. final int crc32 = (int) crc.getValue();
  466. if (oldCRC != crc32)
  467. throw new IOException(MessageFormat.format(JGitText.get().corruptionDetectedReReadingAt, pos));
  468. if (oe == null) {
  469. objectDigest.update(Constants.encodedTypeString(type));
  470. objectDigest.update((byte) ' ');
  471. objectDigest.update(Constants.encodeASCII(data.length));
  472. objectDigest.update((byte) 0);
  473. objectDigest.update(data);
  474. tempObjectId.fromRaw(objectDigest.digest(), 0);
  475. verifySafeObject(tempObjectId, type, data);
  476. oe = new PackedObjectInfo(pos, crc32, tempObjectId);
  477. addObjectAndTrack(oe);
  478. }
  479. resolveChildDeltas(pos, type, data, oe);
  480. }
  481. private UnresolvedDelta removeBaseById(final AnyObjectId id){
  482. final DeltaChain d = baseById.get(id);
  483. return d != null ? d.remove() : null;
  484. }
  485. private static UnresolvedDelta reverse(UnresolvedDelta c) {
  486. UnresolvedDelta tail = null;
  487. while (c != null) {
  488. final UnresolvedDelta n = c.next;
  489. c.next = tail;
  490. tail = c;
  491. c = n;
  492. }
  493. return tail;
  494. }
  495. private void resolveChildDeltas(final long pos, int type, byte[] data,
  496. PackedObjectInfo oe) throws IOException {
  497. UnresolvedDelta a = reverse(removeBaseById(oe));
  498. UnresolvedDelta b = reverse(baseByPos.remove(pos));
  499. while (a != null && b != null) {
  500. if (a.position < b.position) {
  501. resolveDeltas(a.position, a.crc, type, data, null);
  502. a = a.next;
  503. } else {
  504. resolveDeltas(b.position, b.crc, type, data, null);
  505. b = b.next;
  506. }
  507. }
  508. resolveChildDeltaChain(type, data, a);
  509. resolveChildDeltaChain(type, data, b);
  510. }
  511. private void resolveChildDeltaChain(final int type, final byte[] data,
  512. UnresolvedDelta a) throws IOException {
  513. while (a != null) {
  514. resolveDeltas(a.position, a.crc, type, data, null);
  515. a = a.next;
  516. }
  517. }
  518. private void fixThinPack(final ProgressMonitor progress) throws IOException {
  519. growEntries();
  520. if (needBaseObjectIds)
  521. baseObjectIds = new ObjectIdSubclassMap<ObjectId>();
  522. packDigest.reset();
  523. originalEOF = packOut.length() - 20;
  524. final Deflater def = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
  525. final List<DeltaChain> missing = new ArrayList<DeltaChain>(64);
  526. long end = originalEOF;
  527. for (final DeltaChain baseId : baseById) {
  528. if (baseId.head == null)
  529. continue;
  530. if (needBaseObjectIds)
  531. baseObjectIds.add(baseId);
  532. final ObjectLoader ldr;
  533. try {
  534. ldr = readCurs.open(baseId);
  535. } catch (MissingObjectException notFound) {
  536. missing.add(baseId);
  537. continue;
  538. }
  539. final byte[] data = ldr.getCachedBytes(Integer.MAX_VALUE);
  540. final int typeCode = ldr.getType();
  541. final PackedObjectInfo oe;
  542. crc.reset();
  543. packOut.seek(end);
  544. writeWhole(def, typeCode, data);
  545. oe = new PackedObjectInfo(end, (int) crc.getValue(), baseId);
  546. entries[entryCount++] = oe;
  547. end = packOut.getFilePointer();
  548. resolveChildDeltas(oe.getOffset(), typeCode, data, oe);
  549. if (progress.isCancelled())
  550. throw new IOException(JGitText.get().downloadCancelledDuringIndexing);
  551. }
  552. def.end();
  553. for (final DeltaChain base : missing) {
  554. if (base.head != null)
  555. throw new MissingObjectException(base, "delta base");
  556. }
  557. if (end - originalEOF < 20) {
  558. // Ugly corner case; if what we appended on to complete deltas
  559. // doesn't completely cover the SHA-1 we have to truncate off
  560. // we need to shorten the file, otherwise we will include part
  561. // of the old footer as object content.
  562. packOut.setLength(end);
  563. }
  564. fixHeaderFooter(packcsum, packDigest.digest());
  565. }
  566. private void writeWhole(final Deflater def, final int typeCode,
  567. final byte[] data) throws IOException {
  568. int sz = data.length;
  569. int hdrlen = 0;
  570. buf[hdrlen++] = (byte) ((typeCode << 4) | sz & 15);
  571. sz >>>= 4;
  572. while (sz > 0) {
  573. buf[hdrlen - 1] |= 0x80;
  574. buf[hdrlen++] = (byte) (sz & 0x7f);
  575. sz >>>= 7;
  576. }
  577. packDigest.update(buf, 0, hdrlen);
  578. crc.update(buf, 0, hdrlen);
  579. packOut.write(buf, 0, hdrlen);
  580. def.reset();
  581. def.setInput(data);
  582. def.finish();
  583. while (!def.finished()) {
  584. final int datlen = def.deflate(buf);
  585. packDigest.update(buf, 0, datlen);
  586. crc.update(buf, 0, datlen);
  587. packOut.write(buf, 0, datlen);
  588. }
  589. }
  590. private void fixHeaderFooter(final byte[] origcsum, final byte[] tailcsum)
  591. throws IOException {
  592. final MessageDigest origDigest = Constants.newMessageDigest();
  593. final MessageDigest tailDigest = Constants.newMessageDigest();
  594. long origRemaining = originalEOF;
  595. packOut.seek(0);
  596. bAvail = 0;
  597. bOffset = 0;
  598. fill(Source.FILE, 12);
  599. {
  600. final int origCnt = (int) Math.min(bAvail, origRemaining);
  601. origDigest.update(buf, 0, origCnt);
  602. origRemaining -= origCnt;
  603. if (origRemaining == 0)
  604. tailDigest.update(buf, origCnt, bAvail - origCnt);
  605. }
  606. NB.encodeInt32(buf, 8, entryCount);
  607. packOut.seek(0);
  608. packOut.write(buf, 0, 12);
  609. packOut.seek(bAvail);
  610. packDigest.reset();
  611. packDigest.update(buf, 0, bAvail);
  612. for (;;) {
  613. final int n = packOut.read(buf);
  614. if (n < 0)
  615. break;
  616. if (origRemaining != 0) {
  617. final int origCnt = (int) Math.min(n, origRemaining);
  618. origDigest.update(buf, 0, origCnt);
  619. origRemaining -= origCnt;
  620. if (origRemaining == 0)
  621. tailDigest.update(buf, origCnt, n - origCnt);
  622. } else
  623. tailDigest.update(buf, 0, n);
  624. packDigest.update(buf, 0, n);
  625. }
  626. if (!Arrays.equals(origDigest.digest(), origcsum)
  627. || !Arrays.equals(tailDigest.digest(), tailcsum))
  628. throw new IOException(JGitText.get().packCorruptedWhileWritingToFilesystem);
  629. packcsum = packDigest.digest();
  630. packOut.write(packcsum);
  631. }
  632. private void growEntries() {
  633. final PackedObjectInfo[] ne;
  634. ne = new PackedObjectInfo[(int) objectCount + baseById.size()];
  635. System.arraycopy(entries, 0, ne, 0, entryCount);
  636. entries = ne;
  637. }
  638. private void writeIdx() throws IOException {
  639. Arrays.sort(entries, 0, entryCount);
  640. List<PackedObjectInfo> list = Arrays.asList(entries);
  641. if (entryCount < entries.length)
  642. list = list.subList(0, entryCount);
  643. final FileOutputStream os = new FileOutputStream(dstIdx);
  644. try {
  645. final PackIndexWriter iw;
  646. if (outputVersion <= 0)
  647. iw = PackIndexWriter.createOldestPossible(os, list);
  648. else
  649. iw = PackIndexWriter.createVersion(os, outputVersion);
  650. iw.write(list, packcsum);
  651. os.getChannel().force(true);
  652. } finally {
  653. os.close();
  654. }
  655. }
  656. private void readPackHeader() throws IOException {
  657. final int hdrln = Constants.PACK_SIGNATURE.length + 4 + 4;
  658. final int p = fill(Source.INPUT, hdrln);
  659. for (int k = 0; k < Constants.PACK_SIGNATURE.length; k++)
  660. if (buf[p + k] != Constants.PACK_SIGNATURE[k])
  661. throw new IOException(JGitText.get().notAPACKFile);
  662. final long vers = NB.decodeUInt32(buf, p + 4);
  663. if (vers != 2 && vers != 3)
  664. throw new IOException(MessageFormat.format(JGitText.get().unsupportedPackVersion, vers));
  665. objectCount = NB.decodeUInt32(buf, p + 8);
  666. use(hdrln);
  667. }
  668. private void readPackFooter() throws IOException {
  669. sync();
  670. final byte[] cmpcsum = packDigest.digest();
  671. final int c = fill(Source.INPUT, 20);
  672. packcsum = new byte[20];
  673. System.arraycopy(buf, c, packcsum, 0, 20);
  674. use(20);
  675. if (packOut != null)
  676. packOut.write(packcsum);
  677. if (!Arrays.equals(cmpcsum, packcsum))
  678. throw new CorruptObjectException(JGitText.get().corruptObjectPackfileChecksumIncorrect);
  679. }
  680. // Cleanup all resources associated with our input parsing.
  681. private void endInput() {
  682. in = null;
  683. skipBuffer = null;
  684. }
  685. // Read one entire object or delta from the input.
  686. private void indexOneObject() throws IOException {
  687. final long pos = position();
  688. crc.reset();
  689. int c = readFrom(Source.INPUT);
  690. final int typeCode = (c >> 4) & 7;
  691. long sz = c & 15;
  692. int shift = 4;
  693. while ((c & 0x80) != 0) {
  694. c = readFrom(Source.INPUT);
  695. sz += (c & 0x7f) << shift;
  696. shift += 7;
  697. }
  698. switch (typeCode) {
  699. case Constants.OBJ_COMMIT:
  700. case Constants.OBJ_TREE:
  701. case Constants.OBJ_BLOB:
  702. case Constants.OBJ_TAG:
  703. whole(typeCode, pos, sz);
  704. break;
  705. case Constants.OBJ_OFS_DELTA: {
  706. c = readFrom(Source.INPUT);
  707. long ofs = c & 127;
  708. while ((c & 128) != 0) {
  709. ofs += 1;
  710. c = readFrom(Source.INPUT);
  711. ofs <<= 7;
  712. ofs += (c & 127);
  713. }
  714. final long base = pos - ofs;
  715. final UnresolvedDelta n;
  716. inflateAndSkip(Source.INPUT, sz);
  717. n = new UnresolvedDelta(pos, (int) crc.getValue());
  718. n.next = baseByPos.put(base, n);
  719. deltaCount++;
  720. break;
  721. }
  722. case Constants.OBJ_REF_DELTA: {
  723. c = fill(Source.INPUT, 20);
  724. crc.update(buf, c, 20);
  725. final ObjectId base = ObjectId.fromRaw(buf, c);
  726. use(20);
  727. DeltaChain r = baseById.get(base);
  728. if (r == null) {
  729. r = new DeltaChain(base);
  730. baseById.add(r);
  731. }
  732. inflateAndSkip(Source.INPUT, sz);
  733. r.add(new UnresolvedDelta(pos, (int) crc.getValue()));
  734. deltaCount++;
  735. break;
  736. }
  737. default:
  738. throw new IOException(MessageFormat.format(JGitText.get().unknownObjectType, typeCode));
  739. }
  740. }
  741. private void whole(final int type, final long pos, final long sz)
  742. throws IOException {
  743. final byte[] data = inflateAndReturn(Source.INPUT, sz);
  744. objectDigest.update(Constants.encodedTypeString(type));
  745. objectDigest.update((byte) ' ');
  746. objectDigest.update(Constants.encodeASCII(sz));
  747. objectDigest.update((byte) 0);
  748. objectDigest.update(data);
  749. tempObjectId.fromRaw(objectDigest.digest(), 0);
  750. verifySafeObject(tempObjectId, type, data);
  751. final int crc32 = (int) crc.getValue();
  752. addObjectAndTrack(new PackedObjectInfo(pos, crc32, tempObjectId));
  753. }
  754. private void verifySafeObject(final AnyObjectId id, final int type,
  755. final byte[] data) throws IOException {
  756. if (objCheck != null) {
  757. try {
  758. objCheck.check(type, data);
  759. } catch (CorruptObjectException e) {
  760. throw new IOException(MessageFormat.format(JGitText.get().invalidObject
  761. , Constants.typeString(type) , id.name() , e.getMessage()));
  762. }
  763. }
  764. try {
  765. final ObjectLoader ldr = readCurs.open(id, type);
  766. final byte[] existingData = ldr.getCachedBytes(Integer.MAX_VALUE);
  767. if (!Arrays.equals(data, existingData)) {
  768. throw new IOException(MessageFormat.format(JGitText.get().collisionOn, id.name()));
  769. }
  770. } catch (MissingObjectException notLocal) {
  771. // This is OK, we don't have a copy of the object locally
  772. // but the API throws when we try to read it as usually its
  773. // an error to read something that doesn't exist.
  774. }
  775. }
  776. // Current position of {@link #bOffset} within the entire file.
  777. private long position() {
  778. return bBase + bOffset;
  779. }
  780. private void position(final long pos) throws IOException {
  781. packOut.seek(pos);
  782. bBase = pos;
  783. bOffset = 0;
  784. bAvail = 0;
  785. }
  786. // Consume exactly one byte from the buffer and return it.
  787. private int readFrom(final Source src) throws IOException {
  788. if (bAvail == 0)
  789. fill(src, 1);
  790. bAvail--;
  791. final int b = buf[bOffset++] & 0xff;
  792. crc.update(b);
  793. return b;
  794. }
  795. // Consume cnt bytes from the buffer.
  796. private void use(final int cnt) {
  797. bOffset += cnt;
  798. bAvail -= cnt;
  799. }
  800. // Ensure at least need bytes are available in in {@link #buf}.
  801. private int fill(final Source src, final int need) throws IOException {
  802. while (bAvail < need) {
  803. int next = bOffset + bAvail;
  804. int free = buf.length - next;
  805. if (free + bAvail < need) {
  806. switch(src){
  807. case INPUT:
  808. sync();
  809. break;
  810. case FILE:
  811. if (bAvail > 0)
  812. System.arraycopy(buf, bOffset, buf, 0, bAvail);
  813. bOffset = 0;
  814. break;
  815. }
  816. next = bAvail;
  817. free = buf.length - next;
  818. }
  819. switch(src){
  820. case INPUT:
  821. next = in.read(buf, next, free);
  822. break;
  823. case FILE:
  824. next = packOut.read(buf, next, free);
  825. break;
  826. }
  827. if (next <= 0)
  828. throw new EOFException(JGitText.get().packfileIsTruncated);
  829. bAvail += next;
  830. }
  831. return bOffset;
  832. }
  833. // Store consumed bytes in {@link #buf} up to {@link #bOffset}.
  834. private void sync() throws IOException {
  835. packDigest.update(buf, 0, bOffset);
  836. if (packOut != null)
  837. packOut.write(buf, 0, bOffset);
  838. if (bAvail > 0)
  839. System.arraycopy(buf, bOffset, buf, 0, bAvail);
  840. bBase += bOffset;
  841. bOffset = 0;
  842. }
  843. private void inflateAndSkip(final Source src, final long inflatedSize)
  844. throws IOException {
  845. inflate(src, inflatedSize, skipBuffer, false /* do not keep result */);
  846. }
  847. private byte[] inflateAndReturn(final Source src, final long inflatedSize)
  848. throws IOException {
  849. final byte[] dst = new byte[(int) inflatedSize];
  850. inflate(src, inflatedSize, dst, true /* keep result in dst */);
  851. return dst;
  852. }
  853. private void inflate(final Source src, final long inflatedSize,
  854. final byte[] dst, final boolean keep) throws IOException {
  855. final Inflater inf = inflater;
  856. try {
  857. int off = 0;
  858. long cnt = 0;
  859. int p = fill(src, 24);
  860. inf.setInput(buf, p, bAvail);
  861. for (;;) {
  862. int r = inf.inflate(dst, off, dst.length - off);
  863. if (r == 0) {
  864. if (inf.finished())
  865. break;
  866. if (inf.needsInput()) {
  867. if (p >= 0) {
  868. crc.update(buf, p, bAvail);
  869. use(bAvail);
  870. }
  871. p = fill(src, 24);
  872. inf.setInput(buf, p, bAvail);
  873. } else {
  874. throw new CorruptObjectException(MessageFormat.format(
  875. JGitText.get().packfileCorruptionDetected,
  876. JGitText.get().unknownZlibError));
  877. }
  878. }
  879. cnt += r;
  880. if (keep)
  881. off += r;
  882. }
  883. if (cnt != inflatedSize) {
  884. throw new CorruptObjectException(MessageFormat.format(JGitText
  885. .get().packfileCorruptionDetected,
  886. JGitText.get().wrongDecompressedLength));
  887. }
  888. int left = bAvail - inf.getRemaining();
  889. if (left > 0) {
  890. crc.update(buf, p, left);
  891. use(left);
  892. }
  893. } catch (DataFormatException dfe) {
  894. throw new CorruptObjectException(MessageFormat.format(JGitText
  895. .get().packfileCorruptionDetected, dfe.getMessage()));
  896. } finally {
  897. inf.reset();
  898. }
  899. }
  900. private static class DeltaChain extends ObjectId {
  901. UnresolvedDelta head;
  902. DeltaChain(final AnyObjectId id) {
  903. super(id);
  904. }
  905. UnresolvedDelta remove() {
  906. final UnresolvedDelta r = head;
  907. if (r != null)
  908. head = null;
  909. return r;
  910. }
  911. void add(final UnresolvedDelta d) {
  912. d.next = head;
  913. head = d;
  914. }
  915. }
  916. private static class UnresolvedDelta {
  917. final long position;
  918. final int crc;
  919. UnresolvedDelta next;
  920. UnresolvedDelta(final long headerOffset, final int crc32) {
  921. position = headerOffset;
  922. crc = crc32;
  923. }
  924. }
  925. /**
  926. * Rename the pack to it's final name and location and open it.
  927. * <p>
  928. * If the call completes successfully the repository this IndexPack instance
  929. * was created with will have the objects in the pack available for reading
  930. * and use, without needing to scan for packs.
  931. *
  932. * @throws IOException
  933. * The pack could not be inserted into the repository's objects
  934. * directory. The pack no longer exists on disk, as it was
  935. * removed prior to throwing the exception to the caller.
  936. */
  937. public void renameAndOpenPack() throws IOException {
  938. renameAndOpenPack(null);
  939. }
  940. /**
  941. * Rename the pack to it's final name and location and open it.
  942. * <p>
  943. * If the call completes successfully the repository this IndexPack instance
  944. * was created with will have the objects in the pack available for reading
  945. * and use, without needing to scan for packs.
  946. *
  947. * @param lockMessage
  948. * message to place in the pack-*.keep file. If null, no lock
  949. * will be created, and this method returns null.
  950. * @return the pack lock object, if lockMessage is not null.
  951. * @throws IOException
  952. * The pack could not be inserted into the repository's objects
  953. * directory. The pack no longer exists on disk, as it was
  954. * removed prior to throwing the exception to the caller.
  955. */
  956. public PackLock renameAndOpenPack(final String lockMessage)
  957. throws IOException {
  958. if (!keepEmpty && entryCount == 0) {
  959. cleanupTemporaryFiles();
  960. return null;
  961. }
  962. final MessageDigest d = Constants.newMessageDigest();
  963. final byte[] oeBytes = new byte[Constants.OBJECT_ID_LENGTH];
  964. for (int i = 0; i < entryCount; i++) {
  965. final PackedObjectInfo oe = entries[i];
  966. oe.copyRawTo(oeBytes, 0);
  967. d.update(oeBytes);
  968. }
  969. final String name = ObjectId.fromRaw(d.digest()).name();
  970. final File packDir = new File(repo.getObjectsDirectory(), "pack");
  971. final File finalPack = new File(packDir, "pack-" + name + ".pack");
  972. final File finalIdx = new File(packDir, "pack-" + name + ".idx");
  973. final PackLock keep = new PackLock(finalPack, repo.getFS());
  974. if (!packDir.exists() && !packDir.mkdir() && !packDir.exists()) {
  975. // The objects/pack directory isn't present, and we are unable
  976. // to create it. There is no way to move this pack in.
  977. //
  978. cleanupTemporaryFiles();
  979. throw new IOException(MessageFormat.format(JGitText.get().cannotCreateDirectory, packDir.getAbsolutePath()));
  980. }
  981. if (finalPack.exists()) {
  982. // If the pack is already present we should never replace it.
  983. //
  984. cleanupTemporaryFiles();
  985. return null;
  986. }
  987. if (lockMessage != null) {
  988. // If we have a reason to create a keep file for this pack, do
  989. // so, or fail fast and don't put the pack in place.
  990. //
  991. try {
  992. if (!keep.lock(lockMessage))
  993. throw new IOException(MessageFormat.format(JGitText.get().cannotLockPackIn, finalPack));
  994. } catch (IOException e) {
  995. cleanupTemporaryFiles();
  996. throw e;
  997. }
  998. }
  999. if (!dstPack.renameTo(finalPack)) {
  1000. cleanupTemporaryFiles();
  1001. keep.unlock();
  1002. throw new IOException(MessageFormat.format(JGitText.get().cannotMovePackTo, finalPack));
  1003. }
  1004. if (!dstIdx.renameTo(finalIdx)) {
  1005. cleanupTemporaryFiles();
  1006. keep.unlock();
  1007. if (!finalPack.delete())
  1008. finalPack.deleteOnExit();
  1009. throw new IOException(MessageFormat.format(JGitText.get().cannotMoveIndexTo, finalIdx));
  1010. }
  1011. try {
  1012. repo.openPack(finalPack, finalIdx);
  1013. } catch (IOException err) {
  1014. keep.unlock();
  1015. finalPack.delete();
  1016. finalIdx.delete();
  1017. throw err;
  1018. }
  1019. return lockMessage != null ? keep : null;
  1020. }
  1021. private void cleanupTemporaryFiles() {
  1022. if (!dstIdx.delete())
  1023. dstIdx.deleteOnExit();
  1024. if (!dstPack.delete())
  1025. dstPack.deleteOnExit();
  1026. }
  1027. private void addObjectAndTrack(PackedObjectInfo oe) {
  1028. entries[entryCount++] = oe;
  1029. if (needNewObjectIds())
  1030. newObjectIds.add(oe);
  1031. }
  1032. }