2using System.Collections.Generic;
17 List<BadLayeredFileSystemInfo> fileSystems =
new List<BadLayeredFileSystemInfo>();
18 Dictionary<string,BadLayeredFileSystemFileInfo> files =
new Dictionary<string,BadLayeredFileSystemFileInfo>();
21 var fs = layer.FileSystem;
23 foreach (var file
in fs.GetFiles(
"/",
"",
true))
25 if (!files.TryGetValue(file, out var info))
30 PresentIn =
new List<string>()
48 if (s1.Length != s2.Length)
return false;
54 if (b1 != b2)
return false;
62 foreach (var file
in writable.GetFiles(
"/",
"",
true).ToArray())
64 foreach (var layer
in m_Layers.SkipLast(1))
66 if (layer.FileSystem.Exists(file) && layer.FileSystem.IsFile(file))
69 using (var wStream = writable.OpenRead(file))
71 using var rStream = layer.FileSystem.OpenRead(file);
76 writable.DeleteFile(file);
86 if (writable.Exists(path))
88 if (writable.IsFile(path))
90 writable.DeleteFile(path);
94 writable.DeleteDirectory(path,
true);
107 return m_Layers.Any(x => x.FileSystem.Exists(path));
112 return m_Layers.Any(x => x.FileSystem.IsFile(path));
117 return m_Layers.Any(x => x.FileSystem.IsDirectory(path));
120 public IEnumerable<string>
GetFiles(
string path,
string extension,
bool recursive)
123 x.FileSystem.Exists(path) &&
124 x.FileSystem.IsDirectory(path) ?
125 x.FileSystem.GetFiles(path, extension, recursive) :
126 Enumerable.Empty<
string>()).Distinct();
132 x.FileSystem.Exists(path) &&
133 x.FileSystem.IsDirectory(path) ?
134 x.FileSystem.GetDirectories(path, recursive) :
135 Enumerable.Empty<
string>()).Distinct();
163 return fs.OpenRead(path);
169 var dir = Path.GetDirectoryName(path);
170 if (dir !=
null &&
IsDirectory(dir) && !writable.IsDirectory(dir))
173 writable.CreateDirectory(dir,
true);
176 if (mode ==
BadWriteMode.Append && !writable.IsFile(path))
179 using var dst = writable.OpenWrite(path,
BadWriteMode.CreateNew);
182 return writable.OpenWrite(path, mode);
192 foreach (var fs
in m_Layers) fs.FileSystem.SetCurrentDirectory(path);
195 public void Copy(
string src,
string dst,
bool overwrite =
true)
199 if (
IsSubfolderOf(src, dst))
throw new IOException(
"Cannot copy a directory to a subfolder of itself.");
201 if (!overwrite &&
IsDirectory(src))
throw new IOException(
"Directory already exists.");
207 if (!overwrite &&
IsFile(src))
throw new IOException(
"File already exists.");
213 throw new IOException(
"Source path is not a file or directory");
217 public void Move(
string src,
string dst,
bool overwrite =
true)
219 Copy(src, dst, overwrite);
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.
void DeleteDirectory(string path, bool recursive)
Deletes a directory.
string GetFullPath(string path)
Returns the full path of the given path.
BadLayeredFileSystem(params BadLayeredFileSystemLayer[] layers)
void Copy(string src, string dst, bool overwrite=true)
Copies a file or directory to a new location.
void CopyFileToFile(string src, string dst)
readonly BadLayeredFileSystemLayer[] m_Layers
void CopyDirectoryToDirectory(string src, string dst)
bool IsFile(string path)
Returns true if the given path is a file.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
bool Exists(string path)
Returns true if the given path is a file or directory.
bool IsSubfolderOf(string root, string sub)
BadLayeredFileSystemStackInfo GetInfo()
void Move(string src, string dst, bool overwrite=true)
Moves a file or directory to a new location.
BadVirtualFileSystem GetWritable()
bool Restore(string path)
string GetStartupDirectory()
The Startup Directory of the Application.
bool IsDirectory(string path)
Returns true if the given path is a directory.
Stream OpenRead(string path)
Opens a file for reading.
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.
void SetCurrentDirectory(string path)
Sets the current Directory.
bool ContentEquals(Stream s1, Stream s2)
IEnumerable< string > GetDirectories(string path, bool recursive)
Returns all directories in the given directory.
void DeleteFile(string path)
Deletes a file.
string GetCurrentDirectory()
Returns the Current Directory.
BadVirtualFileSystem FileSystem
Virtual File System Implementation for the BadScript Engine.
string GetCurrentDirectory()
Returns the Current Directory.
string GetStartupDirectory()
The Startup Directory of the Application.
void DeleteDirectory(string path, bool recursive)
Deletes a directory.
string GetFullPath(string path)
Returns the full path of the given path.
void DeleteFile(string path)
Deletes a file.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
Contains the Implementation of the BadScript Virtual File System.
Contains IO Implementation for the BadScript2 Runtime.
BadWriteMode
The Write Modes of the File System Abstraction.