Esempio n. 1
0
        private object FindItemByPath(string path, bool preferFs, bool readOnly)
        {
            FileAccess fileAccess = readOnly ? FileAccess.Read : FileAccess.ReadWrite;
            string diskPath;
            string relPath;

            int mountSepIdx = path.IndexOf('!');
            if (mountSepIdx < 0)
            {
                diskPath = path;
                relPath = "";
            }
            else
            {
                diskPath = path.Substring(0, mountSepIdx);
                relPath = path.Substring(mountSepIdx + 1);
            }

            VirtualDisk disk = Disk;
            if( disk == null )
            {
                OnDemandVirtualDisk odvd = new OnDemandVirtualDisk(Utilities.DenormalizePath(diskPath), fileAccess);
                if (odvd.IsValid)
                {
                    disk = odvd;
                    ShowSlowDiskWarning();
                }
                else
                {
                    return null;
                }
            }

            List<string> pathElems = new List<string>(relPath.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries));

            if (pathElems.Count == 0)
            {
                return disk;
            }

            VolumeInfo volInfo = null;
            VolumeManager volMgr = DriveInfo != null ? DriveInfo.VolumeManager : new VolumeManager(disk);
            LogicalVolumeInfo[] volumes = volMgr.GetLogicalVolumes();
            string volNumStr = pathElems[0].StartsWith("Volume", StringComparison.OrdinalIgnoreCase) ? pathElems[0].Substring(6) : null;
            int volNum;
            if (int.TryParse(volNumStr, out volNum) || volNum < 0 || volNum >= volumes.Length)
            {
                volInfo = volumes[volNum];
            }
            else
            {
                volInfo = volMgr.GetVolume(Utilities.DenormalizePath(pathElems[0]));
            }
            pathElems.RemoveAt(0);
            if (volInfo == null || (pathElems.Count == 0 && !preferFs))
            {
                return volInfo;
            }

            bool disposeFs;
            DiscFileSystem fs = GetFileSystem(volInfo, out disposeFs);
            try
            {
                if (fs == null)
                {
                    return null;
                }

                // Special marker in the path - disambiguates the root folder from the volume
                // containing it.  By this point it's done it's job (we didn't return volInfo),
                // so we just remove it.
                if (pathElems.Count > 0 && pathElems[0] == "$Root")
                {
                    pathElems.RemoveAt(0);
                }

                string fsPath = string.Join(@"\", pathElems.ToArray());
                if (fs.DirectoryExists(fsPath))
                {
                    return fs.GetDirectoryInfo(fsPath);
                }
                else if (fs.FileExists(fsPath))
                {
                    return fs.GetFileInfo(fsPath);
                }
            }
            finally
            {
                if (disposeFs && fs != null)
                {
                    fs.Dispose();
                }
            }

            return null;
        }
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();
                }
            }
        }
Esempio n. 3
0
        private object FindItemByPath(string path, bool preferFs, bool readOnly)
        {
            FileAccess fileAccess = readOnly ? FileAccess.Read : FileAccess.ReadWrite;
            string     diskPath;
            string     relPath;

            int mountSepIdx = path.IndexOf('!');

            if (mountSepIdx < 0)
            {
                diskPath = path;
                relPath  = "";
            }
            else
            {
                diskPath = path.Substring(0, mountSepIdx);
                relPath  = path.Substring(mountSepIdx + 1);
            }

            VirtualDisk disk = Disk;

            if (disk == null)
            {
                OnDemandVirtualDisk odvd = new OnDemandVirtualDisk(Utilities.DenormalizePath(diskPath), fileAccess);
                if (odvd.IsValid)
                {
                    disk = odvd;
                    ShowSlowDiskWarning();
                }
                else
                {
                    return(null);
                }
            }

            List <string> pathElems = new List <string>(relPath.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries));

            if (pathElems.Count == 0)
            {
                return(disk);
            }


            VolumeInfo    volInfo = null;
            VolumeManager volMgr  = DriveInfo != null ? DriveInfo.VolumeManager : new VolumeManager(disk);

            LogicalVolumeInfo[] volumes = volMgr.GetLogicalVolumes();
            string volNumStr            = pathElems[0].StartsWith("Volume", StringComparison.OrdinalIgnoreCase) ? pathElems[0].Substring(6) : null;
            int    volNum;

            if (int.TryParse(volNumStr, out volNum) || volNum < 0 || volNum >= volumes.Length)
            {
                volInfo = volumes[volNum];
            }
            else
            {
                volInfo = volMgr.GetVolume(Utilities.DenormalizePath(pathElems[0]));
            }
            pathElems.RemoveAt(0);
            if (volInfo == null || (pathElems.Count == 0 && !preferFs))
            {
                return(volInfo);
            }


            bool           disposeFs;
            DiscFileSystem fs = GetFileSystem(volInfo, out disposeFs);

            try
            {
                if (fs == null)
                {
                    return(null);
                }

                // Special marker in the path - disambiguates the root folder from the volume
                // containing it.  By this point it's done it's job (we didn't return volInfo),
                // so we just remove it.
                if (pathElems.Count > 0 && pathElems[0] == "$Root")
                {
                    pathElems.RemoveAt(0);
                }

                string fsPath = string.Join(@"\", pathElems.ToArray());
                if (fs.DirectoryExists(fsPath))
                {
                    return(fs.GetDirectoryInfo(fsPath));
                }
                else if (fs.FileExists(fsPath))
                {
                    return(fs.GetFileInfo(fsPath));
                }
            }
            finally
            {
                if (disposeFs && fs != null)
                {
                    fs.Dispose();
                }
            }

            return(null);
        }
Esempio n. 4
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);
        }