diff --git a/PInvoke/VirtDisk/VirtDisk.cs b/PInvoke/VirtDisk/VirtDisk.cs index 2ed1c7c4..27b4eda6 100644 --- a/PInvoke/VirtDisk/VirtDisk.cs +++ b/PInvoke/VirtDisk/VirtDisk.cs @@ -155,7 +155,7 @@ namespace Vanara.PInvoke CREATE_VIRTUAL_DISK_FLAG_PRESERVE_PARENT_CHANGE_TRACKING_STATE = 0x20, /// - /// 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. /// diff --git a/System/VirtualDisk.cs b/System/VirtualDisk.cs index 9292a3f7..18630f31 100644 --- a/System/VirtualDisk.cs +++ b/System/VirtualDisk.cs @@ -103,6 +103,8 @@ namespace Vanara.IO /// The logical sector size of the physical disk. public uint LogicalSectorSize => GetInformation(GET_VIRTUAL_DISK_INFO_VERSION.GET_VIRTUAL_DISK_INFO_PHYSICAL_DISK); + /// Gets the metadata associated with this virtual disk. Currently on VHDX files support metadata. + /// The metadata. public VirtualDiskMetadata Metadata => metadata ?? (metadata = new VirtualDiskMetadata(this)); /// @@ -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> GetEnum() => Keys.Select(k => new KeyValuePair(k, this[k])); } + + private class VirtualDiskSnapshot + { + private Guid id; + } } } \ No newline at end of file diff --git a/UnitTests/System/VirtualDiskTests.cs b/UnitTests/System/VirtualDiskTests.cs index 8b9636d8..9679c4bb 100644 --- a/UnitTests/System/VirtualDiskTests.cs +++ b/UnitTests/System/VirtualDiskTests.cs @@ -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