replaced our zip library so we can validate update package before applying.

This commit is contained in:
Keivan Beigi 2013-07-18 16:36:49 -07:00
parent 8f0d3e2e3b
commit 635e206e03
11 changed files with 82 additions and 75 deletions

View File

@ -1,23 +1,79 @@
using System.Linq;
using Ionic.Zip;
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using NLog;
namespace NzbDrone.Common
{
public class ArchiveProvider
public interface IArchiveService
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
void Extract(string compressedFile, string destination);
}
public virtual void ExtractArchive(string compressedFile, string destination)
public class ArchiveService : IArchiveService
{
private readonly Logger _logger;
public ArchiveService(Logger logger)
{
logger.Trace("Extracting archive [{0}] to [{1}]", compressedFile, destination);
_logger = logger;
}
using (var zipFile = ZipFile.Read(compressedFile))
public void Extract(string compressedFile, string destination)
{
_logger.Trace("Extracting archive [{0}] to [{1}]", compressedFile, destination);
using (var fileStream = File.OpenRead(compressedFile))
{
zipFile.ExtractAll(destination);
var zipFile = new ZipFile(fileStream);
_logger.Debug("Validating Archive {0}", compressedFile);
if (!zipFile.TestArchive(true, TestStrategy.FindFirstError, OnZipError))
{
throw new IOException(string.Format("File {0} failed archive validation.", compressedFile));
}
foreach (ZipEntry zipEntry in zipFile)
{
if (!zipEntry.IsFile)
{
continue; // Ignore directories
}
String entryFileName = zipEntry.Name;
// to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
// Optionally match entrynames against a selection list here to skip as desired.
// The unpacked length is available in the zipEntry.Size property.
byte[] buffer = new byte[4096]; // 4K is optimum
Stream zipStream = zipFile.GetInputStream(zipEntry);
// Manipulate the output filename here as desired.
String fullZipToPath = Path.Combine(destination, entryFileName);
string directoryName = Path.GetDirectoryName(fullZipToPath);
if (directoryName.Length > 0)
Directory.CreateDirectory(directoryName);
// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
// of the file, but does not waste memory.
// The "using" will close the stream even if an exception occurs.
using (FileStream streamWriter = File.Create(fullZipToPath))
{
StreamUtils.Copy(zipStream, streamWriter, buffer);
}
}
}
logger.Trace("Extraction complete.");
_logger.Trace("Extraction complete.");
}
private void OnZipError(TestStatus status, string message)
{
if (!string.IsNullOrWhiteSpace(message))
{
_logger.Error("File {0} failed zip validation. {1}", status.File.Name, message);
}
}
}
}

View File

@ -55,8 +55,8 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Ionic.Zip">
<HintPath>..\packages\DotNetZip.1.9.1.8\lib\net20\Ionic.Zip.dll</HintPath>
<Reference Include="ICSharpCode.SharpZipLib">
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="Loggly, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.9.1.8" targetFramework="net40" />
<package id="loggly-csharp" version="2.2" targetFramework="net40" />
<package id="Newtonsoft.Json" version="5.0.3" targetFramework="net35" />
<package id="NLog" version="2.0.1.2" targetFramework="net40" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net40" />
</packages>

View File

@ -196,7 +196,7 @@
<Compile Include="DecisionEngineTests\QualityUpgradableSpecificationFixture.cs" />
<Compile Include="ProviderTests\DiskProviderTests\FreeDiskSpaceTest.cs" />
<Compile Include="NotificationTests\ProwlProviderTest.cs" />
<Compile Include="ProviderTests\DiskProviderTests\ExtractArchiveFixture.cs" />
<Compile Include="ProviderTests\DiskProviderTests\ArchiveProviderFixture.cs" />
<Compile Include="MediaFileTests\DropFolderImportServiceFixture.cs" />
<Compile Include="SeriesStatsTests\SeriesStatisticsFixture.cs" />
<Compile Include="TvTests\SeriesRepositoryTests\QualityProfileRepositoryFixture.cs" />

View File

@ -1,19 +1,19 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Test.Framework;
using System.IO;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
{
[TestFixture]
public class ExtractArchiveFixture : CoreTest
public class ArchiveProviderFixture : TestBase<ArchiveService>
{
[Test]
public void Should_extract_to_correct_folder()
{
var destination = Path.Combine(TempFolder, "destination");
Mocker.Resolve<ArchiveProvider>().ExtractArchive(GetTestFilePath("TestArchive.zip"), destination);
Subject.Extract(GetTestFilePath("TestArchive.zip"), destination);
var destinationFolder = new DirectoryInfo(destination);

View File

@ -22,9 +22,9 @@ namespace NzbDrone.Core.Test.UpdateTests
private readonly UpdatePackage _updatePackage = new UpdatePackage
{
FileName = "NzbDrone.vnext.0.8.1.226.zip",
Url = "http://update.nzbdrone.com/vnext/NzbDrone.vnext.0.8.1.226.zip",
Version = new Version("0.8.1.226")
FileName = "NzbDrone.vnext.0.8.1.385.zip",
Url = "http://update.nzbdrone.com/vnext/NzbDrone.vnext.0.8.1.385.zip",
Version = new Version("0.8.1.385")
};
[SetUp]
@ -83,7 +83,7 @@ namespace NzbDrone.Core.Test.UpdateTests
Subject.Execute(new ApplicationUpdateCommand());
Mocker.GetMock<ArchiveProvider>().Verify(c => c.ExtractArchive(updateArchive, _sandboxFolder));
Mocker.GetMock<IArchiveService>().Verify(c => c.Extract(updateArchive, _sandboxFolder));
}
[Test]
@ -131,7 +131,7 @@ namespace NzbDrone.Core.Test.UpdateTests
updateSubFolder.Exists.Should().BeFalse();
Mocker.Resolve<DiskProvider>();
Mocker.Resolve<ArchiveProvider>();
Mocker.SetConstant<IArchiveService>(Mocker.Resolve<ArchiveService>());
Subject.Execute(new ApplicationUpdateCommand());

View File

@ -136,9 +136,6 @@
<Reference Include="Growl.CoreLibrary">
<HintPath>..\packages\Growl.0.6\lib\Growl.CoreLibrary.dll</HintPath>
</Reference>
<Reference Include="Ionic.Zip">
<HintPath>..\packages\DotNetZip.1.9.1.8\lib\net20\Ionic.Zip.dll</HintPath>
</Reference>
<Reference Include="MediaInfoDotNet">
<HintPath>..\packages\MediaInfoNet.0.3\lib\MediaInfoDotNet.dll</HintPath>
</Reference>
@ -431,9 +428,6 @@
<Compile Include="Providers\XemProvider.cs" />
<Compile Include="Qualities\Quality.cs" />
<Compile Include="Tv\Season.cs" />
<Compile Include="Providers\BackupProvider.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Configuration\ConfigService.cs">
<SubType>Code</SubType>
</Compile>

View File

@ -1,36 +0,0 @@
using System;
using System.Linq;
using Ionic.Zip;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
namespace NzbDrone.Core.Providers
{
public class BackupProvider
{
private readonly IAppFolderInfo _appFolderInfo;
public BackupProvider(IAppFolderInfo appFolderInfo)
{
_appFolderInfo = appFolderInfo;
}
public BackupProvider()
{
}
public virtual string CreateBackupZip()
{
var configFile = _appFolderInfo.GetConfigPath();
var zipFile = _appFolderInfo.GetConfigBackupFile();
using (var zip = new ZipFile())
{
zip.AddFile(configFile, String.Empty);
zip.Save(zipFile);
}
return zipFile;
}
}
}

View File

@ -16,19 +16,19 @@ namespace NzbDrone.Core.Update
private readonly IDiskProvider _diskProvider;
private readonly IHttpProvider _httpProvider;
private readonly ArchiveProvider _archiveProvider;
private readonly IArchiveService _archiveService;
private readonly IProcessProvider _processProvider;
public InstallUpdateService(ICheckUpdateService checkUpdateService, IAppFolderInfo appFolderInfo,
IDiskProvider diskProvider, IHttpProvider httpProvider,
ArchiveProvider archiveProvider, IProcessProvider processProvider, Logger logger)
IArchiveService archiveService, IProcessProvider processProvider, Logger logger)
{
_checkUpdateService = checkUpdateService;
_appFolderInfo = appFolderInfo;
_diskProvider = diskProvider;
_httpProvider = httpProvider;
_archiveProvider = archiveProvider;
_archiveService = archiveService;
_processProvider = processProvider;
_logger = logger;
}
@ -60,7 +60,7 @@ namespace NzbDrone.Core.Update
_logger.Info("Download completed for update package from [{0}]", updatePackage.FileName);
_logger.Info("Extracting Update package");
_archiveProvider.ExtractArchive(packageDestination, updateSandboxFolder);
_archiveService.Extract(packageDestination, updateSandboxFolder);
_logger.Info("Update package extracted successfully");
_logger.Info("Preparing client");

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.9.1.8" />
<package id="FluentMigrator" version="1.1.0.0" targetFramework="net40" />
<package id="Growl" version="0.6" />
<package id="MediaInfoNet" version="0.3" targetFramework="net40" />

View File

@ -1,5 +1,4 @@
using System;
using System.Linq;
using NLog;
using NzbDrone.Common;
using IServiceProvider = NzbDrone.Common.IServiceProvider;
@ -45,13 +44,8 @@ namespace NzbDrone.Update.UpdateEngine
_logger.Info("Killing all running processes");
if (_processProvider.GetProcessByName(ProcessProvider.NzbDroneConsoleProcessName).Any())
{
_processProvider.KillAll(ProcessProvider.NzbDroneConsoleProcessName);
}
_processProvider.KillAll(ProcessProvider.NzbDroneConsoleProcessName);
_processProvider.KillAll(ProcessProvider.NzbDroneProcessName);
}
}
}