Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java7 WatchService - How to detect rename/move of the actual watched directory

I'm using WatchService for synchronization data files with the application workbench. When I rename/move the watched directory I don't get any event nor the WatchKey won't become invalid. I still get events from the renamed directory but as far as I know there no way to find out the actual Path for the WatchKey besides WatchKey.watchable() which however still returns original directory path. I would like to avoid need of locking the watched directory against changes since I want to keep the application as lightweight as possible.

I have experienced this problem with JDK 7u10 on Windows 7

Do you know any workaround for this issue without locking the directory or watching all directories to the root?

UPDATE

On Linux I have observed the same behavior.

So far it seems I have three options now.

1) Rely on user's discipline that he/she won't move the data directories. I don't really like this options since it might lead to undefined behavior.

2) Use more extensive non-standard native library

3) Create hierarchy of watchdogs on superior directories. These would accept only ENTRY_DELETE events since this event (or OVERFLOW) must appear at the moment the actual watched directory is moved or deleted and thus invalid.

like image 760
rjezek Avatar asked Jan 12 '13 13:01

rjezek


1 Answers

My understanding is that renaming a directory will generate file system events on the old and new parent directories, not on the directory that is renamed. According to the answer to Can iNotify tell me where a monitored file is moved?, the OS cannot tell you where something was moved to unless you are monitoring the destination directory. (And besides, in Java 7/8 MOVE events aren't handled by the watch service implementation.)

UPDATE

You could try the jpathwatch project that adds support for (platform specific) extended events using the standard Java7 WatchService APIs.

References:

  • documentation - http://jpathwatch.wordpress.com/
  • javadoc - http://jpathwatch.sourceforge.net/
like image 58
Stephen C Avatar answered Oct 01 '22 11:10

Stephen C