Example #1
0
        /// <summary>
        /// Gets the locations of the parent file.
        /// </summary>
        /// <param name="fileLocator">The file locator to use.</param>
        /// <returns>Array of candidate file locations.</returns>
        private string[] GetParentLocations(FileLocator fileLocator)
        {
            if (!NeedsParent)
            {
                throw new InvalidOperationException("Only differencing disks contain parent locations");
            }

            if (fileLocator == null)
            {
                // Use working directory by default
                fileLocator = new LocalFileLocator(string.Empty);
            }

            List <string> paths = new List <string>();

            ParentLocator locator = _metadata.ParentLocator;
            string        value;

            if (locator.Entries.TryGetValue("relative_path", out value))
            {
                paths.Add(fileLocator.ResolveRelativePath(value));
            }

            if (locator.Entries.TryGetValue("volume_path", out value))
            {
                paths.Add(value);
            }

            if (locator.Entries.TryGetValue("absolute_win32_path", out value))
            {
                paths.Add(value);
            }

            return(paths.ToArray());
        }
Example #2
0
        /// <summary>
        /// Create a new differencing disk.
        /// </summary>
        /// <param name="path">The path (or URI) for the disk to create</param>
        /// <returns>The newly created disk</returns>
        public override VirtualDisk CreateDifferencingDisk(string path)
        {
            FileLocator   locator = new LocalFileLocator(Path.GetDirectoryName(path));
            DiskImageFile file    = _files[0].First.CreateDifferencing(locator, Path.GetFileName(path));

            return(new Disk(file, Ownership.Dispose));
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the Disk class.
        /// </summary>
        /// <param name="path">The path to the disk.</param>
        /// <param name="access">The access requested to the disk.</param>
        public Disk(string path, FileAccess access)
        {
            FileShare share   = access == FileAccess.Read ? FileShare.Read : FileShare.None;
            var       locator = new LocalFileLocator(string.Empty);

            _diskImage = new DiskImageFile(locator.Open(path, FileMode.Open, access, share), Ownership.Dispose);
        }
        /// <summary>
        /// Writes the stream contents to a file.
        /// </summary>
        /// <param name="outputFile">The file to write to.</param>
        public void Build(string outputFile)
        {
            var locator = new LocalFileLocator(string.Empty);

            using (Stream destStream = locator.Open(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                Build(destStream);
            }
        }
Example #5
0
        private void WriteFileData(BuilderContext context)
        {
            Stream outStream = context.RawStream;

            bool disposeSource = false;

            try
            {
                if (_source == null)
                {
                    var locator = new LocalFileLocator(string.Empty);
                    _source       = locator.Open(_sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    disposeSource = true;
                }

                if (_source.Position != 0)
                {
                    _source.Position = 0;
                }

                long startPos      = outStream.Position;
                int  bufferedBytes = StreamUtilities.ReadMaximum(_source, context.IoBuffer, 0, context.DataBlockSize);

                if (bufferedBytes < context.DataBlockSize)
                {
                    // Fragment - less than one complete block of data
                    _inode.StartBlock = 0xFFFFFFFF;

                    _inode.FragmentKey = context.WriteFragment(bufferedBytes, out _inode.FragmentOffset);
                    _inode.FileSize    = (uint)bufferedBytes;
                }
                else
                {
                    // At least one full block, no fragments used
                    _inode.FragmentKey = 0xFFFFFFFF;

                    _lengths          = new List <uint>();
                    _inode.StartBlock = (uint)startPos;
                    _inode.FileSize   = bufferedBytes;
                    while (bufferedBytes > 0)
                    {
                        _lengths.Add(context.WriteDataBlock(context.IoBuffer, 0, bufferedBytes));
                        bufferedBytes    = StreamUtilities.ReadMaximum(_source, context.IoBuffer, 0, context.DataBlockSize);
                        _inode.FileSize += (uint)bufferedBytes;
                    }
                }
            }
            finally
            {
                if (disposeSource)
                {
                    _source.Dispose();
                }
            }
        }
 internal Stream OpenStream()
 {
     if (_contentData != null)
     {
         return(new MemoryStream(_contentData, false));
     }
     if (_contentPath != null)
     {
         var locator = new LocalFileLocator(string.Empty);
         return(locator.Open(_contentPath, FileMode.Open, FileAccess.Read, FileShare.Read));
     }
     return(_contentStream);
 }
Example #7
0
        /// <summary>
        /// Creates a new VHD differencing disk file.
        /// </summary>
        /// <param name="path">The path to the new disk file</param>
        /// <param name="parentPath">The path to the parent disk file</param>
        /// <returns>An object that accesses the new file as a Disk</returns>
        public static Disk InitializeDifferencing(string path, string parentPath)
        {
            LocalFileLocator parentLocator  = new LocalFileLocator(Path.GetDirectoryName(parentPath));
            string           parentFileName = Path.GetFileName(parentPath);

            DiskImageFile newFile;

            using (DiskImageFile parent = new DiskImageFile(parentLocator, parentFileName, FileAccess.Read))
            {
                LocalFileLocator locator = new LocalFileLocator(Path.GetDirectoryName(path));
                newFile = parent.CreateDifferencing(locator, Path.GetFileName(path));
            }
            return(new Disk(newFile, Ownership.Dispose, parentLocator, parentFileName));
        }
Example #8
0
        /// <summary>
        /// Directs trace output to a file as well as storing internally.
        /// </summary>
        /// <param name="path">The path to the file.</param>
        /// <remarks>Call this method after tracing has started to migrate to a new
        /// output file.</remarks>
        public void WriteToFile(string path)
        {
            if (_fileOut != null)
            {
                _fileOut.Dispose();
                _fileOut = null;
            }

            if (!string.IsNullOrEmpty(path))
            {
                var locator = new LocalFileLocator(string.Empty);
                _fileOut = new StreamWriter(locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite));
            }
        }
        /// <summary>
        /// Gets the locations of the parent file.
        /// </summary>
        /// <param name="fileLocator">The file locator to use</param>
        /// <returns>Array of candidate file locations</returns>
        private string[] GetParentLocations(FileLocator fileLocator)
        {
            if (!NeedsParent)
            {
                throw new InvalidOperationException("Only differencing disks contain parent locations");
            }

            if (fileLocator == null)
            {
                // Use working directory by default
                fileLocator = new LocalFileLocator(string.Empty);
            }

            List <string> absPaths = new List <string>(8);
            List <string> relPaths = new List <string>(8);

            foreach (var pl in _dynamicHeader.ParentLocators)
            {
                if (pl.PlatformCode == ParentLocator.PlatformCodeWindowsAbsoluteUnicode ||
                    pl.PlatformCode == ParentLocator.PlatformCodeWindowsRelativeUnicode)
                {
                    _fileStream.Position = pl.PlatformDataOffset;
                    byte[] buffer      = Utilities.ReadFully(_fileStream, pl.PlatformDataLength);
                    string locationVal = Encoding.Unicode.GetString(buffer);

                    if (pl.PlatformCode == ParentLocator.PlatformCodeWindowsAbsoluteUnicode)
                    {
                        absPaths.Add(locationVal);
                    }
                    else
                    {
                        relPaths.Add(fileLocator.ResolveRelativePath(locationVal));
                    }
                }
            }

            // Order the paths to put absolute paths first
            List <string> paths = new List <string>(absPaths.Count + relPaths.Count + 1);

            paths.AddRange(absPaths);
            paths.AddRange(relPaths);

            // As a back-up, try to infer from the parent name...
            if (paths.Count == 0)
            {
                paths.Add(fileLocator.ResolveRelativePath(_dynamicHeader.ParentUnicodeName));
            }

            return(paths.ToArray());
        }
Example #10
0
        /// <summary>
        /// Gets the locations of the parent file.
        /// </summary>
        /// <param name="fileLocator">The file locator to use</param>
        /// <returns>Array of candidate file locations</returns>
        private string[] GetParentLocations(FileLocator fileLocator)
        {
            if (!NeedsParent)
            {
                throw new InvalidOperationException("Only differencing disks contain parent locations");
            }

            if (fileLocator == null)
            {
                // Use working directory by default
                fileLocator = new LocalFileLocator(string.Empty);
            }

            throw new NotImplementedException();
        }
Example #11
0
        /// <summary>
        /// Creates a new virtual disk that is a linked clone of an existing disk.
        /// </summary>
        /// <param name="path">The path to the new disk</param>
        /// <param name="type">The type of the new disk</param>
        /// <param name="parent">The disk to clone</param>
        /// <returns>The new virtual disk</returns>
        public static DiskImageFile InitializeDifferencing(string path, DiskCreateType type, string parent)
        {
            if (type != DiskCreateType.MonolithicSparse && type != DiskCreateType.TwoGbMaxExtentSparse && type != DiskCreateType.VmfsSparse)
            {
                throw new ArgumentException("Differencing disks must be sparse", "type");
            }

            using (DiskImageFile parentFile = new DiskImageFile(parent, FileAccess.Read))
            {
                DescriptorFile baseDescriptor = CreateDifferencingDiskDescriptor(type, parentFile, parent);

                FileLocator locator = new LocalFileLocator(Path.GetDirectoryName(path));
                return(DoInitialize(locator, Path.GetFileName(path), parentFile.Capacity, type, baseDescriptor));
            }
        }
Example #12
0
        /// <summary>
        /// Creates a new (empty) registry hive.
        /// </summary>
        /// <param name="path">The file to create the new hive in.</param>
        /// <returns>The new hive.</returns>
        public static RegistryHive Create(string path)
        {
            var locator = new LocalFileLocator(string.Empty);

            return(Create(locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None), Ownership.Dispose));
        }
Example #13
0
 /// <summary>
 /// Create a new differencing disk.
 /// </summary>
 /// <param name="path">The path (or URI) for the disk to create</param>
 /// <returns>The newly created disk</returns>
 public override VirtualDisk CreateDifferencingDisk(string path)
 {
     FileLocator locator = new LocalFileLocator(Path.GetDirectoryName(path));
     DiskImageFile file = _files[0].First.CreateDifferencing(locator, Path.GetFileName(path));
     return new Disk(file, Ownership.Dispose);
 }
Example #14
0
        /// <summary>
        /// Creates a new VHDX differencing disk file.
        /// </summary>
        /// <param name="path">The path to the new disk file</param>
        /// <param name="parentPath">The path to the parent disk file</param>
        /// <returns>An object that accesses the new file as a Disk</returns>
        public static Disk InitializeDifferencing(string path, string parentPath)
        {
            LocalFileLocator parentLocator = new LocalFileLocator(Path.GetDirectoryName(parentPath));
            string parentFileName = Path.GetFileName(parentPath);

            DiskImageFile newFile;
            using (DiskImageFile parent = new DiskImageFile(parentLocator, parentFileName, FileAccess.Read))
            {
                LocalFileLocator locator = new LocalFileLocator(Path.GetDirectoryName(path));
                newFile = parent.CreateDifferencing(locator, Path.GetFileName(path));
            }

            return new Disk(newFile, Ownership.Dispose, parentLocator, parentFileName);
        }
Example #15
0
        /// <summary>
        /// Creates a new virtual disk that is a linked clone of an existing disk.
        /// </summary>
        /// <param name="path">The path to the new disk</param>
        /// <param name="type">The type of the new disk</param>
        /// <param name="parent">The disk to clone</param>
        /// <returns>The new virtual disk</returns>
        public static DiskImageFile InitializeDifferencing(string path, DiskCreateType type, string parent)
        {
            if (type != DiskCreateType.MonolithicSparse && type != DiskCreateType.TwoGbMaxExtentSparse && type != DiskCreateType.VmfsSparse)
            {
                throw new ArgumentException("Differencing disks must be sparse", "type");
            }

            using (DiskImageFile parentFile = new DiskImageFile(parent, FileAccess.Read))
            {
                DescriptorFile baseDescriptor = CreateDifferencingDiskDescriptor(type, parentFile, parent);

                FileLocator locator = new LocalFileLocator(Path.GetDirectoryName(path));
                return DoInitialize(locator, Path.GetFileName(path), parentFile.Capacity, type, baseDescriptor);
            }
        }
Example #16
0
 /// <summary>
 /// Creates a new virtual disk at the specified path.
 /// </summary>
 /// <param name="path">The name of the VMDK to create.</param>
 /// <param name="parameters">The desired parameters for the new disk.</param>
 /// <returns>The newly created disk image</returns>
 public static DiskImageFile Initialize(string path, DiskParameters parameters)
 {
     FileLocator locator = new LocalFileLocator(Path.GetDirectoryName(path));
     return Initialize(locator, Path.GetFileName(path), parameters);
 }
Example #17
0
        /// <summary>
        /// Gets the locations of the parent file.
        /// </summary>
        /// <param name="fileLocator">The file locator to use</param>
        /// <returns>Array of candidate file locations</returns>
        private string[] GetParentLocations(FileLocator fileLocator)
        {
            if (!NeedsParent)
            {
                throw new InvalidOperationException("Only differencing disks contain parent locations");
            }

            if (fileLocator == null)
            {
                // Use working directory by default
                fileLocator = new LocalFileLocator(string.Empty);
            }

            throw new NotImplementedException();
        }
Example #18
0
        /// <summary>
        /// Creates a new virtual disk at the specified path.
        /// </summary>
        /// <param name="path">The name of the VMDK to create.</param>
        /// <param name="parameters">The desired parameters for the new disk.</param>
        /// <returns>The newly created disk image</returns>
        public static DiskImageFile Initialize(string path, DiskParameters parameters)
        {
            FileLocator locator = new LocalFileLocator(Path.GetDirectoryName(path));

            return(Initialize(locator, Path.GetFileName(path), parameters));
        }
Example #19
0
        public override VirtualDisk OpenDisk(string path, FileAccess access)
        {
            var locator = new LocalFileLocator(string.Empty);

            return(OpenDisk(locator, path, access));
        }
        /// <summary>
        /// Gets the locations of the parent file.
        /// </summary>
        /// <param name="fileLocator">The file locator to use</param>
        /// <returns>Array of candidate file locations</returns>
        private string[] GetParentLocations(FileLocator fileLocator)
        {
            if (!NeedsParent)
            {
                throw new InvalidOperationException("Only differencing disks contain parent locations");
            }

            if (fileLocator == null)
            {
                // Use working directory by default
                fileLocator = new LocalFileLocator(string.Empty);
            }

            List<string> paths = new List<string>();

            ParentLocator locator = _metadata.ParentLocator;
            string value;

            if (locator.Entries.TryGetValue("relative_path", out value))
            {
                paths.Add(fileLocator.ResolveRelativePath(value));
            }

            if (locator.Entries.TryGetValue("volume_path", out value))
            {
                paths.Add(value);
            }

            if (locator.Entries.TryGetValue("absolute_win32_path", out value))
            {
                paths.Add(value);
            }

            return paths.ToArray();
        }
Example #21
0
        /// <summary>
        /// Gets the locations of the parent file.
        /// </summary>
        /// <param name="fileLocator">The file locator to use.</param>
        /// <returns>Array of candidate file locations.</returns>
        private string[] GetParentLocations(FileLocator fileLocator)
        {
            if (!NeedsParent)
            {
                throw new InvalidOperationException("Only differencing disks contain parent locations");
            }

            if (fileLocator == null)
            {
                // Use working directory by default
                fileLocator = new LocalFileLocator(string.Empty);
            }

            List<string> absPaths = new List<string>(8);
            List<string> relPaths = new List<string>(8);
            foreach (var pl in _dynamicHeader.ParentLocators)
            {
                if (pl.PlatformCode == ParentLocator.PlatformCodeWindowsAbsoluteUnicode
                    || pl.PlatformCode == ParentLocator.PlatformCodeWindowsRelativeUnicode)
                {
                    _fileStream.Position = pl.PlatformDataOffset;
                    byte[] buffer = Utilities.ReadFully(_fileStream, pl.PlatformDataLength);
                    string locationVal = Encoding.Unicode.GetString(buffer);

                    if (pl.PlatformCode == ParentLocator.PlatformCodeWindowsAbsoluteUnicode)
                    {
                        absPaths.Add(locationVal);
                    }
                    else
                    {
                        relPaths.Add(fileLocator.ResolveRelativePath(locationVal));
                    }
                }
            }

            // Order the paths to put absolute paths first
            List<string> paths = new List<string>(absPaths.Count + relPaths.Count + 1);
            paths.AddRange(absPaths);
            paths.AddRange(relPaths);

            // As a back-up, try to infer from the parent name...
            if (paths.Count == 0)
            {
                paths.Add(fileLocator.ResolveRelativePath(_dynamicHeader.ParentUnicodeName));
            }

            return paths.ToArray();
        }