forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationGlobber.cs
More file actions
4729 lines (4098 loc) · 188 KB
/
Copy pathLocationGlobber.cs
File metadata and controls
4729 lines (4098 loc) · 188 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Provider;
using System.Text;
using Dbg = System.Management.Automation;
namespace System.Management.Automation
{
/// <summary>
/// Implements the interfaces used by navigation commands to work with
/// the virtual drive system.
/// </summary>
internal sealed class LocationGlobber
{
#region Trace object
/// <summary>
/// An instance of the PSTraceSource class used for trace output
/// using "LocationGlobber" as the category.
/// </summary>
[Dbg.TraceSourceAttribute(
"LocationGlobber",
"The location globber converts PowerShell paths with glob characters to zero or more paths.")]
private static readonly Dbg.PSTraceSource s_tracer =
Dbg.PSTraceSource.GetTracer("LocationGlobber",
"The location globber converts PowerShell paths with glob characters to zero or more paths.");
/// <summary>
/// User level tracing for path resolution.
/// </summary>
[Dbg.TraceSourceAttribute(
"PathResolution",
"Traces the path resolution algorithm.")]
private static readonly Dbg.PSTraceSource s_pathResolutionTracer =
Dbg.PSTraceSource.GetTracer(
"PathResolution",
"Traces the path resolution algorithm.",
false);
#endregion Trace object
#region Constructor
/// <summary>
/// Constructs an instance of the LocationGlobber from the current SessionState.
/// </summary>
/// <param name="sessionState">
/// The instance of session state on which this location globber acts.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="sessionState"/> is null.
/// </exception>
internal LocationGlobber(SessionState sessionState)
{
if (sessionState == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(sessionState));
}
_sessionState = sessionState;
}
#endregion Constructor
#region Public methods
#region PowerShell paths from PowerShell path globbing
/// <summary>
/// Converts a PowerShell path containing glob characters to PowerShell paths that match
/// the glob string.
/// </summary>
/// <param name="path">
/// A PowerShell path containing glob characters.
/// </param>
/// <param name="allowNonexistingPaths">
/// If true, a ItemNotFoundException will not be thrown for non-existing
/// paths. Instead an appropriate path will be returned as if it did exist.
/// </param>
/// <param name="providerInstance">
/// The provider instance used to resolve the path.
/// </param>
/// <returns>
/// The PowerShell paths that match the glob string.
/// </returns>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/> is null.
/// </exception>
/// <exception cref="ProviderNotFoundException">
/// If <paramref name="path"/> is a provider-qualified path
/// and the specified provider does not exist.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider throws an exception when its MakePath gets
/// called.
/// </exception>
/// <exception cref="NotSupportedException">
/// If the provider does not support multiple items.
/// </exception>
/// <exception cref="InvalidOperationException">
/// If the home location for the provider is not set and
/// <paramref name="path"/> starts with a "~".
/// </exception>
/// <exception cref="ItemNotFoundException">
/// If <paramref name="path"/> does not contain glob characters and
/// could not be found.
/// </exception>
internal Collection<PathInfo> GetGlobbedMonadPathsFromMonadPath(
string path,
bool allowNonexistingPaths,
out CmdletProvider providerInstance)
{
CmdletProviderContext context =
new CmdletProviderContext(_sessionState.Internal.ExecutionContext);
return GetGlobbedMonadPathsFromMonadPath(path, allowNonexistingPaths, context, out providerInstance);
}
/// <summary>
/// Converts a PowerShell path containing glob characters to PowerShell paths that match
/// the glob string.
/// </summary>
/// <param name="path">
/// A PowerShell path containing glob characters.
/// </param>
/// <param name="allowNonexistingPaths">
/// If true, a ItemNotFoundException will not be thrown for non-existing
/// paths. Instead an appropriate path will be returned as if it did exist.
/// </param>
/// <param name="context">
/// The context under which the command is running.
/// </param>
/// <param name="providerInstance">
/// The instance of the provider used to resolve the path.
/// </param>
/// <returns>
/// The PowerShell paths that match the glob string.
/// </returns>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/> or <paramref name="context"/> is null.
/// </exception>
/// <exception cref="ProviderNotFoundException">
/// If <paramref name="path"/> is a provider-qualified path
/// and the specified provider does not exist.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider throws an exception when its MakePath gets
/// called.
/// </exception>
/// <exception cref="NotSupportedException">
/// If the provider does not support multiple items.
/// </exception>
/// <exception cref="InvalidOperationException">
/// If the home location for the provider is not set and
/// <paramref name="path"/> starts with a "~".
/// </exception>
/// <exception cref="ItemNotFoundException">
/// If <paramref name="path"/> does not contain glob characters and
/// could not be found.
/// </exception>
/// <exception cref="PipelineStoppedException">
/// If <paramref name="context"/> has been signaled for
/// StopProcessing.
/// </exception>
internal Collection<PathInfo> GetGlobbedMonadPathsFromMonadPath(
string path,
bool allowNonexistingPaths,
CmdletProviderContext context,
out CmdletProvider providerInstance)
{
providerInstance = null;
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
if (context == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(context));
}
Collection<PathInfo> result;
using (s_pathResolutionTracer.TraceScope("Resolving MSH path \"{0}\" to MSH path", path))
{
TraceFilters(context);
// First check to see if the path starts with a ~ (home)
if (IsHomePath(path))
{
using (s_pathResolutionTracer.TraceScope("Resolving HOME relative path."))
{
path = GetHomeRelativePath(path);
}
}
// Now determine how to parse the path
bool isProviderDirectPath = IsProviderDirectPath(path);
bool isProviderQualifiedPath = IsProviderQualifiedPath(path);
if (isProviderDirectPath || isProviderQualifiedPath)
{
result =
ResolvePSPathFromProviderPath(
path,
context,
allowNonexistingPaths,
isProviderDirectPath,
isProviderQualifiedPath,
out providerInstance);
}
else
{
result =
ResolveDriveQualifiedPath(
path,
context,
allowNonexistingPaths,
out providerInstance);
}
if (!allowNonexistingPaths &&
result.Count < 1 &&
(!WildcardPattern.ContainsWildcardCharacters(path) || context.SuppressWildcardExpansion) &&
(context.Include == null || context.Include.Count == 0) &&
(context.Exclude == null || context.Exclude.Count == 0))
{
// Since we are not globbing, throw an exception since
// the path doesn't exist
ItemNotFoundException pathNotFound =
new ItemNotFoundException(
path,
"PathNotFound",
SessionStateStrings.PathNotFound);
s_pathResolutionTracer.TraceError("Item does not exist: {0}", path);
throw pathNotFound;
}
}
return result;
}
private Collection<string> ResolveProviderPathFromProviderPath(
string providerPath,
string providerId,
bool allowNonexistingPaths,
CmdletProviderContext context,
out CmdletProvider providerInstance
)
{
// Check the provider capabilities before globbing
providerInstance = _sessionState.Internal.GetProviderInstance(providerId);
ContainerCmdletProvider containerCmdletProvider = providerInstance as ContainerCmdletProvider;
ItemCmdletProvider itemProvider = providerInstance as ItemCmdletProvider;
Collection<string> stringResult = new Collection<string>();
if (!context.SuppressWildcardExpansion)
{
// See if the provider will expand the wildcard
if (CmdletProviderManagementIntrinsics.CheckProviderCapabilities(
ProviderCapabilities.ExpandWildcards,
providerInstance.ProviderInfo))
{
s_pathResolutionTracer.WriteLine("Wildcard matching is being performed by the provider.");
// Only do the expansion if the path actually contains wildcard
// characters.
if ((itemProvider != null) &&
(WildcardPattern.ContainsWildcardCharacters(providerPath)))
{
stringResult = new Collection<string>(itemProvider.ExpandPath(providerPath, context));
}
else
{
stringResult.Add(providerPath);
}
}
else
{
s_pathResolutionTracer.WriteLine("Wildcard matching is being performed by the engine.");
if (containerCmdletProvider != null)
{
// Since it is really a provider-internal path, use provider-to-provider globbing
// and then add back on the provider ID.
stringResult =
GetGlobbedProviderPathsFromProviderPath(
providerPath,
allowNonexistingPaths,
containerCmdletProvider,
context);
}
else
{
// For simple CmdletProvider instances, we can't resolve the paths any
// further, so just return the providerPath
stringResult.Add(providerPath);
}
}
}
// They are suppressing wildcard expansion
else
{
if (itemProvider != null)
{
if (allowNonexistingPaths || itemProvider.ItemExists(providerPath, context))
{
stringResult.Add(providerPath);
}
}
else
{
stringResult.Add(providerPath);
}
}
// Make sure this resolved to something
if ((!allowNonexistingPaths) &&
stringResult.Count < 1 &&
!WildcardPattern.ContainsWildcardCharacters(providerPath) &&
(context.Include == null || context.Include.Count == 0) &&
(context.Exclude == null || context.Exclude.Count == 0))
{
ItemNotFoundException pathNotFound =
new ItemNotFoundException(
providerPath,
"PathNotFound",
SessionStateStrings.PathNotFound);
s_pathResolutionTracer.TraceError("Item does not exist: {0}", providerPath);
throw pathNotFound;
}
return stringResult;
}
private Collection<PathInfo> ResolvePSPathFromProviderPath(
string path,
CmdletProviderContext context,
bool allowNonexistingPaths,
bool isProviderDirectPath,
bool isProviderQualifiedPath,
out CmdletProvider providerInstance)
{
Collection<PathInfo> result = new Collection<PathInfo>();
providerInstance = null;
string providerId = null;
PSDriveInfo drive = null;
// The path is a provide direct path so use the current
// provider and don't modify the path.
string providerPath = null;
if (isProviderDirectPath)
{
s_pathResolutionTracer.WriteLine("Path is PROVIDER-DIRECT");
providerPath = path;
providerId = _sessionState.Path.CurrentLocation.Provider.Name;
}
else if (isProviderQualifiedPath)
{
s_pathResolutionTracer.WriteLine("Path is PROVIDER-QUALIFIED");
providerPath = ParseProviderPath(path, out providerId);
}
s_pathResolutionTracer.WriteLine("PROVIDER-INTERNAL path: {0}", providerPath);
s_pathResolutionTracer.WriteLine("Provider: {0}", providerId);
Collection<string> stringResult = ResolveProviderPathFromProviderPath(
providerPath,
providerId,
allowNonexistingPaths,
context,
out providerInstance
);
// Get the hidden drive for the provider
drive = providerInstance.ProviderInfo.HiddenDrive;
// Now fix the paths
foreach (string globbedPath in stringResult)
{
string escapedPath = globbedPath;
// Making sure to obey the StopProcessing.
if (context.Stopping)
{
throw new PipelineStoppedException();
}
string constructedProviderPath = null;
if (IsProviderDirectPath(escapedPath))
{
constructedProviderPath = escapedPath;
}
else
{
constructedProviderPath =
string.Format(
System.Globalization.CultureInfo.InvariantCulture,
"{0}::{1}",
providerId,
escapedPath);
}
result.Add(new PathInfo(drive, providerInstance.ProviderInfo, constructedProviderPath, _sessionState));
s_pathResolutionTracer.WriteLine("RESOLVED PATH: {0}", constructedProviderPath);
}
return result;
}
private Collection<PathInfo> ResolveDriveQualifiedPath(
string path,
CmdletProviderContext context,
bool allowNonexistingPaths,
out CmdletProvider providerInstance)
{
providerInstance = null;
PSDriveInfo drive = null;
Collection<PathInfo> result = new Collection<PathInfo>();
s_pathResolutionTracer.WriteLine("Path is DRIVE-QUALIFIED");
string relativePath =
GetDriveRootRelativePathFromPSPath(
path,
context,
!context.SuppressWildcardExpansion,
out drive,
out providerInstance);
Dbg.Diagnostics.Assert(
drive != null,
"GetDriveRootRelativePathFromPSPath should always return a valid drive");
Dbg.Diagnostics.Assert(
relativePath != null,
"There should always be a way to generate a provider path for a " +
"given path");
s_pathResolutionTracer.WriteLine("DRIVE-RELATIVE path: {0}", relativePath);
s_pathResolutionTracer.WriteLine("Drive: {0}", drive.Name);
s_pathResolutionTracer.WriteLine("Provider: {0}", drive.Provider);
// Associate the drive with the context
context.Drive = drive;
providerInstance = _sessionState.Internal.GetContainerProviderInstance(drive.Provider);
ContainerCmdletProvider containerCmdletProvider = providerInstance as ContainerCmdletProvider;
ItemCmdletProvider itemProvider = providerInstance as ItemCmdletProvider;
ProviderInfo provider = providerInstance.ProviderInfo;
string userPath = null;
string itemPath = null;
if (drive.Hidden)
{
userPath = GetProviderQualifiedPath(relativePath, provider);
itemPath = relativePath;
}
else
{
userPath = GetDriveQualifiedPath(relativePath, drive);
itemPath = GetProviderPath(path, context);
}
s_pathResolutionTracer.WriteLine("PROVIDER path: {0}", itemPath);
Collection<string> stringResult = new Collection<string>();
if (!context.SuppressWildcardExpansion)
{
// See if the provider will expand the wildcard
if (CmdletProviderManagementIntrinsics.CheckProviderCapabilities(
ProviderCapabilities.ExpandWildcards,
provider))
{
s_pathResolutionTracer.WriteLine("Wildcard matching is being performed by the provider.");
// Only do the expansion if the path actually contains wildcard
// characters.
if ((itemProvider != null) &&
(WildcardPattern.ContainsWildcardCharacters(relativePath)))
{
foreach (string pathResult in itemProvider.ExpandPath(itemPath, context))
{
stringResult.Add(
GetDriveRootRelativePathFromProviderPath(pathResult, drive, context));
}
}
else
{
stringResult.Add(GetDriveRootRelativePathFromProviderPath(itemPath, drive, context));
}
}
else
{
s_pathResolutionTracer.WriteLine("Wildcard matching is being performed by the engine.");
// Now perform the globbing
stringResult =
ExpandMshGlobPath(
relativePath,
allowNonexistingPaths,
drive,
containerCmdletProvider,
context);
}
}
// They are suppressing wildcard expansion
else
{
if (itemProvider != null)
{
if (allowNonexistingPaths || itemProvider.ItemExists(itemPath, context))
{
stringResult.Add(userPath);
}
}
else
{
stringResult.Add(userPath);
}
}
// Make sure this resolved to something
if ((!allowNonexistingPaths) &&
stringResult.Count < 1 &&
!WildcardPattern.ContainsWildcardCharacters(path) &&
(context.Include == null || context.Include.Count == 0) &&
(context.Exclude == null || context.Exclude.Count == 0))
{
ItemNotFoundException pathNotFound =
new ItemNotFoundException(
path,
"PathNotFound",
SessionStateStrings.PathNotFound);
s_pathResolutionTracer.TraceError("Item does not exist: {0}", path);
throw pathNotFound;
}
// Now fix the paths
foreach (string expandedPath in stringResult)
{
// Make sure to obey StopProcessing
if (context.Stopping)
{
throw new PipelineStoppedException();
}
// Add the drive back into the path
userPath = null;
if (drive.Hidden)
{
if (IsProviderDirectPath(expandedPath))
{
userPath = expandedPath;
}
else
{
userPath =
LocationGlobber.GetProviderQualifiedPath(
expandedPath,
provider);
}
}
else
{
userPath =
LocationGlobber.GetDriveQualifiedPath(
expandedPath,
drive);
}
result.Add(new PathInfo(drive, provider, userPath, _sessionState));
s_pathResolutionTracer.WriteLine("RESOLVED PATH: {0}", userPath);
}
return result;
}
#endregion PowerShell paths from PowerShell path globbing
#region Provider paths from PowerShell path globbing
/// <summary>
/// Converts a PowerShell path containing glob characters to the provider
/// specific paths matching the glob strings.
/// </summary>
/// <param name="path">
/// A PowerShell path containing glob characters.
/// </param>
/// <param name="allowNonexistingPaths">
/// If true, a ItemNotFoundException will not be thrown for non-existing
/// paths. Instead an appropriate path will be returned as if it did exist.
/// </param>
/// <param name="provider">
/// Returns the information of the provider that was used to do the globbing.
/// </param>
/// <param name="providerInstance">
/// The instance of the provider used to resolve the path.
/// </param>
/// <returns>
/// An array of provider specific paths that matched the PowerShell glob path.
/// </returns>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/> is null.
/// </exception>
/// <exception cref="ProviderNotFoundException">
/// If the path is a provider-qualified path for a provider that is
/// not loaded into the system.
/// </exception>
/// <exception cref="DriveNotFoundException">
/// If the <paramref name="path"/> refers to a drive that could not be found.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider used to build the path threw an exception.
/// </exception>
/// <exception cref="NotSupportedException">
/// If the provider that the <paramref name="path"/> represents is not a NavigationCmdletProvider
/// or ContainerCmdletProvider.
/// </exception>
/// <exception cref="InvalidOperationException">
/// If the <paramref name="path"/> starts with "~" and the home location is not set for
/// the provider.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider associated with the <paramref name="path"/> threw an
/// exception when its GetParentPath or MakePath was called while
/// processing the <paramref name="path"/>.
/// </exception>
/// <exception cref="ItemNotFoundException">
/// If <paramref name="path"/> does not contain glob characters and
/// could not be found.
/// </exception>
/// <exception>
/// Any exception can be thrown by the provider that is called to build
/// the provider path.
/// </exception>
internal Collection<string> GetGlobbedProviderPathsFromMonadPath(
string path,
bool allowNonexistingPaths,
out ProviderInfo provider,
out CmdletProvider providerInstance)
{
providerInstance = null;
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
CmdletProviderContext context =
new CmdletProviderContext(_sessionState.Internal.ExecutionContext);
return GetGlobbedProviderPathsFromMonadPath(path, allowNonexistingPaths, context, out provider, out providerInstance);
}
/// <summary>
/// Converts a PowerShell path containing glob characters to the provider
/// specific paths matching the glob strings.
/// </summary>
/// <param name="path">
/// A PowerShell path containing glob characters.
/// </param>
/// <param name="allowNonexistingPaths">
/// If true, a ItemNotFoundException will not be thrown for non-existing
/// paths. Instead an appropriate path will be returned as if it did exist.
/// </param>
/// <param name="context">
/// The context under which the command is running.
/// </param>
/// <param name="provider">
/// Returns the information of the provider that was used to do the globbing.
/// </param>
/// <param name="providerInstance">
/// The instance of the provider used to resolve the path.
/// </param>
/// <returns>
/// An array of provider specific paths that matched the PowerShell glob path.
/// </returns>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/> or <paramref name="context"/> is null.
/// </exception>
/// <exception cref="ProviderNotFoundException">
/// If the path is a provider-qualified path for a provider that is
/// not loaded into the system.
/// </exception>
/// <exception cref="DriveNotFoundException">
/// If the <paramref name="path"/> refers to a drive that could not be found.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider used to build the path threw an exception.
/// </exception>
/// <exception cref="NotSupportedException">
/// If the provider that the <paramref name="path"/> represents is not a NavigationCmdletProvider
/// or ContainerCmdletProvider.
/// </exception>
/// <exception cref="InvalidOperationException">
/// If the <paramref name="path"/> starts with "~" and the home location is not set for
/// the provider.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider associated with the <paramref name="path"/> threw an
/// exception when its GetParentPath or MakePath was called while
/// processing the <paramref name="path"/>.
/// </exception>
/// <exception cref="ItemNotFoundException">
/// If <paramref name="path"/> does not contain glob characters and
/// could not be found.
/// </exception>
/// <exception>
/// Any exception can be thrown by the provider that is called to build
/// the provider path.
/// </exception>
internal Collection<string> GetGlobbedProviderPathsFromMonadPath(
string path,
bool allowNonexistingPaths,
CmdletProviderContext context,
out ProviderInfo provider,
out CmdletProvider providerInstance)
{
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
if (context == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(context));
}
using (s_pathResolutionTracer.TraceScope("Resolving MSH path \"{0}\" to PROVIDER-INTERNAL path", path))
{
TraceFilters(context);
// Remove the drive from the context if this path is not associated with a drive
if (IsProviderQualifiedPath(path))
{
context.Drive = null;
}
PSDriveInfo drive = null;
string providerPath = GetProviderPath(path, context, out provider, out drive);
if (providerPath == null)
{
providerInstance = null;
s_tracer.WriteLine("provider returned a null path so return an empty array");
s_pathResolutionTracer.WriteLine("Provider '{0}' returned null", provider);
return new Collection<string>();
}
if (drive != null)
{
context.Drive = drive;
}
Collection<string> paths = new Collection<string>();
foreach (PathInfo currentPath in
GetGlobbedMonadPathsFromMonadPath(
path,
allowNonexistingPaths,
context,
out providerInstance))
{
paths.Add(currentPath.ProviderPath);
}
return paths;
}
}
#endregion Provider paths from Monad path globbing
#region Provider paths from provider path globbing
/// <summary>
/// Given a provider specific path that contains glob characters, this method
/// will perform the globbing using the specified provider and return the
/// matching provider specific paths.
/// </summary>
/// <param name="path">
/// The path containing the glob characters to resolve.
/// </param>
/// <param name="allowNonexistingPaths">
/// If true, a ItemNotFoundException will not be thrown for non-existing
/// paths. Instead an appropriate path will be returned as if it did exist.
/// </param>
/// <param name="providerId">
/// The ID of the provider to use to do the resolution.
/// </param>
/// <param name="providerInstance">
/// The instance of the provider that was used to resolve the path.
/// </param>
/// <returns>
/// An array of provider specific paths that match the glob path.
/// </returns>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/> is null.
/// </exception>
/// <exception cref="ProviderNotFoundException">
/// If <paramref name="providerId"/> references a provider that does not exist.
/// </exception>
/// <exception cref="NotSupportedException">
/// If the <paramref name="providerId"/> references a provider that is not
/// a ContainerCmdletProvider.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider used to build the path threw an exception.
/// </exception>
/// <exception cref="InvalidOperationException">
/// If the <paramref name="path"/> starts with "~" and the home location is not set for
/// the provider.
/// </exception>
/// <exception cref="ItemNotFoundException">
/// If <paramref name="path"/> does not contain glob characters and
/// could not be found.
/// </exception>
/// <exception>
/// Any exception can be thrown by the provider that is called to build
/// the provider path.
/// </exception>
internal Collection<string> GetGlobbedProviderPathsFromProviderPath(
string path,
bool allowNonexistingPaths,
string providerId,
out CmdletProvider providerInstance)
{
providerInstance = null;
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
CmdletProviderContext context =
new CmdletProviderContext(_sessionState.Internal.ExecutionContext);
Collection<string> results =
GetGlobbedProviderPathsFromProviderPath(
path,
allowNonexistingPaths,
providerId,
context,
out providerInstance);
if (context.HasErrors())
{
// Throw the first error
ErrorRecord errorRecord = context.GetAccumulatedErrorObjects()[0];
if (errorRecord != null)
{
throw errorRecord.Exception;
}
}
return results;
}
/// <summary>
/// Given a provider specific path that contains glob characters, this method
/// will perform the globbing using the specified provider and return the
/// matching provider specific paths.
/// </summary>
/// <param name="path">
/// The path containing the glob characters to resolve. The path must be in the
/// form providerId::providerPath.
/// </param>
/// <param name="allowNonexistingPaths">
/// If true, a ItemNotFoundException will not be thrown for non-existing
/// paths. Instead an appropriate path will be returned as if it did exist.
/// </param>
/// <param name="providerId">
/// The provider identifier for the provider to use to do the globbing.
/// </param>
/// <param name="context">
/// The context under which the command is occurring.
/// </param>
/// <param name="providerInstance">
/// An instance of the provider that was used to perform the globbing.
/// </param>
/// <returns>
/// An array of provider specific paths that match the glob path.
/// </returns>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/>, <paramref name="providerId"/>, or
/// <paramref name="context"/> is null.
/// </exception>
/// <exception cref="ProviderNotFoundException">
/// If <paramref name="providerId"/> references a provider that does not exist.
/// </exception>
/// <exception cref="NotSupportedException">
/// If the <paramref name="providerId"/> references a provider that is not
/// a ContainerCmdletProvider.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider used to build the path threw an exception.
/// </exception>
/// <exception cref="InvalidOperationException">
/// If the <paramref name="path"/> starts with "~" and the home location is not set for
/// the provider.
/// </exception>
/// <exception>
/// Any exception can be thrown by the provider that is called to build
/// the provider path.
/// </exception>
internal Collection<string> GetGlobbedProviderPathsFromProviderPath(
string path,
bool allowNonexistingPaths,
string providerId,
CmdletProviderContext context,
out CmdletProvider providerInstance)
{
providerInstance = null;
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
if (providerId == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(providerId));
}
if (context == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(context));
}
using (s_pathResolutionTracer.TraceScope("Resolving PROVIDER-INTERNAL path \"{0}\" to PROVIDER-INTERNAL path", path))
{
TraceFilters(context);
return ResolveProviderPathFromProviderPath(
path,
providerId,
allowNonexistingPaths,
context,
out providerInstance);
}
}
#endregion Provider path to provider paths globbing
#region Path manipulation
/// <summary>
/// Gets a provider specific path when given an Msh path without resolving the
/// glob characters.
/// </summary>
/// <param name="path">
/// An Msh path.
/// </param>
/// <returns>
/// A provider specific path that the Msh path represents.
/// </returns>
/// <exception cref="ArgumentNullException">
/// If <paramref name="path"/> is null.
/// </exception>
/// <exception cref="ProviderNotFoundException">
/// If the path is a provider-qualified path for a provider that is
/// not loaded into the system.
/// </exception>
/// <exception cref="DriveNotFoundException">
/// If the <paramref name="path"/> refers to a drive that could not be found.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider used to build the path threw an exception.
/// </exception>
/// <exception cref="NotSupportedException">
/// If the provider that the <paramref name="path"/> represents is not a NavigationCmdletProvider
/// or ContainerCmdletProvider.
/// </exception>
/// <exception cref="InvalidOperationException">
/// If the <paramref name="path"/> starts with "~" and the home location is not set for
/// the provider.
/// </exception>
/// <exception cref="ProviderInvocationException">
/// If the provider specified by <paramref name="path"/> threw an