Skip to content

Commit 95e202e

Browse files
committed
fix issue where Google Drive appears under drives
Add Debug.WriteLine logging to track when App.AppModel.GoogleDrivePath is written and read. Add logic to RemoteDrivesService.GetDrivesAsync() that filters out Google Drive by calling GoogleDriveCloudDetector.GetGoogleDriveProvidersFromRegistryAsync(). Adjust GoogleDriveCloudDetector.GetGoogleDriveProvidersFromRegistryAsync() so it can be called from RemoteDrivesService.GetDrivesAsync(), and so it can be told to not append 'My Drive'.
1 parent 97e4156 commit 95e202e

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/Files.App/Services/Storage/StorageDevicesService.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,20 @@ public async IAsyncEnumerable<ILocatableFolder> GetDrivesAsync()
2222
var googleDrivePath = App.AppModel.GoogleDrivePath;
2323
var pCloudDrivePath = App.AppModel.PCloudDrivePath;
2424

25+
#if DEBUG
26+
Debug.WriteLine($"In RDS.GDA: googleDrivePath: {googleDrivePath}");
27+
#endif
28+
2529
foreach (var drive in list)
2630
{
31+
var shouldSkip = false;
32+
await foreach (var cloudProvider in GoogleDriveCloudDetector.GetGoogleDriveProvidersFromRegistryAsync(false))
33+
{
34+
if (cloudProvider.SyncFolder.Equals(drive.Name))
35+
shouldSkip = true;
36+
}
37+
if (shouldSkip)
38+
continue;
2739
var res = await FilesystemTasks.Wrap(() => StorageFolder.GetFolderFromPathAsync(drive.Name).AsTask());
2840
if (res.ErrorCode is FileSystemStatusCode.Unauthorized)
2941
{
@@ -43,6 +55,9 @@ public async IAsyncEnumerable<ILocatableFolder> GetDrivesAsync()
4355
var label = DriveHelpers.GetExtendedDriveLabel(drive);
4456
var driveItem = await DriveItem.CreateFromPropertiesAsync(res.Result, drive.Name.TrimEnd('\\'), label, type, thumbnail);
4557

58+
#if DEBUG
59+
Debug.WriteLine($"In RDS.GDA: drive.Name: {drive.Name}");
60+
#endif
4661
// Don't add here because Google Drive is already displayed under cloud drives
4762
if (drive.Name == googleDrivePath || drive.Name == pCloudDrivePath)
4863
continue;

src/Files.App/Utils/Cloud/Detector/GoogleDriveCloudDetector.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(Path.Combine(a
7272
App.AppModel.GoogleDrivePath = path;
7373

7474
#if DEBUG
75+
Debug.WriteLine($"In GDCD in roots table: App.AppModel.GoogleDrivePath being set to: {path}");
7576
Debug.WriteLine("YIELD RETURNING from GoogleDriveCloudDetector#GetProviders (roots): ");
7677
Debug.WriteLine($"name=Google Drive ({title}); path={path}");
7778
#endif
@@ -106,6 +107,7 @@ await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(Path.Combine(a
106107
var iconFile = await GetGoogleDriveIconFileAsync();
107108

108109
#if DEBUG
110+
Debug.WriteLine($"In GDCD in media table: App.AppModel.GoogleDrivePath being set to: {path}");
109111
Debug.WriteLine("YIELD RETURNING from GoogleDriveCloudDetector#GetProviders (media): ");
110112
Debug.WriteLine($"name={title}; path={path}");
111113
#endif
@@ -160,7 +162,7 @@ private async Task Inspect(SqliteConnection database, string sqlCommand, string
160162
#endif
161163
}
162164

163-
private JsonDocument? GetGoogleDriveRegValJson()
165+
private static JsonDocument? GetGoogleDriveRegValJson()
164166
{
165167
// This will be null if the key name is not found.
166168
using var googleDriveRegKey = Registry.CurrentUser.OpenSubKey(_googleDriveRegKeyName);
@@ -192,7 +194,7 @@ private async Task Inspect(SqliteConnection database, string sqlCommand, string
192194
return googleDriveRegValueJson;
193195
}
194196

195-
private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegistryAsync()
197+
public static async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegistryAsync(bool addMyDriveToPath = true)
196198
{
197199
var googleDriveRegValJson = GetGoogleDriveRegValJson();
198200

@@ -228,13 +230,16 @@ private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegist
228230
if (path is null)
229231
continue;
230232

231-
if (!AddMyDriveToPathAndValidate(ref path))
233+
if (!AddMyDriveToPathAndValidate(ref path, addMyDriveToPath))
232234
{
233235
_logger.LogWarning($"Validation failed for {path} (media)");
234236
continue;
235237
}
236238

237239
App.AppModel.GoogleDrivePath = path;
240+
#if DEBUG
241+
Debug.WriteLine($"In GDCD in registry: App.AppModel.GoogleDrivePath being set to: {path}");
242+
#endif
238243

239244
var iconFile = await GetGoogleDriveIconFileAsync();
240245

@@ -247,7 +252,7 @@ private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegist
247252
}
248253
}
249254

250-
private async Task<StorageFile?> GetGoogleDriveIconFileAsync()
255+
private static async Task<StorageFile?> GetGoogleDriveIconFileAsync()
251256
{
252257
var programFilesEnvVar = Environment.GetEnvironmentVariable("ProgramFiles");
253258

@@ -259,7 +264,7 @@ private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegist
259264
return await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(iconPath).AsTask());
260265
}
261266

262-
private bool AddMyDriveToPathAndValidate(ref string path)
267+
private static bool AddMyDriveToPathAndValidate(ref string path, bool addMyDrive = true)
263268
{
264269
// If Google Drive is mounted as a drive, then the path found in the registry will be
265270
// *just* the drive letter (e.g. just "G" as opposed to "G:\"), and therefore must be
@@ -280,27 +285,31 @@ private bool AddMyDriveToPathAndValidate(ref string path)
280285
path = temp.RootDirectory.Name;
281286
}
282287

283-
// If `path` contains a shortcut named "My Drive", store its target in `shellFolderBaseFirst`.
284-
// This happens when "My Drive syncing options" is set to "Mirror files".
285-
// TODO: Avoid to use Vanara (#15000)
286-
using var shellFolderBase = ShellFolderExtensions.GetShellItemFromPathOrPIDL(path) as ShellFolder;
287-
var shellFolderBaseFirst = Environment.ExpandEnvironmentVariables((
288-
shellFolderBase?.FirstOrDefault(si =>
289-
si.Name?.Equals("My Drive") ?? false) as ShellLink)?.TargetPath
290-
?? string.Empty);
288+
if (addMyDrive)
289+
{
290+
// If `path` contains a shortcut named "My Drive", store its target in `shellFolderBaseFirst`.
291+
// This happens when "My Drive syncing options" is set to "Mirror files".
292+
// TODO: Avoid to use Vanara (#15000)
293+
using var shellFolderBase = ShellFolderExtensions.GetShellItemFromPathOrPIDL(path) as ShellFolder;
294+
var shellFolderBaseFirst = Environment.ExpandEnvironmentVariables((
295+
shellFolderBase?.FirstOrDefault(si =>
296+
si.Name?.Equals("My Drive") ?? false) as ShellLink)?.TargetPath
297+
?? string.Empty);
291298

292299
#if DEBUG
293-
Debug.WriteLine("INVALID PATHS LOGGER");
294-
shellFolderBase?.ForEach(si => Debug.WriteLine(si.Name));
300+
Debug.WriteLine("INVALID PATHS LOGGER");
301+
shellFolderBase?.ForEach(si => Debug.WriteLine(si.Name));
295302
#endif
296303

297-
if (!string.IsNullOrEmpty(shellFolderBaseFirst))
298-
{
299-
path = shellFolderBaseFirst;
300-
return true;
304+
if (!string.IsNullOrEmpty(shellFolderBaseFirst))
305+
{
306+
path = shellFolderBaseFirst;
307+
return true;
308+
}
309+
310+
path = Path.Combine(path, "My Drive");
301311
}
302312

303-
path = Path.Combine(path, "My Drive");
304313
if (Directory.Exists(path))
305314
return true;
306315
_logger.LogWarning($"Invalid Google Drive mount point path: {path}");

0 commit comments

Comments
 (0)