PismoIsoManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. public PismoIsoManager(ILogger logger)
  66. {
  67. Logger = logger;
  68. }
  69. /// <summary>
  70. /// The _my PFM file mount UI
  71. /// </summary>
  72. private readonly MyPfmFileMountUi _myPfmFileMountUi = new MyPfmFileMountUi();
  73. /// <summary>
  74. /// Mounts the specified iso path.
  75. /// </summary>
  76. /// <param name="isoPath">The iso path.</param>
  77. /// <param name="cancellationToken">The cancellation token.</param>
  78. /// <param name="visibleToAllProcesses">if set to <c>true</c> [visible to all processes].</param>
  79. /// <returns>IsoMount.</returns>
  80. /// <exception cref="System.ArgumentNullException">isoPath</exception>
  81. /// <exception cref="System.IO.IOException">Unable to create mount.</exception>
  82. public async Task<IIsoMount> Mount(string isoPath, CancellationToken cancellationToken, bool visibleToAllProcesses = true)
  83. {
  84. if (string.IsNullOrEmpty(isoPath))
  85. {
  86. throw new ArgumentNullException("isoPath");
  87. }
  88. PfmFileMount mount;
  89. var err = PfmApi.FileMountCreate(out mount);
  90. if (err != 0)
  91. {
  92. throw new IOException("Unable to create mount for " + isoPath);
  93. }
  94. _hasInitialized = true;
  95. var fmp = new PfmFileMountCreateParams { };
  96. fmp.ui = _myPfmFileMountUi;
  97. fmp.fileMountFlags |= PfmFileMountFlag.inProcess;
  98. if (visibleToAllProcesses)
  99. {
  100. fmp.visibleProcessId = PfmVisibleProcessId.all;
  101. }
  102. fmp.mountFileName = isoPath;
  103. // unc only
  104. fmp.mountFlags |= PfmMountFlag.uncOnly;
  105. fmp.mountFlags |= PfmMountFlag.noShellNotify;
  106. fmp.mountFlags |= PfmMountFlag.readOnly;
  107. Logger.Info("Mounting {0}", isoPath);
  108. await _mountSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  109. err = mount.Start(fmp);
  110. if (err != 0)
  111. {
  112. _mountSemaphore.Release();
  113. mount.Dispose();
  114. throw new IOException("Unable to start mount for " + isoPath);
  115. }
  116. err = mount.WaitReady();
  117. if (err != 0)
  118. {
  119. _mountSemaphore.Release();
  120. mount.Dispose();
  121. throw new IOException("Unable to start mount for " + isoPath);
  122. }
  123. return new PismoMount(mount, isoPath, this, Logger);
  124. }
  125. public void Dispose()
  126. {
  127. Dispose(true);
  128. }
  129. /// <summary>
  130. /// Releases unmanaged and - optionally - managed resources.
  131. /// </summary>
  132. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  133. protected virtual void Dispose(bool dispose)
  134. {
  135. if (dispose)
  136. {
  137. if (_hasInitialized)
  138. {
  139. Logger.Info("Disposing PfmPapi");
  140. _pfmApi.Dispose();
  141. Logger.Info("PfmStatic.ApiUnload");
  142. PfmStatic.ApiUnload();
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// Gets a value indicating whether this instance can mount.
  148. /// </summary>
  149. /// <param name="path">The path.</param>
  150. /// <returns><c>true</c> if this instance can mount the specified path; otherwise, <c>false</c>.</returns>
  151. /// <value><c>true</c> if this instance can mount; otherwise, <c>false</c>.</value>
  152. public bool CanMount(string path)
  153. {
  154. try
  155. {
  156. return string.Equals(Path.GetExtension(path), ".iso", StringComparison.OrdinalIgnoreCase) && PfmApi != null;
  157. }
  158. catch
  159. {
  160. return false;
  161. }
  162. }
  163. /// <summary>
  164. /// Called when [unmount].
  165. /// </summary>
  166. /// <param name="mount">The mount.</param>
  167. internal void OnUnmount(PismoMount mount)
  168. {
  169. _mountSemaphore.Release();
  170. }
  171. }
  172. }