feat(workspace): move the workspace folder with its projects
Settings › Workspace › Change only pointed Edit at another folder and left every project behind. Moving a workspace to a bigger or faster disk now carries its projects along, without ever putting work at risk: - a plan first: projects, files, bytes, free space at the target, whether folders are renamed (one volume) or copied (another), projects with work not at their home as a warning; refused for the folder itself or one inside it, a target holding a workspace or a same-id folder, a target inside a library, too little room, or a workspace job still pending - the WorkspaceMove job checks the plan's fingerprint again, journals the move in <from>/.move.json, renames each entry on one volume, and across volumes copies each file to a .part, hashes both sides and only then renames it into place; the source goes only after every entry is verified and the desktop switched to the target - a failure before the switch is undone (renamed entries go back, copies are removed) so the source stays the workspace; a pause or a closed program goes on from the journal; a user cancel undoes it - while a move waits, runs or its journal is in the folder in use, opens, checkouts, copies, saves, syncs and another folder are refused, and a journal left at start is offered again with Resume - the setting names the new folder only once the move verified everything Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2e1c67f531
commit
c93a4b0779
8 files changed
+1670
-37
No files matched your search
@@ -4,6 +4,7 @@ using System.Windows.Shell;
|
||||
using Chronos.App.About;
|
||||
using Chronos.App.Infrastructure;
|
||||
using Chronos.App.Sections;
|
||||
using Chronos.App.Workspace;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Ui.Controls;
|
||||
@@ -172,6 +173,8 @@ public sealed class ShellViewModel : LocalizedViewModel
|
||||
Settings.Attach();
|
||||
About.Attach();
|
||||
await Server.AttachAsync();
|
||||
// A move of the workspace folder that stopped before it ended is offered again (phase 5c).
|
||||
await WorkspaceFolderChange.NoticeUnfinishedMoveAsync(this);
|
||||
}
|
||||
|
||||
/// <summary>Opens a sequence in Edit.</summary>
|
||||
|
||||
@@ -4,6 +4,8 @@ using Chronos.App.Infrastructure;
|
||||
using Chronos.App.Shell;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Workspaces;
|
||||
|
||||
namespace Chronos.App.Workspace;
|
||||
|
||||
@@ -12,7 +14,8 @@ namespace Chronos.App.Workspace;
|
||||
/// inside a library, where Edit cannot be used until one is chosen. The folder is picked, its plan shown and confirmed
|
||||
/// (the projects the folder in use keeps, those the new one holds), a workspace project open in the editor closes (its
|
||||
/// last edit stored first), and only then does the setting change. Nothing is moved or deleted: the projects of the
|
||||
/// folder left stay there, listed again once it is chosen back. Moving a workspace with its projects comes in phase 5c.
|
||||
/// folder left stay there, listed again once it is chosen back. Settings › Workspace › Move… (phase 5c) moves the folder
|
||||
/// with its projects instead, after its plan, by a job; a move that stopped before it ended is offered again at start.
|
||||
/// </summary>
|
||||
public static class WorkspaceFolderChange
|
||||
{
|
||||
@@ -20,14 +23,8 @@ public static class WorkspaceFolderChange
|
||||
public static async Task<bool> ChooseAsync(ShellViewModel shell)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(shell);
|
||||
var changed = await ChooseAsync(shell.Services.Workspace, shell.Services.Dialogs, async () =>
|
||||
{
|
||||
if (shell.Sequence.Editor?.Session.Source is not WorkspaceSequenceSource) return true;
|
||||
if (!await shell.Sequence.ConfirmCloseAsync()) return false;
|
||||
await shell.Sequence.CloseAsync();
|
||||
shell.KeepSequence(null);
|
||||
return true;
|
||||
}, message => shell.Notify(Loc.L("Settings.Workspace.NotChanged"), message, isError: true));
|
||||
var changed = await ChooseAsync(shell.Services.Workspace, shell.Services.Dialogs, () => CloseProjectAsync(shell),
|
||||
message => shell.Notify(Loc.L("Settings.Workspace.NotChanged"), message, isError: true));
|
||||
if (changed) shell.Notify(Loc.L("Settings.Workspace.Changed"), shell.Services.Workspace.Root);
|
||||
return changed;
|
||||
}
|
||||
@@ -70,25 +67,115 @@ public static class WorkspaceFolderChange
|
||||
/// Settings › Workspace › Move… (phase 5c): the shell's flow over <see cref="MoveAsync(WorkspaceHost, IAppDialogs, Func{Task{bool}}, Action{string})"/>,
|
||||
/// with its folder picker, confirmation, editor and toasts. True when the move was queued.
|
||||
/// </summary>
|
||||
public static Task<bool> MoveAsync(ShellViewModel shell)
|
||||
public static async Task<bool> MoveAsync(ShellViewModel shell)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(shell);
|
||||
throw new NotImplementedException("Moving the workspace folder comes with phase 5c (P6).");
|
||||
var host = shell.Services.Workspace;
|
||||
var from = host.Root;
|
||||
var queued = await MoveAsync(host, shell.Services.Dialogs, () => CloseProjectAsync(shell),
|
||||
message => shell.Notify(Loc.L("Settings.Workspace.NotMoved"), message, isError: true));
|
||||
if (queued) shell.Notify(Loc.L("Settings.Workspace.Moving"), from);
|
||||
return queued;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Picks the folder the workspace moves to through <paramref name="dialogs"/>, shows its plan
|
||||
/// (<see cref="WorkspaceHost.PlanMoveAsync"/>) and asks for the user's word, lets <paramref name="closeProject"/> close a
|
||||
/// workspace project open in the editor (false keeps everything as it is), then queues the move
|
||||
/// (<see cref="WorkspaceHost.QueueMoveAsync"/>). <paramref name="failed"/> hears why it was refused. True when it was queued.
|
||||
/// (<see cref="WorkspaceHost.QueueMoveAsync"/>) of the plan read again once the project is closed (its last edit
|
||||
/// stored), unless that one refuses. <paramref name="failed"/> hears why it was refused. True when it was queued.
|
||||
/// </summary>
|
||||
public static Task<bool> MoveAsync(WorkspaceHost host, IAppDialogs dialogs, Func<Task<bool>> closeProject, Action<string> failed)
|
||||
public static async Task<bool> MoveAsync(WorkspaceHost host, IAppDialogs dialogs, Func<Task<bool>> closeProject, Action<string> failed)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(host);
|
||||
ArgumentNullException.ThrowIfNull(dialogs);
|
||||
ArgumentNullException.ThrowIfNull(closeProject);
|
||||
ArgumentNullException.ThrowIfNull(failed);
|
||||
throw new NotImplementedException("Moving the workspace folder comes with phase 5c (P6).");
|
||||
if (host.IsMoving)
|
||||
{
|
||||
failed(Loc.L("Errors.Workspace.Moving"));
|
||||
return false;
|
||||
}
|
||||
if (dialogs.PickFolder(Loc.L("Settings.Workspace.MoveTitle"), Directory.Exists(host.Root) ? Path.GetDirectoryName(host.Root) : null) is not { } folder)
|
||||
return false;
|
||||
try
|
||||
{
|
||||
var plan = await host.PlanMoveAsync(folder);
|
||||
if (plan.Refusal is { } refused)
|
||||
{
|
||||
failed(refused);
|
||||
return false;
|
||||
}
|
||||
if (!dialogs.Confirm(MoveRequest(plan))) return false;
|
||||
if (!await closeProject()) return false;
|
||||
// Closing stored the last edit: the job checks the plan of the workspace as it is now.
|
||||
plan = await host.PlanMoveAsync(folder);
|
||||
if (plan.Refusal is { } refusedNow)
|
||||
{
|
||||
failed(refusedNow);
|
||||
return false;
|
||||
}
|
||||
await host.QueueMoveAsync(plan);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or ArgumentException or WorkspaceException)
|
||||
{
|
||||
Log.Warning("workspace", $"The workspace folder was not moved to {folder}", ex);
|
||||
failed(ex is WorkspaceException ? ex.Message : Loc.L("Errors.Workspace.FolderUnusable", folder));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// At start: a move of the workspace folder that stopped before it ended (its journal in the folder in use, no move job
|
||||
/// waiting) is said, with Resume, which queues it again to go on from its journal. A move job still waiting holds the
|
||||
/// workspace until it ends.
|
||||
/// </summary>
|
||||
public static async Task NoticeUnfinishedMoveAsync(ShellViewModel shell)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(shell);
|
||||
var host = shell.Services.Workspace;
|
||||
if (!host.IsAvailable) return;
|
||||
try
|
||||
{
|
||||
if (await host.UnfinishedMoveAsync() is not { } journal) return;
|
||||
shell.Notify(Loc.L("Settings.Workspace.MoveUnfinished"), Loc.L("Settings.Workspace.MoveUnfinishedMessage", journal.To, journal.From),
|
||||
Loc.L("Settings.Workspace.ResumeMove"), Loc.L("Tooltips.Settings.Workspace.ResumeMove"), () => _ = ResumeAsync(shell, journal), isError: true);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or DatabaseException)
|
||||
{
|
||||
Log.Warning("workspace", "An unfinished move of the workspace could not be looked for", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The move's plan as a confirmation: where the projects go, how many and how big, the free bytes at the target, whether
|
||||
/// folders are renamed or files copied and checked, and how many projects hold work not at their home (they move with it).
|
||||
/// </summary>
|
||||
public static ConfirmRequest MoveRequest(WorkspaceMovePlan plan)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(plan);
|
||||
var lines = new List<string>
|
||||
{
|
||||
Loc.L("Settings.Workspace.MovePlan.Projects", Formatting.Count(plan.Projects.Count)),
|
||||
Loc.L("Settings.Workspace.MovePlan.Files", Formatting.Count(plan.Files)),
|
||||
Loc.L("Settings.Workspace.MovePlan.Size", Formatting.Bytes(plan.Bytes)),
|
||||
};
|
||||
if (plan.Free is { } free) lines.Add(Loc.L("Settings.Workspace.MovePlan.Free", Formatting.Bytes(free)));
|
||||
lines.Add(Loc.L(plan.SameVolume ? "Settings.Workspace.MovePlan.SameVolume" : "Settings.Workspace.MovePlan.OtherVolume"));
|
||||
if (plan.Warnings > 0) lines.Add(Loc.L("Settings.Workspace.MovePlan.Unsent", Formatting.Count(plan.Warnings)));
|
||||
return new ConfirmRequest
|
||||
{
|
||||
Heading = Loc.L("Settings.Workspace.MovePlan.Title"),
|
||||
Message = Loc.L("Settings.Workspace.MovePlan.Message", plan.From, plan.To),
|
||||
Detail = string.Join(Environment.NewLine, lines),
|
||||
DetailToolTip = plan.Warnings > 0 ? Loc.L("Tooltips.Settings.Workspace.MovePlan.Unsent") : null,
|
||||
IsDetailMonospaced = false,
|
||||
SafeText = Loc.L("Archive.Dialog.Cancel"),
|
||||
SafeToolTip = Loc.L("Tooltips.Settings.Workspace.MovePlan.Keep"),
|
||||
ActionText = Loc.L("Settings.Workspace.MovePlan.Action"),
|
||||
ActionToolTip = Loc.L("Tooltips.Settings.Workspace.MovePlan.Action"),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The plan as a confirmation: where Edit's projects come from now, and what stays where it is.</summary>
|
||||
@@ -108,4 +195,30 @@ public static class WorkspaceFolderChange
|
||||
ActionToolTip = Loc.L("Tooltips.Settings.UseWorkspace"),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Queues again the move a journal records, the project open in the editor closed first.</summary>
|
||||
private static async Task ResumeAsync(ShellViewModel shell, WorkspaceMoveJournal journal)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!await CloseProjectAsync(shell)) return;
|
||||
await shell.Services.Workspace.ResumeMoveAsync(journal);
|
||||
shell.Notify(Loc.L("Settings.Workspace.Moving"), journal.From);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or WorkspaceException or DatabaseException)
|
||||
{
|
||||
Log.Warning("workspace", $"The move of the workspace to {journal.To} could not be queued again", ex);
|
||||
shell.Notify(Loc.L("Settings.Workspace.NotMoved"), ex is WorkspaceException ? ex.Message : null, isError: true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A workspace project open in the editor closes (its last edit stored first); false when the user kept it open.</summary>
|
||||
private static async Task<bool> CloseProjectAsync(ShellViewModel shell)
|
||||
{
|
||||
if (shell.Sequence.Editor?.Session.Source is not WorkspaceSequenceSource) return true;
|
||||
if (!await shell.Sequence.ConfirmCloseAsync()) return false;
|
||||
await shell.Sequence.CloseAsync();
|
||||
shell.KeepSequence(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
using System.IO;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Workspaces;
|
||||
|
||||
namespace Chronos.App.Workspace;
|
||||
|
||||
@@ -18,8 +23,9 @@ public sealed record WorkspaceMoveProject(Guid ProjectId, string Name, int Files
|
||||
/// <param name="Refusal">
|
||||
/// Why the move cannot go, one localized sentence: the target is the folder itself or inside it
|
||||
/// (<c>Errors.Workspace.MoveSameFolder</c>), already holds a workspace or a project of the same id
|
||||
/// (<c>Errors.Workspace.MoveTargetNotEmpty</c>), lacks the room (<c>Errors.Workspace.DiskFull</c>), or a workspace job is
|
||||
/// pending or running (<c>Errors.Workspace.JobsRunning</c>); null when it can go.
|
||||
/// (<c>Errors.Workspace.MoveTargetNotEmpty</c>), lies inside a library (<c>Errors.Workspace.InsideLibraryFolder</c>), lacks
|
||||
/// the room (<c>Errors.Workspace.MoveNoRoom</c>), or a workspace job is pending or running (<c>Errors.Workspace.JobsRunning</c>);
|
||||
/// null when it can go.
|
||||
/// </param>
|
||||
public sealed record WorkspaceMovePlan(string From, string To, IReadOnlyList<WorkspaceMoveProject> Projects, int Files, long Bytes, long? Free, bool SameVolume,
|
||||
string? Refusal, string Fingerprint)
|
||||
@@ -33,38 +39,169 @@ public sealed record WorkspaceMovePlan(string From, string To, IReadOnlyList<Wor
|
||||
|
||||
/// <summary>
|
||||
/// Moving the workspace folder with its projects (phase 5c, P6): the plan, the <see cref="JobKind.WorkspaceMove"/> that
|
||||
/// carries it out, and the workspace that follows the folder once every file is verified there. While a move is queued or
|
||||
/// running the workspace is refused for syncs, saves and opens (<see cref="EnsureNotMoving"/>). Stubs until the move lands.
|
||||
/// carries it out, and the workspace that follows the folder once every file is verified there. While a move is queued,
|
||||
/// running or paused, and while the folder in use holds a move's journal (a move under way, or one stopped before it
|
||||
/// ended), the workspace is refused for checkouts, copies, saves, syncs, opens and another folder
|
||||
/// (<see cref="EnsureNotMoving"/>).
|
||||
/// </summary>
|
||||
public sealed partial class WorkspaceHost
|
||||
{
|
||||
/// <summary>A move of the workspace folder is queued or running: syncs, saves and opens wait for it.</summary>
|
||||
public bool IsMoving => false;
|
||||
/// <summary>The kinds of job that write into the workspace folder: none may wait or run when a move starts.</summary>
|
||||
private static readonly JobKind[] WorkspaceJobKinds = [JobKind.WorkspaceCopy, JobKind.WorkspaceRender, JobKind.WorkspaceSend, JobKind.WorkspaceMove];
|
||||
|
||||
/// <summary>The plan of moving the workspace folder to <paramref name="to"/> (<see cref="WorkspaceMovePlan"/>); nothing is written.</summary>
|
||||
/// <summary>The thread the host was made on (the UI's): the settings are written there when a move ends.</summary>
|
||||
private readonly SynchronizationContext? _moveContext = SynchronizationContext.Current;
|
||||
|
||||
/// <summary>The move job queued from here (or found waiting at start), until it ends; empty when there is none.</summary>
|
||||
private Guid _moveJob;
|
||||
|
||||
/// <summary>The engine whose job changes the host follows, to see a move end.</summary>
|
||||
private JobEngine? _followed;
|
||||
|
||||
/// <summary>
|
||||
/// A move of the workspace folder is queued, running or paused, or the folder in use holds the journal of one that has
|
||||
/// not ended: syncs, saves and opens wait for it.
|
||||
/// </summary>
|
||||
public bool IsMoving
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_moveJob != Guid.Empty) return true;
|
||||
}
|
||||
return WorkspaceMover.HasJournal(Root);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A move stopped before it ended: the folder in use holds its journal and no move job waits, runs or is paused. Null
|
||||
/// when there is none (a move under way is no move to take up). A move job waiting at start is followed from now on,
|
||||
/// so the workspace waits for it (<see cref="IsMoving"/>).
|
||||
/// </summary>
|
||||
public Task<WorkspaceMoveJournal?> UnfinishedMoveAsync(CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
if (_db() is not { } db) return null;
|
||||
foreach (var state in new[] { JobState.Running, JobState.Queued, JobState.Paused })
|
||||
{
|
||||
if ((await db.Jobs.ListAsync(state, JobKind.WorkspaceMove, 1, cancellationToken: cancellationToken).ConfigureAwait(false)).Items is [var pending, ..])
|
||||
{
|
||||
Follow(pending.Id);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return WorkspaceMover.HasJournal(Root) ? WorkspaceMover.ReadJournal(Root) : null;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// The plan of moving the workspace folder to <paramref name="to"/> (<see cref="WorkspaceMovePlan"/>); nothing is
|
||||
/// written. Each project is read here, never at its home: a move keeps whatever is not sent yet, the plan only says so.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The workspace folder cannot be read.</exception>
|
||||
public Task<WorkspaceMovePlan> PlanMoveAsync(string to, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(to);
|
||||
throw new NotImplementedException("Moving the workspace folder comes with phase 5c (P6).");
|
||||
var root = Root;
|
||||
var free = _archives()?.FreeSpace;
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
var target = Path.GetFullPath(to);
|
||||
var survey = new WorkspaceMover(root, target) { FreeSpace = free }.Survey();
|
||||
var projects = new List<WorkspaceMoveProject>();
|
||||
foreach (var folder in survey.Projects.Where(p => p.IsProject))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
projects.Add(await MoveProjectAsync(folder, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
var refusal = survey.RefusalText;
|
||||
if (refusal is null && await InsideALibraryAsync(survey.To, cancellationToken).ConfigureAwait(false))
|
||||
refusal = Loc.L("Errors.Workspace.InsideLibraryFolder", survey.To);
|
||||
if (refusal is null && (IsMoving || WorkspaceMover.HasJournal(survey.From))) refusal = Loc.L("Errors.Workspace.Moving");
|
||||
if (refusal is null && _db() is { } db && await db.Jobs.HasPendingAsync(WorkspaceJobKinds, cancellationToken: cancellationToken).ConfigureAwait(false))
|
||||
refusal = Loc.L("Errors.Workspace.JobsRunning");
|
||||
return new WorkspaceMovePlan(survey.From, survey.To, projects, survey.Files, survey.Bytes, survey.Free, survey.SameVolume, refusal, survey.Fingerprint);
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Queues the <see cref="JobKind.WorkspaceMove"/> of a plan the user confirmed.</summary>
|
||||
/// <exception cref="WorkspaceException">The plan refuses the move.</exception>
|
||||
public Task<Job> QueueMoveAsync(WorkspaceMovePlan plan, CancellationToken cancellationToken = default)
|
||||
/// <summary>
|
||||
/// Queues the <see cref="JobKind.WorkspaceMove"/> of a plan the user confirmed: from now until it ends the workspace is
|
||||
/// refused for writes and opens (<see cref="IsMoving"/>).
|
||||
/// </summary>
|
||||
/// <exception cref="WorkspaceException">The plan refuses the move, or a move is under way already.</exception>
|
||||
public async Task<Job> QueueMoveAsync(WorkspaceMovePlan plan, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(plan);
|
||||
throw new NotImplementedException("Moving the workspace folder comes with phase 5c (P6).");
|
||||
if (plan.Refusal is { } refusal) throw new WorkspaceException(refusal);
|
||||
EnsureNotMoving();
|
||||
var engine = _engine() ?? throw new WorkspaceException(Loc.L("Editor.Error.NoEngine"));
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Move(plan.From, plan.To, plan.Fingerprint), cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
Follow(job.Id);
|
||||
Log.Info("workspace", $"Move {job.Id} of the workspace {plan.From} to {plan.To} queued ({plan.Projects.Count} project(s), {plan.Bytes} bytes)");
|
||||
return job;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues again the move a journal records (<see cref="UnfinishedMoveAsync"/>): it goes on from the journal, nothing
|
||||
/// verified is copied twice.
|
||||
/// </summary>
|
||||
/// <exception cref="WorkspaceException">The index or the engine is not open.</exception>
|
||||
public async Task<Job> ResumeMoveAsync(WorkspaceMoveJournal journal, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(journal);
|
||||
var engine = _engine() ?? throw new WorkspaceException(Loc.L("Editor.Error.NoEngine"));
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Move(journal.From, journal.To, journal.Fingerprint), cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
Follow(job.Id);
|
||||
Log.Info("workspace", $"Move {job.Id} of the workspace {journal.From} to {journal.To} queued again from its journal");
|
||||
return job;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Told by the move job once every file of the workspace is verified at <paramref name="to"/>
|
||||
/// (<c>WorkspaceJobOptions.Moved</c>): Settings › Workspace names the new folder and the workspace opens there.
|
||||
/// (<c>WorkspaceJobOptions.Moved</c>, any thread): on the thread the host was made on, Settings › Workspace names the new
|
||||
/// folder (none when it is the default) and the workspace opens there. A workspace that is no longer in
|
||||
/// <paramref name="from"/> (another folder chosen meanwhile) keeps its folder.
|
||||
/// </summary>
|
||||
public Task MovedAsync(string from, string to)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(from);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(to);
|
||||
throw new NotImplementedException("Moving the workspace folder comes with phase 5c (P6).");
|
||||
if (_moveContext is not { } context) return Task.Run(() => FollowFolder(from, to));
|
||||
if (SynchronizationContext.Current == context)
|
||||
{
|
||||
FollowFolder(from, to);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
context.Post(_ =>
|
||||
{
|
||||
try
|
||||
{
|
||||
FollowFolder(from, to);
|
||||
done.TrySetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
done.TrySetException(ex);
|
||||
}
|
||||
}, null);
|
||||
return done.Task;
|
||||
}
|
||||
|
||||
/// <summary>The settings name the folder a move verified everything in.</summary>
|
||||
private void FollowFolder(string from, string to)
|
||||
{
|
||||
var target = Full(to);
|
||||
if (!string.Equals(Full(Root), Full(from), StringComparison.OrdinalIgnoreCase) && !string.Equals(Full(Root), target, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Log.Warning("workspace", $"The workspace moved from {from} to {to}, but {Root} is in use now: it stays so");
|
||||
return;
|
||||
}
|
||||
_settings.WorkspaceFolder = string.Equals(target, Full(DefaultRoot(_environment)), StringComparison.OrdinalIgnoreCase) ? null : target;
|
||||
_savePreferences();
|
||||
lock (_gate) _open = null;
|
||||
Log.Info("workspace", $"The workspace folder is now {target}, moved from {from}");
|
||||
|
||||
static string Full(string folder) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(folder));
|
||||
}
|
||||
|
||||
/// <summary>Refuses an operation that writes into the workspace while its folder is being moved.</summary>
|
||||
@@ -73,4 +210,61 @@ public sealed partial class WorkspaceHost
|
||||
{
|
||||
if (IsMoving) throw new WorkspaceException(Loc.L("Errors.Workspace.Moving"));
|
||||
}
|
||||
|
||||
/// <summary>Holds the workspace while move job <paramref name="jobId"/> waits, runs or is paused; lets go once it ends, however it ends.</summary>
|
||||
private void Follow(Guid jobId)
|
||||
{
|
||||
var engine = _engine();
|
||||
lock (_gate)
|
||||
{
|
||||
_moveJob = jobId;
|
||||
if (engine is null || ReferenceEquals(engine, _followed)) return;
|
||||
if (_followed is { } before) before.JobChanged -= OnMoveJobChanged;
|
||||
_followed = engine;
|
||||
engine.JobChanged += OnMoveJobChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMoveJobChanged(Job job)
|
||||
{
|
||||
if (job.Kind != JobKind.WorkspaceMove || job.State is not (JobState.Done or JobState.Failed or JobState.Cancelled)) return;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_moveJob == job.Id) _moveJob = Guid.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 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 renders = 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);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Info("workspace", $"Where project {folder.ProjectId} stands could not be read for the move's plan: {ex.Message}");
|
||||
return new WorkspaceMoveProject(folder.ProjectId, entry.Name, folder.Files, folder.Bytes, true, false);
|
||||
}
|
||||
|
||||
static int UnsentRenders(WorkspaceProject project)
|
||||
{
|
||||
try
|
||||
{
|
||||
return project.ReadRenders().Unsent.Count;
|
||||
}
|
||||
catch (InvalidDataException)
|
||||
{
|
||||
// A ledger this Chronos cannot read: what it lists is not known to be at the home.
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -329,13 +329,19 @@ public sealed partial class WorkspaceHost
|
||||
return (IReadOnlyList<WorkspaceEntry>)entries;
|
||||
}, 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>The workspace projects of one sequence, as Edit opens one.</summary>
|
||||
/// <exception cref="WorkspaceException">The workspace folder is being moved (<c>Errors.Workspace.Moving</c>).</exception>
|
||||
public async Task<IReadOnlyList<WorkspaceEntry>> ProjectsOfAsync(Guid sequenceId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
EnsureNotMoving();
|
||||
return [.. (await ListAsync(cancellationToken).ConfigureAwait(false)).Where(e => e.SequenceId == sequenceId)];
|
||||
}
|
||||
|
||||
/// <summary>A workspace project by id (damaged or newer when this Chronos cannot read its working state); null when the workspace has none.</summary>
|
||||
/// <exception cref="WorkspaceException">The workspace folder is being moved (<c>Errors.Workspace.Moving</c>).</exception>
|
||||
public Task<WorkspaceEntry?> FindAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(async () =>
|
||||
{
|
||||
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;
|
||||
}, cancellationToken);
|
||||
@@ -363,11 +369,12 @@ public sealed partial class WorkspaceHost
|
||||
/// Edit's workspace becomes <paramref name="folder"/> (made, with its manifest, when it is new): Settings › Workspace ›
|
||||
/// Folder names it from now on (none when it is the default). The projects of the folder left stay there, untouched.
|
||||
/// </summary>
|
||||
/// <exception cref="WorkspaceException">The folder lies inside a library (<see cref="WorkspaceException.FolderRefused"/>).</exception>
|
||||
/// <exception cref="WorkspaceException">The folder lies inside a library (<see cref="WorkspaceException.FolderRefused"/>), or the workspace is being moved.</exception>
|
||||
/// <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 async Task UseFolderAsync(string folder, CancellationToken cancellationToken = default)
|
||||
{
|
||||
EnsureNotMoving();
|
||||
var plan = await PlanFolderAsync(folder, cancellationToken).ConfigureAwait(false);
|
||||
if (plan.IsSame) return;
|
||||
if (plan.InsideLibrary) throw new WorkspaceException(Loc.L("Errors.Workspace.InsideLibraryFolder", plan.Folder)) { FolderRefused = true };
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
using System.Globalization;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Jobs;
|
||||
|
||||
@@ -8,16 +11,79 @@ namespace Chronos.Data.Workspaces;
|
||||
/// projects, after the plan the user confirmed (its fingerprint checked again): journaled in <c><from>/.move.json</c>,
|
||||
/// moved per project on one volume, copied and hash-verified on both sides across volumes, a project's source deleted only
|
||||
/// once every file of it is verified, resumable from the journal; at the end <see cref="WorkspaceJobOptions.Moved"/> is told.
|
||||
/// On failure the source stays the workspace and unverified partial copies are removed.
|
||||
/// On failure the source stays the workspace and unverified partial copies are removed (<see cref="WorkspaceMover"/>).
|
||||
/// A move that starts afresh is refused while another job of the workspace waits, runs or is paused (they write into the
|
||||
/// folder that moves). Cancelled by the user, while it runs or while it is paused or failed, what it did is undone as
|
||||
/// after a failure, unless the target already became the workspace; a pause or the program closing keeps the journal,
|
||||
/// from which the next run goes on. The checkpoint carries the progress only: the journal and the files' names say what
|
||||
/// is done (a file reaches its final name in the target only once verified).
|
||||
/// </summary>
|
||||
internal sealed class WorkspaceMoveJobHandler(ChronosDatabase db, WorkspaceJobOptions options) : IJobHandler
|
||||
internal sealed class WorkspaceMoveJobHandler(ChronosDatabase db, WorkspaceJobOptions options) : ICancelledJobHandler
|
||||
{
|
||||
/// <summary>The other jobs of the workspace, which write into the folder a move takes away.</summary>
|
||||
private static readonly JobKind[] WorkspaceKinds = [JobKind.WorkspaceCopy, JobKind.WorkspaceRender, JobKind.WorkspaceSend];
|
||||
|
||||
public JobKind Kind => JobKind.WorkspaceMove;
|
||||
|
||||
public Task RunAsync(JobContext context)
|
||||
/// <summary>Tests: the mover the job runs (one volume or not, a crash, a damaged copy); null makes the real one.</summary>
|
||||
internal Func<WorkspaceMoveParameters, WorkspaceMover>? Movers { get; init; }
|
||||
|
||||
public async Task RunAsync(JobContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
_ = (db, options);
|
||||
throw new NotImplementedException("Moving the workspace folder comes with phase 5c (P6).");
|
||||
var ct = context.Cancellation;
|
||||
var parameters = WorkspaceJobs.MoveParameters(context.Job) is { From.Length: > 0, To.Length: > 0, PlanFingerprint.Length: > 0 } read
|
||||
? read
|
||||
: throw new InvalidDataException(Loc.L("Errors.Workspace.JobParameters"));
|
||||
var mover = Mover(parameters);
|
||||
if (WorkspaceMover.ReadJournal(mover.From) is null && await db.Jobs.HasPendingAsync(WorkspaceKinds, cancellationToken: ct).ConfigureAwait(false))
|
||||
throw new InvalidOperationException(Loc.L("Errors.Workspace.JobsRunning"));
|
||||
try
|
||||
{
|
||||
await mover.MoveAsync(parameters.PlanFingerprint, options.Moved, (done, total, durable) => durable
|
||||
? context.CheckpointAsync(string.Create(CultureInfo.InvariantCulture, $"move:{done}"), done, total)
|
||||
: context.ReportAsync(done, total),
|
||||
context.Log, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
// Stopped: the user's cancel undoes what was done; a pause, a yield or the program closing goes on from the journal.
|
||||
if (await db.Jobs.GetAsync(context.Job.Id, CancellationToken.None).ConfigureAwait(false) is { State: JobState.Cancelled }) Undo(mover, context.Job);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A move cancelled while no handler ran it (paused, failed): what it did is undone, unless the target is the workspace already.</summary>
|
||||
public Task CancelledAsync(Job job, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(job);
|
||||
if (WorkspaceJobs.MoveParameters(job) is not { From.Length: > 0, To.Length: > 0 } parameters) return Task.CompletedTask;
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Undo(Mover(parameters), job);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
// Nobody waits for this task: the journal stays, and the move can be taken up.
|
||||
Log.Warning("workspace", $"Cancelled move {job.Id} could not be undone", ex);
|
||||
}
|
||||
}, CancellationToken.None);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private WorkspaceMover Mover(WorkspaceMoveParameters parameters) => Movers?.Invoke(parameters) ?? new WorkspaceMover(parameters.From, parameters.To);
|
||||
|
||||
private static void Undo(WorkspaceMover mover, Job job)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!mover.Abandon()) Log.Info("workspace", $"Move {job.Id} was cancelled after {mover.To} became the workspace; it is not undone");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("workspace", $"Cancelled move {job.Id} could not be undone; its journal stays to take it up", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,634 @@
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Core.Localization;
|
||||
|
||||
namespace Chronos.Data.Workspaces;
|
||||
|
||||
/// <summary>Why the workspace folder cannot move to a folder (<see cref="WorkspaceMoveSurvey.Refusal"/>).</summary>
|
||||
public enum WorkspaceMoveRefusal
|
||||
{
|
||||
/// <summary>The target is the workspace folder itself or lies inside it (<c>Errors.Workspace.MoveSameFolder</c>).</summary>
|
||||
SameFolder,
|
||||
|
||||
/// <summary>The target holds a workspace (its manifest, its trash, a move's journal) or a folder of the same name as one moved (<c>Errors.Workspace.MoveTargetNotEmpty</c>).</summary>
|
||||
TargetNotEmpty,
|
||||
|
||||
/// <summary>The target's drive lacks the room for a copy across drives (<c>Errors.Workspace.MoveNoRoom</c>).</summary>
|
||||
NoRoom,
|
||||
}
|
||||
|
||||
/// <summary>A folder of a project the workspace holds, as a move surveys it.</summary>
|
||||
/// <param name="IsProject">It holds a working state (<c>project.chronos</c>); a folder that does not (a leftover) moves all the same.</param>
|
||||
public sealed record WorkspaceMoveFolder(Guid ProjectId, bool IsProject, int Files, long Bytes);
|
||||
|
||||
/// <summary>
|
||||
/// What moving a workspace folder <see cref="From"/> to <see cref="To"/> would take, read without writing anything: the
|
||||
/// entries that move (every folder named by a project id, the manifest, the trash), their files and bytes (leases left
|
||||
/// out), the target drive's free bytes, whether both lie on one volume, why it is refused, and a fingerprint of the files
|
||||
/// read (their relative names, sizes and times), which the move checks again before it starts.
|
||||
/// </summary>
|
||||
/// <param name="Free">The target drive's free bytes; null when they cannot be measured (a share), which is not refused.</param>
|
||||
/// <param name="Missing">With <see cref="WorkspaceMoveRefusal.NoRoom"/>, the bytes the target's drive lacks; 0 otherwise.</param>
|
||||
public sealed record WorkspaceMoveSurvey(string From, string To, IReadOnlyList<WorkspaceMoveFolder> Projects, IReadOnlyList<string> Entries, int Files, long Bytes,
|
||||
long? Free, bool SameVolume, WorkspaceMoveRefusal? Refusal, long Missing, string Fingerprint)
|
||||
{
|
||||
public bool CanMove => Refusal is null;
|
||||
|
||||
/// <summary>The refusal as one localized sentence; null when the move can go.</summary>
|
||||
public string? RefusalText => Refusal switch
|
||||
{
|
||||
WorkspaceMoveRefusal.SameFolder => Loc.L("Errors.Workspace.MoveSameFolder", To),
|
||||
WorkspaceMoveRefusal.TargetNotEmpty => Loc.L("Errors.Workspace.MoveTargetNotEmpty", To),
|
||||
WorkspaceMoveRefusal.NoRoom => Loc.L("Errors.Workspace.MoveNoRoom", (Missing / 1e9).ToString("0.0", Loc.Culture)),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>How far a move of the workspace folder went, as its journal records it.</summary>
|
||||
public enum WorkspaceMoveStage
|
||||
{
|
||||
/// <summary>The entries are being moved (one volume) or copied and verified (across volumes); the source is the workspace.</summary>
|
||||
Moving,
|
||||
|
||||
/// <summary>Every entry is verified at the target and the desktop was told: the target is the workspace; what is left of the source goes.</summary>
|
||||
Switched,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The journal of a move of the workspace folder (<c><from>/.move.json</c>, <see cref="WorkspaceMover.JournalFile"/>):
|
||||
/// where it goes, the plan's fingerprint, whether it renames folders on one volume, the entries it moves in their order,
|
||||
/// those copied and verified at the target already (across volumes), and how far it went. Written whole and atomically
|
||||
/// before every step that depends on it, so a move stopped at any moment is taken up, or undone, from it.
|
||||
/// </summary>
|
||||
public sealed record WorkspaceMoveJournal(string Format, int FormatVersion, string From, string To, string Fingerprint, bool SameVolume,
|
||||
IReadOnlyList<string> Entries, IReadOnlyList<string> Verified, WorkspaceMoveStage Stage, DateTimeOffset Started)
|
||||
{
|
||||
public const string FormatName = "chronos-workspace-move";
|
||||
public const int CurrentFormatVersion = 1;
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
UseStringEnumConverter = true, WriteIndented = true)]
|
||||
[JsonSerializable(typeof(WorkspaceMoveJournal))]
|
||||
internal sealed partial class WorkspaceMoveJson : JsonSerializerContext;
|
||||
|
||||
/// <summary>
|
||||
/// Moves a workspace folder with its projects (phase 5c, <see cref="Core.Model.JobKind.WorkspaceMove"/>). What moves: every
|
||||
/// folder named by a project id, then <c>workspace.json</c>, then <c>.trash</c>, in that order; anything else in the folder
|
||||
/// stays where it is. On one volume each entry is renamed into the target (<see cref="Directory.Move(string, string)"/>);
|
||||
/// across volumes each file is copied into a <c>.part</c> beside its place, hashed as it is read and read back to hash the
|
||||
/// copy, and only once both SHA-256 agree renamed into place (with the source's time), so a file at its final name in the
|
||||
/// target is always one verified. The source is the workspace until every entry is at the target: then the desktop is told
|
||||
/// (the switch), and only after it does the source of a copy go. The journal (<see cref="WorkspaceMoveJournal"/>) is
|
||||
/// written in the source before the first step and at every entry: a move stopped (paused, the program closed, the PC
|
||||
/// down) goes on from it, trusting the files at their final names and the entries it records as verified; a move that
|
||||
/// fails before the switch is undone (<see cref="Abandon"/>): renamed entries go back, copies at the target are removed,
|
||||
/// the journal goes.
|
||||
/// </summary>
|
||||
public sealed class WorkspaceMover
|
||||
{
|
||||
/// <summary>The journal of a move under way, in the workspace folder it leaves.</summary>
|
||||
public const string JournalFile = ".move.json";
|
||||
|
||||
/// <summary>Where a file copied across volumes is written until its hash is checked.</summary>
|
||||
internal const string PartSuffix = ".part";
|
||||
|
||||
private const int BufferSize = 1 << 20;
|
||||
private const int DeleteAttempts = 5;
|
||||
|
||||
/// <param name="from">The workspace folder in use.</param>
|
||||
/// <param name="to">The folder it moves to (made when it is not there).</param>
|
||||
public WorkspaceMover(string from, string to)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(from);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(to);
|
||||
From = WorkspaceStore.FullRoot(from);
|
||||
To = WorkspaceStore.FullRoot(to);
|
||||
}
|
||||
|
||||
public string From { get; }
|
||||
|
||||
public string To { get; }
|
||||
|
||||
/// <summary>Free bytes on the volume of a folder (the host's measure); null takes the drive's (<see cref="DriveInfo"/>).</summary>
|
||||
public Func<string, long?>? FreeSpace { get; init; }
|
||||
|
||||
/// <summary>Whether both folders lie on one volume; null decides from their drives (tests set it to try the copy on one disk).</summary>
|
||||
public bool? SameVolume { get; init; }
|
||||
|
||||
/// <summary>Bytes a copy across volumes leaves free on the target's drive besides what it copies (as a copy into the workspace keeps).</summary>
|
||||
public long Reserve { get; init; } = WorkspaceCopyJobHandler.SpaceReserve;
|
||||
|
||||
/// <summary>
|
||||
/// Called at named steps with the path concerned (tests: a crash, a file damaged on its way): <c>verify</c> (a copy's
|
||||
/// <c>.part</c>, written and about to be read back), <c>copied</c> (a file at its final name), <c>moved</c> (an entry
|
||||
/// renamed into the target), <c>switched</c> (the desktop told).
|
||||
/// </summary>
|
||||
internal Action<string, string>? Cut { get; init; }
|
||||
|
||||
/// <summary>The journal of a move under way out of <paramref name="root"/>; null when there is none, or it cannot be read (logged).</summary>
|
||||
public static WorkspaceMoveJournal? ReadJournal(string root)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(root);
|
||||
var path = Path.Combine(WorkspaceStore.FullRoot(root), JournalFile);
|
||||
if (!File.Exists(path)) return null;
|
||||
try
|
||||
{
|
||||
if (JsonSerializer.Deserialize(AtomicFile.ReadAllText(path), WorkspaceMoveJson.Default.WorkspaceMoveJournal) is
|
||||
{ Format: WorkspaceMoveJournal.FormatName, From.Length: > 0, To.Length: > 0, Entries: not null, Verified: not null } journal
|
||||
&& journal.FormatVersion <= WorkspaceMoveJournal.CurrentFormatVersion)
|
||||
return journal;
|
||||
Log.Warning("workspace", $"{path} is not a journal of a move this Chronos reads");
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("workspace", $"{path} cannot be read", ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when <paramref name="root"/> holds a move's journal, readable or not: the folder is being moved, or a move of it
|
||||
/// stopped before it ended, and nothing may write there until it is taken up or undone.
|
||||
/// </summary>
|
||||
public static bool HasJournal(string root) => File.Exists(Path.Combine(WorkspaceStore.FullRoot(root), JournalFile));
|
||||
|
||||
private string JournalPath => Path.Combine(From, JournalFile);
|
||||
|
||||
/// <summary>What the move would take and whether it can go (<see cref="WorkspaceMoveSurvey"/>); nothing is written.</summary>
|
||||
/// <exception cref="IOException">The workspace folder cannot be read.</exception>
|
||||
public WorkspaceMoveSurvey Survey()
|
||||
{
|
||||
var comparison = PathComparison;
|
||||
var projects = new List<WorkspaceMoveFolder>();
|
||||
var entries = new List<string>();
|
||||
var (files, bytes) = (0, 0L);
|
||||
var fingerprint = new StringBuilder();
|
||||
fingerprint.Append(From).Append('\n').Append(To).Append('\n');
|
||||
if (Directory.Exists(From))
|
||||
{
|
||||
foreach (var directory in Directory.EnumerateDirectories(From).Order(StringComparer.Ordinal))
|
||||
{
|
||||
var name = Path.GetFileName(directory);
|
||||
if (!Guid.TryParseExact(name, "N", out var id)) continue;
|
||||
var (count, size) = Read(directory, name, fingerprint);
|
||||
projects.Add(new WorkspaceMoveFolder(id, File.Exists(Path.Combine(directory, WorkspaceProject.BundleFile)), count, size));
|
||||
entries.Add(name);
|
||||
(files, bytes) = (files + count, bytes + size);
|
||||
}
|
||||
var manifest = Path.Combine(From, WorkspaceStore.ManifestFile);
|
||||
if (File.Exists(manifest))
|
||||
{
|
||||
var info = new FileInfo(manifest);
|
||||
entries.Add(WorkspaceStore.ManifestFile);
|
||||
Line(fingerprint, WorkspaceStore.ManifestFile, info);
|
||||
(files, bytes) = (files + 1, bytes + info.Length);
|
||||
}
|
||||
var trash = Path.Combine(From, TrashFolder);
|
||||
if (Directory.Exists(trash))
|
||||
{
|
||||
var (count, size) = Read(trash, TrashFolder, fingerprint);
|
||||
entries.Add(TrashFolder);
|
||||
(files, bytes) = (files + count, bytes + size);
|
||||
}
|
||||
}
|
||||
var same = SameVolume ?? OnOneVolume(From, To);
|
||||
var free = FreeBytes(To);
|
||||
WorkspaceMoveRefusal? refusal = null;
|
||||
long missing = 0;
|
||||
if (string.Equals(From, To, comparison) || To.StartsWith(From + Path.DirectorySeparatorChar, comparison))
|
||||
{
|
||||
refusal = WorkspaceMoveRefusal.SameFolder;
|
||||
}
|
||||
else if (File.Exists(To) || TargetHolds(entries))
|
||||
{
|
||||
refusal = WorkspaceMoveRefusal.TargetNotEmpty;
|
||||
}
|
||||
else if (!same && free is { } room && room < bytes + Reserve)
|
||||
{
|
||||
refusal = WorkspaceMoveRefusal.NoRoom;
|
||||
missing = bytes + Reserve - room;
|
||||
}
|
||||
return new WorkspaceMoveSurvey(From, To, projects, entries, files, bytes, free, same, refusal, missing,
|
||||
Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(fingerprint.ToString()))));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves the workspace, or takes up the move its journal records: the plan of <paramref name="fingerprint"/> is checked
|
||||
/// again first (a move taken up has checked it), every entry is moved or copied and verified, <paramref name="switched"/>
|
||||
/// is told (from, to) once all are at the target, then what is left of the source goes and the journal with it.
|
||||
/// <paramref name="progress"/> hears the files done and their total, with true where the job should keep it as its
|
||||
/// checkpoint; <paramref name="log"/> the lines of the job's log. A failure before the switch undoes what was done
|
||||
/// (<see cref="Abandon"/>) and is thrown; a cancel (a pause, the program closing) leaves the journal for the next run.
|
||||
/// </summary>
|
||||
/// <returns>The survey of the move as it began, and whether it was taken up from a journal.</returns>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// The workspace changed since its plan (<c>Errors.Workspace.MoveChanged</c>), the move is refused now (the target filled,
|
||||
/// the room went), another move's journal is there (<c>Errors.Workspace.Moving</c>), or a copy did not match its source
|
||||
/// (<c>Errors.Workspace.MoveVerify</c>).
|
||||
/// </exception>
|
||||
/// <exception cref="IOException">A file or folder could not be read, written or moved.</exception>
|
||||
public async Task<(WorkspaceMoveSurvey Survey, bool Resumed)> MoveAsync(string fingerprint, Func<string, string, Task>? switched = null,
|
||||
Func<long, long, bool, ValueTask>? progress = null, Action<string>? log = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(fingerprint);
|
||||
var survey = Survey();
|
||||
var journal = ReadJournal(From);
|
||||
var resumed = journal is not null;
|
||||
if (journal is null)
|
||||
{
|
||||
if (HasJournal(From)) throw new InvalidOperationException(Loc.L("Errors.Workspace.Moving"));
|
||||
if (!string.Equals(survey.Fingerprint, fingerprint, StringComparison.Ordinal)) throw new InvalidOperationException(Loc.L("Errors.Workspace.MoveChanged"));
|
||||
if (survey.RefusalText is { } refused) throw new InvalidOperationException(refused);
|
||||
journal = new WorkspaceMoveJournal(WorkspaceMoveJournal.FormatName, WorkspaceMoveJournal.CurrentFormatVersion, From, To, fingerprint, survey.SameVolume,
|
||||
survey.Entries, [], WorkspaceMoveStage.Moving, DateTimeOffset.UtcNow);
|
||||
Directory.CreateDirectory(To);
|
||||
WriteJournal(journal);
|
||||
Log.Info("workspace", $"Moving the workspace {From} to {To} ({survey.Entries.Count} entries, {survey.Files} files, {survey.Bytes} bytes, "
|
||||
+ (survey.SameVolume ? "renamed" : "copied") + ")");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!string.Equals(journal.To, To, PathComparison) || !string.Equals(journal.From, From, PathComparison))
|
||||
throw new InvalidOperationException(Loc.L("Errors.Workspace.Moving"));
|
||||
log?.Invoke(Loc.L("Archive.Log.WorkspaceMoveResumed", JournalPath));
|
||||
Log.Info("workspace", $"The move of the workspace {From} to {To} is taken up from its journal ({journal.Stage})");
|
||||
}
|
||||
|
||||
if (journal.Stage == WorkspaceMoveStage.Moving)
|
||||
{
|
||||
try
|
||||
{
|
||||
journal = journal.SameVolume
|
||||
? await RenameAsync(journal, progress, log, cancellationToken).ConfigureAwait(false)
|
||||
: await CopyAsync(journal, survey, progress, log, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
Log.Warning("workspace", $"The move of the workspace {From} to {To} failed; it is undone", ex);
|
||||
TryAbandon();
|
||||
throw;
|
||||
}
|
||||
journal = journal with { Stage = WorkspaceMoveStage.Switched };
|
||||
WriteJournal(journal);
|
||||
}
|
||||
// From here on the target is the workspace: told again when a run taken up after the switch did not see its answer.
|
||||
if (switched is not null) await switched(From, To).ConfigureAwait(false);
|
||||
Cut?.Invoke("switched", To);
|
||||
Clean(journal);
|
||||
log?.Invoke(Loc.L("Archive.Log.WorkspaceMoved", To, journal.Entries.Count(e => Guid.TryParseExact(e, "N", out _)), survey.Files));
|
||||
Log.Info("workspace", $"The workspace moved from {From} to {To}");
|
||||
return (survey, resumed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Undoes a move its journal records as not switched yet, so the source stays the workspace: on one volume the entries
|
||||
/// renamed into the target go back; across volumes the copies it made at the target are removed (what cannot be removed
|
||||
/// is logged: the source is whole). The journal then goes. A move that switched is not undone (false): the target is the
|
||||
/// workspace, and the move is taken up to finish clearing the source. True when there was nothing left to undo, or it was.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">An entry renamed into the target could not go back; the journal stays, and the move can be taken up.</exception>
|
||||
public bool Abandon()
|
||||
{
|
||||
var journal = ReadJournal(From);
|
||||
if (journal is null) return true;
|
||||
if (journal.Stage != WorkspaceMoveStage.Moving) return false;
|
||||
foreach (var entry in journal.Entries.Reverse())
|
||||
{
|
||||
var source = Path.Combine(From, entry);
|
||||
var target = Path.Combine(To, entry);
|
||||
if (journal.SameVolume)
|
||||
{
|
||||
if (Exists(target) && !Exists(source)) Rename(target, source);
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{
|
||||
// Refused when the target held the entry: whatever is there is this move's copy.
|
||||
if (Directory.Exists(target)) Directory.Delete(target, recursive: true);
|
||||
else if (File.Exists(target)) File.Delete(target);
|
||||
var part = target + PartSuffix;
|
||||
if (File.Exists(part)) File.Delete(part);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("workspace", $"The copy {target} of an undone move could not be removed", ex);
|
||||
}
|
||||
}
|
||||
File.Delete(JournalPath);
|
||||
TryDeleteEmpty(To);
|
||||
Log.Info("workspace", $"The move of the workspace {From} to {To} was undone; the workspace stays in {From}");
|
||||
return true;
|
||||
}
|
||||
|
||||
private void TryAbandon()
|
||||
{
|
||||
try
|
||||
{
|
||||
Abandon();
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("workspace", $"The move of the workspace {From} to {To} could not be undone; its journal stays to take it up", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One volume: each entry renamed into the target, an entry already there (a run that stopped after it) taken as moved.</summary>
|
||||
private async Task<WorkspaceMoveJournal> RenameAsync(WorkspaceMoveJournal journal, Func<long, long, bool, ValueTask>? progress, Action<string>? log,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var moved = 0;
|
||||
foreach (var entry in journal.Entries)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var source = Path.Combine(From, entry);
|
||||
var target = Path.Combine(To, entry);
|
||||
if (Exists(source))
|
||||
{
|
||||
if (Exists(target)) throw new IOException(Loc.L("Errors.Workspace.MoveTargetNotEmpty", To));
|
||||
Rename(source, target);
|
||||
Cut?.Invoke("moved", target);
|
||||
if (Guid.TryParseExact(entry, "N", out _)) log?.Invoke(Loc.L("Archive.Log.WorkspaceMoveProject", Name(target)));
|
||||
}
|
||||
else if (!Exists(target))
|
||||
{
|
||||
// Gone from both sides: nothing to move (the trash emptied meanwhile cannot happen while the move holds the workspace).
|
||||
Log.Warning("workspace", $"{entry} of the workspace {From} is neither there nor at {To}");
|
||||
}
|
||||
moved++;
|
||||
if (progress is not null) await progress(moved, journal.Entries.Count, true).ConfigureAwait(false);
|
||||
}
|
||||
return journal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Across volumes: each entry's files copied and verified (<see cref="CopyFileAsync"/>), the entry recorded as verified in
|
||||
/// the journal once all of its files are; nothing of the source is touched.
|
||||
/// </summary>
|
||||
private async Task<WorkspaceMoveJournal> CopyAsync(WorkspaceMoveJournal journal, WorkspaceMoveSurvey survey, Func<long, long, bool, ValueTask>? progress,
|
||||
Action<string>? log, CancellationToken cancellationToken)
|
||||
{
|
||||
var total = (long)survey.Files;
|
||||
long done = 0;
|
||||
var clock = System.Diagnostics.Stopwatch.StartNew();
|
||||
foreach (var entry in journal.Entries)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var source = Path.Combine(From, entry);
|
||||
var target = Path.Combine(To, entry);
|
||||
if (journal.Verified.Contains(entry, StringComparer.Ordinal))
|
||||
{
|
||||
done += Directory.Exists(source) ? Listed(source).Count : File.Exists(source) ? 1 : 0;
|
||||
continue;
|
||||
}
|
||||
if (File.Exists(source))
|
||||
{
|
||||
await CopyFileAsync(source, target, cancellationToken).ConfigureAwait(false);
|
||||
done++;
|
||||
}
|
||||
else if (Directory.Exists(source))
|
||||
{
|
||||
Directory.CreateDirectory(target);
|
||||
foreach (var folder in new DirectoryInfo(source).EnumerateDirectories("*", SearchOption.AllDirectories))
|
||||
Directory.CreateDirectory(Path.Combine(target, Path.GetRelativePath(source, folder.FullName)));
|
||||
foreach (var relative in Listed(source))
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await CopyFileAsync(Path.Combine(source, relative), Path.Combine(target, relative), cancellationToken).ConfigureAwait(false);
|
||||
done++;
|
||||
var durable = clock.Elapsed >= CheckpointEvery;
|
||||
if (durable) clock.Restart();
|
||||
if (progress is not null) await progress(done, total, durable).ConfigureAwait(false);
|
||||
}
|
||||
if (Guid.TryParseExact(entry, "N", out _)) log?.Invoke(Loc.L("Archive.Log.WorkspaceMoveProject", Name(source)));
|
||||
}
|
||||
journal = journal with { Verified = [.. journal.Verified, entry] };
|
||||
WriteJournal(journal);
|
||||
if (progress is not null) await progress(done, total, true).ConfigureAwait(false);
|
||||
}
|
||||
return journal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One file across volumes: a file at its final name with the source's size was verified by an earlier run and stays;
|
||||
/// otherwise the source is streamed into <c><target>.part</c> and hashed as it is read, the copy is read back and
|
||||
/// hashed, and only when both agree is it renamed into place, with the source's time.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">The copy does not match its source (<c>Errors.Workspace.MoveVerify</c>); the <c>.part</c> is removed.</exception>
|
||||
private async Task CopyFileAsync(string source, string target, CancellationToken cancellationToken)
|
||||
{
|
||||
var info = new FileInfo(source);
|
||||
if (File.Exists(target) && new FileInfo(target).Length == info.Length) return;
|
||||
var part = target + PartSuffix;
|
||||
try
|
||||
{
|
||||
string sha;
|
||||
using (var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
|
||||
{
|
||||
var buffer = new byte[BufferSize];
|
||||
await using (var input = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan))
|
||||
await using (var output = new FileStream(part, FileMode.Create, FileAccess.Write, FileShare.None, BufferSize, FileOptions.Asynchronous))
|
||||
{
|
||||
int read;
|
||||
while ((read = await input.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
|
||||
{
|
||||
hash.AppendData(buffer, 0, read);
|
||||
await output.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
await output.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
output.Flush(flushToDisk: true);
|
||||
}
|
||||
sha = Convert.ToHexStringLower(hash.GetHashAndReset());
|
||||
}
|
||||
Cut?.Invoke("verify", part);
|
||||
if (!string.Equals(await Sha256Async(part, cancellationToken).ConfigureAwait(false), sha, StringComparison.Ordinal))
|
||||
throw new InvalidOperationException(Loc.L("Errors.Workspace.MoveVerify", LongPath.ToPortableRelative(From, source)));
|
||||
File.Move(part, target, overwrite: true);
|
||||
File.SetLastWriteTimeUtc(target, info.LastWriteTimeUtc);
|
||||
Cut?.Invoke("copied", target);
|
||||
}
|
||||
catch
|
||||
{
|
||||
TryDelete(part);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// After the switch: across volumes every entry of the source goes (each one verified at the target), folders first, the
|
||||
/// manifest last; then the journal, and the source folder when nothing else is left in it.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">An entry of the source could not be removed; the journal stays, and the move taken up removes it.</exception>
|
||||
private void Clean(WorkspaceMoveJournal journal)
|
||||
{
|
||||
if (!journal.SameVolume)
|
||||
{
|
||||
foreach (var entry in journal.Entries.OrderBy(e => e == WorkspaceStore.ManifestFile ? 1 : 0))
|
||||
{
|
||||
var source = Path.Combine(From, entry);
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Directory.Exists(source)) Directory.Delete(source, recursive: true);
|
||||
else if (File.Exists(source)) File.Delete(source);
|
||||
break;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException && attempt < DeleteAttempts)
|
||||
{
|
||||
// An indexer or an antivirus reading a file a moment: tried again shortly.
|
||||
Thread.Sleep(100 * attempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File.Delete(JournalPath);
|
||||
TryDeleteEmpty(From);
|
||||
}
|
||||
|
||||
/// <summary>A folder's files relative to it, in ordinal order, the leases left out (they belong to whoever holds the project there).</summary>
|
||||
private static List<string> Listed(string folder) =>
|
||||
[.. new DirectoryInfo(folder).EnumerateFiles("*", SearchOption.AllDirectories).Where(f => !WorkspaceProject.IsLeaseFile(f.Name))
|
||||
.Select(f => Path.GetRelativePath(folder, f.FullName)).Order(StringComparer.Ordinal)];
|
||||
|
||||
/// <summary>Files and bytes of an entry, each file's name, size and time added to the fingerprint.</summary>
|
||||
private static (int Files, long Bytes) Read(string folder, string name, StringBuilder fingerprint)
|
||||
{
|
||||
var (files, bytes) = (0, 0L);
|
||||
foreach (var relative in Listed(folder))
|
||||
{
|
||||
var info = new FileInfo(Path.Combine(folder, relative));
|
||||
Line(fingerprint, name + "/" + relative.Replace('\\', '/'), info);
|
||||
files++;
|
||||
bytes += info.Length;
|
||||
}
|
||||
return (files, bytes);
|
||||
}
|
||||
|
||||
private static void Line(StringBuilder fingerprint, string name, FileInfo info) =>
|
||||
fingerprint.Append(name).Append('|').Append(info.Length.ToString(CultureInfo.InvariantCulture)).Append('|')
|
||||
.Append(info.LastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)).Append('\n');
|
||||
|
||||
/// <summary>The target holds a workspace's own files, or a folder or file named as one of the entries that move.</summary>
|
||||
private bool TargetHolds(IEnumerable<string> entries) =>
|
||||
Directory.Exists(To) && (Exists(Path.Combine(To, WorkspaceStore.ManifestFile)) || Exists(Path.Combine(To, TrashFolder)) || Exists(Path.Combine(To, JournalFile))
|
||||
|| entries.Any(e => Exists(Path.Combine(To, e))));
|
||||
|
||||
private const string TrashFolder = ".trash";
|
||||
|
||||
private static readonly TimeSpan CheckpointEvery = TimeSpan.FromSeconds(5);
|
||||
|
||||
private static StringComparison PathComparison => OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
|
||||
|
||||
private static bool Exists(string path) => Directory.Exists(path) || File.Exists(path);
|
||||
|
||||
private static void Rename(string source, string target)
|
||||
{
|
||||
if (Directory.Exists(source)) Directory.Move(source, target);
|
||||
else File.Move(source, target);
|
||||
}
|
||||
|
||||
/// <summary>The name a project's working state gives it, for the log; its folder's name when it cannot be read.</summary>
|
||||
private static string Name(string folder)
|
||||
{
|
||||
var bundle = Path.Combine(folder, WorkspaceProject.BundleFile);
|
||||
try
|
||||
{
|
||||
if (File.Exists(bundle) && Core.Project.ProjectBundle.Load(bundle).Workspace?.Name is { Length: > 0 } name) return name;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException or JsonException)
|
||||
{
|
||||
Log.Debug("workspace", $"{bundle} names no project: {ex.Message}");
|
||||
}
|
||||
return Path.GetFileName(folder);
|
||||
}
|
||||
|
||||
private void WriteJournal(WorkspaceMoveJournal journal) =>
|
||||
AtomicFile.WriteAllText(JournalPath, JsonSerializer.Serialize(journal, WorkspaceMoveJson.Default.WorkspaceMoveJournal));
|
||||
|
||||
/// <summary>Free bytes on the volume of <paramref name="folder"/> (its nearest folder that exists); null when they cannot be measured.</summary>
|
||||
private long? FreeBytes(string folder)
|
||||
{
|
||||
var existing = Nearest(folder);
|
||||
if (existing is null) return null;
|
||||
if (FreeSpace is { } measure) return measure(existing);
|
||||
try
|
||||
{
|
||||
// Windows wants a drive root; Unix accepts any directory and reports the volume it is mounted on.
|
||||
return new DriveInfo(OperatingSystem.IsWindows() ? Path.GetPathRoot(existing)! : existing).AvailableFreeSpace;
|
||||
}
|
||||
catch (Exception ex) when (ex is ArgumentException or IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Both folders on one volume: on Windows the same drive; on Unix the same mount point. A doubt reads as two volumes,
|
||||
/// whose copy is the safe way (a rename across volumes would fail).
|
||||
/// </summary>
|
||||
private static bool OnOneVolume(string from, string to)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Nearest(to) is not { } target) return false;
|
||||
if (OperatingSystem.IsWindows())
|
||||
return string.Equals(Path.GetPathRoot(from), Path.GetPathRoot(target), StringComparison.OrdinalIgnoreCase)
|
||||
&& !Path.GetPathRoot(from)!.StartsWith(@"\\", StringComparison.Ordinal);
|
||||
var mounts = DriveInfo.GetDrives().Select(d => Path.TrimEndingDirectorySeparator(d.RootDirectory.FullName)).ToList();
|
||||
return string.Equals(Mount(from, mounts), Mount(target, mounts), StringComparison.Ordinal);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static string? Mount(string path, List<string> mounts) =>
|
||||
mounts.Where(m => path == m || path.StartsWith(m.EndsWith('/') ? m : m + "/", StringComparison.Ordinal)).MaxBy(m => m.Length);
|
||||
}
|
||||
|
||||
/// <summary><paramref name="folder"/> or its nearest parent that exists; null when none does.</summary>
|
||||
private static string? Nearest(string folder)
|
||||
{
|
||||
for (var current = folder; !string.IsNullOrEmpty(current); current = Path.GetDirectoryName(current))
|
||||
{
|
||||
if (Directory.Exists(current)) return current;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static async Task<string> Sha256Async(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
return Convert.ToHexStringLower(await SHA256.HashDataAsync(stream, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
private static void TryDelete(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Debug("workspace", $"{path} stays: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryDeleteEmpty(string folder)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Directory.Exists(folder) && !Directory.EnumerateFileSystemEntries(folder).Any()) Directory.Delete(folder);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Debug("workspace", $"{folder} stays: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using Chronos.App.Infrastructure;
|
||||
using Chronos.App.Workspace;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.App.Tests.Workspace;
|
||||
|
||||
/// <summary>
|
||||
/// Settings › Workspace › Move… (phase 5c, P6) as the desktop runs it: the plan and its confirmation, the open project
|
||||
/// closed first, the workspace held while the move waits or runs, the setting switched only once the move verified
|
||||
/// everything, a failed move that leaves both as they were, and a move stopped before it ended taken up again.
|
||||
/// </summary>
|
||||
[Category("workspace")]
|
||||
public sealed class WorkspaceMoveTests(TestContext context)
|
||||
{
|
||||
static WorkspaceMoveTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
||||
|
||||
[Test]
|
||||
public async Task TheWorkspaceWaitsForTheMoveAndFollowsTheFolderOnlyOnceItEnded()
|
||||
{
|
||||
await using var bed = await WorkspaceTestBed.CreateAsync(context, frames: 4);
|
||||
var settings = new AppSettings();
|
||||
var mainId = await bed.CheckoutAsync(bed.Host(settings));
|
||||
await using var moves = await MovesAsync(bed, settings);
|
||||
var host = moves.Host;
|
||||
var from = host.Root;
|
||||
var target = Path.Combine(bed.Directory, "moved workspace");
|
||||
|
||||
var plan = await host.PlanMoveAsync(target);
|
||||
Assert.True(plan.CanMove, plan.Refusal);
|
||||
Assert.Equal((from, Path.GetFullPath(target), true), (plan.From, plan.To, plan.SameVolume));
|
||||
var project = Assert.NotNull(plan.Projects.SingleOrDefault());
|
||||
Assert.Equal(mainId, project.ProjectId);
|
||||
Assert.True(plan.Files >= project.Files && project.Files > 0 && plan.Bytes > 0);
|
||||
|
||||
var failures = new List<string>();
|
||||
var closes = 0;
|
||||
var close = true;
|
||||
Task<bool> CloseProject()
|
||||
{
|
||||
closes++;
|
||||
return Task.FromResult(close);
|
||||
}
|
||||
|
||||
// Declined, or the open project kept open: nothing is queued.
|
||||
var dialogs = new MoveDialogs { Folder = target, Answer = false };
|
||||
Assert.False(await WorkspaceFolderChange.MoveAsync(host, dialogs, CloseProject, failures.Add));
|
||||
var asked = Assert.NotNull(dialogs.Asked.SingleOrDefault());
|
||||
Assert.Equal(Loc.L("Settings.Workspace.MovePlan.Message", plan.From, plan.To), asked.Message);
|
||||
Assert.Contains(Loc.L("Settings.Workspace.MovePlan.Projects", Formatting.Count(1)), asked.Detail);
|
||||
Assert.Contains(Loc.L("Settings.Workspace.MovePlan.SameVolume"), asked.Detail);
|
||||
Assert.True(asked is { IsDestructive: false, ActionText.Length: > 0, ActionToolTip.Length: > 0, SafeToolTip.Length: > 0 });
|
||||
Assert.Equal(0, closes, "nothing closed before the plan is confirmed");
|
||||
dialogs.Answer = true;
|
||||
close = false;
|
||||
Assert.False(await WorkspaceFolderChange.MoveAsync(host, dialogs, CloseProject, failures.Add));
|
||||
Assert.False(host.IsMoving);
|
||||
|
||||
// Queued: until it ends the workspace opens, copies, syncs and changes folder no more, and is not moved twice.
|
||||
close = true;
|
||||
Assert.True(await WorkspaceFolderChange.MoveAsync(host, dialogs, CloseProject, failures.Add));
|
||||
Assert.Empty(failures);
|
||||
Assert.True(host.IsMoving);
|
||||
var moving = Loc.L("Errors.Workspace.Moving");
|
||||
Assert.Equal(moving, (await Assert.ThrowsAsync<WorkspaceException>(() => host.FindAsync(mainId))).Message);
|
||||
Assert.Equal(moving, (await Assert.ThrowsAsync<WorkspaceException>(() => host.ProjectsOfAsync(bed.SequenceId))).Message);
|
||||
Assert.Equal(moving, (await Assert.ThrowsAsync<WorkspaceException>(() => host.SyncAsync(mainId))).Message);
|
||||
Assert.Equal(moving, (await Assert.ThrowsAsync<WorkspaceException>(() => host.QueueCopyAsync(mainId))).Message);
|
||||
Assert.Equal(moving, (await Assert.ThrowsAsync<WorkspaceException>(() => host.CheckoutAsync(bed.SequenceId, mainId, null))).Message);
|
||||
Assert.Equal(moving, (await Assert.ThrowsAsync<WorkspaceException>(() => host.UseFolderAsync(Path.Combine(bed.Directory, "elsewhere")))).Message);
|
||||
Assert.False(await WorkspaceFolderChange.MoveAsync(host, dialogs, CloseProject, failures.Add));
|
||||
Assert.SequenceEqual([moving], failures);
|
||||
Assert.Equal(((string?)null, 0), (settings.WorkspaceFolder, moves.Saves), "the setting waits for the move");
|
||||
|
||||
await moves.Engine.StartAsync(context.Cancellation);
|
||||
var job = Assert.NotNull((await moves.Db.Jobs.ListAsync(null, JobKind.WorkspaceMove, 5, cancellationToken: context.Cancellation)).Items.SingleOrDefault());
|
||||
await moves.WaitAsync(job.Id, JobState.Done);
|
||||
await WorkspaceTestBed.UntilAsync(() => !host.IsMoving, "the workspace is let go");
|
||||
|
||||
Assert.Equal((Path.GetFullPath(target), 1), (settings.WorkspaceFolder, moves.Saves));
|
||||
Assert.Equal(Path.GetFullPath(target), host.Root);
|
||||
Assert.Equal(mainId, Assert.NotNull(await host.FindAsync(mainId)).ProjectId);
|
||||
Assert.Equal(mainId, (await host.ListAsync()).Single().ProjectId);
|
||||
Assert.False(Directory.Exists(Path.Combine(from, mainId.ToString("N"))), "the project left the folder");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMoveThatFailsLeavesTheSettingAndTheWorkspaceWhereTheyWere()
|
||||
{
|
||||
await using var bed = await WorkspaceTestBed.CreateAsync(context, frames: 4);
|
||||
var settings = new AppSettings();
|
||||
var mainId = await bed.CheckoutAsync(bed.Host(settings));
|
||||
await using var moves = await MovesAsync(bed, settings);
|
||||
var host = moves.Host;
|
||||
var from = host.Root;
|
||||
var target = Path.Combine(bed.Directory, "target");
|
||||
|
||||
// A workspace job waiting refuses the plan.
|
||||
var copy = await moves.Engine.EnqueueAsync(JobKind.WorkspaceCopy, [bed.SequenceId], "copy", null, cancellationToken: context.Cancellation);
|
||||
Assert.Equal(Loc.L("Errors.Workspace.JobsRunning"), (await host.PlanMoveAsync(target)).Refusal);
|
||||
await moves.Engine.CancelAsync(copy.Id, context.Cancellation);
|
||||
|
||||
var plan = await host.PlanMoveAsync(target);
|
||||
await host.QueueMoveAsync(plan);
|
||||
// Meanwhile the target became a workspace of its own: the job refuses it.
|
||||
_ = new WorkspaceStore(target);
|
||||
await moves.Engine.StartAsync(context.Cancellation);
|
||||
var job = Assert.NotNull((await moves.Db.Jobs.ListAsync(null, JobKind.WorkspaceMove, 5, cancellationToken: context.Cancellation)).Items.SingleOrDefault());
|
||||
var failed = await moves.WaitAsync(job.Id, JobState.Failed);
|
||||
Assert.Equal(Loc.L("Errors.Workspace.MoveTargetNotEmpty", Path.GetFullPath(target)), failed.Error);
|
||||
await WorkspaceTestBed.UntilAsync(() => !host.IsMoving, "the workspace is let go");
|
||||
|
||||
Assert.Equal(((string?)null, 0, from), (settings.WorkspaceFolder, moves.Saves, host.Root));
|
||||
Assert.Equal(mainId, Assert.NotNull(await host.FindAsync(mainId)).ProjectId);
|
||||
Assert.Empty(new WorkspaceStore(target).List());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMoveStoppedBeforeItEndedIsOfferedAgainAndGoesOn()
|
||||
{
|
||||
await using var bed = await WorkspaceTestBed.CreateAsync(context, frames: 4);
|
||||
var settings = new AppSettings();
|
||||
var mainId = await bed.CheckoutAsync(bed.Host(settings));
|
||||
await using var moves = await MovesAsync(bed, settings);
|
||||
var host = moves.Host;
|
||||
var from = host.Root;
|
||||
var target = Path.Combine(bed.Directory, "target");
|
||||
|
||||
// The program closed as the move began: its journal is in the folder, and no job waits for it.
|
||||
var plan = await host.PlanMoveAsync(target);
|
||||
using (var closed = new CancellationTokenSource())
|
||||
{
|
||||
await closed.CancelAsync();
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => new WorkspaceMover(from, target).MoveAsync(plan.Fingerprint, cancellationToken: closed.Token));
|
||||
}
|
||||
Assert.True(host.IsMoving, "a journal holds the workspace");
|
||||
Assert.Equal(Loc.L("Errors.Workspace.Moving"), (await Assert.ThrowsAsync<WorkspaceException>(() => host.FindAsync(mainId))).Message);
|
||||
var journal = Assert.NotNull(await host.UnfinishedMoveAsync());
|
||||
Assert.Equal((Path.GetFullPath(target), WorkspaceMoveStage.Moving), (journal.To, journal.Stage));
|
||||
|
||||
var job = await host.ResumeMoveAsync(journal);
|
||||
Assert.Null(await host.UnfinishedMoveAsync());
|
||||
await moves.Engine.StartAsync(context.Cancellation);
|
||||
var done = await moves.WaitAsync(job.Id, JobState.Done);
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoveResumed", Path.Combine(from, WorkspaceMover.JournalFile)), done.LogTail);
|
||||
await WorkspaceTestBed.UntilAsync(() => !host.IsMoving, "the workspace is let go");
|
||||
Assert.Equal(Path.GetFullPath(target), settings.WorkspaceFolder);
|
||||
Assert.Equal(mainId, Assert.NotNull(await host.FindAsync(mainId)).ProjectId);
|
||||
Assert.Null(await host.UnfinishedMoveAsync());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A workspace over the bed's folder whose move jobs run on an engine of their own (over an index of their own, so the
|
||||
/// bed's engine never takes them), which tells the workspace once a move ended; the engine is started by the test.
|
||||
/// </summary>
|
||||
private async Task<Moves> MovesAsync(WorkspaceTestBed bed, AppSettings settings)
|
||||
{
|
||||
var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("moves"), "index.db")), context.Cancellation);
|
||||
var moves = new Moves(db);
|
||||
var options = new WorkspaceJobOptions(WorkspaceJobOptions.For(bed.Archives).Processing, () => false, (f, t) => moves.Host.MovedAsync(f, t));
|
||||
moves.Engine = new JobEngine(db, WorkspaceJobs.CreateHandlers(db, bed.Archives, null, options),
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "moves", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
moves.Host = new WorkspaceHost(bed.Environment, settings, () => moves.Saves++, () => db, () => bed.Archives, () => moves.Engine)
|
||||
{
|
||||
LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)),
|
||||
};
|
||||
return moves;
|
||||
}
|
||||
|
||||
private sealed class Moves(ChronosDatabase db) : IAsyncDisposable
|
||||
{
|
||||
public ChronosDatabase Db { get; } = db;
|
||||
public JobEngine Engine { get; set; } = null!;
|
||||
public WorkspaceHost Host { get; set; } = null!;
|
||||
public int Saves { get; set; }
|
||||
|
||||
public async Task<Job> WaitAsync(Guid jobId, JobState expected)
|
||||
{
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(60));
|
||||
while (true)
|
||||
{
|
||||
var job = await Db.Jobs.GetAsync(jobId);
|
||||
if (job is { State: JobState.Done or JobState.Failed or JobState.Cancelled })
|
||||
{
|
||||
Assert.Equal(expected, job.State, job.Error ?? job.LogTail);
|
||||
return job;
|
||||
}
|
||||
if (DateTime.UtcNow > deadline) Assert.Fail($"Job {jobId} still {job?.State} after its time budget");
|
||||
await Task.Delay(50);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await Engine.DisposeAsync();
|
||||
await Db.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Dialogs that pick <see cref="Folder"/> and answer every confirmation with <see cref="Answer"/>, recording what they were asked.</summary>
|
||||
private sealed class MoveDialogs : IAppDialogs
|
||||
{
|
||||
public string? Folder { get; set; }
|
||||
public bool Answer { get; set; }
|
||||
public List<ConfirmRequest> Asked { get; } = [];
|
||||
public string? PickFolder(string title, string? initialFolder = null) => Folder;
|
||||
public string? PickJsonFile(string title) => null;
|
||||
|
||||
public bool Confirm(ConfirmRequest request)
|
||||
{
|
||||
Asked.Add(request);
|
||||
return Answer;
|
||||
}
|
||||
|
||||
public bool SignIn(string server, string? username, Func<string, string, Task<string?>> signIn) => false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,392 @@
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Processing;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Workspaces;
|
||||
|
||||
/// <summary>
|
||||
/// Moving the workspace folder with its projects (phase 5c, P6): renamed on one volume, copied and hash-verified across
|
||||
/// volumes (one disk told it is two), a damaged copy that never costs the source, a move stopped and taken up from its
|
||||
/// journal, the refusals of the plan, a failure undone, and the job that carries it out.
|
||||
/// </summary>
|
||||
[Category("workspace")]
|
||||
public sealed class WorkspaceMoveTests(TestContext context)
|
||||
{
|
||||
static WorkspaceMoveTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
||||
|
||||
[Test]
|
||||
public async Task OnOneVolumeEveryProjectIsRenamedAndTheDesktopToldOnce()
|
||||
{
|
||||
var (from, ids) = Workspace(projects: 2);
|
||||
File.WriteAllText(Path.Combine(from, "notes.txt"), "the user's own");
|
||||
var before = Contents(from);
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
var mover = new WorkspaceMover(from, to);
|
||||
var survey = mover.Survey();
|
||||
Assert.True(survey.CanMove, survey.RefusalText);
|
||||
Assert.True(survey.SameVolume, "both folders lie in the test's own temporary folder");
|
||||
Assert.SequenceEqual([.. ids.Select(i => i.ToString("N")).Order(StringComparer.Ordinal), WorkspaceStore.ManifestFile, ".trash"], survey.Entries);
|
||||
Assert.Equal(before.Keys.Count(k => k != "notes.txt" && !IsLease(k)), survey.Files, "the leases and the user's file are not counted");
|
||||
Assert.True(survey.Projects.All(p => p.IsProject));
|
||||
|
||||
var told = new List<(string, string)>();
|
||||
var log = new List<string>();
|
||||
var (_, resumed) = await mover.MoveAsync(survey.Fingerprint, (f, t) =>
|
||||
{
|
||||
told.Add((f, t));
|
||||
return Task.CompletedTask;
|
||||
}, log: log.Add, cancellationToken: context.Cancellation);
|
||||
|
||||
Assert.False(resumed);
|
||||
Assert.SequenceEqual([(mover.From, mover.To)], told);
|
||||
// Everything of the workspace is at the target as it was, the lease with its project; the user's file stays.
|
||||
var after = Contents(to);
|
||||
foreach (var (name, sha) in before.Where(b => b.Key != "notes.txt")) Assert.Equal(sha, after.GetValueOrDefault(name), name);
|
||||
Assert.SequenceEqual(["notes.txt"], Directory.EnumerateFileSystemEntries(from).Select(Path.GetFileName).ToList());
|
||||
Assert.False(WorkspaceMover.HasJournal(from));
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", mover.To, 2, survey.Files), log);
|
||||
Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AcrossVolumesEveryFileIsCopiedAndCheckedBeforeTheSourceGoes()
|
||||
{
|
||||
var (from, ids) = Workspace(projects: 3);
|
||||
var before = Contents(from);
|
||||
var times = File.GetLastWriteTimeUtc(Path.Combine(from, ids[0].ToString("N"), WorkspaceProject.BundleFile));
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
var atSwitch = (Source: new Dictionary<string, string>(), Target: new Dictionary<string, string>());
|
||||
var mover = new WorkspaceMover(from, to)
|
||||
{
|
||||
SameVolume = false,
|
||||
Cut = (step, _) =>
|
||||
{
|
||||
if (step != "switched") return;
|
||||
atSwitch = (Contents(from), Contents(to));
|
||||
},
|
||||
};
|
||||
var survey = mover.Survey();
|
||||
Assert.False(survey.SameVolume);
|
||||
var told = 0;
|
||||
await mover.MoveAsync(survey.Fingerprint, (_, _) =>
|
||||
{
|
||||
told++;
|
||||
// Told only once everything is at the target: the source is still whole.
|
||||
Assert.Equal(before.Count, Contents(from).Count);
|
||||
return Task.CompletedTask;
|
||||
}, cancellationToken: context.Cancellation);
|
||||
|
||||
Assert.Equal(1, told);
|
||||
// At the switch both sides held everything (the lease is the project holder's: not copied).
|
||||
Assert.Equal(before.Count, atSwitch.Source.Count);
|
||||
foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, atSwitch.Target.GetValueOrDefault(name), name);
|
||||
// Then the source went, the journal last, and the folder with them.
|
||||
Assert.False(Directory.Exists(from));
|
||||
Assert.Equal(times, File.GetLastWriteTimeUtc(Path.Combine(to, ids[0].ToString("N"), WorkspaceProject.BundleFile)), "the copy keeps the file's time");
|
||||
Assert.Empty(Directory.EnumerateFiles(to, "*" + WorkspaceMover.PartSuffix, SearchOption.AllDirectories));
|
||||
Assert.True(Directory.Exists(Path.Combine(to, ids[0].ToString("N"), "media", "empty")), "an empty folder comes along");
|
||||
Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACopyThatDoesNotMatchItsSourceNeverCostsTheSource()
|
||||
{
|
||||
var (from, _) = Workspace(projects: 2);
|
||||
var before = Contents(from);
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
var copies = 0;
|
||||
var mover = new WorkspaceMover(from, to)
|
||||
{
|
||||
SameVolume = false,
|
||||
Cut = (step, path) =>
|
||||
{
|
||||
// The fourth file is damaged on its way to the target disk.
|
||||
if (step != "verify" || ++copies != 4) return;
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
bytes[0] ^= 0xFF;
|
||||
File.WriteAllBytes(path, bytes);
|
||||
},
|
||||
};
|
||||
var told = 0;
|
||||
var failed = await Assert.ThrowsAsync<InvalidOperationException>(() => mover.MoveAsync(mover.Survey().Fingerprint, (_, _) =>
|
||||
{
|
||||
told++;
|
||||
return Task.CompletedTask;
|
||||
}, cancellationToken: context.Cancellation));
|
||||
|
||||
Assert.True(failed.Message.StartsWith(Loc.L("Errors.Workspace.MoveVerify", "§").Split('§')[0], StringComparison.Ordinal), failed.Message);
|
||||
Assert.Equal(0, told);
|
||||
Same(before, Contents(from));
|
||||
Assert.False(WorkspaceMover.HasJournal(from), "the move was undone");
|
||||
Assert.False(Directory.Exists(to) && Directory.EnumerateFileSystemEntries(to).Any(), "no copy is left at the target");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMoveStoppedAcrossVolumesGoesOnFromItsJournalWithoutCopyingTwice()
|
||||
{
|
||||
var (from, _) = Workspace(projects: 3);
|
||||
var before = Contents(from);
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
||||
var copied = 0;
|
||||
var first = new WorkspaceMover(from, to)
|
||||
{
|
||||
SameVolume = false,
|
||||
Cut = (step, _) =>
|
||||
{
|
||||
// The program closes after the fifth file reached the target.
|
||||
if (step == "copied" && ++copied == 5) stop.Cancel();
|
||||
},
|
||||
};
|
||||
var fingerprint = first.Survey().Fingerprint;
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => first.MoveAsync(fingerprint, cancellationToken: stop.Token));
|
||||
Assert.True(WorkspaceMover.HasJournal(from), "the journal stays for the next run");
|
||||
Same(before, Contents(from));
|
||||
Assert.Equal(WorkspaceMoveStage.Moving, WorkspaceMover.ReadJournal(from)!.Stage);
|
||||
|
||||
// The workspace changed since (a lease taken and let go does not count): the move taken up does not ask the plan again.
|
||||
var again = 0;
|
||||
var second = new WorkspaceMover(from, to)
|
||||
{
|
||||
SameVolume = false,
|
||||
Cut = (step, _) =>
|
||||
{
|
||||
if (step == "copied") again++;
|
||||
},
|
||||
};
|
||||
var told = 0;
|
||||
var log = new List<string>();
|
||||
var (_, resumed) = await second.MoveAsync("another plan", (_, _) =>
|
||||
{
|
||||
told++;
|
||||
return Task.CompletedTask;
|
||||
}, log: log.Add, cancellationToken: context.Cancellation);
|
||||
|
||||
Assert.True(resumed);
|
||||
Assert.Equal(1, told);
|
||||
var copiedFiles = before.Keys.Count(k => !IsLease(k));
|
||||
Assert.Equal(copiedFiles - 5, again, "the files verified before the stop are not copied again");
|
||||
Assert.True(log.Any(l => l.StartsWith(Loc.L("Archive.Log.WorkspaceMoveResumed", "§").Split('§')[0], StringComparison.Ordinal)), string.Join("; ", log));
|
||||
var after = Contents(to);
|
||||
foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, after.GetValueOrDefault(name), name);
|
||||
Assert.False(Directory.Exists(from));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMoveStoppedOnOneVolumeGoesOnFromItsJournal()
|
||||
{
|
||||
var (from, ids) = Workspace(projects: 3);
|
||||
var before = Contents(from);
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
||||
var first = new WorkspaceMover(from, to)
|
||||
{
|
||||
SameVolume = true,
|
||||
Cut = (step, _) =>
|
||||
{
|
||||
if (step == "moved") stop.Cancel();
|
||||
},
|
||||
};
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => first.MoveAsync(first.Survey().Fingerprint, cancellationToken: stop.Token));
|
||||
Assert.True(WorkspaceMover.HasJournal(from));
|
||||
Assert.True(Directory.Exists(Path.Combine(to, ids.Select(i => i.ToString("N")).Order(StringComparer.Ordinal).First())), "the first project went");
|
||||
|
||||
var (_, resumed) = await new WorkspaceMover(from, to) { SameVolume = true }.MoveAsync("whatever", cancellationToken: context.Cancellation);
|
||||
Assert.True(resumed);
|
||||
Same(before, Contents(to));
|
||||
Assert.False(Directory.Exists(from));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMoveThatFailsOnOneVolumeIsUndone()
|
||||
{
|
||||
var (from, _) = Workspace(projects: 3);
|
||||
var before = Contents(from);
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
var moved = 0;
|
||||
var mover = new WorkspaceMover(from, to)
|
||||
{
|
||||
SameVolume = true,
|
||||
Cut = (step, _) =>
|
||||
{
|
||||
// The second project's rename meets a file someone holds open.
|
||||
if (step == "moved" && ++moved == 2) throw new IOException("The file is in use.");
|
||||
},
|
||||
};
|
||||
await Assert.ThrowsAsync<IOException>(() => mover.MoveAsync(mover.Survey().Fingerprint, cancellationToken: context.Cancellation));
|
||||
|
||||
Same(before, Contents(from));
|
||||
Assert.False(WorkspaceMover.HasJournal(from));
|
||||
Assert.False(Directory.Exists(to), "the target made for the move went with it");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APlanIsRefusedForTheFolderItselfAFullTargetNoRoomAndAChangedWorkspace()
|
||||
{
|
||||
var (from, ids) = Workspace(projects: 1);
|
||||
Assert.Equal(WorkspaceMoveRefusal.SameFolder, new WorkspaceMover(from, from + Path.DirectorySeparatorChar).Survey().Refusal);
|
||||
var inside = Path.Combine(from, "inner");
|
||||
var refused = new WorkspaceMover(from, inside).Survey();
|
||||
Assert.Equal(WorkspaceMoveRefusal.SameFolder, refused.Refusal);
|
||||
Assert.Equal(Loc.L("Errors.Workspace.MoveSameFolder", refused.To), refused.RefusalText);
|
||||
|
||||
// A target that is a workspace already, or holds a project of this one.
|
||||
var other = context.CreateTempDirectory("other");
|
||||
_ = new WorkspaceStore(other);
|
||||
Assert.Equal(WorkspaceMoveRefusal.TargetNotEmpty, new WorkspaceMover(from, other).Survey().Refusal);
|
||||
var holding = context.CreateTempDirectory("holding");
|
||||
Directory.CreateDirectory(Path.Combine(holding, ids[0].ToString("N")));
|
||||
var full = new WorkspaceMover(from, holding).Survey();
|
||||
Assert.Equal(Loc.L("Errors.Workspace.MoveTargetNotEmpty", full.To), full.RefusalText);
|
||||
// Unrelated files do not refuse it.
|
||||
var used = context.CreateTempDirectory("used");
|
||||
File.WriteAllText(Path.Combine(used, "readme.txt"), "x");
|
||||
Assert.True(new WorkspaceMover(from, used).Survey().CanMove);
|
||||
|
||||
// Across volumes the target's drive must hold the workspace and the reserve; a rename needs no room.
|
||||
var small = new WorkspaceMover(from, Path.Combine(used, "w")) { SameVolume = false, FreeSpace = _ => 1000, Reserve = 0 }.Survey();
|
||||
Assert.Equal((WorkspaceMoveRefusal?)WorkspaceMoveRefusal.NoRoom, small.Refusal);
|
||||
Assert.Equal(small.Bytes - 1000, small.Missing);
|
||||
Assert.True(new WorkspaceMover(from, Path.Combine(used, "w")) { SameVolume = true, FreeSpace = _ => 1000 }.Survey().CanMove);
|
||||
|
||||
// A file written after the plan: the move refuses it and touches nothing.
|
||||
var to = Path.Combine(used, "moved");
|
||||
var mover = new WorkspaceMover(from, to);
|
||||
var fingerprint = mover.Survey().Fingerprint;
|
||||
Assert.Equal(fingerprint, mover.Survey().Fingerprint, "the plan reads the same twice");
|
||||
File.WriteAllText(Path.Combine(from, ids[0].ToString("N"), "versions", "late.json"), "{}");
|
||||
var changed = await Assert.ThrowsAsync<InvalidOperationException>(() => mover.MoveAsync(fingerprint, cancellationToken: context.Cancellation));
|
||||
Assert.Equal(Loc.L("Errors.Workspace.MoveChanged"), changed.Message);
|
||||
Assert.False(Directory.Exists(to));
|
||||
Assert.False(WorkspaceMover.HasJournal(from));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheJobMovesTheWorkspaceAndTellsTheDesktop()
|
||||
{
|
||||
var (from, ids) = Workspace(projects: 2);
|
||||
var before = Contents(from);
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation);
|
||||
var told = new List<(string, string)>();
|
||||
var options = new WorkspaceJobOptions(new ProcessingOptions(), () => false, (f, t) =>
|
||||
{
|
||||
told.Add((f, t));
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
var handler = new WorkspaceMoveJobHandler(db, options) { Movers = p => new WorkspaceMover(p.From, p.To) { SameVolume = false } };
|
||||
await using var engine = new JobEngine(db, [handler],
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
||||
|
||||
// Another job of the workspace waits (nothing here runs it): the move is refused.
|
||||
var copy = await engine.EnqueueAsync(JobKind.WorkspaceCopy, [Guid.CreateVersion7()], "copy", null, cancellationToken: context.Cancellation);
|
||||
var survey = new WorkspaceMover(from, to).Survey();
|
||||
await engine.StartAsync(context.Cancellation);
|
||||
var refused = await engine.EnqueueAsync(WorkspaceJobs.Move(from, to, survey.Fingerprint), cancellationToken: context.Cancellation);
|
||||
var failed = await WaitAsync(db, refused.Id);
|
||||
Assert.Equal(JobState.Failed, failed.State);
|
||||
Assert.Equal(Loc.L("Errors.Workspace.JobsRunning"), failed.Error);
|
||||
Same(before, Contents(from));
|
||||
|
||||
await engine.CancelAsync(copy.Id, context.Cancellation);
|
||||
var job = await engine.EnqueueAsync(WorkspaceJobs.Move(from, to, survey.Fingerprint), cancellationToken: context.Cancellation);
|
||||
var done = await WaitAsync(db, job.Id);
|
||||
Assert.Equal(JobState.Done, done.State, done.Error ?? done.LogTail);
|
||||
Assert.SequenceEqual([(survey.From, survey.To)], told);
|
||||
Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count);
|
||||
Assert.False(Directory.Exists(from));
|
||||
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", survey.To, 2, survey.Files), done.LogTail);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMoveCancelledWhilePausedIsUndone()
|
||||
{
|
||||
var (from, _) = Workspace(projects: 2);
|
||||
var before = Contents(from);
|
||||
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
||||
// A move stopped half way, as a pause leaves it.
|
||||
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
||||
var mover = new WorkspaceMover(from, to) { SameVolume = true, Cut = (step, _) => stop.Cancel() };
|
||||
var fingerprint = mover.Survey().Fingerprint;
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => mover.MoveAsync(fingerprint, cancellationToken: stop.Token));
|
||||
Assert.NotEqual(before.Count, Contents(from).Count);
|
||||
|
||||
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation);
|
||||
var handler = new WorkspaceMoveJobHandler(db, new WorkspaceJobOptions(new ProcessingOptions(), () => false))
|
||||
{
|
||||
Movers = p => new WorkspaceMover(p.From, p.To) { SameVolume = true },
|
||||
};
|
||||
var request = WorkspaceJobs.Move(from, to, fingerprint);
|
||||
await using var engine = new JobEngine(db, [handler]);
|
||||
var job = await engine.EnqueueAsync(request, cancellationToken: context.Cancellation) with { State = JobState.Cancelled };
|
||||
await handler.CancelledAsync(job, context.Cancellation);
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(20));
|
||||
while (WorkspaceMover.HasJournal(from) && DateTime.UtcNow < deadline) await Task.Delay(20, context.Cancellation);
|
||||
|
||||
Assert.False(WorkspaceMover.HasJournal(from));
|
||||
Same(before, Contents(from));
|
||||
Assert.False(Directory.Exists(to));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A workspace with <paramref name="projects"/> projects (a working state, media with an empty folder, versions, the
|
||||
/// lease), its manifest and a trash entry.
|
||||
/// </summary>
|
||||
private (string Root, List<Guid> Ids) Workspace(int projects)
|
||||
{
|
||||
var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace");
|
||||
_ = new WorkspaceStore(root);
|
||||
var ids = new List<Guid>();
|
||||
for (var i = 0; i < projects; i++)
|
||||
{
|
||||
var id = Guid.CreateVersion7();
|
||||
ids.Add(id);
|
||||
var folder = Path.Combine(root, id.ToString("N"));
|
||||
Directory.CreateDirectory(Path.Combine(folder, "media", "proxies"));
|
||||
Directory.CreateDirectory(Path.Combine(folder, "media", "empty"));
|
||||
Directory.CreateDirectory(Path.Combine(folder, "versions"));
|
||||
File.WriteAllText(Path.Combine(folder, WorkspaceProject.BundleFile), "{\"project\": " + i + "}");
|
||||
for (var f = 0; f < 3; f++) File.WriteAllBytes(Path.Combine(folder, "media", "proxies", $"{f}.jpg"), RandomNumberGenerator.GetBytes(4096 + f * 1000));
|
||||
File.WriteAllText(Path.Combine(folder, "versions", "0001.json"), "{}");
|
||||
File.WriteAllText(Path.Combine(folder, ".lock.lease"), "held by nobody");
|
||||
}
|
||||
var trash = Path.Combine(root, ".trash", "removed-1");
|
||||
Directory.CreateDirectory(trash);
|
||||
File.WriteAllText(Path.Combine(trash, "kept.json"), "{}");
|
||||
return (root, ids);
|
||||
}
|
||||
|
||||
/// <summary>Every file under <paramref name="root"/> by its relative name, with its SHA-256; a move's journal is not counted.</summary>
|
||||
private static Dictionary<string, string> Contents(string root) =>
|
||||
Directory.Exists(root)
|
||||
? Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories).Where(f => Path.GetFileName(f) != WorkspaceMover.JournalFile)
|
||||
.ToDictionary(f => Path.GetRelativePath(root, f).Replace('\\', '/'), f => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(f))))
|
||||
: [];
|
||||
|
||||
/// <summary>Both hold the same files with the same contents.</summary>
|
||||
private static void Same(Dictionary<string, string> expected, Dictionary<string, string> actual)
|
||||
{
|
||||
Assert.SequenceEqual(expected.Keys.Order(StringComparer.Ordinal), actual.Keys.Order(StringComparer.Ordinal));
|
||||
foreach (var (name, sha) in expected) Assert.Equal(sha, actual[name], name);
|
||||
}
|
||||
|
||||
private static bool IsLease(string name) => name.EndsWith(".lease", StringComparison.Ordinal);
|
||||
|
||||
private async Task<Job> WaitAsync(ChronosDatabase db, Guid jobId)
|
||||
{
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(60));
|
||||
while (true)
|
||||
{
|
||||
var job = await db.Jobs.GetAsync(jobId, context.Cancellation);
|
||||
if (job is { State: JobState.Done or JobState.Failed or JobState.Cancelled }) return job;
|
||||
if (DateTime.UtcNow > deadline) Assert.Fail($"Job {jobId} still {job?.State}");
|
||||
await Task.Delay(20, context.Cancellation);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user