public void BuildDeltaInvalidTransactionsBasedOnLockTime() { var invalidTransactionList = Enumerable.Range(0, 20).Select(i => { var transaction = TransactionHelper.GetPublicTransaction( transactionFees: 954, timestamp: 157, signature: i.ToString()); transaction.ConfidentialEntries.Add(new ConfidentialEntry { Base = new BaseEntry { ReceiverPublicKey = "this entry makes the transaction invalid".ToUtf8ByteString() } }); return(transaction); }).ToList(); var transactionRetriever = Substitute.For <IDeltaTransactionRetriever>(); transactionRetriever.GetMempoolTransactionsByPriority().Returns(invalidTransactionList); var deltaBuilder = new DeltaBuilder(transactionRetriever, _randomFactory, _hashProvider, _peerSettings, _cache, _dateTimeProvider, _logger); var candidate = deltaBuilder.BuildCandidateDelta(_previousDeltaHash); ValidateDeltaCandidate(candidate, _zeroCoinbaseEntry.ToByteArray()); _cache.Received(1).AddLocalDelta(Arg.Is(candidate), Arg.Any <Delta>()); }
public static bool CreateDelta(Stream contents, PackageSignatureResource signatureResult, string deltaTempFile) { Logger.Info($"Calculating delta"); var deltaBuilder = new DeltaBuilder(); using (var signature = new MemoryStream(signatureResult.Signature)) using (var deltaStream = File.Open(deltaTempFile, FileMode.Create, FileAccess.ReadWrite)) { deltaBuilder.BuildDelta( contents, new SignatureReader(signature, new NullProgressReporter()), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream)) ); } var originalFileSize = contents.Length; var deltaFileSize = new FileInfo(deltaTempFile).Length; var ratio = deltaFileSize / (double)originalFileSize; if (ratio > 0.95) { Logger.Info($"The delta file ({deltaFileSize:n0} bytes) is more than 95% the size of the original file ({originalFileSize:n0} bytes)"); return(false); } Logger.Info($"The delta file ({deltaFileSize:n0} bytes) is {ratio:p2} the size of the original file ({originalFileSize:n0} bytes), uploading..."); return(true); }
public static void Create(Stream oldData, FileStream newData, FileStream signature, FileStream output) { CreateSignature(oldData, signature); var db = new DeltaBuilder { ProgressReporter = reporter }; db.BuildDelta(newData, new SignatureReader(signature, reporter), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(output))); }
public void BuildDeltaCheckForAccuracy() { var transactions = Enumerable.Range(0, 20).Select(i => { var transaction = TransactionHelper.GetPublicTransaction( amount: (uint)i, receiverPublicKey: i.ToString(), transactionFees: (ulong)_random.Next(), timestamp: _random.Next(), signature: i.ToString()); return(transaction); }).ToList(); var transactionRetriever = Substitute.For <IDeltaTransactionRetriever>(); transactionRetriever.GetMempoolTransactionsByPriority().Returns(transactions); var selectedTransactions = transactions.Where(t => t.IsPublicTransaction && t.HasValidEntries()).ToArray(); var expectedCoinBase = new CoinbaseEntry { Amount = selectedTransactions.Sum(t => t.SummedEntryFees()).ToUint256ByteString(), ReceiverPublicKey = _producerId.PublicKey.ToByteString() }; var salt = BitConverter.GetBytes( _randomFactory.GetDeterministicRandomFromSeed(_previousDeltaHash.ToArray()).NextInt()); var rawAndSaltedEntriesBySignature = selectedTransactions.SelectMany( t => t.PublicEntries.Select(e => new { RawEntry = e, SaltedAndHashedEntry = _hashProvider.ComputeMultiHash(e.ToByteArray().Concat(salt)) })); var shuffledEntriesBytes = rawAndSaltedEntriesBySignature .OrderBy(v => v.SaltedAndHashedEntry.ToArray(), ByteUtil.ByteListComparer.Default) .SelectMany(v => v.RawEntry.ToByteArray()) .ToArray(); var signaturesInOrder = selectedTransactions .Select(p => p.Signature.ToByteArray()) .OrderBy(s => s, ByteUtil.ByteListComparer.Default) .SelectMany(b => b) .ToArray(); var expectedBytesToHash = shuffledEntriesBytes.Concat(signaturesInOrder) .Concat(expectedCoinBase.ToByteArray()).ToArray(); var deltaBuilder = new DeltaBuilder(transactionRetriever, _randomFactory, _hashProvider, _peerSettings, _cache, _dateTimeProvider, _logger); var candidate = deltaBuilder.BuildCandidateDelta(_previousDeltaHash); ValidateDeltaCandidate(candidate, expectedBytesToHash); _cache.Received(1).AddLocalDelta(Arg.Is(candidate), Arg.Any <Delta>()); }
public int Execute(string[] commandLineArguments) { options.Parse(commandLineArguments); if (string.IsNullOrWhiteSpace(signatureFilePath)) { throw new OptionException("No signature file was specified", "new-file"); } if (string.IsNullOrWhiteSpace(newFilePath)) { throw new OptionException("No new file was specified", "new-file"); } newFilePath = Path.GetFullPath(newFilePath); signatureFilePath = Path.GetFullPath(signatureFilePath); var delta = new DeltaBuilder(); foreach (var config in configuration) { config(delta); } if (!File.Exists(signatureFilePath)) { throw new FileNotFoundException("File not found: " + signatureFilePath, signatureFilePath); } if (!File.Exists(newFilePath)) { throw new FileNotFoundException("File not found: " + newFilePath, newFilePath); } if (string.IsNullOrWhiteSpace(deltaFilePath)) { deltaFilePath = newFilePath + ".octodelta"; } else { deltaFilePath = Path.GetFullPath(deltaFilePath); var directory = Path.GetDirectoryName(deltaFilePath); if (directory != null && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } } using (var newFileStream = new FileStream(newFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var signatureStream = new FileStream(signatureFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var deltaStream = new FileStream(deltaFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { delta.BuildDeltaAsync(newFileStream, new SignatureReader(signatureStream, delta.ProgressReport), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))).GetAwaiter().GetResult(); } return(0); }
public static void CreateDelta(string deltaFilePath, string oldFileSignaturePath, string newFilePath) { var deltaBuilder = new DeltaBuilder(); using (var newFileStream = new FileStream(newFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var signatureFileStream = new FileStream(oldFileSignaturePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var deltaStream = new FileStream(deltaFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { deltaBuilder.BuildDelta(newFileStream, new SignatureReader(signatureFileStream, new ProgReport()), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); } }
public static void Create(byte[] oldData, byte[] newData, Stream output) { using var signature = CreateSignature(oldData); using var oldStream = new MemoryStream(oldData); using var newStream = new MemoryStream(newData); var db = new DeltaBuilder { ProgressReporter = reporter }; db.BuildDelta(newStream, new SignatureReader(signature, reporter), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(output))); }
public void When_contract_entries_exceed_delta_gas_limit_some_entries_are_ignored() { // each entry at 1 million gas // so only 8 entries allowed var transactions = Enumerable.Range(0, 20).Select(i => { var transaction = TransactionHelper.GetContractTransaction(ByteString.Empty, UInt256.Zero, i > 10 ? (uint)DeltaGasLimit / 8U - 10000U : 70000U, // to test scenarios when both single transaction is ignored and all remaining (20 + i).GFul(), Bytes.Empty, receiverPublicKey: i.ToString(), transactionFees: (ulong)_random.Next(), timestamp: _random.Next(), signature: i.ToString()); return(transaction.PublicEntry); }).ToList(); var transactionRetriever = BuildRetriever(transactions); var expectedSelectedTransactions = BuildSelectedTransactions(transactions.Skip(10).Take(1).Union(transactions.Skip(12).Take(8)).ToList()); var salt = BitConverter.GetBytes( _randomFactory.GetDeterministicRandomFromSeed(_previousDeltaHash.ToArray()).NextInt()); var rawAndSaltedEntriesBySignature = expectedSelectedTransactions.Select(e => { var contractEntriesProtoBuff = e; return(new { RawEntry = contractEntriesProtoBuff, SaltedAndHashedEntry = _hashProvider.ComputeMultiHash(contractEntriesProtoBuff.ToByteArray().Concat(salt)) }); }).ToArray(); var shuffledEntriesBytes = rawAndSaltedEntriesBySignature .OrderBy(v => v.SaltedAndHashedEntry.ToArray(), ByteUtil.ByteListComparer.Default) .SelectMany(v => v.RawEntry.ToByteArray()) .ToArray(); var expectedBytesToHash = BuildExpectedBytesToHash(expectedSelectedTransactions, shuffledEntriesBytes); var deltaBuilder = new DeltaBuilder(transactionRetriever, _randomFactory, _hashProvider, _peerSettings, _cache, _dateTimeProvider, _logger); var candidate = deltaBuilder.BuildCandidateDelta(_previousDeltaHash); ValidateDeltaCandidate(candidate, expectedBytesToHash); _cache.Received(1).AddLocalDelta(Arg.Is(candidate), Arg.Any <Delta>()); }
public void BuildDeltaEmptyPoolContent() { var transactionRetriever = Substitute.For <IDeltaTransactionRetriever>(); transactionRetriever.GetMempoolTransactionsByPriority() .Returns(new List <TransactionBroadcast>()); var deltaBuilder = new DeltaBuilder(transactionRetriever, _randomFactory, _hashProvider, _peerSettings, _cache, _dateTimeProvider, _logger); var candidate = deltaBuilder.BuildCandidateDelta(_previousDeltaHash); ValidateDeltaCandidate(candidate, _zeroCoinbaseEntry.ToByteArray()); _cache.Received(1).AddLocalDelta(Arg.Is(candidate), Arg.Any <Delta>()); }
public void BuildDeltaWithContractEntries() { var transactions = Enumerable.Range(0, 20).Select(i => { var transaction = TransactionHelper.GetContractTransaction(ByteString.Empty, UInt256.Zero, 21000, (20 + i).GFul(), Bytes.Empty, receiverPublicKey: i.ToString(), transactionFees: (ulong)_random.Next(), timestamp: _random.Next(), signature: i.ToString()); return(transaction.PublicEntry); }).ToList(); var transactionRetriever = BuildRetriever(transactions); var selectedTransactions = BuildSelectedTransactions(transactions); var salt = BitConverter.GetBytes( _randomFactory.GetDeterministicRandomFromSeed(_previousDeltaHash.ToArray()).NextInt()); var rawAndSaltedEntriesBySignature = selectedTransactions.Select(e => { var contractEntriesProtoBuff = e; return(new { RawEntry = contractEntriesProtoBuff, SaltedAndHashedEntry = _hashProvider.ComputeMultiHash(contractEntriesProtoBuff.ToByteArray().Concat(salt)) }); }); var shuffledEntriesBytes = rawAndSaltedEntriesBySignature .OrderBy(v => v.SaltedAndHashedEntry.ToArray(), ByteUtil.ByteListComparer.Default) .SelectMany(v => v.RawEntry.ToByteArray()) .ToArray(); var expectedBytesToHash = BuildExpectedBytesToHash(selectedTransactions, shuffledEntriesBytes); var deltaBuilder = new DeltaBuilder(transactionRetriever, _randomFactory, _hashProvider, _peerSettings, _cache, _dateTimeProvider, _logger); var candidate = deltaBuilder.BuildCandidateDelta(_previousDeltaHash); ValidateDeltaCandidate(candidate, expectedBytesToHash); _cache.Received(1).AddLocalDelta(Arg.Is(candidate), Arg.Any <Delta>()); }
public static void CreateDeltaFile(string fileName) { var deltaBuilder = new DeltaBuilder(); using (var signatureStream = new MemoryStream(CND.Program.GetFile(fileName + ".octosig"))) using (var newFileStream = new MemoryStream(File.ReadAllBytes(@"C:\Users\Peter\Desktop\Octodiff test\TestProject\AdminClient\Game\SwaggerGame.txt"))) using (var deltaStream = new MemoryStream()) { deltaBuilder.BuildDelta(newFileStream, new SignatureReader(signatureStream, new ConsoleProgressReporter()), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); var newSig = CreateSignature(newFileStream); //CND.Program.SaveFile($@"{fileName}.octosig", newSig); CND.Program.SaveFile($@"{fileName}.octodelta", deltaStream.ToArray()); // return deltaStream.ToArray(); } }
/// <summary> /// TODO: description /// </summary> /// <param name="sourcePath"></param> /// <param name="targetPath"></param> /// <param name="deltaPath"></param> public void Create(string sourcePath, string targetPath, string deltaPath) { Logger.Info("Creating Octodiff patch at {0} from {1} to {2}", deltaPath, sourcePath, targetPath); using (Stream orig = File.OpenRead(sourcePath)) using (Stream target = File.OpenRead(targetPath)) using (Stream patch = File.OpenWrite(deltaPath)) using (MemoryStream sig = new MemoryStream()) { new SignatureBuilder().Build(orig, new SignatureWriter(sig)); sig.Position = 0; sig.Flush(); DeltaBuilder b = new DeltaBuilder(); b.BuildDelta(target, new SignatureReader(sig, b.ProgressReporter), new BinaryDeltaWriter(patch)); } }
public static byte[] GetDeltaFile() { var newestFile = File.ReadAllBytes(@"C:\Users\Peter\Desktop\Octodiff test\TestProject\CND\Games\SwaggerGamev2.txt"); var signatureFile = File.ReadAllBytes( @"C:\Users\Peter\Desktop\Octodiff test\TestProject\Server\Sig\SwaggerGamev3.txt.octosig"); var deltaBuilder = new DeltaBuilder(); using (var signatureStream = new MemoryStream(signatureFile)) using (var newFileStream = new MemoryStream(newestFile)) using (var deltaStream = new MemoryStream()) { deltaBuilder.BuildDelta(newFileStream, new SignatureReader(signatureStream, new ConsoleProgressReporter()), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); return(deltaStream.ToArray()); } }
public MemoryStream Execute(string[] commandLineArguments, MemoryStream ms = null) { options.Parse(commandLineArguments); if (string.IsNullOrWhiteSpace(signatureFilePath)) { throw new OptionException("No signature file was specified", "new-file"); } if (string.IsNullOrWhiteSpace(newFilePath)) { throw new OptionException("No new file was specified", "new-file"); } newFilePath = Path.GetFullPath(newFilePath); signatureFilePath = Path.GetFullPath(signatureFilePath); var delta = new DeltaBuilder(); foreach (var config in configuration) { config(delta); } if (!File.Exists(signatureFilePath)) { throw new FileNotFoundException("File not found: " + signatureFilePath, signatureFilePath); } if (!File.Exists(newFilePath)) { throw new FileNotFoundException("File not found: " + newFilePath, newFilePath); } using (var newFileStream = new FileStream(newFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var signatureStream = new FileStream(signatureFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var deltaStream = new MemoryStream())//FileStream(deltaFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { delta.BuildDelta(newFileStream, new SignatureReader(signatureStream, delta.ProgressReporter), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); return(deltaStream); } }
private Byte[] BuildDeltaFromStream <TStream>(Byte[] signature, Func <TStream> action) where TStream : Stream { Byte[] result = null; var deltaBuilder = new DeltaBuilder(); deltaBuilder.ProgressReport = new ConsoleProgressReporter(); using (var resultStream = action()) using (var signatureStream = new MemoryStream(signature)) using (var deltaStream = new MemoryStream()) { deltaBuilder.BuildDelta(resultStream, new SignatureReader(signatureStream, deltaBuilder.ProgressReport), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); deltaStream.Position = 0; result = deltaStream.ToArray(); } return(result); }
public void LegacyBinaryDeltaReader_ReadsDelta() { // Arrange var(_, baseSignatureStream, _, newDataStream) = Utils.PrepareTestData(16974, 8452, SignatureBuilder.DefaultChunkSize); var deltaStream = new MemoryStream(); var deltaBuilder = new DeltaBuilder(); deltaBuilder.BuildDelta(newDataStream, new SignatureReader(baseSignatureStream, null), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); deltaStream.Seek(0, SeekOrigin.Begin); // Act var target = new BinaryDeltaReaderLegacy(deltaStream, null); // Assert Assert.AreEqual(new XxHashAlgorithm().Name, target.HashAlgorithm.Name); Assert.AreEqual(new XxHashAlgorithm().HashLength, target.HashAlgorithm.HashLength); Assert.AreEqual(RsyncFormatType.FastRsync, target.Type); Assert.IsNotEmpty(target.Metadata.ExpectedFileHash); Assert.AreEqual("MD5", target.Metadata.ExpectedFileHashAlgorithm); Assert.AreEqual(new XxHashAlgorithm().Name, target.Metadata.HashAlgorithm); }
public async Task PatchingAsyncXXHash_ForOctodiffSignature_PatchesFile(int baseNumberOfBytes, int newDataNumberOfBytes, short chunkSize) { // Arrange var(baseDataStream, baseSignatureStream, newData, newDataStream) = PrepareTestDataWithOctodiffSignature(baseNumberOfBytes, newDataNumberOfBytes, chunkSize); var progressReporter = Substitute.For <IProgress <ProgressReport> >(); // Act var deltaStream = new MemoryStream(); var deltaBuilder = new DeltaBuilder(); await deltaBuilder.BuildDeltaAsync(newDataStream, new SignatureReader(baseSignatureStream, null), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))).ConfigureAwait(false); deltaStream.Seek(0, SeekOrigin.Begin); var patchedDataStream = new MemoryStream(); var deltaApplier = new DeltaApplier(); await deltaApplier.ApplyAsync(baseDataStream, new BinaryDeltaReader(deltaStream, progressReporter), patchedDataStream).ConfigureAwait(false); // Assert CollectionAssert.AreEqual(newData, patchedDataStream.ToArray()); progressReporter.Received().Report(Arg.Any <ProgressReport>()); }
private static void DeltaUploadFile(string fileName) { Stopwatch sw = new Stopwatch(); sw.Start(); var filePath = $@"C:\Users\Peter\Desktop\Octodiff test\Octodiff\Octodiff-client\Game\{fileName}"; var deltaBuilder = new DeltaBuilder(); using (var newFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var signaturStream = new MemoryStream(signatur)) using (var deltaStream = new MemoryStream()) { deltaBuilder.BuildDelta(newFileStream, new SignatureReader(signaturStream, new ConsoleProgressReporter()), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); Octodiff_test.Program.DeltaUploadFile(fileName, deltaStream.ToArray()); } Console.WriteLine($"finished in {sw.Elapsed.TotalSeconds} secounds"); sw.Stop(); }
public void PatchingSyncXXHash_BigFile(string originalFileName, string newFileName) { try { // Arrange var(baseDataStream, baseSignatureStream) = PrepareTestData(originalFileName); var progressReporter = Substitute.For <IProgress <ProgressReport> >(); var deltaFileName = Path.GetTempFileName(); var patchedFileName = Path.GetTempFileName(); // Act using (var deltaStream = new FileStream(deltaFileName, FileMode.OpenOrCreate)) using (var patchedDataStream = new FileStream(patchedFileName, FileMode.OpenOrCreate)) using (var newDataStream = new FileStream(newFileName, FileMode.Open)) { var deltaBuilder = new DeltaBuilder(); deltaBuilder.BuildDelta(newDataStream, new SignatureReader(baseSignatureStream, null), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); deltaStream.Seek(0, SeekOrigin.Begin); var deltaApplier = new DeltaApplier(); deltaApplier.Apply(baseDataStream, new BinaryDeltaReader(deltaStream, progressReporter), patchedDataStream); } // Assert Assert.AreEqual(new FileInfo(newFileName).Length, new FileInfo(patchedFileName).Length); Assert.True(CompareFilesByHash(newFileName, patchedFileName)); progressReporter.Received().Report(Arg.Any <ProgressReport>()); } catch (Exception e) { Assert.Fail(); } }
public async Task PatchingAsyncXXHash_ForTheSameData_PatchesFile(int numberOfBytes, short chunkSize) { // Arrange var baseData = new byte[numberOfBytes]; new Random().NextBytes(baseData); var baseDataStream = new MemoryStream(baseData); var baseSignatureStream = new MemoryStream(); var signatureBuilder = new SignatureBuilder { ChunkSize = chunkSize }; signatureBuilder.Build(baseDataStream, new SignatureWriter(baseSignatureStream)); baseSignatureStream.Seek(0, SeekOrigin.Begin); var newDataStream = new MemoryStream(baseData); var progressReporter = Substitute.For <IProgress <ProgressReport> >(); // Act var deltaStream = new MemoryStream(); var deltaBuilder = new DeltaBuilder(); await deltaBuilder.BuildDeltaAsync(newDataStream, new SignatureReader(baseSignatureStream, null), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))).ConfigureAwait(false); deltaStream.Seek(0, SeekOrigin.Begin); var patchedDataStream = new MemoryStream(); var deltaApplier = new DeltaApplier(); await deltaApplier.ApplyAsync(baseDataStream, new BinaryDeltaReader(deltaStream, progressReporter), patchedDataStream).ConfigureAwait(false); // Assert CollectionAssert.AreEqual(baseData, patchedDataStream.ToArray()); progressReporter.Received().Report(Arg.Any <ProgressReport>()); }
static void Main(string[] args) { CommandLineApplication commandLineApplication = new CommandLineApplication(throwOnUnexpectedArg: false); CommandOption basePathArg = commandLineApplication.Option( "-b |--b <path>", "Path to base file. ", CommandOptionType.SingleValue); CommandOption sigPathArg = commandLineApplication.Option( "-s |--s <path>", "Path to signature file. ", CommandOptionType.SingleValue); CommandOption newPathArg = commandLineApplication.Option( "-n |--n <path>", "Path to new file. ", CommandOptionType.SingleValue); CommandOption deltaPathArg = commandLineApplication.Option( "-d |--d <path>", "Path to delta file. ", CommandOptionType.SingleValue); commandLineApplication.OnExecute(() => { var signatureBaseFilePath = Directory.GetCurrentDirectory(); if (basePathArg.HasValue()) { if (!File.Exists(basePathArg.Value())) { throw new ArgumentException("Path provided does not exist"); } else { signatureBaseFilePath = basePathArg.Value(); } } else { throw new ArgumentException("Must provide base path."); } var signatureFilePath = Directory.GetCurrentDirectory(); if (sigPathArg.HasValue()) { signatureFilePath = sigPathArg.Value(); } else { throw new ArgumentException("Must provide signature path."); } var signatureOutputDirectory = Path.GetDirectoryName(signatureFilePath); if (!Directory.Exists(signatureOutputDirectory)) { Directory.CreateDirectory(signatureOutputDirectory); } var signatureBuilder = new SignatureBuilder(); using (var basisStream = new FileStream(signatureBaseFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var signatureStream = new FileStream(signatureFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { signatureBuilder.Build(basisStream, new SignatureWriter(signatureStream)); } var newFilePath = Directory.GetCurrentDirectory(); if (newPathArg.HasValue()) { if (!File.Exists(newPathArg.Value())) { throw new ArgumentException("Path provided does not exist"); } else { newFilePath = newPathArg.Value(); } } else { throw new ArgumentException("Must provide new file path."); } var deltaFilePath = Directory.GetCurrentDirectory(); if (deltaPathArg.HasValue()) { deltaFilePath = deltaPathArg.Value(); } else { throw new ArgumentException("Must provide delta path."); } var deltaOutputDirectory = Path.GetDirectoryName(deltaFilePath); if (!Directory.Exists(deltaOutputDirectory)) { Directory.CreateDirectory(deltaOutputDirectory); } var deltaBuilder = new DeltaBuilder(); using (var newFileStream = new FileStream(newFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var signatureFileStream = new FileStream(signatureFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var deltaStream = new FileStream(deltaFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { deltaBuilder.BuildDelta(newFileStream, new SignatureReader(signatureFileStream, new ConsoleProgressReporter()), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); } return(0); }); commandLineApplication.Execute(args); }
public void GZip_CompressData_RsyncSignatureAndPatch(int dataLength) { // Arrange var dataBasis = new byte[dataLength]; new Random().NextBytes(dataBasis); var basisStream = new MemoryStream(dataBasis); var basisStreamCompressed = new MemoryStream(); var basisStreamCompressedSignature = new MemoryStream(); var newFileStream = new MemoryStream(); newFileStream.Write(dataBasis, 10, dataLength * 4 / 5); var newRandomData = new byte[dataLength * 2 / 5]; new Random().NextBytes(newRandomData); newFileStream.Write(newRandomData, 0, newRandomData.Length); newFileStream.Seek(0, SeekOrigin.Begin); var newFileStreamCompressed = new MemoryStream(); var deltaStream = new MemoryStream(); var patchedCompressedStream = new MemoryStream(); // Act GZip.Compress(basisStream, basisStreamCompressed); basisStreamCompressed.Seek(0, SeekOrigin.Begin); var signatureBuilder = new SignatureBuilder(); signatureBuilder.Build(basisStreamCompressed, new SignatureWriter(basisStreamCompressedSignature)); basisStreamCompressedSignature.Seek(0, SeekOrigin.Begin); GZip.Compress(newFileStream, newFileStreamCompressed); newFileStreamCompressed.Seek(0, SeekOrigin.Begin); var deltaBuilder = new DeltaBuilder(); deltaBuilder.BuildDelta(newFileStreamCompressed, new SignatureReader(basisStreamCompressedSignature, null), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream))); deltaStream.Seek(0, SeekOrigin.Begin); var deltaApplier = new DeltaApplier { SkipHashCheck = true }; var deltaReader = new BinaryDeltaReader(deltaStream, null); deltaApplier.Apply(basisStreamCompressed, deltaReader, patchedCompressedStream); deltaApplier.HashCheck(deltaReader, patchedCompressedStream); // Assert Assert.AreEqual(newFileStreamCompressed.ToArray(), patchedCompressedStream.ToArray()); patchedCompressedStream.Seek(0, SeekOrigin.Begin); var decompressedStream = new MemoryStream(); using (var gz = new GZipStream(patchedCompressedStream, CompressionMode.Decompress)) { gz.CopyTo(decompressedStream); } var dataOutput = decompressedStream.ToArray(); Assert.AreEqual(newFileStream.ToArray(), dataOutput); }