/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Services/Storage/StorageDevicesService.cs
84 строки
3 KB
Yair
Code Quality: Prevent crash when system drive is inaccessible during drive enumeration
05 май 2026, 17:26
05 май 2026, 17:26
d66213e
Код
Авторство
О чём код?
// Copyright (c) Files Community // Licensed under the MIT License. using Microsoft.Extensions.Logging; using OwlCore.Storage.System.IO; using System.IO; using Windows.Storage; namespace Files.App.Services { public sealed class RemovableDrivesService : IRemovableDrivesService { public IStorageDeviceWatcher CreateWatcher() { return new WindowsStorageDeviceWatcher(); } public async IAsyncEnumerable<IFolder> GetDrivesAsync() { var list = DriveInfo.GetDrives(); var pCloudDrivePath = App.AppModel.PCloudDrivePath; foreach (var drive in list) { if (!drive.IsReady) continue; var driveLabel = DriveHelpers.GetExtendedDriveLabel(drive); // Filter out cloud drives // We don't want cloud drives to appear in the plain "Drives" sections. if (driveLabel.Equals("Google Drive") || drive.Name.Equals(pCloudDrivePath)) continue; var res = await FilesystemTasks.Wrap(() => StorageFolder.GetFolderFromPathAsync(drive.Name).AsTask()); if (res.ErrorCode is FileSystemStatusCode.Unauthorized) { App.Logger.LogWarning($"{res.ErrorCode}: Attempting to add the device, {drive.Name}," + " failed at the StorageFolder initialization step. This device will be ignored."); continue; } else if (!res) { App.Logger.LogWarning($"{res.ErrorCode}: Attempting to add the device, {drive.Name}," + " failed at the StorageFolder initialization step. This device will be ignored."); continue; } using var thumbnail = await DriveHelpers.GetThumbnailAsync(res.Result); var type = DriveHelpers.GetDriveType(drive); var label = DriveHelpers.GetExtendedDriveLabel(drive); var driveItem = await DriveItem.CreateFromPropertiesAsync(res.Result, drive.Name.TrimEnd('\\'), label, type, thumbnail); App.Logger.LogInformation($"Drive added: {driveItem.Path}, {driveItem.Type}"); yield return driveItem; } } public Task<IFolder?> GetPrimaryDriveAsync() { var cDrivePath = $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\"; if (!Directory.Exists(cDrivePath)) { App.Logger.LogWarning($"Primary system drive '{cDrivePath}' could not be found."); return Task.FromResult<IFolder?>(null); } return Task.FromResult<IFolder?>(new SystemFolder(cDrivePath)); } public async Task UpdateDrivePropertiesAsync(IFolder drive) { var rootModified = await FilesystemTasks.Wrap(() => StorageFolder.GetFolderFromPathAsync(drive.Id).AsTask()); if (rootModified && drive is DriveItem matchingDriveEjected) { _ = MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => { matchingDriveEjected.Root = rootModified.Result; matchingDriveEjected.Text = rootModified.Result.DisplayName; return matchingDriveEjected.UpdatePropertiesAsync(); }); } } } }