Skip to content

Commit aaa3e25

Browse files
committed
Async vhdx image initializations
1 parent 2a40dfb commit aaa3e25

File tree

2 files changed

+335
-4
lines changed

2 files changed

+335
-4
lines changed

Library/DiscUtils.Vhdx/Disk.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,22 @@ public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long c
453453
return new Disk(DiskImageFile.InitializeDynamic(stream, ownsStream, capacity, geometry, blockSize), Ownership.Dispose);
454454
}
455455

456+
/// <summary>
457+
/// Initializes a stream as a dynamically-sized VHDX file.
458+
/// </summary>
459+
/// <param name="stream">The stream to initialize.</param>
460+
/// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
461+
/// <param name="capacity">The desired capacity of the new disk.</param>
462+
/// <param name="geometry"></param>
463+
/// <param name="blockSize">The size of each block (unit of allocation).</param>
464+
/// <param name="cancellationToken"></param>
465+
/// <returns>An object that accesses the stream as a VHDX file.</returns>
466+
public static async ValueTask<Disk> InitializeDynamicAsync(Stream stream, Ownership ownsStream, long capacity, Geometry? geometry, long? blockSize, CancellationToken cancellationToken)
467+
{
468+
var diskImageFile = await DiskImageFile.InitializeDynamicAsync(stream, ownsStream, capacity, geometry, blockSize, cancellationToken).ConfigureAwait(false);
469+
return new Disk(diskImageFile, Ownership.Dispose);
470+
}
471+
456472
/// <summary>
457473
/// Creates a new VHDX differencing disk file.
458474
/// </summary>
@@ -484,6 +500,28 @@ public static Disk InitializeDifferencing(string path, string parentPath, bool u
484500
return new Disk(newFile, Ownership.Dispose, parentLocator, parentFileName);
485501
}
486502

503+
/// <summary>
504+
/// Creates a new VHDX differencing disk file.
505+
/// </summary>
506+
/// <param name="path">The path to the new disk file.</param>
507+
/// <param name="parentPath">The path to the parent disk file.</param>
508+
/// <param name="cancellationToken"></param>
509+
/// <returns>An object that accesses the new file as a Disk.</returns>
510+
public static async ValueTask<Disk> InitializeDifferencingAsync(string path, string parentPath, CancellationToken cancellationToken)
511+
{
512+
var parentLocator = new LocalFileLocator(Path.GetDirectoryName(parentPath), useAsync: true);
513+
var parentFileName = Path.GetFileName(parentPath);
514+
515+
DiskImageFile newFile;
516+
using (var parent = new DiskImageFile(parentLocator, parentFileName, FileAccess.Read))
517+
{
518+
var locator = new LocalFileLocator(Path.GetDirectoryName(path), useAsync: true);
519+
newFile = await parent.CreateDifferencingAsync(locator, Path.GetFileName(path), cancellationToken).ConfigureAwait(false);
520+
}
521+
522+
return new Disk(newFile, Ownership.Dispose, parentLocator, parentFileName);
523+
}
524+
487525
/// <summary>
488526
/// Initializes a stream as a differencing disk VHDX file.
489527
/// </summary>
@@ -509,6 +547,33 @@ public static Disk InitializeDifferencing(
509547
return new Disk(file, Ownership.Dispose, parent, ownsParent);
510548
}
511549

550+
/// <summary>
551+
/// Initializes a stream as a differencing disk VHDX file.
552+
/// </summary>
553+
/// <param name="stream">The stream to initialize.</param>
554+
/// <param name="ownsStream">Indicates if the new instance controls the lifetime of the <paramref name="stream"/>.</param>
555+
/// <param name="parent">The disk this file is a different from.</param>
556+
/// <param name="ownsParent">Indicates if the new instance controls the lifetime of the <paramref name="parent"/> file.</param>
557+
/// <param name="parentAbsolutePath">The full path to the parent disk.</param>
558+
/// <param name="parentRelativePath">The relative path from the new disk to the parent disk.</param>
559+
/// <param name="parentModificationTime">The time the parent disk's file was last modified (from file system).</param>
560+
/// <param name="cancellationToken"></param>
561+
/// <returns>An object that accesses the stream as a VHDX file.</returns>
562+
public static async ValueTask<Disk> InitializeDifferencingAsync(
563+
Stream stream,
564+
Ownership ownsStream,
565+
DiskImageFile parent,
566+
Ownership ownsParent,
567+
string parentAbsolutePath,
568+
string parentRelativePath,
569+
DateTime parentModificationTime,
570+
CancellationToken cancellationToken)
571+
{
572+
var file = await DiskImageFile.InitializeDifferencingAsync(stream, ownsStream, parent, parentAbsolutePath,
573+
parentRelativePath, parentModificationTime, cancellationToken).ConfigureAwait(false);
574+
return new Disk(file, Ownership.Dispose, parent, ownsParent);
575+
}
576+
512577
/// <summary>
513578
/// Create a new differencing disk, possibly within an existing disk.
514579
/// </summary>

0 commit comments

Comments
 (0)