Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/tasks/Common/FileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public FileCache(string? cacheFilePath, TaskLoggingHelper log)
Enabled = true;
if (File.Exists(cacheFilePath))
{
_oldCache = JsonSerializer.Deserialize<CompilerCache>(File.ReadAllText(cacheFilePath), s_jsonOptions);
using FileStream fs = File.OpenRead(cacheFilePath);
_oldCache = JsonSerializer.Deserialize<CompilerCache>(fs, s_jsonOptions);
}

_oldCache ??= new();
Expand Down
104 changes: 101 additions & 3 deletions src/tasks/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,112 @@ public static (int, string) TryRunProcess(
return (process.ExitCode, outputBuilder.ToString().Trim('\r', '\n'));
}

private static bool FilesTextEqual(string filePath1, string filePath2)
{
const int bufferSize = 4096;
char[] bufferA = new char[bufferSize];
char[] bufferB = new char[bufferSize];

using StreamReader srA = new StreamReader(filePath1, Encoding.UTF8, true, bufferSize);
using StreamReader srB = new StreamReader(filePath2, Encoding.UTF8, true, bufferSize);

int readA = 0;
int readB = 0;
int consumedA = 0;
int consumedB = 0;

while (true)
{
if (consumedA == readA)
{
readA = srA.Read(bufferA, 0, bufferSize);
consumedA = 0;
}

if (consumedB == readB)
{
readB = srB.Read(bufferB, 0, bufferSize);
consumedB = 0;
}

if (readA == 0 && readB == 0)
return true;

if (readA == 0 || readB == 0)
return false;

int overlap = Math.Min(readA - consumedA, readB - consumedB);
if (!SequenceEqual(bufferA, consumedA, bufferB, consumedB, overlap))
return false;

consumedA += overlap;
consumedB += overlap;
}
}

private static bool FilesEqual(string filePath1, string filePath2)
{
const int bufferSize = 8192;
byte[] bufferA = new byte[bufferSize];
byte[] bufferB = new byte[bufferSize];

using FileStream fsA = new FileStream(filePath1, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.SequentialScan);
using FileStream fsB = new FileStream(filePath2, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.SequentialScan);

int readA = 0;
int readB = 0;
int consumedA = 0;
int consumedB = 0;

while (true)
{
if (consumedA == readA)
{
readA = fsA.Read(bufferA, 0, bufferSize);
consumedA = 0;
}

if (consumedB == readB)
{
readB = fsB.Read(bufferB, 0, bufferSize);
consumedB = 0;
}

if (readA == 0 && readB == 0)
return true;

if (readA == 0 || readB == 0)
return false;

int overlap = Math.Min(readA - consumedA, readB - consumedB);
if (!SequenceEqual(bufferA, consumedA, bufferB, consumedB, overlap))
return false;

consumedA += overlap;
consumedB += overlap;
}
}

private static bool SequenceEqual<T>(T[] arrayA, int offsetA, T[] arrayB, int offsetB, int count) where T : IEquatable<T>
{
#if NET
return arrayA.AsSpan(offsetA, count).SequenceEqual(arrayB.AsSpan(offsetB, count));
#else
for (int i = 0; i < count; i++)
{
if (arrayA[offsetA + i].Equals (arrayB[offsetB + i]))
return false;
}
return true;
#endif
}

public static bool CopyIfDifferent(string src, string dst, bool useHash)
{
if (!File.Exists(src))
throw new ArgumentException($"Cannot find {src} file to copy", nameof(src));

bool areDifferent = !File.Exists(dst) ||
(useHash && ComputeHash(src) != ComputeHash(dst)) ||
(File.ReadAllText(src) != File.ReadAllText(dst));
bool areDifferent = !File.Exists(dst) || (useHash && !FilesEqual(src, dst)) || !FilesTextEqual(src, dst);

if (areDifferent)
File.Copy(src, dst, true);
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ protected virtual void UpdateRuntimeConfigJson()
if (matchingAssemblies.Length > 1)
throw new LogAsErrorException($"Found more than one assembly matching the main assembly name {MainAssemblyName}: {string.Join(",", matchingAssemblies)}");

var rootNode = JsonNode.Parse(File.ReadAllText(RuntimeConfigJsonPath),
new JsonNodeOptions { PropertyNameCaseInsensitive = true });
using FileStream rcs = File.OpenRead(RuntimeConfigJsonPath);
var rootNode = JsonNode.Parse(rcs, new JsonNodeOptions { PropertyNameCaseInsensitive = true });
if (rootNode == null)
throw new LogAsErrorException($"Failed to parse {RuntimeConfigJsonPath}");

Expand Down
Loading