Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a canonical path

Tags:

Does any one know of any Java libraries I could use to generate canonical paths (basically remove back-references).

I need something that will do the following:

Raw Path -> Canonical Path

/../foo/       -> /foo /foo/          -> /foo /../../../     -> / /./foo/./      -> /foo //foo//bar     -> /foo/bar //foo/../bar   -> /bar 

etc...

At the moment I lazily rely on using:

 new File("/", path).getCanonicalPath(); 

But this resolves the path against the actual file system, and is synchronised.

   java.lang.Thread.State: BLOCKED (on object monitor)         at java.io.ExpiringCache.get(ExpiringCache.java:55)         - waiting to lock <0x93a0d180> (a java.io.ExpiringCache)         at java.io.UnixFileSystem.canonicalize(UnixFileSystem.java:137)         at java.io.File.getCanonicalPath(File.java:559) 

The paths that I am canonicalising do not exist on my file system, so just the logic of the method will do me fine, thus not requiring any synchronisation. I'm hoping for a well tested library rather than having to write my own.

like image 264
Joel Avatar asked Apr 21 '10 14:04

Joel


People also ask

How do I get canonical path?

The getCanonicalPath() method is a part of Path class. This function returns the Canonical pathname of the given file object. If the pathname of the file object is Canonical then it simply returns the path of the current file object. The Canonical path is always absolute and unique, the function removes the '.

What is canonical file path?

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 the difference between canonical path and absolute path?

Absolute path defines a path from the root of the file system e.g. C:\\ or D:\\ in Windows and from / in UNIX based operating systems e.g. Linux or Solaris. The canonical path is a little bit tricky because all canonical path is absolute, but vice-versa is not true.

What are Canonical files?

If the File path of the file object is Canonical then it simply returns the File of the current file object. The Canonical File is always absolute and unique, the function removes the '. ' '..' from the path of the File, if present. For Example: if we create a file object using the path as “program.


1 Answers

I think you can use the URI class to do this; e.g. if the path contains no characters that need escaping in a URI path component, you can do this.

String normalized = new URI(path).normalize().getPath(); 

If the path contains (or might contain) characters that need escaping, the multi-argument constructors will escape the path argument, and you can provide null for the other arguments.

Notes:

  1. The above normalizes a file path by treating it as a relative URI. If you want to normalize an entire URI ... including the (optional) scheme, authority, and other components, don't call getPath()!

  2. URI normalization does not involve looking at the file system as File canonicalization does. But the flip side is that normalization behaves differently to canonicalization when there are symbolic links in the path.

like image 77
Stephen C Avatar answered Sep 28 '22 22:09

Stephen C