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 26KB

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