Esempio n. 1
0
        private void CreateNewDisk()
        {
            string[] typeAndVariant = Type.Split('-');
            if (typeAndVariant.Length < 1 || typeAndVariant.Length > 2)
            {
                WriteError(new ErrorRecord(
                               new ArgumentException("Invalid Type of disk"),
                               "BadDiskType",
                               ErrorCategory.InvalidArgument,
                               null));
                return;
            }

            long size;

            if (!DiscUtils.Common.Utilities.TryParseDiskSize(Size, out size))
            {
                WriteError(new ErrorRecord(
                               new ArgumentException("Unable to parse the disk size"),
                               "BadDiskSize",
                               ErrorCategory.InvalidArgument,
                               null));
                return;
            }


            string type    = typeAndVariant[0];
            string variant = typeAndVariant.Length > 1 ? typeAndVariant[1] : null;

            string   child;
            PSObject parentObj = ResolveNewDiskPath(out child);

            VirtualDisk disk = null;

            if (parentObj.BaseObject is DirectoryInfo)
            {
                string path = Path.Combine(((DirectoryInfo)parentObj.BaseObject).FullName, child);
                using (VirtualDisk realDisk = VirtualDisk.CreateDisk(type, variant, path, size, null, null)) { }
                disk = new OnDemandVirtualDisk(path, FileAccess.ReadWrite);
            }
            else if (parentObj.BaseObject is DiscDirectoryInfo)
            {
                DiscDirectoryInfo ddi  = (DiscDirectoryInfo)parentObj.BaseObject;
                string            path = Path.Combine(ddi.FullName, child);
                using (VirtualDisk realDisk = VirtualDisk.CreateDisk(ddi.FileSystem, type, variant, path, size, null, null)) { }
                disk = new OnDemandVirtualDisk(ddi.FileSystem, path, FileAccess.ReadWrite);
            }
            else
            {
                WriteError(new ErrorRecord(
                               new DirectoryNotFoundException("Cannot create a virtual disk in that location"),
                               "BadDiskLocation",
                               ErrorCategory.InvalidArgument,
                               null));
                return;
            }

            WriteObject(disk, false);
        }
Esempio n. 2
0
        private void CreateDiffDisk()
        {
            string   child;
            PSObject parentObj = ResolveNewDiskPath(out child);

            PSObject baseDiskObj = SessionState.InvokeProvider.Item.Get(new string[] { BaseDisk }, false, true)[0];

            VirtualDisk baseDisk = null;

            try
            {
                if (baseDiskObj.BaseObject is FileInfo)
                {
                    baseDisk = VirtualDisk.OpenDisk(((FileInfo)baseDiskObj.BaseObject).FullName, FileAccess.Read);
                }
                else if (baseDiskObj.BaseObject is DiscFileInfo)
                {
                    DiscFileInfo dfi = (DiscFileInfo)baseDiskObj.BaseObject;
                    baseDisk = VirtualDisk.OpenDisk(dfi.FileSystem, dfi.FullName, FileAccess.Read);
                }
                else
                {
                    WriteError(new ErrorRecord(
                                   new FileNotFoundException("The file specified by the BaseDisk parameter doesn't exist"),
                                   "BadBaseDiskLocation",
                                   ErrorCategory.InvalidArgument,
                                   null));
                    return;
                }

                VirtualDisk newDisk = null;
                if (parentObj.BaseObject is DirectoryInfo)
                {
                    string path = Path.Combine(((DirectoryInfo)parentObj.BaseObject).FullName, child);
                    using (baseDisk.CreateDifferencingDisk(path)) { }
                    newDisk = new OnDemandVirtualDisk(path, FileAccess.ReadWrite);
                }
                else if (parentObj.BaseObject is DiscDirectoryInfo)
                {
                    DiscDirectoryInfo ddi  = (DiscDirectoryInfo)parentObj.BaseObject;
                    string            path = Path.Combine(ddi.FullName, child);
                    using (baseDisk.CreateDifferencingDisk(ddi.FileSystem, path)) { }
                    newDisk = new OnDemandVirtualDisk(ddi.FileSystem, path, FileAccess.ReadWrite);
                }
                else
                {
                    WriteError(new ErrorRecord(
                                   new DirectoryNotFoundException("Cannot create a virtual disk in that location"),
                                   "BadDiskLocation",
                                   ErrorCategory.InvalidArgument,
                                   null));
                    return;
                }

                WriteObject(newDisk, false);
            }
            finally
            {
                if (baseDisk != null)
                {
                    baseDisk.Dispose();
                }
            }
        }