Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ static ExperimentalFeature()
new ExperimentalFeature(
name: "PSCultureInvariantReplaceOperator",
description: "Use culture invariant to-string convertor for lval in replace operator"),
new ExperimentalFeature(
name: "PSNativePSPathResolution",
description: "Convert PSPath to filesystem path, if possible, for native commands"),
};
EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);

Expand Down
240 changes: 166 additions & 74 deletions src/System.Management.Automation/engine/NativeCommandParameterBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal void BindParameters(Collection<CommandParameterInternal> parameters)
if (parameter.ParameterNameSpecified)
{
Diagnostics.Assert(!parameter.ParameterText.Contains(' '), "Parameters cannot have whitespace");
PossiblyGlobArg(parameter.ParameterText, usedQuotes: false);
PossiblyGlobArg(parameter.ParameterText, StringConstantType.BareWord);

if (parameter.SpaceAfterParameter)
{
Expand All @@ -107,23 +107,30 @@ internal void BindParameters(Collection<CommandParameterInternal> parameters)
// windbg -k com:port=\\devbox\pipe\debug,pipe,resets=0,reconnect
// The parser produced an array of strings but marked the parameter so we
// can properly reconstruct the correct command line.
bool usedQuotes = false;
StringConstantType stringConstantType = StringConstantType.BareWord;
ArrayLiteralAst arrayLiteralAst = null;
switch (parameter?.ArgumentAst)
{
case StringConstantExpressionAst sce:
usedQuotes = sce.StringConstantType != StringConstantType.BareWord;
stringConstantType = sce.StringConstantType;
break;
case ExpandableStringExpressionAst ese:
usedQuotes = ese.StringConstantType != StringConstantType.BareWord;
stringConstantType = ese.StringConstantType;
break;
case ArrayLiteralAst ala:
arrayLiteralAst = ala;
break;
}

appendOneNativeArgument(Context, argValue,
arrayLiteralAst, sawVerbatimArgumentMarker, usedQuotes);
// Prior to PSNativePSPathResolution experimental feature, a single quote worked the same as a double quote
// so if the feature is not enabled, we treat any quotes as double quotes. When this feature is no longer
// experimental, this code here needs to be removed.
if (!ExperimentalFeature.IsEnabled("PSNativePSPathResolution") && stringConstantType == StringConstantType.SingleQuoted)
{
stringConstantType = StringConstantType.DoubleQuoted;
}

AppendOneNativeArgument(Context, argValue, arrayLiteralAst, sawVerbatimArgumentMarker, stringConstantType);
}
}
}
Expand Down Expand Up @@ -157,14 +164,12 @@ internal string Arguments
/// <param name="obj">The object to append.</param>
/// <param name="argArrayAst">If the argument was an array literal, the Ast, otherwise null.</param>
/// <param name="sawVerbatimArgumentMarker">True if the argument occurs after --%.</param>
/// <param name="usedQuotes">True if the argument was a quoted string (single or double).</param>
private void appendOneNativeArgument(ExecutionContext context, object obj, ArrayLiteralAst argArrayAst, bool sawVerbatimArgumentMarker, bool usedQuotes)
/// <param name="stringConstantType">Bare, SingleQuoted, or DoubleQuoted.</param>
private void AppendOneNativeArgument(ExecutionContext context, object obj, ArrayLiteralAst argArrayAst, bool sawVerbatimArgumentMarker, StringConstantType stringConstantType)
{
IEnumerator list = LanguagePrimitives.GetEnumerator(obj);

Diagnostics.Assert(argArrayAst == null
|| obj is object[] && ((object[])obj).Length == argArrayAst.Elements.Count,
"array argument and ArrayLiteralAst differ in number of elements");
Diagnostics.Assert((argArrayAst == null) || obj is object[] && ((object[])obj).Length == argArrayAst.Elements.Count, "array argument and ArrayLiteralAst differ in number of elements");

int currentElement = -1;
string separator = string.Empty;
Expand Down Expand Up @@ -218,9 +223,18 @@ private void appendOneNativeArgument(ExecutionContext context, object obj, Array
if (NeedQuotes(arg))
{
_arguments.Append('"');

if (stringConstantType == StringConstantType.DoubleQuoted)
{
_arguments.Append(ResolvePath(arg, Context));
}
else
{
_arguments.Append(arg);
}

// need to escape all trailing backslashes so the native command receives it correctly
// according to http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESDOC
_arguments.Append(arg);
for (int i = arg.Length - 1; i >= 0 && arg[i] == '\\'; i--)
{
_arguments.Append('\\');
Expand All @@ -230,106 +244,181 @@ private void appendOneNativeArgument(ExecutionContext context, object obj, Array
}
else
{
PossiblyGlobArg(arg, usedQuotes);
PossiblyGlobArg(arg, stringConstantType);
}
}
}
} while (list != null);
}
while (list != null);
}

/// <summary>
/// On Windows, just append <paramref name="arg"/>.
/// On Unix, do globbing as appropriate, otherwise just append <paramref name="arg"/>.
/// </summary>
/// <param name="arg">The argument that possibly needs expansion.</param>
/// <param name="usedQuotes">True if the argument was a quoted string (single or double).</param>
private void PossiblyGlobArg(string arg, bool usedQuotes)
/// <param name="stringConstantType">Bare, SingleQuoted, or DoubleQuoted.</param>
private void PossiblyGlobArg(string arg, StringConstantType stringConstantType)
{
var argExpanded = false;

#if UNIX
// On UNIX systems, we expand arguments containing wildcard expressions against
// the file system just like bash, etc.
if (!usedQuotes && WildcardPattern.ContainsWildcardCharacters(arg))
{
// See if the current working directory is a filesystem provider location
// We won't do the expansion if it isn't since native commands can only access the file system.
var cwdinfo = Context.EngineSessionState.CurrentLocation;

// If it's a filesystem location then expand the wildcards
if (cwdinfo.Provider.Name.Equals(FileSystemProvider.ProviderName, StringComparison.OrdinalIgnoreCase))
if (stringConstantType == StringConstantType.BareWord)
{
if (WildcardPattern.ContainsWildcardCharacters(arg))
{
// On UNIX, paths starting with ~ or absolute paths are not normalized
bool normalizePath = arg.Length == 0 || !(arg[0] == '~' || arg[0] == '/');
// See if the current working directory is a filesystem provider location
// We won't do the expansion if it isn't since native commands can only access the file system.
var cwdinfo = Context.EngineSessionState.CurrentLocation;

// See if there are any matching paths otherwise just add the pattern as the argument
Collection<PSObject> paths = null;
try
{
paths = Context.EngineSessionState.InvokeProvider.ChildItem.Get(arg, false);
}
catch
// If it's a filesystem location then expand the wildcards
if (cwdinfo.Provider.Name.Equals(FileSystemProvider.ProviderName, StringComparison.OrdinalIgnoreCase))
{
// Fallthrough will append the pattern unchanged.
}
// On UNIX, paths starting with ~ or absolute paths are not normalized
bool normalizePath = arg.Length == 0 || !(arg[0] == '~' || arg[0] == '/');

// Expand paths, but only from the file system.
if (paths?.Count > 0 && paths.All(p => p.BaseObject is FileSystemInfo))
{
var sep = string.Empty;
foreach (var path in paths)
// See if there are any matching paths otherwise just add the pattern as the argument
Collection<PSObject> paths = null;
try
{
_arguments.Append(sep);
sep = " ";
var expandedPath = (path.BaseObject as FileSystemInfo).FullName;
if (normalizePath)
{
expandedPath =
Context.SessionState.Path.NormalizeRelativePath(expandedPath, cwdinfo.ProviderPath);
}
// If the path contains spaces, then add quotes around it.
if (NeedQuotes(expandedPath))
{
_arguments.Append("\"");
_arguments.Append(expandedPath);
_arguments.Append("\"");
}
else
paths = Context.EngineSessionState.InvokeProvider.ChildItem.Get(arg, false);
}
catch
{
// Fallthrough will append the pattern unchanged.
}

// Expand paths, but only from the file system.
if (paths?.Count > 0 && paths.All(p => p.BaseObject is FileSystemInfo))
{
var sep = string.Empty;
foreach (var path in paths)
{
_arguments.Append(expandedPath);
_arguments.Append(sep);
sep = " ";
var expandedPath = (path.BaseObject as FileSystemInfo).FullName;
if (normalizePath)
{
expandedPath =
Context.SessionState.Path.NormalizeRelativePath(expandedPath, cwdinfo.ProviderPath);
}
// If the path contains spaces, then add quotes around it.
if (NeedQuotes(expandedPath))
{
_arguments.Append("\"");
_arguments.Append(expandedPath);
_arguments.Append("\"");
}
else
{
_arguments.Append(expandedPath);
}

argExpanded = true;
}

argExpanded = true;
}
}
}
}
else if (!usedQuotes)
{
// Even if there are no wildcards, we still need to possibly
// expand ~ into the filesystem provider home directory path
ProviderInfo fileSystemProvider = Context.EngineSessionState.GetSingleProvider(FileSystemProvider.ProviderName);
string home = fileSystemProvider.Home;
if (string.Equals(arg, "~"))
{
_arguments.Append(home);
argExpanded = true;
}
else if (arg.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
else
{
var replacementString = home + arg.Substring(1);
_arguments.Append(replacementString);
argExpanded = true;
// Even if there are no wildcards, we still need to possibly
// expand ~ into the filesystem provider home directory path
ProviderInfo fileSystemProvider = Context.EngineSessionState.GetSingleProvider(FileSystemProvider.ProviderName);
string home = fileSystemProvider.Home;
if (string.Equals(arg, "~"))
{
_arguments.Append(home);
argExpanded = true;
}
else if (arg.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
{
var replacementString = home + arg.Substring(1);
_arguments.Append(replacementString);
argExpanded = true;
}
}
}
#endif // UNIX

if (stringConstantType != StringConstantType.SingleQuoted)
{
arg = ResolvePath(arg, Context);
}

if (!argExpanded)
{
_arguments.Append(arg);
}
}

/// <summary>
/// Check if string is prefixed by psdrive, if so, expand it if filesystem path.
/// </summary>
/// <param name="path">The potential PSPath to resolve.</param>
/// <param name="context">The current ExecutionContext.</param>
/// <returns>Resolved PSPath if applicable otherwise the original path</returns>
internal static string ResolvePath(string path, ExecutionContext context)
{
if (ExperimentalFeature.IsEnabled("PSNativePSPathResolution"))
{
#if !UNIX
// on Windows, we need to expand ~ to point to user's home path
if (string.Equals(path, "~", StringComparison.Ordinal) || path.StartsWith(TildeDirectorySeparator, StringComparison.Ordinal) || path.StartsWith(TildeAltDirectorySeparator, StringComparison.Ordinal))
{
try
{
ProviderInfo fileSystemProvider = context.EngineSessionState.GetSingleProvider(FileSystemProvider.ProviderName);
return new StringBuilder(fileSystemProvider.Home)
.Append(path.Substring(1))
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
.ToString();
}
catch
{
return path;
}
}

// check if the driveName is an actual disk drive on Windows, if so, no expansion
if (path.Length >= 2 && path[1] == ':')
{
foreach (var drive in DriveInfo.GetDrives())
{
if (drive.Name.StartsWith(new string(path[0], 1), StringComparison.OrdinalIgnoreCase))
{
return path;
}
}
}
#endif

if (path.Contains(':'))
{
LocationGlobber globber = new LocationGlobber(context.SessionState);
try
{
ProviderInfo providerInfo;

// replace the argument with resolved path if it's a filesystem path
string pspath = globber.GetProviderPath(path, out providerInfo);
if (string.Equals(providerInfo.Name, FileSystemProvider.ProviderName, StringComparison.OrdinalIgnoreCase))
{
path = pspath;
}
}
catch
{
// if it's not a provider path, do nothing
}
}
}

return path;
}

/// <summary>
/// Check to see if the string contains spaces and therefore must be quoted.
/// </summary>
Expand Down Expand Up @@ -384,6 +473,9 @@ private static string GetEnumerableArgSeparator(ArrayLiteralAst arrayLiteralAst,
/// The native command to bind to.
/// </summary>
private NativeCommand _nativeCommand;
private static readonly string TildeDirectorySeparator = $"~{Path.DirectorySeparatorChar}";
private static readonly string TildeAltDirectorySeparator = $"~{Path.AltDirectorySeparatorChar}";

#endregion private members
}
}
Loading