fix(app): a portable copy in a OneDrive folder keeps its index outside it

BRIEF §6.2 forbids a SQLite index in a synchronised folder. AppEnvironment.Detect now resolves the data folder
through Core.Windows' AppPaths and OneDriveFolders: a portable copy whose .\data would lie under a OneDrive sync
root (a OneDrive folder, or a Desktop or Documents folder backed up into one) keeps its data in
%LOCALAPPDATA%\Titano and stays portable otherwise; AppEnvironment.SyncedFolder names the OneDrive folder so the
user can be told. The pure choice lives in Core/IO/DataLocation (tested on Linux); reading the OneDrive accounts
key no longer fails start-up when it cannot be read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:02:33 +00:00
1 parent 8eaaef5085
commit 815782a708
12 files changed
+279 -53

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
namespace Titano.Core.IO;
/// <summary>
/// Where the desktop client keeps its data (BRIEF §6.2): the per-user folder (<c>%LOCALAPPDATA%\Titano</c>), or
/// <c>.\data</c> next to the executable of a portable copy (a <c>portable.txt</c> beside it). SQLite corrupts inside
/// synchronised folders, so a portable copy in a folder OneDrive synchronises (a OneDrive folder, or a Desktop or
/// Documents folder moved into one) keeps its data in the per-user folder instead, and <see cref="PortableSyncRoot"/>
/// says so, to tell the user. Paths are full paths; a folder lies in a sync root when its whole leading segments are
/// that root's, ignoring case as Windows does.
/// </summary>
/// <param name="Root">The data folder.</param>
/// <param name="IsPortable">The copy is portable, also when its data had to go to the per-user folder.</param>
/// <param name="SyncedFolder">The sync root that contains <paramref name="Root"/> itself; null when the data is safe there.</param>
/// <param name="PortableSyncRoot">The sync root a portable copy runs from, whose data went to the per-user folder instead; null otherwise.</param>
public sealed record DataLocation(string Root, bool IsPortable, string? SyncedFolder, string? PortableSyncRoot)
{
/// <summary>The data folder's name under the user's local application data.</summary>
public const string ProductFolderName = "Titano";
/// <summary>The data folder's name next to a portable copy's executable.</summary>
public const string PortableFolderName = "data";
/// <summary>
/// The data folder of a copy in <paramref name="applicationDirectory"/> (<paramref name="portable"/> when it has a
/// <c>portable.txt</c>), given the user's local application data and the OneDrive sync roots.
/// </summary>
public static DataLocation Choose(string applicationDirectory, bool portable, string localAppData, IReadOnlyCollection<string> syncRoots)
{
ArgumentNullException.ThrowIfNull(applicationDirectory);
ArgumentNullException.ThrowIfNull(localAppData);
ArgumentNullException.ThrowIfNull(syncRoots);
var perUser = Path.GetFullPath(Path.Combine(localAppData, ProductFolderName));
if (!portable) return new DataLocation(perUser, false, SyncRootOf(perUser, syncRoots), null);
var beside = Path.GetFullPath(Path.Combine(applicationDirectory, PortableFolderName));
return SyncRootOf(beside, syncRoots) is { } synced
? new DataLocation(perUser, true, SyncRootOf(perUser, syncRoots), synced)
: new DataLocation(beside, true, null, null);
}
/// <summary>The sync root that contains <paramref name="path"/>, or null.</summary>
public static string? SyncRootOf(string path, IEnumerable<string> syncRoots) => syncRoots.FirstOrDefault(root => IsUnder(path, root));
/// <summary>True when <paramref name="path"/> equals <paramref name="root"/> or lies below it (whole path segments, case-insensitive).</summary>
public static bool IsUnder(string path, string root)
{
ArgumentNullException.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(root);
var trimmedRoot = root.TrimEnd('\\', '/');
if (trimmedRoot.Length == 0 || !path.StartsWith(trimmedRoot, StringComparison.OrdinalIgnoreCase)) return false;
return path.Length == trimmedRoot.Length || path[trimmedRoot.Length] is '\\' or '/';
}
}