From dfb0e0d5b9a38e3b159ac177727547046a76912b Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Fri, 30 Jun 2023 13:42:46 +1000 Subject: [PATCH] Fix windows file renaming issue --- .../java/org/hl7/fhir/utilities/TextFile.java | 56 ++++++++++++++----- .../org/hl7/fhir/utilities/Utilities.java | 26 +++++++++ .../npm/FilesystemPackageCacheManager.java | 2 +- 3 files changed, 70 insertions(+), 14 deletions(-) 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 9125470c7..bb9198eef 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 @@ -62,13 +62,18 @@ public class TextFile { List result = new ArrayList(); File file = new CSFile(path); - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8")); - - while( reader.ready() ) - result.add(reader.readLine()); - - reader.close(); - return result; + FileInputStream fs = new FileInputStream(file); + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(fs,"UTF-8")); + + while( reader.ready() ) + result.add(reader.readLine()); + + reader.close(); + return result; + } finally { + fs.close(); + } } public static void writeAllLines(String path, List lines) throws IOException @@ -141,12 +146,22 @@ public class TextFile { } public static String fileToString(File f) throws FileNotFoundException, IOException { - return streamToString(new FileInputStream(f)); + FileInputStream fs = new FileInputStream(f); + try { + return streamToString(fs); + } finally { + fs.close(); + } } public static String fileToString(String src) throws FileNotFoundException, IOException { - return streamToString(new FileInputStream(new CSFile(src))); - } + FileInputStream fs = new FileInputStream(new CSFile(src)); + try { + return streamToString(fs); + } finally { + fs.close(); + } + } public static String streamToString(InputStream input) throws IOException { InputStreamReader sr = new InputStreamReader(input, "UTF-8"); @@ -205,7 +220,12 @@ public class TextFile { } public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException { - return streamToBytes(new FileInputStream(new CSFile(srcFile))); + FileInputStream fs = new FileInputStream(new CSFile(srcFile)); + try { + return streamToBytes(fs); + } finally { + fs.close(); + } } /** @@ -218,11 +238,21 @@ public class TextFile { * @throws IOException */ public static byte[] fileToBytesNCS(String srcFile) throws FileNotFoundException, IOException { - return streamToBytes(new FileInputStream(new File(srcFile))); + FileInputStream fs = new FileInputStream(new File(srcFile)); + try { + return streamToBytes(fs); + } finally { + fs.close(); + } } public static byte[] fileToBytes(File file) throws FileNotFoundException, IOException { - return streamToBytes(new FileInputStream(file)); + FileInputStream fs = new FileInputStream(file); + try { + return streamToBytes(fs); + } finally { + fs.close(); + } } public static String bytesToString(byte[] bs) throws IOException { 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 0d16a1a51..11c2ac1ca 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 @@ -2033,4 +2033,30 @@ public class Utilities { return i == 0 ? "" : s.substring(0, i+1); } +public static void renameDirectory(String source, String dest) throws FHIRException, IOException { + File src = new File(source); + File dst = new File(dest); + if (!src.renameTo(dst)) { + int i = 0; + do { + try { + Thread.sleep(20); + } catch (Exception e) { + // nothing + } + System.gc(); + i++; + } while (!src.renameTo(dst) && i < 10); + if (src.exists()) { + copyDirectory(source, dest, null); + try { + src.delete(); + } catch (Exception e) { + // nothing + } + } + } + +} + } 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 f4e496778..fe4887bef 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 @@ -453,7 +453,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } catch (Throwable t) { log("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later"); } - new File(tempDir).renameTo(new File(packRoot)); + Utilities.renameDirectory(tempDir, packRoot); IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini")); ini.setTimeStampFormat("yyyyMMddhhmmss");