Skip to content

Commit d1e1083

Browse files
authored
refactor: migrate to DefaultBlackListMatcher, deprecate BlackListManager (#415)
Replace BlackListManager singleton with DefaultBlackListMatcher injection via StorageManager.BlackListMatcher static property. - BlackListDefaults: new static class with built-in defaults - DefaultBlackListMatcher.FromConfigInfo(): factory from GlobalConfigInfo - StorageManager.BlackListMatcher: injectable matcher, fallback to old - All call sites use GlobalConfigInfo properties directly instead of BlackListManager.Instance for accumulation + ProcessInfo building - BlackListManager marked [Obsolete] Closes #412
1 parent 19c3a52 commit d1e1083

7 files changed

Lines changed: 81 additions & 26 deletions

File tree

src/c#/GeneralUpdate.Core/Bootstrap/GeneralUpdateBootstrap.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ private void InitializeFromEnvironment()
196196
var processInfo = new EncryptedFileProcessInfoProvider().Receive();
197197
if (processInfo == null) return;
198198

199-
BlackListManager.Instance.AddBlackFormats(processInfo.BlackFileFormats);
200-
BlackListManager.Instance.AddBlackFiles(processInfo.BlackFiles);
201-
BlackListManager.Instance.AddSkipDirectorys(processInfo.SkipDirectorys);
202-
203199
_configInfo = new GlobalConfigInfo
204200
{
205201
MainAppName = processInfo.AppName,
@@ -217,8 +213,13 @@ private void InitializeFromEnvironment()
217213
BackupDirectory = processInfo.BackupDirectory,
218214
Scheme = processInfo.Scheme,
219215
Token = processInfo.Token,
220-
DriverDirectory = processInfo.DriverDirectory
216+
DriverDirectory = processInfo.DriverDirectory,
217+
BlackFiles = processInfo.BlackFiles ?? BlackListDefaults.DefaultBlackFiles,
218+
BlackFormats = processInfo.BlackFileFormats ?? BlackListDefaults.DefaultBlackFormats,
219+
SkipDirectorys = processInfo.SkipDirectorys ?? BlackListDefaults.DefaultSkipDirectories
221220
};
221+
222+
StorageManager.BlackListMatcher = DefaultBlackListMatcher.FromConfigInfo(_configInfo);
222223
}
223224

224225
/// <summary>
@@ -285,9 +286,14 @@ private async Task LaunchSilentAsync()
285286

286287
private void InitBlackList()
287288
{
288-
BlackListManager.Instance.AddBlackFiles(_configInfo.BlackFiles);
289-
BlackListManager.Instance.AddBlackFormats(_configInfo.BlackFormats);
290-
BlackListManager.Instance.AddSkipDirectorys(_configInfo.SkipDirectorys);
289+
// Build blacklist matcher from GlobalConfigInfo and set on StorageManager.
290+
// The matcher combines user config with system defaults.
291+
var effectiveConfig = new BlackListConfig(
292+
_configInfo.BlackFiles?.Count > 0 ? _configInfo.BlackFiles : BlackListDefaults.DefaultBlackFiles,
293+
_configInfo.BlackFormats?.Count > 0 ? _configInfo.BlackFormats : BlackListDefaults.DefaultBlackFormats,
294+
_configInfo.SkipDirectorys?.Count > 0 ? _configInfo.SkipDirectorys : BlackListDefaults.DefaultSkipDirectories
295+
);
296+
StorageManager.BlackListMatcher = new DefaultBlackListMatcher(effectiveConfig);
291297
}
292298

293299
private async Task CallSmallBowlHomeAsync(string processName)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
3+
namespace GeneralUpdate.Core.FileSystem;
4+
5+
/// <summary>Built-in default blacklist items — previously hardcoded in BlackListManager.</summary>
6+
public static class BlackListDefaults
7+
{
8+
/// <summary>Default blacklisted files (system DLLs that ship with the runtime).</summary>
9+
public static readonly List<string> DefaultBlackFiles = new()
10+
{
11+
"Microsoft.Bcl.AsyncInterfaces.dll",
12+
"System.Collections.Immutable.dll",
13+
"System.IO.Pipelines.dll",
14+
"System.Text.Encodings.Web.dll",
15+
"System.Text.Json.dll"
16+
};
17+
18+
/// <summary>Default blacklisted file extensions.</summary>
19+
public static readonly List<string> DefaultBlackFormats = new()
20+
{ ".patch", ".pdb", ".rar", ".tar", ".json", Configuration.Format.ZIP };
21+
22+
/// <summary>Default skipped directory prefixes.</summary>
23+
public static readonly List<string> DefaultSkipDirectories = new()
24+
{ "app-", "fail" };
25+
}

src/c#/GeneralUpdate.Core/FileSystem/BlackListManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ public interface IBlackListMatcher
1515
}
1616

1717
/// <summary>
18-
/// Thread-safe blacklist manager. Uses Lazy<T> singleton.
18+
/// Thread-safe blacklist manager. Uses Lazy&lt;T&gt; singleton.
1919
/// Matching is case-insensitive and supports prefix matching for skip directories.
2020
/// </summary>
21+
[Obsolete("Use DefaultBlackListMatcher + StorageManager.BlackListMatcher instead. See #412.")]
2122
public class BlackListManager : IBlackListMatcher
2223
{
2324
private static readonly Lazy<BlackListManager> _lazy = new(() => new BlackListManager());

src/c#/GeneralUpdate.Core/FileSystem/DefaultBlackListMatcher.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public class DefaultBlackListMatcher : IBlackListMatcher
1313
public DefaultBlackListMatcher(BlackListConfig config)
1414
=> _config = config ?? throw new ArgumentNullException(nameof(config));
1515

16+
/// <summary>Create a matcher from GlobalConfigInfo blacklist properties.</summary>
17+
public static DefaultBlackListMatcher FromConfigInfo(GlobalConfigInfo config)
18+
{
19+
var cfg = new BlackListConfig(
20+
config.BlackFiles?.Count > 0 ? config.BlackFiles : null,
21+
config.BlackFormats?.Count > 0 ? config.BlackFormats : null,
22+
config.SkipDirectorys?.Count > 0 ? config.SkipDirectorys : null);
23+
return new DefaultBlackListMatcher(cfg);
24+
}
25+
1626
public bool IsBlacklisted(string relativeFilePath)
1727
{
1828
var fileName = Path.GetFileName(relativeFilePath);

src/c#/GeneralUpdate.Core/FileSystem/StorageManager.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public sealed class StorageManager
1313
{
1414
private long _fileCount = 0;
1515
public const string DirectoryName = "app-";
16+
17+
/// <summary>Optional blacklist matcher. When set, takes precedence over BlackListManager.</summary>
18+
public static IBlackListMatcher? BlackListMatcher { get; set; }
1619

1720
private ComparisonResult ComparisonResult { get; set; }
1821

@@ -264,7 +267,9 @@ private IEnumerable<FileNode> ReadFileNode(string path, string rootPath = null)
264267

265268
foreach (var subPath in Directory.EnumerateFiles(path))
266269
{
267-
if (BlackListManager.Instance.IsBlacklisted(subPath)) continue;
270+
#pragma warning disable CS0618 // Obsolete fallback
271+
if ((BlackListMatcher ?? BlackListManager.Instance).IsBlacklisted(subPath)) continue;
272+
#pragma warning restore CS0618
268273

269274
var hashAlgorithm = new Sha256HashAlgorithm();
270275
var hash = hashAlgorithm.ComputeHash(subPath);
@@ -283,7 +288,9 @@ private IEnumerable<FileNode> ReadFileNode(string path, string rootPath = null)
283288

284289
foreach (var subPath in Directory.EnumerateDirectories(path))
285290
{
286-
if (BlackListManager.Instance.ShouldSkipDirectory(subPath)) continue;
291+
#pragma warning disable CS0618 // Obsolete fallback
292+
if ((BlackListMatcher ?? BlackListManager.Instance).ShouldSkipDirectory(subPath)) continue;
293+
#pragma warning restore CS0618
287294
resultFiles.AddRange(ReadFileNode(subPath, rootPath));
288295
}
289296

src/c#/GeneralUpdate.Core/Silent/SilentPollOrchestrator.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,13 @@ private async Task PrepareUpdateIfNeededAsync(CancellationToken token)
147147
catch (Exception ex) { GeneralTracer.Warn($"Hook OnBeforeUpdateAsync failed: {ex.Message}"); }
148148
}
149149

150-
// Configure for update
151-
BlackListManager.Instance?.AddBlackFiles(_configInfo.BlackFiles);
152-
BlackListManager.Instance?.AddBlackFormats(_configInfo.BlackFormats);
153-
BlackListManager.Instance?.AddSkipDirectorys(_configInfo.SkipDirectorys);
150+
// Configure matcher from config with defaults
151+
var effectiveConfig = new BlackListConfig(
152+
_configInfo.BlackFiles?.Count > 0 ? _configInfo.BlackFiles : BlackListDefaults.DefaultBlackFiles,
153+
_configInfo.BlackFormats?.Count > 0 ? _configInfo.BlackFormats : BlackListDefaults.DefaultBlackFormats,
154+
_configInfo.SkipDirectorys?.Count > 0 ? _configInfo.SkipDirectorys : BlackListDefaults.DefaultSkipDirectories
155+
);
156+
StorageManager.BlackListMatcher = new DefaultBlackListMatcher(effectiveConfig);
154157

155158
_configInfo.LastVersion = latestVersion;
156159
_configInfo.UpdateVersions = new List<VersionInfo>(); // legacy compat
@@ -160,14 +163,14 @@ private async Task PrepareUpdateIfNeededAsync(CancellationToken token)
160163

161164
// Backup
162165
StorageManager.Backup(_configInfo.InstallPath, _configInfo.BackupDirectory,
163-
BlackListManager.Instance.SkipDirectorys);
166+
_configInfo.SkipDirectorys ?? BlackListDefaults.DefaultSkipDirectories);
164167

165168
// Build ProcessInfo and store for IPC delivery on process exit
166169
_preparedProcessInfo = ConfigurationMapper.MapToProcessInfo(
167170
_configInfo, new List<VersionInfo>(),
168-
BlackListManager.Instance.BlackFormats.ToList(),
169-
BlackListManager.Instance.BlackFiles.ToList(),
170-
BlackListManager.Instance.SkipDirectorys.ToList());
171+
_configInfo.BlackFormats ?? BlackListDefaults.DefaultBlackFormats,
172+
_configInfo.BlackFiles ?? BlackListDefaults.DefaultBlackFiles,
173+
_configInfo.SkipDirectorys ?? BlackListDefaults.DefaultSkipDirectories);
171174
_configInfo.ProcessInfo = JsonSerializer.Serialize(_preparedProcessInfo, ProcessInfoJsonContext.Default.ProcessInfo);
172175

173176
// ═══ Reporter: update started ═══

src/c#/GeneralUpdate.Core/Strategy/ClientUpdateStrategy.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ private async Task ExecuteStandardWorkflowAsync()
173173

174174
var processInfo = ConfigurationMapper.MapToProcessInfo(
175175
_configInfo, downloadVersions,
176-
BlackListManager.Instance.BlackFormats.ToList(),
177-
BlackListManager.Instance.BlackFiles.ToList(),
178-
BlackListManager.Instance.SkipDirectorys.ToList());
176+
_configInfo.BlackFormats ?? BlackListDefaults.DefaultBlackFormats,
177+
_configInfo.BlackFiles ?? BlackListDefaults.DefaultBlackFiles,
178+
_configInfo.SkipDirectorys ?? BlackListDefaults.DefaultSkipDirectories);
179179

180180
// Keep JSON string for backward compatibility (GlobalConfigInfo.ProcessInfo)
181181
_configInfo.ProcessInfo = JsonSerializer.Serialize(processInfo,
@@ -243,16 +243,19 @@ private static IStrategy ResolveOsStrategy()
243243

244244
private void InitBlackList()
245245
{
246-
BlackListManager.Instance.AddBlackFiles(_configInfo!.BlackFiles);
247-
BlackListManager.Instance.AddBlackFormats(_configInfo.BlackFormats);
248-
BlackListManager.Instance.AddSkipDirectorys(_configInfo.SkipDirectorys);
246+
var effectiveConfig = new BlackListConfig(
247+
_configInfo!.BlackFiles?.Count > 0 ? _configInfo.BlackFiles : BlackListDefaults.DefaultBlackFiles,
248+
_configInfo.BlackFormats?.Count > 0 ? _configInfo.BlackFormats : BlackListDefaults.DefaultBlackFormats,
249+
_configInfo.SkipDirectorys?.Count > 0 ? _configInfo.SkipDirectorys : BlackListDefaults.DefaultSkipDirectories
250+
);
251+
StorageManager.BlackListMatcher = new DefaultBlackListMatcher(effectiveConfig);
249252
}
250253

251254
private void Backup()
252255
{
253256
GeneralTracer.Info($"ClientUpdateStrategy: backing up {_configInfo!.InstallPath} -> {_configInfo.BackupDirectory}");
254257
StorageManager.Backup(_configInfo.InstallPath, _configInfo.BackupDirectory,
255-
BlackListManager.Instance.SkipDirectorys);
258+
_configInfo.SkipDirectorys ?? BlackListDefaults.DefaultSkipDirectories);
256259
}
257260

258261
private bool CanSkip(bool isForcibly, UpdateInfoEventArgs updateInfo)

0 commit comments

Comments
 (0)