refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
The owner renamed the product and moved the repository to the self-hosted Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control, and asked for Actions and a first automatic release with the server package and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md). - Chronos everywhere: a byte-exact rename of contents and paths, then a per-language review of the forms the old name's ending had dictated. - Gitea instead of GitHub: the update check, licence, issue and support links read ChronosReleasesUrl; the update check takes the highest stable version, offers a pre-release build its final release and reports a repository it cannot see as a failed check. EULA, security policy and Gitea issue forms live at the repository root. - Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release, nightly image and performance gates in the .NET SDK container with git checkout and the system SQLite. The release cross-compiles the Windows programs, checks their icon, version and DPI manifest, packs installers and zips, publishes the Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER). - New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the docker-exec script, which Gitea's service containers do not allow; the throw-away server runs without durability) and pe-check. - Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR, 3 on the shared runner; the weekly performance gates keep theirs). BREAKING CHANGE: programs, settings, data folders, file types, environment variables and the Docker image use the new name; nothing is migrated from Titano, which never had a release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #6.
This commit is contained in:
1 parent
c99842cc86
commit
f2b58522b9
1801 files changed
+11804
-10676
No files matched your search
@@ -0,0 +1,46 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>Source-generated metadata for every JSON file of the archive layout.</summary>
|
||||
[JsonSourceGenerationOptions(
|
||||
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
AllowTrailingCommas = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip)]
|
||||
[JsonSerializable(typeof(ArchiveManifest))]
|
||||
[JsonSerializable(typeof(SequenceManifest))]
|
||||
[JsonSerializable(typeof(LeaseRecord))]
|
||||
[JsonSerializable(typeof(IngestJournal))]
|
||||
[JsonSerializable(typeof(RenameJournal))]
|
||||
internal sealed partial class ArchiveJson : JsonSerializerContext;
|
||||
|
||||
/// <summary>Indented output for the small files people may open (<c>archive.json</c>, rename journals).</summary>
|
||||
[JsonSourceGenerationOptions(
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(ArchiveManifest))]
|
||||
[JsonSerializable(typeof(RenameJournal))]
|
||||
internal sealed partial class ArchiveJsonIndented : JsonSerializerContext;
|
||||
|
||||
/// <summary>
|
||||
/// Writes enum names and reads unknown names (written by a newer build) or numbers as the default value instead of
|
||||
/// failing, so a sidecar from a newer Chronos is still readable.
|
||||
/// </summary>
|
||||
public sealed class TolerantEnumConverter<TEnum> : JsonConverter<TEnum>
|
||||
where TEnum : struct, Enum
|
||||
{
|
||||
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.String && Enum.TryParse<TEnum>(reader.GetString(), ignoreCase: true, out var value)
|
||||
&& Enum.IsDefined(value))
|
||||
return value;
|
||||
if (reader.TokenType is JsonTokenType.StartObject or JsonTokenType.StartArray) reader.Skip();
|
||||
return default;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using System.Globalization;
|
||||
using Chronos.Core.IO;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>Names of the fixed files and folders of the archive layout (BRIEF §7).</summary>
|
||||
public static class ArchiveLayout
|
||||
{
|
||||
/// <summary>Metadata folder at the archive root and inside every sequence folder.</summary>
|
||||
public const string MetaFolder = ".chronos";
|
||||
|
||||
public const string ArchiveManifestFile = "archive.json";
|
||||
public const string IndexSnapshotFile = "index-snapshot.bin";
|
||||
public const string LocksFolder = "locks";
|
||||
|
||||
/// <summary>Verified copies waiting to be committed; on the archive volume so commits are renames.</summary>
|
||||
public const string StagingFolder = "staging";
|
||||
|
||||
/// <summary>Resumable plans of mass operations (rename).</summary>
|
||||
public const string JournalFolder = "journal";
|
||||
|
||||
public const string OriginalsFolder = "originals";
|
||||
public const string ProxiesFolder = "proxies";
|
||||
public const string ExportsFolder = "exports";
|
||||
public const string SequenceManifestFile = "sequence.json";
|
||||
public const string AnalysisFile = "analysis.json";
|
||||
public const string ProjectFile = "project.json";
|
||||
public const string ThumbsPackFile = "thumbs.pack";
|
||||
|
||||
/// <summary>Moves of an ingest batch that were started but not yet recorded in <see cref="SequenceManifestFile"/>.</summary>
|
||||
public const string IngestJournalFile = "ingest.pending.json";
|
||||
|
||||
/// <summary>Folder names inside a sequence folder that never hold a sequence of their own.</summary>
|
||||
public static bool IsReservedFolder(string name) =>
|
||||
name.Equals(MetaFolder, StringComparison.OrdinalIgnoreCase)
|
||||
|| name.Equals(OriginalsFolder, StringComparison.OrdinalIgnoreCase)
|
||||
|| name.Equals(ProxiesFolder, StringComparison.OrdinalIgnoreCase)
|
||||
|| name.Equals(ExportsFolder, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary><c>0001.jpg</c> for frame index 0: proxies are numbered from 1 like the frames of a render.</summary>
|
||||
public static string ProxyFileName(int index) => (index + 1).ToString("0000", CultureInfo.InvariantCulture) + ".jpg";
|
||||
}
|
||||
|
||||
/// <summary>Absolute paths of one archive root.</summary>
|
||||
public sealed class ArchivePaths
|
||||
{
|
||||
public ArchivePaths(string root)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(root);
|
||||
Root = LongPath.Normalize(Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)));
|
||||
MetaDirectory = Path.Combine(Root, ArchiveLayout.MetaFolder);
|
||||
}
|
||||
|
||||
public string Root { get; }
|
||||
public string MetaDirectory { get; }
|
||||
public string ManifestPath => Path.Combine(MetaDirectory, ArchiveLayout.ArchiveManifestFile);
|
||||
public string SnapshotPath => Path.Combine(MetaDirectory, ArchiveLayout.IndexSnapshotFile);
|
||||
public string LocksDirectory => Path.Combine(MetaDirectory, ArchiveLayout.LocksFolder);
|
||||
public string StagingDirectory => Path.Combine(MetaDirectory, ArchiveLayout.StagingFolder);
|
||||
public string JournalDirectory => Path.Combine(MetaDirectory, ArchiveLayout.JournalFolder);
|
||||
|
||||
/// <summary>Paths of the sequence stored in <paramref name="relativeFolder"/> (forward slashes).</summary>
|
||||
public SequencePaths Sequence(string relativeFolder) => new(this, relativeFolder);
|
||||
|
||||
/// <summary>Archive-relative path with forward slashes, as stored in the index.</summary>
|
||||
public string ToRelative(string path) => LongPath.ToPortableRelative(Root, path);
|
||||
|
||||
public string FromRelative(string relative) => LongPath.FromPortableRelative(Root, relative);
|
||||
}
|
||||
|
||||
/// <summary>Absolute paths of one sequence folder.</summary>
|
||||
public sealed class SequencePaths
|
||||
{
|
||||
internal SequencePaths(ArchivePaths archive, string relativeFolder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(relativeFolder);
|
||||
RelativeFolder = relativeFolder.Replace('\\', '/').Trim('/');
|
||||
Folder = archive.FromRelative(RelativeFolder);
|
||||
MetaDirectory = Path.Combine(Folder, ArchiveLayout.MetaFolder);
|
||||
}
|
||||
|
||||
/// <summary>Folder relative to the archive root, forward slashes; empty when the root itself holds the photos (adopted folder).</summary>
|
||||
public string RelativeFolder { get; }
|
||||
|
||||
public string Folder { get; }
|
||||
public string MetaDirectory { get; }
|
||||
public string Originals => Path.Combine(Folder, ArchiveLayout.OriginalsFolder);
|
||||
public string Proxies => Path.Combine(Folder, ArchiveLayout.ProxiesFolder);
|
||||
public string Exports => Path.Combine(Folder, ArchiveLayout.ExportsFolder);
|
||||
public string ManifestPath => Path.Combine(MetaDirectory, ArchiveLayout.SequenceManifestFile);
|
||||
public string AnalysisPath => Path.Combine(MetaDirectory, ArchiveLayout.AnalysisFile);
|
||||
public string ProjectPath => Path.Combine(MetaDirectory, ArchiveLayout.ProjectFile);
|
||||
public string ThumbsPackPath => Path.Combine(MetaDirectory, ArchiveLayout.ThumbsPackFile);
|
||||
public string IngestJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.IngestJournalFile);
|
||||
|
||||
public string Proxy(int index) => Path.Combine(Proxies, ArchiveLayout.ProxyFileName(index));
|
||||
|
||||
/// <summary>Absolute path of a frame file given its sidecar path (relative to the sequence folder).</summary>
|
||||
public string File(string sequenceRelative) => LongPath.FromPortableRelative(Folder, sequenceRelative);
|
||||
|
||||
/// <summary>Archive-relative path of a frame file (as stored in the frames table).</summary>
|
||||
public string ArchiveRelative(string sequenceRelative) =>
|
||||
RelativeFolder.Length == 0 ? sequenceRelative : RelativeFolder + "/" + sequenceRelative;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Chronos.Core.Naming;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// <c>.chronos/archive.json</c> at the archive root (BRIEF §7). Fields this build does not know are kept in
|
||||
/// <see cref="Extra"/> and written back unchanged.
|
||||
/// </summary>
|
||||
public sealed record ArchiveManifest
|
||||
{
|
||||
public const int CurrentSchemaVersion = 1;
|
||||
|
||||
public int SchemaVersion { get; init; } = CurrentSchemaVersion;
|
||||
public required Guid Id { get; init; }
|
||||
public required string Name { get; init; }
|
||||
public DateTimeOffset Created { get; init; }
|
||||
public LayoutTemplates LayoutTemplates { get; init; } = new();
|
||||
public ProxyPolicy ProxyPolicy { get; init; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// The default policy when absent: archives written before it existed have none, and the source-generated reader
|
||||
/// sets an init-only property it does not find to null rather than keeping its initializer.
|
||||
/// </summary>
|
||||
public ThumbnailPolicy ThumbnailPolicy
|
||||
{
|
||||
get => _thumbnailPolicy;
|
||||
init => _thumbnailPolicy = value ?? new ThumbnailPolicy();
|
||||
}
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
|
||||
private readonly ThumbnailPolicy _thumbnailPolicy = new();
|
||||
}
|
||||
|
||||
/// <summary>Naming templates used when frames enter the archive (BRIEF §7.1).</summary>
|
||||
public sealed record LayoutTemplates
|
||||
{
|
||||
public string Folder { get; init; } = TemplateDefaults.Folder;
|
||||
public string File { get; init; } = TemplateDefaults.File;
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>Proxy generation policy (BRIEF §8.5).</summary>
|
||||
public sealed record ProxyPolicy
|
||||
{
|
||||
public const int Disabled = -1;
|
||||
public const int FullSize = 0;
|
||||
|
||||
/// <summary>Long edge in pixels: 1280, 1920 (default) or 2560; <see cref="FullSize"/> or <see cref="Disabled"/>.</summary>
|
||||
public int LongEdge { get; init; } = 1920;
|
||||
|
||||
public int Quality { get; init; } = 92;
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
|
||||
/// <summary>True for the long edges an archive accepts: disabled, full size, or 256 to 8192 px.</summary>
|
||||
public static bool IsValidLongEdge(int longEdge) => longEdge is Disabled or FullSize or (>= 256 and <= 8192);
|
||||
}
|
||||
|
||||
/// <summary>Thumbnail policy (BRIEF §8.5): long edge and JPEG quality of the thumbnails packed per sequence.</summary>
|
||||
public sealed record ThumbnailPolicy
|
||||
{
|
||||
public const int MinLongEdge = 160;
|
||||
public const int MaxLongEdge = 1024;
|
||||
|
||||
/// <summary>
|
||||
/// Long edge in pixels (320 by default); thumbnails built before a change keep their size until rebuilt. A value the
|
||||
/// file lacks (read as 0) is the default.
|
||||
/// </summary>
|
||||
public int LongEdge
|
||||
{
|
||||
get => _longEdge;
|
||||
init => _longEdge = value > 0 ? value : FrameImageRenderer.ThumbnailLongEdge;
|
||||
}
|
||||
|
||||
/// <summary>JPEG quality (80 by default); a value the file lacks (read as 0) or out of 1–100 is the default.</summary>
|
||||
public int Quality
|
||||
{
|
||||
get => _quality;
|
||||
init => _quality = value is > 0 and <= 100 ? value : FrameImageRenderer.ThumbnailQuality;
|
||||
}
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
|
||||
private readonly int _longEdge = FrameImageRenderer.ThumbnailLongEdge;
|
||||
private readonly int _quality = FrameImageRenderer.ThumbnailQuality;
|
||||
|
||||
public static bool IsValidLongEdge(int longEdge) => longEdge is >= MinLongEdge and <= MaxLongEdge;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Chronos.Core.Imaging;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>A folder that holds a sequence: it has a sidecar, or photos to adopt.</summary>
|
||||
/// <param name="RelativeFolder">Archive-relative, forward slashes; empty for the root.</param>
|
||||
/// <param name="Sidecar">Stamp of <c>.chronos/sequence.json</c>, null when it has none yet.</param>
|
||||
/// <param name="Photos">Stamp of the photo directory (its time changes when files are added or removed).</param>
|
||||
public sealed record SequenceFolder(string RelativeFolder, FileStamp? Sidecar, FileStamp? Photos);
|
||||
|
||||
/// <summary>
|
||||
/// Walks an archive tree (BRIEF §7). No file-system watcher is involved: callers compare the stamps with those of
|
||||
/// the previous scan. Hidden folders, symbolic links and the reserved sequence subfolders are not entered.
|
||||
/// </summary>
|
||||
public static class ArchiveScanner
|
||||
{
|
||||
private const int MaxDepth = 16;
|
||||
|
||||
private static readonly EnumerationOptions Listing = new()
|
||||
{
|
||||
IgnoreInaccessible = true,
|
||||
AttributesToSkip = FileAttributes.ReparsePoint | FileAttributes.System,
|
||||
RecurseSubdirectories = false,
|
||||
};
|
||||
|
||||
/// <summary>Every sequence folder under the root, ordered by relative path (ordinal) so job checkpoints are stable.</summary>
|
||||
public static IReadOnlyList<SequenceFolder> FindSequenceFolders(ArchivePaths archive, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var found = new List<SequenceFolder>();
|
||||
var pending = new Stack<(string Path, int Depth)>();
|
||||
pending.Push((archive.Root, 0));
|
||||
while (pending.Count > 0)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var (path, depth) = pending.Pop();
|
||||
var relative = archive.ToRelative(path);
|
||||
var sequence = archive.Sequence(relative == "." ? string.Empty : relative);
|
||||
var sidecar = FileStamp.Of(sequence.ManifestPath);
|
||||
var photoDirectory = PhotoDirectory(sequence);
|
||||
if (sidecar is not null || EnumeratePhotos(photoDirectory).Any())
|
||||
found.Add(new SequenceFolder(sequence.RelativeFolder, sidecar, FileStamp.Of(photoDirectory)));
|
||||
if (depth >= MaxDepth) continue;
|
||||
foreach (var child in Directory.EnumerateDirectories(path, "*", Listing))
|
||||
{
|
||||
var name = Path.GetFileName(child);
|
||||
if (!IsHidden(child, name) && !ArchiveLayout.IsReservedFolder(name)) pending.Push((child, depth + 1));
|
||||
}
|
||||
}
|
||||
found.Sort((a, b) => string.CompareOrdinal(a.RelativeFolder, b.RelativeFolder));
|
||||
return found;
|
||||
}
|
||||
|
||||
/// <summary><c>originals/</c> when the folder follows the layout, otherwise the folder itself (adopted in place).</summary>
|
||||
public static string PhotoDirectory(SequencePaths sequence) => Directory.Exists(sequence.Originals) ? sequence.Originals : sequence.Folder;
|
||||
|
||||
/// <summary>Photos of a sequence folder as sidecar paths (relative to the sequence folder), ordinal order.</summary>
|
||||
public static IReadOnlyList<string> ListPhotos(SequencePaths sequence)
|
||||
{
|
||||
var directory = PhotoDirectory(sequence);
|
||||
return EnumeratePhotos(directory)
|
||||
.Select(path => Path.GetRelativePath(sequence.Folder, path).Replace('\\', '/'))
|
||||
.Order(StringComparer.Ordinal)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>True for files Chronos imports as photos (AppleDouble <c>._</c> companions excluded).</summary>
|
||||
public static bool IsPhoto(string fileName) =>
|
||||
!fileName.StartsWith("._", StringComparison.Ordinal) && ImageFormatInfo.IsSupportedExtension(fileName);
|
||||
|
||||
private static IEnumerable<string> EnumeratePhotos(string directory) =>
|
||||
Directory.Exists(directory)
|
||||
? Directory.EnumerateFiles(directory, "*", Listing).Where(f => IsPhoto(Path.GetFileName(f)) && !IsHidden(f, Path.GetFileName(f)))
|
||||
: [];
|
||||
|
||||
private static bool IsHidden(string path, string name) =>
|
||||
name.StartsWith('.') || (OperatingSystem.IsWindows() && File.GetAttributes(path).HasFlag(FileAttributes.Hidden));
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Chronos.Core.Localization;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>The archive cannot be reached (or written) right now; nothing was changed.</summary>
|
||||
public sealed class ArchiveOfflineException(string root, ArchiveAvailability availability)
|
||||
: IOException(Loc.L(availability == ArchiveAvailability.ReadOnly ? "Archive.Error.ReadOnly" : "Archive.Error.Offline", root))
|
||||
{
|
||||
public string Root { get; } = root;
|
||||
public ArchiveAvailability Availability { get; } = availability;
|
||||
}
|
||||
|
||||
public enum ArchiveAvailability
|
||||
{
|
||||
Online,
|
||||
|
||||
/// <summary>Readable but not writable (read-only share, write-protected disk): browse only.</summary>
|
||||
ReadOnly,
|
||||
|
||||
/// <summary>Root unreachable (unplugged drive, disconnected share) or no longer an archive.</summary>
|
||||
Offline,
|
||||
}
|
||||
|
||||
/// <summary>Offline detection for archives on removable and network volumes (BRIEF §7).</summary>
|
||||
public static class ArchiveVolume
|
||||
{
|
||||
/// <summary>Throws <see cref="ArchiveOfflineException"/> unless the archive is online (writable).</summary>
|
||||
public static void EnsureWritable(ArchivePaths archive)
|
||||
{
|
||||
var availability = Probe(archive);
|
||||
if (availability != ArchiveAvailability.Online) throw new ArchiveOfflineException(archive.Root, availability);
|
||||
}
|
||||
|
||||
/// <summary>Checks that <c>archive.json</c> is reachable and that the metadata folder accepts writes.</summary>
|
||||
public static ArchiveAvailability Probe(ArchivePaths archive)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(archive.ManifestPath)) return ArchiveAvailability.Offline;
|
||||
var probe = Path.Combine(archive.MetaDirectory, ".probe-" + Guid.NewGuid().ToString("N")[..8]);
|
||||
using (new FileStream(probe, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1, FileOptions.DeleteOnClose))
|
||||
{
|
||||
}
|
||||
return ArchiveAvailability.Online;
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return ArchiveAvailability.ReadOnly;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return File.Exists(archive.ManifestPath) ? ArchiveAvailability.ReadOnly : ArchiveAvailability.Offline;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>SHA-256 of files, streamed (originals can be hundreds of megabytes).</summary>
|
||||
public static class ContentHash
|
||||
{
|
||||
internal const int BufferSize = 1 << 20;
|
||||
|
||||
/// <summary>Lower-case hex SHA-256 of the file.</summary>
|
||||
/// <param name="progress">Receives the number of bytes read since the previous call.</param>
|
||||
public static async Task<string> Sha256Async(string path, Action<long>? progress = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, BufferSize,
|
||||
FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
return await Sha256Async(stream, progress, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static async Task<string> Sha256Async(Stream stream, Action<long>? progress = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
|
||||
var buffer = new byte[BufferSize];
|
||||
int read;
|
||||
while ((read = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
|
||||
{
|
||||
hash.AppendData(buffer, 0, read);
|
||||
progress?.Invoke(read);
|
||||
}
|
||||
return Convert.ToHexStringLower(hash.GetHashAndReset());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Size and modification time of a file, the cheap change detector of incremental scans (BRIEF §7).</summary>
|
||||
public readonly record struct FileStamp(long Size, DateTime ModifiedUtc)
|
||||
{
|
||||
/// <summary>The stamp of a file or directory, or null when it does not exist.</summary>
|
||||
public static FileStamp? Of(string path)
|
||||
{
|
||||
var file = new FileInfo(path);
|
||||
if (file.Exists) return new FileStamp(file.Length, file.LastWriteTimeUtc);
|
||||
var directory = new DirectoryInfo(path);
|
||||
return directory.Exists ? new FileStamp(0, directory.LastWriteTimeUtc) : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
using Chronos.Core.Codecs.Jpeg;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Metadata;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>A JPEG made from a frame.</summary>
|
||||
public sealed record EncodedImage(byte[] Jpeg, int Width, int Height, DecodeTier Tier);
|
||||
|
||||
/// <summary>
|
||||
/// Makes thumbnails and proxies of frames (BRIEF §8.5): the managed JPEG decoder, or for thumbnails an embedded preview
|
||||
/// at least 320 px long, first, then the host's decode providers (WIC on Windows); orientation applied, resampled in
|
||||
/// linear light, encoded in-house. <see cref="EncodedImage.Tier"/> says how the pixels were obtained.
|
||||
/// </summary>
|
||||
public sealed class FrameImageRenderer(ImageDecoder decoder)
|
||||
{
|
||||
public const int ThumbnailLongEdge = 320;
|
||||
public const int ThumbnailQuality = 80;
|
||||
|
||||
/// <summary>A 320 px q80 thumbnail, or null when no decoder can read the frame.</summary>
|
||||
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, CancellationToken cancellationToken = default) =>
|
||||
Thumbnail(sequence, frame, new ThumbnailPolicy(), cancellationToken);
|
||||
|
||||
/// <summary>A thumbnail at the policy's long edge and quality (the archive's, BRIEF §8.5), or null when no decoder can read the frame.</summary>
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, ThumbnailPolicy policy, CancellationToken cancellationToken = default,
|
||||
string? companion = null)
|
||||
{
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Thumbnail, preferPreview: true, companion, cancellationToken);
|
||||
if (decoded is null) return null;
|
||||
using var small = Resampler.Thumbnail(decoded.Pixels, policy.LongEdge);
|
||||
return Encode(small, new JpegEncodeOptions { Quality = policy.Quality }, decoded.Tier);
|
||||
}
|
||||
|
||||
/// <summary>A proxy at the policy's long edge and quality with capture time and camera in its Exif; null when undecodable.</summary>
|
||||
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
||||
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default,
|
||||
string? companion = null)
|
||||
{
|
||||
if (policy.LongEdge == ProxyPolicy.Disabled) throw new InvalidOperationException("Proxies are disabled for this archive.");
|
||||
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, companion, cancellationToken);
|
||||
if (decoded is null) return null;
|
||||
var source = decoded.Pixels;
|
||||
var (w, h) = policy.LongEdge == ProxyPolicy.FullSize ? (source.Width, source.Height) : Resampler.FitWithin(source.Width, source.Height, policy.LongEdge);
|
||||
using var resized = Resampler.Resize(source, w, h);
|
||||
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata
|
||||
{
|
||||
Make = camera?.Make,
|
||||
Model = camera?.Model,
|
||||
BodySerial = camera?.Serial,
|
||||
CapturedAt = frame.CapturedAt,
|
||||
UtcOffsetMinutes = frame.UtcOffset,
|
||||
ExposureTime = frame.Exposure,
|
||||
FNumber = frame.FNumber,
|
||||
Iso = frame.Iso,
|
||||
FocalLength = frame.Focal,
|
||||
ExposureBias = frame.Bias,
|
||||
Software = "Chronos",
|
||||
});
|
||||
// The encoder takes the APP1 payload; ExifWriter returns the whole segment (marker and length first).
|
||||
return Encode(resized, new JpegEncodeOptions { Quality = policy.Quality, App1Segments = [exif[4..]] }, decoded.Tier);
|
||||
}
|
||||
|
||||
private static EncodedImage Encode(ImageBuffer pixels, JpegEncodeOptions options, DecodeTier tier) =>
|
||||
new(JpegEncoder.Encode(pixels, options), pixels.Width, pixels.Height, tier);
|
||||
|
||||
/// <summary>
|
||||
/// The frame's pixels: a JPEG through the managed decoder, the stored embedded preview when <paramref name="preferPreview"/>
|
||||
/// and it reaches <paramref name="maxLongEdge"/> (a smaller one would be upscaled or leave the thumbnail short), then
|
||||
/// the host's decoders (with the GPR's <paramref name="companion"/>), and the stored preview as the last resort. Null
|
||||
/// when nothing decodes.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The file could not be read.</exception>
|
||||
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, string? companion,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (frame.Format == ImageFileFormat.Jpeg && DecodeJpeg(File.ReadAllBytes(path), maxLongEdge, frame.Orientation, DecodeTier.Native) is { } jpeg) return jpeg;
|
||||
byte[]? preview = null;
|
||||
if (preferPreview && (preview = ReadPreview(path, frame)) is not null && Reaches(preview, maxLongEdge)
|
||||
&& DecodeJpeg(preview, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) is { } fast)
|
||||
return fast;
|
||||
var request = new DecodeRequest(path, System.Math.Max(0, maxLongEdge), purpose, ApplyOrientation: true, AllowPreview: true, Companion: companion);
|
||||
if (decoder.Decode(request, cancellationToken) is { } decoded) return new DecodedFrame(decoded.Pixels, decoded.Tier);
|
||||
return (preview ?? ReadPreview(path, frame)) is { } fallback ? DecodeJpeg(fallback, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the stored preview's long edge reaches <paramref name="maxLongEdge"/>, from the JPEG's own header: the
|
||||
/// sidecar's preview size may be a GPR companion's, larger than the preview embedded in the file.
|
||||
/// </summary>
|
||||
private static bool Reaches(byte[] preview, int maxLongEdge)
|
||||
{
|
||||
if (maxLongEdge <= 0) return true;
|
||||
try
|
||||
{
|
||||
var info = JpegDecoder.ReadInfo(preview);
|
||||
return int.Max(info.Width, info.Height) >= maxLongEdge;
|
||||
}
|
||||
catch (InvalidDataException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static DecodedFrame? DecodeJpeg(byte[] data, int maxLongEdge, int orientation, DecodeTier tier)
|
||||
{
|
||||
ImageBuffer decoded;
|
||||
try
|
||||
{
|
||||
decoded = JpegDecoder.DecodeToLinear(data, new JpegDecodeOptions { MaxLongEdge = System.Math.Max(0, maxLongEdge) }, expandGray: true);
|
||||
}
|
||||
catch (InvalidDataException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (orientation is <= 1 or > 8) return new DecodedFrame(decoded, tier);
|
||||
using (decoded) return new DecodedFrame(PixelConvert.ApplyOrientation(decoded, orientation), tier);
|
||||
}
|
||||
|
||||
private static byte[]? ReadPreview(string path, SidecarFrame frame)
|
||||
{
|
||||
if (frame.PreviewOffset is not { } offset || frame.PreviewLength is not ({ } length and > 0 and < int.MaxValue)) return null;
|
||||
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 4096, FileOptions.RandomAccess);
|
||||
if (offset < 0 || offset + length > stream.Length) return null;
|
||||
var data = new byte[length];
|
||||
stream.Position = offset;
|
||||
stream.ReadExactly(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
private sealed record DecodedFrame(ImageBuffer Pixels, DecodeTier Tier) : IDisposable
|
||||
{
|
||||
public void Dispose() => Pixels.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Chronos.Core.Model;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
public enum FrameHealth
|
||||
{
|
||||
/// <summary>Same size and SHA-256 as recorded.</summary>
|
||||
Ok,
|
||||
|
||||
/// <summary>No hash was recorded (adopted file); it has been computed now.</summary>
|
||||
Hashed,
|
||||
|
||||
Missing,
|
||||
|
||||
/// <summary>A different size: the file was replaced or edited.</summary>
|
||||
Changed,
|
||||
|
||||
/// <summary>Same size, different content: bit rot or a damaged copy.</summary>
|
||||
Corrupt,
|
||||
}
|
||||
|
||||
/// <param name="Sha256">The hash computed now (null when the file is missing or changed size).</param>
|
||||
public sealed record FrameCheck(SidecarFrame Frame, FrameHealth Health, string? Sha256)
|
||||
{
|
||||
/// <summary>Frame flags after this check: <see cref="FrameFlags.Missing"/>/<see cref="FrameFlags.Corrupt"/> set or cleared.</summary>
|
||||
public FrameFlags Flags => Health switch
|
||||
{
|
||||
FrameHealth.Missing => (Frame.Flags & ~FrameFlags.Corrupt) | FrameFlags.Missing,
|
||||
FrameHealth.Changed or FrameHealth.Corrupt => (Frame.Flags & ~FrameFlags.Missing) | FrameFlags.Corrupt,
|
||||
_ => Frame.Flags & ~(FrameFlags.Missing | FrameFlags.Corrupt),
|
||||
};
|
||||
|
||||
/// <summary>The sidecar record after this check (flags, and the hash of a newly hashed file).</summary>
|
||||
public SidecarFrame Updated => Frame with { Flags = Flags, Sha256 = Health == FrameHealth.Hashed ? Sha256 : Frame.Sha256 };
|
||||
|
||||
public bool ChangesRecord => Updated != Frame;
|
||||
}
|
||||
|
||||
/// <summary>Re-hashes originals against their sidecar records (BRIEF §12.5 Verify).</summary>
|
||||
public static class FrameVerifier
|
||||
{
|
||||
public static async Task<FrameCheck> CheckAsync(SequencePaths sequence, SidecarFrame frame, Action<long>? bytesRead = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var info = new FileInfo(sequence.File(frame.File));
|
||||
if (!info.Exists) return new FrameCheck(frame, FrameHealth.Missing, null);
|
||||
if (info.Length != frame.Size) return new FrameCheck(frame, FrameHealth.Changed, null);
|
||||
var sha = await ContentHash.Sha256Async(info.FullName, bytesRead, cancellationToken).ConfigureAwait(false);
|
||||
var health = frame.Sha256 is null ? FrameHealth.Hashed
|
||||
: sha.Equals(frame.Sha256, StringComparison.OrdinalIgnoreCase) ? FrameHealth.Ok
|
||||
: FrameHealth.Corrupt;
|
||||
return new FrameCheck(frame, health, sha);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Chronos.Core.IO;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// <c>.chronos/index-snapshot.bin</c> (BRIEF §7): every sidecar of the archive in one file, so a rebuild reads one file
|
||||
/// sequentially instead of thousands of small ones over a share. Optional and never authoritative: an entry is used
|
||||
/// only when its sidecar still has the recorded size and time, and a file whose trailing SHA-256 does not match is
|
||||
/// ignored entirely.
|
||||
/// </summary>
|
||||
/// <remarks>Layout: "TISN", version (u16), reserved (u16), then per sidecar: folder length (u16), UTF-8 folder,
|
||||
/// size (i64), modified UTC ticks (i64), length (i32), sidecar bytes; finally the SHA-256 of everything before it.</remarks>
|
||||
public sealed class IndexSnapshot
|
||||
{
|
||||
private const ushort Version = 1;
|
||||
private static ReadOnlySpan<byte> Magic => "TISN"u8;
|
||||
|
||||
private readonly Dictionary<string, (FileStamp Stamp, byte[] Bytes)> _entries;
|
||||
|
||||
private IndexSnapshot(Dictionary<string, (FileStamp, byte[])> entries) => _entries = entries;
|
||||
|
||||
public int Count => _entries.Count;
|
||||
|
||||
/// <summary>The snapshot at <paramref name="path"/>, or null when it is absent, damaged or of another version.</summary>
|
||||
public static IndexSnapshot? TryLoad(string path)
|
||||
{
|
||||
byte[] data;
|
||||
try
|
||||
{
|
||||
if (!File.Exists(path)) return null;
|
||||
data = AtomicFile.ReadAllBytes(path);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (data.Length < 8 + 32 || !data.AsSpan(0, 4).SequenceEqual(Magic) || BinaryPrimitives.ReadUInt16LittleEndian(data.AsSpan(4)) != Version)
|
||||
return null;
|
||||
var body = data.AsSpan(0, data.Length - 32);
|
||||
if (!SHA256.HashData(body).AsSpan().SequenceEqual(data.AsSpan(data.Length - 32))) return null;
|
||||
|
||||
var entries = new Dictionary<string, (FileStamp, byte[])>(StringComparer.Ordinal);
|
||||
var position = 8;
|
||||
while (position < body.Length)
|
||||
{
|
||||
if (position + 2 > body.Length) return null;
|
||||
var folderLength = BinaryPrimitives.ReadUInt16LittleEndian(body[position..]);
|
||||
position += 2;
|
||||
if (position + folderLength + 20 > body.Length) return null;
|
||||
var folder = Encoding.UTF8.GetString(body.Slice(position, folderLength));
|
||||
position += folderLength;
|
||||
var size = BinaryPrimitives.ReadInt64LittleEndian(body[position..]);
|
||||
var ticks = BinaryPrimitives.ReadInt64LittleEndian(body[(position + 8)..]);
|
||||
var length = BinaryPrimitives.ReadInt32LittleEndian(body[(position + 16)..]);
|
||||
position += 20;
|
||||
if (length < 0 || ticks < 0 || ticks > DateTime.MaxValue.Ticks || position + length > body.Length) return null;
|
||||
entries[folder] = (new FileStamp(size, new DateTime(ticks, DateTimeKind.Utc)), body.Slice(position, length).ToArray());
|
||||
position += length;
|
||||
}
|
||||
return new IndexSnapshot(entries);
|
||||
}
|
||||
|
||||
/// <summary>The recorded sidecar bytes of <paramref name="folder"/> when its current stamp still matches.</summary>
|
||||
public byte[]? TryGet(string folder, FileStamp current) =>
|
||||
_entries.TryGetValue(folder, out var entry) && entry.Stamp == current ? entry.Bytes : null;
|
||||
|
||||
/// <summary>Streams a new snapshot to a temporary file; <see cref="Commit"/> replaces the old one atomically.</summary>
|
||||
public sealed class Writer : IDisposable
|
||||
{
|
||||
private readonly string _path;
|
||||
private readonly string _temp;
|
||||
private readonly FileStream _stream;
|
||||
private readonly IncrementalHash _hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
|
||||
private bool _committed;
|
||||
|
||||
public Writer(string path)
|
||||
{
|
||||
_path = path;
|
||||
_temp = AtomicFile.TempPathFor(path);
|
||||
_stream = new FileStream(_temp, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1 << 16);
|
||||
Span<byte> header = stackalloc byte[8];
|
||||
header.Clear();
|
||||
Magic.CopyTo(header);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(header[4..], Version);
|
||||
Write(header);
|
||||
}
|
||||
|
||||
public void Add(string folder, FileStamp stamp, ReadOnlySpan<byte> sidecar)
|
||||
{
|
||||
var name = Encoding.UTF8.GetBytes(folder);
|
||||
Span<byte> head = stackalloc byte[2];
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(head, checked((ushort)name.Length));
|
||||
Write(head);
|
||||
Write(name);
|
||||
Span<byte> fields = stackalloc byte[20];
|
||||
BinaryPrimitives.WriteInt64LittleEndian(fields, stamp.Size);
|
||||
BinaryPrimitives.WriteInt64LittleEndian(fields[8..], stamp.ModifiedUtc.Ticks);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(fields[16..], sidecar.Length);
|
||||
Write(fields);
|
||||
Write(sidecar);
|
||||
}
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
_stream.Write(_hash.GetHashAndReset());
|
||||
_stream.Dispose();
|
||||
AtomicFile.Commit(_temp, _path);
|
||||
_committed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_stream.Dispose();
|
||||
_hash.Dispose();
|
||||
if (!_committed && File.Exists(_temp)) File.Delete(_temp);
|
||||
}
|
||||
|
||||
private void Write(ReadOnlySpan<byte> data)
|
||||
{
|
||||
_hash.AppendData(data);
|
||||
_stream.Write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.IO;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// Write-ahead record of an ingest batch (<c>.chronos/ingest.pending.json</c>): the frames whose files are about to be
|
||||
/// moved into the sequence folder. Written before the moves and removed once <c>sequence.json</c> lists them, so a
|
||||
/// crash in between is repaired by <see cref="Recover"/> instead of leaving unindexed originals behind.
|
||||
/// </summary>
|
||||
public sealed record IngestJournal(IReadOnlyList<SidecarFrame> Frames)
|
||||
{
|
||||
public static void Write(SequencePaths sequence, IngestJournal journal)
|
||||
{
|
||||
Directory.CreateDirectory(sequence.MetaDirectory);
|
||||
AtomicFile.WriteAllBytes(sequence.IngestJournalPath, JsonSerializer.SerializeToUtf8Bytes(journal, ArchiveJson.Default.IngestJournal));
|
||||
}
|
||||
|
||||
public static void Delete(SequencePaths sequence)
|
||||
{
|
||||
if (File.Exists(sequence.IngestJournalPath)) File.Delete(sequence.IngestJournalPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Completes an interrupted batch: frames whose file reached the sequence folder (same size) but are missing from
|
||||
/// <paramref name="manifest"/> are added in capture order. Returns the manifest to write, or null when nothing has
|
||||
/// to change. The journal is left in place; <see cref="Delete"/> it once the result is written.
|
||||
/// </summary>
|
||||
public static SequenceManifest? Recover(SequencePaths sequence, SequenceManifest manifest)
|
||||
{
|
||||
if (!File.Exists(sequence.IngestJournalPath)) return null;
|
||||
IngestJournal? journal;
|
||||
try
|
||||
{
|
||||
journal = JsonSerializer.Deserialize(AtomicFile.ReadAllBytes(sequence.IngestJournalPath), ArchiveJson.Default.IngestJournal);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
journal = null; // torn while being written: nothing was moved yet
|
||||
}
|
||||
if (journal is null) return null;
|
||||
var known = manifest.Frames.Select(f => f.Id).ToHashSet();
|
||||
var moved = journal.Frames.Where(f => !known.Contains(f.Id) && FileHasSize(sequence.File(f.File), f.Size)).ToList();
|
||||
return moved.Count == 0 ? null : SequenceFrames.Insert(manifest, moved);
|
||||
}
|
||||
|
||||
private static bool FileHasSize(string path, long size)
|
||||
{
|
||||
var info = new FileInfo(path);
|
||||
return info.Exists && info.Length == size;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Localization;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>Content of a lease file: who holds it and until when.</summary>
|
||||
public sealed record LeaseRecord(string Token, string Owner, string Host, int ProcessId, DateTimeOffset Acquired, DateTimeOffset Expires);
|
||||
|
||||
/// <summary>The lease is held by someone else and did not free up in time.</summary>
|
||||
public sealed class LeaseUnavailableException(string name, LeaseRecord? holder)
|
||||
: IOException(Loc.L("Archive.Error.Busy", holder?.Owner ?? "?"))
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public LeaseRecord? Holder { get; } = holder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cross-process exclusive lease stored as <c>.chronos/locks/<name>.lease</c> (BRIEF §7), for writers that share
|
||||
/// an archive over a network (server and client). Creation uses create-new semantics, which SMB and local file
|
||||
/// systems both make atomic. A lease past its expiry is stale (its holder crashed or lost the share) and is taken
|
||||
/// over by renaming it aside, which only one contender can do. The holder renews it in the background while held.
|
||||
/// </summary>
|
||||
public sealed class LeaseFile : IDisposable
|
||||
{
|
||||
public static readonly TimeSpan DefaultDuration = TimeSpan.FromMinutes(2);
|
||||
|
||||
private const string Extension = ".lease";
|
||||
private const string StaleExtension = ".stale";
|
||||
private const int ReadAttempts = 5;
|
||||
|
||||
/// <summary>Creates tried, some 130 ms in all, while a released lease's name is still taken by a reader (<see cref="TryCreate"/>).</summary>
|
||||
private const int CreateAttempts = 8;
|
||||
private static readonly TimeSpan ReadRetryDelay = TimeSpan.FromMilliseconds(20);
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Timer _renewal;
|
||||
private LeaseRecord _record;
|
||||
private bool _released;
|
||||
|
||||
private LeaseFile(string path, string name, LeaseRecord record, TimeSpan duration)
|
||||
{
|
||||
Path = path;
|
||||
Name = name;
|
||||
_record = record;
|
||||
Duration = duration;
|
||||
var period = duration / 3;
|
||||
_renewal = new Timer(_ => Renew(), null, period, period);
|
||||
}
|
||||
|
||||
public string Path { get; }
|
||||
public string Name { get; }
|
||||
public TimeSpan Duration { get; }
|
||||
public LeaseRecord Record => _record;
|
||||
|
||||
/// <summary>False once a renewal found the lease taken over (it expired while this process was stalled).</summary>
|
||||
public bool IsHeld { get; private set; } = true;
|
||||
|
||||
/// <summary>Takes the lease when it is free or stale; null when someone else holds it.</summary>
|
||||
public static LeaseFile? TryAcquire(string locksDirectory, string name, string owner, TimeSpan? duration = null)
|
||||
{
|
||||
var length = duration ?? DefaultDuration;
|
||||
var path = PathFor(locksDirectory, name);
|
||||
Directory.CreateDirectory(locksDirectory);
|
||||
for (var attempt = 0; attempt < 3; attempt++)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var record = new LeaseRecord(Guid.NewGuid().ToString("N"), owner, Environment.MachineName, Environment.ProcessId, now, now + length);
|
||||
if (TryCreate(path, record)) return new LeaseFile(path, name, record, length);
|
||||
if (!IsStale(path, now)) return null;
|
||||
BreakStale(path, now);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Waits up to <paramref name="wait"/> for the lease, polling with back-off.</summary>
|
||||
/// <exception cref="LeaseUnavailableException">Still held by someone else after <paramref name="wait"/>.</exception>
|
||||
public static async Task<LeaseFile> AcquireAsync(string locksDirectory, string name, string owner, TimeSpan wait,
|
||||
TimeSpan? duration = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var deadline = DateTime.UtcNow + wait;
|
||||
var delay = TimeSpan.FromMilliseconds(20);
|
||||
while (true)
|
||||
{
|
||||
if (TryAcquire(locksDirectory, name, owner, duration) is { } lease) return lease;
|
||||
if (DateTime.UtcNow >= deadline) throw new LeaseUnavailableException(name, Read(PathFor(locksDirectory, name)));
|
||||
await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
|
||||
delay = TimeSpan.FromMilliseconds(System.Math.Min(delay.TotalMilliseconds * 2, 500));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The current holder of a lease, or null when free or unreadable.</summary>
|
||||
/// <remarks>
|
||||
/// Readers share read, write and delete so they never block a renewal, a release or a takeover. A renewal rewrites
|
||||
/// the record in place, so a read can meet a torn record or (on Windows) a sharing violation for a few milliseconds:
|
||||
/// it is retried briefly before the lease is reported unreadable.
|
||||
/// </remarks>
|
||||
public static LeaseRecord? Read(string path)
|
||||
{
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
return JsonSerializer.Deserialize(stream, ArchiveJson.Default.LeaseRecord);
|
||||
}
|
||||
catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException)
|
||||
{
|
||||
if (attempt == ReadAttempts) return null;
|
||||
Thread.Sleep(ReadRetryDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Removes every stale lease in <paramref name="locksDirectory"/> (maintenance); returns their names.</summary>
|
||||
public static IReadOnlyList<string> CleanupStale(string locksDirectory, bool dryRun)
|
||||
{
|
||||
if (!Directory.Exists(locksDirectory)) return [];
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var removed = new List<string>();
|
||||
foreach (var path in Directory.EnumerateFiles(locksDirectory, "*" + Extension))
|
||||
{
|
||||
if (!IsStale(path, now)) continue;
|
||||
removed.Add(System.IO.Path.GetFileNameWithoutExtension(path));
|
||||
if (!dryRun) BreakStale(path, now);
|
||||
}
|
||||
if (!dryRun)
|
||||
{
|
||||
foreach (var path in Directory.EnumerateFiles(locksDirectory, "*" + StaleExtension))
|
||||
{
|
||||
if (File.GetLastWriteTimeUtc(path) + DefaultDuration < now.UtcDateTime) TryDelete(path);
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
/// <summary>Extends the expiry; false when the lease is no longer ours.</summary>
|
||||
public bool Renew()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_released || !IsHeld) return false;
|
||||
if (Read(Path)?.Token != _record.Token)
|
||||
{
|
||||
IsHeld = false;
|
||||
Log.Warning("archive", $"Lease {Name} was taken over by another writer");
|
||||
return false;
|
||||
}
|
||||
var renewed = _record with { Expires = DateTimeOffset.UtcNow + Duration };
|
||||
try
|
||||
{
|
||||
// In-place rewrite: a rename would open a window in which the lease looks free.
|
||||
using var fs = new FileStream(Path, FileMode.Truncate, FileAccess.Write, FileShare.Read);
|
||||
fs.Write(JsonSerializer.SerializeToUtf8Bytes(renewed, ArchiveJson.Default.LeaseRecord));
|
||||
fs.Flush(flushToDisk: true);
|
||||
_record = renewed;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
// Runs on a timer thread: an escaping exception would end the process.
|
||||
Log.Warning("archive", $"Lease {Name} could not be renewed", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_renewal.Dispose();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_released) return;
|
||||
_released = true;
|
||||
if (!IsHeld || Read(Path)?.Token != _record.Token) return;
|
||||
try
|
||||
{
|
||||
File.Delete(Path);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
// It expires on its own.
|
||||
Log.Warning("archive", $"Lease {Name} could not be released", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static string PathFor(string locksDirectory, string name) => System.IO.Path.Combine(locksDirectory, name + Extension);
|
||||
|
||||
private static bool TryCreate(string path, LeaseRecord record)
|
||||
{
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.Read);
|
||||
fs.Write(JsonSerializer.SerializeToUtf8Bytes(record, ArchiveJson.Default.LeaseRecord));
|
||||
fs.Flush(flushToDisk: true);
|
||||
return true;
|
||||
}
|
||||
catch (IOException) when (File.Exists(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (IOException) when (attempt < CreateAttempts)
|
||||
{
|
||||
// The holder released the lease between the failed create and the check: the name is free again.
|
||||
}
|
||||
catch (UnauthorizedAccessException) when (attempt < CreateAttempts)
|
||||
{
|
||||
// On Windows a lease released while a reader still holds it open (readers share deletion) stays pending
|
||||
// deletion, and creating its name answers "access denied" until the reader lets go, a moment later.
|
||||
Thread.Sleep(1 << (attempt - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Expired, or unreadable for longer than a lease lasts (a holder that crashed while creating it).</summary>
|
||||
private static bool IsStale(string path, DateTimeOffset now)
|
||||
{
|
||||
if (Read(path) is { } record) return record.Expires < now;
|
||||
try
|
||||
{
|
||||
return File.Exists(path) && File.GetLastWriteTimeUtc(path) + DefaultDuration < now.UtcDateTime;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void BreakStale(string path, DateTimeOffset now)
|
||||
{
|
||||
var aside = path + "." + Guid.NewGuid().ToString("N")[..8] + StaleExtension;
|
||||
try
|
||||
{
|
||||
File.Move(path, aside);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return; // another contender broke it first
|
||||
}
|
||||
if (IsStale(aside, now))
|
||||
{
|
||||
TryDelete(aside);
|
||||
return;
|
||||
}
|
||||
// A contender renewed or re-created the lease between our check and the rename: give it back.
|
||||
try
|
||||
{
|
||||
File.Move(aside, path, overwrite: false);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
TryDelete(aside);
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryDelete(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// A leftover file is harmless; maintenance removes it.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Core.Localization;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>A sidecar written by a newer Chronos: it can be read, but this build refuses to rewrite it.</summary>
|
||||
public sealed class NewerFormatException(string path, int version)
|
||||
: InvalidOperationException(Loc.L("Archive.Error.NewerFormat", System.IO.Path.GetFileName(path), version))
|
||||
{
|
||||
public string Path { get; } = path;
|
||||
public int Version { get; } = version;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads and writes the JSON files of the layout. Reading tolerates unknown fields and enum values; every write
|
||||
/// goes through <see cref="AtomicFile"/> (temp → fsync → rename), so a crash leaves the previous file intact.
|
||||
/// </summary>
|
||||
public static class ManifestStore
|
||||
{
|
||||
/// <exception cref="InvalidDataException">The file is not valid JSON or lacks required fields.</exception>
|
||||
public static ArchiveManifest ReadArchive(string path) => Parse(AtomicFile.ReadAllBytes(path), path, ArchiveJson.Default.ArchiveManifest);
|
||||
|
||||
public static void WriteArchive(string path, ArchiveManifest manifest)
|
||||
{
|
||||
RefuseNewer(path, manifest.SchemaVersion, ArchiveManifest.CurrentSchemaVersion);
|
||||
AtomicFile.WriteAllBytes(path, JsonSerializer.SerializeToUtf8Bytes(manifest, ArchiveJsonIndented.Default.ArchiveManifest));
|
||||
}
|
||||
|
||||
/// <exception cref="InvalidDataException">The file is not valid JSON or lacks required fields.</exception>
|
||||
public static SequenceManifest ReadSequence(string path) => ParseSequence(AtomicFile.ReadAllBytes(path), path);
|
||||
|
||||
/// <summary>Parses sidecar bytes (from disk or from the index snapshot).</summary>
|
||||
public static SequenceManifest ParseSequence(ReadOnlySpan<byte> utf8, string source) => Parse(utf8, source, ArchiveJson.Default.SequenceManifest);
|
||||
|
||||
/// <summary>Replaces the sidecar atomically. Compact JSON: a 100k-frame sidecar stays around 35 MB.</summary>
|
||||
public static void WriteSequence(string path, SequenceManifest manifest)
|
||||
{
|
||||
RefuseNewer(path, manifest.SchemaVersion, SequenceManifest.CurrentSchemaVersion);
|
||||
AtomicFile.WriteAllBytes(path, JsonSerializer.SerializeToUtf8Bytes(manifest, ArchiveJson.Default.SequenceManifest));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the sidecar only if none exists (rename without replace), which makes it the claim on a new
|
||||
/// sequence folder between processes sharing the archive. False when another sidecar is already there.
|
||||
/// </summary>
|
||||
public static bool TryCreateSequence(string path, SequenceManifest manifest)
|
||||
{
|
||||
if (File.Exists(path)) return false;
|
||||
var temp = AtomicFile.TempPathFor(path);
|
||||
File.WriteAllBytes(temp, JsonSerializer.SerializeToUtf8Bytes(manifest, ArchiveJson.Default.SequenceManifest));
|
||||
try
|
||||
{
|
||||
using (var fs = new FileStream(temp, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) fs.Flush(flushToDisk: true);
|
||||
File.Move(temp, path, overwrite: false);
|
||||
return true;
|
||||
}
|
||||
catch (IOException) when (File.Exists(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(temp)) File.Delete(temp);
|
||||
}
|
||||
}
|
||||
|
||||
private static T Parse<T>(ReadOnlySpan<byte> utf8, string source, System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> typeInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize(utf8, typeInfo) ?? throw new InvalidDataException($"{source} is empty.");
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException($"{source} is not a valid Chronos file: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RefuseNewer(string path, int version, int supported)
|
||||
{
|
||||
if (version > supported) throw new NewerFormatException(path, version);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Naming;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>What the planner needs to know about a sequence to rename it.</summary>
|
||||
public sealed record RenameSubject(Guid SequenceId, string Folder, string? Title, DateTime? FirstCapture, string? CameraMake, string? CameraModel, string? Lens);
|
||||
|
||||
/// <summary>One sequence's change: its folder, its title, or both.</summary>
|
||||
/// <param name="Carried">The folder moves only because a folder it is inside moves (a sequence nested in another).</param>
|
||||
public sealed record RenameStep(Guid SequenceId, string FromFolder, string ToFolder, string? FromTitle, string? ToTitle, bool Carried = false)
|
||||
{
|
||||
public bool MovesFolder => !string.Equals(FromFolder, ToFolder, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>A planned folder would lie inside a sequence's folder (nesting one sequence in another); nothing is planned.</summary>
|
||||
public sealed class NestedRenameException(string target, string folder)
|
||||
: ArgumentException(Loc.L("Archive.Error.NestedTarget", target, folder))
|
||||
{
|
||||
public string Target { get; } = target;
|
||||
public string Folder { get; } = folder;
|
||||
}
|
||||
|
||||
/// <summary>The exact changes a mass rename will make (its dry run); applying it makes exactly these.</summary>
|
||||
public sealed record RenamePlan(Guid Id, Guid ArchiveId, IReadOnlyList<RenameStep> Steps);
|
||||
|
||||
/// <summary>A plan being applied, with the steps already completed; lets an interrupted rename resume (BRIEF §7).</summary>
|
||||
public sealed record RenameJournal(RenamePlan Plan, IReadOnlyList<Guid> Completed)
|
||||
{
|
||||
public static string PathFor(ArchivePaths archive, Guid planId) => Path.Combine(archive.JournalDirectory, $"rename-{planId:N}.json");
|
||||
|
||||
public static void Write(ArchivePaths archive, RenameJournal journal)
|
||||
{
|
||||
Directory.CreateDirectory(archive.JournalDirectory);
|
||||
AtomicFile.WriteAllBytes(PathFor(archive, journal.Plan.Id), JsonSerializer.SerializeToUtf8Bytes(journal, ArchiveJsonIndented.Default.RenameJournal));
|
||||
}
|
||||
|
||||
/// <summary>Journals left by interrupted renames.</summary>
|
||||
public static IReadOnlyList<RenameJournal> ListPending(ArchivePaths archive)
|
||||
{
|
||||
if (!Directory.Exists(archive.JournalDirectory)) return [];
|
||||
var list = new List<RenameJournal>();
|
||||
foreach (var path in Directory.EnumerateFiles(archive.JournalDirectory, "rename-*.json").Order(StringComparer.Ordinal))
|
||||
{
|
||||
if (JsonSerializer.Deserialize(File.ReadAllBytes(path), ArchiveJson.Default.RenameJournal) is { } journal) list.Add(journal);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void Delete(ArchivePaths archive, Guid planId) => File.Delete(PathFor(archive, planId));
|
||||
}
|
||||
|
||||
/// <summary>Builds rename plans from folder/title templates (BRIEF §7.1, §12.5).</summary>
|
||||
public static class RenamePlanner
|
||||
{
|
||||
/// <summary>
|
||||
/// Expands <paramref name="titleTemplate"/> (where <c>{title}</c> is the current title) and then
|
||||
/// <paramref name="folderTemplate"/> (where <c>{title}</c> is the new title) for every subject. A target folder
|
||||
/// that is any current sequence folder, exists on disk or was given to an earlier subject gets " (2)", " (3)"….
|
||||
/// Null templates leave that part unchanged; subjects without a change produce no step. A folder that moves takes
|
||||
/// the sequences inside it along: each gets a <see cref="RenameStep.Carried"/> step after every move, so the index
|
||||
/// follows its files; a selected sequence inside a moving folder that moves elsewhere leaves first.
|
||||
/// </summary>
|
||||
/// <param name="occupied">Every sequence folder of the archive (renamed or not).</param>
|
||||
/// <param name="nested">The archive's other sequences inside a subject's folder, with their titles.</param>
|
||||
/// <exception cref="NestedRenameException">A target lies inside a sequence's folder, or inside another target.</exception>
|
||||
public static RenamePlan Plan(Guid archiveId, IReadOnlyList<RenameSubject> subjects, string? folderTemplate, string? titleTemplate,
|
||||
IEnumerable<string> occupied, Func<string, bool> existsOnDisk, IReadOnlyList<RenameSubject>? nested = null)
|
||||
{
|
||||
var folder = folderTemplate is null ? null : PathTemplate.Parse(folderTemplate);
|
||||
var title = titleTemplate is null ? null : PathTemplate.Parse(titleTemplate);
|
||||
var taken = occupied.ToList();
|
||||
var claimed = new HashSet<string>(taken, StringComparer.OrdinalIgnoreCase);
|
||||
var own = new List<RenameStep>(subjects.Count);
|
||||
foreach (var s in subjects)
|
||||
{
|
||||
var context = new NamingContext
|
||||
{
|
||||
CapturedAt = s.FirstCapture ?? DateTime.Today,
|
||||
SessionStart = s.FirstCapture ?? DateTime.Today,
|
||||
CameraMake = s.CameraMake,
|
||||
CameraModel = s.CameraModel,
|
||||
Lens = s.Lens,
|
||||
Title = s.Title,
|
||||
};
|
||||
var newTitle = title is null ? s.Title : Blank(title.ExpandFile(context)) ?? s.Title;
|
||||
var newFolder = s.Folder;
|
||||
// The archive root itself (a folder adopted in place) cannot move; only its title changes.
|
||||
if (folder is not null && s.Folder.Length > 0 && Blank(folder.ExpandFolder(context with { Title = newTitle })) is { } expanded
|
||||
&& !expanded.Equals(s.Folder, StringComparison.Ordinal))
|
||||
{
|
||||
newFolder = expanded.Equals(s.Folder, StringComparison.OrdinalIgnoreCase) ? expanded : Unique(expanded, claimed, existsOnDisk);
|
||||
claimed.Add(newFolder);
|
||||
}
|
||||
own.Add(new RenameStep(s.SequenceId, s.Folder, newFolder, s.Title, newTitle));
|
||||
}
|
||||
|
||||
var moving = own.Where(s => s.MovesFolder).ToList();
|
||||
foreach (var step in moving)
|
||||
{
|
||||
// Into a sequence's folder (its own included) or another target: one sequence would end up inside another.
|
||||
var host = taken.Concat(moving.Where(m => m != step).Select(m => m.ToFolder)).FirstOrDefault(f => IsInside(f, step.ToFolder));
|
||||
if (host is not null) throw new NestedRenameException(step.ToFolder, host);
|
||||
}
|
||||
var steps = new List<(RenameStep Step, int Order)>();
|
||||
foreach (var step in own.Concat((nested ?? []).Select(n => new RenameStep(n.SequenceId, n.Folder, n.Folder, n.Title, n.Title))))
|
||||
{
|
||||
// The nearest folder around it that moves carries it; a sequence that moves elsewhere leaves before its folder does.
|
||||
var carrier = step.MovesFolder ? null : moving.Where(m => IsInside(m.FromFolder, step.FromFolder)).MaxBy(m => m.FromFolder.Length);
|
||||
if (carrier is not null)
|
||||
steps.Add((step with { ToFolder = carrier.ToFolder + step.FromFolder[carrier.FromFolder.Length..], Carried = true }, 1));
|
||||
else if (step.MovesFolder)
|
||||
steps.Add((step, -moving.Count(m => m != step && IsInside(m.FromFolder, step.FromFolder))));
|
||||
else if (step.FromTitle != step.ToTitle)
|
||||
steps.Add((step, 0));
|
||||
}
|
||||
// Stable: subjects keep their order, sequences inside moving folders move out first, carried ones come last.
|
||||
return new RenamePlan(Guid.CreateVersion7(), archiveId, [.. steps.OrderBy(s => s.Order).Select(s => s.Step)]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether <paramref name="path"/> lies inside <paramref name="folder"/> (archive-relative, forward slashes); the
|
||||
/// archive root, a sequence only when a folder was adopted in place, counts as containing nothing.
|
||||
/// </summary>
|
||||
public static bool IsInside(string folder, string path) =>
|
||||
folder.Length > 0 && path.Length > folder.Length + 1 && path[folder.Length] == '/' && path.StartsWith(folder, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// Moves the folder of a step; idempotent so a resumed plan can repeat it: an already moved folder is accepted,
|
||||
/// a case-only change goes through a temporary name (case-insensitive file systems).
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The target is occupied by something else, or neither folder exists.</exception>
|
||||
public static void MoveFolder(ArchivePaths archive, RenameStep step)
|
||||
{
|
||||
if (!step.MovesFolder) return;
|
||||
var from = archive.FromRelative(step.FromFolder);
|
||||
var to = archive.FromRelative(step.ToFolder);
|
||||
var caseOnly = step.FromFolder.Equals(step.ToFolder, StringComparison.OrdinalIgnoreCase);
|
||||
if (Directory.Exists(to) && (!caseOnly || HasExactName(to)))
|
||||
{
|
||||
if (Directory.Exists(from) && !caseOnly) throw new IOException($"Rename target {step.ToFolder} already exists.");
|
||||
return;
|
||||
}
|
||||
if (!Directory.Exists(from)) throw new DirectoryNotFoundException($"Sequence folder {step.FromFolder} is missing.");
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(to)!);
|
||||
if (caseOnly)
|
||||
{
|
||||
var temp = from + ".rename-" + Guid.NewGuid().ToString("N")[..8];
|
||||
Directory.Move(from, temp);
|
||||
Directory.Move(temp, to);
|
||||
}
|
||||
else
|
||||
{
|
||||
Directory.Move(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool HasExactName(string path) =>
|
||||
Directory.EnumerateDirectories(Path.GetDirectoryName(path)!, Path.GetFileName(path))
|
||||
.Any(d => Path.GetFileName(d).Equals(Path.GetFileName(path), StringComparison.Ordinal));
|
||||
|
||||
private static string? Blank(string value) => string.IsNullOrWhiteSpace(value) ? null : value;
|
||||
|
||||
private static string Unique(string folder, HashSet<string> claimed, Func<string, bool> existsOnDisk)
|
||||
{
|
||||
var candidate = folder;
|
||||
for (var n = 2; claimed.Contains(candidate) || existsOnDisk(candidate); n++)
|
||||
candidate = string.Create(CultureInfo.InvariantCulture, $"{folder} ({n})");
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Metadata;
|
||||
using Chronos.Core.Model;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>Files of a sequence folder that the sidecar does not list, and sidecar frames whose file is gone.</summary>
|
||||
public sealed record SequenceOrphans(string Folder, IReadOnlyList<string> UnindexedFiles, IReadOnlyList<SidecarFrame> MissingFiles)
|
||||
{
|
||||
public bool IsEmpty => UnindexedFiles.Count == 0 && MissingFiles.Count == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds and reconciles sidecars from the photos found in a folder (BRIEF §7, §12.5 "Adopt folder"). Only light
|
||||
/// metadata is read; originals are never moved, renamed or rewritten. Hashes are left empty for a verify to fill.
|
||||
/// </summary>
|
||||
public static class SequenceAdopter
|
||||
{
|
||||
/// <summary>A new sidecar for a folder without one. A folder outside the layout (no <c>originals/</c>) is titled by its name.</summary>
|
||||
public static SequenceManifest Build(Guid id, SequencePaths sequence, string? createdBy, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var (frames, first) = ReadFrames(sequence, ArchiveScanner.ListPhotos(sequence), cancellationToken);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var inLayout = Directory.Exists(sequence.Originals);
|
||||
var manifest = new SequenceManifest
|
||||
{
|
||||
Id = id,
|
||||
Title = inLayout ? null : Path.GetFileName(Path.TrimEndingDirectorySeparator(sequence.Folder)),
|
||||
Camera = first is null ? null : new SidecarCamera { Make = first.Make, Model = first.Model, Serial = first.BodySerial },
|
||||
Lens = first?.LensModel,
|
||||
CreatedBy = createdBy,
|
||||
Created = now,
|
||||
UpdatedAt = now,
|
||||
};
|
||||
return SequenceFrames.Insert(manifest, frames);
|
||||
}
|
||||
|
||||
/// <summary>Unindexed photos and missing frame files of one sequence.</summary>
|
||||
public static SequenceOrphans FindOrphans(SequencePaths sequence, SequenceManifest manifest)
|
||||
{
|
||||
var listed = manifest.Frames.Select(f => f.File).ToHashSet(StringComparer.Ordinal);
|
||||
var unindexed = ArchiveScanner.ListPhotos(sequence).Where(f => !listed.Contains(f)).ToList();
|
||||
var missing = manifest.Frames.Where(f => !File.Exists(sequence.File(f.File))).ToList();
|
||||
return new SequenceOrphans(sequence.RelativeFolder, unindexed, missing);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Brings a sidecar in line with its folder: unindexed photos are added (in capture order), frames whose file is
|
||||
/// gone get <see cref="FrameFlags.Missing"/> and frames whose file came back lose it. Null when nothing changed.
|
||||
/// </summary>
|
||||
public static SequenceManifest? Reconcile(SequencePaths sequence, SequenceManifest manifest, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var orphans = FindOrphans(sequence, manifest);
|
||||
var missing = orphans.MissingFiles.Select(f => f.Id).ToHashSet();
|
||||
var flagChanges = new Dictionary<Guid, Func<SidecarFrame, SidecarFrame>>();
|
||||
foreach (var frame in manifest.Frames)
|
||||
{
|
||||
var isMissing = missing.Contains(frame.Id);
|
||||
if (isMissing == frame.Flags.HasFlag(FrameFlags.Missing)) continue;
|
||||
flagChanges[frame.Id] = f => f with { Flags = isMissing ? f.Flags | FrameFlags.Missing : f.Flags & ~FrameFlags.Missing };
|
||||
}
|
||||
if (flagChanges.Count == 0 && orphans.UnindexedFiles.Count == 0) return null;
|
||||
var updated = flagChanges.Count == 0 ? manifest : SequenceFrames.Update(manifest, flagChanges);
|
||||
var (added, first) = ReadFrames(sequence, orphans.UnindexedFiles, cancellationToken);
|
||||
// Photos that cannot be read yet (held open by another program, no read permission, a placeholder not downloaded)
|
||||
// stay unindexed: with nothing else changed, nothing changed.
|
||||
if (added.Count == 0 && flagChanges.Count == 0) return null;
|
||||
if (updated.Camera is null && first is not null)
|
||||
updated = updated with { Camera = new SidecarCamera { Make = first.Make, Model = first.Model, Serial = first.BodySerial }, Lens = updated.Lens ?? first.LensModel };
|
||||
return SequenceFrames.Insert(updated, added);
|
||||
}
|
||||
|
||||
private static (List<SidecarFrame> Frames, FrameMetadata? First) ReadFrames(SequencePaths sequence, IReadOnlyList<string> files, CancellationToken cancellationToken)
|
||||
{
|
||||
var frames = new List<SidecarFrame>(files.Count);
|
||||
FrameMetadata? first = null;
|
||||
foreach (var file in files)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var path = sequence.File(file);
|
||||
try
|
||||
{
|
||||
var metadata = MetadataReader.Read(path, cancellationToken);
|
||||
if (first is null || (metadata.CapturedAt < first.CapturedAt && metadata.Make is not null)) first = metadata;
|
||||
frames.Add(SequenceFrames.FromMetadata(Guid.CreateVersion7(), file, Path.GetFileName(file), new FileInfo(path).Length, null, metadata));
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("archive", $"Skipped unreadable photo {sequence.ArchiveRelative(file)}", ex);
|
||||
}
|
||||
}
|
||||
return (frames, first);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Metadata;
|
||||
using Chronos.Core.Model;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>Edits of the frame list of a <see cref="SequenceManifest"/>; frames stay in capture order with gap-free indexes.</summary>
|
||||
public static class SequenceFrames
|
||||
{
|
||||
/// <summary>Capture time (unknown last), then file name.</summary>
|
||||
public static readonly IComparer<SidecarFrame> CaptureOrder = Comparer<SidecarFrame>.Create((a, b) =>
|
||||
{
|
||||
var byTime = (a.CapturedAt ?? DateTime.MaxValue).CompareTo(b.CapturedAt ?? DateTime.MaxValue);
|
||||
return byTime != 0 ? byTime : string.CompareOrdinal(a.File, b.File);
|
||||
});
|
||||
|
||||
/// <summary>A frame record from light metadata (with <see cref="FrameFlags.Intervalometer"/> when the MakerNote says so).</summary>
|
||||
/// <param name="file">Path relative to the sequence folder.</param>
|
||||
/// <param name="fallbackCapture">Used when the file records no capture time of its own (only a file-system date).</param>
|
||||
public static SidecarFrame FromMetadata(Guid id, string file, string? original, long size, string? sha256, FrameMetadata metadata,
|
||||
DateTime? fallbackCapture = null)
|
||||
{
|
||||
var useFallback = fallbackCapture is not null && metadata.TimestampSource is TimestampSource.FileSystem or TimestampSource.Unknown;
|
||||
return new SidecarFrame
|
||||
{
|
||||
Id = id,
|
||||
File = file,
|
||||
Original = original,
|
||||
Size = size,
|
||||
Sha256 = sha256?.ToLowerInvariant(),
|
||||
CapturedAt = useFallback ? DateTime.SpecifyKind(fallbackCapture!.Value, DateTimeKind.Unspecified) : metadata.CapturedAt,
|
||||
UtcOffset = useFallback ? null : metadata.UtcOffsetMinutes,
|
||||
TimeSource = useFallback ? TimestampSource.Unknown : metadata.TimestampSource,
|
||||
Exposure = metadata.ExposureTime,
|
||||
FNumber = metadata.FNumber,
|
||||
Iso = metadata.Iso,
|
||||
Focal = metadata.FocalLength,
|
||||
Bias = metadata.ExposureBias,
|
||||
Wb = metadata.WhiteBalanceMode,
|
||||
Orientation = metadata.Orientation,
|
||||
W = metadata.Width,
|
||||
H = metadata.Height,
|
||||
Format = metadata.Format,
|
||||
PreviewOffset = metadata.LargestPreview?.Offset,
|
||||
PreviewLength = metadata.LargestPreview?.Length,
|
||||
PreviewW = Positive(metadata.LargestPreview?.Width),
|
||||
PreviewH = Positive(metadata.LargestPreview?.Height),
|
||||
Flags = metadata.IntervalShooting == true ? FrameFlags.Intervalometer : FrameFlags.None,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The frame with its decode tier (BRIEF §8.2) and the flags that follow it: <see cref="FrameFlags.PreviewOnly"/> for
|
||||
/// the embedded-preview tier, <see cref="FrameFlags.Undecodable"/> for none. <paramref name="tier"/>'s size, oriented
|
||||
/// like every <see cref="FileTier"/>, replaces the stored preview size when it is a preview's.
|
||||
/// </summary>
|
||||
/// <param name="damaged">For the unavailable tier: whether that proves the file unreadable. False when the host has no
|
||||
/// native decoder for the format, so a client or render node with one (or a proxy) may still produce its pixels: the
|
||||
/// tier is kept and <see cref="FrameFlags.Undecodable"/> is left as it was.</param>
|
||||
/// <param name="proxied">Whether the host takes the frame's pixels from its proxy (an original it cannot decode, whose
|
||||
/// proxy is there): see <see cref="WithProxy"/>.</param>
|
||||
/// <remarks>A probe reads the file's signature again: the format it found replaces a stored one that is out of date
|
||||
/// (GoPro DNGs converted by Adobe software were stored as GPR before owner review 1).</remarks>
|
||||
public static SidecarFrame WithTier(SidecarFrame frame, FileTier tier, bool damaged = true, bool proxied = false)
|
||||
{
|
||||
if (tier.Format != ImageFileFormat.Unknown && tier.Format != frame.Format) frame = frame with { Format = tier.Format };
|
||||
var flags = frame.Flags & ~(FrameFlags.PreviewOnly | FrameFlags.Undecodable);
|
||||
if (tier.Tier == DecodeTier.EmbeddedPreview) flags |= proxied ? FrameFlags.None : FrameFlags.PreviewOnly;
|
||||
else if (tier.Tier == DecodeTier.Unavailable) flags |= damaged ? FrameFlags.Undecodable : frame.Flags & FrameFlags.Undecodable;
|
||||
var (w, h) = tier.Tier == DecodeTier.EmbeddedPreview && tier.Width > 0 && tier.Height > 0
|
||||
? PixelConvert.OrientedSize(tier.Width, tier.Height, frame.Orientation)
|
||||
: (frame.PreviewW ?? 0, frame.PreviewH ?? 0);
|
||||
return frame with { Tier = tier.Tier, Flags = flags, PreviewW = Positive(w), PreviewH = Positive(h) };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The frame with <see cref="FrameFlags.PreviewOnly"/> following its proxy, for a host that takes the pixels of the
|
||||
/// frame's original from its proxy (HEIF on the server, BRIEF §2.3, §8.1): an embedded-preview frame is read from its
|
||||
/// preview, so preview-only, only while <paramref name="proxied"/> is false. The tier and the preview size stay the
|
||||
/// original's; other tiers are left as they are.
|
||||
/// </summary>
|
||||
public static SidecarFrame WithProxy(SidecarFrame frame, bool proxied) => frame.Tier != DecodeTier.EmbeddedPreview
|
||||
? frame
|
||||
: frame with { Flags = proxied ? frame.Flags & ~FrameFlags.PreviewOnly : frame.Flags | FrameFlags.PreviewOnly };
|
||||
|
||||
private static int? Positive(int? value) => value > 0 ? value : null;
|
||||
|
||||
/// <summary>
|
||||
/// Merges <paramref name="added"/> into the frame list by capture time (existing frames keep their relative order),
|
||||
/// renumbers indexes and bumps the frames version.
|
||||
/// </summary>
|
||||
public static SequenceManifest Insert(SequenceManifest manifest, IReadOnlyList<SidecarFrame> added)
|
||||
{
|
||||
if (added.Count == 0) return manifest;
|
||||
var incoming = added.Order(CaptureOrder).ToList();
|
||||
var merged = new List<SidecarFrame>(manifest.Frames.Count + incoming.Count);
|
||||
var next = 0;
|
||||
foreach (var frame in manifest.Frames)
|
||||
{
|
||||
while (next < incoming.Count && CaptureOrder.Compare(incoming[next], frame) < 0) merged.Add(incoming[next++]);
|
||||
merged.Add(frame);
|
||||
}
|
||||
while (next < incoming.Count) merged.Add(incoming[next++]);
|
||||
return WithFrames(manifest, merged);
|
||||
}
|
||||
|
||||
/// <summary>Replaces the frame list (indexes renumbered) and bumps the frames version.</summary>
|
||||
public static SequenceManifest WithFrames(SequenceManifest manifest, IReadOnlyList<SidecarFrame> frames)
|
||||
{
|
||||
var renumbered = new SidecarFrame[frames.Count];
|
||||
for (var i = 0; i < frames.Count; i++) renumbered[i] = frames[i].I == i ? frames[i] : frames[i] with { I = i };
|
||||
return manifest with
|
||||
{
|
||||
Frames = renumbered,
|
||||
Versions = manifest.Versions with { Frames = manifest.Versions.Frames + 1 },
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Applies per-frame changes by id (flags, hashes); frames not in <paramref name="changes"/> are kept.</summary>
|
||||
public static SequenceManifest Update(SequenceManifest manifest, IReadOnlyDictionary<Guid, Func<SidecarFrame, SidecarFrame>> changes)
|
||||
{
|
||||
var frames = manifest.Frames.Select(f => changes.TryGetValue(f.Id, out var change) ? change(f) : f).ToList();
|
||||
return manifest with { Frames = frames, UpdatedAt = DateTimeOffset.UtcNow };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Model;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// <c>.chronos/sequence.json</c>, the canonical record of one sequence (BRIEF §7). The database is rebuilt from it,
|
||||
/// so it carries the ids of the sequence and of every frame. Fields this build does not know are kept in
|
||||
/// <see cref="Extra"/> and written back unchanged.
|
||||
/// </summary>
|
||||
public sealed record SequenceManifest
|
||||
{
|
||||
public const int CurrentSchemaVersion = 1;
|
||||
|
||||
public int SchemaVersion { get; init; } = CurrentSchemaVersion;
|
||||
public required Guid Id { get; init; }
|
||||
public string? Title { get; init; }
|
||||
public SidecarCamera? Camera { get; init; }
|
||||
public string? Lens { get; init; }
|
||||
|
||||
/// <summary>Frames in capture order; <see cref="SidecarFrame.I"/> equals the position.</summary>
|
||||
public IReadOnlyList<SidecarFrame> Frames { get; init; } = [];
|
||||
|
||||
public IReadOnlyList<string> Tags { get; init; } = [];
|
||||
public int Rating { get; init; }
|
||||
public string? Notes { get; init; }
|
||||
|
||||
/// <summary><see cref="SequenceStatus"/> bits, stored as a number so bits of newer builds survive.</summary>
|
||||
public SequenceStatus Status { get; init; }
|
||||
|
||||
public ManifestVersions Versions { get; init; } = new();
|
||||
|
||||
/// <summary>User id or client name that created the sequence.</summary>
|
||||
public string? CreatedBy { get; init; }
|
||||
|
||||
public DateTimeOffset Created { get; init; }
|
||||
public DateTimeOffset UpdatedAt { get; init; }
|
||||
public DateTimeOffset? VerifiedAt { get; init; }
|
||||
|
||||
/// <summary>Session keys of automatic grouping (uploads, card imports) that resolve to this sequence.</summary>
|
||||
public IReadOnlyList<string> SessionKeys { get; init; } = [];
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
public sealed record SidecarCamera
|
||||
{
|
||||
public string? Make { get; init; }
|
||||
public string? Model { get; init; }
|
||||
public string? Serial { get; init; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>Counters the sync protocol compares (BRIEF §14.5).</summary>
|
||||
public sealed record ManifestVersions
|
||||
{
|
||||
public int Settings { get; init; }
|
||||
public int Frames { get; init; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>One frame of <see cref="SequenceManifest"/>; property names follow BRIEF §7.</summary>
|
||||
public sealed record SidecarFrame
|
||||
{
|
||||
public required Guid Id { get; init; }
|
||||
|
||||
/// <summary>Index in capture order, from 0.</summary>
|
||||
public int I { get; init; }
|
||||
|
||||
/// <summary>Path relative to the sequence folder, forward slashes (<c>originals/…</c> for imported frames).</summary>
|
||||
public required string File { get; init; }
|
||||
|
||||
/// <summary>Name the file had on the card or upload source.</summary>
|
||||
public string? Original { get; init; }
|
||||
|
||||
public long Size { get; init; }
|
||||
|
||||
/// <summary>Lower-case hex SHA-256; null until a verify hashes an adopted file.</summary>
|
||||
public string? Sha256 { get; init; }
|
||||
|
||||
/// <summary>Camera wall-clock time with sub-seconds.</summary>
|
||||
public DateTime? CapturedAt { get; init; }
|
||||
|
||||
public int? UtcOffset { get; init; }
|
||||
|
||||
[JsonConverter(typeof(TolerantEnumConverter<TimestampSource>))]
|
||||
public TimestampSource TimeSource { get; init; }
|
||||
|
||||
public double? Exposure { get; init; }
|
||||
public double? FNumber { get; init; }
|
||||
public int? Iso { get; init; }
|
||||
public double? Focal { get; init; }
|
||||
public double? Bias { get; init; }
|
||||
public int? Wb { get; init; }
|
||||
public int Orientation { get; init; } = 1;
|
||||
public int W { get; init; }
|
||||
public int H { get; init; }
|
||||
|
||||
[JsonConverter(typeof(TolerantEnumConverter<ImageFileFormat>))]
|
||||
public ImageFileFormat Format { get; init; }
|
||||
|
||||
public long? PreviewOffset { get; init; }
|
||||
public long? PreviewLength { get; init; }
|
||||
|
||||
/// <summary>Size of the largest embedded preview (or GPR companion JPEG), stored like <see cref="W"/> and <see cref="H"/>
|
||||
/// before orientation; null when the file has none or it is not known yet.</summary>
|
||||
public int? PreviewW { get; init; }
|
||||
|
||||
public int? PreviewH { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Decode tier of the file (BRIEF §8.2) as the host that indexed or decoded it last found it; omitted until it is
|
||||
/// known. <see cref="FrameFlags.PreviewOnly"/> (unless that host reads the frame from its proxy) and
|
||||
/// <see cref="FrameFlags.Undecodable"/> follow it.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(TolerantEnumConverter<DecodeTier>))]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public DecodeTier Tier { get; init; }
|
||||
|
||||
/// <summary><see cref="FrameFlags"/> bits, stored as a number so bits of newer builds survive.</summary>
|
||||
public FrameFlags Flags { get; init; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,398 @@
|
||||
using System.Buffers.Binary;
|
||||
using Chronos.Core.Hashing;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>One thumbnail of a pack: where its JPEG lies and which source it was made from.</summary>
|
||||
/// <param name="SourceHash">32-byte fingerprint of the original (its SHA-256 when known); a mismatch means stale.</param>
|
||||
public sealed record ThumbEntry(int FrameIndex, long Offset, int Length, int Width, int Height, byte[] SourceHash, uint Crc);
|
||||
|
||||
/// <summary>
|
||||
/// <c>thumbs.pack</c>: every thumbnail of a sequence in one file (BRIEF §8.5), so a share holds one file per sequence
|
||||
/// instead of thousands. Little-endian blocks after a 16-byte header:
|
||||
/// <list type="bullet">
|
||||
/// <item><c>TENT</c> entry: frame index, width, height, 32-byte source hash, length, CRC-32, JPEG bytes.</item>
|
||||
/// <item><c>TIDX</c> index: count, one row per frame (latest entry wins), CRC-32 of the rows.</item>
|
||||
/// <item><c>TFTR</c> footer (last 16 bytes): offset of the index block and its CRC.</item>
|
||||
/// </list>
|
||||
/// Entries are never moved, so offsets stay valid for open readers. A writer appends entries where the last index
|
||||
/// began and writes a new index plus footer on commit; after a crash the footer is missing or invalid, readers fall
|
||||
/// back to walking the entry headers, and an incomplete tail is ignored (the writer truncates it).
|
||||
/// </summary>
|
||||
public static class ThumbPack
|
||||
{
|
||||
public const int HashLength = 32;
|
||||
internal const int HeaderLength = 16;
|
||||
internal const int EntryHeaderLength = 4 + 4 + 2 + 2 + HashLength + 4 + 4;
|
||||
internal const int IndexRowLength = 4 + 8 + 4 + 2 + 2 + HashLength + 4;
|
||||
internal const int FooterLength = 16;
|
||||
internal const ushort Version = 1;
|
||||
|
||||
/// <summary>Largest thumbnail accepted; anything bigger in a header is corruption.</summary>
|
||||
internal const int MaxEntryLength = 16 << 20;
|
||||
|
||||
internal static ReadOnlySpan<byte> Magic => "TTPK"u8;
|
||||
internal static ReadOnlySpan<byte> EntryTag => "TENT"u8;
|
||||
internal static ReadOnlySpan<byte> IndexTag => "TIDX"u8;
|
||||
internal static ReadOnlySpan<byte> FooterTag => "TFTR"u8;
|
||||
|
||||
internal static void WriteHeader(Stream stream)
|
||||
{
|
||||
Span<byte> header = stackalloc byte[HeaderLength];
|
||||
header.Clear();
|
||||
Magic.CopyTo(header);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(header[4..], Version);
|
||||
stream.Write(header);
|
||||
}
|
||||
|
||||
internal static bool HasValidHeader(Stream stream)
|
||||
{
|
||||
if (stream.Length < HeaderLength) return false;
|
||||
Span<byte> header = stackalloc byte[HeaderLength];
|
||||
stream.Position = 0;
|
||||
stream.ReadExactly(header);
|
||||
return header[..4].SequenceEqual(Magic) && BinaryPrimitives.ReadUInt16LittleEndian(header[4..]) == Version;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The latest entry per frame and where the valid content ends: after the footer when the index is intact
|
||||
/// (<paramref name="indexOffset"/> = start of that index), otherwise after the last complete block.
|
||||
/// </summary>
|
||||
internal static Dictionary<int, ThumbEntry> Load(Stream stream, out long validEnd, out long? indexOffset)
|
||||
{
|
||||
indexOffset = null;
|
||||
if (TryLoadIndex(stream, out var entries, out var offset))
|
||||
{
|
||||
validEnd = stream.Length;
|
||||
indexOffset = offset;
|
||||
return entries;
|
||||
}
|
||||
return Scan(stream, out validEnd);
|
||||
}
|
||||
|
||||
private static bool TryLoadIndex(Stream stream, out Dictionary<int, ThumbEntry> entries, out long indexOffset)
|
||||
{
|
||||
entries = [];
|
||||
indexOffset = 0;
|
||||
var length = stream.Length;
|
||||
if (length < HeaderLength + 12 + FooterLength) return false;
|
||||
Span<byte> footer = stackalloc byte[FooterLength];
|
||||
stream.Position = length - FooterLength;
|
||||
stream.ReadExactly(footer);
|
||||
if (!footer[..4].SequenceEqual(FooterTag)) return false;
|
||||
indexOffset = BinaryPrimitives.ReadInt64LittleEndian(footer[4..]);
|
||||
if (Crc32.Compute(footer[4..12]) != BinaryPrimitives.ReadUInt32LittleEndian(footer[12..])) return false;
|
||||
if (indexOffset < HeaderLength || indexOffset > length - FooterLength - 12) return false;
|
||||
|
||||
Span<byte> head = stackalloc byte[8];
|
||||
stream.Position = indexOffset;
|
||||
stream.ReadExactly(head);
|
||||
var count = BinaryPrimitives.ReadInt32LittleEndian(head[4..]);
|
||||
if (!head[..4].SequenceEqual(IndexTag) || count < 0 || indexOffset + 8 + (long)count * IndexRowLength + 4 != length - FooterLength) return false;
|
||||
var rows = new byte[count * IndexRowLength + 4];
|
||||
stream.ReadExactly(rows);
|
||||
if (Crc32.Compute(rows.AsSpan(0, count * IndexRowLength)) != BinaryPrimitives.ReadUInt32LittleEndian(rows.AsSpan(count * IndexRowLength))) return false;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var row = rows.AsSpan(i * IndexRowLength, IndexRowLength);
|
||||
var entry = new ThumbEntry(
|
||||
BinaryPrimitives.ReadInt32LittleEndian(row),
|
||||
BinaryPrimitives.ReadInt64LittleEndian(row[4..]),
|
||||
BinaryPrimitives.ReadInt32LittleEndian(row[12..]),
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(row[16..]),
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(row[18..]),
|
||||
row.Slice(20, HashLength).ToArray(),
|
||||
BinaryPrimitives.ReadUInt32LittleEndian(row[(20 + HashLength)..]));
|
||||
if (entry.Offset < HeaderLength || entry.Length < 0 || entry.Offset + entry.Length > indexOffset) return false;
|
||||
entries[entry.FrameIndex] = entry;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Walks block headers from the start; stops at the first incomplete or unknown block.</summary>
|
||||
private static Dictionary<int, ThumbEntry> Scan(Stream stream, out long validEnd)
|
||||
{
|
||||
var entries = new Dictionary<int, ThumbEntry>();
|
||||
var length = stream.Length;
|
||||
var position = (long)HeaderLength;
|
||||
var header = new byte[EntryHeaderLength];
|
||||
while (position + 4 <= length)
|
||||
{
|
||||
stream.Position = position;
|
||||
stream.ReadExactly(header.AsSpan(0, 4));
|
||||
var tag = header.AsSpan(0, 4);
|
||||
long next;
|
||||
if (tag.SequenceEqual(EntryTag))
|
||||
{
|
||||
if (position + EntryHeaderLength > length) break;
|
||||
stream.ReadExactly(header.AsSpan(4, EntryHeaderLength - 4));
|
||||
var size = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan(12 + HashLength));
|
||||
if (size is < 0 or > MaxEntryLength) break;
|
||||
next = position + EntryHeaderLength + size;
|
||||
if (next > length) break;
|
||||
var frame = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan(4));
|
||||
entries[frame] = new ThumbEntry(frame, position + EntryHeaderLength, size,
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(header.AsSpan(8)), BinaryPrimitives.ReadUInt16LittleEndian(header.AsSpan(10)),
|
||||
header.AsSpan(12, HashLength).ToArray(), BinaryPrimitives.ReadUInt32LittleEndian(header.AsSpan(16 + HashLength)));
|
||||
}
|
||||
else if (tag.SequenceEqual(IndexTag))
|
||||
{
|
||||
if (position + 8 > length) break;
|
||||
stream.ReadExactly(header.AsSpan(4, 4));
|
||||
var count = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan(4));
|
||||
if (count < 0) break;
|
||||
next = position + 8 + (long)count * IndexRowLength + 4;
|
||||
if (next > length) break;
|
||||
}
|
||||
else if (tag.SequenceEqual(FooterTag))
|
||||
{
|
||||
next = position + FooterLength;
|
||||
if (next > length) break;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
position = next;
|
||||
}
|
||||
validEnd = position;
|
||||
return entries;
|
||||
}
|
||||
|
||||
/// <summary>Writes one entry block at the stream position and returns its entry.</summary>
|
||||
internal static ThumbEntry WriteEntry(Stream stream, int frameIndex, int width, int height, byte[] sourceHash, ReadOnlySpan<byte> jpeg)
|
||||
{
|
||||
var crc = Crc32.Compute(jpeg);
|
||||
Span<byte> header = stackalloc byte[EntryHeaderLength];
|
||||
EntryTag.CopyTo(header);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(header[4..], frameIndex);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(header[8..], (ushort)width);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(header[10..], (ushort)height);
|
||||
sourceHash.CopyTo(header[12..]);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(header[(12 + HashLength)..], jpeg.Length);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(header[(16 + HashLength)..], crc);
|
||||
var payload = stream.Position + EntryHeaderLength;
|
||||
stream.Write(header);
|
||||
stream.Write(jpeg);
|
||||
return new ThumbEntry(frameIndex, payload, jpeg.Length, width, height, sourceHash, crc);
|
||||
}
|
||||
|
||||
internal static void WriteIndexAndFooter(Stream stream, IReadOnlyCollection<ThumbEntry> entries)
|
||||
{
|
||||
var indexOffset = stream.Position;
|
||||
var block = new byte[8 + entries.Count * IndexRowLength + 4];
|
||||
IndexTag.CopyTo(block);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(block.AsSpan(4), entries.Count);
|
||||
var i = 0;
|
||||
foreach (var e in entries.OrderBy(e => e.FrameIndex))
|
||||
{
|
||||
var row = block.AsSpan(8 + i++ * IndexRowLength, IndexRowLength);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(row, e.FrameIndex);
|
||||
BinaryPrimitives.WriteInt64LittleEndian(row[4..], e.Offset);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(row[12..], e.Length);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(row[16..], (ushort)e.Width);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(row[18..], (ushort)e.Height);
|
||||
e.SourceHash.CopyTo(row[20..]);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(row[(20 + HashLength)..], e.Crc);
|
||||
}
|
||||
var rowsLength = entries.Count * IndexRowLength;
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(block.AsSpan(8 + rowsLength), Crc32.Compute(block.AsSpan(8, rowsLength)));
|
||||
stream.Write(block);
|
||||
|
||||
Span<byte> footer = stackalloc byte[FooterLength];
|
||||
FooterTag.CopyTo(footer);
|
||||
BinaryPrimitives.WriteInt64LittleEndian(footer[4..], indexOffset);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(footer[12..], Crc32.Compute(footer[4..12]));
|
||||
stream.Write(footer);
|
||||
}
|
||||
|
||||
/// <summary>Size of a pack holding only <paramref name="entries"/>; the rest of a file is reclaimable garbage.</summary>
|
||||
public static long LiveBytes(IEnumerable<ThumbEntry> entries)
|
||||
{
|
||||
long count = 0, payload = 0;
|
||||
foreach (var e in entries)
|
||||
{
|
||||
count++;
|
||||
payload += EntryHeaderLength + e.Length;
|
||||
}
|
||||
return HeaderLength + payload + 8 + count * IndexRowLength + 4 + FooterLength;
|
||||
}
|
||||
|
||||
/// <summary>A 32-byte source fingerprint: the SHA-256 itself when the original's hash is known.</summary>
|
||||
public static byte[] Fingerprint(string? sha256, string file, long size) =>
|
||||
sha256 is { Length: HashLength * 2 }
|
||||
? Convert.FromHexString(sha256)
|
||||
: System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes($"{file}|{size}"));
|
||||
|
||||
/// <summary>
|
||||
/// Whether a thumbnail with <paramref name="sourceHash"/> was drawn from this original: its <see cref="Fingerprint"/>,
|
||||
/// or, for an original hashed since (a verify hashes the photos a scan adopted), the fingerprint of its file name and
|
||||
/// size it was drawn with before. The hash changes nothing about the photo.
|
||||
/// </summary>
|
||||
public static bool IsFrom(ReadOnlySpan<byte> sourceHash, string? sha256, string file, long size) =>
|
||||
sourceHash.SequenceEqual(Fingerprint(sha256, file, size))
|
||||
|| (sha256 is { Length: HashLength * 2 } && sourceHash.SequenceEqual(Fingerprint(null, file, size)));
|
||||
}
|
||||
|
||||
/// <summary>Random access to the thumbnails of a pack without loading the file; safe while a writer appends.</summary>
|
||||
public sealed class ThumbPackReader : IDisposable
|
||||
{
|
||||
private readonly FileStream? _stream;
|
||||
private readonly Lock _gate = new();
|
||||
|
||||
private ThumbPackReader(FileStream? stream, Dictionary<int, ThumbEntry> entries)
|
||||
{
|
||||
_stream = stream;
|
||||
Entries = entries;
|
||||
}
|
||||
|
||||
/// <summary>The latest entry of every frame that has one.</summary>
|
||||
public IReadOnlyDictionary<int, ThumbEntry> Entries { get; }
|
||||
|
||||
/// <summary>Bytes held by replaced entries (reclaimed by <see cref="ThumbPackWriter.Compact"/>).</summary>
|
||||
public long GarbageBytes => _stream is null ? 0 : System.Math.Max(0, _stream.Length - ThumbPack.LiveBytes(Entries.Values));
|
||||
|
||||
public long Length => _stream?.Length ?? 0;
|
||||
|
||||
/// <summary>Opens a pack; a missing or damaged pack reads as empty.</summary>
|
||||
public static ThumbPackReader Open(string path)
|
||||
{
|
||||
if (!File.Exists(path)) return new ThumbPackReader(null, []);
|
||||
var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 4096, FileOptions.RandomAccess);
|
||||
try
|
||||
{
|
||||
var entries = ThumbPack.HasValidHeader(stream) ? ThumbPack.Load(stream, out _, out _) : [];
|
||||
return new ThumbPackReader(stream, entries);
|
||||
}
|
||||
catch
|
||||
{
|
||||
stream.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The JPEG of a frame, or null when absent or damaged (CRC mismatch).</summary>
|
||||
public byte[]? Read(int frameIndex)
|
||||
{
|
||||
if (_stream is null || !Entries.TryGetValue(frameIndex, out var entry)) return null;
|
||||
var data = new byte[entry.Length];
|
||||
lock (_gate)
|
||||
{
|
||||
if (entry.Offset + entry.Length > _stream.Length) return null;
|
||||
_stream.Position = entry.Offset;
|
||||
_stream.ReadExactly(data);
|
||||
}
|
||||
return Crc32.Compute(data) == entry.Crc ? data : null;
|
||||
}
|
||||
|
||||
public void Dispose() => _stream?.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>Appends thumbnails to a pack; one writer per pack at a time (the <c>thumbs-<sequence id></c> lease guarantees it).</summary>
|
||||
public sealed class ThumbPackWriter : IDisposable
|
||||
{
|
||||
private readonly FileStream _stream;
|
||||
private readonly Dictionary<int, ThumbEntry> _entries;
|
||||
private long? _indexOffset;
|
||||
private bool _dirty;
|
||||
|
||||
private ThumbPackWriter(FileStream stream, Dictionary<int, ThumbEntry> entries, long? indexOffset)
|
||||
{
|
||||
_stream = stream;
|
||||
_entries = entries;
|
||||
_indexOffset = indexOffset;
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<int, ThumbEntry> Entries => _entries;
|
||||
|
||||
/// <summary>Opens or creates a pack, dropping an incomplete tail left by a crash.</summary>
|
||||
public static ThumbPackWriter Open(string path)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))!);
|
||||
var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, 1 << 16);
|
||||
try
|
||||
{
|
||||
if (!ThumbPack.HasValidHeader(stream))
|
||||
{
|
||||
// Not a pack (or a header torn by a crash): thumbnails are regenerable, start over.
|
||||
stream.SetLength(0);
|
||||
ThumbPack.WriteHeader(stream);
|
||||
return new ThumbPackWriter(stream, [], null);
|
||||
}
|
||||
var entries = ThumbPack.Load(stream, out var validEnd, out var indexOffset);
|
||||
if (validEnd < stream.Length) stream.SetLength(validEnd);
|
||||
return new ThumbPackWriter(stream, entries, indexOffset);
|
||||
}
|
||||
catch
|
||||
{
|
||||
stream.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Appends a thumbnail; it replaces any earlier entry of the frame once committed.</summary>
|
||||
public void Append(int frameIndex, int width, int height, byte[] sourceHash, ReadOnlySpan<byte> jpeg)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(frameIndex);
|
||||
if (sourceHash.Length != ThumbPack.HashLength) throw new ArgumentException("A source hash has 32 bytes.", nameof(sourceHash));
|
||||
if (jpeg.Length > ThumbPack.MaxEntryLength) throw new ArgumentException("Thumbnail too large.", nameof(jpeg));
|
||||
if (width is < 1 or > ushort.MaxValue || height is < 1 or > ushort.MaxValue) throw new ArgumentOutOfRangeException(nameof(width));
|
||||
// The last committed index sits at the end; new entries overwrite it (readers fall back to a header walk).
|
||||
_stream.Position = _indexOffset ?? _stream.Length;
|
||||
if (_indexOffset is { } offset) _stream.SetLength(offset);
|
||||
_indexOffset = null;
|
||||
|
||||
_entries[frameIndex] = ThumbPack.WriteEntry(_stream, frameIndex, width, height, sourceHash, jpeg);
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
/// <summary>Writes the index and footer and flushes to disk.</summary>
|
||||
public void Commit()
|
||||
{
|
||||
if (!_dirty) return;
|
||||
_stream.Position = _stream.Length;
|
||||
var indexOffset = _stream.Position;
|
||||
ThumbPack.WriteIndexAndFooter(_stream, _entries.Values);
|
||||
_stream.Flush(flushToDisk: true);
|
||||
_indexOffset = indexOffset;
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
/// <summary>Rewrites the pack with only its live entries (atomically, so readers keep a consistent file).</summary>
|
||||
public static void Compact(string path)
|
||||
{
|
||||
var temp = IO.AtomicFile.TempPathFor(path);
|
||||
try
|
||||
{
|
||||
using (var source = ThumbPackReader.Open(path))
|
||||
using (var target = new FileStream(temp, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1 << 16))
|
||||
{
|
||||
ThumbPack.WriteHeader(target);
|
||||
var entries = new List<ThumbEntry>(source.Entries.Count);
|
||||
foreach (var entry in source.Entries.Values.OrderBy(e => e.FrameIndex))
|
||||
{
|
||||
if (source.Read(entry.FrameIndex) is { } jpeg)
|
||||
entries.Add(ThumbPack.WriteEntry(target, entry.FrameIndex, entry.Width, entry.Height, entry.SourceHash, jpeg));
|
||||
}
|
||||
ThumbPack.WriteIndexAndFooter(target, entries);
|
||||
}
|
||||
IO.AtomicFile.Commit(temp, path);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(temp)) File.Delete(temp);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
Commit();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_stream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user