View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
5    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.eclipse.jgit.dircache;
48  
49  import java.io.ByteArrayOutputStream;
50  import java.io.EOFException;
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.io.OutputStream;
54  import java.nio.ByteBuffer;
55  import java.security.MessageDigest;
56  import java.text.MessageFormat;
57  import java.util.Arrays;
58  
59  import org.eclipse.jgit.errors.CorruptObjectException;
60  import org.eclipse.jgit.internal.JGitText;
61  import org.eclipse.jgit.lib.AnyObjectId;
62  import org.eclipse.jgit.lib.Constants;
63  import org.eclipse.jgit.lib.FileMode;
64  import org.eclipse.jgit.lib.ObjectId;
65  import org.eclipse.jgit.util.IO;
66  import org.eclipse.jgit.util.MutableInteger;
67  import org.eclipse.jgit.util.NB;
68  import org.eclipse.jgit.util.SystemReader;
69  
70  /**
71   * A single file (or stage of a file) in a {@link DirCache}.
72   * <p>
73   * An entry represents exactly one stage of a file. If a file path is unmerged
74   * then multiple DirCacheEntry instances may appear for the same path name.
75   */
76  public class DirCacheEntry {
77  	private static final byte[] nullpad = new byte[8];
78  
79  	/** The standard (fully merged) stage for an entry. */
80  	public static final int STAGE_0 = 0;
81  
82  	/** The base tree revision for an entry. */
83  	public static final int STAGE_1 = 1;
84  
85  	/** The first tree revision (usually called "ours"). */
86  	public static final int STAGE_2 = 2;
87  
88  	/** The second tree revision (usually called "theirs"). */
89  	public static final int STAGE_3 = 3;
90  
91  	private static final int P_CTIME = 0;
92  
93  	// private static final int P_CTIME_NSEC = 4;
94  
95  	private static final int P_MTIME = 8;
96  
97  	// private static final int P_MTIME_NSEC = 12;
98  
99  	// private static final int P_DEV = 16;
100 
101 	// private static final int P_INO = 20;
102 
103 	private static final int P_MODE = 24;
104 
105 	// private static final int P_UID = 28;
106 
107 	// private static final int P_GID = 32;
108 
109 	private static final int P_SIZE = 36;
110 
111 	private static final int P_OBJECTID = 40;
112 
113 	private static final int P_FLAGS = 60;
114 	private static final int P_FLAGS2 = 62;
115 
116 	/** Mask applied to data in {@link #P_FLAGS} to get the name length. */
117 	private static final int NAME_MASK = 0xfff;
118 
119 	private static final int INTENT_TO_ADD = 0x20000000;
120 	private static final int SKIP_WORKTREE = 0x40000000;
121 	private static final int EXTENDED_FLAGS = (INTENT_TO_ADD | SKIP_WORKTREE);
122 
123 	private static final int INFO_LEN = 62;
124 	private static final int INFO_LEN_EXTENDED = 64;
125 
126 	private static final int EXTENDED = 0x40;
127 	private static final int ASSUME_VALID = 0x80;
128 
129 	/** In-core flag signaling that the entry should be considered as modified. */
130 	private static final int UPDATE_NEEDED = 0x1;
131 
132 	/** (Possibly shared) header information storage. */
133 	private final byte[] info;
134 
135 	/** First location within {@link #info} where our header starts. */
136 	private final int infoOffset;
137 
138 	/** Our encoded path name, from the root of the repository. */
139 	final byte[] path;
140 
141 	/** Flags which are never stored to disk. */
142 	private byte inCoreFlags;
143 
144 	DirCacheEntry(final byte[] sharedInfo, final MutableInteger infoAt,
145 			final InputStream in, final MessageDigest md, final int smudge_s,
146 			final int smudge_ns) throws IOException {
147 		info = sharedInfo;
148 		infoOffset = infoAt.value;
149 
150 		IO.readFully(in, info, infoOffset, INFO_LEN);
151 
152 		final int len;
153 		if (isExtended()) {
154 			len = INFO_LEN_EXTENDED;
155 			IO.readFully(in, info, infoOffset + INFO_LEN, INFO_LEN_EXTENDED - INFO_LEN);
156 
157 			if ((getExtendedFlags() & ~EXTENDED_FLAGS) != 0)
158 				throw new IOException(MessageFormat.format(JGitText.get()
159 						.DIRCUnrecognizedExtendedFlags, String.valueOf(getExtendedFlags())));
160 		} else
161 			len = INFO_LEN;
162 
163 		infoAt.value += len;
164 		md.update(info, infoOffset, len);
165 
166 		int pathLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
167 		int skipped = 0;
168 		if (pathLen < NAME_MASK) {
169 			path = new byte[pathLen];
170 			IO.readFully(in, path, 0, pathLen);
171 			md.update(path, 0, pathLen);
172 		} else {
173 			final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
174 			{
175 				final byte[] buf = new byte[NAME_MASK];
176 				IO.readFully(in, buf, 0, NAME_MASK);
177 				tmp.write(buf);
178 			}
179 			for (;;) {
180 				final int c = in.read();
181 				if (c < 0)
182 					throw new EOFException(JGitText.get().shortReadOfBlock);
183 				if (c == 0)
184 					break;
185 				tmp.write(c);
186 			}
187 			path = tmp.toByteArray();
188 			pathLen = path.length;
189 			skipped = 1; // we already skipped 1 '\0' above to break the loop.
190 			md.update(path, 0, pathLen);
191 			md.update((byte) 0);
192 		}
193 
194 		try {
195 			checkPath(path);
196 		} catch (InvalidPathException e) {
197 			CorruptObjectException p =
198 				new CorruptObjectException(e.getMessage());
199 			if (e.getCause() != null)
200 				p.initCause(e.getCause());
201 			throw p;
202 		}
203 
204 		// Index records are padded out to the next 8 byte alignment
205 		// for historical reasons related to how C Git read the files.
206 		//
207 		final int actLen = len + pathLen;
208 		final int expLen = (actLen + 8) & ~7;
209 		final int padLen = expLen - actLen - skipped;
210 		if (padLen > 0) {
211 			IO.skipFully(in, padLen);
212 			md.update(nullpad, 0, padLen);
213 		}
214 
215 		if (mightBeRacilyClean(smudge_s, smudge_ns))
216 			smudgeRacilyClean();
217 	}
218 
219 	/**
220 	 * Create an empty entry at stage 0.
221 	 *
222 	 * @param newPath
223 	 *            name of the cache entry.
224 	 * @throws IllegalArgumentException
225 	 *             If the path starts or ends with "/", or contains "//" either
226 	 *             "\0". These sequences are not permitted in a git tree object
227 	 *             or DirCache file.
228 	 */
229 	public DirCacheEntry(final String newPath) {
230 		this(Constants.encode(newPath), STAGE_0);
231 	}
232 
233 	/**
234 	 * Create an empty entry at the specified stage.
235 	 *
236 	 * @param newPath
237 	 *            name of the cache entry.
238 	 * @param stage
239 	 *            the stage index of the new entry.
240 	 * @throws IllegalArgumentException
241 	 *             If the path starts or ends with "/", or contains "//" either
242 	 *             "\0". These sequences are not permitted in a git tree object
243 	 *             or DirCache file.  Or if {@code stage} is outside of the
244 	 *             range 0..3, inclusive.
245 	 */
246 	public DirCacheEntry(final String newPath, final int stage) {
247 		this(Constants.encode(newPath), stage);
248 	}
249 
250 	/**
251 	 * Create an empty entry at stage 0.
252 	 *
253 	 * @param newPath
254 	 *            name of the cache entry, in the standard encoding.
255 	 * @throws IllegalArgumentException
256 	 *             If the path starts or ends with "/", or contains "//" either
257 	 *             "\0". These sequences are not permitted in a git tree object
258 	 *             or DirCache file.
259 	 */
260 	public DirCacheEntry(final byte[] newPath) {
261 		this(newPath, STAGE_0);
262 	}
263 
264 	/**
265 	 * Create an empty entry at the specified stage.
266 	 *
267 	 * @param path
268 	 *            name of the cache entry, in the standard encoding.
269 	 * @param stage
270 	 *            the stage index of the new entry.
271 	 * @throws IllegalArgumentException
272 	 *             If the path starts or ends with "/", or contains "//" either
273 	 *             "\0". These sequences are not permitted in a git tree object
274 	 *             or DirCache file.  Or if {@code stage} is outside of the
275 	 *             range 0..3, inclusive.
276 	 */
277 	@SuppressWarnings("boxing")
278 	public DirCacheEntry(byte[] path, final int stage) {
279 		checkPath(path);
280 		if (stage < 0 || 3 < stage)
281 			throw new IllegalArgumentException(MessageFormat.format(
282 					JGitText.get().invalidStageForPath,
283 					stage, toString(path)));
284 
285 		info = new byte[INFO_LEN];
286 		infoOffset = 0;
287 		this.path = path;
288 
289 		int flags = ((stage & 0x3) << 12);
290 		if (path.length < NAME_MASK)
291 			flags |= path.length;
292 		else
293 			flags |= NAME_MASK;
294 		NB.encodeInt16(info, infoOffset + P_FLAGS, flags);
295 	}
296 
297 	/**
298 	 * Duplicate DirCacheEntry with same path and copied info.
299 	 * <p>
300 	 * The same path buffer is reused (avoiding copying), however a new info
301 	 * buffer is created and its contents are copied.
302 	 *
303 	 * @param src
304 	 *            entry to clone.
305 	 * @since 4.2
306 	 */
307 	public DirCacheEntry(DirCacheEntry src) {
308 		path = src.path;
309 		info = new byte[INFO_LEN];
310 		infoOffset = 0;
311 		System.arraycopy(src.info, src.infoOffset, info, 0, INFO_LEN);
312 	}
313 
314 	void write(final OutputStream os) throws IOException {
315 		final int len = isExtended() ? INFO_LEN_EXTENDED : INFO_LEN;
316 		final int pathLen = path.length;
317 		os.write(info, infoOffset, len);
318 		os.write(path, 0, pathLen);
319 
320 		// Index records are padded out to the next 8 byte alignment
321 		// for historical reasons related to how C Git read the files.
322 		//
323 		final int actLen = len + pathLen;
324 		final int expLen = (actLen + 8) & ~7;
325 		if (actLen != expLen)
326 			os.write(nullpad, 0, expLen - actLen);
327 	}
328 
329 	/**
330 	 * Is it possible for this entry to be accidentally assumed clean?
331 	 * <p>
332 	 * The "racy git" problem happens when a work file can be updated faster
333 	 * than the filesystem records file modification timestamps. It is possible
334 	 * for an application to edit a work file, update the index, then edit it
335 	 * again before the filesystem will give the work file a new modification
336 	 * timestamp. This method tests to see if file was written out at the same
337 	 * time as the index.
338 	 *
339 	 * @param smudge_s
340 	 *            seconds component of the index's last modified time.
341 	 * @param smudge_ns
342 	 *            nanoseconds component of the index's last modified time.
343 	 * @return true if extra careful checks should be used.
344 	 */
345 	public final boolean mightBeRacilyClean(final int smudge_s, final int smudge_ns) {
346 		// If the index has a modification time then it came from disk
347 		// and was not generated from scratch in memory. In such cases
348 		// the entry is 'racily clean' if the entry's cached modification
349 		// time is equal to or later than the index modification time. In
350 		// such cases the work file is too close to the index to tell if
351 		// it is clean or not based on the modification time alone.
352 		//
353 		final int base = infoOffset + P_MTIME;
354 		final int mtime = NB.decodeInt32(info, base);
355 		if (smudge_s == mtime)
356 			return smudge_ns <= NB.decodeInt32(info, base + 4);
357 		return false;
358 	}
359 
360 	/**
361 	 * Force this entry to no longer match its working tree file.
362 	 * <p>
363 	 * This avoids the "racy git" problem by making this index entry no longer
364 	 * match the file in the working directory. Later git will be forced to
365 	 * compare the file content to ensure the file matches the working tree.
366 	 */
367 	public final void smudgeRacilyClean() {
368 		// To mark an entry racily clean we set its length to 0 (like native git
369 		// does). Entries which are not racily clean and have zero length can be
370 		// distinguished from racily clean entries by checking P_OBJECTID
371 		// against the SHA1 of empty content. When length is 0 and P_OBJECTID is
372 		// different from SHA1 of empty content we know the entry is marked
373 		// racily clean
374 		final int base = infoOffset + P_SIZE;
375 		Arrays.fill(info, base, base + 4, (byte) 0);
376 	}
377 
378 	/**
379 	 * Check whether this entry has been smudged or not
380 	 * <p>
381 	 * If a blob has length 0 we know his id see {@link Constants#EMPTY_BLOB_ID}. If an entry
382 	 * has length 0 and an ID different from the one for empty blob we know this
383 	 * entry was smudged.
384 	 *
385 	 * @return <code>true</code> if the entry is smudged, <code>false</code>
386 	 *         otherwise
387 	 */
388 	public final boolean isSmudged() {
389 		final int base = infoOffset + P_OBJECTID;
390 		return (getLength() == 0) && (Constants.EMPTY_BLOB_ID.compareTo(info, base) != 0);
391 	}
392 
393 	final byte[] idBuffer() {
394 		return info;
395 	}
396 
397 	final int idOffset() {
398 		return infoOffset + P_OBJECTID;
399 	}
400 
401 	/**
402 	 * Is this entry always thought to be unmodified?
403 	 * <p>
404 	 * Most entries in the index do not have this flag set. Users may however
405 	 * set them on if the file system stat() costs are too high on this working
406 	 * directory, such as on NFS or SMB volumes.
407 	 *
408 	 * @return true if we must assume the entry is unmodified.
409 	 */
410 	public boolean isAssumeValid() {
411 		return (info[infoOffset + P_FLAGS] & ASSUME_VALID) != 0;
412 	}
413 
414 	/**
415 	 * Set the assume valid flag for this entry,
416 	 *
417 	 * @param assume
418 	 *            true to ignore apparent modifications; false to look at last
419 	 *            modified to detect file modifications.
420 	 */
421 	public void setAssumeValid(final boolean assume) {
422 		if (assume)
423 			info[infoOffset + P_FLAGS] |= ASSUME_VALID;
424 		else
425 			info[infoOffset + P_FLAGS] &= ~ASSUME_VALID;
426 	}
427 
428 	/**
429 	 * @return true if this entry should be checked for changes
430 	 */
431 	public boolean isUpdateNeeded() {
432 		return (inCoreFlags & UPDATE_NEEDED) != 0;
433 	}
434 
435 	/**
436 	 * Set whether this entry must be checked for changes
437 	 *
438 	 * @param updateNeeded
439 	 */
440 	public void setUpdateNeeded(boolean updateNeeded) {
441 		if (updateNeeded)
442 			inCoreFlags |= UPDATE_NEEDED;
443 		else
444 			inCoreFlags &= ~UPDATE_NEEDED;
445 	}
446 
447 	/**
448 	 * Get the stage of this entry.
449 	 * <p>
450 	 * Entries have one of 4 possible stages: 0-3.
451 	 *
452 	 * @return the stage of this entry.
453 	 */
454 	public int getStage() {
455 		return (info[infoOffset + P_FLAGS] >>> 4) & 0x3;
456 	}
457 
458 	/**
459 	 * Returns whether this entry should be skipped from the working tree.
460 	 *
461 	 * @return true if this entry should be skipepd.
462 	 */
463 	public boolean isSkipWorkTree() {
464 		return (getExtendedFlags() & SKIP_WORKTREE) != 0;
465 	}
466 
467 	/**
468 	 * Returns whether this entry is intent to be added to the Index.
469 	 *
470 	 * @return true if this entry is intent to add.
471 	 */
472 	public boolean isIntentToAdd() {
473 		return (getExtendedFlags() & INTENT_TO_ADD) != 0;
474 	}
475 
476 	/**
477 	 * Returns whether this entry is in the fully-merged stage (0).
478 	 *
479 	 * @return true if this entry is merged
480 	 * @since 2.2
481 	 */
482 	public boolean isMerged() {
483 		return getStage() == STAGE_0;
484 	}
485 
486 	/**
487 	 * Obtain the raw {@link FileMode} bits for this entry.
488 	 *
489 	 * @return mode bits for the entry.
490 	 * @see FileMode#fromBits(int)
491 	 */
492 	public int getRawMode() {
493 		return NB.decodeInt32(info, infoOffset + P_MODE);
494 	}
495 
496 	/**
497 	 * Obtain the {@link FileMode} for this entry.
498 	 *
499 	 * @return the file mode singleton for this entry.
500 	 */
501 	public FileMode getFileMode() {
502 		return FileMode.fromBits(getRawMode());
503 	}
504 
505 	/**
506 	 * Set the file mode for this entry.
507 	 *
508 	 * @param mode
509 	 *            the new mode constant.
510 	 * @throws IllegalArgumentException
511 	 *             If {@code mode} is {@link FileMode#MISSING},
512 	 *             {@link FileMode#TREE}, or any other type code not permitted
513 	 *             in a tree object.
514 	 */
515 	public void setFileMode(final FileMode mode) {
516 		switch (mode.getBits() & FileMode.TYPE_MASK) {
517 		case FileMode.TYPE_MISSING:
518 		case FileMode.TYPE_TREE:
519 			throw new IllegalArgumentException(MessageFormat.format(
520 					JGitText.get().invalidModeForPath, mode, getPathString()));
521 		}
522 		NB.encodeInt32(info, infoOffset + P_MODE, mode.getBits());
523 	}
524 
525 	void setFileMode(int mode) {
526 		NB.encodeInt32(info, infoOffset + P_MODE, mode);
527 	}
528 
529 	/**
530 	 * Get the cached creation time of this file, in milliseconds.
531 	 *
532 	 * @return cached creation time of this file, in milliseconds since the
533 	 *         Java epoch (midnight Jan 1, 1970 UTC).
534 	 */
535 	public long getCreationTime() {
536 		return decodeTS(P_CTIME);
537 	}
538 
539 	/**
540 	 * Set the cached creation time of this file, using milliseconds.
541 	 *
542 	 * @param when
543 	 *            new cached creation time of the file, in milliseconds.
544 	 */
545 	public void setCreationTime(final long when) {
546 		encodeTS(P_CTIME, when);
547 	}
548 
549 	/**
550 	 * Get the cached last modification date of this file, in milliseconds.
551 	 * <p>
552 	 * One of the indicators that the file has been modified by an application
553 	 * changing the working tree is if the last modification time for the file
554 	 * differs from the time stored in this entry.
555 	 *
556 	 * @return last modification time of this file, in milliseconds since the
557 	 *         Java epoch (midnight Jan 1, 1970 UTC).
558 	 */
559 	public long getLastModified() {
560 		return decodeTS(P_MTIME);
561 	}
562 
563 	/**
564 	 * Set the cached last modification date of this file, using milliseconds.
565 	 *
566 	 * @param when
567 	 *            new cached modification date of the file, in milliseconds.
568 	 */
569 	public void setLastModified(final long when) {
570 		encodeTS(P_MTIME, when);
571 	}
572 
573 	/**
574 	 * Get the cached size (mod 4 GB) (in bytes) of this file.
575 	 * <p>
576 	 * One of the indicators that the file has been modified by an application
577 	 * changing the working tree is if the size of the file (in bytes) differs
578 	 * from the size stored in this entry.
579 	 * <p>
580 	 * Note that this is the length of the file in the working directory, which
581 	 * may differ from the size of the decompressed blob if work tree filters
582 	 * are being used, such as LF&lt;-&gt;CRLF conversion.
583 	 * <p>
584 	 * Note also that for very large files, this is the size of the on-disk file
585 	 * truncated to 32 bits, i.e. modulo 4294967296. If that value is larger
586 	 * than 2GB, it will appear negative.
587 	 *
588 	 * @return cached size of the working directory file, in bytes.
589 	 */
590 	public int getLength() {
591 		return NB.decodeInt32(info, infoOffset + P_SIZE);
592 	}
593 
594 	/**
595 	 * Set the cached size (in bytes) of this file.
596 	 *
597 	 * @param sz
598 	 *            new cached size of the file, as bytes. If the file is larger
599 	 *            than 2G, cast it to (int) before calling this method.
600 	 */
601 	public void setLength(final int sz) {
602 		NB.encodeInt32(info, infoOffset + P_SIZE, sz);
603 	}
604 
605 	/**
606 	 * Set the cached size (in bytes) of this file.
607 	 *
608 	 * @param sz
609 	 *            new cached size of the file, as bytes.
610 	 */
611 	public void setLength(final long sz) {
612 		setLength((int) sz);
613 	}
614 
615 	/**
616 	 * Obtain the ObjectId for the entry.
617 	 * <p>
618 	 * Using this method to compare ObjectId values between entries is
619 	 * inefficient as it causes memory allocation.
620 	 *
621 	 * @return object identifier for the entry.
622 	 */
623 	public ObjectId getObjectId() {
624 		return ObjectId.fromRaw(idBuffer(), idOffset());
625 	}
626 
627 	/**
628 	 * Set the ObjectId for the entry.
629 	 *
630 	 * @param id
631 	 *            new object identifier for the entry. May be
632 	 *            {@link ObjectId#zeroId()} to remove the current identifier.
633 	 */
634 	public void setObjectId(final AnyObjectId id) {
635 		id.copyRawTo(idBuffer(), idOffset());
636 	}
637 
638 	/**
639 	 * Set the ObjectId for the entry from the raw binary representation.
640 	 *
641 	 * @param bs
642 	 *            the raw byte buffer to read from. At least 20 bytes after p
643 	 *            must be available within this byte array.
644 	 * @param p
645 	 *            position to read the first byte of data from.
646 	 */
647 	public void setObjectIdFromRaw(final byte[] bs, final int p) {
648 		final int n = Constants.OBJECT_ID_LENGTH;
649 		System.arraycopy(bs, p, idBuffer(), idOffset(), n);
650 	}
651 
652 	/**
653 	 * Get the entry's complete path.
654 	 * <p>
655 	 * This method is not very efficient and is primarily meant for debugging
656 	 * and final output generation. Applications should try to avoid calling it,
657 	 * and if invoked do so only once per interesting entry, where the name is
658 	 * absolutely required for correct function.
659 	 *
660 	 * @return complete path of the entry, from the root of the repository. If
661 	 *         the entry is in a subtree there will be at least one '/' in the
662 	 *         returned string.
663 	 */
664 	public String getPathString() {
665 		return toString(path);
666 	}
667 
668 	/**
669 	 * Get a copy of the entry's raw path bytes.
670 	 *
671 	 * @return raw path bytes.
672 	 * @since 3.4
673 	 */
674 	public byte[] getRawPath() {
675 		return path.clone();
676 	}
677 
678 	/**
679 	 * Use for debugging only !
680 	 */
681 	@SuppressWarnings("nls")
682 	@Override
683 	public String toString() {
684 		return getFileMode() + " " + getLength() + " " + getLastModified()
685 				+ " " + getObjectId() + " " + getStage() + " "
686 				+ getPathString() + "\n";
687 	}
688 
689 	/**
690 	 * Copy the ObjectId and other meta fields from an existing entry.
691 	 * <p>
692 	 * This method copies everything except the path from one entry to another,
693 	 * supporting renaming.
694 	 *
695 	 * @param src
696 	 *            the entry to copy ObjectId and meta fields from.
697 	 */
698 	public void copyMetaData(final DirCacheEntry src) {
699 		copyMetaData(src, false);
700 	}
701 
702 	/**
703 	 * Copy the ObjectId and other meta fields from an existing entry.
704 	 * <p>
705 	 * This method copies everything except the path and possibly stage from one
706 	 * entry to another, supporting renaming.
707 	 *
708 	 * @param src
709 	 *            the entry to copy ObjectId and meta fields from.
710 	 * @param keepStage
711 	 *            if true, the stage attribute will not be copied
712 	 */
713 	void copyMetaData(final DirCacheEntry src, boolean keepStage) {
714 		int origflags = NB.decodeUInt16(info, infoOffset + P_FLAGS);
715 		int newflags = NB.decodeUInt16(src.info, src.infoOffset + P_FLAGS);
716 		System.arraycopy(src.info, src.infoOffset, info, infoOffset, INFO_LEN);
717 		final int pLen = origflags & NAME_MASK;
718 		final int SHIFTED_STAGE_MASK = 0x3 << 12;
719 		final int pStageShifted;
720 		if (keepStage)
721 			pStageShifted = origflags & SHIFTED_STAGE_MASK;
722 		else
723 			pStageShifted = newflags & SHIFTED_STAGE_MASK;
724 		NB.encodeInt16(info, infoOffset + P_FLAGS, pStageShifted | pLen
725 				| (newflags & ~NAME_MASK & ~SHIFTED_STAGE_MASK));
726 	}
727 
728 	/**
729 	 * @return true if the entry contains extended flags.
730 	 */
731 	boolean isExtended() {
732 		return (info[infoOffset + P_FLAGS] & EXTENDED) != 0;
733 	}
734 
735 	private long decodeTS(final int pIdx) {
736 		final int base = infoOffset + pIdx;
737 		final int sec = NB.decodeInt32(info, base);
738 		final int ms = NB.decodeInt32(info, base + 4) / 1000000;
739 		return 1000L * sec + ms;
740 	}
741 
742 	private void encodeTS(final int pIdx, final long when) {
743 		final int base = infoOffset + pIdx;
744 		NB.encodeInt32(info, base, (int) (when / 1000));
745 		NB.encodeInt32(info, base + 4, ((int) (when % 1000)) * 1000000);
746 	}
747 
748 	private int getExtendedFlags() {
749 		if (isExtended())
750 			return NB.decodeUInt16(info, infoOffset + P_FLAGS2) << 16;
751 		else
752 			return 0;
753 	}
754 
755 	private static void checkPath(byte[] path) {
756 		try {
757 			SystemReader.getInstance().checkPath(path);
758 		} catch (CorruptObjectException e) {
759 			InvalidPathException p = new InvalidPathException(toString(path));
760 			p.initCause(e);
761 			throw p;
762 		}
763 	}
764 
765 	static String toString(final byte[] path) {
766 		return Constants.CHARSET.decode(ByteBuffer.wrap(path)).toString();
767 	}
768 
769 	static int getMaximumInfoLength(boolean extended) {
770 		return extended ? INFO_LEN_EXTENDED : INFO_LEN;
771 	}
772 }