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

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