# HG changeset patch # User mchung # Date 1485037917 28800 # Node ID 6c33c81be92700cadb4c1e87b0fe3d0700f54bf5 # Parent a3290a8e2803e197fd1b7c42a62a7527a207d370 8173096: jmod files are not world-readable Reviewed-by: alanb diff -r a3290a8e2803 -r 6c33c81be927 jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java --- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java Sat Jan 21 10:12:29 2017 -0800 +++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java Sat Jan 21 14:31:57 2017 -0800 @@ -73,7 +73,6 @@ 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 @@ // 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 cmds = options.cmds; final List libs = options.libs; @@ -908,7 +912,7 @@ 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 @@ 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; } diff -r a3290a8e2803 -r 6c33c81be927 jdk/test/tools/jmod/JmodTest.java --- a/jdk/test/tools/jmod/JmodTest.java Sat Jan 21 10:12:29 2017 -0800 +++ b/jdk/test/tools/jmod/JmodTest.java Sat Jan 21 14:31:57 2017 -0800 @@ -43,7 +43,6 @@ 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 @@ // 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 @@ jmod("create", "--class-path", cp, jmod.toString()) - .assertFailure() - .resultChecker(r -> { - assertContains(r.output, "unnamed package"); - List 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 findTmpFiles(String prefix) { - Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir")); - try (Stream 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); + }); } // ---