-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-269g-pwp5-87pp
When running on Java 7 or later, temporary directories are now created Using Java’s NIO API which restricts permissions to owner-only by default.
- Loading branch information
1 parent
b6cfd1e
commit 610155b
Showing
2 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Array; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
import org.junit.Rule; | ||
|
||
|
@@ -229,7 +232,45 @@ public File newFolder() throws IOException { | |
return createTemporaryFolderIn(getRoot()); | ||
} | ||
|
||
private File createTemporaryFolderIn(File parentFolder) throws IOException { | ||
private static File createTemporaryFolderIn(File parentFolder) throws IOException { | ||
try { | ||
return createTemporaryFolderWithNioApi(parentFolder); | ||
} catch (ClassNotFoundException ignore) { | ||
// Fallback for Java 5 and 6 | ||
return createTemporaryFolderWithFileApi(parentFolder); | ||
} catch (InvocationTargetException e) { | ||
Throwable cause = e.getCause(); | ||
if (cause instanceof IOException) { | ||
throw (IOException) cause; | ||
} | ||
if (cause instanceof RuntimeException) { | ||
throw (RuntimeException) cause; | ||
} | ||
IOException exception = new IOException("Failed to create temporary folder in " + parentFolder); | ||
exception.initCause(cause); | ||
throw exception; | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to create temporary folder in " + parentFolder, e); | ||
} | ||
} | ||
|
||
private static File createTemporaryFolderWithNioApi(File parentFolder) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
marcphilipp
Author
Member
|
||
Class<?> filesClass = Class.forName("java.nio.file.Files"); | ||
Object fileAttributeArray = Array.newInstance(Class.forName("java.nio.file.attribute.FileAttribute"), 0); | ||
Class<?> pathClass = Class.forName("java.nio.file.Path"); | ||
Object tempDir; | ||
if (parentFolder != null) { | ||
Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", pathClass, String.class, fileAttributeArray.getClass()); | ||
Object parentPath = File.class.getDeclaredMethod("toPath").invoke(parentFolder); | ||
tempDir = createTempDirectoryMethod.invoke(null, parentPath, TMP_PREFIX, fileAttributeArray); | ||
} else { | ||
Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", String.class, fileAttributeArray.getClass()); | ||
tempDir = createTempDirectoryMethod.invoke(null, TMP_PREFIX, fileAttributeArray); | ||
} | ||
return (File) pathClass.getDeclaredMethod("toFile").invoke(tempDir); | ||
} | ||
|
||
private static File createTemporaryFolderWithFileApi(File parentFolder) throws IOException { | ||
File createdFolder = null; | ||
for (int i = 0; i < TEMP_DIR_ATTEMPTS; ++i) { | ||
// Use createTempFile to get a suitable folder name. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hi @marcphilipp , may I know the reason to use reflection, IMO the direct class and method call has higher performance.