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

This commit is contained in:
Erick Erickson 2017-05-29 20:05:03 -07:00
parent 963f43f6c0
commit 412e4ae2c1
3 changed files with 27 additions and 1 deletions

View File

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

View File

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

View File

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