mirror of https://github.com/dahall/Vanara.git
Minor vd tweaks
parent
0ce814c2ca
commit
13af3cd273
|
@ -155,7 +155,7 @@ namespace Vanara.PInvoke
|
|||
CREATE_VIRTUAL_DISK_FLAG_PRESERVE_PARENT_CHANGE_TRACKING_STATE = 0x20,
|
||||
|
||||
/// <summary>
|
||||
/// When creating a VHD Set from source, don't copy the data in the original backing store, but intsead use the file as is. If this flag is not
|
||||
/// When creating a VHD Set from source, don't copy the data in the original backing store, but instead use the file as is. If this flag is not
|
||||
/// specified and a source file is passed to CreateVirtualDisk for a VHDSet file, the data in the source file is copied. If this flag is set the data
|
||||
/// is moved. The name of the file may change.
|
||||
/// </summary>
|
||||
|
|
|
@ -103,6 +103,8 @@ namespace Vanara.IO
|
|||
/// <summary>The logical sector size of the physical disk.</summary>
|
||||
public uint LogicalSectorSize => GetInformation<uint>(GET_VIRTUAL_DISK_INFO_VERSION.GET_VIRTUAL_DISK_INFO_PHYSICAL_DISK);
|
||||
|
||||
/// <summary>Gets the metadata associated with this virtual disk. Currently on VHDX files support metadata.</summary>
|
||||
/// <value>The metadata.</value>
|
||||
public VirtualDiskMetadata Metadata => metadata ?? (metadata = new VirtualDiskMetadata(this));
|
||||
|
||||
/// <summary>
|
||||
|
@ -777,7 +779,7 @@ namespace Vanara.IO
|
|||
if (parent.Handle.IsClosed) throw new InvalidOperationException("Virtual disk not valid.");
|
||||
uint count = 0;
|
||||
var err = EnumerateVirtualDiskMetadata(parent.Handle, ref count, IntPtr.Zero);
|
||||
if (err != Win32Error.ERROR_MORE_DATA) err.ThrowIfFailed();
|
||||
if (err != Win32Error.ERROR_MORE_DATA && err != Win32Error.ERROR_INSUFFICIENT_BUFFER) err.ThrowIfFailed();
|
||||
if (count == 0) return new Guid[0];
|
||||
var mem = new SafeCoTaskMemHandle(Marshal.SizeOf(typeof(Guid)) * (int)count);
|
||||
EnumerateVirtualDiskMetadata(parent.Handle, ref count, (IntPtr)mem).ThrowIfFailed();
|
||||
|
@ -802,7 +804,7 @@ namespace Vanara.IO
|
|||
if (parent.Handle.IsClosed) throw new InvalidOperationException("Virtual disk not valid.");
|
||||
uint sz = 0;
|
||||
var err = GetVirtualDiskMetadata(parent.Handle, key, ref sz, SafeCoTaskMemHandle.Null);
|
||||
if (err != Win32Error.ERROR_MORE_DATA) err.ThrowIfFailed();
|
||||
if (err != Win32Error.ERROR_MORE_DATA && err != Win32Error.ERROR_INSUFFICIENT_BUFFER) err.ThrowIfFailed();
|
||||
var ret = new SafeCoTaskMemHandle((int)sz);
|
||||
GetVirtualDiskMetadata(parent.Handle, key, ref sz, ret).ThrowIfFailed();
|
||||
return ret;
|
||||
|
@ -899,5 +901,10 @@ namespace Vanara.IO
|
|||
|
||||
private IEnumerable<KeyValuePair<Guid, SafeCoTaskMemHandle>> GetEnum() => Keys.Select(k => new KeyValuePair<Guid, SafeCoTaskMemHandle>(k, this[k]));
|
||||
}
|
||||
|
||||
private class VirtualDiskSnapshot
|
||||
{
|
||||
private Guid id;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -235,7 +235,16 @@ namespace Vanara.IO.Tests
|
|||
{
|
||||
using (var vhd = VirtualDisk.Create(lfn, sz))
|
||||
{
|
||||
vhd.Attach(true, true, false, GetWorldFullFileSecurity());
|
||||
var count = 0;
|
||||
Assert.That(() => count = vhd.Metadata.Count, Throws.Nothing);
|
||||
|
||||
// Try get and set
|
||||
var guid = Guid.NewGuid();
|
||||
Assert.That(() => vhd.Metadata.Add(guid, new SafeCoTaskMemHandle("Testing")), Throws.Nothing);
|
||||
Assert.That(vhd.Metadata.Count, Is.EqualTo(count + 1));
|
||||
Assert.That(vhd.Metadata.ContainsKey(Guid.NewGuid()), Is.False);
|
||||
Assert.That(vhd.Metadata.TryGetValue(guid, out var mem), Is.True);
|
||||
Assert.That(mem.ToString(-1), Is.EqualTo("Testing"));
|
||||
|
||||
// Try enumerate and get
|
||||
foreach (var mkv in vhd.Metadata)
|
||||
|
@ -245,14 +254,10 @@ namespace Vanara.IO.Tests
|
|||
TestContext.WriteLine($"{mkv.Key}={mkv.Value.Size}b:{mkv.Value.ToString(-1)}");
|
||||
}
|
||||
|
||||
// Try set and remove
|
||||
var guid = Guid.NewGuid();
|
||||
Assert.That(() => vhd.Metadata.Add(guid, new SafeCoTaskMemHandle("Testing")), Throws.Nothing);
|
||||
Assert.That(vhd.Metadata.TryGetValue(Guid.NewGuid(), out SafeCoTaskMemHandle mem), Is.False);
|
||||
Assert.That(vhd.Metadata.TryGetValue(guid, out mem), Is.True);
|
||||
Assert.That(mem.ToString(-1), Is.EqualTo("Testing"));
|
||||
// Try remove
|
||||
Assert.That(vhd.Metadata.Remove(guid), Is.True);
|
||||
Assert.That(vhd.Metadata.TryGetValue(guid, out mem), Is.False);
|
||||
Assert.That(vhd.Metadata.Count, Is.EqualTo(count));
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
|
Loading…
Reference in New Issue