Assume I have the following java.io.File
and the corresponding java.nio.file.Path
objects:
final String windir = "WINNT";
final String comspec = "cmd.exe";
final File absoluteFile = new File(format("C:\\./foo/../bar/../%s/./././././SYSTEM32/../system/../System32/%s", windir, comspec)).getAbsoluteFile();
final Path absolutePath = absoluteFile.toPath();
Now, I want to determine the canonical path, i. e. remove any .
or ..
path entries, so that the resulting path is C:\WINNT\System32\cmd.exe
.
java.io.File.getCanonicalPath()
is fine, except that it follows symbolic links on Unices which I would like to avoid.
java.nio.file.Path.toRealPath(NOFOLLOW_LINKS)
, on the other hand, returns the canonical path without following symbolic links, but it throws a java.nio.file.NoSuchFileException
.
How can I determine the canonical path of a file
- in a safe way (so that no exceptions are thrown in case the file doesn't exist),
- in a platform-independent way, and
- without following symbolic links?
The only solution I have found so far is falling back to the old java.io
API:
@NonNull Path toCanonicalPath(final @NonNull Path path) throws IOException {
try {
/*
* Fails for nonexistent files.
*/
return path.toRealPath(NOFOLLOW_LINKS);
} catch (final NoSuchFileException ignored) {
/*
* This one is fine except it always follows symbolic links on Unices.
*/
return path.toFile().getCanonicalFile().toPath();
} catch (final FileSystemException ignored) {
/*
* Thrown when there's a file/directory conflict, e. g.
* for a non-existent file "foo/bar", "foo" already
* exists and is a symlink, not a directory. In this
* case, we can't use the File#getCanonicalFile() call.
*/
return path.toAbsolutePath();
}
}
Is there any less ugly approach?