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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 static org.eclipse.jgit.util.FS.FileStoreAttributes.FALLBACK_FILESTORE_ATTRIBUTES;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.nio.file.attribute.BasicFileAttributes;
  48. import java.time.Duration;
  49. import java.time.Instant;
  50. import java.time.ZoneId;
  51. import java.time.format.DateTimeFormatter;
  52. import java.util.Locale;
  53. import java.util.Objects;
  54. import java.util.concurrent.TimeUnit;
  55. import org.eclipse.jgit.annotations.NonNull;
  56. import org.eclipse.jgit.util.FS;
  57. import org.eclipse.jgit.util.FS.FileStoreAttributes;
  58. import org.slf4j.Logger;
  59. import org.slf4j.LoggerFactory;
  60. /**
  61. * Caches when a file was last read, making it possible to detect future edits.
  62. * <p>
  63. * This object tracks the last modified time of a file. Later during an
  64. * invocation of {@link #isModified(File)} the object will return true if the
  65. * file may have been modified and should be re-read from disk.
  66. * <p>
  67. * A snapshot does not "live update" when the underlying filesystem changes.
  68. * Callers must poll for updates by periodically invoking
  69. * {@link #isModified(File)}.
  70. * <p>
  71. * To work around the "racy git" problem (where a file may be modified multiple
  72. * times within the granularity of the filesystem modification clock) this class
  73. * may return true from isModified(File) if the last modification time of the
  74. * file is less than 3 seconds ago.
  75. */
  76. public class FileSnapshot {
  77. private static final Logger LOG = LoggerFactory
  78. .getLogger(FileSnapshot.class);
  79. /**
  80. * An unknown file size.
  81. *
  82. * This value is used when a comparison needs to happen purely on the lastUpdate.
  83. */
  84. public static final long UNKNOWN_SIZE = -1;
  85. private static final Instant UNKNOWN_TIME = Instant.ofEpochMilli(-1);
  86. private static final Object MISSING_FILEKEY = new Object();
  87. private static final DateTimeFormatter dateFmt = DateTimeFormatter
  88. .ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnnnnn") //$NON-NLS-1$
  89. .withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault());
  90. /**
  91. * A FileSnapshot that is considered to always be modified.
  92. * <p>
  93. * This instance is useful for application code that wants to lazily read a
  94. * file, but only after {@link #isModified(File)} gets invoked. The returned
  95. * snapshot contains only invalid status information.
  96. */
  97. public static final FileSnapshot DIRTY = new FileSnapshot(UNKNOWN_TIME,
  98. UNKNOWN_TIME, UNKNOWN_SIZE, Duration.ZERO, MISSING_FILEKEY);
  99. /**
  100. * A FileSnapshot that is clean if the file does not exist.
  101. * <p>
  102. * This instance is useful if the application wants to consider a missing
  103. * file to be clean. {@link #isModified(File)} will return false if the file
  104. * path does not exist.
  105. */
  106. public static final FileSnapshot MISSING_FILE = new FileSnapshot(
  107. Instant.EPOCH, Instant.EPOCH, 0, Duration.ZERO, MISSING_FILEKEY) {
  108. @Override
  109. public boolean isModified(File path) {
  110. return FS.DETECTED.exists(path);
  111. }
  112. };
  113. /**
  114. * Record a snapshot for a specific file path.
  115. * <p>
  116. * This method should be invoked before the file is accessed.
  117. *
  118. * @param path
  119. * the path to later remember. The path's current status
  120. * information is saved.
  121. * @return the snapshot.
  122. */
  123. public static FileSnapshot save(File path) {
  124. return new FileSnapshot(path);
  125. }
  126. /**
  127. * Record a snapshot for a specific file path without using config file to
  128. * get filesystem timestamp resolution.
  129. * <p>
  130. * This method should be invoked before the file is accessed. It is used by
  131. * FileBasedConfig to avoid endless recursion.
  132. *
  133. * @param path
  134. * the path to later remember. The path's current status
  135. * information is saved.
  136. * @return the snapshot.
  137. */
  138. public static FileSnapshot saveNoConfig(File path) {
  139. return new FileSnapshot(path, false);
  140. }
  141. private static Object getFileKey(BasicFileAttributes fileAttributes) {
  142. Object fileKey = fileAttributes.fileKey();
  143. return fileKey == null ? MISSING_FILEKEY : fileKey;
  144. }
  145. /**
  146. * Record a snapshot for a file for which the last modification time is
  147. * already known.
  148. * <p>
  149. * This method should be invoked before the file is accessed.
  150. * <p>
  151. * Note that this method cannot rely on measuring file timestamp resolution
  152. * to avoid racy git issues caused by finite file timestamp resolution since
  153. * it's unknown in which filesystem the file is located. Hence the worst
  154. * case fallback for timestamp resolution is used.
  155. *
  156. * @param modified
  157. * the last modification time of the file
  158. * @return the snapshot.
  159. * @deprecated use {@link #save(Instant)} instead.
  160. */
  161. @Deprecated
  162. public static FileSnapshot save(long modified) {
  163. final Instant read = Instant.now();
  164. return new FileSnapshot(read, Instant.ofEpochMilli(modified),
  165. UNKNOWN_SIZE, Duration.ZERO, MISSING_FILEKEY);
  166. }
  167. /**
  168. * Record a snapshot for a file for which the last modification time is
  169. * already known.
  170. * <p>
  171. * This method should be invoked before the file is accessed.
  172. * <p>
  173. * Note that this method cannot rely on measuring file timestamp resolution
  174. * to avoid racy git issues caused by finite file timestamp resolution since
  175. * it's unknown in which filesystem the file is located. Hence the worst
  176. * case fallback for timestamp resolution is used.
  177. *
  178. * @param modified
  179. * the last modification time of the file
  180. * @return the snapshot.
  181. */
  182. public static FileSnapshot save(Instant modified) {
  183. final Instant read = Instant.now();
  184. return new FileSnapshot(read, modified, UNKNOWN_SIZE, Duration.ZERO,
  185. MISSING_FILEKEY);
  186. }
  187. /** Last observed modification time of the path. */
  188. private final Instant lastModified;
  189. /** Last wall-clock time the path was read. */
  190. private volatile Instant lastRead;
  191. /** True once {@link #lastRead} is far later than {@link #lastModified}. */
  192. private boolean cannotBeRacilyClean;
  193. /** Underlying file-system size in bytes.
  194. *
  195. * When set to {@link #UNKNOWN_SIZE} the size is not considered for modification checks. */
  196. private final long size;
  197. /** measured FileStore attributes */
  198. private FileStoreAttributes fileStoreAttributeCache;
  199. /**
  200. * Object that uniquely identifies the given file, or {@code
  201. * null} if a file key is not available
  202. */
  203. private final Object fileKey;
  204. private final File file;
  205. /**
  206. * Record a snapshot for a specific file path.
  207. * <p>
  208. * This method should be invoked before the file is accessed.
  209. *
  210. * @param file
  211. * the path to remember meta data for. The path's current status
  212. * information is saved.
  213. */
  214. protected FileSnapshot(File file) {
  215. this(file, true);
  216. }
  217. /**
  218. * Record a snapshot for a specific file path.
  219. * <p>
  220. * This method should be invoked before the file is accessed.
  221. *
  222. * @param file
  223. * the path to remember meta data for. The path's current status
  224. * information is saved.
  225. * @param useConfig
  226. * if {@code true} read filesystem time resolution from
  227. * configuration file otherwise use fallback resolution
  228. */
  229. protected FileSnapshot(File file, boolean useConfig) {
  230. this.file = file;
  231. this.lastRead = Instant.now();
  232. this.fileStoreAttributeCache = useConfig
  233. ? FS.getFileStoreAttributes(file.toPath().getParent())
  234. : FALLBACK_FILESTORE_ATTRIBUTES;
  235. BasicFileAttributes fileAttributes = null;
  236. try {
  237. fileAttributes = FS.DETECTED.fileAttributes(file);
  238. } catch (IOException e) {
  239. this.lastModified = Instant.ofEpochMilli(file.lastModified());
  240. this.size = file.length();
  241. this.fileKey = MISSING_FILEKEY;
  242. return;
  243. }
  244. this.lastModified = fileAttributes.lastModifiedTime().toInstant();
  245. this.size = fileAttributes.size();
  246. this.fileKey = getFileKey(fileAttributes);
  247. if (LOG.isDebugEnabled()) {
  248. LOG.debug("file={}, create new FileSnapshot: lastRead={}, lastModified={}, size={}, fileKey={}", //$NON-NLS-1$
  249. file, dateFmt.format(lastRead),
  250. dateFmt.format(lastModified), Long.valueOf(size),
  251. fileKey.toString());
  252. }
  253. }
  254. private boolean sizeChanged;
  255. private boolean fileKeyChanged;
  256. private boolean lastModifiedChanged;
  257. private boolean wasRacyClean;
  258. private long delta;
  259. private long racyThreshold;
  260. private FileSnapshot(Instant read, Instant modified, long size,
  261. @NonNull Duration fsTimestampResolution, @NonNull Object fileKey) {
  262. this.file = null;
  263. this.lastRead = read;
  264. this.lastModified = modified;
  265. this.fileStoreAttributeCache = new FileStoreAttributes(
  266. fsTimestampResolution);
  267. this.size = size;
  268. this.fileKey = fileKey;
  269. }
  270. /**
  271. * Get time of last snapshot update
  272. *
  273. * @return time of last snapshot update
  274. * @deprecated use {@link #lastModifiedInstant()} instead
  275. */
  276. @Deprecated
  277. public long lastModified() {
  278. return lastModified.toEpochMilli();
  279. }
  280. /**
  281. * Get time of last snapshot update
  282. *
  283. * @return time of last snapshot update
  284. */
  285. public Instant lastModifiedInstant() {
  286. return lastModified;
  287. }
  288. /**
  289. * @return file size in bytes of last snapshot update
  290. */
  291. public long size() {
  292. return size;
  293. }
  294. /**
  295. * Check if the path may have been modified since the snapshot was saved.
  296. *
  297. * @param path
  298. * the path the snapshot describes.
  299. * @return true if the path needs to be read again.
  300. */
  301. public boolean isModified(File path) {
  302. Instant currLastModified;
  303. long currSize;
  304. Object currFileKey;
  305. try {
  306. BasicFileAttributes fileAttributes = FS.DETECTED.fileAttributes(path);
  307. currLastModified = fileAttributes.lastModifiedTime().toInstant();
  308. currSize = fileAttributes.size();
  309. currFileKey = getFileKey(fileAttributes);
  310. } catch (IOException e) {
  311. currLastModified = Instant.ofEpochMilli(path.lastModified());
  312. currSize = path.length();
  313. currFileKey = MISSING_FILEKEY;
  314. }
  315. sizeChanged = isSizeChanged(currSize);
  316. if (sizeChanged) {
  317. return true;
  318. }
  319. fileKeyChanged = isFileKeyChanged(currFileKey);
  320. if (fileKeyChanged) {
  321. return true;
  322. }
  323. lastModifiedChanged = isModified(currLastModified);
  324. if (lastModifiedChanged) {
  325. return true;
  326. }
  327. return false;
  328. }
  329. /**
  330. * Update this snapshot when the content hasn't changed.
  331. * <p>
  332. * If the caller gets true from {@link #isModified(File)}, re-reads the
  333. * content, discovers the content is identical, and
  334. * {@link #equals(FileSnapshot)} is true, it can use
  335. * {@link #setClean(FileSnapshot)} to make a future
  336. * {@link #isModified(File)} return false. The logic goes something like
  337. * this:
  338. *
  339. * <pre>
  340. * if (snapshot.isModified(path)) {
  341. * FileSnapshot other = FileSnapshot.save(path);
  342. * Content newContent = ...;
  343. * if (oldContent.equals(newContent) &amp;&amp; snapshot.equals(other))
  344. * snapshot.setClean(other);
  345. * }
  346. * </pre>
  347. *
  348. * @param other
  349. * the other snapshot.
  350. */
  351. public void setClean(FileSnapshot other) {
  352. final Instant now = other.lastRead;
  353. if (!isRacyClean(now)) {
  354. cannotBeRacilyClean = true;
  355. }
  356. lastRead = now;
  357. }
  358. /**
  359. * Wait until this snapshot's file can't be racy anymore
  360. *
  361. * @throws InterruptedException
  362. * if sleep was interrupted
  363. */
  364. public void waitUntilNotRacy() throws InterruptedException {
  365. long timestampResolution = fileStoreAttributeCache
  366. .getFsTimestampResolution().toNanos();
  367. while (isRacyClean(Instant.now())) {
  368. TimeUnit.NANOSECONDS.sleep(timestampResolution);
  369. }
  370. }
  371. /**
  372. * Compare two snapshots to see if they cache the same information.
  373. *
  374. * @param other
  375. * the other snapshot.
  376. * @return true if the two snapshots share the same information.
  377. */
  378. public boolean equals(FileSnapshot other) {
  379. boolean sizeEq = size == UNKNOWN_SIZE || other.size == UNKNOWN_SIZE || size == other.size;
  380. return lastModified.equals(other.lastModified) && sizeEq
  381. && Objects.equals(fileKey, other.fileKey);
  382. }
  383. /** {@inheritDoc} */
  384. @Override
  385. public boolean equals(Object obj) {
  386. if (this == obj) {
  387. return true;
  388. }
  389. if (obj == null) {
  390. return false;
  391. }
  392. if (!(obj instanceof FileSnapshot)) {
  393. return false;
  394. }
  395. FileSnapshot other = (FileSnapshot) obj;
  396. return equals(other);
  397. }
  398. /** {@inheritDoc} */
  399. @Override
  400. public int hashCode() {
  401. return Objects.hash(lastModified, Long.valueOf(size), fileKey);
  402. }
  403. /**
  404. * @return {@code true} if FileSnapshot.isModified(File) found the file size
  405. * changed
  406. */
  407. boolean wasSizeChanged() {
  408. return sizeChanged;
  409. }
  410. /**
  411. * @return {@code true} if FileSnapshot.isModified(File) found the file key
  412. * changed
  413. */
  414. boolean wasFileKeyChanged() {
  415. return fileKeyChanged;
  416. }
  417. /**
  418. * @return {@code true} if FileSnapshot.isModified(File) found the file's
  419. * lastModified changed
  420. */
  421. boolean wasLastModifiedChanged() {
  422. return lastModifiedChanged;
  423. }
  424. /**
  425. * @return {@code true} if FileSnapshot.isModified(File) detected that
  426. * lastModified is racily clean
  427. */
  428. boolean wasLastModifiedRacilyClean() {
  429. return wasRacyClean;
  430. }
  431. /**
  432. * @return the delta in nanoseconds between lastModified and lastRead during
  433. * last racy check
  434. */
  435. public long lastDelta() {
  436. return delta;
  437. }
  438. /**
  439. * @return the racyLimitNanos threshold in nanoseconds during last racy
  440. * check
  441. */
  442. public long lastRacyThreshold() {
  443. return racyThreshold;
  444. }
  445. /** {@inheritDoc} */
  446. @SuppressWarnings("nls")
  447. @Override
  448. public String toString() {
  449. if (this == DIRTY) {
  450. return "DIRTY";
  451. }
  452. if (this == MISSING_FILE) {
  453. return "MISSING_FILE";
  454. }
  455. return "FileSnapshot[modified: " + dateFmt.format(lastModified)
  456. + ", read: " + dateFmt.format(lastRead) + ", size:" + size
  457. + ", fileKey: " + fileKey + "]";
  458. }
  459. private boolean isRacyClean(Instant read) {
  460. racyThreshold = getEffectiveRacyThreshold();
  461. delta = Duration.between(lastModified, read).toNanos();
  462. wasRacyClean = delta <= racyThreshold;
  463. if (LOG.isDebugEnabled()) {
  464. LOG.debug(
  465. "file={}, isRacyClean={}, read={}, lastModified={}, delta={} ns, racy<={} ns", //$NON-NLS-1$
  466. file, Boolean.valueOf(wasRacyClean), dateFmt.format(read),
  467. dateFmt.format(lastModified), Long.valueOf(delta),
  468. Long.valueOf(racyThreshold));
  469. }
  470. return wasRacyClean;
  471. }
  472. private long getEffectiveRacyThreshold() {
  473. long timestampResolution = fileStoreAttributeCache
  474. .getFsTimestampResolution().toNanos();
  475. long minRacyInterval = fileStoreAttributeCache.getMinimalRacyInterval()
  476. .toNanos();
  477. // add a 30% safety margin
  478. return Math.max(timestampResolution, minRacyInterval) * 13 / 10;
  479. }
  480. private boolean isModified(Instant currLastModified) {
  481. // Any difference indicates the path was modified.
  482. lastModifiedChanged = !lastModified.equals(currLastModified);
  483. if (lastModifiedChanged) {
  484. if (LOG.isDebugEnabled()) {
  485. LOG.debug(
  486. "file={}, lastModified changed from {} to {}", //$NON-NLS-1$
  487. file, dateFmt.format(lastModified),
  488. dateFmt.format(currLastModified));
  489. }
  490. return true;
  491. }
  492. // We have already determined the last read was far enough
  493. // after the last modification that any new modifications
  494. // are certain to change the last modified time.
  495. if (cannotBeRacilyClean) {
  496. LOG.debug("file={}, cannot be racily clean", file); //$NON-NLS-1$
  497. return false;
  498. }
  499. if (!isRacyClean(lastRead)) {
  500. // Our last read should have marked cannotBeRacilyClean,
  501. // but this thread may not have seen the change. The read
  502. // of the volatile field lastRead should have fixed that.
  503. LOG.debug("file={}, is unmodified", file); //$NON-NLS-1$
  504. return false;
  505. }
  506. // We last read this path too close to its last observed
  507. // modification time. We may have missed a modification.
  508. // Scan again, to ensure we still see the same state.
  509. LOG.debug("file={}, is racily clean", file); //$NON-NLS-1$
  510. return true;
  511. }
  512. private boolean isFileKeyChanged(Object currFileKey) {
  513. boolean changed = currFileKey != MISSING_FILEKEY
  514. && !currFileKey.equals(fileKey);
  515. if (changed) {
  516. LOG.debug("file={}, FileKey changed from {} to {}", //$NON-NLS-1$
  517. file, fileKey, currFileKey);
  518. }
  519. return changed;
  520. }
  521. private boolean isSizeChanged(long currSize) {
  522. boolean changed = (currSize != UNKNOWN_SIZE) && (currSize != size);
  523. if (changed) {
  524. LOG.debug("file={}, size changed from {} to {} bytes", //$NON-NLS-1$
  525. file, Long.valueOf(size), Long.valueOf(currSize));
  526. }
  527. return changed;
  528. }
  529. }