/// <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="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> public static VirtualDisk OpenDisk(string path, FileAccess access, string user, string password) { Uri uri = PathToUri(path); VirtualDisk result = null; VirtualDiskTransport transport; if (!DiskTransports.TryGetValue(uri.Scheme.ToUpperInvariant(), out transport)) { throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Unable to parse path '{0}'", path), path); } try { transport.Connect(uri, user, password); if (transport.IsRawDisk) { result = transport.OpenDisk(access); } else { string extension = Path.GetExtension(uri.AbsolutePath).ToUpperInvariant(); if (extension.StartsWith(".", StringComparison.Ordinal)) { extension = extension.Substring(1); } VirtualDiskFactory factory; if (ExtensionMap.TryGetValue(extension, out factory)) { result = factory.OpenDisk(transport.GetFileLocator(), transport.GetFileName(), access); } } if (result != null) { result._transport = transport; transport = null; } return(result); } finally { if (transport != null) { transport.Dispose(); } } }
/// <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="capacity">The capacity of the new disk</param> /// <param name="geometry">The geometry of the new disk (or null).</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> /// <param name="parameters">Untyped parameters controlling the creation process (TBD)</param> /// <returns>The newly created disk</returns> public static VirtualDisk CreateDisk(string type, string variant, string path, long capacity, Geometry geometry, string user, string password, Dictionary <string, string> parameters) { Uri uri = PathToUri(path); VirtualDisk result = null; VirtualDiskTransport transport; if (!DiskTransports.TryGetValue(uri.Scheme.ToUpperInvariant(), out transport)) { throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Unable to parse path '{0}'", path), path); } 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), capacity, geometry, parameters ?? new Dictionary <string, string>()); } if (result != null) { result._transport = transport; transport = null; } return(result); } finally { if (transport != null) { transport.Dispose(); } } }
/// <summary> /// Locates VirtualDiskFactory factories attributed with VirtualDiskFactoryAttribute, and types marked with VirtualDiskTransportAttribute, that are able to work with Virtual Disk types. /// </summary> /// <param name="assembly">An assembly to scan</param> public static void RegisterVirtualDiskTypes(Assembly assembly) { foreach (Type type in assembly.GetTypes()) { VirtualDiskFactoryAttribute diskFactoryAttribute = (VirtualDiskFactoryAttribute)ReflectionHelper.GetCustomAttribute(type, typeof(VirtualDiskFactoryAttribute), false); if (diskFactoryAttribute != null) { VirtualDiskFactory factory = (VirtualDiskFactory)Activator.CreateInstance(type); TypeMap.Add(diskFactoryAttribute.Type, factory); foreach (string extension in diskFactoryAttribute.FileExtensions) { ExtensionMap.Add(extension.ToUpperInvariant(), factory); } } VirtualDiskTransportAttribute diskTransportAttribute = ReflectionHelper.GetCustomAttribute(type, typeof(VirtualDiskTransportAttribute), false) as VirtualDiskTransportAttribute; if (diskTransportAttribute != null) { DiskTransports.Add(diskTransportAttribute.Scheme.ToUpperInvariant(), type); } } }