WatchService
Alan Bateman
Alan.Bateman at oracle.com
Thu Dec 16 06:48:41 PST 2010
wolfgang.baltes wrote:
>
> Hi,
>
> I have started to use the WatchService and would like to get
> confirmation for some of my observations. At least one of them is not
> in agreement with the documentation.
>
> My system is Windows 7 Ultimate 64bit with JDK7-b121 32bit and using
> Eclipse 3.61 32bit as the development platform. I am writing a piece
> of code that watches one or several directories recursively or not for
> file additions and deletions.
>
> My observations are for a directory tree where all
> directories/subdirectories are being watched. I use Windows Explorer
> to copy files and directory trees into this directory tree and to
> delete them.
>
> 1 - If I delete a file at any level in the tree, then I get the
> ENTRY_DELETE event for the file and an ENTRY_MODIFIED event for its
> parent directory. - This should be normal.
>
> 2 - If I delete a subdirectory that contains files but no further
> subdirectories, then I receive an ENTRY_DELETE event for the
> subdirectory and an ENTRY_MODIFIED event for its parent directory. In
> addition - and only sometimes - I also receieve ENTRY_DELETE events
> for the files within the subdirectory. - I assume that this is normal,
> although I don't understand the random behavior of getting or not
> getting events regarding the files.
>
> 3 - If I try to delete a subdirectory tree - that is a subdirectory
> with nested directories - then Windows Explorer does not let me delete
> the tree. The WatchService seems to block this. I can however, delete
> this tree by starting at the bottom (leaf) directories and delete them
> one at a time. This corresponds essentially to the scenario described
> in 2. - This may be normal, but is certainly not convenient. Is this a
> Windows feature?
>
> 4 - Contrary to the documentation (tutorial on WatchService), the
> WatchKey remains valid (isValid()) after deletion of the associated
> directory. That is, the expression (dir.notExists() && key.isValid()),
> where key is the WatchKey associated with dir is true. The first part
> of the expression confirms that dir is deleted, and the second part
> should be false if the key is automatically cancelled. - I don' think
> this is normal at all!
>
> Thanks for any comments you may have,
>
For item 3, yes, unfortunately if you are watching a directory then
Windows prevents you deleting or moving the directory's parent. It's not
a problem deleting the directory that is being watched, it's the parent
that is the issue. We have a bug tracking it:
http://bugs.sun.com/view_bug.do?bug_id=6972833
but I don't think there is anything we can do because it seems to be a
Windows limitation.
I'm interested if you can create a test case to demonstrate item 4. When
a watched directory is deleted then we get a notification from Windows
(via a completion port) and that causes the key to be invalidated. We
have various test cases for this and I'm not aware of any issues.
-Alan
More information about the nio-dev
mailing list