Skip to content

Commit

Permalink
Directory Separator char was not handled properly in linux. (#709)
Browse files Browse the repository at this point in the history
* Directory Separator char was not handled properly in linux.
It fixes error with Shell function: The system cannot find the file specified.

* Make StaticPhysicalPath return a path ending with DirectorySeparator char to keep compatibility.

* Replace Path combine that changes the path to the root path.

* Fix error with shell in Ubuntu. Executable delimiters are not supported and not needed.

(cherry picked from commit 85fe332)
  • Loading branch information
claudiamurialdo committed Feb 3, 2023
1 parent d784c7d commit e7da0a0
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 31 deletions.
4 changes: 2 additions & 2 deletions dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void CreateXmlTextReader (string URL)
else
{
string sBaseDirectory = GxContext.StaticPhysicalPath();
if (!sBaseDirectory.EndsWith("\\")) sBaseDirectory += '\\';
if (!sBaseDirectory.EndsWith(Path.DirectorySeparatorChar)) sBaseDirectory += Path.DirectorySeparatorChar;
Uri baseUri = new Uri( sBaseDirectory );
Resolver.Myself = new Uri(baseUri, URL);
XMLInput = URL;
Expand Down Expand Up @@ -263,7 +263,7 @@ public void OpenFromString(string s)
Close();
EntitiesContainer.Reset();
string sBaseDirectory = GxContext.StaticPhysicalPath();
if (!sBaseDirectory.EndsWith("\\")) sBaseDirectory += '\\';
if (!sBaseDirectory.EndsWith(Path.DirectorySeparatorChar)) sBaseDirectory += Path.DirectorySeparatorChar;
Uri baseUri = new Uri( sBaseDirectory );
Resolver.Myself = baseUri;
CreateXMLSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public int Shell(string commandString, int modal, int redirectOutput)
{
if (Path.IsPathRooted(file))
{
p.StartInfo.WorkingDirectory = "\"" + Path.GetDirectoryName(file) + "\"";
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(file);
}
else
{
Expand All @@ -145,7 +145,7 @@ public int Shell(string commandString, int modal, int redirectOutput)
}
try
{
p.StartInfo.FileName = "\"" + file + "\"";
p.StartInfo.FileName = file;
}
catch (Exception e)
{
Expand Down
29 changes: 14 additions & 15 deletions dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2892,21 +2892,17 @@ public string GetPhysicalPath()
{
if (HttpContext != null)
{
string phPath = HttpHelper.RequestPhysicalApplicationPath(_HttpContext);
if (phPath.EndsWith("\\") || phPath.EndsWith("/"))
_physicalPath = phPath;
else
_physicalPath = phPath + "/";
_physicalPath = RequestPhysicalPath();
}
else
{
_physicalPath = "";
_physicalPath = String.Empty;
}
}
catch (Exception ex)
{
GXLogging.Debug(log, "GetPhysicalPath error", ex);
_physicalPath = "";
_physicalPath = String.Empty;
}
}
return _physicalPath;
Expand Down Expand Up @@ -2942,18 +2938,22 @@ public static bool IsHttpContext
}
#endif
}

private static string RequestPhysicalPath()
{
string phPath = HttpHelper.RequestPhysicalApplicationPath();
string dirSeparator = Path.DirectorySeparatorChar.ToString();
if (!phPath.EndsWith(dirSeparator))
return $"{phPath}{dirSeparator}";
else
return phPath;
}
public static string StaticPhysicalPath()
{
try
{
if (IsHttpContext)
{
string phPath = HttpHelper.RequestPhysicalApplicationPath();
if (phPath.EndsWith("\\") || phPath.EndsWith("/"))
return phPath;
else
return phPath + "/";
return RequestPhysicalPath();
}
else if (IsRestService)
{
Expand All @@ -2963,7 +2963,6 @@ public static string StaticPhysicalPath()
{
return _physicalPath;
}

else
{
return Directory.GetCurrentDirectory();
Expand All @@ -2972,7 +2971,7 @@ public static string StaticPhysicalPath()
}
catch
{
return "";
return string.Empty;
}

}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3836,7 +3836,7 @@ public static string EnumerationDescription(int domainNumber, string domainValue
if (domains == null)
{
domains = new Hashtable();
DOMAINS_FILE = GxContext.StaticPhysicalPath() + Path.DirectorySeparatorChar + "domains.ini";
DOMAINS_FILE = Path.Combine(GxContext.StaticPhysicalPath(), "domains.ini");
}
if (domains[domainId] == null)
{
Expand Down
8 changes: 4 additions & 4 deletions dotnet/src/dotnetframework/GxClasses/Core/gxconfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,10 @@ static NameValueCollection config
}
#if !NETCORE
if (GxContext.IsHttpContext &&
File.Exists(GxContext.StaticPhysicalPath() + "web.config"))
File.Exists(Path.Combine(GxContext.StaticPhysicalPath(), "web.config")))
{
logConfig(null);
if (log.IsDebugEnabled) loadedConfigFile = GxContext.StaticPhysicalPath() + "web.config";
if (log.IsDebugEnabled) loadedConfigFile = Path.Combine(GxContext.StaticPhysicalPath(), "web.config");
_config = ConfigurationSettings.AppSettings;
foreach (string key in _config.Keys)
{
Expand All @@ -529,13 +529,13 @@ static NameValueCollection config
return _config;
}
if (GxContext.IsHttpContext &&
File.Exists(GxContext.StaticPhysicalPath() + "bin/client.exe.config"))
File.Exists(Path.Combine(GxContext.StaticPhysicalPath(), "bin", "client.exe.config")))

{

logConfig("bin/log.config");
if (log.IsDebugEnabled)
loadedConfigFile = GxContext.StaticPhysicalPath() + "bin/client.exe.config";
loadedConfigFile = Path.Combine(GxContext.StaticPhysicalPath(), "bin", "client.exe.config");
_config = loadConfig("bin/client.exe.config");
return _config;
}
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private XmlTextReader CreateXmlTextReader (string URL)
#pragma warning restore SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}'
string sBaseDirectory = GxContext.StaticPhysicalPath();

if (!sBaseDirectory.EndsWith("\\")) sBaseDirectory += '\\';
if (!sBaseDirectory.EndsWith(Path.DirectorySeparatorChar.ToString())) sBaseDirectory += Path.DirectorySeparatorChar;
Uri baseUri = new Uri( sBaseDirectory );
Resolver.Myself = new Uri(baseUri, URL);
return new XmlTextReader (sr);
Expand Down Expand Up @@ -286,7 +286,7 @@ public void OpenFromString(string s)

string sBaseDirectory = GxContext.StaticPhysicalPath();

if (!sBaseDirectory.EndsWith("\\")) sBaseDirectory += '\\';
if (!sBaseDirectory.EndsWith(Path.DirectorySeparatorChar.ToString())) sBaseDirectory += Path.DirectorySeparatorChar;
Uri baseUri = new Uri( sBaseDirectory );
Resolver.Myself = baseUri;
treader = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static public DataUpdateStatus CheckDataStatus(string queryId, DateTime dateLast

static void loadQueryTables()
{
string basePath = Path.Combine(GxContext.StaticPhysicalPath(), "Metadata\\TableAccess");
string basePath = Path.Combine(GxContext.StaticPhysicalPath(), "Metadata", "TableAccess");
queryTables = new ConcurrentDictionary<string, List<string>>();
if (Directory.Exists(basePath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public UserControlGenerator(string ucType)

private static string GetTemplateFile(string type)
{
return Path.Combine(GxContext.StaticPhysicalPath(), $"gxusercontrols\\{type}.view");
return Path.Combine(GxContext.StaticPhysicalPath(), "gxusercontrols", $"{type}.view");
}

internal string Render(string internalName, GxDictionary propbag)
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/dotnetframework/GxExcel/GxExcelLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public short Init(string previousMsgError)
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "GemBox.Spreadsheet.dll"));
if (ass==null)
{
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), @"bin\GemBox.Spreadsheet.dll"));
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", "GemBox.Spreadsheet.dll"));
}
if (ass!=null)
{
Expand All @@ -398,7 +398,7 @@ public short Init(string previousMsgError)
}
if (ass==null)
{
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), @"bin\GemBox.ExcelLite.dll"));
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", "GemBox.ExcelLite.dll"));
}
if (ass!=null)
{
Expand All @@ -412,7 +412,7 @@ public short Init(string previousMsgError)
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), nmspace + ".dll"));
if (ass==null)
{
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), @"bin\" + nmspace + ".dll"));
ass = loadAssembly(Path.Combine(GxContext.StaticPhysicalPath(), "bin", nmspace + ".dll"));
}
}
GXLogging.Debug(log, "nmspace:" + nmspace);
Expand Down
1 change: 1 addition & 0 deletions dotnet/test/DotNetCoreUnitTest/DotNetCoreUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<Compile Include="..\DotNetUnitTest\ConfigMappings\ConfigTest.cs" Link="ConfigMappings\ConfigTest.cs" />
<Compile Include="..\DotNetUnitTest\Domain\GxHttpClientTest.cs" Link="Domain\GxHttpClientTest.cs" />
<Compile Include="..\DotNetUnitTest\Domain\ShellTest.cs" Link="Domain\ShellTest.cs" />
<Compile Include="..\DotNetUnitTest\FileIO\DfrgFunctions.cs" Link="FileIO\DfrgFunctions.cs" />
<Compile Include="..\DotNetUnitTest\FileIO\FileSystemTest.cs" Link="FileIO\FileSystemTest.cs" />
<Compile Include="..\DotNetUnitTest\FileIO\Xslt.cs" Link="FileIO\Xslt.cs" />
Expand Down
38 changes: 38 additions & 0 deletions dotnet/test/DotNetUnitTest/Domain/ShellTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using GeneXus.Utils;
using Xunit;

namespace xUnitTesting
{

public class ShellTest
{
[Fact]
public void ExecutableTest()
{
string fileName = "hello.bat";
File.WriteAllText(fileName, "echo \"hello %1\"");
int errorCode = GXUtil.Shell($"{fileName} test", 1, 1);
Assert.Equal(0, errorCode);
}
[Fact]
public void ExecutableWithSpacesTest()
{
string fileName = "hello world.bat";
File.WriteAllText(fileName, "echo \"hello %1\"");
int errorCode = GXUtil.Shell($"'{fileName}' test", 1, 1);
Assert.Equal(0, errorCode);
}

[Fact]
public void WorkingDirWithSpacesTest()
{
string fileName = Path.Combine(Directory.GetCurrentDirectory(), "my dir", "hello world.bat");
FileInfo fi = new FileInfo(fileName);
Directory.CreateDirectory(fi.DirectoryName);
File.WriteAllText(fileName, "echo \"hello %1\"");
int errorCode = GXUtil.Shell($"'{fileName}' test", 1, 1);
Assert.Equal(0, errorCode);
}
}
}
1 change: 1 addition & 0 deletions dotnet/test/DotNetUnitTest/DotNetUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\dotnetframework\GxClasses.Win\GxClasses.Win.csproj" />
<ProjectReference Include="..\..\src\dotnetframework\GxClasses\GxClasses.csproj" />
<ProjectReference Include="..\..\src\dotnetframework\GxMail\GxMail.csproj" />
<ProjectReference Include="..\..\src\dotnetframework\GxPdfReportsCS\GxPdfReportsCS.csproj" />
Expand Down

0 comments on commit e7da0a0

Please sign in to comment.