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: andrew
Backport-of: 5f63f7a742a1071a87ca69463bae6e04a44fe462
  • Loading branch information
Alan Bateman authored and gnu-andrew committed Dec 21, 2022
1 parent a11f38a commit d5e0d44
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
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 @@ -224,10 +225,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 @@ -236,11 +237,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 @@ -283,8 +284,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 @@ -361,10 +361,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 @@ -376,8 +375,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 @@ -396,7 +394,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 d5e0d44

@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.