InstallationManager.cs 26 KB

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