feat: single setup, mass-pass progress, per-server homes, Desktop shares its library (owner review 5, v1.1.0) (#68)
CI / Build and test (push) Successful in 11m36s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 12s
Release / Build, test and publish (push) Successful in 14m56s
CI / PostgreSQL 16 integration (push) Successful in 10m11s
CI / PostgreSQL 17 integration (push) Successful in 10m15s
CI / Docker image (push) Successful in 45s
Release / Server image (push) Successful in 4m55s
CI / Build and test (push) Successful in 11m36s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 12s
Release / Build, test and publish (push) Successful in 14m56s
CI / PostgreSQL 16 integration (push) Successful in 10m11s
CI / PostgreSQL 17 integration (push) Successful in 10m15s
CI / Docker image (push) Successful in 45s
Release / Server image (push) Successful in 4m55s
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #68.
This commit is contained in:
1 parent
988068ee8e
commit
20d64af4a4
793 files changed
+23882
-2096
No files matched your search
@@ -986,7 +986,7 @@ public sealed class ProjectMenuViewModel : LocalizedViewModel
|
||||
/// </summary>
|
||||
private void OnHomeChanged(WorkspaceHomeChange change)
|
||||
{
|
||||
if (_disposed || (change.ProjectId is { } id ? id != ProjectId : !IsServerHome || !string.Equals(change.Server, Home.Server, StringComparison.OrdinalIgnoreCase)))
|
||||
if (_disposed || (change.ProjectId is { } id ? id != ProjectId : !IsServerHome || !_host.SameServer(change.Server, Home.Server)))
|
||||
return;
|
||||
if (_context is { } context) context.Post(_ => _ = HomeChangedAsync(change), null);
|
||||
else _ = HomeChangedAsync(change);
|
||||
|
||||
@@ -36,6 +36,12 @@ public sealed record WorkspaceTrashPlan(string Folder, IReadOnlyList<WorkspaceTr
|
||||
{
|
||||
/// <summary>Nothing to erase: Empty… is disabled.</summary>
|
||||
public bool IsEmpty => Items.Count == 0;
|
||||
|
||||
/// <summary>
|
||||
/// The trash of each home the plan lists (owner review 5, separate homes: <c>local\</c> and each <c>servers\<id>\</c>
|
||||
/// keep their own), each with its own fingerprint; emptying erases each one only while all of them still match.
|
||||
/// </summary>
|
||||
public IReadOnlyList<WorkspaceTrashListing> Listings { get; init; } = [];
|
||||
}
|
||||
|
||||
/// <summary>What emptying the workspace's trash erased.</summary>
|
||||
@@ -63,21 +69,28 @@ public sealed partial class WorkspaceHost
|
||||
var root = Root;
|
||||
if (!Directory.Exists(root)) return WorkspaceUsage.None;
|
||||
var media = 0L;
|
||||
foreach (var folder in Directory.EnumerateDirectories(root).Where(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)))
|
||||
foreach (var store in Homes.All())
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
media += WorkspaceCleaner.Measure(Path.Combine(folder, WorkspaceProject.MediaFolder)).Bytes;
|
||||
if (!Directory.Exists(store.Root)) continue;
|
||||
foreach (var folder in Directory.EnumerateDirectories(store.Root).Where(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
media += WorkspaceCleaner.Measure(Path.Combine(folder, WorkspaceProject.MediaFolder)).Bytes;
|
||||
}
|
||||
}
|
||||
var trash = WorkspaceTrash.List(root, cancellationToken);
|
||||
return new WorkspaceUsage(media, WorkspaceCleaner.Measure(root).Bytes, trash.Bytes, trash.Entries.Count);
|
||||
var trash = Homes.Trash(cancellationToken);
|
||||
return new WorkspaceUsage(media, WorkspaceCleaner.Measure(root).Bytes, trash.Sum(t => t.Bytes), trash.Sum(t => t.Entries.Count));
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>The plan of emptying the workspace's trash (<see cref="WorkspaceTrashPlan"/>); nothing is erased.</summary>
|
||||
/// <summary>The plan of emptying the trash of every home of the workspace (<see cref="WorkspaceTrashPlan"/>); nothing is erased.</summary>
|
||||
public Task<WorkspaceTrashPlan> PlanTrashAsync(CancellationToken cancellationToken = default) => Task.Run(() =>
|
||||
{
|
||||
var listing = WorkspaceTrash.List(Root, cancellationToken);
|
||||
return new WorkspaceTrashPlan(listing.Folder, [.. listing.Entries.Select(e => new WorkspaceTrashItem(e.Name, e.Bytes, e.Files, e.Trashed))], listing.Bytes,
|
||||
listing.Fingerprint);
|
||||
var listings = Homes.Trash(cancellationToken);
|
||||
var folder = listings.Count == 1 ? listings[0].Folder : WorkspaceTrash.FolderOf(Root);
|
||||
var fingerprint = string.Join('|', listings.Select(l => l.Folder + "=" + l.Fingerprint));
|
||||
return new WorkspaceTrashPlan(folder, [.. listings.SelectMany(l => l.Entries).Select(e => new WorkspaceTrashItem(e.Name, e.Bytes, e.Files, e.Trashed))
|
||||
.OrderBy(i => i.Trashed)], listings.Sum(l => l.Bytes), fingerprint)
|
||||
{ Listings = listings };
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
@@ -92,11 +105,25 @@ public sealed partial class WorkspaceHost
|
||||
return Task.Run(() =>
|
||||
{
|
||||
EnsureNotMoving();
|
||||
var listing = new WorkspaceTrashListing(plan.Folder, [.. plan.Items.Select(i => new WorkspaceTrashEntry(i.Name, i.Bytes, i.Files, i.Trashed))], plan.Bytes,
|
||||
plan.Fingerprint);
|
||||
IReadOnlyList<WorkspaceTrashListing> listings = plan.Listings.Count > 0 ? plan.Listings
|
||||
: [new WorkspaceTrashListing(plan.Folder, [.. plan.Items.Select(i => new WorkspaceTrashEntry(i.Name, i.Bytes, i.Files, i.Trashed))], plan.Bytes,
|
||||
plan.Fingerprint)];
|
||||
try
|
||||
{
|
||||
var (items, bytes) = WorkspaceTrash.Empty(Root, listing, cancellationToken);
|
||||
// Every trash is checked before any is emptied: a plan is erased whole or not at all.
|
||||
foreach (var listing in listings)
|
||||
{
|
||||
var now = WorkspaceTrash.List(Path.GetDirectoryName(listing.Folder)!, cancellationToken);
|
||||
if (!string.Equals(now.Fingerprint, listing.Fingerprint, StringComparison.Ordinal))
|
||||
throw new WorkspaceTrashChangedException($"The trash {now.Folder} changed after it was listed.");
|
||||
}
|
||||
var (items, bytes) = (0, 0L);
|
||||
foreach (var listing in listings.Where(l => !l.IsEmpty))
|
||||
{
|
||||
var (erased, freed) = WorkspaceTrash.Empty(Path.GetDirectoryName(listing.Folder)!, listing, cancellationToken);
|
||||
items += erased;
|
||||
bytes += freed;
|
||||
}
|
||||
return new WorkspaceTrashResult(items, bytes);
|
||||
}
|
||||
catch (WorkspaceTrashChangedException ex)
|
||||
@@ -130,7 +157,7 @@ public sealed partial class WorkspaceHost
|
||||
await _cleaning.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var cleaner = new WorkspaceCleaner(Sync, db, HomeOf) { LockWait = LockWait };
|
||||
var cleaner = new WorkspaceCleaner(AllSyncs(), db, HomeOf) { LockWait = LockWait };
|
||||
return await Task.Run(() => cleaner.CleanAsync(options, cancellationToken), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
@@ -147,10 +174,7 @@ public sealed partial class WorkspaceHost
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(job);
|
||||
if (job is not { Kind: JobKind.WorkspaceCopy, State: JobState.Done } || _settings.WorkspaceSizeLimitGb <= 0) return;
|
||||
if (WorkspaceJobs.CopyParameters(job) is not { } copied
|
||||
|| !string.Equals(Path.TrimEndingDirectorySeparator(Path.GetFullPath(copied.WorkspaceRoot)), Path.TrimEndingDirectorySeparator(Path.GetFullPath(Root)),
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
return;
|
||||
if (WorkspaceJobs.CopyParameters(job) is not { } copied || !IsUnderRoot(copied.WorkspaceRoot)) return;
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
@@ -165,6 +189,16 @@ public sealed partial class WorkspaceHost
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>The folder is the workspace's own or one of its homes (<c>local\</c>, <c>servers\<id>\</c>).</summary>
|
||||
private bool IsUnderRoot(string folder)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(folder)) return false;
|
||||
var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(Root));
|
||||
var full = Path.TrimEndingDirectorySeparator(Path.GetFullPath(folder));
|
||||
return string.Equals(full, root, StringComparison.OrdinalIgnoreCase)
|
||||
|| full.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>When each project was opened last, as Settings hold it now (written on the UI thread: copied until a copy reads whole).</summary>
|
||||
private IReadOnlyDictionary<Guid, DateTimeOffset> OpenedSnapshot()
|
||||
{
|
||||
@@ -331,7 +365,7 @@ public sealed partial class WorkspaceHost
|
||||
if (WorkspaceJobs.SendParameters(pending)?.ProjectId == project.ProjectId) return;
|
||||
}
|
||||
var title = project.ReadBundle().Sequence.Title ?? string.Empty;
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Send(workspace.Home.SequenceId, title, Store.Root, project.ProjectId, [], workspace.Home.Server),
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Send(workspace.Home.SequenceId, title, project.Store.Root, project.ProjectId, [], workspace.Home.Server),
|
||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("workspace", $"The renders of project {project.ProjectId} not sent yet are queued to be sent (job {job.Id})");
|
||||
}
|
||||
|
||||
@@ -268,13 +268,13 @@ public sealed partial class WorkspaceHost
|
||||
/// <summary>A project as the move's plan lists it, read from the workspace alone (its versions not sent, its ledger, its changes).</summary>
|
||||
private async Task<WorkspaceMoveProject> MoveProjectAsync(WorkspaceMoveFolder folder, CancellationToken cancellationToken)
|
||||
{
|
||||
var project = Store.Project(folder.ProjectId);
|
||||
var project = Find(folder.ProjectId) ?? Homes.Local.Project(folder.ProjectId);
|
||||
var entry = await EntryOrUnreadableAsync(project, cancellationToken).ConfigureAwait(false);
|
||||
// What a copy this Chronos cannot read holds is not known: it moves as it is, counted as work not at its home.
|
||||
if (!entry.IsReadable) return new WorkspaceMoveProject(folder.ProjectId, entry.KnownName ?? entry.Name, folder.Files, folder.Bytes, true, false);
|
||||
try
|
||||
{
|
||||
var status = await Sync.StatusAsync(folder.ProjectId, null, cancellationToken).ConfigureAwait(false);
|
||||
var status = await SyncOf(folder.ProjectId).StatusAsync(folder.ProjectId, null, cancellationToken).ConfigureAwait(false);
|
||||
var renders = WorkspaceCleaner.UnsentRenders(project);
|
||||
return new WorkspaceMoveProject(folder.ProjectId, entry.Name, folder.Files, folder.Bytes, status.Ahead + status.Damaged + renders > 0 || status.Conflict is not null,
|
||||
status.Modified && !status.ReadOnly);
|
||||
|
||||
@@ -12,6 +12,7 @@ using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Archiving.Jobs;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Protocol;
|
||||
|
||||
@@ -163,6 +164,7 @@ public sealed partial class WorkspaceHost
|
||||
private readonly Func<JobEngine?> _engine;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<Guid, Guid> _copies = [];
|
||||
private readonly Dictionary<Guid, int> _inEditor = [];
|
||||
private OpenWorkspace? _open;
|
||||
|
||||
public WorkspaceHost(AppEnvironment environment, AppSettings settings, Action savePreferences, Func<ChronosDatabase?> db, Func<ArchiveService?> archives,
|
||||
@@ -203,27 +205,116 @@ public sealed partial class WorkspaceHost
|
||||
|
||||
public AppSettings Settings => _settings;
|
||||
|
||||
/// <summary>The sync over the workspace folder in use (made, with the folder and its manifest, at first use).</summary>
|
||||
/// <summary>
|
||||
/// The servers known by id and their aliases (owner review 5, #66; the desktop's <c>SyncHost.Identity</c>): a server home's
|
||||
/// folder is named by its server's canonical key. Unset, every key is its own.
|
||||
/// </summary>
|
||||
public Func<ServerIdentity>? Identity { get; set; }
|
||||
|
||||
/// <summary>The two server keys name the same server (its id key and the addresses it was reached by, <see cref="Identity"/>).</summary>
|
||||
public bool SameServer(string? left, string? right) => (Identity?.Invoke() ?? ServerIdentity.None).Same(left, right);
|
||||
|
||||
/// <summary>
|
||||
/// The workspace folder in use split into its homes (owner review 5, #66: <c>local</c> for this PC's archive,
|
||||
/// <c>servers\<id></c> for each server; a folder still in the 1.0 layout is one store until its folders are sorted),
|
||||
/// made, with the folder and its manifest, at first use.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The folder cannot be made or read.</exception>
|
||||
/// <exception cref="InvalidDataException">The folder holds another program's or a newer workspace manifest.</exception>
|
||||
public WorkspaceSync Sync
|
||||
public WorkspaceHomes Homes
|
||||
{
|
||||
get
|
||||
{
|
||||
var root = Root;
|
||||
var identity = Identity?.Invoke() ?? ServerIdentity.None;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_open is not { } open || !string.Equals(open.Root, root, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var store = new WorkspaceStore(root);
|
||||
_open = open = new OpenWorkspace(root, new WorkspaceSync(store, ClientName) { LockWait = LockWait });
|
||||
}
|
||||
return open.Sync;
|
||||
_open = open = new OpenWorkspace(root, new WorkspaceHomes(root, identity));
|
||||
open.Homes.Identity = identity;
|
||||
return open.Homes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public WorkspaceStore Store => Sync.Store;
|
||||
/// <summary>Opens the folder in use again at next use (after its folders were sorted into homes: its layout changed).</summary>
|
||||
public void Reopen()
|
||||
{
|
||||
lock (_gate) _open = null;
|
||||
}
|
||||
|
||||
/// <summary>The project's folder in whichever home holds it; null when the workspace has none.</summary>
|
||||
public WorkspaceProject? Find(Guid projectId) => Homes.Find(projectId);
|
||||
|
||||
/// <summary>
|
||||
/// Marks <paramref name="projectId"/> open in the editor until the result is disposed. The editor keeps its project's
|
||||
/// folder for as long as it is open, so the move of a server's home to its id waits meanwhile (owner review 5, #66).
|
||||
/// </summary>
|
||||
public IDisposable HoldInEditor(Guid projectId)
|
||||
{
|
||||
lock (_inEditor) _inEditor[projectId] = _inEditor.GetValueOrDefault(projectId) + 1;
|
||||
return new EditorHold(this, projectId);
|
||||
}
|
||||
|
||||
/// <summary>A project is open in the editor (<see cref="HoldInEditor"/>).</summary>
|
||||
public bool HasProjectsInEditor
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_inEditor) return _inEditor.Count > 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseInEditor(Guid projectId)
|
||||
{
|
||||
lock (_inEditor)
|
||||
{
|
||||
if (!_inEditor.TryGetValue(projectId, out var count)) return;
|
||||
if (count <= 1) _inEditor.Remove(projectId);
|
||||
else _inEditor[projectId] = count - 1;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class EditorHold(WorkspaceHost host, Guid projectId) : IDisposable
|
||||
{
|
||||
private int _released;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _released, 1) == 0) host.ReleaseInEditor(projectId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The sync over the store of the home that holds <paramref name="projectId"/>.</summary>
|
||||
/// <exception cref="WorkspaceException">The workspace has no such project (<c>Errors.Workspace.NotCheckedOut</c>).</exception>
|
||||
public WorkspaceSync SyncOf(Guid projectId) => SyncOf(Existing(projectId).Store);
|
||||
|
||||
/// <summary>The sync over the store a project of <paramref name="home"/> is checked out into (made, with its manifest, when it is not there).</summary>
|
||||
public WorkspaceSync SyncFor(BundleHome home) => SyncOf(Homes.StoreFor(home));
|
||||
|
||||
/// <summary>The sync over <paramref name="store"/> (one per store of the folder in use).</summary>
|
||||
public WorkspaceSync SyncOf(WorkspaceStore store)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
_ = Homes;
|
||||
lock (_gate)
|
||||
{
|
||||
var open = _open!;
|
||||
if (!open.Syncs.TryGetValue(store.Root, out var sync))
|
||||
{
|
||||
sync = new WorkspaceSync(store, ClientName) { LockWait = LockWait };
|
||||
open.Syncs[store.Root] = sync;
|
||||
}
|
||||
return sync;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The syncs of every home's store (the clean-up counts them together).</summary>
|
||||
private IReadOnlyList<WorkspaceSync> AllSyncs()
|
||||
{
|
||||
var stores = Homes.All();
|
||||
return stores.Count == 0 ? [SyncOf(Homes.Local)] : [.. stores.Select(SyncOf)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The project home of a server home (phase 5b; the desktop's <c>SyncHost.ProjectHome</c>): null while this PC is not
|
||||
@@ -273,8 +364,8 @@ public sealed partial class WorkspaceHost
|
||||
BundleHome home;
|
||||
try
|
||||
{
|
||||
if (!IsAvailable || Store.Find(change.ProjectId)?.ReadBundle().Workspace?.Home is not { IsArchive: false } held || held.SequenceId != change.SequenceId
|
||||
|| !string.Equals(held.Server, server, StringComparison.OrdinalIgnoreCase))
|
||||
if (!IsAvailable || Find(change.ProjectId)?.ReadBundle().Workspace?.Home is not { IsArchive: false } held || held.SequenceId != change.SequenceId
|
||||
|| !SameServer(held.Server, server))
|
||||
return;
|
||||
home = held;
|
||||
}
|
||||
@@ -298,7 +389,7 @@ public sealed partial class WorkspaceHost
|
||||
{
|
||||
// Nothing is written into a folder being moved: the next status read takes the name.
|
||||
if (IsMoving) Log.Info("workspace", $"The new name of project {change.ProjectId} on {server} waits for the workspace's move");
|
||||
else if (HomeOf(home) is { } reached) await Sync.FollowNameAsync(change.ProjectId, reached).ConfigureAwait(false);
|
||||
else if (HomeOf(home) is { } reached) await SyncOf(change.ProjectId).FollowNameAsync(change.ProjectId, reached).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
@@ -316,7 +407,7 @@ public sealed partial class WorkspaceHost
|
||||
{
|
||||
if (!Directory.Exists(Root) && await InsideALibraryAsync(cancellationToken).ConfigureAwait(false)) return [];
|
||||
var entries = new List<WorkspaceEntry>();
|
||||
foreach (var project in Store.List())
|
||||
foreach (var project in Homes.List())
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -345,7 +436,7 @@ public sealed partial class WorkspaceHost
|
||||
{
|
||||
EnsureNotMoving();
|
||||
if (!Directory.Exists(Root) && await InsideALibraryAsync(cancellationToken).ConfigureAwait(false)) return null;
|
||||
return Store.Find(projectId) is { } project ? await EntryOrUnreadableAsync(project, cancellationToken).ConfigureAwait(false) : null;
|
||||
return Find(projectId) is { } project ? await EntryOrUnreadableAsync(project, cancellationToken).ConfigureAwait(false) : null;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>Why a copy this Chronos cannot read neither opens nor is checked out over: a newer Chronos wrote it, or it is damaged.</summary>
|
||||
@@ -381,7 +472,7 @@ public sealed partial class WorkspaceHost
|
||||
if (plan.IsSame) return;
|
||||
if (plan.InsideLibrary) throw new WorkspaceException(Loc.L("Errors.Workspace.InsideLibraryFolder", plan.Folder)) { FolderRefused = true };
|
||||
// Opened before it is named: a folder that is no workspace is refused while the one in use still is.
|
||||
await Task.Run(() => new WorkspaceStore(plan.Folder), cancellationToken).ConfigureAwait(false);
|
||||
await Task.Run(() => new WorkspaceHomes(plan.Folder), cancellationToken).ConfigureAwait(false);
|
||||
// Back on the caller's thread (the UI's): the settings are written there.
|
||||
_settings.WorkspaceFolder = string.Equals(plan.Folder, Path.GetFullPath(DefaultRoot(_environment)), StringComparison.OrdinalIgnoreCase) ? null : plan.Folder;
|
||||
_savePreferences();
|
||||
@@ -470,10 +561,10 @@ public sealed partial class WorkspaceHost
|
||||
: listed.FirstOrDefault(p => p.IsMain) ?? throw new ProjectException(ProjectError.SequenceEmpty, $"Sequence {sequenceId} has no projects yet.");
|
||||
}
|
||||
// A copy here this Chronos cannot read (damaged, or a newer Chronos's): nothing is checked out or copied over it.
|
||||
if (Store.Find(project.Id) is { } here && await EntryOrUnreadableAsync(here, cancellationToken).ConfigureAwait(false) is { IsReadable: false } unreadable)
|
||||
if (Find(project.Id) is { } here && await EntryOrUnreadableAsync(here, cancellationToken).ConfigureAwait(false) is { IsReadable: false } unreadable)
|
||||
throw Unreadable(unreadable);
|
||||
var folder = await Sync.CheckoutAsync(reached, project.Id, media, cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("workspace", $"Project {project.Id} ('{project.Name}') of sequence {sequenceId} checked out into {Store.Root}");
|
||||
var folder = await SyncFor(home).CheckoutAsync(reached, project.Id, media, cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("workspace", $"Project {project.Id} ('{project.Name}') of sequence {sequenceId} checked out into {folder.Store.Root}");
|
||||
var title = folder.ReadBundle().Sequence.Title ?? string.Empty;
|
||||
return new WorkspaceCheckout(project, await QueueCopyCoreAsync(home, project.Id, media, title, cancellationToken).ConfigureAwait(false));
|
||||
}, cancellationToken);
|
||||
@@ -507,14 +598,14 @@ public sealed partial class WorkspaceHost
|
||||
var home = askHome ? HomeOf(named) : null;
|
||||
try
|
||||
{
|
||||
if (askHome && home is null && !named.IsArchive) return Offline(await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false));
|
||||
return await Sync.StatusAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
if (askHome && home is null && !named.IsArchive) return Offline(await SyncOf(projectId).StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false));
|
||||
return await SyncOf(projectId).StatusAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (home is not null && ex is ProjectException or DatabaseException or IOException)
|
||||
{
|
||||
var gone = ex is ProjectException { Reason: ProjectError.SequenceNotFound or ProjectError.NotFound };
|
||||
Log.Info("workspace", gone ? $"The home no longer has project {projectId}: {ex.Message}" : $"The home of project {projectId} could not be asked: {ex.Message}");
|
||||
var local = await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
var local = await SyncOf(projectId).StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
return local with
|
||||
{
|
||||
Offline = true,
|
||||
@@ -545,8 +636,8 @@ public sealed partial class WorkspaceHost
|
||||
var version = await Task.Run(async () =>
|
||||
{
|
||||
EnsureNotMoving();
|
||||
EnsureWritable(await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false));
|
||||
return await Sync.CommitAsync(projectId, note, null, cancellationToken).ConfigureAwait(false);
|
||||
EnsureWritable(await SyncOf(projectId).StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false));
|
||||
return await SyncOf(projectId).CommitAsync(projectId, note, null, cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
if (version is not null) Log.Info("workspace", $"Version {version.Number} of project {projectId} saved");
|
||||
if (!_settings.WorkspaceAutoSync) return new WorkspaceSaveResult(version, null, await StatusAsync(projectId, cancellationToken: cancellationToken).ConfigureAwait(false), null);
|
||||
@@ -568,8 +659,8 @@ public sealed partial class WorkspaceHost
|
||||
public Task<ProjectVersion?> CommitAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
EnsureNotMoving();
|
||||
EnsureWritable(await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false));
|
||||
return await Sync.CommitAsync(projectId, null, null, cancellationToken).ConfigureAwait(false);
|
||||
EnsureWritable(await SyncOf(projectId).StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false));
|
||||
return await SyncOf(projectId).CommitAsync(projectId, null, null, cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
@@ -583,10 +674,10 @@ public sealed partial class WorkspaceHost
|
||||
{
|
||||
EnsureNotMoving();
|
||||
var home = HomeOfProject(projectId);
|
||||
var pulled = await Sync.PullAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
var pulled = await SyncOf(projectId).PullAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
// Deleted at the library: nothing to send, nothing to prune; the copy stays as it is.
|
||||
if (pulled.Status.State is WorkspaceState.Conflict or WorkspaceState.Gone) return pulled;
|
||||
var pushed = await Sync.PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
var pushed = await SyncOf(projectId).PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
await PruneQuietlyAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("workspace", $"Project {projectId} synced: {pushed.Pushed.Count} sent, {pulled.Pulled.Count + pushed.Pulled.Count} taken, {pushed.Status.State}");
|
||||
return new WorkspaceSyncResult(pushed.Status, pushed.Pushed, [.. pulled.Pulled, .. pushed.Pulled]);
|
||||
@@ -609,16 +700,16 @@ public sealed partial class WorkspaceHost
|
||||
var name = SiblingName(Existing(projectId).ReadBundle().Workspace!.Name);
|
||||
try
|
||||
{
|
||||
resolved = await Sync.ResolveAsync(projectId, home, choice, takeServer, name, shown, cancellationToken).ConfigureAwait(false);
|
||||
resolved = await SyncOf(projectId).ResolveAsync(projectId, home, choice, takeServer, name, shown, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (ProjectException ex) when (ex.Reason is ProjectError.NameTaken or ProjectError.InvalidName)
|
||||
{
|
||||
resolved = await Sync.ResolveAsync(projectId, home, choice, takeServer, null, shown, cancellationToken).ConfigureAwait(false);
|
||||
resolved = await SyncOf(projectId).ResolveAsync(projectId, home, choice, takeServer, null, shown, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resolved = await Sync.ResolveAsync(projectId, home, choice, takeServer, null, shown, cancellationToken).ConfigureAwait(false);
|
||||
resolved = await SyncOf(projectId).ResolveAsync(projectId, home, choice, takeServer, null, shown, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
if (resolved.Status.State == WorkspaceState.Conflict)
|
||||
{
|
||||
@@ -628,7 +719,7 @@ public sealed partial class WorkspaceHost
|
||||
// Deleted in the library: nothing more to send (a Save both's copy holds the versions already).
|
||||
if (resolved.Status.State == WorkspaceState.Gone) return resolved;
|
||||
Log.Info("workspace", $"Conflict of project {projectId} settled: {choice}{(resolved.SavedAs is { } copy ? $", saved as '{copy.Name}'" : string.Empty)}");
|
||||
var pushed = await Sync.PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
var pushed = await SyncOf(projectId).PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
return new WorkspaceSyncResult(pushed.Status, [.. resolved.Pushed, .. pushed.Pushed], [.. resolved.Pulled, .. pushed.Pulled], resolved.SavedAs)
|
||||
{
|
||||
Kept = resolved.Kept,
|
||||
@@ -652,11 +743,11 @@ public sealed partial class WorkspaceHost
|
||||
public Task<ProjectSettings> RestoreAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
EnsureNotMoving();
|
||||
var status = await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
var status = await SyncOf(projectId).StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
if (status.ReadOnly) throw new WorkspaceException(Loc.L("Errors.Workspace.ReadOnly"));
|
||||
if (status.MediaRemoved) throw new WorkspaceException(Loc.L("Errors.Workspace.MediaMissing"));
|
||||
var home = HomeOf(Existing(projectId).ReadBundle().Workspace!.Home);
|
||||
var restored = await Sync.RestoreAsync(projectId, versionId, home, cancellationToken).ConfigureAwait(false);
|
||||
var restored = await SyncOf(projectId).RestoreAsync(projectId, versionId, home, cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("workspace", $"Version {versionId} of project {projectId} restored as the working state");
|
||||
return restored;
|
||||
}, cancellationToken);
|
||||
@@ -665,7 +756,7 @@ public sealed partial class WorkspaceHost
|
||||
public Task<WorkspaceHistory> HistoryAsync(Guid projectId, int limit = 50, CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
var project = Existing(projectId);
|
||||
var unsent = Sync.Unpushed(projectId);
|
||||
var unsent = SyncOf(projectId).Unpushed(projectId);
|
||||
var home = HomeOf(project.ReadBundle().Workspace!.Home);
|
||||
IReadOnlyList<ProjectVersion> stored;
|
||||
var offline = false;
|
||||
@@ -740,9 +831,9 @@ public sealed partial class WorkspaceHost
|
||||
var workspace = project.ReadBundle().Workspace!;
|
||||
if (workspace.IsMain) throw new ProjectException(ProjectError.MainCannotBeDeleted, "The Main project cannot be deleted.");
|
||||
var home = HomeOfProject(projectId);
|
||||
var local = await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
var local = await SyncOf(projectId).StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
if (local.Conflict is not null) throw new WorkspaceException(Loc.L("Errors.Workspace.Conflict"));
|
||||
if (!local.ReadOnly) await Sync.CommitAsync(projectId, null, null, cancellationToken).ConfigureAwait(false);
|
||||
if (!local.ReadOnly) await SyncOf(projectId).CommitAsync(projectId, null, null, cancellationToken).ConfigureAwait(false);
|
||||
var status = (await SendAsync(projectId, home, cancellationToken).ConfigureAwait(false)).Status;
|
||||
if (status.State == WorkspaceState.Gone) throw new WorkspaceException(Loc.L("Errors.Workspace.DeletedAtHome"));
|
||||
if (status.Conflict is not null) throw new WorkspaceException(Loc.L("Errors.Workspace.Conflict"));
|
||||
@@ -773,7 +864,7 @@ public sealed partial class WorkspaceHost
|
||||
/// <exception cref="WorkspaceException">A conflict waits (open the project and Sync to settle it), or something is still not at the home.</exception>
|
||||
public async Task SendAllAsync(Guid projectId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var local = await Task.Run(() => Sync.StatusAsync(projectId, null, cancellationToken), cancellationToken).ConfigureAwait(false);
|
||||
var local = await Task.Run(() => SyncOf(projectId).StatusAsync(projectId, null, cancellationToken), cancellationToken).ConfigureAwait(false);
|
||||
if (local is { Conflict: null, ReadOnly: false, MediaRemoved: false }) await CommitAsync(projectId, cancellationToken).ConfigureAwait(false);
|
||||
var status = (await SyncAsync(projectId, cancellationToken).ConfigureAwait(false)).Status;
|
||||
if (status.State == WorkspaceState.Gone) throw new WorkspaceException(Loc.L("Errors.Workspace.DeletedAtHome"));
|
||||
@@ -837,7 +928,7 @@ public sealed partial class WorkspaceHost
|
||||
var workspace = project.ReadBundle().Workspace!;
|
||||
if (!discard)
|
||||
{
|
||||
var status = await Sync.StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
var status = await SyncOf(projectId).StatusAsync(projectId, null, cancellationToken).ConfigureAwait(false);
|
||||
// A version saved here that cannot be read may be one the home never had: only discarding keeps it aside.
|
||||
if (status.Ahead > 0 || status.Damaged > 0 || (status.Modified && !status.ReadOnly) || status.Conflict is not null)
|
||||
throw new WorkspaceException(Unsent(workspace.Home));
|
||||
@@ -894,8 +985,8 @@ public sealed partial class WorkspaceHost
|
||||
/// <summary>The local versions sent (after the home's new ones were taken), as one sync without the pruning.</summary>
|
||||
private async Task<WorkspaceSyncResult> SendAsync(Guid projectId, IProjectHome home, CancellationToken cancellationToken)
|
||||
{
|
||||
var pulled = await Sync.PullAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
return pulled.Status.State is WorkspaceState.Conflict or WorkspaceState.Gone ? pulled : await Sync.PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
var pulled = await SyncOf(projectId).PullAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
return pulled.Status.State is WorkspaceState.Conflict or WorkspaceState.Gone ? pulled : await SyncOf(projectId).PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>The home holds this copy's work: its head and the base this copy was last in step with read back, each matching its hash.</summary>
|
||||
@@ -948,7 +1039,7 @@ public sealed partial class WorkspaceHost
|
||||
{ State: JobState.Queued or JobState.Running or JobState.Paused })
|
||||
await engine.CancelAsync(copy, cancellationToken).ConfigureAwait(false);
|
||||
// Filled under a staging name Empty… never lists, and named once whole.
|
||||
using var trash = WorkspaceTrash.Begin(Store.Root, project.ProjectId, DateTime.UtcNow);
|
||||
using var trash = WorkspaceTrash.Begin(project.Store.Root, project.ProjectId, DateTime.UtcNow);
|
||||
var aside = trash.Path;
|
||||
using (await project.LockAsync(LockWait, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
@@ -1087,11 +1178,12 @@ public sealed partial class WorkspaceHost
|
||||
{
|
||||
var sequence = await db.Sequences.GetAsync(home.SequenceId, cancellationToken).ConfigureAwait(false)
|
||||
?? throw new ProjectException(ProjectError.SequenceNotFound, $"Sequence {home.SequenceId} is not in the index.");
|
||||
request = WorkspaceJobs.Copy(sequence, Store.Root, projectId, media);
|
||||
request = WorkspaceJobs.Copy(sequence, Existing(projectId).Store.Root, projectId, media);
|
||||
}
|
||||
else
|
||||
{
|
||||
request = WorkspaceJobs.CopyFromServer(home.SequenceId, title, home.Server ?? throw new InvalidDataException("A server home names no server."), Store.Root,
|
||||
request = WorkspaceJobs.CopyFromServer(home.SequenceId, title, home.Server ?? throw new InvalidDataException("A server home names no server."),
|
||||
Existing(projectId).Store.Root,
|
||||
projectId, media);
|
||||
}
|
||||
var job = await engine.EnqueueAsync(request, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
@@ -1108,7 +1200,7 @@ public sealed partial class WorkspaceHost
|
||||
: "This PC is not signed in to the server of the project, or it keeps no projects.");
|
||||
}
|
||||
|
||||
private WorkspaceProject Existing(Guid projectId) => Store.Find(projectId) ?? throw new WorkspaceException(Loc.L("Errors.Workspace.NotCheckedOut"));
|
||||
private WorkspaceProject Existing(Guid projectId) => Find(projectId) ?? throw new WorkspaceException(Loc.L("Errors.Workspace.NotCheckedOut"));
|
||||
|
||||
private static WorkspaceEntry Entry(WorkspaceProject project, ProjectBundle bundle)
|
||||
{
|
||||
@@ -1206,14 +1298,20 @@ public sealed partial class WorkspaceHost
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The projects a workspace folder holds (folders named by a project id with a working state), without making or opening it.</summary>
|
||||
/// <summary>
|
||||
/// The projects a workspace folder holds (folders named by a project id with a working state), in it and in its homes'
|
||||
/// folders (<c>local</c>, <c>servers\<folder></c>, owner review 5), without making or opening it.
|
||||
/// </summary>
|
||||
private static int CountProjects(string folder)
|
||||
{
|
||||
if (!Directory.Exists(folder)) return 0;
|
||||
try
|
||||
{
|
||||
return Directory.EnumerateDirectories(folder).Count(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)
|
||||
&& File.Exists(Path.Combine(d, WorkspaceProject.BundleFile)));
|
||||
var stores = new List<string> { folder, HomeFolders.LocalPath(folder) };
|
||||
var servers = Path.Combine(folder, HomeFolders.Servers);
|
||||
if (Directory.Exists(servers)) stores.AddRange(Directory.EnumerateDirectories(servers).Where(d => HomeFolders.IsServerFolder(Path.GetFileName(d))));
|
||||
return stores.Where(Directory.Exists).Sum(store => Directory.EnumerateDirectories(store).Count(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)
|
||||
&& File.Exists(Path.Combine(d, WorkspaceProject.BundleFile))));
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
@@ -1236,5 +1334,9 @@ public sealed partial class WorkspaceHost
|
||||
return (files, bytes);
|
||||
}
|
||||
|
||||
private sealed record OpenWorkspace(string Root, WorkspaceSync Sync);
|
||||
/// <summary>The workspace folder in use, its homes and the sync of each of its stores (by root).</summary>
|
||||
private sealed record OpenWorkspace(string Root, WorkspaceHomes Homes)
|
||||
{
|
||||
public Dictionary<string, WorkspaceSync> Syncs { get; } = new(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user