Skip to content
Permalink
Browse files
8257514: Fix the issues in jdk.jpackage identified by SpotBugs
Reviewed-by: asemenyuk, almatvee, shade
  • Loading branch information
Andy Herrick committed Dec 7, 2020
1 parent bbc44f5 commit e3793e5a608fd44ff479734182af20a06eb0ec14
@@ -112,7 +112,7 @@ void create(Path appImage) throws IOException {
}

Path cfgFile = appImagelayout.appDirectory().resolve(launcherName + ".cfg");
Files.createDirectories(cfgFile.getParent());
Files.createDirectories(IOUtils.getParent(cfgFile));

boolean[] addLineBreakAtSection = new boolean[1];
Stream<String> lines = content.stream().map(entry -> {
@@ -111,7 +111,7 @@ static void validateName(String s, boolean forApp)
if (forApp) {
return;
} else {
throw new PackagerException(exceptionKey, s);
throw new PackagerException(exceptionKey);
}
}
if (s.length() == 0 || s.charAt(s.length() - 1) == '\\') {
@@ -208,7 +208,9 @@ public void validate() throws PackagerException {
Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId());
if (appImage != null) {
Path appImageDir = Path.of(appImage);
if (!Files.exists(appImageDir) || appImageDir.toFile().list().length == 0) {
if (!Files.exists(appImageDir)
|| appImageDir.toFile().list() == null
|| appImageDir.toFile().list().length == 0) {
throw new PackagerException("ERR_AppImageNotExist", appImage);
}
}
@@ -120,7 +120,7 @@ public FileVisitResult visitFile(final Path file,

public static void copyFile(Path sourceFile, Path destFile)
throws IOException {
Files.createDirectories(destFile.getParent());
Files.createDirectories(getParent(destFile));

Files.copy(sourceFile, destFile,
StandardCopyOption.REPLACE_EXISTING,
@@ -140,8 +140,9 @@ public static void run(String launcher, Path paramFile)
throws IOException {
if (IOUtils.exists(paramFile)) {
ProcessBuilder pb =
new ProcessBuilder(launcher, paramFile.getFileName().toString());
pb = pb.directory(paramFile.getParent().toFile());
new ProcessBuilder(launcher,
getFileName(paramFile).toString());
pb = pb.directory(getParent(paramFile).toFile());
exec(pb);
}
}
@@ -254,20 +255,20 @@ static void writableOutputDir(Path outdir) throws PackagerException {

public static Path replaceSuffix(Path path, String suffix) {
Path parent = path.getParent();
String filename = path.getFileName().toString().replaceAll("\\.[^.]*$", "")
String filename = getFileName(path).toString().replaceAll("\\.[^.]*$", "")
+ Optional.ofNullable(suffix).orElse("");
return parent != null ? parent.resolve(filename) : Path.of(filename);
}

public static Path addSuffix(Path path, String suffix) {
Path parent = path.getParent();
String filename = path.getFileName().toString() + suffix;
String filename = getFileName(path).toString() + suffix;
return parent != null ? parent.resolve(filename) : Path.of(filename);
}

public static String getSuffix(Path path) {
String filename = replaceSuffix(path.getFileName(), null).toString();
return path.getFileName().toString().substring(filename.length());
String filename = replaceSuffix(getFileName(path), null).toString();
return getFileName(path).toString().substring(filename.length());
}

@FunctionalInterface
@@ -278,7 +279,7 @@ public static String getSuffix(Path path) {
public static void createXml(Path dstFile, XmlConsumer xmlConsumer) throws
IOException {
XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
Files.createDirectories(dstFile.getParent());
Files.createDirectories(getParent(dstFile));
try (Writer w = Files.newBufferedWriter(dstFile)) {
// Wrap with pretty print proxy
XMLStreamWriter xml = (XMLStreamWriter) Proxy.newProxyInstance(
@@ -298,6 +299,28 @@ public static void createXml(Path dstFile, XmlConsumer xmlConsumer) throws
}
}

public static Path getParent(Path p) {
Path parent = p.getParent();
if (parent == null) {
IllegalArgumentException iae =
new IllegalArgumentException(p.toString());
Log.verbose(iae);
throw iae;
}
return parent;
}

public static Path getFileName(Path p) {
Path filename = p.getFileName();
if (filename == null) {
IllegalArgumentException iae =
new IllegalArgumentException(p.toString());
Log.verbose(iae);
throw iae;
}
return filename;
}

private static class PrettyPrintHandler implements InvocationHandler {

PrettyPrintHandler(XMLStreamWriter target) {
@@ -173,7 +173,7 @@ public Path publicName() {

@Override
public void consume(InputStream in) throws IOException {
Files.createDirectories(dest.getParent());
Files.createDirectories(IOUtils.getParent(dest));
Files.copy(in, dest, StandardCopyOption.REPLACE_EXISTING);
}
});
@@ -179,7 +179,7 @@ private static void copy(boolean move, List<Map.Entry<Path, Path>> entries,
handler = new TransformHandler() {
@Override
public void copyFile(Path src, Path dst) throws IOException {
Files.createDirectories(dst.getParent());
Files.createDirectories(IOUtils.getParent(dst));
if (move) {
Files.move(src, dst);
} else {
@@ -84,7 +84,7 @@ ConfigException validate() {
cmdline.add(toolPath.toString());
cmdline.addAll(args);

String name = toolPath.getFileName().toString();
String name = IOUtils.getFileName(toolPath).toString();
try {
ProcessBuilder pb = new ProcessBuilder(cmdline);
AtomicBoolean canUseTool = new AtomicBoolean();
@@ -170,6 +170,8 @@ public String nextToken() throws IOException {
case 'f':
ch = '\f';
break;
default:
break;
}
}
sb.append((char) ch);
@@ -181,7 +181,7 @@ private static String getFixedFileVersion(String value) {
private void rebrandProperties(long resourceLock, OverridableResource properties,
Map<String, String> data, Path target) throws IOException {

String targetExecutableName = target.getFileName().toString();
String targetExecutableName = IOUtils.getFileName(target).toString();
data.put("INTERNAL_NAME", targetExecutableName);
data.put("ORIGINAL_FILENAME", targetExecutableName);

@@ -426,7 +426,7 @@ public Path execute(Map<String, ? super Object> params,

String licenseFile = LICENSE_FILE.fetchFrom(params);
if (licenseFile != null) {
String lname = Path.of(licenseFile).getFileName().toString();
String lname = IOUtils.getFileName(Path.of(licenseFile)).toString();
Path destFile = CONFIG_ROOT.fetchFrom(params).resolve(lname);
data.put("JpLicenseRtf", destFile.toAbsolutePath().toString());
}
@@ -75,7 +75,7 @@
}

private void writeEntry(InputStream in, Path dstFile) throws IOException {
Files.createDirectories(dstFile.getParent());
Files.createDirectories(IOUtils.getParent(dstFile));
Files.copy(in, dstFile);
}

@@ -95,7 +95,7 @@ void buildMsi(Path msi) throws IOException {
lightCmdline.addAll(lightOptions);
wixObjs.stream().map(Path::toString).forEach(lightCmdline::add);

Files.createDirectories(msi.getParent());
Files.createDirectories(IOUtils.getParent(msi));
execute(lightCmdline);
}

@@ -105,7 +105,7 @@ private Path compile(WixSource wixSource) throws IOException {
};

Path wixObj = adjustPath.apply(wixObjDir).resolve(IOUtils.replaceSuffix(
wixSource.source.getFileName(), ".wixobj"));
IOUtils.getFileName(wixSource.source), ".wixobj"));

List<String> cmdline = new ArrayList<>(List.of(
toolset.get(WixTool.Candle).toString(),
@@ -236,7 +236,7 @@ private static UUID createNameUUID(Path path, String role) {

String of(Path path) {
if (this == Folder && KNOWN_DIRS.contains(path)) {
return path.getFileName().toString();
return IOUtils.getFileName(path).toString();
}

String result = of(path, prefix, name());
@@ -437,7 +437,7 @@ private String addShortcutComponent(XMLStreamWriter xml, Path launcherPath,
}

String launcherBasename = IOUtils.replaceSuffix(
launcherPath.getFileName(), "").toString();
IOUtils.getFileName(launcherPath), "").toString();

Path shortcutPath = folder.getPath(this).resolve(launcherBasename);
return addComponent(xml, shortcutPath, Component.Shortcut, unused -> {
@@ -612,7 +612,7 @@ public void createDirectory(final Path dir) throws IOException {
xml.writeAttribute("Id", Id.Folder.of(dir.getParent()));
xml.writeStartElement("Directory");
xml.writeAttribute("Id", Id.Folder.of(dir));
xml.writeAttribute("Name", dir.getFileName().toString());
xml.writeAttribute("Name", IOUtils.getFileName(dir).toString());
xml.writeEndElement();
xml.writeEndElement();
}
@@ -669,7 +669,7 @@ private void addIconsFragment(XMLStreamWriter xml) throws
srcPathGroup.transform(dstPathGroup, new PathGroup.TransformHandler() {
@Override
public void copyFile(Path src, Path dst) throws IOException {
if (src.getFileName().toString().endsWith(".ico")) {
if (IOUtils.getFileName(src).toString().endsWith(".ico")) {
icoFiles.add(Map.entry(src, dst));
}
}

1 comment on commit e3793e5

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on e3793e5 Dec 7, 2020

Please sign in to comment.