2using System.Collections.Generic;
18 List<BadLayeredFileSystemInfo> fileSystems =
new List<BadLayeredFileSystemInfo>();
19 Dictionary<string,BadLayeredFileSystemFileInfo> files =
new Dictionary<string,BadLayeredFileSystemFileInfo>();
22 var fs = layer.FileSystem;
24 foreach (var file
in fs.GetFiles(
"/",
"",
true))
26 if (!files.TryGetValue(file, out var info))
31 PresentIn =
new List<string>()
49 if (s1.Length != s2.Length)
return false;
55 if (b1 != b2)
return false;
63 foreach (var file
in writable.GetFiles(
"/",
"",
true).ToArray())
65 foreach (var layer
in m_Layers.SkipLast(1))
67 if (layer.FileSystem.Exists(file) && layer.FileSystem.IsFile(file))
70 using (var wStream = writable.OpenRead(file))
72 using var rStream = layer.FileSystem.OpenRead(file);
77 writable.DeleteFile(file);
87 if (writable.Exists(path))
89 if (writable.IsFile(path))
91 writable.DeleteFile(path);
95 writable.DeleteDirectory(path,
true);
108 return m_Layers.Any(x => x.FileSystem.Exists(path));
113 return m_Layers.Any(x => x.FileSystem.IsFile(path));
118 return m_Layers.Any(x => x.FileSystem.IsDirectory(path));
121 public IEnumerable<string>
GetFiles(
string path,
string extension,
bool recursive)
124 x.FileSystem.Exists(path) &&
125 x.FileSystem.IsDirectory(path) ?
126 x.FileSystem.GetFiles(path, extension, recursive) :
127 Enumerable.Empty<
string>()).Distinct();
133 x.FileSystem.Exists(path) &&
134 x.FileSystem.IsDirectory(path) ?
135 x.FileSystem.GetDirectories(path, recursive) :
136 Enumerable.Empty<
string>()).Distinct();
164 return fs.OpenRead(path);
170 var dir = Path.GetDirectoryName(path);
171 if (dir !=
null &&
IsDirectory(dir) && !writable.IsDirectory(dir))
174 writable.CreateDirectory(dir,
true);
177 if (mode ==
BadWriteMode.Append && !writable.IsFile(path))
180 using var dst = writable.OpenWrite(path,
BadWriteMode.CreateNew);
183 return writable.OpenWrite(path, mode);
193 foreach (var fs
in m_Layers) fs.FileSystem.SetCurrentDirectory(path);
196 public void Copy(
string src,
string dst,
bool overwrite =
true)
200 if (
IsSubfolderOf(src, dst))
throw new IOException(
"Cannot copy a directory to a subfolder of itself.");
202 if (!overwrite &&
IsDirectory(src))
throw new IOException(
"Directory already exists.");
208 if (!overwrite &&
IsFile(src))
throw new IOException(
"File already exists.");
214 throw new IOException(
"Source path is not a file or directory");
218 public void Move(
string src,
string dst,
bool overwrite =
true)
220 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.
Contains Utility Functions and Classes.