RFR [6883354] File.mkdirs() method doesn't behave well when given /../
Dan Xu
dan.xu at oracle.com
Fri Aug 16 21:56:19 UTC 2013
On 08/16/2013 03:40 AM, Alan Bateman wrote:
> 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()).
>
> -Alan.
>
>
Hi Ivan,
As you mentioned, the code behaves differently on Windows platform, so
you cannot use the same test for all platforms. Your proposed Mkdir.java
will fail on windows. Thanks!
-Dan
More information about the core-libs-dev
mailing list