8232280: close() is not called on return value of Files.walk() JDK-8200758-branch
authorherrick
Thu, 17 Oct 2019 07:55:35 -0400
branchJDK-8200758-branch
changeset 58670 6fb9e12d5595
parent 58648 3bf53ffa9ae7
child 58671 3b578a5976df
8232280: close() is not called on return value of Files.walk() Reviewed-by: asemenyuk, asemenuk
src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LibProvidersLookup.java
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppImageBuilder.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/PathGroup.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java
--- a/src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LibProvidersLookup.java	Wed Oct 16 10:32:08 2019 -0400
+++ b/src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LibProvidersLookup.java	Thu Oct 17 07:55:35 2019 -0400
@@ -52,9 +52,12 @@
 
     List<String> execute(Path root) throws IOException {
         // Get the list of files in the root for which to look up for needed shared libraries
-        List<Path> allPackageFiles = Files.walk(root).filter(
-                Files::isRegularFile).filter(LibProvidersLookup::canDependOnLibs).collect(
-                Collectors.toList());
+        List<Path> allPackageFiles;
+        try (Stream<Path> stream = Files.walk(root)) {
+            allPackageFiles = stream.filter(Files::isRegularFile).filter(
+                    LibProvidersLookup::canDependOnLibs).collect(
+                    Collectors.toList());
+        }
 
         Collection<Path> neededLibs = getNeededLibsForFiles(allPackageFiles);
 
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppImageBuilder.java	Wed Oct 16 10:32:08 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppImageBuilder.java	Thu Oct 17 07:55:35 2019 -0400
@@ -48,6 +48,7 @@
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
+import java.util.stream.Stream;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.xpath.XPath;
@@ -747,78 +748,79 @@
         String keyChain = SIGNING_KEYCHAIN.fetchFrom(params);
 
         // sign all dylibs and jars
-        Files.walk(appLocation).peek(path -> { // fix permissions
-                    try {
-                        Set<PosixFilePermission> pfp =
-                                Files.getPosixFilePermissions(path);
-                        if (!pfp.contains(PosixFilePermission.OWNER_WRITE)) {
-                            pfp = EnumSet.copyOf(pfp);
-                            pfp.add(PosixFilePermission.OWNER_WRITE);
-                            Files.setPosixFilePermissions(path, pfp);
-                        }
-                    } catch (IOException e) {
-                        Log.verbose(e);
+        try (Stream<Path> stream = Files.walk(appLocation)) {
+            stream.peek(path -> { // fix permissions
+                try {
+                    Set<PosixFilePermission> pfp =
+                            Files.getPosixFilePermissions(path);
+                    if (!pfp.contains(PosixFilePermission.OWNER_WRITE)) {
+                        pfp = EnumSet.copyOf(pfp);
+                        pfp.add(PosixFilePermission.OWNER_WRITE);
+                        Files.setPosixFilePermissions(path, pfp);
                     }
-                }).filter(p -> Files.isRegularFile(p)
-                          && !(p.toString().contains("/Contents/MacOS/libjli.dylib")
-                          || p.toString().endsWith(appExecutable)
-                          || p.toString().contains("/Contents/runtime")
-                          || p.toString().contains("/Contents/Frameworks"))).forEach(p -> {
-                    //noinspection ThrowableResultOfMethodCallIgnored
-                    if (toThrow.get() != null) return;
-
-                    // If p is a symlink then skip the signing process.
-                    if (Files.isSymbolicLink(p)) {
-                        if (VERBOSE.fetchFrom(params)) {
-                            Log.verbose(MessageFormat.format(I18N.getString(
-                                    "message.ignoring.symlink"), p.toString()));
-                        }
-                    } else {
-                        if (p.toString().endsWith(LIBRARY_NAME)) {
-                            if (isFileSigned(p)) {
-                                return;
-                            }
-                        }
+                } catch (IOException e) {
+                    Log.verbose(e);
+                }
+            }).filter(p -> Files.isRegularFile(p)
+                      && !(p.toString().contains("/Contents/MacOS/libjli.dylib")
+                      || p.toString().endsWith(appExecutable)
+                      || p.toString().contains("/Contents/runtime")
+                      || p.toString().contains("/Contents/Frameworks"))).forEach(p -> {
+                //noinspection ThrowableResultOfMethodCallIgnored
+                if (toThrow.get() != null) return;
 
-                        List<String> args = new ArrayList<>();
-                        args.addAll(Arrays.asList("codesign",
-                                "-s", signingIdentity, // sign with this key
-                                "--prefix", identifierPrefix,
-                                // use the identifier as a prefix
-                                "-vvvv"));
-                        if (entitlementsFile != null &&
-                                (p.toString().endsWith(".jar")
-                                || p.toString().endsWith(".dylib"))) {
-                            args.add("--entitlements");
-                            args.add(entitlementsFile); // entitlements
-                        } else if (inheritedEntitlements != null &&
-                                Files.isExecutable(p)) {
-                            args.add("--entitlements");
-                            args.add(inheritedEntitlements);
-                            // inherited entitlements for executable processes
-                        }
-                        if (keyChain != null && !keyChain.isEmpty()) {
-                            args.add("--keychain");
-                            args.add(keyChain);
-                        }
-                        args.add(p.toString());
-
-                        try {
-                            Set<PosixFilePermission> oldPermissions =
-                                    Files.getPosixFilePermissions(p);
-                            File f = p.toFile();
-                            f.setWritable(true, true);
-
-                            ProcessBuilder pb = new ProcessBuilder(args);
-                            IOUtils.exec(pb);
-
-                            Files.setPosixFilePermissions(p, oldPermissions);
-                        } catch (IOException ioe) {
-                            toThrow.set(ioe);
+                // If p is a symlink then skip the signing process.
+                if (Files.isSymbolicLink(p)) {
+                    if (VERBOSE.fetchFrom(params)) {
+                        Log.verbose(MessageFormat.format(I18N.getString(
+                                "message.ignoring.symlink"), p.toString()));
+                    }
+                } else {
+                    if (p.toString().endsWith(LIBRARY_NAME)) {
+                        if (isFileSigned(p)) {
+                            return;
                         }
                     }
-                });
 
+                    List<String> args = new ArrayList<>();
+                    args.addAll(Arrays.asList("codesign",
+                            "-s", signingIdentity, // sign with this key
+                            "--prefix", identifierPrefix,
+                            // use the identifier as a prefix
+                            "-vvvv"));
+                    if (entitlementsFile != null &&
+                            (p.toString().endsWith(".jar")
+                            || p.toString().endsWith(".dylib"))) {
+                        args.add("--entitlements");
+                        args.add(entitlementsFile); // entitlements
+                    } else if (inheritedEntitlements != null &&
+                            Files.isExecutable(p)) {
+                        args.add("--entitlements");
+                        args.add(inheritedEntitlements);
+                        // inherited entitlements for executable processes
+                    }
+                    if (keyChain != null && !keyChain.isEmpty()) {
+                        args.add("--keychain");
+                        args.add(keyChain);
+                    }
+                    args.add(p.toString());
+
+                    try {
+                        Set<PosixFilePermission> oldPermissions =
+                                Files.getPosixFilePermissions(p);
+                        File f = p.toFile();
+                        f.setWritable(true, true);
+
+                        ProcessBuilder pb = new ProcessBuilder(args);
+                        IOUtils.exec(pb);
+
+                        Files.setPosixFilePermissions(p, oldPermissions);
+                    } catch (IOException ioe) {
+                        toThrow.set(ioe);
+                    }
+                }
+            });
+        }
         IOException ioe = toThrow.get();
         if (ioe != null) {
             throw ioe;
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/PathGroup.java	Wed Oct 16 10:32:08 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/PathGroup.java	Thu Oct 17 07:55:35 2019 -0400
@@ -36,6 +36,7 @@
 import java.util.Map;
 import java.util.function.BiFunction;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 
 /**
@@ -82,8 +83,10 @@
         long reply = 0;
         for (Path dir : roots().stream().filter(f -> Files.isDirectory(f)).collect(
                 Collectors.toList())) {
-            reply += Files.walk(dir).filter(p -> Files.isRegularFile(p)).mapToLong(
-                    f -> f.toFile().length()).sum();
+            try (Stream<Path> stream = Files.walk(dir)) {
+                reply += stream.filter(p -> Files.isRegularFile(p)).mapToLong(
+                        f -> f.toFile().length()).sum();
+            }
         }
         return reply;
     }
@@ -145,8 +148,11 @@
             Path src = action.getKey();
             Path dst = action.getValue();
             if (src.toFile().isDirectory()) {
-                Files.walk(src).forEach(path -> actions.put(dst.resolve(
-                        src.relativize(path)).toAbsolutePath().normalize(), path));
+               try (Stream<Path> stream = Files.walk(src)) {
+                   stream.forEach(path -> actions.put(dst.resolve(
+                           src.relativize(path)).toAbsolutePath().normalize(),
+                           path));
+               }
             } else {
                 actions.put(dst.toAbsolutePath().normalize(), src);
             }
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Wed Oct 16 10:32:08 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Thu Oct 17 07:55:35 2019 -0400
@@ -50,6 +50,7 @@
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * StandardBundlerParam
@@ -673,9 +674,10 @@
                 }
                 Set<File> theFiles = new HashSet<>();
                 try {
-                    Files.walk(f.toPath())
-                            .filter(Files::isRegularFile)
-                            .forEach(p -> theFiles.add(p.toFile()));
+                    try (Stream<Path> stream = Files.walk(f.toPath())) {
+                        stream.filter(Files::isRegularFile)
+                                .forEach(p -> theFiles.add(p.toFile()));
+                    }
                 } catch (IOException e) {
                     e.printStackTrace();
                 }