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

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