-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSBackendDisk.cs
More file actions
256 lines (229 loc) · 8.7 KB
/
Copy pathFSBackendDisk.cs
File metadata and controls
256 lines (229 loc) · 8.7 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DokanNet;
using Mutagen.Bethesda.Archives;
using NC.DokanFS;
using FileAccess = System.IO.FileAccess;
namespace BsaFS
{
public class FSBackendDisk: IDokanDisk
{
private IArchiveReader myArchiveReader;
public FSBackendDisk(IArchiveReader testing)
{
myArchiveReader = testing;
}
public string GetPath(string osPath)
{
return osPath.TrimStart('\\');
}
public IDokanFileContext CreateFileContext(string path, FileMode mode, FileAccess access, FileShare share = FileShare.None,
FileOptions options = FileOptions.None)
{
var dokanFileContext = new FSBackendFile(myArchiveReader.Files.Single(mfile => mfile.Path == path));
return dokanFileContext;
}
public void CreateDirectory(string path)
{
throw new IOException();
}
public bool IsDirectory(string path)
{
if (path == "") return true;
var isDirectory = myArchiveReader.Files.All(mfile => mfile.Path != path);
return isDirectory;
}
public bool DirectoryExists(string path)
{
if (path == "") return true;
var tryGetFolder =
myArchiveReader.Files.Any(mfile => mfile.Path.StartsWith(path) && !mfile.Path.Equals(path));
return tryGetFolder;
}
public bool IsDirectoryEmpty(string path)
{
return false;
}
public void DeleteDirectory(string path)
{
throw new IOException();
}
public bool DirectoryCanBeDeleted(string path)
{
return false;
}
public void DeleteFile(string path)
{
throw new IOException();
}
public bool FileExists(string path)
{
return myArchiveReader.Files.Any(mfile => mfile.Path == path);
}
public IList<FileInformation> FindFiles(string directory, string searchPattern)
{
var filelist = new List<FileInformation>();
var mdir = directory.TrimStart('\\');
var Patterntrim = searchPattern.TrimStart('\\');
var JoinedSearch = Path.Join(mdir, Patterntrim);
if (mdir == "")
{
return FindFilesRootDir();
}
//Handle Joined
if (IsDirectory(JoinedSearch) && !JoinedSearch.EndsWith("*"))
{
var foundFiles = myArchiveReader.Files.Select(mfile => mfile.Path)
.Where(mdir2 => mdir2.StartsWith(JoinedSearch));
var StrippedFiles = foundFiles.Select(mdir2 => mdir2.Replace(JoinedSearch,"").TrimStart('\\'));
var DirectoryContents = StrippedFiles.Select(mdir2 => mdir2.Split('\\')[0]).Distinct();
foreach (var directoryContent in DirectoryContents)
{
var fullentry = Path.Join(JoinedSearch, directoryContent);
if (FileExists(fullentry))
{
var mfilea = myArchiveReader.Files.Single(mfile => mfile.Path == fullentry);
filelist.Add(new FileInformation
{
Attributes = FileAttributes.Normal,
FileName = directoryContent,
Length = mfilea.Size
});
}
else
{
filelist.Add(new FileInformation
{
Attributes = FileAttributes.Directory,
FileName = directoryContent
});
}
}
}
if (IsDirectory(mdir) && !FileExists(mdir))
{
var foundFiles = myArchiveReader.Files.Select(mfile => mfile.Path)
.Where(mdir2 => mdir2.StartsWith(mdir));
var StrippedFiles = foundFiles.Select(mdir2 => mdir2.Replace(mdir,"").TrimStart('\\'));
var DirectoryContents = StrippedFiles.Select(mdir2 => mdir2.Split('\\')[0]).Distinct();
foreach (var directoryContent in DirectoryContents)
{
var fullentry = Path.Join(mdir, directoryContent);
if (FileExists(fullentry))
{
var mfilea = myArchiveReader.Files.Single(mfile => mfile.Path == fullentry);
filelist.Add(new FileInformation
{
Attributes = FileAttributes.Normal,
FileName = directoryContent,
Length = mfilea.Size
});
}
else
{
filelist.Add(new FileInformation
{
Attributes = FileAttributes.Directory,
FileName = directoryContent
});
}
}
}
if (FileExists(mdir))
{
var file = myArchiveReader.Files.Single(mfile => mfile.Path == mdir);
filelist.Add(new FileInformation
{
Attributes = FileAttributes.Normal,
FileName = System.IO.Path.GetFileName(mdir),
Length = file.Size
});
}
return filelist;
}
private IList<FileInformation> FindFilesRootDir()
{
var listOfFoldersAndFiles = new HashSet<FileInformation>();
var filePaths = myArchiveReader.Files.Select(mfile => mfile.Path);
foreach (var filePath in filePaths)
{
if (filePath.Contains("\\"))
{
listOfFoldersAndFiles.Add(new FileInformation
{
Attributes = FileAttributes.Directory,
FileName = filePath.Split("\\")[0]
});
}
else
{
listOfFoldersAndFiles.Add(new FileInformation
{
Attributes = FileAttributes.Normal,
FileName = filePath
});
}
}
return listOfFoldersAndFiles.ToList();
}
public bool GetFileInfo(string path, out FileInformation fi)
{
fi = new FileInformation();
//handle directory
if (path == "" || IsDirectory(path))
{
fi.Attributes = FileAttributes.Directory;
return true;
}
//handle file doesnt exist
if (!FileExists(path)) return false;
//handle file
var thefile = myArchiveReader.Files.Single(mfile => mfile.Path == path);
fi.Attributes = FileAttributes.Normal;
fi.Length = thefile.Size;
fi.FileName = Path.GetFileName(path);
return true;
}
public void GetDiskFreeSpace(out long freeBytesAvailable, out long totalNumberOfBytes, out long totalNumberOfFreeBytes)
{
freeBytesAvailable = 0;
totalNumberOfBytes = myArchiveReader.Files.Sum(mfile => mfile.Size);
totalNumberOfFreeBytes = 0;
}
public void MoveDirectory(string oldPath, string newPath)
{
throw new IOException();
}
public void MoveFile(string oldPath, string newPath)
{
throw new IOException();
}
public void SetFileAttribute(string path, FileAttributes attr)
{
throw new IOException();
}
public void SetFileTime(string path, DateTime? creationTime, DateTime? lastAccessTime, DateTime? lastWriteTime)
{
throw new IOException();
}
public IDokanFile Touch(string path, FileAttributes attributes)
{
throw new IOException();
}
public void UpdateFileInformation(IDokanFile file)
{
throw new IOException();
}
public void UpdateDirectoryInformation(IDokanDirectory dir)
{
throw new IOException();
}
public string Id { get; }
public string VolumeLabel { get; }
public string FileSystemName { get; }
public FileSystemFeatures FileSystemFeatures => FileSystemFeatures.ReadOnlyVolume;
public uint MaximumComponentLength => 256;
}
}