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.

DirCache.java 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
  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.dircache;
  46. import java.io.BufferedInputStream;
  47. import java.io.EOFException;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.io.OutputStream;
  54. import java.io.UnsupportedEncodingException;
  55. import java.security.DigestOutputStream;
  56. import java.security.MessageDigest;
  57. import java.text.MessageFormat;
  58. import java.util.ArrayList;
  59. import java.util.Arrays;
  60. import java.util.Comparator;
  61. import java.util.List;
  62. import org.eclipse.jgit.errors.CorruptObjectException;
  63. import org.eclipse.jgit.errors.IndexReadException;
  64. import org.eclipse.jgit.errors.LockFailedException;
  65. import org.eclipse.jgit.errors.UnmergedPathException;
  66. import org.eclipse.jgit.events.IndexChangedEvent;
  67. import org.eclipse.jgit.events.IndexChangedListener;
  68. import org.eclipse.jgit.internal.JGitText;
  69. import org.eclipse.jgit.internal.storage.file.FileSnapshot;
  70. import org.eclipse.jgit.internal.storage.file.LockFile;
  71. import org.eclipse.jgit.lib.AnyObjectId;
  72. import org.eclipse.jgit.lib.Constants;
  73. import org.eclipse.jgit.lib.ObjectId;
  74. import org.eclipse.jgit.lib.ObjectInserter;
  75. import org.eclipse.jgit.lib.ObjectReader;
  76. import org.eclipse.jgit.lib.Repository;
  77. import org.eclipse.jgit.treewalk.FileTreeIterator;
  78. import org.eclipse.jgit.treewalk.TreeWalk;
  79. import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
  80. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  81. import org.eclipse.jgit.util.FS;
  82. import org.eclipse.jgit.util.IO;
  83. import org.eclipse.jgit.util.MutableInteger;
  84. import org.eclipse.jgit.util.NB;
  85. import org.eclipse.jgit.util.TemporaryBuffer;
  86. import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
  87. /**
  88. * Support for the Git dircache (aka index file).
  89. * <p>
  90. * The index file keeps track of which objects are currently checked out in the
  91. * working directory, and the last modified time of those working files. Changes
  92. * in the working directory can be detected by comparing the modification times
  93. * to the cached modification time within the index file.
  94. * <p>
  95. * Index files are also used during merges, where the merge happens within the
  96. * index file first, and the working directory is updated as a post-merge step.
  97. * Conflicts are stored in the index file to allow tool (and human) based
  98. * resolutions to be easily performed.
  99. */
  100. public class DirCache {
  101. private static final byte[] SIG_DIRC = { 'D', 'I', 'R', 'C' };
  102. private static final int EXT_TREE = 0x54524545 /* 'TREE' */;
  103. private static final DirCacheEntry[] NO_ENTRIES = {};
  104. private static final byte[] NO_CHECKSUM = {};
  105. static final Comparator<DirCacheEntry> ENT_CMP = new Comparator<DirCacheEntry>() {
  106. public int compare(final DirCacheEntry o1, final DirCacheEntry o2) {
  107. final int cr = cmp(o1, o2);
  108. if (cr != 0)
  109. return cr;
  110. return o1.getStage() - o2.getStage();
  111. }
  112. };
  113. static int cmp(final DirCacheEntry a, final DirCacheEntry b) {
  114. return cmp(a.path, a.path.length, b);
  115. }
  116. static int cmp(final byte[] aPath, final int aLen, final DirCacheEntry b) {
  117. return cmp(aPath, aLen, b.path, b.path.length);
  118. }
  119. static int cmp(final byte[] aPath, final int aLen, final byte[] bPath,
  120. final int bLen) {
  121. for (int cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
  122. final int cmp = (aPath[cPos] & 0xff) - (bPath[cPos] & 0xff);
  123. if (cmp != 0)
  124. return cmp;
  125. }
  126. return aLen - bLen;
  127. }
  128. /**
  129. * Create a new empty index which is never stored on disk.
  130. *
  131. * @return an empty cache which has no backing store file. The cache may not
  132. * be read or written, but it may be queried and updated (in
  133. * memory).
  134. */
  135. public static DirCache newInCore() {
  136. return new DirCache(null, null);
  137. }
  138. /**
  139. * Create a new in memory index read from the contents of a tree.
  140. *
  141. * @param reader
  142. * reader to access the tree objects from a repository.
  143. * @param treeId
  144. * tree to read. Must identify a tree, not a tree-ish.
  145. * @return a new cache which has no backing store file, but contains the
  146. * contents of {@code treeId}.
  147. * @throws IOException
  148. * one or more trees not available from the ObjectReader.
  149. * @since 4.2
  150. */
  151. public static DirCache read(ObjectReader reader, AnyObjectId treeId)
  152. throws IOException {
  153. DirCache d = newInCore();
  154. DirCacheBuilder b = d.builder();
  155. b.addTree(null, DirCacheEntry.STAGE_0, reader, treeId);
  156. b.finish();
  157. return d;
  158. }
  159. /**
  160. * Create a new in-core index representation and read an index from disk.
  161. * <p>
  162. * The new index will be read before it is returned to the caller. Read
  163. * failures are reported as exceptions and therefore prevent the method from
  164. * returning a partially populated index.
  165. *
  166. * @param repository
  167. * repository containing the index to read
  168. * @return a cache representing the contents of the specified index file (if
  169. * it exists) or an empty cache if the file does not exist.
  170. * @throws IOException
  171. * the index file is present but could not be read.
  172. * @throws CorruptObjectException
  173. * the index file is using a format or extension that this
  174. * library does not support.
  175. */
  176. public static DirCache read(final Repository repository)
  177. throws CorruptObjectException, IOException {
  178. final DirCache c = read(repository.getIndexFile(), repository.getFS());
  179. c.repository = repository;
  180. return c;
  181. }
  182. /**
  183. * Create a new in-core index representation and read an index from disk.
  184. * <p>
  185. * The new index will be read before it is returned to the caller. Read
  186. * failures are reported as exceptions and therefore prevent the method from
  187. * returning a partially populated index.
  188. *
  189. * @param indexLocation
  190. * location of the index file on disk.
  191. * @param fs
  192. * the file system abstraction which will be necessary to perform
  193. * certain file system operations.
  194. * @return a cache representing the contents of the specified index file (if
  195. * it exists) or an empty cache if the file does not exist.
  196. * @throws IOException
  197. * the index file is present but could not be read.
  198. * @throws CorruptObjectException
  199. * the index file is using a format or extension that this
  200. * library does not support.
  201. */
  202. public static DirCache read(final File indexLocation, final FS fs)
  203. throws CorruptObjectException, IOException {
  204. final DirCache c = new DirCache(indexLocation, fs);
  205. c.read();
  206. return c;
  207. }
  208. /**
  209. * Create a new in-core index representation, lock it, and read from disk.
  210. * <p>
  211. * The new index will be locked and then read before it is returned to the
  212. * caller. Read failures are reported as exceptions and therefore prevent
  213. * the method from returning a partially populated index. On read failure,
  214. * the lock is released.
  215. *
  216. * @param indexLocation
  217. * location of the index file on disk.
  218. * @param fs
  219. * the file system abstraction which will be necessary to perform
  220. * certain file system operations.
  221. * @return a cache representing the contents of the specified index file (if
  222. * it exists) or an empty cache if the file does not exist.
  223. * @throws IOException
  224. * the index file is present but could not be read, or the lock
  225. * could not be obtained.
  226. * @throws CorruptObjectException
  227. * the index file is using a format or extension that this
  228. * library does not support.
  229. */
  230. public static DirCache lock(final File indexLocation, final FS fs)
  231. throws CorruptObjectException, IOException {
  232. final DirCache c = new DirCache(indexLocation, fs);
  233. if (!c.lock())
  234. throw new LockFailedException(indexLocation);
  235. try {
  236. c.read();
  237. } catch (IOException e) {
  238. c.unlock();
  239. throw e;
  240. } catch (RuntimeException e) {
  241. c.unlock();
  242. throw e;
  243. } catch (Error e) {
  244. c.unlock();
  245. throw e;
  246. }
  247. return c;
  248. }
  249. /**
  250. * Create a new in-core index representation, lock it, and read from disk.
  251. * <p>
  252. * The new index will be locked and then read before it is returned to the
  253. * caller. Read failures are reported as exceptions and therefore prevent
  254. * the method from returning a partially populated index. On read failure,
  255. * the lock is released.
  256. *
  257. * @param repository
  258. * repository containing the index to lock and read
  259. * @param indexChangedListener
  260. * listener to be informed when DirCache is committed
  261. * @return a cache representing the contents of the specified index file (if
  262. * it exists) or an empty cache if the file does not exist.
  263. * @throws IOException
  264. * the index file is present but could not be read, or the lock
  265. * could not be obtained.
  266. * @throws CorruptObjectException
  267. * the index file is using a format or extension that this
  268. * library does not support.
  269. * @since 2.0
  270. */
  271. public static DirCache lock(final Repository repository,
  272. final IndexChangedListener indexChangedListener)
  273. throws CorruptObjectException, IOException {
  274. DirCache c = lock(repository.getIndexFile(), repository.getFS(),
  275. indexChangedListener);
  276. c.repository = repository;
  277. return c;
  278. }
  279. /**
  280. * Create a new in-core index representation, lock it, and read from disk.
  281. * <p>
  282. * The new index will be locked and then read before it is returned to the
  283. * caller. Read failures are reported as exceptions and therefore prevent
  284. * the method from returning a partially populated index. On read failure,
  285. * the lock is released.
  286. *
  287. * @param indexLocation
  288. * location of the index file on disk.
  289. * @param fs
  290. * the file system abstraction which will be necessary to perform
  291. * certain file system operations.
  292. * @param indexChangedListener
  293. * listener to be informed when DirCache is committed
  294. * @return a cache representing the contents of the specified index file (if
  295. * it exists) or an empty cache if the file does not exist.
  296. * @throws IOException
  297. * the index file is present but could not be read, or the lock
  298. * could not be obtained.
  299. * @throws CorruptObjectException
  300. * the index file is using a format or extension that this
  301. * library does not support.
  302. */
  303. public static DirCache lock(final File indexLocation, final FS fs,
  304. IndexChangedListener indexChangedListener)
  305. throws CorruptObjectException,
  306. IOException {
  307. DirCache c = lock(indexLocation, fs);
  308. c.registerIndexChangedListener(indexChangedListener);
  309. return c;
  310. }
  311. /** Location of the current version of the index file. */
  312. private final File liveFile;
  313. /** Individual file index entries, sorted by path name. */
  314. private DirCacheEntry[] sortedEntries;
  315. /** Number of positions within {@link #sortedEntries} that are valid. */
  316. private int entryCnt;
  317. /** Cache tree for this index; null if the cache tree is not available. */
  318. private DirCacheTree tree;
  319. /** Our active lock (if we hold it); null if we don't have it locked. */
  320. private LockFile myLock;
  321. /** file system abstraction **/
  322. private final FS fs;
  323. /** Keep track of whether the index has changed or not */
  324. private FileSnapshot snapshot;
  325. /** index checksum when index was read from disk */
  326. private byte[] readIndexChecksum;
  327. /** index checksum when index was written to disk */
  328. private byte[] writeIndexChecksum;
  329. /** listener to be informed on commit */
  330. private IndexChangedListener indexChangedListener;
  331. /** Repository containing this index */
  332. private Repository repository;
  333. /**
  334. * Create a new in-core index representation.
  335. * <p>
  336. * The new index will be empty. Callers may wish to read from the on disk
  337. * file first with {@link #read()}.
  338. *
  339. * @param indexLocation
  340. * location of the index file on disk.
  341. * @param fs
  342. * the file system abstraction which will be necessary to perform
  343. * certain file system operations.
  344. */
  345. public DirCache(final File indexLocation, final FS fs) {
  346. liveFile = indexLocation;
  347. this.fs = fs;
  348. clear();
  349. }
  350. /**
  351. * Create a new builder to update this cache.
  352. * <p>
  353. * Callers should add all entries to the builder, then use
  354. * {@link DirCacheBuilder#finish()} to update this instance.
  355. *
  356. * @return a new builder instance for this cache.
  357. */
  358. public DirCacheBuilder builder() {
  359. return new DirCacheBuilder(this, entryCnt + 16);
  360. }
  361. /**
  362. * Create a new editor to recreate this cache.
  363. * <p>
  364. * Callers should add commands to the editor, then use
  365. * {@link DirCacheEditor#finish()} to update this instance.
  366. *
  367. * @return a new builder instance for this cache.
  368. */
  369. public DirCacheEditor editor() {
  370. return new DirCacheEditor(this, entryCnt + 16);
  371. }
  372. void replace(final DirCacheEntry[] e, final int cnt) {
  373. sortedEntries = e;
  374. entryCnt = cnt;
  375. tree = null;
  376. }
  377. /**
  378. * Read the index from disk, if it has changed on disk.
  379. * <p>
  380. * This method tries to avoid loading the index if it has not changed since
  381. * the last time we consulted it. A missing index file will be treated as
  382. * though it were present but had no file entries in it.
  383. *
  384. * @throws IOException
  385. * the index file is present but could not be read. This
  386. * DirCache instance may not be populated correctly.
  387. * @throws CorruptObjectException
  388. * the index file is using a format or extension that this
  389. * library does not support.
  390. */
  391. public void read() throws IOException, CorruptObjectException {
  392. if (liveFile == null)
  393. throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
  394. if (!liveFile.exists())
  395. clear();
  396. else if (snapshot == null || snapshot.isModified(liveFile)) {
  397. try {
  398. final FileInputStream inStream = new FileInputStream(liveFile);
  399. try {
  400. clear();
  401. readFrom(inStream);
  402. } finally {
  403. try {
  404. inStream.close();
  405. } catch (IOException err2) {
  406. // Ignore any close failures.
  407. }
  408. }
  409. } catch (FileNotFoundException fnfe) {
  410. if (liveFile.exists()) {
  411. // Panic: the index file exists but we can't read it
  412. throw new IndexReadException(
  413. MessageFormat.format(JGitText.get().cannotReadIndex,
  414. liveFile.getAbsolutePath(), fnfe));
  415. }
  416. // Someone must have deleted it between our exists test
  417. // and actually opening the path. That's fine, its empty.
  418. //
  419. clear();
  420. }
  421. snapshot = FileSnapshot.save(liveFile);
  422. }
  423. }
  424. /**
  425. * @return true if the memory state differs from the index file
  426. * @throws IOException
  427. */
  428. public boolean isOutdated() throws IOException {
  429. if (liveFile == null || !liveFile.exists())
  430. return false;
  431. return snapshot == null || snapshot.isModified(liveFile);
  432. }
  433. /** Empty this index, removing all entries. */
  434. public void clear() {
  435. snapshot = null;
  436. sortedEntries = NO_ENTRIES;
  437. entryCnt = 0;
  438. tree = null;
  439. readIndexChecksum = NO_CHECKSUM;
  440. }
  441. private void readFrom(final InputStream inStream) throws IOException,
  442. CorruptObjectException {
  443. final BufferedInputStream in = new BufferedInputStream(inStream);
  444. final MessageDigest md = Constants.newMessageDigest();
  445. // Read the index header and verify we understand it.
  446. //
  447. final byte[] hdr = new byte[20];
  448. IO.readFully(in, hdr, 0, 12);
  449. md.update(hdr, 0, 12);
  450. if (!is_DIRC(hdr))
  451. throw new CorruptObjectException(JGitText.get().notADIRCFile);
  452. final int ver = NB.decodeInt32(hdr, 4);
  453. boolean extended = false;
  454. if (ver == 3)
  455. extended = true;
  456. else if (ver != 2)
  457. throw new CorruptObjectException(MessageFormat.format(
  458. JGitText.get().unknownDIRCVersion, Integer.valueOf(ver)));
  459. entryCnt = NB.decodeInt32(hdr, 8);
  460. if (entryCnt < 0)
  461. throw new CorruptObjectException(JGitText.get().DIRCHasTooManyEntries);
  462. snapshot = FileSnapshot.save(liveFile);
  463. int smudge_s = (int) (snapshot.lastModified() / 1000);
  464. int smudge_ns = ((int) (snapshot.lastModified() % 1000)) * 1000000;
  465. // Load the individual file entries.
  466. //
  467. final int infoLength = DirCacheEntry.getMaximumInfoLength(extended);
  468. final byte[] infos = new byte[infoLength * entryCnt];
  469. sortedEntries = new DirCacheEntry[entryCnt];
  470. final MutableInteger infoAt = new MutableInteger();
  471. for (int i = 0; i < entryCnt; i++)
  472. sortedEntries[i] = new DirCacheEntry(infos, infoAt, in, md, smudge_s, smudge_ns);
  473. // After the file entries are index extensions, and then a footer.
  474. //
  475. for (;;) {
  476. in.mark(21);
  477. IO.readFully(in, hdr, 0, 20);
  478. if (in.read() < 0) {
  479. // No extensions present; the file ended where we expected.
  480. //
  481. break;
  482. }
  483. in.reset();
  484. md.update(hdr, 0, 8);
  485. IO.skipFully(in, 8);
  486. long sz = NB.decodeUInt32(hdr, 4);
  487. switch (NB.decodeInt32(hdr, 0)) {
  488. case EXT_TREE: {
  489. if (Integer.MAX_VALUE < sz) {
  490. throw new CorruptObjectException(MessageFormat.format(
  491. JGitText.get().DIRCExtensionIsTooLargeAt,
  492. formatExtensionName(hdr), Long.valueOf(sz)));
  493. }
  494. final byte[] raw = new byte[(int) sz];
  495. IO.readFully(in, raw, 0, raw.length);
  496. md.update(raw, 0, raw.length);
  497. tree = new DirCacheTree(raw, new MutableInteger(), null);
  498. break;
  499. }
  500. default:
  501. if (hdr[0] >= 'A' && hdr[0] <= 'Z') {
  502. // The extension is optional and is here only as
  503. // a performance optimization. Since we do not
  504. // understand it, we can safely skip past it, after
  505. // we include its data in our checksum.
  506. //
  507. skipOptionalExtension(in, md, hdr, sz);
  508. } else {
  509. // The extension is not an optimization and is
  510. // _required_ to understand this index format.
  511. // Since we did not trap it above we must abort.
  512. //
  513. throw new CorruptObjectException(MessageFormat.format(JGitText.get().DIRCExtensionNotSupportedByThisVersion
  514. , formatExtensionName(hdr)));
  515. }
  516. }
  517. }
  518. readIndexChecksum = md.digest();
  519. if (!Arrays.equals(readIndexChecksum, hdr)) {
  520. throw new CorruptObjectException(JGitText.get().DIRCChecksumMismatch);
  521. }
  522. }
  523. private void skipOptionalExtension(final InputStream in,
  524. final MessageDigest md, final byte[] hdr, long sz)
  525. throws IOException {
  526. final byte[] b = new byte[4096];
  527. while (0 < sz) {
  528. int n = in.read(b, 0, (int) Math.min(b.length, sz));
  529. if (n < 0) {
  530. throw new EOFException(
  531. MessageFormat.format(
  532. JGitText.get().shortReadOfOptionalDIRCExtensionExpectedAnotherBytes,
  533. formatExtensionName(hdr), Long.valueOf(sz)));
  534. }
  535. md.update(b, 0, n);
  536. sz -= n;
  537. }
  538. }
  539. private static String formatExtensionName(final byte[] hdr)
  540. throws UnsupportedEncodingException {
  541. return "'" + new String(hdr, 0, 4, "ISO-8859-1") + "'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  542. }
  543. private static boolean is_DIRC(final byte[] hdr) {
  544. if (hdr.length < SIG_DIRC.length)
  545. return false;
  546. for (int i = 0; i < SIG_DIRC.length; i++)
  547. if (hdr[i] != SIG_DIRC[i])
  548. return false;
  549. return true;
  550. }
  551. /**
  552. * Try to establish an update lock on the cache file.
  553. *
  554. * @return true if the lock is now held by the caller; false if it is held
  555. * by someone else.
  556. * @throws IOException
  557. * the output file could not be created. The caller does not
  558. * hold the lock.
  559. */
  560. public boolean lock() throws IOException {
  561. if (liveFile == null)
  562. throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
  563. final LockFile tmp = new LockFile(liveFile, fs);
  564. if (tmp.lock()) {
  565. tmp.setNeedStatInformation(true);
  566. myLock = tmp;
  567. return true;
  568. }
  569. return false;
  570. }
  571. /**
  572. * Write the entry records from memory to disk.
  573. * <p>
  574. * The cache must be locked first by calling {@link #lock()} and receiving
  575. * true as the return value. Applications are encouraged to lock the index,
  576. * then invoke {@link #read()} to ensure the in-memory data is current,
  577. * prior to updating the in-memory entries.
  578. * <p>
  579. * Once written the lock is closed and must be either committed with
  580. * {@link #commit()} or rolled back with {@link #unlock()}.
  581. *
  582. * @throws IOException
  583. * the output file could not be created. The caller no longer
  584. * holds the lock.
  585. */
  586. public void write() throws IOException {
  587. final LockFile tmp = myLock;
  588. requireLocked(tmp);
  589. try {
  590. writeTo(liveFile.getParentFile(),
  591. new SafeBufferedOutputStream(tmp.getOutputStream()));
  592. } catch (IOException err) {
  593. tmp.unlock();
  594. throw err;
  595. } catch (RuntimeException err) {
  596. tmp.unlock();
  597. throw err;
  598. } catch (Error err) {
  599. tmp.unlock();
  600. throw err;
  601. }
  602. }
  603. void writeTo(File dir, final OutputStream os) throws IOException {
  604. final MessageDigest foot = Constants.newMessageDigest();
  605. final DigestOutputStream dos = new DigestOutputStream(os, foot);
  606. boolean extended = false;
  607. for (int i = 0; i < entryCnt; i++)
  608. extended |= sortedEntries[i].isExtended();
  609. // Write the header.
  610. //
  611. final byte[] tmp = new byte[128];
  612. System.arraycopy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.length);
  613. NB.encodeInt32(tmp, 4, extended ? 3 : 2);
  614. NB.encodeInt32(tmp, 8, entryCnt);
  615. dos.write(tmp, 0, 12);
  616. // Write the individual file entries.
  617. final int smudge_s;
  618. final int smudge_ns;
  619. if (myLock != null) {
  620. // For new files we need to smudge the index entry
  621. // if they have been modified "now". Ideally we'd
  622. // want the timestamp when we're done writing the index,
  623. // so we use the current timestamp as a approximation.
  624. myLock.createCommitSnapshot();
  625. snapshot = myLock.getCommitSnapshot();
  626. smudge_s = (int) (snapshot.lastModified() / 1000);
  627. smudge_ns = ((int) (snapshot.lastModified() % 1000)) * 1000000;
  628. } else {
  629. // Used in unit tests only
  630. smudge_ns = 0;
  631. smudge_s = 0;
  632. }
  633. // Check if tree is non-null here since calling updateSmudgedEntries
  634. // will automatically build it via creating a DirCacheIterator
  635. final boolean writeTree = tree != null;
  636. if (repository != null && entryCnt > 0)
  637. updateSmudgedEntries();
  638. for (int i = 0; i < entryCnt; i++) {
  639. final DirCacheEntry e = sortedEntries[i];
  640. if (e.mightBeRacilyClean(smudge_s, smudge_ns))
  641. e.smudgeRacilyClean();
  642. e.write(dos);
  643. }
  644. if (writeTree) {
  645. TemporaryBuffer bb = new TemporaryBuffer.LocalFile(dir, 5 << 20);
  646. try {
  647. tree.write(tmp, bb);
  648. bb.close();
  649. NB.encodeInt32(tmp, 0, EXT_TREE);
  650. NB.encodeInt32(tmp, 4, (int) bb.length());
  651. dos.write(tmp, 0, 8);
  652. bb.writeTo(dos, null);
  653. } finally {
  654. bb.destroy();
  655. }
  656. }
  657. writeIndexChecksum = foot.digest();
  658. os.write(writeIndexChecksum);
  659. os.close();
  660. }
  661. /**
  662. * Commit this change and release the lock.
  663. * <p>
  664. * If this method fails (returns false) the lock is still released.
  665. *
  666. * @return true if the commit was successful and the file contains the new
  667. * data; false if the commit failed and the file remains with the
  668. * old data.
  669. * @throws IllegalStateException
  670. * the lock is not held.
  671. */
  672. public boolean commit() {
  673. final LockFile tmp = myLock;
  674. requireLocked(tmp);
  675. myLock = null;
  676. if (!tmp.commit())
  677. return false;
  678. snapshot = tmp.getCommitSnapshot();
  679. if (indexChangedListener != null
  680. && !Arrays.equals(readIndexChecksum, writeIndexChecksum))
  681. indexChangedListener.onIndexChanged(new IndexChangedEvent());
  682. return true;
  683. }
  684. private void requireLocked(final LockFile tmp) {
  685. if (liveFile == null)
  686. throw new IllegalStateException(JGitText.get().dirCacheIsNotLocked);
  687. if (tmp == null)
  688. throw new IllegalStateException(MessageFormat.format(JGitText.get().dirCacheFileIsNotLocked
  689. , liveFile.getAbsolutePath()));
  690. }
  691. /**
  692. * Unlock this file and abort this change.
  693. * <p>
  694. * The temporary file (if created) is deleted before returning.
  695. */
  696. public void unlock() {
  697. final LockFile tmp = myLock;
  698. if (tmp != null) {
  699. myLock = null;
  700. tmp.unlock();
  701. }
  702. }
  703. /**
  704. * Locate the position a path's entry is at in the index. For details refer
  705. * to #findEntry(byte[], int).
  706. *
  707. * @param path
  708. * the path to search for.
  709. * @return if &gt;= 0 then the return value is the position of the entry in
  710. * the index; pass to {@link #getEntry(int)} to obtain the entry
  711. * information. If &lt; 0 the entry does not exist in the index.
  712. */
  713. public int findEntry(final String path) {
  714. final byte[] p = Constants.encode(path);
  715. return findEntry(p, p.length);
  716. }
  717. /**
  718. * Locate the position a path's entry is at in the index.
  719. * <p>
  720. * If there is at least one entry in the index for this path the position of
  721. * the lowest stage is returned. Subsequent stages can be identified by
  722. * testing consecutive entries until the path differs.
  723. * <p>
  724. * If no path matches the entry -(position+1) is returned, where position is
  725. * the location it would have gone within the index.
  726. *
  727. * @param p
  728. * the byte array starting with the path to search for.
  729. * @param pLen
  730. * the length of the path in bytes
  731. * @return if &gt;= 0 then the return value is the position of the entry in
  732. * the index; pass to {@link #getEntry(int)} to obtain the entry
  733. * information. If &lt; 0 the entry does not exist in the index.
  734. * @since 3.4
  735. */
  736. public int findEntry(byte[] p, int pLen) {
  737. return findEntry(0, p, pLen);
  738. }
  739. int findEntry(int low, byte[] p, int pLen) {
  740. int high = entryCnt;
  741. while (low < high) {
  742. int mid = (low + high) >>> 1;
  743. final int cmp = cmp(p, pLen, sortedEntries[mid]);
  744. if (cmp < 0)
  745. high = mid;
  746. else if (cmp == 0) {
  747. while (mid > 0 && cmp(p, pLen, sortedEntries[mid - 1]) == 0)
  748. mid--;
  749. return mid;
  750. } else
  751. low = mid + 1;
  752. }
  753. return -(low + 1);
  754. }
  755. /**
  756. * Determine the next index position past all entries with the same name.
  757. * <p>
  758. * As index entries are sorted by path name, then stage number, this method
  759. * advances the supplied position to the first position in the index whose
  760. * path name does not match the path name of the supplied position's entry.
  761. *
  762. * @param position
  763. * entry position of the path that should be skipped.
  764. * @return position of the next entry whose path is after the input.
  765. */
  766. public int nextEntry(final int position) {
  767. DirCacheEntry last = sortedEntries[position];
  768. int nextIdx = position + 1;
  769. while (nextIdx < entryCnt) {
  770. final DirCacheEntry next = sortedEntries[nextIdx];
  771. if (cmp(last, next) != 0)
  772. break;
  773. last = next;
  774. nextIdx++;
  775. }
  776. return nextIdx;
  777. }
  778. int nextEntry(final byte[] p, final int pLen, int nextIdx) {
  779. while (nextIdx < entryCnt) {
  780. final DirCacheEntry next = sortedEntries[nextIdx];
  781. if (!DirCacheTree.peq(p, next.path, pLen))
  782. break;
  783. nextIdx++;
  784. }
  785. return nextIdx;
  786. }
  787. /**
  788. * Total number of file entries stored in the index.
  789. * <p>
  790. * This count includes unmerged stages for a file entry if the file is
  791. * currently conflicted in a merge. This means the total number of entries
  792. * in the index may be up to 3 times larger than the number of files in the
  793. * working directory.
  794. * <p>
  795. * Note that this value counts only <i>files</i>.
  796. *
  797. * @return number of entries available.
  798. * @see #getEntry(int)
  799. */
  800. public int getEntryCount() {
  801. return entryCnt;
  802. }
  803. /**
  804. * Get a specific entry.
  805. *
  806. * @param i
  807. * position of the entry to get.
  808. * @return the entry at position <code>i</code>.
  809. */
  810. public DirCacheEntry getEntry(final int i) {
  811. return sortedEntries[i];
  812. }
  813. /**
  814. * Get a specific entry.
  815. *
  816. * @param path
  817. * the path to search for.
  818. * @return the entry for the given <code>path</code>.
  819. */
  820. public DirCacheEntry getEntry(final String path) {
  821. final int i = findEntry(path);
  822. return i < 0 ? null : sortedEntries[i];
  823. }
  824. /**
  825. * Recursively get all entries within a subtree.
  826. *
  827. * @param path
  828. * the subtree path to get all entries within.
  829. * @return all entries recursively contained within the subtree.
  830. */
  831. public DirCacheEntry[] getEntriesWithin(String path) {
  832. if (path.length() == 0) {
  833. DirCacheEntry[] r = new DirCacheEntry[entryCnt];
  834. System.arraycopy(sortedEntries, 0, r, 0, entryCnt);
  835. return r;
  836. }
  837. if (!path.endsWith("/")) //$NON-NLS-1$
  838. path += "/"; //$NON-NLS-1$
  839. final byte[] p = Constants.encode(path);
  840. final int pLen = p.length;
  841. int eIdx = findEntry(p, pLen);
  842. if (eIdx < 0)
  843. eIdx = -(eIdx + 1);
  844. final int lastIdx = nextEntry(p, pLen, eIdx);
  845. final DirCacheEntry[] r = new DirCacheEntry[lastIdx - eIdx];
  846. System.arraycopy(sortedEntries, eIdx, r, 0, r.length);
  847. return r;
  848. }
  849. void toArray(final int i, final DirCacheEntry[] dst, final int off,
  850. final int cnt) {
  851. System.arraycopy(sortedEntries, i, dst, off, cnt);
  852. }
  853. /**
  854. * Obtain (or build) the current cache tree structure.
  855. * <p>
  856. * This method can optionally recreate the cache tree, without flushing the
  857. * tree objects themselves to disk.
  858. *
  859. * @param build
  860. * if true and the cache tree is not present in the index it will
  861. * be generated and returned to the caller.
  862. * @return the cache tree; null if there is no current cache tree available
  863. * and <code>build</code> was false.
  864. */
  865. public DirCacheTree getCacheTree(final boolean build) {
  866. if (build) {
  867. if (tree == null)
  868. tree = new DirCacheTree();
  869. tree.validate(sortedEntries, entryCnt, 0, 0);
  870. }
  871. return tree;
  872. }
  873. /**
  874. * Write all index trees to the object store, returning the root tree.
  875. *
  876. * @param ow
  877. * the writer to use when serializing to the store. The caller is
  878. * responsible for flushing the inserter before trying to use the
  879. * returned tree identity.
  880. * @return identity for the root tree.
  881. * @throws UnmergedPathException
  882. * one or more paths contain higher-order stages (stage &gt; 0),
  883. * which cannot be stored in a tree object.
  884. * @throws IllegalStateException
  885. * one or more paths contain an invalid mode which should never
  886. * appear in a tree object.
  887. * @throws IOException
  888. * an unexpected error occurred writing to the object store.
  889. */
  890. public ObjectId writeTree(final ObjectInserter ow)
  891. throws UnmergedPathException, IOException {
  892. return getCacheTree(true).writeTree(sortedEntries, 0, 0, ow);
  893. }
  894. /**
  895. * Tells whether this index contains unmerged paths.
  896. *
  897. * @return {@code true} if this index contains unmerged paths. Means: at
  898. * least one entry is of a stage different from 0. {@code false}
  899. * will be returned if all entries are of stage 0.
  900. */
  901. public boolean hasUnmergedPaths() {
  902. for (int i = 0; i < entryCnt; i++) {
  903. if (sortedEntries[i].getStage() > 0) {
  904. return true;
  905. }
  906. }
  907. return false;
  908. }
  909. private void registerIndexChangedListener(IndexChangedListener listener) {
  910. this.indexChangedListener = listener;
  911. }
  912. /**
  913. * Update any smudged entries with information from the working tree.
  914. *
  915. * @throws IOException
  916. */
  917. private void updateSmudgedEntries() throws IOException {
  918. List<String> paths = new ArrayList<String>(128);
  919. try (TreeWalk walk = new TreeWalk(repository)) {
  920. walk.setOperationType(OperationType.CHECKIN_OP);
  921. for (int i = 0; i < entryCnt; i++)
  922. if (sortedEntries[i].isSmudged())
  923. paths.add(sortedEntries[i].getPathString());
  924. if (paths.isEmpty())
  925. return;
  926. walk.setFilter(PathFilterGroup.createFromStrings(paths));
  927. DirCacheIterator iIter = new DirCacheIterator(this);
  928. FileTreeIterator fIter = new FileTreeIterator(repository);
  929. walk.addTree(iIter);
  930. walk.addTree(fIter);
  931. fIter.setDirCacheIterator(walk, 0);
  932. walk.setRecursive(true);
  933. while (walk.next()) {
  934. iIter = walk.getTree(0, DirCacheIterator.class);
  935. if (iIter == null)
  936. continue;
  937. fIter = walk.getTree(1, FileTreeIterator.class);
  938. if (fIter == null)
  939. continue;
  940. DirCacheEntry entry = iIter.getDirCacheEntry();
  941. if (entry.isSmudged() && iIter.idEqual(fIter)) {
  942. entry.setLength(fIter.getEntryLength());
  943. entry.setLastModified(fIter.getEntryLastModified());
  944. }
  945. }
  946. }
  947. }
  948. }