Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DirCacheEntry.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. * @throws IllegalArgumentException
  147. * If the path starts or ends with "/", or contains "//" either
  148. * "\0". These sequences are not permitted in a git tree object
  149. * or DirCache file.
  150. */
  151. public DirCacheEntry(final String newPath) {
  152. this(Constants.encode(newPath));
  153. }
  154. /**
  155. * Create an empty entry at the specified stage.
  156. *
  157. * @param newPath
  158. * name of the cache entry.
  159. * @param stage
  160. * the stage index of the new entry.
  161. * @throws IllegalArgumentException
  162. * If the path starts or ends with "/", or contains "//" either
  163. * "\0". These sequences are not permitted in a git tree object
  164. * or DirCache file. Or if {@code stage} is outside of the
  165. * range 0..3, inclusive.
  166. */
  167. public DirCacheEntry(final String newPath, final int stage) {
  168. this(Constants.encode(newPath), stage);
  169. }
  170. /**
  171. * Create an empty entry at stage 0.
  172. *
  173. * @param newPath
  174. * name of the cache entry, in the standard encoding.
  175. * @throws IllegalArgumentException
  176. * If the path starts or ends with "/", or contains "//" either
  177. * "\0". These sequences are not permitted in a git tree object
  178. * or DirCache file.
  179. */
  180. public DirCacheEntry(final byte[] newPath) {
  181. this(newPath, STAGE_0);
  182. }
  183. /**
  184. * Create an empty entry at the specified stage.
  185. *
  186. * @param newPath
  187. * name of the cache entry, in the standard encoding.
  188. * @param stage
  189. * the stage index of the new entry.
  190. * @throws IllegalArgumentException
  191. * If the path starts or ends with "/", or contains "//" either
  192. * "\0". These sequences are not permitted in a git tree object
  193. * or DirCache file. Or if {@code stage} is outside of the
  194. * range 0..3, inclusive.
  195. */
  196. public DirCacheEntry(final byte[] newPath, final int stage) {
  197. if (!isValidPath(newPath))
  198. throw new IllegalArgumentException("Invalid path: "
  199. + toString(newPath));
  200. if (stage < 0 || 3 < stage)
  201. throw new IllegalArgumentException("Invalid stage " + stage
  202. + " for path " + toString(newPath));
  203. info = new byte[INFO_LEN];
  204. infoOffset = 0;
  205. path = newPath;
  206. int flags = ((stage & 0x3) << 12);
  207. if (path.length < NAME_MASK)
  208. flags |= path.length;
  209. else
  210. flags |= NAME_MASK;
  211. NB.encodeInt16(info, infoOffset + P_FLAGS, flags);
  212. }
  213. void write(final OutputStream os) throws IOException {
  214. final int pathLen = path.length;
  215. os.write(info, infoOffset, INFO_LEN);
  216. os.write(path, 0, pathLen);
  217. // Index records are padded out to the next 8 byte alignment
  218. // for historical reasons related to how C Git read the files.
  219. //
  220. final int actLen = INFO_LEN + pathLen;
  221. final int expLen = (actLen + 8) & ~7;
  222. if (actLen != expLen)
  223. os.write(nullpad, 0, expLen - actLen);
  224. }
  225. /**
  226. * Is it possible for this entry to be accidentally assumed clean?
  227. * <p>
  228. * The "racy git" problem happens when a work file can be updated faster
  229. * than the filesystem records file modification timestamps. It is possible
  230. * for an application to edit a work file, update the index, then edit it
  231. * again before the filesystem will give the work file a new modification
  232. * timestamp. This method tests to see if file was written out at the same
  233. * time as the index.
  234. *
  235. * @param smudge_s
  236. * seconds component of the index's last modified time.
  237. * @param smudge_ns
  238. * nanoseconds component of the index's last modified time.
  239. * @return true if extra careful checks should be used.
  240. */
  241. final boolean mightBeRacilyClean(final int smudge_s, final int smudge_ns) {
  242. // If the index has a modification time then it came from disk
  243. // and was not generated from scratch in memory. In such cases
  244. // the entry is 'racily clean' if the entry's cached modification
  245. // time is equal to or later than the index modification time. In
  246. // such cases the work file is too close to the index to tell if
  247. // it is clean or not based on the modification time alone.
  248. //
  249. final int base = infoOffset + P_MTIME;
  250. final int mtime = NB.decodeInt32(info, base);
  251. if (smudge_s < mtime)
  252. return true;
  253. if (smudge_s == mtime)
  254. return smudge_ns <= NB.decodeInt32(info, base + 4) / 1000000;
  255. return false;
  256. }
  257. /**
  258. * Force this entry to no longer match its working tree file.
  259. * <p>
  260. * This avoids the "racy git" problem by making this index entry no longer
  261. * match the file in the working directory. Later git will be forced to
  262. * compare the file content to ensure the file matches the working tree.
  263. */
  264. final void smudgeRacilyClean() {
  265. // We don't use the same approach as C Git to smudge the entry,
  266. // as we cannot compare the working tree file to our SHA-1 and
  267. // thus cannot use the "size to 0" trick without accidentally
  268. // thinking a zero length file is clean.
  269. //
  270. // Instead we force the mtime to the largest possible value, so
  271. // it is certainly after the index's own modification time and
  272. // on a future read will cause mightBeRacilyClean to say "yes!".
  273. // It is also unlikely to match with the working tree file.
  274. //
  275. // I'll see you again before Jan 19, 2038, 03:14:07 AM GMT.
  276. //
  277. final int base = infoOffset + P_MTIME;
  278. Arrays.fill(info, base, base + 8, (byte) 127);
  279. }
  280. final byte[] idBuffer() {
  281. return info;
  282. }
  283. final int idOffset() {
  284. return infoOffset + P_OBJECTID;
  285. }
  286. /**
  287. * Is this entry always thought to be unmodified?
  288. * <p>
  289. * Most entries in the index do not have this flag set. Users may however
  290. * set them on if the file system stat() costs are too high on this working
  291. * directory, such as on NFS or SMB volumes.
  292. *
  293. * @return true if we must assume the entry is unmodified.
  294. */
  295. public boolean isAssumeValid() {
  296. return (info[infoOffset + P_FLAGS] & ASSUME_VALID) != 0;
  297. }
  298. /**
  299. * Set the assume valid flag for this entry,
  300. *
  301. * @param assume
  302. * true to ignore apparent modifications; false to look at last
  303. * modified to detect file modifications.
  304. */
  305. public void setAssumeValid(final boolean assume) {
  306. if (assume)
  307. info[infoOffset + P_FLAGS] |= ASSUME_VALID;
  308. else
  309. info[infoOffset + P_FLAGS] &= ~ASSUME_VALID;
  310. }
  311. /**
  312. * Get the stage of this entry.
  313. * <p>
  314. * Entries have one of 4 possible stages: 0-3.
  315. *
  316. * @return the stage of this entry.
  317. */
  318. public int getStage() {
  319. return (info[infoOffset + P_FLAGS] >>> 4) & 0x3;
  320. }
  321. /**
  322. * Obtain the raw {@link FileMode} bits for this entry.
  323. *
  324. * @return mode bits for the entry.
  325. * @see FileMode#fromBits(int)
  326. */
  327. public int getRawMode() {
  328. return NB.decodeInt32(info, infoOffset + P_MODE);
  329. }
  330. /**
  331. * Obtain the {@link FileMode} for this entry.
  332. *
  333. * @return the file mode singleton for this entry.
  334. */
  335. public FileMode getFileMode() {
  336. return FileMode.fromBits(getRawMode());
  337. }
  338. /**
  339. * Set the file mode for this entry.
  340. *
  341. * @param mode
  342. * the new mode constant.
  343. * @throws IllegalArgumentException
  344. * If {@code mode} is {@link FileMode#MISSING},
  345. * {@link FileMode#TREE}, or any other type code not permitted
  346. * in a tree object.
  347. */
  348. public void setFileMode(final FileMode mode) {
  349. switch (mode.getBits() & FileMode.TYPE_MASK) {
  350. case FileMode.TYPE_MISSING:
  351. case FileMode.TYPE_TREE:
  352. throw new IllegalArgumentException("Invalid mode " + mode
  353. + " for path " + getPathString());
  354. }
  355. NB.encodeInt32(info, infoOffset + P_MODE, mode.getBits());
  356. }
  357. /**
  358. * Get the cached last modification date of this file, in milliseconds.
  359. * <p>
  360. * One of the indicators that the file has been modified by an application
  361. * changing the working tree is if the last modification time for the file
  362. * differs from the time stored in this entry.
  363. *
  364. * @return last modification time of this file, in milliseconds since the
  365. * Java epoch (midnight Jan 1, 1970 UTC).
  366. */
  367. public long getLastModified() {
  368. return decodeTS(P_MTIME);
  369. }
  370. /**
  371. * Set the cached last modification date of this file, using milliseconds.
  372. *
  373. * @param when
  374. * new cached modification date of the file, in milliseconds.
  375. */
  376. public void setLastModified(final long when) {
  377. encodeTS(P_MTIME, when);
  378. }
  379. /**
  380. * Get the cached size (in bytes) of this file.
  381. * <p>
  382. * One of the indicators that the file has been modified by an application
  383. * changing the working tree is if the size of the file (in bytes) differs
  384. * from the size stored in this entry.
  385. * <p>
  386. * Note that this is the length of the file in the working directory, which
  387. * may differ from the size of the decompressed blob if work tree filters
  388. * are being used, such as LF<->CRLF conversion.
  389. *
  390. * @return cached size of the working directory file, in bytes.
  391. */
  392. public int getLength() {
  393. return NB.decodeInt32(info, infoOffset + P_SIZE);
  394. }
  395. /**
  396. * Set the cached size (in bytes) of this file.
  397. *
  398. * @param sz
  399. * new cached size of the file, as bytes.
  400. */
  401. public void setLength(final int sz) {
  402. NB.encodeInt32(info, infoOffset + P_SIZE, sz);
  403. }
  404. /**
  405. * Obtain the ObjectId for the entry.
  406. * <p>
  407. * Using this method to compare ObjectId values between entries is
  408. * inefficient as it causes memory allocation.
  409. *
  410. * @return object identifier for the entry.
  411. */
  412. public ObjectId getObjectId() {
  413. return ObjectId.fromRaw(idBuffer(), idOffset());
  414. }
  415. /**
  416. * Set the ObjectId for the entry.
  417. *
  418. * @param id
  419. * new object identifier for the entry. May be
  420. * {@link ObjectId#zeroId()} to remove the current identifier.
  421. */
  422. public void setObjectId(final AnyObjectId id) {
  423. id.copyRawTo(idBuffer(), idOffset());
  424. }
  425. /**
  426. * Set the ObjectId for the entry from the raw binary representation.
  427. *
  428. * @param bs
  429. * the raw byte buffer to read from. At least 20 bytes after p
  430. * must be available within this byte array.
  431. * @param p
  432. * position to read the first byte of data from.
  433. */
  434. public void setObjectIdFromRaw(final byte[] bs, final int p) {
  435. final int n = Constants.OBJECT_ID_LENGTH;
  436. System.arraycopy(bs, p, idBuffer(), idOffset(), n);
  437. }
  438. /**
  439. * Get the entry's complete path.
  440. * <p>
  441. * This method is not very efficient and is primarily meant for debugging
  442. * and final output generation. Applications should try to avoid calling it,
  443. * and if invoked do so only once per interesting entry, where the name is
  444. * absolutely required for correct function.
  445. *
  446. * @return complete path of the entry, from the root of the repository. If
  447. * the entry is in a subtree there will be at least one '/' in the
  448. * returned string.
  449. */
  450. public String getPathString() {
  451. return toString(path);
  452. }
  453. /**
  454. * Copy the ObjectId and other meta fields from an existing entry.
  455. * <p>
  456. * This method copies everything except the path from one entry to another,
  457. * supporting renaming.
  458. *
  459. * @param src
  460. * the entry to copy ObjectId and meta fields from.
  461. */
  462. public void copyMetaData(final DirCacheEntry src) {
  463. final int pLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
  464. System.arraycopy(src.info, src.infoOffset, info, infoOffset, INFO_LEN);
  465. NB.encodeInt16(info, infoOffset + P_FLAGS, pLen
  466. | NB.decodeUInt16(info, infoOffset + P_FLAGS) & ~NAME_MASK);
  467. }
  468. private long decodeTS(final int pIdx) {
  469. final int base = infoOffset + pIdx;
  470. final int sec = NB.decodeInt32(info, base);
  471. final int ms = NB.decodeInt32(info, base + 4) / 1000000;
  472. return 1000L * sec + ms;
  473. }
  474. private void encodeTS(final int pIdx, final long when) {
  475. final int base = infoOffset + pIdx;
  476. NB.encodeInt32(info, base, (int) (when / 1000));
  477. NB.encodeInt32(info, base + 4, ((int) (when % 1000)) * 1000000);
  478. }
  479. private static String toString(final byte[] path) {
  480. return Constants.CHARSET.decode(ByteBuffer.wrap(path)).toString();
  481. }
  482. static boolean isValidPath(final byte[] path) {
  483. if (path.length == 0)
  484. return false; // empty path is not permitted.
  485. boolean componentHasChars = false;
  486. for (final byte c : path) {
  487. switch (c) {
  488. case 0:
  489. return false; // NUL is never allowed within the path.
  490. case '/':
  491. if (componentHasChars)
  492. componentHasChars = false;
  493. else
  494. return false;
  495. break;
  496. default:
  497. componentHasChars = true;
  498. }
  499. }
  500. return componentHasChars;
  501. }
  502. }