RFR [6883354] File.mkdirs() method doesn't behave well when given /../

Alan Bateman Alan.Bateman at oracle.com
Fri Aug 16 10:40:51 UTC 2013

On 12/08/2013 17:02, Ivan Gerasimov wrote:
> Hello everybody!
> Would you please help review a small change to File.mkdirs() method?
> The current implementation of the method, when past an argument 
> "dir1/../dir2" only tries to create dir2.
> mkdir -p 'dir1/../dir2' command on Linux creates both dir1 and dir2.
> java.nio.file.Files.createDirectories() also creates both dir1 and dir2.
> The proposed fix makes File.mkdirs() method to behave in the same way 
> on all the platforms except for Windows.
> The problem with Windows is that it reports 'dir1/..' as existent even 
> if dir1 does not exist.
> Because of that Files.createDirectories() doesn't work this way on 
> Windows either.
> Proposed fix:
> - makes File.mkdirs() match the behavior of 'mkdir -p' on Linux, 
> Solaris and MacOS, and
> - doesn't change its behavior on Windows.
> http://cr.openjdk.java.net/~igerasim/6883354/0/webrev/
This is old code so we have to be cautious about changing it (and 
understanding why it used the canonical path in the first place). What 
would you think about adding a few tests to cover more types of file 
path, Windows drive-relative and directory-relative paths in particular 
as these are cases where we would need to confident that it doesn't break.

As an aside, Dan Xu and I were chatting recently about just replacing 
most of the java.io.File implementation to just use java.nio.file. This 
specific one is a case where it might be easier to just change the 
method to Files.createDirectories(toPath()).


More information about the core-libs-dev mailing list