File size: 44,872 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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Management.Automation.Internal;
namespace System.Management.Automation.Provider
{
#region NavigationCmdletProvider
/// <summary>
/// The base class for a Cmdlet provider that expose a hierarchy of items and containers.
/// </summary>
/// <remarks>
/// The NavigationCmdletProvider class is a base class that provider can derive from
/// to implement a set of methods that allow
/// the use of a set of core commands against the data store that the provider
/// gives access to. By implementing this interface users can take advantage
/// the recursive commands, nested containers, and relative paths.
/// </remarks>
public abstract class NavigationCmdletProvider : ContainerCmdletProvider
{
#region Internal methods
/// <summary>
/// Internal wrapper for the MakePath protected method. It is called instead
/// of the protected method that is overridden by derived classes so that the
/// context of the command can be set.
/// </summary>
/// <param name="parent">
/// The parent segment of a path to be joined with the child.
/// </param>
/// <param name="child">
/// The child segment of a path to be joined with the parent.
/// </param>
/// <param name="context">
/// The context under which this method is being called.
/// </param>
/// <returns>
/// A string that represents the parent and child segments of the path
/// joined by a path separator.
/// </returns>
/// <remarks>
/// This method should use lexical joining of two path segments with a path
/// separator character. It should not validate the path as a legal fully
/// qualified path in the provider namespace as each parameter could be only
/// partial segments of a path and joined they may not generate a fully
/// qualified path.
/// Example: the file system provider may get "windows\system32" as the parent
/// parameter and "foo.dll" as the child parameter. The method should join these
/// with the "\" separator and return "windows\system32\foo.dll". Note that
/// the returned path is not a fully qualified file system path.
///
/// Also beware that the path segments may contain characters that are illegal
/// in the provider namespace. These characters are most likely being used
/// for globbing and should not be removed by the implementation of this method.
/// </remarks>
internal string MakePath(
string parent,
string child,
CmdletProviderContext context)
{
Context = context;
// Call virtual method
return MakePath(parent, child);
}
/// <summary>
/// Internal wrapper for the GetParentPath protected method. It is called instead
/// of the protected method that is overridden by derived classes so that the
/// context of the command can be set.
/// </summary>
/// <param name="path">
/// A fully qualified provider specific path to an item. The item may or
/// may not exist.
/// </param>
/// <param name="root">
/// The fully qualified path to the root of a drive. This parameter may be null
/// or empty if a mounted drive is not in use for this operation. If this parameter
/// is not null or empty the result of the method should not be a path to a container
/// that is a parent or in a different tree than the root.
/// </param>
/// <param name="context">
/// The context under which this method is being called.
/// </param>
/// <returns>
/// The path of the parent of the path parameter.
/// </returns>
/// <remarks>
/// This should be a lexical splitting of the path on the path separator character
/// for the provider namespace. For example, the file system provider should look
/// for the last "\" and return everything to the left of the "\".
/// </remarks>
internal string GetParentPath(
string path,
string root,
CmdletProviderContext context)
{
Context = context;
// Call virtual method
return GetParentPath(path, root);
}
/// <summary>
/// Internal wrapper for the NormalizeRelativePath method. It is called instead
/// of the protected method that is overridden by derived classes so that the
/// context of the command can be set.
/// </summary>
/// <param name="path">
/// A fully qualified provider specific path to an item. The item should exist
/// or the provider should write out an error.
/// </param>
/// <param name="basePath">
/// The path that the return value should be relative to.
/// </param>
/// <param name="context">
/// The context under which this method is being called.
/// </param>
/// <returns>
/// A normalized path that is relative to the basePath that was passed. The
/// provider should parse the path parameter, normalize the path, and then
/// return the normalized path relative to the basePath.
/// </returns>
/// <remarks>
/// This method does not have to be purely syntactical parsing of the path. It
/// is encouraged that the provider actually use the path to lookup in its store
/// and create a relative path that matches the casing, and standardized path syntax.
/// </remarks>
internal string NormalizeRelativePath(
string path,
string basePath,
CmdletProviderContext context)
{
Context = context;
// Call virtual method
return NormalizeRelativePath(path, basePath);
}
/// <summary>
/// Internal wrapper for the GetChildName protected method. It is called instead
/// of the protected method that is overridden by derived classes so that the
/// context of the command can be set.
/// </summary>
/// <param name="path">
/// The fully qualified path to the item
/// </param>
/// <returns>
/// The leaf element in the path.
/// </returns>
/// <param name="context">
/// The context under which this method is being called.
/// </param>
/// <remarks>
/// This should be implemented as a split on the path separator. The characters
/// in the fullPath may not be legal characters in the namespace but may be
/// used in globing or regular expression matching. The provider should not error
/// unless there are no path separators in the fully qualified path.
/// </remarks>
internal string GetChildName(
string path,
CmdletProviderContext context)
{
Context = context;
// Call virtual method
return GetChildName(path);
}
/// <summary>
/// Internal wrapper for the IsItemContainer protected method. It is called instead
/// of the protected method that is overridden by derived classes so that the
/// context of the command can be set.
/// </summary>
/// <param name="path">
/// The path to the item to determine if it is a container.
/// </param>
/// <param name="context">
/// The context under which this method is being called.
/// </param>
/// <returns>
/// true if the item specified by path is a container, false otherwise.
/// </returns>
internal bool IsItemContainer(
string path,
CmdletProviderContext context)
{
Context = context;
// Call virtual method
return IsItemContainer(path);
}
/// <summary>
/// Internal wrapper for the MoveItem protected method. It is called instead
/// of the protected method that is overridden by derived classes so that the
/// context of the command can be set.
/// </summary>
/// <param name="path">
/// The path to the item to be moved.
/// </param>
/// <param name="destination">
/// The path of the destination container.
/// </param>
/// <param name="context">
/// The context under which this method is being called.
/// </param>
/// <returns>
/// Nothing. All objects that are moved should be written to the WriteObject method.
/// </returns>
internal void MoveItem(
string path,
string destination,
CmdletProviderContext context)
{
Context = context;
// Call virtual method
MoveItem(path, destination);
}
/// <summary>
/// Gives the provider to attach additional parameters to
/// the move-item cmdlet.
/// </summary>
/// <param name="path">
/// If the path was specified on the command line, this is the path
/// to the item to get the dynamic parameters for.
/// </param>
/// <param name="destination">
/// The path of the destination container.
/// </param>
/// <param name="context">
/// The context under which this method is being called.
/// </param>
/// <returns>
/// An object that has properties and fields decorated with
/// parsing attributes similar to a cmdlet class.
/// </returns>
internal object MoveItemDynamicParameters(
string path,
string destination,
CmdletProviderContext context)
{
Context = context;
return MoveItemDynamicParameters(path, destination);
}
#endregion Internal methods
#region protected methods
/// <summary>
/// Joins two strings with a path a provider specific path separator.
/// </summary>
/// <param name="parent">
/// The parent segment of a path to be joined with the child.
/// </param>
/// <param name="child">
/// The child segment of a path to be joined with the parent.
/// </param>
/// <returns>
/// A string that represents the parent and child segments of the path
/// joined by a path separator.
/// </returns>
/// <remarks>
/// This method should use lexical joining of two path segments with a path
/// separator character. It should not validate the path as a legal fully
/// qualified path in the provider namespace as each parameter could be only
/// partial segments of a path and joined they may not generate a fully
/// qualified path.
/// Example: the file system provider may get "windows\system32" as the parent
/// parameter and "foo.dll" as the child parameter. The method should join these
/// with the "\" separator and return "windows\system32\foo.dll". Note that
/// the returned path is not a fully qualified file system path.
///
/// Also beware that the path segments may contain characters that are illegal
/// in the provider namespace. These characters are most likely being used
/// for globbing and should not be removed by the implementation of this method.
/// </remarks>
protected virtual string MakePath(string parent, string child)
{
return MakePath(parent, child, childIsLeaf: false);
}
/// <summary>
/// Joins two strings with a path a provider specific path separator.
/// </summary>
/// <param name="parent">
/// The parent segment of a path to be joined with the child.
/// </param>
/// <param name="child">
/// The child segment of a path to be joined with the parent.
/// </param>
/// <param name="childIsLeaf">
/// Indicate that the <paramref name="child"/> is the name of a child item that's guaranteed to exist
/// </param>
/// <remarks>
/// If the <paramref name="childIsLeaf"/> is True, then we don't normalize the child path, and would do
/// some checks to decide whether to normalize the parent path.
/// </remarks>
/// <returns>New path string.</returns>
protected string MakePath(string parent, string child, bool childIsLeaf)
{
using (PSTransactionManager.GetEngineProtectionScope())
{
string result = null;
if (parent == null &&
child == null)
{
throw PSTraceSource.NewArgumentException(nameof(parent));
}
if (string.IsNullOrEmpty(parent) &&
string.IsNullOrEmpty(child))
{
result = string.Empty;
}
else if (string.IsNullOrEmpty(parent) &&
!string.IsNullOrEmpty(child))
{
result = NormalizePath(child);
}
else if (!string.IsNullOrEmpty(parent) &&
(string.IsNullOrEmpty(child) ||
child.Equals(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal) ||
child.Equals(StringLiterals.AlternatePathSeparatorString, StringComparison.Ordinal)))
{
if (parent.EndsWith(StringLiterals.DefaultPathSeparator))
{
result = parent;
}
else
{
result = parent + StringLiterals.DefaultPathSeparator;
}
}
else
{
// Both parts are not empty so join them
// 'childIsLeaf == true' indicates that 'child' is actually the name of a child item and
// guaranteed to exist. In this case, we don't normalize the child path.
if (childIsLeaf)
{
parent = NormalizePath(parent);
}
else
{
// Normalize the path so that only the default path separator is used as a
// separator even if the user types the alternate slash.
parent = NormalizePath(parent);
child = NormalizePath(child);
}
ReadOnlySpan<char> appendChild = child.AsSpan();
if (child.StartsWith(StringLiterals.DefaultPathSeparator))
{
appendChild = appendChild.Slice(1);
}
result = IO.Path.Join(parent.AsSpan(), appendChild);
}
return result;
}
}
/// <summary>
/// Removes the child segment of a path and returns the remaining parent
/// portion.
/// </summary>
/// <param name="path">
/// A fully qualified provider specific path to an item. The item may or
/// may not exist.
/// </param>
/// <param name="root">
/// The fully qualified path to the root of a drive. This parameter may be null
/// or empty if a mounted drive is not in use for this operation. If this parameter
/// is not null or empty the result of the method should not be a path to a container
/// that is a parent or in a different tree than the root.
/// </param>
/// <returns>
/// The path of the parent of the path parameter.
/// </returns>
/// <remarks>
/// This should be a lexical splitting of the path on the path separator character
/// for the provider namespace. For example, the file system provider should look
/// for the last "\" and return everything to the left of the "\".
/// </remarks>
protected virtual string GetParentPath(string path, string root)
{
using (PSTransactionManager.GetEngineProtectionScope())
{
string parentPath = null;
// Verify the parameters
if (string.IsNullOrEmpty(path))
{
throw PSTraceSource.NewArgumentException(nameof(path));
}
if (root == null)
{
if (PSDriveInfo != null)
{
root = PSDriveInfo.Root;
}
}
// Normalize the path
path = NormalizePath(path);
path = path.TrimEnd(StringLiterals.DefaultPathSeparator);
string rootPath = string.Empty;
if (root != null)
{
rootPath = NormalizePath(root);
}
// Check to see if the path is equal to the root
// of the virtual drive
if (string.Equals(
path,
rootPath,
StringComparison.OrdinalIgnoreCase))
{
parentPath = string.Empty;
}
else
{
int lastIndex = path.LastIndexOf(StringLiterals.DefaultPathSeparator);
if (lastIndex != -1)
{
if (lastIndex == 0)
{
++lastIndex;
}
// Get the parent directory
parentPath = path.Substring(0, lastIndex);
}
else
{
parentPath = string.Empty;
}
}
return parentPath;
}
}
/// <summary>
/// Normalizes the path that was passed in and returns the normalized path
/// as a relative path to the basePath that was passed.
/// </summary>
/// <param name="path">
/// A fully qualified provider specific path to an item. The item should exist
/// or the provider should write out an error.
/// </param>
/// <param name="basePath">
/// The path that the return value should be relative to.
/// </param>
/// <returns>
/// A normalized path that is relative to the basePath that was passed. The
/// provider should parse the path parameter, normalize the path, and then
/// return the normalized path relative to the basePath.
/// </returns>
/// <remarks>
/// This method does not have to be purely syntactical parsing of the path. It
/// is encouraged that the provider actually use the path to lookup in its store
/// and create a relative path that matches the casing, and standardized path syntax.
///
/// Note, the base class implementation uses GetParentPath, GetChildName, and MakePath
/// to normalize the path and then make it relative to basePath. All string comparisons
/// are done using StringComparison.InvariantCultureIgnoreCase.
/// </remarks>
protected virtual string NormalizeRelativePath(
string path,
string basePath)
{
using (PSTransactionManager.GetEngineProtectionScope())
{
return ContractRelativePath(path, basePath, false, Context);
}
}
internal string ContractRelativePath(
string path,
string basePath,
bool allowNonExistingPaths,
CmdletProviderContext context)
{
Context = context;
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
if (path.Length == 0)
{
return string.Empty;
}
basePath ??= string.Empty;
providerBaseTracer.WriteLine("basePath = {0}", basePath);
string result = path;
bool originalPathHadTrailingSlash = false;
string normalizedPath = path;
string normalizedBasePath = basePath;
// NTRAID#Windows 7-697922-2009/06/29-leeholm
// WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND
//
// This path normalization got moved here from the MakePath override in V2 to prevent
// over-normalization of paths. This was a net-improvement for providers that use the default
// implementations, but now incorrectly replaces forward slashes with back slashes during the call to
// GetParentPath and GetChildName. This breaks providers that are sensitive to slash direction, the only
// one we are aware of being the Active Directory provider. This change prevents this over-normalization
// from being done on AD paths.
//
// For more information, see Win7:695292. Do not change this code without closely working with the
// Active Directory team.
//
// WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND WORKAROUND
if (!string.Equals(context.ProviderInstance.ProviderInfo.FullName,
@"Microsoft.ActiveDirectory.Management\ActiveDirectory", StringComparison.OrdinalIgnoreCase))
{
normalizedPath = NormalizePath(path);
normalizedBasePath = NormalizePath(basePath);
}
do // false loop
{
// Convert to the correct path separators and trim trailing separators
string originalPath = path;
Stack<string> tokenizedPathStack = null;
if (path.EndsWith(StringLiterals.DefaultPathSeparator))
{
path = path.TrimEnd(StringLiterals.DefaultPathSeparator);
originalPathHadTrailingSlash = true;
}
basePath = basePath.TrimEnd(StringLiterals.DefaultPathSeparator);
// See if the base and the path are already the same. We resolve this to
// ..\Leaf, since resolving "." to "." doesn't offer much information.
if (string.Equals(normalizedPath, normalizedBasePath, StringComparison.OrdinalIgnoreCase) &&
(!originalPath.EndsWith(StringLiterals.DefaultPathSeparator)))
{
string childName = GetChildName(path);
result = MakePath("..", childName);
break;
}
// If the base path isn't really a base, then we resolve to a parent
// path (such as ../../foo)
if (!normalizedPath.StartsWith(normalizedBasePath, StringComparison.OrdinalIgnoreCase) &&
(basePath.Length > 0))
{
result = string.Empty;
string commonBase = GetCommonBase(normalizedPath, normalizedBasePath);
Stack<string> parentNavigationStack = TokenizePathToStack(normalizedBasePath, commonBase);
int parentPopCount = parentNavigationStack.Count;
if (string.IsNullOrEmpty(commonBase))
{
parentPopCount--;
}
for (int leafCounter = 0; leafCounter < parentPopCount; leafCounter++)
{
result = MakePath("..", result);
}
// This is true if we get passed a base path like:
// c:\directory1\directory2
// and an actual path of
// c:\directory1
// Which happens when the user is in c:\directory1\directory2
// and wants to resolve something like:
// ..\..\dir*
// In that case (as above,) we keep the ..\..\directory1
// instead of ".." as would usually be returned
if (!string.IsNullOrEmpty(commonBase))
{
if (string.Equals(normalizedPath, commonBase, StringComparison.OrdinalIgnoreCase) &&
(!normalizedPath.EndsWith(StringLiterals.DefaultPathSeparator)))
{
string childName = GetChildName(path);
result = MakePath("..", result);
result = MakePath(result, childName);
}
else
{
string[] childNavigationItems = TokenizePathToStack(normalizedPath, commonBase).ToArray();
for (int leafCounter = 0; leafCounter < childNavigationItems.Length; leafCounter++)
{
result = MakePath(result, childNavigationItems[leafCounter]);
}
}
}
}
// Otherwise, we resolve to a child path (such as foo/bar)
else
{
tokenizedPathStack = TokenizePathToStack(path, basePath);
// Now we have to normalize the path
// by processing each token on the stack
Stack<string> normalizedPathStack;
try
{
normalizedPathStack = NormalizeThePath(tokenizedPathStack, path, basePath, allowNonExistingPaths);
}
catch (ArgumentException argumentException)
{
WriteError(new ErrorRecord(argumentException, argumentException.GetType().FullName, ErrorCategory.InvalidArgument, null));
result = null;
break;
}
// Now that the path has been normalized, create the relative path
result = CreateNormalizedRelativePathFromStack(normalizedPathStack);
}
} while (false);
if (originalPathHadTrailingSlash)
{
result += StringLiterals.DefaultPathSeparator;
}
return result;
}
/// <summary>
/// Get the common base path of two paths.
/// </summary>
/// <param name="path1">One path.</param>
/// <param name="path2">Another path.</param>
private string GetCommonBase(string path1, string path2)
{
// Always see if the shorter path is a substring of the
// longer path. If it is not, take the child off of the longer
// path and compare again.
while (!string.Equals(path1, path2, StringComparison.OrdinalIgnoreCase))
{
if (path2.Length > path1.Length)
{
path2 = GetParentPath(path2, null);
}
else
{
path1 = GetParentPath(path1, null);
}
}
return path1;
}
/// <summary>
/// Gets the name of the leaf element in the specified path.
/// </summary>
/// <param name="path">
/// The fully qualified path to the item
/// </param>
/// <returns>
/// The leaf element in the path.
/// </returns>
/// <remarks>
/// This should be implemented as a split on the path separator. The characters
/// in the fullPath may not be legal characters in the namespace but may be
/// used in globing or regular expression matching. The provider should not error
/// unless there are no path separators in the fully qualified path.
/// </remarks>
protected virtual string GetChildName(string path)
{
using (PSTransactionManager.GetEngineProtectionScope())
{
// Verify the parameters
if (string.IsNullOrEmpty(path))
{
throw PSTraceSource.NewArgumentException(nameof(path));
}
// Normalize the path
path = NormalizePath(path);
// Trim trailing back slashes
path = path.TrimEnd(StringLiterals.DefaultPathSeparator);
string result = null;
int separatorIndex = path.LastIndexOf(StringLiterals.DefaultPathSeparator);
// Since there was no path separator return the entire path
if (separatorIndex == -1)
{
result = path;
}
// If the full path existed, we must semantically evaluate the parent path
else if (ItemExists(path, Context))
{
string parentPath = GetParentPath(path, null);
// No parent, return the entire path
if (string.IsNullOrEmpty(parentPath))
result = path;
// If the parent path ends with the path separator, we can't split
// the path based on that
else if (parentPath.IndexOf(StringLiterals.DefaultPathSeparator) == (parentPath.Length - 1))
{
separatorIndex = path.IndexOf(parentPath, StringComparison.OrdinalIgnoreCase) + parentPath.Length;
result = path.Substring(separatorIndex);
}
else
{
separatorIndex = path.IndexOf(parentPath, StringComparison.OrdinalIgnoreCase) + parentPath.Length;
result = path.Substring(separatorIndex + 1);
}
}
// Otherwise, use lexical parsing
else
{
result = path.Substring(separatorIndex + 1);
}
return result;
}
}
/// <summary>
/// Determines if the item specified by the path is a container.
/// </summary>
/// <param name="path">
/// The path to the item to determine if it is a container.
/// </param>
/// <returns>
/// true if the item specified by path is a container, false otherwise.
/// </returns>
/// <remarks>
/// Providers override this method to give the user the ability to check
/// to see if a provider object is a container using the test-path -container cmdlet.
///
/// Providers that declare <see cref="System.Management.Automation.Provider.ProviderCapabilities"/>
/// of ExpandWildcards, Filter, Include, or Exclude should ensure that the path passed meets those
/// requirements by accessing the appropriate property from the base class.
///
/// The default implementation of this method throws an <see cref="System.Management.Automation.PSNotSupportedException"/>.
/// </remarks>
protected virtual bool IsItemContainer(string path)
{
using (PSTransactionManager.GetEngineProtectionScope())
{
throw
PSTraceSource.NewNotSupportedException(
SessionStateStrings.CmdletProvider_NotSupported);
}
}
/// <summary>
/// Moves the item specified by path to the specified destination.
/// </summary>
/// <param name="path">
/// The path to the item to be moved.
/// </param>
/// <param name="destination">
/// The path of the destination container.
/// </param>
/// <returns>
/// Nothing is returned, but all the objects that were moved should be written to the WriteItemObject method.
/// </returns>
/// <remarks>
/// Providers override this method to give the user the ability to move provider objects using
/// the move-item cmdlet.
///
/// Providers that declare <see cref="System.Management.Automation.Provider.ProviderCapabilities"/>
/// of ExpandWildcards, Filter, Include, or Exclude should ensure that the path and items being moved
/// meets those requirements by accessing the appropriate property from the base class.
///
/// By default overrides of this method should not move objects over existing items unless the Force
/// property is set to true. For instance, the FileSystem provider should not move c:\temp\foo.txt over
/// c:\bar.txt if c:\bar.txt already exists unless the Force parameter is true.
///
/// If <paramref name="destination"/> exists and is a container then Force isn't required and <paramref name="path"/>
/// should be moved into the <paramref name="destination"/> container as a child.
///
/// The default implementation of this method throws an <see cref="System.Management.Automation.PSNotSupportedException"/>.
/// </remarks>
protected virtual void MoveItem(
string path,
string destination)
{
using (PSTransactionManager.GetEngineProtectionScope())
{
throw
PSTraceSource.NewNotSupportedException(
SessionStateStrings.CmdletProvider_NotSupported);
}
}
/// <summary>
/// Gives the provider an opportunity to attach additional parameters to
/// the move-item cmdlet.
/// </summary>
/// <param name="path">
/// If the path was specified on the command line, this is the path
/// to the item to get the dynamic parameters for.
/// </param>
/// <param name="destination">
/// The path of the destination container.
/// </param>
/// <returns>
/// Overrides of this method should return an object that has properties and fields decorated with
/// parsing attributes similar to a cmdlet class or a
/// <see cref="System.Management.Automation.RuntimeDefinedParameterDictionary"/>.
///
/// The default implementation returns null. (no additional parameters)
/// </returns>
protected virtual object MoveItemDynamicParameters(
string path,
string destination)
{
using (PSTransactionManager.GetEngineProtectionScope())
{
return null;
}
}
#endregion Protected methods
#region private members
/// <summary>
/// When a path contains both forward slash and backslash, we may introduce some errors by
/// normalizing the path. This method does some smart checks to reduce the chances of making
/// those errors.
/// </summary>
/// <param name="path">
/// The path to normalize.
/// </param>
/// <returns>
/// Normalized path or the original path.
/// </returns>
private string NormalizePath(string path)
{
// If we have a mix of slashes, then we may introduce an error by normalizing the path.
// For example: path HKCU:\Test\/ is pointing to a subkey '/' of 'HKCU:\Test', if we
// normalize it, then we will get a wrong path.
//
// Fast return if nothing to normalize.
if (!path.Contains(StringLiterals.AlternatePathSeparator))
{
return path;
}
bool pathHasBackSlash = path.Contains(StringLiterals.DefaultPathSeparator);
string normalizedPath;
// There is a mix of slashes & the path is rooted & the path exists without normalization.
// In this case, we might want to skip the normalization to the path.
if (pathHasBackSlash && IsAbsolutePath(path) && ItemExists(path))
{
// 1. The path exists and ends with a forward slash, in this case, it's very possible the ending forward slash
// make sense to the underlying provider, so we skip normalization
// 2. The path exists, but not anymore after normalization, then we skip normalization
if (path.EndsWith(StringLiterals.AlternatePathSeparator))
{
return path;
}
normalizedPath = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator);
if (!ItemExists(normalizedPath))
{
return path;
}
else
{
return normalizedPath;
}
}
normalizedPath = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator);
return normalizedPath;
}
/// <summary>
/// Test if the path is an absolute path.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private bool IsAbsolutePath(string path)
{
bool result = false;
if (LocationGlobber.IsAbsolutePath(path))
{
result = true;
}
else if (this.PSDriveInfo != null && !string.IsNullOrEmpty(this.PSDriveInfo.Root) &&
path.StartsWith(this.PSDriveInfo.Root, StringComparison.OrdinalIgnoreCase))
{
result = true;
}
return result;
}
/// <summary>
/// Tokenizes the specified path onto a stack.
/// </summary>
/// <param name="path">
/// The path to tokenize.
/// </param>
/// <param name="basePath">
/// The base part of the path that should not be tokenized.
/// </param>
/// <returns>
/// A stack containing the tokenized path with leaf elements on the bottom
/// of the stack and the most ancestral parent at the top.
/// </returns>
private Stack<string> TokenizePathToStack(string path, string basePath)
{
Stack<string> tokenizedPathStack = new Stack<string>();
string tempPath = path;
string previousParent = path;
while (tempPath.Length > basePath.Length)
{
// Get the child name and push it onto the stack
// if its valid
string childName = GetChildName(tempPath);
if (string.IsNullOrEmpty(childName))
{
// Push the parent on and then stop
tokenizedPathStack.Push(tempPath);
break;
}
providerBaseTracer.WriteLine("tokenizedPathStack.Push({0})", childName);
tokenizedPathStack.Push(childName);
// Get the parent path and verify if we have to continue
// tokenizing
tempPath = GetParentPath(tempPath, basePath);
if (tempPath.Length >= previousParent.Length)
{
break;
}
previousParent = tempPath;
}
return tokenizedPathStack;
}
/// <summary>
/// Given the tokenized path, the relative path elements are removed.
/// </summary>
/// <param name="tokenizedPathStack">
/// A stack containing path elements where the leaf most element is at
/// the bottom of the stack and the most ancestral parent is on the top.
/// Generally this stack comes from TokenizePathToStack().
/// </param>
/// <param name="path">
/// The path being normalized. Just used for error reporting.
/// </param>
/// <param name="basePath">
/// The base path to make the path relative to. Just used for error reporting.
/// </param>
/// <param name="allowNonExistingPaths">
/// Determines whether to throw an exception on non-existing paths.
/// </param>
/// <returns>
/// A stack in reverse order with the path elements normalized and all relative
/// path tokens removed.
/// </returns>
private static Stack<string> NormalizeThePath(
Stack<string> tokenizedPathStack, string path,
string basePath, bool allowNonExistingPaths)
{
Stack<string> normalizedPathStack = new Stack<string>();
while (tokenizedPathStack.Count > 0)
{
string childName = tokenizedPathStack.Pop();
providerBaseTracer.WriteLine("childName = {0}", childName);
// Ignore the current directory token
if (childName.Equals(".", StringComparison.OrdinalIgnoreCase))
{
// Just ignore it and move on.
continue;
}
// Make sure we don't have
if (childName.Equals("..", StringComparison.OrdinalIgnoreCase))
{
if (normalizedPathStack.Count > 0)
{
// Pop the result and continue processing
string poppedName = normalizedPathStack.Pop();
providerBaseTracer.WriteLine("normalizedPathStack.Pop() : {0}", poppedName);
continue;
}
else
{
if (!allowNonExistingPaths)
{
PSArgumentException e =
(PSArgumentException)PSTraceSource.NewArgumentException(
nameof(path),
SessionStateStrings.NormalizeRelativePathOutsideBase,
path,
basePath);
throw e;
}
}
}
providerBaseTracer.WriteLine("normalizedPathStack.Push({0})", childName);
normalizedPathStack.Push(childName);
}
return normalizedPathStack;
}
/// <summary>
/// Pops each leaf element of the stack and uses MakePath to generate the relative path.
/// </summary>
/// <param name="normalizedPathStack">
/// The stack containing the leaf elements of the path.
/// </param>
/// <returns>
/// A path that is made up of the leaf elements on the given stack.
/// </returns>
/// <remarks>
/// The elements on the stack start from the leaf element followed by its parent
/// followed by its parent, etc. Each following element on the stack is the parent
/// of the one before it.
/// </remarks>
private string CreateNormalizedRelativePathFromStack(Stack<string> normalizedPathStack)
{
string leafElement = string.Empty;
while (normalizedPathStack.Count > 0)
{
if (string.IsNullOrEmpty(leafElement))
{
leafElement = normalizedPathStack.Pop();
}
else
{
string parentElement = normalizedPathStack.Pop();
leafElement = MakePath(parentElement, leafElement);
}
}
return leafElement;
}
#endregion private members
}
#endregion NavigationCmdletProvider
}
|