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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.nio.file.attribute.BasicFileAttributes;
  47. import java.text.DateFormat;
  48. import java.text.SimpleDateFormat;
  49. import java.util.Date;
  50. import java.util.Locale;
  51. import java.util.Objects;
  52. import org.eclipse.jgit.util.FS;
  53. /**
  54. * Caches when a file was last read, making it possible to detect future edits.
  55. * <p>
  56. * This object tracks the last modified time of a file. Later during an
  57. * invocation of {@link #isModified(File)} the object will return true if the
  58. * file may have been modified and should be re-read from disk.
  59. * <p>
  60. * A snapshot does not "live update" when the underlying filesystem changes.
  61. * Callers must poll for updates by periodically invoking
  62. * {@link #isModified(File)}.
  63. * <p>
  64. * To work around the "racy git" problem (where a file may be modified multiple
  65. * times within the granularity of the filesystem modification clock) this class
  66. * may return true from isModified(File) if the last modification time of the
  67. * file is less than 3 seconds ago.
  68. */
  69. public class FileSnapshot {
  70. /**
  71. * An unknown file size.
  72. *
  73. * This value is used when a comparison needs to happen purely on the lastUpdate.
  74. */
  75. public static final long UNKNOWN_SIZE = -1;
  76. /**
  77. * A FileSnapshot that is considered to always be modified.
  78. * <p>
  79. * This instance is useful for application code that wants to lazily read a
  80. * file, but only after {@link #isModified(File)} gets invoked. The returned
  81. * snapshot contains only invalid status information.
  82. */
  83. public static final FileSnapshot DIRTY = new FileSnapshot(-1, -1, UNKNOWN_SIZE);
  84. /**
  85. * A FileSnapshot that is clean if the file does not exist.
  86. * <p>
  87. * This instance is useful if the application wants to consider a missing
  88. * file to be clean. {@link #isModified(File)} will return false if the file
  89. * path does not exist.
  90. */
  91. public static final FileSnapshot MISSING_FILE = new FileSnapshot(0, 0, 0) {
  92. @Override
  93. public boolean isModified(File path) {
  94. return FS.DETECTED.exists(path);
  95. }
  96. };
  97. /**
  98. * Record a snapshot for a specific file path.
  99. * <p>
  100. * This method should be invoked before the file is accessed.
  101. *
  102. * @param path
  103. * the path to later remember. The path's current status
  104. * information is saved.
  105. * @return the snapshot.
  106. */
  107. public static FileSnapshot save(File path) {
  108. long read = System.currentTimeMillis();
  109. long modified;
  110. long size;
  111. try {
  112. BasicFileAttributes fileAttributes = FS.DETECTED.fileAttributes(path);
  113. modified = fileAttributes.lastModifiedTime().toMillis();
  114. size = fileAttributes.size();
  115. } catch (IOException e) {
  116. modified = path.lastModified();
  117. size = path.length();
  118. }
  119. return new FileSnapshot(read, modified, size);
  120. }
  121. /**
  122. * Record a snapshot for a file for which the last modification time is
  123. * already known.
  124. * <p>
  125. * This method should be invoked before the file is accessed.
  126. *
  127. * @param modified
  128. * the last modification time of the file
  129. * @return the snapshot.
  130. */
  131. public static FileSnapshot save(long modified) {
  132. final long read = System.currentTimeMillis();
  133. return new FileSnapshot(read, modified, -1);
  134. }
  135. /** Last observed modification time of the path. */
  136. private final long lastModified;
  137. /** Last wall-clock time the path was read. */
  138. private volatile long lastRead;
  139. /** True once {@link #lastRead} is far later than {@link #lastModified}. */
  140. private boolean cannotBeRacilyClean;
  141. /** Underlying file-system size in bytes.
  142. *
  143. * When set to {@link #UNKNOWN_SIZE} the size is not considered for modification checks. */
  144. private final long size;
  145. private FileSnapshot(long read, long modified, long size) {
  146. this.lastRead = read;
  147. this.lastModified = modified;
  148. this.cannotBeRacilyClean = notRacyClean(read);
  149. this.size = size;
  150. }
  151. /**
  152. * Get time of last snapshot update
  153. *
  154. * @return time of last snapshot update
  155. */
  156. public long lastModified() {
  157. return lastModified;
  158. }
  159. /**
  160. * @return file size in bytes of last snapshot update
  161. */
  162. public long size() {
  163. return size;
  164. }
  165. /**
  166. * Check if the path may have been modified since the snapshot was saved.
  167. *
  168. * @param path
  169. * the path the snapshot describes.
  170. * @return true if the path needs to be read again.
  171. */
  172. public boolean isModified(File path) {
  173. long currLastModified;
  174. long currSize;
  175. try {
  176. BasicFileAttributes fileAttributes = FS.DETECTED.fileAttributes(path);
  177. currLastModified = fileAttributes.lastModifiedTime().toMillis();
  178. currSize = fileAttributes.size();
  179. } catch (IOException e) {
  180. currLastModified = path.lastModified();
  181. currSize = path.length();
  182. }
  183. return (currSize != UNKNOWN_SIZE && currSize != size) || isModified(currLastModified);
  184. }
  185. /**
  186. * Update this snapshot when the content hasn't changed.
  187. * <p>
  188. * If the caller gets true from {@link #isModified(File)}, re-reads the
  189. * content, discovers the content is identical, and
  190. * {@link #equals(FileSnapshot)} is true, it can use
  191. * {@link #setClean(FileSnapshot)} to make a future
  192. * {@link #isModified(File)} return false. The logic goes something like
  193. * this:
  194. *
  195. * <pre>
  196. * if (snapshot.isModified(path)) {
  197. * FileSnapshot other = FileSnapshot.save(path);
  198. * Content newContent = ...;
  199. * if (oldContent.equals(newContent) &amp;&amp; snapshot.equals(other))
  200. * snapshot.setClean(other);
  201. * }
  202. * </pre>
  203. *
  204. * @param other
  205. * the other snapshot.
  206. */
  207. public void setClean(FileSnapshot other) {
  208. final long now = other.lastRead;
  209. if (notRacyClean(now))
  210. cannotBeRacilyClean = true;
  211. lastRead = now;
  212. }
  213. /**
  214. * Compare two snapshots to see if they cache the same information.
  215. *
  216. * @param other
  217. * the other snapshot.
  218. * @return true if the two snapshots share the same information.
  219. */
  220. public boolean equals(FileSnapshot other) {
  221. return lastModified == other.lastModified && size == other.size;
  222. }
  223. /** {@inheritDoc} */
  224. @Override
  225. public boolean equals(Object obj) {
  226. if (this == obj) {
  227. return true;
  228. }
  229. if (obj == null) {
  230. return false;
  231. }
  232. if (!(obj instanceof FileSnapshot)) {
  233. return false;
  234. }
  235. FileSnapshot other = (FileSnapshot) obj;
  236. return equals(other);
  237. }
  238. /** {@inheritDoc} */
  239. @Override
  240. public int hashCode() {
  241. return Objects.hash(Long.valueOf(lastModified), Long.valueOf(size));
  242. }
  243. /** {@inheritDoc} */
  244. @SuppressWarnings("nls")
  245. @Override
  246. public String toString() {
  247. if (this == DIRTY) {
  248. return "DIRTY";
  249. }
  250. if (this == MISSING_FILE) {
  251. return "MISSING_FILE";
  252. }
  253. DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS",
  254. Locale.US);
  255. return "FileSnapshot[modified: " + f.format(new Date(lastModified))
  256. + ", read: " + f.format(new Date(lastRead)) + ", size:" + size
  257. + "]";
  258. }
  259. private boolean notRacyClean(long read) {
  260. // The last modified time granularity of FAT filesystems is 2 seconds.
  261. // Using 2.5 seconds here provides a reasonably high assurance that
  262. // a modification was not missed.
  263. //
  264. return read - lastModified > 2500;
  265. }
  266. private boolean isModified(long currLastModified) {
  267. // Any difference indicates the path was modified.
  268. //
  269. if (lastModified != currLastModified)
  270. return true;
  271. // We have already determined the last read was far enough
  272. // after the last modification that any new modifications
  273. // are certain to change the last modified time.
  274. //
  275. if (cannotBeRacilyClean)
  276. return false;
  277. if (notRacyClean(lastRead)) {
  278. // Our last read should have marked cannotBeRacilyClean,
  279. // but this thread may not have seen the change. The read
  280. // of the volatile field lastRead should have fixed that.
  281. //
  282. return false;
  283. }
  284. // We last read this path too close to its last observed
  285. // modification time. We may have missed a modification.
  286. // Scan again, to ensure we still see the same state.
  287. //
  288. return true;
  289. }
  290. }