2024-09-24 09:46:52 +00:00
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace Common;
|
2024-06-24 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
public enum DirOrFile
|
|
|
|
|
{
|
|
|
|
|
Dir = 0,
|
|
|
|
|
File = 1,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum NextOpType
|
|
|
|
|
{
|
|
|
|
|
Add = 0,
|
|
|
|
|
Modify = 1,
|
|
|
|
|
Del = 2
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 09:46:52 +00:00
|
|
|
|
[JsonDerivedType(typeof(AFileOrDir), typeDiscriminator: 1)]
|
|
|
|
|
[JsonDerivedType(typeof(File), typeDiscriminator: 2)]
|
|
|
|
|
[JsonDerivedType(typeof(Dir), typeDiscriminator: 3)]
|
|
|
|
|
public class AFileOrDir
|
2024-06-24 09:27:12 +00:00
|
|
|
|
{
|
2024-09-24 06:27:39 +00:00
|
|
|
|
public DirOrFile Type { get; set; }
|
|
|
|
|
public NextOpType? NextOp { get; set; }
|
2024-06-24 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
// private string Path = path;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 全部为绝对路径... 占用资源会大一点,但是完全OK
|
|
|
|
|
/// </summary>
|
|
|
|
|
///
|
2024-09-24 06:27:39 +00:00
|
|
|
|
public required string Path { get; set; }
|
2024-06-24 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 相当于Path 包装,天杀的windows在路径字符串中使用两种分隔符,“/” 和“\”,这导致,即使两个字符串不相同,也可能是同一个路径。现在把它们统一起来
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string FormatedPath
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Replace("\\", "/"); }
|
|
|
|
|
set { Path = value; }
|
|
|
|
|
}
|
2024-09-24 09:46:52 +00:00
|
|
|
|
|
2024-10-12 06:30:56 +00:00
|
|
|
|
// public bool IsEqual(AFileOrDir other)
|
|
|
|
|
// {
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
2024-06-24 09:27:12 +00:00
|
|
|
|
|
|
|
|
|
public static int Compare(AFileOrDir l, AFileOrDir r)
|
|
|
|
|
{
|
|
|
|
|
var pv = l.FormatedPath.CompareTo(r.FormatedPath);
|
|
|
|
|
if (pv == 0)
|
|
|
|
|
{
|
|
|
|
|
pv = l.Type.CompareTo(r.Type);
|
|
|
|
|
}
|
|
|
|
|
return pv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">绝对路径</param>
|
|
|
|
|
/// <param name="mtime">文件的修改时间</param>/
|
2024-09-24 06:27:39 +00:00
|
|
|
|
public class File : AFileOrDir
|
2024-06-24 09:27:12 +00:00
|
|
|
|
{
|
2024-09-24 09:46:52 +00:00
|
|
|
|
public File()
|
2024-09-24 06:27:39 +00:00
|
|
|
|
{
|
|
|
|
|
Type = DirOrFile.File;
|
|
|
|
|
}
|
2024-09-24 09:46:52 +00:00
|
|
|
|
|
2024-09-24 06:27:39 +00:00
|
|
|
|
public required DateTime MTime { get; set; }
|
2024-06-24 09:27:12 +00:00
|
|
|
|
|
2024-10-12 06:30:56 +00:00
|
|
|
|
public bool IsEqual(AFileOrDir other)
|
2024-06-24 09:27:12 +00:00
|
|
|
|
{
|
|
|
|
|
if (other is not File otherFile)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var r =
|
|
|
|
|
this.MTime == otherFile.MTime
|
|
|
|
|
&& this.FormatedPath == otherFile.FormatedPath
|
|
|
|
|
&& this.NextOp == otherFile.NextOp;
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-24 06:27:39 +00:00
|
|
|
|
|
|
|
|
|
public class Dir : AFileOrDir
|
|
|
|
|
{
|
|
|
|
|
public Dir()
|
|
|
|
|
{
|
|
|
|
|
Type = DirOrFile.Dir;
|
|
|
|
|
}
|
2024-09-24 09:46:52 +00:00
|
|
|
|
|
2024-09-24 06:27:39 +00:00
|
|
|
|
public required List<AFileOrDir> Children { get; set; }
|
|
|
|
|
|
2024-10-12 06:30:56 +00:00
|
|
|
|
public bool IsEqual(AFileOrDir other)
|
2024-09-24 06:27:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (other is not Dir otherDir)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (this.FormatedPath != otherDir.FormatedPath || this.NextOp != otherDir.NextOp)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (this.Children.Count != otherDir.Children.Count)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
this.Children.Sort(AFileOrDir.Compare);
|
|
|
|
|
otherDir.Children.Sort(AFileOrDir.Compare);
|
|
|
|
|
for (int i = 0; i < this.Children.Count; i++)
|
|
|
|
|
{
|
2024-10-12 06:30:56 +00:00
|
|
|
|
var l = this.Children[i];
|
|
|
|
|
var r = otherDir.Children[i];
|
|
|
|
|
if (l is Dir ld && r is Dir rd)
|
|
|
|
|
{
|
|
|
|
|
if (!ld.IsEqual(rd))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (l is File lf && r is File rf)
|
|
|
|
|
{
|
|
|
|
|
if (!lf.IsEqual(rf))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-09-24 06:27:39 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|