Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7057783

The Path retrieved by WatchKey.Watchable() does not change when dir renamed.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 7
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      ava version "1.7.0"
      ava(TM) SE Runtime Environment (build 1.7.0-b146)
      ava HotSpot(TM) Client VM (build 21.0-b16, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      I was trying to monitor a directory and its sub-directories for changes. When a sub-directory is created, I register it with the watcher. When a sub-directory renames, the path of WatchKey.Watchable() remains the old path name. When I try to register the new path with the watcher, the same instance of WatchKey as that of old path is returned. This time WatchKey.Watchable() still represents the old path name. This prevents me from registering watchers for sub-directories of the renamed directory.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile the attached source and run.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      D:\Workspace\FileMonitor\src\filemonftp>java FileMonitor
      Populating file tree
      Started monitoring directories
      -------EVENT-------
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\New Folder
      DIR? :true
      WKEY :sun.nio.fs.WindowsWatchService$WindowsWatchKey@691177
      -------EVENT-------
      TYPE :ENTRY_DELETE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\New Folder
      DIR? :false
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\hello
      DIR? :true
      WKEY :sun.nio.fs.WindowsWatchService$WindowsWatchKey@691177
      -------EVENT-------
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify\hello
      ABSPATH :d:\inotify\hello\New Folder
      DIR? :true

      ACTUAL -
      D:\Workspace\FileMonitor\src\filemonftp>java FileMonitor
      Populating file tree
      Started monitoring directories
      -------EVENT-------
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\New Folder
      DIR? :true
      WKEY :sun.nio.fs.WindowsWatchService$WindowsWatchKey@691177
      -------EVENT-------
      TYPE :ENTRY_DELETE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\New Folder
      DIR? :false
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\hello
      DIR? :true
      WKEY :sun.nio.fs.WindowsWatchService$WindowsWatchKey@691177
      -------EVENT-------
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify\New Folder
      ABSPATH :d:\inotify\New Folder\New Folder
      DIR? :false
      java.nio.file.NoSuchFileException: d:\inotify\New Folder\New Folder
              at sun.nio.fs.WindowsException.translateToIOException(WindowsException.j
              at sun.nio.fs.WindowsException.asIOException(WindowsException.java:106)
              at sun.nio.fs.WindowsWatchService$Poller.implRegister(WindowsWatchServic
              at sun.nio.fs.AbstractPoller.processRequests(AbstractPoller.java:260)
              at sun.nio.fs.WindowsWatchService$Poller.run(WindowsWatchService.java:53
              at java.lang.Thread.run(Thread.java:722)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      D:\Workspace\FileMonitor\src\filemonftp>java FileMonitor
      Populating file tree
      Started monitoring directories
      -------EVENT-------
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\New Folder
      DIR? :true
      WKEY :sun.nio.fs.WindowsWatchService$WindowsWatchKey@691177
      -------EVENT-------
      TYPE :ENTRY_DELETE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\New Folder
      DIR? :false
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify
      ABSPATH :d:\inotify\hello
      DIR? :true
      WKEY :sun.nio.fs.WindowsWatchService$WindowsWatchKey@691177
      -------EVENT-------
      TYPE :ENTRY_CREATE
      PARENT :d:\inotify\New Folder
      ABSPATH :d:\inotify\New Folder\New Folder
      DIR? :false
      java.nio.file.NoSuchFileException: d:\inotify\New Folder\New Folder
              at sun.nio.fs.WindowsException.translateToIOException(WindowsException.j
              at sun.nio.fs.WindowsException.asIOException(WindowsException.java:106)
              at sun.nio.fs.WindowsWatchService$Poller.implRegister(WindowsWatchServic
              at sun.nio.fs.AbstractPoller.processRequests(AbstractPoller.java:260)
              at sun.nio.fs.WindowsWatchService$Poller.run(WindowsWatchService.java:53
              at java.lang.Thread.run(Thread.java:722)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.File;
      import java.io.IOException;
      import java.nio.file.FileVisitResult;
      import java.nio.file.Files;
      import static java.nio.file.LinkOption.*;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.nio.file.FileSystem;
      import java.nio.file.FileSystems;
      import java.nio.file.SimpleFileVisitor;

      import static java.nio.file.StandardWatchEventKinds.*;
      import java.nio.file.WatchEvent;
      import java.nio.file.WatchKey;
      import java.nio.file.WatchService;
      class FileMonitor
      {

      private static WatchService getWatcher()
      {
      try{
      return FileSystems.getDefault().newWatchService();
      }
      catch(IOException e)
      {
      e.printStackTrace();
      return null;
      }
      }

      public static void main(String[] args)
      {
      WatchKey key=null;
      final WatchService watcher = getWatcher();
      Path homedir=null;
      try{



      homedir = Paths.get("d:\\inotify");
      System.out.println("Populating file tree");
      key=homedir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      try{
      Files.walkFileTree(homedir, new SimpleFileVisitor<Path>() {
      @Override
      public FileVisitResult postVisitDirectory(Path dir, IOException e)
      throws IOException
      {
      if (e == null) {
      if (Files.isDirectory(dir, NOFOLLOW_LINKS))
      {

      dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      }

      return FileVisitResult.CONTINUE;
      } else {
      // directory iteration failed
      e.printStackTrace();
      return FileVisitResult.CONTINUE;
      }
      }
      });
      }
      catch (IOException e)
      {
      e.printStackTrace();
      }


      }
      catch(Exception e)
      {
      e.printStackTrace();
      }
      System.out.println("Started monitoring directories");
      try{
      while (true){
      try {
      key = watcher.take();
      } catch (InterruptedException x) {
      return;
      }

      System.out.println("-------EVENT-------");
      for(WatchEvent<?> we:key.pollEvents())
      {

      if (we.kind() == OVERFLOW) {
      continue;
      }

      WatchEvent<Path> wep=(WatchEvent<Path>)we;
      Path path=wep.context();
      Path parent=(Path)key.watchable();
      Path absolute=parent.resolve(path);
      System.out.println("TYPE :"+we.kind().toString());
      System.out.println("PARENT :"+parent);
      System.out.println("ABSPATH :"+absolute);
      if (we.kind() == ENTRY_CREATE)
      {
      System.out.println("DIR? :"+Files.isDirectory(absolute, NOFOLLOW_LINKS));

      //if (Files.isDirectory(absolute, NOFOLLOW_LINKS))
      {
      WatchKey wk=absolute.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      System.out.println("WKEY :"+wk);
      }
      }
      if (we.kind() == ENTRY_DELETE)
      {
      System.out.println("DIR? :"+Files.isDirectory(absolute, NOFOLLOW_LINKS));

      //if (Files.isDirectory(absolute, NOFOLLOW_LINKS))
      /*{
      System.out.println("unregistering for: "+absolute);
      WatchKey wk=absolute.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      wk.cancel();
      }*/
      }

      }
      boolean valid = key.reset();
      if (((Path)key.watchable()).equals(homedir) && !valid) {
      break;
      }
      else if (!valid)
      {
      System.out.println("unregistering for: "+((Path)key.watchable()));
      key.cancel();
      }

      }
      }
      catch (IOException e)
      {
      e.printStackTrace();
      }

      }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: