Skip to content

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

Description

@mP1

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<Name, FileTime> 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions