Refactor createTempDirectory method to use java.nio.file.Files for secure and atomic temporary directory creation (#1559)

Signed-off-by: hsuk04 <hsuhweek@gmail.com>
This commit is contained in:
hsuk04
2025-05-28 00:54:04 +08:00
committed by GitHub
parent 0cbb4ba60f
commit 4bce9a2342

View File

@@ -12,7 +12,8 @@ package org.springsource.ide.eclipse.commons.frameworks.core.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* @author Steffen Pingel
* @author Leo Dos Santos
@@ -56,13 +57,8 @@ public class FileUtil {
* @throws IOException
*/
public static File createTempDirectory(String name, String suffix) throws IOException {
File tempFolder = File.createTempFile(name, suffix);
if (!tempFolder.delete()) {
throw new IOException("Could not delete temp file: " + tempFolder.getAbsolutePath());
}
if (!tempFolder.mkdirs()) {
throw new IOException("Could not create temp directory: " + tempFolder.getAbsolutePath());
}
String tempDirPrefix = (name != null ? name : "") + (suffix != null ? suffix : "");
File tempFolder = Files.createTempDirectory(tempDirPrefix).toFile();
deleteOnShutdown(tempFolder);
return tempFolder;
}