using System.Globalization; using Chronos.Core.Archiving; using Chronos.Core.Diagnostics; using Chronos.Core.Model; namespace Chronos.Data.Projects; /// What a reconcile of a sequence's projects met (). /// The sequence has no projects folder: nothing was read, and nothing taken for gone. /// The projects the archive has a folder for, live or in the projects' trash, whether or not its record read. /// The live projects the index could not be brought in line with: a record that cannot be read (damaged, /// of a newer Chronos, a read failure), a head whose version this Chronos cannot read, or a row the index refused. Each stays /// as the index has it, and nothing is written for it until it reads again. /// The records of the live projects read, by id (their head as moved forward). internal sealed record ProjectReconcile(bool Missing, IReadOnlySet Seen, IReadOnlySet HeldBack, IReadOnlyDictionary Live) { /// Every live project of the folder was read and indexed: a sequence without a Main may be given one. public bool Complete => HeldBack.Count == 0; } /// /// Brings the index's projects of one sequence in line with their sidecars (owner review 2, R8), which are the truth: a /// project or version the index lacks is added (an index rebuilt, a sequence restored from the trash), a head the index is /// behind on moves forward, a name or deletion recorded since is taken, and a row the index marked deleted is listed again /// when its live record says it is not. Only a record that says so, live or in the projects' trash, deletes a project: /// never a folder that is not there (moved for a moment, a backup put back, a sync tool) nor a record that cannot be read, /// whose project stays as the index has it (). A version file written past its /// record's head by a push a crash cut short is adopted when it follows the head (its file is complete: versions are /// written atomically, then the record); any other file past the head, or a second file of a number the record's chain /// names another for, is moved aside to versions/.stray, never deleted. Runs under the sequence's lease, or without /// writing to the archive at all when it is read-only. /// internal sealed class ProjectIndexer(ChronosDatabase db) { private const string StrayFolder = ".stray"; /// Reconciles the projects of in ; the caller holds the sequence's lease. /// False for a read-only archive: the sidecars are read as they are and only the index is written /// (a version past its record's head is indexed, not adopted into the record; nothing is moved aside). public async Task ReconcileAsync(SequencePaths folder, Guid sequenceId, CancellationToken cancellationToken, bool writable = true) { var seen = new HashSet(); var heldBack = new HashSet(); var live = new Dictionary(); if (!Directory.Exists(folder.ProjectsDirectory)) return new ProjectReconcile(true, seen, heldBack, live); foreach (var (directory, trashed) in ProjectFolders(folder)) { cancellationToken.ThrowIfCancellationRequested(); ProjectRecord record; try { record = ProjectSidecars.ReadRecord(Path.Combine(directory, ArchiveLayout.ProjectFile)); } catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) { // Its folder names it (, or -