fix up copy directory for case differences

This commit is contained in:
Grahame Grieve 2023-08-02 19:58:24 +10:00
parent 32f2f6acc7
commit 882d18251b
3 changed files with 61 additions and 4 deletions

View File

@ -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());
}
}
}

View File

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

View File

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