-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBadLayeredFileSystem.cs
More file actions
333 lines (300 loc) · 11.7 KB
/
BadLayeredFileSystem.cs
File metadata and controls
333 lines (300 loc) · 11.7 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BadScript2.IO;
using BadScript2.IO.Virtual;
/// <summary>
/// Contains a layered file system/containerization implementation.
/// </summary>
namespace BadScript2.Container
{
/// <summary>
/// Implements a layered file system.
/// </summary>
public class BadLayeredFileSystem : IVirtualFileSystem
{
/// <summary>
/// The layers of the file system.
/// </summary>
private readonly BadLayeredFileSystemLayer[] m_Layers;
/// <summary>
/// All layers of the file system.
/// </summary>
public IReadOnlyList<IBadLayeredFileSystemLayer> Layers => m_Layers;
/// <summary>
/// Returns information about the writable layers states.
/// </summary>
/// <returns>Information about the writable layers states.</returns>
public BadLayeredFileSystemStackInfo GetInfo()
{
List<BadLayeredFileSystemInfo> fileSystems = new List<BadLayeredFileSystemInfo>();
Dictionary<string,BadLayeredFileSystemFileInfo> files = new Dictionary<string,BadLayeredFileSystemFileInfo>();
foreach (var layer in m_Layers)
{
var fs = layer.FileSystem;
fileSystems.Add(new BadLayeredFileSystemInfo() { Writable = fs == GetWritable(), Name = layer.Name, MetaData = layer.MetaData });
foreach (var file in fs.GetFiles("/", "", true))
{
if (!files.TryGetValue(file, out var info))
{
info = new BadLayeredFileSystemFileInfo()
{
Path = file,
PresentIn = new List<string>()
};
files[file] = info;
}
info.PresentIn.Add(layer.Name);
}
}
return new BadLayeredFileSystemStackInfo() { FileSystems = fileSystems.ToArray(), Files = files.Values.ToArray() };
}
/// <summary>
/// Creates a new layered file system.
/// </summary>
/// <param name="layers">The layers of the file system.</param>
public BadLayeredFileSystem(params BadLayeredFileSystemLayer[] layers)
{
m_Layers = layers;
}
/// <summary>
/// Returns true if the content of the two streams is equal.
/// </summary>
/// <param name="s1">stream 1</param>
/// <param name="s2">stream 2</param>
/// <returns>true if the content of the two streams is equal.</returns>
private bool ContentEquals(Stream s1, Stream s2)
{
if (s1.Length != s2.Length) return false;
int b1, b2;
do
{
b1 = s1.ReadByte();
b2 = s2.ReadByte();
if (b1 != b2) return false;
} while (b1 != -1);
return true;
}
/// <summary>
/// Optimizes the file system by removing files that are present in the writable layer and have the same content as files in the read-only layers.
/// </summary>
/// <param name="onlyReport">if true, no changes are made</param>
/// <returns>List of files that were deleted due to optimization</returns>
public List<string> Optimize(bool onlyReport = false)
{
//Ensure that the writable layer does not have a file that already exists in a read-only layer and has the same content
var writable = GetWritable();
var deletedFiles = new List<string>();
foreach (var file in writable.GetFiles("/", "", true).ToArray())
{
foreach (var layer in m_Layers.SkipLast(1))
{
if (layer.FileSystem.Exists(file) && layer.FileSystem.IsFile(file))
{
bool equals;
using (var wStream = writable.OpenRead(file))
{
using var rStream = layer.FileSystem.OpenRead(file);
equals = ContentEquals(wStream, rStream);
}
if (equals)
{
deletedFiles.Add(file);
if (!onlyReport)
{
writable.DeleteFile(file);
}
break;
}
}
}
}
return deletedFiles;
}
/// <summary>
/// Removes a file or directory from the writable layer.
/// </summary>
/// <param name="path">The path to the file or directory to remove.</param>
/// <returns>true if the file or directory was removed, false otherwise.</returns>
public bool Restore(string path)
{
var writable = GetWritable();
if (writable.Exists(path))
{
if (writable.IsFile(path))
{
writable.DeleteFile(path);
}
else
{
writable.DeleteDirectory(path, true);
}
return true;
}
return false;
}
/// <inheritdoc />
public string GetStartupDirectory()
{
return GetWritable().GetStartupDirectory();
}
/// <inheritdoc />
public bool Exists(string path)
{
return m_Layers.Any(x => x.FileSystem.Exists(path));
}
/// <inheritdoc />
public bool IsFile(string path)
{
return m_Layers.Any(x => x.FileSystem.IsFile(path));
}
/// <inheritdoc />
public bool IsDirectory(string path)
{
return m_Layers.Any(x => x.FileSystem.IsDirectory(path));
}
/// <inheritdoc />
public IEnumerable<string> GetFiles(string path, string extension, bool recursive)
{
return m_Layers.SelectMany(x =>
x.FileSystem.Exists(path) &&
x.FileSystem.IsDirectory(path) ?
x.FileSystem.GetFiles(path, extension, recursive) :
Enumerable.Empty<string>()).Distinct();
}
/// <inheritdoc />
public IEnumerable<string> GetDirectories(string path, bool recursive)
{
return m_Layers.SelectMany(x =>
x.FileSystem.Exists(path) &&
x.FileSystem.IsDirectory(path) ?
x.FileSystem.GetDirectories(path, recursive) :
Enumerable.Empty<string>()).Distinct();
}
/// <inheritdoc />
public void CreateDirectory(string path, bool recursive = false)
{
GetWritable().CreateDirectory(path, recursive);
}
/// <inheritdoc />
public void DeleteDirectory(string path, bool recursive)
{
if (!GetWritable().IsDirectory(path)) throw new Exception("Is Readonly");
GetWritable().DeleteDirectory(path, recursive);
}
/// <inheritdoc />
public void DeleteFile(string path)
{
if (!GetWritable().IsFile(path)) throw new Exception("Is Readonly");
GetWritable().DeleteFile(path);
}
/// <inheritdoc />
public string GetFullPath(string path)
{
return GetWritable().GetFullPath(path);
}
/// <inheritdoc />
public Stream OpenRead(string path)
{
var fs = m_Layers.LastOrDefault(x => x.FileSystem.IsFile(path))?.FileSystem ?? GetWritable();
return fs.OpenRead(path);
}
/// <inheritdoc />
public Stream OpenWrite(string path, BadWriteMode mode)
{
var writable = GetWritable();
var dir = Path.GetDirectoryName(path);
if (dir != null && IsDirectory(dir) && !writable.IsDirectory(dir))
{
//Create the directory if it does not exist(it exists in some other layer, we need to create it in the writable layer)
writable.CreateDirectory(dir, true);
}
//In order to properly work with Append mode, we need to copy the file to the writable file system if it does not exist
if (mode == BadWriteMode.Append && !writable.IsFile(path))
{
using var src = OpenRead(path);
using var dst = writable.OpenWrite(path, BadWriteMode.CreateNew);
src.CopyTo(dst);
}
return writable.OpenWrite(path, mode);
}
/// <inheritdoc />
public string GetCurrentDirectory()
{
return GetWritable().GetCurrentDirectory();
}
/// <inheritdoc />
public void SetCurrentDirectory(string path)
{
foreach (var fs in m_Layers) fs.FileSystem.SetCurrentDirectory(path);
}
/// <inheritdoc />
public void Copy(string src, string dst, bool overwrite = true)
{
if (IsDirectory(src))
{
if (IsSubfolderOf(src, dst)) throw new IOException("Cannot copy a directory to a subfolder of itself.");
if (!overwrite && IsDirectory(src)) throw new IOException("Directory already exists.");
CopyDirectoryToDirectory(src, dst);
}
else if (IsFile(src))
{
if (!overwrite && IsFile(src)) throw new IOException("File already exists.");
CopyFileToFile(src, dst);
}
else
{
throw new IOException("Source path is not a file or directory");
}
}
/// <inheritdoc />
public void Move(string src, string dst, bool overwrite = true)
{
Copy(src, dst, overwrite);
if (IsDirectory(src))
DeleteDirectory(src, true);
else
DeleteFile(src);
}
/// <summary>
/// Returns the writable layer of the file system.
/// </summary>
/// <returns>The writable layer of the file system.</returns>
public BadVirtualFileSystem GetWritable()
{
return m_Layers.Last().FileSystem;
}
/// <summary>
/// Returns true if the sub path is a subfolder of the root path.
/// </summary>
/// <param name="root">The root path.</param>
/// <param name="sub">The sub path.</param>
/// <returns></returns>
private bool IsSubfolderOf(string root, string sub)
{
return GetFullPath(sub).StartsWith(GetFullPath(root));
}
/// <summary>
/// Copies a directory to another directory.
/// </summary>
/// <param name="src">Source directory</param>
/// <param name="dst">Destination directory</param>
private void CopyDirectoryToDirectory(string src, string dst)
{
foreach (var directory in GetDirectories(src, true)) CreateDirectory(directory);
foreach (var file in GetFiles(src, "*", true)) CopyFileToFile(file, file.Replace(src, dst));
}
/// <summary>
/// Copies a file to another file.
/// </summary>
/// <param name="src">Source file</param>
/// <param name="dst">Destination file</param>
private void CopyFileToFile(string src, string dst)
{
using var s = OpenRead(src);
using var d = OpenWrite(dst, BadWriteMode.CreateNew);
s.CopyTo(d);
}
}
}