]> source.dussan.org Git - poi.git/commitdiff
Fix #55647 - When creating a temp file, ensure the name isn't already taken
authorNick Burch <nick@apache.org>
Thu, 10 Oct 2013 16:20:07 +0000 (16:20 +0000)
committerNick Burch <nick@apache.org>
Thu, 10 Oct 2013 16:20:07 +0000 (16:20 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1531040 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/util/TempFile.java

index d9489fb8ad5e76f5238b8495f8631fcc42a47607..82578886bfc7125ba2715e613c4ba6783e53c221 100644 (file)
@@ -37,6 +37,7 @@ public final class TempFile {
      * Don't forget to close all files or it might not be possible to delete them.
      */
     public static File createTempFile(String prefix, String suffix) {
+        // Identify and create our temp dir, if needed
         if (dir == null)
         {
             dir = new File(System.getProperty("java.io.tmpdir"), "poifiles");
@@ -45,9 +46,19 @@ public final class TempFile {
                 dir.deleteOnExit();
         }
 
+        // Generate a unique new filename 
         File newFile = new File(dir, prefix + rnd.nextInt() + suffix);
+        if (newFile.exists())
+        {
+           // That name is already taken, try another
+           newFile = createTempFile(prefix, suffix);
+        }
+
+        // Set the delete on exit flag, unless explicitly disabled
         if (System.getProperty("poi.keep.tmp.files") == null)
             newFile.deleteOnExit();
+
+        // All done
         return newFile;
     }
 }