forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
339 lines (305 loc) · 10.5 KB
/
Copy pathUtils.cs
File metadata and controls
339 lines (305 loc) · 10.5 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.IO;
using System.Management.Automation;
using Microsoft.PowerShell.Commands;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Security;
using System.Security.Principal;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using DWORD = System.UInt32;
using BOOL = System.UInt32;
using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
namespace Microsoft.PowerShell
{
internal static class SecurityUtils
{
/// <summary>
/// gets the size of a file
/// </summary>
///
/// <param name="filePath"> path to file </param>
///
/// <returns> file size </returns>
///
/// <remarks> </remarks>
///
internal static long GetFileSize(string filePath)
{
long size = 0;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
size = fs.Length;
}
return size;
}
#if false
/// <summary>
/// throw if file is smaller than 4 bytes in length
/// </summary>
///
/// <param name="filePath"> path to file </param>
///
/// <returns> Does not return a value </returns>
///
/// <remarks> </remarks>
///
internal static void CheckIfFileSmallerThan4Bytes(string filePath)
{
if (GetFileSize(filePath) < 4)
{
string message =
StringUtil.Format(
UtilsStrings.FileSmallerThan4Bytes,
new object[] { filePath }
);
throw PSTraceSource.NewArgumentException(message, "path");
/*
// 2004/10/22-JonN The above form of the constructor
// no longer exists. This should probably be as below,
// however I have not tested this. This method is not
// used so I have removed it.
throw PSTraceSource.NewArgumentException(
"path",
"Utils",
"FileSmallerThan4Bytes",
filePath
);
*/
}
}
#endif
/// <summary>
/// present a prompt for a SecureString data
/// </summary>
///
/// <param name="hostUI"> ref to host ui interface </param>
///
/// <param name="prompt"> prompt text </param>
///
/// <returns> user input as secure string </returns>
///
/// <remarks> </remarks>
///
internal static SecureString PromptForSecureString(PSHostUserInterface hostUI,
string prompt)
{
SecureString ss = null;
hostUI.Write(prompt);
ss = hostUI.ReadLineAsSecureString();
hostUI.WriteLine("");
return ss;
}
#if !CORECLR
/// <summary>
/// get plain text string from a SecureString
///
/// This function will not be required once all of the methods
/// that we call accept a SecureString. The list below has
/// classes/methods that will be changed to accept a SecureString
/// after Whidbey beta1
///
/// -- X509Certificate2.Import (String, String, X509KeyStorageFlags)
/// (DCR #33007 in the DevDiv Schedule db)
///
/// -- NetworkCredential(string, string);
///
/// </summary>
///
/// <param name="ss"> input data </param>
///
/// <returns> a string representing clear-text equivalent of ss </returns>
///
/// <remarks> </remarks>
///
[ArchitectureSensitive]
internal static string GetStringFromSecureString(SecureString ss)
{
IntPtr p = Marshal.SecureStringToGlobalAllocUnicode(ss);
string s = Marshal.PtrToStringUni(p);
Marshal.ZeroFreeGlobalAllocUnicode(p);
return s;
}
#endif
/*
/// <summary>
/// display sec-desc of a file
/// </summary>
///
/// <param name="sd"> file security descriptor </param>
///
/// <returns> Does not return a value </returns>
///
/// <remarks> </remarks>
///
internal static void ShowFileSd(FileSecurity sd)
{
string userName = null;
FileSystemRights rights = 0;
AccessControlType aceType = 0;
rules = sd.GetAccessRules(true, false, typeof(NTAccount));
foreach (FileSystemAccessRule r in rules)
{
userName = r.IdentityReference.ToString();
aceType = r.AccessControlType;
rights = r.FileSystemRights;
Console.WriteLine("{0} : {1} : {2}",
userName,
aceType.ToString(),
rights.ToString());
}
}
}
*/
/// <summary>
///
/// </summary>
///
/// <param name="resourceStr"> resource string </param>
///
/// <param name="errorId"> error identifier </param>
///
/// <param name="args"> replacement params for resource string formatting </param>
///
/// <returns> </returns>
///
/// <remarks> </remarks>
///
internal static
ErrorRecord CreateFileNotFoundErrorRecord(string resourceStr,
string errorId,
params object[] args)
{
string message =
StringUtil.Format(
resourceStr,
args
);
FileNotFoundException e =
new FileNotFoundException(message);
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.ObjectNotFound,
null);
return er;
}
/// <summary>
///
/// </summary>
///
/// <param name="path"> path that was not found </param>
///
/// <param name="errorId"> error identifier </param>
///
/// <returns> ErrorRecord instance </returns>
///
/// <remarks> </remarks>
///
internal static
ErrorRecord CreatePathNotFoundErrorRecord(string path,
string errorId)
{
ItemNotFoundException e =
new ItemNotFoundException(path, "PathNotFound", SessionStateStrings.PathNotFound);
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.ObjectNotFound,
null);
return er;
}
/// <summary>
/// Create an error record for 'operation not supported' condition
/// </summary>
///
/// <param name="resourceStr"> resource string </param>
///
/// <param name="errorId"> error identifier </param>
///
/// <param name="args"> replacement params for resource string formatting </param>
///
/// <returns> </returns>
///
/// <remarks> </remarks>
///
internal static
ErrorRecord CreateNotSupportedErrorRecord(string resourceStr,
string errorId,
params object[] args)
{
string message = StringUtil.Format(resourceStr, args);
NotSupportedException e =
new NotSupportedException(message);
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.NotImplemented,
null);
return er;
}
/// <summary>
/// Create an error record for 'operation not supported' condition
/// </summary>
///
/// <param name="e"> exception to include in ErrorRecord </param>
///
/// <param name="errorId"> error identifier </param>
///
/// <returns> </returns>
///
/// <remarks> </remarks>
///
internal static
ErrorRecord CreateInvalidArgumentErrorRecord(Exception e,
string errorId)
{
ErrorRecord er =
new ErrorRecord(e,
errorId,
ErrorCategory.InvalidArgument,
null);
return er;
}
/// <summary>
/// convert the specifed provider path to a provider path
/// and make sure that all of the following is true:
/// -- it represents a FileSystem path
/// -- it points to a file
/// -- the file exists
/// </summary>
///
/// <param name="cmdlet"> cmdlet instance </param>
///
/// <param name="path"> provider path </param>
///
/// <returns>
/// filesystem path if all conditions are true,
/// null otherwise
/// </returns>
///
/// <remarks> </remarks>
///
internal static string GetFilePathOfExistingFile(PSCmdlet cmdlet,
string path)
{
string resolvedProviderPath = cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path);
if (Utils.NativeFileExists(resolvedProviderPath))
{
return resolvedProviderPath;
}
else
{
return null;
}
}
}
}