Remove
This commit is contained in:
parent
631d47ddf9
commit
33f550a51f
@ -11,35 +11,23 @@ public record Record(string RelativePath,
|
||||
long Ticks)
|
||||
{
|
||||
|
||||
internal static ReadOnlyCollection<Record> GetCollection(SyncConfiguration syncConfiguration, string rightDirectory)
|
||||
{
|
||||
ReadOnlyCollection<Record> results;
|
||||
Matcher matcher = new();
|
||||
string excludePatternsFile = Path.Combine(rightDirectory, syncConfiguration.ExcludePatternsFile);
|
||||
string includePatternsFile = Path.Combine(rightDirectory, syncConfiguration.IncludePatternsFile);
|
||||
matcher.AddIncludePatterns(!File.Exists(includePatternsFile) ? ["*"] : File.ReadAllLines(includePatternsFile));
|
||||
matcher.AddExcludePatterns(!File.Exists(excludePatternsFile) ? ["System Volume Information"] : File.ReadAllLines(excludePatternsFile));
|
||||
results = GetRecords(rightDirectory, matcher);
|
||||
return results;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<Record> GetRecords(string rightDirectory, Matcher matcher)
|
||||
internal static ReadOnlyCollection<Record> GetRecords(Matcher matcher, string directory)
|
||||
{
|
||||
List<Record> results = [];
|
||||
Record record;
|
||||
FileInfo fileInfo;
|
||||
string relativePath;
|
||||
ReadOnlyCollection<ReadOnlyCollection<string>> collection = GetFilesCollection(rightDirectory, "*", "*");
|
||||
ReadOnlyCollection<ReadOnlyCollection<string>> collection = GetFilesCollection(directory, "*", "*");
|
||||
foreach (ReadOnlyCollection<string> c in collection)
|
||||
{
|
||||
foreach (string f in c)
|
||||
{
|
||||
if (!matcher.Match(rightDirectory, f).HasMatches)
|
||||
if (!matcher.Match(directory, f).HasMatches)
|
||||
continue;
|
||||
fileInfo = new(f);
|
||||
if (fileInfo.Length == 0)
|
||||
continue;
|
||||
relativePath = Path.GetRelativePath(rightDirectory, fileInfo.FullName);
|
||||
relativePath = Path.GetRelativePath(directory, fileInfo.FullName);
|
||||
record = new(RelativePath: relativePath,
|
||||
Size: fileInfo.Length,
|
||||
Ticks: fileInfo.LastWriteTime.ToUniversalTime().Ticks);
|
||||
|
@ -5,7 +5,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace FileExposer.Models;
|
||||
|
||||
public record RelativePath(string Path,
|
||||
public record RelativePath(string LeftDirectory,
|
||||
string? RightDirectory,
|
||||
Record[] Records)
|
||||
{
|
||||
|
||||
|
@ -36,29 +36,17 @@ public record Review(Segment[]? AreEqual,
|
||||
private static ReadOnlyCollection<Segment> GetAreEqual(RelativePath relativePath, ReadOnlyCollection<Record> records)
|
||||
{
|
||||
List<Segment> results = [];
|
||||
Record? record;
|
||||
Segment segment;
|
||||
double totalSeconds;
|
||||
string? checkDirectory = null;
|
||||
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(relativePath);
|
||||
foreach (Record r in records)
|
||||
{
|
||||
if (checkDirectory is null && r.Size == 0 && r.Ticks == 0)
|
||||
{
|
||||
checkDirectory = r.RelativePath;
|
||||
continue;
|
||||
}
|
||||
if (r.RelativePath == relativePath.Path)
|
||||
continue;
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record))
|
||||
continue;
|
||||
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
|
||||
if (record.Size != r.Size || totalSeconds is > 2 or < -2)
|
||||
continue;
|
||||
segment = new(Left: r,
|
||||
LeftDirectory: checkDirectory,
|
||||
Right: record,
|
||||
RightDirectory: relativePath.Path);
|
||||
segment = new(Left: r, Right: record);
|
||||
results.Add(segment);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
@ -78,31 +66,19 @@ public record Review(Segment[]? AreEqual,
|
||||
private static ReadOnlyCollection<Segment> GetNotEqualBut(RelativePath relativePath, ReadOnlyCollection<Record> records)
|
||||
{
|
||||
List<Segment> results = [];
|
||||
Record? record;
|
||||
Segment segment;
|
||||
double totalSeconds;
|
||||
string? checkDirectory = null;
|
||||
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(relativePath);
|
||||
foreach (Record r in records)
|
||||
{
|
||||
if (checkDirectory is null && r.Size == 0 && r.Ticks == 0)
|
||||
{
|
||||
checkDirectory = r.RelativePath;
|
||||
continue;
|
||||
}
|
||||
if (r.RelativePath == relativePath.Path)
|
||||
continue;
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record))
|
||||
continue;
|
||||
if (record.Size == r.Size)
|
||||
continue;
|
||||
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
|
||||
if (totalSeconds is >= 2 or <= -2)
|
||||
continue;
|
||||
segment = new(Left: r,
|
||||
LeftDirectory: checkDirectory,
|
||||
Right: record,
|
||||
RightDirectory: relativePath.Path);
|
||||
segment = new(Left: r, Right: record);
|
||||
results.Add(segment);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
@ -111,25 +87,13 @@ public record Review(Segment[]? AreEqual,
|
||||
private static ReadOnlyCollection<Segment> GetLeftSideOnly(RelativePath relativePath, ReadOnlyCollection<Record> records)
|
||||
{
|
||||
List<Segment> results = [];
|
||||
Record? record;
|
||||
Segment segment;
|
||||
string? checkDirectory = null;
|
||||
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(relativePath);
|
||||
foreach (Record r in records)
|
||||
{
|
||||
if (checkDirectory is null && r.Size == 0 && r.Ticks == 0)
|
||||
{
|
||||
checkDirectory = r.RelativePath;
|
||||
if (keyValuePairs.TryGetValue(r.RelativePath, out Record? record))
|
||||
continue;
|
||||
}
|
||||
if (r.RelativePath == relativePath.Path)
|
||||
continue;
|
||||
if (keyValuePairs.TryGetValue(r.RelativePath, out record))
|
||||
continue;
|
||||
segment = new(Left: r,
|
||||
LeftDirectory: checkDirectory,
|
||||
Right: record,
|
||||
RightDirectory: relativePath.Path);
|
||||
segment = new(Left: r, Right: record);
|
||||
results.Add(segment);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
@ -138,25 +102,13 @@ public record Review(Segment[]? AreEqual,
|
||||
private static ReadOnlyCollection<Segment> GetRightSideOnly(RelativePath relativePath, ReadOnlyCollection<Record> records)
|
||||
{
|
||||
List<Segment> results = [];
|
||||
Record? record;
|
||||
Segment segment;
|
||||
string? checkDirectory = null;
|
||||
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(records);
|
||||
foreach (Record r in relativePath.Records)
|
||||
{
|
||||
if (checkDirectory is null && r.Size == 0 && r.Ticks == 0)
|
||||
{
|
||||
checkDirectory = r.RelativePath;
|
||||
if (keyValuePairs.TryGetValue(r.RelativePath, out Record? record))
|
||||
continue;
|
||||
}
|
||||
if (r.RelativePath == relativePath.Path)
|
||||
continue;
|
||||
if (keyValuePairs.TryGetValue(r.RelativePath, out record))
|
||||
continue;
|
||||
segment = new(Left: record,
|
||||
LeftDirectory: null,
|
||||
Right: r,
|
||||
RightDirectory: relativePath.Path);
|
||||
segment = new(Left: record, Right: r);
|
||||
results.Add(segment);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
@ -165,29 +117,17 @@ public record Review(Segment[]? AreEqual,
|
||||
private static ReadOnlyCollection<Segment> GetLeftSideIsNewer(RelativePath relativePath, ReadOnlyCollection<Record> records)
|
||||
{
|
||||
List<Segment> results = [];
|
||||
Record? record;
|
||||
Segment segment;
|
||||
double totalSeconds;
|
||||
string? checkDirectory = null;
|
||||
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(relativePath);
|
||||
foreach (Record r in records)
|
||||
{
|
||||
if (checkDirectory is null && r.Size == 0 && r.Ticks == 0)
|
||||
{
|
||||
checkDirectory = r.RelativePath;
|
||||
continue;
|
||||
}
|
||||
if (r.RelativePath == relativePath.Path)
|
||||
continue;
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record))
|
||||
continue;
|
||||
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
|
||||
if (totalSeconds is > -2)
|
||||
continue;
|
||||
segment = new(Left: r,
|
||||
LeftDirectory: checkDirectory,
|
||||
Right: record,
|
||||
RightDirectory: relativePath.Path);
|
||||
segment = new(Left: r, Right: record);
|
||||
results.Add(segment);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
@ -196,29 +136,17 @@ public record Review(Segment[]? AreEqual,
|
||||
private static ReadOnlyCollection<Segment> GetRightSideIsNewer(RelativePath relativePath, ReadOnlyCollection<Record> records)
|
||||
{
|
||||
List<Segment> results = [];
|
||||
Record? record;
|
||||
Segment segment;
|
||||
double totalSeconds;
|
||||
string? checkDirectory = null;
|
||||
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(records);
|
||||
foreach (Record r in relativePath.Records)
|
||||
{
|
||||
if (checkDirectory is null && r.Size == 0 && r.Ticks == 0)
|
||||
{
|
||||
checkDirectory = r.RelativePath;
|
||||
continue;
|
||||
}
|
||||
if (r.RelativePath == relativePath.Path)
|
||||
continue;
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out record))
|
||||
if (!keyValuePairs.TryGetValue(r.RelativePath, out Record? record))
|
||||
continue;
|
||||
totalSeconds = new TimeSpan(record.Ticks - r.Ticks).TotalSeconds;
|
||||
if (totalSeconds is > -2)
|
||||
continue;
|
||||
segment = new(Left: record,
|
||||
LeftDirectory: null,
|
||||
Right: r,
|
||||
RightDirectory: relativePath.Path);
|
||||
segment = new(Left: record, Right: r);
|
||||
results.Add(segment);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
|
@ -3,9 +3,7 @@ using System.Text.Json.Serialization;
|
||||
namespace FileExposer.Models;
|
||||
|
||||
public record Segment(Record? Left,
|
||||
string? LeftDirectory,
|
||||
Record? Right,
|
||||
string RightDirectory);
|
||||
Record? Right);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Segment))]
|
||||
|
@ -5,6 +5,7 @@ using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.FileSystemGlobbing;
|
||||
|
||||
public class SyncV1Repository(AppSettings appSettings) : ISyncV1Repository
|
||||
{
|
||||
@ -35,13 +36,24 @@ public class SyncV1Repository(AppSettings appSettings) : ISyncV1Repository
|
||||
if (File.Exists(path))
|
||||
throw new Exception("Must pass size and ticks when passing a file!");
|
||||
string directory = Path.GetFullPath(path);
|
||||
ReadOnlyCollection<Record> records = Record.GetCollection(_AppSettings.SyncConfiguration, directory);
|
||||
RelativePath relativePath = new(Path: path, Records: [.. records]);
|
||||
string excludePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.ExcludePatternsFile);
|
||||
string includePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.IncludePatternsFile);
|
||||
Matcher matcher = GetMatcher(excludePatternsFile, includePatternsFile);
|
||||
ReadOnlyCollection<Record> records = Record.GetRecords(matcher, directory);
|
||||
RelativePath relativePath = new(LeftDirectory: path, Records: [.. records], RightDirectory: null);
|
||||
result = new(JSON: JsonSerializer.Serialize(relativePath, RelativePathSourceGenerationContext.Default.RelativePath), Bytes: null);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Matcher GetMatcher(string excludePatternsFile, string includePatternsFile)
|
||||
{
|
||||
Matcher result = new();
|
||||
result.AddIncludePatterns(!File.Exists(includePatternsFile) ? ["*"] : File.ReadAllLines(includePatternsFile));
|
||||
result.AddExcludePatterns(!File.Exists(excludePatternsFile) ? ["System Volume Information"] : File.ReadAllLines(excludePatternsFile));
|
||||
return result;
|
||||
}
|
||||
|
||||
private FileInfo Verify(string path, long size, long ticks)
|
||||
{
|
||||
FileInfo fileInfo = new(path);
|
||||
@ -58,7 +70,11 @@ public class SyncV1Repository(AppSettings appSettings) : ISyncV1Repository
|
||||
string result;
|
||||
RelativePath? relativePath = RelativePath.Get(stream) ??
|
||||
throw new MissingFieldException();
|
||||
ReadOnlyCollection<Record> records = Record.GetCollection(_AppSettings.SyncConfiguration, relativePath.Path);
|
||||
string directory = Path.GetFullPath(relativePath.LeftDirectory);
|
||||
string excludePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.ExcludePatternsFile);
|
||||
string includePatternsFile = Path.Combine(directory, _AppSettings.SyncConfiguration.IncludePatternsFile);
|
||||
Matcher matcher = GetMatcher(excludePatternsFile, includePatternsFile);
|
||||
ReadOnlyCollection<Record> records = Record.GetRecords(matcher, directory);
|
||||
if (records.Count == 0)
|
||||
throw new Exception("No source records");
|
||||
Review review = Review.Get(relativePath, records);
|
||||
|
Loading…
x
Reference in New Issue
Block a user