This repository has been archived by the owner on Feb 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathFileSystemProvider.cs
570 lines (500 loc) · 19.4 KB
/
FileSystemProvider.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
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
// Copyright (C) Pash Contributors. License: GPL/BSD. See https://github.com/Pash-Project/Pash/
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Provider;
using System.Security;
using System.Security.AccessControl;
using System.Management;
using System.Management.Pash.Implementation;
using System.Text;
namespace Microsoft.PowerShell.Commands
{
[CmdletProvider("FileSystem", ProviderCapabilities.Filter | ProviderCapabilities.ShouldProcess)]
public sealed class FileSystemProvider :
NavigationCmdletProvider,
IContentCmdletProvider,
IPropertyCmdletProvider,
ISecurityDescriptorCmdletProvider
{
internal const string FallbackDriveName = "File";
private enum ItemType {
Unknown,
Directory,
File
};
public const string ProviderName = "FileSystem";
public FileSystemProvider()
{
}
protected override string NormalizeRelativePath(string path, string basePath)
{
var normPath = new Path(path).NormalizeSlashes();
var normBase = new Path(basePath).NormalizeSlashes();
if (!normPath.StartsWith(normBase))
{
var ex = new PSArgumentException("Path is outside of base path!", "PathOutsideBasePath",
ErrorCategory.InvalidArgument);
WriteError(ex.ErrorRecord);
return null;
}
return new Path(path.Substring(basePath.Length)).TrimStartSlash().ToString();
}
protected override void CopyItem(string path, string destinationPath, bool recurse)
{
throw new NotImplementedException();
}
protected override string MakePath(string parent, string child)
{
return new Path(parent).Combine(child).ToString();
}
protected override void GetChildItems(string path, bool recurse)
{
if (string.IsNullOrEmpty(path))
{
throw new Exception("Path can't be empty");
}
path = NormalizePath(path);
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(path);
if (directory.Exists)
{
GetDirectoryContent(directory, recurse);
}
else
{
System.IO.FileInfo item = new System.IO.FileInfo(path);
if (item.Exists)
{
if ((item.Attributes & System.IO.FileAttributes.Hidden) == 0)
{
WriteItemObject(item, path, false);
}
}
else
{
Exception exception = new System.IO.IOException("Path doesn't exist: " + path);
WriteError(new ErrorRecord(exception, "ItemDoesNotExist", ErrorCategory.ObjectNotFound, path));
}
}
}
private void GetDirectoryContent(System.IO.DirectoryInfo directory, bool recurse)
{
System.IO.DirectoryInfo[] directories = null;
var list = new List<System.IO.FileSystemInfo>();
if (recurse)
{
directories = directory.GetDirectories();
}
// Get all the location-related items
list.AddRange(directory.GetDirectories());
list.AddRange(directory.GetFiles());
foreach (System.IO.FileSystemInfo info in list)
{
if ((info.Attributes & System.IO.FileAttributes.Hidden) == 0)
{
if (info is System.IO.FileInfo)
{
WriteItemObject(info, info.FullName, false);
}
else
{
WriteItemObject(info, info.FullName, true);
}
}
}
if (recurse && (directories != null))
{
foreach (var dir in directories)
{
if ((dir.Attributes & System.IO.FileAttributes.Hidden) == 0)
{
GetDirectoryContent(dir, recurse);
}
}
}
}
protected override string GetChildName(string path)
{
path = NormalizePath(path);
if (string.IsNullOrEmpty(path))
{
throw new NullReferenceException("Path can't be null");
}
return new Path(path).GetChildNameOrSelfIfNoChild();
//
// path = PathIntrinsics.NormalizePath(path);
// path = path.TrimEnd('\\');
//
// int num = path.LastIndexOf('\\');
// if (num == -1)
// {
//TODO: what is this MakeSlashedPath and should the above GetChildNameOrSelfIfNoChild do somthing like that?
// return MakeSlashedPath(path);
// }
// return path.Substring(num + 1);
}
protected override void GetChildNames(string path, ReturnContainers returnContainers)
{
if (path == String.Empty)
{
path = ".";
}
var names = GetChildNamesPrivate(path);
foreach (var name in names)
{
ProviderRuntime.WriteObject(name);
}
}
protected override void GetItem(string path)
{
bool isContainer = false;
var fileSystemInfo = GetFileSystemInfo(path, ref isContainer, false);
WriteItemObject(fileSystemInfo, fileSystemInfo.FullName, isContainer);
}
protected override string GetParentPath(string path, string root)
{
Path parentPath = new Path(path).GetParentPath(root);
// TODO: deal with UNC
if (!path.StartsWith("\\\\")) // UNC?
{
parentPath = MakeSlashedPath(parentPath);
}
return parentPath;
}
private string MakeSlashedPath(string path)
{
// Make sure that the path is ended bith '\'
int index = path.IndexOf(':');
if ((index != -1) && ((index + 1) == path.Length))
{
path += '\\';
}
return path;
}
protected override bool HasChildItems(string path)
{
return GetChildNamesPrivate(path).Count > 0;
}
protected override Collection<PSDriveInfo> InitializeDefaultDrives()
{
/*
* The concept of drives in Powershell is obviously inspired by the
* Windows file system. However, in combination with providers, this concept
* has much more power as it allows access to arbitrary data stores.
* Handling paths is therefore a little bit more complicated, because we don't
* just deal with the FS. As a consequence, we need to make sure that
* drive qualified path can be identified with the colon and that drives have valid
* names.
* On non-Windows machines, mono would return "drives" like '/', '/proc', etc, because
* that's what's closest to Windows-drives. However, we can't adopt these drives,
* because their names are invalid and wouldn't be native on *nix systems with a colon.
* So instead, if we have a platform with mono drives that don't include a colon itself,
* and therefore aren't compatible to the PS/Pash drive concept, we will use a single
* default drive called "File" (see FallbackDriveName) instead to uniquely access the
* root of the file system.
* This drive get's a hidden flag, so it will be ignored in various places when Pash
* would for example display the path. Doing this should result in a feeling that is native
* for the platform.
*/
var fsDrives = System.IO.DriveInfo.GetDrives();
var drives = (from fd in fsDrives
where fd.Name.Contains(":")
select NewDrive(fd)).ToList();
if (drives.Count == 0)
{
var root = System.IO.Path.GetPathRoot(Environment.CurrentDirectory);
var defaultDrive = new PSDriveInfo(FallbackDriveName, ProviderInfo, root, "Root", null);
defaultDrive.Hidden = true;
drives.Add(defaultDrive);
}
return new Collection<PSDriveInfo>(drives);
}
private PSDriveInfo NewDrive(System.IO.DriveInfo fsDrive)
{
Path name = fsDrive.Name;
var iColon = fsDrive.Name.IndexOf(":");
if (iColon > 0)
name = fsDrive.Name.Substring(0, iColon);
Path description = string.Empty;
Path root = fsDrive.Name;
if (fsDrive.DriveType == System.IO.DriveType.Fixed)
{
try
{
description = fsDrive.VolumeLabel;
root = fsDrive.RootDirectory.FullName;
}
catch
{
}
}
PSDriveInfo info = new PSDriveInfo(name, base.ProviderInfo, root, description, null);
info.RemovableDrive = fsDrive.DriveType != System.IO.DriveType.Fixed;
return info;
}
protected override void InvokeDefaultAction(string path) { throw new NotImplementedException(); }
protected override void NewItem(string path, string itemTypeName, object newItemValue)
{
path = NormalizePath(path);
var type = GetItemType(itemTypeName);
if (type.Equals(ItemType.Unknown))
{
throw new PSInvalidOperationException("Cannot create an item of unknown type");
}
System.IO.FileMode mode = System.IO.FileMode.CreateNew;
if (Force.IsPresent)
{
mode = System.IO.FileMode.Create;
CreateIntermediateDirectories(path);
}
if (type.Equals(ItemType.Directory))
{
var dirinfo = new System.IO.DirectoryInfo(path.ToString());
dirinfo.Create();
WriteItemObject(dirinfo, path, true);
return;
}
// else ItemType is File
if (!ShouldProcess(path))
{
return;
}
try
{
using (var stream = new System.IO.FileStream(path, mode, System.IO.FileAccess.Write, System.IO.FileShare.None))
{
if (newItemValue != null)
{
var writer = new System.IO.StreamWriter(stream);
writer.Write(newItemValue.ToString());
writer.Flush();
writer.Close();
}
}
WriteItemObject(new System.IO.FileInfo(path), path, false);
}
catch (System.IO.IOException ex)
{
WriteError(new ErrorRecord(ex, "NewItem", ErrorCategory.WriteError, path));
}
}
protected override bool IsItemContainer(string path)
{
path = path == "" ? "." : path; // Workaround until our globber handles relative paths
path = NormalizePath(path);
return (new System.IO.DirectoryInfo(path)).Exists;
}
protected override bool IsValidPath(string path) { throw new NotImplementedException(); }
protected override bool ItemExists(string path)
{
path = path == "" ? "." : path; // Workaround until our globber handles relative paths
path = NormalizePath(path);
try
{
var info = new System.IO.FileInfo(path);
if (info.Exists)
{
return true;
}
else
{
return (new System.IO.DirectoryInfo(path)).Exists;
}
}
catch
{
}
return false;
}
protected override void MoveItem(string path, string destination) { throw new NotImplementedException(); }
protected override PSDriveInfo NewDrive(PSDriveInfo drive)
{
try
{
var driveInfo = new System.IO.DriveInfo(System.IO.Path.GetPathRoot(drive.Root));
if (driveInfo.DriveType == System.IO.DriveType.Fixed)
{
return (ItemExists(drive.Root) && IsItemContainer(drive.Root)) ? drive : null;
}
return drive;
}
catch
{
}
return null;
}
protected override ProviderInfo Start(ProviderInfo providerInfo)
{
if (providerInfo == null || !string.IsNullOrEmpty(providerInfo.Home))
{
return providerInfo;
}
string path = null;
Path homeDrive = Environment.GetEnvironmentVariable("HOMEDRIVE");
Path homePath = Environment.GetEnvironmentVariable("HOMEPATH");
if (!string.IsNullOrEmpty(homeDrive) && !string.IsNullOrEmpty(homePath))
{
path = MakePath(homeDrive, homePath);
}
else
{
// FIXME: taken from Path.ResolveTilde(). Make sure to clean all that mess up...
path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
// HACK: Use $Env:HOME until Mono 2.10 dies out.
if (path == "")
{
path = Environment.GetEnvironmentVariable("HOME");
}
}
if (System.IO.Directory.Exists(path))
{
providerInfo.Home = path;
}
return providerInfo;
}
void CreateIntermediateDirectories(string path)
{
var dirinfo = new System.IO.DirectoryInfo(path).Parent;
var createStack = new Stack<System.IO.DirectoryInfo>();
while (dirinfo != null && !dirinfo.Exists)
{
createStack.Push(dirinfo);
dirinfo = dirinfo.Parent;
}
while (createStack.Count > 0)
{
dirinfo = createStack.Pop();
dirinfo.Create();
}
}
#region IContentCmdletProvider Members
public void ClearContent(string path)
{
path = NormalizePath(path);
if (!ItemExists(path))
{
throw new ItemNotFoundException(string.Format("Cannot find path '{0}' because it does not exist.", path));
}
using (var writer = new System.IO.StreamWriter(path, false, Encoding.ASCII))
{
}
}
public object ClearContentDynamicParameters(string path)
{
return null;
}
public IContentReader GetContentReader(string path)
{
path = NormalizePath(path);
return new FileContentReader(path);
}
public object GetContentReaderDynamicParameters(string path)
{
return new FileSystemContentReaderDynamicParameters();
}
public IContentWriter GetContentWriter(string path)
{
path = NormalizePath(path);
return new FileContentWriter(path);
}
public object GetContentWriterDynamicParameters(string path)
{
return new FileSystemContentWriterDynamicParameters();
}
#endregion
#region IPropertyCmdletProvider Members
public void ClearProperty(string path, Collection<string> propertyToClear)
{
throw new NotImplementedException();
}
public object ClearPropertyDynamicParameters(string path, Collection<string> propertyToClear)
{
throw new NotImplementedException();
}
public void GetProperty(string path, Collection<string> providerSpecificPickList)
{
throw new NotImplementedException();
}
public object GetPropertyDynamicParameters(string path, Collection<string> providerSpecificPickList)
{
throw new NotImplementedException();
}
public void SetProperty(string path, PSObject propertyValue)
{
throw new NotImplementedException();
}
public object SetPropertyDynamicParameters(string path, PSObject propertyValue)
{
throw new NotImplementedException();
}
#endregion
#region ISecurityDescriptorCmdletProvider Members
public void GetSecurityDescriptor(string path, AccessControlSections includeSections)
{
throw new NotImplementedException();
}
public ObjectSecurity NewSecurityDescriptorFromPath(string path, AccessControlSections includeSections)
{
throw new NotImplementedException();
}
public ObjectSecurity NewSecurityDescriptorOfType(string type, AccessControlSections includeSections)
{
throw new NotImplementedException();
}
public void SetSecurityDescriptor(string path, ObjectSecurity securityDescriptor)
{
throw new NotImplementedException();
}
#endregion
internal string NormalizePath(string path)
{
var p = new Path(path).NormalizeSlashes();
return p.ToString();
}
private List<string> GetChildNamesPrivate(string path)
{
path = path == "" ? "." : path; // Workaround until our globber handles relative paths
path = NormalizePath(path);
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(path);
if (!directory.Exists)
{
return new List<string> { new System.IO.FileInfo(path).Name };
}
return (from fs in directory.GetFileSystemInfos() select fs.Name).ToList();
}
private ItemType GetItemType(string type)
{
var pattern = new WildcardPattern(type + "*", WildcardOptions.IgnoreCase);
if (pattern.IsMatch("directory") || pattern.IsMatch("container"))
{
return ItemType.Directory;
}
else if (pattern.IsMatch("file"))
{
return ItemType.File;
}
return ItemType.Unknown;
}
private System.IO.FileSystemInfo GetFileSystemInfo(string path, ref bool isContainer, bool showHidden)
{
path = NormalizePath(path);
var fi = new System.IO.FileInfo(path);
if (fi.Exists && ((fi.Attributes & System.IO.FileAttributes.Directory) == 0))
{
return fi;
}
else
{
var di = new System.IO.DirectoryInfo(path);
if (di.Exists)
{
isContainer = true;
return di;
}
}
return null;
}
}
}