tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
Read a cache record apart from judging it
- Commit
33eadabaa439ac64feb3602d66cbbed992d803b7- Author
- Leitwolf11 ·
- Parents
3d940941c91c
Changes
src/Progmasoft.Tazel/Cli/CacheCommands.cs | 65 ++++++++++++++++++------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/src/Progmasoft.Tazel/Cli/CacheCommands.cs b/src/Progmasoft.Tazel/Cli/CacheCommands.cs index a294cc0..28d9fbb 100644 --- a/src/Progmasoft.Tazel/Cli/CacheCommands.cs +++ b/src/Progmasoft.Tazel/Cli/CacheCommands.cs @@ -64,8 +64,8 @@ internal sealed class CacheChecks(string root) return new CacheCensus(0, 0, [], []); } - // A pass is kept under the log directory's path with / as _ (a test of the root package with a _ before it); - // the log must be there. + // A pass is kept under the path of its log directory with every / written as _, and a test of the root package + // gets one more _ in front. The log must be there. var logged = Directory.EnumerateFiles(logs, "test.log", SearchOption.AllDirectories) .Select(file => Path.GetRelativePath(logs, Path.GetDirectoryName(file)!).Replace('\\', '/')) .SelectMany(directory => new[] { directory.Replace('/', '_'), "_" + directory.Replace('/', '_') }) @@ -134,6 +134,38 @@ internal sealed class CacheChecks(string root) } private static RecordState Classify(string record, out string? reason) + { + if (ReadRecord(record, out reason) is not { } files) + { + return RecordState.Broken; + } + + var output = record[..^".action".Length]; + if (!files.TryGetValue(output, out var recorded)) + { + reason = "it does not name the output it is kept beside"; + return RecordState.Broken; + } + + var changed = files.Where(entry => DigestOf(entry.Key) != entry.Value).Select(entry => entry.Key).ToList(); + if (changed.Count == 0) + { + return RecordState.Valid; + } + + if (changed is [var only] && only == output) + { + reason = DigestOf(output) is null + ? $"{output} is gone while its inputs are as recorded" + : $"{output} changed while its inputs are as recorded (it was {recorded[..12]}…)"; + return RecordState.Broken; + } + + return RecordState.Stale; + } + + /// <summary>The files and digests a record lists, or null with the reason it is broken.</summary> + private static Dictionary<string, string>? ReadRecord(string record, out string? reason) { reason = null; string[] lines; @@ -144,14 +176,14 @@ internal sealed class CacheChecks(string root) catch (Exception error) when (error is IOException or UnauthorizedAccessException) { reason = $"it cannot be read: {error.Message}"; - return RecordState.Broken; + return null; } var files = new Dictionary<string, string>(StringComparer.Ordinal); if (lines.Length == 0 || !lines[0].StartsWith("key ", StringComparison.Ordinal) || !IsDigest(lines[0][4..])) { reason = "it does not start with the key of its action"; - return RecordState.Broken; + return null; } foreach (var line in lines.Skip(1)) @@ -160,34 +192,13 @@ internal sealed class CacheChecks(string root) if (parts.Length != 3 || parts[0] != "file" || !IsDigest(parts[1]) || parts[2].Length == 0) { reason = $"'{line}' is not a line of a record"; - return RecordState.Broken; + return null; } files[parts[2]] = parts[1]; } - var output = record[..^".action".Length]; - if (!files.TryGetValue(output, out var recorded)) - { - reason = "it does not name the output it is kept beside"; - return RecordState.Broken; - } - - var changed = files.Where(entry => DigestOf(entry.Key) != entry.Value).Select(entry => entry.Key).ToList(); - if (changed.Count == 0) - { - return RecordState.Valid; - } - - if (changed is [var only] && only == output) - { - reason = DigestOf(output) is null - ? $"{output} is gone while its inputs are as recorded" - : $"{output} changed while its inputs are as recorded (it was {recorded[..12]}…)"; - return RecordState.Broken; - } - - return RecordState.Stale; + return files; } private static string? DigestOf(string file)