8173096: jmod files are not world-readable

Reviewed-by: alanb
This commit is contained in:
Mandy Chung 2017-01-21 14:31:57 -08:00
parent 20bcdb3a1c
commit ea6b99d730
2 changed files with 25 additions and 51 deletions

View File

@ -73,7 +73,6 @@ import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.jar.JarEntry;
@ -395,25 +394,30 @@ public class JmodTask {
// create jmod with temporary name to avoid it being examined
// when scanning the module path
Path target = options.jmodFile;
Path tempTarget = Files.createTempFile(target.getFileName().toString(), ".tmp");
Path tempTarget = jmodTempFilePath(target);
try {
try (JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget)) {
jmod.write(jos);
}
Files.move(tempTarget, target);
} catch (Exception e) {
if (Files.exists(tempTarget)) {
try {
Files.delete(tempTarget);
} catch (IOException ioe) {
e.addSuppressed(ioe);
}
try {
Files.deleteIfExists(tempTarget);
} catch (IOException ioe) {
e.addSuppressed(ioe);
}
throw e;
}
return true;
}
/*
* Create a JMOD .tmp file for the given target JMOD file
*/
private static Path jmodTempFilePath(Path target) throws IOException {
return target.resolveSibling("." + target.getFileName() + ".tmp");
}
private class JmodFileWriter {
final List<Path> cmds = options.cmds;
final List<Path> libs = options.libs;
@ -908,7 +912,7 @@ public class JmodTask {
throws IOException
{
Path target = moduleToPath(name);
Path tempTarget = Files.createTempFile(target.getFileName().toString(), ".tmp");
Path tempTarget = jmodTempFilePath(target);
try {
if (target.getFileName().toString().endsWith(".jmod")) {
updateJmodFile(target, tempTarget, moduleHashes);
@ -916,12 +920,10 @@ public class JmodTask {
updateModularJar(target, tempTarget, moduleHashes);
}
} catch (IOException|RuntimeException e) {
if (Files.exists(tempTarget)) {
try {
Files.delete(tempTarget);
} catch (IOException ioe) {
e.addSuppressed(ioe);
}
try {
Files.deleteIfExists(tempTarget);
} catch (IOException ioe) {
e.addSuppressed(ioe);
}
throw e;
}

View File

@ -43,7 +43,6 @@ import java.util.spi.ToolProvider;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.testlibrary.FileUtils;
import jdk.testlibrary.JDKToolFinder;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
@ -587,17 +586,10 @@ public class JmodTest {
// Ensure that it is removed in the event of a failure.
// The failure in this case is a class in the unnamed package.
String filename = "testTmpFileRemoved.jmod";
Path jmod = MODS_DIR.resolve(filename);
// clean up files
Path jmod = MODS_DIR.resolve("testTmpFileRemoved.jmod");
Path tmp = MODS_DIR.resolve(".testTmpFileRemoved.jmod.tmp");
FileUtils.deleteFileIfExistsWithRetry(jmod);
findTmpFiles(filename).forEach(tmp -> {
try {
FileUtils.deleteFileIfExistsWithRetry(tmp);
} catch (IOException e) {}
});
FileUtils.deleteFileIfExistsWithRetry(tmp);
String cp = EXPLODED_DIR.resolve("foo").resolve("classes") + File.pathSeparator +
EXPLODED_DIR.resolve("foo").resolve("classes")
.resolve("jdk").resolve("test").resolve("foo").toString();
@ -605,31 +597,11 @@ public class JmodTest {
jmod("create",
"--class-path", cp,
jmod.toString())
.assertFailure()
.resultChecker(r -> {
assertContains(r.output, "unnamed package");
List<Path> tmpfiles = findTmpFiles(filename);
assertTrue(tmpfiles.isEmpty(), "Unexpected tmp file:" + tmpfiles);
});
}
/*
* Returns the list of writeable tmp files with the given prefix.
*
* Ignore the non-writeable tmp files because this test is possibly
* running by another user.
*/
private List<Path> findTmpFiles(String prefix) {
Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir"));
try (Stream<Path> stream = Files.list(tmpdir)) {
return stream.filter(p -> {
String fn = p.getFileName().toString();
return Files.isWritable(p)
&& fn.startsWith(prefix) && fn.endsWith(".tmp");
}).collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
.assertFailure()
.resultChecker(r -> {
assertContains(r.output, "unnamed package");
assertTrue(Files.notExists(tmp), "Unexpected tmp file:" + tmp);
});
}
// ---