-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathReadOnlyFileSystem.cs
275 lines (232 loc) · 9.13 KB
/
ReadOnlyFileSystem.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
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Security.AccessControl;
using Fsp;
using VolumeInfo = Fsp.Interop.VolumeInfo;
using FileInfo = Fsp.Interop.FileInfo;
namespace XboxWinFsp
{
public class ReadOnlyFileSystem : FileSystemBase
{
internal string InputPath;
internal Stream Stream;
public List<IFileEntry> RootFiles;
public int SectorSize;
public ulong BytesFree;
public ulong BytesInUse;
public string VolumeLabel;
public Object StreamLock = new object();
public ReadOnlyFileSystem(Stream stream, string inputPath, int sectorSize)
{
Stream = stream;
SectorSize = sectorSize;
InputPath = inputPath;
VolumeLabel = Path.GetFileName(inputPath);
}
public interface IFileEntry
{
string Name { get; set; }
ulong Size { get; set; }
bool IsDirectory { get; set; }
DateTime CreationTime { get; set; }
DateTime LastWriteTime { get; set; }
DateTime LastAccessTime { get; set; }
List<IFileEntry> Children { get; set; }
IFileEntry Parent { get; set; }
uint ReadBytes(IntPtr buffer, ulong fileOffset, uint length);
}
public class FileInstance
{
ReadOnlyFileSystem FileSystem;
internal IFileEntry FileEntry;
public FileInstance(IFileEntry entry, ReadOnlyFileSystem fileSystem)
{
FileEntry = entry;
FileSystem = fileSystem;
}
public Int32 GetFileInfo(out FileInfo FileInfo)
{
FileInfo = new FileInfo();
FileInfo.FileAttributes = (uint)FileAttributes.Directory;
if (FileEntry != null)
{
FileInfo.CreationTime = (ulong)FileEntry.CreationTime.ToFileTimeUtc();
FileInfo.LastWriteTime = FileInfo.ChangeTime = (ulong)FileEntry.LastWriteTime.ToFileTimeUtc();
FileInfo.LastAccessTime = (ulong)FileEntry.LastAccessTime.ToFileTimeUtc();
FileInfo.FileSize = FileEntry.Size;
FileInfo.AllocationSize = Utility.RoundToPages(FileEntry.Size, (ulong)FileSystem.SectorSize) * (ulong)FileSystem.SectorSize;
FileInfo.FileAttributes = FileEntry.IsDirectory ? (uint)FileAttributes.Directory : 0;
}
FileInfo.FileAttributes |= (uint)FileAttributes.ReadOnly;
return STATUS_SUCCESS;
}
}
IFileEntry FindFile(string path)
{
IFileEntry foundEntry = null;
if (path.StartsWith("\\", StringComparison.InvariantCultureIgnoreCase))
path = path.Substring(1);
var filePath = path.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
// Try going through the split path to try and find the file entry
var searchList = RootFiles;
for (int i = 0; i < filePath.Length; i++)
{
var entry = searchList.Find(s => string.Compare(s.Name, filePath[i], StringComparison.InvariantCultureIgnoreCase) == 0);
if (entry == null)
break;
else
{
// If this is the last section then we've found the file!
// Otherwise we'll try searching the children of the current entry instead...
if (i + 1 == filePath.Length)
foundEntry = entry;
else
searchList = entry.Children;
}
}
return foundEntry;
}
public override Int32 Open(
String FileName,
UInt32 CreateOptions,
UInt32 GrantedAccess,
out Object FileNode,
out Object FileDesc0,
out FileInfo FileInfo,
out String NormalizedName)
{
FileNode = default;
NormalizedName = default;
if (string.IsNullOrEmpty(FileName) || FileName == "\\")
{
// No file path? Return some info about the root dir..
var fileDesc = new FileInstance(null, this);
FileDesc0 = fileDesc;
return fileDesc.GetFileInfo(out FileInfo);
}
var foundEntry = FindFile(FileName);
if (foundEntry == null)
{
FileInfo = default;
FileDesc0 = null;
return STATUS_OBJECT_NAME_NOT_FOUND;
}
var fileDesc2 = new FileInstance(foundEntry, this);
FileDesc0 = fileDesc2;
return fileDesc2.GetFileInfo(out FileInfo);
}
public override Boolean ReadDirectoryEntry(
Object FileNode,
Object FileDesc0,
String Pattern,
String Marker,
ref Object Context,
out String FileName,
out FileInfo FileInfo)
{
var Instance = (FileInstance)FileDesc0;
var Enumerator = (IEnumerator<IFileEntry>)Context;
if (Enumerator == null)
{
var childArr = RootFiles;
if (Instance.FileEntry != null)
childArr = Instance.FileEntry.Children;
Enumerator = childArr.GetEnumerator();
Context = Enumerator;
int Index = 0;
if (null != Marker)
{
Index = childArr.FindIndex(s => s.Name == Marker);
if (0 <= Index)
Index++;
else
Index = ~Index;
}
if (Index > 0)
for (int i = 0; i < Index; i++)
Enumerator.MoveNext();
}
if (Enumerator.MoveNext())
{
var entry = Enumerator.Current;
FileName = entry.Name;
var desc = new FileInstance(entry, this);
desc.GetFileInfo(out FileInfo);
return true;
}
FileName = default;
FileInfo = default;
return false;
}
public override Int32 Read(
Object FileNode,
Object FileDesc0,
IntPtr Buffer,
UInt64 Offset,
UInt32 Length,
out UInt32 PBytesTransferred)
{
var Instance = (FileInstance)FileDesc0;
if (Offset >= Instance.FileEntry.Size)
{
PBytesTransferred = 0;
return STATUS_END_OF_FILE;
}
PBytesTransferred = Instance.FileEntry.ReadBytes(Buffer, Offset, Length);
return STATUS_SUCCESS;
}
public override Int32 GetSecurityByName(
String FileName,
out UInt32 FileAttributes1/* or ReparsePointIndex */,
ref Byte[] SecurityDescriptor)
{
FileAttributes1 = 0;
if (FileName == "\\")
FileAttributes1 = (uint)FileAttributes.Directory;
else
{
var entry = FindFile(FileName);
if (entry != null)
FileAttributes1 = entry.IsDirectory ? (uint)FileAttributes.Directory : 0;
else
return STATUS_OBJECT_NAME_NOT_FOUND;
}
/*var RootSddl = "D:P(A;;RPWPLC;;;WD)";
var RootSecurityDescriptor = new RawSecurityDescriptor(RootSddl);
var securityDesc = new byte[RootSecurityDescriptor.BinaryLength];
RootSecurityDescriptor.GetBinaryForm(securityDesc, 0);*/
//SecurityDescriptor = securityDesc;
return STATUS_SUCCESS;
}
public override Int32 GetFileInfo(
Object FileNode,
Object FileDesc0,
out FileInfo FileInfo)
{
var instance = (FileInstance)FileDesc0;
return instance.GetFileInfo(out FileInfo);
}
public override Int32 GetVolumeInfo(out VolumeInfo VolumeInfo)
{
VolumeInfo = default;
VolumeInfo.FreeSize = BytesFree;
VolumeInfo.TotalSize = BytesInUse + BytesFree;
VolumeInfo.SetVolumeLabel(VolumeLabel.Trim(new char[] { '\0', (char)0xff }));
return STATUS_SUCCESS;
}
public override Int32 Init(Object Host0)
{
var Host = (FileSystemHost)Host0;
// Hack for us to set ReadOnlyVolume flag...
var paramsField = Host.GetType().GetField("_VolumeParams", BindingFlags.NonPublic | BindingFlags.Instance);
object paramsVal = paramsField.GetValue(Host);
var flagsField = paramsVal.GetType().GetField("Flags", BindingFlags.NonPublic | BindingFlags.Instance);
uint value = (uint)flagsField.GetValue(paramsVal);
flagsField.SetValue(paramsVal, value | 0x200);
paramsField.SetValue(Host, paramsVal);
return STATUS_SUCCESS;
}
}
}