WatchService / detecting renames and moves

Johannes.Lichtenberger Johannes.Lichtenberger at uni-konstanz.de
Tue Jan 10 06:40:16 PST 2012


On 01/10/2012 12:33 PM, Alan Bateman wrote:
> On 09/01/2012 19:56, Johannes.Lichtenberger wrote:
>> Hello,
>>
>> I'm using a WatchService to monitor changes in a directory recursively.
>> Now I want to add a simple rename or move-detection. Therefore I
>> probably can use java.nio.file.attribute.BasicFileAttributes.fileKey()
>> but I think the order in which I receive events isn't specified (CREATE
>> =>  DELETE, DELETE =>  CREATE) or is it also possible to get other CREATE
>> / DELETE events between the events originating from a move or
>> rename-operation? That would for sure complicate the situation, but I
>> think that's possible because I haven't read anything which guarantees
>> any order. Furthermore the fileKeys can be reused by the Filesystem,
>> which is also why I'm questioning my approach. Maybe using hashes and
>> the fileKeys... but I don't want to save a new queue of events, which
>> would be a heuristic, because after some time no equal fileKey + hash is
>> encountered I would suggest they are "real" DELETE/CREATE-events. Maybe
>> I should simply switch to JPathWatch as it should definately work on
>> Linux and optionally on Windows, which both seem to support and "export"
>> move-events)?
>>
>> kind regards,
>> Johannes
> Rename or move-in/out events aren't strictly required as the CREATE and
> DELETE events should allow you to keep your view of the directory up to
> the date. The reason that these events weren't included originally is
> because out of concern that they would not be feasible with all
> implementations and also the concern as to how such events would be
> linked when renaming between directories.  That said, it is something
> that we should come back to. As regards the fileKey() method then in
> theory it could be recycled quickly although in practice it should do
> what you want on the platforms where fileKey() returns non-null.

So, is it save to assume that renames or moves, that is maybe a
file/directory has been moved from one watched directory to another
watched directory (I'm watching recursively all sub-directories) come in
pairs of CREATE/DELETE events or DELETE/CREATE events without any other
events between these pairs? For instance can I assume that all I have to
do is to keep one event temporarily and compare the fileKey of the last
and the current event to detect if it's a rename or if the parent path
changes to detect a move?

That said I just want to keep a database (tree-structured) up to date
which allows renames and subtree-moves, which I'm going to visualize
afterwards (for instance the attached screenshot which currently only
visualizes INSERTS/DELETES between snapshots). As it can also visualize
moves and renames/updates of nodes I want to support it within my
directory-watching application in the first place.

kind regards,
Johannes

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 120107_191320_1491.png
Type: image/png
Size: 130869 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/nio-discuss/attachments/20120110/98272e82/120107_191320_1491-0001.png 


More information about the nio-discuss mailing list