PismoIsoManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.IsoMounter
  8. {
  9. /// <summary>
  10. /// Class IsoManager
  11. /// </summary>
  12. public class PismoIsoManager : IIsoManager
  13. {
  14. /// <summary>
  15. /// The mount semaphore - limit to four at a time.
  16. /// </summary>
  17. private readonly SemaphoreSlim _mountSemaphore = new SemaphoreSlim(4,4);
  18. /// <summary>
  19. /// The PFM API
  20. /// </summary>
  21. private PfmApi _pfmApi;
  22. /// <summary>
  23. /// The _PFM API initialized
  24. /// </summary>
  25. private bool _pfmApiInitialized;
  26. /// <summary>
  27. /// The _PFM API sync lock
  28. /// </summary>
  29. private object _pfmApiSyncLock = new object();
  30. /// <summary>
  31. /// Gets the display prefs.
  32. /// </summary>
  33. /// <value>The display prefs.</value>
  34. private PfmApi PfmApi
  35. {
  36. get
  37. {
  38. LazyInitializer.EnsureInitialized(ref _pfmApi, ref _pfmApiInitialized, ref _pfmApiSyncLock, () =>
  39. {
  40. var err = PfmStatic.InstallCheck();
  41. if (err != PfmInst.installed)
  42. {
  43. throw new Exception("Pismo File Mount Audit Package is not installed");
  44. }
  45. PfmApi pfmApi;
  46. err = PfmStatic.ApiFactory(out pfmApi);
  47. if (err != 0)
  48. {
  49. throw new IOException("Unable to open PFM Api. Pismo File Mount Audit Package is probably not installed.");
  50. }
  51. return pfmApi;
  52. });
  53. return _pfmApi;
  54. }
  55. }
  56. /// <summary>
  57. /// The _has initialized
  58. /// </summary>
  59. private bool _hasInitialized;
  60. /// <summary>
  61. /// Gets or sets the logger.
  62. /// </summary>
  63. /// <value>The logger.</value>
  64. private ILogger Logger { get; set; }
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="PismoIsoManager" /> class.
  67. /// </summary>
  68. /// <param name="logger">The logger.</param>
  69. public PismoIsoManager(ILogger logger)
  70. {
  71. Logger = logger;
  72. _myPfmFileMountUi = new MyPfmFileMountUi(Logger);
  73. }
  74. /// <summary>
  75. /// The _my PFM file mount UI
  76. /// </summary>
  77. private readonly MyPfmFileMountUi _myPfmFileMountUi;
  78. /// <summary>
  79. /// Mounts the specified iso path.
  80. /// </summary>
  81. /// <param name="isoPath">The iso path.</param>
  82. /// <param name="cancellationToken">The cancellation token.</param>
  83. /// <param name="visibleToAllProcesses">if set to <c>true</c> [visible to all processes].</param>
  84. /// <returns>IsoMount.</returns>
  85. /// <exception cref="System.ArgumentNullException">isoPath</exception>
  86. /// <exception cref="System.IO.IOException">Unable to create mount.</exception>
  87. public async Task<IIsoMount> Mount(string isoPath, CancellationToken cancellationToken, bool visibleToAllProcesses = true)
  88. {
  89. if (string.IsNullOrEmpty(isoPath))
  90. {
  91. throw new ArgumentNullException("isoPath");
  92. }
  93. PfmFileMount mount;
  94. var err = PfmApi.FileMountCreate(out mount);
  95. if (err != 0)
  96. {
  97. throw new IOException("Unable to create mount for " + isoPath);
  98. }
  99. _hasInitialized = true;
  100. var fmp = new PfmFileMountCreateParams { };
  101. fmp.ui = _myPfmFileMountUi;
  102. fmp.fileMountFlags |= PfmFileMountFlag.inProcess;
  103. if (visibleToAllProcesses)
  104. {
  105. fmp.visibleProcessId = PfmVisibleProcessId.all;
  106. }
  107. fmp.mountFileName = isoPath;
  108. // unc only
  109. fmp.mountFlags |= PfmMountFlag.uncOnly;
  110. fmp.mountFlags |= PfmMountFlag.noShellNotify;
  111. fmp.mountFlags |= PfmMountFlag.readOnly;
  112. Logger.Info("Mounting {0}", isoPath);
  113. await _mountSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  114. err = mount.Start(fmp);
  115. if (err != 0)
  116. {
  117. _mountSemaphore.Release();
  118. mount.Dispose();
  119. throw new IOException("Unable to start mount for " + isoPath);
  120. }
  121. err = mount.WaitReady();
  122. if (err != 0)
  123. {
  124. _mountSemaphore.Release();
  125. mount.Dispose();
  126. throw new IOException("Unable to start mount for " + isoPath);
  127. }
  128. return new PismoMount(mount, isoPath, this, Logger);
  129. }
  130. /// <summary>
  131. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  132. /// </summary>
  133. public void Dispose()
  134. {
  135. Dispose(true);
  136. }
  137. /// <summary>
  138. /// Releases unmanaged and - optionally - managed resources.
  139. /// </summary>
  140. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  141. protected virtual void Dispose(bool dispose)
  142. {
  143. if (dispose)
  144. {
  145. if (_hasInitialized)
  146. {
  147. Logger.Info("Disposing PfmPapi");
  148. _pfmApi.Dispose();
  149. Logger.Info("PfmStatic.ApiUnload");
  150. PfmStatic.ApiUnload();
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// Gets a value indicating whether this instance can mount.
  156. /// </summary>
  157. /// <param name="path">The path.</param>
  158. /// <returns><c>true</c> if this instance can mount the specified path; otherwise, <c>false</c>.</returns>
  159. /// <value><c>true</c> if this instance can mount; otherwise, <c>false</c>.</value>
  160. public bool CanMount(string path)
  161. {
  162. try
  163. {
  164. return string.Equals(Path.GetExtension(path), ".iso", StringComparison.OrdinalIgnoreCase) && PfmApi != null;
  165. }
  166. catch
  167. {
  168. return false;
  169. }
  170. }
  171. /// <summary>
  172. /// Called when [unmount].
  173. /// </summary>
  174. /// <param name="mount">The mount.</param>
  175. internal void OnUnmount(PismoMount mount)
  176. {
  177. _mountSemaphore.Release();
  178. }
  179. }
  180. }