Files
Chronos/src/Chronos.Data/Workspaces/WorkspaceStore.cs
T
Alby96andClaude Opus 5.5 279ebf437f fix(workspace): a deleted project asks nothing, prunes and copies clean up only after what ended well
Owner review 2, phase 5a (R8), fix round 4.

A project the library no longer has now reads as Gone with no conflict to
answer. The conflict stays recorded in sync.json, so Save both on a deleted
project says Gone once and offers its copy. It no longer reopens the conflict
dialog. Keep mine and Take the library's first check that the home still has
the project. Neither is merged onto the last version a deleted project had,
so the working state and the unsent versions stay as they were.

Old files in versions/.replaced are pruned only after a push that ended well,
never after one that stopped at a conflict. A file an earlier build set aside
kept the time its version was written. It now counts as set aside no earlier
than when this build first knew the folder (versions/.replaced.since), so an
upgrade does not prune it by an old mtime. The Merge mark goes on the replayed
version whose own change met a setting the conflict listed. A version that
only rides along with a working-state conflict stays a commit.

A first copy cancelled while paused or failed, which no handler runs, now has
its partial media removed under the project's lease, as a running one does
(ICancelledJobHandler). The space estimate counts a proxy held under its
<id>-<sha16>.jpg name. The frames stamp carries a weighted SQL checksum of
the hashes, so a hash replaced in place (a Verify) makes the workspace read
the frames again.

Desktop: a removal that cannot move a file puts back what it moved, so the
copy stays whole and listed, and the failure is said. The Removed toast names
a damaged copy as it is known, never by its folder id. One copy that cannot
be listed no longer hides the others. A Main this Chronos cannot read (newer
or damaged) is refused at once with the reason, and a damaged one offers
Remove local copy. Save version is off on a Gone project, and Send/Delete
there say the project was deleted in the library.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 14:07:35 +01:00

597 lines
29 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.IO;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
namespace Chronos.Data.Workspaces;
/// <summary>The manifest of a workspace folder (<c>workspace.json</c>).</summary>
public sealed record WorkspaceManifest(string Format, int FormatVersion, Guid Id, DateTimeOffset Created);
/// <summary>
/// A workspace's sync state of one project besides its versions (<c>sync.json</c>): a restore to record, a conflict to
/// settle, and the journal of a replay or a save-both under way, which the next sync finishes or undoes after a crash.
/// </summary>
/// <param name="RestoredFrom">The version a restore brought back; the next commit is a "restore" of it.</param>
/// <param name="Conflict">The conflict a pull or push stopped at, until the user settles it.</param>
/// <param name="Replay">A replay (or a base moved onto local commits the home holds) written in steps; null when none is under way.</param>
/// <param name="SaveBoth">A save-both that made its copy at the home and has not finished; null when none is under way.</param>
public sealed record WorkspaceSyncState(Guid? RestoredFrom, WorkspaceConflict? Conflict, WorkspaceReplay? Replay = null, WorkspaceSaveBoth? SaveBoth = null)
{
public static WorkspaceSyncState None { get; } = new(null, null);
/// <summary>Nothing to keep: the file can go.</summary>
[JsonIgnore]
public bool IsEmpty => RestoredFrom is null && Conflict is null && Replay is null && SaveBoth is null;
}
/// <summary>
/// The journal of a replay (<see cref="WorkspaceSync"/>): the base it moves the project onto, the local versions it
/// replaces, the replayed ones it writes and the local versions the home already holds (their answer lost), whose files
/// take the home's copy. The bundle's base is the commit point: once it is <see cref="OntoBase"/> the replay is finished
/// (the replaced set aside, the held ones adopted), before it the replayed ones are set aside and nothing else moved.
/// </summary>
/// <param name="Settles">
/// The replay settles the conflict recorded beside it (a pull, a push or a choice that merged): once its base is
/// <see cref="OntoBase"/> that conflict is over, the choice made applied. False for a base moved onto versions the home
/// held, which leaves the conflict to settle (and for a journal an earlier build wrote).
/// </param>
public sealed record WorkspaceReplay(Guid OntoBase, IReadOnlyList<Guid> Replaced, IReadOnlyList<Guid> Replayed, IReadOnlyList<Guid> Adopted,
bool Settles = false);
/// <summary>
/// A save-both under way (<see cref="WorkspaceSync"/>): the copy made at the home and its first version (the base the
/// local versions are sent on top of), the base this project had when it started, and the id each local version is sent
/// under, so a retry sends to the same copy what it lacks and the home answers "already stored" for the rest. Before the
/// copy is asked of the home its name and the version it is made from are journaled (<see cref="Intent"/>, the copy's ids
/// still empty): a retry after a cut finds the copy the home made by them instead of making another.
/// </summary>
public sealed record WorkspaceSaveBoth(Guid CopyId, Guid CopyBaseId, int CopyBaseNumber, Guid? FromBase, IReadOnlyList<WorkspaceMove> Moves,
WorkspaceCopyIntent? Intent = null)
{
/// <summary>The copy is asked of the home but not known yet: only its <see cref="Intent"/> is.</summary>
[JsonIgnore]
public bool Intended => CopyId == Guid.Empty;
}
/// <summary>
/// The copy a save-both asks the home for, journaled before it asks: its name, the version it is made from (null: Main's
/// head) with that version's content hash, and when it was asked. A project of that name made since, whose only version
/// holds that content, is that copy.
/// </summary>
public sealed record WorkspaceCopyIntent(string Name, Guid? From, string? FromHash, DateTimeOffset Asked);
/// <summary>A local version (<paramref name="LocalId"/>) and the id it is sent under to a save-both's copy.</summary>
public sealed record WorkspaceMove(Guid LocalId, Guid CopyVersionId);
/// <summary>A version file of a workspace that cannot be read (<see cref="WorkspaceProject.UnreadableVersions"/>): the number and id its name carries, and why.</summary>
public sealed record UnreadableVersion(int Number, Guid Id, string Path, string Reason);
/// <summary>
/// A pull or push that met real conflicts (owner review 2, R8): the home's head it met, the version the local commits were
/// made from, the local commits (oldest first) and the members both sides changed (mine, theirs). Settled by keeping mine,
/// taking the server's (each member or all), or saving both (<see cref="WorkspaceSync.ResolveAsync"/>).
/// </summary>
public sealed record WorkspaceConflict(Guid TheirsVersionId, int TheirsNumber, Guid BaseVersionId, IReadOnlyList<Guid> Mine,
IReadOnlyList<SettingsDifference> Differences, DateTimeOffset Found)
{
/// <summary>
/// The home's head holds settings of a newer Chronos (a difference at <see cref="ProjectMerge.FormatPath"/>): nothing of
/// this workspace's is merged into them. Only Save both settles it (this workspace's versions go to a new project in this
/// Chronos's format), or a Chronos that reads them; keeping mine or taking the server's is refused.
/// </summary>
[JsonIgnore]
public bool NewerFormat => Differences.Any(d => d.Path == ProjectMerge.FormatPath);
}
/// <summary>
/// Parameters of a <see cref="JobKind.WorkspaceCopy"/> job: target = the home's sequence; the workspace folder, the
/// project whose media to copy and what to copy (<see cref="BundleMedia.Proxies"/>; originals in phase 5c).
/// </summary>
public sealed record WorkspaceCopyParameters(string WorkspaceRoot, Guid ProjectId, string Media);
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
UseStringEnumConverter = true, WriteIndented = true)]
[JsonSerializable(typeof(WorkspaceManifest))]
[JsonSerializable(typeof(WorkspaceMedia))]
[JsonSerializable(typeof(WorkspaceSyncState))]
[JsonSerializable(typeof(WorkspaceCopyParameters))]
internal sealed partial class WorkspaceJson : JsonSerializerContext;
/// <summary>
/// The workspace of this PC (owner review 2, R8): a folder on a fast disk where projects are edited, never inside the
/// library. <c>&lt;root&gt;/workspace.json</c> names it; each project has a folder named by its id
/// (<see cref="WorkspaceProject"/>). There is no index: the store lists the folders holding a <c>project.chronos</c>. The
/// desktop puts it in <c>%LOCALAPPDATA%\Chronos\workspace</c> unless set otherwise; the store takes any folder.
/// </summary>
public sealed class WorkspaceStore
{
public const string ManifestFile = "workspace.json";
public const string FormatName = "chronos-workspace";
public const int FormatVersion = 1;
/// <summary>The folder name of the workspace inside the program's data folder.</summary>
public const string DefaultFolderName = "workspace";
/// <summary>
/// Opens the workspace at <paramref name="root"/>, making the folder and its manifest when there is none. The manifest
/// is written whole to a temporary file and moved into place, so a crash never leaves half of one; a manifest left
/// empty, or unreadable in a folder that holds no project yet (an earlier build's interrupted first write), is made again.
/// </summary>
/// <exception cref="InvalidDataException">The folder's <c>workspace.json</c> is not a workspace manifest, or a newer one.</exception>
/// <exception cref="IOException">The folder cannot be made or read.</exception>
public WorkspaceStore(string root)
{
ArgumentException.ThrowIfNullOrWhiteSpace(root);
Root = LongPath.Normalize(Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)));
Directory.CreateDirectory(Root);
var path = Path.Combine(Root, ManifestFile);
if (!File.Exists(path)) Create(path, replace: false);
else if (Unfinished(path)) Create(path, replace: true);
Manifest = Read(path);
}
public string Root { get; }
public WorkspaceManifest Manifest { get; }
public Guid Id => Manifest.Id;
/// <summary>The folder of a project, whether it exists or not.</summary>
public WorkspaceProject Project(Guid projectId) => new(this, projectId);
/// <summary>The project's folder when it holds a working state; null otherwise.</summary>
public WorkspaceProject? Find(Guid projectId)
{
var project = Project(projectId);
return project.Exists ? project : null;
}
/// <summary>The projects of the workspace: every folder named by a project id that holds a <c>project.chronos</c>.</summary>
public IReadOnlyList<WorkspaceProject> List()
{
var found = new List<WorkspaceProject>();
foreach (var directory in Directory.EnumerateDirectories(Root).Order(StringComparer.Ordinal))
{
if (Guid.TryParseExact(Path.GetFileName(directory), "N", out var id) && File.Exists(Path.Combine(directory, WorkspaceProject.BundleFile)))
found.Add(Project(id));
}
return found;
}
/// <summary>The working states of the workspace's projects (one unreadable is left out, and logged).</summary>
public IReadOnlyList<(WorkspaceProject Project, ProjectBundle Bundle)> ListBundles()
{
var bundles = new List<(WorkspaceProject, ProjectBundle)>();
foreach (var project in List())
{
try
{
bundles.Add((project, project.ReadBundle()));
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
Log.Warning("workspace", $"The project in {project.Folder} cannot be read", ex);
}
}
return bundles;
}
/// <summary>
/// Writes a new manifest. Without <paramref name="replace"/> another program opening the same folder at the same moment
/// may have made one first: that one wins (the move never replaces a file).
/// </summary>
private static void Create(string path, bool replace)
{
var manifest = new WorkspaceManifest(FormatName, FormatVersion, Guid.CreateVersion7(), DateTimeOffset.UtcNow);
var text = JsonSerializer.Serialize(manifest, WorkspaceJson.Default.WorkspaceManifest);
if (replace)
{
Log.Warning("workspace", $"{path} was left unfinished; a new manifest is written");
AtomicFile.WriteAllText(path, text);
return;
}
var temp = AtomicFile.TempPathFor(path);
try
{
using (var stream = new FileStream(temp, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
stream.Write(System.Text.Encoding.UTF8.GetBytes(text));
stream.Flush(flushToDisk: true);
}
try
{
File.Move(temp, path, overwrite: false);
}
catch (IOException) when (File.Exists(path))
{
// Another program's manifest came first.
}
}
finally
{
if (File.Exists(temp)) File.Delete(temp);
}
}
/// <summary>
/// A manifest an interrupted first write left (an earlier build wrote it in place): empty, or not JSON in a folder that
/// holds no project yet. A readable manifest, or any in a folder with projects, is never written over.
/// </summary>
private bool Unfinished(string path)
{
string text;
try
{
text = AtomicFile.ReadAllText(path);
}
catch (IOException)
{
return false;
}
if (string.IsNullOrWhiteSpace(text.Trim('\0'))) return true;
try
{
using var _ = JsonDocument.Parse(text);
return false;
}
catch (JsonException)
{
return List().Count == 0;
}
}
private static WorkspaceManifest Read(string path)
{
WorkspaceManifest? manifest;
try
{
manifest = JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), WorkspaceJson.Default.WorkspaceManifest);
}
catch (JsonException ex)
{
throw new InvalidDataException($"{path} is not a workspace manifest.", ex);
}
if (manifest is null || manifest.Format != FormatName || manifest.Id == Guid.Empty) throw new InvalidDataException($"{path} is not a workspace manifest.");
if (manifest.FormatVersion > FormatVersion)
throw new InvalidDataException($"{path} uses format {manifest.FormatVersion}, newer than this Chronos ({FormatVersion}).");
return manifest;
}
}
/// <summary>
/// One project in the workspace (owner review 2, R8): <c>&lt;root&gt;/&lt;project id&gt;/</c> with
/// <c>project.chronos</c> (the working state: a project file of format 2, its settings placed on the media's frames),
/// <c>media/media.json</c> with <c>media/proxies/&lt;frame id&gt;.jpg</c> (originals later) and <c>media/analysis.json</c>
/// (the home's deep report), <c>versions/&lt;number&gt;-&lt;id&gt;.json</c> (commits not pushed yet, and the versions pulled),
/// <c>sync.json</c>, <c>exports/</c> and the lease <c>.lock.lease</c> that one writer at a time holds.
/// </summary>
public sealed class WorkspaceProject
{
public const string BundleFile = "project.chronos";
public const string MediaFolder = "media";
public const string MediaIndexFile = "media.json";
public const string ProxiesFolder = "proxies";
public const string OriginalsFolder = "originals";
public const string VersionsFolder = "versions";
public const string ExportsFolder = "exports";
public const string SyncFile = "sync.json";
/// <summary>
/// Old local commits replaced by their replay on top of the home's head, files the home's copy replaced, and the working
/// states a take of the server's values replaced: kept up to a month, the newest 20 at most (<see cref="PruneReplaced"/>).
/// </summary>
public const string ReplacedFolder = ".replaced";
/// <summary>
/// Beside <see cref="ReplacedFolder"/> in <c>versions/</c>: when this Chronos first set a file aside there or pruned it
/// (an ISO-8601 time). A file dated before it was set aside by an earlier build, which kept the time it was written: it
/// counts as set aside then, at the earliest (<see cref="PruneReplaced"/>).
/// </summary>
public const string ReplacedSinceFile = ".replaced.since";
private const string LeaseName = ".lock";
internal WorkspaceProject(WorkspaceStore store, Guid projectId)
{
Store = store;
ProjectId = projectId;
Folder = Path.Combine(store.Root, projectId.ToString("N"));
}
public WorkspaceStore Store { get; }
public Guid ProjectId { get; }
public string Folder { get; }
public string BundlePath => Path.Combine(Folder, BundleFile);
public string MediaDirectory => Path.Combine(Folder, MediaFolder);
public string MediaIndexPath => Path.Combine(MediaDirectory, MediaIndexFile);
public string ProxiesDirectory => Path.Combine(MediaDirectory, ProxiesFolder);
public string OriginalsDirectory => Path.Combine(MediaDirectory, OriginalsFolder);
public string AnalysisPath => Path.Combine(MediaDirectory, ArchiveLayout.AnalysisFile);
public string VersionsDirectory => Path.Combine(Folder, VersionsFolder);
public string ExportsDirectory => Path.Combine(Folder, ExportsFolder);
public string SyncPath => Path.Combine(Folder, SyncFile);
public bool Exists => File.Exists(BundlePath);
/// <summary>The file a frame's proxy is copied to, relative to <see cref="MediaDirectory"/>.</summary>
public static string ProxyName(Guid frameId) => ProxiesFolder + "/" + ProxyCache.FileName(frameId);
/// <summary>
/// The file a frame's proxy of other bytes is copied to while the media name <see cref="ProxyName(Guid)"/> for the frame
/// (a conversion since): the editor keeps reading that one until the copy commits.
/// </summary>
public static string ProxyName(Guid frameId, string sha256)
{
ArgumentException.ThrowIfNullOrEmpty(sha256);
return ProxiesFolder + "/" + frameId.ToString("N") + "-" + sha256[..Math.Min(16, sha256.Length)].ToLowerInvariant() + ".jpg";
}
/// <summary>The absolute path of a file named relative to <see cref="MediaDirectory"/>.</summary>
public string MediaFile(string relative) => LongPath.FromPortableRelative(MediaDirectory, relative);
/// <summary>Holds the project for one writer (this program's sync, a copy job) until disposed.</summary>
/// <exception cref="LeaseUnavailableException">Another writer kept it for longer than <paramref name="wait"/>.</exception>
public async Task<IDisposable> LockAsync(TimeSpan wait, CancellationToken cancellationToken = default)
{
Directory.CreateDirectory(Folder);
return await LeaseFile.AcquireAsync(Folder, LeaseName, "workspace@" + Environment.MachineName, wait, cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <exception cref="InvalidDataException">Not a project file of a workspace, damaged or newer.</exception>
/// <exception cref="IOException">The file cannot be read.</exception>
public ProjectBundle ReadBundle()
{
var bundle = ProjectBundle.Load(BundlePath);
if (bundle.Workspace is not { } workspace || workspace.ProjectId != ProjectId)
throw new InvalidDataException($"{BundlePath} is not the working state of project {ProjectId}.");
return bundle;
}
public void WriteBundle(ProjectBundle bundle)
{
ArgumentNullException.ThrowIfNull(bundle);
if (bundle.Workspace?.ProjectId != ProjectId) throw new ArgumentException("The project file is another project's.", nameof(bundle));
Directory.CreateDirectory(Folder);
bundle.Save(BundlePath);
}
/// <summary>The media index; null while the media were never copied, or were removed.</summary>
/// <exception cref="InvalidDataException">The index is damaged or newer.</exception>
public WorkspaceMedia? ReadMedia()
{
if (!File.Exists(MediaIndexPath)) return null;
WorkspaceMedia? media;
try
{
media = JsonSerializer.Deserialize(AtomicFile.ReadAllText(MediaIndexPath), WorkspaceJson.Default.WorkspaceMedia);
}
catch (JsonException ex)
{
throw new InvalidDataException($"{MediaIndexPath} cannot be read.", ex);
}
if (media is null || media.Format != WorkspaceMedia.FormatName) throw new InvalidDataException($"{MediaIndexPath} is not a media index.");
if (media.FormatVersion > WorkspaceMedia.CurrentFormatVersion) throw new InvalidDataException($"{MediaIndexPath} is newer than this Chronos.");
return media;
}
internal void WriteMedia(WorkspaceMedia media)
{
Directory.CreateDirectory(MediaDirectory);
AtomicFile.WriteAllText(MediaIndexPath, JsonSerializer.Serialize(media, WorkspaceJson.Default.WorkspaceMedia));
}
/// <summary>
/// The versions this workspace holds, oldest first. One that cannot be read is left out here, logged, and listed by
/// <see cref="UnreadableVersions"/>: the sync treats one above the base as a gap in the chain of versions to send, never
/// as a version that is not there (<see cref="WorkspaceSync"/>).
/// </summary>
public IReadOnlyList<ProjectVersion> Versions() => ReadVersions(log: true).Versions;
/// <summary>
/// The version files of the workspace that cannot be read now (damaged, held open, over the size a version may be, of
/// a newer Chronos), by the number and id their names carry, oldest first.
/// </summary>
public IReadOnlyList<UnreadableVersion> UnreadableVersions() => ReadVersions(log: false).Unreadable;
/// <summary>
/// <see cref="Versions"/> and <see cref="UnreadableVersions"/> from one read of the folder: each file (up to
/// <see cref="ProjectSidecars.MaxVersionBytes"/>) is parsed once.
/// </summary>
internal (IReadOnlyList<ProjectVersion> Versions, IReadOnlyList<UnreadableVersion> Unreadable) ReadVersionFiles() => ReadVersions(log: true);
private (List<ProjectVersion> Versions, List<UnreadableVersion> Unreadable) ReadVersions(bool log)
{
var versions = new List<ProjectVersion>();
var unreadable = new List<UnreadableVersion>();
foreach (var (number, id, path) in ProjectSidecars.ListVersions(VersionsDirectory))
{
try
{
var version = ProjectSidecars.ReadVersion(path);
if (version.ProjectId == ProjectId) versions.Add(version);
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
// Said once per file as it is now: every sync step lists the versions again.
if (log && (!Reported.TryGetValue(path, out var said) || said != ex.Message))
{
Reported[path] = ex.Message;
Log.Warning("workspace", $"Version file {path} cannot be read", ex);
}
unreadable.Add(new UnreadableVersion(number, id, path, ex.Message));
}
}
return (versions, unreadable);
}
/// <summary>The version files whose read failure was logged, with why (logged again when the reason changes).</summary>
private static readonly System.Collections.Concurrent.ConcurrentDictionary<string, string> Reported = new(StringComparer.Ordinal);
/// <summary>The path of a version in <see cref="VersionsDirectory"/>.</summary>
public string VersionPath(ProjectVersion version) => Path.Combine(VersionsDirectory, ArchiveLayout.ProjectVersionFileName(version.Number, version.Id));
internal void WriteVersion(ProjectVersion version, bool replace = false) => ProjectSidecars.WriteVersion(VersionPath(version), version, replace);
/// <summary>
/// Moves a version file aside into <c>versions/.replaced</c> (a local commit replayed under another id), trying again
/// for a moment while another program (an indexer, an antivirus) holds it open.
/// </summary>
internal void SetAside(ProjectVersion version) => SetAside(VersionPath(version));
/// <summary>Moves the version file at <paramref name="path"/> aside into <c>versions/.replaced</c> (see <see cref="SetAside(ProjectVersion)"/>).</summary>
internal void SetAside(string path)
{
if (!File.Exists(path)) return;
var aside = Path.Combine(VersionsDirectory, ReplacedFolder);
Directory.CreateDirectory(aside);
ReplacedSince(DateTime.UtcNow);
var target = Path.Combine(aside, Path.GetFileName(path));
if (File.Exists(target)) target += "." + DateTime.UtcNow.Ticks.ToString(System.Globalization.CultureInfo.InvariantCulture);
for (var attempt = 1; ; attempt++)
{
try
{
File.Move(path, target);
break;
}
catch (IOException) when (attempt < SetAsideAttempts && File.Exists(path) && !File.Exists(target))
{
Thread.Sleep(10 * attempt);
}
}
// Dated as set aside, which the pruning goes by (a move keeps the time the version was written).
TryTouch(target);
}
/// <summary>
/// Writes a version straight into <c>versions/.replaced</c>: kept, never part of the chain of versions to send (the
/// working state a take of the server's values replaced).
/// </summary>
internal void WriteAside(ProjectVersion version)
{
ArgumentNullException.ThrowIfNull(version);
ProjectSidecars.WriteVersion(Path.Combine(VersionsDirectory, ReplacedFolder, Path.GetFileName(VersionPath(version))), version);
ReplacedSince(DateTime.UtcNow);
}
/// <summary>Files <see cref="PruneReplaced"/> keeps in <c>versions/.replaced</c> whatever their age.</summary>
public const int ReplacedKept = 20;
/// <summary>The age past which <see cref="PruneReplaced"/> removes a file of <c>versions/.replaced</c>.</summary>
public static readonly TimeSpan ReplacedMaxAge = TimeSpan.FromDays(30);
/// <summary>
/// Removes from <c>versions/.replaced</c> the files set aside more than <see cref="ReplacedMaxAge"/> ago and those past
/// the newest <see cref="ReplacedKept"/>, never one of a version in <paramref name="referenced"/> (a journal's). Every
/// file there is a copy of what the chain or the home holds, or what a take of the server's values replaced (its undo):
/// called only after a sync that ended well. A file is set aside when this Chronos dated it (it touches what it sets
/// aside), and no earlier than this Chronos first knew the folder (<see cref="ReplacedSinceFile"/>): one an earlier build
/// set aside keeps the time its version was written, which says nothing of when it went aside. How many went.
/// </summary>
internal int PruneReplaced(IReadOnlySet<Guid> referenced, DateTime nowUtc)
{
ArgumentNullException.ThrowIfNull(referenced);
var aside = Path.Combine(VersionsDirectory, ReplacedFolder);
if (!Directory.Exists(aside)) return 0;
var since = ReplacedSince(nowUtc);
var files = new DirectoryInfo(aside).EnumerateFiles()
.Select(f => (File: f, Aside: f.LastWriteTimeUtc > since ? f.LastWriteTimeUtc : since))
.OrderByDescending(f => f.Aside).ThenByDescending(f => f.File.LastWriteTimeUtc).ThenByDescending(f => f.File.Name, StringComparer.Ordinal).ToList();
var removed = 0;
for (var i = 0; i < files.Count; i++)
{
var (file, set) = files[i];
if (i < ReplacedKept && nowUtc - set <= ReplacedMaxAge) continue;
if (VersionIdOf(file.Name) is { } id && referenced.Contains(id)) continue;
try
{
file.Delete();
removed++;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("workspace", $"{file.FullName} could not be removed", ex);
}
}
return removed;
// "<number>-<id>.json", maybe with a suffix a second file of the same version took.
static Guid? VersionIdOf(string name)
{
var end = name.IndexOf(".json", StringComparison.OrdinalIgnoreCase);
return end > 0 && ProjectSidecars.TryParseName(name[..(end + 5)], out _, out var id) ? id : null;
}
}
/// <summary>
/// When this Chronos first knew <c>versions/.replaced</c> (<see cref="ReplacedSinceFile"/>): read, else written as
/// <paramref name="nowUtc"/> (also over a file that cannot be read). One that cannot be written counts as now: nothing
/// set aside by an earlier build goes by its age then.
/// </summary>
private DateTime ReplacedSince(DateTime nowUtc)
{
var path = Path.Combine(VersionsDirectory, ReplacedSinceFile);
try
{
if (File.Exists(path)
&& DateTime.TryParse(File.ReadAllText(path).Trim(), System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind,
out var known)
&& known.Kind == DateTimeKind.Utc && known <= nowUtc)
return known;
Directory.CreateDirectory(VersionsDirectory);
AtomicFile.WriteAllText(path, nowUtc.ToString("O", System.Globalization.CultureInfo.InvariantCulture));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Debug("workspace", $"{path} could not be kept: {ex.Message}");
}
return nowUtc;
}
private static void TryTouch(string path)
{
try
{
if (File.Exists(path)) File.SetLastWriteTimeUtc(path, DateTime.UtcNow);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Debug("workspace", $"{path} keeps the time it was written: {ex.Message}");
}
}
/// <summary>Moves tried, some 1 s in all, before a version file held open fails to move aside.</summary>
private const int SetAsideAttempts = 15;
public WorkspaceSyncState ReadSync()
{
if (!File.Exists(SyncPath)) return WorkspaceSyncState.None;
try
{
return JsonSerializer.Deserialize(AtomicFile.ReadAllText(SyncPath), WorkspaceJson.Default.WorkspaceSyncState) ?? WorkspaceSyncState.None;
}
catch (JsonException ex)
{
throw new InvalidDataException($"{SyncPath} cannot be read.", ex);
}
}
internal void WriteSync(WorkspaceSyncState state)
{
ArgumentNullException.ThrowIfNull(state);
if (state.IsEmpty)
{
if (File.Exists(SyncPath)) File.Delete(SyncPath);
return;
}
Directory.CreateDirectory(Folder);
AtomicFile.WriteAllText(SyncPath, JsonSerializer.Serialize(state, WorkspaceJson.Default.WorkspaceSyncState));
}
}