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:
12 files changed
+279
-53
No files matched your search
@@ -45,6 +45,8 @@ public partial class App : Application
|
||||
// Capture runs in CI: mirror the log to stderr so failures are visible in the job output.
|
||||
Log.SetSink(capture ? new TeeLogSink(_log, new StderrLogSink()) : _log);
|
||||
Log.Info("app", $"Titano {AppServices.VersionText} starting; data at {Environment.DataDirectory}");
|
||||
// Settings tells the user once the window is up.
|
||||
if (Environment.SyncedFolder is { } synced) Log.Warning("app", $"This portable copy runs in the OneDrive folder {synced}: its data is kept outside it");
|
||||
|
||||
var projectFile = capture ? null : ProjectFileArgument(e.Args);
|
||||
if (!capture)
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
using System.IO;
|
||||
using Titano.Core.Windows.Shell;
|
||||
|
||||
namespace Titano.App.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Where the client keeps its data (BRIEF §6.2): <c>%LOCALAPPDATA%\Titano</c>, or <c>.\data</c> next to the
|
||||
/// executable when <c>portable.txt</c> exists. Never inside Documents/OneDrive (SQLite corrupts in synced folders).
|
||||
/// Where the client keeps its data (BRIEF §6.2, <see cref="AppPaths"/>): <c>%LOCALAPPDATA%\Titano</c>, or <c>.\data</c>
|
||||
/// next to the executable when <c>portable.txt</c> exists. Never inside a folder OneDrive synchronises (SQLite corrupts
|
||||
/// there): a portable copy in one keeps its data in <c>%LOCALAPPDATA%\Titano</c>, and Settings tells the user so
|
||||
/// (<see cref="SyncedFolder"/>).
|
||||
/// </summary>
|
||||
public sealed class AppEnvironment
|
||||
{
|
||||
private AppEnvironment(string dataDirectory, bool portable, bool isCapture)
|
||||
private AppEnvironment(string dataDirectory, bool portable, bool isCapture, string? syncedFolder = null)
|
||||
{
|
||||
DataDirectory = dataDirectory;
|
||||
IsPortable = portable;
|
||||
IsCapture = isCapture;
|
||||
SyncedFolder = syncedFolder;
|
||||
Directory.CreateDirectory(DataDirectory);
|
||||
Directory.CreateDirectory(LogDirectory);
|
||||
Directory.CreateDirectory(CacheDirectory);
|
||||
@@ -24,20 +28,20 @@ public sealed class AppEnvironment
|
||||
/// <summary>A throw-away environment for <c>--capture</c>: nothing of the user's data is read or written.</summary>
|
||||
public bool IsCapture { get; }
|
||||
|
||||
/// <summary>The OneDrive folder this portable copy runs from; its data is in <c>%LOCALAPPDATA%\Titano</c> instead of <c>.\data</c>. Null otherwise.</summary>
|
||||
public string? SyncedFolder { get; }
|
||||
|
||||
public string LogDirectory => Path.Combine(DataDirectory, "logs");
|
||||
public string CacheDirectory => Path.Combine(DataDirectory, "cache");
|
||||
public string BackupDirectory => Path.Combine(DataDirectory, "backups");
|
||||
public string DatabasePath => Path.Combine(DataDirectory, "titano.db");
|
||||
public string SettingsPath => Path.Combine(DataDirectory, "settings.json");
|
||||
|
||||
public static AppEnvironment Detect()
|
||||
{
|
||||
var exeDir = AppContext.BaseDirectory;
|
||||
if (File.Exists(Path.Combine(exeDir, "portable.txt")))
|
||||
return new AppEnvironment(Path.Combine(exeDir, "data"), portable: true, isCapture: false);
|
||||
var local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
return new AppEnvironment(Path.Combine(local, "Titano"), portable: false, isCapture: false);
|
||||
}
|
||||
/// <summary>The environment of this process: its executable's folder and the user's OneDrive folders decide.</summary>
|
||||
public static AppEnvironment Detect() => From(AppPaths.Resolve());
|
||||
|
||||
/// <summary>The environment at <paramref name="paths"/> (its folders are created).</summary>
|
||||
public static AppEnvironment From(AppPaths paths) => new(paths.DataRoot, paths.IsPortable, isCapture: false, paths.PortableSyncRoot);
|
||||
|
||||
/// <summary>A fresh temporary data folder for screenshots and smoke tests; delete it with <see cref="DeleteCaptureData"/>.</summary>
|
||||
public static AppEnvironment ForCapture() =>
|
||||
|
||||
@@ -1,22 +1,28 @@
|
||||
using Titano.Core.IO;
|
||||
|
||||
namespace Titano.Core.Windows.Shell;
|
||||
|
||||
/// <summary>
|
||||
/// Where the desktop programs keep their data (BRIEF §6.2): <c>%LOCALAPPDATA%\Titano</c>, or <c>.\data</c> next to the
|
||||
/// executable in portable mode. SQLite corrupts inside synchronised folders, so a data root under OneDrive is
|
||||
/// reported by <see cref="SyncedFolder"/> and refused by <see cref="CreateDirectories"/>.
|
||||
/// Where the desktop programs keep their data (BRIEF §6.2, <see cref="DataLocation"/>): <c>%LOCALAPPDATA%\Titano</c>, or
|
||||
/// <c>.\data</c> next to the executable in portable mode. SQLite corrupts inside synchronised folders, so a portable
|
||||
/// copy in a OneDrive folder keeps its data in <c>%LOCALAPPDATA%\Titano</c> instead (<see cref="PortableSyncRoot"/>
|
||||
/// names that folder to tell the user), and a data root still under OneDrive is reported by <see cref="SyncedFolder"/>
|
||||
/// and refused by <see cref="CreateDirectories"/>.
|
||||
/// </summary>
|
||||
public sealed class AppPaths
|
||||
{
|
||||
public const string ProductFolderName = "Titano";
|
||||
public const string ProductFolderName = DataLocation.ProductFolderName;
|
||||
public const string DatabaseFileName = "titano.db";
|
||||
|
||||
private AppPaths(string dataRoot, bool isPortable, string? syncedFolder)
|
||||
private AppPaths(DataLocation location)
|
||||
{
|
||||
DataRoot = dataRoot;
|
||||
IsPortable = isPortable;
|
||||
SyncedFolder = syncedFolder;
|
||||
DataRoot = location.Root;
|
||||
IsPortable = location.IsPortable;
|
||||
SyncedFolder = location.SyncedFolder;
|
||||
PortableSyncRoot = location.PortableSyncRoot;
|
||||
}
|
||||
|
||||
/// <summary>A portable copy, also when its data had to go to <c>%LOCALAPPDATA%\Titano</c>.</summary>
|
||||
public bool IsPortable { get; }
|
||||
|
||||
public string DataRoot { get; }
|
||||
@@ -34,6 +40,9 @@ public sealed class AppPaths
|
||||
|
||||
public bool IsInSyncedFolder => SyncedFolder is not null;
|
||||
|
||||
/// <summary>The OneDrive sync root a portable copy runs from, whose data is in <c>%LOCALAPPDATA%\Titano</c> instead; null otherwise.</summary>
|
||||
public string? PortableSyncRoot { get; }
|
||||
|
||||
/// <summary>Paths for this process: the executable's folder decides portable mode.</summary>
|
||||
public static AppPaths Resolve() => Resolve(AppContext.BaseDirectory, KnownFolders.LocalAppData);
|
||||
|
||||
@@ -41,14 +50,9 @@ public sealed class AppPaths
|
||||
public static AppPaths Resolve(string applicationDirectory, string localAppData) =>
|
||||
Resolve(applicationDirectory, localAppData, OneDriveFolders.SyncRoots());
|
||||
|
||||
internal static AppPaths Resolve(string applicationDirectory, string localAppData, IReadOnlyList<string> syncRoots)
|
||||
{
|
||||
var portable = PortableMode.IsEnabled(applicationDirectory);
|
||||
var root = Path.GetFullPath(portable
|
||||
? Path.Combine(applicationDirectory, PortableMode.DataFolderName)
|
||||
: Path.Combine(localAppData, ProductFolderName));
|
||||
return new AppPaths(root, portable, OneDriveFolders.Containing(root, syncRoots));
|
||||
}
|
||||
/// <summary>Paths for an executable in <paramref name="applicationDirectory"/>, checked against <paramref name="syncRoots"/>.</summary>
|
||||
public static AppPaths Resolve(string applicationDirectory, string localAppData, IReadOnlyCollection<string> syncRoots) =>
|
||||
new(DataLocation.Choose(applicationDirectory, PortableMode.IsEnabled(applicationDirectory), localAppData, syncRoots));
|
||||
|
||||
/// <summary>Creates the data, logs, cache and thumbnails folders.</summary>
|
||||
/// <exception cref="InvalidOperationException">The data root lies inside a OneDrive-synced folder.</exception>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Security;
|
||||
using Microsoft.Win32;
|
||||
using Titano.Core.IO;
|
||||
|
||||
namespace Titano.Core.Windows.Shell;
|
||||
|
||||
@@ -10,34 +12,34 @@ public static class OneDriveFolders
|
||||
|
||||
/// <summary>
|
||||
/// Sync roots from the OneDrive environment variables and <c>HKCU\Software\Microsoft\OneDrive\Accounts\*\UserFolder</c>,
|
||||
/// as full paths without a trailing separator, distinct (case-insensitive).
|
||||
/// as full paths without a trailing separator, distinct (case-insensitive). An account key that cannot be read is
|
||||
/// skipped: the programs start all the same.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<string> SyncRoots()
|
||||
{
|
||||
var roots = new List<string>();
|
||||
foreach (var variable in EnvironmentVariables) Add(roots, Environment.GetEnvironmentVariable(variable));
|
||||
using var accounts = Registry.CurrentUser.OpenSubKey(AccountsKey);
|
||||
if (accounts is not null)
|
||||
try
|
||||
{
|
||||
foreach (var name in accounts.GetSubKeyNames())
|
||||
using var accounts = Registry.CurrentUser.OpenSubKey(AccountsKey);
|
||||
if (accounts is not null)
|
||||
{
|
||||
using var account = accounts.OpenSubKey(name);
|
||||
Add(roots, account?.GetValue("UserFolder") as string);
|
||||
foreach (var name in accounts.GetSubKeyNames())
|
||||
{
|
||||
using var account = accounts.OpenSubKey(name);
|
||||
Add(roots, account?.GetValue("UserFolder") as string);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is SecurityException or IOException or UnauthorizedAccessException)
|
||||
{
|
||||
// The environment variables name the usual roots.
|
||||
}
|
||||
return roots;
|
||||
}
|
||||
|
||||
/// <summary>The sync root that contains <paramref name="path"/>, or null. Both are expected to be full paths.</summary>
|
||||
public static string? Containing(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>
|
||||
internal static bool IsUnder(string path, string 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 '/';
|
||||
}
|
||||
public static string? Containing(string path, IEnumerable<string> syncRoots) => DataLocation.SyncRootOf(path, syncRoots);
|
||||
|
||||
private static void Add(List<string> roots, string? candidate)
|
||||
{
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using Titano.Core.IO;
|
||||
|
||||
namespace Titano.Core.Windows.Shell;
|
||||
|
||||
/// <summary>Portable mode (BRIEF §6.2): a <c>portable.txt</c> next to the executable keeps all data under <c>.\data\</c>.</summary>
|
||||
public static class PortableMode
|
||||
{
|
||||
public const string MarkerFileName = "portable.txt";
|
||||
public const string DataFolderName = "data";
|
||||
public const string DataFolderName = DataLocation.PortableFolderName;
|
||||
|
||||
public static bool IsEnabled(string applicationDirectory) => File.Exists(Path.Combine(applicationDirectory, MarkerFileName));
|
||||
}
|
||||
@@ -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 '/';
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,8 @@ namespace Titano.Uploader.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Where the uploader keeps its files: <c>%LOCALAPPDATA%\Titano</c> (shared with the client), or <c>.\data</c> next
|
||||
/// to the executable when <c>portable.txt</c> exists — the same rule as the desktop client.
|
||||
/// to the executable when <c>portable.txt</c> exists — the desktop client's rule, except that the client never keeps
|
||||
/// its SQLite index in a folder OneDrive synchronises; the Uploader's JSON files, written atomically, stay there.
|
||||
/// </summary>
|
||||
public sealed class UploaderEnvironment
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user