Skip to content

Commit

Permalink
add usb drive to .net standard (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev authored Apr 15, 2024
1 parent d4d5071 commit 12501d8
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions SIL.Core.Desktop/UsbDrive/UsbDriveInfo.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#if !NETSTANDARD
// Copyright (c) 2009-2016 SIL International
// Copyright (c) 2009-2016 SIL International
// This software is licensed under the MIT License (http://opensource.org/licenses/MIT)

using System;
using System.Collections.Generic;
using System.IO;
using SIL.PlatformUtilities;
using SIL.UsbDrive.Linux;
using SIL.UsbDrive.Windows;
using System.Linq;

namespace SIL.UsbDrive
{
Expand All @@ -21,6 +18,26 @@ public interface IUsbDriveInfo
ulong AvailableFreeSpace { get; }
}

internal class UsbDriveWrapper : IUsbDriveInfo
{
private DriveInfo _driveInfo;

public UsbDriveWrapper(DriveInfo driveInfo)
{
_driveInfo = driveInfo;
}

public ulong AvailableFreeSpace => (ulong)_driveInfo.AvailableFreeSpace;

public bool IsReady => _driveInfo.IsReady;

public DirectoryInfo RootDirectory => _driveInfo.RootDirectory;

public ulong TotalSize => (ulong) _driveInfo.TotalSize;

public string VolumeLabel => _driveInfo.VolumeLabel;
}

/// <summary>
/// This class allows tests to set up pretend usb drives, in order to test situations like
/// 1) no drives found
Expand Down Expand Up @@ -103,8 +120,12 @@ public abstract ulong TotalSize

public static List<IUsbDriveInfo> GetDrives()
{
if (Platform.IsWindows)
return UsbDriveInfoWindows.GetDrives();
#if NETSTANDARD
return DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Removable)
.Select(d => new UsbDriveWrapper(d)).ToList<IUsbDriveInfo>();
#else
if (PlatformUtilities.Platform.IsWindows)
return Windows.UsbDriveInfoWindows.GetDrives();

// Using SIL.UsbDrive on Linux/Mono results in NDesk spinning up a thread that
// continues until NDesk Bus is closed. Failure to close the thread results in a
Expand All @@ -115,11 +136,11 @@ public static List<IUsbDriveInfo> GetDrives()
// Ubuntu 12.04 uses udisks. HAL use is deprecated.
// Ubuntu 14.04 can use udisks or udisks2.
// Ubuntu 16.04 uses udisks2.
return UsbDriveInfoUDisks2.IsUDisks2Available
? UsbDriveInfoUDisks2.GetDrives()
: UsbDriveInfoUDisks.GetDrives();
return Linux.UsbDriveInfoUDisks2.IsUDisks2Available
? Linux.UsbDriveInfoUDisks2.GetDrives()
: Linux.UsbDriveInfoUDisks.GetDrives();
#endif
}
}

}
#endif
}

0 comments on commit 12501d8

Please sign in to comment.