feat(archive): lay out the archive's whole folder tree wherever its root is

The owner asked (2026-11-06) that whatever path is given as the archive
root, on the server in Docker or on Windows, Chronos creates inside it
every folder the archive keeps. Until now only .chronos was made up
front; locks, journal, staging, trash and its photos and videos areas
appeared on first use, and an archive copied or made by an older build
could lack them.

ArchiveStructure.Ensure (Core) creates the missing folders of BRIEF §7
idempotently, never touching existing folders or files, and reports why
it could not (drive or share unavailable, permission denied, read-only,
a file in the way) as one localized sentence. ArchiveService runs it
when it creates an archive (refused with the reason when the tree cannot
be made) and at every open. The server checks it at start-up and on
each dashboard read: it logs a warning, the dashboard shows an "Archive
incomplete" chip with the reason, and /health reports archive
"incomplete". The desktop runs it when an archive is created, adopted or
opened at start, and a folder typed in Settings that is not an archive
yet becomes one in place, as Welcome's Create does; a writable archive
whose tree cannot be completed is refused with the reason.

Refs #31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-06 17:05:57 +01:00
1 parent fe50b87726
commit a5facf0b5b
24 files changed
+605 -21

No files matched your search

@@ -19,6 +19,15 @@ public static class ArchiveLayout
/// <summary>Resumable plans of mass operations (rename).</summary>
public const string JournalFolder = "journal";
/// <summary>Deleted sequences, photos and renders inside the root <see cref="MetaFolder"/>, kept until Empty.</summary>
public const string TrashFolder = "trash";
/// <summary>The photo entries inside <see cref="TrashFolder"/>.</summary>
public const string TrashPhotosFolder = "photos";
/// <summary>The deleted renders inside <see cref="TrashFolder"/> (owner review 1, R9).</summary>
public const string TrashVideosFolder = "videos";
public const string OriginalsFolder = "originals";
public const string ProxiesFolder = "proxies";
public const string ExportsFolder = "exports";
@@ -86,6 +95,9 @@ public sealed class ArchivePaths
public string LocksDirectory => Path.Combine(MetaDirectory, ArchiveLayout.LocksFolder);
public string StagingDirectory => Path.Combine(MetaDirectory, ArchiveLayout.StagingFolder);
public string JournalDirectory => Path.Combine(MetaDirectory, ArchiveLayout.JournalFolder);
public string TrashDirectory => Path.Combine(MetaDirectory, ArchiveLayout.TrashFolder);
public string TrashPhotosDirectory => Path.Combine(TrashDirectory, ArchiveLayout.TrashPhotosFolder);
public string TrashVideosDirectory => Path.Combine(TrashDirectory, ArchiveLayout.TrashVideosFolder);
/// <summary>Paths of the sequence stored in <paramref name="relativeFolder"/> (forward slashes).</summary>
public SequencePaths Sequence(string relativeFolder) => new(this, relativeFolder);
@@ -0,0 +1,134 @@
using Chronos.Core.Localization;
namespace Chronos.Core.Archiving;
/// <summary>Why the folders of an archive's layout could not all be created (<see cref="ArchiveStructure.Ensure"/>).</summary>
public enum ArchiveStructureProblem
{
None,
/// <summary>The drive or share of the archive is not there (unplugged, disconnected, not ready).</summary>
Unavailable,
/// <summary>The account Chronos runs as may not create folders there.</summary>
Denied,
/// <summary>The drive or share is read-only (write-protected disk, read-only mount).</summary>
ReadOnly,
/// <summary>Any other failure, a file where a folder belongs included.</summary>
Failed,
}
/// <summary>What <see cref="ArchiveStructure.Ensure"/> found and did.</summary>
/// <param name="Root">The archive root.</param>
/// <param name="Created">The folders created, parents first.</param>
/// <param name="Problem">Why the layout is not complete; <see cref="ArchiveStructureProblem.None"/> when it is.</param>
/// <param name="Folder">The folder that could not be created; null when the layout is complete.</param>
/// <param name="Detail">The system's own text of the failure, for the log; null when the layout is complete.</param>
public sealed record ArchiveStructureReport(string Root, IReadOnlyList<string> Created, ArchiveStructureProblem Problem, string? Folder, string? Detail)
{
public bool IsComplete => Problem == ArchiveStructureProblem.None;
/// <summary>The problem as one localized sentence naming the folder; null when the layout is complete.</summary>
public string? Message => Problem switch
{
ArchiveStructureProblem.None => null,
ArchiveStructureProblem.Unavailable => Loc.L("Archive.Structure.Unavailable", Folder ?? Root),
ArchiveStructureProblem.Denied => Loc.L("Archive.Structure.Denied", Folder ?? Root),
ArchiveStructureProblem.ReadOnly => Loc.L("Archive.Structure.ReadOnly", Folder ?? Root),
_ => Loc.L("Archive.Structure.Failed", Folder ?? Root),
};
}
/// <summary>The folders of an archive's layout could not all be created; the message is one localized sentence.</summary>
public sealed class ArchiveStructureException(ArchiveStructureReport report) : IOException(report.Message)
{
public ArchiveStructureReport Report { get; } = report;
}
/// <summary>
/// The folders every archive holds (BRIEF §7) whatever made it: the root's <c>.chronos</c> with <c>locks</c>,
/// <c>journal</c>, <c>staging</c> and <c>trash</c>, whose <c>photos</c> and <c>videos</c> areas hold the deleted photos and
/// renders. <c>archive.json</c> is the archive service's (it needs a name and an id); the folders of a sequence
/// (<c>originals</c>, <c>proxies</c>, <c>exports</c>, its <c>.chronos</c>) are made when the sequence is, and the
/// trash's <c>.erasing</c> and <c>.erased</c> only when Empty needs them.
/// </summary>
public static class ArchiveStructure
{
private const int WriteProtected = unchecked((int)0x80070013); // ERROR_WRITE_PROTECT
private const int NotReady = unchecked((int)0x80070015); // ERROR_NOT_READY
private const int BadNetworkPath = unchecked((int)0x80070035); // ERROR_BAD_NETPATH
private const int NetworkNameDeleted = unchecked((int)0x80070040); // ERROR_NETNAME_DELETED
private const int BadNetworkName = unchecked((int)0x80070043); // ERROR_BAD_NET_NAME
private const int UnixReadOnlyFileSystem = 30; // EROFS, which .NET keeps as the IOException's HResult
private const int UnixNoDevice = 19; // ENODEV
/// <summary>The layout's folders of <paramref name="archive"/>, parents first, the root included.</summary>
public static IReadOnlyList<string> Folders(ArchivePaths archive)
{
ArgumentNullException.ThrowIfNull(archive);
return [archive.Root, archive.MetaDirectory, archive.LocksDirectory, archive.JournalDirectory, archive.StagingDirectory,
archive.TrashDirectory, archive.TrashPhotosDirectory, archive.TrashVideosDirectory];
}
/// <summary>The layout's folders that are not there, parents first; nothing is created.</summary>
public static IReadOnlyList<string> Missing(ArchivePaths archive) => [.. Folders(archive).Where(f => !Directory.Exists(f))];
/// <summary>
/// Creates the layout's folders that are missing, never touching what is there: an existing folder and every file
/// stay as they are, and a file where a folder belongs is reported, not replaced. Safe to run any number of times
/// and by several processes at once. Stops at the first folder that cannot be created and says why; never throws for
/// the file system.
/// </summary>
/// <param name="archive">The archive root.</param>
/// <param name="createRoot">Whether a missing root is created (a new archive); otherwise it is reported unavailable.</param>
/// <param name="createDirectory">Creates one folder (<see cref="Directory.CreateDirectory(string)"/>; tests replace it).</param>
public static ArchiveStructureReport Ensure(ArchivePaths archive, bool createRoot = false, Action<string>? createDirectory = null)
{
ArgumentNullException.ThrowIfNull(archive);
createDirectory ??= static path => Directory.CreateDirectory(path);
var created = new List<string>();
if (!Directory.Exists(archive.Root) && (!createRoot || !VolumeIsThere(archive.Root)))
return new(archive.Root, created, ArchiveStructureProblem.Unavailable, archive.Root, null);
foreach (var folder in Folders(archive))
{
if (Directory.Exists(folder)) continue;
try
{
createDirectory(folder);
created.Add(folder);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
return new(archive.Root, created, Classify(ex), folder, ex.Message);
}
}
return new(archive.Root, created, ArchiveStructureProblem.None, null, null);
}
/// <summary>Why creating a folder failed, from the exception the file system threw.</summary>
public static ArchiveStructureProblem Classify(Exception ex) => ex switch
{
UnauthorizedAccessException => ArchiveStructureProblem.Denied,
DirectoryNotFoundException or DriveNotFoundException => ArchiveStructureProblem.Unavailable,
IOException { HResult: WriteProtected } => ArchiveStructureProblem.ReadOnly,
IOException { HResult: NotReady or BadNetworkPath or NetworkNameDeleted or BadNetworkName } => ArchiveStructureProblem.Unavailable,
IOException { HResult: UnixReadOnlyFileSystem } when !OperatingSystem.IsWindows() => ArchiveStructureProblem.ReadOnly,
IOException { HResult: UnixNoDevice } when !OperatingSystem.IsWindows() => ArchiveStructureProblem.Unavailable,
_ => ArchiveStructureProblem.Failed,
};
/// <summary>Whether the drive or share <paramref name="root"/> lives on is there.</summary>
private static bool VolumeIsThere(string root)
{
try
{
return Path.GetPathRoot(root) is { Length: > 0 } volume && Directory.Exists(volume);
}
catch (Exception ex) when (ex is ArgumentException or IOException or UnauthorizedAccessException)
{
return false;
}
}
}