Skip to content

Commit

Permalink
8298726: (fs) Change PollingWatchService to record last modified time…
Browse files Browse the repository at this point in the history
… as FileTime rather than milliseconds

Reviewed-by: bpb, jpai
  • Loading branch information
Alan Bateman committed Dec 15, 2022
1 parent 3ae7187 commit 5f63f7a
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/java.base/share/classes/sun/nio/fs/PollingWatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
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;
Expand All @@ -51,6 +51,7 @@
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
Expand Down Expand Up @@ -220,10 +221,10 @@ public Void run() {
* 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;
}
Expand All @@ -232,11 +233,11 @@ int lastTickCount() {
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;
}
Expand Down Expand Up @@ -278,8 +279,7 @@ private class PollingWatchKey extends AbstractWatchKey {
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) {
Expand Down Expand Up @@ -356,10 +356,9 @@ synchronized void poll() {
// 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
Expand All @@ -371,8 +370,7 @@ synchronized void poll() {
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)) {
Expand All @@ -391,7 +389,7 @@ synchronized void poll() {
}

// 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());
Expand Down

1 comment on commit 5f63f7a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.