changeset 16550:7eacab16d40e

8173096: jmod files are not world-readable Reviewed-by: alanb
author mchung
date Sat, 21 Jan 2017 14:31:57 -0800
parents 4b325e200cc2
children 6905bb05d585 e4fcda859df2
files src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java test/tools/jmod/JmodTest.java
diffstat 2 files changed, 25 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java	Sat Jan 21 10:12:29 2017 -0800
+++ b/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<Path> cmds = options.cmds;
         final List<Path> 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;
             }
--- a/test/tools/jmod/JmodTest.java	Sat Jan 21 10:12:29 2017 -0800
+++ b/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<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);
+            });
     }
 
     // ---