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
|
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Kernel;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.IsoMounter
{
/// <summary>
/// Class IsoManager
/// </summary>
public class IsoManager : BaseManager<IKernel>, IIsoManager
{
/// <summary>
/// The mount semaphore - limit to four at a time.
/// </summary>
private readonly SemaphoreSlim _mountSemaphore = new SemaphoreSlim(4,4);
/// <summary>
/// The PFM API
/// </summary>
private PfmApi _pfmApi;
/// <summary>
/// The _PFM API initialized
/// </summary>
private bool _pfmApiInitialized;
/// <summary>
/// The _PFM API sync lock
/// </summary>
private object _pfmApiSyncLock = new object();
/// <summary>
/// Gets the display prefs.
/// </summary>
/// <value>The display prefs.</value>
private PfmApi PfmApi
{
get
{
LazyInitializer.EnsureInitialized(ref _pfmApi, ref _pfmApiInitialized, ref _pfmApiSyncLock, () =>
{
var err = PfmStatic.InstallCheck();
if (err != PfmInst.installed)
{
throw new Exception("Pismo File Mount Audit Package is not installed");
}
PfmApi pfmApi;
err = PfmStatic.ApiFactory(out pfmApi);
if (err != 0)
{
throw new IOException("Unable to open PFM Api. Pismo File Mount Audit Package is probably not installed.");
}
return pfmApi;
});
return _pfmApi;
}
}
/// <summary>
/// The _has initialized
/// </summary>
private bool _hasInitialized;
/// <summary>
/// Initializes a new instance of the <see cref="IsoManager" /> class.
/// </summary>
/// <param name="kernel">The kernel.</param>
public IsoManager(IKernel kernel)
: base(kernel)
{
}
/// <summary>
/// The _my PFM file mount UI
/// </summary>
private readonly MyPfmFileMountUi _myPfmFileMountUi = new MyPfmFileMountUi();
/// <summary>
/// Mounts the specified iso path.
/// </summary>
/// <param name="isoPath">The iso path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="visibleToAllProcesses">if set to <c>true</c> [visible to all processes].</param>
/// <returns>IsoMount.</returns>
/// <exception cref="System.ArgumentNullException">isoPath</exception>
/// <exception cref="System.IO.IOException">Unable to create mount.</exception>
public async Task<IIsoMount> Mount(string isoPath, CancellationToken cancellationToken, bool visibleToAllProcesses = true)
{
if (string.IsNullOrEmpty(isoPath))
{
throw new ArgumentNullException("isoPath");
}
PfmFileMount mount;
var err = PfmApi.FileMountCreate(out mount);
if (err != 0)
{
throw new IOException("Unable to create mount for " + isoPath);
}
_hasInitialized = true;
var fmp = new PfmFileMountCreateParams { };
fmp.ui = _myPfmFileMountUi;
fmp.fileMountFlags |= PfmFileMountFlag.inProcess;
if (visibleToAllProcesses)
{
fmp.visibleProcessId = PfmVisibleProcessId.all;
}
fmp.mountFileName = isoPath;
// unc only
fmp.mountFlags |= PfmMountFlag.uncOnly;
fmp.mountFlags |= PfmMountFlag.noShellNotify;
fmp.mountFlags |= PfmMountFlag.readOnly;
Logger.Info("Mounting {0}", isoPath);
await _mountSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
err = mount.Start(fmp);
if (err != 0)
{
_mountSemaphore.Release();
mount.Dispose();
throw new IOException("Unable to start mount for " + isoPath);
}
err = mount.WaitReady();
if (err != 0)
{
_mountSemaphore.Release();
mount.Dispose();
throw new IOException("Unable to start mount for " + isoPath);
}
return new IsoMount(mount, isoPath, this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool dispose)
{
if (dispose)
{
if (_hasInitialized)
{
Logger.Info("Disposing PfmPapi");
_pfmApi.Dispose();
Logger.Info("PfmStatic.ApiUnload");
PfmStatic.ApiUnload();
}
}
base.Dispose(dispose);
}
/// <summary>
/// Gets a value indicating whether this instance can mount.
/// </summary>
/// <param name="path">The path.</param>
/// <returns><c>true</c> if this instance can mount the specified path; otherwise, <c>false</c>.</returns>
/// <value><c>true</c> if this instance can mount; otherwise, <c>false</c>.</value>
public bool CanMount(string path)
{
try
{
return string.Equals(Path.GetExtension(path), ".iso", StringComparison.OrdinalIgnoreCase) && PfmApi != null;
}
catch
{
return false;
}
}
/// <summary>
/// Called when [unmount].
/// </summary>
/// <param name="mount">The mount.</param>
internal void OnUnmount(IsoMount mount)
{
_mountSemaphore.Release();
}
}
}
|