/// <summary> /// Disposes of underlying resources. /// </summary> /// <param name="disposing"><c>true</c> if running inside Dispose(), indicating /// graceful cleanup of all managed objects should be performed, or <c>false</c> /// if running inside destructor.</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (_transport != null) { _transport.Dispose(); } _transport = null; } }
/// <summary> /// Create a new virtual disk. /// </summary> /// <param name="type">The type of disk to create (see <see cref="SupportedDiskTypes"/>)</param> /// <param name="variant">The variant of the type to create (see <see cref="GetSupportedDiskVariants"/>)</param> /// <param name="path">The path (or URI) for the disk to create</param> /// <param name="diskParameters">Parameters controlling the capacity, geometry, etc of the new disk.</param> /// <param name="user">The user identity to use when accessing the <c>path</c> (or null)</param> /// <param name="password">The password to use when accessing the <c>path</c> (or null)</param> /// <returns>The newly created disk</returns> public static VirtualDisk CreateDisk(string type, string variant, string path, VirtualDiskParameters diskParameters, string user, string password) { Uri uri = PathToUri(path); VirtualDisk result = null; Type transportType; if (!DiskTransports.TryGetValue(uri.Scheme.ToUpperInvariant(), out transportType)) { throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Unable to parse path '{0}'", path), path); } VirtualDiskTransport transport = (VirtualDiskTransport)Activator.CreateInstance(transportType); try { transport.Connect(uri, user, password); if (transport.IsRawDisk) { result = transport.OpenDisk(FileAccess.ReadWrite); } else { VirtualDiskFactory factory = TypeMap[type]; result = factory.CreateDisk(transport.GetFileLocator(), variant.ToLowerInvariant(), Utilities.GetFileFromPath(path), diskParameters); } if (result != null) { result._transport = transport; transport = null; } return(result); } finally { if (transport != null) { transport.Dispose(); } } }
/// <summary> /// Disposes of underlying resources. /// </summary> /// <param name="disposing"><c>true</c> if running inside Dispose(), indicating /// graceful cleanup of all managed objects should be performed, or <c>false</c> /// if running inside destructor.</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (_transport != null) { _transport.Dispose(); } _transport = null; } }
/// <summary> /// Opens an existing virtual disk. /// </summary> /// <param name="path">The path of the virtual disk to open, can be a URI</param> /// <param name="forceType">Force the detected disk type (<c>null</c> to detect)</param> /// <param name="access">The desired access to the disk</param> /// <param name="user">The user name to use for authentication (if necessary)</param> /// <param name="password">The password to use for authentication (if necessary)</param> /// <returns>The Virtual Disk, or <c>null</c> if an unknown disk format</returns> /// <remarks> /// The detected disk type can be forced by specifying a known disk type: /// RAW, VHD, VMDK, etc. /// </remarks> public static VirtualDisk OpenDisk(string path, string forceType, FileAccess access, string user, string password) { Uri uri = PathToUri(path); VirtualDisk result = null; Type transportType; if (!DiskTransports.TryGetValue(uri.Scheme.ToUpperInvariant(), out transportType)) { throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Unable to parse path '{0}'", path), path); } VirtualDiskTransport transport = (VirtualDiskTransport)Activator.CreateInstance(transportType); try { transport.Connect(uri, user, password); if (transport.IsRawDisk) { result = transport.OpenDisk(access); } else { bool foundFactory; VirtualDiskFactory factory; if (!string.IsNullOrEmpty(forceType)) { foundFactory = TypeMap.TryGetValue(forceType, out factory); } else { string extension = Path.GetExtension(uri.AbsolutePath).ToUpperInvariant(); if (extension.StartsWith(".", StringComparison.Ordinal)) { extension = extension.Substring(1); } foundFactory = ExtensionMap.TryGetValue(extension, out factory); } if (foundFactory) { result = factory.OpenDisk(transport.GetFileLocator(), transport.GetFileName(), access); } } if (result != null) { result._transport = transport; transport = null; } return(result); } finally { if (transport != null) { transport.Dispose(); } } }