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 12 of file BadLayeredFileSystem.cs.

Constructor & Destructor Documentation

◆ BadLayeredFileSystem()

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

Definition at line 42 of file BadLayeredFileSystem.cs.

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

Member Function Documentation

◆ ContentEquals()

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

Definition at line 47 of file BadLayeredFileSystem.cs.

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

◆ 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 196 of file BadLayeredFileSystem.cs.

197 {
198 if (IsDirectory(src))
199 {
200 if (IsSubfolderOf(src, dst)) throw new IOException("Cannot copy a directory to a subfolder of itself.");
201
202 if (!overwrite && IsDirectory(src)) throw new IOException("Directory already exists.");
203
204 CopyDirectoryToDirectory(src, dst);
205 }
206 else if (IsFile(src))
207 {
208 if (!overwrite && IsFile(src)) throw new IOException("File already exists.");
209
210 CopyFileToFile(src, dst);
211 }
212 else
213 {
214 throw new IOException("Source path is not a file or directory");
215 }
216 }
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 238 of file BadLayeredFileSystem.cs.

239 {
240 foreach (var directory in GetDirectories(src, true)) CreateDirectory(directory);
241
242 foreach (var file in GetFiles(src, "*", true)) CopyFileToFile(file, file.Replace(src, dst));
243 }
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 245 of file BadLayeredFileSystem.cs.

246 {
247 using var s = OpenRead(src);
248 using var d = OpenWrite(dst, BadWriteMode.CreateNew);
249 s.CopyTo(d);
250 }
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 139 of file BadLayeredFileSystem.cs.

140 {
141 GetWritable().CreateDirectory(path, recursive);
142 }
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 144 of file BadLayeredFileSystem.cs.

145 {
146 if (!GetWritable().IsDirectory(path)) throw new Exception("Is Readonly");
147 GetWritable().DeleteDirectory(path, recursive);
148 }
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 150 of file BadLayeredFileSystem.cs.

151 {
152 if (!GetWritable().IsFile(path)) throw new Exception("Is Readonly");
153 GetWritable().DeleteFile(path);
154 }
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 106 of file BadLayeredFileSystem.cs.

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

◆ GetCurrentDirectory()

string BadScript2.Container.BadLayeredFileSystem.GetCurrentDirectory ( )

Returns the Current Directory.

Returns
The Current Directory

Implements BadScript2.IO.IFileSystem.

Definition at line 186 of file BadLayeredFileSystem.cs.

187 {
189 }
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 130 of file BadLayeredFileSystem.cs.

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

◆ 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 121 of file BadLayeredFileSystem.cs.

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

◆ 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 156 of file BadLayeredFileSystem.cs.

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

◆ GetInfo()

BadLayeredFileSystemStackInfo BadScript2.Container.BadLayeredFileSystem.GetInfo ( )

Definition at line 16 of file BadLayeredFileSystem.cs.

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

◆ 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 101 of file BadLayeredFileSystem.cs.

102 {
104 }
string GetStartupDirectory()
The Startup Directory of the Application.

◆ GetWritable()

BadVirtualFileSystem BadScript2.Container.BadLayeredFileSystem.GetWritable ( )

Definition at line 228 of file BadLayeredFileSystem.cs.

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

◆ 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 116 of file BadLayeredFileSystem.cs.

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

◆ 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 111 of file BadLayeredFileSystem.cs.

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

◆ IsSubfolderOf()

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

Definition at line 233 of file BadLayeredFileSystem.cs.

234 {
235 return GetFullPath(sub).StartsWith(GetFullPath(root));
236 }
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 218 of file BadLayeredFileSystem.cs.

219 {
220 Copy(src, dst, overwrite);
221
222 if (IsDirectory(src))
223 DeleteDirectory(src, true);
224 else
225 DeleteFile(src);
226 }
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 161 of file BadLayeredFileSystem.cs.

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

◆ 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 167 of file BadLayeredFileSystem.cs.

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

◆ Optimize()

void BadScript2.Container.BadLayeredFileSystem.Optimize ( )

Definition at line 59 of file BadLayeredFileSystem.cs.

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

◆ Restore()

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

Definition at line 84 of file BadLayeredFileSystem.cs.

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

◆ SetCurrentDirectory()

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

Sets the current Directory.

Parameters
paththe new current directory

Implements BadScript2.IO.IFileSystem.

Definition at line 191 of file BadLayeredFileSystem.cs.

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

Member Data Documentation

◆ m_Layers

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

Definition at line 14 of file BadLayeredFileSystem.cs.


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