This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPathUtils.cs
115 lines (103 loc) · 4.52 KB
/
PathUtils.cs
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
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Text;
using Microsoft.PowerShell.TextUtility.Resources;
namespace Microsoft.PowerShell.TextUtility
{
/// <summary>
/// Defines generic path utilities and helper methods for TextUtility.
/// </summary>
internal static class PathUtils
{
/// <summary>
/// Resolves user provided path using file system provider.
/// </summary>
/// <param name="path">The path to resolve.</param>
/// <param name="command">The command.</param>
/// <param name="isLiteralPath">True if the wildcard resolution should not be attempted.</param>
/// <returns>The resolved (absolute) path.</returns>
internal static string ResolveFilePath(string path, PSCmdlet command, bool isLiteralPath)
{
string resolvedPath;
try
{
ProviderInfo provider = null;
PSDriveInfo drive = null;
PathIntrinsics sessionStatePath = command.SessionState.Path;
if (isLiteralPath)
{
resolvedPath = sessionStatePath.GetUnresolvedProviderPathFromPSPath(path, out provider, out drive);
}
else
{
Collection<string> filePaths = sessionStatePath.GetResolvedProviderPathFromPSPath(path, out provider);
if (!provider.Name.Equals("FileSystem", StringComparison.OrdinalIgnoreCase))
{
ReportOnlySupportsFileSystemPaths(path, command);
}
if (filePaths.Count > 1)
{
ReportMultipleFilesNotSupported(command);
}
resolvedPath = filePaths[0];
}
}
catch (ItemNotFoundException)
{
resolvedPath = null;
}
return resolvedPath;
}
/// <summary>
/// Throws terminating error for not using file system provider.
/// </summary>
/// <param name="path">The path to report.</param>
/// <param name="command">The command.</param>
internal static void ReportOnlySupportsFileSystemPaths(string path, PSCmdlet command)
{
var errorMessage = string.Format(CultureInfo.CurrentCulture, PathUtilityStrings.OnlySupportsFileSystemPaths, path);
var exception = new ArgumentException(errorMessage);
var errorRecord = new ErrorRecord(exception, "OnlySupportsFileSystemPaths", ErrorCategory.InvalidArgument, path);
command.ThrowTerminatingError(errorRecord);
}
/// <summary>
/// Throws terminating error for path not found.
/// </summary>
/// <param name="path">The path to report.</param>
/// <param name="command">The command.</param>
internal static void ReportPathNotFound(string path, PSCmdlet command)
{
var errorMessage = string.Format(CultureInfo.CurrentCulture, PathUtilityStrings.PathNotFound, path);
var exception = new ArgumentException(errorMessage);
var errorRecord = new ErrorRecord(exception, "PathNotFound", ErrorCategory.ObjectNotFound, path);
command.ThrowTerminatingError(errorRecord);
}
/// <summary>
/// Throws terminating error for multiple files being used.
/// </summary>
/// <param name="command">The command.</param>
internal static void ReportMultipleFilesNotSupported(PSCmdlet command)
{
var errorMessage = string.Format(CultureInfo.CurrentCulture, PathUtilityStrings.MultipleFilesNotSupported);
var exception = new ArgumentException(errorMessage);
var errorRecord = new ErrorRecord(exception, "MultipleFilesNotSupported", ErrorCategory.InvalidArgument, null);
command.ThrowTerminatingError(errorRecord);
}
/// <summary>
/// Gets encoding for path.
/// </summary>
/// <param name="path">The path to get file encoding.</param>
/// <returns>The encoding of file.</returns>
internal static Encoding GetPathEncoding(string path)
{
using (var reader = new StreamReader(path, Encoding.Default, detectEncodingFromByteOrderMarks: true))
{
_ = reader.Read();
return reader.CurrentEncoding;
}
}
}
}