-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicalFileSystem.cs
More file actions
72 lines (60 loc) · 2.09 KB
/
PhysicalFileSystem.cs
File metadata and controls
72 lines (60 loc) · 2.09 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
using System.Diagnostics;
namespace Ramstack.FileSystem.Physical;
/// <summary>
/// Represents an implementation of <see cref="IVirtualFileSystem"/> for physical files.
/// </summary>
[DebuggerDisplay("Root = {Root,nq}")]
public sealed class PhysicalFileSystem : IVirtualFileSystem
{
/// <summary>
/// Gets the physical path of the root directory for this instance.
/// </summary>
public string Root { get; }
/// <summary>
/// Gets or sets a value indicating whether the file system is read-only.
/// </summary>
public bool IsReadOnly { get; init; }
/// <summary>
/// Initializes a new instance of the <see cref="PhysicalFileSystem"/> class.
/// </summary>
/// <param name="path">The physical path of root the directory.</param>
public PhysicalFileSystem(string path)
{
if (!Path.IsPathRooted(path))
Error(path);
Root = Path.GetFullPath(path);
static void Error(string path) =>
throw new ArgumentException($"The path '{path}' must be absolute.");
}
/// <inheritdoc />
public VirtualFile GetFile(string path)
{
path = VirtualPath.Normalize(path);
var physicalPath = GetPhysicalPath(path);
return new PhysicalFile(this, path, physicalPath);
}
/// <inheritdoc />
public VirtualDirectory GetDirectory(string path)
{
path = VirtualPath.Normalize(path);
var physicalPath = GetPhysicalPath(path);
return new PhysicalDirectory(this, path, physicalPath);
}
/// <inheritdoc />
void IDisposable.Dispose()
{
}
/// <summary>
/// Converts the specified virtual path within this file system
/// to its corresponding physical path.
/// </summary>
/// <param name="path">The virtual path within this file system.</param>
/// <returns>
/// The corresponding physical path.
/// </returns>
private string GetPhysicalPath(string path)
{
Debug.Assert(VirtualPath.IsNormalized(path));
return Path.Join(Root, path);
}
}