google / google/jimfs

WatchService(PollingWatchService): Only uses timestamp to determine if a file has "changed" ignores content.

Open
#483 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.6k
Forks
296
Avg merge
9m
Merged PRs (30d)
7

Description

Currently my tests use a fixed timestamp that never changes during the lifetime of a test. I have some functions like getting a listing of entries for a path and these entries include the last mod timestamp. To make my tests simpler i have replaced the system clock provider with a constant "timestamp". Reasons for this is because i include a directory abstract which captures amongst the file metadata the last mod timestamp. My assertions break for this because MyDirectoryAbstraction.equals incldues everything including the last mod timestamp.

```
final FileSystem fileSystem = Jimfs.newFileSystem(
Configuration.unix()
.toBuilder()
.setFileTimeSource(
new FileTimeSource() {
@Override
public FileTime now() {
return FILE_TIME_NOW; <----- hardcoded constant system time.
}
}
).build()
);
```

Now the design problem and my commentary, but first the cause.

Some quick debugging during a PollingWatchService eventually leads to the "real" code that determines if any events have happened when the `Snapshots` are compared for change. The `Snapshots` only use the before and after last mod timestamps, the content of the files are not included in anyway and there does not appear to be a "dirty" flag.

```
#postChanges

if (key.subscribesTo(ENTRY_MODIFY)) {
for (Map.Entry entry : modifiedTimes.entrySet()) {
Name name = entry.getKey();
FileTime modifiedTime = entry.getValue();

FileTime newModifiedTime = newState.modifiedTimes.get(name);
if (newModifiedTime != null && !modifiedTime.equals(newModifiedTime)) { <-------- PROBLEM
key.post(new Event<>(ENTRY_MODIFY, 1, pathService.createFileName(name)));
changesPosted = true;
}
}
}
```

I believe this is wrong, i think if the file content changes, the file should be dirty and the next poll of the WatchService should report the "updated" file with the new content as changed even if the lastmod did not change. Only a different level, i think if the system clock provider had a low res, it would be possible that file writes could mean a file is changed and of course the last mod did not change, meaning the polling of course does not detect and report a "file modified event".

Maybe its me, but i think a generation counter should be used rather than lastModifiedTime, by Snapshot to determine if a file has changed aka been modified.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.