-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathBlackListManager.cs
More file actions
88 lines (74 loc) · 3.56 KB
/
Copy pathBlackListManager.cs
File metadata and controls
88 lines (74 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GeneralUpdate.Core.Configuration;
namespace GeneralUpdate.Core.FileSystem;
/// <summary>Matches files/directories against a blacklist configuration.</summary>
public interface IBlackListMatcher
{
bool IsBlacklisted(string relativeFilePath);
bool IsBlacklistedFormat(string extension);
bool ShouldSkipDirectory(string directoryName);
}
/// <summary>
/// Thread-safe blacklist manager. Uses Lazy<T> singleton.
/// Matching is case-insensitive and supports prefix matching for skip directories.
/// </summary>
[Obsolete("Use DefaultBlackListMatcher + StorageManager.BlackListMatcher instead. See #412.")]
public class BlackListManager : IBlackListMatcher
{
private static readonly Lazy<BlackListManager> _lazy = new(() => new BlackListManager());
private readonly object _lock = new();
private readonly List<string> _blackFiles =
[
"Microsoft.Bcl.AsyncInterfaces.dll",
"System.Collections.Immutable.dll",
"System.IO.Pipelines.dll",
"System.Text.Encodings.Web.dll",
"System.Text.Json.dll"
];
private readonly List<string> _blackFormats = [".patch", ".pdb", ".rar", ".tar", ".json", Format.ZIP];
private readonly List<string> _skipDirs = ["app-", "fail"];
private BlackListManager() { }
public static BlackListManager Instance => _lazy.Value;
// Read-only accessors
public IReadOnlyList<string> BlackFiles { get { lock (_lock) return _blackFiles.ToList(); } }
public IReadOnlyList<string> BlackFormats { get { lock (_lock) return _blackFormats.ToList(); } }
public IReadOnlyList<string> SkipDirectorys { get { lock (_lock) return _skipDirs.ToList(); } }
// Mutation
public void AddBlackFiles(List<string>? files)
{ if (files == null) return; lock (_lock) { foreach (var f in files) AddBlackFileLocked(f); } }
public void AddBlackFile(string file)
{ if (string.IsNullOrWhiteSpace(file)) return; lock (_lock) AddBlackFileLocked(file); }
private void AddBlackFileLocked(string file)
{ if (!_blackFiles.Contains(file)) _blackFiles.Add(file); }
public void AddBlackFormats(List<string>? formats)
{ if (formats == null) return; lock (_lock) { foreach (var f in formats) AddBlackFormatLocked(f); } }
public void AddBlackFormat(string format)
{ if (string.IsNullOrWhiteSpace(format)) return; lock (_lock) AddBlackFormatLocked(format); }
private void AddBlackFormatLocked(string format)
{ if (!_blackFormats.Contains(format)) _blackFormats.Add(format); }
public void AddSkipDirectorys(List<string>? dirs)
{ if (dirs == null) return; lock (_lock) { foreach (var d in dirs) AddSkipDirectoryLocked(d); } }
public void AddSkipDirectory(string dir)
{ if (string.IsNullOrWhiteSpace(dir)) return; lock (_lock) AddSkipDirectoryLocked(dir); }
private void AddSkipDirectoryLocked(string dir)
{ if (!_skipDirs.Contains(dir)) _skipDirs.Add(dir); }
// Matching (read operations — no lock needed for immutable reads)
public bool IsBlacklisted(string relativeFilePath)
{
var fileName = Path.GetFileName(relativeFilePath);
var ext = Path.GetExtension(relativeFilePath);
lock (_lock)
return _blackFiles.Contains(fileName) || _blackFormats.Contains(ext);
}
public bool IsBlacklistedFormat(string extension)
{
lock (_lock) return _blackFormats.Contains(extension);
}
public bool ShouldSkipDirectory(string directoryName)
{
lock (_lock) return _skipDirs.Any(d => directoryName.Contains(d));
}
}