File size: 28,332 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma warning disable 1634, 1691
using System.Threading;
using Dbg = System.Management.Automation;
namespace System.Management.Automation
{
/// <summary>
/// Defines a drive that exposes a provider path to the user.
/// </summary>
/// <remarks>
/// A cmdlet provider may want to derive from this class to provide their
/// own public members or to cache information related to the drive. For instance,
/// if a drive is a connection to a remote machine and making that connection
/// is expensive, then the provider may want keep a handle to the connection as
/// a member of their derived <see cref="PSDriveInfo"/> class and use it when
/// the provider is invoked.
/// </remarks>
public class PSDriveInfo : IComparable
{
/// <summary>
/// An instance of the PSTraceSource class used for trace output
/// using "SessionState" as the category.
/// This is the same category as the SessionState tracer class.
/// </summary>
[Dbg.TraceSource(
"PSDriveInfo",
"The namespace navigation tracer")]
private static readonly Dbg.PSTraceSource s_tracer =
Dbg.PSTraceSource.GetTracer("PSDriveInfo",
"The namespace navigation tracer");
/// <summary>
/// Gets or sets the current working directory for the drive.
/// </summary>
public string CurrentLocation
{
get
{
return _currentWorkingDirectory;
}
set
{
_currentWorkingDirectory = value;
}
}
/// <summary>
/// The current working directory for the virtual drive
/// as a relative path from Root.
/// </summary>
private string _currentWorkingDirectory;
/// <summary>
/// Gets the name of the drive.
/// </summary>
public string Name
{
get
{
return _name;
}
}
/// <summary>
/// The name of the virtual drive.
/// </summary>
private string _name;
/// <summary>
/// Gets the name of the provider that root path
/// of the drive represents.
/// </summary>
public ProviderInfo Provider
{
get
{
return _provider;
}
}
/// <summary>
/// The provider information for the provider that implements
/// the functionality for the drive.
/// </summary>
private ProviderInfo _provider;
/// <summary>
/// Gets the root path of the drive.
/// </summary>
public string Root
{
get
{
return _root;
}
internal set
{
_root = value;
}
}
/// <summary>
/// Sets the root of the drive.
/// </summary>
/// <param name="path">
/// The root path to set for the drive.
/// </param>
/// <remarks>
/// This method can only be called during drive
/// creation. A NotSupportedException if this method
/// is called outside of drive creation.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/> is null.
/// </exception>
/// <exception cref="NotSupportedException">
/// If this method gets called any other time except
/// during drive creation.
/// </exception>
internal void SetRoot(string path)
{
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
if (!DriveBeingCreated)
{
NotSupportedException e =
PSTraceSource.NewNotSupportedException();
throw e;
}
_root = path;
}
/// <summary>
/// The root of the virtual drive.
/// </summary>
private string _root;
/// <summary>
/// Gets or sets the description for the drive.
/// </summary>
public string Description { get; set; }
/// <summary>
/// When supported by provider this specifies a maximum drive size.
/// </summary>
public long? MaximumSize { get; internal set; }
/// <summary>
/// Gets the credential to use with the drive.
/// </summary>
public PSCredential Credential { get; } = PSCredential.Empty;
/// <summary>
/// Determines if the root of the drive can
/// be modified during drive creation through
/// the SetRoot method.
/// </summary>
/// <value>
/// True if the drive is being created and the
/// root can be modified through the SetRoot method.
/// False otherwise.
/// </value>
internal bool DriveBeingCreated { get; set; }
/// <summary>
/// True if the drive was automounted by the system,
/// false otherwise.
/// </summary>
/// <value></value>
internal bool IsAutoMounted { get; set; }
/// <summary>
/// True if the drive was automounted by the system,
/// and then manually removed by the user.
/// </summary>
internal bool IsAutoMountedManuallyRemoved { get; set; }
/// <summary>
/// Gets or sets the Persist Switch parameter.
/// If this switch parameter is set then the created PSDrive
/// would be persisted across PowerShell sessions.
/// </summary>
internal bool Persist { get; } = false;
/// <summary>
/// Get or sets the value indicating if the created drive is a network drive.
/// </summary>
internal bool IsNetworkDrive { get; set; } = false;
/// <summary>
/// Gets or sets the UNC path of the drive. This property would be populated only
/// if the created PSDrive is targeting a network drive or else this property
/// would be null.
/// </summary>
public string DisplayRoot { get; internal set; } = null;
/// <summary>
/// Gets or sets if the drive-root relative paths on this drive are separated by a
/// colon or not.
///
/// This is true for all PSDrives on all platforms, except for filesystems on
/// non-Windows platforms.
///
/// This is not a path separator in the sense of separating paths in a single
/// string.
///
/// The biggest difference in filesystem handling between PS internally, and Unix
/// style systems is, that paths on Windows separate the drive letter from the
/// actual path by a colon. The second difference is, that a path that starts with
/// a \ or / on Windows is considered to be a relative path (drive-relative in
/// that case) where a similar path on a Unix style filesystem would be
/// root-relative, which is basically drive-relative for the filesystem, as there
/// is only one filesystem drive.
///
/// This property indicates, that a path can be checked for that drive-relativity
/// by checking for a colon. The main reason for this can be seen in all the
/// places that use this property, where PowerShell's code checks/splits/string
/// manipulates paths according to the colon character. This happens in many
/// places.
///
/// The idea here was to introduce a property that allows a code to query if a
/// PSDrive expects colon to be such a separator or not. I talked to Jim back then
/// about the problem, and this seemed to be a reasonable solution, given that
/// there is no other way to know for a PSDrive if paths can be qualified only in
/// a certain windows way on all platforms, or need special treatment on platforms
/// where colon does not exist as drive separator (regular filesystems on Unix
/// platforms are the only exception).
///
/// Globally this property can also be only true for one single PSDrive, because
/// if there is no drive separator, there is also no drive, and because there is
/// no drive there is no way to match against multiple such drives.
///
/// Additional data:
/// It seems that on single rooted filesystems, only the default
/// drive of "/" needs to set this VolumeSeparatedByColon to false
/// otherwise, creating new drives from the filesystem should actually
/// have this set to true as all the drives will have <string>: except
/// for "/"
/// </summary>
public bool VolumeSeparatedByColon { get; internal set; } = true;
#region ctor
/// <summary>
/// Constructs a new instance of the PSDriveInfo using another PSDriveInfo
/// as a template.
/// </summary>
/// <param name="driveInfo">
/// An existing PSDriveInfo object that should be copied to this instance.
/// </param>
/// <remarks>
/// A protected constructor that derived classes can call with an instance
/// of this class. This allows for easy creation of derived PSDriveInfo objects
/// which can be created in CmdletProvider's NewDrive method using the PSDriveInfo
/// that is passed in.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// If <paramref name="PSDriveInfo"/> is null.
/// </exception>
protected PSDriveInfo(PSDriveInfo driveInfo)
{
if (driveInfo == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(driveInfo));
}
_name = driveInfo.Name;
_provider = driveInfo.Provider;
Credential = driveInfo.Credential;
_currentWorkingDirectory = driveInfo.CurrentLocation;
Description = driveInfo.Description;
this.MaximumSize = driveInfo.MaximumSize;
DriveBeingCreated = driveInfo.DriveBeingCreated;
_hidden = driveInfo._hidden;
IsAutoMounted = driveInfo.IsAutoMounted;
_root = driveInfo._root;
Persist = driveInfo.Persist;
this.Trace();
}
/// <summary>
/// Constructs a drive that maps a PowerShell Path in
/// the shell to a Cmdlet Provider.
/// </summary>
/// <param name="name">
/// The name of the drive.
/// </param>
/// <param name="provider">
/// The name of the provider which implements the functionality
/// for the root path of the drive.
/// </param>
/// <param name="root">
/// The root path of the drive. For example, the root of a
/// drive in the file system can be c:\windows\system32
/// </param>
/// <param name="description">
/// The description for the drive.
/// </param>
/// <param name="credential">
/// The credentials under which all operations on the drive should occur.
/// If null, the current user credential is used.
/// </param>
/// <throws>
/// ArgumentNullException - if <paramref name="name"/>,
/// <paramref name="provider"/>, or <paramref name="root"/>
/// is null.
/// </throws>
public PSDriveInfo(
string name,
ProviderInfo provider,
string root,
string description,
PSCredential credential)
{
// Verify the parameters
if (name == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(name));
}
if (provider == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(provider));
}
if (root == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(root));
}
// Copy the parameters to the local members
_name = name;
_provider = provider;
_root = root;
Description = description;
if (credential != null)
{
Credential = credential;
}
// Set the current working directory to the empty
// string since it is relative to the root.
_currentWorkingDirectory = string.Empty;
Dbg.Diagnostics.Assert(
_currentWorkingDirectory != null,
"The currentWorkingDirectory cannot be null");
// Trace out the fields
this.Trace();
}
/// <summary>
/// Constructs a drive that maps a PowerShell Path in
/// the shell to a Cmdlet Provider.
/// </summary>
/// <param name="name">
/// The name of the drive.
/// </param>
/// <param name="provider">
/// The name of the provider which implements the functionality
/// for the root path of the drive.
/// </param>
/// <param name="root">
/// The root path of the drive. For example, the root of a
/// drive in the file system can be c:\windows\system32
/// </param>
/// <param name="description">
/// The description for the drive.
/// </param>
/// <param name="credential">
/// The credentials under which all operations on the drive should occur.
/// If null, the current user credential is used.
/// </param>
/// <param name="displayRoot">
/// The network path of the drive. This field would be populated only if PSDriveInfo
/// is targeting the network drive or else this filed is null for local drives.
/// </param>
/// <throws>
/// ArgumentNullException - if <paramref name="name"/>,
/// <paramref name="provider"/>, or <paramref name="root"/>
/// is null.
/// </throws>
public PSDriveInfo(
string name,
ProviderInfo provider,
string root,
string description,
PSCredential credential, string displayRoot)
: this(name, provider, root, description, credential)
{
DisplayRoot = displayRoot;
}
/// <summary>
/// Constructs a drive that maps a PowerShell Path in
/// the shell to a Cmdlet Provider.
/// </summary>
/// <param name="name">
/// The name of the drive.
/// </param>
/// <param name="provider">
/// The name of the provider which implements the functionality
/// for the root path of the drive.
/// </param>
/// <param name="root">
/// The root path of the drive. For example, the root of a
/// drive in the file system can be c:\windows\system32
/// </param>
/// <param name="description">
/// The description for the drive.
/// </param>
/// <param name="credential">
/// The credentials under which all operations on the drive should occur.
/// If null, the current user credential is used.
/// </param>
/// <param name="persist">
/// It indicates if the created PSDrive would be
/// persisted across PowerShell sessions.
/// </param>
/// <throws>
/// ArgumentNullException - if <paramref name="name"/>,
/// <paramref name="provider"/>, or <paramref name="root"/>
/// is null.
/// </throws>
public PSDriveInfo(
string name,
ProviderInfo provider,
string root,
string description,
PSCredential credential,
bool persist)
: this(name, provider, root, description, credential)
{
Persist = persist;
}
#endregion ctor
/// <summary>
/// Gets the name of the drive as a string.
/// </summary>
/// <returns>
/// Returns a String that is that name of the drive.
/// </returns>
public override string ToString()
{
return Name;
}
/// <summary>
/// Gets or sets the hidden property. The hidden property
/// determines if the drive should be hidden from the user.
/// </summary>
/// <value>
/// True if the drive should be hidden from the user, false
/// otherwise.
/// </value>
internal bool Hidden
{
get
{
return _hidden;
}
set
{
_hidden = value;
}
}
/// <summary>
/// Determines if the drive should be hidden from the user.
/// </summary>
private bool _hidden;
/// <summary>
/// Sets the name of the drive to a new name.
/// </summary>
/// <param name="newName">
/// The new name for the drive.
/// </param>
/// <remarks>
/// This must be internal so that we allow the renaming of drives
/// via the Core Command API but not through a reference to the
/// drive object. More goes in to renaming a drive than just modifying
/// the name in this class.
/// </remarks>
/// <exception cref="ArgumentException">
/// If <paramref name="newName"/> is null or empty.
/// </exception>
internal void SetName(string newName)
{
if (string.IsNullOrEmpty(newName))
{
throw PSTraceSource.NewArgumentException(nameof(newName));
}
_name = newName;
}
/// <summary>
/// Sets the provider of the drive to a new provider.
/// </summary>
/// <param name="newProvider">
/// The new provider for the drive.
/// </param>
/// <remarks>
/// This must be internal so that we allow the renaming of providers.
/// All drives must be associated with the new provider name and can
/// be changed using the Core Command API but not through a reference to the
/// drive object. More goes in to renaming a provider than just modifying
/// the provider in this class.
/// </remarks>
/// <exception cref="ArgumentNullException">
/// If <paramref name="newProvider"/> is null.
/// </exception>
internal void SetProvider(ProviderInfo newProvider)
{
if (newProvider == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(newProvider));
}
_provider = newProvider;
}
/// <summary>
/// Traces the virtual drive.
/// </summary>
internal void Trace()
{
s_tracer.WriteLine(
"A drive was found:");
if (Name != null)
{
s_tracer.WriteLine(
"\tName: {0}",
Name);
}
if (Provider != null)
{
s_tracer.WriteLine(
"\tProvider: {0}",
Provider);
}
if (Root != null)
{
s_tracer.WriteLine(
"\tRoot: {0}",
Root);
}
if (CurrentLocation != null)
{
s_tracer.WriteLine(
"\tCWD: {0}",
CurrentLocation);
}
if (Description != null)
{
s_tracer.WriteLine(
"\tDescription: {0}",
Description);
}
}
/// <summary>
/// Compares this instance to the specified drive.
/// </summary>
/// <param name="drive">
/// A PSDriveInfo object to compare.
/// </param>
/// <returns>
/// A signed number indicating the relative values of this instance and object specified.
/// Return Value: Less than zero Meaning: This instance is less than object.
/// Return Value: Zero Meaning: This instance is equal to object.
/// Return Value: Greater than zero Meaning: This instance is greater than object or object is a null reference.
/// </returns>
public int CompareTo(PSDriveInfo drive)
{
#pragma warning disable 56506
if (drive == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(drive));
}
return string.Compare(Name, drive.Name, StringComparison.OrdinalIgnoreCase);
#pragma warning restore 56506
}
/// <summary>
/// Compares this instance to the specified object. The object must be a PSDriveInfo.
/// </summary>
/// <param name="obj">
/// An object to compare.
/// </param>
/// <returns>
/// A signed number indicating the relative values of this
/// instance and object specified.
/// </returns>
/// <exception cref="ArgumentException">
/// If <paramref name="obj"/> is not a PSDriveInfo instance.
/// </exception>
public int CompareTo(object obj)
{
PSDriveInfo drive = obj as PSDriveInfo;
if (drive == null)
{
ArgumentException e =
PSTraceSource.NewArgumentException(
nameof(obj),
SessionStateStrings.OnlyAbleToComparePSDriveInfo);
throw e;
}
return (CompareTo(drive));
}
/// <summary>
/// Compares this instance to the specified object.
/// </summary>
/// <param name="obj">
/// An object to compare.
/// </param>
/// <returns>
/// True if the drive names are equal, false otherwise.
/// </returns>
public override bool Equals(object obj)
{
if (obj is PSDriveInfo)
{
return CompareTo(obj) == 0;
}
else
{
return false;
}
}
/// <summary>
/// Compares this instance to the specified object.
/// </summary>
/// <param name="drive">
/// An object to compare.
/// </param>
/// <returns>
/// True if the drive names are equal, false otherwise.
/// </returns>
public bool Equals(PSDriveInfo drive)
{
return CompareTo(drive) == 0;
}
/// <summary>
/// Equality operator for the drive determines if the drives
/// are equal by having the same name.
/// </summary>
/// <param name="drive1">
/// The first object to compare to the second.
/// </param>
/// <param name="drive2">
/// The second object to compare to the first.
/// </param>
/// <returns>
/// True if the objects are PSDriveInfo objects and have the same name,
/// false otherwise.
/// </returns>
public static bool operator ==(PSDriveInfo drive1, PSDriveInfo drive2)
{
object drive1Object = drive1;
object drive2Object = drive2;
if ((drive1Object == null) == (drive2Object == null))
{
if (drive1Object != null)
{
return drive1.Equals(drive2);
}
return true;
}
else
{
return false;
}
}
/// <summary>
/// Inequality operator for the drive determines if the drives
/// are not equal by using the drive name.
/// </summary>
/// <param name="drive1">
/// The first object to compare to the second.
/// </param>
/// <param name="drive2">
/// The second object to compare to the first.
/// </param>
/// <returns>
/// True if the PSDriveInfo objects do not have the same name,
/// false otherwise.
/// </returns>
public static bool operator !=(PSDriveInfo drive1, PSDriveInfo drive2)
{
return !(drive1 == drive2);
}
/// <summary>
/// Compares the specified drives to determine if drive1 is less than
/// drive2.
/// </summary>
/// <param name="drive1">
/// The drive to determine if it is less than the other drive.
/// </param>
/// <param name="drive2">
/// The drive to compare drive1 against.
/// </param>
/// <returns>
/// True if the lexical comparison of drive1's name is less than drive2's name.
/// </returns>
public static bool operator <(PSDriveInfo drive1, PSDriveInfo drive2)
{
object drive1Object = drive1;
object drive2Object = drive2;
if (drive1Object == null)
{
return (drive2Object != null);
}
else
{
if (drive2Object == null)
{
// Since drive1 is not null and drive2 is, drive1 is greater than drive2
return false;
}
else
{
// Since drive1 and drive2 are not null use the CompareTo
return drive1.CompareTo(drive2) < 0;
}
}
}
/// <summary>
/// Compares the specified drives to determine if drive1 is greater than
/// drive2.
/// </summary>
/// <param name="drive1">
/// The drive to determine if it is greater than the other drive.
/// </param>
/// <param name="drive2">
/// The drive to compare drive1 against.
/// </param>
/// <returns>
/// True if the lexical comparison of drive1's name is greater than drive2's name.
/// </returns>
public static bool operator >(PSDriveInfo drive1, PSDriveInfo drive2)
{
object drive1Object = drive1;
object drive2Object = drive2;
if ((drive1Object == null))
{
// Since both drives are null, they are equal
// Since drive1 is null it is less than drive2 which is not null
return false;
}
else
{
if (drive2Object == null)
{
// Since drive1 is not null and drive2 is, drive1 is greater than drive2
return true;
}
else
{
// Since drive1 and drive2 are not null use the CompareTo
return drive1.CompareTo(drive2) > 0;
}
}
}
/// <summary>
/// Gets the hash code for this instance.
/// </summary>
/// <returns>The result of base.GetHashCode().</returns>
/// <!-- Override the base GetHashCode because the compiler complains
/// if you don't when you implement operator== and operator!= -->
public override int GetHashCode()
{
return base.GetHashCode();
}
private PSNoteProperty _noteProperty;
internal PSNoteProperty GetNotePropertyForProviderCmdlets(string name)
{
if (_noteProperty == null)
{
Interlocked.CompareExchange(ref _noteProperty,
new PSNoteProperty(name, this), null);
}
return _noteProperty;
}
}
}
|