Skip to content

Commit

Permalink
Moved method for finding disk space closer to where it is used.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <[email protected]>
  • Loading branch information
kc284 committed Nov 20, 2023
1 parent 5ab65cc commit 1f666fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 42 deletions.
27 changes: 15 additions & 12 deletions XenAdmin/Commands/ExportVMCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@

using System;
using System.Collections.Generic;
using XenAPI;
using XenAdmin.Dialogs;
using XenAdmin.Core;
using System.IO;
using System.Windows.Forms;
using XenAdmin.Actions;
using XenCenterLib;
using System.IO;
using XenAdmin.Core;
using XenAdmin.Dialogs;
using XenAPI;


namespace XenAdmin.Commands
Expand Down Expand Up @@ -111,23 +110,27 @@ protected override void RunCore(SelectedItemCollection selection)

// CA-12975: Warn the user if the export operation does not have enough disk space to
// complete. This is an approximation only.
Win32.DiskSpaceInfo diskSpaceInfo = null;

ulong freeSpace;
bool isFAT;
try
{
diskSpaceInfo = Win32.GetDiskSpaceInfo(filename);
string driveLetter = Path.GetPathRoot(filename).TrimEnd('\\');
var o = new System.Management.ManagementObject($"Win32_LogicalDisk.DeviceID=\"{driveLetter}\"");

string fsType = o.Properties["FileSystem"].Value.ToString();
isFAT = fsType == "FAT" || fsType == "FAT32";

freeSpace = ulong.Parse(o.Properties["FreeSpace"].Value.ToString());
}
catch (Exception exn)
{
log.Warn(exn, exn);
}

if (diskSpaceInfo == null)
{
// Could not determine free disk space. Carry on regardless.
break;
}

ulong freeSpace = diskSpaceInfo.FreeBytesAvailable;
ulong neededSpace = vm.GetTotalSize();
ulong spaceLeft = 100 * Util.BINARY_MEGA; // We want the user to be left with some disk space afterwards

Expand All @@ -137,7 +140,7 @@ protected override void RunCore(SelectedItemCollection selection)
);

(Func<bool> check, string msg) c2 = (
() => diskSpaceInfo.IsFAT && neededSpace > 4 * Util.BINARY_GIGA - 1,
() => isFAT && neededSpace > 4 * Util.BINARY_GIGA - 1,
string.Format(Messages.CONFIRM_EXPORT_FAT, Util.DiskSizeString(neededSpace), Util.DiskSizeString(4 * Util.BINARY_GIGA), vm.Name())
);

Expand Down
30 changes: 0 additions & 30 deletions XenCenterLib/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,36 +508,6 @@ public enum ScrollState
[DllImport("Kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool GetDiskFreeSpaceEx(string lpszPath, ref long lpFreeBytesAvailable, ref long lpTotalNumberOfBytes, ref long lpTotalNumberOfFreeBytes);

/// <summary>
/// Will return null if the disk parameters could not be determined.
/// </summary>
/// <param name="path">An absolute path</param>
/// <exception cref="Exception">May be thrown</exception>>
public static DiskSpaceInfo GetDiskSpaceInfo(string path)
{
string DriveLetter = Path.GetPathRoot(path).TrimEnd('\\');
var o = new System.Management.ManagementObject($"Win32_LogicalDisk.DeviceID=\"{DriveLetter}\"");
string fsType = o.Properties["FileSystem"].Value.ToString();
bool isFAT = (fsType == "FAT" || fsType == "FAT32");
UInt64 freeBytes = UInt64.Parse(o.Properties["FreeSpace"].Value.ToString());
UInt64 totalBytes = UInt64.Parse(o.Properties["Size"].Value.ToString());
return new DiskSpaceInfo(freeBytes, totalBytes, isFAT);
}

public class DiskSpaceInfo
{
public readonly UInt64 FreeBytesAvailable;
public readonly UInt64 TotalBytes;
public readonly bool IsFAT;

public DiskSpaceInfo(UInt64 freeBytesAvailable, UInt64 totalBytes, bool isFAT)
{
FreeBytesAvailable = freeBytesAvailable;
TotalBytes = totalBytes;
IsFAT = isFAT;
}
}

public const int CP_NOCLOSE_BUTTON = 0x200;

public const int GWL_WNDPROC = -4;
Expand Down

0 comments on commit 1f666fc

Please sign in to comment.