Skip to content

Commit 2f6afe5

Browse files
author
Andy Herrick
committed
8249289: Exception thrown when --temp points to non-existant directory
Reviewed-by: asemenyuk, almatvee
1 parent e13cb76 commit 2f6afe5

2 files changed

Lines changed: 43 additions & 24 deletions

File tree

src/jdk.incubator.jpackage/share/classes/jdk/incubator/jpackage/internal/DeployParams.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,20 @@ List<Path> expandFileset(Path root) throws IOException {
7979
List<Path> files = new LinkedList<>();
8080
if (!Files.isSymbolicLink(root)) {
8181
if (Files.isDirectory(root)) {
82-
List<Path> children = Files.list(root).collect(Collectors.toList());
83-
if (children != null && children.size() > 0) {
84-
children.forEach(f -> {
85-
try {
86-
files.addAll(expandFileset(f));
87-
} catch (IOException ex) {
88-
throw new RuntimeException(ex);
89-
}
90-
});
91-
} else {
92-
// Include empty folders
93-
files.add(root);
82+
try (Stream<Path> stream = Files.list(root)) {
83+
List<Path> children = stream.collect(Collectors.toList());
84+
if (children != null && children.size() > 0) {
85+
children.forEach(f -> {
86+
try {
87+
files.addAll(expandFileset(f));
88+
} catch (IOException ex) {
89+
throw new RuntimeException(ex);
90+
}
91+
});
92+
} else {
93+
// Include empty folders
94+
files.add(root);
95+
}
9496
}
9597
} else {
9698
files.add(root);
@@ -214,13 +216,13 @@ public void validate() throws PackagerException {
214216
// Validate temp dir
215217
String root = (String)bundlerArguments.get(
216218
Arguments.CLIOptions.TEMP_ROOT.getId());
217-
if (root != null) {
218-
try {
219-
String [] contents = Files.list(Path.of(root))
220-
.toArray(String[]::new);
221-
222-
if (contents != null && contents.length > 0) {
223-
throw new PackagerException("ERR_BuildRootInvalid", root);
219+
if (root != null && Files.exists(Path.of(root))) {
220+
try (Stream<Path> stream = Files.walk(Path.of(root), 1)) {
221+
Path [] contents = stream.toArray(Path[]::new);
222+
// contents.length > 1 because Files.walk(path) includes path
223+
if (contents != null && contents.length > 1) {
224+
throw new PackagerException(
225+
"ERR_BuildRootInvalid", root);
224226
}
225227
} catch (IOException ioe) {
226228
throw new PackagerException(ioe);

test/jdk/tools/jpackage/share/jdk/jpackage/tests/BasicTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,24 @@ public void testAddModules(String... addModulesArg) {
245245
* @throws IOException
246246
*/
247247
@Test
248-
public void testTemp() throws IOException {
249-
final Path tempRoot = TKit.createTempDirectory("temp-root");
250-
248+
@Parameter("true")
249+
@Parameter("false")
250+
public void testTemp(boolean withExistingTempDir) throws IOException {
251+
final Path tempRoot = TKit.createTempDirectory("tmp");
252+
// This Test has problems on windows where path in the temp dir are too long
253+
// for the wix tools. We can't use a tempDir outside the TKit's WorkDir, so
254+
// we minimize both the tempRoot directory name (above) and the tempDir name
255+
// (below) to the extension part (which is necessary to differenciate between
256+
// the multiple PackageTypes that will be run for one JPackageCommand).
257+
// It might be beter if the whole work dir name was shortened from:
258+
// jtreg_open_test_jdk_tools_jpackage_share_jdk_jpackage_tests_BasicTest_java.
251259
Function<JPackageCommand, Path> getTempDir = cmd -> {
252-
return tempRoot.resolve(cmd.outputBundle().getFileName());
260+
String ext = cmd.outputBundle().getFileName().toString();
261+
int i = ext.lastIndexOf(".");
262+
if (i > 0 && i < (ext.length() - 1)) {
263+
ext = ext.substring(i+1);
264+
}
265+
return tempRoot.resolve(ext);
253266
};
254267

255268
Supplier<PackageTest> createTest = () -> {
@@ -259,7 +272,11 @@ public void testTemp() throws IOException {
259272
.addInitializer(JPackageCommand::setDefaultInputOutput)
260273
.addInitializer(cmd -> {
261274
Path tempDir = getTempDir.apply(cmd);
262-
Files.createDirectories(tempDir);
275+
if (withExistingTempDir) {
276+
Files.createDirectories(tempDir);
277+
} else {
278+
Files.createDirectories(tempDir.getParent());
279+
}
263280
cmd.addArguments("--temp", tempDir);
264281
});
265282
};

0 commit comments

Comments
 (0)