Apache Lucene open-source search software
SOLR-10719: Creating a core.properties fails if the parent of core.properties is a symlinked dierctory
Erick Erickson412e4ae2c192
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex c413cf82b51..3661037ca77 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -241,6 +241,9 @@ Bug Fixes* SOLR-10723 JSON Facet API: resize() implemented incorrectly for CountSlotAcc, HllAgg.NumericAccresulting in exceptions when using a hashing faceting method and sorting by hll(numeric_field).(yonik)++* SOLR-10719: Creating a core.properties fails if the parent of core.properties is a symlinked dierctory+ (Erick Erickson)Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/core/CorePropertiesLocator.java b/solr/core/src/java/org/apache/solr/core/CorePropertiesLocator.javaindex e942c9b90ee..99c101bfac0 100644--- a/solr/core/src/java/org/apache/solr/core/CorePropertiesLocator.java+++ b/solr/core/src/java/org/apache/solr/core/CorePropertiesLocator.java@@ -39,6 +39,7 @@ import java.util.stream.Collectors;import com.google.common.collect.Lists;import org.apache.solr.common.SolrException;+import org.apache.solr.util.FileUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -85,13 +86,15 @@ public class CorePropertiesLocator implements CoresLocator {private void writePropertiesFile(CoreDescriptor cd, Path propfile) {Properties p = buildCoreProperties(cd);try {- Files.createDirectories(propfile.getParent());+ FileUtils.createDirectories(propfile.getParent()); // Handling for symlinks.try (Writer os = new OutputStreamWriter(Files.newOutputStream(propfile), StandardCharsets.UTF_8)) {p.store(os, "Written by CorePropertiesLocator");}}catch (IOException e) {logger.error("Couldn't persist core properties to {}: {}", propfile, e.getMessage());+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "Couldn't persist core properties to " + propfile.toAbsolutePath().toString() + " : " + e.getMessage());}}diff --git a/solr/core/src/java/org/apache/solr/util/FileUtils.java b/solr/core/src/java/org/apache/solr/util/FileUtils.javaindex 09db4f0f686..20462625d55 100644--- a/solr/core/src/java/org/apache/solr/util/FileUtils.java+++ b/solr/core/src/java/org/apache/solr/util/FileUtils.java@@ -18,6 +18,10 @@ package org.apache.solr.util;import java.io.*;import java.nio.channels.FileChannel;+import java.nio.file.Files;+import java.nio.file.Path;++import org.apache.commons.io.FileExistsException;/***@@ -96,4 +100,20 @@ public class FileUtils {public static boolean fileExists(String filePathString) {return new File(filePathString).exists();}++ // Files.createDirectories has odd behavior if the path is a symlink and it already exists+ // _even if it's a symlink to a directory_.+ //+ // oddly, if the path to be created just contains a symlink in intermediate levels, Files.createDirectories+ // works just fine.+ //+ // This works around that issue+ public static Path createDirectories(Path path) throws IOException {+ if (Files.exists(path) && Files.isSymbolicLink(path)) {+ Path real = path.toRealPath();+ if (Files.isDirectory(real)) return real;+ throw new FileExistsException("Tried to create a directory at to an existing non-directory symlink: " + path.toString());+ }+ return Files.createDirectories(path);+ }}