Skip to content

Commit 5f5d723

Browse files
Report OneDrive size on 23H2.
1 parent d079525 commit 5f5d723

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/Files.App/Utils/Storage/Helpers/DriveHelpers.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22
// Licensed under the MIT License.
33

44
using DiscUtils.Udf;
5+
using Files.App.Services.SizeProvider;
56
using Microsoft.Management.Infrastructure;
7+
using Microsoft.Win32;
8+
using System.Runtime.InteropServices;
69
using Windows.Devices.Enumeration;
710
using Windows.Devices.Portable;
811
using Windows.Storage;
912
using Windows.Storage.FileProperties;
13+
using Windows.Storage.Provider;
1014
using Windows.Win32;
1115
using Windows.Win32.Foundation;
16+
using WinRT;
1217

1318
namespace Files.App.Utils.Storage
1419
{
1520
public static class DriveHelpers
1621
{
22+
private static readonly Guid IID_IStorageProviderStatusUISourceFactory = new Guid("12e46b74-4e5a-58d1-a62f-0376e8ee7dd8");
23+
1724
public static async void EjectDeviceAsync(string path)
1825
{
1926
await ContextMenu.InvokeVerb("eject", path);
@@ -168,5 +175,60 @@ public static async Task<StorageItemThumbnail> GetThumbnailAsync(StorageFolder f
168175
=> (StorageItemThumbnail)await FilesystemTasks.Wrap(()
169176
=> folder.GetThumbnailAsync(ThumbnailMode.SingleItem, 40, ThumbnailOptions.UseCurrentScale).AsTask()
170177
);
178+
179+
public static async Task<(bool Success, ulong Capacity, ulong Used)> GetSyncRootQuotaAsync(string path)
180+
{
181+
Windows.Storage.StorageFolder folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(path);
182+
StorageProviderSyncRootInfo? syncRootInfo = null;
183+
184+
try
185+
{
186+
syncRootInfo = StorageProviderSyncRootManager.GetSyncRootInformationForFolder(folder);
187+
}
188+
catch
189+
{
190+
return (false, 0, 0);
191+
}
192+
193+
RegistryKey? key;
194+
if ((key = Registry.LocalMachine.OpenSubKey($"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SyncRootManager\\{syncRootInfo.Id}")) is null)
195+
{
196+
return (false, 0, 0);
197+
}
198+
199+
using (key)
200+
{
201+
if (key.GetValue("StorageProviderStatusUISourceFactory") is string statusUIclass)
202+
{
203+
StorageProviderStatusUI statusUI;
204+
205+
unsafe
206+
{
207+
if (PInvoke.CoCreateInstance(Guid.Parse(statusUIclass), null, Windows.Win32.System.Com.CLSCTX.CLSCTX_LOCAL_SERVER, IID_IStorageProviderStatusUISourceFactory, out void* statusUISourceFactoryAbi) != 0)
208+
{
209+
return (false, 0, 0);
210+
}
211+
212+
// CsWinRT wrappers won't work.
213+
// TODO: look to replace MarshalString with MarshalString.Pinnable?
214+
215+
nint statusUISourceAbi = 0;
216+
nint syncRootIdHstring = MarshalString.FromManaged(syncRootInfo.Id);
217+
nint statusUIAbi = 0;
218+
ExceptionHelpers.ThrowExceptionForHR(((delegate* unmanaged[MemberFunction]<IntPtr, IntPtr, IntPtr*, int>)(*(IntPtr*)((nint)(*(IntPtr*)statusUISourceFactoryAbi) + (nint)6 * (nint)sizeof(delegate* unmanaged[Stdcall]<IntPtr, IntPtr, IntPtr*, int>))))((nint)statusUISourceFactoryAbi, syncRootIdHstring, &statusUISourceAbi));
219+
ExceptionHelpers.ThrowExceptionForHR(((delegate* unmanaged[MemberFunction]<nint, nint*, int>)(*(IntPtr*)((nint)(*(IntPtr*)statusUISourceAbi) + (nint)6 * (nint)sizeof(delegate* unmanaged[Stdcall]<nint, nint*, int>))))(statusUISourceAbi, &statusUIAbi));
220+
statusUI = StorageProviderStatusUI.FromAbi(statusUIAbi);
221+
Marshal.Release(statusUISourceAbi);
222+
Marshal.Release((nint)statusUISourceFactoryAbi);
223+
MarshalString.DisposeAbi(statusUISourceAbi);
224+
}
225+
return (true, statusUI.QuotaUI.QuotaTotalInBytes, statusUI.QuotaUI.QuotaUsedInBytes);
226+
}
227+
else
228+
{
229+
return (false, 0, 0);
230+
}
231+
}
232+
}
171233
}
172234
}

src/Files.App/ViewModels/Properties/Items/DriveProperties.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public override void GetBaseProperties()
4545
public async override Task GetSpecialPropertiesAsync()
4646
{
4747
ViewModel.ItemAttributesVisibility = false;
48+
4849
var item = await FilesystemTasks.Wrap(() => DriveHelpers.GetRootFromPathAsync(Drive.Path));
4950
BaseStorageFolder diskRoot = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(Drive.Path, item));
5051

@@ -77,6 +78,15 @@ public async override Task GetSpecialPropertiesAsync()
7778
return;
7879
}
7980

81+
var syncRootStatus = await DriveHelpers.GetSyncRootQuotaAsync(Drive.Path);
82+
if (syncRootStatus.Success)
83+
{
84+
ViewModel.DriveCapacityValue = syncRootStatus.Capacity;
85+
ViewModel.DriveUsedSpaceValue = syncRootStatus.Used;
86+
ViewModel.DriveFreeSpaceValue = syncRootStatus.Capacity - syncRootStatus.Used;
87+
return;
88+
}
89+
8090
try
8191
{
8292
string freeSpace = "System.FreeSpace";

0 commit comments

Comments
 (0)