Repository navigation
Expand file tree
/
Copy pathIsoFileSystem.cs
More file actions
308 lines (269 loc) · 9.93 KB
/
Copy pathIsoFileSystem.cs
File metadata and controls
308 lines (269 loc) · 9.93 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DiscUtils.Udf;
namespace QuickLook.Plugin.IsoViewer
{
/// <summary>
/// Hybrid ISO9660/Joliet + UDF.
/// Windows install ISOs: ISO9660 often only README.TXT; real tree is UDF.
/// </summary>
public sealed class IsoFileSystem : IDisposable
{
readonly SimpleIsoReader _iso;
readonly UdfBackend _udf;
readonly bool _useUdf;
public string VolumeId { get; private set; }
public IsoFileSystem(string path)
{
int isoCount = 0;
int udfCount = 0;
Exception isoEx = null, udfEx = null;
SimpleIsoReader iso = null;
UdfBackend udf = null;
try
{
iso = new SimpleIsoReader(path);
VolumeId = iso.VolumeId;
isoCount = CountEntries(iso.ListDirectory(""), iso, "", 0, 3);
}
catch (Exception ex)
{
isoEx = ex;
try { iso?.Dispose(); } catch { }
iso = null;
}
try
{
udf = new UdfBackend(path);
if (string.IsNullOrEmpty(VolumeId))
VolumeId = udf.VolumeId;
else if (!string.IsNullOrEmpty(udf.VolumeId))
VolumeId = udf.VolumeId;
udfCount = CountEntries(udf.ListDirectory(""), udf, "", 0, 3);
}
catch (Exception ex)
{
udfEx = ex;
try { udf?.Dispose(); } catch { }
udf = null;
}
if (udf != null && (iso == null || udfCount > isoCount || (isoCount <= 3 && udfCount > isoCount)))
{
_udf = udf;
_useUdf = true;
try { iso?.Dispose(); } catch { }
return;
}
if (iso != null)
{
_iso = iso;
_useUdf = false;
try { udf?.Dispose(); } catch { }
return;
}
throw new Exception(
"Cannot read image as ISO9660/Joliet or UDF. " +
(isoEx != null ? "ISO: " + isoEx.Message + ". " : "") +
(udfEx != null ? "UDF: " + udfEx.Message : "UDF: not available"));
}
static int CountEntries(IEnumerable<IsoDirEntry> entries, object fs, string path, int depth, int maxDepth)
{
int n = 0;
if (entries == null || depth > maxDepth) return 0;
foreach (var e in entries)
{
n++;
if (e.IsDir && depth < maxDepth)
{
string child = string.IsNullOrEmpty(path) ? e.Name : path + "/" + e.Name;
try
{
IEnumerable<IsoDirEntry> sub = null;
if (fs is SimpleIsoReader r) sub = r.ListDirectory(child);
else if (fs is UdfBackend u) sub = u.ListDirectory(child);
n += CountEntries(sub, fs, child, depth + 1, maxDepth);
}
catch { }
}
}
return n;
}
public bool Exists(string path) =>
_useUdf ? _udf.Exists(path) : _iso.Exists(path);
public byte[] ReadFile(string path) =>
_useUdf ? _udf.ReadFile(path) : _iso.ReadFile(path);
public IEnumerable<IsoDirEntry> ListDirectory(string path) =>
_useUdf ? _udf.ListDirectory(path) : _iso.ListDirectory(path);
public void Dispose()
{
try { _iso?.Dispose(); } catch { }
try { _udf?.Dispose(); } catch { }
}
}
/// <summary>
/// DiscUtils UDF backend using GetDirectories / GetFiles / GetFileLength
/// (reliable dir detection and sizes — unlike DiscFileSystemInfo casts).
/// </summary>
sealed class UdfBackend : IDisposable
{
readonly FileStream _stream;
readonly UdfReader _udf;
readonly Dictionary<string, Node> _root =
new Dictionary<string, Node>(StringComparer.OrdinalIgnoreCase);
public string VolumeId { get; }
class Node
{
public string Name;
public bool IsDir;
public long Size;
public Dictionary<string, Node> Children;
}
public UdfBackend(string path)
{
_stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
_udf = new UdfReader(_stream);
VolumeId = _udf.VolumeLabel;
BuildTree("", _root, 0);
if (_root.Count == 0)
throw new Exception("UDF tree is empty");
}
void BuildTree(string udfDirPath, Dictionary<string, Node> parent, int depth)
{
if (depth > 24) return;
// udfDirPath: "" for root, or "sources", or "sources\install"
string listPath = string.IsNullOrEmpty(udfDirPath) ? "\\" : "\\" + udfDirPath.Trim('\\');
string[] dirs = null;
string[] files = null;
try { dirs = _udf.GetDirectories(listPath); } catch { dirs = new string[0]; }
try { files = _udf.GetFiles(listPath); } catch { files = new string[0]; }
if (dirs != null)
{
foreach (var fullDir in dirs)
{
string name = Path.GetFileName(fullDir.TrimEnd('\\', '/'));
if (string.IsNullOrEmpty(name) || name == "." || name == "..") continue;
if (parent.ContainsKey(name)) continue;
string childUdf = string.IsNullOrEmpty(udfDirPath)
? name
: udfDirPath.TrimEnd('\\') + "\\" + name;
var node = new Node
{
Name = name,
IsDir = true,
Size = 0,
Children = new Dictionary<string, Node>(StringComparer.OrdinalIgnoreCase)
};
parent[name] = node;
try { BuildTree(childUdf, node.Children, depth + 1); }
catch { }
}
}
if (files != null)
{
foreach (var fullFile in files)
{
string name = Path.GetFileName(fullFile);
if (string.IsNullOrEmpty(name)) continue;
if (parent.ContainsKey(name)) continue;
long size = 0;
try { size = _udf.GetFileLength(fullFile); }
catch
{
try
{
using (var s = _udf.OpenFile(fullFile, FileMode.Open))
size = s.Length;
}
catch { }
}
parent[name] = new Node
{
Name = name,
IsDir = false,
Size = size
};
}
}
}
public bool Exists(string path)
{
if (Find(path) != null) return true;
string p = ToUdfPath(path);
try
{
return _udf.FileExists(p) || _udf.DirectoryExists(p);
}
catch { return false; }
}
public byte[] ReadFile(string path)
{
try
{
string p = ToUdfPath(path);
if (!_udf.FileExists(p)) return null;
using (var s = _udf.OpenFile(p, FileMode.Open))
{
if (s.Length <= 0 || s.Length > 64L * 1024 * 1024) return null;
byte[] data = new byte[s.Length];
int read = 0;
while (read < data.Length)
{
int n = s.Read(data, read, data.Length - read);
if (n <= 0) break;
read += n;
}
if (read != data.Length) Array.Resize(ref data, read);
return data;
}
}
catch { return null; }
}
public IEnumerable<IsoDirEntry> ListDirectory(string path)
{
Dictionary<string, Node> dict;
if (string.IsNullOrEmpty(path) || path == "/" || path == "\\")
dict = _root;
else
dict = Find(path)?.Children;
if (dict == null) yield break;
foreach (var kv in dict)
{
yield return new IsoDirEntry
{
Name = kv.Key,
IsDir = kv.Value.IsDir,
Size = kv.Value.Size
};
}
}
Node Find(string path)
{
if (string.IsNullOrEmpty(path) || path == "/" || path == "\\")
return null;
path = path.Replace('\\', '/').Trim('/');
var parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, Node> cur = _root;
Node found = null;
for (int i = 0; i < parts.Length; i++)
{
if (cur == null || !cur.TryGetValue(parts[i], out found))
return null;
if (i < parts.Length - 1)
cur = found.Children;
}
return found;
}
static string ToUdfPath(string path)
{
if (string.IsNullOrEmpty(path)) return "\\";
return "\\" + path.Replace('/', '\\').Trim('\\');
}
public void Dispose()
{
try { _udf?.Dispose(); } catch { }
try { _stream?.Dispose(); } catch { }
}
}
}