From 882d18251ba1c44b854202a0601cabe2ee3d3973 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 2 Aug 2023 19:58:24 +1000 Subject: [PATCH] fix up copy directory for case differences --- .../java/org/hl7/fhir/utilities/CSFile.java | 3 +- .../org/hl7/fhir/utilities/Utilities.java | 19 +++++++- .../org/hl7/fhir/utilities/UtilitiesTest.java | 43 ++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/CSFile.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/CSFile.java index b071162e8..975328f7f 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/CSFile.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/CSFile.java @@ -65,8 +65,9 @@ public class CSFile extends File { //attempt to open a file triggers a directory listing if (exists()) { - if(!this.getCanonicalFile().getName().equals(this.getName())) + if(!this.getCanonicalFile().getName().equals(this.getName())) { throw new Error("Case mismatch of file "+ pathname+": found "+this.getCanonicalFile().getName()); + } } } 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 828327aed..1c404ffb4 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 @@ -307,7 +307,18 @@ public class Utilities { CSFile src = new CSFile(sourceFolder); if (!src.exists()) throw new FHIRException("Folder " + sourceFolder + " not found"); - createDirectory(destFolder); + File dst = new File(destFolder); + if(!dst.getCanonicalFile().getName().equals(dst.getName())) { + File tmp = new File(destFolder+System.currentTimeMillis()); + if (!dst.renameTo(tmp)) { + throw new IOException("fixing case from "+dst.getCanonicalFile().getName()+" to "+tmp.getName()+" failed"); + } + if (!tmp.renameTo(dst)) { + throw new IOException("fixing case from "+tmp.getCanonicalFile().getName()+" to "+dst.getName()+" failed"); + } + } else if (!dst.exists()) { + createDirectory(destFolder); + } String[] files = src.list(); for (String f : files) { @@ -318,7 +329,7 @@ public class Utilities { } else { if (notifier != null) notifier.copyFile(sourceFolder + File.separator + f, destFolder + File.separator + f); - copyFile(new CSFile(sourceFolder + File.separator + f), new CSFile(destFolder + File.separator + f)); + copyFile(new CSFile(sourceFolder + File.separator + f), new /*CS*/File(destFolder + File.separator + f)); // case doesn't have to match on the target } } } @@ -358,6 +369,10 @@ public class Utilities { createDirectory(destFile.getParent()); } destFile.createNewFile(); + } else if (!destFile.getCanonicalFile().getName().equals(destFile.getName())) { + // case mismatch + destFile.delete(); + destFile.createNewFile(); } FileInputStream source = null; diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java index da5f1f3a3..fd00f3449 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java @@ -422,5 +422,46 @@ class UtilitiesTest { Assertions.assertFalse("\u0009\n\u000B\u000C\r\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000".matches(".+")); Assertions.assertFalse("\u0009\n\u000B\u000C\r\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000".matches("^.+$")); } - + + @Test + @DisplayName("directory copy case tests") + void testFDirectoryCopy() throws IOException { + String src = Utilities.path("[tmp]", "test", "copy-source"); + String dst = Utilities.path("[tmp]", "test", "copy-dest"); + makeDir (src); + makeFile(Utilities.path(src, "Test.txt"), "source1"); + makeDir (Utilities.path(src, "SUB")); + makeFile(Utilities.path(src, "SUB", "TEST.txt"), "source2"); + + makeDir (dst); + makeFile(Utilities.path(dst, "test.txt"), "dest1"); + makeDir (Utilities.path(dst, "sub")); + makeFile(Utilities.path(dst, "sub", "test.txt"), "dest2"); + + Utilities.copyDirectory(src, dst, null); + + checkDir (dst); + checkFile(Utilities.path(dst, "Test.txt"), "source1"); + checkDir (Utilities.path(dst, "SUB")); + checkFile(Utilities.path(dst, "SUB", "TEST.txt"), "source2"); + } + + private void checkFile(String path, String content) throws IOException { + Assertions.assertTrue(new CSFile(path).exists()); + Assertions.assertEquals(content, TextFile.fileToString(path)); + } + + private void checkDir(String path) throws IOException { + Assertions.assertTrue(new CSFile(path).exists()); + } + + private void makeFile(String path, String content) throws IOException { + TextFile.stringToFile(content, path); + } + + private void makeDir(String path) throws IOException { + Utilities.createDirectory(path); + Utilities.clearDirectory(path); + } } +