From d80670f1ca69930c48a0519f515dd90c61eca659 Mon Sep 17 00:00:00 2001 From: Andrea Aime Date: Thu, 25 Apr 2024 19:33:00 +0200 Subject: [PATCH] [GEOS-11332] Renaming style with uppercase/downcase empty the sld file --- .../src/main/java/org/geoserver/util/IOUtils.java | 7 +++++-- .../src/test/java/org/geoserver/util/IOUtilsTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/platform/src/main/java/org/geoserver/util/IOUtils.java b/src/platform/src/main/java/org/geoserver/util/IOUtils.java index de75a528f79..188041704b1 100644 --- a/src/platform/src/main/java/org/geoserver/util/IOUtils.java +++ b/src/platform/src/main/java/org/geoserver/util/IOUtils.java @@ -30,6 +30,7 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.io.FileUtils; +import org.geoserver.platform.resource.Files; import org.geotools.util.logging.Logging; /** @@ -470,7 +471,7 @@ public static void rename(File f, String newName) throws IOException { */ public static void rename(File source, File dest) throws IOException { // same path? Do nothing - if (source.getCanonicalPath().equalsIgnoreCase(dest.getCanonicalPath())) return; + if (source.getCanonicalPath().equals(dest.getCanonicalPath())) return; // windows needs special treatment, we cannot rename onto an existing file boolean win = System.getProperty("os.name").startsWith("Windows"); @@ -482,7 +483,9 @@ public static void rename(File source, File dest) throws IOException { } } // make sure the rename actually succeeds - if (!source.renameTo(dest)) { + // using Files.move() instead of File.renameTo() to get better cross-platform support + // see also the javadoc of File.renameTo for more details + if (!Files.move(source, dest)) { FileUtils.deleteQuietly(dest); if (source.isDirectory()) { FileUtils.moveDirectory(source, dest); diff --git a/src/platform/src/test/java/org/geoserver/util/IOUtilsTest.java b/src/platform/src/test/java/org/geoserver/util/IOUtilsTest.java index 512de069d80..a8802e170e5 100644 --- a/src/platform/src/test/java/org/geoserver/util/IOUtilsTest.java +++ b/src/platform/src/test/java/org/geoserver/util/IOUtilsTest.java @@ -70,4 +70,15 @@ public void testDecompressFileBadEntryName() throws IOException { assertThat(e.getMessage(), startsWith("Entry is outside of the target directory")); } } + + @Test + public void testRenameCaseChange() throws IOException { + File f = temp.newFile("foo.txt"); + IOUtils.rename(f, "FOO.txt"); + File renamed = new File(f.getParent(), "FOO.txt"); + + // file system can be case sensitive or not, so we can't really test + // that the old file is gone, but the new file should be there + assertTrue(renamed.exists()); + } }