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);