Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
/ jdk20 Public archive

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

Backport-of: 5f63f7a
  • Loading branch information
Tyler Steele committed Dec 21, 2022
1 parent 81933b7 commit 9adc349
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 @@ -222,10 +223,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 @@ -234,11 +235,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 @@ -280,8 +281,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 @@ -358,10 +358,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 @@ -373,8 +372,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 @@ -393,7 +391,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

3 comments on commit 9adc349

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@JesperIRL
Copy link
Member

Choose a reason for hiding this comment

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

/tag jdk-20+29

@openjdk
Copy link

@openjdk openjdk bot commented on 9adc349 Dec 22, 2022

Choose a reason for hiding this comment

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

@JesperIRL The tag jdk-20+29 was successfully created.

Please sign in to comment.