Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DirCacheEntry.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. package org.eclipse.jgit.dircache;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.EOFException;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.OutputStream;
  52. import java.nio.ByteBuffer;
  53. import java.security.MessageDigest;
  54. import java.text.MessageFormat;
  55. import java.util.Arrays;
  56. import org.eclipse.jgit.internal.JGitText;
  57. import org.eclipse.jgit.lib.AnyObjectId;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.lib.FileMode;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.util.IO;
  62. import org.eclipse.jgit.util.MutableInteger;
  63. import org.eclipse.jgit.util.NB;
  64. import org.eclipse.jgit.util.SystemReader;
  65. /**
  66. * A single file (or stage of a file) in a {@link DirCache}.
  67. * <p>
  68. * An entry represents exactly one stage of a file. If a file path is unmerged
  69. * then multiple DirCacheEntry instances may appear for the same path name.
  70. */
  71. public class DirCacheEntry {
  72. private static final byte[] nullpad = new byte[8];
  73. /** The standard (fully merged) stage for an entry. */
  74. public static final int STAGE_0 = 0;
  75. /** The base tree revision for an entry. */
  76. public static final int STAGE_1 = 1;
  77. /** The first tree revision (usually called "ours"). */
  78. public static final int STAGE_2 = 2;
  79. /** The second tree revision (usually called "theirs"). */
  80. public static final int STAGE_3 = 3;
  81. private static final int P_CTIME = 0;
  82. // private static final int P_CTIME_NSEC = 4;
  83. private static final int P_MTIME = 8;
  84. // private static final int P_MTIME_NSEC = 12;
  85. // private static final int P_DEV = 16;
  86. // private static final int P_INO = 20;
  87. private static final int P_MODE = 24;
  88. // private static final int P_UID = 28;
  89. // private static final int P_GID = 32;
  90. private static final int P_SIZE = 36;
  91. private static final int P_OBJECTID = 40;
  92. private static final int P_FLAGS = 60;
  93. private static final int P_FLAGS2 = 62;
  94. /** Mask applied to data in {@link #P_FLAGS} to get the name length. */
  95. private static final int NAME_MASK = 0xfff;
  96. private static final int INTENT_TO_ADD = 0x20000000;
  97. private static final int SKIP_WORKTREE = 0x40000000;
  98. private static final int EXTENDED_FLAGS = (INTENT_TO_ADD | SKIP_WORKTREE);
  99. private static final int INFO_LEN = 62;
  100. private static final int INFO_LEN_EXTENDED = 64;
  101. private static final int EXTENDED = 0x40;
  102. private static final int ASSUME_VALID = 0x80;
  103. /** In-core flag signaling that the entry should be considered as modified. */
  104. private static final int UPDATE_NEEDED = 0x1;
  105. /** (Possibly shared) header information storage. */
  106. private final byte[] info;
  107. /** First location within {@link #info} where our header starts. */
  108. private final int infoOffset;
  109. /** Our encoded path name, from the root of the repository. */
  110. final byte[] path;
  111. /** Flags which are never stored to disk. */
  112. private byte inCoreFlags;
  113. DirCacheEntry(final byte[] sharedInfo, final MutableInteger infoAt,
  114. final InputStream in, final MessageDigest md) throws IOException {
  115. info = sharedInfo;
  116. infoOffset = infoAt.value;
  117. IO.readFully(in, info, infoOffset, INFO_LEN);
  118. final int len;
  119. if (isExtended()) {
  120. len = INFO_LEN_EXTENDED;
  121. IO.readFully(in, info, infoOffset + INFO_LEN, INFO_LEN_EXTENDED - INFO_LEN);
  122. if ((getExtendedFlags() & ~EXTENDED_FLAGS) != 0)
  123. throw new IOException(MessageFormat.format(JGitText.get()
  124. .DIRCUnrecognizedExtendedFlags, String.valueOf(getExtendedFlags())));
  125. } else
  126. len = INFO_LEN;
  127. infoAt.value += len;
  128. md.update(info, infoOffset, len);
  129. int pathLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
  130. int skipped = 0;
  131. if (pathLen < NAME_MASK) {
  132. path = new byte[pathLen];
  133. IO.readFully(in, path, 0, pathLen);
  134. md.update(path, 0, pathLen);
  135. } else {
  136. final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  137. {
  138. final byte[] buf = new byte[NAME_MASK];
  139. IO.readFully(in, buf, 0, NAME_MASK);
  140. tmp.write(buf);
  141. }
  142. for (;;) {
  143. final int c = in.read();
  144. if (c < 0)
  145. throw new EOFException(JGitText.get().shortReadOfBlock);
  146. if (c == 0)
  147. break;
  148. tmp.write(c);
  149. }
  150. path = tmp.toByteArray();
  151. pathLen = path.length;
  152. skipped = 1; // we already skipped 1 '\0' above to break the loop.
  153. md.update(path, 0, pathLen);
  154. md.update((byte) 0);
  155. }
  156. // Index records are padded out to the next 8 byte alignment
  157. // for historical reasons related to how C Git read the files.
  158. //
  159. final int actLen = len + pathLen;
  160. final int expLen = (actLen + 8) & ~7;
  161. final int padLen = expLen - actLen - skipped;
  162. if (padLen > 0) {
  163. IO.skipFully(in, padLen);
  164. md.update(nullpad, 0, padLen);
  165. }
  166. }
  167. /**
  168. * Create an empty entry at stage 0.
  169. *
  170. * @param newPath
  171. * name of the cache entry.
  172. * @throws IllegalArgumentException
  173. * If the path starts or ends with "/", or contains "//" either
  174. * "\0". These sequences are not permitted in a git tree object
  175. * or DirCache file.
  176. */
  177. public DirCacheEntry(final String newPath) {
  178. this(Constants.encode(newPath));
  179. }
  180. /**
  181. * Create an empty entry at the specified stage.
  182. *
  183. * @param newPath
  184. * name of the cache entry.
  185. * @param stage
  186. * the stage index of the new entry.
  187. * @throws IllegalArgumentException
  188. * If the path starts or ends with "/", or contains "//" either
  189. * "\0". These sequences are not permitted in a git tree object
  190. * or DirCache file. Or if {@code stage} is outside of the
  191. * range 0..3, inclusive.
  192. */
  193. public DirCacheEntry(final String newPath, final int stage) {
  194. this(Constants.encode(newPath), stage);
  195. }
  196. /**
  197. * Create an empty entry at stage 0.
  198. *
  199. * @param newPath
  200. * name of the cache entry, in the standard encoding.
  201. * @throws IllegalArgumentException
  202. * If the path starts or ends with "/", or contains "//" either
  203. * "\0". These sequences are not permitted in a git tree object
  204. * or DirCache file.
  205. */
  206. public DirCacheEntry(final byte[] newPath) {
  207. this(newPath, STAGE_0);
  208. }
  209. /**
  210. * Create an empty entry at the specified stage.
  211. *
  212. * @param newPath
  213. * name of the cache entry, in the standard encoding.
  214. * @param stage
  215. * the stage index of the new entry.
  216. * @throws IllegalArgumentException
  217. * If the path starts or ends with "/", or contains "//" either
  218. * "\0". These sequences are not permitted in a git tree object
  219. * or DirCache file. Or if {@code stage} is outside of the
  220. * range 0..3, inclusive.
  221. */
  222. @SuppressWarnings("boxing")
  223. public DirCacheEntry(final byte[] newPath, final int stage) {
  224. if (!isValidPath(newPath))
  225. throw new InvalidPathException(toString(newPath));
  226. if (stage < 0 || 3 < stage)
  227. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidStageForPath
  228. , stage, toString(newPath)));
  229. info = new byte[INFO_LEN];
  230. infoOffset = 0;
  231. path = newPath;
  232. int flags = ((stage & 0x3) << 12);
  233. if (path.length < NAME_MASK)
  234. flags |= path.length;
  235. else
  236. flags |= NAME_MASK;
  237. NB.encodeInt16(info, infoOffset + P_FLAGS, flags);
  238. }
  239. void write(final OutputStream os) throws IOException {
  240. final int len = isExtended() ? INFO_LEN_EXTENDED : INFO_LEN;
  241. final int pathLen = path.length;
  242. os.write(info, infoOffset, len);
  243. os.write(path, 0, pathLen);
  244. // Index records are padded out to the next 8 byte alignment
  245. // for historical reasons related to how C Git read the files.
  246. //
  247. final int actLen = len + pathLen;
  248. final int expLen = (actLen + 8) & ~7;
  249. if (actLen != expLen)
  250. os.write(nullpad, 0, expLen - actLen);
  251. }
  252. /**
  253. * Is it possible for this entry to be accidentally assumed clean?
  254. * <p>
  255. * The "racy git" problem happens when a work file can be updated faster
  256. * than the filesystem records file modification timestamps. It is possible
  257. * for an application to edit a work file, update the index, then edit it
  258. * again before the filesystem will give the work file a new modification
  259. * timestamp. This method tests to see if file was written out at the same
  260. * time as the index.
  261. *
  262. * @param smudge_s
  263. * seconds component of the index's last modified time.
  264. * @param smudge_ns
  265. * nanoseconds component of the index's last modified time.
  266. * @return true if extra careful checks should be used.
  267. */
  268. public final boolean mightBeRacilyClean(final int smudge_s, final int smudge_ns) {
  269. // If the index has a modification time then it came from disk
  270. // and was not generated from scratch in memory. In such cases
  271. // the entry is 'racily clean' if the entry's cached modification
  272. // time is equal to or later than the index modification time. In
  273. // such cases the work file is too close to the index to tell if
  274. // it is clean or not based on the modification time alone.
  275. //
  276. final int base = infoOffset + P_MTIME;
  277. final int mtime = NB.decodeInt32(info, base);
  278. if (smudge_s == mtime)
  279. return smudge_ns <= NB.decodeInt32(info, base + 4);
  280. return false;
  281. }
  282. /**
  283. * Force this entry to no longer match its working tree file.
  284. * <p>
  285. * This avoids the "racy git" problem by making this index entry no longer
  286. * match the file in the working directory. Later git will be forced to
  287. * compare the file content to ensure the file matches the working tree.
  288. */
  289. public final void smudgeRacilyClean() {
  290. // To mark an entry racily clean we set its length to 0 (like native git
  291. // does). Entries which are not racily clean and have zero length can be
  292. // distinguished from racily clean entries by checking P_OBJECTID
  293. // against the SHA1 of empty content. When length is 0 and P_OBJECTID is
  294. // different from SHA1 of empty content we know the entry is marked
  295. // racily clean
  296. final int base = infoOffset + P_SIZE;
  297. Arrays.fill(info, base, base + 4, (byte) 0);
  298. }
  299. /**
  300. * Check whether this entry has been smudged or not
  301. * <p>
  302. * If a blob has length 0 we know his id see {@link Constants#EMPTY_BLOB_ID}. If an entry
  303. * has length 0 and an ID different from the one for empty blob we know this
  304. * entry was smudged.
  305. *
  306. * @return <code>true</code> if the entry is smudged, <code>false</code>
  307. * otherwise
  308. */
  309. public final boolean isSmudged() {
  310. final int base = infoOffset + P_OBJECTID;
  311. return (getLength() == 0) && (Constants.EMPTY_BLOB_ID.compareTo(info, base) != 0);
  312. }
  313. final byte[] idBuffer() {
  314. return info;
  315. }
  316. final int idOffset() {
  317. return infoOffset + P_OBJECTID;
  318. }
  319. /**
  320. * Is this entry always thought to be unmodified?
  321. * <p>
  322. * Most entries in the index do not have this flag set. Users may however
  323. * set them on if the file system stat() costs are too high on this working
  324. * directory, such as on NFS or SMB volumes.
  325. *
  326. * @return true if we must assume the entry is unmodified.
  327. */
  328. public boolean isAssumeValid() {
  329. return (info[infoOffset + P_FLAGS] & ASSUME_VALID) != 0;
  330. }
  331. /**
  332. * Set the assume valid flag for this entry,
  333. *
  334. * @param assume
  335. * true to ignore apparent modifications; false to look at last
  336. * modified to detect file modifications.
  337. */
  338. public void setAssumeValid(final boolean assume) {
  339. if (assume)
  340. info[infoOffset + P_FLAGS] |= ASSUME_VALID;
  341. else
  342. info[infoOffset + P_FLAGS] &= ~ASSUME_VALID;
  343. }
  344. /**
  345. * @return true if this entry should be checked for changes
  346. */
  347. public boolean isUpdateNeeded() {
  348. return (inCoreFlags & UPDATE_NEEDED) != 0;
  349. }
  350. /**
  351. * Set whether this entry must be checked for changes
  352. *
  353. * @param updateNeeded
  354. */
  355. public void setUpdateNeeded(boolean updateNeeded) {
  356. if (updateNeeded)
  357. inCoreFlags |= UPDATE_NEEDED;
  358. else
  359. inCoreFlags &= ~UPDATE_NEEDED;
  360. }
  361. /**
  362. * Get the stage of this entry.
  363. * <p>
  364. * Entries have one of 4 possible stages: 0-3.
  365. *
  366. * @return the stage of this entry.
  367. */
  368. public int getStage() {
  369. return (info[infoOffset + P_FLAGS] >>> 4) & 0x3;
  370. }
  371. /**
  372. * Returns whether this entry should be skipped from the working tree.
  373. *
  374. * @return true if this entry should be skipepd.
  375. */
  376. public boolean isSkipWorkTree() {
  377. return (getExtendedFlags() & SKIP_WORKTREE) != 0;
  378. }
  379. /**
  380. * Returns whether this entry is intent to be added to the Index.
  381. *
  382. * @return true if this entry is intent to add.
  383. */
  384. public boolean isIntentToAdd() {
  385. return (getExtendedFlags() & INTENT_TO_ADD) != 0;
  386. }
  387. /**
  388. * Obtain the raw {@link FileMode} bits for this entry.
  389. *
  390. * @return mode bits for the entry.
  391. * @see FileMode#fromBits(int)
  392. */
  393. public int getRawMode() {
  394. return NB.decodeInt32(info, infoOffset + P_MODE);
  395. }
  396. /**
  397. * Obtain the {@link FileMode} for this entry.
  398. *
  399. * @return the file mode singleton for this entry.
  400. */
  401. public FileMode getFileMode() {
  402. return FileMode.fromBits(getRawMode());
  403. }
  404. /**
  405. * Set the file mode for this entry.
  406. *
  407. * @param mode
  408. * the new mode constant.
  409. * @throws IllegalArgumentException
  410. * If {@code mode} is {@link FileMode#MISSING},
  411. * {@link FileMode#TREE}, or any other type code not permitted
  412. * in a tree object.
  413. */
  414. public void setFileMode(final FileMode mode) {
  415. switch (mode.getBits() & FileMode.TYPE_MASK) {
  416. case FileMode.TYPE_MISSING:
  417. case FileMode.TYPE_TREE:
  418. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidModeForPath
  419. , mode, getPathString()));
  420. }
  421. NB.encodeInt32(info, infoOffset + P_MODE, mode.getBits());
  422. }
  423. /**
  424. * Get the cached creation time of this file, in milliseconds.
  425. *
  426. * @return cached creation time of this file, in milliseconds since the
  427. * Java epoch (midnight Jan 1, 1970 UTC).
  428. */
  429. public long getCreationTime() {
  430. return decodeTS(P_CTIME);
  431. }
  432. /**
  433. * Set the cached creation time of this file, using milliseconds.
  434. *
  435. * @param when
  436. * new cached creation time of the file, in milliseconds.
  437. */
  438. public void setCreationTime(final long when) {
  439. encodeTS(P_CTIME, when);
  440. }
  441. /**
  442. * Get the cached last modification date of this file, in milliseconds.
  443. * <p>
  444. * One of the indicators that the file has been modified by an application
  445. * changing the working tree is if the last modification time for the file
  446. * differs from the time stored in this entry.
  447. *
  448. * @return last modification time of this file, in milliseconds since the
  449. * Java epoch (midnight Jan 1, 1970 UTC).
  450. */
  451. public long getLastModified() {
  452. return decodeTS(P_MTIME);
  453. }
  454. /**
  455. * Set the cached last modification date of this file, using milliseconds.
  456. *
  457. * @param when
  458. * new cached modification date of the file, in milliseconds.
  459. */
  460. public void setLastModified(final long when) {
  461. encodeTS(P_MTIME, when);
  462. }
  463. /**
  464. * Get the cached size (mod 4 GB) (in bytes) of this file.
  465. * <p>
  466. * One of the indicators that the file has been modified by an application
  467. * changing the working tree is if the size of the file (in bytes) differs
  468. * from the size stored in this entry.
  469. * <p>
  470. * Note that this is the length of the file in the working directory, which
  471. * may differ from the size of the decompressed blob if work tree filters
  472. * are being used, such as LF<->CRLF conversion.
  473. * <p>
  474. * Note also that for very large files, this is the size of the on-disk file
  475. * truncated to 32 bits, i.e. modulo 4294967296. If that value is larger
  476. * than 2GB, it will appear negative.
  477. *
  478. * @return cached size of the working directory file, in bytes.
  479. */
  480. public int getLength() {
  481. return NB.decodeInt32(info, infoOffset + P_SIZE);
  482. }
  483. /**
  484. * Set the cached size (in bytes) of this file.
  485. *
  486. * @param sz
  487. * new cached size of the file, as bytes. If the file is larger
  488. * than 2G, cast it to (int) before calling this method.
  489. */
  490. public void setLength(final int sz) {
  491. NB.encodeInt32(info, infoOffset + P_SIZE, sz);
  492. }
  493. /**
  494. * Set the cached size (in bytes) of this file.
  495. *
  496. * @param sz
  497. * new cached size of the file, as bytes.
  498. */
  499. @SuppressWarnings("boxing")
  500. public void setLength(final long sz) {
  501. setLength((int) sz);
  502. }
  503. /**
  504. * Obtain the ObjectId for the entry.
  505. * <p>
  506. * Using this method to compare ObjectId values between entries is
  507. * inefficient as it causes memory allocation.
  508. *
  509. * @return object identifier for the entry.
  510. */
  511. public ObjectId getObjectId() {
  512. return ObjectId.fromRaw(idBuffer(), idOffset());
  513. }
  514. /**
  515. * Set the ObjectId for the entry.
  516. *
  517. * @param id
  518. * new object identifier for the entry. May be
  519. * {@link ObjectId#zeroId()} to remove the current identifier.
  520. */
  521. public void setObjectId(final AnyObjectId id) {
  522. id.copyRawTo(idBuffer(), idOffset());
  523. }
  524. /**
  525. * Set the ObjectId for the entry from the raw binary representation.
  526. *
  527. * @param bs
  528. * the raw byte buffer to read from. At least 20 bytes after p
  529. * must be available within this byte array.
  530. * @param p
  531. * position to read the first byte of data from.
  532. */
  533. public void setObjectIdFromRaw(final byte[] bs, final int p) {
  534. final int n = Constants.OBJECT_ID_LENGTH;
  535. System.arraycopy(bs, p, idBuffer(), idOffset(), n);
  536. }
  537. /**
  538. * Get the entry's complete path.
  539. * <p>
  540. * This method is not very efficient and is primarily meant for debugging
  541. * and final output generation. Applications should try to avoid calling it,
  542. * and if invoked do so only once per interesting entry, where the name is
  543. * absolutely required for correct function.
  544. *
  545. * @return complete path of the entry, from the root of the repository. If
  546. * the entry is in a subtree there will be at least one '/' in the
  547. * returned string.
  548. */
  549. public String getPathString() {
  550. return toString(path);
  551. }
  552. /**
  553. * Use for debugging only !
  554. */
  555. @Override
  556. public String toString() {
  557. return getFileMode() + " " + getLength() + " " + getLastModified()
  558. + " " + getObjectId() + " " + getStage() + " "
  559. + getPathString() + "\n";
  560. }
  561. /**
  562. * Copy the ObjectId and other meta fields from an existing entry.
  563. * <p>
  564. * This method copies everything except the path from one entry to another,
  565. * supporting renaming.
  566. *
  567. * @param src
  568. * the entry to copy ObjectId and meta fields from.
  569. */
  570. public void copyMetaData(final DirCacheEntry src) {
  571. copyMetaData(src, false);
  572. }
  573. /**
  574. * Copy the ObjectId and other meta fields from an existing entry.
  575. * <p>
  576. * This method copies everything except the path and possibly stage from one
  577. * entry to another, supporting renaming.
  578. *
  579. * @param src
  580. * the entry to copy ObjectId and meta fields from.
  581. * @param keepStage
  582. * if true, the stage attribute will not be copied
  583. */
  584. void copyMetaData(final DirCacheEntry src, boolean keepStage) {
  585. int origflags = NB.decodeUInt16(info, infoOffset + P_FLAGS);
  586. int newflags = NB.decodeUInt16(src.info, src.infoOffset + P_FLAGS);
  587. System.arraycopy(src.info, src.infoOffset, info, infoOffset, INFO_LEN);
  588. final int pLen = origflags & NAME_MASK;
  589. final int SHIFTED_STAGE_MASK = 0x3 << 12;
  590. final int pStageShifted;
  591. if (keepStage)
  592. pStageShifted = origflags & SHIFTED_STAGE_MASK;
  593. else
  594. pStageShifted = newflags & SHIFTED_STAGE_MASK;
  595. NB.encodeInt16(info, infoOffset + P_FLAGS, pStageShifted | pLen
  596. | (newflags & ~NAME_MASK & ~SHIFTED_STAGE_MASK));
  597. }
  598. /**
  599. * @return true if the entry contains extended flags.
  600. */
  601. boolean isExtended() {
  602. return (info[infoOffset + P_FLAGS] & EXTENDED) != 0;
  603. }
  604. private long decodeTS(final int pIdx) {
  605. final int base = infoOffset + pIdx;
  606. final int sec = NB.decodeInt32(info, base);
  607. final int ms = NB.decodeInt32(info, base + 4) / 1000000;
  608. return 1000L * sec + ms;
  609. }
  610. private void encodeTS(final int pIdx, final long when) {
  611. final int base = infoOffset + pIdx;
  612. NB.encodeInt32(info, base, (int) (when / 1000));
  613. NB.encodeInt32(info, base + 4, ((int) (when % 1000)) * 1000000);
  614. }
  615. private int getExtendedFlags() {
  616. if (isExtended())
  617. return NB.decodeUInt16(info, infoOffset + P_FLAGS2) << 16;
  618. else
  619. return 0;
  620. }
  621. private static String toString(final byte[] path) {
  622. return Constants.CHARSET.decode(ByteBuffer.wrap(path)).toString();
  623. }
  624. static boolean isValidPath(final byte[] path) {
  625. if (path.length == 0)
  626. return false; // empty path is not permitted.
  627. boolean componentHasChars = false;
  628. for (final byte c : path) {
  629. switch (c) {
  630. case 0:
  631. return false; // NUL is never allowed within the path.
  632. case '/':
  633. if (componentHasChars)
  634. componentHasChars = false;
  635. else
  636. return false;
  637. break;
  638. case '\\':
  639. case ':':
  640. // Tree's never have a backslash in them, not even on Windows
  641. // but even there we regard it as an invalid path
  642. if ("Windows".equals(SystemReader.getInstance().getProperty(
  643. "os.name")))
  644. return false;
  645. //$FALL-THROUGH$
  646. default:
  647. componentHasChars = true;
  648. }
  649. }
  650. return componentHasChars;
  651. }
  652. static int getMaximumInfoLength(boolean extended) {
  653. return extended ? INFO_LEN_EXTENDED : INFO_LEN;
  654. }
  655. }