fix(portable): SharePoint and Teams libraries OneDrive syncs count as sync roots
A library synced with its Sync button lies beside the account's UserFolder (%USERPROFILE%\<tenant>\<site> - <library>), so a portable copy unpacked there kept .\data inside a synchronised folder. OneDriveFolders.SyncRoots also reads HKCU\Software\SyncEngines\Providers\OneDrive\*\MountPoint, where OneDrive registers every folder it syncs (the location Office reads), and takes the registry root as a parameter for tests on a scratch key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+88
-22
No files matched your search
@@ -4,43 +4,62 @@ using Titano.Core.IO;
|
||||
|
||||
namespace Titano.Core.Windows.Shell;
|
||||
|
||||
/// <summary>Folders synchronised by OneDrive for the current user (personal and work/school accounts).</summary>
|
||||
/// <summary>
|
||||
/// Folders synchronised by OneDrive for the current user: personal and work/school accounts, and the SharePoint and Teams
|
||||
/// libraries synced with their Sync button.
|
||||
/// </summary>
|
||||
public static class OneDriveFolders
|
||||
{
|
||||
private const string AccountsKey = @"Software\Microsoft\OneDrive\Accounts";
|
||||
|
||||
/// <summary>
|
||||
/// 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 <c>MountPoint</c>.
|
||||
/// </summary>
|
||||
private const string MountPointsKey = @"Software\SyncEngines\Providers\OneDrive";
|
||||
|
||||
private static readonly string[] EnvironmentVariables = ["OneDrive", "OneDriveConsumer", "OneDriveCommercial"];
|
||||
|
||||
/// <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). An account key that cannot be read is
|
||||
/// skipped: the programs start all the same.
|
||||
/// Sync roots from the OneDrive environment variables, <c>HKCU\Software\Microsoft\OneDrive\Accounts\*\UserFolder</c>
|
||||
/// and <c>HKCU\Software\SyncEngines\Providers\OneDrive\*\MountPoint</c>, as full paths without a trailing separator,
|
||||
/// distinct (case-insensitive). A key that cannot be read is skipped: the programs start all the same.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<string> SyncRoots()
|
||||
public static IReadOnlyList<string> SyncRoots() => SyncRoots(Registry.CurrentUser);
|
||||
|
||||
/// <summary><see cref="SyncRoots()"/> with the registry keys read under <paramref name="user"/> instead of the current user's hive.</summary>
|
||||
public static IReadOnlyList<string> SyncRoots(RegistryKey user)
|
||||
{
|
||||
var roots = new List<string>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <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) => DataLocation.SyncRootOf(path, syncRoots);
|
||||
|
||||
/// <summary>Adds the folder that value <paramref name="valueName"/> of each subkey of <paramref name="keyPath"/> names.</summary>
|
||||
private static void AddFolders(List<string> 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<string> roots, string? candidate)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(candidate) || !Path.IsPathFullyQualified(candidate)) return;
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
using Microsoft.Win32;
|
||||
using Titano.Core.Windows.Shell;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Windows.Tests.Shell;
|
||||
|
||||
/// <summary>The OneDrive folders of the user as Windows reports them. Serial: a test sets a process environment variable.</summary>
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
[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%\<tenant>, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user