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.

FileSnapshot.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.text.DateFormat;
  47. import java.text.SimpleDateFormat;
  48. import java.util.Date;
  49. import java.util.Locale;
  50. import org.eclipse.jgit.util.FS;
  51. /**
  52. * Caches when a file was last read, making it possible to detect future edits.
  53. * <p>
  54. * This object tracks the last modified time of a file. Later during an
  55. * invocation of {@link #isModified(File)} the object will return true if the
  56. * file may have been modified and should be re-read from disk.
  57. * <p>
  58. * A snapshot does not "live update" when the underlying filesystem changes.
  59. * Callers must poll for updates by periodically invoking
  60. * {@link #isModified(File)}.
  61. * <p>
  62. * To work around the "racy git" problem (where a file may be modified multiple
  63. * times within the granularity of the filesystem modification clock) this class
  64. * may return true from isModified(File) if the last modification time of the
  65. * file is less than 3 seconds ago.
  66. */
  67. public class FileSnapshot {
  68. /**
  69. * A FileSnapshot that is considered to always be modified.
  70. * <p>
  71. * This instance is useful for application code that wants to lazily read a
  72. * file, but only after {@link #isModified(File)} gets invoked. The returned
  73. * snapshot contains only invalid status information.
  74. */
  75. public static final FileSnapshot DIRTY = new FileSnapshot(-1, -1);
  76. /**
  77. * A FileSnapshot that is clean if the file does not exist.
  78. * <p>
  79. * This instance is useful if the application wants to consider a missing
  80. * file to be clean. {@link #isModified(File)} will return false if the file
  81. * path does not exist.
  82. */
  83. public static final FileSnapshot MISSING_FILE = new FileSnapshot(0, 0) {
  84. @Override
  85. public boolean isModified(File path) {
  86. return FS.DETECTED.exists(path);
  87. }
  88. };
  89. /**
  90. * Record a snapshot for a specific file path.
  91. * <p>
  92. * This method should be invoked before the file is accessed.
  93. *
  94. * @param path
  95. * the path to later remember. The path's current status
  96. * information is saved.
  97. * @return the snapshot.
  98. */
  99. public static FileSnapshot save(File path) {
  100. long read = System.currentTimeMillis();
  101. long modified;
  102. try {
  103. modified = FS.DETECTED.lastModified(path);
  104. } catch (IOException e) {
  105. modified = path.lastModified();
  106. }
  107. return new FileSnapshot(read, modified);
  108. }
  109. /**
  110. * Record a snapshot for a file for which the last modification time is
  111. * already known.
  112. * <p>
  113. * This method should be invoked before the file is accessed.
  114. *
  115. * @param modified
  116. * the last modification time of the file
  117. *
  118. * @return the snapshot.
  119. */
  120. public static FileSnapshot save(long modified) {
  121. final long read = System.currentTimeMillis();
  122. return new FileSnapshot(read, modified);
  123. }
  124. /** Last observed modification time of the path. */
  125. private final long lastModified;
  126. /** Last wall-clock time the path was read. */
  127. private volatile long lastRead;
  128. /** True once {@link #lastRead} is far later than {@link #lastModified}. */
  129. private boolean cannotBeRacilyClean;
  130. private FileSnapshot(long read, long modified) {
  131. this.lastRead = read;
  132. this.lastModified = modified;
  133. this.cannotBeRacilyClean = notRacyClean(read);
  134. }
  135. /**
  136. * @return time of last snapshot update
  137. */
  138. public long lastModified() {
  139. return lastModified;
  140. }
  141. /**
  142. * Check if the path may have been modified since the snapshot was saved.
  143. *
  144. * @param path
  145. * the path the snapshot describes.
  146. * @return true if the path needs to be read again.
  147. */
  148. public boolean isModified(File path) {
  149. long currLastModified;
  150. try {
  151. currLastModified = FS.DETECTED.lastModified(path);
  152. } catch (IOException e) {
  153. currLastModified = path.lastModified();
  154. }
  155. return isModified(currLastModified);
  156. }
  157. /**
  158. * Update this snapshot when the content hasn't changed.
  159. * <p>
  160. * If the caller gets true from {@link #isModified(File)}, re-reads the
  161. * content, discovers the content is identical, and
  162. * {@link #equals(FileSnapshot)} is true, it can use
  163. * {@link #setClean(FileSnapshot)} to make a future
  164. * {@link #isModified(File)} return false. The logic goes something like
  165. * this:
  166. *
  167. * <pre>
  168. * if (snapshot.isModified(path)) {
  169. * FileSnapshot other = FileSnapshot.save(path);
  170. * Content newContent = ...;
  171. * if (oldContent.equals(newContent) &amp;&amp; snapshot.equals(other))
  172. * snapshot.setClean(other);
  173. * }
  174. * </pre>
  175. *
  176. * @param other
  177. * the other snapshot.
  178. */
  179. public void setClean(FileSnapshot other) {
  180. final long now = other.lastRead;
  181. if (notRacyClean(now))
  182. cannotBeRacilyClean = true;
  183. lastRead = now;
  184. }
  185. /**
  186. * Compare two snapshots to see if they cache the same information.
  187. *
  188. * @param other
  189. * the other snapshot.
  190. * @return true if the two snapshots share the same information.
  191. */
  192. public boolean equals(FileSnapshot other) {
  193. return lastModified == other.lastModified;
  194. }
  195. @Override
  196. public boolean equals(Object other) {
  197. if (other instanceof FileSnapshot)
  198. return equals((FileSnapshot) other);
  199. return false;
  200. }
  201. @Override
  202. public int hashCode() {
  203. // This is pretty pointless, but override hashCode to ensure that
  204. // x.hashCode() == y.hashCode() when x.equals(y) is true.
  205. //
  206. return (int) lastModified;
  207. }
  208. @Override
  209. public String toString() {
  210. if (this == DIRTY)
  211. return "DIRTY"; //$NON-NLS-1$
  212. if (this == MISSING_FILE)
  213. return "MISSING_FILE"; //$NON-NLS-1$
  214. DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", //$NON-NLS-1$
  215. Locale.US);
  216. return "FileSnapshot[modified: " + f.format(new Date(lastModified)) //$NON-NLS-1$
  217. + ", read: " + f.format(new Date(lastRead)) + "]"; //$NON-NLS-1$ //$NON-NLS-2$
  218. }
  219. private boolean notRacyClean(final long read) {
  220. // The last modified time granularity of FAT filesystems is 2 seconds.
  221. // Using 2.5 seconds here provides a reasonably high assurance that
  222. // a modification was not missed.
  223. //
  224. return read - lastModified > 2500;
  225. }
  226. private boolean isModified(final long currLastModified) {
  227. // Any difference indicates the path was modified.
  228. //
  229. if (lastModified != currLastModified)
  230. return true;
  231. // We have already determined the last read was far enough
  232. // after the last modification that any new modifications
  233. // are certain to change the last modified time.
  234. //
  235. if (cannotBeRacilyClean)
  236. return false;
  237. if (notRacyClean(lastRead)) {
  238. // Our last read should have marked cannotBeRacilyClean,
  239. // but this thread may not have seen the change. The read
  240. // of the volatile field lastRead should have fixed that.
  241. //
  242. return false;
  243. }
  244. // We last read this path too close to its last observed
  245. // modification time. We may have missed a modification.
  246. // Scan again, to ensure we still see the same state.
  247. //
  248. return true;
  249. }
  250. }