Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/c#/DifferentialTest/DifferentialTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<ProjectReference Include="..\GeneralUpdate.Differential\GeneralUpdate.Differential.csproj" />
<ProjectReference Include="..\GeneralUpdate.Core\GeneralUpdate.Core.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
43 changes: 18 additions & 25 deletions src/c#/GeneralUpdate.Differential/Differ/BsdiffDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ namespace GeneralUpdate.Differential.Differ
/// Default: <see cref="BZip2CompressionProvider"/> (backward compatible).
/// Use <see cref="DeflateCompressionProvider"/> (0x01) for faster decompression.
///
/// Thread-safety: this class stores per-call state in instance fields.
/// A single instance MUST NOT be used concurrently by multiple callers.
/// Create separate instances for concurrent operations.
/// Thread-safety: this class is safe for concurrent calls provided the
/// configured <see cref="ICompressionProvider"/> is thread-safe.
/// The built-in providers (<see cref="BZip2CompressionProvider"/>,
/// <see cref="DeflateCompressionProvider"/>, <see cref="BrotliCompressionProvider"/>)
/// are all safe for concurrent use.
/// </remarks>
public class BsdiffDiffer : IBinaryDiffer
{
Expand All @@ -31,8 +33,6 @@ public class BsdiffDiffer : IBinaryDiffer
private const int ExtendedHeaderSize = 33;

private readonly ICompressionProvider _compressionProvider;
// Per-call mutable state. NOT thread-safe for concurrent calls on the same instance.
private string _oldfilePath, _newfilePath, _patchPath;

#endregion Private Members

Expand Down Expand Up @@ -84,15 +84,12 @@ public async Task Clean(string oldfilePath, string newfilePath, string patchPath
{
await Task.Run(() =>
{
_oldfilePath = oldfilePath;
_newfilePath = newfilePath;
_patchPath = patchPath;
ValidationParameters();
ValidationParameters(oldfilePath, newfilePath, patchPath);

using (var output = new FileStream(patchPath, FileMode.Create))
{
var oldBytes = File.ReadAllBytes(_oldfilePath);
var newBytes = File.ReadAllBytes(_newfilePath);
var oldBytes = File.ReadAllBytes(oldfilePath);
var newBytes = File.ReadAllBytes(newfilePath);

// Header layout:
// 0 8 "BSDIFF40" (magic)
Expand Down Expand Up @@ -270,13 +267,10 @@ public async Task Dirty(string oldfilePath, string newfilePath, string patchPath
{
await Task.Run(() =>
{
_oldfilePath = oldfilePath;
_newfilePath = newfilePath;
_patchPath = patchPath;
ValidationParameters();
ValidationParameters(oldfilePath, newfilePath, patchPath);

using (var input = new FileStream(_oldfilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var output = new FileStream(_newfilePath, FileMode.Create))
using (var input = new FileStream(oldfilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var output = new FileStream(newfilePath, FileMode.Create))
{
long controlLength, diffLength, newSize;
byte formatVersion;
Expand Down Expand Up @@ -432,7 +426,6 @@ await Task.Run(() =>
}
}

// The result is left at _newfilePath.
// Atomic replacement (if needed) is handled by the caller
// (e.g. DefaultDirtyStrategy.ApplyPatch).
});
Expand All @@ -450,14 +443,14 @@ private static FileStream OpenPatchStream(string patchPath)
return new FileStream(patchPath, FileMode.Open, FileAccess.Read, FileShare.Read);
}

private void ValidationParameters()
private static void ValidationParameters(string oldfilePath, string newfilePath, string patchPath)
{
if (string.IsNullOrWhiteSpace(_oldfilePath))
throw new ArgumentNullException(nameof(_oldfilePath), "Old file path must not be empty.");
if (string.IsNullOrWhiteSpace(_newfilePath))
throw new ArgumentNullException(nameof(_newfilePath), "New file path must not be empty.");
if (string.IsNullOrWhiteSpace(_patchPath))
throw new ArgumentNullException(nameof(_patchPath), "Patch path must not be empty.");
if (string.IsNullOrWhiteSpace(oldfilePath))
throw new ArgumentNullException(nameof(oldfilePath), "Old file path must not be empty.");
if (string.IsNullOrWhiteSpace(newfilePath))
throw new ArgumentNullException(nameof(newfilePath), "New file path must not be empty.");
if (string.IsNullOrWhiteSpace(patchPath))
throw new ArgumentNullException(nameof(patchPath), "Patch path must not be empty.");
}

private static int CompareBytes(byte[] left, int leftOffset, byte[] right, int rightOffset)
Expand Down
Loading
Loading