BadScript 2
Loading...
Searching...
No Matches
BadScript2.Container.BadLayeredFileSystem Class Reference
Inheritance diagram for BadScript2.Container.BadLayeredFileSystem:
BadScript2.IO.IVirtualFileSystem BadScript2.IO.IFileSystem

Public Member Functions

BadLayeredFileSystemStackInfo GetInfo ()
 
 BadLayeredFileSystem (params BadLayeredFileSystemLayer[] layers)
 
void Optimize ()
 
bool Restore (string path)
 
string GetStartupDirectory ()
 The Startup Directory of the Application.
 
bool Exists (string path)
 Returns true if the given path is a file or directory.
 
bool IsFile (string path)
 Returns true if the given path is a file.
 
bool IsDirectory (string path)
 Returns true if the given path is a directory.
 
IEnumerable< string > GetFiles (string path, string extension, bool recursive)
 Returns all files in the given directory that match the specified extension.
 
IEnumerable< string > GetDirectories (string path, bool recursive)
 Returns all directories in the given directory.
 
void CreateDirectory (string path, bool recursive=false)
 Creates a new directory.
 
void DeleteDirectory (string path, bool recursive)
 Deletes a directory.
 
void DeleteFile (string path)
 Deletes a file.
 
string GetFullPath (string path)
 Returns the full path of the given path.
 
Stream OpenRead (string path)
 Opens a file for reading.
 
Stream OpenWrite (string path, BadWriteMode mode)
 Opens a file for writing.
 
string GetCurrentDirectory ()
 Returns the Current Directory.
 
void SetCurrentDirectory (string path)
 Sets the current Directory.
 
void Copy (string src, string dst, bool overwrite=true)
 Copies a file or directory to a new location.
 
void Move (string src, string dst, bool overwrite=true)
 Moves a file or directory to a new location.
 
BadVirtualFileSystem GetWritable ()
 

Private Member Functions

bool ContentEquals (Stream s1, Stream s2)
 
bool IsSubfolderOf (string root, string sub)
 
void CopyDirectoryToDirectory (string src, string dst)
 
void CopyFileToFile (string src, string dst)
 

Private Attributes

readonly BadLayeredFileSystemLayer[] m_Layers
 

Detailed Description

Definition at line 11 of file BadLayeredFileSystem.cs.

Constructor & Destructor Documentation

◆ BadLayeredFileSystem()

BadScript2.Container.BadLayeredFileSystem.BadLayeredFileSystem ( params BadLayeredFileSystemLayer[]  layers)

Definition at line 41 of file BadLayeredFileSystem.cs.

42 {
43 m_Layers = layers;
44 }
readonly BadLayeredFileSystemLayer[] m_Layers

Member Function Documentation

◆ ContentEquals()

bool BadScript2.Container.BadLayeredFileSystem.ContentEquals ( Stream  s1,
Stream  s2 
)
private

Definition at line 46 of file BadLayeredFileSystem.cs.

47 {
48 if (s1.Length != s2.Length) return false;
49 int b1, b2;
50 do
51 {
52 b1 = s1.ReadByte();
53 b2 = s2.ReadByte();
54 if (b1 != b2) return false;
55 } while (b1 != -1);
56 return true;
57 }

◆ Copy()

void BadScript2.Container.BadLayeredFileSystem.Copy ( string  src,
string  dst,
bool  overwrite = true 
)

Copies a file or directory to a new location.

Parameters
srcSource path
dstDestination path
overwriteShould file entries be overwritten?

Implements BadScript2.IO.IFileSystem.

Definition at line 195 of file BadLayeredFileSystem.cs.

196 {
197 if (IsDirectory(src))
198 {
199 if (IsSubfolderOf(src, dst)) throw new IOException("Cannot copy a directory to a subfolder of itself.");
200
201 if (!overwrite && IsDirectory(src)) throw new IOException("Directory already exists.");
202
203 CopyDirectoryToDirectory(src, dst);
204 }
205 else if (IsFile(src))
206 {
207 if (!overwrite && IsFile(src)) throw new IOException("File already exists.");
208
209 CopyFileToFile(src, dst);
210 }
211 else
212 {
213 throw new IOException("Source path is not a file or directory");
214 }
215 }
void CopyDirectoryToDirectory(string src, string dst)
bool IsFile(string path)
Returns true if the given path is a file.
bool IsDirectory(string path)
Returns true if the given path is a directory.

◆ CopyDirectoryToDirectory()

void BadScript2.Container.BadLayeredFileSystem.CopyDirectoryToDirectory ( string  src,
string  dst 
)
private

Definition at line 237 of file BadLayeredFileSystem.cs.

238 {
239 foreach (var directory in GetDirectories(src, true)) CreateDirectory(directory);
240
241 foreach (var file in GetFiles(src, "*", true)) CopyFileToFile(file, file.Replace(src, dst));
242 }
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.
IEnumerable< string > GetDirectories(string path, bool recursive)
Returns all directories in the given directory.

◆ CopyFileToFile()

void BadScript2.Container.BadLayeredFileSystem.CopyFileToFile ( string  src,
string  dst 
)
private

Definition at line 244 of file BadLayeredFileSystem.cs.

245 {
246 using var s = OpenRead(src);
247 using var d = OpenWrite(dst, BadWriteMode.CreateNew);
248 s.CopyTo(d);
249 }
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.
Stream OpenRead(string path)
Opens a file for reading.
BadWriteMode
The Write Modes of the File System Abstraction.

◆ CreateDirectory()

void BadScript2.Container.BadLayeredFileSystem.CreateDirectory ( string  path,
bool  recursive = false 
)

Creates a new directory.

Parameters
pathThe path to the directory
recursiveIf true, all parent directories will be created if they do not exist

Implements BadScript2.IO.IFileSystem.

Definition at line 138 of file BadLayeredFileSystem.cs.

139 {
140 GetWritable().CreateDirectory(path, recursive);
141 }
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.

◆ DeleteDirectory()

void BadScript2.Container.BadLayeredFileSystem.DeleteDirectory ( string  path,
bool  recursive 
)

Deletes a directory.

Parameters
pathThe path to the directory
recursiveIf true, all subdirectories will be deleted

Implements BadScript2.IO.IFileSystem.

Definition at line 143 of file BadLayeredFileSystem.cs.

144 {
145 if (!GetWritable().IsDirectory(path)) throw new Exception("Is Readonly");
146 GetWritable().DeleteDirectory(path, recursive);
147 }
void DeleteDirectory(string path, bool recursive)
Deletes a directory.

◆ DeleteFile()

void BadScript2.Container.BadLayeredFileSystem.DeleteFile ( string  path)

Deletes a file.

Parameters
pathThe path to the directory

Implements BadScript2.IO.IFileSystem.

Definition at line 149 of file BadLayeredFileSystem.cs.

150 {
151 if (!GetWritable().IsFile(path)) throw new Exception("Is Readonly");
152 GetWritable().DeleteFile(path);
153 }
void DeleteFile(string path)
Deletes a file.

◆ Exists()

bool BadScript2.Container.BadLayeredFileSystem.Exists ( string  path)

Returns true if the given path is a file or directory.

Parameters
pathThe path to check
Returns
true if the given path is a file or directory

Implements BadScript2.IO.IFileSystem.

Definition at line 105 of file BadLayeredFileSystem.cs.

106 {
107 return m_Layers.Any(x => x.FileSystem.Exists(path));
108 }

◆ GetCurrentDirectory()

string BadScript2.Container.BadLayeredFileSystem.GetCurrentDirectory ( )

Returns the Current Directory.

Returns
The Current Directory

Implements BadScript2.IO.IFileSystem.

Definition at line 185 of file BadLayeredFileSystem.cs.

186 {
188 }
string GetCurrentDirectory()
Returns the Current Directory.

◆ GetDirectories()

IEnumerable< string > BadScript2.Container.BadLayeredFileSystem.GetDirectories ( string  path,
bool  recursive 
)

Returns all directories in the given directory.

Parameters
pathThe path to the directory
recursiveTrue if the search should be recursive
Returns
Enumeration of all directories in the given directory that match the specified extension

Implements BadScript2.IO.IFileSystem.

Definition at line 129 of file BadLayeredFileSystem.cs.

130 {
131 return m_Layers.SelectMany(x =>
132 x.FileSystem.Exists(path) &&
133 x.FileSystem.IsDirectory(path) ?
134 x.FileSystem.GetDirectories(path, recursive) :
135 Enumerable.Empty<string>()).Distinct();
136 }

◆ GetFiles()

IEnumerable< string > BadScript2.Container.BadLayeredFileSystem.GetFiles ( string  path,
string  extension,
bool  recursive 
)

Returns all files in the given directory that match the specified extension.

Parameters
pathThe path to the directory
extensionThe extension to match
recursiveTrue if the search should be recursive
Returns
Enumeration of all files in the given directory that match the specified extension

Implements BadScript2.IO.IFileSystem.

Definition at line 120 of file BadLayeredFileSystem.cs.

121 {
122 return m_Layers.SelectMany(x =>
123 x.FileSystem.Exists(path) &&
124 x.FileSystem.IsDirectory(path) ?
125 x.FileSystem.GetFiles(path, extension, recursive) :
126 Enumerable.Empty<string>()).Distinct();
127 }

◆ GetFullPath()

string BadScript2.Container.BadLayeredFileSystem.GetFullPath ( string  path)

Returns the full path of the given path.

Parameters
pathPath
Returns
Full Path

Implements BadScript2.IO.IFileSystem.

Definition at line 155 of file BadLayeredFileSystem.cs.

156 {
157 return GetWritable().GetFullPath(path);
158 }
string GetFullPath(string path)
Returns the full path of the given path.

◆ GetInfo()

BadLayeredFileSystemStackInfo BadScript2.Container.BadLayeredFileSystem.GetInfo ( )

Definition at line 15 of file BadLayeredFileSystem.cs.

16 {
17 List<BadLayeredFileSystemInfo> fileSystems = new List<BadLayeredFileSystemInfo>();
18 Dictionary<string,BadLayeredFileSystemFileInfo> files = new Dictionary<string,BadLayeredFileSystemFileInfo>();
19 foreach (var layer in m_Layers)
20 {
21 var fs = layer.FileSystem;
22 fileSystems.Add(new BadLayeredFileSystemInfo() { Writable = fs == GetWritable(), Name = layer.Name, MetaData = layer.MetaData });
23 foreach (var file in fs.GetFiles("/", "", true))
24 {
25 if (!files.TryGetValue(file, out var info))
26 {
27 info = new BadLayeredFileSystemFileInfo()
28 {
29 Path = file,
30 PresentIn = new List<string>()
31 };
32 files[file] = info;
33 }
34
35 info.PresentIn.Add(layer.Name);
36 }
37 }
38
39 return new BadLayeredFileSystemStackInfo() { FileSystems = fileSystems.ToArray(), Files = files.Values.ToArray() };
40 }

◆ GetStartupDirectory()

string BadScript2.Container.BadLayeredFileSystem.GetStartupDirectory ( )

The Startup Directory of the Application.

Returns
The Startup Directory of the Application

Implements BadScript2.IO.IFileSystem.

Definition at line 100 of file BadLayeredFileSystem.cs.

101 {
103 }
string GetStartupDirectory()
The Startup Directory of the Application.

◆ GetWritable()

BadVirtualFileSystem BadScript2.Container.BadLayeredFileSystem.GetWritable ( )

Definition at line 227 of file BadLayeredFileSystem.cs.

228 {
229 return m_Layers.Last().FileSystem;
230 }

◆ IsDirectory()

bool BadScript2.Container.BadLayeredFileSystem.IsDirectory ( string  path)

Returns true if the given path is a directory.

Parameters
pathThe path to check
Returns
true if the given path is a directory

Implements BadScript2.IO.IFileSystem.

Definition at line 115 of file BadLayeredFileSystem.cs.

116 {
117 return m_Layers.Any(x => x.FileSystem.IsDirectory(path));
118 }

◆ IsFile()

bool BadScript2.Container.BadLayeredFileSystem.IsFile ( string  path)

Returns true if the given path is a file.

Parameters
pathThe path to check
Returns
true if the given path is a file

Implements BadScript2.IO.IFileSystem.

Definition at line 110 of file BadLayeredFileSystem.cs.

111 {
112 return m_Layers.Any(x => x.FileSystem.IsFile(path));
113 }

◆ IsSubfolderOf()

bool BadScript2.Container.BadLayeredFileSystem.IsSubfolderOf ( string  root,
string  sub 
)
private

Definition at line 232 of file BadLayeredFileSystem.cs.

233 {
234 return GetFullPath(sub).StartsWith(GetFullPath(root));
235 }
string GetFullPath(string path)
Returns the full path of the given path.

◆ Move()

void BadScript2.Container.BadLayeredFileSystem.Move ( string  src,
string  dst,
bool  overwrite = true 
)

Moves a file or directory to a new location.

Parameters
srcSource path
dstDestination path
overwriteShould file entries be overwritten?

Implements BadScript2.IO.IFileSystem.

Definition at line 217 of file BadLayeredFileSystem.cs.

218 {
219 Copy(src, dst, overwrite);
220
221 if (IsDirectory(src))
222 DeleteDirectory(src, true);
223 else
224 DeleteFile(src);
225 }
void DeleteDirectory(string path, bool recursive)
Deletes a directory.
void Copy(string src, string dst, bool overwrite=true)
Copies a file or directory to a new location.
void DeleteFile(string path)
Deletes a file.

◆ OpenRead()

Stream BadScript2.Container.BadLayeredFileSystem.OpenRead ( string  path)

Opens a file for reading.

Parameters
pathThe path to the file
Returns
File Stream

Implements BadScript2.IO.IFileSystem.

Definition at line 160 of file BadLayeredFileSystem.cs.

161 {
162 var fs = m_Layers.Last(x => x.FileSystem.IsFile(path))?.FileSystem ?? GetWritable();
163 return fs.OpenRead(path);
164 }

◆ OpenWrite()

Stream BadScript2.Container.BadLayeredFileSystem.OpenWrite ( string  path,
BadWriteMode  mode 
)

Opens a file for writing.

Parameters
pathThe path to the file
modeThe Write Mode
Returns
File Stream

Implements BadScript2.IO.IFileSystem.

Definition at line 166 of file BadLayeredFileSystem.cs.

167 {
168 var writable = GetWritable();
169 var dir = Path.GetDirectoryName(path);
170 if (dir != null && IsDirectory(dir) && !writable.IsDirectory(dir))
171 {
172 //Create the directory if it does not exist(it exists in some other layer, we need to create it in the writable layer)
173 writable.CreateDirectory(dir, true);
174 }
175 //In order to properly work with Append mode, we need to copy the file to the writable file system if it does not exist
176 if (mode == BadWriteMode.Append && !writable.IsFile(path))
177 {
178 using var src = OpenRead(path);
179 using var dst = writable.OpenWrite(path, BadWriteMode.CreateNew);
180 src.CopyTo(dst);
181 }
182 return writable.OpenWrite(path, mode);
183 }

◆ Optimize()

void BadScript2.Container.BadLayeredFileSystem.Optimize ( )

Definition at line 58 of file BadLayeredFileSystem.cs.

59 {
60 //Ensure that the writable layer does not have a file that already exists in a read-only layer and has the same content
61 var writable = GetWritable();
62 foreach (var file in writable.GetFiles("/", "", true).ToArray())
63 {
64 foreach (var layer in m_Layers.SkipLast(1))
65 {
66 if (layer.FileSystem.Exists(file) && layer.FileSystem.IsFile(file))
67 {
68 bool equals;
69 using (var wStream = writable.OpenRead(file))
70 {
71 using var rStream = layer.FileSystem.OpenRead(file);
72 equals = ContentEquals(wStream, rStream);
73 }
74 if (equals)
75 {
76 writable.DeleteFile(file);
77 break;
78 }
79 }
80 }
81 }
82 }

◆ Restore()

bool BadScript2.Container.BadLayeredFileSystem.Restore ( string  path)

Definition at line 83 of file BadLayeredFileSystem.cs.

84 {
85 var writable = GetWritable();
86 if (writable.Exists(path))
87 {
88 if (writable.IsFile(path))
89 {
90 writable.DeleteFile(path);
91 }
92 else
93 {
94 writable.DeleteDirectory(path, true);
95 }
96 return true;
97 }
98 return false;
99 }

◆ SetCurrentDirectory()

void BadScript2.Container.BadLayeredFileSystem.SetCurrentDirectory ( string  path)

Sets the current Directory.

Parameters
paththe new current directory

Implements BadScript2.IO.IFileSystem.

Definition at line 190 of file BadLayeredFileSystem.cs.

191 {
192 foreach (var fs in m_Layers) fs.FileSystem.SetCurrentDirectory(path);
193 }

Member Data Documentation

◆ m_Layers

readonly BadLayeredFileSystemLayer [] BadScript2.Container.BadLayeredFileSystem.m_Layers
private

Definition at line 13 of file BadLayeredFileSystem.cs.


The documentation for this class was generated from the following file: