merge: review2-p5a-fix-desktop into phase 5a fix round 1 integration
# Conflicts: # src/Chronos.Data/Workspaces/WorkspaceSync.cs
This commit is contained in:
commit
1a35fcdb27
28 files changed
+1328
-139
No files matched your search
@@ -2,6 +2,7 @@ using System.IO;
|
||||
using Chronos.App.Infrastructure;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
@@ -38,7 +39,11 @@ public sealed record WorkspaceHistory(IReadOnlyList<ProjectVersion> Versions, IR
|
||||
/// The plan of "Remove local copy": what the project's folder holds (files and bytes) and what would not be at the home
|
||||
/// after it (versions not sent, changes not saved as a version, a conflict to settle).
|
||||
/// </summary>
|
||||
public sealed record WorkspaceRemovalPlan(Guid ProjectId, string Name, int Files, long Bytes, int Unsent, bool Modified, bool Conflict, bool Offline)
|
||||
/// <param name="Gone">
|
||||
/// The home answered it no longer has the project or its sequence: nothing can be sent or checked there, so the copy can
|
||||
/// only go with its work kept aside (Discard).
|
||||
/// </param>
|
||||
public sealed record WorkspaceRemovalPlan(Guid ProjectId, string Name, int Files, long Bytes, int Unsent, bool Modified, bool Conflict, bool Offline, bool Gone = false)
|
||||
{
|
||||
/// <summary>Everything the copy holds is at the home: it can go without discarding anything.</summary>
|
||||
public bool IsSafe => Unsent == 0 && !Modified && !Conflict;
|
||||
@@ -80,8 +85,11 @@ public sealed class WorkspaceHost
|
||||
_engine = engine;
|
||||
}
|
||||
|
||||
/// <summary>The program and PC versions made here carry, as the archive's own versions do.</summary>
|
||||
public static string ClientName { get; } = "Chronos on " + Environment.MachineName;
|
||||
/// <summary>
|
||||
/// The PC versions made here carry ("STUDIO-PC"): data, shown as it is by History (no words of the UI in it, so every
|
||||
/// language reads it alike).
|
||||
/// </summary>
|
||||
public static string ClientName { get; } = Environment.MachineName;
|
||||
|
||||
/// <summary>How long an operation waits for a project another writer holds (a copy job writing its media index).</summary>
|
||||
public TimeSpan LockWait { get; init; } = TimeSpan.FromSeconds(30);
|
||||
@@ -138,17 +146,24 @@ public sealed class WorkspaceHost
|
||||
public IProjectHome HomeOfSequence(Guid sequenceId) =>
|
||||
HomeOf(new BundleHome(BundleHome.ArchiveKind, sequenceId)) ?? throw new WorkspaceException(Loc.L("Editor.Error.NoEngine"));
|
||||
|
||||
/// <summary>The projects of the workspace (one whose working state cannot be read is left out, and logged).</summary>
|
||||
public Task<IReadOnlyList<WorkspaceEntry>> ListAsync(CancellationToken cancellationToken = default) =>
|
||||
Task.Run(() => (IReadOnlyList<WorkspaceEntry>)[.. Store.ListBundles().Select(b => Entry(b.Project, b.Bundle))], cancellationToken);
|
||||
/// <summary>
|
||||
/// The projects of the workspace (one whose working state cannot be read is left out, and logged). A workspace folder
|
||||
/// not made yet that lies inside a library is not made by listing it: it lists nothing.
|
||||
/// </summary>
|
||||
public Task<IReadOnlyList<WorkspaceEntry>> ListAsync(CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
if (!Directory.Exists(Root) && await InsideALibraryAsync(cancellationToken).ConfigureAwait(false)) return [];
|
||||
return (IReadOnlyList<WorkspaceEntry>)[.. Store.ListBundles().Select(b => Entry(b.Project, b.Bundle))];
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>The workspace projects of one sequence.</summary>
|
||||
public async Task<IReadOnlyList<WorkspaceEntry>> ProjectsOfAsync(Guid sequenceId, CancellationToken cancellationToken = default) =>
|
||||
[.. (await ListAsync(cancellationToken).ConfigureAwait(false)).Where(e => e.SequenceId == sequenceId)];
|
||||
|
||||
/// <summary>A workspace project by id; null when the workspace has none (or it cannot be read).</summary>
|
||||
public Task<WorkspaceEntry?> FindAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(() =>
|
||||
public Task<WorkspaceEntry?> FindAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
if (!Directory.Exists(Root) && await InsideALibraryAsync(cancellationToken).ConfigureAwait(false)) return null;
|
||||
if (Store.Find(projectId) is not { } project) return null;
|
||||
try
|
||||
{
|
||||
@@ -191,12 +206,15 @@ public sealed class WorkspaceHost
|
||||
/// </summary>
|
||||
/// <exception cref="ProjectException">The home has no such project, or the name is invalid or taken.</exception>
|
||||
/// <exception cref="HomeUnavailableException">The archive is offline or busy.</exception>
|
||||
/// <exception cref="WorkspaceException">The workspace folder lies inside a library: nothing was written, here or at the home.</exception>
|
||||
public async Task<WorkspaceCheckout> CheckoutAsync(Guid sequenceId, Guid? projectId, string? newName, string media = BundleMedia.Proxies,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var home = HomeOfSequence(sequenceId);
|
||||
var checkout = await Task.Run(async () =>
|
||||
{
|
||||
// Refused before anything is written: the copy job would refuse such a folder after the checkout filled it.
|
||||
if (await InsideALibraryAsync(cancellationToken).ConfigureAwait(false)) throw new WorkspaceException(Loc.L("Errors.Workspace.InsideLibraryEdit", Root));
|
||||
SequenceProject project;
|
||||
if (newName is not null)
|
||||
{
|
||||
@@ -229,8 +247,9 @@ public sealed class WorkspaceHost
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Where a project stands. With <paramref name="askHome"/>, against its home's head too: a home that cannot be reached,
|
||||
/// or that no longer has the project (deleted elsewhere), reads as offline.
|
||||
/// Where a project stands. With <paramref name="askHome"/>, against its home's head too: a home that cannot be reached
|
||||
/// reads as offline; one that answers it no longer has the project or its sequence (the sequence left the index) reads
|
||||
/// as <see cref="WorkspaceState.Gone"/>, whose local copy can then only be removed (nothing is removed by itself).
|
||||
/// </summary>
|
||||
public Task<WorkspaceStatus> StatusAsync(Guid projectId, bool askHome = true, CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
@@ -242,9 +261,16 @@ public sealed class WorkspaceHost
|
||||
}
|
||||
catch (Exception ex) when (home is not null && ex is ProjectException or DatabaseException or IOException)
|
||||
{
|
||||
Log.Info("workspace", $"The home of project {projectId} could not be asked: {ex.Message}");
|
||||
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);
|
||||
return local with { Offline = true, State = local.State is WorkspaceState.MediaRemoved or WorkspaceState.Conflict ? local.State : WorkspaceState.Offline };
|
||||
return local with
|
||||
{
|
||||
Offline = true,
|
||||
State = local.State is WorkspaceState.MediaRemoved or WorkspaceState.Conflict && !gone ? local.State
|
||||
: gone ? WorkspaceState.Gone
|
||||
: WorkspaceState.Offline,
|
||||
};
|
||||
}
|
||||
}, cancellationToken);
|
||||
|
||||
@@ -303,11 +329,13 @@ public sealed class WorkspaceHost
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Settles the conflict a sync stopped at, then sends what waits. <see cref="ConflictChoice.SaveBoth"/> sends this
|
||||
/// Settles the conflict a sync stopped at, as <paramref name="shown"/> listed it to the user (else as recorded), then
|
||||
/// sends what waits. When what differs now is not what was shown, nothing is settled: the conflict found now comes back
|
||||
/// (<see cref="WorkspaceState.Conflict"/>) to be asked again. <see cref="ConflictChoice.SaveBoth"/> sends this
|
||||
/// workspace's versions to a sibling project named "<name> (<this PC>)" (a number added when that name is taken).
|
||||
/// </summary>
|
||||
public Task<WorkspaceSyncResult> ResolveAsync(Guid projectId, ConflictChoice choice, IReadOnlySet<string>? takeServer = null,
|
||||
CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
WorkspaceConflict? shown = null, CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
var home = HomeOfProject(projectId);
|
||||
WorkspaceSyncResult resolved;
|
||||
@@ -316,21 +344,28 @@ public sealed class WorkspaceHost
|
||||
var name = SiblingName(Existing(projectId).ReadBundle().Workspace!.Name);
|
||||
try
|
||||
{
|
||||
resolved = await Sync.ResolveAsync(projectId, home, choice, takeServer, name, cancellationToken).ConfigureAwait(false);
|
||||
resolved = await Sync.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, cancellationToken).ConfigureAwait(false);
|
||||
resolved = await Sync.ResolveAsync(projectId, home, choice, takeServer, null, shown, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
resolved = await Sync.ResolveAsync(projectId, home, choice, takeServer, null, cancellationToken).ConfigureAwait(false);
|
||||
resolved = await Sync.ResolveAsync(projectId, home, choice, takeServer, null, shown, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
if (resolved.Status.State == WorkspaceState.Conflict)
|
||||
{
|
||||
Log.Info("workspace", $"Conflict of project {projectId} not settled ({choice}): what differs changed since it was shown");
|
||||
return resolved;
|
||||
}
|
||||
Log.Info("workspace", $"Conflict of project {projectId} settled: {choice}{(resolved.SavedAs is { } copy ? $", saved as '{copy.Name}'" : string.Empty)}");
|
||||
if (resolved.Status.State == WorkspaceState.Conflict) return resolved;
|
||||
var pushed = await Sync.PushAsync(projectId, home, cancellationToken).ConfigureAwait(false);
|
||||
return new WorkspaceSyncResult(pushed.Status, [.. resolved.Pushed, .. pushed.Pushed], [.. resolved.Pulled, .. pushed.Pulled], resolved.SavedAs);
|
||||
return new WorkspaceSyncResult(pushed.Status, [.. resolved.Pushed, .. pushed.Pushed], [.. resolved.Pulled, .. pushed.Pulled], resolved.SavedAs)
|
||||
{
|
||||
Kept = resolved.Kept,
|
||||
};
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>"<paramref name="name"/> (<c>PC</c>)", within the 60 characters of a project name.</summary>
|
||||
@@ -414,27 +449,56 @@ public sealed class WorkspaceHost
|
||||
/// Deletes a project (never Main): the working state saved and sent first, so its home's trash
|
||||
/// (<c>.chronos/projects/.trash</c>) holds every version; then the local copy goes.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// False when the home deleted the project but its local copy could not be removed (a file in use): the copy stays,
|
||||
/// and Remove local copy takes it later.
|
||||
/// </returns>
|
||||
/// <exception cref="WorkspaceException">Something could not be sent (the home out of reach, a conflict): nothing was deleted.</exception>
|
||||
/// <exception cref="ProjectException">Main, or the project is gone.</exception>
|
||||
public async Task DeleteProjectAsync(Guid projectId, CancellationToken cancellationToken = default)
|
||||
public async Task<bool> DeleteProjectAsync(Guid projectId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var sequenceId = await Task.Run(async () =>
|
||||
var (sequenceId, removed) = await Task.Run(async () =>
|
||||
{
|
||||
var project = Existing(projectId);
|
||||
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);
|
||||
if (local.Conflict is not null) throw new WorkspaceException(Loc.L("Errors.Workspace.Unsent"));
|
||||
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);
|
||||
var status = (await SendAsync(projectId, home, cancellationToken).ConfigureAwait(false)).Status;
|
||||
if (status.Ahead > 0 || status.Conflict is not null || (status.Modified && !status.ReadOnly)) throw new WorkspaceException(Loc.L("Errors.Workspace.Unsent"));
|
||||
if (status.Conflict is not null) throw new WorkspaceException(Loc.L("Errors.Workspace.Conflict"));
|
||||
if (status.Ahead > 0 || (status.Modified && !status.ReadOnly)) throw new WorkspaceException(Loc.L("Errors.Workspace.Unsent"));
|
||||
await ArchiveProjects(project).DeleteAsync(projectId, cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("workspace", $"Project {projectId} ('{workspace.Name}') deleted at its home");
|
||||
await RemoveFolderAsync(project, cancellationToken).ConfigureAwait(false);
|
||||
return workspace.Home.SequenceId;
|
||||
try
|
||||
{
|
||||
await RemoveFolderAsync(project, cancellationToken).ConfigureAwait(false);
|
||||
return (workspace.Home.SequenceId, true);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or LeaseUnavailableException)
|
||||
{
|
||||
// The home's delete stands (its trash holds every version); the copy, all of it sent, waits for Remove local copy.
|
||||
Log.Warning("workspace", $"Project {projectId} was deleted at its home, but its local copy stays", ex);
|
||||
return (workspace.Home.SequenceId, false);
|
||||
}
|
||||
}, cancellationToken);
|
||||
Forget(sequenceId, projectId);
|
||||
return removed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// "Send first" for a project not open in the editor (the picker's Remove local copy): its working state saved as a
|
||||
/// version when it can be, then the sync.
|
||||
/// </summary>
|
||||
/// <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);
|
||||
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.Conflict is not null) throw new WorkspaceException(Loc.L("Errors.Workspace.Conflict"));
|
||||
if (status.Ahead > 0 || (status.Modified && !status.ReadOnly)) throw new WorkspaceException(Loc.L("Errors.Workspace.Unsent"));
|
||||
}
|
||||
|
||||
/// <summary>The plan of "Remove local copy": the folder's files and size, and what would not be at the home after it.</summary>
|
||||
@@ -444,7 +508,8 @@ public sealed class WorkspaceHost
|
||||
var name = project.ReadBundle().Workspace!.Name;
|
||||
var status = await StatusAsync(projectId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
var (files, bytes) = Measure(project.Folder);
|
||||
return new WorkspaceRemovalPlan(projectId, name, files, bytes, status.Ahead, status.Modified, status.Conflict is not null, status.Offline);
|
||||
return new WorkspaceRemovalPlan(projectId, name, files, bytes, status.Ahead, status.Modified, status.Conflict is not null, status.Offline,
|
||||
status.State == WorkspaceState.Gone);
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
@@ -588,6 +653,27 @@ public sealed class WorkspaceHost
|
||||
if (status.MediaRemoved) throw new WorkspaceException(Loc.L("Errors.Workspace.MediaMissing"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The workspace folder lies inside a library this index knows (a portable copy's <c>data</c> folder under a library
|
||||
/// at a drive's root): editing happens outside every library, so nothing is checked out or copied there.
|
||||
/// </summary>
|
||||
private async Task<bool> InsideALibraryAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_db() is not { } db) return false;
|
||||
var root = Plain(Path.GetFullPath(Root));
|
||||
foreach (var archive in await db.Archives.ListAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
if (DataLocation.IsUnder(root, Plain(Path.GetFullPath(archive.RootPath)))) return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
// Without the long-path prefix, which only one of the two may carry.
|
||||
static string Plain(string path) =>
|
||||
path.StartsWith(@"\\?\UNC\", StringComparison.Ordinal) ? @"\\" + path[8..]
|
||||
: path.StartsWith(@"\\?\", StringComparison.Ordinal) ? path[4..]
|
||||
: path;
|
||||
}
|
||||
|
||||
private void Forget(Guid sequenceId, Guid projectId)
|
||||
{
|
||||
if (!_settings.WorkspaceRecent.TryGetValue(sequenceId, out var known) || known != projectId) return;
|
||||
|
||||
Reference in new issue
Block a user