/
DirectoryInfoWrapper.cs
125 lines (112 loc) · 4.48 KB
/
DirectoryInfoWrapper.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
namespace Microsoft.Extensions.FileSystemGlobbing.Abstractions
{
/// <summary>
/// Wraps an instance of <see cref="System.IO.DirectoryInfo" /> and provides implementation of
/// <see cref="DirectoryInfoBase" />.
/// </summary>
public class DirectoryInfoWrapper : DirectoryInfoBase
{
private readonly DirectoryInfo _directoryInfo;
private readonly bool _isParentPath;
/// <summary>
/// Initializes an instance of <see cref="DirectoryInfoWrapper" />.
/// </summary>
/// <param name="directoryInfo">The <see cref="DirectoryInfo" />.</param>
public DirectoryInfoWrapper(DirectoryInfo directoryInfo)
: this(directoryInfo, isParentPath: false)
{ }
private DirectoryInfoWrapper(DirectoryInfo directoryInfo, bool isParentPath)
{
_directoryInfo = directoryInfo;
_isParentPath = isParentPath;
}
/// <inheritdoc />
public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
{
if (_directoryInfo.Exists)
{
IEnumerable<FileSystemInfo> fileSystemInfos;
try
{
fileSystemInfos = _directoryInfo.EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly);
}
catch (DirectoryNotFoundException)
{
yield break;
}
foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
{
if (fileSystemInfo is DirectoryInfo directoryInfo)
{
yield return new DirectoryInfoWrapper(directoryInfo);
}
else
{
yield return new FileInfoWrapper((FileInfo)fileSystemInfo);
}
}
}
}
/// <summary>
/// Returns an instance of <see cref="DirectoryInfoBase" /> that represents a subdirectory.
/// </summary>
/// <remarks>
/// If <paramref name="name" /> equals '..', this returns the parent directory.
/// </remarks>
/// <param name="name">The directory name</param>
/// <returns>The directory</returns>
public override DirectoryInfoBase? GetDirectory(string name)
{
bool isParentPath = string.Equals(name, "..", StringComparison.Ordinal);
if (isParentPath)
{
return new DirectoryInfoWrapper(
new DirectoryInfo(Path.Combine(_directoryInfo.FullName, name)),
isParentPath);
}
else
{
DirectoryInfo[] dirs = _directoryInfo.GetDirectories(name);
if (dirs.Length == 1)
{
return new DirectoryInfoWrapper(dirs[0], isParentPath);
}
else if (dirs.Length == 0)
{
return null;
}
else
{
// This shouldn't happen. The parameter name isn't supposed to contain wild card.
throw new InvalidOperationException(
$"More than one sub directories are found under {_directoryInfo.FullName} with name {name}.");
}
}
}
/// <inheritdoc />
public override FileInfoBase GetFile(string name)
=> new FileInfoWrapper(new FileInfo(Path.Combine(_directoryInfo.FullName, name)));
/// <inheritdoc />
public override string Name => _isParentPath ? ".." : _directoryInfo.Name;
/// <summary>
/// Returns the full path to the directory.
/// </summary>
/// <remarks>
/// Equals the value of <seealso cref="System.IO.FileSystemInfo.FullName" />.
/// </remarks>
public override string FullName => _directoryInfo.FullName;
/// <summary>
/// Returns the parent directory.
/// </summary>
/// <remarks>
/// Equals the value of <seealso cref="System.IO.DirectoryInfo.Parent" />.
/// </remarks>
public override DirectoryInfoBase? ParentDirectory
=> new DirectoryInfoWrapper(_directoryInfo.Parent!);
}
}