You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by cp...@apache.org on 2017/05/30 11:28:26 UTC

[17/25] lucene-solr:jira/solr-8668: SOLR-10719: Creating a core.properties fails if the parent of core.properties is a symlinked dierctory

SOLR-10719: Creating a core.properties fails if the parent of core.properties is a symlinked dierctory


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/412e4ae2
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/412e4ae2
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/412e4ae2

Branch: refs/heads/jira/solr-8668
Commit: 412e4ae2c192a5b444dd63220e5918f4e7fd47be
Parents: 963f43f
Author: Erick Erickson <er...@apache.org>
Authored: Mon May 29 20:05:03 2017 -0700
Committer: Erick Erickson <er...@apache.org>
Committed: Mon May 29 20:08:44 2017 -0700

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  3 +++
 .../apache/solr/core/CorePropertiesLocator.java |  5 ++++-
 .../java/org/apache/solr/util/FileUtils.java    | 20 ++++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/412e4ae2/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index c413cf8..3661037 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.NumericAcc
   resulting 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
 ----------------------

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/412e4ae2/solr/core/src/java/org/apache/solr/core/CorePropertiesLocator.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/core/CorePropertiesLocator.java b/solr/core/src/java/org/apache/solr/core/CorePropertiesLocator.java
index e942c9b..99c101b 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());
     }
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/412e4ae2/solr/core/src/java/org/apache/solr/util/FileUtils.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/util/FileUtils.java b/solr/core/src/java/org/apache/solr/util/FileUtils.java
index 09db4f0..2046262 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);
+  }
 }