-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checksum based file comparison (#20)
- Loading branch information
Showing
14 changed files
with
217 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/AvaloniaSyncer/Sections/Synchronization/Actions/CopyAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ByteSizeLib; | ||
using CSharpFunctionalExtensions; | ||
using ReactiveUI; | ||
using ReactiveUI.Fody.Helpers; | ||
using Zafiro.Actions; | ||
using Zafiro.FileSystem; | ||
using Zafiro.FileSystem.Actions; | ||
using Zafiro.Mixins; | ||
|
||
namespace AvaloniaSyncer.Sections.Synchronization.Actions; | ||
|
||
public class CopyAction : ReactiveObject, IFileActionViewModel | ||
{ | ||
private readonly CopyFileAction copyAction; | ||
private readonly BehaviorSubject<bool> isSyncing = new(false); | ||
|
||
private CopyAction(CopyFileAction copyAction, Maybe<string> comment) | ||
{ | ||
this.copyAction = copyAction; | ||
Progress = copyAction.Progress; | ||
Description = $"Copy {copyAction.Source} to {copyAction.Destination}"; | ||
Comment = comment.GetValueOrDefault(""); | ||
LeftFile = Maybe<IZafiroFile>.From(this.copyAction.Source); | ||
RightFile = Maybe<IZafiroFile>.From(this.copyAction.Destination); | ||
} | ||
|
||
public string Comment { get; } | ||
public Maybe<IZafiroFile> LeftFile { get; } | ||
public Maybe<IZafiroFile> RightFile { get; } | ||
public string Description { get; } | ||
public bool IsIgnored => false; | ||
[Reactive] public bool IsSynced { get; private set; } | ||
public IObservable<LongProgress> Progress { get; } | ||
public IObservable<bool> IsSyncing => isSyncing.AsObservable(); | ||
[Reactive] public string? Error { get; private set; } | ||
public IObservable<ByteSize> Rate => Progress.Select(x => x.Current).Rate().Select(ByteSize.FromBytes); | ||
|
||
public async Task<Result> Execute(CancellationToken cancellationToken) | ||
{ | ||
isSyncing.OnNext(true); | ||
var execute = await copyAction.Execute(cancellationToken); | ||
isSyncing.OnNext(false); | ||
execute.TapError(e => Error = e); | ||
execute.Tap(() => IsSynced = true); | ||
return execute; | ||
} | ||
|
||
public static Task<Result<CopyAction>> Create(IZafiroFile source, IZafiroFile destination, Maybe<string> comment) | ||
{ | ||
return CopyFileAction.Create(source, destination).Map(action => new CopyAction(action, comment)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/AvaloniaSyncer/Sections/Synchronization/Actions/FileActionFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using CSharpFunctionalExtensions; | ||
using Zafiro.CSharpFunctionalExtensions; | ||
using Zafiro.FileSystem; | ||
using Zafiro.FileSystem.Comparer; | ||
using Zafiro.Mixins; | ||
|
||
namespace AvaloniaSyncer.Sections.Synchronization.Actions; | ||
|
||
public class FileActionFactory | ||
{ | ||
private readonly IZafiroDirectory destination; | ||
|
||
public FileActionFactory(IZafiroDirectory destination) | ||
{ | ||
this.destination = destination; | ||
} | ||
|
||
public Task<Result<IFileActionViewModel>> Create(FileDiff diff) | ||
{ | ||
|
||
return diff switch | ||
{ | ||
BothDiff bothDiff => AreEquivalent(bothDiff.Left, bothDiff.Right) ? FileAreEqual(bothDiff) : FileAreDifferent(bothDiff.Left.File, bothDiff.Right.File), | ||
LeftOnlyDiff leftOnlyDiff => CopyToDestination(leftOnlyDiff.Left.File), | ||
RightOnlyDiff rightOnlyDiff => Delete(rightOnlyDiff.Right.File), | ||
_ => throw new ArgumentOutOfRangeException(nameof(diff)) | ||
}; | ||
} | ||
|
||
private static async Task<Result<IFileActionViewModel>> FileAreEqual(BothDiff bothDiff) | ||
{ | ||
Maybe<string> comment = $""" | ||
One of the checksums match: | ||
· {bothDiff.Left.File}: | ||
{FormatChecksums(bothDiff.Left.Hashes)} | ||
· {bothDiff.Right.File}: | ||
{FormatChecksums(bothDiff.Left.Hashes)} | ||
"""; | ||
|
||
var fileActionViewModel = new DoNothing( | ||
"Skip", | ||
comment, | ||
Maybe<IZafiroFile>.From(bothDiff.Left.File), | ||
Maybe<IZafiroFile>.From(bothDiff.Right.File)); | ||
|
||
return fileActionViewModel; | ||
} | ||
|
||
private static string FormatChecksums(IDictionary<ChecksumKind, byte[]> leftHashes) | ||
{ | ||
return leftHashes.Select(pair => "\t" + pair.Key + "=" + Convert.ToHexString(pair.Value)).JoinWithLines(); | ||
} | ||
|
||
private Task<Result<IFileActionViewModel>> Delete(IZafiroFile rightFile) | ||
{ | ||
// Implement this | ||
return Task.FromResult(Result.Success<IFileActionViewModel>(new DoNothing("Skip", "File only appear of the right side. Ignoring!", Maybe<IZafiroFile>.None, Maybe.From(rightFile)))); | ||
} | ||
|
||
private Task<Result<IFileActionViewModel>> CopyToDestination(IZafiroFile source) | ||
{ | ||
return CopyAction.Create(source, source.EquivalentIn(destination), $"File {source} does not exist in {destination}").Cast(action => (IFileActionViewModel)action); | ||
} | ||
|
||
private static Task<Result<IFileActionViewModel>> FileAreDifferent(IZafiroFile source, IZafiroFile destination) | ||
{ | ||
return CopyAction.Create(source, destination, "Files are different").Cast(action => (IFileActionViewModel)action); | ||
} | ||
|
||
private static bool AreEquivalent(FileWithMetadata left, FileWithMetadata right) | ||
{ | ||
var hashCombinations = from leftHash in left.Hashes | ||
join rightHash in right.Hashes on leftHash.Key equals rightHash.Key | ||
select new { LeftHash = leftHash, RightHash = rightHash }; | ||
|
||
return hashCombinations.Any(combination => | ||
StructuralComparisons.StructuralEqualityComparer.Equals(combination.LeftHash.Value, combination.RightHash.Value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.