[rt.cpan.org #77261] Win32::ChangeNotify always watches subtree

[email protected] ("Christopher J. Madsen via RT")
Newsgroups perl.libwin32
Message-ID <[email protected]>
Thu May 17 16:28:49 2012: Request 77261 was acted upon.
Transaction: Correspondence added by CJM
       Queue: Win32-IPC
     Subject: Win32::ChangeNotify always watches subtree
   Broken in: (no value)
    Severity: Unimportant
       Owner: Nobody
  Requestors: [email protected]
      Status: new
 Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=77261 >


This turns out to be a problem with the underlying
FindFirstChangeNotification API.  There's nothing I can do about it.

Here's the program I used to test it:

  use 5.010;
  use strict;
  use warnings;
  use Win32::ChangeNotify;

  my $notify = Win32::ChangeNotify->new(".", 0, # no subtree
                                        'FILE_NAME DIR_NAME LAST_WRITE');

  say 'waiting...';

  while ($notify->wait) {
    say "change!";
    $notify->reset;
  }

I ran that script, and then in another console created & removed files
in the watched directory and subdirectories of it.  I sometimes got a
notification when changing files in a subdirectory (although not all the
time).

I verified that it's a Windows bug by running the equivalent C program,
which gave similar results:

  #define WIN32_LEAN_AND_MEAN
  #include <windows.h>
  #include <stdio.h>

  int main(int argc, char* argv[])
  {
    HANDLE cn = FindFirstChangeNotification(".", FALSE,
                                            FILE_NOTIFY_CHANGE_FILE_NAME |
                                            FILE_NOTIFY_CHANGE_DIR_NAME |
                                            FILE_NOTIFY_CHANGE_LAST_WRITE );

    puts("waiting");

    while (WaitForSingleObject(cn, INFINITE) == WAIT_OBJECT_0) {
      puts("change!");
      FindNextChangeNotification(cn);
    }

    return 0;
  } /* end main */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.