-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFile.cs
More file actions
267 lines (242 loc) · 13.9 KB
/
VirtualFile.cs
File metadata and controls
267 lines (242 loc) · 13.9 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
namespace Ramstack.FileSystem;
/// <summary>
/// Represents a virtual file within a specified file system.
/// </summary>
[DebuggerDisplay("{FullName,nq}")]
public abstract class VirtualFile : VirtualNode
{
/// <summary>
/// Gets the full path of the directory containing this file.
/// </summary>
public string DirectoryName => VirtualPath.GetDirectoryName(FullName);
/// <summary>
/// Gets a <see cref="VirtualDirectory"/> instance representing the parent directory of this file.
/// </summary>
public VirtualDirectory Directory => FileSystem.GetDirectory(DirectoryName);
/// <summary>
/// Initializes a new instance of the <see cref="VirtualFile"/> class with the specified path.
/// </summary>
/// <param name="path">The full path of the file.</param>
protected VirtualFile(string path) : base(path)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VirtualFile"/> class with the specified path and properties.
/// </summary>
/// <param name="path">The full path of the file.</param>
/// <param name="properties">The properties of the file.</param>
protected VirtualFile(string path, VirtualNodeProperties? properties) : base(path, properties)
{
}
/// <summary>
/// Asynchronously opens the file for reading.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// with a result of type <see cref="Stream"/> for reading the file content.
/// </returns>
public ValueTask<Stream> OpenReadAsync(CancellationToken cancellationToken = default) =>
OpenReadCoreAsync(cancellationToken);
/// <summary>
/// Asynchronously opens the file for writing.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// with a result of type <see cref="Stream"/> for writing to the file.
/// </returns>
public ValueTask<Stream> OpenWriteAsync(CancellationToken cancellationToken = default)
{
EnsureWritable();
Refresh();
return OpenWriteCoreAsync(cancellationToken);
}
/// <summary>
/// Asynchronously writes the specified content to the file, creating a new file or overwriting an existing one.
/// </summary>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file; <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
public ValueTask WriteAsync(Stream stream, bool overwrite, CancellationToken cancellationToken = default)
{
EnsureWritable();
Refresh();
return WriteCoreAsync(stream, overwrite, cancellationToken);
}
/// <summary>
/// Asynchronously deletes the current file.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public ValueTask DeleteAsync(CancellationToken cancellationToken = default)
{
EnsureWritable();
Refresh();
return DeleteCoreAsync(cancellationToken);
}
/// <summary>
/// Asynchronously copies the file to the specified destination path.
/// </summary>
/// <param name="destinationPath">The path of the destination file. This cannot be a directory.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file; <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
public ValueTask CopyToAsync(string destinationPath, bool overwrite, CancellationToken cancellationToken = default)
{
EnsureWritable();
destinationPath = VirtualPath.Normalize(destinationPath);
EnsureDistinctTargets(FullName, destinationPath);
return CopyToCoreAsync(destinationPath, overwrite, cancellationToken);
}
/// <summary>
/// Asynchronously copies the contents of the current <see cref="VirtualFile"/> to the specified destination <see cref="VirtualFile"/>.
/// </summary>
/// <param name="destination">The destination <see cref="VirtualFile"/> where the contents will be copied to.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file; <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> that represents the asynchronous copy operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
public ValueTask CopyToAsync(VirtualFile destination, bool overwrite, CancellationToken cancellationToken = default)
{
destination.EnsureWritable();
destination.Refresh();
if (destination.FileSystem != FileSystem)
return CopyToCoreAsync(destination, overwrite, cancellationToken);
EnsureDistinctTargets(FullName, destination.FullName);
return CopyToCoreAsync(destination.FullName, overwrite, cancellationToken);
}
/// <summary>
/// Core implementation for asynchronously opening the file for reading.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// with a result of type <see cref="Stream"/> for reading the file content.
/// </returns>
protected abstract ValueTask<Stream> OpenReadCoreAsync(CancellationToken cancellationToken);
/// <summary>
/// Core implementation for asynchronously opening the file for writing.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// with a result of type <see cref="Stream"/> for writing to the file.
/// </returns>
protected abstract ValueTask<Stream> OpenWriteCoreAsync(CancellationToken cancellationToken);
/// <summary>
/// Core implementation for asynchronously writing the specified content to the file,
/// creating a new file or overwriting an existing one.
/// </summary>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file; <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it should be created.</description></item>
/// <item><description>If the file exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file should be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception should be thrown.</description></item>
/// </list>
/// </remarks>
protected abstract ValueTask WriteCoreAsync(Stream stream, bool overwrite, CancellationToken cancellationToken);
/// <summary>
/// Core implementation for asynchronously deleting the current file.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
protected abstract ValueTask DeleteCoreAsync(CancellationToken cancellationToken);
/// <summary>
/// Core implementation for asynchronously copying the file to the specified destination path.
/// </summary>
/// <param name="destinationPath">The path of the destination file.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file; <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
protected virtual async ValueTask CopyToCoreAsync(string destinationPath, bool overwrite, CancellationToken cancellationToken)
{
await using var source = await OpenReadAsync(cancellationToken).ConfigureAwait(false);
await FileSystem.WriteAsync(destinationPath, source, overwrite, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Core implementation for asynchronously copying the contents of the current <see cref="VirtualFile"/> to the specified destination <see cref="VirtualFile"/>.
/// </summary>
/// <param name="destination">The destination <see cref="VirtualFile"/> where the contents will be copied to.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file; <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> that represents the asynchronous copy operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
protected virtual async ValueTask CopyToCoreAsync(VirtualFile destination, bool overwrite, CancellationToken cancellationToken)
{
await using var stream = await OpenReadAsync(cancellationToken).ConfigureAwait(false);
await destination.WriteAsync(stream, overwrite, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Ensures that the source and destination paths are distinct.
/// </summary>
/// <param name="path">The source file path.</param>
/// <param name="destinationPath">The destination file path.</param>
/// <remarks>
/// Distinct target validation, if the underlying file system is case-insensitive,
/// should be handled by the appropriate provider.
/// </remarks>
private static void EnsureDistinctTargets(string path, string destinationPath)
{
// Distinct target validation, if the underlying file system is case-insensitive,
// should be handled by the appropriate provider.
if (path == destinationPath)
Error(path);
static void Error(string path) =>
throw new IOException($"Cannot copy a file '{path}' to itself.");
}
}