From 4d697a42553da32bc342cc72d695928034a9926c Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Sun, 15 Nov 2026 13:15:11 +0100 Subject: [PATCH 1/3] feat: stable server id and per-home folder layout (owner review 5 P3.1) The desktop keyed every server by the address it was reached at, so one NAS reached by name and by IP, or after an address change, looked like two servers, and all servers shared one workspace and one cache. Each server now announces a stable id, and the desktop's folders are sorted into one home per server, so that every server can have its own archive of downloaded originals (or5 answers Q1). - The server makes a Guid v7 once, keeps it as the app setting server.id (so it survives restarts and restores, on SQLite and Postgres) and announces it as ServerInfo.ServerId. Older clients ignore the field, and older servers read as null. - ServerIdentity keys a server as id: when it announces an id, and by its address otherwise. Address keys are kept as aliases. - HomeFolders names the homes: local, servers/ or servers/addr-. The hash equals the 1.0 proxy cache folder name. It also names the per-server originals archive root. - WorkspaceHomes splits the workspace into one format-1 store per home under a format-2 parent manifest, which 1.0 refuses. A 1.0 workspace stays one store until it is sorted. - HomeLayoutMigration plans as a dry run and applies through a journal: - only same-folder renames, each checked by inventory; - a clash keeps the copy saved last and sends the other to the trash; - it resumes after a crash at any step; - it skips leased projects; - it defers an offline workspace or one being moved, while the cache part still runs. - RekeyAsync moves an alias home to the id home: whole folders, or project-by-project merges. It also rewrites working states under their lease, and moves sync_links (the newer row wins a clash). - ProxyCache, the job root resolver, WorkspaceMover and WorkspaceCleaner understand the split layout. The new layout is opt-in through SyncOptions.ServersDirectory, so the app sees no change until it is wired. Refs #66 Co-Authored-By: Claude Opus 5.5 --- src/Chronos.Core/IO/HomeFolders.cs | 178 ++++ src/Chronos.Core/Project/ProjectBundle.cs | 5 +- src/Chronos.Data/Homes/HomeLayoutJournal.cs | 102 +++ src/Chronos.Data/Homes/HomeLayoutMigration.cs | 788 ++++++++++++++++++ .../Projects/ServerProjectHome.cs | 19 +- src/Chronos.Data/Sync/ProxyCache.cs | 35 +- src/Chronos.Data/Sync/ServerIdentity.cs | 121 +++ src/Chronos.Data/Sync/SyncLinkStore.cs | 47 ++ src/Chronos.Data/Sync/SyncRemote.cs | 8 +- src/Chronos.Data/Sync/SyncService.cs | 38 +- .../Workspaces/WorkspaceCleaner.cs | 30 +- src/Chronos.Data/Workspaces/WorkspaceHomes.cs | 293 +++++++ src/Chronos.Data/Workspaces/WorkspaceJobs.cs | 73 +- src/Chronos.Data/Workspaces/WorkspaceMover.cs | 60 +- src/Chronos.Data/Workspaces/WorkspaceStore.cs | 43 +- src/Chronos.Protocol/Dtos.cs | 8 +- src/Chronos.Server/Api/SystemEndpoints.cs | 9 +- src/Chronos.Server/Hosting/ServerHost.cs | 36 + .../Foundation/HomeFoldersTests.cs | 90 ++ .../Chronos.Data.Tests/Homes/HomeFixtures.cs | 122 +++ .../Homes/HomeLayoutMigrationTests.cs | 297 +++++++ .../Homes/HomeRekeyTests.cs | 215 +++++ .../Sync/ProxyCacheTests.cs | 58 ++ .../Sync/ServerIdentityTests.cs | 84 ++ .../Sync/SyncLinkRekeyTests.cs | 72 ++ .../Workspaces/WorkspaceHomesTests.cs | 172 ++++ .../Api/ServerInfoIdTests.cs | 117 +++ 27 files changed, 3082 insertions(+), 38 deletions(-) create mode 100644 src/Chronos.Core/IO/HomeFolders.cs create mode 100644 src/Chronos.Data/Homes/HomeLayoutJournal.cs create mode 100644 src/Chronos.Data/Homes/HomeLayoutMigration.cs create mode 100644 src/Chronos.Data/Sync/ServerIdentity.cs create mode 100644 src/Chronos.Data/Workspaces/WorkspaceHomes.cs create mode 100644 tests/Chronos.Core.Tests/Foundation/HomeFoldersTests.cs create mode 100644 tests/Chronos.Data.Tests/Homes/HomeFixtures.cs create mode 100644 tests/Chronos.Data.Tests/Homes/HomeLayoutMigrationTests.cs create mode 100644 tests/Chronos.Data.Tests/Homes/HomeRekeyTests.cs create mode 100644 tests/Chronos.Data.Tests/Sync/ProxyCacheTests.cs create mode 100644 tests/Chronos.Data.Tests/Sync/ServerIdentityTests.cs create mode 100644 tests/Chronos.Data.Tests/Sync/SyncLinkRekeyTests.cs create mode 100644 tests/Chronos.Data.Tests/Workspaces/WorkspaceHomesTests.cs create mode 100644 tests/Chronos.Server.Tests/Api/ServerInfoIdTests.cs diff --git a/src/Chronos.Core/IO/HomeFolders.cs b/src/Chronos.Core/IO/HomeFolders.cs new file mode 100644 index 00000000..3d5b83fe --- /dev/null +++ b/src/Chronos.Core/IO/HomeFolders.cs @@ -0,0 +1,178 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text; + +namespace Chronos.Core.IO; + +/// +/// The folders each home of the desktop keeps apart (owner review 5, #66): this PC's archive is the home , +/// and every Chronos Server is a home of its own under , named by the server's stable id +/// (servers\<id:N>), or, for a server that announces none (an older server), by a hash of its address key +/// (servers\addr-<16 hex>: the hash the 1.0 proxy cache named its folders by, so moving them is a rename). +/// The same names are used in the workspace (<workspace>\local, <workspace>\servers\<folder>) +/// and in the cache (<cache>\servers\<folder>\{proxies,render-node,import}). A folder name is never renamed +/// after the server: its name lives in beside it. +/// +public static class HomeFolders +{ + /// The folder of this PC's archive's home. + public const string Local = "local"; + + /// The folder that holds one folder per server home. + public const string Servers = "servers"; + + /// What a server home's folder says about its server (id, name, last address), for someone browsing it. + public const string ServerFile = "server.json"; + + /// A server home's proxies of its sequences, in the cache. + public const string Proxies = "proxies"; + + /// A server home's render-node downloads, in the cache. + public const string RenderNode = "render-node"; + + /// A server home's staged card imports, in the cache. + public const string Import = "import"; + + /// The start of a server key made from the server's announced id (id:<32 hex>). + public const string IdKeyPrefix = "id:"; + + /// The start of the folder of a server known by its address only. + public const string AddressPrefix = "addr-"; + + /// Hex digits of the address hash in a folder name (as the 1.0 proxy cache used). + public const int HashLength = 16; + + /// + /// The folder of a server home: the 32 hex digits of the id of an id key (id:<N>), else addr- and the + /// first 16 hex digits of the SHA-256 of the key (an address key, as SyncService.ServerKey makes it). + /// + public static string ServerFolder(string serverKey) + { + ArgumentException.ThrowIfNullOrWhiteSpace(serverKey); + return IdOfKey(serverKey) is { } id ? id.ToString("N") : AddressPrefix + AddressHash(serverKey); + } + + /// The 16 hex digits the 1.0 proxy cache named a server's folder by: the start of the SHA-256 of its key. + public static string AddressHash(string serverKey) + { + ArgumentNullException.ThrowIfNull(serverKey); + return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(serverKey)))[..HashLength]; + } + + /// The id an id key names (id:<N>); null for an address key. + public static Guid? IdOfKey(string? serverKey) => + serverKey is { Length: > 0 } key && key.StartsWith(IdKeyPrefix, StringComparison.OrdinalIgnoreCase) + && Guid.TryParseExact(key.AsSpan(IdKeyPrefix.Length), "N", out var id) && id != Guid.Empty + ? id + : null; + + /// The key of a server known by its announced id. + public static string IdKey(Guid serverId) + { + if (serverId == Guid.Empty) throw new ArgumentException("A server id is never empty.", nameof(serverId)); + return IdKeyPrefix + serverId.ToString("N"); + } + + /// + /// A name a server home's folder can have: 32 lower-case hex digits (an id) or addr- with 16 lower-case hex + /// digits. Anything else under is not a home (left alone). + /// + public static bool IsServerFolder(string? name) => name is { Length: > 0 } && (IdOfFolder(name) is not null || IsAddressFolder(name)); + + /// The server id a folder named by one stands for; null for an address folder or another name. + public static Guid? IdOfFolder(string? name) => + name is { Length: 32 } && IsLowerHex(name) && Guid.TryParseExact(name, "N", out var id) && id != Guid.Empty ? id : null; + + /// A folder of a server known by its address only (addr- and 16 lower-case hex digits). + public static bool IsAddressFolder(string? name) => + name is { Length: > 0 } && name.Length == AddressPrefix.Length + HashLength && name.StartsWith(AddressPrefix, StringComparison.Ordinal) + && IsLowerHex(name.AsSpan(AddressPrefix.Length)); + + /// The folder of this PC's home under (a workspace or a cache folder). + public static string LocalPath(string root) + { + ArgumentException.ThrowIfNullOrWhiteSpace(root); + return Path.Combine(root, Local); + } + + /// The folder of the home of under (a workspace or a cache folder). + public static string ServerPath(string root, string serverKey) + { + ArgumentException.ThrowIfNullOrWhiteSpace(root); + return Path.Combine(root, Servers, ServerFolder(serverKey)); + } + + /// + /// Where the originals a server's sequences download go by default (owner review 5, Q1): an archive of their own beside + /// this PC's, <parent of the archive>\<archive folder name> - <server name>, for example + /// D:\ChronosArchive - Studio NAS. The server's name is made safe as a folder name (); a + /// folder says is taken gets " (2)", " (3)"… With no archive of this PC's, + /// (where a first archive would go) stands for it. + /// + /// The root of this PC's archive; null when there is none. + /// The server's name as its server/info gives it. + /// Where this PC's first archive would go (the desktop's default archive folder). + /// Whether a folder is taken (it exists, or another archive uses it); null takes an existing folder or file. + public static string OriginalsArchiveRoot(string? localArchiveRoot, string serverName, string fallbackArchiveRoot, Func? taken = null) + { + ArgumentException.ThrowIfNullOrWhiteSpace(fallbackArchiveRoot); + var archive = Path.TrimEndingDirectorySeparator(Path.GetFullPath(string.IsNullOrWhiteSpace(localArchiveRoot) ? fallbackArchiveRoot : localArchiveRoot)); + var parent = Path.GetDirectoryName(archive) ?? archive; + var name = Path.GetFileName(archive); + if (string.IsNullOrEmpty(name)) name = SafeName(Path.GetFileName(Path.TrimEndingDirectorySeparator(fallbackArchiveRoot))); + var stem = name + " - " + SafeName(serverName); + taken ??= path => Directory.Exists(path) || File.Exists(path); + for (var n = 1; ; n++) + { + var candidate = Path.Combine(parent, n == 1 ? stem : stem + " (" + n.ToString(CultureInfo.InvariantCulture) + ")"); + if (!taken(candidate)) return candidate; + } + } + + /// Characters no folder name may hold on Windows (and so on any PC a folder may be copied to). + private static readonly char[] Unsafe = ['<', '>', ':', '"', '/', '\\', '|', '?', '*']; + + private static readonly string[] Reserved = + [ + "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", + "LPT6", "LPT7", "LPT8", "LPT9", + ]; + + /// The longest folder name gives. + public const int MaxNameLength = 60; + + /// + /// as a folder name every Windows file system takes: characters it refuses and control characters + /// become spaces, runs of spaces one, leading and trailing spaces and dots go, a device name (CON, NUL, COM1…) gets an + /// underscore, and it is cut to . "Server" when nothing is left. + /// + public static string SafeName(string? name) + { + var text = new StringBuilder(name?.Length ?? 0); + foreach (var c in name ?? string.Empty) + { + var bad = char.IsControl(c) || Array.IndexOf(Unsafe, c) >= 0; + var space = bad || char.IsWhiteSpace(c); + if (space) + { + if (text.Length > 0 && text[^1] != ' ') text.Append(' '); + continue; + } + text.Append(c); + } + var safe = text.ToString().Trim(' ', '.'); + if (safe.Length > MaxNameLength) safe = safe[..MaxNameLength].TrimEnd(' ', '.'); + if (safe.Length == 0) return "Server"; + var stem = safe.Split('.')[0]; + return Reserved.Contains(stem, StringComparer.OrdinalIgnoreCase) ? safe + "_" : safe; + } + + private static bool IsLowerHex(ReadOnlySpan text) + { + foreach (var c in text) + { + if (c is not ((>= '0' and <= '9') or (>= 'a' and <= 'f'))) return false; + } + return true; + } +} diff --git a/src/Chronos.Core/Project/ProjectBundle.cs b/src/Chronos.Core/Project/ProjectBundle.cs index 86921be2..bb462dec 100644 --- a/src/Chronos.Core/Project/ProjectBundle.cs +++ b/src/Chronos.Core/Project/ProjectBundle.cs @@ -23,7 +23,10 @@ public sealed record BundleSequence(Guid Id, string Folder, string? Title, int F /// /// Where a workspace project's versions live (owner review 2, R8): this PC's archive (, with the -/// archive's id) or a server (, with its address), and the sequence there. +/// archive's id) or a server (, with its key), and the sequence there. A server's key is its id key +/// (id:<N>, ) once the server announced its stable id (owner review 5, #66), +/// else the address it was reached at (https://nas.local:8443, the 1.0 key, which an id-keyed server keeps as an +/// alias); the format does not change. /// public sealed record BundleHome(string Kind, Guid SequenceId, Guid? ArchiveId = null, string? Server = null) { diff --git a/src/Chronos.Data/Homes/HomeLayoutJournal.cs b/src/Chronos.Data/Homes/HomeLayoutJournal.cs new file mode 100644 index 00000000..e68a3d89 --- /dev/null +++ b/src/Chronos.Data/Homes/HomeLayoutJournal.cs @@ -0,0 +1,102 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Chronos.Core.Diagnostics; +using Chronos.Core.IO; + +namespace Chronos.Data.Homes; + +/// What one entry of the sorting of the folders into homes moves (). +public enum HomeEntryKind +{ + /// A workspace project's folder (<workspace>\<project id>) into its home's store. + Project, + + /// An entry of the workspace's trash (<workspace>\.trash\<name>) into its home's trash. + Trash, + + /// A server's proxy cache (<cache>\proxies\<hash>, or one sequence of it) into its home in the cache. + Proxies, + + /// The render node's downloads (<cache>\render-node) into the home of the server they came from. + RenderNode, + + /// A card import staged for a server (<cache>\server-import) into that server's home in the cache. + Import, +} + +/// Why the workspace part of the sorting waits for a later start (the cache part runs all the same). +public enum HomeLayoutDeferral +{ + /// The workspace folder cannot be reached (an unplugged drive). + Offline, + + /// A move of the workspace folder is under way or stopped (its journal, or a move job not finished): it is taken up or undone first. + Moving, +} + +/// +/// One rename of the sorting (): what it is, the folder it is in (: the +/// workspace or the cache), the path it leaves and the path it takes (both full, in , so on one +/// volume), and the inventory read at planning: files and bytes, and the SHA-256 of their relative names and sizes, which the +/// target must show after the rename. Leases are left out of the inventory. +/// +/// The server key of the home it goes to (canonical); null for this PC's home and for a server known only by hash. +/// The server's name as the project's file gives it, for the home's server.json. +public sealed record HomeLayoutEntry(HomeEntryKind Kind, string Root, string Source, string Target, int Files, long Bytes, string Inventory, + string? ServerKey = null, string? ServerName = null); + +/// How far the sorting went. +public enum HomeLayoutStage +{ + /// Entries are being renamed; the workspace's parent manifest is not split yet. + Moving, + + /// Every entry was renamed or skipped and the parent manifest names the split layout; only the journal is left to go. + Committed, +} + +/// +/// The journal of the sorting of the folders into homes (<data>\homes-migration.json, +/// ): the plan's fingerprint, its entries in order, the ones done (renamed, found +/// renamed, or skipped because in use), whether it splits the workspace's parent, and the stage. Written whole and +/// atomically before the first rename and after each one, so a sorting stopped at any moment is taken up from it. +/// +public sealed record HomeLayoutJournal(string Format, int FormatVersion, string Fingerprint, IReadOnlyList Entries, IReadOnlyList Done, + string? WorkspaceRoot, bool SplitsWorkspace, HomeLayoutStage Stage, DateTimeOffset Started) +{ + public const string FormatName = "chronos-homes-migration"; + public const int CurrentFormatVersion = 1; + + /// The journal at ; null when there is none, or it cannot be read (logged). + public static HomeLayoutJournal? Read(string path) + { + ArgumentException.ThrowIfNullOrWhiteSpace(path); + if (!File.Exists(path)) return null; + try + { + if (JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), HomeLayoutJson.Default.HomeLayoutJournal) is + { Format: FormatName, Entries: not null, Done: not null } journal && journal.FormatVersion <= CurrentFormatVersion) + return journal; + Log.Warning("homes", $"{path} is not a journal of the sorting of the folders this Chronos reads"); + } + catch (Exception ex) when (ex is JsonException or IOException or UnauthorizedAccessException) + { + Log.Warning("homes", $"{path} cannot be read", ex); + } + return null; + } + + /// Writes the journal whole to a temporary file and moves it into place. + public static void Write(string path, HomeLayoutJournal journal) + { + ArgumentException.ThrowIfNullOrWhiteSpace(path); + ArgumentNullException.ThrowIfNull(journal); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + AtomicFile.WriteAllText(path, JsonSerializer.Serialize(journal, HomeLayoutJson.Default.HomeLayoutJournal)); + } +} + +[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + UseStringEnumConverter = true, WriteIndented = true)] +[JsonSerializable(typeof(HomeLayoutJournal))] +internal sealed partial class HomeLayoutJson : JsonSerializerContext; diff --git a/src/Chronos.Data/Homes/HomeLayoutMigration.cs b/src/Chronos.Data/Homes/HomeLayoutMigration.cs new file mode 100644 index 00000000..2d354a36 --- /dev/null +++ b/src/Chronos.Data/Homes/HomeLayoutMigration.cs @@ -0,0 +1,788 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text; +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; +using Chronos.Data.Workspaces; + +namespace Chronos.Data.Homes; + +/// What the sorting of the folders into homes works on (). +public sealed record HomeLayoutOptions +{ + /// The program's data folder, which holds the journal (). + public required string DataDirectory { get; init; } + + /// The workspace folder (Settings › Workspace › Folder: the parent of the homes); null leaves the workspace alone. + public string? WorkspaceRoot { get; init; } + + /// The cache folder (Settings › Storage › Cache folder); null leaves the cache alone. + public string? CacheRoot { get; init; } + + /// The servers known by id and their aliases: a home is named by its server's canonical key. + public ServerIdentity Identity { get; init; } = ServerIdentity.None; + + /// + /// The server this PC is signed in to, or was last (its key): the 1.0 render-node downloads and staged server import, + /// shared by every server then, go to its home. Null leaves them where they are (the cache trims them). + /// + public string? CurrentServer { get; init; } + + /// The index: a workspace move job not finished defers the workspace part, and a re-key moves the server's links. Null skips both. + public ChronosDatabase? Database { get; init; } + + public TimeProvider Time { get; init; } = TimeProvider.System; +} + +/// +/// What the sorting would move, read without writing anything (): its entries, the +/// workspace projects in use now that wait for a later start, why the workspace part waits (null when it does not), whether it +/// splits the workspace's parent (its manifest goes to format ), and a +/// fingerprint of what was read. +/// +public sealed record HomeLayoutPlan(IReadOnlyList Entries, IReadOnlyList InUse, HomeLayoutDeferral? Deferred, bool SplitsWorkspace, + string Fingerprint) +{ + /// Nothing to rename and no manifest to write. + public bool IsEmpty => Entries.Count == 0 && !SplitsWorkspace; + + /// The workspace projects the sorting moves. + public int Projects => Entries.Count(e => e.Kind == HomeEntryKind.Project); + + /// The bytes of everything it moves (renamed, never copied). + public long Bytes => Entries.Sum(e => e.Bytes); +} + +/// What a sorting did: projects and entries moved, projects left where they are (in use), why the workspace waits, whether it was taken up from a journal. +/// Entries whose target was taken: what lost went to its home's trash (workspace) or stayed where it is (cache); never deleted. +public sealed record HomeLayoutResult(int ProjectsMoved, int EntriesMoved, int InUse, HomeLayoutDeferral? Deferred, bool Resumed, int Clashes, bool Split) +{ + public static HomeLayoutResult Nothing { get; } = new(0, 0, 0, null, false, 0, false); +} + +/// What a re-key did (). +/// Home folders renamed whole to the id's (workspace and cache). +/// Projects moved one by one into the id's home (the same server reached at two addresses). +/// Copies of one project in both homes whose older one went to the trash of the id's home. +/// Working states whose home now names the id key. +/// Working states held by a writer: left naming an alias, which matches all the same. +public sealed record HomeRekeyResult(int HomesMoved, int ProjectsMoved, int ProjectsTrashed, int BundlesRewritten, int BundlesInUse, int LinksMoved, int LinksDropped); + +/// A rename of the sorting did not leave the folder as planned: it stopped there, its journal kept, and nothing was deleted. +public sealed class HomeLayoutException(string message, Exception? inner = null) : IOException(message, inner); + +/// +/// The one-time sorting of the desktop's folders into homes (owner review 5, #66), run at the start of 1.1 before the job +/// engine runs, and the re-key of a server's home when it first announces its id. +/// +/// Plan (, a dry run): each workspace project folder of the 1.0 layout goes to the store of its +/// home (: local, or servers\<folder> of its server), one whose working +/// state cannot be read to local (it still lists and opens as damaged); each trash entry goes to its project's home's +/// trash; the 1.0 proxy cache <cache>\proxies\<hash> goes to <cache>\servers\<folder>\proxies +/// (the folder of a known server's id when the hash is one of its aliases', else addr-<hash>, the same hash), and +/// the shared render-node and server-import to the current server's home. A project in use (a live lease) is +/// left for a later start. A workspace folder out of reach, or one being moved, defers the workspace part; the cache part runs. +/// +/// +/// Apply (): the journal is written first and after every rename; every move is a rename inside +/// the folder it is in (so on one volume, no byte copied, nothing half-copied), checked by inventory (the same relative names +/// and sizes at the target, the source gone), which is enough where no byte is rewritten (the rule +/// uses on one volume). A rename that does not check stops the sorting with its journal; the next start takes it up. A +/// target already taken is a clash: in the workspace the older working state goes to the home's trash, in the cache the +/// entry stays where it is; nothing is deleted. Writing the split parent manifest is the last step: it commits the layout. +/// Running again on sorted folders does nothing. +/// +/// +public sealed class HomeLayoutMigration +{ + /// The journal of a sorting under way, in the data folder (only while one is unfinished). + public const string JournalFile = "homes-migration.json"; + + /// The 1.0 folder of the proxy cache in the cache folder. + public const string LegacyProxies = "proxies"; + + /// The 1.0 folder of the render node's downloads in the cache folder. + public const string LegacyRenderNode = "render-node"; + + /// The 1.0 folder of card imports staged for a server in the cache folder. + public const string LegacyServerImport = "server-import"; + + private const string TrashFolder = WorkspaceTrash.FolderName; + + private readonly HomeLayoutOptions _options; + + public HomeLayoutMigration(HomeLayoutOptions options) + { + ArgumentNullException.ThrowIfNull(options); + ArgumentException.ThrowIfNullOrWhiteSpace(options.DataDirectory); + _options = options; + } + + /// The journal's path. + public string JournalPath => Path.Combine(_options.DataDirectory, JournalFile); + + /// + /// Called at named steps with the path concerned (tests: a crash): journal (the journal first written), rename + /// (an entry about to be renamed), renamed (an entry renamed, its journal not written yet), commit (the parent + /// manifest about to be written). + /// + internal Action? Cut { get; init; } + + private DateTimeOffset Now => _options.Time.GetUtcNow(); + + // ---------------- Plan ---------------- + + /// What the sorting would move now (); nothing is written. + /// A folder cannot be read. + public async Task PlanAsync(CancellationToken cancellationToken = default) + { + var entries = new List(); + var inUse = new List(); + HomeLayoutDeferral? deferred = null; + var splits = false; + if (_options.WorkspaceRoot is { Length: > 0 } configured) + { + var root = WorkspaceStore.FullRoot(configured); + if (!Directory.Exists(root)) + { + // Never made yet (the workspace opens split), or on a drive that is not there. + if (!Reachable(root)) deferred = HomeLayoutDeferral.Offline; + } + else if (WorkspaceMover.HasJournal(root) || await MovePendingAsync(cancellationToken).ConfigureAwait(false)) + { + deferred = HomeLayoutDeferral.Moving; + } + else + { + splits = !WorkspaceHomes.IsSplitRoot(root); + PlanWorkspace(root, entries, inUse, cancellationToken); + } + } + if (_options.CacheRoot is { Length: > 0 } cache && Directory.Exists(cache)) PlanCache(Path.GetFullPath(cache), entries, cancellationToken); + var fingerprint = new StringBuilder(); + foreach (var entry in entries) + fingerprint.Append(CultureInfo.InvariantCulture, $"{entry.Kind}|{entry.Source}|{entry.Target}|{entry.Files}|{entry.Bytes}|{entry.Inventory}\n"); + if (splits) fingerprint.Append("split\n"); + var plan = new HomeLayoutPlan(entries, inUse, deferred, splits, Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(fingerprint.ToString())))); + if (!plan.IsEmpty || inUse.Count > 0 || deferred is not null) + { + Log.Info("homes", $"Sorting the folders into homes: {plan.Projects} project(s), {entries.Count} entr(ies), {plan.Bytes} bytes" + + (splits ? ", the workspace split" : string.Empty) + (inUse.Count > 0 ? $", {inUse.Count} project(s) in use" : string.Empty) + + (deferred is { } why ? $", the workspace deferred ({why})" : string.Empty)); + foreach (var entry in entries) Log.Info("homes", $" {entry.Kind}: {entry.Source} -> {entry.Target} ({entry.Files} files, {entry.Bytes} bytes)"); + } + return plan; + } + + private void PlanWorkspace(string root, List entries, List inUse, CancellationToken cancellationToken) + { + var homes = new Dictionary(); + foreach (var directory in Directory.EnumerateDirectories(root).Order(StringComparer.Ordinal)) + { + cancellationToken.ThrowIfCancellationRequested(); + if (!Guid.TryParseExact(Path.GetFileName(directory), "N", out var id) || !File.Exists(Path.Combine(directory, WorkspaceProject.BundleFile))) continue; + if (Leased(directory)) + { + inUse.Add(id); + continue; + } + var (partition, key, name) = PartitionOf(root, Path.Combine(directory, WorkspaceProject.BundleFile), id); + homes[id] = (partition, key, name); + var (files, bytes, inventory) = Inventory(directory); + entries.Add(new HomeLayoutEntry(HomeEntryKind.Project, root, directory, Path.Combine(partition, id.ToString("N")), files, bytes, inventory, key, name)); + } + var trash = Path.Combine(root, TrashFolder); + if (!Directory.Exists(trash)) return; + foreach (var entry in new DirectoryInfo(trash).EnumerateFileSystemInfos().OrderBy(e => e.Name, StringComparer.Ordinal)) + { + cancellationToken.ThrowIfCancellationRequested(); + var partition = HomeFolders.LocalPath(root); + string? key = null; + string? name = null; + var named = TrashProject(entry.Name); + if (named is { } projectId && homes.TryGetValue(projectId, out var home)) (partition, key, name) = home; + else if (entry is DirectoryInfo && File.Exists(Path.Combine(entry.FullName, WorkspaceProject.BundleFile))) + (partition, key, name) = PartitionOf(root, Path.Combine(entry.FullName, WorkspaceProject.BundleFile), named); + var (files, bytes, inventory) = entry is DirectoryInfo ? Inventory(entry.FullName) : FileInventory(entry.FullName); + entries.Add(new HomeLayoutEntry(HomeEntryKind.Trash, root, entry.FullName, Path.Combine(partition, TrashFolder, entry.Name), files, bytes, inventory, key, name)); + } + } + + /// The store a working state's home belongs in, its server's canonical key and name; local when it cannot be read. + private (string Partition, string? Key, string? Name) PartitionOf(string root, string bundlePath, Guid? projectId) + { + try + { + var bundle = ProjectBundle.Load(bundlePath); + if (bundle.Workspace is { } workspace && (projectId is null || workspace.ProjectId == projectId) + && workspace.Home is { Kind: BundleHome.ServerKind, Server: { Length: > 0 } server }) + { + var key = _options.Identity.Canonical(server); + return (Path.Combine(root, HomeFolders.Servers, HomeFolders.ServerFolder(key)), key, bundle.Archive.Name); + } + } + catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException or System.Text.Json.JsonException) + { + Log.Info("homes", $"{bundlePath} cannot be read: it goes to this PC's home ({ex.Message})"); + } + return (HomeFolders.LocalPath(root), null, null); + } + + /// The project a trash entry was set aside from (<id>-<ticks>, also while staged as .incoming-…). + private static Guid? TrashProject(string name) + { + var text = name.StartsWith(".incoming-", StringComparison.Ordinal) ? name[".incoming-".Length..] : name; + return text.Length >= 32 && Guid.TryParseExact(text.AsSpan(0, 32), "N", out var id) ? id : null; + } + + private void PlanCache(string cache, List entries, CancellationToken cancellationToken) + { + var servers = Path.Combine(cache, HomeFolders.Servers); + var proxies = Path.Combine(cache, LegacyProxies); + if (Directory.Exists(proxies)) + { + foreach (var directory in Directory.EnumerateDirectories(proxies).Order(StringComparer.Ordinal)) + { + cancellationToken.ThrowIfCancellationRequested(); + var hash = Path.GetFileName(directory); + if (hash.Length != HomeFolders.HashLength || !hash.All(Uri.IsHexDigit)) continue; + var key = _options.Identity.Servers.SelectMany(s => s.Aliases).FirstOrDefault(a => HomeFolders.AddressHash(a) == hash.ToLowerInvariant()); + var folder = key is not null ? _options.Identity.FolderName(key) : HomeFolders.AddressPrefix + hash.ToLowerInvariant(); + var target = Path.Combine(servers, folder, HomeFolders.Proxies); + AddCache(HomeEntryKind.Proxies, cache, directory, target, entries, key is null ? null : _options.Identity.Canonical(key)); + } + } + if (_options.CurrentServer is not { Length: > 0 } current) return; + var home = Path.Combine(servers, _options.Identity.FolderName(current)); + var key2 = _options.Identity.Canonical(current); + var renderNode = Path.Combine(cache, LegacyRenderNode); + if (Directory.Exists(renderNode)) AddCache(HomeEntryKind.RenderNode, cache, renderNode, Path.Combine(home, HomeFolders.RenderNode), entries, key2); + var import = Path.Combine(cache, LegacyServerImport); + if (Directory.Exists(import)) AddCache(HomeEntryKind.Import, cache, import, Path.Combine(home, HomeFolders.Import), entries, key2); + } + + /// + /// A cache folder renamed whole into its home, or, where the home already has one, each of its entries the home lacks (an + /// entry the home has stays where it is: the cache can be made again). + /// + private static void AddCache(HomeEntryKind kind, string cache, string source, string target, List entries, string? key) + { + if (!Directory.Exists(target)) + { + var (files, bytes, inventory) = Inventory(source); + entries.Add(new HomeLayoutEntry(kind, cache, source, target, files, bytes, inventory, key)); + return; + } + foreach (var child in new DirectoryInfo(source).EnumerateFileSystemInfos().OrderBy(e => e.Name, StringComparer.Ordinal)) + { + var into = Path.Combine(target, child.Name); + if (Directory.Exists(into) || File.Exists(into)) + { + Log.Info("homes", $"{child.FullName} stays where it is: {into} is there already"); + continue; + } + var (files, bytes, inventory) = child is DirectoryInfo ? Inventory(child.FullName) : FileInventory(child.FullName); + entries.Add(new HomeLayoutEntry(kind, cache, child.FullName, into, files, bytes, inventory, key)); + } + } + + // ---------------- Apply ---------------- + + /// + /// Sorts the folders, or takes up the sorting its journal records (see the class). Projects in use and a workspace that + /// waits are left for a later start; a rename that does not check stops the run with its journal kept. + /// + /// A rename did not leave the folder as planned; the journal stays for the next start. + /// A folder could not be read or renamed; the journal stays for the next start. + public async Task ApplyAsync(CancellationToken cancellationToken = default) + { + var journal = HomeLayoutJournal.Read(JournalPath); + var resumed = journal is not null; + var inUse = 0; + HomeLayoutDeferral? deferred = null; + if (journal is null) + { + if (File.Exists(JournalPath)) throw new HomeLayoutException($"{JournalPath} cannot be read; the folders are left as they are."); + var plan = await PlanAsync(cancellationToken).ConfigureAwait(false); + inUse = plan.InUse.Count; + deferred = plan.Deferred; + if (plan.IsEmpty) return HomeLayoutResult.Nothing with { InUse = inUse, Deferred = deferred }; + journal = new HomeLayoutJournal(HomeLayoutJournal.FormatName, HomeLayoutJournal.CurrentFormatVersion, plan.Fingerprint, plan.Entries, [], + _options.WorkspaceRoot is { Length: > 0 } root ? WorkspaceStore.FullRoot(root) : null, plan.SplitsWorkspace, HomeLayoutStage.Moving, Now); + HomeLayoutJournal.Write(JournalPath, journal); + Cut?.Invoke("journal", JournalPath); + } + else + { + Log.Info("homes", $"The sorting of the folders into homes is taken up from {JournalPath} ({journal.Done.Count} of {journal.Entries.Count} done, {journal.Stage})"); + } + + var (projects, moved, clashes) = (0, 0, 0); + var waiting = false; + if (journal.Stage == HomeLayoutStage.Moving) + { + var done = journal.Done.ToHashSet(); + for (var i = 0; i < journal.Entries.Count; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + if (done.Contains(i)) continue; + var entry = journal.Entries[i]; + if (!Directory.Exists(entry.Root)) + { + // The workspace's drive went since the plan: this entry waits, with the journal, for a start that reaches it. + waiting = true; + if (entry.Root == journal.WorkspaceRoot) deferred = HomeLayoutDeferral.Offline; + continue; + } + switch (Move(entry)) + { + case Outcome.Moved: + moved++; + if (entry.Kind == HomeEntryKind.Project) projects++; + break; + case Outcome.Clash: + moved++; + clashes++; + if (entry.Kind == HomeEntryKind.Project) projects++; + break; + case Outcome.InUse: + inUse++; + break; + } + done.Add(i); + journal = journal with { Done = [.. done.Order()] }; + HomeLayoutJournal.Write(JournalPath, journal); + } + if (waiting) + { + Log.Info("homes", $"The sorting of the folders waits for a folder out of reach; {JournalPath} stays for the next start"); + return new HomeLayoutResult(projects, moved, inUse, deferred ?? HomeLayoutDeferral.Offline, resumed, clashes, false); + } + if (journal.WorkspaceRoot is { } workspace) + { + if (journal.SplitsWorkspace) + { + Cut?.Invoke("commit", workspace); + Commit(workspace, journal); + } + TryDeleteEmpty(Path.Combine(workspace, TrashFolder)); + } + journal = journal with { Stage = HomeLayoutStage.Committed }; + HomeLayoutJournal.Write(JournalPath, journal); + } + File.Delete(JournalPath); + Log.Info("homes", $"The folders are sorted into homes: {projects} project(s) and {moved} entr(ies) moved" + + (clashes > 0 ? $", {clashes} clash(es) kept in the trash or left in place" : string.Empty) + + (inUse > 0 ? $", {inUse} project(s) in use left for a later start" : string.Empty)); + return new HomeLayoutResult(projects, moved, inUse, deferred, resumed, clashes, journal.SplitsWorkspace); + } + + private enum Outcome + { + Moved, + AlreadyMoved, + Clash, + InUse, + } + + /// One entry renamed and checked (see the class). + private Outcome Move(HomeLayoutEntry entry) + { + if (!Exists(entry.Source)) + { + // A run that stopped after the rename: the target holds it. + if (Exists(entry.Target)) + { + if (EntryInventory(entry.Target).Inventory != entry.Inventory) + Log.Warning("homes", $"{entry.Target} is not as {entry.Source} was planned; it is taken as moved"); + return Outcome.AlreadyMoved; + } + Log.Warning("homes", $"{entry.Source} is neither there nor at {entry.Target}"); + return Outcome.AlreadyMoved; + } + if (entry.Kind == HomeEntryKind.Project && Leased(entry.Source)) + { + Log.Info("homes", $"{entry.Source} is in use: it stays where it is and moves at a later start"); + return Outcome.InUse; + } + var outcome = Outcome.Moved; + if (Exists(entry.Target)) + { + if (!IsWorkspaceEntry(entry)) + { + Log.Info("homes", $"{entry.Source} stays where it is: {entry.Target} is there already"); + return Outcome.Clash; + } + outcome = Outcome.Clash; + if (!ResolveClash(entry)) return Outcome.Clash; + } + Prepare(entry); + Cut?.Invoke("rename", entry.Source); + try + { + if (Directory.Exists(entry.Source)) Directory.Move(entry.Source, entry.Target); + else File.Move(entry.Source, entry.Target); + } + catch (Exception ex) when (entry.Kind == HomeEntryKind.Project && ex is IOException or UnauthorizedAccessException) + { + // A file of the project held open (another Chronos on the same workspace): it moves at a later start. + Log.Info("homes", $"{entry.Source} could not be renamed now ({ex.Message}); it moves at a later start"); + return Outcome.InUse; + } + Cut?.Invoke("renamed", entry.Target); + if (Exists(entry.Source) || EntryInventory(entry.Target).Inventory != entry.Inventory) + throw new HomeLayoutException($"{entry.Source} was renamed to {entry.Target}, which does not hold the files planned; the sorting stops there."); + return outcome; + } + + private static bool IsWorkspaceEntry(HomeLayoutEntry entry) => entry.Kind is HomeEntryKind.Project or HomeEntryKind.Trash; + + /// + /// A workspace entry whose target is taken: for a project, the copy whose working state was saved last stays at the target, + /// the other goes to its home's trash (never deleted); for a trash entry, another free name is taken. False when the source + /// itself went to the trash (nothing more to rename). + /// + private bool ResolveClash(HomeLayoutEntry entry) + { + var partition = entry.Kind == HomeEntryKind.Project ? Path.GetDirectoryName(entry.Target)! : Path.GetDirectoryName(Path.GetDirectoryName(entry.Target))!; + if (entry.Kind == HomeEntryKind.Trash) + { + for (var n = 2; ; n++) + { + var free = entry.Target + "-" + n.ToString(CultureInfo.InvariantCulture); + if (Exists(free)) continue; + Prepare(entry); + if (Directory.Exists(entry.Source)) Directory.Move(entry.Source, free); + else File.Move(entry.Source, free); + Log.Info("homes", $"{entry.Source} went to {free}: {entry.Target} is there already"); + return false; + } + } + var id = Guid.ParseExact(Path.GetFileName(entry.Target), "N"); + var sourceNewer = Saved(entry.Source) > Saved(entry.Target); + var loser = sourceNewer ? entry.Target : entry.Source; + _ = new WorkspaceStore(partition); + var trash = WorkspaceTrash.NewEntry(partition, id, Now.UtcDateTime); + Directory.CreateDirectory(Path.GetDirectoryName(trash)!); + Directory.Move(loser, trash); + Log.Warning("homes", $"Project {id} is in {entry.Source} and in {entry.Target}: the copy saved last stays, the other went to {trash}"); + return sourceNewer; + } + + /// When a project folder's working state was saved (its file's time when it names none); the oldest time when it cannot be read. + private static DateTimeOffset Saved(string folder) + { + var path = Path.Combine(folder, WorkspaceProject.BundleFile); + try + { + return ProjectBundle.Load(path).Saved; + } + catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException or System.Text.Json.JsonException) + { + return File.Exists(path) ? new DateTimeOffset(File.GetLastWriteTimeUtc(path), TimeSpan.Zero) : DateTimeOffset.MinValue; + } + } + + /// The folder an entry goes into is made: a workspace home's store with its manifest (and a server's record), a cache home's folder. + private void Prepare(HomeLayoutEntry entry) + { + if (IsWorkspaceEntry(entry)) + { + var partition = entry.Kind == HomeEntryKind.Project ? Path.GetDirectoryName(entry.Target)! : Path.GetDirectoryName(Path.GetDirectoryName(entry.Target))!; + _ = new WorkspaceStore(partition); + if (entry.ServerKey is { } key && ServerHomeRecord.Read(partition) is null) + ServerHomeRecord.Write(partition, ServerHomeRecord.Of(key, entry.ServerName, ServerIdentity.IsIdKey(key) ? null : key, null, Now)); + } + Directory.CreateDirectory(Path.GetDirectoryName(entry.Target)!); + } + + /// The last step: the workspace's parent manifest names the split layout (its id kept), and this PC's home exists. + private static void Commit(string workspace, HomeLayoutJournal journal) + { + var path = Path.Combine(workspace, WorkspaceStore.ManifestFile); + var known = WorkspaceStore.TryRead(path); + if (known is { Layout: WorkspaceHomes.HomesLayout }) return; + _ = new WorkspaceStore(HomeFolders.LocalPath(workspace)); + var manifest = new WorkspaceManifest(WorkspaceStore.FormatName, WorkspaceHomes.ParentFormatVersion, known?.Id ?? Guid.CreateVersion7(), + known?.Created ?? journal.Started, WorkspaceHomes.HomesLayout); + WorkspaceStore.Write(path, manifest); + Log.Info("homes", $"The workspace {workspace} is split into homes (manifest format {WorkspaceHomes.ParentFormatVersion})"); + } + + // ---------------- Re-key ---------------- + + /// + /// The server announced its id: whatever this PC keeps under its address keys + /// moves to its id (owner review 5, #66). The home folder servers\addr-<hash> of each + /// alias is renamed to servers\<id> in the workspace and in the cache (and the 1.0 proxy folder of the alias + /// with it); where the id's home already has one (the server reached at two addresses), projects move one by one and a + /// project in both keeps the copy saved last, the other going to the id home's trash. Working states whose home names an + /// alias name the id key afterwards, each rewritten under its lease (one in use keeps the alias, which matches all the + /// same); and the server's links move to the id key ('s rule). Run before the sync + /// agent starts for the server. Nothing is deleted but a home folder left holding only its manifest and record. + /// + /// A folder could not be read or renamed; what was done stays done, and a later re-key goes on. + public async Task RekeyAsync(Guid serverId, IReadOnlyCollection aliases, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(aliases); + if (serverId == Guid.Empty) throw new ArgumentException("A server id is never empty.", nameof(serverId)); + var idKey = HomeFolders.IdKey(serverId); + var keys = aliases.Where(a => !string.IsNullOrWhiteSpace(a) && !ServerIdentity.IsIdKey(a)).Distinct(StringComparer.OrdinalIgnoreCase).ToList(); + var (homes, projects, trashed, rewritten, held, links, dropped) = (0, 0, 0, 0, 0, 0, 0); + string? workspaceHome = null; + if (_options.WorkspaceRoot is { Length: > 0 } configured && WorkspaceHomes.IsSplitRoot(configured)) + { + var root = WorkspaceStore.FullRoot(configured); + workspaceHome = Path.Combine(root, HomeFolders.Servers, serverId.ToString("N")); + foreach (var alias in keys) + { + cancellationToken.ThrowIfCancellationRequested(); + var from = Path.Combine(root, HomeFolders.Servers, HomeFolders.ServerFolder(alias)); + if (!Directory.Exists(from)) continue; + if (!Directory.Exists(workspaceHome) && !Directory.EnumerateDirectories(from).Any(Leased)) + { + RenameChecked(from, workspaceHome); + homes++; + continue; + } + var (merged, lost) = MergeWorkspaceHome(from, workspaceHome, serverId); + (projects, trashed) = (projects + merged, trashed + lost); + } + if (Directory.Exists(workspaceHome)) + { + var (done, busy) = await RewriteBundlesAsync(workspaceHome, keys, idKey, cancellationToken).ConfigureAwait(false); + (rewritten, held) = (done, busy); + var known = ServerHomeRecord.Read(workspaceHome); + ServerHomeRecord.Write(workspaceHome, (known ?? ServerHomeRecord.Of(idKey, null, keys.FirstOrDefault(), null, Now)) with + { + Key = idKey, + Id = serverId, + Address = known?.Address is { } address && !ServerIdentity.IsIdKey(address) ? address : keys.FirstOrDefault(), + LastSeen = Now, + }); + } + } + if (_options.CacheRoot is { Length: > 0 } cacheRoot && Directory.Exists(cacheRoot)) + { + var cache = Path.GetFullPath(cacheRoot); + var target = Path.Combine(cache, HomeFolders.Servers, serverId.ToString("N")); + foreach (var alias in keys) + { + cancellationToken.ThrowIfCancellationRequested(); + var from = Path.Combine(cache, HomeFolders.Servers, HomeFolders.ServerFolder(alias)); + if (Directory.Exists(from)) + { + if (!Directory.Exists(target)) + { + RenameChecked(from, target); + homes++; + } + else + { + foreach (var area in new DirectoryInfo(from).EnumerateDirectories()) MergeCache(area.FullName, Path.Combine(target, area.Name)); + } + } + var legacy = Path.Combine(cache, LegacyProxies, HomeFolders.AddressHash(alias)); + if (Directory.Exists(legacy)) MergeCache(legacy, Path.Combine(target, HomeFolders.Proxies)); + } + } + if (_options.Database is { } db) + { + var store = new SyncLinkStore(db); + foreach (var alias in keys) + { + var (m, d) = await store.RekeyAsync(alias, idKey, cancellationToken).ConfigureAwait(false); + (links, dropped) = (links + m, dropped + d); + } + } + var result = new HomeRekeyResult(homes, projects, trashed, rewritten, held, links, dropped); + Log.Info("homes", $"Server {serverId} re-keyed from {string.Join(", ", keys)}: {result}"); + return result; + } + + /// The projects and trash of the alias home moved into the id home (see ). + private (int Moved, int Trashed) MergeWorkspaceHome(string from, string into, Guid serverId) + { + var (moved, trashed) = (0, 0); + _ = new WorkspaceStore(into); + foreach (var directory in Directory.EnumerateDirectories(from).Order(StringComparer.Ordinal)) + { + if (!Guid.TryParseExact(Path.GetFileName(directory), "N", out var id)) continue; + if (Leased(directory)) + { + Log.Info("homes", $"{directory} is in use: it stays in the home of an address of server {serverId} and moves at a later re-key"); + continue; + } + var target = Path.Combine(into, id.ToString("N")); + if (Directory.Exists(target)) + { + var sourceNewer = Saved(directory) > Saved(target); + var loser = sourceNewer ? target : directory; + var trash = WorkspaceTrash.NewEntry(into, id, Now.UtcDateTime); + Directory.CreateDirectory(Path.GetDirectoryName(trash)!); + RenameChecked(loser, trash); + trashed++; + Log.Warning("homes", $"Project {id} is in {directory} and in {target}: the copy saved last stays, the other went to {trash}"); + if (!sourceNewer) continue; + } + RenameChecked(directory, target); + moved++; + } + var trashFrom = Path.Combine(from, TrashFolder); + if (Directory.Exists(trashFrom)) + { + var trashInto = Path.Combine(into, TrashFolder); + Directory.CreateDirectory(trashInto); + foreach (var entry in new DirectoryInfo(trashFrom).EnumerateFileSystemInfos().OrderBy(e => e.Name, StringComparer.Ordinal)) + { + var target = Path.Combine(trashInto, entry.Name); + for (var n = 2; Exists(target); n++) target = Path.Combine(trashInto, entry.Name + "-" + n.ToString(CultureInfo.InvariantCulture)); + RenameChecked(entry.FullName, target); + } + TryDeleteEmpty(trashFrom); + } + // Left with its manifest and record only: Chronos's own description of a folder that holds nothing now. + var left = Directory.EnumerateFileSystemEntries(from).Select(Path.GetFileName).ToList(); + if (left.All(n => n is WorkspaceStore.ManifestFile or HomeFolders.ServerFile)) + { + foreach (var name in left) File.Delete(Path.Combine(from, name!)); + TryDeleteEmpty(from); + } + return (moved, trashed); + } + + /// Each working state in whose home names an alias rewritten to name , under its lease. + private static async Task<(int Rewritten, int InUse)> RewriteBundlesAsync(string home, IReadOnlyList aliases, string idKey, CancellationToken cancellationToken) + { + var (rewritten, inUse) = (0, 0); + var store = new WorkspaceStore(home); + foreach (var project in store.List()) + { + cancellationToken.ThrowIfCancellationRequested(); + ProjectBundle bundle; + try + { + bundle = project.ReadBundle(); + } + catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) + { + Log.Info("homes", $"The project in {project.Folder} cannot be read: its home stays as it is ({ex.Message})"); + continue; + } + if (bundle.Workspace is not { Home: { Kind: BundleHome.ServerKind, Server: { } server } } || !aliases.Contains(server, StringComparer.OrdinalIgnoreCase)) + continue; + IDisposable lease; + try + { + lease = await project.LockAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false); + } + catch (LeaseUnavailableException) + { + inUse++; + continue; + } + using (lease) + { + bundle = project.ReadBundle(); + if (bundle.Workspace is not { Home: { Server: { } now } } current || !aliases.Contains(now, StringComparer.OrdinalIgnoreCase)) continue; + var archive = bundle.Archive.Id == ServerProjectHome.ArchiveIdOf(now) ? bundle.Archive with { Id = ServerProjectHome.ArchiveIdOf(idKey) } : bundle.Archive; + project.WriteBundle(bundle with { Archive = archive, Workspace = current with { Home = current.Home with { Server = idKey } } }); + rewritten++; + } + } + return (rewritten, inUse); + } + + /// A cache folder's entries moved into where it lacks them; the source renamed whole when is not there. What is left stays (the cache trims it). + private static void MergeCache(string from, string into) + { + if (!Directory.Exists(into)) + { + Directory.CreateDirectory(Path.GetDirectoryName(into)!); + RenameChecked(from, into); + return; + } + foreach (var entry in new DirectoryInfo(from).EnumerateFileSystemInfos()) + { + var target = Path.Combine(into, entry.Name); + if (Exists(target)) continue; + RenameChecked(entry.FullName, target); + } + TryDeleteEmpty(from); + } + + /// A folder or file renamed, checked by inventory: the same relative names and sizes at the target, the source gone. + private static void RenameChecked(string source, string target) + { + var directory = Directory.Exists(source); + var before = directory ? Inventory(source).Inventory : FileInventory(source).Inventory; + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + if (directory) Directory.Move(source, target); + else File.Move(source, target); + var after = directory ? Inventory(target).Inventory : FileInventory(target).Inventory; + if (Exists(source) || after != before) throw new HomeLayoutException($"{source} was renamed to {target}, which does not hold the same files."); + } + + // ---------------- Helpers ---------------- + + /// A workspace move job is waiting, running or stopped (null database: none known). + private async Task MovePendingAsync(CancellationToken cancellationToken) => + _options.Database is { } db && await db.Jobs.HasPendingAsync([JobKind.WorkspaceMove], null, cancellationToken).ConfigureAwait(false); + + /// The folder's volume is there (so a folder missing on it was never made, rather than out of reach). + private static bool Reachable(string folder) + { + for (var current = Path.GetDirectoryName(folder); !string.IsNullOrEmpty(current); current = Path.GetDirectoryName(current)) + { + if (Directory.Exists(current)) return true; + } + return false; + } + + /// The project folder's lease is held now (another Chronos, with another data folder, works on the same workspace). + private bool Leased(string folder) => + LeaseFile.Read(Path.Combine(folder, WorkspaceProject.LeaseFileName)) is { } held && held.Expires > Now; + + private static bool Exists(string path) => Directory.Exists(path) || File.Exists(path); + + /// The inventory of an entry at : a folder's, or a single file's (a trash entry that is one). + private static (int Files, long Bytes, string Inventory) EntryInventory(string path) => File.Exists(path) ? FileInventory(path) : Inventory(path); + + /// A folder's files (leases left out): their count and bytes, and the SHA-256 of their relative names and sizes in ordinal order. + internal static (int Files, long Bytes, string Inventory) Inventory(string folder) + { + var lines = new List(); + var bytes = 0L; + foreach (var file in new DirectoryInfo(folder).EnumerateFiles("*", SearchOption.AllDirectories)) + { + if (WorkspaceProject.IsLeaseFile(file.Name)) continue; + bytes += file.Length; + lines.Add(Path.GetRelativePath(folder, file.FullName).Replace('\\', '/') + "|" + file.Length.ToString(CultureInfo.InvariantCulture)); + } + lines.Sort(StringComparer.Ordinal); + return (lines.Count, bytes, Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(string.Join('\n', lines))))); + } + + private static (int Files, long Bytes, string Inventory) FileInventory(string file) + { + var length = new FileInfo(file).Length; + return (1, length, Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes("|" + length.ToString(CultureInfo.InvariantCulture))))); + } + + private static void TryDeleteEmpty(string folder) + { + try + { + if (Directory.Exists(folder) && !Directory.EnumerateFileSystemEntries(folder).Any()) Directory.Delete(folder); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Debug("homes", $"{folder} stays: {ex.Message}"); + } + } +} diff --git a/src/Chronos.Data/Projects/ServerProjectHome.cs b/src/Chronos.Data/Projects/ServerProjectHome.cs index 7f97ddb3..a146d3aa 100644 --- a/src/Chronos.Data/Projects/ServerProjectHome.cs +++ b/src/Chronos.Data/Projects/ServerProjectHome.cs @@ -28,12 +28,16 @@ public sealed class ServerProjectHome : IProjectHome /// The server's name as its server/info gives it, for the project file; the address's host when null. /// The settings format its server/info announces (); null, an older server's 1. - public ServerProjectHome(ChronosClient client, Guid sequenceId, string? serverName = null, int? settingsFormat = null) + /// + /// The key workspace projects name the server by (: its id key once it + /// announced one, owner review 5); null takes the client's address key (). + /// + public ServerProjectHome(ChronosClient client, Guid sequenceId, string? serverName = null, int? settingsFormat = null, string? server = null) { ArgumentNullException.ThrowIfNull(client); Client = client; SequenceId = sequenceId; - Server = SyncService.ServerKey(client.BaseAddress); + Server = string.IsNullOrWhiteSpace(server) ? SyncService.ServerKey(client.BaseAddress) : server; ServerName = string.IsNullOrWhiteSpace(serverName) ? client.BaseAddress.Host : serverName.Trim(); SettingsFormat = settingsFormat is > 0 and var format ? format : ProjectSettings.BaseVersion; } @@ -80,6 +84,17 @@ public sealed class ServerProjectHome : IProjectHome return new Guid(hash.AsSpan(0, 16), bigEndian: true); } + /// + /// is the archive id a project file of a sequence of the server name + /// carries ( of any of them: its id key, or an address key it was reached under before it + /// announced its id, owner review 5). + /// + public static bool IsArchiveIdOf(Guid archiveId, IEnumerable keys) + { + ArgumentNullException.ThrowIfNull(keys); + return keys.Any(key => !string.IsNullOrWhiteSpace(key) && ArchiveIdOf(key) == archiveId); + } + public Task> ListAsync(CancellationToken cancellationToken) => Reach>(async () => [.. (await Client.ListProjectsAsync(SequenceId, cancellationToken).ConfigureAwait(false)).Select(ProjectWire.ToProject)], cancellationToken); diff --git a/src/Chronos.Data/Sync/ProxyCache.cs b/src/Chronos.Data/Sync/ProxyCache.cs index 6af4f9c4..2834ee11 100644 --- a/src/Chronos.Data/Sync/ProxyCache.cs +++ b/src/Chronos.Data/Sync/ProxyCache.cs @@ -69,11 +69,15 @@ internal sealed record ProxyCacheIndex(SequenceDto Sequence, string? SettingsJso internal sealed partial class ProxyCacheJson : JsonSerializerContext; /// -/// Proxies of server sequences on this PC: <root>/<server hash>/<sequence id>/ with one JPEG per -/// frame (named by frame id, so renumbering never mixes them up) and remote.json. Everything here can be -/// downloaded again, so the folder lives in the client's cache. +/// Proxies of server sequences on this PC, one folder per sequence with one JPEG per frame (named by frame id, so +/// renumbering never mixes them up) and remote.json. With a servers root (owner review 5, #66) a sequence's folder is +/// <servers>/<home folder>/proxies/<sequence id>/ ( of the +/// server's canonical key); a folder still where an earlier layout put it (<root>/<address hash>/<sequence +/// id>/, 1.0; or the home folder of one of the server's aliases) is used where it is until it is moved, so a sequence +/// always has one folder. Without a servers root the 1.0 layout stays. Everything here can be downloaded again, so the +/// folder lives in the client's cache. /// -internal sealed class ProxyCache(string root) +internal sealed class ProxyCache(string root, string? servers = null, Func? identity = null) { /// The asset kind of a cached proxy (the server's name for it). public const string Proxy = "proxy"; @@ -83,8 +87,27 @@ internal sealed class ProxyCache(string root) private const string IndexFile = "remote.json"; - public string Folder(string server, Guid remoteId) => - Path.Combine(root, Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(server)))[..16], remoteId.ToString("N")); + /// The 1.0 folder of a server sequence's proxies: <root>/<address hash>/<sequence id>. + internal static string LegacyFolder(string root, string server, Guid remoteId) => + Path.Combine(root, Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(server)))[..HomeFolders.HashLength], remoteId.ToString("N")); + + /// The folder of a server sequence's proxies: where it is now, or where a new one goes (see the class). + public string Folder(string server, Guid remoteId) + { + if (servers is null) return LegacyFolder(root, server, remoteId); + var known = identity?.Invoke() ?? ServerIdentity.None; + var name = remoteId.ToString("N"); + var target = Path.Combine(servers, known.FolderName(server), HomeFolders.Proxies, name); + if (Directory.Exists(target)) return target; + foreach (var key in known.KeysOf(server)) + { + var home = Path.Combine(servers, HomeFolders.ServerFolder(key), HomeFolders.Proxies, name); + if (Directory.Exists(home)) return home; + var legacy = LegacyFolder(root, key, remoteId); + if (Directory.Exists(legacy)) return legacy; + } + return target; + } public static string FileName(Guid frameId) => frameId.ToString("N") + ".jpg"; diff --git a/src/Chronos.Data/Sync/ServerIdentity.cs b/src/Chronos.Data/Sync/ServerIdentity.cs new file mode 100644 index 00000000..3be8b836 --- /dev/null +++ b/src/Chronos.Data/Sync/ServerIdentity.cs @@ -0,0 +1,121 @@ +using Chronos.Core.IO; +using Chronos.Protocol; + +namespace Chronos.Data.Sync; + +/// A server known by its stable id, with the address keys it was reached under (). +/// The server's id (). +/// The legacy address keys this id was reached under, in any case. +public sealed record KnownServerKey(Guid Id, IReadOnlyCollection Aliases); + +/// +/// How the desktop names a server (owner review 5, #66). A server that announces its stable id +/// () is keyed id:<N> (), the same whatever +/// address it is reached by; an older one keeps its address key (, the 1.0 key, here the +/// ). An instance holds the alias table: the address keys each known id was reached under, which +/// projects, links and jobs made before the id was known still carry. turns an alias into its id +/// key, compares two keys through it, and is the home's folder +/// () of the canonical key. Keys compare ignoring case, as address keys always did. +/// +public sealed class ServerIdentity +{ + private readonly Dictionary _canonical = new(StringComparer.OrdinalIgnoreCase); + private readonly Dictionary> _aliases = new(StringComparer.OrdinalIgnoreCase); + + /// No server known by id: every key is its own. + public static ServerIdentity None { get; } = new([]); + + public ServerIdentity(IEnumerable servers) + { + ArgumentNullException.ThrowIfNull(servers); + var known = new List(); + foreach (var server in servers) + { + if (server.Id == Guid.Empty) continue; + var idKey = HomeFolders.IdKey(server.Id); + if (!_aliases.TryGetValue(idKey, out var list)) + { + list = []; + _aliases[idKey] = list; + } + foreach (var alias in server.Aliases ?? []) + { + if (string.IsNullOrWhiteSpace(alias) || string.Equals(alias, idKey, StringComparison.OrdinalIgnoreCase)) continue; + // An address that two ids claim (a server replaced at the same address) stays with the first one listed. + if (!_canonical.TryAdd(alias, idKey)) continue; + list.Add(alias); + } + known.Add(server); + } + Servers = known; + } + + /// The servers this table knows, as it was given them. + public IReadOnlyList Servers { get; } + + /// The key of a server reached at : its id key when announces an id, else its address key. + public static string Key(ServerInfo? info, Uri address) => Key(info?.ServerId, address); + + /// The key of a server reached at : id:<N> for an id, else its address key. + public static string Key(Guid? serverId, Uri address) + { + ArgumentNullException.ThrowIfNull(address); + return serverId is { } id && id != Guid.Empty ? HomeFolders.IdKey(id) : LegacyKey(address); + } + + /// The key of a server known by its id. + public static string IdKey(Guid serverId) => HomeFolders.IdKey(serverId); + + /// The address key (): how 1.0 named every server, and how an older server is still named. + public static string LegacyKey(Uri address) => SyncService.ServerKey(address); + + /// The id of an id key; null for an address key. + public static Guid? IdOf(string? key) => HomeFolders.IdOfKey(key); + + /// The key names the server by its id. + public static bool IsIdKey(string? key) => IdOf(key) is not null; + + /// The id key an alias stands for; any other key as it is. + public string Canonical(string key) + { + ArgumentNullException.ThrowIfNull(key); + if (IdOf(key) is { } id) return HomeFolders.IdKey(id); + return _canonical.TryGetValue(key, out var idKey) ? idKey : key; + } + + /// Both keys name one server: equal ignoring case, or one an alias of the other's id (null equals only null). + public bool Same(string? a, string? b) + { + if (a is null || b is null) return a is null && b is null; + return string.Equals(Canonical(a), Canonical(b), StringComparison.OrdinalIgnoreCase); + } + + /// Every key that names the server of : its canonical key first, then its aliases. + public IReadOnlyList KeysOf(string key) + { + var canonical = Canonical(key); + var keys = new List { canonical }; + if (_aliases.TryGetValue(canonical, out var aliases)) keys.AddRange(aliases); + if (!keys.Contains(key, StringComparer.OrdinalIgnoreCase)) keys.Add(key); + return keys; + } + + /// The aliases of a server id (its legacy address keys); empty when none is known. + public IReadOnlyList AliasesOf(Guid serverId) => + _aliases.TryGetValue(HomeFolders.IdKey(serverId), out var aliases) ? aliases : []; + + /// The folder of the home of ( of its canonical key). + public string FolderName(string key) => HomeFolders.ServerFolder(Canonical(key)); + + /// This table with added to the aliases of (an address it was reached under). + public ServerIdentity With(Guid serverId, string alias) + { + ArgumentException.ThrowIfNullOrWhiteSpace(alias); + var servers = Servers.ToList(); + var index = servers.FindIndex(s => s.Id == serverId); + if (index < 0) servers.Add(new KnownServerKey(serverId, [alias])); + else if (!servers[index].Aliases.Contains(alias, StringComparer.OrdinalIgnoreCase)) servers[index] = servers[index] with { Aliases = [.. servers[index].Aliases, alias] }; + else return this; + return new ServerIdentity(servers); + } +} diff --git a/src/Chronos.Data/Sync/SyncLinkStore.cs b/src/Chronos.Data/Sync/SyncLinkStore.cs index ca38a076..238ad221 100644 --- a/src/Chronos.Data/Sync/SyncLinkStore.cs +++ b/src/Chronos.Data/Sync/SyncLinkStore.cs @@ -43,6 +43,53 @@ internal sealed class SyncLinkStore(ChronosDatabase db) public Task DeleteAsync(string server, Guid remoteId, CancellationToken cancellationToken) => _db.ExecuteAsync("DELETE FROM sync_links WHERE server = $1 AND remote_id = $2", [server, remoteId], cancellationToken); + /// + /// Moves every link of to in one transaction (owner review 5, #66: an address + /// key becomes the server's id key). Where already links the same server sequence, or the same local + /// copy, the row compared last (, never compared counting oldest) is kept and the other + /// deleted: link rows are recomputed by the next comparison, so nothing is lost. + /// + public async Task<(int Moved, int Dropped)> RekeyAsync(string from, string to, CancellationToken cancellationToken) + { + ArgumentException.ThrowIfNullOrWhiteSpace(from); + ArgumentException.ThrowIfNullOrWhiteSpace(to); + if (string.Equals(from, to, StringComparison.Ordinal)) return (0, 0); + return await _db.InTransactionAsync(async session => + { + var inner = new DbAccess(session); + var old = await inner.ListAsync($"SELECT {Columns} FROM sync_links WHERE server = $1 ORDER BY remote_id", [from], Map, cancellationToken) + .ConfigureAwait(false); + if (old.Count == 0) return (0, 0); + var current = (await inner.ListAsync($"SELECT {Columns} FROM sync_links WHERE server = $1 ORDER BY remote_id", [to], Map, cancellationToken) + .ConfigureAwait(false)).ToList(); + var (moved, dropped) = (0, 0); + foreach (var link in old) + { + var clashes = current.Where(c => c.RemoteId == link.RemoteId || (link.LocalId is { } local && c.LocalId == local)).ToList(); + if (clashes.Any(c => Newer(c, link))) + { + await inner.ExecuteAsync("DELETE FROM sync_links WHERE server = $1 AND remote_id = $2", [from, link.RemoteId], cancellationToken).ConfigureAwait(false); + dropped++; + continue; + } + foreach (var clash in clashes) + { + await inner.ExecuteAsync("DELETE FROM sync_links WHERE server = $1 AND remote_id = $2", [to, clash.RemoteId], cancellationToken).ConfigureAwait(false); + current.Remove(clash); + dropped++; + } + await inner.ExecuteAsync("UPDATE sync_links SET server = $3 WHERE server = $1 AND remote_id = $2", [from, link.RemoteId, to], cancellationToken) + .ConfigureAwait(false); + current.Add(link with { Server = to }); + moved++; + } + return (moved, dropped); + }, cancellationToken).ConfigureAwait(false); + + // The row already under the new key stays on a tie: it is the one the signed-in session works with. + static bool Newer(SyncLink kept, SyncLink other) => (kept.SyncedAt ?? DateTimeOffset.MinValue) >= (other.SyncedAt ?? DateTimeOffset.MinValue); + } + /// Records where a local sequence stands for the Library; touches neither its versions nor its update time. public Task SetLocalStateAsync(Guid localId, SyncState state, CancellationToken cancellationToken) => _db.ExecuteAsync("UPDATE sequences SET sync_state = $2 WHERE id = $1 AND sync_state <> $2", [localId, (int)state], cancellationToken); diff --git a/src/Chronos.Data/Sync/SyncRemote.cs b/src/Chronos.Data/Sync/SyncRemote.cs index 0261abfa..e9a43b85 100644 --- a/src/Chronos.Data/Sync/SyncRemote.cs +++ b/src/Chronos.Data/Sync/SyncRemote.cs @@ -115,7 +115,11 @@ public interface ISyncConnector } /// over a signed-in , which it disposes when it owns it. -public sealed class ChronosSyncRemote(ChronosClient client, bool ownsClient) : ISyncRemote +/// +/// The key links and homes name the server by (: its id key once it announced +/// one, owner review 5); null takes the client's address key (). +/// +public sealed class ChronosSyncRemote(ChronosClient client, bool ownsClient, string? server = null) : ISyncRemote { private const int SequencePage = 500; private const int FramePage = 1000; @@ -137,7 +141,7 @@ public sealed class ChronosSyncRemote(ChronosClient client, bool ownsClient) : I /// The settings format the server announced, and when it was read (a timestamp). private sealed record KnownFormat(int Format, long Read); - public string Server { get; } = SyncService.ServerKey(client.BaseAddress); + public string Server { get; } = string.IsNullOrWhiteSpace(server) ? SyncService.ServerKey(client.BaseAddress) : server; public async Task> ListSequencesAsync(CancellationToken cancellationToken) { diff --git a/src/Chronos.Data/Sync/SyncService.cs b/src/Chronos.Data/Sync/SyncService.cs index 7927bb61..6aebaaed 100644 --- a/src/Chronos.Data/Sync/SyncService.cs +++ b/src/Chronos.Data/Sync/SyncService.cs @@ -26,9 +26,23 @@ public sealed record SyncOptions /// Decoders, lease owner and lease timing for sidecar and project writes (shared with the archive engine). public ArchiveServiceOptions Archive { get; init; } = new(); - /// Folder of the proxy cache of server sequences (inside the client's cache). + /// + /// Folder of the proxy cache of server sequences as 1.0 laid it out (<cache>\proxies\<address hash>): + /// where proxies go without , and where a sequence's proxies are still read from, with it, + /// until they are moved into their server's home. + /// public required string ProxyCacheDirectory { get; init; } + /// + /// The servers' homes in the client's cache (<cache>\servers, owner review 5, #66): with it, a server's + /// proxies go to <servers>\<home folder>\proxies\<sequence id> + /// (). Null keeps the 1.0 layout under . + /// + public string? ServersDirectory { get; init; } + + /// The alias table the cache finds a server's home by (); null: every key is its own. + public Func? Identity { get; init; } + /// Concurrent frame downloads of one job. public int Downloads { get; init; } = 4; @@ -74,7 +88,7 @@ public sealed class SyncService Options = options; _links = new SyncLinkStore(db); _writer = new SequenceWriter(options.Archive); - Proxies = new ProxyCache(options.ProxyCacheDirectory); + Proxies = new ProxyCache(options.ProxyCacheDirectory, options.ServersDirectory, options.Identity); } public SyncOptions Options { get; } @@ -90,9 +104,27 @@ public sealed class SyncService internal ArchiveService Archives { get; } - /// "https://nas.local:8443": the key links are stored under (scheme, lower-case host, port, path). + /// + /// "https://nas.local:8443": the address key (scheme, lower-case host, port, path), the key every server had in 1.0 and an + /// older server, which announces no id, still has. A server that announces one is keyed by it + /// (), its address keys kept as aliases. + /// public static string ServerKey(Uri address) => address.GetLeftPart(UriPartial.Path).TrimEnd('/').ToLowerInvariant(); + /// + /// Moves the links of (an address key the server was reached under) to (its + /// id key, owner review 5, #66) in one transaction. A link both keys hold for one server sequence, or for one local copy, + /// keeps the row compared last (); the other row goes, which loses nothing: the next + /// comparison makes links again. Nothing happens when the keys are the same. + /// + /// The links moved, and those dropped for a newer one. + public async Task<(int Moved, int Dropped)> RekeyAsync(string from, string to, CancellationToken cancellationToken) + { + var result = await _links.RekeyAsync(from, to, cancellationToken).ConfigureAwait(false); + if (result.Moved + result.Dropped > 0) Changed?.Invoke(to); + return result; + } + /// What an upload or download job moves; null for any other job. public static SyncTransferKind? TransferKind(Job job) => SyncJobParameters.Read(job)?.Kind(job.Kind); diff --git a/src/Chronos.Data/Workspaces/WorkspaceCleaner.cs b/src/Chronos.Data/Workspaces/WorkspaceCleaner.cs index dc36204e..d363c476 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceCleaner.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceCleaner.cs @@ -92,21 +92,39 @@ public sealed class WorkspaceCleaner { private static readonly JobKind[] ProjectJobs = [JobKind.WorkspaceCopy, JobKind.WorkspaceRender, JobKind.WorkspaceSend]; - private readonly WorkspaceSync _sync; + private readonly IReadOnlyList _syncs; private readonly ChronosDatabase _db; private readonly Func _homes; /// The home of a workspace project; null for one this PC cannot reach now. public WorkspaceCleaner(WorkspaceSync sync, ChronosDatabase db, Func homes) + : this([sync ?? throw new ArgumentNullException(nameof(sync))], db, homes) { - ArgumentNullException.ThrowIfNull(sync); + } + + /// + /// The clean-up of a workspace split into homes (owner review 5, ): one sync per home's store, + /// and one run over all of them, so the size limit counts every home's projects together. + /// + /// The sync of each home's store. + /// The home of a workspace project; null for one this PC cannot reach now. + public WorkspaceCleaner(IReadOnlyList syncs, ChronosDatabase db, Func homes) + { + ArgumentNullException.ThrowIfNull(syncs); ArgumentNullException.ThrowIfNull(db); ArgumentNullException.ThrowIfNull(homes); - _sync = sync; + if (syncs.Count == 0 || syncs.Any(s => s is null)) throw new ArgumentException("At least one sync, and none null.", nameof(syncs)); + _syncs = syncs; _db = db; _homes = homes; } + /// The sync of the store is in. + private WorkspaceSync SyncOf(WorkspaceProject project) => + _syncs.FirstOrDefault(s => ReferenceEquals(s.Store, project.Store)) + ?? _syncs.FirstOrDefault(s => string.Equals(s.Store.Root, project.Store.Root, StringComparison.OrdinalIgnoreCase)) + ?? _syncs[0]; + /// How long a project another writer holds is waited for; past it the project is kept (). public TimeSpan LockWait { get; init; } = TimeSpan.FromSeconds(30); @@ -120,7 +138,7 @@ public sealed class WorkspaceCleaner ArgumentNullException.ThrowIfNull(options); var candidates = new List(); var used = 0L; - foreach (var project in _sync.Store.List()) + foreach (var project in _syncs.SelectMany(s => s.Store.List())) { cancellationToken.ThrowIfCancellationRequested(); var candidate = Candidate.Of(project, options); @@ -251,7 +269,7 @@ public sealed class WorkspaceCleaner WorkspaceStatus status; try { - status = await _sync.StatusAsync(id, home, cancellationToken).ConfigureAwait(false); + status = await SyncOf(project).StatusAsync(id, home, cancellationToken).ConfigureAwait(false); } catch (Exception ex) when (ex is ProjectException or DatabaseException or IOException or InvalidDataException or LeaseUnavailableException) { @@ -288,7 +306,7 @@ public sealed class WorkspaceCleaner { // Asked again under the lease: the editor may have opened it, a save or a render may have come meanwhile. if (options.IsOpen(id)) return (WorkspaceKeep.Open, 0, 0); - var local = await _sync.StatusAsync(id, null, cancellationToken).ConfigureAwait(false); + var local = await SyncOf(project).StatusAsync(id, null, cancellationToken).ConfigureAwait(false); if (local.State != WorkspaceState.InSync) return (WorkspaceKeep.NotInSync, 0, 0); if (ReadLedger(project) is not { } renders) return (WorkspaceKeep.Unreadable, 0, 0); if (renders.Unsent.Count > 0) return (WorkspaceKeep.UnsentRenders, 0, 0); diff --git a/src/Chronos.Data/Workspaces/WorkspaceHomes.cs b/src/Chronos.Data/Workspaces/WorkspaceHomes.cs new file mode 100644 index 00000000..d5a8b16f --- /dev/null +++ b/src/Chronos.Data/Workspaces/WorkspaceHomes.cs @@ -0,0 +1,293 @@ +using System.Text.Json; +using Chronos.Core.Diagnostics; +using Chronos.Core.IO; +using Chronos.Core.Project; +using Chronos.Data.Sync; + +namespace Chronos.Data.Workspaces; + +/// +/// What a server home's folder says about its server (, owner review 5, #66): the key it +/// is named by, the id, the name and last address it was reached at, its TLS fingerprint and when it was first and last seen. +/// Someone browsing the folders, or a workspace moved to another PC, knows from it what the folder is. +/// +public sealed record ServerHomeRecord(string Format, int FormatVersion, string Key, Guid? Id, string? Name, string? Address, string? TlsFingerprint, + DateTimeOffset FirstSeen, DateTimeOffset LastSeen) +{ + public const string FormatName = "chronos-server-home"; + public const int CurrentFormatVersion = 1; + + /// A record of first and last seen . + public static ServerHomeRecord Of(string key, string? name, string? address, string? tlsFingerprint, DateTimeOffset now) => + new(FormatName, CurrentFormatVersion, key, ServerIdentity.IdOf(key), name, address, tlsFingerprint, now, now); + + /// + /// Reads the record in ; null when there is none or it cannot be read (it only describes the + /// folder: nothing depends on it). + /// + public static ServerHomeRecord? Read(string folder) + { + ArgumentException.ThrowIfNullOrWhiteSpace(folder); + var path = Path.Combine(folder, HomeFolders.ServerFile); + if (!File.Exists(path)) return null; + try + { + return JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), WorkspaceJson.Default.ServerHomeRecord) is { Format: FormatName, Key.Length: > 0 } record + ? record + : null; + } + catch (Exception ex) when (ex is JsonException or IOException or UnauthorizedAccessException) + { + Log.Debug("workspace", $"{path} cannot be read: {ex.Message}"); + return null; + } + } + + /// Writes into (made when it is not there), whole and atomically. + public static void Write(string folder, ServerHomeRecord record) + { + ArgumentException.ThrowIfNullOrWhiteSpace(folder); + ArgumentNullException.ThrowIfNull(record); + Directory.CreateDirectory(folder); + AtomicFile.WriteAllText(Path.Combine(folder, HomeFolders.ServerFile), JsonSerializer.Serialize(record, WorkspaceJson.Default.ServerHomeRecord)); + } +} + +/// One home of a workspace (): its folder and store, and for a server home its key and record. +/// The folder's name: , a server folder (), or empty for the projects still in the parent. +/// The key the server home's folder is named by (from its record, else made from the folder's name); null for this PC's home. +public sealed record WorkspaceHome(string Folder, WorkspaceStore Store, string? ServerKey, ServerHomeRecord? Server) +{ + /// This PC's archive's home. + public bool IsLocal => Folder == HomeFolders.Local; + + /// Projects 1.0 kept in the parent itself, not moved into their home yet (in use when the folders were sorted). + public bool IsLegacy => Folder.Length == 0; +} + +/// +/// The desktop's workspace split into one store per home (owner review 5, #66): Settings › Workspace › Folder is the +/// parent, whose workspace.json is of format with layout +/// (a 1.0 Chronos refuses it as newer, so it never shows the workspace empty), and each home keeps its projects in its own +/// : <parent>\local for this PC's archive and <parent>\servers\<folder> +/// for each server (, with a beside its manifest). Each +/// is an ordinary store with its own manifest and .trash, so sync, copies, renders, sends, leases and the trash work +/// on it unchanged. A parent still in the 1.0 layout (projects side by side in it, a manifest of format 1) is used as it is, +/// one store for every home, until sorts it; projects that sorting could not move +/// (in use then) are still listed where they are. A new, empty parent is made split. +/// +public sealed class WorkspaceHomes +{ + /// The format of a split parent's manifest (a store's is ). + public const int ParentFormatVersion = 2; + + /// The layout a split parent's manifest names. + public const string HomesLayout = "homes"; + + private readonly Lock _gate = new(); + private readonly Dictionary _stores = new(StringComparer.OrdinalIgnoreCase); + private readonly WorkspaceStore? _legacy; + private ServerIdentity _identity; + + /// Opens the workspace at , making the folder, and a split manifest when it holds none and no project. + /// The servers known by id and their aliases (). + /// The folder's workspace.json is not a workspace manifest, or a newer one. + /// The folder cannot be made or read. + public WorkspaceHomes(string root, ServerIdentity? identity = null) + { + ArgumentException.ThrowIfNullOrWhiteSpace(root); + _identity = identity ?? ServerIdentity.None; + Root = WorkspaceStore.FullRoot(root); + Directory.CreateDirectory(Root); + var path = Path.Combine(Root, WorkspaceStore.ManifestFile); + var manifest = File.Exists(path) ? WorkspaceStore.TryRead(path) : null; + if (manifest is { Layout: HomesLayout }) + { + if (manifest.FormatVersion > ParentFormatVersion) + throw new InvalidDataException($"{path} uses format {manifest.FormatVersion}, newer than this Chronos ({ParentFormatVersion})."); + Manifest = manifest; + } + else if (manifest is null && !File.Exists(path) && !HoldsProjects(Root)) + { + Manifest = CreateSplit(path); + } + else + { + // The 1.0 layout (or a manifest the store makes again or refuses): one store for every home until it is sorted. + _legacy = new WorkspaceStore(Root); + Manifest = _legacy.Manifest; + } + if (IsSplit && HoldsProjects(Root)) _legacy = new WorkspaceStore(Root, Manifest); + } + + /// The parent folder (Settings › Workspace › Folder). + public string Root { get; } + + /// The parent's manifest: a split one, or the 1.0 store's. + public WorkspaceManifest Manifest { get; } + + /// The parent is split into homes; false while it is in the 1.0 layout (one store for every home). + public bool IsSplit => Manifest.Layout == HomesLayout; + + /// The servers known by id and their aliases: a server home's folder is named by its canonical key. + public ServerIdentity Identity + { + get => Volatile.Read(ref _identity); + set => Volatile.Write(ref _identity, value ?? ServerIdentity.None); + } + + /// The folder of this PC's home (whether it exists or not). + public string LocalRoot => HomeFolders.LocalPath(Root); + + /// The folder of the home of server (whether it exists or not). + public string ServerRoot(string serverKey) + { + ArgumentException.ThrowIfNullOrWhiteSpace(serverKey); + return Path.Combine(Root, HomeFolders.Servers, Identity.FolderName(serverKey)); + } + + /// The folder of the store a project of belongs in: its home's, or the parent while it is not split. + public string RootFor(BundleHome home) + { + ArgumentNullException.ThrowIfNull(home); + if (!IsSplit) return Root; + return home.Kind == BundleHome.ServerKind && home.Server is { Length: > 0 } server ? ServerRoot(server) : LocalRoot; + } + + /// + /// The store a project of is checked out into: its home's (made with its manifest, and for a server + /// its , when it is not there), or the parent's while it is not split. + /// + public WorkspaceStore StoreFor(BundleHome home) + { + ArgumentNullException.ThrowIfNull(home); + if (!IsSplit) return _legacy!; + var root = RootFor(home); + var store = Open(root); + if (home.Kind == BundleHome.ServerKind && home.Server is { Length: > 0 } server && ServerHomeRecord.Read(root) is null) + ServerHomeRecord.Write(root, ServerHomeRecord.Of(Identity.Canonical(server), null, ServerIdentity.IsIdKey(server) ? null : server, null, DateTimeOffset.UtcNow)); + return store; + } + + /// The store of this PC's home (the parent's while it is not split). + public WorkspaceStore Local => StoreFor(new BundleHome(BundleHome.ArchiveKind, Guid.Empty)); + + /// + /// Records what is known of server in its home's (made with the + /// home when it is not there; its first sighting kept); nothing while the parent is not split. + /// + public ServerHomeRecord? Describe(string serverKey, string? name, string? address, string? tlsFingerprint, DateTimeOffset now) + { + ArgumentException.ThrowIfNullOrWhiteSpace(serverKey); + if (!IsSplit) return null; + var root = ServerRoot(serverKey); + Open(root); + var key = Identity.Canonical(serverKey); + var known = ServerHomeRecord.Read(root); + var record = ServerHomeRecord.Of(key, name ?? known?.Name, address ?? known?.Address, tlsFingerprint ?? known?.TlsFingerprint, now) with + { + FirstSeen = known?.FirstSeen ?? now, + }; + ServerHomeRecord.Write(root, record); + return record; + } + + /// + /// Every home that has a folder (this PC's first, then the servers' by folder name), and the parent's projects not moved + /// into their home yet; while the parent is not split, the parent's store (and any home a sorting stopped half-way made). + /// + public IReadOnlyList Homes() + { + var homes = new List(); + if (_legacy is not null) homes.Add(new WorkspaceHome(string.Empty, _legacy, null, null)); + if (Directory.Exists(LocalRoot) && HasManifest(LocalRoot)) homes.Add(new WorkspaceHome(HomeFolders.Local, Open(LocalRoot), null, null)); + var servers = Path.Combine(Root, HomeFolders.Servers); + if (Directory.Exists(servers)) + { + foreach (var folder in Directory.EnumerateDirectories(servers).Order(StringComparer.Ordinal)) + { + var name = Path.GetFileName(folder); + if (!HomeFolders.IsServerFolder(name) || !HasManifest(folder)) continue; + var record = ServerHomeRecord.Read(folder); + var key = record?.Key ?? (HomeFolders.IdOfFolder(name) is { } id ? ServerIdentity.IdKey(id) : null); + homes.Add(new WorkspaceHome(name, Open(folder), key, record)); + } + } + return homes; + } + + /// The stores of every home that has a folder (). + public IReadOnlyList All() => [.. Homes().Select(h => h.Store)]; + + /// The projects of every home. + public IReadOnlyList List() => [.. All().SelectMany(s => s.List())]; + + /// The working states of every home's projects (one unreadable is left out, and logged). + public IReadOnlyList<(WorkspaceProject Project, ProjectBundle Bundle)> ListBundles() => [.. All().SelectMany(s => s.ListBundles())]; + + /// The project's folder in whichever home holds it; null when none does. + public WorkspaceProject? Find(Guid projectId) + { + foreach (var store in All()) + { + if (store.Find(projectId) is { } project) return project; + } + return null; + } + + /// The trash of every home (), each listed with its own fingerprint. + /// A trash cannot be read. + public IReadOnlyList Trash(CancellationToken cancellationToken = default) => + [.. All().Select(s => WorkspaceTrash.List(s.Root, cancellationToken))]; + + /// The folder at is the parent of a workspace split into homes (its manifest says so). + public static bool IsSplitRoot(string root) + { + ArgumentException.ThrowIfNullOrWhiteSpace(root); + var path = Path.Combine(WorkspaceStore.FullRoot(root), WorkspaceStore.ManifestFile); + return File.Exists(path) && WorkspaceStore.TryRead(path) is { Layout: HomesLayout }; + } + + /// The folder holds a folder named by a project id with a working state (the 1.0 layout). + internal static bool HoldsProjects(string root) => + Directory.Exists(root) && Directory.EnumerateDirectories(root).Any(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _) + && File.Exists(Path.Combine(d, WorkspaceProject.BundleFile))); + + /// The manifest of a split parent, made now; another program's made at the same moment wins. + internal static WorkspaceManifest CreateSplit(string path) + { + var manifest = new WorkspaceManifest(WorkspaceStore.FormatName, ParentFormatVersion, Guid.CreateVersion7(), DateTimeOffset.UtcNow, HomesLayout); + var temp = AtomicFile.TempPathFor(path); + try + { + File.WriteAllText(temp, JsonSerializer.Serialize(manifest, WorkspaceJson.Default.WorkspaceManifest)); + try + { + File.Move(temp, path, overwrite: false); + } + catch (IOException) when (File.Exists(path)) + { + if (WorkspaceStore.TryRead(path) is { Layout: HomesLayout } first) return first; + throw new InvalidDataException($"{path} was made by another program meanwhile and is not a workspace of homes."); + } + } + finally + { + if (File.Exists(temp)) File.Delete(temp); + } + return manifest; + } + + private static bool HasManifest(string folder) => File.Exists(Path.Combine(folder, WorkspaceStore.ManifestFile)); + + private WorkspaceStore Open(string root) + { + lock (_gate) + { + if (_stores.TryGetValue(root, out var store)) return store; + store = new WorkspaceStore(root); + _stores[root] = store; + return store; + } + } +} diff --git a/src/Chronos.Data/Workspaces/WorkspaceJobs.cs b/src/Chronos.Data/Workspaces/WorkspaceJobs.cs index 9f19fb31..6f55e2dd 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceJobs.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceJobs.cs @@ -1,5 +1,6 @@ using System.Text.Json; using Chronos.Core.Diagnostics; +using Chronos.Core.IO; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; @@ -30,10 +31,21 @@ namespace Chronos.Data.Workspaces; /// /// The workspace folder in use now (the desktop's Settings › Workspace): a copy, render or send queued before the workspace /// moved finds its project there when the folder its parameters name no longer holds it; null where only that one counts. +/// A workspace split into homes (owner review 5, ) is searched in every home, so a job queued +/// before its folders were sorted, or before its server's home was re-keyed, finds its project in the home it went to. +/// +/// +/// Whether two server keys name one server ( over the desktop's alias table, owner review +/// 5): a job queued under an address key the server was reached under works on a project whose home names its id key. Null +/// compares the keys ignoring case (). /// public sealed record WorkspaceJobOptions(ProcessingOptions Processing, Func HoldTransfers, Func? Moved = null, - Func? CurrentRoot = null) + Func? CurrentRoot = null, Func? SameServer = null) { + /// Both keys name one server (, else equal ignoring case; null equals only null). + public bool IsSameServer(string? a, string? b) => + SameServer is { } same ? same(a, b) : string.Equals(a, b, StringComparison.OrdinalIgnoreCase); + /// The options of an engine without a processing service of its own: the archive engine's decoders, nothing held, no move followed. public static WorkspaceJobOptions For(ArchiveService archives) { @@ -204,10 +216,16 @@ internal static class WorkspaceJobRoots /// resource key) when no folder holds the project. /// /// The workspace is being moved, or no folder holds the project. + /// + /// A folder that is a home of a workspace split into homes (owner review 5: <parent>\local, + /// <parent>\servers\<folder>) is moved with its parent, whose journal counts for it; and a split parent is + /// searched in each of its homes (and in itself, for a project not moved into its home yet), so a job queued with the 1.0 + /// folder, or with a server's home before it was re-keyed, finds the project in the home it went to. + /// public static string Resolve(string named, Guid projectId, Func? current, string missing) { ArgumentException.ThrowIfNullOrWhiteSpace(named); - var candidates = new List(); + var folders = new List(); string? currentRoot = null; try { @@ -220,23 +238,60 @@ internal static class WorkspaceJobRoots foreach (var root in new[] { WorkspaceStore.FullRoot(named), currentRoot }) { if (root is null) continue; - if (WorkspaceMover.HasJournal(root)) + foreach (var folder in new[] { root, SplitParentOf(root) }) { - // Past its switch the move's target is the workspace, and what is left here is a source still to clear. - if (WorkspaceMover.ReadJournal(root) is not { Stage: WorkspaceMoveStage.Switched } journal) throw new InvalidOperationException(Loc.L("Errors.Workspace.Moving")); - candidates.Add(WorkspaceStore.FullRoot(journal.To)); - continue; + if (folder is null) continue; + if (WorkspaceMover.HasJournal(folder)) + { + // Past its switch the move's target is the workspace, and what is left here is a source still to clear. + if (WorkspaceMover.ReadJournal(folder) is not { Stage: WorkspaceMoveStage.Switched } journal) + throw new InvalidOperationException(Loc.L("Errors.Workspace.Moving")); + folders.Add(WorkspaceStore.FullRoot(journal.To)); + continue; + } + folders.Add(folder); } - candidates.Add(root); + } + var candidates = new List(); + foreach (var folder in folders.Distinct(StringComparer.OrdinalIgnoreCase)) + { + candidates.Add(folder); + if (WorkspaceHomes.IsSplitRoot(folder)) candidates.AddRange(HomesOf(folder)); } foreach (var root in candidates.Distinct(StringComparer.OrdinalIgnoreCase)) { if (!File.Exists(Path.Combine(root, projectId.ToString("N"), WorkspaceProject.BundleFile))) continue; - if (WorkspaceMover.HasJournal(root)) throw new InvalidOperationException(Loc.L("Errors.Workspace.Moving")); + if (WorkspaceMover.HasJournal(root) || (SplitParentOf(root) is { } parent && WorkspaceMover.HasJournal(parent))) + throw new InvalidOperationException(Loc.L("Errors.Workspace.Moving")); if (!string.Equals(root, WorkspaceStore.FullRoot(named), StringComparison.OrdinalIgnoreCase)) Log.Info("workspace", $"Project {projectId} is no longer in {named}: the job works in {root}, where the workspace is now"); return root; } throw new InvalidOperationException(Loc.L(missing)); } + + /// The split parent is a home of (local, or a folder in servers); null for any other folder. + internal static string? SplitParentOf(string root) + { + var name = Path.GetFileName(root); + var up = Path.GetDirectoryName(root); + if (up is null) return null; + string? parent = name == HomeFolders.Local ? up + : HomeFolders.IsServerFolder(name) && Path.GetFileName(up) == HomeFolders.Servers ? Path.GetDirectoryName(up) + : null; + return parent is not null && WorkspaceHomes.IsSplitRoot(parent) ? WorkspaceStore.FullRoot(parent) : null; + } + + /// The homes of the split parent that have a folder: this PC's, then the servers'. + private static IEnumerable HomesOf(string parent) + { + var local = HomeFolders.LocalPath(parent); + if (Directory.Exists(local)) yield return local; + var servers = Path.Combine(parent, HomeFolders.Servers); + if (!Directory.Exists(servers)) yield break; + foreach (var folder in Directory.EnumerateDirectories(servers).Order(StringComparer.Ordinal)) + { + if (HomeFolders.IsServerFolder(Path.GetFileName(folder))) yield return folder; + } + } } diff --git a/src/Chronos.Data/Workspaces/WorkspaceMover.cs b/src/Chronos.Data/Workspaces/WorkspaceMover.cs index 53317149..5955e7ce 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceMover.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceMover.cs @@ -79,8 +79,9 @@ internal sealed partial class WorkspaceMoveJson : JsonSerializerContext; /// /// Moves a workspace folder with its projects (phase 5c, ). What moves: every -/// folder named by a project id, then workspace.json, then .trash, in that order; anything else in the folder -/// stays where it is. On one volume each entry is renamed into the target (); +/// folder named by a project id, then the homes' folders of a workspace split into homes (local, servers, +/// owner review 5), then workspace.json, then .trash, in that order; anything else in the folder stays where it +/// is. On one volume each entry is renamed into the target (); /// across volumes each file is copied into a .part beside its place, hashed as it is read and read back to hash the /// copy, and only once both SHA-256 agree renamed into place (with the source's time), so a file at its final name in the /// target is always one verified. The source is the workspace until every entry is at the target: then the desktop is told @@ -185,6 +186,21 @@ public sealed class WorkspaceMover entries.Add(name); (files, bytes) = (files + count, bytes + size); } + // A workspace split into homes (owner review 5): each home's folder moves whole, its projects counted one by one. + foreach (var home in HomeEntries) + { + var folder = Path.Combine(From, home); + if (!Directory.Exists(folder)) continue; + var (count, size) = Read(folder, home, fingerprint); + foreach (var project in ProjectFolders(folder)) + { + var listed = Listed(project); + projects.Add(new WorkspaceMoveFolder(Guid.ParseExact(Path.GetFileName(project), "N"), File.Exists(Path.Combine(project, WorkspaceProject.BundleFile)), + listed.Count, listed.Sum(f => new FileInfo(Path.Combine(project, f)).Length))); + } + entries.Add(home); + (files, bytes) = (files + count, bytes + size); + } var manifest = Path.Combine(From, WorkspaceStore.ManifestFile); if (File.Exists(manifest)) { @@ -303,7 +319,7 @@ public sealed class WorkspaceMover if (switched is not null) await switched(From, To).ConfigureAwait(false); Cut?.Invoke("switched", To); var left = Clean(journal); - log?.Invoke(Loc.L("Archive.Log.WorkspaceMoved", To, journal.Entries.Count(e => Guid.TryParseExact(e, "N", out _)), survey.Files)); + log?.Invoke(Loc.L("Archive.Log.WorkspaceMoved", To, MovedProjects(journal), survey.Files)); if (left > 0) log?.Invoke(Loc.L("Archive.Log.WorkspaceMoveLeft", From, left)); Log.Info("workspace", $"The workspace moved from {From} to {To}" + (left > 0 ? $"; {left} entr(ies) of the old folder stay there" : string.Empty)); return (survey, resumed); @@ -383,7 +399,7 @@ public sealed class WorkspaceMover Cut?.Invoke("rename", source); Rename(source, target); Cut?.Invoke("moved", target); - if (Guid.TryParseExact(entry, "N", out _)) log?.Invoke(Loc.L("Archive.Log.WorkspaceMoveProject", Name(target))); + LogProjects(entry, target, log); } else if (!Exists(target)) { @@ -435,7 +451,7 @@ public sealed class WorkspaceMover if (durable) clock.Restart(); if (progress is not null) await progress(done, total, durable).ConfigureAwait(false); } - if (Guid.TryParseExact(entry, "N", out _)) log?.Invoke(Loc.L("Archive.Log.WorkspaceMoveProject", Name(source))); + LogProjects(entry, source, log); } journal = journal with { Verified = [.. journal.Verified, entry] }; WriteJournal(journal); @@ -600,6 +616,40 @@ public sealed class WorkspaceMover private const string TrashFolder = ".trash"; + /// The homes' folders of a workspace split into homes, which move whole after the 1.0 project folders and before the manifest. + private static readonly string[] HomeEntries = [HomeFolders.Local, HomeFolders.Servers]; + + /// The project folders in a home's folder: local's own, or those of every server home in servers. + private static IEnumerable ProjectFolders(string home) + { + var homes = Path.GetFileName(home) == HomeFolders.Servers + ? Directory.EnumerateDirectories(home).Where(d => HomeFolders.IsServerFolder(Path.GetFileName(d))).Order(StringComparer.Ordinal) + : Enumerable.Repeat(home, 1); + foreach (var store in homes) + { + foreach (var folder in Directory.EnumerateDirectories(store).Order(StringComparer.Ordinal)) + { + if (Guid.TryParseExact(Path.GetFileName(folder), "N", out _)) yield return folder; + } + } + } + + /// The projects a move took: its 1.0 project folders, and the project folders of the homes it moved (now at the target). + private int MovedProjects(WorkspaceMoveJournal journal) => + journal.Entries.Count(e => Guid.TryParseExact(e, "N", out _)) + + journal.Entries.Where(e => HomeEntries.Contains(e) && Directory.Exists(Path.Combine(To, e))).Sum(e => ProjectFolders(Path.Combine(To, e)).Count()); + + /// The log line of each project an entry moved: the entry's own, or those of a home's folder. + private static void LogProjects(string entry, string folder, Action? log) + { + if (log is null) return; + if (Guid.TryParseExact(entry, "N", out _)) log(Loc.L("Archive.Log.WorkspaceMoveProject", Name(folder))); + else if (HomeEntries.Contains(entry) && Directory.Exists(folder)) + { + foreach (var project in ProjectFolders(folder)) log(Loc.L("Archive.Log.WorkspaceMoveProject", Name(project))); + } + } + private static readonly TimeSpan CheckpointEvery = TimeSpan.FromSeconds(5); private static StringComparison PathComparison => OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; diff --git a/src/Chronos.Data/Workspaces/WorkspaceStore.cs b/src/Chronos.Data/Workspaces/WorkspaceStore.cs index ce74bc6f..cc2477f6 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceStore.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceStore.cs @@ -11,7 +11,12 @@ using Chronos.Data.Sync; namespace Chronos.Data.Workspaces; /// The manifest of a workspace folder (workspace.json). -public sealed record WorkspaceManifest(string Format, int FormatVersion, Guid Id, DateTimeOffset Created); +/// +/// in the manifest of format of a +/// workspace split into homes (owner review 5, #66): the folder holds no projects of its own but one store per home; null +/// for a store. +/// +public sealed record WorkspaceManifest(string Format, int FormatVersion, Guid Id, DateTimeOffset Created, string? Layout = null); /// /// A workspace's sync state of one project besides its versions (sync.json): a restore to record, a conflict to @@ -133,13 +138,15 @@ public sealed record WorkspaceMoveParameters(string From, string To, string Plan [JsonSerializable(typeof(WorkspaceSendParameters))] [JsonSerializable(typeof(WorkspaceMoveParameters))] [JsonSerializable(typeof(WorkspaceRenders))] +[JsonSerializable(typeof(ServerHomeRecord))] internal sealed partial class WorkspaceJson : JsonSerializerContext; /// /// The workspace of this PC (owner review 2, R8): a folder on a fast disk where projects are edited, never inside the /// library. <root>/workspace.json names it; each project has a folder named by its id /// (). There is no index: the store lists the folders holding a project.chronos. The -/// desktop puts it in %LOCALAPPDATA%\Chronos\workspace unless set otherwise; the store takes any folder. +/// store takes any folder; the desktop's workspace (%LOCALAPPDATA%\Chronos\workspace unless set otherwise) is split +/// into one store per home since owner review 5 (: local, servers\<id>). /// public sealed class WorkspaceStore { @@ -168,6 +175,17 @@ public sealed class WorkspaceStore Manifest = Read(path); } + /// + /// The projects a workspace split into homes still holds where 1.0 kept them (<parent>/<project id>: one in + /// use when the folders were sorted, owner review 5): a view of the parent with its manifest, which makes and writes nothing + /// (). + /// + internal WorkspaceStore(string root, WorkspaceManifest manifest) + { + Root = FullRoot(root); + Manifest = manifest; + } + public string Root { get; } public WorkspaceManifest Manifest { get; } @@ -319,6 +337,25 @@ public sealed class WorkspaceStore } } + /// The manifest of the folder names, of any format (a store's or a parent's of homes); null when it is not one. + internal static WorkspaceManifest? TryRead(string path) + { + try + { + return JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), WorkspaceJson.Default.WorkspaceManifest) is { Format: FormatName } manifest && manifest.Id != Guid.Empty + ? manifest + : null; + } + catch (Exception ex) when (ex is JsonException or IOException or UnauthorizedAccessException) + { + return null; + } + } + + /// Writes whole to a temporary file and moves it into place. + internal static void Write(string path, WorkspaceManifest manifest) => + AtomicFile.WriteAllText(path, JsonSerializer.Serialize(manifest, WorkspaceJson.Default.WorkspaceManifest)); + private static WorkspaceManifest Read(string path) { WorkspaceManifest? manifest; @@ -331,6 +368,8 @@ public sealed class WorkspaceStore 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.Layout == WorkspaceHomes.HomesLayout) + throw new InvalidDataException($"{path} is the manifest of a workspace split into homes, whose projects are in its home folders ({HomeFolders.Local}, {HomeFolders.Servers})."); if (manifest.FormatVersion > FormatVersion) throw new InvalidDataException($"{path} uses format {manifest.FormatVersion}, newer than this Chronos ({FormatVersion})."); return manifest; diff --git a/src/Chronos.Protocol/Dtos.cs b/src/Chronos.Protocol/Dtos.cs index d94ef267..86d61b69 100644 --- a/src/Chronos.Protocol/Dtos.cs +++ b/src/Chronos.Protocol/Dtos.cs @@ -17,6 +17,11 @@ public sealed record ServerLimits(long MaxUploadBytes, int ChunkSizeBytes, int P /// The highest project settings format the server reads and renders (ProjectSettings.CurrentVersion, owner review 1, /// phase 4); null from an older server, which reads format 1 only. The desktop holds back a push or a render that needs more. /// +/// +/// The server's stable id (owner review 5, #66): made once, kept in its database, the same whatever address or name it is +/// reached by. A desktop keys the server's homes, links and folders by it. Null from an older server, which a desktop keys by +/// its address. +/// public sealed record ServerInfo( string Name, string Version, @@ -26,7 +31,8 @@ public sealed record ServerInfo( IReadOnlyList Formats, ServerLimits Limits, IReadOnlyList? Extensions = null, - int? SettingsFormat = null); + int? SettingsFormat = null, + Guid? ServerId = null); /// Names of that clients act on. public static class ServerCapabilities diff --git a/src/Chronos.Server/Api/SystemEndpoints.cs b/src/Chronos.Server/Api/SystemEndpoints.cs index 6ad2e623..7fcfd00e 100644 --- a/src/Chronos.Server/Api/SystemEndpoints.cs +++ b/src/Chronos.Server/Api/SystemEndpoints.cs @@ -49,7 +49,8 @@ public static class SystemEndpoints ["jpeg", "png", "tiff", "dng", "cr2", "cr3", "nef", "arw", "raf", "orf", "rw2", "pef", "heic", "webp"], new ServerLimits(options.MaxUploadBytes, options.ChunkSizeBytes, PasswordHasher.MinimumLength), PhotoExtensions, - ProjectSettings.CurrentVersion), ProtocolJson.Default.ServerInfo)).AllowAnonymousCaller(); + ProjectSettings.CurrentVersion, + state.ServerId), ProtocolJson.Default.ServerInfo)).AllowAnonymousCaller(); api.MapGet("/i18n/{culture}", (string culture) => { @@ -114,6 +115,12 @@ public sealed class ServerState { public string? TlsFingerprint { get; init; } + /// + /// The server's stable id (), kept in its database's settings + /// (); null only where a host built the state without a database. + /// + public Guid? ServerId { get; init; } + /// Engine and version of the database ("PostgreSQL 17.2", "SQLite 3.45.1"). public string Database { get; init; } = string.Empty; diff --git a/src/Chronos.Server/Hosting/ServerHost.cs b/src/Chronos.Server/Hosting/ServerHost.cs index 647e4175..2c83814f 100644 --- a/src/Chronos.Server/Hosting/ServerHost.cs +++ b/src/Chronos.Server/Hosting/ServerHost.cs @@ -58,6 +58,7 @@ public static class ServerHost { TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate), Database = ServerDatabase.DescribeAsync(database, CancellationToken.None).GetAwaiter().GetResult(), + ServerId = ServerIdAsync(database, bootLogger, CancellationToken.None).GetAwaiter().GetResult(), }; builder.WebHost.ConfigureKestrel(k => @@ -117,6 +118,41 @@ public static class ServerHost } } + /// The app setting that holds the server's stable id (). + public const string ServerIdKey = "server.id"; + + /// + /// The server's stable id (owner review 5, #66): read from its database's app settings (), or made + /// once (a version 7 UUID) and stored there at the first start. It lives in the database, so a restore keeps it and the + /// server keeps its id whatever address, port or name it is reached by. A value that does not read as an id is kept aside + /// in the log and replaced. + /// + public static async Task ServerIdAsync(ChronosDatabase database, ILogger? logger, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(database); + var json = await database.Settings.GetJsonAsync(Data.Repositories.SettingScope.App, null, ServerIdKey, cancellationToken).ConfigureAwait(false); + if (json is not null) + { + try + { + using var stored = System.Text.Json.JsonDocument.Parse(json); + if (stored.RootElement.ValueKind == System.Text.Json.JsonValueKind.String && Guid.TryParse(stored.RootElement.GetString(), out var known) + && known != Guid.Empty) + return known; + } + catch (System.Text.Json.JsonException) + { + // Replaced below; the old value is logged. + } + logger?.LogWarning("The server id stored in the database ({Value}) is not an id; a new one is made", json); + } + var id = Guid.CreateVersion7(); + await database.Settings.SetJsonAsync(Data.Repositories.SettingScope.App, null, ServerIdKey, "\"" + id.ToString("D") + "\"", cancellationToken) + .ConfigureAwait(false); + logger?.LogInformation("Server id {Id} made", id); + return id; + } + /// The single composition root of the server's services. public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive) { diff --git a/tests/Chronos.Core.Tests/Foundation/HomeFoldersTests.cs b/tests/Chronos.Core.Tests/Foundation/HomeFoldersTests.cs new file mode 100644 index 00000000..23145373 --- /dev/null +++ b/tests/Chronos.Core.Tests/Foundation/HomeFoldersTests.cs @@ -0,0 +1,90 @@ +using System.Security.Cryptography; +using System.Text; +using Chronos.Core.IO; +using Chronos.Testing; + +namespace Chronos.Core.Tests.Foundation; + +/// The folders of the homes (owner review 5, #66): this PC's, a server's by id, a server's by address hash. +public sealed class HomeFoldersTests(TestContext context) +{ + private static readonly Guid ServerId = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + + [Test] + public void AnIdKeyNamesItsFolderByTheId() + { + var key = HomeFolders.IdKey(ServerId); + Assert.Equal("id:0192a4c17b3e7cc08a512f9d0e6b1a01", key); + Assert.Equal("0192a4c17b3e7cc08a512f9d0e6b1a01", HomeFolders.ServerFolder(key)); + Assert.Equal(ServerId, HomeFolders.IdOfKey(key)); + Assert.Equal(ServerId, HomeFolders.IdOfKey("ID:0192A4C17B3E7CC08A512F9D0E6B1A01")); + Assert.Equal(ServerId, HomeFolders.IdOfFolder(HomeFolders.ServerFolder(key))); + Assert.Throws(() => HomeFolders.IdKey(Guid.Empty)); + } + + [Test] + public void AnAddressKeyNamesItsFolderAsTheProxyCacheOf10Did() + { + const string key = "https://nas.local:8443"; + // ProxyCache 1.0: Convert.ToHexStringLower(SHA256(UTF-8 key))[..16]. + var hash = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(key)))[..16]; + Assert.Equal(hash, HomeFolders.AddressHash(key)); + Assert.Equal("addr-" + hash, HomeFolders.ServerFolder(key)); + Assert.Null(HomeFolders.IdOfKey(key)); + Assert.True(HomeFolders.IsAddressFolder("addr-" + hash)); + Assert.True(HomeFolders.IsServerFolder("addr-" + hash)); + Assert.Null(HomeFolders.IdOfFolder("addr-" + hash)); + // "id:" with no id after it is an address key like any other. + Assert.Equal("addr-" + HomeFolders.AddressHash("id:nothing"), HomeFolders.ServerFolder("id:nothing")); + } + + [Test] + public void OnlyAHomeFolderNameIsAServerHome() + { + Assert.True(HomeFolders.IsServerFolder(ServerId.ToString("N"))); + foreach (var name in new[] { null, "", "local", "servers", ".trash", ServerId.ToString("N").ToUpperInvariant(), ServerId.ToString("D"), "addr-", "addr-0123", + "addr-0123456789ABCDEF", "addr-0123456789abcdefa", "xaddr-0123456789abcdef", Guid.Empty.ToString("N") }) + Assert.False(HomeFolders.IsServerFolder(name), name ?? "null"); + } + + [Test] + public void HomePathsLieUnderTheirRoot() + { + var root = context.CreateTempDirectory("homes"); + Assert.Equal(Path.Combine(root, "local"), HomeFolders.LocalPath(root)); + Assert.Equal(Path.Combine(root, "servers", ServerId.ToString("N")), HomeFolders.ServerPath(root, HomeFolders.IdKey(ServerId))); + Assert.Equal(Path.Combine(root, "servers", "addr-" + HomeFolders.AddressHash("https://x")), HomeFolders.ServerPath(root, "https://x")); + } + + [Test] + public void TheOriginalsOfAServerGetAnArchiveBesideThisPcs() + { + var parent = context.CreateTempDirectory("originals"); + var archive = Path.Combine(parent, "ChronosArchive"); + Directory.CreateDirectory(archive); + Assert.Equal(Path.Combine(parent, "ChronosArchive - Studio NAS"), HomeFolders.OriginalsArchiveRoot(archive, "Studio NAS", Path.Combine(parent, "fallback"))); + + // Taken: " (2)"; a name Windows refuses is made safe. + Directory.CreateDirectory(Path.Combine(parent, "ChronosArchive - Studio NAS")); + Assert.Equal(Path.Combine(parent, "ChronosArchive - Studio NAS (2)"), HomeFolders.OriginalsArchiveRoot(archive + Path.DirectorySeparatorChar, "Studio NAS", "unused")); + Assert.Equal(Path.Combine(parent, "ChronosArchive - A B C"), HomeFolders.OriginalsArchiveRoot(archive, "A:B/C", "unused")); + Assert.Equal(Path.Combine(parent, "ChronosArchive - Taken (3)"), + HomeFolders.OriginalsArchiveRoot(archive, "Taken", "unused", path => !path.EndsWith("(3)", StringComparison.Ordinal))); + + // No archive of this PC's: beside where its first one would go. + var fallback = Path.Combine(parent, "Pictures", "ChronosArchive"); + Assert.Equal(Path.Combine(parent, "Pictures", "ChronosArchive - NAS"), HomeFolders.OriginalsArchiveRoot(null, "NAS", fallback)); + } + + [Test] + public void ASafeNameIsAFolderNameEveryDriveTakes() + { + Assert.Equal("Studio NAS", HomeFolders.SafeName(" Studio \t NAS. ")); + Assert.Equal("a b", HomeFolders.SafeName("a<>:\"|?*b")); + Assert.Equal("CON_", HomeFolders.SafeName("con".ToUpperInvariant())); + Assert.Equal("nul.box_", HomeFolders.SafeName("nul.box")); + Assert.Equal("Server", HomeFolders.SafeName("...")); + Assert.Equal("Server", HomeFolders.SafeName(null)); + Assert.Equal(HomeFolders.MaxNameLength, HomeFolders.SafeName(new string('x', 200)).Length); + } +} diff --git a/tests/Chronos.Data.Tests/Homes/HomeFixtures.cs b/tests/Chronos.Data.Tests/Homes/HomeFixtures.cs new file mode 100644 index 00000000..648a4344 --- /dev/null +++ b/tests/Chronos.Data.Tests/Homes/HomeFixtures.cs @@ -0,0 +1,122 @@ +using System.Security.Cryptography; +using System.Text; +using Chronos.Core.Archiving; +using Chronos.Core.Project; +using Chronos.Data.Workspaces; + +namespace Chronos.Data.Tests.Homes; + +/// Workspaces and caches laid out as 1.0 left them, for the sorting into homes (owner review 5, #66). +internal static class HomeFixtures +{ + public const string Nas = "https://nas.local:8443"; + public const string NasByIp = "https://192.168.1.5:8443"; + public const string Other = "https://other.example:8443"; + + /// A working state of written into <root>/<id> with some media, versions and a render. + public static Guid Project(string root, BundleHome home, string name = "Main", DateTimeOffset? saved = null, Guid? id = null, int proxies = 3) + { + var projectId = id ?? Guid.CreateVersion7(); + var folder = Path.Combine(root, projectId.ToString("N")); + Directory.CreateDirectory(folder); + var archive = home.IsArchive + ? new BundleArchive(home.ArchiveId ?? Guid.CreateVersion7(), "This PC", @"D:\ChronosArchive") + : new BundleArchive(Data.Projects.ServerProjectHome.ArchiveIdOf(home.Server!), "Studio NAS", null); + var bundle = new ProjectBundle(archive, new BundleSequence(home.SequenceId, "2026/2026-11-15 Harbour", "Harbour", proxies), new ProjectSettings(), + saved ?? DateTimeOffset.UtcNow) + { + Workspace = new BundleWorkspace(projectId, name, true, home, null, new BundleMedia(BundleMedia.Proxies, 1)), + }; + bundle.Save(Path.Combine(folder, WorkspaceProject.BundleFile)); + var media = Path.Combine(folder, WorkspaceProject.MediaFolder, WorkspaceProject.ProxiesFolder); + Directory.CreateDirectory(media); + for (var i = 0; i < proxies; i++) File.WriteAllBytes(Path.Combine(media, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(projectId, i, 500 + i)); + Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.VersionsFolder)); + File.WriteAllText(Path.Combine(folder, WorkspaceProject.VersionsFolder, "1-" + Guid.CreateVersion7().ToString("N") + ".json"), "{}"); + Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.ExportsFolder)); + File.WriteAllBytes(Path.Combine(folder, WorkspaceProject.ExportsFolder, "render.mp4"), Bytes(projectId, 99, 2048)); + return projectId; + } + + public static BundleHome Local() => new(BundleHome.ArchiveKind, Guid.CreateVersion7(), Guid.CreateVersion7()); + + public static BundleHome Server(string key) => new(BundleHome.ServerKind, Guid.CreateVersion7(), null, key); + + /// A 1.0 workspace: its manifest (format 1) and the folder. + public static string LegacyWorkspace(string parent) + { + var root = Path.Combine(parent, "workspace"); + _ = new WorkspaceStore(root); + return root; + } + + /// A trash entry of holding a working state, as a removal leaves it. + public static string TrashEntry(string root, Guid projectId, BundleHome home) + { + var trash = Path.Combine(root, WorkspaceTrash.FolderName); + var staging = Path.Combine(trash, "staging"); + Directory.CreateDirectory(staging); + Project(staging, home, id: projectId, proxies: 1); + var entry = Path.Combine(trash, projectId.ToString("N") + "-638000000000000000"); + Directory.Move(Path.Combine(staging, projectId.ToString("N")), entry); + Directory.Delete(staging); + return entry; + } + + /// The 1.0 proxy cache of 's sequence . + public static string LegacyProxies(string cache, string key, Guid remoteId, int frames = 2) + { + var folder = Data.Sync.ProxyCache.LegacyFolder(Path.Combine(cache, "proxies"), key, remoteId); + Directory.CreateDirectory(folder); + File.WriteAllText(Path.Combine(folder, "remote.json"), "{}"); + for (var i = 0; i < frames; i++) File.WriteAllBytes(Path.Combine(folder, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(remoteId, i, 300)); + return folder; + } + + /// A live lease on a project folder, as another Chronos working on the same workspace holds it. + public static LeaseFile Hold(string projectFolder) => + LeaseFile.TryAcquire(projectFolder, ".lock", "workspace@other", TimeSpan.FromMinutes(5)) ?? throw new InvalidOperationException("The lease is held."); + + /// + /// The SHA-256 of every file under by its name relative to the project or entry it belongs to, + /// whatever home folder holds it: the same before and after a sorting that only renames (the test-only proof). + /// + public static SortedDictionary Manifest(string root) + { + var manifest = new SortedDictionary(StringComparer.Ordinal); + if (!Directory.Exists(root)) return manifest; + foreach (var file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories)) + { + var relative = Path.GetRelativePath(root, file).Replace('\\', '/'); + var name = Path.GetFileName(file); + if (name is WorkspaceStore.ManifestFile or "server.json" && !relative.Contains('/', StringComparison.Ordinal)) continue; + if (name is WorkspaceStore.ManifestFile or "server.json" && IsHomeLevel(relative)) continue; + manifest[Strip(relative)] = Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(file))) + "|" + new FileInfo(file).Length; + } + return manifest; + } + + /// A path without the home folders it lies in (local/, servers/<folder>/). + private static string Strip(string relative) + { + if (relative.StartsWith("local/", StringComparison.Ordinal)) return relative["local/".Length..]; + if (relative.StartsWith("servers/", StringComparison.Ordinal)) + { + var rest = relative["servers/".Length..]; + var slash = rest.IndexOf('/', StringComparison.Ordinal); + return slash < 0 ? rest : rest[(slash + 1)..]; + } + return relative; + } + + private static bool IsHomeLevel(string relative) => + relative.Split('/') is { Length: 2 } parts && parts[0] == "local" || relative.Split('/') is { Length: 3 } more && more[0] == "servers"; + + private static byte[] Bytes(Guid seed, int index, int length) + { + var bytes = new byte[length]; + var hash = SHA256.HashData(Encoding.UTF8.GetBytes(seed.ToString("N") + index)); + for (var i = 0; i < length; i++) bytes[i] = hash[i % hash.Length]; + return bytes; + } +} diff --git a/tests/Chronos.Data.Tests/Homes/HomeLayoutMigrationTests.cs b/tests/Chronos.Data.Tests/Homes/HomeLayoutMigrationTests.cs new file mode 100644 index 00000000..ad5404bc --- /dev/null +++ b/tests/Chronos.Data.Tests/Homes/HomeLayoutMigrationTests.cs @@ -0,0 +1,297 @@ +using Chronos.Core.IO; +using Chronos.Core.Project; +using Chronos.Data.Homes; +using Chronos.Data.Sync; +using Chronos.Data.Workspaces; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Homes; + +/// The one-time sorting of a 1.0 workspace and cache into homes (owner review 5, #66). +[Category("homes")] +public sealed class HomeLayoutMigrationTests(TestContext context) +{ + private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + + /// A 1.0 data folder: a workspace with local, server and damaged projects and a trash, and a cache with proxies, render-node files and a staged import. + private sealed class Legacy + { + public required string Data { get; init; } + public required string Workspace { get; init; } + public required string Cache { get; init; } + public required Guid Local { get; init; } + public required Guid OnNas { get; init; } + public required Guid OnOther { get; init; } + public required Guid Damaged { get; init; } + public required Guid NasRemote { get; init; } + + public HomeLayoutMigration Migration(ServerIdentity? identity = null, Action? cut = null) => + new(new HomeLayoutOptions + { + DataDirectory = Data, + WorkspaceRoot = Workspace, + CacheRoot = Cache, + Identity = identity ?? ServerIdentity.None, + CurrentServer = HomeFixtures.Nas, + }) + { Cut = cut }; + } + + private Legacy Make() + { + var data = context.CreateTempDirectory("data"); + var workspace = HomeFixtures.LegacyWorkspace(data); + var local = HomeFixtures.Project(workspace, HomeFixtures.Local()); + var onNas = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Nas)); + var onOther = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Other)); + var damaged = Guid.CreateVersion7(); + Directory.CreateDirectory(Path.Combine(workspace, damaged.ToString("N"))); + File.WriteAllText(Path.Combine(workspace, damaged.ToString("N"), WorkspaceProject.BundleFile), "{ not json"); + HomeFixtures.TrashEntry(workspace, onNas, HomeFixtures.Server(HomeFixtures.Nas)); + File.WriteAllText(Path.Combine(workspace, WorkspaceTrash.FolderName, "loose.txt"), "kept"); + var cache = Path.Combine(data, "cache"); + var nasRemote = Guid.CreateVersion7(); + HomeFixtures.LegacyProxies(cache, HomeFixtures.Nas, nasRemote); + HomeFixtures.LegacyProxies(cache, HomeFixtures.Other, Guid.CreateVersion7()); + Directory.CreateDirectory(Path.Combine(cache, "render-node", "job1")); + File.WriteAllText(Path.Combine(cache, "render-node", "job1", "frame.jpg"), "rn"); + Directory.CreateDirectory(Path.Combine(cache, "server-import")); + File.WriteAllText(Path.Combine(cache, "server-import", "card.part"), "import"); + return new Legacy + { + Data = data, + Workspace = workspace, + Cache = cache, + Local = local, + OnNas = onNas, + OnOther = onOther, + Damaged = damaged, + NasRemote = nasRemote, + }; + } + + /// Every file and folder under with its size and time: what a dry run must leave as it was. + private static List Snapshot(string root) => + [.. new DirectoryInfo(root).EnumerateFileSystemInfos("*", SearchOption.AllDirectories) + .Select(e => Path.GetRelativePath(root, e.FullName) + "|" + (e is FileInfo f ? f.Length + "|" + f.LastWriteTimeUtc.Ticks : "dir")) + .Order(StringComparer.Ordinal)]; + + private static string Home(string workspace, string key) => Path.Combine(workspace, "servers", HomeFolders.ServerFolder(key)); + + private static void AssertSorted(Legacy legacy, string nasFolder) + { + var ws = legacy.Workspace; + Assert.True(WorkspaceHomes.IsSplitRoot(ws)); + Assert.FileExists(Path.Combine(ws, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile)); + Assert.FileExists(Path.Combine(ws, "local", legacy.Damaged.ToString("N"), WorkspaceProject.BundleFile)); + Assert.FileExists(Path.Combine(ws, "servers", nasFolder, legacy.OnNas.ToString("N"), WorkspaceProject.BundleFile)); + Assert.FileExists(Path.Combine(Home(ws, HomeFixtures.Other), legacy.OnOther.ToString("N"), WorkspaceProject.BundleFile)); + Assert.FileExists(Path.Combine(ws, "local", WorkspaceTrash.FolderName, "loose.txt")); + Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(ws, "servers", nasFolder, WorkspaceTrash.FolderName))); + Assert.False(Directory.EnumerateDirectories(ws).Any(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)), "no project is left in the parent"); + Assert.False(Directory.Exists(Path.Combine(ws, WorkspaceTrash.FolderName)), "the parent's emptied trash went"); + Assert.Equal("Studio NAS", Assert.NotNull(ServerHomeRecord.Read(Path.Combine(ws, "servers", nasFolder))).Name); + Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "proxies", legacy.NasRemote.ToString("N"), "remote.json")); + Assert.True(Directory.Exists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Other), "proxies"))); + Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "render-node", "job1", "frame.jpg")); + Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "import", "card.part")); + Assert.False(Directory.Exists(Path.Combine(legacy.Cache, "render-node"))); + Assert.FileMissing(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile)); + var homes = new WorkspaceHomes(ws); + Assert.True(homes.IsSplit); + Assert.Equal(4, homes.List().Count); + } + + [Test] + public async Task ThePlanWritesNothing() + { + var legacy = Make(); + var before = Snapshot(legacy.Data); + var plan = await legacy.Migration().PlanAsync(context.Cancellation); + Assert.SequenceEqual(before, Snapshot(legacy.Data)); + Assert.True(plan.SplitsWorkspace); + Assert.Null(plan.Deferred); + Assert.Equal(4, plan.Projects); + Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Trash)); + Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Proxies)); + Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.RenderNode)); + Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.Import)); + var damaged = plan.Entries.Single(e => e.Source.EndsWith(legacy.Damaged.ToString("N"), StringComparison.Ordinal)); + Assert.Equal(Path.Combine(legacy.Workspace, "local", legacy.Damaged.ToString("N")), damaged.Target); + Assert.Equal(plan.Fingerprint, (await legacy.Migration().PlanAsync(context.Cancellation)).Fingerprint); + } + + [Test] + public async Task SortingMovesEveryHomeAndRenamesOnly() + { + var legacy = Make(); + var before = HomeFixtures.Manifest(legacy.Workspace); + var cacheBefore = HomeFixtures.Manifest(legacy.Cache); + var result = await legacy.Migration().ApplyAsync(context.Cancellation); + Assert.Equal((4, 0, false, true), (result.ProjectsMoved, result.InUse, result.Resumed, result.Split)); + AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas)); + // Every file is there with the same name relative to its project and the same bytes: the sorting only renamed. + Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace)); + Assert.SequenceEqual(cacheBefore.Values.Order(), HomeFixtures.Manifest(legacy.Cache).Values.Order()); + + // A second run does nothing. + var again = await legacy.Migration().ApplyAsync(context.Cancellation); + Assert.Equal(HomeLayoutResult.Nothing, again); + Assert.True((await legacy.Migration().PlanAsync(context.Cancellation)).IsEmpty); + } + + [Test] + public async Task AServerKnownByIdGetsItsIdHome() + { + var legacy = Make(); + await legacy.Migration(new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])])).ApplyAsync(context.Cancellation); + AssertSorted(legacy, Id.ToString("N")); + Assert.Equal(ServerIdentity.IdKey(Id), Assert.NotNull(ServerHomeRecord.Read(Path.Combine(legacy.Workspace, "servers", Id.ToString("N")))).Key); + } + + [Test] + public async Task ACrashAtAnyStepIsTakenUpToTheSameEnd() + { + var entries = (await Make().Migration().PlanAsync(context.Cancellation)).Entries.Count; + var cuts = new List<(string Step, int Occurrence)> { ("journal", 1), ("commit", 1) }; + for (var i = 1; i <= entries; i++) cuts.AddRange([("rename", i), ("renamed", i)]); + foreach (var (step, occurrence) in cuts) + { + var legacy = Make(); + var before = HomeFixtures.Manifest(legacy.Workspace); + var seen = 0; + var crashing = legacy.Migration(cut: (name, _) => + { + if (name == step && ++seen == occurrence) throw new IOException("cut at " + step); + }); + await Assert.ThrowsAsync(() => crashing.ApplyAsync(context.Cancellation)); + Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile)); + var resumed = await legacy.Migration().ApplyAsync(context.Cancellation); + Assert.True(resumed.Resumed, $"{step} #{occurrence}"); + AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas)); + Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace), $"{step} #{occurrence}"); + Assert.Equal(HomeLayoutResult.Nothing, await legacy.Migration().ApplyAsync(context.Cancellation)); + } + } + + [Test] + public async Task AProjectInUseIsLeftAndMovedAtALaterStart() + { + var legacy = Make(); + var held = HomeFixtures.Hold(Path.Combine(legacy.Workspace, legacy.Local.ToString("N"))); + HomeLayoutResult result; + using (held) + { + var plan = await legacy.Migration().PlanAsync(context.Cancellation); + Assert.SequenceEqual(new[] { legacy.Local }, plan.InUse); + result = await legacy.Migration().ApplyAsync(context.Cancellation); + Assert.Equal((3, 1, true), (result.ProjectsMoved, result.InUse, result.Split)); + Assert.True(WorkspaceHomes.IsSplitRoot(legacy.Workspace)); + var homes = new WorkspaceHomes(legacy.Workspace); + Assert.Equal(homes.Root, Path.GetDirectoryName(Assert.NotNull(homes.Find(legacy.Local)).Folder)); + Assert.Equal(4, homes.List().Count); + } + result = await legacy.Migration().ApplyAsync(context.Cancellation); + Assert.Equal((1, 0), (result.ProjectsMoved, result.InUse)); + Assert.FileExists(Path.Combine(legacy.Workspace, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile)); + Assert.Null(new WorkspaceHomes(legacy.Workspace).Homes().FirstOrDefault(h => h.IsLegacy)); + } + + [Test] + public async Task AnUnfinishedMoveDefersTheWorkspaceWhileTheCacheIsSorted() + { + var legacy = Make(); + File.WriteAllText(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile), "{}"); + var before = Snapshot(legacy.Workspace); + var result = await legacy.Migration().ApplyAsync(context.Cancellation); + Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Moving, result.Deferred); + Assert.False(result.Split); + Assert.SequenceEqual(before, Snapshot(legacy.Workspace)); + Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "proxies", legacy.NasRemote.ToString("N"), "remote.json")); + + File.Delete(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile)); + await legacy.Migration().ApplyAsync(context.Cancellation); + AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas)); + } + + [Test] + public async Task AWorkspaceThatWentOfflineWaitsWithItsJournal() + { + var legacy = Make(); + var before = HomeFixtures.Manifest(legacy.Workspace); + await Assert.ThrowsAsync(() => legacy.Migration(cut: (name, _) => + { + if (name == "journal") throw new IOException("cut"); + }).ApplyAsync(context.Cancellation)); + // The drive goes before the next start. + var away = legacy.Workspace + "-unplugged"; + Directory.Move(legacy.Workspace, away); + var waiting = await legacy.Migration().ApplyAsync(context.Cancellation); + Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Offline, waiting.Deferred); + Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile)); + Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "import", "card.part")); + // Back: the workspace is sorted, the cache left as it is now. + Directory.Move(away, legacy.Workspace); + await legacy.Migration().ApplyAsync(context.Cancellation); + AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas)); + Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace)); + } + + [Test] + public async Task AProjectInBothPlacesKeepsTheCopySavedLastAndTrashesTheOther() + { + var data = context.CreateTempDirectory("data"); + var workspace = HomeFixtures.LegacyWorkspace(data); + var id = Guid.CreateVersion7(); + var home = HomeFixtures.Local(); + HomeFixtures.Project(workspace, home, "newer", DateTimeOffset.UtcNow, id); + HomeFixtures.Project(Path.Combine(workspace, "local"), home, "older", DateTimeOffset.UtcNow.AddDays(-1), id); + var files = Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories).Count(f => Path.GetFileName(f) != WorkspaceStore.ManifestFile); + var result = await new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = workspace }).ApplyAsync(context.Cancellation); + Assert.Equal(1, result.Clashes); + var kept = ProjectBundle.Load(Path.Combine(workspace, "local", id.ToString("N"), WorkspaceProject.BundleFile)); + Assert.Equal("newer", kept.Workspace!.Name); + var trashed = Assert.NotNull(Directory.EnumerateDirectories(Path.Combine(workspace, "local", WorkspaceTrash.FolderName)).SingleOrDefault()); + Assert.Equal("older", ProjectBundle.Load(Path.Combine(trashed, WorkspaceProject.BundleFile)).Workspace!.Name); + Assert.Equal(files, Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories) + .Count(f => Path.GetFileName(f) is not (WorkspaceStore.ManifestFile or "server.json")), "nothing was deleted"); + } + + [Test] + public async Task ARenameThatDoesNotCheckStopsWithItsJournal() + { + var legacy = Make(); + var tampered = false; + var migration = legacy.Migration(cut: (name, path) => + { + if (name != "renamed" || tampered || !Directory.Exists(path)) return; + tampered = true; + File.WriteAllText(Path.Combine(path, "stray.txt"), "written meanwhile"); + }); + await Assert.ThrowsAsync(() => migration.ApplyAsync(context.Cancellation)); + Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile)); + // The next start takes it up; the entry found renamed is taken as moved. + await legacy.Migration().ApplyAsync(context.Cancellation); + AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas)); + } + + [Test] + public async Task AWorkspaceNeverMadeOrAlreadySortedHasNothingToDo() + { + var data = context.CreateTempDirectory("data"); + var options = new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = Path.Combine(data, "workspace"), CacheRoot = Path.Combine(data, "cache") }; + Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty); + Assert.Equal(HomeLayoutResult.Nothing, await new HomeLayoutMigration(options).ApplyAsync(context.Cancellation)); + Assert.False(Directory.Exists(options.WorkspaceRoot)); + + _ = new WorkspaceHomes(options.WorkspaceRoot); + Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty); + + // An empty 1.0 workspace is split by its manifest alone. + var empty = HomeFixtures.LegacyWorkspace(context.CreateTempDirectory("empty")); + var split = await new HomeLayoutMigration(options with { WorkspaceRoot = empty }).ApplyAsync(context.Cancellation); + Assert.True(split.Split); + Assert.True(WorkspaceHomes.IsSplitRoot(empty)); + Assert.Equal(WorkspaceStore.FormatVersion, new WorkspaceStore(Path.Combine(empty, "local")).Manifest.FormatVersion); + } +} diff --git a/tests/Chronos.Data.Tests/Homes/HomeRekeyTests.cs b/tests/Chronos.Data.Tests/Homes/HomeRekeyTests.cs new file mode 100644 index 00000000..caa6eb29 --- /dev/null +++ b/tests/Chronos.Data.Tests/Homes/HomeRekeyTests.cs @@ -0,0 +1,215 @@ +using Chronos.Core.IO; +using Chronos.Core.Project; +using Chronos.Data.Homes; +using Chronos.Data.Projects; +using Chronos.Data.Sync; +using Chronos.Data.Tests.Infrastructure; +using Chronos.Data.Tests.Repositories; +using Chronos.Data.Workspaces; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Homes; + +/// A server's home moves from its address to its id when it first announces one (owner review 5, #66). +[Category("homes")] +public sealed class HomeRekeyTests(TestContext context) +{ + private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + private static string IdFolder => Id.ToString("N"); + + private (string Data, WorkspaceHomes Homes, string Cache) Make() + { + var data = context.CreateTempDirectory("data"); + return (data, new WorkspaceHomes(Path.Combine(data, "workspace")), Path.Combine(data, "cache")); + } + + private static HomeLayoutMigration Migration(string data, WorkspaceHomes homes, string cache) => + new(new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = homes.Root, CacheRoot = cache }); + + private static string AddressHome(string root, string key) => Path.Combine(root, "servers", HomeFolders.ServerFolder(key)); + + private static ProjectBundle Bundle(string home, Guid id) => ProjectBundle.Load(Path.Combine(home, id.ToString("N"), WorkspaceProject.BundleFile)); + + [Test] + public async Task TheHomeOfTheAddressBecomesTheHomeOfTheId() + { + var (data, homes, cache) = Make(); + var home = HomeFixtures.Server(HomeFixtures.Nas); + var store = homes.StoreFor(home); + var (a, b) = (HomeFixtures.Project(store.Root, home), HomeFixtures.Project(store.Root, HomeFixtures.Server(HomeFixtures.Nas))); + HomeFixtures.TrashEntry(store.Root, a, home); + var before = HomeFixtures.Manifest(homes.Root); + var (sorted, old) = (Guid.CreateVersion7(), Guid.CreateVersion7()); + var cached = Path.Combine(AddressHome(cache, HomeFixtures.Nas), "proxies", sorted.ToString("N")); + Directory.CreateDirectory(cached); + File.WriteAllText(Path.Combine(cached, "remote.json"), "{}"); + HomeFixtures.LegacyProxies(cache, HomeFixtures.Nas, old); + + var result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation); + Assert.Equal(new HomeRekeyResult(2, 0, 0, 2, 0, 0, 0), result); + var idHome = Path.Combine(homes.Root, "servers", IdFolder); + Assert.False(Directory.Exists(AddressHome(homes.Root, HomeFixtures.Nas))); + var idKey = ServerIdentity.IdKey(Id); + foreach (var id in new[] { a, b }) + { + var bundle = Bundle(idHome, id); + Assert.Equal(idKey, bundle.Workspace!.Home!.Server); + Assert.Equal(ServerProjectHome.ArchiveIdOf(idKey), bundle.Archive.Id); + } + var record = Assert.NotNull(ServerHomeRecord.Read(idHome)); + Assert.Equal((idKey, (Guid?)Id, HomeFixtures.Nas), (record.Key, record.Id, record.Address)); + Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(idHome, WorkspaceTrash.FolderName))); + // Only the working states were rewritten: every other file is as it was. + var after = HomeFixtures.Manifest(homes.Root); + Assert.SequenceEqual(before.Keys, after.Keys); + Assert.SequenceEqual(before.Where(f => !f.Key.EndsWith(WorkspaceProject.BundleFile, StringComparison.Ordinal)), + after.Where(f => !f.Key.EndsWith(WorkspaceProject.BundleFile, StringComparison.Ordinal))); + + Assert.FileExists(Path.Combine(cache, "servers", IdFolder, "proxies", sorted.ToString("N"), "remote.json")); + Assert.FileExists(Path.Combine(cache, "servers", IdFolder, "proxies", old.ToString("N"), "remote.json")); + Assert.False(Directory.Exists(AddressHome(cache, HomeFixtures.Nas))); + // The store now finds the id home by either key. + var identity = new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]); + Assert.Equal(idHome, new WorkspaceHomes(homes.Root, identity).ServerRoot(HomeFixtures.Nas)); + + // Again: nothing left to do. + Assert.Equal(new HomeRekeyResult(0, 0, 0, 0, 0, 0, 0), await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation)); + } + + [Test] + public async Task TwoAddressesOfOneServerMergeAndTheOlderCopyGoesToTheTrash() + { + var (data, homes, cache) = Make(); + var byName = HomeFixtures.Server(HomeFixtures.Nas); + var byIp = HomeFixtures.Server(HomeFixtures.NasByIp); + var first = homes.StoreFor(byName).Root; + var second = homes.StoreFor(byIp).Root; + var both = Guid.CreateVersion7(); + HomeFixtures.Project(first, byName, "older", DateTimeOffset.UtcNow.AddDays(-1), both); + HomeFixtures.Project(second, byIp, "newer", DateTimeOffset.UtcNow, both); + var onlyFirst = HomeFixtures.Project(first, byName); + var onlySecond = HomeFixtures.Project(second, byIp); + HomeFixtures.TrashEntry(first, onlyFirst, byName); + HomeFixtures.TrashEntry(second, onlyFirst, byIp); + var files = Directory.EnumerateFiles(homes.Root, "*", SearchOption.AllDirectories).Count(IsProjectFile); + // Each address had a proxy cache too. + Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.Nas), "proxies", "a")); + Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.NasByIp), "proxies", "a")); + Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.NasByIp), "proxies", "b")); + + var result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas, HomeFixtures.NasByIp], context.Cancellation); + Assert.Equal((2, 2, 1, 3), (result.HomesMoved, result.ProjectsMoved, result.ProjectsTrashed, result.BundlesRewritten)); + var idHome = Path.Combine(homes.Root, "servers", IdFolder); + Assert.Equal("newer", Bundle(idHome, both).Workspace!.Name); + foreach (var id in new[] { onlyFirst, onlySecond }) Assert.FileExists(Path.Combine(idHome, id.ToString("N"), WorkspaceProject.BundleFile)); + var trash = Directory.EnumerateDirectories(Path.Combine(idHome, WorkspaceTrash.FolderName)).ToList(); + Assert.Equal(3, trash.Count); + Assert.Contains(trash, t => ProjectBundle.Load(Path.Combine(t, WorkspaceProject.BundleFile)).Workspace!.Name == "older"); + // The second address's home held only its manifest and record afterwards: it went. Nothing else was deleted. + Assert.False(Directory.Exists(second)); + Assert.Equal(files, Directory.EnumerateFiles(homes.Root, "*", SearchOption.AllDirectories).Count(IsProjectFile)); + Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(homes.Root, "servers"))); + Assert.True(Directory.Exists(Path.Combine(cache, "servers", IdFolder, "proxies", "b"))); + } + + [Test] + public async Task AProjectInUseStaysAndKeepsItsAliasWhichStillMatches() + { + var (data, homes, cache) = Make(); + var home = HomeFixtures.Server(HomeFixtures.Nas); + var root = homes.StoreFor(home).Root; + var (held, free) = (HomeFixtures.Project(root, home), HomeFixtures.Project(root, HomeFixtures.Server(HomeFixtures.Nas))); + HomeRekeyResult result; + using (HomeFixtures.Hold(Path.Combine(root, held.ToString("N")))) + { + // A project in use keeps the whole home where it is until a later re-key; the id home is made beside it. + _ = new WorkspaceStore(Path.Combine(homes.Root, "servers", IdFolder)); + result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation); + } + Assert.Equal((0, 1, 1), (result.HomesMoved, result.ProjectsMoved, result.BundlesRewritten)); + Assert.FileExists(Path.Combine(root, held.ToString("N"), WorkspaceProject.BundleFile)); + Assert.Equal(HomeFixtures.Nas, Bundle(root, held).Workspace!.Home!.Server); + var idHome = Path.Combine(homes.Root, "servers", IdFolder); + Assert.Equal(ServerIdentity.IdKey(Id), Bundle(idHome, free).Workspace!.Home!.Server); + Assert.True(ServerProjectHome.IsArchiveIdOf(Bundle(root, held).Archive.Id, [ServerIdentity.IdKey(Id), HomeFixtures.Nas])); + Assert.NotNull(homes.Find(held)); + + result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation); + Assert.Equal((1, 1), (result.ProjectsMoved, result.BundlesRewritten)); + Assert.False(Directory.Exists(root)); + Assert.Equal(ServerIdentity.IdKey(Id), Bundle(idHome, held).Workspace!.Home!.Server); + } + + [Test] + public async Task AWorkingStateInUseInTheIdHomeIsLeftNamingTheAlias() + { + var (data, homes, cache) = Make(); + var home = HomeFixtures.Server(HomeFixtures.Nas); + var root = homes.StoreFor(home).Root; + var id = HomeFixtures.Project(root, home); + // Renamed whole first (nothing held), then held while its working state would be rewritten. + var idHome = Path.Combine(homes.Root, "servers", IdFolder); + Directory.Move(root, idHome); + HomeRekeyResult result; + using (HomeFixtures.Hold(Path.Combine(idHome, id.ToString("N")))) + result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation); + Assert.Equal((0, 1), (result.BundlesRewritten, result.BundlesInUse)); + Assert.Equal(HomeFixtures.Nas, Bundle(idHome, id).Workspace!.Home!.Server); + Assert.Equal(ServerIdentity.IdKey(Id), Assert.NotNull(ServerHomeRecord.Read(idHome)).Key); + } + + [Test] + public async Task AnIdKeyIsNeverAnAliasAndAnEmptyIdIsRefused() + { + var (data, homes, cache) = Make(); + await Assert.ThrowsAsync(() => Migration(data, homes, cache).RekeyAsync(Guid.Empty, [HomeFixtures.Nas], context.Cancellation)); + var other = ServerIdentity.IdKey(Guid.CreateVersion7()); + var otherHome = homes.StoreFor(HomeFixtures.Server(other)).Root; + Assert.Equal(new HomeRekeyResult(0, 0, 0, 0, 0, 0, 0), await Migration(data, homes, cache).RekeyAsync(Id, [other], context.Cancellation)); + Assert.True(Directory.Exists(otherHome)); + } + + private static bool IsProjectFile(string path) => Path.GetFileName(path) is not (WorkspaceStore.ManifestFile or HomeFolders.ServerFile); +} + +/// A re-key moves the server's links in the index too. +[Category("homes")] +public abstract class HomeRekeyLinkTests(TestContext context) : RepositoryTest(context) +{ + private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + + [Test] + public async Task TheLinksOfEveryAliasMoveToTheId() + { + var db = await OpenChronosAsync(); + var store = new SyncLinkStore(db); + foreach (var key in new[] { HomeFixtures.Nas, HomeFixtures.NasByIp, HomeFixtures.Other }) + { + await store.UpsertAsync(new SyncLink + { + Server = key, + RemoteId = Guid.CreateVersion7(), + Title = "Harbour", + RemoteUpdatedAt = TestData.Now, + SyncedAt = TestData.Now, + }, Ct); + } + var data = Context.CreateTempDirectory("data"); + var migration = new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = data, Database = db }); + var result = await migration.RekeyAsync(Id, [HomeFixtures.Nas, HomeFixtures.NasByIp], Ct); + Assert.Equal((2, 0), (result.LinksMoved, result.LinksDropped)); + Assert.Count(2, await store.ListAsync(ServerIdentity.IdKey(Id), Ct)); + Assert.Count(1, await store.ListAsync(HomeFixtures.Other, Ct)); + Assert.Empty(await store.ListAsync(HomeFixtures.Nas, Ct)); + } +} + +public sealed class SqliteHomeRekeyLinkTests(TestContext context) : HomeRekeyLinkTests(context) +{ + protected override Backend Backend => Backend.SqliteMemory; +} + +public sealed class PostgresHomeRekeyLinkTests(TestContext context) : HomeRekeyLinkTests(context) +{ + protected override Backend Backend => Backend.Postgres; +} diff --git a/tests/Chronos.Data.Tests/Sync/ProxyCacheTests.cs b/tests/Chronos.Data.Tests/Sync/ProxyCacheTests.cs new file mode 100644 index 00000000..35e93d19 --- /dev/null +++ b/tests/Chronos.Data.Tests/Sync/ProxyCacheTests.cs @@ -0,0 +1,58 @@ +using Chronos.Core.IO; +using Chronos.Data.Sync; +using Chronos.Data.Tests.Homes; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Sync; + +/// Where the proxies of a server's sequence live (owner review 5, #66): its home in the cache, else where an earlier layout left them. +[Category("sync")] +public sealed class ProxyCacheTests(TestContext context) +{ + private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + + [Test] + public void WithoutAServersRootTheLayoutIs10s() + { + var root = context.CreateTempDirectory("proxies"); + var remote = Guid.CreateVersion7(); + var cache = new ProxyCache(root); + Assert.Equal(Path.Combine(root, HomeFolders.AddressHash(HomeFixtures.Nas), remote.ToString("N")), cache.Folder(HomeFixtures.Nas, remote)); + } + + [Test] + public void ANewSequenceGoesToItsServersHomeAndAnOldOneIsReadWhereItIs() + { + var cacheRoot = context.CreateTempDirectory("cache"); + var (legacyRoot, servers) = (Path.Combine(cacheRoot, "proxies"), Path.Combine(cacheRoot, "servers")); + var identity = new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]); + var cache = new ProxyCache(legacyRoot, servers, () => identity); + var idKey = ServerIdentity.IdKey(Id); + + var fresh = Guid.CreateVersion7(); + Assert.Equal(Path.Combine(servers, Id.ToString("N"), "proxies", fresh.ToString("N")), cache.Folder(idKey, fresh)); + Assert.Equal(cache.Folder(idKey, fresh), cache.Folder(HomeFixtures.Nas, fresh), "an alias names the same home"); + + // A sequence cached by 1.0 under the address is used where it is until it is moved. + var old = Guid.CreateVersion7(); + var legacy = HomeFixtures.LegacyProxies(cacheRoot, HomeFixtures.Nas, old); + Assert.Equal(legacy, cache.Folder(idKey, old)); + Assert.True(cache.Exists(idKey, old)); + + // In the home of the address (sorted before the server announced its id): used there too. + var sorted = Guid.CreateVersion7(); + var byAddress = Path.Combine(servers, HomeFolders.ServerFolder(HomeFixtures.Nas), "proxies", sorted.ToString("N")); + Directory.CreateDirectory(byAddress); + Assert.Equal(byAddress, cache.Folder(idKey, sorted)); + + // Once in its home, that is where it is. + var home = Path.Combine(servers, Id.ToString("N"), "proxies", old.ToString("N")); + Directory.CreateDirectory(Path.GetDirectoryName(home)!); + Directory.Move(legacy, home); + Assert.Equal(home, cache.Folder(HomeFixtures.Nas, old)); + + // A server known by its address only has an address home. + var other = Guid.CreateVersion7(); + Assert.Equal(Path.Combine(servers, HomeFolders.ServerFolder(HomeFixtures.Other), "proxies", other.ToString("N")), cache.Folder(HomeFixtures.Other, other)); + } +} diff --git a/tests/Chronos.Data.Tests/Sync/ServerIdentityTests.cs b/tests/Chronos.Data.Tests/Sync/ServerIdentityTests.cs new file mode 100644 index 00000000..f5bd09b7 --- /dev/null +++ b/tests/Chronos.Data.Tests/Sync/ServerIdentityTests.cs @@ -0,0 +1,84 @@ +using Chronos.Core.IO; +using Chronos.Data.Projects; +using Chronos.Data.Sync; +using Chronos.Protocol; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Sync; + +/// How the desktop names a server (owner review 5, #66): by its announced id, its address keys kept as aliases. +[Category("sync")] +public sealed class ServerIdentityTests +{ + private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + + private static ServerInfo Info(Guid? id) => new("Studio NAS", "1.1.0", 1, [], null, [], new ServerLimits(1, 1), null, null, id); + + [Test] + public void AServerThatAnnouncesAnIdIsKeyedByIt() + { + var key = ServerIdentity.Key(Info(Id), new Uri("https://NAS.local:8443/")); + Assert.Equal("id:" + Id.ToString("N"), key); + Assert.True(ServerIdentity.IsIdKey(key)); + Assert.Equal(Id, ServerIdentity.IdOf(key)); + // Another address of the same server: the same key. + Assert.Equal(key, ServerIdentity.Key(Info(Id), new Uri("https://192.168.1.5:8443"))); + } + + [Test] + public void AnOlderServerKeepsItsAddressKey() + { + var address = new Uri("https://NAS.local:8443/"); + Assert.Equal("https://nas.local:8443", ServerIdentity.Key(Info(null), address)); + Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key((ServerInfo?)null, address)); + Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key(Guid.Empty, address)); + Assert.False(ServerIdentity.IsIdKey(SyncService.ServerKey(address))); + } + + [Test] + public void AliasesNameTheServerOfTheirId() + { + var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443", "https://192.168.1.5:8443"])]); + var key = ServerIdentity.IdKey(Id); + Assert.Equal(key, identity.Canonical("https://NAS.local:8443")); + Assert.Equal(key, identity.Canonical(key.ToUpperInvariant())); + Assert.Equal("https://other:1", identity.Canonical("https://other:1")); + Assert.True(identity.Same("https://nas.local:8443", "https://192.168.1.5:8443")); + Assert.True(identity.Same(key, "https://192.168.1.5:8443")); + Assert.False(identity.Same(key, "https://other:1")); + Assert.True(identity.Same(null, null)); + Assert.False(identity.Same(key, null)); + Assert.Equal(Id.ToString("N"), identity.FolderName("https://192.168.1.5:8443")); + Assert.Equal(HomeFolders.ServerFolder("https://other:1"), identity.FolderName("https://other:1")); + Assert.SequenceEqual(new[] { key, "https://nas.local:8443", "https://192.168.1.5:8443" }, identity.KeysOf("https://192.168.1.5:8443")); + Assert.SequenceEqual(new[] { "https://other:1" }, identity.KeysOf("https://other:1")); + Assert.Equal(2, identity.AliasesOf(Id).Count); + + // Without the table every key is its own. + Assert.False(ServerIdentity.None.Same(key, "https://nas.local:8443")); + } + + [Test] + public void AnAddressChangeKeepsTheKeyOnceTheAliasIsKnown() + { + var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443"])]); + var widened = identity.With(Id, "https://nas.local:9443"); + Assert.True(widened.Same("https://nas.local:8443", "https://nas.local:9443")); + Assert.Same(widened, widened.With(Id, "HTTPS://nas.local:9443")); + Assert.False(identity.Same("https://nas.local:8443", "https://nas.local:9443"), "the table it came from is unchanged"); + var other = Guid.CreateVersion7(); + Assert.Equal(ServerIdentity.IdKey(other), widened.With(other, "https://second:1").Canonical("https://second:1")); + // An address two ids claim stays with the first one listed. + var clash = new ServerIdentity([new KnownServerKey(Id, ["https://x"]), new KnownServerKey(other, ["https://x"])]); + Assert.Equal(ServerIdentity.IdKey(Id), clash.Canonical("https://x")); + } + + [Test] + public void TheArchiveIdOfAnyAliasIsTheServers() + { + string[] keys = [ServerIdentity.IdKey(Id), "https://nas.local:8443"]; + Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://nas.local:8443"), keys)); + Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf(keys[0]), keys)); + Assert.False(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://other:1"), keys)); + } +} diff --git a/tests/Chronos.Data.Tests/Sync/SyncLinkRekeyTests.cs b/tests/Chronos.Data.Tests/Sync/SyncLinkRekeyTests.cs new file mode 100644 index 00000000..0d751b17 --- /dev/null +++ b/tests/Chronos.Data.Tests/Sync/SyncLinkRekeyTests.cs @@ -0,0 +1,72 @@ +using Chronos.Data.Sync; +using Chronos.Data.Tests.Infrastructure; +using Chronos.Data.Tests.Repositories; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Sync; + +/// A server's links move from its address key to its id key (owner review 5, #66), the newer row winning a clash. +[Category("sync")] +public abstract class SyncLinkRekeyTests(TestContext context) : RepositoryTest(context) +{ + private const string Address = "https://nas.local:8443"; + private static readonly string IdKey = ServerIdentity.IdKey(Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01")); + + private static SyncLink Link(string server, Guid remoteId, Guid? localId, DateTimeOffset? synced, string title = "Harbour") => new() + { + Server = server, + RemoteId = remoteId, + LocalId = localId, + Title = title, + RemoteUpdatedAt = TestData.Now, + SyncedAt = synced, + }; + + [Test] + public async Task TheLinksOfAnAddressMoveToTheIdAndTheNewerRowWinsAClash() + { + var db = await OpenChronosAsync(); + var archive = TestData.Archive(); + await db.Archives.UpsertAsync(archive, Ct); + var (a, b, c) = (TestData.Sequence(archive.Id), TestData.Sequence(archive.Id), TestData.Sequence(archive.Id)); + foreach (var sequence in new[] { a, b, c }) await db.Sequences.UpsertAsync(sequence, Ct); + var store = new SyncLinkStore(db); + var (plain, sameRemote, sameLocal, stale) = (Guid.CreateVersion7(), Guid.CreateVersion7(), Guid.CreateVersion7(), Guid.CreateVersion7()); + // Only under the address: it moves. + await store.UpsertAsync(Link(Address, plain, a.Id, TestData.Now), Ct); + // Under both keys for one server sequence: the address's row is newer, so it replaces the id's. + await store.UpsertAsync(Link(Address, sameRemote, null, TestData.Now.AddHours(2), "newer"), Ct); + await store.UpsertAsync(Link(IdKey, sameRemote, null, TestData.Now.AddHours(1), "older"), Ct); + // Both keys link local copy b to different server sequences: the id's row is newer, so the address's goes. + await store.UpsertAsync(Link(Address, sameLocal, b.Id, TestData.Now), Ct); + await store.UpsertAsync(Link(IdKey, Guid.CreateVersion7(), b.Id, TestData.Now.AddDays(1)), Ct); + // Never compared under the address, compared under the id: the id's stays. + await store.UpsertAsync(Link(Address, stale, c.Id, null), Ct); + await store.UpsertAsync(Link(IdKey, stale, null, TestData.Now), Ct); + + var (moved, dropped) = await store.RekeyAsync(Address, IdKey, Ct); + Assert.Equal((2, 3), (moved, dropped)); + Assert.Empty(await store.ListAsync(Address, Ct)); + var links = (await store.ListAsync(IdKey, Ct)).ToDictionary(l => l.RemoteId); + Assert.Equal(4, links.Count); + Assert.Equal(a.Id, links[plain].LocalId); + Assert.Equal("newer", links[sameRemote].Title); + Assert.False(links.ContainsKey(sameLocal)); + Assert.Null(links[stale].LocalId); + Assert.Equal(b.Id, Assert.NotNull(await store.FindByLocalAsync(IdKey, b.Id, Ct)).LocalId); + + // Again: nothing left to move; and a key onto itself does nothing. + Assert.Equal((0, 0), await store.RekeyAsync(Address, IdKey, Ct)); + Assert.Equal((0, 0), await store.RekeyAsync(IdKey, IdKey, Ct)); + } +} + +public sealed class SqliteSyncLinkRekeyTests(TestContext context) : SyncLinkRekeyTests(context) +{ + protected override Backend Backend => Backend.SqliteMemory; +} + +public sealed class PostgresSyncLinkRekeyTests(TestContext context) : SyncLinkRekeyTests(context) +{ + protected override Backend Backend => Backend.Postgres; +} diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceHomesTests.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceHomesTests.cs new file mode 100644 index 00000000..a3f6ee29 --- /dev/null +++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceHomesTests.cs @@ -0,0 +1,172 @@ +using Chronos.Core.IO; +using Chronos.Core.Localization; +using Chronos.Core.Project; +using Chronos.Data.Sync; +using Chronos.Data.Tests.Homes; +using Chronos.Data.Workspaces; +using Chronos.Testing; + +namespace Chronos.Data.Tests.Workspaces; + +/// The workspace split into one store per home (owner review 5, #66). +[Category("workspace")] +public sealed class WorkspaceHomesTests(TestContext context) +{ + private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + + [Test] + public void ANewWorkspaceIsSplitAndEachHomeGetsItsStore() + { + var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace"); + var homes = new WorkspaceHomes(root, new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])])); + Assert.True(homes.IsSplit); + Assert.True(WorkspaceHomes.IsSplitRoot(root)); + Assert.Equal((WorkspaceHomes.ParentFormatVersion, WorkspaceHomes.HomesLayout), (homes.Manifest.FormatVersion, homes.Manifest.Layout)); + Assert.Empty(homes.Homes()); + // A 1.0 Chronos (store format 1) refuses the parent: it never shows the workspace empty. + Assert.Throws(() => new WorkspaceStore(root)); + + var local = homes.StoreFor(HomeFixtures.Local()); + Assert.Equal(Path.Combine(homes.Root, "local"), local.Root); + var server = homes.StoreFor(HomeFixtures.Server(HomeFixtures.Nas)); + Assert.Equal(Path.Combine(homes.Root, "servers", Id.ToString("N")), server.Root); + Assert.Same(server, homes.StoreFor(HomeFixtures.Server(ServerIdentity.IdKey(Id)))); + var record = Assert.NotNull(ServerHomeRecord.Read(server.Root)); + Assert.Equal((ServerIdentity.IdKey(Id), (Guid?)Id), (record.Key, record.Id)); + var other = homes.StoreFor(HomeFixtures.Server(HomeFixtures.Other)); + Assert.Equal(Path.Combine(homes.Root, "servers", HomeFolders.ServerFolder(HomeFixtures.Other)), other.Root); + Assert.Equal(HomeFixtures.Other, Assert.NotNull(ServerHomeRecord.Read(other.Root)).Address); + Assert.NotEqual(local.Id, server.Id); + + var described = Assert.NotNull(homes.Describe(HomeFixtures.Nas, "Studio NAS", HomeFixtures.Nas, "AA:BB", DateTimeOffset.UtcNow)); + Assert.Equal(("Studio NAS", HomeFixtures.Nas, "AA:BB"), (described.Name, described.Address, described.TlsFingerprint)); + Assert.Equal(record.FirstSeen, described.FirstSeen); + + var listed = homes.Homes(); + Assert.Equal(3, listed.Count); + Assert.True(listed[0].IsLocal); + Assert.Contains(listed, h => h.ServerKey == ServerIdentity.IdKey(Id) && h.Server?.Name == "Studio NAS"); + } + + [Test] + public void ProjectsAreFoundInEveryHomeAndTheTrashIsListedPerHome() + { + var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace"); + var homes = new WorkspaceHomes(root); + var a = HomeFixtures.Project(homes.StoreFor(HomeFixtures.Local()).Root, HomeFixtures.Local()); + var serverHome = HomeFixtures.Server(HomeFixtures.Nas); + var b = HomeFixtures.Project(homes.StoreFor(serverHome).Root, serverHome); + Assert.Equal(homes.LocalRoot, Path.GetDirectoryName(Assert.NotNull(homes.Find(a)).Folder)); + Assert.Equal(homes.ServerRoot(HomeFixtures.Nas), Path.GetDirectoryName(Assert.NotNull(homes.Find(b)).Folder)); + Assert.Null(homes.Find(Guid.CreateVersion7())); + Assert.Equal(2, homes.List().Count); + Assert.Equal(2, homes.ListBundles().Count); + + HomeFixtures.TrashEntry(homes.ServerRoot(HomeFixtures.Nas), b, serverHome); + var trash = homes.Trash(); + Assert.Equal(2, trash.Count); + Assert.Equal(1, trash.Sum(t => t.Entries.Count)); + } + + [Test] + public void A10WorkspaceStaysOneStoreUntilItIsSorted() + { + var root = HomeFixtures.LegacyWorkspace(context.CreateTempDirectory("ws")); + var a = HomeFixtures.Project(root, HomeFixtures.Local()); + var homes = new WorkspaceHomes(root); + Assert.False(homes.IsSplit); + Assert.Equal(1, homes.Manifest.FormatVersion); + Assert.Equal(homes.Root, homes.StoreFor(HomeFixtures.Server(HomeFixtures.Nas)).Root); + Assert.Equal(homes.Root, homes.Local.Root); + Assert.True(Assert.NotNull(homes.Find(a)).Exists); + Assert.True(homes.Homes().Single().IsLegacy); + Assert.False(Directory.Exists(homes.LocalRoot), "nothing is made beside the 1.0 projects"); + + // A folder with projects and no manifest is a 1.0 workspace too, never a new split one. + var bare = Path.Combine(context.CreateTempDirectory("bare"), "workspace"); + HomeFixtures.Project(bare, HomeFixtures.Local()); + Assert.False(new WorkspaceHomes(bare).IsSplit); + } + + [Test] + public void AProjectNotSortedYetIsListedWhereItIs() + { + var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace"); + _ = new WorkspaceHomes(root); + var left = HomeFixtures.Project(root, HomeFixtures.Local()); + var homes = new WorkspaceHomes(root); + Assert.True(homes.IsSplit); + Assert.Equal(homes.Root, Path.GetDirectoryName(Assert.NotNull(homes.Find(left)).Folder)); + Assert.True(homes.Homes()[0].IsLegacy); + Assert.Equal(homes.LocalRoot, homes.Local.Root); + } + + [Test] + public void AJobQueuedBeforeTheSortingFindsItsProjectInItsHome() + { + var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace"); + var homes = new WorkspaceHomes(root); + var home = HomeFixtures.Server(HomeFixtures.Nas); + var id = HomeFixtures.Project(homes.ServerRoot(HomeFixtures.Nas), home); + _ = homes.StoreFor(home); + // Queued with the 1.0 folder (the parent): found in the server's home. + Assert.Equal(homes.ServerRoot(HomeFixtures.Nas), WorkspaceJobRoots.Resolve(root, id, null, "Errors.Workspace.NotCheckedOut")); + // Queued with the home of the address, which was re-keyed to the id since. + var rekeyed = Path.Combine(root, "servers", Id.ToString("N")); + Directory.Move(homes.ServerRoot(HomeFixtures.Nas), rekeyed); + Assert.Equal(rekeyed, WorkspaceJobRoots.Resolve(Path.Combine(root, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas)), id, null, "Errors.Workspace.NotCheckedOut")); + // With the parent in use now (Settings › Workspace) and an older folder named. + Assert.Equal(rekeyed, WorkspaceJobRoots.Resolve(Path.Combine(context.CreateTempDirectory("gone"), "workspace"), id, () => root, "Errors.Workspace.NotCheckedOut")); + // While the parent is being moved nothing runs. + File.WriteAllText(Path.Combine(root, WorkspaceMover.JournalFile), "{}"); + var moving = Assert.Throws(() => WorkspaceJobRoots.Resolve(rekeyed, id, null, "Errors.Workspace.NotCheckedOut")); + Assert.Equal(Loc.L("Errors.Workspace.Moving"), moving.Message); + File.Delete(Path.Combine(root, WorkspaceMover.JournalFile)); + Assert.Throws(() => WorkspaceJobRoots.Resolve(root, Guid.CreateVersion7(), null, "Errors.Workspace.NotCheckedOut")); + } + + [Test] + public void SameServerGoesThroughTheAliases() + { + var identity = new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]); + var options = new WorkspaceJobOptions(new Chronos.Data.Processing.ProcessingOptions(), () => false); + Assert.True(options.IsSameServer("https://NAS.local:8443", HomeFixtures.Nas)); + Assert.False(options.IsSameServer(ServerIdentity.IdKey(Id), HomeFixtures.Nas)); + var aliased = options with { SameServer = identity.Same }; + Assert.True(aliased.IsSameServer(ServerIdentity.IdKey(Id), HomeFixtures.Nas)); + Assert.False(aliased.IsSameServer(ServerIdentity.IdKey(Id), HomeFixtures.Other)); + } + + [Test] + public async Task MovingASplitWorkspaceMovesEveryHome() + { + foreach (var sameVolume in new[] { true, false }) + { + var parent = context.CreateTempDirectory("move"); + var from = Path.Combine(parent, "workspace"); + var homes = new WorkspaceHomes(from); + var serverHome = HomeFixtures.Server(HomeFixtures.Nas); + var ids = new[] + { + HomeFixtures.Project(homes.StoreFor(HomeFixtures.Local()).Root, HomeFixtures.Local()), + HomeFixtures.Project(homes.StoreFor(serverHome).Root, serverHome), + HomeFixtures.Project(homes.StoreFor(HomeFixtures.Server(HomeFixtures.Other)).Root, HomeFixtures.Server(HomeFixtures.Other)), + }; + var before = HomeFixtures.Manifest(from); + var to = Path.Combine(parent, "moved"); + var mover = new WorkspaceMover(from, to) { SameVolume = sameVolume }; + var survey = mover.Survey(); + Assert.Equal(3, survey.Projects.Count); + Assert.SequenceEqual(new[] { "local", "servers", WorkspaceStore.ManifestFile }, survey.Entries); + var lines = new List(); + await mover.MoveAsync(survey.Fingerprint, log: lines.Add, cancellationToken: context.Cancellation); + Assert.False(Directory.Exists(from), "the old folder went"); + var moved = new WorkspaceHomes(to); + Assert.True(moved.IsSplit); + Assert.Equal(3, moved.List().Count); + foreach (var id in ids) Assert.NotNull(moved.Find(id)); + Assert.SequenceEqual(before, HomeFixtures.Manifest(to)); + Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", mover.To, 3, survey.Files), lines); + } + } +} diff --git a/tests/Chronos.Server.Tests/Api/ServerInfoIdTests.cs b/tests/Chronos.Server.Tests/Api/ServerInfoIdTests.cs new file mode 100644 index 00000000..2c5c17d8 --- /dev/null +++ b/tests/Chronos.Server.Tests/Api/ServerInfoIdTests.cs @@ -0,0 +1,117 @@ +using System.Text.Json; +using Chronos.Data; +using Chronos.Data.Repositories; +using Chronos.Protocol; +using Chronos.Server.Hosting; +using Chronos.Testing; + +namespace Chronos.Server.Tests.Api; + +/// The server's stable id announced by /server/info (owner review 5, #66). +[Category("server")] +public sealed class ServerInfoIdTests(TestContext context) +{ + private static async Task InfoAsync(TestServer server) + { + using var response = await server.Client.GetAsync("/api/v1/server/info"); + return await response.ReadAsync(ProtocolJson.Default.ServerInfo); + } + + [Test] + public async Task TheIdIsMadeOnceAndKeptAcrossRestarts() + { + var root = context.CreateTempDirectory("kept"); + void Same(Configuration.ServerOptions o) + { + o.DataPath = Path.Combine(root, "data"); + o.ArchivePath = Path.Combine(root, "archive"); + } + Guid first; + await using (var server = await TestServer.StartAsync(context, Same)) + { + first = Assert.NotNull((await InfoAsync(server)).ServerId); + Assert.NotEqual(Guid.Empty, first); + var stored = await server.Database.Settings.GetJsonAsync(SettingScope.App, null, ServerHost.ServerIdKey, context.Cancellation); + Assert.Equal("\"" + first.ToString("D") + "\"", stored); + } + await using (var again = await TestServer.StartAsync(context, Same)) + Assert.Equal((Guid?)first, (await InfoAsync(again)).ServerId); + // Another server (another database) has another id. + await using (var other = await TestServer.StartAsync(context)) + Assert.NotEqual((Guid?)first, (await InfoAsync(other)).ServerId); + } + + [Test] + public async Task TheIdSurvivesARestoreOfTheIndex() + { + var folder = context.CreateTempDirectory("restore"); + Guid id; + string backup; + await using (var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "chronos.db")), context.Cancellation)) + { + id = await ServerHost.ServerIdAsync(db, null, context.Cancellation); + backup = await db.BackupAsync(Path.Combine(folder, "backups"), cancellationToken: context.Cancellation); + } + // A new index (the disk replaced), then the backup put back. + await using var restored = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "new.db")), context.Cancellation); + await restored.RestoreAsync(backup, context.Cancellation); + Assert.Equal(id, await ServerHost.ServerIdAsync(restored, null, context.Cancellation)); + } + + [Test] + public async Task AStoredIdThatIsNotOneIsMadeAgain() + { + var folder = context.CreateTempDirectory("damaged"); + await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "chronos.db")), context.Cancellation); + foreach (var damaged in new[] { "\"not a guid\"", "\"" + Guid.Empty.ToString("D") + "\"", "42" }) + { + await db.Settings.SetJsonAsync(SettingScope.App, null, ServerHost.ServerIdKey, damaged, context.Cancellation); + var made = await ServerHost.ServerIdAsync(db, null, context.Cancellation); + Assert.NotEqual(Guid.Empty, made); + Assert.Equal(made, await ServerHost.ServerIdAsync(db, null, context.Cancellation)); + } + } + + [Test] + [RequiresEnvironment("CHRONOS_TEST_PG")] + public async Task TheIdIsKeptOnPostgres() + { + var baseConnection = Environment.GetEnvironmentVariable("CHRONOS_TEST_PG")!; + var admin = DatabaseFactory.Open(DatabaseOptions.Postgres(baseConnection)); + var schema = "chronos_s_" + Guid.NewGuid().ToString("N")[..16]; + await using (var session = await admin.OpenAsync(context.Cancellation)) + await session.ExecuteScriptAsync($"CREATE SCHEMA {schema}", context.Cancellation); + try + { + var options = DatabaseOptions.Postgres(baseConnection.TrimEnd(';', ' ') + $";Search Path={schema}"); + Guid id; + await using (var db = await ChronosDatabase.OpenAsync(options, context.Cancellation)) + id = await ServerHost.ServerIdAsync(db, null, context.Cancellation); + await using (var db = await ChronosDatabase.OpenAsync(options, context.Cancellation)) + Assert.Equal(id, await ServerHost.ServerIdAsync(db, null, context.Cancellation)); + } + finally + { + await using (var session = await admin.OpenAsync(CancellationToken.None)) + await session.ExecuteScriptAsync($"DROP SCHEMA IF EXISTS {schema} CASCADE", CancellationToken.None); + await admin.DisposeAsync(); + } + } + + [Test] + public void AnOlderClientAndAnOlderServerReadEachOther() + { + var id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); + var info = new ServerInfo("Studio NAS", "1.1.0", 1, ["sync"], null, ["jpeg"], new ServerLimits(1, 1), null, 3, id); + var json = JsonSerializer.Serialize(info, ProtocolJson.Default.ServerInfo); + Assert.Contains("\"serverId\":\"" + id.ToString("D") + "\"", json); + Assert.Equal(info.ServerId, Assert.NotNull(JsonSerializer.Deserialize(json, ProtocolJson.Default.ServerInfo)).ServerId); + + // A 1.0 server sends no id: it reads as null, and a 1.0 client ignores the member it does not know. + const string older = """{"name":"NAS","version":"1.0.0","apiVersion":1,"capabilities":["sync"],"formats":["jpeg"],"limits":{"maxUploadBytes":1,"chunkSizeBytes":1}}"""; + Assert.Null(Assert.NotNull(JsonSerializer.Deserialize(older, ProtocolJson.Default.ServerInfo)).ServerId); + Assert.False(JsonSerializer.Serialize(info with { ServerId = null }, ProtocolJson.Default.ServerInfo).Contains("serverId", StringComparison.Ordinal)); + using var parsed = JsonDocument.Parse(json); + Assert.Equal("Studio NAS", parsed.RootElement.GetProperty("name").GetString()); + } +} From 9ad8c6f7a930f0aa579022306da2485d8042e714 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Sun, 15 Nov 2026 14:00:46 +0100 Subject: [PATCH 2/3] feat: wire separate homes into the desktop (owner review 5 P3.2) The desktop now uses the per-home layout from P3.1: each home keeps its own projects, proxies and originals, and a server is known by its id whichever address reaches it. - Settings keep the known servers (id, name, last address, certificate, aliases, originals archive). Reset and Import keep them; Export leaves them out. - SyncHost records each server the first time it signs in with an id. It names the cache and workspace folders by that id, and re-keys the folders a server had under its addresses at sign-in. The re-key waits while transfer or workspace jobs are pending. - Download originals goes to the server's own archive. It is created beside this PC's archive at the first download, and a toast says where. A sequence that already has a local copy keeps receiving into that copy's archive. A server without an id keeps the 1.0 behaviour. - At start, after the index opens and before any job runs, the folders are sorted into their homes. A toast reports the result: sorted, pending or failed. - The workspace host gives each home's store its own sync. Find, List, usage, clean-up and the trash plan and empty cover every home. Server comparisons go through the identity, so a project stays Online when its server is reached by another address. - The render-node downloads and the server import staging move into the server's folder of the cache. The cache trim keeps `servers`, and the scheduled scan also scans the originals archives. - Settings shows the Home folders rows (Workspace) and the Known servers table (Server). The diagnostics bundle lists the homes. - The dead `AppPaths.CreateDirectories` and `ThumbnailsDirectory` are removed. Refs #66 Co-Authored-By: Claude Opus 5.5 --- i18n/en/Archive.resx | 1 + i18n/en/Errors.resx | 3 + i18n/en/Settings.resx | 8 + i18n/en/Shell.resx | 7 + i18n/en/Tooltips.resx | 13 + i18n/it/Archive.it.resx | 1 + i18n/it/Errors.it.resx | 3 + i18n/it/Settings.it.resx | 8 + i18n/it/Shell.it.resx | 7 + i18n/it/Tooltips.it.resx | 13 + src/Chronos.App/DesignTime/SampleData.cs | 12 +- src/Chronos.App/Editor/ProjectFiles.cs | 2 +- src/Chronos.App/Infrastructure/AppServices.cs | 79 +++++- src/Chronos.App/Infrastructure/AppSettings.cs | 38 +++ .../Infrastructure/DiagnosticsBundle.cs | 45 +++ src/Chronos.App/Jobs/DesktopScheduleHost.cs | 11 +- src/Chronos.App/Library/LibraryActions.cs | 6 +- src/Chronos.App/Sections/ImportViewModel.cs | 15 +- .../Sections/SequencePickerViewModel.cs | 2 +- src/Chronos.App/Sections/SequenceViewModel.cs | 6 +- src/Chronos.App/Sections/SettingsView.xaml | 78 ++++++ src/Chronos.App/Sections/SettingsViewModel.cs | 97 ++++++- src/Chronos.App/Sections/VideosViewModel.cs | 2 +- src/Chronos.App/Server/SyncHost.cs | 260 ++++++++++++++++-- src/Chronos.App/Server/SyncTableViewModel.cs | 6 +- src/Chronos.App/Settings/HomeRows.cs | 97 +++++++ src/Chronos.App/Videos/VideoSources.cs | 20 +- .../Workspace/ProjectMenuViewModel.cs | 2 +- .../Workspace/WorkspaceHost.Cleanup.cs | 70 +++-- .../Workspace/WorkspaceHost.Move.cs | 4 +- src/Chronos.App/Workspace/WorkspaceHost.cs | 156 +++++++---- src/Chronos.Core.Windows/Shell/AppPaths.cs | 16 +- .../Workspace/ServerHomeTests.cs | 64 ++++- .../Workspace/WorkspaceCleanupHostTests.cs | 10 +- .../Workspace/WorkspaceEditorTests.cs | 8 +- .../Workspace/WorkspaceFallbackTests.cs | 14 +- .../Workspace/WorkspaceHostTests.cs | 86 ++++-- .../Workspace/WorkspaceLocalRenderTests.cs | 14 +- .../Workspace/WorkspaceMenuTests.cs | 10 +- .../Workspace/WorkspaceNewerFormatTests.cs | 6 +- .../Workspace/WorkspaceOpenPolicyTests.cs | 2 +- .../Workspace/WorkspaceSectionTests.cs | 6 +- .../Workspace/WorkspaceTestBed.cs | 2 +- .../Shell/AppPathsTests.cs | 16 +- 44 files changed, 1106 insertions(+), 220 deletions(-) create mode 100644 src/Chronos.App/Settings/HomeRows.cs diff --git a/i18n/en/Archive.resx b/i18n/en/Archive.resx index 3cb381b3..77fb1442 100644 --- a/i18n/en/Archive.resx +++ b/i18n/en/Archive.resx @@ -291,6 +291,7 @@ Restore database… Run maintenance Trash… + {0} (server)the name of the archive of a server's downloaded originals; {0} the server's name {0:N0} issue {0:N0} issues The orphan search stoppedlong diff --git a/i18n/en/Errors.resx b/i18n/en/Errors.resx index 5c16556f..6b0eb1c2 100644 --- a/i18n/en/Errors.resx +++ b/i18n/en/Errors.resx @@ -9,6 +9,8 @@ The archive could not be opened.long The current index could not be backed up or replaced; the restore runs again at the next start.long This backup cannot be restored; choose another one.long + {0} presents the ID of {1} with another certificate; check that it is not a copy of that server.{0} the server's name now, {1} the name it had + The folders could not be sorted: {0}. Nothing was deleted; Chronos tries again at the next start.{0} the reason Enter a number from {0} to {1}. Enter a whole number from {0} to {1}. Enter a folder. @@ -34,6 +36,7 @@ {0} did not arrive intact; download it again.{0} file name The sequence is no longer in this PC's archive index. Open or create an archive on this PC to receive the originals. + No folder could be made for the originals of {0}; check that the drive of this PC's archive can be written.{0} the server's name Not connected to this server; connect and retry the job. This sequence is not on the server yet; upload it first. No original reached the server: they are missing here or already in another server sequence. diff --git a/i18n/en/Settings.resx b/i18n/en/Settings.resx index 8619d1c8..38ce56ae 100644 --- a/i18n/en/Settings.resx +++ b/i18n/en/Settings.resx @@ -178,12 +178,18 @@ testing… Reset section Search settings + Server cache Trusted certificates Default server Forget: {0} + Known servers None Offer this PC + Offline + Originals folder Render node + Server ID + Signed in Clear Spilled frames and decoded thumbnails are deleted and rebuilt when needed. Originals, proxies and renders are not touched.long Clear the cache? @@ -206,11 +212,13 @@ Workspace folder changed Choose the workspace folderlong; folder picker title Folder + Home folders Kept versions Folder not changed Projects found: {0}{0} count of the projects the folder chosen holds Projects left: {0}{0} count of the projects the folder in use keeps Sync + This PC Use folder {0} version {0} versions diff --git a/i18n/en/Shell.resx b/i18n/en/Shell.resx index d9200b8f..e8b4e776 100644 --- a/i18n/en/Shell.resx +++ b/i18n/en/Shell.resx @@ -5,6 +5,13 @@ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Chronosinvariant + Server ID clash + {0} project moved{0} count + {0} projects moved{0} count + Originals folder made + Folders pending + Projects in use, or a workspace being moved, follow at the next start.long; toast message under Folders pending + Folders sorted Job finished {0} job finished {0} jobs finished diff --git a/i18n/en/Tooltips.resx b/i18n/en/Tooltips.resx index 3be6f60e..ba8ca96d 100644 --- a/i18n/en/Tooltips.resx +++ b/i18n/en/Tooltips.resx @@ -805,6 +805,14 @@ Frames whose mean brightness is at most this share of white count as night (star fields, night presets) · Enter applies, Esc reverts Open the data folder in Explorer Show the workspace folder in Explorer + This server's proxies, render-node files and staged imports, inside the cache folder + Servers this PC has signed in to, with the folders kept for each + Not signed in: its projects open from the workspace and wait to sync + Show this server's share of the cache in Explorer + Show the archive of this server's downloaded originals in Explorer + Where Download originals puts this server's photos, apart from this PC's archive + Identifies the server even when its address or name changes: {0}{0} the whole id + This PC is signed in to this server now Version, build, system, licence, third-party code Portable mode, logs, diagnostics, database backup and restore, reset Analysis width, night and star thresholds, deep analysis @@ -892,6 +900,7 @@ Replace the installed version in place · your data is kept Collapse navigation to icons · Ctrl+B Show navigation names · Ctrl+B + Show the logs folder in Explorer: the log says which folder could not be sorted Pause the job shown; it continues from its checkpoint when resumed Stop the current job · it can be resumed later Add a folder to watch @@ -975,6 +984,10 @@ Keep the workspace's trash as it is What the workspace folder takes on its drive, its trash included The workspace's folder (a fast disk is best); Change uses another one, Move takes the projects along + Each home keeps its projects in its own folder inside the workspace + Open this home's folder in Explorer + Projects of sequences on {0}: {1}{0} the server's name, {1} the folder + Projects of sequences in this PC's archive: {0}{0} the folder Move the workspace folder with its projects to another folder, after a plan; each file is checked before its source goes Past this size, the media of the projects used longest ago are removed once they are in step with their home; their versions stay The workspace grows as needed; only Clean after removes media diff --git a/i18n/it/Archive.it.resx b/i18n/it/Archive.it.resx index 3fecf541..cea69074 100644 --- a/i18n/it/Archive.it.resx +++ b/i18n/it/Archive.it.resx @@ -285,6 +285,7 @@ Ripristina database… Esegui manutenzione Cestino… + {0} (server) {0:N0} problema {0:N0} problemi Ricerca degli orfani interrotta diff --git a/i18n/it/Errors.it.resx b/i18n/it/Errors.it.resx index 601bb54b..2000a8d5 100644 --- a/i18n/it/Errors.it.resx +++ b/i18n/it/Errors.it.resx @@ -9,6 +9,8 @@ Impossibile aprire l'archivio. Non è stato possibile copiare o sostituire l'indice attuale; il ripristino riparte al prossimo avvio. Questa copia non si può ripristinare; scegline un'altra. + {0} presenta l'ID di {1} con un altro certificato; verifica che non sia una copia di quel server. + Non è stato possibile riordinare le cartelle: {0}. Non è stato eliminato nulla; Chronos riprova al prossimo avvio. Inserisci un numero da {0} a {1}. Inserisci un numero intero da {0} a {1}. Inserisci una cartella. @@ -34,6 +36,7 @@ {0} non è arrivato integro; scaricalo di nuovo. La sequenza non è più nell'indice dell'archivio di questo PC. Apri o crea un archivio su questo PC per ricevere gli originali. + Non è stato possibile creare una cartella per gli originali di {0}; verifica che il disco dell'archivio di questo PC sia scrivibile. Non connesso a questo server; collegati e riprova l'attività. Questa sequenza non è ancora sul server; caricala prima. Nessun originale è arrivato al server: mancano qui o sono già in un'altra sequenza del server. diff --git a/i18n/it/Settings.it.resx b/i18n/it/Settings.it.resx index c0bc4322..0a78d616 100644 --- a/i18n/it/Settings.it.resx +++ b/i18n/it/Settings.it.resx @@ -175,12 +175,18 @@ in prova… Ripristina sezione Cerca impostazioni + Cache del server Certificati attendibili Server predefinito Dimentica: {0} + Server noti Nessuno Offri questo PC + Non in linea + Cartella originali Nodo di rendering + ID server + Connesso Svuota I fotogrammi temporanei e le miniature vengono eliminati e ricreati quando servono. Originali, proxy ed esportazioni restano intatti. Svuotare la cache? @@ -203,11 +209,13 @@ Cartella cambiata Scegli la cartella dello spazio di lavoro Cartella + Cartelle per origine Versioni tenute Cartella non cambiata Progetti trovati: {0} Progetti lasciati: {0} Sincronizzazione + Questo PC Usa cartella {0} versione {0} versioni diff --git a/i18n/it/Shell.it.resx b/i18n/it/Shell.it.resx index 0b51d68b..13b193e9 100644 --- a/i18n/it/Shell.it.resx +++ b/i18n/it/Shell.it.resx @@ -4,6 +4,13 @@ 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + ID server in conflitto + {0} progetto spostato + {0} progetti spostati + Cartella originali creata + Cartelle in attesa + I progetti in uso, o uno spazio di lavoro in spostamento, seguono al prossimo avvio. + Cartelle riordinate Attività completata {0} attività completata {0} attività completate diff --git a/i18n/it/Tooltips.it.resx b/i18n/it/Tooltips.it.resx index 4a6a5ca8..3ab4112f 100644 --- a/i18n/it/Tooltips.it.resx +++ b/i18n/it/Tooltips.it.resx @@ -876,6 +876,14 @@ I fotogrammi con luminosità media al massimo pari a questa quota del bianco contano come notturni (campi stellari, preset notturni) · Invio applica, Esc annulla Apri la cartella dati in Esplora file Mostra la cartella dello spazio di lavoro in Esplora file + Proxy, file del nodo di rendering e importazioni in attesa di questo server, dentro la cartella della cache + I server a cui questo PC ha effettuato l'accesso, con le cartelle tenute per ciascuno + Accesso non effettuato: i suoi progetti si aprono dallo spazio di lavoro e attendono la sincronizzazione + Mostra la parte della cache di questo server in Esplora file + Mostra l'archivio degli originali scaricati da questo server in Esplora file + Dove Scarica originali mette le foto di questo server, separate dall'archivio di questo PC + Identifica il server anche quando cambiano indirizzo o nome: {0} + Questo PC ha effettuato l'accesso a questo server ora Quando Chronos è ridotto a icona o in secondo piano, schede inserite, attività finite e messaggi del server compaiono anche come notifiche di Windows Versione, build, sistema, licenza, codice di terze parti Modalità portatile, log, diagnostica, backup e ripristino del database, valori predefiniti @@ -935,6 +943,10 @@ Svuota per sempre il cestino dello spazio di lavoro, dopo un elenco di ciò che contiene e una conferma scritta Il cestino dello spazio di lavoro è vuoto La cartella dello spazio di lavoro (meglio un disco veloce); Cambia ne usa un'altra, Sposta porta con sé i progetti + Ogni origine tiene i suoi progetti in una cartella propria dentro lo spazio di lavoro + Apri la cartella di questa origine in Esplora file + Progetti delle sequenze su {0}: {1} + Progetti delle sequenze nell'archivio di questo PC: {0} Il progetto che hai aperto per ultimo per la sequenza Il progetto Principale della sequenza, che ne tiene le impostazioni Sposta la cartella dello spazio di lavoro con i suoi progetti in un'altra cartella, dopo un piano; ogni file è verificato prima di rimuovere l'origine @@ -989,6 +1001,7 @@ Sostituisci la versione installata · i tuoi dati restano Riduci la navigazione alle icone · Ctrl+B Mostra i nomi della navigazione · Ctrl+B + Mostra la cartella dei log in Esplora file: il log dice quale cartella non è stata riordinata Mette in pausa l'attività mostrata; quando la riprendi continua dal punto di ripresa Ferma l'attività in corso · potrai riprenderla Aggiungi una cartella da controllare diff --git a/src/Chronos.App/DesignTime/SampleData.cs b/src/Chronos.App/DesignTime/SampleData.cs index ace1ca49..27577fb2 100644 --- a/src/Chronos.App/DesignTime/SampleData.cs +++ b/src/Chronos.App/DesignTime/SampleData.cs @@ -84,7 +84,7 @@ public sealed class SampleData services.CheckForUpdateAsync = _ => Task.FromResult(null); services.Devices.Dispose(); services.Devices = new SampleDeviceMonitor(); - services.Sync.RemoteFactory = (_, _, _) => sample._remote; + services.Sync.RemoteFactory = (_, _, _, _) => sample._remote; services.Sync.EditorFactory = (_, _, _) => sample._remote; // The schedules keep the sample's moment: nothing becomes due while the screenshots are taken. services.SchedulerTime = new FixedTime(Now); @@ -134,6 +134,16 @@ public sealed class SampleData settings.ProtectedToken = "sample-token"; settings.TrustedCertificates = [new TrustedCertificate { Fingerprint = ServerFingerprint, Server = "https://nas.local:8443", TrustedAt = new DateTimeOffset(2026, 9, 1, 9, 30, 0, TimeSpan.Zero) }]; settings.RecentServers = ["https://nas.local:8443"]; + // Settings › Server › Known servers (owner review 5): the sample's server, known by its id. + settings.KnownServers = + [ + new KnownServer + { + Id = Guid.Parse("5a3c1f0e-7d2b-4c8e-9f10-2b6d4e8a1c37"), Name = "Studio NAS", LastAddress = "https://nas.local:8443", Fingerprint = ServerFingerprint, + Aliases = [Chronos.Data.Sync.SyncService.ServerKey(new Uri("https://nas.local:8443"))], LastSeen = Now, + }, + ]; + _services.Sync.KnownServersChanged(); settings.RenderNodeOptIn = true; // The jobs sample shows a deep analysis that yielded to the render (owner review 1). settings.PriorityQueue = true; diff --git a/src/Chronos.App/Editor/ProjectFiles.cs b/src/Chronos.App/Editor/ProjectFiles.cs index e1436daa..49eb6313 100644 --- a/src/Chronos.App/Editor/ProjectFiles.cs +++ b/src/Chronos.App/Editor/ProjectFiles.cs @@ -74,7 +74,7 @@ public sealed class ProjectFiles(ShellViewModel shell) if (bundle.Workspace is { } workspace) { // A workspace's own working state opens as it is; a copy of one (saved elsewhere, another PC's) gives its project its settings. - var own = shell.Services.Workspace.Store.Find(workspace.ProjectId) is { } project + var own = shell.Services.Workspace.Find(workspace.ProjectId) is { } project && string.Equals(Path.GetFullPath(project.BundlePath), Path.GetFullPath(path), StringComparison.OrdinalIgnoreCase); await shell.Sequence.ShowAsync(sequence.Id, new ProjectRequest(sequence.Id, workspace.ProjectId), own ? null : editor => Apply(editor, bundle)); return; diff --git a/src/Chronos.App/Infrastructure/AppServices.cs b/src/Chronos.App/Infrastructure/AppServices.cs index d87a7c8d..c88ee52b 100644 --- a/src/Chronos.App/Infrastructure/AppServices.cs +++ b/src/Chronos.App/Infrastructure/AppServices.cs @@ -12,6 +12,7 @@ using Chronos.App.Workspace; using Chronos.Core.Archiving; using Chronos.Core.Diagnostics; using Chronos.Core.Export; +using Chronos.Core.IO; using Chronos.Core.Imaging; using Chronos.Core.Imaging.Decoders; using Chronos.Core.Model; @@ -21,6 +22,7 @@ using Chronos.Core.Windows.MediaFoundation; using Chronos.Core.Windows.Wic; using Chronos.Data; using Chronos.Data.Archiving; +using Chronos.Data.Homes; using Chronos.Data.Jobs; using Chronos.Data.Processing; using Chronos.Data.Scheduling; @@ -78,6 +80,8 @@ public sealed class AppServices : IAsyncDisposable Workspace = new WorkspaceHost(environment, settings, saveSettings, () => Database, () => Archives, () => Engine); // A server that keeps projects is the home of the workspace projects of its sequences (owner review 2, R8, phase 5b). Workspace.ServerHomes = Sync.ProjectHome; + // A server home's folder is named by its server's id, whichever address reached it (owner review 5, #66). + Workspace.Identity = () => Sync.Identity; Sync.ProjectChanged += Workspace.OnServerProjectChanged; Server = new ServerSession(settings, saveSettings, new ChronosServerApiFactory(), new DpapiTokenProtector()); Encoders = new MediaFoundationInventory(); @@ -151,6 +155,12 @@ public sealed class AppServices : IAsyncDisposable /// How the restore staged in Settings › Advanced went when the index opened; null when none was staged. public StagedRestoreResult? StagedRestore { get; private set; } + /// What the sorting of the folders into homes did when the index opened (owner review 5, #66); null when it had nothing to do. + public HomeLayoutResult? HomeLayout { get; private set; } + + /// Why the folders could not be sorted into homes when the index opened; null when they were, or had nothing to sort. + public Exception? HomeLayoutError { get; private set; } + /// Card detection, eject and format (BRIEF §13); started with the index, replaced by a fake in capture. public IDeviceMonitor Devices { get; set; } = new WindowsDeviceMonitor(); @@ -201,7 +211,8 @@ public sealed class AppServices : IAsyncDisposable new(services.Processing?.Options ?? WorkspaceJobOptions.For(archives).Processing, () => services.Settings.PauseUploadsOnMetered && MeteredNetwork.IsMetered(), services.Workspace.MovedAsync, - () => services.Workspace.Root); + () => services.Workspace.Root, + services.Workspace.SameServer); /// /// Queues a scan of unless one is already waiting or running; the sequences it indexes get @@ -284,6 +295,8 @@ public sealed class AppServices : IAsyncDisposable _schedulerReady.TrySetResult(false); return; } + // Before any job runs: the folders of the workspace and the cache go to their homes (owner review 5, #66). + await SortHomesAsync(Database, ct); try { await StartJobsAsync(Database, startEngine: true, ct); @@ -474,6 +487,66 @@ public sealed class AppServices : IAsyncDisposable } } + /// + /// Sorts the workspace's projects and the cache's server folders into their homes (owner review 5, #66: + /// ; renames only, a clash to the trash, never deleted), or takes up a sorting a previous + /// start left half done. Projects in use or a workspace being moved wait for a later start; a failure is logged and kept + /// for Settings to tell (), and the start goes on: the folders not sorted read as before. + /// + private async Task SortHomesAsync(ChronosDatabase database, CancellationToken ct) + { + try + { + var migration = new HomeLayoutMigration(new HomeLayoutOptions + { + DataDirectory = Environment.DataDirectory, + WorkspaceRoot = Workspace.IsEnabled ? Workspace.Root : null, + CacheRoot = CacheStore.Folder(Environment, Settings), + Identity = Sync.Identity, + CurrentServer = Sync.LastServer, + Database = database, + }); + var result = await Task.Run(() => migration.ApplyAsync(ct), ct); + if (result != HomeLayoutResult.Nothing) + { + HomeLayout = result; + Log.Info("app", $"Folders sorted into homes: {result.ProjectsMoved} project(s), {result.EntriesMoved} entr(ies), {result.InUse} in use, " + + $"{result.Clashes} clash(es){(result.Deferred is { } deferred ? $", the workspace waits ({deferred})" : string.Empty)}"); + } + } + catch (Exception ex) when (ex is HomeLayoutException or IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException) + { + HomeLayoutError = ex; + Log.Error("app", "The folders could not be sorted into homes; the next start tries again", ex); + } + finally + { + Workspace.Reopen(); + } + } + + /// + /// The scan schedule scanned this PC's archive (any thread): the archives of the servers' downloaded originals are scanned + /// after it (owner review 5, Q1: they are archives of their own, which the schedule, made for one archive, does not name). + /// + private void OnScheduledScanDone(Job job) + { + if (job is not { Kind: JobKind.Scan, State: JobState.Done, ScheduleId: not null, TargetIds: [var scanned] }) return; + _ = Task.Run(async () => + { + try + { + var archives = await Sync.OriginalsArchivesAsync(_stopping.Token).ConfigureAwait(false); + if (archives.Any(a => a.Id == scanned)) return; + foreach (var originals in archives) await QueueScanAsync(originals, full: false, _stopping.Token).ConfigureAwait(false); + } + catch (Exception ex) when (ex is DatabaseException or InvalidOperationException or OperationCanceledException) + { + Log.Warning("app", "The archives of the servers' originals could not be queued to be scanned", ex); + } + }); + } + private async Task StartJobsAsync(ChronosDatabase database, bool startEngine, CancellationToken ct) { _wic = new WicDecoderProvider(); @@ -508,6 +581,7 @@ public sealed class AppServices : IAsyncDisposable Engine = new JobEngine(database, handlers, new JobEngineOptions { WorkerId = WorkerId, Kinds = kinds, PriorityQueue = () => PriorityQueue }); // The workspace's size limit runs after each copy of media into it (phase 5c). Engine.JobChanged += Workspace.OnJobChanged; + Engine.JobChanged += OnScheduledScanDone; // Sync follows the engine's jobs before it runs any: a render resumed at start that finishes at once is still sent (Export "Sync"). Sync.Update(); // With no handler the engine must not run: an empty kind list would let workers claim (and fail) every job. @@ -554,7 +628,8 @@ public sealed class AppServices : IAsyncDisposable var node = new RenderNodeClient(client, processing.Options, new RenderNodeOptions { ClientName = System.Environment.MachineName, - CacheDirectory = Path.Combine(cache, "render-node"), + // Each server keeps its own downloads (owner review 5, #66: the render-node folder of its home in the cache). + CacheDirectory = Path.Combine(CacheStore.ServerFolder(cache, Sync.KeyOf(session.Address), Sync.Identity), HomeFolders.RenderNode), CacheLimitBytes = Settings.CacheLimitGb * (1L << 30), }); node.Start(); diff --git a/src/Chronos.App/Infrastructure/AppSettings.cs b/src/Chronos.App/Infrastructure/AppSettings.cs index f8abdfe0..0e41bd62 100644 --- a/src/Chronos.App/Infrastructure/AppSettings.cs +++ b/src/Chronos.App/Infrastructure/AppSettings.cs @@ -10,6 +10,7 @@ using Chronos.Core.Naming; using Chronos.Core.Project; using Chronos.Data.Archiving; using Chronos.Data.Repositories; +using Chronos.Data.Sync; using Chronos.Ui.Theming; namespace Chronos.App.Infrastructure; @@ -66,6 +67,27 @@ public sealed class TrustedCertificate public DateTimeOffset TrustedAt { get; set; } } +/// +/// A server this PC signed in to that announced its stable id (owner review 5, #66): its name and last address, the TLS +/// fingerprint it presented, the address keys it was reached under before (or besides) its id +/// (: projects, links and jobs made then still name them), the archive its +/// downloaded originals go to, and when it was last seen. Its folders (servers\<id> in the workspace and the +/// cache) are named by the id, never by the name. +/// +public sealed class KnownServer +{ + public Guid Id { get; set; } + public string? Name { get; set; } + public string? LastAddress { get; set; } + public string? Fingerprint { get; set; } + public List Aliases { get; set; } = []; + + /// The archive of this PC that holds the server's downloaded originals (owner review 5, Q1); null until the first download. + public Guid? OriginalsArchiveId { get; set; } + + public DateTimeOffset LastSeen { get; set; } +} + /// /// Per-user preferences (language, theme, window layout, archive defaults, server session). Kept in /// settings.json next to the database and written atomically, because language and theme apply before the @@ -257,6 +279,19 @@ public sealed class AppSettings public List TrustedCertificates { get; set; } = []; public List RecentServers { get; set; } = []; + /// + /// The servers known by their stable id and the address keys each was reached under (owner review 5, #66): a server's + /// projects, links and jobs stay its own whatever address it is reached by. This PC's state: Reset and Import keep it, + /// Export leaves it out. + /// + public List KnownServers { get; set; } = []; + + /// The alias table of , as the sync and the workspace compare server keys through it. + public ServerIdentity KnownIdentity() => new([.. KnownServers.Where(k => k.Id != Guid.Empty).Select(k => new KnownServerKey(k.Id, [.. k.Aliases]))]); + + /// The known server of ; null when this PC has none. + public KnownServer? FindKnownServer(Guid id) => KnownServers.FirstOrDefault(k => k.Id == id); + /// Let the server send render jobs to this PC (BRIEF §14.7). public bool RenderNodeOptIn { get; set; } @@ -377,6 +412,7 @@ public sealed class AppSettings ProtectedToken = ProtectedToken, TrustedCertificates = TrustedCertificates, RecentServers = RecentServers, + KnownServers = KnownServers, Window = Window, RailCollapsed = RailCollapsed, EditorGroups = EditorGroups, @@ -400,6 +436,7 @@ public sealed class AppSettings copy.WorkspaceFolder = null; copy.WorkspaceRecent = []; copy.WorkspaceOpened = []; + copy.KnownServers = []; return copy; } @@ -423,6 +460,7 @@ public sealed class AppSettings imported.WorkspaceFolder = WorkspaceFolder; imported.WorkspaceRecent = WorkspaceRecent; imported.WorkspaceOpened = WorkspaceOpened; + imported.KnownServers = KnownServers; return imported; } } diff --git a/src/Chronos.App/Infrastructure/DiagnosticsBundle.cs b/src/Chronos.App/Infrastructure/DiagnosticsBundle.cs index 35c54b61..ad08192c 100644 --- a/src/Chronos.App/Infrastructure/DiagnosticsBundle.cs +++ b/src/Chronos.App/Infrastructure/DiagnosticsBundle.cs @@ -5,10 +5,12 @@ using System.Text; using Chronos.App.Settings; using Chronos.Core.Diagnostics; using Chronos.Core.Export; +using Chronos.Core.IO; using Chronos.Core.Model; using Chronos.Core.Windows.Codecs; using Chronos.Data; using Chronos.Data.Sqlite; +using Chronos.Data.Sync; namespace Chronos.App.Infrastructure; @@ -34,8 +36,11 @@ public static class DiagnosticsBundle var settings = services.Settings.ToJson(); var tailBytes = AppLogging.DiagnosticsTailBytes(services.Settings); var report = Report(services.Environment, extraLines) + DatabaseLine(services.Database); + var workspace = services.Workspace; + var cache = CacheStore.Folder(services.Environment, services.Settings); return Task.Run(async () => { + report += HomesLines(workspace, cache); var failures = services.Database is { } db ? (await db.Jobs.ListAsync(JobState.Failed, FailedJobs, null, ct).ConfigureAwait(false)).Items : []; var codecs = Codecs(services.Encoders); var path = ExportNaming.Unique(folder, $"Chronos diagnostics {DateTime.Now.ToString("yyyy-MM-dd HHmm", CultureInfo.InvariantCulture)}", ".zip"); @@ -76,6 +81,33 @@ public static class DiagnosticsBundle return sb.ToString(); } + /// The homes' folders (owner review 5, #66): the workspace's, each with its projects, and the servers' in the cache. + private static string HomesLines(Chronos.App.Workspace.WorkspaceHost workspace, string cache) + { + var sb = new StringBuilder(); + try + { + if (workspace.IsEnabled && Directory.Exists(workspace.Root)) + { + var homes = workspace.Homes; + sb.AppendLine(CultureInfo.InvariantCulture, $"Workspace {homes.Root} ({(homes.IsSplit ? "homes" : "1.0 layout")})"); + foreach (var home in homes.Homes()) + sb.AppendLine(CultureInfo.InvariantCulture, $" {(home.IsLegacy ? "(parent)" : home.Folder)}: {home.Store.List().Count} project(s){(home.Server?.Name is { } name ? " · " + name : string.Empty)}"); + } + var servers = CacheStore.ServersFolder(cache); + if (Directory.Exists(servers)) + { + foreach (var folder in Directory.EnumerateDirectories(servers).Order(StringComparer.Ordinal)) + sb.AppendLine(CultureInfo.InvariantCulture, $"Cache home {Path.GetFileName(folder)}"); + } + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException) + { + sb.AppendLine("The homes could not be listed: " + ex.Message); + } + return sb.ToString(); + } + private static string DatabaseLine(ChronosDatabase? database) { if (database is null) return "Index not open\n"; @@ -135,6 +167,19 @@ public static class CacheStore public static string Folder(AppEnvironment environment, AppSettings settings) => string.IsNullOrWhiteSpace(settings.CacheFolder) ? environment.CacheDirectory : settings.CacheFolder!; + /// The servers' homes in the cache (<cache>\servers, owner review 5, #66). + public static string ServersFolder(string cache) => Path.Combine(cache, HomeFolders.Servers); + + /// + /// The home of server in the cache (<cache>\servers\<folder>, named by its + /// canonical key through ): its proxies, render-node downloads and staged imports. + /// + public static string ServerFolder(string cache, string serverKey, ServerIdentity identity) + { + ArgumentNullException.ThrowIfNull(identity); + return Path.Combine(ServersFolder(cache), identity.FolderName(serverKey)); + } + /// Creates the folder (if needed) and marks it as Chronos's. public static void Prepare(string folder) { diff --git a/src/Chronos.App/Jobs/DesktopScheduleHost.cs b/src/Chronos.App/Jobs/DesktopScheduleHost.cs index cf29b2f5..3a378ce3 100644 --- a/src/Chronos.App/Jobs/DesktopScheduleHost.cs +++ b/src/Chronos.App/Jobs/DesktopScheduleHost.cs @@ -84,7 +84,11 @@ public sealed class DesktopScheduleHost(AppSettings settings, Func GetArchiveAsync(CancellationToken cancellationToken) { if (database() is not { } db || archives() is not { } service) return null; - var archive = CurrentArchive.Pick(await db.Archives.ListAsync(cancellationToken).ConfigureAwait(false), settings.LastArchivePath); + var listed = await db.Archives.ListAsync(cancellationToken).ConfigureAwait(false); + // Never a server's originals archive in place of this PC's (owner review 5): the app scans those after this one. + var originals = settings.KnownServers.Select(k => k.OriginalsArchiveId).OfType().ToHashSet(); + var archive = (settings.LastArchivePath is { Length: > 0 } last ? CurrentArchive.At(listed, last) : null) + ?? CurrentArchive.Pick([.. listed.Where(a => !originals.Contains(a.Id))], null) ?? CurrentArchive.Pick(listed, null); if (archive is null || ArchiveService.GetAvailability(archive) == ArchiveAvailability.Offline) return null; try { @@ -110,14 +114,15 @@ public sealed class DesktopScheduleHost(AppSettings settings, Funccache-trim (owner review 2, R10): brings the cache folder down to Settings › Storage › /// Cache limit, oldest files first, skipping files in use (). The proxies of server sequences /// (complete sets the sync keeps), the render node's folder (it trims its own downloads to the same limit) and the copies an -/// import to the server stages are never trimmed; Settings › Storage › Clear still empties everything. +/// import to the server stages are never trimmed, in the former shared folders and in each server's own (servers, +/// owner review 5); Settings › Storage › Clear still empties everything. /// public sealed class CacheTrimStep(Func folder, Func limitBytes) : ICleanupStep { public const string StepName = "cache-trim"; /// Folders of the cache the trim leaves whole. - public static IReadOnlyList Kept { get; } = ["proxies", "render-node", "server-import"]; + public static IReadOnlyList Kept { get; } = ["proxies", "render-node", "server-import", Chronos.Core.IO.HomeFolders.Servers]; public string Name => StepName; diff --git a/src/Chronos.App/Library/LibraryActions.cs b/src/Chronos.App/Library/LibraryActions.cs index 6f99ff4d..a13f8711 100644 --- a/src/Chronos.App/Library/LibraryActions.cs +++ b/src/Chronos.App/Library/LibraryActions.cs @@ -117,11 +117,7 @@ public sealed class LibraryActions(ShellViewModel shell) await remote.Service.EnqueueSendExportsAsync(engine, remote.Server, selected.Sequence); return true; case SyncAction.DownloadOriginals when entry is not null: - if ((entry.Local?.ArchiveId ?? (await Services.Sync.CurrentArchiveAsync())?.Id) is not { } archive) - { - shell.Notify(Loc.L("Errors.Sync.NoArchive")); - return false; - } + if (await Services.Sync.OriginalsForDownloadAsync(entry.Local, (title, message, isError) => shell.Notify(title, message, isError: isError)) is not { } archive) return false; await remote.Service.EnqueueDownloadAsync(engine, remote.Server, entry.Id, entry.Title, SyncDownloadMode.Originals, archive); return true; case SyncAction.GetExports when entry is not null: diff --git a/src/Chronos.App/Sections/ImportViewModel.cs b/src/Chronos.App/Sections/ImportViewModel.cs index acd4a894..fc17cc81 100644 --- a/src/Chronos.App/Sections/ImportViewModel.cs +++ b/src/Chronos.App/Sections/ImportViewModel.cs @@ -989,7 +989,20 @@ public sealed class ImportViewModel : SectionViewModel /// What the signed-in user may still store on the server; null without a quota. private long? ServerSpace() => Services.Server.User is { QuotaBytes: > 0 } user ? System.Math.Max(0, user.QuotaBytes - user.UsedBytes) : null; - private string ServerStagingRoot => Path.Combine(CacheStore.Folder(Services.Environment, Services.Settings), "server-import"); + /// + /// Where an import to the server is staged before it is uploaded: the signed-in server's own folder of the cache + /// (servers\<id>\import, owner review 5); signed out, the cache's former shared folder. + /// + private string ServerStagingRoot + { + get + { + var cache = CacheStore.Folder(Services.Environment, Services.Settings); + return Services.Sync.Server is { } server + ? Path.Combine(CacheStore.ServerFolder(cache, server, Services.Sync.Identity), Chronos.Core.IO.HomeFolders.Import) + : Path.Combine(cache, Chronos.Data.Homes.HomeLayoutMigration.LegacyServerImport); + } + } private async Task PlanAsync() { diff --git a/src/Chronos.App/Sections/SequencePickerViewModel.cs b/src/Chronos.App/Sections/SequencePickerViewModel.cs index 8cb7db73..21fef618 100644 --- a/src/Chronos.App/Sections/SequencePickerViewModel.cs +++ b/src/Chronos.App/Sections/SequencePickerViewModel.cs @@ -189,7 +189,7 @@ public sealed class SequencePickerViewModel : LocalizedViewModel { var generation = _generation; foreach (var item in WorkspaceItems.Where(i => change.ProjectId is { } id ? i.ProjectId == id - : i.Entry.IsServerHome && string.Equals(i.Entry.Home.Server, change.Server, StringComparison.OrdinalIgnoreCase)).ToList()) + : i.Entry.IsServerHome && Shell.Services.Workspace.SameServer(i.Entry.Home.Server, change.Server)).ToList()) { if (generation != _generation) return; try diff --git a/src/Chronos.App/Sections/SequenceViewModel.cs b/src/Chronos.App/Sections/SequenceViewModel.cs index c6e7a8fb..e9987ec0 100644 --- a/src/Chronos.App/Sections/SequenceViewModel.cs +++ b/src/Chronos.App/Sections/SequenceViewModel.cs @@ -643,7 +643,7 @@ public sealed class SequenceViewModel : SectionViewModel { workspace.Remember(row.Id, entry.ProjectId); workspace.RecordOpened(entry.ProjectId); - return (new WorkspaceSequenceSource(workspace.Sync, entry.ProjectId, db, decoder, services.Engine), row.DisplayTitle); + return (new WorkspaceSequenceSource(workspace.SyncOf(entry.ProjectId), entry.ProjectId, db, decoder, services.Engine), row.DisplayTitle); } var projectId = entry?.ProjectId ?? request?.ProjectId; string? newName = null; @@ -748,9 +748,9 @@ public sealed class SequenceViewModel : SectionViewModel // Its analyses and renders travel on a connection of the signed-in session, when that is the project's server: // made again whenever the sign-in changes, so a project opened signed out renders once signed in. var server = entry.Home.Server; - var editorHome = new WorkspaceServerHome(() => string.Equals(sync.Server, server, StringComparison.OrdinalIgnoreCase) ? sync.ConnectEditor() : null, + var editorHome = new WorkspaceServerHome(() => sync.Identity.Same(sync.Server, server) ? sync.ConnectEditor() : null, () => sync.Session); - return new ServerOpen((new WorkspaceSequenceSource(workspace.Sync, entry.ProjectId, db, decoder, services.Engine, editorHome), entry.SequenceTitle)); + return new ServerOpen((new WorkspaceSequenceSource(workspace.SyncOf(entry.ProjectId), entry.ProjectId, db, decoder, services.Engine, editorHome), entry.SequenceTitle)); } // An account that may not edit checks nothing out: it is shown with proxies, read-only, as on an older server. if (!sync.Policy.CanEdit || sync.ServerHome(remoteId) is not { } home) return null; diff --git a/src/Chronos.App/Sections/SettingsView.xaml b/src/Chronos.App/Sections/SettingsView.xaml index d1deb8e3..299d4d7d 100644 --- a/src/Chronos.App/Sections/SettingsView.xaml +++ b/src/Chronos.App/Sections/SettingsView.xaml @@ -563,6 +563,28 @@ Visibility="{Binding WorkspaceRows.HasUsageText, Converter={StaticResource BoolToVisibility}}" /> + + + + + + + + + + + + + + public bool ImagesAfterScan { get; init; } + /// + /// Whether holds now, read at every scan and trash restore (owner review 5, #67): Chronos + /// Desktop makes thumbnails and proxies after its scans while it shares its library, as a server does. Null: the fixed + /// . + /// + public Func? ImagesAfterScanWhen { get; init; } + + /// as it is now, else . + public bool QueuesImagesAfterScan => ImagesAfterScanWhen?.Invoke() ?? ImagesAfterScan; + /// /// The index may hold settings newer than a sequence's project.json: the server, whose PATCH wrote the /// settings a client sent into the index alone before owner review 2 phase 5b. When the projects meet a sequence whose two diff --git a/src/Chronos.Server.Host/Chronos.Server.Host.csproj b/src/Chronos.Server.Host/Chronos.Server.Host.csproj new file mode 100644 index 00000000..e7445bf5 --- /dev/null +++ b/src/Chronos.Server.Host/Chronos.Server.Host.csproj @@ -0,0 +1,17 @@ + + + + Exe + net10.0 + Chronos.Server.Host + Chronos.Server.Host + $(ChronosIconDir)chronos-server.ico + true + false + + + + + + diff --git a/src/Chronos.Server.Host/Program.cs b/src/Chronos.Server.Host/Program.cs new file mode 100644 index 00000000..40ff87a8 --- /dev/null +++ b/src/Chronos.Server.Host/Program.cs @@ -0,0 +1,30 @@ +using Chronos.Server.Configuration; +using Chronos.Server.Hosting; +using Chronos.Server.Storage; + +var command = ServerCommandLine.Parse(args); +if (command.Error is not null) +{ + await Console.Error.WriteLineAsync(command.Error); + return 2; +} + +switch (command.Command) +{ + case ServerCommand.Help: + Console.WriteLine(ServerCommandLine.Usage); + return 0; + case ServerCommand.HealthCheck: + return await HealthProbe.RunAsync(); + default: + var app = ServerHost.Build(ServerOptions.Load(), [.. command.HostArguments], RegisterModules); + await app.RunAsync(); + return 0; +} + +// The archive engine, then analysis and managed rendering over it. +static void RegisterModules(IServiceCollection services) +{ + ArchiveModule.Register(services); + ProcessingModule.Register(services); +} diff --git a/src/Chronos.Server/Admin/web/pages/settings.js b/src/Chronos.Server/Admin/web/pages/settings.js index 76039383..e6f7fb79 100644 --- a/src/Chronos.Server/Admin/web/pages/settings.js +++ b/src/Chronos.Server/Admin/web/pages/settings.js @@ -49,7 +49,9 @@ export async function render(root, { api, info, setMeta, actions }) { // The server names the variable that fixes each locked setting: CHRONOS_LOG_LEVEL fixes the console's level too, unless // CHRONOS_LOG_CONSOLE_LEVEL sets its own (BRIEF §14.9). const variable = (name) => Object.entries(settings.lockedBy ?? {}).find(([setting]) => same(setting, name))?.[1] ?? name; - const lock = (name) => locked(name) ? `disabled title="${attr(t("Admin.Settings.Locked.Tip", variable(name)))}"` : `data-i18n-title="Admin.Settings.${cap(name)}.Tip"`; + // Chronos Desktop owns the settings of the library it shares (owner review 5, #67): no variable names them. + const lockTip = (name) => variable(name) === "desktop" ? t("Admin.Settings.LockedByDesktop.Tip") : t("Admin.Settings.Locked.Tip", variable(name)); + const lock = (name) => locked(name) ? `disabled title="${attr(lockTip(name))}"` : `data-i18n-title="Admin.Settings.${cap(name)}.Tip"`; const field = (name, input) => ``; const number = (name, value, attrs = "") => field(name, ``); const text = (name, value, attrs = "dir=\"auto\"") => field(name, ``); diff --git a/src/Chronos.Server/Api/JobEndpoints.cs b/src/Chronos.Server/Api/JobEndpoints.cs index 9f40e190..d1ce3f6a 100644 --- a/src/Chronos.Server/Api/JobEndpoints.cs +++ b/src/Chronos.Server/Api/JobEndpoints.cs @@ -32,8 +32,8 @@ public static class JobEndpoints var jobs = api.MapGroup("/jobs").Requires(Permissions.Read); jobs.MapGet("", ListAsync); jobs.MapPost("", CreateAsync); - jobs.MapGet("/{id:guid}", async (HttpContext context, Guid id, ChronosDatabase db) => - await db.Jobs.GetAsync(id, context.RequestAborted) is { } job + jobs.MapGet("/{id:guid}", async (HttpContext context, Guid id, ChronosDatabase db, JobEngineHost host) => + await db.Jobs.GetAsync(id, context.RequestAborted) is { } job && host.Exposes(job.Kind) ? Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto) : ApiErrors.NotFound(context, "job")); jobs.MapPost("/{id:guid}/cancel", (HttpContext c, Caller caller, Guid id, ChronosDatabase db, JobEngineHost host) => @@ -47,7 +47,7 @@ public static class JobEndpoints jobs.MapPost("/{id:guid}/priority", PriorityAsync); } - private static async Task ListAsync(HttpContext context, ChronosDatabase db, string? state, string? kind, string? cursor, int? limit) + private static async Task ListAsync(HttpContext context, ChronosDatabase db, JobEngineHost host, string? state, string? kind, string? cursor, int? limit) { JobState? stateFilter = null; JobKind? kindFilter = null; @@ -62,8 +62,10 @@ public static class JobEndpoints kindFilter = k; } // A job's answer holds no parameters (a conversion's or an import's can name every sequence or file): the list reads none. + if (kindFilter is { } wanted && !host.Exposes(wanted)) return Results.Json(new Page([], null, null), ProtocolJson.Default.PageJobDto); var page = await db.Jobs.ListBriefAsync(stateFilter, kindFilter, limit ?? 50, string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted); - return Results.Json(new Page([.. page.Items.Select(Dto.Job)], page.NextCursor, null), ProtocolJson.Default.PageJobDto); + // A shared desktop library lists the kinds a server runs alone (owner review 5, #67); the cursor still pages the whole list. + return Results.Json(new Page([.. page.Items.Where(j => host.Exposes(j.Kind)).Select(Dto.Job)], page.NextCursor, null), ProtocolJson.Default.PageJobDto); } private static async Task CreateAsync(HttpContext context, Caller caller, CreateJobRequest request, ChronosDatabase db, JobEngineHost host, @@ -90,7 +92,7 @@ public static class JobEndpoints var titles = new List(ids.Count); foreach (var id in ids) { - if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); + if (await ServedItems.SequenceAsync(db, archive, id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); titles.Add(sequence.DisplayTitle); } targets = ids; @@ -99,7 +101,11 @@ public static class JobEndpoints List<(string Label, string? Parameters)> queue = [(label, request.ParametersJson)]; if (kind == JobKind.Render) { - if (ParseRender(request.ParametersJson) is not { } parsed) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson"); + if (ParseRender(request.ParametersJson) is not { } given) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson"); + // A client's export folder means nothing to the server, and on a shared desktop library it would be anywhere the + // desktop's user may write (owner review 5, #67): a render always goes to the sequence's exports. + var parsed = WithoutExportFolder(given); + if (!ReferenceEquals(parsed, given)) request = request with { ParametersJson = JsonSerializer.Serialize(parsed, ProtocolJson.Default.RenderJobParameters) }; // A render of a project version (owner review 2, R8): of the sequence's projects, a project alone pinned to its head. var (render, missing) = await ProjectRenderAsync(db, archives, targets[0], parsed, ct); if (missing is not null) return ApiErrors.NotFound(context, missing); @@ -221,7 +227,9 @@ public static class JobEndpoints private static async Task ActAsync(HttpContext context, Caller caller, Guid id, ChronosDatabase db, JobState[] allowed, Func action) { var ct = context.RequestAborted; - if (await db.Jobs.GetAsync(id, ct) is not { } job) return ApiErrors.NotFound(context, "job"); + // A shared desktop library's own jobs (workspace, sync, downloads) are not the server's (owner review 5, #67). + if (await db.Jobs.GetAsync(id, ct) is not { } job || !context.RequestServices.GetRequiredService().Exposes(job.Kind)) + return ApiErrors.NotFound(context, "job"); if (job.CreatedBy != caller.Id && !caller.Has(Permissions.ManageArchive)) return ApiErrors.Forbidden(context); if (!allowed.Contains(job.State)) return ApiErrors.Conflict(context, "invalid_state", JobWire.State(job.State)); await action(job, ct); @@ -230,6 +238,20 @@ public static class JobEndpoints } /// Render parameters, or null when they are missing or incomplete. + /// + /// without the export folder its preset names (key folder, any case), or the same + /// instance when it names none (owner review 5, #67). + /// + public static RenderJobParameters WithoutExportFolder(RenderJobParameters parameters) + { + ArgumentNullException.ThrowIfNull(parameters); + if (System.Text.Json.Nodes.JsonNode.Parse(parameters.PresetJson) is not System.Text.Json.Nodes.JsonObject preset) return parameters; + var folders = preset.Select(p => p.Key).Where(k => k.Equals("folder", StringComparison.OrdinalIgnoreCase)).ToList(); + if (folders.Count == 0) return parameters; + foreach (var key in folders) preset.Remove(key); + return parameters with { PresetJson = preset.ToJsonString() }; + } + public static RenderJobParameters? ParseRender(string? json) { if (string.IsNullOrWhiteSpace(json)) return null; diff --git a/src/Chronos.Server/Api/LibraryEndpoints.cs b/src/Chronos.Server/Api/LibraryEndpoints.cs index f4af700c..cc187a19 100644 --- a/src/Chronos.Server/Api/LibraryEndpoints.cs +++ b/src/Chronos.Server/Api/LibraryEndpoints.cs @@ -219,7 +219,8 @@ public static class LibraryEndpoints var allowed = new List(); foreach (var id in ids) { - if (await db.Sequences.GetAsync(id, ct) is not { } sequence) items[id] = Skip(context, id, "not_found"); + if (await db.Sequences.GetAsync(id, ct) is not { } sequence || !context.RequestServices.GetRequiredService().Owns(sequence)) + items[id] = Skip(context, id, "not_found"); else if (!caller.IsAdmin && sequence.OwnerUserId != caller.Id) items[id] = Skip(context, id, "forbidden"); else allowed.Add(sequence); } @@ -337,7 +338,8 @@ public static class LibraryEndpoints { for (var attempt = 0; attempt < 3; attempt++) { - if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return Skip(context, id, "not_found"); + if (await db.Sequences.GetAsync(id, ct) is not { } sequence || !context.RequestServices.GetRequiredService().Owns(sequence)) + return Skip(context, id, "not_found"); var tags = sequence.Tags.Where(t => !remove.Contains(t, StringComparer.OrdinalIgnoreCase)).ToList(); tags.AddRange(add.Where(t => !tags.Contains(t, StringComparer.OrdinalIgnoreCase))); if (tags.Count > SequenceEndpoints.MaxTags) return Skip(context, id, "bad_request"); diff --git a/src/Chronos.Server/Api/ServedItems.cs b/src/Chronos.Server/Api/ServedItems.cs new file mode 100644 index 00000000..98786421 --- /dev/null +++ b/src/Chronos.Server/Api/ServedItems.cs @@ -0,0 +1,90 @@ +using Microsoft.AspNetCore.Routing.Patterns; +using Chronos.Core.Model; +using Chronos.Data; +using Chronos.Server.Storage; + +namespace Chronos.Server.Api; + +/// +/// The served archive's items alone (owner review 5, #67): an index may hold several archives (Chronos Desktop's, which +/// shares one of them, also keeps the archives of other servers' originals), and a sequence, frame, render or video of +/// another one answers 404 as an unknown id would, on every route that names one and in every body that lists them. +/// The rule holds on a server of its own too, whose index holds one archive. +/// +public static class ServedItems +{ + /// What the id in a route names, by the route's prefix and the id's name. + private static readonly (string Prefix, string Value, ItemKind Kind)[] Routes = + [ + ("/api/v1/sequences/{id", "id", ItemKind.Sequence), + ("/api/v1/sequences/{sid", "sid", ItemKind.Sequence), + ("/api/v1/frames/{id", "id", ItemKind.Frame), + ("/api/v1/renders/{id", "id", ItemKind.Render), + ("/api/v1/videos/{id", "id", ItemKind.Render), + ]; + + private enum ItemKind + { + Sequence, + Frame, + Render, + } + + /// The endpoint filter of the API group: an id of another archive's item answers 404 before the endpoint runs. + public static async ValueTask FilterAsync(EndpointFilterInvocationContext invocation, EndpointFilterDelegate next) + { + ArgumentNullException.ThrowIfNull(invocation); + ArgumentNullException.ThrowIfNull(next); + var context = invocation.HttpContext; + if (context.GetEndpoint() is RouteEndpoint { RoutePattern: { } pattern } && Named(pattern, context) is var (kind, id)) + { + var db = context.RequestServices.GetRequiredService(); + var archive = context.RequestServices.GetRequiredService(); + if (await ArchiveOfAsync(db, kind, id, context.RequestAborted).ConfigureAwait(false) is { } owner && owner != archive.Id) + return ApiErrors.NotFound(context, kind == ItemKind.Render ? "render" : kind.ToString().ToLowerInvariant()); + } + return await next(invocation).ConfigureAwait(false); + } + + /// The sequence when it belongs to the served archive; null for another archive's or an unknown one. + public static async Task SequenceAsync(ChronosDatabase db, ServerArchive archive, Guid id, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(db); + ArgumentNullException.ThrowIfNull(archive); + return await db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) is { } sequence && archive.Owns(sequence) ? sequence : null; + } + + /// The render when its sequence belongs to the served archive; null otherwise. + public static async Task RenderAsync(ChronosDatabase db, ServerArchive archive, Guid id, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(db); + ArgumentNullException.ThrowIfNull(archive); + return await db.Renders.GetAsync(id, cancellationToken).ConfigureAwait(false) is { } render + && await SequenceAsync(db, archive, render.SequenceId, cancellationToken).ConfigureAwait(false) is not null + ? render + : null; + } + + private static (ItemKind Kind, Guid Id)? Named(RoutePattern pattern, HttpContext context) + { + if (pattern.RawText is not { } text) return null; + foreach (var (prefix, value, kind) in Routes) + { + if (text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && context.Request.RouteValues.TryGetValue(value, out var raw) + && Guid.TryParse(raw?.ToString(), out var id)) + return (kind, id); + } + return null; + } + + private static async Task ArchiveOfAsync(ChronosDatabase db, ItemKind kind, Guid id, CancellationToken cancellationToken) + { + var sequenceId = kind switch + { + ItemKind.Sequence => id, + ItemKind.Frame => (await db.Frames.GetAsync(id, cancellationToken).ConfigureAwait(false))?.SequenceId, + _ => (await db.Renders.GetAsync(id, cancellationToken).ConfigureAwait(false))?.SequenceId, + }; + return sequenceId is { } sid ? (await db.Sequences.GetAsync(sid, cancellationToken).ConfigureAwait(false))?.ArchiveId : null; + } +} diff --git a/src/Chronos.Server/Api/VideoEndpoints.cs b/src/Chronos.Server/Api/VideoEndpoints.cs index 0086db40..49d39eca 100644 --- a/src/Chronos.Server/Api/VideoEndpoints.cs +++ b/src/Chronos.Server/Api/VideoEndpoints.cs @@ -100,6 +100,7 @@ public static class VideoEndpoints [FromServices] ArchiveService? archives, [FromServices] IVideoLibrary? videos, AuditLog audit, EventHub events, IHostApplicationLifetime lifetime) { if (Ids(request.Ids) is not { } ids) return ApiErrors.BadRequest(context, "bad_request", "ids"); + ids = await ServedAsync(context, db, ids); var library = Library(videos, archives, db); if (!library.IsAvailable) return Unavailable(context); try @@ -166,7 +167,8 @@ public static class VideoEndpoints var renders = new List(ids.Count); foreach (var id in ids) { - if (await db.Renders.GetAsync(id, ct) is not { } render) return ApiErrors.NotFound(context, "render"); + if (await ServedItems.RenderAsync(db, context.RequestServices.GetRequiredService(), id, ct) is not { } render) + return ApiErrors.NotFound(context, "render"); renders.Add(render); } if (!library.IsAvailable || !host.CanRun(JobKind.VideoCopy)) return Unavailable(context); @@ -200,6 +202,23 @@ public static class VideoEndpoints private static IResult Unavailable(HttpContext context) => ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(JobKind.VideoCopy)); /// The ids of a request, each once in order; null when there are none, too many, or an empty one. + /// + /// without the renders of another archive of the index (owner review 5, #67): they are left out as + /// ids the server does not know. Unknown ids stay, for the library to report. + /// + private static async Task> ServedAsync(HttpContext context, ChronosDatabase db, List ids) + { + var archive = context.RequestServices.GetRequiredService(); + var served = new List(ids.Count); + foreach (var id in ids) + { + if (await db.Renders.GetAsync(id, context.RequestAborted) is not { } render + || archive.Owns(await db.Sequences.GetAsync(render.SequenceId, context.RequestAborted))) + served.Add(id); + } + return served; + } + private static List? Ids(IReadOnlyList? ids) { if (ids is null || ids.Count is 0 or > MaxIds || ids.Contains(Guid.Empty)) return null; diff --git a/src/Chronos.Server/Chronos.Server.csproj b/src/Chronos.Server/Chronos.Server.csproj index 34ca3208..878cbd67 100644 --- a/src/Chronos.Server/Chronos.Server.csproj +++ b/src/Chronos.Server/Chronos.Server.csproj @@ -1,12 +1,12 @@ + + Library net10.0 Chronos.Server Chronos.Server - $(ChronosIconDir)chronos-server.ico - true false - diff --git a/src/Chronos.Server/Configuration/ServerOptions.cs b/src/Chronos.Server/Configuration/ServerOptions.cs index dbb79c97..49be97c6 100644 --- a/src/Chronos.Server/Configuration/ServerOptions.cs +++ b/src/Chronos.Server/Configuration/ServerOptions.cs @@ -369,6 +369,121 @@ public sealed class ServerOptions return options; } + /// What names for a setting Chronos Desktop owns while it shares its library (owner review 5, #67). + public const string DesktopLock = "desktop"; + + /// + /// The server Chronos Desktop hosts in-process to share this PC's library (owner review 5, #67): its index, archive service, + /// engine and scheduler are the desktop's (), its own state is in the desktop's data folder. + /// + [JsonIgnore] + public bool Hosted { get; private set; } + + /// + /// The options of the server Chronos Desktop hosts (owner review 5, #67): 's server.json + /// (what the admin panel saved: token lifetime, uploads, sign-in limits, render nodes, audit retention), then the values + /// the desktop owns, locked there with (its name, ports, discovery, archive, videos folder, jobs, + /// language and logs). No environment variable is read: a CHRONOS_* variable set for a server never reaches the + /// desktop. The index is the desktop's (SQLite), the certificate a self-signed one. + /// + public static ServerOptions ForHosted(string dataPath, HostedSettings desktop) + { + ArgumentException.ThrowIfNullOrWhiteSpace(dataPath); + ArgumentNullException.ThrowIfNull(desktop); + var options = new ServerOptions(); + var file = Path.Combine(dataPath, "server.json"); + if (File.Exists(file)) + { + try + { + options = JsonSerializer.Deserialize(DataFolderAccess.ReadTrustedText(file), ServerOptionsJson.Default.ServerOptions) ?? options; + } + catch (JsonException ex) + { + throw new InvalidOperationException($"{file} is not valid JSON: {ex.Message}", ex); + } + } + options.Hosted = true; + options.DataPath = dataPath; + options.InContainer = false; + options.AdminUser = null; + options.AdminPassword = null; + options.DbConnectionString = null; + options.Database = DatabaseKind.Sqlite; + options.TlsMode = TlsMode.Auto; + options.TlsCert = null; + options.TlsKey = null; + options.TrustedProxies = DefaultTrustedProxies; + options.SpacePath = null; + options.ApplyDesktop(desktop); + options.Locked = HostedLocks; + options.LockedBy = HostedLocks.ToDictionary(s => s, _ => DesktopLock, StringComparer.Ordinal); + options.ValidateHosted(); + return options; + } + + /// The settings Chronos Desktop owns while it shares its library: the admin panel shows them locked. + public static IReadOnlyList HostedLocks { get; } = + [ + nameof(ServerName), nameof(HttpPort), nameof(HttpsPort), nameof(PublicHttpPort), nameof(PublicHttpsPort), nameof(Discovery), nameof(ArchivePath), + nameof(VideosPath), nameof(Parallelism), nameof(PriorityQueue), nameof(JobPriorities), nameof(Language), nameof(LogLevel), nameof(ConsoleLogLevel), + nameof(LogRetentionDays), nameof(LogMaxFileMb), nameof(LogMaxTotalMb), nameof(LogRotation), nameof(DiagnosticsLogMb), nameof(TlsMode), + nameof(Database), nameof(JobHistoryDays), + ]; + + /// + /// Puts the values of Chronos Desktop's settings into these options (owner review 5, #67): when the share starts and + /// whenever the desktop saves its settings, so the parallelism, the priority queue, the language and the logs follow at + /// once; a change of name, port or discovery restarts the share. + /// + public void ApplyDesktop(HostedSettings desktop) + { + ArgumentNullException.ThrowIfNull(desktop); + ServerName = string.IsNullOrWhiteSpace(desktop.ServerName) ? Environment.MachineName : desktop.ServerName.Trim(); + HttpPort = desktop.HttpPort; + HttpsPort = desktop.HttpsPort; + PublicHttpPort = desktop.PublicHttpPort; + PublicHttpsPort = desktop.PublicHttpsPort; + Discovery = desktop.Discovery; + ArchivePath = desktop.ArchivePath; + VideosPath = string.IsNullOrWhiteSpace(desktop.VideosPath) ? null : desktop.VideosPath.Trim(); + Parallelism = Math.Clamp(desktop.Parallelism, 1, MaxParallelism); + PriorityQueue = desktop.PriorityQueue; + JobPriorities = new Dictionary(desktop.JobPriorities, StringComparer.Ordinal); + Language = string.IsNullOrWhiteSpace(desktop.Language) ? "en" : desktop.Language; + LogLevel = MsLevel(desktop.LogLevel); + // Nobody reads the console of a desktop program. + ConsoleLogLevel = LogLevel.None; + LogRetentionDays = desktop.LogRetentionDays; + LogMaxFileMb = desktop.LogMaxFileMb; + LogMaxTotalMb = desktop.LogMaxTotalMb; + LogRotation = desktop.LogRotation; + DiagnosticsLogMb = desktop.DiagnosticsLogMb; + JobHistoryDays = Chronos.Data.Housekeeping.CleanupSteps.DefaultHistoryDays; + } + + private static LogLevel MsLevel(Chronos.Core.Diagnostics.LogLevel level) => level switch + { + Chronos.Core.Diagnostics.LogLevel.Trace => LogLevel.Trace, + Chronos.Core.Diagnostics.LogLevel.Debug => LogLevel.Debug, + Chronos.Core.Diagnostics.LogLevel.Info => LogLevel.Information, + Chronos.Core.Diagnostics.LogLevel.Warning => LogLevel.Warning, + _ => LogLevel.Error, + }; + + /// for : no platform check, as the index is the desktop's. + private void ValidateHosted() + { + if (HttpPort is < 0 or > 65535 || HttpsPort is < 0 or > 65535 || PublicHttpPort is < 1 or > 65535 || PublicHttpsPort is < 1 or > 65535) + throw new InvalidOperationException("The ports must be between 1 and 65535."); + if (ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024) + throw new InvalidOperationException("The chunk size must be between 64 KiB and 64 MiB."); + if (MaxUploadBytes < 1 || TokenLifetimeDays < 1 || JobHistoryDays < 1 || UploadRetentionHours < 1) + throw new InvalidOperationException("Upload size, token lifetime, job history and upload retention must be positive."); + if (ServerLogging.Problem(LogRetentionDays, LogMaxFileMb, LogMaxTotalMb, AuditRetentionDays, DiagnosticsLogMb) is { } log) + throw new InvalidOperationException($"{log} is out of range: {ServerLogging.Ranges}."); + } + /// The database to open: , the SQLite file in the data folder, or PostgreSQL from the parts. public DatabaseOptions ToDatabaseOptions() { @@ -457,6 +572,31 @@ public sealed class ServerOptions } } +/// +/// What Chronos Desktop decides for the server it hosts when it shares this PC's library (owner review 5, #67): Settings › +/// Server (name, ports, discovery), the current archive, Settings › Videos, Settings › Rendering (parallelism, priority +/// queue), the language and Settings › Advanced › Logs. locks them in the admin panel. +/// +public sealed record HostedSettings( + string? ServerName, + int HttpPort, + int HttpsPort, + int? PublicHttpPort, + int? PublicHttpsPort, + bool Discovery, + string ArchivePath, + string? VideosPath, + int Parallelism, + bool PriorityQueue, + IReadOnlyDictionary JobPriorities, + string? Language, + Chronos.Core.Diagnostics.LogLevel LogLevel, + int LogRetentionDays, + int LogMaxFileMb, + int LogMaxTotalMb, + LogRotation LogRotation, + int DiagnosticsLogMb); + [JsonSourceGenerationOptions(WriteIndented = true, UseStringEnumConverter = true, PropertyNameCaseInsensitive = true)] [JsonSerializable(typeof(ServerOptions))] internal sealed partial class ServerOptionsJson : JsonSerializerContext; diff --git a/src/Chronos.Server/Hosting/HealthProbe.cs b/src/Chronos.Server/Hosting/HealthProbe.cs index b2b2b9fe..776ef16c 100644 --- a/src/Chronos.Server/Hosting/HealthProbe.cs +++ b/src/Chronos.Server/Hosting/HealthProbe.cs @@ -3,7 +3,7 @@ using Chronos.Server.Configuration; namespace Chronos.Server.Hosting; /// -/// Chronos.Server --healthcheck: probes the local /health endpoint and exits 0/1, so the container +/// Chronos.Server.Host --healthcheck: probes the local /health endpoint and exits 0/1, so the container /// HEALTHCHECK needs no curl/wget in the image. /// public static class HealthProbe diff --git a/src/Chronos.Server/Hosting/HostedServer.cs b/src/Chronos.Server/Hosting/HostedServer.cs new file mode 100644 index 00000000..815a51b9 --- /dev/null +++ b/src/Chronos.Server/Hosting/HostedServer.cs @@ -0,0 +1,168 @@ +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; +using Chronos.Core.Model; +using Chronos.Data; +using Chronos.Data.Archiving; +using Chronos.Data.Archiving.Jobs; +using Chronos.Data.Housekeeping; +using Chronos.Data.Jobs; +using Chronos.Data.Processing; +using Chronos.Data.Projects; +using Chronos.Data.Scheduling; +using Chronos.Server.Api; +using Chronos.Server.Configuration; +using Chronos.Server.Events; +using Chronos.Server.Jobs; +using Chronos.Server.Security; +using Chronos.Server.Storage; + +namespace Chronos.Server.Hosting; + +/// +/// What Chronos Desktop lends the server it hosts to share this PC's library (owner review 5, #67): its index, archive and +/// processing services, job engine and running scheduler, and the archive it serves. The server uses them and never +/// creates, starts, stops, replaces or disposes any of them. +/// +public sealed record HostedEngine(ChronosDatabase Database, ArchiveService Archives, ProcessingService Processing, JobEngine Engine, Scheduler Scheduler, + Archive Archive); + +/// +/// The server Chronos Desktop runs in its own process while it shares this PC's library (owner review 5, #67), built by +/// : start, stop, and what the desktop shows of it (fingerprint, id, ports, the clients +/// of the last minutes, the first administrator's temporary password); and the way the desktop's own engines tell its +/// clients of what they change (, ). +/// +public sealed class HostedServer : IAsyncDisposable +{ + private readonly WebApplication _app; + private readonly ServerLogging _logging; + private int _started; + private int _disposed; + + internal HostedServer(WebApplication app, ServerOptions options, ServerState state, ServerLogging logging) + { + _app = app; + _logging = logging; + Options = options; + State = state; + var jobs = app.Services.GetRequiredService(); + jobs.ChangedOutside += job => JobChangedOutside?.Invoke(job); + } + + public ServerOptions Options { get; } + + internal ServerState State { get; } + + public IServiceProvider Services => _app.Services; + + /// SHA-256 fingerprint of the certificate, colon hex, as clients see it the first time they connect. + public string? Fingerprint => State.TlsFingerprint; + + /// The server's stable id (), kept in the desktop's index. + public Guid? ServerId => State.ServerId; + + /// The events connected clients receive. + public EventHub Events => _app.Services.GetRequiredService(); + + /// The programs that used the server in the last minutes. + public ClientActivity Clients => _app.Services.GetRequiredService(); + + /// A render node changed a job's state, outside the desktop's engine. + public event Action? JobChangedOutside; + + /// The port the HTTP listener got (the configured one, or the system's choice for 0); 0 before the start. + public int HttpPort { get; private set; } + + /// The port the HTTPS listener got; 0 before the start or without a certificate. + public int HttpsPort { get; private set; } + + /// The file that holds the first administrator's temporary password until it is changed. + public string AdminPasswordFile => Path.Combine(Options.DataPath, AdminBootstrap.PasswordFileName); + + /// The first administrator's temporary password while its file exists; null once an administrator set their own. + public string? AdminPassword() + { + try + { + return File.Exists(AdminPasswordFile) ? File.ReadAllText(AdminPasswordFile).Trim() : null; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + return null; + } + } + + /// Listens and starts the server's own services; a port in use throws (). + public async Task StartAsync(CancellationToken cancellationToken = default) + { + if (Interlocked.Exchange(ref _started, 1) == 1) return; + await _app.StartAsync(cancellationToken).ConfigureAwait(false); + foreach (var address in _app.Services.GetRequiredService().Features.Get()?.Addresses ?? []) + { + if (!Uri.TryCreate(address.Replace("[::]", "localhost", StringComparison.Ordinal).Replace("0.0.0.0", "localhost", StringComparison.Ordinal), + UriKind.Absolute, out var uri)) + continue; + if (uri.Scheme == Uri.UriSchemeHttps) HttpsPort = uri.Port; + else if (uri.Scheme == Uri.UriSchemeHttp) HttpPort = uri.Port; + } + } + + /// Stops listening and the server's own services; the desktop's engines run on. + public async Task StopAsync(CancellationToken cancellationToken = default) + { + if (Volatile.Read(ref _started) == 0) return; + await _app.StopAsync(cancellationToken).ConfigureAwait(false); + } + + /// The desktop saved its settings: the parallelism, the priority queue, the language and the logs apply at once. + public void ApplyDesktop(HostedSettings desktop) + { + Options.ApplyDesktop(desktop); + _logging.Apply(Options); + } + + /// The desktop's archive or processing service changed or made sequence : clients are told. + public Task SequenceChangedAsync(Guid id, string change, CancellationToken cancellationToken) => + ServerEventBridge.SequenceChangedAsync(Services.GetRequiredService(), Events, Services.GetService(), id, change, + cancellationToken); + + /// The desktop's archive service changed a sequence's projects: clients are told. + public Task ProjectsChangedAsync(ProjectChangeSet changed, CancellationToken cancellationToken) => + ServerEventBridge.ProjectsChangedAsync(Services.GetRequiredService(), Events, changed, cancellationToken); + + /// The uploads clean-up step of the server: abandoned uploads and old upload records (the upload-cleanup schedule). + public Task SweepUploadsAsync(CleanupContext context, CleanupParameters parameters) => + Services.GetRequiredService().RunAsync(context, parameters); + + /// The render nodes online now, by name. + public async Task> RenderNodesAsync(CancellationToken cancellationToken) + { + var since = DateTimeOffset.UtcNow - TimeSpan.FromSeconds(Options.NodeTimeoutSeconds); + var nodes = await Services.GetRequiredService().RenderNodes.ListOnlineAsync(since, cancellationToken).ConfigureAwait(false); + return [.. nodes.Select(n => n.ClientName)]; + } + + public async ValueTask DisposeAsync() + { + if (Interlocked.Exchange(ref _disposed, 1) == 1) return; + try + { + await StopAsync(CancellationToken.None).ConfigureAwait(false); + } + finally + { + await _app.DisposeAsync().ConfigureAwait(false); + } + } +} + +/// +/// The lifetime of the server Chronos Desktop hosts (owner review 5, #67): the desktop starts and stops it, so nothing hooks +/// Ctrl+C, the process's exit or a termination signal, as the console lifetime would. +/// +internal sealed class DesktopHostLifetime : IHostLifetime +{ + public Task WaitForStartAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +} diff --git a/src/Chronos.Server/Hosting/ServerCommandLine.cs b/src/Chronos.Server/Hosting/ServerCommandLine.cs index a91d85bb..ef797770 100644 --- a/src/Chronos.Server/Hosting/ServerCommandLine.cs +++ b/src/Chronos.Server/Hosting/ServerCommandLine.cs @@ -1,17 +1,11 @@ namespace Chronos.Server.Hosting; -/// What Chronos.Server was asked to do. +/// What Chronos.Server.Host was asked to do. public enum ServerCommand { /// Run in the console (Docker, Linux, development). Run, - /// Run under the Windows Service Control Manager (the command line the installed service uses). - Service, - - InstallService, - UninstallService, - /// Probe /health of the local server and exit 0/1 (container health check). HealthCheck, @@ -24,12 +18,9 @@ public sealed record ServerCommandLine(ServerCommand Command, IReadOnlyList ServerCommand.Service, - "--install-service" => ServerCommand.InstallService, - "--uninstall-service" => ServerCommand.UninstallService, "--healthcheck" => ServerCommand.HealthCheck, "--help" or "-h" or "-?" or "/?" => ServerCommand.Help, _ => null, diff --git a/src/Chronos.Server/Hosting/ServerHost.cs b/src/Chronos.Server/Hosting/ServerHost.cs index 2c83814f..77920f8a 100644 --- a/src/Chronos.Server/Hosting/ServerHost.cs +++ b/src/Chronos.Server/Hosting/ServerHost.cs @@ -16,12 +16,13 @@ using Chronos.Server.Storage; namespace Chronos.Server.Hosting; /// -/// Builds the Chronos Server web application from (used by Program, the Windows service -/// host and tests). The archive module plugs in through the configureServices callback of , +/// Builds the Chronos Server web application from (used by Chronos.Server.Host's Program and +/// tests). The archive module plugs in through the configureServices callback of , /// which runs before the server's own registrations: it must register and /// (the server refuses to start without them), may add /// s, and may resolve , -/// and from DI in its factories. +/// and from DI in its factories. builds the same server over the index, +/// engines and scheduler of Chronos Desktop, which shares its library with it (owner review 5, #67). /// public static class ServerHost { @@ -61,19 +62,7 @@ public static class ServerHost ServerId = ServerIdAsync(database, bootLogger, CancellationToken.None).GetAwaiter().GetResult(), }; - builder.WebHost.ConfigureKestrel(k => - { - k.AddServerHeader = false; - k.Limits.MaxRequestBodySize = JsonBodyLimit; - k.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30); - k.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); - k.ListenAnyIP(options.HttpPort); - if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l => - { - l.Protocols = HttpProtocols.Http1AndHttp2; - l.UseHttps(certificate); - }); - }); + ConfigureKestrel(builder, options, certificate); ConfigureServices(builder.Services, options, state, database, archive); var app = builder.Build(); @@ -118,6 +107,69 @@ public static class ServerHost } } + /// + /// The server Chronos Desktop runs in its own process to share this PC's library (owner review 5, #67): the same API, admin + /// panel, accounts, TLS, discovery, uploads, sync, events and render nodes as a server of its own, over the desktop's + /// index, archive service, processing service, job engine and scheduler (), which it never + /// creates, starts, stops or disposes. Its own state is in (made private first); + /// the first administrator is made there as on a new server. It sets no process-wide log sink or level and hooks no + /// console signal: the desktop's stay. Runs synchronously: call it off the UI thread. + /// + public static HostedServer BuildHosted(ServerOptions options, HostedEngine engine) + { + ArgumentNullException.ThrowIfNull(options); + ArgumentNullException.ThrowIfNull(engine); + if (!options.Hosted) throw new ArgumentException("The options are not those of a hosted server (ServerOptions.ForHosted).", nameof(options)); + DataFolderAccess.Restrict(options.DataPath); + Directory.CreateDirectory(options.LogPath); + Directory.CreateDirectory(options.UploadsPath); + + // The desktop's command line and environment are not the server's. + var builder = WebApplication.CreateSlimBuilder(new WebApplicationOptions { Args = [], ContentRootPath = AppContext.BaseDirectory }); + var logging = new ServerLogging(options, setsEngineLevel: false); + logging.AddTo(builder.Logging); + builder.Services.AddSingleton(logging); + + var bootLogger = new FileLoggerProvider(logging).CreateLogger("Chronos.Server"); + var certificate = CertificateStore.Load(options, bootLogger); + AdminBootstrap.RunAsync(engine.Database, options, TimeProvider.System, bootLogger, CancellationToken.None).GetAwaiter().GetResult(); + var state = new ServerState + { + TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate), + Database = ServerDatabase.DescribeAsync(engine.Database, CancellationToken.None).GetAwaiter().GetResult(), + ServerId = ServerIdAsync(engine.Database, bootLogger, CancellationToken.None).GetAwaiter().GetResult(), + }; + var archive = ServerArchive.ForArchive(engine.Archive); + + ConfigureKestrel(builder, options, certificate); + ConfigureServices(builder.Services, options, state, engine.Database, archive, engine); + + var app = builder.Build(); + ConfigurePipeline(app, options); + app.Logger.LogInformation("Chronos Server {Version} in Chronos Desktop · archive {Archive} · data {Data} · TLS {Tls}{Fingerprint}", + SystemEndpoints.Version, archive.Root, options.DataPath, options.TlsMode, + state.TlsFingerprint is null ? string.Empty : " · SHA-256 " + state.TlsFingerprint); + if (archive.Structure is { IsComplete: false } structure) + app.Logger.LogWarning("The archive's folders are incomplete: {Problem} {Detail}", structure.Message, structure.Detail ?? string.Empty); + return new HostedServer(app, options, state, logging); + } + + /// Kestrel on every interface: the HTTP port, and the HTTPS port with HTTP/2 when there is a certificate. + private static void ConfigureKestrel(WebApplicationBuilder builder, ServerOptions options, System.Security.Cryptography.X509Certificates.X509Certificate2? certificate) => + builder.WebHost.ConfigureKestrel(k => + { + k.AddServerHeader = false; + k.Limits.MaxRequestBodySize = JsonBodyLimit; + k.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30); + k.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); + k.ListenAnyIP(options.HttpPort); + if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l => + { + l.Protocols = HttpProtocols.Http1AndHttp2; + l.UseHttps(certificate); + }); + }); + /// The app setting that holds the server's stable id (). public const string ServerIdKey = "server.id"; @@ -153,8 +205,12 @@ public static class ServerHost return id; } - /// The single composition root of the server's services. - public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive) + /// + /// The single composition root of the server's services; with , those of the library Chronos + /// Desktop shares, whose engines, engine and scheduler are the desktop's (owner review 5, #67). + /// + public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive, + HostedEngine? hosted = null) { services.AddSingleton(options); services.AddSingleton(state); @@ -165,7 +221,9 @@ public static class ServerHost services.TryAddSingleton(); services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default)); services.Configure(o => o.ThrowOnBadRequest = true); - services.Configure(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30)); + // The desktop's exit waits for everything it runs: the server it hosts stops within a few seconds. + services.Configure(o => o.ShutdownTimeout = TimeSpan.FromSeconds(hosted is null ? 30 : 5)); + if (hosted is not null) RegisterHosted(services, hosted); if (options.TlsMode == TlsMode.Proxy) { // Only the configured proxies may say who the client is: a client that reaches the HTTP port directly would @@ -192,18 +250,24 @@ public static class ServerHost services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); + if (hosted is null) services.AddSingleton(); + else services.AddSingleton(sp => new JobEngineHost(hosted.Engine, sp.GetRequiredService())); services.AddHostedService(sp => sp.GetRequiredService()); // After the engine's host: the scheduler queues on its engine, starts after it and stops before it (owner review 2, R10). services.AddSingleton(); - services.AddSingleton(); + if (hosted is null) services.AddSingleton(); + else services.AddSingleton(sp => new SchedulerHost(database, hosted.Scheduler, sp.GetRequiredService(), options, + sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService>())); services.AddHostedService(sp => sp.GetRequiredService()); services.AddSingleton(); services.AddHostedService(sp => sp.GetRequiredService()); // Recovers interrupted commits at start; its sweep is the clean-up step of the upload-cleanup schedule. services.AddSingleton(); services.AddHostedService(sp => sp.GetRequiredService()); - services.AddSingleton(); + // The desktop's archive service holds a step of its own that runs this one while it shares its library. + services.AddSingleton(); + services.AddSingleton(sp => sp.GetRequiredService()); // Main takes settings written outside the projects soon after they are (owner review 2, R8). services.AddSingleton(); services.AddHostedService(sp => sp.GetRequiredService()); @@ -211,6 +275,22 @@ public static class ServerHost services.AddHostedService(sp => sp.GetRequiredService()); } + /// + /// The desktop's engines as the archive and processing modules would register them, and a lifetime that hooks no console + /// signal of the desktop's process (owner review 5, #67). + /// + private static void RegisterHosted(IServiceCollection services, HostedEngine hosted) + { + services.AddSingleton(hosted); + services.AddSingleton(hosted.Archives); + services.AddSingleton(hosted.Archives.Ingest); + services.AddSingleton(hosted.Archives.Videos); + services.AddSingleton(); + services.AddSingleton(hosted.Processing); + services.AddSingleton(); + services.AddSingleton(); + } + private static void ConfigurePipeline(WebApplication app, ServerOptions options) { if (options.TlsMode == TlsMode.Proxy) app.UseForwardedHeaders(); @@ -221,6 +301,8 @@ public static class ServerHost AdminPanel.Map(app, options); var api = app.MapGroup("/api/v1"); + // A sequence, frame, render or video of another archive of the index answers as an unknown id (owner review 5, #67). + api.AddEndpointFilter(ServedItems.FilterAsync); SystemEndpoints.Map(app, api); AuthEndpoints.Map(api); UserEndpoints.Map(api); diff --git a/src/Chronos.Server/Hosting/ServerLogging.cs b/src/Chronos.Server/Hosting/ServerLogging.cs index e9a196d1..28ef568a 100644 --- a/src/Chronos.Server/Hosting/ServerLogging.cs +++ b/src/Chronos.Server/Hosting/ServerLogging.cs @@ -33,12 +33,21 @@ public sealed class ServerLogging private volatile MsLogLevel _console; /// Opens the log files of and applies its levels (call once the data folder is prepared). - public ServerLogging(ServerOptions options) + /// The server's settings. + /// + /// Whether the levels also set the engine's : false when Chronos Desktop hosts the server + /// (owner review 5, #67), whose own log settings keep it. + /// + public ServerLogging(ServerOptions options, bool setsEngineLevel = true) { + SetsEngineLevel = setsEngineLevel; Sink = new FileLogSink(options.LogPath, "server", FileOptions(options)); Apply(options); } + /// The levels set the process's too (a server of its own, not one Chronos Desktop hosts). + public bool SetsEngineLevel { get; } + public FileLogSink Sink { get; } public MsLogLevel FileLevel => _file; @@ -63,7 +72,7 @@ public sealed class ServerLogging _file = options.LogLevel; _console = options.ConsoleLogLevel; Sink.Configure(FileOptions(options)); - Log.MinimumLevel = Core(options.LogLevel < options.ConsoleLogLevel ? options.LogLevel : options.ConsoleLogLevel); + if (SetsEngineLevel) Log.MinimumLevel = Core(options.LogLevel < options.ConsoleLogLevel ? options.LogLevel : options.ConsoleLogLevel); } public static LogFileOptions FileOptions(ServerOptions options) => new() diff --git a/src/Chronos.Server/Hosting/Windows/ServiceEventLog.cs b/src/Chronos.Server/Hosting/Windows/ServiceEventLog.cs deleted file mode 100644 index 81ada5f3..00000000 --- a/src/Chronos.Server/Hosting/Windows/ServiceEventLog.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System.Runtime.Versioning; -using static Chronos.Server.Hosting.Windows.ServiceNative; - -namespace Chronos.Server.Hosting.Windows; - -/// -/// Errors of the Windows service in the Application event log (source ChronosServer). A service has no console, -/// and a start-up failure comes before the server's log file exists (the data folder that holds it may be the very -/// thing refused), so Event Viewer is where an operator finds why the service stopped. The source is not registered -/// with a message file: Event Viewer says it found no description for the event, then shows the text whole. -/// -internal static unsafe class ServiceEventLog -{ - public const uint StartFailed = 1; - public const uint StopFailed = 2; - - /// ReportEvent takes at most 31,839 characters per string. - public const int MaxLength = 31_839; - - /// - /// The event text: and the reason the server gave (the entry to check, for a refused data - /// folder), then the exception for a bug report; cut to . - /// - public static string Describe(string what, Exception exception) - { - var text = $"{what} {exception.Message}{Environment.NewLine}{Environment.NewLine}{exception}"; - if (text.Length <= MaxLength) return text; - var cut = MaxLength - 1; - // Never half a surrogate pair. - if (char.IsHighSurrogate(text[cut - 1])) cut--; - return string.Concat(text.AsSpan(0, cut), "…"); - } - - /// Writes an error event; false when the event log could not be written (the console still gets it). - [SupportedOSPlatform("windows")] - public static bool Error(uint eventId, string text) => Error(WindowsServiceHost.ServiceName, eventId, text); - - /// as (tests write as their own source). - [SupportedOSPlatform("windows")] - internal static bool Error(string sourceName, uint eventId, string text) - { - var source = RegisterEventSourceW(null, sourceName); - if (source == 0) return false; - try - { - fixed (char* chars = text.Length <= MaxLength ? text : text[..MaxLength]) - { - var strings = stackalloc char*[1]; - strings[0] = chars; - return ReportEventW(source, EventLogErrorType, 0, eventId, 0, 1, 0, strings, null); - } - } - finally - { - DeregisterEventSource(source); - } - } -} diff --git a/src/Chronos.Server/Hosting/Windows/ServiceInstaller.cs b/src/Chronos.Server/Hosting/Windows/ServiceInstaller.cs deleted file mode 100644 index 84c578ab..00000000 --- a/src/Chronos.Server/Hosting/Windows/ServiceInstaller.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System.ComponentModel; -using System.Runtime.InteropServices; -using System.Runtime.Versioning; -using System.Security.Principal; -using Chronos.Core.Localization; -using static Chronos.Server.Hosting.Windows.ServiceNative; - -namespace Chronos.Server.Hosting.Windows; - -/// -/// --install-service / --uninstall-service: registers the server with the Service Control Manager -/// (automatic start, LocalSystem, command line "…\Chronos.Server.exe" --service) or stops and removes it. -/// Both need an elevated prompt; failures surface as . A LocalSystem service runs whatever -/// sits at its image path, so it is registered only from a copy under Program Files that nobody but SYSTEM, -/// Administrators and TrustedInstaller can change: a per-user install or a zip unpacked elsewhere would hand SYSTEM to -/// any program of that user. Otherwise refuses with . -/// -[SupportedOSPlatform("windows")] -public static unsafe class ServiceInstaller -{ - private const string DisplayName = "Chronos Server"; - - /// TrustedInstaller, the owner of Windows' own files under Program Files. - private static readonly SecurityIdentifier TrustedInstaller = new("S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464"); - - /// This copy is not in a folder that only administrators can change. - public static void Install() - { - if (UnsafeLocation() is { } problem) throw new InvalidOperationException(problem); - var manager = OpenSCManagerW(null, null, ScManagerConnect | ScManagerCreateService); - if (manager == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); - try - { - var service = CreateServiceW(manager, WindowsServiceHost.ServiceName, DisplayName, ServiceAllAccess, ServiceWin32OwnProcess, ServiceAutoStart, - ServiceErrorNormal, CommandLine(), null, 0, null, null, null); - if (service == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); - try - { - fixed (char* text = Loc.L("Server.Service.Description")) - { - var description = new ServiceDescription { Description = text }; - ChangeServiceConfig2W(service, ServiceConfigDescription, &description); - } - } - finally - { - CloseServiceHandle(service); - } - } - finally - { - CloseServiceHandle(manager); - } - } - - public static void Uninstall() - { - var manager = OpenSCManagerW(null, null, ScManagerConnect); - if (manager == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); - try - { - var service = OpenServiceW(manager, WindowsServiceHost.ServiceName, Delete | ServiceStop | ServiceQueryStatus); - if (service == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); - try - { - ServiceStatus status; - if (!ControlService(service, ControlStop, &status) && Marshal.GetLastPInvokeError() is var stopError && stopError != ErrorServiceNotActive) - throw new Win32Exception(stopError); - if (!DeleteService(service) && Marshal.GetLastPInvokeError() is var deleteError && deleteError != ErrorServiceMarkedForDelete) - throw new Win32Exception(deleteError); - } - finally - { - CloseServiceHandle(service); - } - } - finally - { - CloseServiceHandle(manager); - } - } - - /// - /// Why this copy may not be registered, as a localized sentence, or null when it may: the program folder (and, when - /// run by dotnet, the host's folder) must be inside Program Files and changeable only by administrators. - /// - internal static string? UnsafeLocation() - { - var program = Path.TrimEndingDirectorySeparator(AppContext.BaseDirectory); - if (!IsUnderProgramFiles(program)) return Loc.L("Server.Service.NotProgramFiles", program); - if (FindChangeable(new DirectoryInfo(program), recursive: true) is { } open) return Loc.L("Server.Service.Writable", open); - // Under dotnet the host runs as SYSTEM too; its folder is checked without the thousands of framework files below it. - if (Environment.ProcessPath is { } process && Path.GetDirectoryName(process) is { } host - && !string.Equals(Path.TrimEndingDirectorySeparator(host), program, StringComparison.OrdinalIgnoreCase)) - { - if (!IsUnderProgramFiles(host)) return Loc.L("Server.Service.NotProgramFiles", host); - if (FindChangeable(new DirectoryInfo(host), recursive: false) is { } hostOpen) return Loc.L("Server.Service.Writable", hostOpen); - } - return null; - } - - /// True for a path inside %ProgramFiles% or %ProgramFiles(x86)% (not the folder itself). - internal static bool IsUnderProgramFiles(string path) - { - var full = Path.GetFullPath(path); - foreach (var folder in new[] { Environment.SpecialFolder.ProgramFiles, Environment.SpecialFolder.ProgramFilesX86 }) - { - var root = Environment.GetFolderPath(folder); - if (root.Length > 0 && full.StartsWith(Path.TrimEndingDirectorySeparator(root) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) - return true; - } - return false; - } - - /// - /// The first of the folder, its files and (when recursive) its subfolders whose ACL lets an account other than - /// SYSTEM, Administrators, TrustedInstaller or the registering administrator change it; null when there is none. - /// - internal static string? FindChangeable(DirectoryInfo folder, bool recursive) => FindChangeable(folder, recursive, TrustedAccounts()); - - private static string? FindChangeable(DirectoryInfo folder, bool recursive, HashSet trusted) - { - if (DataFolderAccess.IsChangeableByOthers(folder.GetAccessControl(), trusted, checkOwner: false)) return folder.FullName; - foreach (var file in folder.EnumerateFiles()) - { - if (DataFolderAccess.IsChangeableByOthers(file.GetAccessControl(), trusted, checkOwner: false)) return file.FullName; - } - if (!recursive) return null; - foreach (var child in folder.EnumerateDirectories()) - { - if (FindChangeable(child, recursive: true, trusted) is { } path) return path; - } - return null; - } - - private static HashSet TrustedAccounts() - { - var trusted = new HashSet - { - new(WellKnownSidType.LocalSystemSid, null), - new(WellKnownSidType.BuiltinAdministratorsSid, null), - TrustedInstaller, - }; - // Registering needs an elevated prompt, so the account doing it is an administrator; setups often name it. - using var identity = WindowsIdentity.GetCurrent(); - if (identity.User is { } user && new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator)) trusted.Add(user); - return trusted; - } - - /// The service's command line: this executable (or dotnet plus this assembly) with --service. - private static string CommandLine() - { - var process = Environment.ProcessPath ?? throw new InvalidOperationException("The executable path is unknown."); - // Assembly.Location is empty in a single-file app; under dotnet the entry assembly sits in the base directory. - var assembly = Path.Combine(AppContext.BaseDirectory, typeof(ServiceInstaller).Assembly.GetName().Name + ".dll"); - return Path.GetFileNameWithoutExtension(process).Equals("dotnet", StringComparison.OrdinalIgnoreCase) - ? $"\"{process}\" \"{assembly}\" --service" - : $"\"{process}\" --service"; - } -} diff --git a/src/Chronos.Server/Hosting/Windows/ServiceNative.cs b/src/Chronos.Server/Hosting/Windows/ServiceNative.cs deleted file mode 100644 index aa74fd87..00000000 --- a/src/Chronos.Server/Hosting/Windows/ServiceNative.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System.Runtime.InteropServices; -using System.Runtime.Versioning; - -namespace Chronos.Server.Hosting.Windows; - -/// Service Control Manager and event log functions of advapi32 (no Windows-only package needed). -[SupportedOSPlatform("windows")] -internal static unsafe partial class ServiceNative -{ - public const uint ServiceWin32OwnProcess = 0x10; - public const uint ServiceAutoStart = 2; - public const uint ServiceErrorNormal = 1; - public const uint ScManagerConnect = 0x1; - public const uint ScManagerCreateService = 0x2; - public const uint ServiceAllAccess = 0xF01FF; - public const uint ServiceQueryStatus = 0x4; - public const uint ServiceStop = 0x20; - public const uint Delete = 0x10000; - public const uint ServiceConfigDescription = 1; - - public const uint ControlStop = 1; - public const uint ControlInterrogate = 4; - public const uint ControlShutdown = 5; - - public const int ErrorFailedServiceControllerConnect = 1063; - public const int ErrorServiceNotActive = 1062; - public const int ErrorServiceMarkedForDelete = 1072; - public const uint NoError = 0; - public const uint ErrorCallNotImplemented = 120; - public const uint ErrorExceptionInService = 1064; - - public const ushort EventLogErrorType = 0x1; - - [StructLayout(LayoutKind.Sequential)] - public struct ServiceStatus - { - public uint ServiceType; - public uint CurrentState; - public uint ControlsAccepted; - public uint Win32ExitCode; - public uint ServiceSpecificExitCode; - public uint CheckPoint; - public uint WaitHint; - } - - [StructLayout(LayoutKind.Sequential)] - public struct ServiceTableEntry - { - public char* ServiceName; - public delegate* unmanaged[Stdcall] ServiceProc; - } - - [StructLayout(LayoutKind.Sequential)] - public struct ServiceDescription - { - public char* Description; - } - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool StartServiceCtrlDispatcherW(ServiceTableEntry* table); - - [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] - public static partial nint RegisterServiceCtrlHandlerExW(string serviceName, delegate* unmanaged[Stdcall] handler, nint context); - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool SetServiceStatus(nint statusHandle, ServiceStatus* status); - - [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] - public static partial nint OpenSCManagerW(string? machineName, string? databaseName, uint desiredAccess); - - [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] - public static partial nint CreateServiceW(nint manager, string serviceName, string displayName, uint desiredAccess, uint serviceType, uint startType, - uint errorControl, string binaryPathName, string? loadOrderGroup, nint tagId, string? dependencies, string? serviceStartName, string? password); - - [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] - public static partial nint OpenServiceW(nint manager, string serviceName, uint desiredAccess); - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool ChangeServiceConfig2W(nint service, uint infoLevel, void* info); - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool ControlService(nint service, uint control, ServiceStatus* status); - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool DeleteService(nint service); - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool CloseServiceHandle(nint handle); - - [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] - public static partial nint RegisterEventSourceW(string? uncServerName, string sourceName); - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool ReportEventW(nint eventLog, ushort type, ushort category, uint eventId, nint userSid, ushort stringCount, uint dataSize, - char** strings, void* rawData); - - [LibraryImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static partial bool DeregisterEventSource(nint eventLog); -} diff --git a/src/Chronos.Server/Hosting/Windows/ServiceStatusTracker.cs b/src/Chronos.Server/Hosting/Windows/ServiceStatusTracker.cs deleted file mode 100644 index 548aafeb..00000000 --- a/src/Chronos.Server/Hosting/Windows/ServiceStatusTracker.cs +++ /dev/null @@ -1,68 +0,0 @@ -namespace Chronos.Server.Hosting.Windows; - -/// SERVICE_STATUS.dwCurrentState values the server reports. -public enum ServiceState : uint -{ - Stopped = 1, - StartPending = 2, - StopPending = 3, - Running = 4, -} - -/// One status report for SetServiceStatus. -public readonly record struct ServiceStatusReport(ServiceState State, uint ControlsAccepted, uint CheckPoint, uint WaitHintMilliseconds, uint ExitCode); - -/// -/// The service's state machine and the rules of SERVICE_STATUS: no controls are accepted while pending, the check point -/// grows with every pending report and is zero otherwise, a stopped service carries its exit code. -/// -public sealed class ServiceStatusTracker -{ - public const uint AcceptStop = 0x1; - public const uint AcceptShutdown = 0x4; - - private readonly Lock _gate = new(); - private uint _checkPoint; - - public ServiceStatusReport Current { get; private set; } = new(ServiceState.Stopped, 0, 0, 0, 0); - - public ServiceStatusReport StartPending(uint waitHintMilliseconds) => Pending(ServiceState.StartPending, waitHintMilliseconds, ServiceState.Stopped, ServiceState.StartPending); - - public ServiceStatusReport Running() - { - lock (_gate) - { - Require(ServiceState.StartPending); - _checkPoint = 0; - return Current = new ServiceStatusReport(ServiceState.Running, AcceptStop | AcceptShutdown, 0, 0, 0); - } - } - - /// Stopping from start-up or from running; repeating it reports progress (a higher check point). - public ServiceStatusReport StopPending(uint waitHintMilliseconds) => - Pending(ServiceState.StopPending, waitHintMilliseconds, ServiceState.StartPending, ServiceState.Running, ServiceState.StopPending); - - public ServiceStatusReport Stopped(uint exitCode) - { - lock (_gate) - { - _checkPoint = 0; - return Current = new ServiceStatusReport(ServiceState.Stopped, 0, 0, 0, exitCode); - } - } - - private ServiceStatusReport Pending(ServiceState state, uint waitHintMilliseconds, params ServiceState[] from) - { - lock (_gate) - { - Require(from); - _checkPoint = Current.State == state ? _checkPoint + 1 : 1; - return Current = new ServiceStatusReport(state, 0, _checkPoint, waitHintMilliseconds, 0); - } - } - - private void Require(params ServiceState[] allowed) - { - if (!allowed.Contains(Current.State)) throw new InvalidOperationException($"A service cannot go from {Current.State} to that state."); - } -} diff --git a/src/Chronos.Server/Hosting/Windows/WindowsServiceHost.cs b/src/Chronos.Server/Hosting/Windows/WindowsServiceHost.cs deleted file mode 100644 index 4da49b6e..00000000 --- a/src/Chronos.Server/Hosting/Windows/WindowsServiceHost.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System.ComponentModel; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.Versioning; -using static Chronos.Server.Hosting.Windows.ServiceNative; - -namespace Chronos.Server.Hosting.Windows; - -/// -/// Runs the server as a Windows service with the SCM API directly (StartServiceCtrlDispatcherW, -/// RegisterServiceCtrlHandlerExW, SetServiceStatus). Stop and shutdown requests stop the web host gracefully: running -/// jobs keep their checkpoints and resume at the next start. A failure to start or stop goes to the Application event -/// log (), since a service has no console. -/// -[SupportedOSPlatform("windows")] -public static class WindowsServiceHost -{ - public const string ServiceName = "ChronosServer"; - private const uint StartWaitHint = 60_000; - private const uint StopWaitHint = 35_000; - - private static readonly ServiceStatusTracker Status = new(); - private static Func? _build; - private static WebApplication? _app; - private static nint _statusHandle; - - /// - /// Hands the process to the Service Control Manager and returns when the service has stopped. False when the - /// process was not started by the SCM (for example from a console). - /// - public static unsafe bool Run(Func build) - { - _build = build; - fixed (char* name = ServiceName) - { - var table = stackalloc ServiceTableEntry[2]; - table[0] = new ServiceTableEntry { ServiceName = name, ServiceProc = &ServiceMain }; - table[1] = default; - if (StartServiceCtrlDispatcherW(table)) return true; - } - var error = Marshal.GetLastPInvokeError(); - if (error == ErrorFailedServiceControllerConnect) return false; - throw new Win32Exception(error); - } - - [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] - private static unsafe void ServiceMain(uint argc, char** argv) - { - _statusHandle = RegisterServiceCtrlHandlerExW(ServiceName, &Handler, 0); - if (_statusHandle == 0) return; - Report(Status.StartPending(StartWaitHint)); - try - { - _app = _build!(); - _app.StartAsync().GetAwaiter().GetResult(); - Report(Status.Running()); - } - catch (Exception ex) - { - // A refused data folder or server.json says which entry to check; the SCM itself records only error 1064. - Console.Error.WriteLine(ex); - ServiceEventLog.Error(ServiceEventLog.StartFailed, ServiceEventLog.Describe("Chronos Server did not start:", ex)); - Report(Status.Stopped(ErrorExceptionInService)); - } - } - - [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] - private static unsafe uint Handler(uint control, uint eventType, nint eventData, nint context) - { - switch (control) - { - case ControlStop or ControlShutdown: - if (Status.Current.State is ServiceState.Running or ServiceState.StartPending) - { - Report(Status.StopPending(StopWaitHint)); - // The handler must return at once; the host stops on the thread pool. - _ = Task.Run(StopAsync); - } - return NoError; - case ControlInterrogate: - Report(Status.Current); - return NoError; - default: - return ErrorCallNotImplemented; - } - } - - private static async Task StopAsync() - { - uint exitCode = 0; - try - { - if (_app is { } app) - { - await app.StopAsync(); - await app.DisposeAsync(); - } - } - catch (Exception ex) - { - Console.Error.WriteLine(ex); - ServiceEventLog.Error(ServiceEventLog.StopFailed, ServiceEventLog.Describe("Chronos Server did not stop cleanly:", ex)); - exitCode = ErrorExceptionInService; - } - Report(Status.Stopped(exitCode)); - } - - private static unsafe void Report(ServiceStatusReport report) - { - var status = new ServiceStatus - { - ServiceType = ServiceWin32OwnProcess, - CurrentState = (uint)report.State, - ControlsAccepted = report.ControlsAccepted, - Win32ExitCode = report.ExitCode, - CheckPoint = report.CheckPoint, - WaitHint = report.WaitHintMilliseconds, - }; - SetServiceStatus(_statusHandle, &status); - } -} diff --git a/src/Chronos.Server/Jobs/JobEngineHost.cs b/src/Chronos.Server/Jobs/JobEngineHost.cs index e77f3fc1..5cbac8f6 100644 --- a/src/Chronos.Server/Jobs/JobEngineHost.cs +++ b/src/Chronos.Server/Jobs/JobEngineHost.cs @@ -16,21 +16,24 @@ namespace Chronos.Server.Jobs; /// running one, in the order of the priority queue of the settings (owner review 1). Render jobs run here only when the registered render /// handler accepts them (: managed formats, and MP4 jobs no render node can take); /// render nodes claim the others (BRIEF §14.5, §14.7). On shutdown running jobs stay marked running and are requeued from their checkpoints at the next start. +/// When Chronos Desktop shares its library (owner review 5, #67) the engine is the desktop's own: this host relays it while the +/// server runs, never starts, stops or disposes it, and the jobs API shows only the kinds a server runs (): +/// the desktop's workspace, sync and download jobs stay its own. /// public sealed class JobEngineHost : IHostedService { private static readonly long ProgressInterval = Stopwatch.Frequency / 4; - private readonly ChronosDatabase _db; private readonly EventHub _events; private readonly HashSet _kinds; private readonly Dictionary _lastProgress = []; private readonly Lock _gate = new(); + private readonly bool _ownsEngine = true; + private int _subscribed; public JobEngineHost(ChronosDatabase db, IEnumerable handlers, IEnumerable providers, ServerOptions options, EventHub events) { - _db = db; _events = events; // A render handler must be selective: a plain one would take the jobs that need a render node's encoder. var local = handlers.Concat(providers.SelectMany(p => p.CreateJobHandlers())).Where(h => h.Kind != JobKind.Render || h is ISelectiveJobHandler).ToList(); @@ -42,8 +45,20 @@ public sealed class JobEngineHost : IHostedService Kinds = _kinds, PriorityQueue = () => ServerSettingsStore.PriorityQueue(options), }); - Engine.JobChanged += OnJobChanged; - Engine.Progress += OnProgress; + Subscribe(); + } + + /// + /// Relays , an engine Chronos Desktop runs (owner review 5, #67), from to + /// ; the engine stays the desktop's. + /// + public JobEngineHost(JobEngine engine, EventHub events) + { + ArgumentNullException.ThrowIfNull(engine); + _events = events; + _ownsEngine = false; + _kinds = []; + Engine = engine; } /// Node id stamped on jobs this server runs. @@ -51,15 +66,60 @@ public sealed class JobEngineHost : IHostedService public JobEngine Engine { get; } + /// The engine is the one Chronos Desktop runs, which shares its library (owner review 5, #67). + public bool Hosted => !_ownsEngine; + /// True when a job of has somewhere to run: a local handler, or render nodes for renders. - public bool CanRun(JobKind kind) => kind == JobKind.Render || _kinds.Contains(kind); + public bool CanRun(JobKind kind) => kind == JobKind.Render || (_ownsEngine ? _kinds.Contains(kind) : Engine.CanRun(kind) && Exposes(kind)); - public Task StartAsync(CancellationToken cancellationToken) => Engine.StartAsync(cancellationToken); + /// + /// Whether the jobs API lists, shows and acts on jobs of : every kind on a server of its own; on a + /// shared desktop library only the kinds a server runs (), so a client never sees or + /// cancels the desktop's own workspace, sync and download jobs (owner review 5, #67). + /// + public bool Exposes(JobKind kind) => _ownsEngine || ServerSettingsStore.Kinds.Contains(kind); - public async Task StopAsync(CancellationToken cancellationToken) => await Engine.DisposeAsync(); + public Task StartAsync(CancellationToken cancellationToken) + { + if (_ownsEngine) return Engine.StartAsync(cancellationToken); + Subscribe(); + return Task.CompletedTask; + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + if (_ownsEngine) + { + await Engine.DisposeAsync(); + return; + } + // The desktop's engine runs on: only this server stops following it. + if (Interlocked.Exchange(ref _subscribed, 0) == 1) + { + Engine.JobChanged -= OnJobChanged; + Engine.Progress -= OnProgress; + } + } + + private void Subscribe() + { + if (Interlocked.Exchange(ref _subscribed, 1) == 1) return; + Engine.JobChanged += OnJobChanged; + Engine.Progress += OnProgress; + } + + /// + /// A job's state changed outside the engine (render nodes, cancellation of node jobs): Chronos Desktop's Jobs section + /// follows these as it follows its engine's own changes while it shares its library (owner review 5, #67). + /// + public event Action? ChangedOutside; /// Publishes a state change made outside the engine (render nodes, cancellation of node jobs). - public void Publish(Job job) => OnJobChanged(job); + public void Publish(Job job) + { + OnJobChanged(job); + ChangedOutside?.Invoke(job); + } private void OnJobChanged(Job job) { diff --git a/src/Chronos.Server/Jobs/SchedulerHost.cs b/src/Chronos.Server/Jobs/SchedulerHost.cs index 8d8ae5e8..f5dd3a2e 100644 --- a/src/Chronos.Server/Jobs/SchedulerHost.cs +++ b/src/Chronos.Server/Jobs/SchedulerHost.cs @@ -19,7 +19,10 @@ namespace Chronos.Server.Jobs; /// retention in force is the audit clean-up schedule's age limit, 0 while it is off or without its audit step: /// Settings › Logs shows it () and sets it (); /// is only what the schedule is seeded and reset with, or what an environment -/// variable fixes. Every schedule change and run goes out as a . +/// variable fixes. Every schedule change and run goes out as a . On the library Chronos +/// Desktop shares (owner review 5, #67) the scheduler is the desktop's, already running: this host follows it while the +/// server runs, adds the server's own system schedules to it () and never +/// starts, seeds the desktop's schedules for or disposes it. /// public sealed class SchedulerHost : IHostedService { @@ -29,6 +32,10 @@ public sealed class SchedulerHost : IHostedService private readonly CancellationTokenSource _stopping = new(); private Task _startScan = Task.CompletedTask; private Task _refresh = Task.CompletedTask; + private readonly bool _ownsScheduler = true; + private readonly Action _changed; + private readonly Action _deleted; + private readonly int _auditRetentionDays; private int _stopped; public SchedulerHost(ChronosDatabase db, JobEngineHost jobs, ServerScheduleHost host, EventHub events, TimeProvider time, ILogger logger) @@ -41,8 +48,31 @@ public sealed class SchedulerHost : IHostedService Host = host; Scheduler = new Scheduler(db, jobs.Engine, new SchedulerOptions { Time = time }); // The Scheduled tab of the admin panel follows them, the runs that queue no job included. - Scheduler.ScheduleChanged += schedule => events.Publish(new ScheduleChangedEvent(schedule.Id, Deleted: false)); - Scheduler.ScheduleDeleted += id => events.Publish(new ScheduleChangedEvent(id, Deleted: true)); + _changed = schedule => events.Publish(new ScheduleChangedEvent(schedule.Id, Deleted: false)); + _deleted = id => events.Publish(new ScheduleChangedEvent(id, Deleted: true)); + Scheduler.ScheduleChanged += _changed; + Scheduler.ScheduleDeleted += _deleted; + } + + /// + /// Follows , the running scheduler of Chronos Desktop (owner review 5, #67), from + /// to . + /// + public SchedulerHost(ChronosDatabase db, Scheduler scheduler, ServerScheduleHost host, ServerOptions options, EventHub events, TimeProvider time, + ILogger logger) + { + ArgumentNullException.ThrowIfNull(scheduler); + ArgumentNullException.ThrowIfNull(options); + ArgumentNullException.ThrowIfNull(events); + _db = db; + _time = time; + _logger = logger; + _ownsScheduler = false; + _auditRetentionDays = options.AuditRetentionDays; + Host = host; + Scheduler = scheduler; + _changed = schedule => events.Publish(new ScheduleChangedEvent(schedule.Id, Deleted: false)); + _deleted = id => events.Publish(new ScheduleChangedEvent(id, Deleted: true)); } public Scheduler Scheduler { get; } @@ -51,10 +81,21 @@ public sealed class SchedulerHost : IHostedService public async Task StartAsync(CancellationToken cancellationToken) { - var started = Scheduler.Options.Time.GetUtcNow(); - await Scheduler.StartAsync(Host, ApplyLockedAgesAsync, cancellationToken); var stopping = _stopping.Token; - _startScan = Task.Run(() => ScanAtStartAsync(started, stopping), CancellationToken.None); + if (!_ownsScheduler) + { + Scheduler.ScheduleChanged += _changed; + Scheduler.ScheduleDeleted += _deleted; + // The desktop seeds its own system schedules; the server's state brings two more (never overwritten once there). + if (await _db.Schedules.EnsureSystemAsync(ServerScheduleHost.ShareSchedules(_auditRetentionDays), _time.GetUtcNow(), cancellationToken) > 0) + Scheduler.Wake(); + } + else + { + var started = Scheduler.Options.Time.GetUtcNow(); + await Scheduler.StartAsync(Host, ApplyLockedAgesAsync, cancellationToken); + _startScan = Task.Run(() => ScanAtStartAsync(started, stopping), CancellationToken.None); + } // Created here, so its first period counts from the start. var timer = new PeriodicTimer(TimeSpan.FromMinutes(ServerScheduleHost.ScanMinutes), _time); _refresh = Task.Run(() => RefreshArchiveAsync(timer, stopping), CancellationToken.None); @@ -66,7 +107,16 @@ public sealed class SchedulerHost : IHostedService await _stopping.CancelAsync(); await _startScan; await _refresh; - await Scheduler.DisposeAsync(); + if (_ownsScheduler) + { + await Scheduler.DisposeAsync(); + } + else + { + // The desktop's scheduler runs on: only this server stops following it. + Scheduler.ScheduleChanged -= _changed; + Scheduler.ScheduleDeleted -= _deleted; + } _stopping.Dispose(); } diff --git a/src/Chronos.Server/Jobs/ServerScheduleHost.cs b/src/Chronos.Server/Jobs/ServerScheduleHost.cs index 0cffda9a..9a37aa86 100644 --- a/src/Chronos.Server/Jobs/ServerScheduleHost.cs +++ b/src/Chronos.Server/Jobs/ServerScheduleHost.cs @@ -66,13 +66,13 @@ public sealed class ServerScheduleHost : IScheduleHost { get { - var nightly = (Backup: new TimeOnly(3, 0), History: new TimeOnly(4, 0), Audit: new TimeOnly(4, 30)); + var nightly = (Backup: new TimeOnly(3, 0), History: new TimeOnly(4, 0)); + var share = ShareSchedules(_options.AuditRetentionDays); var list = new List { // A missed scan is not caught up: the next one comes within minutes and SchedulerHost scans once at start. new(ArchiveScan, "Archive scan", ScheduleActions.Scan, ScheduleRecurrence.Interval(ScanMinutes, RecurrenceUnit.Minutes), CatchUp: false), - new(UploadCleanup, "Upload clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.Interval(UploadSweepHours, RecurrenceUnit.Hours), - new ScheduleParametersDto(OlderThanDays: UploadMaintenance.FinishedRetentionDays, Steps: [UploadCleanupStep.StepName])), + share[0], }; // PostgreSQL is backed up with its own tools (BRIEF §6.2): no schedule that could never run. Seeded due at once // when the newest copy is a day old, as the daily loop it replaced made one at start. @@ -83,13 +83,24 @@ public sealed class ServerScheduleHost : IScheduleHost } list.Add(new(Housekeeping, "Housekeeping", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(nightly.History), new ScheduleParametersDto(OlderThanDays: _options.JobHistoryDays, Steps: [Chronos.Data.Housekeeping.CleanupSteps.JobHistory]))); - var audit = _options.AuditRetentionDays; - list.Add(new(AuditCleanup, "Audit log clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(nightly.Audit), - new ScheduleParametersDto(OlderThanDays: audit, Steps: [Chronos.Data.Housekeeping.CleanupSteps.Audit]), Enabled: audit > 0)); + list.Add(share[1]); return list; } } + /// + /// The system schedules of a server's own state, which Chronos Desktop adds to its own while it shares its library + /// (owner review 5, #67), the archive scan, the index backup and the housekeeping being the desktop's: the uploads sweep + /// every hours and the audit log clean-up, nightly at 04:30, off at 0 days. + /// + public static IReadOnlyList ShareSchedules(int auditRetentionDays) => + [ + new(UploadCleanup, "Upload clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.Interval(UploadSweepHours, RecurrenceUnit.Hours), + new ScheduleParametersDto(OlderThanDays: UploadMaintenance.FinishedRetentionDays, Steps: [UploadCleanupStep.StepName])), + new(AuditCleanup, "Audit log clean-up", ScheduleActions.Cleanup, ScheduleRecurrence.DailyAt(new TimeOnly(4, 30)), + new ScheduleParametersDto(OlderThanDays: auditRetentionDays, Steps: [Chronos.Data.Housekeeping.CleanupSteps.Audit]), Enabled: auditRetentionDays > 0), + ]; + /// /// The served archive, after taking the settings another writer sharing it (a desktop) put into its archive.json, as /// the former polling did; while it is offline, or the file cannot be read, as the index has it. diff --git a/src/Chronos.Server/Program.cs b/src/Chronos.Server/Program.cs deleted file mode 100644 index 7c19fafd..00000000 --- a/src/Chronos.Server/Program.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System.ComponentModel; -using Chronos.Server.Configuration; -using Chronos.Server.Hosting; -using Chronos.Server.Hosting.Windows; -using Chronos.Server.Storage; - -var command = ServerCommandLine.Parse(args); -if (command.Error is not null) -{ - await Console.Error.WriteLineAsync(command.Error); - return 2; -} - -switch (command.Command) -{ - case ServerCommand.Help: - Console.WriteLine(ServerCommandLine.Usage); - return 0; - case ServerCommand.HealthCheck: - return await HealthProbe.RunAsync(); - case ServerCommand.Service or ServerCommand.InstallService or ServerCommand.UninstallService when !OperatingSystem.IsWindows(): - await Console.Error.WriteLineAsync("Windows services are available on Windows only."); - return 2; - case ServerCommand.InstallService when OperatingSystem.IsWindows(): - return ServiceAction(ServiceInstaller.Install, $"Installed the {WindowsServiceHost.ServiceName} service."); - case ServerCommand.UninstallService when OperatingSystem.IsWindows(): - return ServiceAction(ServiceInstaller.Uninstall, $"Removed the {WindowsServiceHost.ServiceName} service."); - case ServerCommand.Service when OperatingSystem.IsWindows(): - if (WindowsServiceHost.Run(() => ServerHost.Build(ServerOptions.Load(), [.. command.HostArguments], RegisterModules))) return 0; - await Console.Error.WriteLineAsync("--service is used by the Service Control Manager; start the service with 'sc start ChronosServer'."); - return 1; - default: - var app = ServerHost.Build(ServerOptions.Load(), [.. command.HostArguments], RegisterModules); - await app.RunAsync(); - return 0; -} - -// The archive engine, then analysis and managed rendering over it. -static void RegisterModules(IServiceCollection services) -{ - ArchiveModule.Register(services); - ProcessingModule.Register(services); -} - -static int ServiceAction(Action action, string done) -{ - try - { - action(); - Console.WriteLine(done); - return 0; - } - catch (Exception ex) when (ex is Win32Exception or InvalidOperationException) - { - // Win32: the Service Control Manager's reason; InvalidOperation: a folder the service must not run from. - Console.Error.WriteLine(ex.Message); - return 1; - } -} - -/// Entry point type, visible to the in-process test host. -public partial class Program; diff --git a/src/Chronos.Server/Security/AdminBootstrap.cs b/src/Chronos.Server/Security/AdminBootstrap.cs index bdb69966..3ddfe773 100644 --- a/src/Chronos.Server/Security/AdminBootstrap.cs +++ b/src/Chronos.Server/Security/AdminBootstrap.cs @@ -16,7 +16,8 @@ namespace Chronos.Server.Security; public static class AdminBootstrap { public const string DefaultUsername = "admin"; - private const string PasswordFileName = "initial-admin-password.txt"; + /// The file in the data folder that holds a generated temporary password until an administrator sets their own. + public const string PasswordFileName = "initial-admin-password.txt"; public static async Task RunAsync(ChronosDatabase db, ServerOptions options, TimeProvider time, ILogger logger, CancellationToken cancellationToken) { diff --git a/src/Chronos.Server/Security/AuthenticationMiddleware.cs b/src/Chronos.Server/Security/AuthenticationMiddleware.cs index 322801dc..834c4585 100644 --- a/src/Chronos.Server/Security/AuthenticationMiddleware.cs +++ b/src/Chronos.Server/Security/AuthenticationMiddleware.cs @@ -6,12 +6,15 @@ namespace Chronos.Server.Security; /// /// Resolves the caller of every /api request (bearer token or admin cookie) and enforces the endpoint's /// : 401 without a valid token, 403 for a missing permission, a missing CSRF header on a -/// cookie request, or a pending password change. Runs after routing, so the endpoint's metadata is known. +/// cookie request, or a pending password change. Runs after routing, so the endpoint's metadata is known. Every request it +/// lets through is noted in (owner review 5, #67). /// public sealed class AuthenticationMiddleware(RequestDelegate next, TokenService tokens) { - public async Task InvokeAsync(HttpContext context) + public async Task InvokeAsync(HttpContext context, ClientActivity activity) { + ArgumentNullException.ThrowIfNull(context); + ArgumentNullException.ThrowIfNull(activity); var rule = context.GetEndpoint()?.Metadata.GetMetadata(); if (rule is null && !context.Request.Path.StartsWithSegments("/api")) { @@ -48,6 +51,8 @@ public sealed class AuthenticationMiddleware(RequestDelegate next, TokenService return; } context.Features.Set(caller); + var address = context.Connection.RemoteIpAddress; + activity.Record(caller, (address is { IsIPv4MappedToIPv6: true } ? address.MapToIPv4() : address)?.ToString()); await next(context); } diff --git a/src/Chronos.Server/Security/ClientActivity.cs b/src/Chronos.Server/Security/ClientActivity.cs new file mode 100644 index 00000000..6994b414 --- /dev/null +++ b/src/Chronos.Server/Security/ClientActivity.cs @@ -0,0 +1,42 @@ +using System.Collections.Concurrent; + +namespace Chronos.Server.Security; + +/// A program that used the server lately: its account, its name (the token's client), its address and when. +public sealed record ClientSeen(string User, string Client, string Address, DateTimeOffset LastSeen); + +/// +/// The programs that used the server in the last (owner review 5, #67): Chronos Desktop shows them in +/// its Server section while it shares its library, and asks before it stops sharing while there are any. Kept in memory +/// only; records every signed-in request. +/// +public sealed class ClientActivity(TimeProvider time) +{ + /// How long a program counts as active after its last request. + public static TimeSpan Window { get; } = TimeSpan.FromMinutes(5); + + /// What a request signed in through the admin panel's cookie shows as its program. + public const string AdminPanel = "Admin panel"; + + private readonly ConcurrentDictionary<(Guid User, string Client, string Address), ClientSeen> _seen = new(); + + /// A signed-in request of from . + public void Record(Caller caller, string? address) + { + ArgumentNullException.ThrowIfNull(caller); + var client = caller.Source == CredentialSource.Cookie ? AdminPanel : string.IsNullOrWhiteSpace(caller.ClientName) ? "?" : caller.ClientName.Trim(); + var from = string.IsNullOrEmpty(address) ? "?" : address; + _seen[(caller.Id, client, from)] = new ClientSeen(caller.User.Username, client, from, time.GetUtcNow()); + } + + /// The programs seen in the last , the latest first; older entries are dropped. + public IReadOnlyList Recent() + { + var since = time.GetUtcNow() - Window; + foreach (var (key, seen) in _seen) + { + if (seen.LastSeen < since) _seen.TryRemove(new KeyValuePair<(Guid, string, string), ClientSeen>(key, seen)); + } + return [.. _seen.Values.Where(s => s.LastSeen >= since).OrderByDescending(s => s.LastSeen)]; + } +} diff --git a/src/Chronos.Server/Storage/ArchiveModule.cs b/src/Chronos.Server/Storage/ArchiveModule.cs index 7776227b..6aeddd69 100644 --- a/src/Chronos.Server/Storage/ArchiveModule.cs +++ b/src/Chronos.Server/Storage/ArchiveModule.cs @@ -23,8 +23,6 @@ namespace Chronos.Server.Storage; /// public static class ArchiveModule { - /// The change a conversion's Replace tells about a sequence (sequenceChanged). - private const string ConvertedChange = "converted"; /// Name the server writes into the archive's lease files. public static string Owner { get; } = "server@" + Environment.MachineName; @@ -40,24 +38,10 @@ public static class ArchiveModule // Photos the running job processes at once: the settings' value as it is when each sequence starts (owner review 1). var service = new ArchiveService(db, new ArchiveServiceOptions { - // A Reorganize or a conversion tells connected clients of every sequence it changed or made (BRIEF §14.3). - SequenceChanged = async (id, change, ct) => - { - if (await db.Sequences.GetAsync(id, ct) is { } sequence) - events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion)); - // A conversion may leave settings Main has not taken yet (owner review 2, R8). - if (change == ConvertedChange) sp.GetService()?.Touch(id); - }, - // Every change of a sequence's projects reaches connected clients (owner review 2, R8): a workspace with that - // project checked out knows its home moved, the admin panel lists the projects again. Main's head written into the - // sequence's settings is a settings change for the clients that read those (an older client, a desktop's sync). - ProjectsChanged = async (changed, ct) => - { - foreach (var change in changed.Changes) - events.Publish(new ProjectChangedEvent(changed.SequenceId, change.ProjectId, change.Change, change.HeadNumber, change.HeadVersionId)); - if (changed.SettingsChanged && await db.Sequences.GetAsync(changed.SequenceId, ct) is { } sequence) - events.Publish(new SequenceChangedEvent(sequence.Id, "settings", sequence.FramesVersion, sequence.SettingsVersion)); - }, + // A Reorganize or a conversion tells connected clients of every sequence it changed or made (BRIEF §14.3), and + // every change of a sequence's projects reaches them too (owner review 2, R8). + SequenceChanged = (id, change, ct) => ServerEventBridge.SequenceChangedAsync(db, events, sp.GetService(), id, change, ct), + ProjectsChanged = (changed, ct) => ServerEventBridge.ProjectsChangedAsync(db, events, changed, ct), Decoder = DecoderFactory.CreateServer(), // Where a conversion writes (owner review 2, R7): the tighter of what the panel shows as the archive's free space // (R2: the space path's volume) and the volume the archive's folders are on, which takes every byte it stages diff --git a/src/Chronos.Server/Storage/ProcessingModule.cs b/src/Chronos.Server/Storage/ProcessingModule.cs index 5314e46b..e46f485e 100644 --- a/src/Chronos.Server/Storage/ProcessingModule.cs +++ b/src/Chronos.Server/Storage/ProcessingModule.cs @@ -45,13 +45,8 @@ public static class ProcessingModule Host = EncoderHost.Server, AwaitsRenderNode = sp.GetRequiredService().AwaitsNodeAsync, ProducedBy = options.ServerName, - SequenceChanged = async (id, change, ct) => - { - if (await db.Sequences.GetAsync(id, ct) is { } sequence) - events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion)); - // Auto values an analysis wrote into the settings become Main's next version now (owner review 2, R8). - if (change == ProcessingOptions.SettingsChange) sp.GetService()?.Touch(id); - }, + // Auto values an analysis wrote into the settings become Main's next version now (owner review 2, R8). + SequenceChanged = (id, change, ct) => ServerEventBridge.SequenceChangedAsync(db, events, sp.GetService(), id, change, ct), }); }); services.AddSingleton(sp => sp.GetRequiredService()); diff --git a/src/Chronos.Server/Storage/ServerArchive.cs b/src/Chronos.Server/Storage/ServerArchive.cs index 8b48633a..e0cf658e 100644 --- a/src/Chronos.Server/Storage/ServerArchive.cs +++ b/src/Chronos.Server/Storage/ServerArchive.cs @@ -45,6 +45,12 @@ public sealed class ServerArchive(Archive archive) public Guid Id => _current.Id; + /// + /// Whether is one of this archive's (owner review 5, #67): an index may hold other archives, + /// whose items the API treats as unknown (). + /// + public bool Owns(Sequence? sequence) => sequence is not null && sequence.ArchiveId == Id; + public string Root => _current.RootPath; /// @@ -87,6 +93,18 @@ public sealed class ServerArchive(Archive archive) /// (and relinked if it moved), any other folder becomes a new archive with the default templates. Either way the /// folders of the archive's layout are made (). /// + /// + /// Serves , an archive Chronos Desktop already opened (owner review 5, #67): nothing is adopted, + /// the folders of its layout are made as at a server's start (). + /// + public static ServerArchive ForArchive(Archive archive) + { + ArgumentNullException.ThrowIfNull(archive); + var served = new ServerArchive(archive); + served.CheckStructure(); + return served; + } + public static async Task OpenAsync(ChronosDatabase db, ServerOptions options, CancellationToken cancellationToken) { var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(options.ArchivePath)); diff --git a/src/Chronos.Server/Storage/ServerEventBridge.cs b/src/Chronos.Server/Storage/ServerEventBridge.cs new file mode 100644 index 00000000..5715812b --- /dev/null +++ b/src/Chronos.Server/Storage/ServerEventBridge.cs @@ -0,0 +1,48 @@ +using Chronos.Data; +using Chronos.Data.Processing; +using Chronos.Data.Projects; +using Chronos.Protocol; +using Chronos.Server.Events; + +namespace Chronos.Server.Storage; + +/// +/// What the archive and processing engines tell about sequences and projects, as events for connected clients (BRIEF +/// §14.3; owner review 2, R8): the same on a server of its own (, ) +/// and on the library Chronos Desktop shares, whose engines are the desktop's (owner review 5, #67). +/// +public static class ServerEventBridge +{ + /// The change a conversion's Replace tells about a sequence (sequenceChanged). + public const string ConvertedChange = "converted"; + + /// + /// A Reorganize, a conversion, a render or an analysis changed or made sequence : clients are told + /// (sequenceChanged), and a conversion's or an analysis's settings become Main's next version soon. + /// + public static async Task SequenceChangedAsync(ChronosDatabase db, EventHub events, ProjectCatchUp? projects, Guid id, string change, CancellationToken ct) + { + ArgumentNullException.ThrowIfNull(db); + ArgumentNullException.ThrowIfNull(events); + if (await db.Sequences.GetAsync(id, ct).ConfigureAwait(false) is { } sequence) + events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion)); + // A conversion may leave settings Main has not taken yet, and an analysis writes Auto values (owner review 2, R8). + if (change is ConvertedChange or ProcessingOptions.SettingsChange) projects?.Touch(id); + } + + /// + /// Every change of a sequence's projects reaches connected clients (owner review 2, R8): a workspace with that project + /// checked out knows its home moved, the admin panel lists the projects again. Main's head written into the sequence's + /// settings is a settings change for the clients that read those (an older client, a desktop's sync). + /// + public static async Task ProjectsChangedAsync(ChronosDatabase db, EventHub events, ProjectChangeSet changed, CancellationToken ct) + { + ArgumentNullException.ThrowIfNull(db); + ArgumentNullException.ThrowIfNull(events); + ArgumentNullException.ThrowIfNull(changed); + foreach (var change in changed.Changes) + events.Publish(new ProjectChangedEvent(changed.SequenceId, change.ProjectId, change.Change, change.HeadNumber, change.HeadVersionId)); + if (changed.SettingsChanged && await db.Sequences.GetAsync(changed.SequenceId, ct).ConfigureAwait(false) is { } sequence) + events.Publish(new SequenceChangedEvent(sequence.Id, "settings", sequence.FramesVersion, sequence.SettingsVersion)); + } +} diff --git a/tests/Chronos.App.Tests/Server/ConnectFlowTests.cs b/tests/Chronos.App.Tests/Server/ConnectFlowTests.cs index 1e492f39..e67fb7a0 100644 --- a/tests/Chronos.App.Tests/Server/ConnectFlowTests.cs +++ b/tests/Chronos.App.Tests/Server/ConnectFlowTests.cs @@ -41,6 +41,27 @@ public sealed class ConnectFlowTests Assert.Contains(Fingerprint, server.Created[^1]); } + /// Owner review 5, #67: this PC's own shared library is refused before the trust question, and once trusted. + [Test] + public async Task ThisPcsOwnSharedLibraryIsRefused() + { + var server = new FakeServer { SelfSigned = true }; + var trust = new MemoryTrust(); + using var flow = new ConnectFlow(server, trust) { IsOwnLibrary = (fingerprint, _) => fingerprint == Fingerprint }; + Assert.Equal(ConnectStep.Failed, await flow.ProbeAsync("localhost:8443")); + Assert.Equal(ConnectError.OwnLibrary, flow.Error); + Assert.Null(flow.PendingFingerprint); + Assert.Empty(trust.Fingerprints); + + trust.Trust("https://localhost:8443", Fingerprint); + Assert.Equal(ConnectStep.Failed, await flow.ProbeAsync("localhost:8443")); + Assert.Equal(ConnectError.OwnLibrary, flow.Error); + Assert.Equal(0, server.Logins); + + flow.IsOwnLibrary = (_, _) => false; + Assert.Equal(ConnectStep.AwaitingCredentials, await flow.ProbeAsync("localhost:8443")); + } + [Test] public async Task RejectingTheCertificateSendsNothing() { diff --git a/tests/Chronos.App.Tests/Server/LibraryShareTests.cs b/tests/Chronos.App.Tests/Server/LibraryShareTests.cs new file mode 100644 index 00000000..3bb06529 --- /dev/null +++ b/tests/Chronos.App.Tests/Server/LibraryShareTests.cs @@ -0,0 +1,192 @@ +using System.IO; +using System.Net; +using System.Net.Http; +using System.Net.Sockets; +using Chronos.App.Infrastructure; +using Chronos.App.Jobs; +using Chronos.App.Server; +using Chronos.App.Tests.Uploader; +using Chronos.Core.Localization; +using Chronos.Data; +using Chronos.Data.Archiving; +using Chronos.Server.Jobs; +using Chronos.Testing; + +namespace Chronos.App.Tests.Server; + +/// Settings › Server › Share this library (owner review 5, #67): the server Chronos hosts over its own index and engines. +[WindowsOnly] +[Serial] // the hosted server's logging and Kestrel share the process with the other tests' Log sink +[Timeout(180_000)] +public sealed class LibraryShareTests +{ + private sealed class NoDialogs : IAppDialogs + { + public string? PickFolder(string title, string? initialFolder = null) => null; + public string? PickJsonFile(string title) => null; + public bool Confirm(ConfirmRequest request) => false; + public bool SignIn(string server, string? username, Func> signIn) => false; + } + + [Test] + public void A10SettingsFileSharesNothingOnDockersPortsAndResetTurnsSharingOff() + { + var settings = Assert.NotNull(AppSettings.Parse("""{ "Language": "en" }""")); + Assert.False(settings.Share.Enabled); + Assert.Equal((8443, 8080, true), (settings.Share.HttpsPort, settings.Share.HttpPort, settings.Share.Discovery)); + Assert.Null(settings.Share.PublicHttpsPort); + + settings.Share.Enabled = true; + settings.Share.HttpsPort = 9443; + var copy = Assert.NotNull(AppSettings.Parse(settings.ToJson())); + Assert.True(copy.Share.Enabled); + Assert.Equal(9443, copy.Share.HttpsPort); + Assert.False(settings.ResetKeepingState().Share.Enabled); + Assert.Equal(8443, settings.ResetKeepingState().Share.HttpsPort); + } + + [Test] + public void TheSharedLibrarysCleanUpsAreScheduledOnlyWhileSharing() + { + var settings = new AppSettings(); + var host = new DesktopScheduleHost(settings, () => null, () => null); + Assert.False(host.SystemSchedules.Any(s => s.SystemKey == ServerScheduleHost.UploadCleanup)); + settings.Share.Enabled = true; + var keys = host.SystemSchedules.Select(s => s.SystemKey).ToList(); + Assert.Contains(ServerScheduleHost.UploadCleanup, keys); + Assert.Contains(ServerScheduleHost.AuditCleanup, keys); + Assert.Contains(DesktopScheduleHost.ArchiveScan, keys); + } + + [Test] + public void SharingStartsAfterTheSchedulesOnTheDesktopsEngineAndStopsFirst() + { + var environment = AppEnvironment.ForCapture(); + try + { + SingleThreadedContext.Run(async () => + { + var root = await ArchiveAsync(environment); + var (https, http) = (FreePort(), FreePort()); + var settings = Settings(root, https, http); + var services = new AppServices(environment, settings, () => { }, new NoDialogs()); + try + { + await services.OpenAsync(environment.DatabasePath); + Assert.True(await services.SchedulerReady, "the schedules started"); + await WaitAsync(services.Share, ShareState.On); + + var share = services.Share; + Assert.Equal((https, http), (share.HttpsPort, share.HttpPort)); + Assert.True(share.Addresses.Count > 0, "addresses"); + Assert.True(share.Addresses.All(a => a.StartsWith("https://", StringComparison.Ordinal) && a.EndsWith($":{https}", StringComparison.Ordinal)), + string.Join(", ", share.Addresses)); + var fingerprint = Assert.NotNull(share.Fingerprint); + Assert.True(share.IsOwn(fingerprint, null), "own by fingerprint"); + Assert.True(share.IsOwn(null, share.Server!.ServerId), "own by id"); + Assert.False(share.IsOwn("00:11", Guid.NewGuid()), "another server"); + Assert.NotNull(share.AdminPassword); + Assert.Same(services.Engine, share.Server.Services.GetService(typeof(JobEngineHost)) is JobEngineHost jobs ? jobs.Engine : null); + using (var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }) + { + using var health = await client.GetAsync($"http://127.0.0.1:{http}/health"); + Assert.Equal(HttpStatusCode.OK, health.StatusCode); + } + Assert.NotNull(await services.Database!.Schedules.GetBySystemKeyAsync(ServerScheduleHost.UploadCleanup)); + Assert.True(Directory.Exists(environment.ShareDirectory), "the share's folder"); + + // Stopped, the engine and the index run on; started again, the same certificate. + await share.StopAsync(); + Assert.Equal(ShareState.Off, share.State); + Assert.Null(share.Server); + Assert.NotNull(await services.Database.Archives.ListAsync()); + await share.RestartAsync(); + Assert.Equal(ShareState.On, share.State); + Assert.Equal(fingerprint, share.Fingerprint); + + // Sharing turned off in Settings stops it. + settings.Share.Enabled = false; + share.Apply(); + await WaitAsync(share, ShareState.Off); + } + finally + { + Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(20)), "services stopped"); + } + Assert.Null(services.Share.Server); + }); + } + finally + { + environment.DeleteCaptureData(); + } + } + + [Test] + public void APortInUseLeavesSharingNotStartedNamingThePort() + { + var environment = AppEnvironment.ForCapture(); + try + { + SingleThreadedContext.Run(async () => + { + var root = await ArchiveAsync(environment); + // Held as another server would (both address families, exclusively): Windows lets a second program share a port otherwise. + using var taken = new TcpListener(IPAddress.IPv6Any, 0) { ExclusiveAddressUse = true }; + taken.Server.DualMode = true; + taken.Start(); + var https = ((IPEndPoint)taken.LocalEndpoint).Port; + var services = new AppServices(environment, Settings(root, https, FreePort()), () => { }, new NoDialogs()); + try + { + await services.OpenAsync(environment.DatabasePath); + Assert.True(await services.SchedulerReady, "the schedules started"); + await WaitAsync(services.Share, ShareState.Failed); + Assert.Equal(Loc.L("Errors.Share.PortInUse", https), services.Share.Reason); + Assert.Null(services.Share.Server); + Assert.False(services.Share.IsOwn(null, null), "nothing shared"); + } + finally + { + Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(20)), "services stopped"); + } + }); + } + finally + { + environment.DeleteCaptureData(); + } + } + + private static AppSettings Settings(string root, int https, int http) => new() + { + Language = "en", + LastArchivePath = root, + Share = new ShareSettings { Enabled = true, HttpsPort = https, HttpPort = http, Discovery = false }, + }; + + private static async Task ArchiveAsync(AppEnvironment environment) + { + var root = Path.Combine(environment.DataDirectory, "archive"); + await using var setup = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(environment.DataDirectory, "setup.db"))); + await new ArchiveService(setup).CreateAsync(root, "Shared"); + return root; + } + + private static int FreePort() + { + using var listener = new TcpListener(IPAddress.Loopback, 0); + listener.Start(); + return ((IPEndPoint)listener.LocalEndpoint).Port; + } + + private static async Task WaitAsync(LibraryShare share, ShareState state) + { + var until = DateTime.UtcNow + TimeSpan.FromSeconds(60); + while (share.State != state) + { + Assert.True(DateTime.UtcNow < until, $"sharing is {share.State} ({share.Reason}), not {state}"); + await Task.Delay(50); + } + } +} diff --git a/tests/Chronos.Server.Tests/Hosting/HostedServerTests.cs b/tests/Chronos.Server.Tests/Hosting/HostedServerTests.cs new file mode 100644 index 00000000..4db81467 --- /dev/null +++ b/tests/Chronos.Server.Tests/Hosting/HostedServerTests.cs @@ -0,0 +1,336 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Security.Cryptography.X509Certificates; +using Microsoft.Extensions.DependencyInjection; +using Chronos.Core.Diagnostics; +using Chronos.Core.Imaging.Decoders; +using Chronos.Core.Model; +using Chronos.Core.Video.Managed; +using Chronos.Data; +using Chronos.Data.Archiving; +using Chronos.Data.Jobs; +using Chronos.Data.Processing; +using Chronos.Data.Scheduling; +using Chronos.Protocol; +using Chronos.Server.Api; +using Chronos.Server.Configuration; +using Chronos.Server.Hosting; +using Chronos.Server.Jobs; +using Chronos.Server.Security; +using Chronos.Testing; + +namespace Chronos.Server.Tests.Hosting; + +/// +/// The server Chronos Desktop hosts to share this PC's library (owner review 5, #67): built over an index, engines, a job +/// engine and a scheduler the test owns, as the desktop does, which it never creates, starts, stops or disposes. +/// +[Category("server")] +[Serial] // Log.Sink is process-wide: the standalone server tests running beside these set their own +public sealed class HostedServerTests(TestContext context) +{ + [Test] + public async Task ItAnswersWithTheCertificateOfItsDataFolderAndKeepsItsIdAcrossRestarts() + { + await using var desktop = await DesktopFixture.StartAsync(context); + Guid? firstId; + string? fingerprint; + await using (var shared = await desktop.ShareAsync()) + { + using var http = desktop.Client(shared); + using var health = await http.GetAsync("/health"); + Assert.Equal(HttpStatusCode.OK, health.StatusCode); + var info = Assert.NotNull(await http.GetFromJsonAsync("/api/v1/server/info", ProtocolJson.Default.ServerInfo)); + var pfx = X509CertificateLoader.LoadPkcs12FromFile(Path.Combine(desktop.ShareData, "tls", "server.pfx"), null); + Assert.Equal(CertificateStore.Fingerprint(pfx), shared.Fingerprint); + Assert.Equal(shared.ServerId, info.ServerId); + Assert.True(shared.HttpsPort > 0, "the HTTPS listener's port"); + firstId = shared.ServerId; + fingerprint = shared.Fingerprint; + } + // A second start on the same folder and index: the same certificate, id and administrator. + await using var again = await desktop.ShareAsync(); + Assert.Equal(fingerprint, again.Fingerprint); + Assert.Equal(firstId, again.ServerId); + Assert.Count(1, (await desktop.Database.Users.ListAsync()).Where(u => u.Role == UserRole.Admin)); + } + + [Test] + public async Task JobsQueuedByClientsRunOnTheDesktopsEngineWhichOutlivesTheServer() + { + await using var desktop = await DesktopFixture.StartAsync(context); + var sink = Log.Sink; + await using (var shared = await desktop.ShareAsync()) + { + Assert.Same(desktop.Engine, shared.Services.GetRequiredService().Engine); + Assert.True(shared.Services.GetRequiredService().Hosted, "hosted"); + using var admin = await desktop.AdminAsync(shared); + using var created = await admin.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("scan", [], null), ProtocolJson.Default.CreateJobRequest); + Assert.Equal(HttpStatusCode.Accepted, created.StatusCode); + var job = Assert.NotNull(await created.Content.ReadFromJsonAsync(ProtocolJson.Default.JobDto)); + var done = await desktop.WaitAsync(job.Id); + Assert.Equal(JobState.Done, done.State); + Assert.Equal(DesktopFixture.WorkerId, done.NodeId); + Assert.Same(sink, Log.Sink); + } + // Stopped: the index answers, the engine runs jobs, the scheduler still works, the log sink is the desktop's. + Assert.Same(sink, Log.Sink); + var after = await desktop.Engine.EnqueueAsync(JobKind.Scan, [desktop.Archive.Id], "after"); + Assert.Equal(JobState.Done, (await desktop.WaitAsync(after.Id)).State); + Assert.NotNull(await desktop.Scheduler.ListAsync(context.Cancellation)); + } + + [Test] + public async Task TheServerAddsOnlyItsOwnSystemSchedulesToTheDesktops() + { + await using var desktop = await DesktopFixture.StartAsync(context); + var before = (await desktop.Scheduler.ListAsync(context.Cancellation)).Items.Select(s => s.SystemKey).OfType().ToHashSet(); + await using var shared = await desktop.ShareAsync(); + var after = (await desktop.Scheduler.ListAsync(context.Cancellation)).Items.Select(s => s.SystemKey).OfType().ToHashSet(); + Assert.SequenceEqual([ServerScheduleHost.AuditCleanup, ServerScheduleHost.UploadCleanup], after.Except(before).Order(StringComparer.Ordinal)); + Assert.SequenceEqual([DesktopFixture.ScanKey], before); + } + + [Test] + public async Task ItServesTheDesktopsArchiveAloneAndHidesItsPrivateJobs() + { + await using var desktop = await DesktopFixture.StartAsync(context); + var mine = await desktop.SequenceAsync(desktop.Archive); + var other = await desktop.Archives.CreateAsync(context.CreateTempDirectory("other"), "Another archive", cancellationToken: context.Cancellation); + var theirs = await desktop.SequenceAsync(other); + var workspace = await desktop.Engine.EnqueueAsync(JobKind.WorkspaceCopy, [mine.Id], "workspace"); + await using var shared = await desktop.ShareAsync(); + using var admin = await desktop.AdminAsync(shared); + + using (var ok = await admin.GetAsync($"/api/v1/sequences/{mine.Id}")) Assert.Equal(HttpStatusCode.OK, ok.StatusCode); + foreach (var path in new[] { $"/api/v1/sequences/{theirs.Id}", $"/api/v1/sequences/{theirs.Id}/projects", $"/api/v1/sequences/{theirs.Id}/frames" }) + { + using var hidden = await admin.GetAsync(path); + Assert.Equal(HttpStatusCode.NotFound, hidden.StatusCode, path); + } + using (var job = await admin.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("thumbs", [theirs.Id], null), ProtocolJson.Default.CreateJobRequest)) + Assert.Equal(HttpStatusCode.NotFound, job.StatusCode); + + var listed = Assert.NotNull(await admin.GetFromJsonAsync("/api/v1/jobs", ProtocolJson.Default.PageJobDto)); + Assert.False(listed.Items.Any(j => j.Id == workspace.Id), "a workspace job is the desktop's own"); + using (var get = await admin.GetAsync($"/api/v1/jobs/{workspace.Id}")) Assert.Equal(HttpStatusCode.NotFound, get.StatusCode); + using (var cancel = await admin.PostAsync($"/api/v1/jobs/{workspace.Id}/cancel", null)) Assert.Equal(HttpStatusCode.NotFound, cancel.StatusCode); + Assert.Equal(JobState.Queued, (await desktop.Database.Jobs.GetAsync(workspace.Id))!.State); + } + + [Test] + public async Task TheDesktopOwnsItsSettingsAndTheFirstAdministratorsPasswordIsInTheShareFolder() + { + await using var desktop = await DesktopFixture.StartAsync(context); + Environment.SetEnvironmentVariable("CHRONOS_SERVER_NAME", "from a variable"); + try + { + var options = ServerOptions.ForHosted(desktop.ShareData, desktop.Settings with { ServerName = "Studio PC" }); + Assert.Equal("Studio PC", options.ServerName); + Assert.True(options.Hosted, "hosted"); + Assert.Contains(nameof(ServerOptions.HttpsPort), options.Locked); + Assert.Equal(ServerOptions.DesktopLock, options.LockedBy[nameof(ServerOptions.Parallelism)]); + Assert.False(options.Locked.Contains(nameof(ServerOptions.TokenLifetimeDays)), "the admin panel owns the token lifetime"); + Assert.Equal(Microsoft.Extensions.Logging.LogLevel.None, options.ConsoleLogLevel); + } + finally + { + Environment.SetEnvironmentVariable("CHRONOS_SERVER_NAME", null); + } + await using var shared = await desktop.ShareAsync(); + Assert.True(File.Exists(shared.AdminPasswordFile), "the temporary password's file"); + Assert.Equal(Path.Combine(desktop.ShareData, AdminBootstrap.PasswordFileName), shared.AdminPasswordFile); + Assert.NotNull(shared.AdminPassword()); + using var admin = await desktop.AdminAsync(shared); + Assert.Null(shared.AdminPassword()); + var settings = Assert.NotNull(await admin.GetFromJsonAsync("/api/v1/server/settings", ProtocolJson.Default.ServerSettingsDto)); + Assert.Equal(ServerOptions.DesktopLock, settings.LockedBy["HttpsPort"]); + } + + [Test] + public async Task ClientsAreKeptForFiveMinutes() + { + var time = new ManualTime(DateTimeOffset.Parse("2026-11-15T10:00:00Z", System.Globalization.CultureInfo.InvariantCulture)); + var activity = new ClientActivity(time); + var user = new User { Id = Guid.CreateVersion7(), Username = "alberto", PasswordHash = "x", Role = UserRole.Editor, Created = time.GetUtcNow() }; + activity.Record(new Caller(user, "hash", CredentialSource.Bearer, "Chronos Uploader (Studio)"), "192.168.1.20"); + activity.Record(new Caller(user, "hash", CredentialSource.Cookie, null), "192.168.1.21"); + var seen = activity.Recent(); + Assert.Count(2, seen); + Assert.Contains("Chronos Uploader (Studio)", seen.Select(s => s.Client)); + Assert.Contains(ClientActivity.AdminPanel, seen.Select(s => s.Client)); + time.Advance(TimeSpan.FromMinutes(6)); + Assert.Count(0, activity.Recent()); + } + + [Test] + public void ARenderNeverKeepsTheExportFolderAClientChose() + { + var parameters = new RenderJobParameters("1080p", """{"preset":"1080p","Folder":"C:\\Users\\someone","nameTemplate":"{title}"}""", "h264", 1920, 1080, false, null); + var stripped = JobEndpoints.WithoutExportFolder(parameters); + Assert.False(stripped.PresetJson.Contains("Users", StringComparison.Ordinal), stripped.PresetJson); + Assert.True(stripped.PresetJson.Contains("nameTemplate", StringComparison.Ordinal), stripped.PresetJson); + var plain = parameters with { PresetJson = """{"preset":"1080p"}""" }; + Assert.Same(plain, JobEndpoints.WithoutExportFolder(plain)); + } + + [Test] + public void TheServiceSwitchesAreGone() + { + var parsed = ServerCommandLine.Parse(["--service", "--install-service", "--uninstall-service"]); + Assert.Equal(ServerCommand.Run, parsed.Command); + Assert.SequenceEqual(["--service", "--install-service", "--uninstall-service"], parsed.HostArguments); + Assert.False(ServerCommandLine.Usage.Contains("service", StringComparison.OrdinalIgnoreCase), "usage names a service"); + } + + /// A clock the test moves. + private sealed class ManualTime(DateTimeOffset start) : TimeProvider + { + private DateTimeOffset _now = start; + + public override DateTimeOffset GetUtcNow() => _now; + + public void Advance(TimeSpan by) => _now += by; + } +} + +/// +/// What Chronos Desktop has when it shares its library: a SQLite index, the archive and processing services (managed +/// codecs), a started job engine and a started scheduler with one system schedule of its own. +/// +internal sealed class DesktopFixture : IAsyncDisposable +{ + public const string WorkerId = "STUDIO-PC"; + public const string ScanKey = "archive-scan"; + + private readonly TestContext _context; + + private DesktopFixture(TestContext context, string root, ChronosDatabase db, ArchiveService archives, ProcessingService processing, JobEngine engine, + Scheduler scheduler, Archive archive) + { + _context = context; + Root = root; + Database = db; + Archives = archives; + Processing = processing; + Engine = engine; + Scheduler = scheduler; + Archive = archive; + } + + public string Root { get; } + public ChronosDatabase Database { get; } + public ArchiveService Archives { get; } + public ProcessingService Processing { get; } + public JobEngine Engine { get; } + public Scheduler Scheduler { get; } + public Archive Archive { get; } + public string ShareData => Path.Combine(Root, "data", "server"); + + public HostedSettings Settings => new(null, 0, 0, null, null, Discovery: false, Archive.RootPath, null, 2, false, new Dictionary(), "en", + LogLevel.Warning, 14, 50, 1024, LogRotation.Daily, 4); + + public static async Task StartAsync(TestContext context) + { + var root = context.CreateTempDirectory("desktop"); + Directory.CreateDirectory(Path.Combine(root, "data")); + var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(root, "data", "chronos.db")), context.Cancellation); + var archiveOptions = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Parallelism = () => 2 }; + var archives = new ArchiveService(db, archiveOptions); + var processing = new ProcessingService(db, new ProcessingOptions { Archive = archiveOptions, Encoders = [new ManagedEncoderProvider()] }); + var archive = await archives.CreateAsync(Path.Combine(root, "archive"), "Studio", cancellationToken: context.Cancellation); + var handlers = archives.CreateJobHandlers().Concat(processing.CreateJobHandlers()).ToList(); + var engine = new JobEngine(db, handlers, new JobEngineOptions + { + WorkerId = WorkerId, + Kinds = [.. handlers.Select(h => h.Kind).Distinct()], + PollInterval = TimeSpan.FromMilliseconds(100), + }); + await engine.StartAsync(context.Cancellation); + var scheduler = new Scheduler(db, engine); + await scheduler.StartAsync(new Host(archives, archive), context.Cancellation); + return new DesktopFixture(context, root, db, archives, processing, engine, scheduler, archive); + } + + public async Task ShareAsync() + { + var options = ServerOptions.ForHosted(ShareData, Settings); + options.LoginAttemptsPerMinute = 1000; + var server = ServerHost.BuildHosted(options, new HostedEngine(Database, Archives, Processing, Engine, Scheduler, Archive)); + await server.StartAsync(_context.Cancellation); + return server; + } + + public HttpClient Client(HostedServer server, string? token = null) + { + var client = new HttpClient { BaseAddress = new Uri($"http://127.0.0.1:{server.HttpPort}"), Timeout = TimeSpan.FromSeconds(60) }; + if (token is not null) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + return client; + } + + /// The first administrator, signed in with its temporary password, which it then replaces. + public async Task AdminAsync(HostedServer server) + { + var temporary = server.AdminPassword() ?? throw new InvalidOperationException("No temporary password"); + using var anonymous = Client(server); + using var login = await anonymous.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", temporary, "tests"), ProtocolJson.Default.LoginRequest); + login.EnsureSuccessStatusCode(); + var token = (await login.Content.ReadFromJsonAsync(ProtocolJson.Default.LoginResponse))!.Token!; + var client = Client(server, token); + using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(temporary, "a-good-password-1"), + ProtocolJson.Default.ChangePasswordRequest); + changed.EnsureSuccessStatusCode(); + return client; + } + + public async Task SequenceAsync(Archive archive) + { + var sequence = new Sequence + { + Id = Guid.CreateVersion7(), + ArchiveId = archive.Id, + Title = "Roof " + archive.Name, + Folder = string.Empty, + Origin = SequenceOrigin.Server, + UpdatedAt = DateTimeOffset.UtcNow, + }; + await Database.Sequences.UpsertAsync(sequence, _context.Cancellation); + return sequence; + } + + public async Task WaitAsync(Guid id) + { + var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); + while (true) + { + if (await Database.Jobs.GetAsync(id, _context.Cancellation) is { State: JobState.Done or JobState.Failed or JobState.Cancelled } job) return job; + Assert.True(DateTime.UtcNow < until, "the job did not finish"); + await Task.Delay(50, _context.Cancellation); + } + } + + public async ValueTask DisposeAsync() + { + await Scheduler.DisposeAsync(); + await Engine.DisposeAsync(); + await Database.DisposeAsync(); + } + + /// A desktop's schedule host: one archive scan schedule, the archive service's clean-up steps. + private sealed class Host(ArchiveService archives, Archive archive) : IScheduleHost + { + public TimeZoneInfo TimeZone => TimeZoneInfo.Utc; + + public IReadOnlyList SystemSchedules => + [new(ScanKey, "Archive scan", ScheduleActions.Scan, ScheduleRecurrence.Interval(10, RecurrenceUnit.Minutes), CatchUp: false)]; + + public IReadOnlyList CleanupSteps => [.. archives.CleanupSteps.Select(s => s.Name)]; + + public bool DeepAnalysisAfterScan => false; + + public ValueTask GetArchiveAsync(CancellationToken cancellationToken) => ValueTask.FromResult(archive); + + public string? Unavailable(string action) => null; + } +} diff --git a/tests/Chronos.Server.Tests/Hosting/WindowsServiceTests.cs b/tests/Chronos.Server.Tests/Hosting/WindowsServiceTests.cs deleted file mode 100644 index 32771694..00000000 --- a/tests/Chronos.Server.Tests/Hosting/WindowsServiceTests.cs +++ /dev/null @@ -1,152 +0,0 @@ -using System.Diagnostics.Eventing.Reader; -using System.Security.AccessControl; -using System.Security.Principal; -using Chronos.Server.Hosting; -using Chronos.Server.Hosting.Windows; -using Chronos.Testing; - -namespace Chronos.Server.Tests.Hosting; - -public sealed class WindowsServiceTests(TestContext context) -{ - [Test] - public void CommandSwitchesAreParsedAndTheRestPassedOn() - { - Assert.Equal(ServerCommand.Run, ServerCommandLine.Parse([]).Command); - var service = ServerCommandLine.Parse(["--service", "--urls", "x"]); - Assert.Equal(ServerCommand.Service, service.Command); - Assert.SequenceEqual(["--urls", "x"], service.HostArguments); - Assert.Equal(ServerCommand.InstallService, ServerCommandLine.Parse(["--install-service"]).Command); - Assert.Equal(ServerCommand.UninstallService, ServerCommandLine.Parse(["--uninstall-service"]).Command); - Assert.Equal(ServerCommand.HealthCheck, ServerCommandLine.Parse(["--healthcheck"]).Command); - Assert.Equal(ServerCommand.Help, ServerCommandLine.Parse(["/?"]).Command); - Assert.NotNull(ServerCommandLine.Parse(["--install-service", "--uninstall-service"]).Error); - Assert.Null(ServerCommandLine.Parse(["--service", "--service"]).Error); - } - - [Test] - public void StatusReportsFollowTheServiceControlManagerRules() - { - var tracker = new ServiceStatusTracker(); - var start = tracker.StartPending(60_000); - Assert.Equal((ServiceState.StartPending, 0u, 1u, 60_000u), (start.State, start.ControlsAccepted, start.CheckPoint, start.WaitHintMilliseconds)); - Assert.Equal(2u, tracker.StartPending(60_000).CheckPoint); - var running = tracker.Running(); - Assert.Equal((ServiceState.Running, ServiceStatusTracker.AcceptStop | ServiceStatusTracker.AcceptShutdown, 0u), (running.State, running.ControlsAccepted, running.CheckPoint)); - Assert.Throws(() => tracker.Running()); - Assert.Equal(1u, tracker.StopPending(35_000).CheckPoint); - Assert.Equal(2u, tracker.StopPending(35_000).CheckPoint); - var stopped = tracker.Stopped(0); - Assert.Equal((ServiceState.Stopped, 0u, 0u), (stopped.State, stopped.ControlsAccepted, stopped.CheckPoint)); - Assert.Throws(() => tracker.StopPending(1)); - Assert.Equal(1064u, new ServiceStatusTracker().Stopped(1064).ExitCode); - } - - [Test] - public void AServiceFailureLeadsWithTheReasonTheServerGave() - { - var refused = new InvalidOperationException(@"C:\ProgramData\Chronos Server belongs to PC\ada, not to SYSTEM, Administrators or the account Chronos Server runs as."); - var text = ServiceEventLog.Describe("Chronos Server did not start:", refused); - Assert.True(text.StartsWith(@"Chronos Server did not start: C:\ProgramData\Chronos Server belongs to PC\ada", StringComparison.Ordinal), text); - Assert.Contains("System.InvalidOperationException", text); - - // ReportEvent refuses a longer string: the text is cut, never between the halves of a surrogate pair. - var cut = ServiceEventLog.Describe("x", new InvalidOperationException(new string('a', ServiceEventLog.MaxLength))); - Assert.Equal(ServiceEventLog.MaxLength, cut.Length); - Assert.True(cut.EndsWith("a\u2026", StringComparison.Ordinal), cut[^5..]); - var pair = ServiceEventLog.Describe("x", new InvalidOperationException(new string('a', ServiceEventLog.MaxLength - 4) + "\uD83D\uDE00" + new string('b', 99))); - Assert.Equal(ServiceEventLog.MaxLength - 1, pair.Length); - Assert.True(pair.EndsWith("a\u2026", StringComparison.Ordinal), pair[^5..]); - } - - [Test] - [WindowsOnly] - public void AServiceFailureReachesTheApplicationEventLog() - { - if (!OperatingSystem.IsWindows()) return; - // Written as a source of its own, so a test run never looks like a failed service in Event Viewer. - const string source = "Chronos.Server.Tests"; - var marker = source + " " + Guid.NewGuid().ToString("N"); - Assert.True(ServiceEventLog.Error(source, ServiceEventLog.StartFailed, marker), "the Application log takes the event"); - var filter = $"*[System[Provider[@Name='{source}'] and EventID={ServiceEventLog.StartFailed}]]"; - var query = new EventLogQuery("Application", PathType.LogName, filter) { ReverseDirection = true }; - for (var attempt = 0; attempt < 50; attempt++) - { - using (var reader = new EventLogReader(query)) - { - for (var read = 0; read < 100 && reader.ReadEvent() is { } record; read++) - { - using (record) - { - // The event's one string, which Event Viewer shows whole. - if (record.Properties.Count != 1 || record.Properties[0].Value as string != marker) continue; - Assert.Equal((byte?)2, record.Level, "an error"); - return; - } - } - } - Thread.Sleep(100); - } - Assert.Fail($"No {source} event carries '{marker}'."); - } - - [Test] - [WindowsOnly] - public void TheServiceIsRegisteredOnlyFromInsideProgramFiles() - { - if (!OperatingSystem.IsWindows()) return; - var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); - Assert.True(ServiceInstaller.IsUnderProgramFiles(Path.Combine(programFiles, "Chronos", "Chronos.Server.exe"))); - Assert.False(ServiceInstaller.IsUnderProgramFiles(programFiles)); - Assert.False(ServiceInstaller.IsUnderProgramFiles(programFiles + " Tools\\Chronos")); - Assert.False(ServiceInstaller.IsUnderProgramFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs", "Chronos"))); - Assert.False(ServiceInstaller.IsUnderProgramFiles(Path.Combine(programFiles, "..", "Chronos"))); - // The test build is not under Program Files: registering it is refused, naming the folder. - Assert.Contains(Path.TrimEndingDirectorySeparator(AppContext.BaseDirectory), Assert.NotNull(ServiceInstaller.UnsafeLocation())); - } - - [Test] - [WindowsOnly] - public void AnyFileOrdinaryUsersCanChangeIsFound() - { - if (!OperatingSystem.IsWindows()) return; - using var identity = WindowsIdentity.GetCurrent(); - if (!new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator)) Assert.Inconclusive("registering a service needs an administrator"); - var folder = new DirectoryInfo(context.CreateTempDirectory("program")); - File.WriteAllBytes(Path.Combine(folder.FullName, "Chronos.Server.exe"), [1]); - var satellite = new FileInfo(Path.Combine(folder.CreateSubdirectory("it").FullName, "Chronos.Core.resources.dll")); - File.WriteAllBytes(satellite.FullName, [1]); - - // Like Program Files: administrators may change the folder, users only read it. - var locked = new DirectorySecurity(); - locked.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); - var everything = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; - foreach (var account in new[] { new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null), identity.User! }) - locked.AddAccessRule(new FileSystemAccessRule(account, FileSystemRights.FullControl, everything, PropagationFlags.None, AccessControlType.Allow)); - locked.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), FileSystemRights.ReadAndExecute, everything, - PropagationFlags.None, AccessControlType.Allow)); - folder.SetAccessControl(locked); - Assert.Null(ServiceInstaller.FindChangeable(folder, recursive: true)); - - // One satellite assembly users may replace is enough: the service would load it as SYSTEM. - var open = satellite.GetAccessControl(); - open.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), FileSystemRights.Modify, AccessControlType.Allow)); - satellite.SetAccessControl(open); - Assert.Equal(satellite.FullName, ServiceInstaller.FindChangeable(folder, recursive: true)); - Assert.Null(ServiceInstaller.FindChangeable(folder, recursive: false)); - } - - [Test] - [WindowsOnly] - public void TheDispatcherDeclinesAConsoleProcess() - { - if (!OperatingSystem.IsWindows()) return; - var built = false; - Assert.False(WindowsServiceHost.Run(() => - { - built = true; - throw new InvalidOperationException("never built outside the SCM"); - })); - Assert.False(built); - } -}