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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.dircache;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.EOFException;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.nio.ByteBuffer;
  51. import java.security.MessageDigest;
  52. import java.util.Arrays;
  53. import org.eclipse.jgit.lib.AnyObjectId;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.FileMode;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.util.IO;
  58. import org.eclipse.jgit.util.NB;
  59. /**
  60. * A single file (or stage of a file) in a {@link DirCache}.
  61. * <p>
  62. * An entry represents exactly one stage of a file. If a file path is unmerged
  63. * then multiple DirCacheEntry instances may appear for the same path name.
  64. */
  65. public class DirCacheEntry {
  66. private static final byte[] nullpad = new byte[8];
  67. /** The standard (fully merged) stage for an entry. */
  68. public static final int STAGE_0 = 0;
  69. /** The base tree revision for an entry. */
  70. public static final int STAGE_1 = 1;
  71. /** The first tree revision (usually called "ours"). */
  72. public static final int STAGE_2 = 2;
  73. /** The second tree revision (usually called "theirs"). */
  74. public static final int STAGE_3 = 3;
  75. // private static final int P_CTIME = 0;
  76. // private static final int P_CTIME_NSEC = 4;
  77. private static final int P_MTIME = 8;
  78. // private static final int P_MTIME_NSEC = 12;
  79. // private static final int P_DEV = 16;
  80. // private static final int P_INO = 20;
  81. private static final int P_MODE = 24;
  82. // private static final int P_UID = 28;
  83. // private static final int P_GID = 32;
  84. private static final int P_SIZE = 36;
  85. private static final int P_OBJECTID = 40;
  86. private static final int P_FLAGS = 60;
  87. /** Mask applied to data in {@link #P_FLAGS} to get the name length. */
  88. private static final int NAME_MASK = 0xfff;
  89. static final int INFO_LEN = 62;
  90. private static final int ASSUME_VALID = 0x80;
  91. /** (Possibly shared) header information storage. */
  92. private final byte[] info;
  93. /** First location within {@link #info} where our header starts. */
  94. private final int infoOffset;
  95. /** Our encoded path name, from the root of the repository. */
  96. final byte[] path;
  97. DirCacheEntry(final byte[] sharedInfo, final int infoAt,
  98. final InputStream in, final MessageDigest md) throws IOException {
  99. info = sharedInfo;
  100. infoOffset = infoAt;
  101. IO.readFully(in, info, infoOffset, INFO_LEN);
  102. md.update(info, infoOffset, INFO_LEN);
  103. int pathLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
  104. int skipped = 0;
  105. if (pathLen < NAME_MASK) {
  106. path = new byte[pathLen];
  107. IO.readFully(in, path, 0, pathLen);
  108. md.update(path, 0, pathLen);
  109. } else {
  110. final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  111. {
  112. final byte[] buf = new byte[NAME_MASK];
  113. IO.readFully(in, buf, 0, NAME_MASK);
  114. tmp.write(buf);
  115. }
  116. for (;;) {
  117. final int c = in.read();
  118. if (c < 0)
  119. throw new EOFException("Short read of block.");
  120. if (c == 0)
  121. break;
  122. tmp.write(c);
  123. }
  124. path = tmp.toByteArray();
  125. pathLen = path.length;
  126. skipped = 1; // we already skipped 1 '\0' above to break the loop.
  127. md.update(path, 0, pathLen);
  128. md.update((byte) 0);
  129. }
  130. // Index records are padded out to the next 8 byte alignment
  131. // for historical reasons related to how C Git read the files.
  132. //
  133. final int actLen = INFO_LEN + pathLen;
  134. final int expLen = (actLen + 8) & ~7;
  135. final int padLen = expLen - actLen - skipped;
  136. if (padLen > 0) {
  137. IO.skipFully(in, padLen);
  138. md.update(nullpad, 0, padLen);
  139. }
  140. }
  141. /**
  142. * Create an empty entry at stage 0.
  143. *
  144. * @param newPath
  145. * name of the cache entry.
  146. */
  147. public DirCacheEntry(final String newPath) {
  148. this(Constants.encode(newPath));
  149. }
  150. /**
  151. * Create an empty entry at the specified stage.
  152. *
  153. * @param newPath
  154. * name of the cache entry.
  155. * @param stage
  156. * the stage index of the new entry.
  157. */
  158. public DirCacheEntry(final String newPath, final int stage) {
  159. this(Constants.encode(newPath), stage);
  160. }
  161. /**
  162. * Create an empty entry at stage 0.
  163. *
  164. * @param newPath
  165. * name of the cache entry, in the standard encoding.
  166. */
  167. public DirCacheEntry(final byte[] newPath) {
  168. this(newPath, STAGE_0);
  169. }
  170. /**
  171. * Create an empty entry at the specified stage.
  172. *
  173. * @param newPath
  174. * name of the cache entry, in the standard encoding.
  175. * @param stage
  176. * the stage index of the new entry.
  177. */
  178. public DirCacheEntry(final byte[] newPath, final int stage) {
  179. info = new byte[INFO_LEN];
  180. infoOffset = 0;
  181. path = newPath;
  182. int flags = ((stage & 0x3) << 12);
  183. if (path.length < NAME_MASK)
  184. flags |= path.length;
  185. else
  186. flags |= NAME_MASK;
  187. NB.encodeInt16(info, infoOffset + P_FLAGS, flags);
  188. }
  189. void write(final OutputStream os) throws IOException {
  190. final int pathLen = path.length;
  191. os.write(info, infoOffset, INFO_LEN);
  192. os.write(path, 0, pathLen);
  193. // Index records are padded out to the next 8 byte alignment
  194. // for historical reasons related to how C Git read the files.
  195. //
  196. final int actLen = INFO_LEN + pathLen;
  197. final int expLen = (actLen + 8) & ~7;
  198. if (actLen != expLen)
  199. os.write(nullpad, 0, expLen - actLen);
  200. }
  201. /**
  202. * Is it possible for this entry to be accidentally assumed clean?
  203. * <p>
  204. * The "racy git" problem happens when a work file can be updated faster
  205. * than the filesystem records file modification timestamps. It is possible
  206. * for an application to edit a work file, update the index, then edit it
  207. * again before the filesystem will give the work file a new modification
  208. * timestamp. This method tests to see if file was written out at the same
  209. * time as the index.
  210. *
  211. * @param smudge_s
  212. * seconds component of the index's last modified time.
  213. * @param smudge_ns
  214. * nanoseconds component of the index's last modified time.
  215. * @return true if extra careful checks should be used.
  216. */
  217. final boolean mightBeRacilyClean(final int smudge_s, final int smudge_ns) {
  218. // If the index has a modification time then it came from disk
  219. // and was not generated from scratch in memory. In such cases
  220. // the entry is 'racily clean' if the entry's cached modification
  221. // time is equal to or later than the index modification time. In
  222. // such cases the work file is too close to the index to tell if
  223. // it is clean or not based on the modification time alone.
  224. //
  225. final int base = infoOffset + P_MTIME;
  226. final int mtime = NB.decodeInt32(info, base);
  227. if (smudge_s < mtime)
  228. return true;
  229. if (smudge_s == mtime)
  230. return smudge_ns <= NB.decodeInt32(info, base + 4) / 1000000;
  231. return false;
  232. }
  233. /**
  234. * Force this entry to no longer match its working tree file.
  235. * <p>
  236. * This avoids the "racy git" problem by making this index entry no longer
  237. * match the file in the working directory. Later git will be forced to
  238. * compare the file content to ensure the file matches the working tree.
  239. */
  240. final void smudgeRacilyClean() {
  241. // We don't use the same approach as C Git to smudge the entry,
  242. // as we cannot compare the working tree file to our SHA-1 and
  243. // thus cannot use the "size to 0" trick without accidentally
  244. // thinking a zero length file is clean.
  245. //
  246. // Instead we force the mtime to the largest possible value, so
  247. // it is certainly after the index's own modification time and
  248. // on a future read will cause mightBeRacilyClean to say "yes!".
  249. // It is also unlikely to match with the working tree file.
  250. //
  251. // I'll see you again before Jan 19, 2038, 03:14:07 AM GMT.
  252. //
  253. final int base = infoOffset + P_MTIME;
  254. Arrays.fill(info, base, base + 8, (byte) 127);
  255. }
  256. final byte[] idBuffer() {
  257. return info;
  258. }
  259. final int idOffset() {
  260. return infoOffset + P_OBJECTID;
  261. }
  262. /**
  263. * Is this entry always thought to be unmodified?
  264. * <p>
  265. * Most entries in the index do not have this flag set. Users may however
  266. * set them on if the file system stat() costs are too high on this working
  267. * directory, such as on NFS or SMB volumes.
  268. *
  269. * @return true if we must assume the entry is unmodified.
  270. */
  271. public boolean isAssumeValid() {
  272. return (info[infoOffset + P_FLAGS] & ASSUME_VALID) != 0;
  273. }
  274. /**
  275. * Set the assume valid flag for this entry,
  276. *
  277. * @param assume
  278. * true to ignore apparent modifications; false to look at last
  279. * modified to detect file modifications.
  280. */
  281. public void setAssumeValid(final boolean assume) {
  282. if (assume)
  283. info[infoOffset + P_FLAGS] |= ASSUME_VALID;
  284. else
  285. info[infoOffset + P_FLAGS] &= ~ASSUME_VALID;
  286. }
  287. /**
  288. * Get the stage of this entry.
  289. * <p>
  290. * Entries have one of 4 possible stages: 0-3.
  291. *
  292. * @return the stage of this entry.
  293. */
  294. public int getStage() {
  295. return (info[infoOffset + P_FLAGS] >>> 4) & 0x3;
  296. }
  297. /**
  298. * Obtain the raw {@link FileMode} bits for this entry.
  299. *
  300. * @return mode bits for the entry.
  301. * @see FileMode#fromBits(int)
  302. */
  303. public int getRawMode() {
  304. return NB.decodeInt32(info, infoOffset + P_MODE);
  305. }
  306. /**
  307. * Obtain the {@link FileMode} for this entry.
  308. *
  309. * @return the file mode singleton for this entry.
  310. */
  311. public FileMode getFileMode() {
  312. return FileMode.fromBits(getRawMode());
  313. }
  314. /**
  315. * Set the file mode for this entry.
  316. *
  317. * @param mode
  318. * the new mode constant.
  319. */
  320. public void setFileMode(final FileMode mode) {
  321. NB.encodeInt32(info, infoOffset + P_MODE, mode.getBits());
  322. }
  323. /**
  324. * Get the cached last modification date of this file, in milliseconds.
  325. * <p>
  326. * One of the indicators that the file has been modified by an application
  327. * changing the working tree is if the last modification time for the file
  328. * differs from the time stored in this entry.
  329. *
  330. * @return last modification time of this file, in milliseconds since the
  331. * Java epoch (midnight Jan 1, 1970 UTC).
  332. */
  333. public long getLastModified() {
  334. return decodeTS(P_MTIME);
  335. }
  336. /**
  337. * Set the cached last modification date of this file, using milliseconds.
  338. *
  339. * @param when
  340. * new cached modification date of the file, in milliseconds.
  341. */
  342. public void setLastModified(final long when) {
  343. encodeTS(P_MTIME, when);
  344. }
  345. /**
  346. * Get the cached size (in bytes) of this file.
  347. * <p>
  348. * One of the indicators that the file has been modified by an application
  349. * changing the working tree is if the size of the file (in bytes) differs
  350. * from the size stored in this entry.
  351. * <p>
  352. * Note that this is the length of the file in the working directory, which
  353. * may differ from the size of the decompressed blob if work tree filters
  354. * are being used, such as LF<->CRLF conversion.
  355. *
  356. * @return cached size of the working directory file, in bytes.
  357. */
  358. public int getLength() {
  359. return NB.decodeInt32(info, infoOffset + P_SIZE);
  360. }
  361. /**
  362. * Set the cached size (in bytes) of this file.
  363. *
  364. * @param sz
  365. * new cached size of the file, as bytes.
  366. */
  367. public void setLength(final int sz) {
  368. NB.encodeInt32(info, infoOffset + P_SIZE, sz);
  369. }
  370. /**
  371. * Obtain the ObjectId for the entry.
  372. * <p>
  373. * Using this method to compare ObjectId values between entries is
  374. * inefficient as it causes memory allocation.
  375. *
  376. * @return object identifier for the entry.
  377. */
  378. public ObjectId getObjectId() {
  379. return ObjectId.fromRaw(idBuffer(), idOffset());
  380. }
  381. /**
  382. * Set the ObjectId for the entry.
  383. *
  384. * @param id
  385. * new object identifier for the entry. May be
  386. * {@link ObjectId#zeroId()} to remove the current identifier.
  387. */
  388. public void setObjectId(final AnyObjectId id) {
  389. id.copyRawTo(idBuffer(), idOffset());
  390. }
  391. /**
  392. * Set the ObjectId for the entry from the raw binary representation.
  393. *
  394. * @param bs
  395. * the raw byte buffer to read from. At least 20 bytes after p
  396. * must be available within this byte array.
  397. * @param p
  398. * position to read the first byte of data from.
  399. */
  400. public void setObjectIdFromRaw(final byte[] bs, final int p) {
  401. final int n = Constants.OBJECT_ID_LENGTH;
  402. System.arraycopy(bs, p, idBuffer(), idOffset(), n);
  403. }
  404. /**
  405. * Get the entry's complete path.
  406. * <p>
  407. * This method is not very efficient and is primarily meant for debugging
  408. * and final output generation. Applications should try to avoid calling it,
  409. * and if invoked do so only once per interesting entry, where the name is
  410. * absolutely required for correct function.
  411. *
  412. * @return complete path of the entry, from the root of the repository. If
  413. * the entry is in a subtree there will be at least one '/' in the
  414. * returned string.
  415. */
  416. public String getPathString() {
  417. return Constants.CHARSET.decode(ByteBuffer.wrap(path)).toString();
  418. }
  419. /**
  420. * Copy the ObjectId and other meta fields from an existing entry.
  421. * <p>
  422. * This method copies everything except the path from one entry to another,
  423. * supporting renaming.
  424. *
  425. * @param src
  426. * the entry to copy ObjectId and meta fields from.
  427. */
  428. public void copyMetaData(final DirCacheEntry src) {
  429. final int pLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
  430. System.arraycopy(src.info, src.infoOffset, info, infoOffset, INFO_LEN);
  431. NB.encodeInt16(info, infoOffset + P_FLAGS, pLen
  432. | NB.decodeUInt16(info, infoOffset + P_FLAGS) & ~NAME_MASK);
  433. }
  434. private long decodeTS(final int pIdx) {
  435. final int base = infoOffset + pIdx;
  436. final int sec = NB.decodeInt32(info, base);
  437. final int ms = NB.decodeInt32(info, base + 4) / 1000000;
  438. return 1000L * sec + ms;
  439. }
  440. private void encodeTS(final int pIdx, final long when) {
  441. final int base = infoOffset + pIdx;
  442. NB.encodeInt32(info, base, (int) (when / 1000));
  443. NB.encodeInt32(info, base + 4, ((int) (when % 1000)) * 1000000);
  444. }
  445. }