2016-03-30 23:26:05 +02:00
|
|
|
/********************************************************************++
|
|
|
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
--********************************************************************/
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.InteropServices;
|
2016-10-04 00:22:28 +02:00
|
|
|
using System.ComponentModel;
|
2016-03-30 23:26:05 +02:00
|
|
|
using Microsoft.Win32;
|
|
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
namespace System.Management.Automation
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// These are platform abstractions and platform specific implementations
|
|
|
|
/// </summary>
|
2016-07-05 23:16:35 +02:00
|
|
|
public static class Platform
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-07-19 03:23:29 +02:00
|
|
|
/// <summary>
|
|
|
|
/// True if the current platform is Linux.
|
|
|
|
/// </summary>
|
2016-04-09 00:08:16 +02:00
|
|
|
public static bool IsLinux
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-04-08 23:54:41 +02:00
|
|
|
get
|
|
|
|
{
|
2016-08-13 00:29:37 +02:00
|
|
|
#if CORECLR
|
2016-04-08 23:54:41 +02:00
|
|
|
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
2016-08-13 00:29:37 +02:00
|
|
|
#else
|
2016-04-08 23:54:41 +02:00
|
|
|
return false;
|
2016-08-13 00:29:37 +02:00
|
|
|
#endif
|
2016-04-08 23:54:41 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 22:37:27 +02:00
|
|
|
|
2016-07-19 03:23:29 +02:00
|
|
|
/// <summary>
|
|
|
|
/// True if the current platform is OS X.
|
|
|
|
/// </summary>
|
2016-04-09 00:08:16 +02:00
|
|
|
public static bool IsOSX
|
2016-04-08 23:54:41 +02:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-08-13 00:29:37 +02:00
|
|
|
#if CORECLR
|
2016-04-08 23:54:41 +02:00
|
|
|
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
2016-08-13 00:29:37 +02:00
|
|
|
#else
|
2016-04-08 23:54:41 +02:00
|
|
|
return false;
|
2016-08-13 00:29:37 +02:00
|
|
|
#endif
|
2016-04-08 23:54:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 03:23:29 +02:00
|
|
|
/// <summary>
|
|
|
|
/// True if the current platform is Windows.
|
|
|
|
/// </summary>
|
2016-04-09 00:08:16 +02:00
|
|
|
public static bool IsWindows
|
2016-04-08 23:54:41 +02:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-08-13 00:29:37 +02:00
|
|
|
#if CORECLR
|
2016-04-08 23:54:41 +02:00
|
|
|
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
2016-08-13 00:29:37 +02:00
|
|
|
#else
|
2016-04-08 23:54:41 +02:00
|
|
|
return true;
|
2016-08-13 00:29:37 +02:00
|
|
|
#endif
|
2016-04-08 23:54:41 +02:00
|
|
|
}
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 03:23:29 +02:00
|
|
|
/// <summary>
|
|
|
|
/// True if PowerShell was built targeting .NET Core.
|
|
|
|
/// </summary>
|
2016-07-22 01:52:22 +02:00
|
|
|
public static bool IsCoreCLR
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-04-09 00:02:51 +02:00
|
|
|
get
|
|
|
|
{
|
2016-08-13 00:29:37 +02:00
|
|
|
#if CORECLR
|
2016-04-09 00:02:51 +02:00
|
|
|
return true;
|
2016-08-13 00:29:37 +02:00
|
|
|
#else
|
2016-04-09 00:02:51 +02:00
|
|
|
return false;
|
2016-08-13 00:29:37 +02:00
|
|
|
#endif
|
2016-04-09 00:02:51 +02:00
|
|
|
}
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
2016-08-10 19:37:42 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// True if the underlying system is NanoServer.
|
|
|
|
/// </summary>
|
|
|
|
public static bool IsNanoServer
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
#if !CORECLR
|
|
|
|
return false;
|
|
|
|
#elif UNIX
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (_isNanoServer.HasValue) { return _isNanoServer.Value; }
|
|
|
|
|
|
|
|
_isNanoServer = false;
|
|
|
|
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels"))
|
|
|
|
{
|
|
|
|
if (regKey != null)
|
|
|
|
{
|
|
|
|
object value = regKey.GetValue("NanoServer");
|
|
|
|
if (value != null && regKey.GetValueKind("NanoServer") == RegistryValueKind.DWord)
|
|
|
|
{
|
|
|
|
_isNanoServer = (int)value == 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _isNanoServer.Value;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// True if the underlying system is IoT.
|
|
|
|
/// </summary>
|
|
|
|
public static bool IsIoT
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
#if !CORECLR
|
|
|
|
return false;
|
|
|
|
#elif UNIX
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (_isIoT.HasValue) { return _isIoT.Value; }
|
|
|
|
|
|
|
|
_isIoT = false;
|
|
|
|
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
|
|
|
|
{
|
|
|
|
if (regKey != null)
|
|
|
|
{
|
|
|
|
object value = regKey.GetValue("ProductName");
|
|
|
|
if (value != null && regKey.GetValueKind("ProductName") == RegistryValueKind.String)
|
|
|
|
{
|
|
|
|
_isIoT = string.Equals("IoTUAP", (string)value, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _isIoT.Value;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CORECLR
|
|
|
|
/// <summary>
|
|
|
|
/// True if it is the inbox powershell for NanoServer or IoT.
|
|
|
|
/// </summary>
|
|
|
|
internal static bool IsInbox
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
#if UNIX
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (_isInbox.HasValue) { return _isInbox.Value; }
|
|
|
|
|
|
|
|
_isInbox = false;
|
|
|
|
if (IsNanoServer || IsIoT)
|
|
|
|
{
|
|
|
|
_isInbox = string.Equals(
|
|
|
|
Utils.GetApplicationBase(Utils.DefaultPowerShellShellID),
|
|
|
|
Utils.GetApplicationBaseFromRegistry(Utils.DefaultPowerShellShellID),
|
|
|
|
StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _isInbox.Value;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !UNIX
|
|
|
|
private static bool? _isNanoServer = null;
|
|
|
|
private static bool? _isIoT = null;
|
|
|
|
private static bool? _isInbox = null;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
2016-03-30 23:26:05 +02:00
|
|
|
|
|
|
|
// format files
|
|
|
|
internal static List<string> FormatFileNames = new List<string>
|
|
|
|
{
|
|
|
|
"Certificate.format.ps1xml",
|
|
|
|
"Diagnostics.format.ps1xml",
|
|
|
|
"DotNetTypes.format.ps1xml",
|
|
|
|
"Event.format.ps1xml",
|
|
|
|
"FileSystem.format.ps1xml",
|
|
|
|
"Help.format.ps1xml",
|
|
|
|
"HelpV3.format.ps1xml",
|
|
|
|
"PowerShellCore.format.ps1xml",
|
|
|
|
"PowerShellTrace.format.ps1xml",
|
|
|
|
"Registry.format.ps1xml",
|
|
|
|
"WSMan.format.ps1xml"
|
|
|
|
};
|
|
|
|
|
2016-07-19 03:23:29 +02:00
|
|
|
/// <summary>
|
2016-08-10 19:37:42 +02:00
|
|
|
/// Some common environment variables used in PS have different
|
|
|
|
/// names in different OS platforms
|
|
|
|
/// </summary>
|
|
|
|
internal static class CommonEnvVariableNames
|
|
|
|
{
|
|
|
|
#if UNIX
|
|
|
|
internal const string Home = "HOME";
|
|
|
|
#else
|
|
|
|
internal const string Home = "USERPROFILE";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:22:23 +02:00
|
|
|
#if UNIX
|
2016-08-10 19:37:42 +02:00
|
|
|
/// <summary>
|
2016-07-19 03:23:29 +02:00
|
|
|
/// X Desktop Group configuration type enum.
|
|
|
|
/// </summary>
|
2016-08-11 01:34:07 +02:00
|
|
|
public enum XDG_Type
|
2016-07-19 03:23:29 +02:00
|
|
|
{
|
|
|
|
/// <summary> XDG_CONFIG_HOME/powershell </summary>
|
|
|
|
CONFIG,
|
|
|
|
/// <summary> XDG_CACHE_HOME/powershell </summary>
|
|
|
|
CACHE,
|
|
|
|
/// <summary> XDG_DATA_HOME/powershell </summary>
|
|
|
|
DATA,
|
|
|
|
/// <summary> XDG_DATA_HOME/powershell/Modules </summary>
|
2016-08-11 23:22:23 +02:00
|
|
|
USER_MODULES,
|
|
|
|
/// <summary> /usr/local/share/powershell/Modules </summary>
|
|
|
|
SHARED_MODULES,
|
2016-07-19 03:23:29 +02:00
|
|
|
/// <summary> XDG_CONFIG_HOME/powershell </summary>
|
|
|
|
DEFAULT
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// function for choosing directory location of PowerShell for profile loading
|
|
|
|
/// </summary>
|
2016-08-11 01:34:07 +02:00
|
|
|
public static string SelectProductNameForDirectory(Platform.XDG_Type dirpath)
|
2016-06-02 19:14:51 +02:00
|
|
|
{
|
2016-06-03 12:15:13 +02:00
|
|
|
//TODO: XDG_DATA_DIRS implementation as per GitHub issue #1060
|
|
|
|
|
2016-05-24 23:29:00 +02:00
|
|
|
string xdgconfighome = System.Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
|
2016-06-02 19:14:51 +02:00
|
|
|
string xdgdatahome = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME");
|
2016-05-24 23:29:00 +02:00
|
|
|
string xdgcachehome = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME");
|
2016-07-29 22:02:49 +02:00
|
|
|
string xdgConfigHomeDefault = Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), ".config", "powershell");
|
|
|
|
string xdgDataHomeDefault = Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), ".local", "share", "powershell");
|
|
|
|
string xdgModuleDefault = Path.Combine(xdgDataHomeDefault, "Modules");
|
|
|
|
string xdgCacheDefault = Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), ".cache", "powershell");
|
2016-06-06 22:37:27 +02:00
|
|
|
|
2016-07-29 22:02:49 +02:00
|
|
|
switch (dirpath)
|
|
|
|
{
|
2016-07-06 19:28:31 +02:00
|
|
|
case Platform.XDG_Type.CONFIG:
|
2016-08-26 22:46:03 +02:00
|
|
|
//the user has set XDG_CONFIG_HOME corresponding to profile path
|
2016-06-03 12:15:13 +02:00
|
|
|
if (String.IsNullOrEmpty(xdgconfighome))
|
2016-06-02 19:14:51 +02:00
|
|
|
{
|
2016-06-03 12:15:13 +02:00
|
|
|
//xdg values have not been set
|
2016-06-06 22:37:27 +02:00
|
|
|
return xdgConfigHomeDefault;
|
2016-06-01 20:03:05 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 19:14:51 +02:00
|
|
|
else
|
|
|
|
{
|
2016-06-06 22:37:27 +02:00
|
|
|
return Path.Combine(xdgconfighome, "powershell");
|
2016-05-24 23:29:00 +02:00
|
|
|
}
|
2016-06-06 22:37:27 +02:00
|
|
|
|
2016-07-06 19:28:31 +02:00
|
|
|
case Platform.XDG_Type.DATA:
|
2016-07-05 23:16:35 +02:00
|
|
|
//the user has set XDG_DATA_HOME corresponding to module path
|
2016-07-29 22:02:49 +02:00
|
|
|
if (String.IsNullOrEmpty(xdgdatahome))
|
|
|
|
{
|
2016-07-14 06:10:43 +02:00
|
|
|
// create the xdg folder if needed
|
|
|
|
if (!Directory.Exists(xdgDataHomeDefault))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(xdgDataHomeDefault);
|
|
|
|
}
|
|
|
|
return xdgDataHomeDefault;
|
2016-07-05 23:16:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Path.Combine(xdgdatahome, "powershell");
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:22:23 +02:00
|
|
|
case Platform.XDG_Type.USER_MODULES:
|
2016-06-06 22:37:27 +02:00
|
|
|
//the user has set XDG_DATA_HOME corresponding to module path
|
2016-07-29 22:02:49 +02:00
|
|
|
if (String.IsNullOrEmpty(xdgdatahome))
|
|
|
|
{
|
2016-07-14 06:10:43 +02:00
|
|
|
//xdg values have not been set
|
|
|
|
if (!Directory.Exists(xdgModuleDefault)) //module folder not always guaranteed to exist
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(xdgModuleDefault);
|
|
|
|
}
|
|
|
|
return xdgModuleDefault;
|
2016-06-03 12:15:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-06 22:37:27 +02:00
|
|
|
return Path.Combine(xdgdatahome, "powershell", "Modules");
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:22:23 +02:00
|
|
|
case Platform.XDG_Type.SHARED_MODULES:
|
|
|
|
return "/usr/local/share/powershell/Modules";
|
|
|
|
|
2016-06-03 22:55:23 +02:00
|
|
|
case Platform.XDG_Type.CACHE:
|
2016-06-01 20:03:05 +02:00
|
|
|
//the user has set XDG_CACHE_HOME
|
2016-06-03 22:55:23 +02:00
|
|
|
if (String.IsNullOrEmpty(xdgcachehome))
|
|
|
|
{
|
2016-07-14 06:10:43 +02:00
|
|
|
//xdg values have not been set
|
2016-06-03 22:55:23 +02:00
|
|
|
if (!Directory.Exists(xdgCacheDefault)) //module folder not always guaranteed to exist
|
2016-06-06 22:37:27 +02:00
|
|
|
{
|
|
|
|
Directory.CreateDirectory(xdgCacheDefault);
|
2016-06-03 22:55:23 +02:00
|
|
|
}
|
2016-06-06 22:37:27 +02:00
|
|
|
|
|
|
|
return xdgCacheDefault;
|
2016-06-01 20:03:05 +02:00
|
|
|
}
|
2016-06-06 09:36:15 +02:00
|
|
|
|
2016-06-02 19:14:51 +02:00
|
|
|
else
|
|
|
|
{
|
2016-06-06 18:29:30 +02:00
|
|
|
if (!Directory.Exists(Path.Combine(xdgcachehome, "powershell")))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(Path.Combine(xdgcachehome, "powershell"));
|
|
|
|
}
|
|
|
|
|
2016-06-06 22:37:27 +02:00
|
|
|
return Path.Combine(xdgcachehome, "powershell");
|
|
|
|
}
|
|
|
|
|
|
|
|
case Platform.XDG_Type.DEFAULT:
|
2016-06-01 20:03:05 +02:00
|
|
|
//default for profile location
|
2016-06-03 12:15:13 +02:00
|
|
|
return xdgConfigHomeDefault;
|
2016-06-01 20:03:05 +02:00
|
|
|
|
2016-06-06 22:37:27 +02:00
|
|
|
default:
|
2016-06-03 22:55:23 +02:00
|
|
|
//xdgConfigHomeDefault needs to be created in the edge case that we do not have the folder or it was deleted
|
|
|
|
//This folder is the default in the event of all other failures for data storage
|
2016-06-06 22:37:27 +02:00
|
|
|
if (!Directory.Exists(xdgConfigHomeDefault))
|
|
|
|
{
|
2016-07-29 22:02:49 +02:00
|
|
|
try
|
|
|
|
{
|
2016-06-03 22:55:23 +02:00
|
|
|
Directory.CreateDirectory(xdgConfigHomeDefault);
|
2016-06-06 22:37:27 +02:00
|
|
|
}
|
2016-07-29 22:02:49 +02:00
|
|
|
catch
|
|
|
|
{
|
2016-06-03 22:55:23 +02:00
|
|
|
Console.Error.WriteLine("Failed to create default data directory: " + xdgConfigHomeDefault);
|
2016-06-06 22:37:27 +02:00
|
|
|
}
|
2016-06-01 20:03:05 +02:00
|
|
|
}
|
2016-06-06 22:37:27 +02:00
|
|
|
|
|
|
|
return xdgConfigHomeDefault;
|
2016-05-24 23:29:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 23:22:23 +02:00
|
|
|
#endif
|
2016-03-30 23:26:05 +02:00
|
|
|
|
|
|
|
// Platform methods prefixed NonWindows are:
|
|
|
|
// - non-windows by the definition of the IsWindows method above
|
|
|
|
// - here, because porting to Linux and other operating systems
|
|
|
|
// should not move the original Windows code out of the module
|
|
|
|
// it belongs to, so this way the windows code can remain in it's
|
|
|
|
// original source file and only the non-windows code has been moved
|
|
|
|
// out here
|
|
|
|
// - only to be used with the IsWindows feature query, and only if
|
|
|
|
// no other more specific feature query makes sense
|
|
|
|
|
|
|
|
internal static bool NonWindowsIsHardLink(ref IntPtr handle)
|
|
|
|
{
|
2016-07-14 06:11:34 +02:00
|
|
|
return Unix.IsHardLink(ref handle);
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool NonWindowsIsHardLink(FileSystemInfo fileInfo)
|
|
|
|
{
|
2016-07-14 06:11:34 +02:00
|
|
|
return Unix.IsHardLink(fileInfo);
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool NonWindowsIsSymLink(FileSystemInfo fileInfo)
|
|
|
|
{
|
2016-07-15 02:30:54 +02:00
|
|
|
return Unix.NativeMethods.IsSymLink(fileInfo.FullName);
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static string NonWindowsInternalGetTarget(SafeFileHandle handle)
|
|
|
|
{
|
|
|
|
// SafeHandle is a Windows concept. Use the string version instead.
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string NonWindowsInternalGetTarget(string path)
|
|
|
|
{
|
2016-07-15 02:30:54 +02:00
|
|
|
return Unix.NativeMethods.FollowSymLink(path);
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 05:05:11 +02:00
|
|
|
internal static string NonWindowsGetUserFromPid(int path)
|
2016-07-13 00:27:11 +02:00
|
|
|
{
|
2016-07-15 02:30:54 +02:00
|
|
|
return Unix.NativeMethods.GetUserFromPid(path);
|
2016-07-13 00:27:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 23:26:05 +02:00
|
|
|
internal static string NonWindowsInternalGetLinkType(FileSystemInfo fileInfo)
|
|
|
|
{
|
2016-07-14 06:02:00 +02:00
|
|
|
if (NonWindowsIsSymLink(fileInfo))
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-07-14 06:02:00 +02:00
|
|
|
return "SymbolicLink";
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
2016-07-14 06:02:00 +02:00
|
|
|
|
|
|
|
if (NonWindowsIsHardLink(fileInfo))
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-07-14 06:02:00 +02:00
|
|
|
return "HardLink";
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:02:00 +02:00
|
|
|
return null;
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-15 02:13:45 +02:00
|
|
|
internal static bool NonWindowsCreateSymbolicLink(string path, string target)
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-07-14 06:02:00 +02:00
|
|
|
// Linux doesn't care if target is a directory or not
|
2016-10-04 00:28:58 +02:00
|
|
|
return Unix.NativeMethods.CreateSymLink(path, target) == 0;
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool NonWindowsCreateHardLink(string path, string strTargetPath)
|
|
|
|
{
|
2016-07-14 06:11:34 +02:00
|
|
|
return Unix.CreateHardLink(path, strTargetPath);
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 23:47:24 +02:00
|
|
|
internal static bool NonWindowsSetDate(DateTime dateToUse)
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-07-14 06:11:34 +02:00
|
|
|
Unix.SetDateInfoInternal date = new Unix.SetDateInfoInternal(dateToUse);
|
2016-09-30 23:47:24 +02:00
|
|
|
return Unix.NativeMethods.SetDate(date) == 0;
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static string NonWindowsGetDomainName()
|
|
|
|
{
|
2016-08-05 22:29:27 +02:00
|
|
|
string name = Unix.NativeMethods.GetFullyQualifiedName();
|
|
|
|
if (!string.IsNullOrEmpty(name))
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-05 22:29:27 +02:00
|
|
|
// name is hostname.domainname, so extract domainname
|
|
|
|
int index = name.IndexOf('.');
|
|
|
|
if (index >= 0)
|
|
|
|
{
|
|
|
|
return name.Substring(index + 1);
|
|
|
|
}
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
2016-08-05 22:29:27 +02:00
|
|
|
// if the domain name could not be found, do not throw, just return empty
|
|
|
|
return string.Empty;
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hostname in this context seems to be the FQDN
|
|
|
|
internal static string NonWindowsGetHostName()
|
|
|
|
{
|
2016-08-05 22:29:27 +02:00
|
|
|
return Unix.NativeMethods.GetFullyQualifiedName() ?? string.Empty;
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 06:44:04 +02:00
|
|
|
internal static bool NonWindowsIsFile(string path)
|
|
|
|
{
|
|
|
|
return Unix.NativeMethods.IsFile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool NonWindowsIsDirectory(string path)
|
|
|
|
{
|
|
|
|
return Unix.NativeMethods.IsDirectory(path);
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:26:05 +02:00
|
|
|
internal static bool NonWindowsIsExecutable(string path)
|
|
|
|
{
|
2016-07-15 02:30:54 +02:00
|
|
|
return Unix.NativeMethods.IsExecutable(path);
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static uint NonWindowsGetThreadId()
|
|
|
|
{
|
|
|
|
// TODO:PSL clean this up
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 00:22:28 +02:00
|
|
|
// Unix specific implementations of required functionality
|
|
|
|
//
|
|
|
|
// Please note that `Win32Exception(Marshal.GetLastWin32Error())`
|
|
|
|
// works *correctly* on Linux in that it creates an exception with
|
|
|
|
// the string perror would give you for the last set value of errno.
|
|
|
|
// No manual mapping is required. .NET Core maps the Linux errno
|
|
|
|
// to a PAL value and calls strerror_r underneath to generate the message.
|
2016-08-06 00:32:48 +02:00
|
|
|
internal static class Unix
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-10-04 00:41:38 +02:00
|
|
|
// This is a helper that attempts to map errno into a PowerShell ErrorCategory
|
|
|
|
internal static ErrorCategory GetErrorCategory(int errno)
|
|
|
|
{
|
|
|
|
return (ErrorCategory)Unix.NativeMethods.GetErrorCategory(errno);
|
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
private static string s_userName;
|
|
|
|
public static string UserName
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
get
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
if (string.IsNullOrEmpty(s_userName))
|
|
|
|
{
|
|
|
|
s_userName = NativeMethods.GetUserName();
|
|
|
|
}
|
|
|
|
return s_userName ?? string.Empty;
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
public static string TemporaryDirectory
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
get
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
// POSIX temporary directory environment variables
|
|
|
|
string[] environmentVariables = { "TMPDIR", "TMP", "TEMP", "TEMPDIR" };
|
|
|
|
string dir = string.Empty;
|
|
|
|
foreach (string s in environmentVariables)
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
dir = System.Environment.GetEnvironmentVariable(s);
|
|
|
|
if (!string.IsNullOrEmpty(dir))
|
|
|
|
{
|
|
|
|
return dir;
|
|
|
|
}
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
2016-08-06 00:32:48 +02:00
|
|
|
return "/tmp";
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
public static bool IsHardLink(ref IntPtr handle)
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
// TODO:PSL implement using fstat to query inode refcount to see if it is a hard link
|
2016-03-30 23:26:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
|
|
|
|
public static bool IsHardLink(FileSystemInfo fs)
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
if (!fs.Exists || (fs.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count;
|
|
|
|
string filePath = fs.FullName;
|
|
|
|
int ret = NativeMethods.GetLinkCount(filePath, out count);
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
return count > 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-04 00:22:28 +02:00
|
|
|
throw new Win32Exception(Marshal.GetLastWin32Error());
|
2016-08-06 00:32:48 +02:00
|
|
|
}
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
public static bool CreateHardLink(string path, string strTargetPath)
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
int ret = NativeMethods.CreateHardLink(path, strTargetPath);
|
|
|
|
return ret == 1 ? true : false;
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
internal class SetDateInfoInternal
|
2016-03-30 23:26:05 +02:00
|
|
|
{
|
2016-08-06 00:32:48 +02:00
|
|
|
public int Year;
|
|
|
|
public int Month;
|
|
|
|
public int Day;
|
|
|
|
public int Hour;
|
|
|
|
public int Minute;
|
|
|
|
public int Second;
|
|
|
|
public int Millisecond;
|
|
|
|
public int DST;
|
|
|
|
|
|
|
|
public SetDateInfoInternal(DateTime d)
|
|
|
|
{
|
|
|
|
Year = d.Year;
|
|
|
|
Month = d.Month;
|
|
|
|
Day = d.Day;
|
|
|
|
Hour = d.Hour;
|
|
|
|
Minute = d.Minute;
|
|
|
|
Second = d.Second;
|
|
|
|
Millisecond = d.Millisecond;
|
|
|
|
DST = d.IsDaylightSavingTime() ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
string ret = String.Format("Year = {0}; Month = {1}; Day = {2}; Hour = {3}; Minute = {4}; Second = {5}; Millisec = {6}; DST = {7}", Year, Month, Day, Hour, Minute, Second, Millisecond, DST);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
internal static class NativeMethods
|
|
|
|
{
|
|
|
|
private const string psLib = "libpsl-native";
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
// Ansi is a misnomer, it is hardcoded to UTF-8 on Linux and OS X
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
// C bools are 1 byte and so must be marshaled as I1
|
2016-07-14 06:39:34 +02:00
|
|
|
|
2016-10-04 00:41:38 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi)]
|
|
|
|
internal static extern int GetErrorCategory(int errno);
|
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
internal static extern string GetUserName();
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
internal static extern int GetLinkCount([MarshalAs(UnmanagedType.LPStr)]string filePath, out int linkCount);
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.I1)]
|
|
|
|
internal static extern bool IsSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.I1)]
|
|
|
|
internal static extern bool IsExecutable([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
internal static extern string GetFullyQualifiedName();
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
internal static extern int SetDate(SetDateInfoInternal info);
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
2016-10-04 00:28:58 +02:00
|
|
|
internal static extern int CreateSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
|
|
|
[MarshalAs(UnmanagedType.LPStr)]string target);
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
internal static extern int CreateHardLink([MarshalAs(UnmanagedType.LPStr)]string filePath,
|
|
|
|
[MarshalAs(UnmanagedType.LPStr)]string target);
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
internal static extern string FollowSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
2016-03-30 23:26:05 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
internal static extern string GetUserFromPid(int pid);
|
2016-07-14 06:44:04 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.I1)]
|
|
|
|
internal static extern bool IsFile([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
2016-07-14 06:44:04 +02:00
|
|
|
|
2016-08-06 00:32:48 +02:00
|
|
|
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.I1)]
|
|
|
|
internal static extern bool IsDirectory([MarshalAs(UnmanagedType.LPStr)]string filePath);
|
|
|
|
}
|
2016-03-30 23:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace System.Management.Automation
|