Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-798

DeferredFileOutputStream throws exception when system temp dir is a symlink

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 2.12.0
    • 2.13.0
    • Streams/Writers
    • None

    Description

      We have some code that creates an org.apache.commons.fileupload.FileItem and writes to its OutputStream. This worked well until commons-io version 12 came out, then started to throw a FileAlreadyExistsException. After some investigation, this is what seems to be happening:

      Our FileItem (created by a DiskFileItemFactory) uses a DeferredFileOutputStream as its output stream. DeferredFileOutputStream uses the system temp folder to save the file if it gets too big. When saving the file, it wants to create any parent folders if they don't already exist.

      The problem is, the logic for creating the parent folders seems to have been rewritten for version 2.12.0, and now calls:

      PathUtils.createParentDirectories(outputPath); _(DeferredFileOutputStream.java, line 333)_

      This method, by default, uses a parameter telling it not to follow links. On our servers, the temp folder is, unfortunately, a link. So the method looks at the folder in the file path, and sees the link. Since it does not follow links, it just sees that something other than a directory exists in this path, and throws an Exception.

      The code looks something like this:

      FileItemFactory fileFact = new DiskFileItemFactory(); FileItem file = fileFact.createItem(fieldName, contentType, false, fileName); try (OutputStream out = file.getOutputStream()) {
      <code that writes into out>
      {color}}

       

      And the stack trace:

      java.nio.file.FileAlreadyExistsException: /usr/local/apache-tomcat-base/temp at
       java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:94) at
       java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) at
       java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116) at
       java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:389) at
       java.base/java.nio.file.Files.createDirectory(Files.java:690) at
       java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:797) at
       java.base/java.nio.file.Files.createDirectories(Files.java:743) at
       org.apache.commons.io.file.PathUtils.createParentDirectories(PathUtils.java:383) at
       org.apache.commons.io.file.PathUtils.createParentDirectories(PathUtils.java:367) at
       org.apache.commons.io.output.DeferredFileOutputStream.thresholdReached(DeferredFileOutputStream.java:333) at
       org.apache.commons.io.output.ThresholdingOutputStream.checkThreshold(ThresholdingOutputStream.java:105) at
       org.apache.commons.io.output.ThresholdingOutputStream.write(ThresholdingOutputStream.java:231) at

      Attachments

        Activity

          People

            Unassigned Unassigned
            shaishapira Shai Shapira
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: