Skip to content

Commit

Permalink
拆分黑名单逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Sep 23, 2024
1 parent 22fb2e1 commit 0e03e47
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 92 deletions.
4 changes: 2 additions & 2 deletions src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public override async Task ExecuteAsync()
//patch middleware
context.Add("SourcePath", patchPath);
context.Add("TargetPath", Packet.InstallPath);
context.Add("BlackFiles", GeneralFileManager.BlackFiles);
context.Add("BlackFileFormats", GeneralFileManager.BlackFileFormats);
context.Add("BlackFiles", BlackListManager.Instance.BlackFiles);
context.Add("BlackFileFormats", BlackListManager.Instance.BlackFileFormats);

var pipelineBuilder = new PipelineBuilder(context)
.UseMiddleware<PatchMiddleware>()
Expand Down
99 changes: 99 additions & 0 deletions src/c#/GeneralUpdate.Common/File/BlackListManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Collections.Generic;
using System.IO;

namespace GeneralUpdate.Common;

public class BlackListManager
{
private readonly static object _lockObject = new object();

private static BlackListManager _instance;

private static readonly List<string> _blackFileFormats =
[
".patch",
".7z",
".zip",
".rar",
".tar",
".json"
];

private static readonly List<string> _blackFiles = ["Newtonsoft.Json.dll"];

private BlackListManager()
{
}

public static BlackListManager Instance
{
get
{
if (_instance == null)
{
lock (_lockObject)
{
if (_instance == null)
{
_instance = new BlackListManager();
}
}
}

return _instance;
}
}

public IReadOnlyList<string> BlackFileFormats => _blackFileFormats.AsReadOnly();
public IReadOnlyList<string> BlackFiles => _blackFiles.AsReadOnly();

public void AddBlackFileFormats(List<string> formats)
{
foreach (var format in formats)
{
AddBlackFileFormat(format);
}
}

public void AddBlackFileFormat(string format)
{
if (!_blackFileFormats.Contains(format))
{
_blackFileFormats.Add(format);
}
}

public void RemoveBlackFileFormat(string format)
{
_blackFileFormats.Remove(format);
}

public void AddBlackFiles(List<string> fileNames)
{
foreach (var fileName in fileNames)
{
AddBlackFile(fileName);
}
}

public void AddBlackFile(string fileName)
{
if (!_blackFiles.Contains(fileName))
{
_blackFiles.Add(fileName);
}
}

public void RemoveBlackFile(string fileName)
{
_blackFiles.Remove(fileName);
}

public bool IsBlacklisted(string relativeFilePath)
{
var fileName = Path.GetFileName(relativeFilePath);
var fileExtension = Path.GetExtension(relativeFilePath);

return _blackFiles.Contains(fileName) || _blackFileFormats.Contains(fileExtension);
}
}
4 changes: 2 additions & 2 deletions src/c#/GeneralUpdate.Common/File/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace GeneralUpdate.Common;

public static class FileExtensions
{
public static List<FileInfo> ToFileInfoList(this List<string> filePaths)
public static List<FileInfo> AsFileInfo(this List<string> filePaths)
{
return filePaths.Select(path => new FileInfo(path)).ToList();
}

public static List<FileInfo> ToFileInfoList(this IReadOnlyList<string> filePaths)
public static List<FileInfo> AsFileInfo(this IReadOnlyList<string> filePaths)
{
return filePaths.Select(path => new FileInfo(path)).ToList();
}
Expand Down
78 changes: 2 additions & 76 deletions src/c#/GeneralUpdate.Common/File/GeneralFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,14 @@ namespace GeneralUpdate.Common
{
public sealed class GeneralFileManager
{
#region Private Members

private static readonly List<string> _blackFileFormats =
[
".patch",
".7z",
".zip",
".rar",
".tar",
".json"
];

private static readonly List<string> _blackFiles = ["Newtonsoft.Json.dll"];

#endregion

#region Public Properties

public static IReadOnlyList<string> BlackFileFormats => _blackFileFormats.AsReadOnly();
public static IReadOnlyList<string> BlackFiles => _blackFiles.AsReadOnly();

public ComparisonResult ComparisonResult { get; private set; }

#endregion

#region Public Methods

public static List<FileInfo> ToFileInfoList(IReadOnlyList<string> filePaths)
{
return filePaths.Select(path => new FileInfo(path)).ToList();
}

public static void AddBlackFileFormats(List<string> formats)
{
foreach (var format in formats)
{
AddBlackFileFormat(format);
}
}

public static void AddBlackFileFormat(string format)
{
if (!_blackFileFormats.Contains(format))
{
_blackFileFormats.Add(format);
}
}

public static void RemoveBlackFileFormat(string format)
{
_blackFileFormats.Remove(format);
}

public static void AddBlackFiles(List<string> fileNames)
{
foreach (var fileName in fileNames)
{
AddBlackFile(fileName);
}
}

public static void AddBlackFile(string fileName)
{
if (!_blackFiles.Contains(fileName))
{
_blackFiles.Add(fileName);
}
}

public static void RemoveBlackFile(string fileName)
{
_blackFiles.Remove(fileName);
}

/// <summary>
/// Compare two directories.
/// </summary>
Expand All @@ -92,8 +26,8 @@ public void CompareDirectories(string dirA, string dirB)
{
ComparisonResult = new ComparisonResult();

var filesA = GetRelativeFilePaths(dirA, dirA).Where(f => !IsBlacklisted(f)).ToList();
var filesB = GetRelativeFilePaths(dirB, dirB).Where(f => !IsBlacklisted(f)).ToList();
var filesA = GetRelativeFilePaths(dirA, dirA).Where(f => !BlackListManager.Instance.IsBlacklisted(f)).ToList();
var filesB = GetRelativeFilePaths(dirB, dirB).Where(f => !BlackListManager.Instance.IsBlacklisted(f)).ToList();

ComparisonResult.AddUniqueToA(filesA.Except(filesB).Select(f => Path.Combine(dirA, f)));
ComparisonResult.AddUniqueToB(filesB.Except(filesA).Select(f => Path.Combine(dirB, f)));
Expand Down Expand Up @@ -187,14 +121,6 @@ private string GetRelativePath(string fromPath, string toPath)
return relativePath;
}

private bool IsBlacklisted(string relativeFilePath)
{
var fileName = Path.GetFileName(relativeFilePath);
var fileExtension = Path.GetExtension(relativeFilePath);

return _blackFiles.Contains(fileName) || _blackFileFormats.Contains(fileExtension);
}

private bool FilesAreEqual(string fileA, string fileB)
{
var sha256 = new Sha256HashAlgorithm();
Expand Down
4 changes: 2 additions & 2 deletions src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public override async Task ExecuteAsync()
//patch middleware
context.Add("SourcePath", patchPath);
context.Add("TargetPath", Packet.InstallPath);
context.Add("BlackFiles", GeneralFileManager.BlackFiles);
context.Add("BlackFileFormats", GeneralFileManager.BlackFileFormats);
context.Add("BlackFiles", BlackListManager.Instance.BlackFiles);
context.Add("BlackFileFormats", BlackListManager.Instance.BlackFileFormats);
//driver middleware
context.Add("DriverPath", new List<string>());
context.Add("Version", version.Version);
Expand Down
20 changes: 10 additions & 10 deletions src/c#/GeneralUpdate.Differential/DifferentialCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath =
var result = fileManager.ComparisonResult;

//Binary differencing of like terms .
foreach (var file in GeneralFileManager.ToFileInfoList(result.DifferentFiles))
foreach (var file in result.DifferentFiles.AsFileInfo())
{
var dirSeparatorChar = Path.DirectorySeparatorChar.ToString().ToCharArray();
var tempPath = file.FullName.Replace(targetPath, "").Replace(Path.GetFileName(file.FullName), "").TrimStart(dirSeparatorChar).TrimEnd(dirSeparatorChar);
Expand All @@ -97,11 +97,11 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath =
tempPath0 = Path.Combine(tempDir, $"{file.Name}{PATCH_FORMAT}");
}

var finOldFile = GeneralFileManager.ToFileInfoList(result.UniqueToA).FirstOrDefault(i => i.Name.Equals(file.Name));
var finOldFile = (result.UniqueToA.AsFileInfo()).FirstOrDefault(i => i.Name.Equals(file.Name));
var oldFile = finOldFile == null ? "" : finOldFile.FullName;
var newFile = file.FullName;
var extensionName = Path.GetExtension(file.FullName);
if (File.Exists(oldFile) && File.Exists(newFile) && !GeneralFileManager.BlackFileFormats.Contains(extensionName))
if (File.Exists(oldFile) && File.Exists(newFile) && !BlackListManager.Instance.BlackFileFormats.Contains(extensionName))
{
var hashAlgorithm = new Sha256HashAlgorithm();
if (hashAlgorithm.ComputeHash(oldFile)
Expand Down Expand Up @@ -148,14 +148,14 @@ public async Task Dirty(string appPath, string patchPath)
var fileManager = new GeneralFileManager();
fileManager.CompareDirectories(appPath, patchPath);
var result = fileManager.ComparisonResult;
var patchFiles = GeneralFileManager.ToFileInfoList(result.DifferentFiles);
var oldFiles = GeneralFileManager.ToFileInfoList(result.UniqueToA);
var patchFiles = result.DifferentFiles.AsFileInfo();
var oldFiles = result.UniqueToA.AsFileInfo();

//If a JSON file for the deletion list is found in the update package, it will be deleted based on its contents.
var deleteListJson = patchFiles.FirstOrDefault(i => i.Name.Equals(DELETE_FILES_NAME));
if (deleteListJson != null)
{
var deleteFiles = GeneralFileManager.ToFileInfoList(GeneralFileManager.GetJson<List<string>>(deleteListJson.FullName)) ;
var deleteFiles = GeneralFileManager.GetJson<List<string>>(deleteListJson.FullName).AsFileInfo() ;
var hashAlgorithm = new Sha256HashAlgorithm();
foreach (var file in deleteFiles)
{
Expand Down Expand Up @@ -204,8 +204,8 @@ public async Task Dirty(string appPath, string patchPath)
/// <param name="blackFileFormats">A collection of blacklist file name extensions that are skipped on update.</param>
public void SetBlocklist(List<string> blackFiles, List<string> blackFileFormats)
{
GeneralFileManager.AddBlackFiles(blackFiles);
GeneralFileManager.AddBlackFileFormats(blackFileFormats);
BlackListManager.Instance.AddBlackFiles(blackFiles);
BlackListManager.Instance.AddBlackFileFormats(blackFileFormats);
}

#endregion Public Methods
Expand Down Expand Up @@ -245,10 +245,10 @@ private Task DirtyUnknow(string appPath, string patchPath)
var fileManager = new GeneralFileManager();
fileManager.CompareDirectories(appPath, patchPath);
var result = fileManager.ComparisonResult;
foreach (var file in GeneralFileManager.ToFileInfoList(result.DifferentFiles))
foreach (var file in (result.DifferentFiles.AsFileInfo()))
{
var extensionName = Path.GetExtension(file.FullName);
if (GeneralFileManager.BlackFileFormats.Contains(extensionName)) continue;
if (BlackListManager.Instance.BlackFileFormats.Contains(extensionName)) continue;
var targetFileName = file.FullName.Replace(patchPath, "").TrimStart("\\".ToCharArray());
var targetPath = Path.Combine(appPath, targetFileName);
var parentFolder = Directory.GetParent(targetPath);
Expand Down

0 comments on commit 0e03e47

Please sign in to comment.