Skip to content

Commit

Permalink
Merge pull request #5 from MatthiWare/CLI
Browse files Browse the repository at this point in the history
Add CLI
  • Loading branch information
Matthiee authored Nov 6, 2019
2 parents c7e39b7 + cc1329b commit 6986728
Show file tree
Hide file tree
Showing 23 changed files with 282 additions and 133 deletions.
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,33 @@ Using the CLI for creating delta patches and applying delta

### Create delta patch

`dotnet .\VCDiff.Core.Cli.dll -e .\original.exe .\updated.exe .\delta 8`
`dotnet .\VCDiff.Core.Cli create -o .\original.exe -n .\updated.exe -d .\delta -b 8`

To encode/create delta patch you need to specify `-e [original] [updated] [output] [window size]`.
or

`dotnet .\VCDiff.Core.Cli create --old .\original.exe --new .\updated.exe --delta .\delta --buffer 8`

To encode/create delta patch you need to specify `create -o [original] -n [updated] -d [output] -b [window size]`.

_[Window size]: The maximum buffer size for window chunking (in megabytes)._

### Apply delta patch

`dotnet .\VCDiff.Core.Cli.dll -d .\original.exe .\delta .\updated.exe`
`dotnet .\VCDiff.Core.Cli patch -o .\original.exe -d .\delta -n .\updated.exe`

or

`dotnet .\VCDiff.Core.Cli patch --old .\original.exe --delta .\delta --new .\updated.exe`

To apply delta patch you need to specify `-d [original] [delta] [output]`.
To apply delta patch you need to specify `apply -o [original] -d [delta] -o [output]`.

### Verify hashes in PowerShell

- Encode
`dotnet .\VCDiff.Core.Cli.dll -e .\original.exe .\updated.exe .\delta 8`
- Encode/Create patch
`dotnet .\VCDiff.Core.Cli create -o .\original.exe -n .\updated.exe -d .\delta -b 8`

- Decode
`dotnet .\VCDiff.Core.Cli.dll -d .\original.exe .\delta .\updated_with_delta.exe`
- Decode/Apply patch
`dotnet .\VCDiff.Core.Cli patch -o .\original.exe -d .\delta -n .\updated.exe`

- Verify hash
`get-filehash -Path ".\updated.exe", ".\updated_with_delta.exe" -algorithm MD5`
Expand Down
19 changes: 19 additions & 0 deletions VCDiff.Core.Cli/Options/CreateOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MatthiWare.CommandLine.Core.Attributes;

namespace MatthiWare.Compression.VCDiff.Cli.Options
{
public class CreateOptions
{
[Required, Name("o", "old"), Description("Path to the old version of the file")]
public string OldFile { get; set; }

[Required, Name("n", "new"), Description("Path to the new version of the file")]
public string NewFile { get; set; }

[Required, Name("d", "delta"), Description("Output path of the delta patch file")]
public string DeltaFile { get; set; }

[Name("b", "buffer"), Description("Optional buffer size to use"), DefaultValue(1)]
public int BufferSize { get; set; }
}
}
16 changes: 16 additions & 0 deletions VCDiff.Core.Cli/Options/PatchOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using MatthiWare.CommandLine.Core.Attributes;

namespace MatthiWare.Compression.VCDiff.Cli.Options
{
public class PatchOptions
{
[Required, Name("o", "old"), Description("Path to the old version of the file")]
public string OldFile { get; set; }

[Required, Name("n", "new"), Description("Output path of the patched file")]
public string NewFile { get; set; }

[Required, Name("d", "delta"), Description("Path of the delta patch file")]
public string DeltaFile { get; set; }
}
}
10 changes: 10 additions & 0 deletions VCDiff.Core.Cli/Options/ProgramOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MatthiWare.CommandLine.Core.Attributes;

namespace MatthiWare.Compression.VCDiff.Cli.Options
{
public class ProgramOptions
{
[Required(false), Name("v", "verbose"), DefaultValue(false), Description("Verbose output")]
public bool Verbose { get; set; }
}
}
81 changes: 54 additions & 27 deletions VCDiff.Core.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,78 @@
using System;
using System.IO;
using MatthiWare.CommandLine;
using MatthiWare.Compression.VCDiff.Cli.Options;
using MatthiWare.Compression.VCDiff.Decoders;
using MatthiWare.Compression.VCDiff.Encoders;
using MatthiWare.Compression.VCDiff.Includes;
using System;
using System.IO;

namespace MatthiWare.Compression.VCDiff.Cli
{
class Program
{
static int Main(string[] args)
{
string type = args[0]; // -e -d
var oldPath = args[1];
var newPath = args[2];
var outPath = args[3];
var cliParserOptions = new CommandLineParserOptions
{
AppName = "VCDiff.Core.Cli"
};

if (type != "-e" && type != "-d") throw new ArgumentException("Invalid parameters, use -e or -d");
var result = VCDiffResult.NOOP;

var result = VCDiffResult.SUCCESS;
var parser = new CommandLineParser<ProgramOptions>();

if (type == "-e")
{
var size = int.Parse(args[4]);
parser.AddCommand<CreateOptions>()
.Name("create")
.Required(false)
.Description("Creates a delta patch from the given input")
.OnExecuting((o, opt) => result = Create(opt));

using (var sold = File.OpenRead(oldPath)) // old file
using (var snew = File.OpenRead(newPath)) // new file
using (var sout = File.OpenWrite(outPath)) // delta file
result = Encode(sold, snew, sout, size);
}
else if (type == "-d")
{
using (var sold = File.OpenRead(oldPath)) // old file
using (var snew = File.OpenRead(newPath)) // delta file
using (var sout = File.OpenWrite(outPath)) // out file
result = Decode(sold, snew, sout);
}
parser.AddCommand<PatchOptions>()
.Name("patch")
.Required(false)
.Description("Appplies the give patch to the file")
.OnExecuting((o, opt) => result = Patch(opt));

var parserResult = parser.Parse(args);

if (parserResult.HasErrors && !parserResult.HelpRequested)
result = VCDiffResult.Error;

switch (result)
{
case VCDiffResult.SUCCESS:
case VCDiffResult.NOOP:
parser.Printer.PrintUsage();
break;
case VCDiffResult.Succes:
default:
return 0;
case VCDiffResult.ERROR:
break;
case VCDiffResult.Error:
Console.Error.WriteLine("Unexpected error occured");

return -1;
case VCDiffResult.EOD:
Console.Error.WriteLine("Unexpected end of data");

return -2;
}

return 0;
}

private static VCDiffResult Patch(PatchOptions opt)
{
using (var sold = File.OpenRead(opt.OldFile)) // old file
using (var snew = File.OpenRead(opt.DeltaFile)) // delta file
using (var sout = File.OpenWrite(opt.NewFile)) // out file
return Decode(sold, snew, sout);
}

private static VCDiffResult Create(CreateOptions opt)
{
using (var sold = File.OpenRead(opt.OldFile)) // old file
using (var snew = File.OpenRead(opt.NewFile)) // new file
using (var sout = File.OpenWrite(opt.DeltaFile)) // delta file
return Encode(sold, snew, sout, opt.BufferSize);
}

private static VCDiffResult Encode(Stream sold, Stream snew, Stream sout, int bufferSize)
Expand All @@ -61,7 +88,7 @@ private static VCDiffResult Decode(Stream sold, Stream sdelta, Stream sout)

var result = decoder.Start();

if (result != VCDiffResult.SUCCESS)
if (result != VCDiffResult.Succes)
return result;

return decoder.Decode(out _);
Expand Down
3 changes: 2 additions & 1 deletion VCDiff.Core.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"profiles": {
"VCDiff.Core.Cli": {
"commandName": "Project"
"commandName": "Project",
"commandLineArgs": "create -o blob.bin -n blob_updated.bin -d delta"
}
}
}
5 changes: 5 additions & 0 deletions VCDiff.Core.Cli/VCDiff.Core.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
<PackageTags>VCDiff delta cli</PackageTags>
<RootNamespace>MatthiWare.Compression.VCDiff.Cli</RootNamespace>
<StartupObject></StartupObject>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MatthiWare.CommandLineParser" Version="0.2.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VCDiff.Core\VCDiff.Core.csproj" />
</ItemGroup>
Expand Down
36 changes: 13 additions & 23 deletions VCDiff.Core.Tests/Integration/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using MatthiWare.Compression.VCDiff.Decoders;
using MatthiWare.Compression.VCDiff.Decoders;
using MatthiWare.Compression.VCDiff.Encoders;
using MatthiWare.Compression.VCDiff.Includes;
using Xunit;
using Xunit.Abstractions;
using NUnit.Framework;
using System;
using System.IO;

namespace VCDiff.Core.Tests.Integration
{

[TestFixture]
public class IntegrationTests
{

private readonly ITestOutputHelper output;

public IntegrationTests(ITestOutputHelper output)
{
this.output = output;
}

[Fact]
[Test]
public void TestEncodeAndDecodeShouldBeTheSame()
{
int size = 20 * 1024 * 1024; // 20 MB
Expand All @@ -38,9 +28,9 @@ public void TestEncodeAndDecodeShouldBeTheSame()
var sDelta = new MemoryStream(new byte[size], true);

var coder = new VCCoder(sOld, sNew, sDelta);
Assert.Equal(VCDiffResult.SUCCESS, coder.Encode());
Assert.AreEqual(VCDiffResult.Succes, coder.Encode());

output.WriteLine($"Delta is {sDelta.Position / 1024 / 1024} MB's");
TestContext.Out.WriteLine($"Delta is {sDelta.Position / 1024 / 1024} MB's");

sDelta.SetLength(sDelta.Position);
sDelta.Position = 0;
Expand All @@ -50,15 +40,15 @@ public void TestEncodeAndDecodeShouldBeTheSame()
var sPatched = new MemoryStream(new byte[size], true);

var decoder = new VCDecoder(sOld, sDelta, sPatched);
Assert.Equal(VCDiffResult.SUCCESS, decoder.Start());
Assert.Equal(VCDiffResult.SUCCESS, decoder.Decode(out long bytesWritten));
Assert.AreEqual(VCDiffResult.Succes, decoder.Start());
Assert.AreEqual(VCDiffResult.Succes, decoder.Decode(out long bytesWritten));

output.WriteLine($"Written {bytesWritten / 1024 / 1024} MB's");
TestContext.Out.WriteLine($"Written {bytesWritten / 1024 / 1024} MB's");

Assert.Equal(sNew.ToArray(), sPatched.ToArray());
Assert.AreEqual(sNew.ToArray(), sPatched.ToArray());
}

private Random random = new Random(DateTime.Now.GetHashCode());
private static readonly Random random = new Random(DateTime.Now.GetHashCode());

private byte[] CreateRandomByteArray(int size)
{
Expand Down
7 changes: 5 additions & 2 deletions VCDiff.Core.Tests/VCDiff.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>

<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit.Console" Version="3.8.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions VCDiff.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
build.cake = build.cake
build.ps1 = build.ps1
build.sh = build.sh
codecov.yml = codecov.yml
LICENSE = LICENSE
README.md = README.md
EndProjectSection
Expand Down
Loading

0 comments on commit 6986728

Please sign in to comment.