Skip to content

Commit

Permalink
8290359: Ensure that all directory streams are closed in jdk.link
Browse files Browse the repository at this point in the history
Reviewed-by: chegar
  • Loading branch information
rjernst authored and ChrisHegarty committed Jul 21, 2022
1 parent 53fc495 commit 3582fd9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 52 deletions.
Expand Up @@ -41,6 +41,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -51,6 +52,9 @@
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.stream.Stream;

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

// jspawnhelper is in lib or lib/<arch>
Path lib = root.resolve(LIB_DIRNAME);
if (Files.isDirectory(lib)) {
Files.find(lib, 2, (path, attrs) -> {
return path.getFileName().toString().equals("jspawnhelper")
|| path.getFileName().toString().equals("jexec");
}).forEach(this::setExecutable);
forEachPath(lib,
(path, attrs) -> path.getFileName().toString().equals("jspawnhelper")
|| path.getFileName().toString().equals("jexec"),
this::setExecutable);
}

// read-only legal notices/license files
Path legal = root.resolve(LEGAL_DIRNAME);
if (Files.isDirectory(legal)) {
Files.find(legal, 2, (path, attrs) -> {
return attrs.isRegularFile();
}).forEach(this::setReadOnly);
forEachPath(legal,
(path, attrs) -> attrs.isRegularFile(),
this::setReadOnly);
}
}

Expand Down Expand Up @@ -528,34 +532,34 @@ public ExecutableImage getExecutableImage() {
private static void patchScripts(ExecutableImage img, List<String> args) throws IOException {
Objects.requireNonNull(args);
if (!args.isEmpty()) {
Files.find(img.getHome().resolve(BIN_DIRNAME), 2, (path, attrs) -> {
return img.getModules().contains(path.getFileName().toString());
}).forEach((p) -> {
try {
String pattern = "JLINK_VM_OPTIONS=";
byte[] content = Files.readAllBytes(p);
String str = new String(content, StandardCharsets.UTF_8);
int index = str.indexOf(pattern);
StringBuilder builder = new StringBuilder();
if (index != -1) {
builder.append(str.substring(0, index)).
append(pattern);
for (String s : args) {
builder.append(s).append(" ");
}
String remain = str.substring(index + pattern.length());
builder.append(remain);
str = builder.toString();
try (BufferedWriter writer = Files.newBufferedWriter(p,
StandardCharsets.ISO_8859_1,
StandardOpenOption.WRITE)) {
writer.write(str);
forEachPath(img.getHome().resolve(BIN_DIRNAME),
(path, attrs) -> img.getModules().contains(path.getFileName().toString()),
p -> {
try {
String pattern = "JLINK_VM_OPTIONS=";
byte[] content = Files.readAllBytes(p);
String str = new String(content, StandardCharsets.UTF_8);
int index = str.indexOf(pattern);
StringBuilder builder = new StringBuilder();
if (index != -1) {
builder.append(str.substring(0, index)).
append(pattern);
for (String s : args) {
builder.append(s).append(" ");
}
String remain = str.substring(index + pattern.length());
builder.append(remain);
str = builder.toString();
try (BufferedWriter writer = Files.newBufferedWriter(p,
StandardCharsets.ISO_8859_1,
StandardOpenOption.WRITE)) {
writer.write(str);
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
});
}
}

Expand Down Expand Up @@ -592,4 +596,11 @@ private static Set<String> retrieveModules(Path root) {
}
return modules;
}

// finds subpaths matching the given criteria (up to 2 levels deep) and applies the given lambda
private static void forEachPath(Path dir, BiPredicate<Path, BasicFileAttributes> matcher, Consumer<Path> consumer) throws IOException {
try (Stream<Path> stream = Files.find(dir, 2, matcher)) {
stream.forEach(consumer);
}
}
}
24 changes: 13 additions & 11 deletions src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java
Expand Up @@ -821,23 +821,25 @@ private Archive newArchive(String module, Path path) {
} else if (path.toString().endsWith(".jar")) {
ModularJarArchive modularJarArchive = new ModularJarArchive(module, path, version);

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

return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && (
return name.startsWith("META-INF/") && name.indexOf('/', 9) == -1 && (
name.endsWith(".SF") ||
name.endsWith(".DSA") ||
name.endsWith(".RSA") ||
name.endsWith(".EC") ||
name.startsWith("META-INF/SIG-")
);
});

if (signatures.count() != 0) {
if (ignoreSigning) {
System.err.println(taskHelper.getMessage("warn.signing", path));
} else {
throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path));
);
});

if (hasSignatures) {
if (ignoreSigning) {
System.err.println(taskHelper.getMessage("warn.signing", path));
} else {
throw new IllegalArgumentException(taskHelper.getMessage("err.signing", path));
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/jdk.jlink/share/classes/jdk/tools/jmod/JmodTask.java
Expand Up @@ -59,6 +59,7 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -682,11 +683,10 @@ Set<String> findPackages(List<Path> classpath) {
* Returns the set of packages in the given directory tree.
*/
Set<String> findPackages(Path dir) {
try {
return Files.find(dir, Integer.MAX_VALUE,
((path, attrs) -> attrs.isRegularFile()),
FileVisitOption.FOLLOW_LINKS)
.map(dir::relativize)
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE,
(path, attrs) -> attrs.isRegularFile(),
FileVisitOption.FOLLOW_LINKS)) {
return stream.map(dir::relativize)
.filter(path -> isResource(path.toString()))
.map(path -> toPackageName(path))
.filter(pkg -> pkg.length() > 0)
Expand Down

0 comments on commit 3582fd9

Please sign in to comment.