Skip to content

Commit 5f63f7a

Browse files
author
Alan Bateman
committedDec 15, 2022
8298726: (fs) Change PollingWatchService to record last modified time as FileTime rather than milliseconds
Reviewed-by: bpb, jpai
1 parent 3ae7187 commit 5f63f7a

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed
 

‎src/java.base/share/classes/sun/nio/fs/PollingWatchService.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
import java.nio.file.DirectoryIteratorException;
3030
import java.nio.file.DirectoryStream;
3131
import java.nio.file.Files;
32-
import java.nio.file.LinkOption;
3332
import java.nio.file.NotDirectoryException;
3433
import java.nio.file.Path;
3534
import java.nio.file.StandardWatchEventKinds;
3635
import java.nio.file.WatchEvent;
3736
import java.nio.file.WatchKey;
3837
import java.nio.file.attribute.BasicFileAttributes;
38+
import java.nio.file.attribute.FileTime;
3939
import java.security.AccessController;
4040
import java.security.PrivilegedAction;
4141
import java.security.PrivilegedExceptionAction;
@@ -51,6 +51,7 @@
5151
import java.util.concurrent.ScheduledFuture;
5252
import java.util.concurrent.ThreadFactory;
5353
import java.util.concurrent.TimeUnit;
54+
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
5455

5556
/**
5657
* Simple WatchService implementation that uses periodic tasks to poll
@@ -220,10 +221,10 @@ public Void run() {
220221
* Entry in directory cache to record file last-modified-time and tick-count
221222
*/
222223
private static class CacheEntry {
223-
private long lastModified;
224+
private FileTime lastModified;
224225
private int lastTickCount;
225226

226-
CacheEntry(long lastModified, int lastTickCount) {
227+
CacheEntry(FileTime lastModified, int lastTickCount) {
227228
this.lastModified = lastModified;
228229
this.lastTickCount = lastTickCount;
229230
}
@@ -232,11 +233,11 @@ int lastTickCount() {
232233
return lastTickCount;
233234
}
234235

235-
long lastModified() {
236+
FileTime lastModified() {
236237
return lastModified;
237238
}
238239

239-
void update(long lastModified, int tickCount) {
240+
void update(FileTime lastModified, int tickCount) {
240241
this.lastModified = lastModified;
241242
this.lastTickCount = tickCount;
242243
}
@@ -278,8 +279,7 @@ private class PollingWatchKey extends AbstractWatchKey {
278279
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
279280
for (Path entry: stream) {
280281
// don't follow links
281-
long lastModified =
282-
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
282+
FileTime lastModified = Files.getLastModifiedTime(entry, NOFOLLOW_LINKS);
283283
entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
284284
}
285285
} catch (DirectoryIteratorException e) {
@@ -356,10 +356,9 @@ synchronized void poll() {
356356
// iterate over all entries in directory
357357
try {
358358
for (Path entry: stream) {
359-
long lastModified = 0L;
359+
FileTime lastModified;
360360
try {
361-
lastModified =
362-
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
361+
lastModified = Files.getLastModifiedTime(entry, NOFOLLOW_LINKS);
363362
} catch (IOException x) {
364363
// unable to get attributes of entry. If file has just
365364
// been deleted then we'll report it as deleted on the
@@ -371,8 +370,7 @@ synchronized void poll() {
371370
CacheEntry e = entries.get(entry.getFileName());
372371
if (e == null) {
373372
// new file found
374-
entries.put(entry.getFileName(),
375-
new CacheEntry(lastModified, tickCount));
373+
entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
376374

377375
// queue ENTRY_CREATE if event enabled
378376
if (events.contains(StandardWatchEventKinds.ENTRY_CREATE)) {
@@ -391,7 +389,7 @@ synchronized void poll() {
391389
}
392390

393391
// check if file has changed
394-
if (e.lastModified != lastModified) {
392+
if (!e.lastModified().equals(lastModified)) {
395393
if (events.contains(StandardWatchEventKinds.ENTRY_MODIFY)) {
396394
signalEvent(StandardWatchEventKinds.ENTRY_MODIFY,
397395
entry.getFileName());

0 commit comments

Comments
 (0)
Please sign in to comment.