8298726: (fs) Change PollingWatchService to record last modified time as FileTime rather than milliseconds
Reviewed-by: bpb, jpai
This commit is contained in:
parent
3ae718725a
commit
5f63f7a742
@ -29,13 +29,13 @@ import java.nio.file.ClosedWatchServiceException;
|
||||
import java.nio.file.DirectoryIteratorException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.NotDirectoryException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
@ -51,6 +51,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private static class CacheEntry {
|
||||
private long lastModified;
|
||||
private FileTime lastModified;
|
||||
private int lastTickCount;
|
||||
|
||||
CacheEntry(long lastModified, int lastTickCount) {
|
||||
CacheEntry(FileTime lastModified, int lastTickCount) {
|
||||
this.lastModified = lastModified;
|
||||
this.lastTickCount = lastTickCount;
|
||||
}
|
||||
@ -232,11 +233,11 @@ class PollingWatchService
|
||||
return lastTickCount;
|
||||
}
|
||||
|
||||
long lastModified() {
|
||||
FileTime lastModified() {
|
||||
return lastModified;
|
||||
}
|
||||
|
||||
void update(long lastModified, int tickCount) {
|
||||
void update(FileTime lastModified, int tickCount) {
|
||||
this.lastModified = lastModified;
|
||||
this.lastTickCount = tickCount;
|
||||
}
|
||||
@ -278,8 +279,7 @@ class PollingWatchService
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
|
||||
for (Path entry: stream) {
|
||||
// don't follow links
|
||||
long lastModified =
|
||||
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
|
||||
FileTime lastModified = Files.getLastModifiedTime(entry, NOFOLLOW_LINKS);
|
||||
entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
|
||||
}
|
||||
} catch (DirectoryIteratorException e) {
|
||||
@ -356,10 +356,9 @@ class PollingWatchService
|
||||
// iterate over all entries in directory
|
||||
try {
|
||||
for (Path entry: stream) {
|
||||
long lastModified = 0L;
|
||||
FileTime lastModified;
|
||||
try {
|
||||
lastModified =
|
||||
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
|
||||
lastModified = Files.getLastModifiedTime(entry, NOFOLLOW_LINKS);
|
||||
} catch (IOException x) {
|
||||
// unable to get attributes of entry. If file has just
|
||||
// been deleted then we'll report it as deleted on the
|
||||
@ -371,8 +370,7 @@ class PollingWatchService
|
||||
CacheEntry e = entries.get(entry.getFileName());
|
||||
if (e == null) {
|
||||
// new file found
|
||||
entries.put(entry.getFileName(),
|
||||
new CacheEntry(lastModified, tickCount));
|
||||
entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
|
||||
|
||||
// queue ENTRY_CREATE if event enabled
|
||||
if (events.contains(StandardWatchEventKinds.ENTRY_CREATE)) {
|
||||
@ -391,7 +389,7 @@ class PollingWatchService
|
||||
}
|
||||
|
||||
// check if file has changed
|
||||
if (e.lastModified != lastModified) {
|
||||
if (!e.lastModified().equals(lastModified)) {
|
||||
if (events.contains(StandardWatchEventKinds.ENTRY_MODIFY)) {
|
||||
signalEvent(StandardWatchEventKinds.ENTRY_MODIFY,
|
||||
entry.getFileName());
|
||||
|
Loading…
x
Reference in New Issue
Block a user