File size: 18,531 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Management.Automation;
using System.Management.Automation.Internal;
using Dbg = System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// A command to resolve PowerShell paths containing glob characters to
/// PowerShell paths that match the glob strings.
/// </summary>
[Cmdlet(VerbsCommon.Split, "Path", DefaultParameterSetName = "ParentSet", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097149")]
[OutputType(typeof(string), ParameterSetName = new[] { leafSet,
leafBaseSet,
extensionSet,
noQualifierSet,
parentSet,
qualifierSet,
literalPathSet})]
[OutputType(typeof(bool), ParameterSetName = new[] { isAbsoluteSet })]
public class SplitPathCommand : CoreCommandWithCredentialsBase
{
#region Parameters
/// <summary>
/// The parameter set name to get the parent path.
/// </summary>
private const string parentSet = "ParentSet";
/// <summary>
/// The parameter set name to get the leaf name.
/// </summary>
private const string leafSet = "LeafSet";
/// <summary>
/// The parameter set name to get the leaf base name.
/// </summary>
private const string leafBaseSet = "LeafBaseSet";
/// <summary>
/// The parameter set name to get the extension.
/// </summary>
private const string extensionSet = "ExtensionSet";
/// <summary>
/// The parameter set name to get the qualifier set.
/// </summary>
private const string qualifierSet = "QualifierSet";
/// <summary>
/// The parameter set name to get the noqualifier set.
/// </summary>
private const string noQualifierSet = "NoQualifierSet";
/// <summary>
/// The parameter set name to get the IsAbsolute set.
/// </summary>
private const string isAbsoluteSet = "IsAbsoluteSet";
/// <summary>
/// The parameter set name to get the LiteralPath set.
/// </summary>
private const string literalPathSet = "LiteralPathSet";
/// <summary>
/// Gets or sets the path parameter to the command.
/// </summary>
[Parameter(Position = 0, ParameterSetName = parentSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Parameter(Position = 0, ParameterSetName = leafSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Parameter(Position = 0, ParameterSetName = leafBaseSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Parameter(Position = 0, ParameterSetName = extensionSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Parameter(Position = 0, ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Parameter(Position = 0, ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Parameter(Position = 0, ParameterSetName = isAbsoluteSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string[] Path { get; set; }
/// <summary>
/// Gets or sets the literal path parameter to the command.
/// </summary>
[Parameter(ParameterSetName = "LiteralPathSet", Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
[Alias("PSPath", "LP")]
public string[] LiteralPath
{
get
{
return Path;
}
set
{
base.SuppressWildcardExpansion = true;
Path = value;
}
}
/// <summary>
/// Determines if the qualifier should be returned.
/// </summary>
/// <value>
/// If true the qualifier of the path will be returned.
/// The qualifier is the drive or provider that is qualifying
/// the PowerShell path.
/// </value>
[Parameter(ParameterSetName = qualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Qualifier { get; set; }
/// <summary>
/// Determines if the qualifier should be returned.
/// </summary>
/// <value>
/// If true the qualifier of the path will be returned.
/// The qualifier is the drive or provider that is qualifying
/// the PowerShell path.
/// </value>
[Parameter(ParameterSetName = noQualifierSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter NoQualifier { get; set; }
/// <summary>
/// Determines if the parent path should be returned.
/// </summary>
/// <value>
/// If true the parent of the path will be returned.
/// </value>
[Parameter(ParameterSetName = parentSet, Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Parent { get; set; } = true;
/// <summary>
/// Determines if the leaf name should be returned.
/// </summary>
/// <value>
/// If true the leaf name of the path will be returned.
/// </value>
[Parameter(ParameterSetName = leafSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Leaf { get; set; }
/// <summary>
/// Determines if the leaf base name (name without extension) should be returned.
/// </summary>
/// <value>
/// If true the leaf base name of the path will be returned.
/// </value>
[Parameter(ParameterSetName = leafBaseSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter LeafBase { get; set; }
/// <summary>
/// Determines if the extension should be returned.
/// </summary>
/// <value>
/// If true the extension of the path will be returned.
/// </value>
[Parameter(ParameterSetName = extensionSet, Mandatory = true, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Extension { get; set; }
/// <summary>
/// Determines if the path should be resolved before being parsed.
/// </summary>
/// <value></value>
[Parameter]
public SwitchParameter Resolve { get; set; }
/// <summary>
/// Determines if the path is an absolute path.
/// </summary>
[Parameter(ParameterSetName = isAbsoluteSet, Mandatory = true)]
public SwitchParameter IsAbsolute { get; set; }
#endregion Parameters
#region parameter data
#endregion parameter data
#region Command code
/// <summary>
/// Parses the specified path and returns the portion determined by the
/// boolean parameters.
/// </summary>
protected override void ProcessRecord()
{
StringCollection pathsToParse = new();
if (Resolve)
{
CmdletProviderContext currentContext = CmdletProviderContext;
foreach (string path in Path)
{
// resolve the paths and then parse each one.
Collection<PathInfo> resolvedPaths;
try
{
resolvedPaths =
SessionState.Path.GetResolvedPSPathFromPSPath(path, currentContext);
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
continue;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
continue;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
continue;
}
foreach (PathInfo resolvedPath in resolvedPaths)
{
try
{
if (InvokeProvider.Item.Exists(resolvedPath.Path, currentContext))
{
pathsToParse.Add(resolvedPath.Path);
}
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
continue;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
continue;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
continue;
}
}
}
}
else
{
pathsToParse.AddRange(Path);
}
// Now parse each path
for (int index = 0; index < pathsToParse.Count; ++index)
{
string result = null;
// Check switch parameters in order of specificity
if (IsAbsolute)
{
string ignored;
bool isPathAbsolute =
SessionState.Path.IsPSAbsolute(pathsToParse[index], out ignored);
WriteObject(isPathAbsolute);
continue;
}
else if (Qualifier)
{
int separatorIndex = pathsToParse[index].IndexOf(':');
if (separatorIndex < 0)
{
FormatException e =
new(
StringUtil.Format(NavigationResources.ParsePathFormatError, pathsToParse[index]));
WriteError(
new ErrorRecord(
e,
"ParsePathFormatError", // RENAME
ErrorCategory.InvalidArgument,
pathsToParse[index]));
continue;
}
else
{
// Check to see if it is provider or drive qualified
if (SessionState.Path.IsProviderQualified(pathsToParse[index]))
{
// The plus 2 is for the length of the provider separator
// which is "::"
result =
pathsToParse[index].Substring(
0,
separatorIndex + 2);
}
else
{
result =
pathsToParse[index].Substring(
0,
separatorIndex + 1);
}
}
}
else if (Leaf || LeafBase || Extension)
{
try
{
result =
SessionState.Path.ParseChildName(
pathsToParse[index],
CmdletProviderContext,
true);
if (LeafBase)
{
result = System.IO.Path.GetFileNameWithoutExtension(result);
}
else if (Extension)
{
result = System.IO.Path.GetExtension(result);
}
}
catch (PSNotSupportedException)
{
// Since getting the leaf part of a path is not supported,
// the provider must be a container, item, or drive
// provider. Since the paths for these types of
// providers can't be split, asking for the leaf
// is asking for the specified path back.
result = pathsToParse[index];
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
continue;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
}
else if (NoQualifier)
{
result = RemoveQualifier(pathsToParse[index]);
}
else
{
// None of the switch parameters are true: default to -Parent behavior
try
{
result =
SessionState.Path.ParseParent(
pathsToParse[index],
string.Empty,
CmdletProviderContext,
true);
}
catch (PSNotSupportedException)
{
// Since getting the parent path is not supported,
// the provider must be a container, item, or drive
// provider. Since the paths for these types of
// providers can't be split, asking for the parent
// is asking for an empty string.
result = string.Empty;
}
}
if (result != null)
{
WriteObject(result);
}
}
}
#endregion Command code
/// <summary>
/// Removes either the drive or provider qualifier or both from the path.
/// </summary>
/// <param name="path">
/// The path to strip the provider qualifier from.
/// </param>
/// <returns>
/// The path without the qualifier.
/// </returns>
private string RemoveQualifier(string path)
{
Dbg.Diagnostics.Assert(
path != null,
"Path should be verified by the caller");
string result = path;
if (SessionState.Path.IsProviderQualified(path))
{
int index = path.IndexOf("::", StringComparison.Ordinal);
if (index != -1)
{
// remove the qualifier
result = path.Substring(index + 2);
}
}
else
{
string driveName = string.Empty;
if (SessionState.Path.IsPSAbsolute(path, out driveName))
{
var driveNameLength = driveName.Length;
if (path.Length > (driveNameLength + 1) && path[driveNameLength] == ':')
{
// Remove the drive name and colon
result = path.Substring(driveNameLength + 1);
}
}
}
return result;
}
}
}
|