WebFreak001 / FSWatch

A cross-platform folder & file watching library using win32, inotify or std.file
33 stars 8 forks source link

Double update messages #16

Open dkorpel opened 5 years ago

dkorpel commented 5 years ago

I tried out watching a folder named "test" in Windows 10:

import core.thread: Thread, msecs;
import std.stdio;
import fswatch;

void main(string[] args) {
    FileWatch watcher = FileWatch("test", true);
    while(true) {
        writeln(watcher.getEvents());
        Thread.sleep(500.msecs);
    }
}

I noticed that I receive more messages than needed:

// Saving a file in notepad
[]
[FileChangeEvent(modify, "file.txt", "")]
[]
[FileChangeEvent(modify, "file.txt", "")]
[]
[]
// Saving a new file in notepad
[]
[FileChangeEvent(create, "newfile.txt", "")]
[]
[FileChangeEvent(remove, "newfile.txt", ""), FileChangeEvent(create, "newfile.txt", ""), FileChangeEvent(modify, "newfile.txt", ""), FileChangeEvent(modify, "newfile.txt", "")]
[]
// Creating a file using std.file.write
[]
[FileChangeEvent(create, "hey.txt", "")]
[]
[FileChangeEvent(modify, "hey.txt", "")]
[]

Even if I change the sleep time, it's always first an empty event array and then an array with redundant messages. I don't know if this is just how the Windows API works or if some extra modifications are being done under the hood, but it's weird how they show up only after calling getEvents() twice more.

dkorpel commented 5 years ago

So it seems the double messages are just a quirk of the Windows filesystem: https://stackoverflow.com/questions/14036449/c-winapi-readdirectorychangesw-receiving-double-notifications https://stackoverflow.com/questions/9902758/filesystemwatcher-double-entries

The empty array between the double messages seems to have to do with the queued variable switching between true and false every time you call getEvents. When queued is true, ReadDirectoryChangesW is called and queued is set to false. When queued is false, GetOverlappedResult is called, actual messages are gathered, and queued is set to true. What is the purpose behind the queued variable?

WebFreak001 commented 5 years ago

uh I can't really remember what I wrote there and can't really read what it was supposed to do either