Skip to content

Commit 4e706fd

Browse files
committed
Improved determination of indexes path when indexBase use parent path syntax (eg. "../indexes")
1 parent 808b8b3 commit 4e706fd

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/NHibernate.Search.Tests/Util/DirectoryHelperTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace NHibernate.Search.Tests.Util
44
{
5+
using System;
56
using System.IO;
7+
using System.Linq;
68
using System.Threading;
79

810
using NUnit.Framework;
@@ -55,6 +57,22 @@ public void CreateViaRelative()
5557
Assert.IsTrue(info.Exists);
5658
}
5759

60+
[Test]
61+
public void CreateViaParent()
62+
{
63+
var properties = new Dictionary<string, string>();
64+
properties["indexBase"] = "../Wilma";
65+
properties["indexName"] = "fakeIndex";
66+
67+
DirectoryInfo info = DirectoryProviderHelper.DetermineIndexDir(null, properties);
68+
69+
// get process execution path
70+
DirectoryInfo targetParentDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
71+
72+
// check if a directory info object was returned and "Wilma" folder was created into parent folder of execution process
73+
Assert.IsTrue(targetParentDir.Parent.GetDirectories().Any(d => d.Name.Equals(info.Parent.Name)));
74+
}
75+
5876
#region Helper methods
5977

6078
[SetUp]

src/NHibernate.Search/Util/DirectoryProviderHelper.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,37 @@ public static string GetSourceDirectory(string rootPropertyName, string relative
8888

8989
public static DirectoryInfo DetermineIndexDir(String directoryProviderName, IDictionary properties)
9090
{
91-
string indexBase = (string) properties["indexBase"] ?? ".";
92-
string indexName = (string) properties["indexName"] ?? directoryProviderName;
91+
string indexBase = (string)properties["indexBase"];
92+
93+
if (indexBase == null)
94+
{
95+
indexBase = ".";
96+
}
97+
else if (indexBase.StartsWith("~"))
98+
{
99+
// We need this to allow using the search from the web, where the "." directory is somewhere in the system root.
100+
indexBase = indexBase.Replace("~", AppDomain.CurrentDomain.BaseDirectory);
101+
}
102+
else if (indexBase.StartsWith(".."))
103+
{
104+
// determine the indexBase path when using parent directory (eg. "../indexes")
105+
106+
DirectoryInfo targetParentDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
107+
108+
string path = indexBase;
109+
110+
while (path.StartsWith(".."))
111+
{
112+
if (targetParentDir.Parent == null)
113+
throw new HibernateException("IndexBase path not valid");
114+
115+
targetParentDir = targetParentDir.Parent;
116+
path = path.Remove(0, 3);
117+
}
118+
119+
indexBase = Path.Combine(targetParentDir.FullName, path);
120+
}
93121

94-
// We need this to allow using the search from the web, where the "." directory is somewhere in the system root.
95-
indexBase = indexBase.Replace("~", AppDomain.CurrentDomain.BaseDirectory);
96122
DirectoryInfo indexDir = new DirectoryInfo(indexBase);
97123
if (!indexDir.Exists)
98124
{
@@ -105,8 +131,9 @@ public static DirectoryInfo DetermineIndexDir(String directoryProviderName, IDic
105131
throw new HibernateException("Cannot write into index directory: " + indexBase);
106132
}
107133

108-
indexDir = new DirectoryInfo(Path.Combine(indexDir.FullName, indexName));
109-
return indexDir;
134+
string indexName = (string)properties["indexName"] ?? directoryProviderName;
135+
136+
return new DirectoryInfo(Path.Combine(indexDir.FullName, indexName));
110137
}
111138

112139
private static bool HasWriteAccess(DirectoryInfo indexDir)

0 commit comments

Comments
 (0)