chadlwilson opened a new pull request #324:
URL: https://github.com/apache/commons-io/pull/324


   - `FileUtils.openOutputStream` (used by 2.11 implementation) allows this 
(per the test - which passes)
   - `PathUtils.newOutputStream` and `PathUtils.createParentDirectories` do 
**not** allow this currently (per added tests)
   - The switch from the former to the latter inside `FileUtils` seems to have 
broken `copyToFile`, `copyInputStreamToFile`, `copyURLToFile`, `write`, 
`writeStringToFile`, `writeLines`, `touch` compared to `2.11` when working with 
a target file that would be written inside an existing symlinked dir
   
   As discussed at 
https://github.com/apache/commons-io/pull/319#issuecomment-1025934935 writing 
will currently fail with errors like
   
   ```
   java.nio.file.FileAlreadyExistsException: 
/var/folders/3s/x7hkyjrd0y76fsw7417x7j1h0000gn/T/junit1985630074694639589/FileUtilsTest8511279228626771736/symlinked-dir
        at 
java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:94)
        at 
java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
        at 
java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
        at 
java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:388)
        at java.base/java.nio.file.Files.createDirectory(Files.java:694)
        at 
java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:801)
        at java.base/java.nio.file.Files.createDirectories(Files.java:747)
        at 
org.apache.commons.io.file.PathUtils.createParentDirectories(PathUtils.java:339)
        at 
org.apache.commons.io.file.PathUtils.newOutputStream(PathUtils.java:1066)
        at org.apache.commons.io.FileUtils.newOutputStream(FileUtils.java:2454)
        at 
org.apache.commons.io.FileUtils.writeStringToFile(FileUtils.java:3543)
        at 
org.apache.commons.io.FileUtils.writeStringToFile(FileUtils.java:3528)
        at 
org.apache.commons.io.FileUtilsTest.testWriteStringToFileIntoSymlinkedDir(FileUtilsTest.java:3096)
   ```
   
   I don't know how to address this while still using NIO-based 
`PathUtils.createParentDirectories` and not breaking the contract. Users 
possibly expect `PathUtils.createParentDirectories` to fail if there is already 
a symlink to another directory there as this is existing behaviour since 
`2.9.0`.
   
   The current implementation uses `Files.createDirectories(dir)` underneath 
which does not seem to allow treating the symlinked dir as a valid dir, as when 
it checks directories it uses `LinkOption.NOFOLLOW_LINKS` which is not 
parameterised to allow user to control it. If we want to keep using NIO for 
this, I suspect we would need our own version of `Files.createDirectories`
   
   ```java
       private static void createAndCheckIsDirectory(Path dir,
                                                     FileAttribute<?>... attrs)
           throws IOException
       {
           try {
               createDirectory(dir, attrs);
           } catch (FileAlreadyExistsException x) {
               if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
                   throw x;
           }
       }
   ```
   
   Marking as draft, because it deliberately has failing tests.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to