Snapshot Diff
This commit is contained in:
parent
6fc40bb831
commit
4358d956f0
@ -5,6 +5,7 @@ global using DTLib.Filesystem;
|
|||||||
global using File = DTLib.Filesystem.File;
|
global using File = DTLib.Filesystem.File;
|
||||||
global using Directory = DTLib.Filesystem.Directory;
|
global using Directory = DTLib.Filesystem.Directory;
|
||||||
using DTLib.Ben.Demystifier;
|
using DTLib.Ben.Demystifier;
|
||||||
|
using DTLib.Extensions;
|
||||||
using SyncDirectory.Storage;
|
using SyncDirectory.Storage;
|
||||||
|
|
||||||
Console.InputEncoding = Encoding.UTF8;
|
Console.InputEncoding = Encoding.UTF8;
|
||||||
@ -18,7 +19,10 @@ try
|
|||||||
"SyncDirectory-data-debug"
|
"SyncDirectory-data-debug"
|
||||||
#endif
|
#endif
|
||||||
);
|
);
|
||||||
storage.CreateSnapshot("tmp", "tmp");
|
var s1=storage.CreateSnapshot("tmp", "tmp");
|
||||||
|
var s2=storage.CreateSnapshot("tmp2", "tmp2");
|
||||||
|
var diff = DirectorySnapshotDiff.Diff(s1, s2);
|
||||||
|
Console.WriteLine(diff.FileDiffs.MergeToString('\n'));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Immutable;
|
using System.Collections;
|
||||||
|
using System.Collections.Immutable;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using DTLib;
|
using DTLib;
|
||||||
@ -10,8 +11,11 @@ public record DirectorySnapshot(
|
|||||||
string Name,
|
string Name,
|
||||||
IOPath Path,
|
IOPath Path,
|
||||||
DateTime SnapshotTimeUTC,
|
DateTime SnapshotTimeUTC,
|
||||||
IReadOnlyCollection<FileSnapshot> Files)
|
FileSnapshot[] Files)
|
||||||
{
|
{
|
||||||
|
public Lazy<Dictionary<string, FileSnapshot>> PathMap = new(() =>
|
||||||
|
new Dictionary<string, FileSnapshot>(Files.ToDictionary(f=>f.Path.Str)));
|
||||||
|
|
||||||
public static DirectorySnapshot Parse(DtsodV23 dtsod) =>
|
public static DirectorySnapshot Parse(DtsodV23 dtsod) =>
|
||||||
new DirectorySnapshot(
|
new DirectorySnapshot(
|
||||||
dtsod["name"],
|
dtsod["name"],
|
||||||
@ -22,7 +26,7 @@ public record DirectorySnapshot(
|
|||||||
CultureInfo.InvariantCulture),
|
CultureInfo.InvariantCulture),
|
||||||
((List<object>)dtsod["files"])
|
((List<object>)dtsod["files"])
|
||||||
.Select(fd=>FileSnapshot.Parse((DtsodV23)fd))
|
.Select(fd=>FileSnapshot.Parse((DtsodV23)fd))
|
||||||
.ToImmutableList()
|
.ToArray()
|
||||||
);
|
);
|
||||||
|
|
||||||
public DtsodV23 ToDtsod() =>
|
public DtsodV23 ToDtsod() =>
|
||||||
@ -38,10 +42,7 @@ public record DirectorySnapshot(
|
|||||||
{
|
{
|
||||||
var fileSnapshots = new List<FileSnapshot>();
|
var fileSnapshots = new List<FileSnapshot>();
|
||||||
foreach (var filePath in Directory.GetAllFiles(dirPath))
|
foreach (var filePath in Directory.GetAllFiles(dirPath))
|
||||||
{
|
fileSnapshots.Add(FileSnapshot.Create(filePath, dirPath));
|
||||||
fileSnapshots.Add(FileSnapshot.Create(filePath));
|
return new DirectorySnapshot(name, dirPath, DateTime.UtcNow, fileSnapshots.ToArray());
|
||||||
}
|
|
||||||
|
|
||||||
return new DirectorySnapshot(name, dirPath, DateTime.UtcNow, fileSnapshots);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
28
Storage/DirectorySnapshotDiff.cs
Normal file
28
Storage/DirectorySnapshotDiff.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace SyncDirectory.Storage;
|
||||||
|
|
||||||
|
public record DirectorySnapshotDiff(ICollection<FileSnapshotDiff> FileDiffs)
|
||||||
|
{
|
||||||
|
public static DirectorySnapshotDiff Diff(DirectorySnapshot firstLocal, DirectorySnapshot secondLocal)
|
||||||
|
{
|
||||||
|
List<FileSnapshotDiff> files = new();
|
||||||
|
foreach (var file in secondLocal.Files)
|
||||||
|
{
|
||||||
|
if(firstLocal.PathMap.Value.TryGetValue(file.Path.Str, out var filePrev))
|
||||||
|
{
|
||||||
|
files.Add(new FileSnapshotDiff(file,
|
||||||
|
file.ModifyTimeUTC == filePrev.ModifyTimeUTC ? FileStatus.Unchanged : FileStatus.Modified));
|
||||||
|
}
|
||||||
|
else files.Add(new FileSnapshotDiff(file, FileStatus.Created));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var filePrev in firstLocal.Files)
|
||||||
|
{
|
||||||
|
if(!secondLocal.PathMap.Value.ContainsKey(filePrev.Path.Str))
|
||||||
|
files.Add(new FileSnapshotDiff(filePrev, FileStatus.Deleted));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DirectorySnapshotDiff(files);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -10,13 +10,19 @@ public record FileSnapshot(
|
|||||||
DateTime ModifyTimeUTC,
|
DateTime ModifyTimeUTC,
|
||||||
DateTime SnapshotTimeUTC)
|
DateTime SnapshotTimeUTC)
|
||||||
{
|
{
|
||||||
public static FileSnapshot Create(IOPath filePath)
|
/// <summary>
|
||||||
|
/// Creates new FileSnapshot from an exnsting file
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fullFilePath">baseDirPath+filePathInBaseDir</param>
|
||||||
|
/// <param name="baseDirPath">will be removed from fullFilePath in the rezult FileSnapshot.Path</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static FileSnapshot Create(IOPath fullFilePath, IOPath baseDirPath)
|
||||||
{
|
{
|
||||||
var creationTime = System.IO.File.GetCreationTimeUtc(filePath.Str);
|
var creationTime = System.IO.File.GetCreationTimeUtc(fullFilePath.Str);
|
||||||
var modifyTime = System.IO.File.GetLastWriteTimeUtc(filePath.Str);
|
var modifyTime = System.IO.File.GetLastWriteTimeUtc(fullFilePath.Str);
|
||||||
// sometimes LastWriteTime can be less then CreationTime when unpacking archives
|
// sometimes LastWriteTime can be less then CreationTime when unpacking archives
|
||||||
var latest = modifyTime > creationTime ? modifyTime : creationTime;
|
var latest = modifyTime > creationTime ? modifyTime : creationTime;
|
||||||
return new FileSnapshot(filePath, FileSize.Get(filePath), latest, DateTime.UtcNow);
|
return new FileSnapshot(fullFilePath.RemoveBase(baseDirPath), FileSize.Get(fullFilePath), latest, DateTime.UtcNow);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FileSnapshot Parse(DtsodV23 dtsod) =>
|
public static FileSnapshot Parse(DtsodV23 dtsod) =>
|
||||||
|
|||||||
6
Storage/FileSnapshotDiff.cs
Normal file
6
Storage/FileSnapshotDiff.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace SyncDirectory.Storage;
|
||||||
|
|
||||||
|
public record FileSnapshotDiff(FileSnapshot File, FileStatus Status)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
9
Storage/FileStatus.cs
Normal file
9
Storage/FileStatus.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace SyncDirectory.Storage;
|
||||||
|
|
||||||
|
public enum FileStatus
|
||||||
|
{
|
||||||
|
Unchanged,
|
||||||
|
Created,
|
||||||
|
Modified,
|
||||||
|
Deleted,
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user