Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

canonical file path in java - optimization problem?

My file structure has a symbolic link to a directory /home/me/myDir -> /some/other/dir. This link gets updated by another process and the notifies my process. Upon notification I attempt to get the new canonical path:

public static String getPath()
{
   File file = new File("/home/me/myDir");
   if(file.exists())
   {
      try
      {
         String canonical = file.getCanonicalPath();
         return canonical;
      }
      catch ...
   }

}

The problem is that after the link is changed (an i have verified it changes) it is taking 3-5 times of calling the above getPath() method for to actually get the new path before that the previous path is returned. The only thing I can think of is that java might be optimizing this method and returning the old path. Any ideas or insight is greatly appreciated.

like image 919
brad12s Avatar asked Sep 20 '11 01:09

brad12s


People also ask

What is canonical path in Java?

The canonical path is always an absolute and unique path. If String pathname is used to create a file object, it simply returns the pathname. This method first converts this pathname to absolute form if needed. To do that it will invoke the getAbsolutePath() Method and then maps it to its unique form.

What is canonical path example?

The most issues with canonical paths occur when you are passing the name of a dir and not file. For file, if we are providing absolute path that is also the canonical path. But for dir it means omitting the last "/". For example, "/var/tmp/foo" is a canonical path while "/var/tmp/foo/" is not.

What is the difference between absolute path and canonical path in Java?

A canonical path is always an absolute path. Converting from a path to a canonical path makes it absolute (usually tack on the current working directory so e.g. ./file. txt becomes c:/temp/file. txt ).

What are Canonical files?

A canonical document is a standardized representation that a document might assume while it is passing through your webMethods system. A canonical document acts as the intermediary data format between resources.


1 Answers

Try disabling Java's canonicalization cache. This can be done by setting the system properties sun.io.useCanonCaches and sun.io.useCanonPrefixCache to false.

By default, canonical file names are cached for 30 seconds (read from source here).

like image 83
prunge Avatar answered Sep 23 '22 19:09

prunge