Skip to content

Commit 3582fd9

Browse files
rjernstChrisHegarty
authored andcommitted
8290359: Ensure that all directory streams are closed in jdk.link
Reviewed-by: chegar
1 parent 53fc495 commit 3582fd9

File tree

3 files changed

+65
-52
lines changed

3 files changed

+65
-52
lines changed

src/jdk.jlink/share/classes/jdk/tools/jlink/builder/DefaultImageBuilder.java

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.nio.file.Path;
4242
import java.nio.file.Paths;
4343
import java.nio.file.StandardOpenOption;
44+
import java.nio.file.attribute.BasicFileAttributes;
4445
import java.nio.file.attribute.PosixFilePermission;
4546
import java.util.Collections;
4647
import java.util.HashMap;
@@ -51,6 +52,9 @@
5152
import java.util.Optional;
5253
import java.util.Properties;
5354
import java.util.Set;
55+
import java.util.function.BiPredicate;
56+
import java.util.function.Consumer;
57+
import java.util.stream.Stream;
5458

5559
import jdk.tools.jlink.internal.BasicImageWriter;
5660
import jdk.tools.jlink.internal.ExecutableImage;
@@ -202,26 +206,26 @@ public void storeFiles(ResourcePool files) {
202206
// launchers in the bin directory need execute permission.
203207
// On Windows, "bin" also subdirectories containing jvm.dll.
204208
if (Files.isDirectory(bin)) {
205-
Files.find(bin, 2, (path, attrs) -> {
206-
return attrs.isRegularFile() && !path.toString().endsWith(".diz");
207-
}).forEach(this::setExecutable);
209+
forEachPath(bin,
210+
(path, attrs) -> attrs.isRegularFile() && !path.toString().endsWith(".diz"),
211+
this::setExecutable);
208212
}
209213

210214
// jspawnhelper is in lib or lib/<arch>
211215
Path lib = root.resolve(LIB_DIRNAME);
212216
if (Files.isDirectory(lib)) {
213-
Files.find(lib, 2, (path, attrs) -> {
214-
return path.getFileName().toString().equals("jspawnhelper")
215-
|| path.getFileName().toString().equals("jexec");
216-
}).forEach(this::setExecutable);
217+
forEachPath(lib,
218+
(path, attrs) -> path.getFileName().toString().equals("jspawnhelper")
219+
|| path.getFileName().toString().equals("jexec"),
220+
this::setExecutable);
217221
}
218222

219223
// read-only legal notices/license files
220224
Path legal = root.resolve(LEGAL_DIRNAME);
221225
if (Files.isDirectory(legal)) {
222-
Files.find(legal, 2, (path, attrs) -> {
223-
return attrs.isRegularFile();
224-
}).forEach(this::setReadOnly);
226+
forEachPath(legal,
227+
(path, attrs) -> attrs.isRegularFile(),
228+
this::setReadOnly);
225229
}
226230
}
227231

@@ -528,34 +532,34 @@ public ExecutableImage getExecutableImage() {
528532
private static void patchScripts(ExecutableImage img, List<String> args) throws IOException {
529533
Objects.requireNonNull(args);
530534
if (!args.isEmpty()) {
531-
Files.find(img.getHome().resolve(BIN_DIRNAME), 2, (path, attrs) -> {
532-
return img.getModules().contains(path.getFileName().toString());
533-
}).forEach((p) -> {
534-
try {
535-
String pattern = "JLINK_VM_OPTIONS=";
536-
byte[] content = Files.readAllBytes(p);
537-
String str = new String(content, StandardCharsets.UTF_8);
538-
int index = str.indexOf(pattern);
539-
StringBuilder builder = new StringBuilder();
540-
if (index != -1) {
541-
builder.append(str.substring(0, index)).
542-
append(pattern);
543-
for (String s : args) {
544-
builder.append(s).append(" ");
545-
}
546-
String remain = str.substring(index + pattern.length());
547-
builder.append(remain);
548-
str = builder.toString();
549-
try (BufferedWriter writer = Files.newBufferedWriter(p,
550-
StandardCharsets.ISO_8859_1,
551-
StandardOpenOption.WRITE)) {
552-
writer.write(str);
535+
forEachPath(img.getHome().resolve(BIN_DIRNAME),
536+
(path, attrs) -> img.getModules().contains(path.getFileName().toString()),
537+
p -> {
538+
try {
539+
String pattern = "JLINK_VM_OPTIONS=";
540+
byte[] content = Files.readAllBytes(p);
541+
String str = new String(content, StandardCharsets.UTF_8);
542+
int index = str.indexOf(pattern);
543+
StringBuilder builder = new StringBuilder();
544+
if (index != -1) {
545+
builder.append(str.substring(0, index)).
546+
append(pattern);
547+
for (String s : args) {
548+
builder.append(s).append(" ");
549+
}
550+
String remain = str.substring(index + pattern.length());
551+
builder.append(remain);
552+
str = builder.toString();
553+
try (BufferedWriter writer = Files.newBufferedWriter(p,
554+
StandardCharsets.ISO_8859_1,
555+
StandardOpenOption.WRITE)) {
556+
writer.write(str);
557+
}
553558
}
559+
} catch (IOException ex) {
560+
throw new RuntimeException(ex);
554561
}
555-
} catch (IOException ex) {
556-
throw new RuntimeException(ex);
557-
}
558-
});
562+
});
559563
}
560564
}
561565

@@ -592,4 +596,11 @@ private static Set<String> retrieveModules(Path root) {
592596
}
593597
return modules;
594598
}
599+
600+
// finds subpaths matching the given criteria (up to 2 levels deep) and applies the given lambda
601+
private static void forEachPath(Path dir, BiPredicate<Path, BasicFileAttributes> matcher, Consumer<Path> consumer) throws IOException {
602+
try (Stream<Path> stream = Files.find(dir, 2, matcher)) {
603+
stream.forEach(consumer);
604+
}
605+
}
595606
}

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -821,23 +821,25 @@ private Archive newArchive(String module, Path path) {
821821
} else if (path.toString().endsWith(".jar")) {
822822
ModularJarArchive modularJarArchive = new ModularJarArchive(module, path, version);
823823

824-
Stream<Archive.Entry> signatures = modularJarArchive.entries().filter((entry) -> {
825-
String name = entry.name().toUpperCase(Locale.ENGLISH);
824+
try (Stream<Archive.Entry> entries = modularJarArchive.entries()) {
825+
boolean hasSignatures = entries.anyMatch((entry) -> {
826+
String name = entry.name().toUpperCase(Locale.ENGLISH);
826827

827-
return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && (
828+
return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && (
828829
name.endsWith(".SF") ||
829830
name.endsWith(".DSA") ||
830831
name.endsWith(".RSA") ||
831832
name.endsWith(".EC") ||
832833
name.startsWith("META-INF/SIG-")
833-
);
834-
});
835-
836-
if (signatures.count() != 0) {
837-
if (ignoreSigning) {
838-
System.err.println(taskHelper.getMessage("warn.signing", path));
839-
} else {
840-
throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path));
834+
);
835+
});
836+
837+
if (hasSignatures) {
838+
if (ignoreSigning) {
839+
System.err.println(taskHelper.getMessage("warn.signing", path));
840+
} else {
841+
throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path));
842+
}
841843
}
842844
}
843845

src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.util.regex.Pattern;
6060
import java.util.regex.PatternSyntaxException;
6161
import java.util.stream.Collectors;
62+
import java.util.stream.Stream;
6263
import java.util.zip.ZipEntry;
6364
import java.util.zip.ZipException;
6465
import java.util.zip.ZipFile;
@@ -682,11 +683,10 @@ Set<String> findPackages(List<Path> classpath) {
682683
* Returns the set of packages in the given directory tree.
683684
*/
684685
Set<String> findPackages(Path dir) {
685-
try {
686-
return Files.find(dir, Integer.MAX_VALUE,
687-
((path, attrs) -> attrs.isRegularFile()),
688-
FileVisitOption.FOLLOW_LINKS)
689-
.map(dir::relativize)
686+
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE,
687+
(path, attrs) -> attrs.isRegularFile(),
688+
FileVisitOption.FOLLOW_LINKS)) {
689+
return stream.map(dir::relativize)
690690
.filter(path -> isResource(path.toString()))
691691
.map(path -> toPackageName(path))
692692
.filter(pkg -> pkg.length() > 0)

0 commit comments

Comments
 (0)