Support symlinks in configtree property paths

Prior to this commit, the configtree property source would not traverse
into a sub-directory in a property path if the sub-directory was a
symbolic link. This commit allows symlinked sub-directories to be
traversed like any other sub-directory in the property path.

Fixes gh-24530
pull/24986/head
Scott Frederick 4 years ago
parent 615a8ae56e
commit 505340909a

@ -19,6 +19,7 @@ package org.springframework.boot.env;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
@ -203,15 +204,16 @@ public class ConfigTreePropertySource extends EnumerablePropertySource<Path> imp
static Map<String, PropertyFile> findAll(Path sourceDirectory, Set<Option> options) {
try {
Map<String, PropertyFile> propertyFiles = new TreeMap<>();
Files.find(sourceDirectory, MAX_DEPTH, PropertyFile::isPropertyFile).forEach((path) -> {
String name = getName(sourceDirectory.relativize(path));
if (StringUtils.hasText(name)) {
if (options.contains(Option.USE_LOWERCASE_NAMES)) {
name = name.toLowerCase();
}
propertyFiles.put(name, new PropertyFile(path, options));
}
});
Files.find(sourceDirectory, MAX_DEPTH, PropertyFile::isPropertyFile, FileVisitOption.FOLLOW_LINKS)
.forEach((path) -> {
String name = getName(sourceDirectory.relativize(path));
if (StringUtils.hasText(name)) {
if (options.contains(Option.USE_LOWERCASE_NAMES)) {
name = name.toLowerCase();
}
propertyFiles.put(name, new PropertyFile(path, options));
}
});
return Collections.unmodifiableMap(propertyFiles);
}
catch (IOException ex) {

@ -88,6 +88,15 @@ class ConfigTreePropertySourceTests {
assertThat(propertySource.getPropertyNames()).containsExactly("c", "fa.a", "fa.b", "fb.a", "fb.fa.a");
}
@Test
void getPropertyNamesFromNestedWithSymlinkInPathReturnsPropertyNames() throws Exception {
addNested();
Path symlinkTempDir = Files.createSymbolicLink(this.directory.resolveSibling("symlinkTempDir"), this.directory);
ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", symlinkTempDir);
Files.delete(symlinkTempDir);
assertThat(propertySource.getPropertyNames()).containsExactly("c", "fa.a", "fa.b", "fb.a", "fb.fa.a");
}
@Test
void getPropertyNamesFromFlatWithSymlinksIgnoresHiddenFiles() throws Exception {
ConfigTreePropertySource propertySource = getSymlinkedFlatPropertySource();

Loading…
Cancel
Save