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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.storage.file;
  44. import java.io.File;
  45. /**
  46. * Caches when a file was last read, making it possible to detect future edits.
  47. * <p>
  48. * This object tracks the last modified time of a file. Later during an
  49. * invocation of {@link #isModified(File)} the object will return true if the
  50. * file may have been modified and should be re-read from disk.
  51. * <p>
  52. * A snapshot does not "live update" when the underlying filesystem changes.
  53. * Callers must poll for updates by periodically invoking
  54. * {@link #isModified(File)}.
  55. * <p>
  56. * To work around the "racy git" problem (where a file may be modified multiple
  57. * times within the granularity of the filesystem modification clock) this class
  58. * may return true from isModified(File) if the last modification time of the
  59. * file is less than 3 seconds ago.
  60. */
  61. public class FileSnapshot {
  62. /**
  63. * A FileSnapshot that is considered to always be modified.
  64. * <p>
  65. * This instance is useful for application code that wants to lazily read a
  66. * file, but only after {@link #isModified(File)} gets invoked. The returned
  67. * snapshot contains only invalid status information.
  68. */
  69. public static final FileSnapshot DIRTY = new FileSnapshot(-1, -1);
  70. /**
  71. * Record a snapshot for a specific file path.
  72. * <p>
  73. * This method should be invoked before the file is accessed.
  74. *
  75. * @param path
  76. * the path to later remember. The path's current status
  77. * information is saved.
  78. * @return the snapshot.
  79. */
  80. public static FileSnapshot save(File path) {
  81. final long read = System.currentTimeMillis();
  82. final long modified = path.lastModified();
  83. return new FileSnapshot(read, modified);
  84. }
  85. /** Last observed modification time of the path. */
  86. private final long lastModified;
  87. /** Last wall-clock time the path was read. */
  88. private volatile long lastRead;
  89. /** True once {@link #lastRead} is far later than {@link #lastModified}. */
  90. private boolean cannotBeRacilyClean;
  91. private FileSnapshot(long read, long modified) {
  92. this.lastRead = read;
  93. this.lastModified = modified;
  94. this.cannotBeRacilyClean = notRacyClean(read);
  95. }
  96. long lastModified() {
  97. return lastModified;
  98. }
  99. /**
  100. * Check if the path may have been modified since the snapshot was saved.
  101. *
  102. * @param path
  103. * the path the snapshot describes.
  104. * @return true if the path needs to be read again.
  105. */
  106. public boolean isModified(File path) {
  107. return isModified(path.lastModified());
  108. }
  109. /**
  110. * Update this snapshot when the content hasn't changed.
  111. * <p>
  112. * If the caller gets true from {@link #isModified(File)}, re-reads the
  113. * content, discovers the content is identical, and
  114. * {@link #equals(FileSnapshot)} is true, it can use
  115. * {@link #setClean(FileSnapshot)} to make a future
  116. * {@link #isModified(File)} return false. The logic goes something like
  117. * this:
  118. *
  119. * <pre>
  120. * if (snapshot.isModified(path)) {
  121. * FileSnapshot other = FileSnapshot.save(path);
  122. * Content newContent = ...;
  123. * if (oldContent.equals(newContent) &amp;&amp; snapshot.equals(other))
  124. * snapshot.setClean(other);
  125. * }
  126. * </pre>
  127. *
  128. * @param other
  129. * the other snapshot.
  130. */
  131. public void setClean(FileSnapshot other) {
  132. final long now = other.lastRead;
  133. if (notRacyClean(now))
  134. cannotBeRacilyClean = true;
  135. lastRead = now;
  136. }
  137. /**
  138. * Compare two snapshots to see if they cache the same information.
  139. *
  140. * @param other
  141. * the other snapshot.
  142. * @return true if the two snapshots share the same information.
  143. */
  144. public boolean equals(FileSnapshot other) {
  145. return lastModified == other.lastModified;
  146. }
  147. @Override
  148. public boolean equals(Object other) {
  149. if (other instanceof FileSnapshot)
  150. return equals((FileSnapshot) other);
  151. return false;
  152. }
  153. @Override
  154. public int hashCode() {
  155. // This is pretty pointless, but override hashCode to ensure that
  156. // x.hashCode() == y.hashCode() when x.equals(y) is true.
  157. //
  158. return (int) lastModified;
  159. }
  160. private boolean notRacyClean(final long read) {
  161. // The last modified time granularity of FAT filesystems is 2 seconds.
  162. // Using 2.5 seconds here provides a reasonably high assurance that
  163. // a modification was not missed.
  164. //
  165. return read - lastModified > 2500;
  166. }
  167. private boolean isModified(final long currLastModified) {
  168. // Any difference indicates the path was modified.
  169. //
  170. if (lastModified != currLastModified)
  171. return true;
  172. // We have already determined the last read was far enough
  173. // after the last modification that any new modifications
  174. // are certain to change the last modified time.
  175. //
  176. if (cannotBeRacilyClean)
  177. return false;
  178. if (notRacyClean(lastRead)) {
  179. // Our last read should have marked cannotBeRacilyClean,
  180. // but this thread may not have seen the change. The read
  181. // of the volatile field lastRead should have fixed that.
  182. //
  183. return false;
  184. }
  185. // Our lastRead flag may be old, refresh and retry
  186. lastRead = System.currentTimeMillis();
  187. if (notRacyClean(lastRead)) {
  188. return false;
  189. }
  190. // We last read this path too close to its last observed
  191. // modification time. We may have missed a modification.
  192. // Scan again, to ensure we still see the same state.
  193. //
  194. return true;
  195. }
  196. }