forked from kgober/FSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HostFS.cs
291 lines (261 loc) · 10.9 KB
/
HostFS.cs
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
// HostFS.cs
// Copyright © 2019-2020,2023 Kenneth Gober
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// HostFS - Host File System
// The HostFS and HostPath classes provide access to the 'native' host file system,
// mainly for use as a source or destination for load/save operations.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace FSX
{
// HostFS - mount a host volume
class HostFS : FileSystem
{
protected String mSource;
protected String mType;
protected DirectoryInfo mCWD;
protected String mDir;
protected HostFS()
{
}
public HostFS(String source, String format)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (source.EndsWith(@"\")) source = source.Substring(0, source.Length - 1);
mSource = source;
mType = String.Concat("Host/", format);
mCWD = new DirectoryInfo(String.Concat(source, "."));
String dir = mCWD.FullName.Substring(2);
if (!dir.EndsWith(@"\")) dir = String.Concat(dir, @"\");
mDir = dir;
}
else
{
mSource = source;
mType = String.Concat("Host/", format);
mCWD = new DirectoryInfo(String.Concat(source, "."));
String dir = mCWD.FullName;
mDir = dir;
}
}
public override String Source
{
get { return mSource; }
}
public override String Type
{
get { return mType; }
}
public override String Info
{
get { return null; }
}
public override String Dir
{
get { return mDir; }
}
public override Encoding DefaultEncoding
{
get { return Encoding.Default; }
}
public override void ChangeDir(String dirSpec)
{
String dir = (dirSpec.StartsWith(@"\")) ? dirSpec : String.Concat(mDir, dirSpec);
if (!IsValidDir(dir)) return;
mCWD = new DirectoryInfo(dir);
dir = mCWD.FullName.Substring(2);
if (!dir.EndsWith(@"\")) dir = String.Concat(dir, @"\");
mDir = dir;
}
public override void ListDir(String fileSpec, TextWriter output)
{
if ((fileSpec == null) || (fileSpec.Length == 0)) fileSpec = "*.*";
String s = mCWD.FullName.Substring(0, 1);
DriveInfo di = new DriveInfo(s);
output.WriteLine(" Volume in drive {0} is {1}", s, di.VolumeLabel);
output.WriteLine();
output.WriteLine(" Directory of {0}", mCWD.FullName);
output.WriteLine();
Int32 fc = 0;
Int64 fs = 0;
Int32 dc = 0;
if (mCWD.FullName.Length > 3)
{
DirectoryInfo i = new DirectoryInfo(".");
s = "<DIR> ";
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1} {2}", i.LastWriteTime, s, ".");
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1} {2}", i.LastWriteTime, s, "..");
dc = 2;
}
foreach (FileSystemInfo e in mCWD.GetFileSystemInfos(fileSpec))
{
if ((e.Attributes & (FileAttributes.Directory|FileAttributes.Hidden)) == FileAttributes.Directory)
{
DirectoryInfo i = new DirectoryInfo(e.FullName);
s = "<DIR> ";
if ((i.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) s = "<JUNCTION> ";
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1} {2}", i.LastWriteTime, s, i.Name);
dc++;
}
else if ((e.Attributes & FileAttributes.Hidden) == 0)
{
FileInfo i = new FileInfo(e.FullName);
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1,17:N0} {2}", i.LastWriteTime, i.Length, i.Name);
fc++;
fs += i.Length;
}
}
output.WriteLine(" {0,14:N0} File(s) {1,14:N0} bytes", fc, fs);
output.WriteLine(" {0,14:N0} Dir(s) {1,14:N0} bytes free", dc, di.AvailableFreeSpace);
}
public override void DumpDir(String fileSpec, TextWriter output)
{
if ((fileSpec == null) || (fileSpec.Length == 0)) fileSpec = "*.*";
String s = mCWD.FullName.Substring(0, 1);
DriveInfo di = new DriveInfo(s);
output.WriteLine(" Volume in drive {0} is {1}", s, di.VolumeLabel);
output.WriteLine();
output.WriteLine(" Directory of {0}", mCWD.FullName);
output.WriteLine();
Int32 fc = 0;
Int64 fs = 0;
Int32 dc = 0;
if (mCWD.FullName.Length > 3)
{
DirectoryInfo i = new DirectoryInfo(".");
s = "<DIR> ";
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1} {2}", i.LastWriteTime, s, ".");
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1} {2}", i.LastWriteTime, s, "..");
dc = 2;
}
foreach (FileSystemInfo e in mCWD.GetFileSystemInfos(fileSpec))
{
if ((e.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
DirectoryInfo i = new DirectoryInfo(e.FullName);
s = "<DIR> ";
if ((i.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) s = "<JUNCTION> ";
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1} {2}", i.LastWriteTime, s, i.Name);
dc++;
}
else
{
FileInfo i = new FileInfo(e.FullName);
output.WriteLine("{0:MM/dd/yyyy hh:mm tt} {1,17:N0} {2}", i.LastWriteTime, i.Length, i.Name);
fc++;
fs += i.Length;
}
}
output.WriteLine(" {0,14:N0} File(s) {1,14:N0} bytes", fc, fs);
output.WriteLine(" {0,14:N0} Dir(s) {1,14:N0} bytes free", dc, di.AvailableFreeSpace);
}
public override void ListFile(String fileSpec, Encoding encoding, TextWriter output)
{
String fn = (fileSpec.StartsWith(@"\")) ? fileSpec : String.Concat(mCWD.FullName, @"\", fileSpec);
if (!IsValidFile(fn)) return;
output.Write(File.ReadAllText(fn, encoding));
}
public override void DumpFile(String fileSpec, TextWriter output)
{
String fn = (fileSpec.StartsWith(@"\")) ? fileSpec : String.Concat(mCWD.FullName, @"\", fileSpec);
if (!IsValidFile(fn)) return;
Program.Dump(null, File.ReadAllBytes(fn), output, 16, 512, Program.DumpOptions.DOS | Program.DumpOptions.ANSI);
}
public override String FullName(String fileSpec)
{
String fn = (fileSpec.StartsWith(@"\")) ? fileSpec : String.Concat(mCWD.FullName, @"\", fileSpec);
if (!IsValidFile(fn)) return null;
FileInfo i = new FileInfo(fn);
return i.FullName.Substring(2);
}
public override Byte[] ReadFile(String fileSpec)
{
String fn = (fileSpec.StartsWith(@"\")) ? fileSpec : String.Concat(mCWD.FullName, @"\", fileSpec);
if (!IsValidFile(fn)) return new Byte[0];
try
{
return File.ReadAllBytes(fn);
}
catch (Exception ex)
{
Console.Error.WriteLine("{0}: {1}", ex.GetType().ToString(), ex.Message);
return new Byte[0];
}
}
public override Boolean SaveFS(String fileName, String format)
{
return false;
}
protected virtual Boolean IsValidDir(String hostPath)
{
return Directory.Exists(hostPath);
}
protected virtual Boolean IsValidFile(String hostPath)
{
return File.Exists(hostPath);
}
}
// HostPath - mount a host directory as a volume (with "chroot" semantics)
class HostPath : HostFS
{
public HostPath(String source)
{
String dir = source;
if (!dir.EndsWith(@"\")) dir = String.Concat(dir, @"\");
mCWD = new DirectoryInfo(dir);
mSource = mCWD.FullName;
mType = "HostPath";
mDir = @"\";
}
public override void ChangeDir(String dirSpec)
{
String dir = String.Concat((dirSpec.StartsWith(@"\")) ? mSource : mCWD.FullName, dirSpec);
if (!dir.EndsWith(@"\")) dir = String.Concat(dir, @"\");
if (!IsValidDir(dir)) return;
DirectoryInfo i = new DirectoryInfo(dir);
mCWD = i;
mDir = i.FullName.Substring(mSource.Length - 1);
}
public override String FullName(String fileSpec)
{
String fn = String.Concat((fileSpec.StartsWith(@"\")) ? mSource : mCWD.FullName, fileSpec);
if (!IsValidFile(fn)) return null;
FileInfo i = new FileInfo(fn);
return i.FullName.Substring(mSource.Length - 1);
}
protected override Boolean IsValidDir(String hostPath)
{
if (!Directory.Exists(hostPath)) return false;
DirectoryInfo i = new DirectoryInfo(hostPath);
if (!i.FullName.StartsWith(mSource)) return false;
return true;
}
protected override Boolean IsValidFile(String hostPath)
{
if (!File.Exists(hostPath)) return false;
FileInfo i = new FileInfo(hostPath);
if (!i.FullName.StartsWith(mSource)) return false;
return true;
}
}
}