8298726: (fs) Change PollingWatchService to record last modified time as FileTime rather than milliseconds

Reviewed-by: bpb, jpai
This commit is contained in:
Alan Bateman 2022-12-15 07:14:02 +00:00
parent 3ae718725a
commit 5f63f7a742

View file

@ -29,13 +29,13 @@ import java.nio.file.ClosedWatchServiceException;
import java.nio.file.DirectoryIteratorException; import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream; import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NotDirectoryException; import java.nio.file.NotDirectoryException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds; import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent; import java.nio.file.WatchEvent;
import java.nio.file.WatchKey; import java.nio.file.WatchKey;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
@ -51,6 +51,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
/** /**
* Simple WatchService implementation that uses periodic tasks to poll * Simple WatchService implementation that uses periodic tasks to poll
@ -220,10 +221,10 @@ class PollingWatchService
* Entry in directory cache to record file last-modified-time and tick-count * Entry in directory cache to record file last-modified-time and tick-count
*/ */
private static class CacheEntry { private static class CacheEntry {
private long lastModified; private FileTime lastModified;
private int lastTickCount; private int lastTickCount;
CacheEntry(long lastModified, int lastTickCount) { CacheEntry(FileTime lastModified, int lastTickCount) {
this.lastModified = lastModified; this.lastModified = lastModified;
this.lastTickCount = lastTickCount; this.lastTickCount = lastTickCount;
} }
@ -232,11 +233,11 @@ class PollingWatchService
return lastTickCount; return lastTickCount;
} }
long lastModified() { FileTime lastModified() {
return lastModified; return lastModified;
} }
void update(long lastModified, int tickCount) { void update(FileTime lastModified, int tickCount) {
this.lastModified = lastModified; this.lastModified = lastModified;
this.lastTickCount = tickCount; this.lastTickCount = tickCount;
} }
@ -278,8 +279,7 @@ class PollingWatchService
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry: stream) { for (Path entry: stream) {
// don't follow links // don't follow links
long lastModified = FileTime lastModified = Files.getLastModifiedTime(entry, NOFOLLOW_LINKS);
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount)); entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
} }
} catch (DirectoryIteratorException e) { } catch (DirectoryIteratorException e) {
@ -356,10 +356,9 @@ class PollingWatchService
// iterate over all entries in directory // iterate over all entries in directory
try { try {
for (Path entry: stream) { for (Path entry: stream) {
long lastModified = 0L; FileTime lastModified;
try { try {
lastModified = lastModified = Files.getLastModifiedTime(entry, NOFOLLOW_LINKS);
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
} catch (IOException x) { } catch (IOException x) {
// unable to get attributes of entry. If file has just // unable to get attributes of entry. If file has just
// been deleted then we'll report it as deleted on the // been deleted then we'll report it as deleted on the
@ -371,8 +370,7 @@ class PollingWatchService
CacheEntry e = entries.get(entry.getFileName()); CacheEntry e = entries.get(entry.getFileName());
if (e == null) { if (e == null) {
// new file found // new file found
entries.put(entry.getFileName(), entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
new CacheEntry(lastModified, tickCount));
// queue ENTRY_CREATE if event enabled // queue ENTRY_CREATE if event enabled
if (events.contains(StandardWatchEventKinds.ENTRY_CREATE)) { if (events.contains(StandardWatchEventKinds.ENTRY_CREATE)) {
@ -391,7 +389,7 @@ class PollingWatchService
} }
// check if file has changed // check if file has changed
if (e.lastModified != lastModified) { if (!e.lastModified().equals(lastModified)) {
if (events.contains(StandardWatchEventKinds.ENTRY_MODIFY)) { if (events.contains(StandardWatchEventKinds.ENTRY_MODIFY)) {
signalEvent(StandardWatchEventKinds.ENTRY_MODIFY, signalEvent(StandardWatchEventKinds.ENTRY_MODIFY,
entry.getFileName()); entry.getFileName());