Fix bug in Package installation

This commit is contained in:
Grahame Grieve 2023-07-01 07:31:51 +10:00
parent eb20ad87e8
commit 5382025b44
3 changed files with 32 additions and 21 deletions

View File

@ -628,6 +628,15 @@ public class Utilities {
return PathBuilder.getPathBuilder().buildPath(args); 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. * Composes a path string using by concatenating the passed arguments.
* *

View File

@ -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\\-\\_]+)*)?$"; 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 Logger ourLog = LoggerFactory.getLogger(FilesystemPackageCacheManager.class);
private static final String CACHE_VERSION = "3"; // second version - see wiki page private static final String CACHE_VERSION = "3"; // second version - see wiki page
private String cacheFolder; private File cacheFolder;
private boolean progress = true; private boolean progress = true;
private List<NpmPackage> temporaryPackages = new ArrayList<>(); private List<NpmPackage> temporaryPackages = new ArrayList<>();
private boolean buildLoaded = false; private boolean buildLoaded = false;
@ -134,7 +134,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
* @throws IOException * @throws IOException
*/ */
public FilesystemPackageCacheManager(String customFolder) throws IOException { public FilesystemPackageCacheManager(String customFolder) throws IOException {
this.cacheFolder = customFolder; this.cacheFolder = new File(customFolder);
init(FilesystemPackageCacheMode.CUSTOM); init(FilesystemPackageCacheMode.CUSTOM);
} }
@ -144,27 +144,33 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
switch (mode) { switch (mode) {
case SYSTEM: case SYSTEM:
cacheFolder = Utilities.path("var", "lib", ".fhir", "packages"); cacheFolder = new File(Utilities.path("var", "lib", ".fhir", "packages"));
break; break;
case USER: case USER:
cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "packages"); cacheFolder = new File(Utilities.path(System.getProperty("user.home"), ".fhir", "packages"));
break; break;
case TESTING: case TESTING:
cacheFolder = Utilities.path("[tmp]", ".fhir", "packages"); cacheFolder = new File(Utilities.path("[tmp]", ".fhir", "packages"));
break; break;
case CUSTOM: case CUSTOM:
if (!new File(cacheFolder).exists()) { if (!cacheFolder.exists()) {
throw new FHIRException("The folder ''"+cacheFolder+"' could not be found"); throw new FHIRException("The folder ''"+cacheFolder+"' could not be found");
} }
default: default:
break; break;
} }
if (!(new File(cacheFolder).exists())) if (!(cacheFolder.exists()))
Utilities.createDirectory(cacheFolder); Utilities.createDirectory(cacheFolder.getAbsolutePath());
if (!(new File(Utilities.path(cacheFolder, "packages.ini")).exists())) 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); 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(); createIniFile();
for (File f : cacheFolder.listFiles()) {
if (f.isDirectory() && Utilities.isValidUUID(f.getName())) {
Utilities.clearDirectory(f.getAbsolutePath());
f.delete();
}
}
} }
public boolean isMinimalMemory() { public boolean isMinimalMemory() {
@ -199,7 +205,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
public String getFolder() { public String getFolder() {
return cacheFolder; return cacheFolder.getAbsolutePath();
} }
private NpmPackage loadPackageInfo(String path) throws IOException { private NpmPackage loadPackageInfo(String path) throws IOException {
@ -208,7 +214,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
private void clearCache() throws IOException { private void clearCache() throws IOException {
for (File f : new File(cacheFolder).listFiles()) { for (File f : cacheFolder.listFiles()) {
if (f.isDirectory()) { if (f.isDirectory()) {
new CacheLock(f.getName()).doWithLock(() -> { new CacheLock(f.getName()).doWithLock(() -> {
Utilities.clearDirectory(f.getAbsolutePath()); Utilities.clearDirectory(f.getAbsolutePath());
@ -308,7 +314,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
public String getLatestVersionFromCache(String id) throws IOException { 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)); File cf = new File(Utilities.path(cacheFolder, f));
if (cf.isDirectory()) { if (cf.isDirectory()) {
if (f.startsWith(id + "#")) { if (f.startsWith(id + "#")) {
@ -396,7 +402,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
String foundPackage = null; String foundPackage = null;
String foundVersion = 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)); File cf = new File(Utilities.path(cacheFolder, f));
if (cf.isDirectory()) { if (cf.isDirectory()) {
if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) { if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
@ -865,7 +871,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
public List<String> listPackages() { public List<String> listPackages() {
List<String> res = new ArrayList<>(); List<String> res = new ArrayList<>();
for (File f : new File(cacheFolder).listFiles()) { for (File f : cacheFolder.listFiles()) {
if (f.isDirectory() && f.getName().contains("#")) { if (f.isDirectory() && f.getName().contains("#")) {
res.add(f.getName()); 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 + "#"))) { if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
return true; return true;
} }

View File

@ -447,8 +447,7 @@ public class NpmPackage {
} }
public static NpmPackage extractFromTgz(InputStream tgz, String desc, String tempDir, boolean minimal) throws IOException { public static NpmPackage extractFromTgz(InputStream tgz, String desc, String tempDir, boolean minimal) throws IOException {
String dest = Utilities.path(tempDir, "package"); Utilities.createDirectory(tempDir);
Utilities.createDirectory(dest);
int size = 0; int size = 0;
@ -463,21 +462,18 @@ public class NpmPackage {
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
String n = entry.getName(); String n = entry.getName();
if (n.startsWith("package/")) {
n = n.substring(8);
}
if (n.contains("..")) { if (n.contains("..")) {
throw new RuntimeException("Entry with an illegal name: " + n); throw new RuntimeException("Entry with an illegal name: " + n);
} }
if (entry.isDirectory()) { if (entry.isDirectory()) {
if (!Utilities.noString(n)) { if (!Utilities.noString(n)) {
String dir = n.substring(0, n.length()-1); String dir = n.substring(0, n.length()-1);
Utilities.createDirectory(Utilities.path(dest, dir)); Utilities.createDirectory(Utilities.path(tempDir, dir));
} }
} else { } else {
int count; int count;
byte data[] = new byte[BUFFER_SIZE]; byte data[] = new byte[BUFFER_SIZE];
String filename = Utilities.path(dest, n); String filename = Utilities.path(tempDir, n);
String folder = Utilities.getDirectoryForFile(filename); String folder = Utilities.getDirectoryForFile(filename);
Utilities.createDirectory(folder); Utilities.createDirectory(folder);
FileOutputStream fos = new FileOutputStream(filename); FileOutputStream fos = new FileOutputStream(filename);