feat: single setup, mass-pass progress, per-server homes, Desktop shares its library (owner review 5, v1.1.0) #68
No files matched your search
@@ -0,0 +1,178 @@
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Chronos.Core.IO;
|
||||
|
||||
/// <summary>
|
||||
/// The folders each home of the desktop keeps apart (owner review 5, #66): this PC's archive is the home <see cref="Local"/>,
|
||||
/// and every Chronos Server is a home of its own under <see cref="Servers"/>, named by the server's stable id
|
||||
/// (<c>servers\<id:N></c>), or, for a server that announces none (an older server), by a hash of its address key
|
||||
/// (<c>servers\addr-<16 hex></c>: 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 (<c><workspace>\local</c>, <c><workspace>\servers\<folder></c>)
|
||||
/// and in the cache (<c><cache>\servers\<folder>\{proxies,render-node,import}</c>). A folder name is never renamed
|
||||
/// after the server: its name lives in <see cref="ServerFile"/> beside it.
|
||||
/// </summary>
|
||||
public static class HomeFolders
|
||||
{
|
||||
/// <summary>The folder of this PC's archive's home.</summary>
|
||||
public const string Local = "local";
|
||||
|
||||
/// <summary>The folder that holds one folder per server home.</summary>
|
||||
public const string Servers = "servers";
|
||||
|
||||
/// <summary>What a server home's folder says about its server (id, name, last address), for someone browsing it.</summary>
|
||||
public const string ServerFile = "server.json";
|
||||
|
||||
/// <summary>A server home's proxies of its sequences, in the cache.</summary>
|
||||
public const string Proxies = "proxies";
|
||||
|
||||
/// <summary>A server home's render-node downloads, in the cache.</summary>
|
||||
public const string RenderNode = "render-node";
|
||||
|
||||
/// <summary>A server home's staged card imports, in the cache.</summary>
|
||||
public const string Import = "import";
|
||||
|
||||
/// <summary>The start of a server key made from the server's announced id (<c>id:<32 hex></c>).</summary>
|
||||
public const string IdKeyPrefix = "id:";
|
||||
|
||||
/// <summary>The start of the folder of a server known by its address only.</summary>
|
||||
public const string AddressPrefix = "addr-";
|
||||
|
||||
/// <summary>Hex digits of the address hash in a folder name (as the 1.0 proxy cache used).</summary>
|
||||
public const int HashLength = 16;
|
||||
|
||||
/// <summary>
|
||||
/// The folder of a server home: the 32 hex digits of the id of an id key (<c>id:<N></c>), else <c>addr-</c> and the
|
||||
/// first 16 hex digits of the SHA-256 of the key (an address key, as <c>SyncService.ServerKey</c> makes it).
|
||||
/// </summary>
|
||||
public static string ServerFolder(string serverKey)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(serverKey);
|
||||
return IdOfKey(serverKey) is { } id ? id.ToString("N") : AddressPrefix + AddressHash(serverKey);
|
||||
}
|
||||
|
||||
/// <summary>The 16 hex digits the 1.0 proxy cache named a server's folder by: the start of the SHA-256 of its key.</summary>
|
||||
public static string AddressHash(string serverKey)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(serverKey);
|
||||
return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(serverKey)))[..HashLength];
|
||||
}
|
||||
|
||||
/// <summary>The id an id key names (<c>id:<N></c>); null for an address key.</summary>
|
||||
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;
|
||||
|
||||
/// <summary>The key of a server known by its announced id.</summary>
|
||||
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");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A name a server home's folder can have: 32 lower-case hex digits (an id) or <c>addr-</c> with 16 lower-case hex
|
||||
/// digits. Anything else under <see cref="Servers"/> is not a home (left alone).
|
||||
/// </summary>
|
||||
public static bool IsServerFolder(string? name) => name is { Length: > 0 } && (IdOfFolder(name) is not null || IsAddressFolder(name));
|
||||
|
||||
/// <summary>The server id a folder named by one stands for; null for an address folder or another name.</summary>
|
||||
public static Guid? IdOfFolder(string? name) =>
|
||||
name is { Length: 32 } && IsLowerHex(name) && Guid.TryParseExact(name, "N", out var id) && id != Guid.Empty ? id : null;
|
||||
|
||||
/// <summary>A folder of a server known by its address only (<c>addr-</c> and 16 lower-case hex digits).</summary>
|
||||
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));
|
||||
|
||||
/// <summary>The folder of this PC's home under <paramref name="root"/> (a workspace or a cache folder).</summary>
|
||||
public static string LocalPath(string root)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(root);
|
||||
return Path.Combine(root, Local);
|
||||
}
|
||||
|
||||
/// <summary>The folder of the home of <paramref name="serverKey"/> under <paramref name="root"/> (a workspace or a cache folder).</summary>
|
||||
public static string ServerPath(string root, string serverKey)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(root);
|
||||
return Path.Combine(root, Servers, ServerFolder(serverKey));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Where the originals a server's sequences download go by default (owner review 5, Q1): an archive of their own beside
|
||||
/// this PC's, <c><parent of the archive>\<archive folder name> - <server name></c>, for example
|
||||
/// <c>D:\ChronosArchive - Studio NAS</c>. The server's name is made safe as a folder name (<see cref="SafeName"/>); a
|
||||
/// folder <paramref name="taken"/> says is taken gets " (2)", " (3)"… With no archive of this PC's,
|
||||
/// <paramref name="fallbackArchiveRoot"/> (where a first archive would go) stands for it.
|
||||
/// </summary>
|
||||
/// <param name="localArchiveRoot">The root of this PC's archive; null when there is none.</param>
|
||||
/// <param name="serverName">The server's name as its <c>server/info</c> gives it.</param>
|
||||
/// <param name="fallbackArchiveRoot">Where this PC's first archive would go (the desktop's default archive folder).</param>
|
||||
/// <param name="taken">Whether a folder is taken (it exists, or another archive uses it); null takes an existing folder or file.</param>
|
||||
public static string OriginalsArchiveRoot(string? localArchiveRoot, string serverName, string fallbackArchiveRoot, Func<string, bool>? 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Characters no folder name may hold on Windows (and so on any PC a folder may be copied to).</summary>
|
||||
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",
|
||||
];
|
||||
|
||||
/// <summary>The longest folder name <see cref="SafeName"/> gives.</summary>
|
||||
public const int MaxNameLength = 60;
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="name"/> 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 <see cref="MaxNameLength"/>. "Server" when nothing is left.
|
||||
/// </summary>
|
||||
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<char> text)
|
||||
{
|
||||
foreach (var c in text)
|
||||
{
|
||||
if (c is not ((>= '0' and <= '9') or (>= 'a' and <= 'f'))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,10 @@ public sealed record BundleSequence(Guid Id, string Folder, string? Title, int F
|
||||
|
||||
/// <summary>
|
||||
/// Where a workspace project's versions live (owner review 2, R8): this PC's archive (<see cref="ArchiveKind"/>, with the
|
||||
/// archive's id) or a server (<see cref="ServerKind"/>, with its address), and the sequence there.
|
||||
/// archive's id) or a server (<see cref="ServerKind"/>, with its key), and the sequence there. A server's key is its id key
|
||||
/// (<c>id:<N></c>, <see cref="IO.HomeFolders.IdKey"/>) once the server announced its stable id (owner review 5, #66),
|
||||
/// else the address it was reached at (<c>https://nas.local:8443</c>, the 1.0 key, which an id-keyed server keeps as an
|
||||
/// alias); the format does not change.
|
||||
/// </summary>
|
||||
public sealed record BundleHome(string Kind, Guid SequenceId, Guid? ArchiveId = null, string? Server = null)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
/// <summary>What one entry of the sorting of the folders into homes moves (<see cref="HomeLayoutEntry"/>).</summary>
|
||||
public enum HomeEntryKind
|
||||
{
|
||||
/// <summary>A workspace project's folder (<c><workspace>\<project id></c>) into its home's store.</summary>
|
||||
Project,
|
||||
|
||||
/// <summary>An entry of the workspace's trash (<c><workspace>\.trash\<name></c>) into its home's trash.</summary>
|
||||
Trash,
|
||||
|
||||
/// <summary>A server's proxy cache (<c><cache>\proxies\<hash></c>, or one sequence of it) into its home in the cache.</summary>
|
||||
Proxies,
|
||||
|
||||
/// <summary>The render node's downloads (<c><cache>\render-node</c>) into the home of the server they came from.</summary>
|
||||
RenderNode,
|
||||
|
||||
/// <summary>A card import staged for a server (<c><cache>\server-import</c>) into that server's home in the cache.</summary>
|
||||
Import,
|
||||
}
|
||||
|
||||
/// <summary>Why the workspace part of the sorting waits for a later start (the cache part runs all the same).</summary>
|
||||
public enum HomeLayoutDeferral
|
||||
{
|
||||
/// <summary>The workspace folder cannot be reached (an unplugged drive).</summary>
|
||||
Offline,
|
||||
|
||||
/// <summary>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.</summary>
|
||||
Moving,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One rename of the sorting (<see cref="HomeLayoutMigration"/>): what it is, the folder it is in (<paramref name="Root"/>: the
|
||||
/// workspace or the cache), the path it leaves and the path it takes (both full, in <paramref name="Root"/>, 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.
|
||||
/// </summary>
|
||||
/// <param name="ServerKey">The server key of the home it goes to (canonical); null for this PC's home and for a server known only by hash.</param>
|
||||
/// <param name="ServerName">The server's name as the project's file gives it, for the home's <c>server.json</c>.</param>
|
||||
public sealed record HomeLayoutEntry(HomeEntryKind Kind, string Root, string Source, string Target, int Files, long Bytes, string Inventory,
|
||||
string? ServerKey = null, string? ServerName = null);
|
||||
|
||||
/// <summary>How far the sorting went.</summary>
|
||||
public enum HomeLayoutStage
|
||||
{
|
||||
/// <summary>Entries are being renamed; the workspace's parent manifest is not split yet.</summary>
|
||||
Moving,
|
||||
|
||||
/// <summary>Every entry was renamed or skipped and the parent manifest names the split layout; only the journal is left to go.</summary>
|
||||
Committed,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The journal of the sorting of the folders into homes (<c><data>\homes-migration.json</c>,
|
||||
/// <see cref="HomeLayoutMigration.JournalFile"/>): 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.
|
||||
/// </summary>
|
||||
public sealed record HomeLayoutJournal(string Format, int FormatVersion, string Fingerprint, IReadOnlyList<HomeLayoutEntry> Entries, IReadOnlyList<int> Done,
|
||||
string? WorkspaceRoot, bool SplitsWorkspace, HomeLayoutStage Stage, DateTimeOffset Started)
|
||||
{
|
||||
public const string FormatName = "chronos-homes-migration";
|
||||
public const int CurrentFormatVersion = 1;
|
||||
|
||||
/// <summary>The journal at <paramref name="path"/>; null when there is none, or it cannot be read (logged).</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Writes the journal whole to a temporary file and moves it into place.</summary>
|
||||
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;
|
||||
@@ -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;
|
||||
|
||||
/// <summary>What the sorting of the folders into homes works on (<see cref="HomeLayoutMigration"/>).</summary>
|
||||
public sealed record HomeLayoutOptions
|
||||
{
|
||||
/// <summary>The program's data folder, which holds the journal (<see cref="HomeLayoutMigration.JournalFile"/>).</summary>
|
||||
public required string DataDirectory { get; init; }
|
||||
|
||||
/// <summary>The workspace folder (Settings › Workspace › Folder: the parent of the homes); null leaves the workspace alone.</summary>
|
||||
public string? WorkspaceRoot { get; init; }
|
||||
|
||||
/// <summary>The cache folder (Settings › Storage › Cache folder); null leaves the cache alone.</summary>
|
||||
public string? CacheRoot { get; init; }
|
||||
|
||||
/// <summary>The servers known by id and their aliases: a home is named by its server's canonical key.</summary>
|
||||
public ServerIdentity Identity { get; init; } = ServerIdentity.None;
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
public string? CurrentServer { get; init; }
|
||||
|
||||
/// <summary>The index: a workspace move job not finished defers the workspace part, and a re-key moves the server's links. Null skips both.</summary>
|
||||
public ChronosDatabase? Database { get; init; }
|
||||
|
||||
public TimeProvider Time { get; init; } = TimeProvider.System;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What the sorting would move, read without writing anything (<see cref="HomeLayoutMigration.PlanAsync"/>): 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 <see cref="WorkspaceHomes.ParentFormatVersion"/>), and a
|
||||
/// fingerprint of what was read.
|
||||
/// </summary>
|
||||
public sealed record HomeLayoutPlan(IReadOnlyList<HomeLayoutEntry> Entries, IReadOnlyList<Guid> InUse, HomeLayoutDeferral? Deferred, bool SplitsWorkspace,
|
||||
string Fingerprint)
|
||||
{
|
||||
/// <summary>Nothing to rename and no manifest to write.</summary>
|
||||
public bool IsEmpty => Entries.Count == 0 && !SplitsWorkspace;
|
||||
|
||||
/// <summary>The workspace projects the sorting moves.</summary>
|
||||
public int Projects => Entries.Count(e => e.Kind == HomeEntryKind.Project);
|
||||
|
||||
/// <summary>The bytes of everything it moves (renamed, never copied).</summary>
|
||||
public long Bytes => Entries.Sum(e => e.Bytes);
|
||||
}
|
||||
|
||||
/// <summary>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.</summary>
|
||||
/// <param name="Clashes">Entries whose target was taken: what lost went to its home's trash (workspace) or stayed where it is (cache); never deleted.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>What a re-key did (<see cref="HomeLayoutMigration.RekeyAsync"/>).</summary>
|
||||
/// <param name="HomesMoved">Home folders renamed whole to the id's (workspace and cache).</param>
|
||||
/// <param name="ProjectsMoved">Projects moved one by one into the id's home (the same server reached at two addresses).</param>
|
||||
/// <param name="ProjectsTrashed">Copies of one project in both homes whose older one went to the trash of the id's home.</param>
|
||||
/// <param name="BundlesRewritten">Working states whose home now names the id key.</param>
|
||||
/// <param name="BundlesInUse">Working states held by a writer: left naming an alias, which matches all the same.</param>
|
||||
public sealed record HomeRekeyResult(int HomesMoved, int ProjectsMoved, int ProjectsTrashed, int BundlesRewritten, int BundlesInUse, int LinksMoved, int LinksDropped);
|
||||
|
||||
/// <summary>A rename of the sorting did not leave the folder as planned: it stopped there, its journal kept, and nothing was deleted.</summary>
|
||||
public sealed class HomeLayoutException(string message, Exception? inner = null) : IOException(message, inner);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// <para>
|
||||
/// <b>Plan</b> (<see cref="PlanAsync"/>, a dry run): each workspace project folder of the 1.0 layout goes to the store of its
|
||||
/// home (<see cref="WorkspaceHomes.RootFor"/>: <c>local</c>, or <c>servers\<folder></c> of its server), one whose working
|
||||
/// state cannot be read to <c>local</c> (it still lists and opens as damaged); each trash entry goes to its project's home's
|
||||
/// trash; the 1.0 proxy cache <c><cache>\proxies\<hash></c> goes to <c><cache>\servers\<folder>\proxies</c>
|
||||
/// (the folder of a known server's id when the hash is one of its aliases', else <c>addr-<hash></c>, the same hash), and
|
||||
/// the shared <c>render-node</c> and <c>server-import</c> 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.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Apply</b> (<see cref="ApplyAsync"/>): 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 <see cref="WorkspaceMover"/>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class HomeLayoutMigration
|
||||
{
|
||||
/// <summary>The journal of a sorting under way, in the data folder (only while one is unfinished).</summary>
|
||||
public const string JournalFile = "homes-migration.json";
|
||||
|
||||
/// <summary>The 1.0 folder of the proxy cache in the cache folder.</summary>
|
||||
public const string LegacyProxies = "proxies";
|
||||
|
||||
/// <summary>The 1.0 folder of the render node's downloads in the cache folder.</summary>
|
||||
public const string LegacyRenderNode = "render-node";
|
||||
|
||||
/// <summary>The 1.0 folder of card imports staged for a server in the cache folder.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The journal's path.</summary>
|
||||
public string JournalPath => Path.Combine(_options.DataDirectory, JournalFile);
|
||||
|
||||
/// <summary>
|
||||
/// Called at named steps with the path concerned (tests: a crash): <c>journal</c> (the journal first written), <c>rename</c>
|
||||
/// (an entry about to be renamed), <c>renamed</c> (an entry renamed, its journal not written yet), <c>commit</c> (the parent
|
||||
/// manifest about to be written).
|
||||
/// </summary>
|
||||
internal Action<string, string>? Cut { get; init; }
|
||||
|
||||
private DateTimeOffset Now => _options.Time.GetUtcNow();
|
||||
|
||||
// ---------------- Plan ----------------
|
||||
|
||||
/// <summary>What the sorting would move now (<see cref="HomeLayoutPlan"/>); nothing is written.</summary>
|
||||
/// <exception cref="IOException">A folder cannot be read.</exception>
|
||||
public async Task<HomeLayoutPlan> PlanAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entries = new List<HomeLayoutEntry>();
|
||||
var inUse = new List<Guid>();
|
||||
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<HomeLayoutEntry> entries, List<Guid> inUse, CancellationToken cancellationToken)
|
||||
{
|
||||
var homes = new Dictionary<Guid, (string Partition, string? Key, string? Name)>();
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The store a working state's home belongs in, its server's canonical key and name; <c>local</c> when it cannot be read.</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>The project a trash entry was set aside from (<c><id>-<ticks></c>, also while staged as <c>.incoming-…</c>).</summary>
|
||||
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<HomeLayoutEntry> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
private static void AddCache(HomeEntryKind kind, string cache, string source, string target, List<HomeLayoutEntry> 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 ----------------
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <exception cref="HomeLayoutException">A rename did not leave the folder as planned; the journal stays for the next start.</exception>
|
||||
/// <exception cref="IOException">A folder could not be read or renamed; the journal stays for the next start.</exception>
|
||||
public async Task<HomeLayoutResult> 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,
|
||||
}
|
||||
|
||||
/// <summary>One entry renamed and checked (see the class).</summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>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.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>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.</summary>
|
||||
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)!);
|
||||
}
|
||||
|
||||
/// <summary>The last step: the workspace's parent manifest names the split layout (its id kept), and this PC's home exists.</summary>
|
||||
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 ----------------
|
||||
|
||||
/// <summary>
|
||||
/// The server <paramref name="serverId"/> announced its id: whatever this PC keeps under its address keys
|
||||
/// <paramref name="aliases"/> moves to its id (owner review 5, #66). The home folder <c>servers\addr-<hash></c> of each
|
||||
/// alias is renamed to <c>servers\<id></c> 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 (<see cref="SyncService.RekeyAsync"/>'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.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">A folder could not be read or renamed; what was done stays done, and a later re-key goes on.</exception>
|
||||
public async Task<HomeRekeyResult> RekeyAsync(Guid serverId, IReadOnlyCollection<string> 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;
|
||||
}
|
||||
|
||||
/// <summary>The projects and trash of the alias home <paramref name="from"/> moved into the id home <paramref name="into"/> (see <see cref="RekeyAsync"/>).</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Each working state in <paramref name="home"/> whose home names an alias rewritten to name <paramref name="idKey"/>, under its lease.</summary>
|
||||
private static async Task<(int Rewritten, int InUse)> RewriteBundlesAsync(string home, IReadOnlyList<string> 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);
|
||||
}
|
||||
|
||||
/// <summary>A cache folder's entries moved into <paramref name="into"/> where it lacks them; the source renamed whole when <paramref name="into"/> is not there. What is left stays (the cache trims it).</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>A folder or file renamed, checked by inventory: the same relative names and sizes at the target, the source gone.</summary>
|
||||
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 ----------------
|
||||
|
||||
/// <summary>A workspace move job is waiting, running or stopped (null database: none known).</summary>
|
||||
private async Task<bool> MovePendingAsync(CancellationToken cancellationToken) =>
|
||||
_options.Database is { } db && await db.Jobs.HasPendingAsync([JobKind.WorkspaceMove], null, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
/// <summary>The folder's volume is there (so a folder missing on it was never made, rather than out of reach).</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The project folder's lease is held now (another Chronos, with another data folder, works on the same workspace).</summary>
|
||||
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);
|
||||
|
||||
/// <summary>The inventory of an entry at <paramref name="path"/>: a folder's, or a single file's (a trash entry that is one).</summary>
|
||||
private static (int Files, long Bytes, string Inventory) EntryInventory(string path) => File.Exists(path) ? FileInventory(path) : Inventory(path);
|
||||
|
||||
/// <summary>A folder's files (leases left out): their count and bytes, and the SHA-256 of their relative names and sizes in ordinal order.</summary>
|
||||
internal static (int Files, long Bytes, string Inventory) Inventory(string folder)
|
||||
{
|
||||
var lines = new List<string>();
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,12 +28,16 @@ public sealed class ServerProjectHome : IProjectHome
|
||||
|
||||
/// <param name="serverName">The server's name as its <c>server/info</c> gives it, for the project file; the address's host when null.</param>
|
||||
/// <param name="settingsFormat">The settings format its <c>server/info</c> announces (<see cref="ServerInfo.SettingsFormat"/>); null, an older server's 1.</param>
|
||||
public ServerProjectHome(ChronosClient client, Guid sequenceId, string? serverName = null, int? settingsFormat = null)
|
||||
/// <param name="server">
|
||||
/// The key workspace projects name the server by (<see cref="ServerIdentity.Key(Guid?, Uri)"/>: its id key once it
|
||||
/// announced one, owner review 5); null takes the client's address key (<see cref="SyncService.ServerKey"/>).
|
||||
/// </param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="archiveId"/> is the archive id a project file of a sequence of the server <paramref name="keys"/> name
|
||||
/// carries (<see cref="ArchiveIdOf"/> of any of them: its id key, or an address key it was reached under before it
|
||||
/// announced its id, owner review 5).
|
||||
/// </summary>
|
||||
public static bool IsArchiveIdOf(Guid archiveId, IEnumerable<string> keys)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(keys);
|
||||
return keys.Any(key => !string.IsNullOrWhiteSpace(key) && ArchiveIdOf(key) == archiveId);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) =>
|
||||
Reach<IReadOnlyList<SequenceProject>>(async () =>
|
||||
[.. (await Client.ListProjectsAsync(SequenceId, cancellationToken).ConfigureAwait(false)).Select(ProjectWire.ToProject)], cancellationToken);
|
||||
|
||||
@@ -69,11 +69,15 @@ internal sealed record ProxyCacheIndex(SequenceDto Sequence, string? SettingsJso
|
||||
internal sealed partial class ProxyCacheJson : JsonSerializerContext;
|
||||
|
||||
/// <summary>
|
||||
/// Proxies of server sequences on this PC: <c><root>/<server hash>/<sequence id>/</c> with one JPEG per
|
||||
/// frame (named by frame id, so renumbering never mixes them up) and <c>remote.json</c>. 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 <c>remote.json</c>. With a servers root (owner review 5, #66) a sequence's folder is
|
||||
/// <c><servers>/<home folder>/proxies/<sequence id>/</c> (<see cref="HomeFolders.ServerFolder"/> of the
|
||||
/// server's canonical key); a folder still where an earlier layout put it (<c><root>/<address hash>/<sequence
|
||||
/// id>/</c>, 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.
|
||||
/// </summary>
|
||||
internal sealed class ProxyCache(string root)
|
||||
internal sealed class ProxyCache(string root, string? servers = null, Func<ServerIdentity>? identity = null)
|
||||
{
|
||||
/// <summary>The asset kind of a cached proxy (the server's name for it).</summary>
|
||||
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"));
|
||||
/// <summary>The 1.0 folder of a server sequence's proxies: <c><root>/<address hash>/<sequence id></c>.</summary>
|
||||
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"));
|
||||
|
||||
/// <summary>The folder of a server sequence's proxies: where it is now, or where a new one goes (see the class).</summary>
|
||||
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";
|
||||
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Protocol;
|
||||
|
||||
namespace Chronos.Data.Sync;
|
||||
|
||||
/// <summary>A server known by its stable id, with the address keys it was reached under (<see cref="SyncService.ServerKey"/>).</summary>
|
||||
/// <param name="Id">The server's id (<see cref="ServerInfo.ServerId"/>).</param>
|
||||
/// <param name="Aliases">The legacy address keys this id was reached under, in any case.</param>
|
||||
public sealed record KnownServerKey(Guid Id, IReadOnlyCollection<string> Aliases);
|
||||
|
||||
/// <summary>
|
||||
/// How the desktop names a server (owner review 5, #66). A server that announces its stable id
|
||||
/// (<see cref="ServerInfo.ServerId"/>) is keyed <c>id:<N></c> (<see cref="Key(Guid?, Uri)"/>), the same whatever
|
||||
/// address it is reached by; an older one keeps its address key (<see cref="SyncService.ServerKey"/>, the 1.0 key, here the
|
||||
/// <see cref="LegacyKey"/>). 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. <see cref="Canonical"/> turns an alias into its id
|
||||
/// key, <see cref="Same"/> compares two keys through it, and <see cref="FolderName"/> is the home's folder
|
||||
/// (<see cref="HomeFolders.ServerFolder"/>) of the canonical key. Keys compare ignoring case, as address keys always did.
|
||||
/// </summary>
|
||||
public sealed class ServerIdentity
|
||||
{
|
||||
private readonly Dictionary<string, string> _canonical = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, List<string>> _aliases = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>No server known by id: every key is its own.</summary>
|
||||
public static ServerIdentity None { get; } = new([]);
|
||||
|
||||
public ServerIdentity(IEnumerable<KnownServerKey> servers)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(servers);
|
||||
var known = new List<KnownServerKey>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The servers this table knows, as it was given them.</summary>
|
||||
public IReadOnlyList<KnownServerKey> Servers { get; }
|
||||
|
||||
/// <summary>The key of a server reached at <paramref name="address"/>: its id key when <paramref name="info"/> announces an id, else its address key.</summary>
|
||||
public static string Key(ServerInfo? info, Uri address) => Key(info?.ServerId, address);
|
||||
|
||||
/// <summary>The key of a server reached at <paramref name="address"/>: <c>id:<N></c> for an id, else its address key.</summary>
|
||||
public static string Key(Guid? serverId, Uri address)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(address);
|
||||
return serverId is { } id && id != Guid.Empty ? HomeFolders.IdKey(id) : LegacyKey(address);
|
||||
}
|
||||
|
||||
/// <summary>The key of a server known by its id.</summary>
|
||||
public static string IdKey(Guid serverId) => HomeFolders.IdKey(serverId);
|
||||
|
||||
/// <summary>The address key (<see cref="SyncService.ServerKey"/>): how 1.0 named every server, and how an older server is still named.</summary>
|
||||
public static string LegacyKey(Uri address) => SyncService.ServerKey(address);
|
||||
|
||||
/// <summary>The id of an id key; null for an address key.</summary>
|
||||
public static Guid? IdOf(string? key) => HomeFolders.IdOfKey(key);
|
||||
|
||||
/// <summary>The key names the server by its id.</summary>
|
||||
public static bool IsIdKey(string? key) => IdOf(key) is not null;
|
||||
|
||||
/// <summary>The id key an alias stands for; any other key as it is.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Both keys name one server: equal ignoring case, or one an alias of the other's id (null equals only null).</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Every key that names the server of <paramref name="key"/>: its canonical key first, then its aliases.</summary>
|
||||
public IReadOnlyList<string> KeysOf(string key)
|
||||
{
|
||||
var canonical = Canonical(key);
|
||||
var keys = new List<string> { canonical };
|
||||
if (_aliases.TryGetValue(canonical, out var aliases)) keys.AddRange(aliases);
|
||||
if (!keys.Contains(key, StringComparer.OrdinalIgnoreCase)) keys.Add(key);
|
||||
return keys;
|
||||
}
|
||||
|
||||
/// <summary>The aliases of a server id (its legacy address keys); empty when none is known.</summary>
|
||||
public IReadOnlyList<string> AliasesOf(Guid serverId) =>
|
||||
_aliases.TryGetValue(HomeFolders.IdKey(serverId), out var aliases) ? aliases : [];
|
||||
|
||||
/// <summary>The folder of the home of <paramref name="key"/> (<see cref="HomeFolders.ServerFolder"/> of its canonical key).</summary>
|
||||
public string FolderName(string key) => HomeFolders.ServerFolder(Canonical(key));
|
||||
|
||||
/// <summary>This table with <paramref name="alias"/> added to the aliases of <paramref name="serverId"/> (an address it was reached under).</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// Moves every link of <paramref name="from"/> to <paramref name="to"/> in one transaction (owner review 5, #66: an address
|
||||
/// key becomes the server's id key). Where <paramref name="to"/> already links the same server sequence, or the same local
|
||||
/// copy, the row compared last (<see cref="SyncLink.SyncedAt"/>, never compared counting oldest) is kept and the other
|
||||
/// deleted: link rows are recomputed by the next comparison, so nothing is lost.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>Records where a local sequence stands for the Library; touches neither its versions nor its update time.</summary>
|
||||
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);
|
||||
|
||||
@@ -115,7 +115,11 @@ public interface ISyncConnector
|
||||
}
|
||||
|
||||
/// <summary><see cref="ISyncRemote"/> over a signed-in <see cref="ChronosClient"/>, which it disposes when it owns it.</summary>
|
||||
public sealed class ChronosSyncRemote(ChronosClient client, bool ownsClient) : ISyncRemote
|
||||
/// <param name="server">
|
||||
/// The key links and homes name the server by (<see cref="ServerIdentity.Key(Guid?, Uri)"/>: its id key once it announced
|
||||
/// one, owner review 5); null takes the client's address key (<see cref="SyncService.ServerKey"/>).
|
||||
/// </param>
|
||||
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
|
||||
/// <summary>The settings format the server announced, and when it was read (a <see cref="System.Diagnostics.Stopwatch"/> timestamp).</summary>
|
||||
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<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
@@ -26,9 +26,23 @@ public sealed record SyncOptions
|
||||
/// <summary>Decoders, lease owner and lease timing for sidecar and project writes (shared with the archive engine).</summary>
|
||||
public ArchiveServiceOptions Archive { get; init; } = new();
|
||||
|
||||
/// <summary>Folder of the proxy cache of server sequences (inside the client's cache).</summary>
|
||||
/// <summary>
|
||||
/// Folder of the proxy cache of server sequences as 1.0 laid it out (<c><cache>\proxies\<address hash></c>):
|
||||
/// where proxies go without <see cref="ServersDirectory"/>, and where a sequence's proxies are still read from, with it,
|
||||
/// until they are moved into their server's home.
|
||||
/// </summary>
|
||||
public required string ProxyCacheDirectory { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The servers' homes in the client's cache (<c><cache>\servers</c>, owner review 5, #66): with it, a server's
|
||||
/// proxies go to <c><servers>\<home folder>\proxies\<sequence id></c>
|
||||
/// (<see cref="Core.IO.HomeFolders.ServerFolder"/>). Null keeps the 1.0 layout under <see cref="ProxyCacheDirectory"/>.
|
||||
/// </summary>
|
||||
public string? ServersDirectory { get; init; }
|
||||
|
||||
/// <summary>The alias table the cache finds a server's home by (<see cref="ServerIdentity"/>); null: every key is its own.</summary>
|
||||
public Func<ServerIdentity>? Identity { get; init; }
|
||||
|
||||
/// <summary>Concurrent frame downloads of one job.</summary>
|
||||
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; }
|
||||
|
||||
/// <summary>"https://nas.local:8443": the key links are stored under (scheme, lower-case host, port, path).</summary>
|
||||
/// <summary>
|
||||
/// "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
|
||||
/// (<see cref="ServerIdentity.Key(Guid?, Uri)"/>), its address keys kept as aliases.
|
||||
/// </summary>
|
||||
public static string ServerKey(Uri address) => address.GetLeftPart(UriPartial.Path).TrimEnd('/').ToLowerInvariant();
|
||||
|
||||
/// <summary>
|
||||
/// Moves the links of <paramref name="from"/> (an address key the server was reached under) to <paramref name="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 (<see cref="SyncLink.SyncedAt"/>); the other row goes, which loses nothing: the next
|
||||
/// comparison makes links again. Nothing happens when the keys are the same.
|
||||
/// </summary>
|
||||
/// <returns>The links moved, and those dropped for a newer one.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>What an upload or download job moves; null for any other job.</summary>
|
||||
public static SyncTransferKind? TransferKind(Job job) => SyncJobParameters.Read(job)?.Kind(job.Kind);
|
||||
|
||||
|
||||
@@ -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<WorkspaceSync> _syncs;
|
||||
private readonly ChronosDatabase _db;
|
||||
private readonly Func<BundleHome, IProjectHome?> _homes;
|
||||
|
||||
/// <param name="homes">The home of a workspace project; null for one this PC cannot reach now.</param>
|
||||
public WorkspaceCleaner(WorkspaceSync sync, ChronosDatabase db, Func<BundleHome, IProjectHome?> homes)
|
||||
: this([sync ?? throw new ArgumentNullException(nameof(sync))], db, homes)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(sync);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The clean-up of a workspace split into homes (owner review 5, <see cref="WorkspaceHomes"/>): one sync per home's store,
|
||||
/// and one run over all of them, so the size limit counts every home's projects together.
|
||||
/// </summary>
|
||||
/// <param name="syncs">The sync of each home's store.</param>
|
||||
/// <param name="homes">The home of a workspace project; null for one this PC cannot reach now.</param>
|
||||
public WorkspaceCleaner(IReadOnlyList<WorkspaceSync> syncs, ChronosDatabase db, Func<BundleHome, IProjectHome?> 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;
|
||||
}
|
||||
|
||||
/// <summary>The sync of the store <paramref name="project"/> is in.</summary>
|
||||
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];
|
||||
|
||||
/// <summary>How long a project another writer holds is waited for; past it the project is kept (<see cref="WorkspaceKeep.Jobs"/>).</summary>
|
||||
public TimeSpan LockWait { get; init; } = TimeSpan.FromSeconds(30);
|
||||
|
||||
@@ -120,7 +138,7 @@ public sealed class WorkspaceCleaner
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
var candidates = new List<Candidate>();
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// What a server home's folder says about its server (<see cref="HomeFolders.ServerFile"/>, 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.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>A record of <paramref name="key"/> first and last seen <paramref name="now"/>.</summary>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// Reads the record in <paramref name="folder"/>; null when there is none or it cannot be read (it only describes the
|
||||
/// folder: nothing depends on it).
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Writes <paramref name="record"/> into <paramref name="folder"/> (made when it is not there), whole and atomically.</summary>
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One home of a workspace (<see cref="WorkspaceHomes.Homes"/>): its folder and store, and for a server home its key and record.</summary>
|
||||
/// <param name="Folder">The folder's name: <see cref="HomeFolders.Local"/>, a server folder (<see cref="HomeFolders.ServerFolder"/>), or empty for the projects still in the parent.</param>
|
||||
/// <param name="ServerKey">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.</param>
|
||||
public sealed record WorkspaceHome(string Folder, WorkspaceStore Store, string? ServerKey, ServerHomeRecord? Server)
|
||||
{
|
||||
/// <summary>This PC's archive's home.</summary>
|
||||
public bool IsLocal => Folder == HomeFolders.Local;
|
||||
|
||||
/// <summary>Projects 1.0 kept in the parent itself, not moved into their home yet (in use when the folders were sorted).</summary>
|
||||
public bool IsLegacy => Folder.Length == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The desktop's workspace split into one store per home (owner review 5, #66): Settings › Workspace › Folder is the
|
||||
/// parent, whose <c>workspace.json</c> is of format <see cref="ParentFormatVersion"/> with layout <see cref="HomesLayout"/>
|
||||
/// (a 1.0 Chronos refuses it as newer, so it never shows the workspace empty), and each home keeps its projects in its own
|
||||
/// <see cref="WorkspaceStore"/>: <c><parent>\local</c> for this PC's archive and <c><parent>\servers\<folder></c>
|
||||
/// for each server (<see cref="ServerIdentity.FolderName"/>, with a <see cref="ServerHomeRecord"/> beside its manifest). Each
|
||||
/// is an ordinary store with its own manifest and <c>.trash</c>, 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 <see cref="Chronos.Data.Homes.HomeLayoutMigration"/> sorts it; projects that sorting could not move
|
||||
/// (in use then) are still listed where they are. A new, empty parent is made split.
|
||||
/// </summary>
|
||||
public sealed class WorkspaceHomes
|
||||
{
|
||||
/// <summary>The format of a split parent's manifest (a store's is <see cref="WorkspaceStore.FormatVersion"/>).</summary>
|
||||
public const int ParentFormatVersion = 2;
|
||||
|
||||
/// <summary>The layout a split parent's manifest names.</summary>
|
||||
public const string HomesLayout = "homes";
|
||||
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<string, WorkspaceStore> _stores = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly WorkspaceStore? _legacy;
|
||||
private ServerIdentity _identity;
|
||||
|
||||
/// <summary>Opens the workspace at <paramref name="root"/>, making the folder, and a split manifest when it holds none and no project.</summary>
|
||||
/// <param name="identity">The servers known by id and their aliases (<see cref="Identity"/>).</param>
|
||||
/// <exception cref="InvalidDataException">The folder's <c>workspace.json</c> is not a workspace manifest, or a newer one.</exception>
|
||||
/// <exception cref="IOException">The folder cannot be made or read.</exception>
|
||||
public 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);
|
||||
}
|
||||
|
||||
/// <summary>The parent folder (Settings › Workspace › Folder).</summary>
|
||||
public string Root { get; }
|
||||
|
||||
/// <summary>The parent's manifest: a split one, or the 1.0 store's.</summary>
|
||||
public WorkspaceManifest Manifest { get; }
|
||||
|
||||
/// <summary>The parent is split into homes; false while it is in the 1.0 layout (one store for every home).</summary>
|
||||
public bool IsSplit => Manifest.Layout == HomesLayout;
|
||||
|
||||
/// <summary>The servers known by id and their aliases: a server home's folder is named by its canonical key.</summary>
|
||||
public ServerIdentity Identity
|
||||
{
|
||||
get => Volatile.Read(ref _identity);
|
||||
set => Volatile.Write(ref _identity, value ?? ServerIdentity.None);
|
||||
}
|
||||
|
||||
/// <summary>The folder of this PC's home (whether it exists or not).</summary>
|
||||
public string LocalRoot => HomeFolders.LocalPath(Root);
|
||||
|
||||
/// <summary>The folder of the home of server <paramref name="serverKey"/> (whether it exists or not).</summary>
|
||||
public string ServerRoot(string serverKey)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(serverKey);
|
||||
return Path.Combine(Root, HomeFolders.Servers, Identity.FolderName(serverKey));
|
||||
}
|
||||
|
||||
/// <summary>The folder of the store a project of <paramref name="home"/> belongs in: its home's, or the parent while it is not split.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The store a project of <paramref name="home"/> is checked out into: its home's (made with its manifest, and for a server
|
||||
/// its <see cref="ServerHomeRecord"/>, when it is not there), or the parent's while it is not split.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The store of this PC's home (the parent's while it is not split).</summary>
|
||||
public WorkspaceStore Local => StoreFor(new BundleHome(BundleHome.ArchiveKind, Guid.Empty));
|
||||
|
||||
/// <summary>
|
||||
/// Records what is known of server <paramref name="serverKey"/> in its home's <see cref="ServerHomeRecord"/> (made with the
|
||||
/// home when it is not there; its first sighting kept); nothing while the parent is not split.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
public IReadOnlyList<WorkspaceHome> Homes()
|
||||
{
|
||||
var homes = new List<WorkspaceHome>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The stores of every home that has a folder (<see cref="Homes"/>).</summary>
|
||||
public IReadOnlyList<WorkspaceStore> All() => [.. Homes().Select(h => h.Store)];
|
||||
|
||||
/// <summary>The projects of every home.</summary>
|
||||
public IReadOnlyList<WorkspaceProject> List() => [.. All().SelectMany(s => s.List())];
|
||||
|
||||
/// <summary>The working states of every home's projects (one unreadable is left out, and logged).</summary>
|
||||
public IReadOnlyList<(WorkspaceProject Project, ProjectBundle Bundle)> ListBundles() => [.. All().SelectMany(s => s.ListBundles())];
|
||||
|
||||
/// <summary>The project's folder in whichever home holds it; null when none does.</summary>
|
||||
public WorkspaceProject? Find(Guid projectId)
|
||||
{
|
||||
foreach (var store in All())
|
||||
{
|
||||
if (store.Find(projectId) is { } project) return project;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>The trash of every home (<see cref="WorkspaceTrash.List"/>), each listed with its own fingerprint.</summary>
|
||||
/// <exception cref="IOException">A trash cannot be read.</exception>
|
||||
public IReadOnlyList<WorkspaceTrashListing> Trash(CancellationToken cancellationToken = default) =>
|
||||
[.. All().Select(s => WorkspaceTrash.List(s.Root, cancellationToken))];
|
||||
|
||||
/// <summary>The folder at <paramref name="root"/> is the parent of a workspace split into homes (its manifest says so).</summary>
|
||||
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 };
|
||||
}
|
||||
|
||||
/// <summary>The folder holds a folder named by a project id with a working state (the 1.0 layout).</summary>
|
||||
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)));
|
||||
|
||||
/// <summary>The manifest of a split parent, made now; another program's made at the same moment wins.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
/// <param name="CurrentRoot">
|
||||
/// 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, <see cref="WorkspaceHomes"/>) 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.
|
||||
/// </param>
|
||||
/// <param name="SameServer">
|
||||
/// Whether two server keys name one server (<see cref="ServerIdentity.Same"/> 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 (<see cref="IsSameServer"/>).
|
||||
/// </param>
|
||||
public sealed record WorkspaceJobOptions(ProcessingOptions Processing, Func<bool> HoldTransfers, Func<string, string, Task>? Moved = null,
|
||||
Func<string?>? CurrentRoot = null)
|
||||
Func<string?>? CurrentRoot = null, Func<string?, string?, bool>? SameServer = null)
|
||||
{
|
||||
/// <summary>Both keys name one server (<see cref="SameServer"/>, else equal ignoring case; null equals only null).</summary>
|
||||
public bool IsSameServer(string? a, string? b) =>
|
||||
SameServer is { } same ? same(a, b) : string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>The options of an engine without a processing service of its own: the archive engine's decoders, nothing held, no move followed.</summary>
|
||||
public static WorkspaceJobOptions For(ArchiveService archives)
|
||||
{
|
||||
@@ -204,10 +216,16 @@ internal static class WorkspaceJobRoots
|
||||
/// resource key) when no folder holds the project.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">The workspace is being moved, or no folder holds the project.</exception>
|
||||
/// <remarks>
|
||||
/// A folder that is a home of a workspace split into homes (owner review 5: <c><parent>\local</c>,
|
||||
/// <c><parent>\servers\<folder></c>) 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.
|
||||
/// </remarks>
|
||||
public static string Resolve(string named, Guid projectId, Func<string?>? current, string missing)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(named);
|
||||
var candidates = new List<string>();
|
||||
var folders = new List<string>();
|
||||
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<string>();
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>The split parent <paramref name="root"/> is a home of (<c>local</c>, or a folder in <c>servers</c>); null for any other folder.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The homes of the split parent <paramref name="parent"/> that have a folder: this PC's, then the servers'.</summary>
|
||||
private static IEnumerable<string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,8 +79,9 @@ internal sealed partial class WorkspaceMoveJson : JsonSerializerContext;
|
||||
|
||||
/// <summary>
|
||||
/// Moves a workspace folder with its projects (phase 5c, <see cref="Core.Model.JobKind.WorkspaceMove"/>). What moves: every
|
||||
/// folder named by a project id, then <c>workspace.json</c>, then <c>.trash</c>, in that order; anything else in the folder
|
||||
/// stays where it is. On one volume each entry is renamed into the target (<see cref="Directory.Move(string, string)"/>);
|
||||
/// folder named by a project id, then the homes' folders of a workspace split into homes (<c>local</c>, <c>servers</c>,
|
||||
/// owner review 5), then <c>workspace.json</c>, then <c>.trash</c>, in that order; anything else in the folder stays where it
|
||||
/// is. On one volume each entry is renamed into the target (<see cref="Directory.Move(string, string)"/>);
|
||||
/// across volumes each file is copied into a <c>.part</c> 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";
|
||||
|
||||
/// <summary>The homes' folders of a workspace split into homes, which move whole after the 1.0 project folders and before the manifest.</summary>
|
||||
private static readonly string[] HomeEntries = [HomeFolders.Local, HomeFolders.Servers];
|
||||
|
||||
/// <summary>The project folders in a home's folder: <c>local</c>'s own, or those of every server home in <c>servers</c>.</summary>
|
||||
private static IEnumerable<string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The projects a move took: its 1.0 project folders, and the project folders of the homes it moved (now at the target).</summary>
|
||||
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());
|
||||
|
||||
/// <summary>The log line of each project an entry moved: the entry's own, or those of a home's folder.</summary>
|
||||
private static void LogProjects(string entry, string folder, Action<string>? 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;
|
||||
|
||||
@@ -11,7 +11,12 @@ using Chronos.Data.Sync;
|
||||
namespace Chronos.Data.Workspaces;
|
||||
|
||||
/// <summary>The manifest of a workspace folder (<c>workspace.json</c>).</summary>
|
||||
public sealed record WorkspaceManifest(string Format, int FormatVersion, Guid Id, DateTimeOffset Created);
|
||||
/// <param name="Layout">
|
||||
/// <see cref="WorkspaceHomes.HomesLayout"/> in the manifest of format <see cref="WorkspaceHomes.ParentFormatVersion"/> 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.
|
||||
/// </param>
|
||||
public sealed record WorkspaceManifest(string Format, int FormatVersion, Guid Id, DateTimeOffset Created, string? Layout = null);
|
||||
|
||||
/// <summary>
|
||||
/// A workspace's sync state of one project besides its versions (<c>sync.json</c>): 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;
|
||||
|
||||
/// <summary>
|
||||
/// The workspace of this PC (owner review 2, R8): a folder on a fast disk where projects are edited, never inside the
|
||||
/// library. <c><root>/workspace.json</c> names it; each project has a folder named by its id
|
||||
/// (<see cref="WorkspaceProject"/>). There is no index: the store lists the folders holding a <c>project.chronos</c>. The
|
||||
/// desktop puts it in <c>%LOCALAPPDATA%\Chronos\workspace</c> unless set otherwise; the store takes any folder.
|
||||
/// store takes any folder; the desktop's workspace (<c>%LOCALAPPDATA%\Chronos\workspace</c> unless set otherwise) is split
|
||||
/// into one store per home since owner review 5 (<see cref="WorkspaceHomes"/>: <c>local</c>, <c>servers\<id></c>).
|
||||
/// </summary>
|
||||
public sealed class WorkspaceStore
|
||||
{
|
||||
@@ -168,6 +175,17 @@ public sealed class WorkspaceStore
|
||||
Manifest = Read(path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The projects a workspace split into homes still holds where 1.0 kept them (<c><parent>/<project id></c>: one in
|
||||
/// use when the folders were sorted, owner review 5): a view of the parent with its manifest, which makes and writes nothing
|
||||
/// (<see cref="WorkspaceHomes"/>).
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The manifest of the folder <paramref name="path"/> names, of any format (a store's or a parent's of homes); null when it is not one.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Writes <paramref name="manifest"/> whole to a temporary file and moves it into place.</summary>
|
||||
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;
|
||||
|
||||
@@ -17,6 +17,11 @@ public sealed record ServerLimits(long MaxUploadBytes, int ChunkSizeBytes, int P
|
||||
/// The highest project settings format the server reads and renders (<c>ProjectSettings.CurrentVersion</c>, 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.
|
||||
/// </param>
|
||||
/// <param name="ServerId">
|
||||
/// 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.
|
||||
/// </param>
|
||||
public sealed record ServerInfo(
|
||||
string Name,
|
||||
string Version,
|
||||
@@ -26,7 +31,8 @@ public sealed record ServerInfo(
|
||||
IReadOnlyList<string> Formats,
|
||||
ServerLimits Limits,
|
||||
IReadOnlyList<string>? Extensions = null,
|
||||
int? SettingsFormat = null);
|
||||
int? SettingsFormat = null,
|
||||
Guid? ServerId = null);
|
||||
|
||||
/// <summary>Names of <see cref="ServerInfo.Capabilities"/> that clients act on.</summary>
|
||||
public static class ServerCapabilities
|
||||
|
||||
@@ -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; }
|
||||
|
||||
/// <summary>
|
||||
/// The server's stable id (<see cref="ServerInfo.ServerId"/>), kept in its database's settings
|
||||
/// (<see cref="Hosting.ServerHost.ServerIdKey"/>); null only where a host built the state without a database.
|
||||
/// </summary>
|
||||
public Guid? ServerId { get; init; }
|
||||
|
||||
/// <summary>Engine and version of the database ("PostgreSQL 17.2", "SQLite 3.45.1").</summary>
|
||||
public string Database { get; init; } = string.Empty;
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The app setting that holds the server's stable id (<see cref="ServerInfo.ServerId"/>).</summary>
|
||||
public const string ServerIdKey = "server.id";
|
||||
|
||||
/// <summary>
|
||||
/// The server's stable id (owner review 5, #66): read from its database's app settings (<see cref="ServerIdKey"/>), 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.
|
||||
/// </summary>
|
||||
public static async Task<Guid> 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;
|
||||
}
|
||||
|
||||
/// <summary>The single composition root of the server's services.</summary>
|
||||
public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Foundation;
|
||||
|
||||
/// <summary>The folders of the homes (owner review 5, #66): this PC's, a server's by id, a server's by address hash.</summary>
|
||||
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<ArgumentException>(() => 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>Workspaces and caches laid out as 1.0 left them, for the sorting into homes (owner review 5, #66).</summary>
|
||||
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";
|
||||
|
||||
/// <summary>A working state of <paramref name="home"/> written into <c><root>/<id></c> with some media, versions and a render.</summary>
|
||||
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);
|
||||
|
||||
/// <summary>A 1.0 workspace: its manifest (format 1) and the folder.</summary>
|
||||
public static string LegacyWorkspace(string parent)
|
||||
{
|
||||
var root = Path.Combine(parent, "workspace");
|
||||
_ = new WorkspaceStore(root);
|
||||
return root;
|
||||
}
|
||||
|
||||
/// <summary>A trash entry of <paramref name="projectId"/> holding a working state, as a removal leaves it.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The 1.0 proxy cache of <paramref name="key"/>'s sequence <paramref name="remoteId"/>.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>A live lease on a project folder, as another Chronos working on the same workspace holds it.</summary>
|
||||
public static LeaseFile Hold(string projectFolder) =>
|
||||
LeaseFile.TryAcquire(projectFolder, ".lock", "workspace@other", TimeSpan.FromMinutes(5)) ?? throw new InvalidOperationException("The lease is held.");
|
||||
|
||||
/// <summary>
|
||||
/// The SHA-256 of every file under <paramref name="root"/> 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).
|
||||
/// </summary>
|
||||
public static SortedDictionary<string, string> Manifest(string root)
|
||||
{
|
||||
var manifest = new SortedDictionary<string, string>(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;
|
||||
}
|
||||
|
||||
/// <summary>A path without the home folders it lies in (<c>local/</c>, <c>servers/<folder>/</c>).</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>The one-time sorting of a 1.0 workspace and cache into homes (owner review 5, #66).</summary>
|
||||
[Category("homes")]
|
||||
public sealed class HomeLayoutMigrationTests(TestContext context)
|
||||
{
|
||||
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
|
||||
|
||||
/// <summary>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.</summary>
|
||||
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<string, string>? 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,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Every file and folder under <paramref name="root"/> with its size and time: what a dry run must leave as it was.</summary>
|
||||
private static List<string> 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<IOException>(() => 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<IOException>(() => 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<HomeLayoutException>(() => 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>A server's home moves from its address to its id when it first announces one (owner review 5, #66).</summary>
|
||||
[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<ArgumentException>(() => 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);
|
||||
}
|
||||
|
||||
/// <summary>A re-key moves the server's links in the index too.</summary>
|
||||
[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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>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.</summary>
|
||||
[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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>How the desktop names a server (owner review 5, #66): by its announced id, its address keys kept as aliases.</summary>
|
||||
[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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>A server's links move from its address key to its id key (owner review 5, #66), the newer row winning a clash.</summary>
|
||||
[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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>The workspace split into one store per home (owner review 5, #66).</summary>
|
||||
[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<InvalidDataException>(() => 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<InvalidOperationException>(() => 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<InvalidOperationException>(() => 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<string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>The server's stable id announced by <c>/server/info</c> (owner review 5, #66).</summary>
|
||||
[Category("server")]
|
||||
public sealed class ServerInfoIdTests(TestContext context)
|
||||
{
|
||||
private static async Task<ServerInfo> 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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user