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.

DirCacheEntry.java 24KB

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