The desktop keyed every server by the address it was reached at, so one
NAS reached by name and by IP, or after an address change, looked like two
servers, and all servers shared one workspace and one cache. Each server
now announces a stable id, and the desktop's folders are sorted into one
home per server, so that every server can have its own archive of
downloaded originals (or5 answers Q1).
- The server makes a Guid v7 once, keeps it as the app setting server.id
(so it survives restarts and restores, on SQLite and Postgres) and
announces it as ServerInfo.ServerId. Older clients ignore the field, and
older servers read as null.
- ServerIdentity keys a server as id:<N> when it announces an id, and by
its address otherwise. Address keys are kept as aliases.
- HomeFolders names the homes: local, servers/<id> or
servers/addr-<hash16>. The hash equals the 1.0 proxy cache folder name.
It also names the per-server originals archive root.
- WorkspaceHomes splits the workspace into one format-1 store per home
under a format-2 parent manifest, which 1.0 refuses. A 1.0 workspace
stays one store until it is sorted.
- HomeLayoutMigration plans as a dry run and applies through a journal:
- only same-folder renames, each checked by inventory;
- a clash keeps the copy saved last and sends the other to the trash;
- it resumes after a crash at any step;
- it skips leased projects;
- it defers an offline workspace or one being moved, while the cache
part still runs.
- RekeyAsync moves an alias home to the id home: whole folders, or
project-by-project merges. It also rewrites working states under their
lease, and moves sync_links (the newer row wins a clash).
- ProxyCache, the job root resolver, WorkspaceMover and WorkspaceCleaner
understand the split layout. The new layout is opt-in through
SyncOptions.ServersDirectory, so the app sees no change until it is
wired.
Refs #66
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
6.6 KiB
C#
123 lines
6.6 KiB
C#
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;
|
|
}
|
|
}
|