diff --git a/src/Titano.Core.Windows/Shell/OneDriveFolders.cs b/src/Titano.Core.Windows/Shell/OneDriveFolders.cs index 3b9ce98a..f4e116da 100644 --- a/src/Titano.Core.Windows/Shell/OneDriveFolders.cs +++ b/src/Titano.Core.Windows/Shell/OneDriveFolders.cs @@ -4,43 +4,62 @@ using Titano.Core.IO; namespace Titano.Core.Windows.Shell; -/// Folders synchronised by OneDrive for the current user (personal and work/school accounts). +/// +/// Folders synchronised by OneDrive for the current user: personal and work/school accounts, and the SharePoint and Teams +/// libraries synced with their Sync button. +/// public static class OneDriveFolders { private const string AccountsKey = @"Software\Microsoft\OneDrive\Accounts"; + + /// + /// The sync engines' registry, which Office reads to find synchronised files: one subkey per folder OneDrive + /// synchronises ("Personal", each work or school account, and each library synced with its Sync button, which lies + /// beside the account's folder, not in it), its local folder in MountPoint. + /// + private const string MountPointsKey = @"Software\SyncEngines\Providers\OneDrive"; + private static readonly string[] EnvironmentVariables = ["OneDrive", "OneDriveConsumer", "OneDriveCommercial"]; /// - /// Sync roots from the OneDrive environment variables and HKCU\Software\Microsoft\OneDrive\Accounts\*\UserFolder, - /// 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. + /// Sync roots from the OneDrive environment variables, HKCU\Software\Microsoft\OneDrive\Accounts\*\UserFolder + /// and HKCU\Software\SyncEngines\Providers\OneDrive\*\MountPoint, as full paths without a trailing separator, + /// distinct (case-insensitive). A key that cannot be read is skipped: the programs start all the same. /// - public static IReadOnlyList SyncRoots() + public static IReadOnlyList SyncRoots() => SyncRoots(Registry.CurrentUser); + + /// with the registry keys read under instead of the current user's hive. + public static IReadOnlyList SyncRoots(RegistryKey user) { var roots = new List(); foreach (var variable in EnvironmentVariables) Add(roots, Environment.GetEnvironmentVariable(variable)); - try - { - using var accounts = Registry.CurrentUser.OpenSubKey(AccountsKey); - if (accounts is not null) - { - 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. - } + AddFolders(roots, user, AccountsKey, "UserFolder"); + AddFolders(roots, user, MountPointsKey, "MountPoint"); return roots; } /// The sync root that contains , or null. Both are expected to be full paths. public static string? Containing(string path, IEnumerable syncRoots) => DataLocation.SyncRootOf(path, syncRoots); + /// Adds the folder that value of each subkey of names. + private static void AddFolders(List roots, RegistryKey user, string keyPath, string valueName) + { + try + { + using var parent = user.OpenSubKey(keyPath); + if (parent is null) return; + foreach (var name in parent.GetSubKeyNames()) + { + using var entry = parent.OpenSubKey(name); + Add(roots, entry?.GetValue(valueName) as string); + } + } + catch (Exception ex) when (ex is SecurityException or IOException or UnauthorizedAccessException) + { + // The environment variables and the other key name the usual roots. + } + } + private static void Add(List roots, string? candidate) { if (string.IsNullOrWhiteSpace(candidate) || !Path.IsPathFullyQualified(candidate)) return; diff --git a/tests/Titano.Core.Windows.Tests/Shell/OneDriveFoldersTests.cs b/tests/Titano.Core.Windows.Tests/Shell/OneDriveFoldersTests.cs index cd0c9bd6..8cac13c1 100644 --- a/tests/Titano.Core.Windows.Tests/Shell/OneDriveFoldersTests.cs +++ b/tests/Titano.Core.Windows.Tests/Shell/OneDriveFoldersTests.cs @@ -1,9 +1,13 @@ +using Microsoft.Win32; using Titano.Core.Windows.Shell; using Titano.Testing; namespace Titano.Core.Windows.Tests.Shell; -/// The OneDrive folders of the user as Windows reports them. Serial: a test sets a process environment variable. +/// +/// The OneDrive folders of the user as Windows reports them, from its environment and registry (a scratch key in tests). +/// Serial: a test sets a process environment variable. +/// [Serial] public sealed class OneDriveFoldersTests(TestContext context) { @@ -36,4 +40,47 @@ public sealed class OneDriveFoldersTests(TestContext context) Environment.SetEnvironmentVariable(Variable, previous); } } + + [Test] + [WindowsOnly] + public void ALibrarySyncedBesideTheAccountsFolderIsASyncRootToo() + { + // A scratch hive of its own: the user's OneDrive settings are never read or touched. + var hive = @"Software\Titano.OneDriveTests." + Guid.NewGuid().ToString("N"); + try + { + var profile = context.CreateTempDirectory("profile"); + var account = Path.Combine(profile, "OneDrive - Contoso"); + // The Sync button of a SharePoint or Teams library puts it in %USERPROFILE%\, not in the account's folder. + var library = Path.Combine(profile, "Contoso", "Team Site - Documents"); + using var user = Registry.CurrentUser.CreateSubKey(hive, writable: true); + using (var business = user.CreateSubKey(@"Software\Microsoft\OneDrive\Accounts\Business1")) business.SetValue("UserFolder", account); + using (var engine = user.CreateSubKey(@"Software\SyncEngines\Providers\OneDrive\Business1")) engine.SetValue("MountPoint", account); + using (var site = user.CreateSubKey(@"Software\SyncEngines\Providers\OneDrive\8c2f0b1e7d6a4f3e9b5c2d1a0e9f8b7c+1")) site.SetValue("MountPoint", library + @"\"); + // Entries without a full path name nothing. + using (var relative = user.CreateSubKey(@"Software\SyncEngines\Providers\OneDrive\Relative")) relative.SetValue("MountPoint", @"Contoso\Elsewhere"); + using (user.CreateSubKey(@"Software\SyncEngines\Providers\OneDrive\Empty")) { } + + var roots = OneDriveFolders.SyncRoots(user); + + Assert.Equal(1, roots.Count(r => string.Equals(r, account, StringComparison.OrdinalIgnoreCase)), "the account's folder once"); + Assert.Contains(roots, r => string.Equals(r, library, StringComparison.OrdinalIgnoreCase)); + Assert.False(roots.Any(r => r.EndsWith("Elsewhere", StringComparison.OrdinalIgnoreCase)), "a relative mount point is ignored"); + + // A portable copy unpacked in that library keeps its data in %LOCALAPPDATA%\Titano. + var app = Directory.CreateDirectory(Path.Combine(library, "Tools", "Titano")).FullName; + File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), ""); + var local = context.CreateTempDirectory("local"); + + var paths = AppPaths.Resolve(app, local, roots); + + Assert.True(paths.IsPortable); + Assert.Equal(Path.Combine(local, AppPaths.ProductFolderName), paths.DataRoot); + Assert.True(string.Equals(library, paths.PortableSyncRoot, StringComparison.OrdinalIgnoreCase), $"sync root {paths.PortableSyncRoot}"); + } + finally + { + Registry.CurrentUser.DeleteSubKeyTree(hive, throwOnMissingSubKey: false); + } + } }