8025128: File.createTempFile fails if prefix is absolute path
Use only the file name from the supplied prefix for backward compatibility Reviewed-by: alanb, chegar
This commit is contained in:
parent
114ccbddf8
commit
512fbc2a59
@ -1908,10 +1908,18 @@ public class File
|
|||||||
} else {
|
} else {
|
||||||
n = Math.abs(n);
|
n = Math.abs(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use only the file name from the supplied prefix
|
||||||
|
prefix = (new File(prefix)).getName();
|
||||||
|
|
||||||
String name = prefix + Long.toString(n) + suffix;
|
String name = prefix + Long.toString(n) + suffix;
|
||||||
File f = new File(dir, name);
|
File f = new File(dir, name);
|
||||||
if (!name.equals(f.getName()) || f.isInvalid())
|
if (!name.equals(f.getName()) || f.isInvalid()) {
|
||||||
throw new IOException("Unable to create temporary file");
|
if (System.getSecurityManager() != null)
|
||||||
|
throw new IOException("Unable to create temporary file");
|
||||||
|
else
|
||||||
|
throw new IOException("Unable to create temporary file, " + f);
|
||||||
|
}
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 8013827 8011950 8017212
|
* @bug 8013827 8011950 8017212 8025128
|
||||||
* @summary Check whether File.createTempFile can handle special parameters
|
* @summary Check whether File.createTempFile can handle special parameters
|
||||||
* @author Dan Xu
|
* @author Dan Xu
|
||||||
*/
|
*/
|
||||||
@ -33,7 +33,9 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class SpecialTempFile {
|
public class SpecialTempFile {
|
||||||
|
|
||||||
private static void test(String name, String[] prefix, String[] suffix) {
|
private static void test(String name, String[] prefix, String[] suffix,
|
||||||
|
boolean exceptionExpected) throws IOException
|
||||||
|
{
|
||||||
if (prefix == null || suffix == null
|
if (prefix == null || suffix == null
|
||||||
|| prefix.length != suffix.length)
|
|| prefix.length != suffix.length)
|
||||||
{
|
{
|
||||||
@ -41,24 +43,38 @@ public class SpecialTempFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final String exceptionMsg = "Unable to create temporary file";
|
final String exceptionMsg = "Unable to create temporary file";
|
||||||
final String errMsg = "IOException is expected";
|
String[] dirs = { null, "." };
|
||||||
|
|
||||||
for (int i = 0; i < prefix.length; i++) {
|
for (int i = 0; i < prefix.length; i++) {
|
||||||
boolean exceptionThrown = false;
|
boolean exceptionThrown = false;
|
||||||
File f = null;
|
File f = null;
|
||||||
System.out.println("In test " + name
|
|
||||||
+ ", creating temp file with prefix, "
|
for (String dir: dirs) {
|
||||||
+ prefix[i] + ", suffix, " + suffix[i]);
|
System.out.println("In test " + name +
|
||||||
try {
|
", creating temp file with prefix, " +
|
||||||
f = File.createTempFile(prefix[i], suffix[i]);
|
prefix[i] + ", suffix, " + suffix[i] +
|
||||||
} catch (IOException e) {
|
", in dir, " + dir);
|
||||||
if (exceptionMsg.equals(e.getMessage()))
|
|
||||||
exceptionThrown = true;
|
try {
|
||||||
else
|
if (dir == null || dir.isEmpty())
|
||||||
System.out.println("Wrong error message:" + e.getMessage());
|
f = File.createTempFile(prefix[i], suffix[i]);
|
||||||
|
else
|
||||||
|
f = File.createTempFile(prefix[i], suffix[i], new File(dir));
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (exceptionExpected) {
|
||||||
|
if (e.getMessage().startsWith(exceptionMsg))
|
||||||
|
exceptionThrown = true;
|
||||||
|
else
|
||||||
|
System.out.println("Wrong error message:" +
|
||||||
|
e.getMessage());
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exceptionExpected && (!exceptionThrown || f != null))
|
||||||
|
throw new RuntimeException("IOException is expected");
|
||||||
}
|
}
|
||||||
if (!exceptionThrown || f != null)
|
|
||||||
throw new RuntimeException(errMsg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +87,17 @@ public class SpecialTempFile {
|
|||||||
}
|
}
|
||||||
String[] nulPre = { name + "\u0000" };
|
String[] nulPre = { name + "\u0000" };
|
||||||
String[] nulSuf = { ".test" };
|
String[] nulSuf = { ".test" };
|
||||||
test("NulName", nulPre, nulSuf);
|
test("NulName", nulPre, nulSuf, true);
|
||||||
|
|
||||||
|
// Test JDK-8025128
|
||||||
|
String[] goodPre = { "///..///", "/foo" };
|
||||||
|
String[] goodSuf = { ".temp", ".tmp" };
|
||||||
|
test("goodName", goodPre, goodSuf, false);
|
||||||
|
|
||||||
|
// Test JDK-8011950
|
||||||
|
String[] slashPre = { "temp", "///..///", "/foo" };
|
||||||
|
String[] slashSuf = { "///..///..", "///..///..", "///..///.." };
|
||||||
|
test("SlashedName", slashPre, slashSuf, true);
|
||||||
|
|
||||||
// Windows tests
|
// Windows tests
|
||||||
if (!System.getProperty("os.name").startsWith("Windows"))
|
if (!System.getProperty("os.name").startsWith("Windows"))
|
||||||
@ -80,11 +106,6 @@ public class SpecialTempFile {
|
|||||||
// Test JDK-8013827
|
// Test JDK-8013827
|
||||||
String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
|
String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
|
||||||
String[] resvSuf = { ".temp", ".temp" };
|
String[] resvSuf = { ".temp", ".temp" };
|
||||||
test("ReservedName", resvPre, resvSuf);
|
test("ReservedName", resvPre, resvSuf, true);
|
||||||
|
|
||||||
// Test JDK-8011950
|
|
||||||
String[] slashPre = { "///..///", "temp", "///..///" };
|
|
||||||
String[] slashSuf = { ".temp", "///..///..", "///..///.." };
|
|
||||||
test("SlashedName", slashPre, slashSuf);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user