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());
+ }
+}