Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergiy Kuryata <sergeyk@microsoft.com>2017-05-12 19:57:58 +0300
committerJan Kotas <jkotas@microsoft.com>2017-05-12 19:57:58 +0300
commit4d2311a91d6a4ce59917d88305fec6ed8e54a026 (patch)
tree3def2229d19d334795f911cf1451cc3f480ea940 /src/System.Private.CoreLib
parenteac8575a8d54d7f190908adf47a518b1fb211ce7 (diff)
Implement AppContext.BaseDirectory for Linux (#3588)
Diffstat (limited to 'src/System.Private.CoreLib')
-rw-r--r--src/System.Private.CoreLib/src/System.Private.CoreLib.csproj3
-rw-r--r--src/System.Private.CoreLib/src/System/AppContext.Unix.cs38
-rw-r--r--src/System.Private.CoreLib/src/System/AppContext.Windows.cs38
-rw-r--r--src/System.Private.CoreLib/src/System/AppContext.cs12
4 files changed, 70 insertions, 21 deletions
diff --git a/src/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/System.Private.CoreLib/src/System.Private.CoreLib.csproj
index 1b992a98e..c080ce9d6 100644
--- a/src/System.Private.CoreLib/src/System.Private.CoreLib.csproj
+++ b/src/System.Private.CoreLib/src/System.Private.CoreLib.csproj
@@ -658,6 +658,9 @@
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.GetEnv.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.GetEnv.cs</Link>
</Compile>
+ <Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.GetExecutableAbsolutePath.cs">
+ <Link>Interop\Unix\System.Private.CoreLib.Native\Interop.GetExecutableAbsolutePath.cs</Link>
+ </Compile>
<Compile Include="..\..\Common\src\Interop\Unix\System.Private.CoreLib.Native\Interop.MemAllocFree.cs">
<Link>Interop\Unix\System.Private.CoreLib.Native\Interop.MemAllocFree.cs</Link>
</Compile>
diff --git a/src/System.Private.CoreLib/src/System/AppContext.Unix.cs b/src/System.Private.CoreLib/src/System/AppContext.Unix.cs
index 9a59c759e..6067804e8 100644
--- a/src/System.Private.CoreLib/src/System/AppContext.Unix.cs
+++ b/src/System.Private.CoreLib/src/System/AppContext.Unix.cs
@@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Buffers;
using System.Collections;
using System.Collections.Generic;
+using System.Diagnostics;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
@@ -13,11 +15,41 @@ namespace System
{
public static partial class AppContext
{
- public static string BaseDirectory
+ /// <summary>
+ /// Return the directory of the executable image for the current process
+ /// as the default value for AppContext.BaseDirectory
+ /// </summary>
+ private static string GetBaseDirectoryCore()
{
- get
+ // Start with a relatively small buffer
+ int currentSize = 256;
+ for (;;)
{
- throw new NotImplementedException();
+ char[] buffer = ArrayPool<char>.Shared.Rent(currentSize);
+
+ // Get full path to the executable image
+ int actualSize = Interop.Sys.GetExecutableAbsolutePath(buffer, buffer.Length);
+
+ if (actualSize < 0)
+ {
+ // The call to GetExecutableAbsolutePath function failed.
+ Interop.ErrorInfo error = Interop.Sys.GetLastErrorInfo();
+ ArrayPool<char>.Shared.Return(buffer);
+ throw Interop.GetExceptionForIoErrno(error);
+ }
+
+ Debug.Assert(actualSize > 0);
+ if (actualSize <= buffer.Length)
+ {
+ string fileName = new string(buffer, 0, actualSize);
+ ArrayPool<char>.Shared.Return(buffer);
+
+ // Return path to the executable image including the terminating slash
+ return fileName.Substring(0, fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1);
+ }
+
+ ArrayPool<char>.Shared.Return(buffer);
+ currentSize = actualSize;
}
}
}
diff --git a/src/System.Private.CoreLib/src/System/AppContext.Windows.cs b/src/System.Private.CoreLib/src/System/AppContext.Windows.cs
index d14936129..7d535c5de 100644
--- a/src/System.Private.CoreLib/src/System/AppContext.Windows.cs
+++ b/src/System.Private.CoreLib/src/System/AppContext.Windows.cs
@@ -13,29 +13,31 @@ namespace System
{
public static partial class AppContext
{
- public static string BaseDirectory
+ /// <summary>
+ /// Return the directory of the executable image for the current process
+ /// as the default value for AppContext.BaseDirectory
+ /// </summary>
+ private static string GetBaseDirectoryCore()
{
- get
+ StringBuilder buffer = new StringBuilder(Interop.mincore.MAX_PATH);
+ while (true)
{
- StringBuilder buffer = new StringBuilder(Interop.mincore.MAX_PATH);
- while (true)
+ int size = Interop.mincore.GetModuleFileName(IntPtr.Zero, buffer, buffer.Capacity);
+ if (size == 0)
{
- int size = Interop.mincore.GetModuleFileName(IntPtr.Zero, buffer, buffer.Capacity);
- if (size == 0)
- {
- throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
- }
-
- if (Marshal.GetLastWin32Error() == Interop.mincore.ERROR_INSUFFICIENT_BUFFER)
- {
- // Enlarge the buffer and try again.
- buffer.EnsureCapacity(buffer.Capacity * 2);
- continue;
- }
+ throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
+ }
- string fileName = buffer.ToString();
- return fileName.Substring(0, fileName.LastIndexOf('\\'));
+ if (Marshal.GetLastWin32Error() == Interop.mincore.ERROR_INSUFFICIENT_BUFFER)
+ {
+ // Enlarge the buffer and try again.
+ buffer.EnsureCapacity(buffer.Capacity * 2);
+ continue;
}
+
+ // Return path to the executable image including the terminating slash
+ string fileName = buffer.ToString();
+ return fileName.Substring(0, fileName.LastIndexOf('\\') + 1);
}
}
}
diff --git a/src/System.Private.CoreLib/src/System/AppContext.cs b/src/System.Private.CoreLib/src/System/AppContext.cs
index cd688f2ba..5b593b70c 100644
--- a/src/System.Private.CoreLib/src/System/AppContext.cs
+++ b/src/System.Private.CoreLib/src/System/AppContext.cs
@@ -23,6 +23,7 @@ namespace System
}
private static readonly Dictionary<string, SwitchValueState> s_switchMap = new Dictionary<string, SwitchValueState>();
private static Dictionary<String, Object> s_localStore = new Dictionary<String, Object>();
+ private static string s_defaultBaseDirectory;
static AppContext()
{
@@ -38,6 +39,17 @@ namespace System
}
}
+ public static string BaseDirectory
+ {
+ get
+ {
+ // The value of APP_CONTEXT_BASE_DIRECTORY key has to be a string and it is not allowed to be any other type.
+ // Otherwise the caller will get invalid cast exception
+ return (string)GetData("APP_CONTEXT_BASE_DIRECTORY") ??
+ (s_defaultBaseDirectory ?? (s_defaultBaseDirectory = GetBaseDirectoryCore()));
+ }
+ }
+
public static object GetData(string name)
{
if (name == null)