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