InstallationManager.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Events;
  3. using MediaBrowser.Common.Implementations.Security;
  4. using MediaBrowser.Common.IO;
  5. using MediaBrowser.Common.Net;
  6. using MediaBrowser.Common.Plugins;
  7. using MediaBrowser.Common.Progress;
  8. using MediaBrowser.Common.Security;
  9. using MediaBrowser.Common.Updates;
  10. using MediaBrowser.Model.Events;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.Serialization;
  13. using MediaBrowser.Model.Updates;
  14. using System;
  15. using System.Collections.Concurrent;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Security.Cryptography;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. namespace MediaBrowser.Common.Implementations.Updates
  23. {
  24. /// <summary>
  25. /// Manages all install, uninstall and update operations (both plugins and system)
  26. /// </summary>
  27. public class InstallationManager : IInstallationManager
  28. {
  29. public event EventHandler<InstallationEventArgs> PackageInstalling;
  30. public event EventHandler<InstallationEventArgs> PackageInstallationCompleted;
  31. public event EventHandler<InstallationFailedEventArgs> PackageInstallationFailed;
  32. public event EventHandler<InstallationEventArgs> PackageInstallationCancelled;
  33. /// <summary>
  34. /// The current installations
  35. /// </summary>
  36. public List<Tuple<InstallationInfo, CancellationTokenSource>> CurrentInstallations { get; set; }
  37. /// <summary>
  38. /// The completed installations
  39. /// </summary>
  40. public ConcurrentBag<InstallationInfo> CompletedInstallations { get; set; }
  41. #region PluginUninstalled Event
  42. /// <summary>
  43. /// Occurs when [plugin uninstalled].
  44. /// </summary>
  45. public event EventHandler<GenericEventArgs<IPlugin>> PluginUninstalled;
  46. /// <summary>
  47. /// Called when [plugin uninstalled].
  48. /// </summary>
  49. /// <param name="plugin">The plugin.</param>
  50. private void OnPluginUninstalled(IPlugin plugin)
  51. {
  52. EventHelper.FireEventIfNotNull(PluginUninstalled, this, new GenericEventArgs<IPlugin> { Argument = plugin }, _logger);
  53. }
  54. #endregion
  55. #region PluginUpdated Event
  56. /// <summary>
  57. /// Occurs when [plugin updated].
  58. /// </summary>
  59. public event EventHandler<GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>>> PluginUpdated;
  60. /// <summary>
  61. /// Called when [plugin updated].
  62. /// </summary>
  63. /// <param name="plugin">The plugin.</param>
  64. /// <param name="newVersion">The new version.</param>
  65. private void OnPluginUpdated(IPlugin plugin, PackageVersionInfo newVersion)
  66. {
  67. _logger.Info("Plugin updated: {0} {1} {2}", newVersion.name, newVersion.versionStr ?? string.Empty, newVersion.classification);
  68. EventHelper.FireEventIfNotNull(PluginUpdated, this, new GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> { Argument = new Tuple<IPlugin, PackageVersionInfo>(plugin, newVersion) }, _logger);
  69. _applicationHost.NotifyPendingRestart();
  70. }
  71. #endregion
  72. #region PluginInstalled Event
  73. /// <summary>
  74. /// Occurs when [plugin updated].
  75. /// </summary>
  76. public event EventHandler<GenericEventArgs<PackageVersionInfo>> PluginInstalled;
  77. /// <summary>
  78. /// Called when [plugin installed].
  79. /// </summary>
  80. /// <param name="package">The package.</param>
  81. private void OnPluginInstalled(PackageVersionInfo package)
  82. {
  83. _logger.Info("New plugin installed: {0} {1} {2}", package.name, package.versionStr ?? string.Empty, package.classification);
  84. EventHelper.FireEventIfNotNull(PluginInstalled, this, new GenericEventArgs<PackageVersionInfo> { Argument = package }, _logger);
  85. _applicationHost.NotifyPendingRestart();
  86. }
  87. #endregion
  88. /// <summary>
  89. /// The _logger
  90. /// </summary>
  91. private readonly ILogger _logger;
  92. private readonly IApplicationPaths _appPaths;
  93. private readonly IHttpClient _httpClient;
  94. private readonly IJsonSerializer _jsonSerializer;
  95. private readonly ISecurityManager _securityManager;
  96. private readonly IConfigurationManager _config;
  97. private readonly IFileSystem _fileSystem;
  98. /// <summary>
  99. /// Gets the application host.
  100. /// </summary>
  101. /// <value>The application host.</value>
  102. private readonly IApplicationHost _applicationHost;
  103. public InstallationManager(ILogger logger, IApplicationHost appHost, IApplicationPaths appPaths, IHttpClient httpClient, IJsonSerializer jsonSerializer, ISecurityManager securityManager, IConfigurationManager config, IFileSystem fileSystem)
  104. {
  105. if (logger == null)
  106. {
  107. throw new ArgumentNullException("logger");
  108. }
  109. CurrentInstallations = new List<Tuple<InstallationInfo, CancellationTokenSource>>();
  110. CompletedInstallations = new ConcurrentBag<InstallationInfo>();
  111. _applicationHost = appHost;
  112. _appPaths = appPaths;
  113. _httpClient = httpClient;
  114. _jsonSerializer = jsonSerializer;
  115. _securityManager = securityManager;
  116. _config = config;
  117. _fileSystem = fileSystem;
  118. _logger = logger;
  119. }
  120. private Version GetPackageVersion(PackageVersionInfo version)
  121. {
  122. return new Version(ValueOrDefault(version.versionStr, "0.0.0.1"));
  123. }
  124. private static string ValueOrDefault(string str, string def)
  125. {
  126. return string.IsNullOrEmpty(str) ? def : str;
  127. }
  128. /// <summary>
  129. /// Gets all available packages.
  130. /// </summary>
  131. /// <param name="cancellationToken">The cancellation token.</param>
  132. /// <param name="withRegistration">if set to <c>true</c> [with registration].</param>
  133. /// <param name="packageType">Type of the package.</param>
  134. /// <param name="applicationVersion">The application version.</param>
  135. /// <returns>Task{List{PackageInfo}}.</returns>
  136. public async Task<IEnumerable<PackageInfo>> GetAvailablePackages(CancellationToken cancellationToken,
  137. bool withRegistration = true,
  138. PackageType? packageType = null,
  139. Version applicationVersion = null)
  140. {
  141. var data = new Dictionary<string, string>
  142. {
  143. { "key", _securityManager.SupporterKey },
  144. { "mac", _applicationHost.SystemId },
  145. { "systemid", _applicationHost.SystemId }
  146. };
  147. if (withRegistration)
  148. {
  149. using (var json = await _httpClient.Post(MbAdmin.HttpsUrl + "service/package/retrieveall", data, cancellationToken).ConfigureAwait(false))
  150. {
  151. cancellationToken.ThrowIfCancellationRequested();
  152. var packages = _jsonSerializer.DeserializeFromStream<List<PackageInfo>>(json).ToList();
  153. return FilterPackages(packages, packageType, applicationVersion);
  154. }
  155. }
  156. else
  157. {
  158. var packages = await GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
  159. return FilterPackages(packages.ToList(), packageType, applicationVersion);
  160. }
  161. }
  162. private Tuple<List<PackageInfo>, DateTime> _lastPackageListResult;
  163. /// <summary>
  164. /// Gets all available packages.
  165. /// </summary>
  166. /// <param name="cancellationToken">The cancellation token.</param>
  167. /// <returns>Task{List{PackageInfo}}.</returns>
  168. public async Task<IEnumerable<PackageInfo>> GetAvailablePackagesWithoutRegistrationInfo(CancellationToken cancellationToken)
  169. {
  170. if (_lastPackageListResult != null)
  171. {
  172. TimeSpan cacheLength;
  173. switch (_config.CommonConfiguration.SystemUpdateLevel)
  174. {
  175. case PackageVersionClass.Beta:
  176. cacheLength = TimeSpan.FromMinutes(30);
  177. break;
  178. case PackageVersionClass.Dev:
  179. cacheLength = TimeSpan.FromMinutes(3);
  180. break;
  181. default:
  182. cacheLength = TimeSpan.FromHours(24);
  183. break;
  184. }
  185. if ((DateTime.UtcNow - _lastPackageListResult.Item2) < cacheLength)
  186. {
  187. return _lastPackageListResult.Item1;
  188. }
  189. }
  190. using (var json = await _httpClient.Get(MbAdmin.HttpUrl + "service/MB3Packages.json", cancellationToken).ConfigureAwait(false))
  191. {
  192. cancellationToken.ThrowIfCancellationRequested();
  193. var packages = _jsonSerializer.DeserializeFromStream<List<PackageInfo>>(json).ToList();
  194. packages = FilterPackages(packages).ToList();
  195. _lastPackageListResult = new Tuple<List<PackageInfo>, DateTime>(packages, DateTime.UtcNow);
  196. return _lastPackageListResult.Item1;
  197. }
  198. }
  199. protected IEnumerable<PackageInfo> FilterPackages(List<PackageInfo> packages)
  200. {
  201. foreach (var package in packages)
  202. {
  203. package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl))
  204. .OrderByDescending(GetPackageVersion).ToList();
  205. }
  206. // Remove packages with no versions
  207. packages = packages.Where(p => p.versions.Any()).ToList();
  208. return packages;
  209. }
  210. protected IEnumerable<PackageInfo> FilterPackages(List<PackageInfo> packages, PackageType? packageType, Version applicationVersion)
  211. {
  212. foreach (var package in packages)
  213. {
  214. package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl))
  215. .OrderByDescending(GetPackageVersion).ToList();
  216. }
  217. if (packageType.HasValue)
  218. {
  219. packages = packages.Where(p => p.type == packageType.Value).ToList();
  220. }
  221. // If an app version was supplied, filter the versions for each package to only include supported versions
  222. if (applicationVersion != null)
  223. {
  224. foreach (var package in packages)
  225. {
  226. package.versions = package.versions.Where(v => IsPackageVersionUpToDate(v, applicationVersion)).ToList();
  227. }
  228. }
  229. // Remove packages with no versions
  230. packages = packages.Where(p => p.versions.Any()).ToList();
  231. return packages;
  232. }
  233. /// <summary>
  234. /// Determines whether [is package version up to date] [the specified package version info].
  235. /// </summary>
  236. /// <param name="packageVersionInfo">The package version info.</param>
  237. /// <param name="currentServerVersion">The current server version.</param>
  238. /// <returns><c>true</c> if [is package version up to date] [the specified package version info]; otherwise, <c>false</c>.</returns>
  239. private bool IsPackageVersionUpToDate(PackageVersionInfo packageVersionInfo, Version currentServerVersion)
  240. {
  241. if (string.IsNullOrEmpty(packageVersionInfo.requiredVersionStr))
  242. {
  243. return true;
  244. }
  245. Version requiredVersion;
  246. return Version.TryParse(packageVersionInfo.requiredVersionStr, out requiredVersion) && currentServerVersion >= requiredVersion;
  247. }
  248. /// <summary>
  249. /// Gets the package.
  250. /// </summary>
  251. /// <param name="name">The name.</param>
  252. /// <param name="guid">The assembly guid</param>
  253. /// <param name="classification">The classification.</param>
  254. /// <param name="version">The version.</param>
  255. /// <returns>Task{PackageVersionInfo}.</returns>
  256. public async Task<PackageVersionInfo> GetPackage(string name, string guid, PackageVersionClass classification, Version version)
  257. {
  258. var packages = await GetAvailablePackages(CancellationToken.None).ConfigureAwait(false);
  259. var package = packages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
  260. ?? packages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
  261. if (package == null)
  262. {
  263. return null;
  264. }
  265. return package.versions.FirstOrDefault(v => GetPackageVersion(v).Equals(version) && v.classification == classification);
  266. }
  267. /// <summary>
  268. /// Gets the latest compatible version.
  269. /// </summary>
  270. /// <param name="name">The name.</param>
  271. /// <param name="guid">The assembly guid if this is a plug-in</param>
  272. /// <param name="currentServerVersion">The current server version.</param>
  273. /// <param name="classification">The classification.</param>
  274. /// <returns>Task{PackageVersionInfo}.</returns>
  275. public async Task<PackageVersionInfo> GetLatestCompatibleVersion(string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
  276. {
  277. var packages = await GetAvailablePackages(CancellationToken.None).ConfigureAwait(false);
  278. return GetLatestCompatibleVersion(packages, name, guid, currentServerVersion, classification);
  279. }
  280. /// <summary>
  281. /// Gets the latest compatible version.
  282. /// </summary>
  283. /// <param name="availablePackages">The available packages.</param>
  284. /// <param name="name">The name.</param>
  285. /// <param name="currentServerVersion">The current server version.</param>
  286. /// <param name="classification">The classification.</param>
  287. /// <returns>PackageVersionInfo.</returns>
  288. public PackageVersionInfo GetLatestCompatibleVersion(IEnumerable<PackageInfo> availablePackages, string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
  289. {
  290. var package = availablePackages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
  291. ?? availablePackages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
  292. if (package == null)
  293. {
  294. return null;
  295. }
  296. return package.versions
  297. .OrderByDescending(GetPackageVersion)
  298. .FirstOrDefault(v => v.classification <= classification && IsPackageVersionUpToDate(v, currentServerVersion));
  299. }
  300. /// <summary>
  301. /// Gets the available plugin updates.
  302. /// </summary>
  303. /// <param name="applicationVersion">The current server version.</param>
  304. /// <param name="withAutoUpdateEnabled">if set to <c>true</c> [with auto update enabled].</param>
  305. /// <param name="cancellationToken">The cancellation token.</param>
  306. /// <returns>Task{IEnumerable{PackageVersionInfo}}.</returns>
  307. public async Task<IEnumerable<PackageVersionInfo>> GetAvailablePluginUpdates(Version applicationVersion, bool withAutoUpdateEnabled, CancellationToken cancellationToken)
  308. {
  309. var catalog = await GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
  310. var plugins = _applicationHost.Plugins.ToList();
  311. if (withAutoUpdateEnabled)
  312. {
  313. plugins = plugins
  314. .Where(p => _config.CommonConfiguration.EnableAutoUpdate)
  315. .ToList();
  316. }
  317. // Figure out what needs to be installed
  318. var packages = plugins.Select(p =>
  319. {
  320. var latestPluginInfo = GetLatestCompatibleVersion(catalog, p.Name, p.Id.ToString(), applicationVersion, _config.CommonConfiguration.SystemUpdateLevel);
  321. return latestPluginInfo != null && GetPackageVersion(latestPluginInfo) > p.Version ? latestPluginInfo : null;
  322. }).Where(i => i != null).ToList();
  323. return packages
  324. .Where(p => !string.IsNullOrWhiteSpace(p.sourceUrl) && !CompletedInstallations.Any(i => string.Equals(i.AssemblyGuid, p.guid, StringComparison.OrdinalIgnoreCase)));
  325. }
  326. /// <summary>
  327. /// Installs the package.
  328. /// </summary>
  329. /// <param name="package">The package.</param>
  330. /// <param name="progress">The progress.</param>
  331. /// <param name="cancellationToken">The cancellation token.</param>
  332. /// <returns>Task.</returns>
  333. /// <exception cref="System.ArgumentNullException">package</exception>
  334. public async Task InstallPackage(PackageVersionInfo package, IProgress<double> progress, CancellationToken cancellationToken)
  335. {
  336. if (package == null)
  337. {
  338. throw new ArgumentNullException("package");
  339. }
  340. if (progress == null)
  341. {
  342. throw new ArgumentNullException("progress");
  343. }
  344. var installationInfo = new InstallationInfo
  345. {
  346. Id = Guid.NewGuid().ToString("N"),
  347. Name = package.name,
  348. AssemblyGuid = package.guid,
  349. UpdateClass = package.classification,
  350. Version = package.versionStr
  351. };
  352. var innerCancellationTokenSource = new CancellationTokenSource();
  353. var tuple = new Tuple<InstallationInfo, CancellationTokenSource>(installationInfo, innerCancellationTokenSource);
  354. // Add it to the in-progress list
  355. lock (CurrentInstallations)
  356. {
  357. CurrentInstallations.Add(tuple);
  358. }
  359. var innerProgress = new ActionableProgress<double>();
  360. // Whenever the progress updates, update the outer progress object and InstallationInfo
  361. innerProgress.RegisterAction(percent =>
  362. {
  363. progress.Report(percent);
  364. installationInfo.PercentComplete = percent;
  365. });
  366. var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, innerCancellationTokenSource.Token).Token;
  367. var installationEventArgs = new InstallationEventArgs
  368. {
  369. InstallationInfo = installationInfo,
  370. PackageVersionInfo = package
  371. };
  372. EventHelper.FireEventIfNotNull(PackageInstalling, this, installationEventArgs, _logger);
  373. try
  374. {
  375. await InstallPackageInternal(package, innerProgress, linkedToken).ConfigureAwait(false);
  376. lock (CurrentInstallations)
  377. {
  378. CurrentInstallations.Remove(tuple);
  379. }
  380. progress.Report(100);
  381. CompletedInstallations.Add(installationInfo);
  382. EventHelper.FireEventIfNotNull(PackageInstallationCompleted, this, installationEventArgs, _logger);
  383. }
  384. catch (OperationCanceledException)
  385. {
  386. lock (CurrentInstallations)
  387. {
  388. CurrentInstallations.Remove(tuple);
  389. }
  390. _logger.Info("Package installation cancelled: {0} {1}", package.name, package.versionStr);
  391. EventHelper.FireEventIfNotNull(PackageInstallationCancelled, this, installationEventArgs, _logger);
  392. throw;
  393. }
  394. catch (Exception ex)
  395. {
  396. _logger.ErrorException("Package installation failed", ex);
  397. lock (CurrentInstallations)
  398. {
  399. CurrentInstallations.Remove(tuple);
  400. }
  401. EventHelper.FireEventIfNotNull(PackageInstallationFailed, this, new InstallationFailedEventArgs
  402. {
  403. InstallationInfo = installationInfo,
  404. Exception = ex
  405. }, _logger);
  406. throw;
  407. }
  408. finally
  409. {
  410. // Dispose the progress object and remove the installation from the in-progress list
  411. innerProgress.Dispose();
  412. tuple.Item2.Dispose();
  413. }
  414. }
  415. /// <summary>
  416. /// Installs the package internal.
  417. /// </summary>
  418. /// <param name="package">The package.</param>
  419. /// <param name="progress">The progress.</param>
  420. /// <param name="cancellationToken">The cancellation token.</param>
  421. /// <returns>Task.</returns>
  422. private async Task InstallPackageInternal(PackageVersionInfo package, IProgress<double> progress, CancellationToken cancellationToken)
  423. {
  424. // Do the install
  425. await PerformPackageInstallation(progress, package, cancellationToken).ConfigureAwait(false);
  426. var extension = Path.GetExtension(package.targetFilename) ?? "";
  427. // Do plugin-specific processing
  428. if (!string.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase) && !string.Equals(extension, ".rar", StringComparison.OrdinalIgnoreCase) && !string.Equals(extension, ".7z", StringComparison.OrdinalIgnoreCase))
  429. {
  430. // Set last update time if we were installed before
  431. var plugin = _applicationHost.Plugins.FirstOrDefault(p => string.Equals(p.Id.ToString(), package.guid, StringComparison.OrdinalIgnoreCase))
  432. ?? _applicationHost.Plugins.FirstOrDefault(p => p.Name.Equals(package.name, StringComparison.OrdinalIgnoreCase));
  433. if (plugin != null)
  434. {
  435. OnPluginUpdated(plugin, package);
  436. }
  437. else
  438. {
  439. OnPluginInstalled(package);
  440. }
  441. }
  442. }
  443. private async Task PerformPackageInstallation(IProgress<double> progress, PackageVersionInfo package, CancellationToken cancellationToken)
  444. {
  445. // Target based on if it is an archive or single assembly
  446. // zip archives are assumed to contain directory structures relative to our ProgramDataPath
  447. var extension = Path.GetExtension(package.targetFilename);
  448. var isArchive = string.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".rar", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".7z", StringComparison.OrdinalIgnoreCase);
  449. var target = Path.Combine(isArchive ? _appPaths.TempUpdatePath : _appPaths.PluginsPath, package.targetFilename);
  450. // Download to temporary file so that, if interrupted, it won't destroy the existing installation
  451. var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
  452. {
  453. Url = package.sourceUrl,
  454. CancellationToken = cancellationToken,
  455. Progress = progress
  456. }).ConfigureAwait(false);
  457. cancellationToken.ThrowIfCancellationRequested();
  458. // Validate with a checksum
  459. var packageChecksum = string.IsNullOrWhiteSpace(package.checksum) ? Guid.Empty : new Guid(package.checksum);
  460. if (packageChecksum != Guid.Empty) // support for legacy uploads for now
  461. {
  462. using (var crypto = new MD5CryptoServiceProvider())
  463. using (var stream = new BufferedStream(_fileSystem.OpenRead(tempFile), 100000))
  464. {
  465. var check = Guid.Parse(BitConverter.ToString(crypto.ComputeHash(stream)).Replace("-", String.Empty));
  466. if (check != packageChecksum)
  467. {
  468. throw new ApplicationException(string.Format("Download validation failed for {0}. Probably corrupted during transfer.", package.name));
  469. }
  470. }
  471. }
  472. cancellationToken.ThrowIfCancellationRequested();
  473. // Success - move it to the real target
  474. try
  475. {
  476. _fileSystem.CreateDirectory(Path.GetDirectoryName(target));
  477. _fileSystem.CopyFile(tempFile, target, true);
  478. //If it is an archive - write out a version file so we know what it is
  479. if (isArchive)
  480. {
  481. _fileSystem.WriteAllText(target + ".ver", package.versionStr);
  482. }
  483. }
  484. catch (IOException e)
  485. {
  486. _logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target);
  487. throw;
  488. }
  489. try
  490. {
  491. _fileSystem.DeleteFile(tempFile);
  492. }
  493. catch (IOException e)
  494. {
  495. // Don't fail because of this
  496. _logger.ErrorException("Error deleting temp file {0]", e, tempFile);
  497. }
  498. }
  499. /// <summary>
  500. /// Uninstalls a plugin
  501. /// </summary>
  502. /// <param name="plugin">The plugin.</param>
  503. /// <exception cref="System.ArgumentException"></exception>
  504. public void UninstallPlugin(IPlugin plugin)
  505. {
  506. plugin.OnUninstalling();
  507. // Remove it the quick way for now
  508. _applicationHost.RemovePlugin(plugin);
  509. _fileSystem.DeleteFile(plugin.AssemblyFilePath);
  510. OnPluginUninstalled(plugin);
  511. _applicationHost.NotifyPendingRestart();
  512. }
  513. /// <summary>
  514. /// Releases unmanaged and - optionally - managed resources.
  515. /// </summary>
  516. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  517. protected virtual void Dispose(bool dispose)
  518. {
  519. if (dispose)
  520. {
  521. lock (CurrentInstallations)
  522. {
  523. foreach (var tuple in CurrentInstallations)
  524. {
  525. tuple.Item2.Dispose();
  526. }
  527. CurrentInstallations.Clear();
  528. }
  529. }
  530. }
  531. public void Dispose()
  532. {
  533. Dispose(true);
  534. }
  535. }
  536. }