Files
Chronos/src/Titano.Core.Windows/Shell/AppPaths.cs
T
Claude 7606957276 fix(app): warn about a synced data folder and name a portable copy's earlier data
AppEnvironment now carries AppPaths.SyncedFolder: when the per-user data
folder itself lies in a OneDrive sync root, Titano still starts (nowhere
is safer), logs it and shows the error toast "Data inside OneDrive" with
Open, instead of the false "Data kept outside OneDrive". The latter now
needs PortableSyncRoot (renamed from the old SyncedFolder meaning).

A portable copy now in a OneDrive folder whose .\data still holds an
index or settings.json gets that folder named in the log and in a toast
("Earlier data unused", with Open). Nothing is moved or deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 16:34:05 +00:00

85 lines
4.3 KiB
C#

using Titano.Core.IO;
namespace Titano.Core.Windows.Shell;
/// <summary>
/// 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, <see cref="EarlierPortableData"/> the data it kept there before), 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 = DataLocation.ProductFolderName;
public const string DatabaseFileName = "titano.db";
public const string SettingsFileName = "settings.json";
private AppPaths(DataLocation location, string? earlierPortableData)
{
DataRoot = location.Root;
IsPortable = location.IsPortable;
SyncedFolder = location.SyncedFolder;
PortableSyncRoot = location.PortableSyncRoot;
EarlierPortableData = earlierPortableData;
}
/// <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; }
public string DatabasePath => Path.Combine(DataRoot, DatabaseFileName);
public string LogsDirectory => Path.Combine(DataRoot, "logs");
public string CacheDirectory => Path.Combine(DataRoot, "cache");
public string ThumbnailsDirectory => Path.Combine(DataRoot, "thumbnails");
/// <summary>The OneDrive sync root containing <see cref="DataRoot"/>, or null when the location is safe.</summary>
public string? SyncedFolder { get; }
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>
/// The <c>.\data</c> folder of a portable copy now in a OneDrive folder when it still holds the index or the settings
/// of an earlier start: nothing moves or deletes it, and the user is told where it is. Null otherwise.
/// </summary>
public string? EarlierPortableData { get; }
/// <summary>Paths for this process: the executable's folder decides portable mode.</summary>
public static AppPaths Resolve() => Resolve(AppContext.BaseDirectory, KnownFolders.LocalAppData);
/// <summary>Paths for an executable in <paramref name="applicationDirectory"/>, checked against the user's OneDrive roots.</summary>
public static AppPaths Resolve(string applicationDirectory, string localAppData) =>
Resolve(applicationDirectory, localAppData, OneDriveFolders.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)
{
var location = DataLocation.Choose(applicationDirectory, PortableMode.IsEnabled(applicationDirectory), localAppData, syncRoots);
var beside = Path.GetFullPath(Path.Combine(applicationDirectory, PortableMode.DataFolderName));
var earlier = location.PortableSyncRoot is not null
&& (File.Exists(Path.Combine(beside, DatabaseFileName)) || File.Exists(Path.Combine(beside, SettingsFileName)))
? beside
: null;
return new AppPaths(location, earlier);
}
/// <summary>Creates the data, logs, cache and thumbnails folders.</summary>
/// <exception cref="InvalidOperationException">The data root lies inside a OneDrive-synced folder.</exception>
public void CreateDirectories()
{
if (SyncedFolder is not null)
throw new InvalidOperationException($"The data folder '{DataRoot}' is inside the OneDrive folder '{SyncedFolder}'; the database would be corrupted by synchronisation.");
Directory.CreateDirectory(DataRoot);
Directory.CreateDirectory(LogsDirectory);
Directory.CreateDirectory(CacheDirectory);
Directory.CreateDirectory(ThumbnailsDirectory);
}
}