From adfa344e5e0ecd6594219a2ca62cf3ea476fc7a8 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Fri, 30 Jun 2023 14:38:04 +1000 Subject: [PATCH 1/3] pay attention to closing streams --- .../org/hl7/fhir/utilities/FTPClient.java | 7 ++-- .../java/org/hl7/fhir/utilities/TextFile.java | 18 +++++++---- .../org/hl7/fhir/utilities/XsltUtilities.java | 32 +++++++++++++------ .../utilities/i18n/JsonLangFileProducer.java | 7 +++- .../npm/FilesystemPackageCacheManager.java | 7 +++- .../hl7/fhir/utilities/npm/PackageHacker.java | 15 +++++++-- .../fhir/utilities/npm/ResourceRenamer.java | 7 +++- .../xhtml/HierarchicalTableGenerator.java | 10 ++++-- .../fhir/utilities/xls/XLSXmlNormaliser.java | 7 +++- .../org/hl7/fhir/utilities/xml/XMLUtil.java | 15 +++++++-- .../hl7/fhir/utilities/xml/XmlEscaper.java | 7 +++- .../hl7/fhir/utilities/xml/XmlGenerator.java | 6 +++- 12 files changed, 108 insertions(+), 30 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/FTPClient.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/FTPClient.java index 544a6b87f..1d8bd65ee 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/FTPClient.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/FTPClient.java @@ -258,8 +258,11 @@ public class FTPClient { private void attemptUpload(String source, String resolvedPath) throws IOException { final long startTime = System.nanoTime(); FileInputStream localStream = new FileInputStream(source); - clientImpl.storeFile(resolvedPath, localStream); - localStream.close(); + try { + clientImpl.storeFile(resolvedPath, localStream); + } finally { + localStream.close(); + } this.storeFileTimeNanos += System.nanoTime() - startTime; } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java index bb9198eef..ad6ea9484 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java @@ -90,11 +90,16 @@ public class TextFile { public static void stringToFile(String content, File file) throws IOException { - OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); - sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter - sw.write(content); - sw.flush(); - sw.close(); + FileOutputStream fs = new FileOutputStream(file); + try { + OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8"); + sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter + sw.write(content); + sw.flush(); + sw.close(); + } finally { + fs.close(); + } } public static void stringToStream(String content, OutputStream stream, boolean bom) throws IOException { @@ -124,7 +129,8 @@ public class TextFile { } public static void stringToFile(String content, File file, boolean bom) throws IOException { - OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); + FileOutputStream fs = new FileOutputStream(file); + OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8"); if (bom) sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter sw.write(content); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/XsltUtilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/XsltUtilities.java index 493020115..b04718a3f 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/XsltUtilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/XsltUtilities.java @@ -91,11 +91,11 @@ public class XsltUtilities { return res.getOutputStream().toString(); } - public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException { + public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws TransformerException, IOException { saxonTransform(xsltDir, source, xslt, dest, alt, null); } - public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map params) throws FileNotFoundException, TransformerException { + public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map params) throws TransformerException, IOException { TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl(); f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); StreamSource xsrc = new StreamSource(new FileInputStream(xslt)); @@ -108,12 +108,19 @@ public class XsltUtilities { } t.setURIResolver(new MyURIResolver(xsltDir, alt)); - StreamSource src = new StreamSource(new FileInputStream(source)); - StreamResult res = new StreamResult(new FileOutputStream(dest)); - t.transform(src, res); + FileInputStream fso = new FileInputStream(source); + FileOutputStream fsr = new FileOutputStream(dest); + try { + StreamSource src = new StreamSource(fso); + StreamResult res = new StreamResult(fsr); + t.transform(src, res); + } finally { + fso.close(); + fsr.close(); + } } - public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException { + public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws TransformerException, IOException { TransformerFactory f = TransformerFactory.newInstance(); StreamSource xsrc = new StreamSource(new FileInputStream(xslt)); @@ -121,9 +128,16 @@ public class XsltUtilities { Transformer t = f.newTransformer(xsrc); t.setURIResolver(new MyURIResolver(xsltDir, alt)); - StreamSource src = new StreamSource(new FileInputStream(source)); - StreamResult res = new StreamResult(new FileOutputStream(dest)); - t.transform(src, res); + FileInputStream fss = new FileInputStream(source); + FileOutputStream fsr = new FileOutputStream(dest); + try { + StreamSource src = new StreamSource(fss); + StreamResult res = new StreamResult(fsr); + t.transform(src, res); + } finally { + fss.close(); + fsr.close(); + } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java index d24e8b2f9..00e0d73d3 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java @@ -49,7 +49,12 @@ public class JsonLangFileProducer extends LanguageFileProducer { @Override public void finish() throws IOException { - JsonParser.compose(json, new FileOutputStream(getFileName(id, baseLang))); + FileOutputStream fs = new FileOutputStream(getFileName(id, baseLang)); + try { + JsonParser.compose(json, fs); + } finally { + fs.close(); + } } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java index fe4887bef..4f44fbcb3 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java @@ -187,7 +187,12 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple if (files != null) { for (File f : files) { if (f.getName().endsWith(".tgz")) { - temporaryPackages.add(NpmPackage.fromPackage(new FileInputStream(f))); + FileInputStream fs = new FileInputStream(f); + try { + temporaryPackages.add(NpmPackage.fromPackage(fs)); + } finally { + fs.close(); + } } } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java index def0a0e04..cc1c76fe9 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageHacker.java @@ -37,7 +37,13 @@ public class PackageHacker { if (!f.exists()) throw new Error("Unable to find "+f.getAbsolutePath()); - NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f)); + NpmPackage pck = null; + FileInputStream fs = new FileInputStream(f); + try { + pck = NpmPackage.fromPackage(fs); + } finally { + fs.close(); + } System.out.println("Altering Package "+f.getAbsolutePath()); System.out.println(nice(pck.getNpm())); @@ -51,7 +57,12 @@ public class PackageHacker { int r = System.in.read(); if (r == 'y') { f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak"))); - pck.save(new FileOutputStream(f)); + FileOutputStream fso = new FileOutputStream(f); + try { + pck.save(fso); + } finally { + fso.close(); + } } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java index dd9a451da..a133e89d9 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/ResourceRenamer.java @@ -30,7 +30,12 @@ public class ResourceRenamer { String rt = r.asString("resourceType"); String id = r.asString("id"); String nn = Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json"); - JsonParser.compose(r, new FileOutputStream(nn), true); + FileOutputStream fs = new FileOutputStream(nn); + try { + JsonParser.compose(r, fs, true); + } finally { + fs.close(); + } } } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/HierarchicalTableGenerator.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/HierarchicalTableGenerator.java index 71d4aa05b..ffddfbb81 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/HierarchicalTableGenerator.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/HierarchicalTableGenerator.java @@ -1008,9 +1008,13 @@ public class HierarchicalTableGenerator extends TranslatingUtilities { } newFile.createNewFile(); FileOutputStream stream = new FileOutputStream(file); - genImage(indents, hasChildren, lineColor, stream); - if (outputTracker!=null) - outputTracker.add(file); + try { + genImage(indents, hasChildren, lineColor, stream); + if (outputTracker!=null) + outputTracker.add(file); + } finally { + stream.close(); + } } return b.toString(); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xls/XLSXmlNormaliser.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xls/XLSXmlNormaliser.java index fc95f45e2..5427d76cd 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xls/XLSXmlNormaliser.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xls/XLSXmlNormaliser.java @@ -117,7 +117,12 @@ public class XLSXmlNormaliser { if (!hasComment) root.appendChild(xml.createComment("canonicalized")); try { - saveXml(new FileOutputStream(dest)); + FileOutputStream fs = new FileOutputStream(dest); + try { + saveXml(fs); + } finally { + fs.close(); + } String s = TextFile.fileToString(dest); s = s.replaceAll("\r\n","\n"); s = replaceSignificantEoln(s); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLUtil.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLUtil.java index c70e2d2f4..f85bc1a0c 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLUtil.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLUtil.java @@ -55,6 +55,7 @@ import javax.xml.transform.stream.StreamResult; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.npm.NpmPackage; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -458,14 +459,24 @@ public class XMLUtil { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); - return builder.parse(new FileInputStream(filename)); + FileInputStream fs = new FileInputStream(filename); + try { + return builder.parse(fs); + } finally { + fs.close(); + } } public static Document parseFileToDom(String filename, boolean ns) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(ns); DocumentBuilder builder = factory.newDocumentBuilder(); - return builder.parse(new FileInputStream(filename)); + FileInputStream fs = new FileInputStream(filename); + try { + return builder.parse(fs); + } finally { + fs.close(); + } } public static Element getLastChild(Element e) { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java index 378642886..ca237f516 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlEscaper.java @@ -27,7 +27,12 @@ public class XmlEscaper { } public static void convert(String source, String target) throws IOException { - convertAndClose(new FileInputStream(source), new FileOutputStream(target)); + FileOutputStream fs = new FileOutputStream(target); + try { + convertAndClose(new FileInputStream(source), fs); + } finally { + fs.close(); + } } public static void convertAndClose(InputStream source, OutputStream target) throws IOException { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlGenerator.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlGenerator.java index a8cbc6ec9..62212ecce 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlGenerator.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XmlGenerator.java @@ -74,7 +74,11 @@ public class XmlGenerator { public void generate(Element element, File file) throws IOException, FHIRException { OutputStream stream = new FileOutputStream(file); - generate(element, stream); + try { + generate(element, stream); + } finally { + stream.close(); + } } public void generate(Element element, OutputStream stream) throws IOException, FHIRException { From bef82f563032c46b85c4048810e2cdb9dacef44f Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Fri, 30 Jun 2023 16:32:29 +1000 Subject: [PATCH 2/3] remove bad logging --- .../src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java | 1 - 1 file changed, 1 deletion(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java index 1278e8482..57821d53f 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java @@ -68,7 +68,6 @@ public class PackageScanner { } } } - System.out.println("!"); } } From 5382025b4430e3c43d9062669c2297042f2fcf31 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sat, 1 Jul 2023 07:31:51 +1000 Subject: [PATCH 3/3] Fix bug in Package installation --- .../org/hl7/fhir/utilities/Utilities.java | 9 +++++ .../npm/FilesystemPackageCacheManager.java | 34 +++++++++++-------- .../hl7/fhir/utilities/npm/NpmPackage.java | 10 ++---- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java index 11c2ac1ca..0a48d8351 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java @@ -628,6 +628,15 @@ public class Utilities { return PathBuilder.getPathBuilder().buildPath(args); } + public static String path(File f, String... args) throws IOException { + String[] a = new String[args.length+1]; + a[0] = f.getAbsolutePath(); + for (int i = 0; i < args.length; i++) { + a[i+1] = args[i]; + } + return PathBuilder.getPathBuilder().buildPath(a); + } + /** * Composes a path string using by concatenating the passed arguments. * diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java index 4f44fbcb3..15a252796 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java @@ -109,7 +109,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple public static final String PACKAGE_VERSION_REGEX_OPT = "^[A-Za-z][A-Za-z0-9\\_\\-]*(\\.[A-Za-z0-9\\_\\-]+)+(\\#[A-Za-z0-9\\-\\_]+(\\.[A-Za-z0-9\\-\\_]+)*)?$"; private static final Logger ourLog = LoggerFactory.getLogger(FilesystemPackageCacheManager.class); private static final String CACHE_VERSION = "3"; // second version - see wiki page - private String cacheFolder; + private File cacheFolder; private boolean progress = true; private List temporaryPackages = new ArrayList<>(); private boolean buildLoaded = false; @@ -134,7 +134,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple * @throws IOException */ public FilesystemPackageCacheManager(String customFolder) throws IOException { - this.cacheFolder = customFolder; + this.cacheFolder = new File(customFolder); init(FilesystemPackageCacheMode.CUSTOM); } @@ -144,27 +144,33 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple switch (mode) { case SYSTEM: - cacheFolder = Utilities.path("var", "lib", ".fhir", "packages"); + cacheFolder = new File(Utilities.path("var", "lib", ".fhir", "packages")); break; case USER: - cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "packages"); + cacheFolder = new File(Utilities.path(System.getProperty("user.home"), ".fhir", "packages")); break; case TESTING: - cacheFolder = Utilities.path("[tmp]", ".fhir", "packages"); + cacheFolder = new File(Utilities.path("[tmp]", ".fhir", "packages")); break; case CUSTOM: - if (!new File(cacheFolder).exists()) { + if (!cacheFolder.exists()) { throw new FHIRException("The folder ''"+cacheFolder+"' could not be found"); } default: break; } - if (!(new File(cacheFolder).exists())) - Utilities.createDirectory(cacheFolder); + if (!(cacheFolder.exists())) + Utilities.createDirectory(cacheFolder.getAbsolutePath()); if (!(new File(Utilities.path(cacheFolder, "packages.ini")).exists())) TextFile.stringToFile("[cache]\r\nversion=" + CACHE_VERSION + "\r\n\r\n[urls]\r\n\r\n[local]\r\n\r\n", Utilities.path(cacheFolder, "packages.ini"), false); createIniFile(); + for (File f : cacheFolder.listFiles()) { + if (f.isDirectory() && Utilities.isValidUUID(f.getName())) { + Utilities.clearDirectory(f.getAbsolutePath()); + f.delete(); + } + } } public boolean isMinimalMemory() { @@ -199,7 +205,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } public String getFolder() { - return cacheFolder; + return cacheFolder.getAbsolutePath(); } private NpmPackage loadPackageInfo(String path) throws IOException { @@ -208,7 +214,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } private void clearCache() throws IOException { - for (File f : new File(cacheFolder).listFiles()) { + for (File f : cacheFolder.listFiles()) { if (f.isDirectory()) { new CacheLock(f.getName()).doWithLock(() -> { Utilities.clearDirectory(f.getAbsolutePath()); @@ -308,7 +314,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } public String getLatestVersionFromCache(String id) throws IOException { - for (String f : Utilities.reverseSorted(new File(cacheFolder).list())) { + for (String f : Utilities.reverseSorted(cacheFolder.list())) { File cf = new File(Utilities.path(cacheFolder, f)); if (cf.isDirectory()) { if (f.startsWith(id + "#")) { @@ -396,7 +402,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } String foundPackage = null; String foundVersion = null; - for (String f : Utilities.reverseSorted(new File(cacheFolder).list())) { + for (String f : Utilities.reverseSorted(cacheFolder.list())) { File cf = new File(Utilities.path(cacheFolder, f)); if (cf.isDirectory()) { if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) { @@ -865,7 +871,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple public List listPackages() { List res = new ArrayList<>(); - for (File f : new File(cacheFolder).listFiles()) { + for (File f : cacheFolder.listFiles()) { if (f.isDirectory() && f.getName().contains("#")) { res.add(f.getName()); } @@ -1021,7 +1027,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } } - for (String f : Utilities.sorted(new File(cacheFolder).list())) { + for (String f : Utilities.sorted(cacheFolder.list())) { if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) { return true; } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java index e19b26b9b..6b0460b8a 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java @@ -447,8 +447,7 @@ public class NpmPackage { } public static NpmPackage extractFromTgz(InputStream tgz, String desc, String tempDir, boolean minimal) throws IOException { - String dest = Utilities.path(tempDir, "package"); - Utilities.createDirectory(dest); + Utilities.createDirectory(tempDir); int size = 0; @@ -463,21 +462,18 @@ public class NpmPackage { while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { String n = entry.getName(); - if (n.startsWith("package/")) { - n = n.substring(8); - } if (n.contains("..")) { throw new RuntimeException("Entry with an illegal name: " + n); } if (entry.isDirectory()) { if (!Utilities.noString(n)) { String dir = n.substring(0, n.length()-1); - Utilities.createDirectory(Utilities.path(dest, dir)); + Utilities.createDirectory(Utilities.path(tempDir, dir)); } } else { int count; byte data[] = new byte[BUFFER_SIZE]; - String filename = Utilities.path(dest, n); + String filename = Utilities.path(tempDir, n); String folder = Utilities.getDirectoryForFile(filename); Utilities.createDirectory(folder); FileOutputStream fos = new FileOutputStream(filename);