mirror of
https://github.com/Pathoschild/SMAPI.git
synced 2026-10-11 22:19:56 +00:00
fix malware blacklist compatibility with SMAPI 4.5.2 and earlier
The revamped blacklist is more compact, which triggers the bug fixed in f090df0db3 (where syncing a smaller new version corrupts the file). This pads the file to the largest previous blacklist size to avoid the issue.
This commit is contained in:
@@ -20,6 +20,10 @@ internal class MalwareBlacklistJob : IBackgroundJob
|
||||
/*********
|
||||
** Fields
|
||||
*********/
|
||||
/// <summary>The minimum size in bytes for the generated malware blacklist file.</summary>
|
||||
/// <remarks>The generated file is padded to this length, to avoid a bug in SMAPI 4.5.2 and earlier where a smaller newer version corrupts the file (since it overwrites the previous file without truncating).</remarks>
|
||||
private const int MinBlacklistBytes = 8589;
|
||||
|
||||
/// <summary>The HTTP client for fetching data from GitHub.</summary>
|
||||
private readonly IGitHubClient GitHubClient;
|
||||
|
||||
@@ -64,6 +68,7 @@ internal class MalwareBlacklistJob : IBackgroundJob
|
||||
MalwareBlacklistConfig config = this.Config.Value;
|
||||
string repo = config.GitHubRepo ?? throw new InvalidOperationException("Can't update the malware blacklist: no GitHub repository is configured.");
|
||||
string outputPath = Path.Combine(this.WebRootPath, Startup.BlacklistFileName);
|
||||
UTF8Encoding encoding = new(encoderShouldEmitUTF8Identifier: false);
|
||||
|
||||
// fetch file
|
||||
GitFile file;
|
||||
@@ -93,10 +98,15 @@ internal class MalwareBlacklistJob : IBackgroundJob
|
||||
string publicJson = MalwareBlacklistConverter.ToPublicJson(blacklist);
|
||||
context.WriteLine($"Updated malware blacklist ({blacklist.Blacklist.Length} mod entries and {blacklist.LooseFileBlacklist.Length} loose file entries).");
|
||||
|
||||
// pad to minimum size
|
||||
int byteCount = encoding.GetByteCount(publicJson);
|
||||
if (byteCount < MalwareBlacklistJob.MinBlacklistBytes)
|
||||
publicJson += new string(' ', MalwareBlacklistJob.MinBlacklistBytes - byteCount);
|
||||
|
||||
// save file
|
||||
{
|
||||
string tempPath = $"{outputPath}.tmp";
|
||||
await File.WriteAllTextAsync(tempPath, publicJson, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
|
||||
await File.WriteAllTextAsync(tempPath, publicJson, encoding);
|
||||
File.Move(tempPath, outputPath, overwrite: true);
|
||||
|
||||
context.WriteLine($"Saved to {Startup.BlacklistFileName}.");
|
||||
|
||||
Reference in New Issue
Block a user