Exemple #1
0
 /// <summary>
 /// Creates a new instance from an existing file, differencing disks are supported.
 /// </summary>
 /// <param name="path">The path to the disk image</param>
 public Disk(string path)
 {
     DiskImageFile file = new DiskImageFile(path, FileAccess.ReadWrite);
     _files = new List<Tuple<DiskImageFile, Ownership>>();
     _files.Add(new Tuple<DiskImageFile, Ownership>(file, Ownership.Dispose));
     ResolveFileChain();
 }
        public void GetParentLocations()
        {
            MemoryStream baseStream = new MemoryStream();
            MemoryStream diffStream = new MemoryStream();
            using (DiskImageFile baseFile = DiskImageFile.InitializeDynamic(baseStream, Ownership.Dispose, 16 * 1024L * 1024 * 1024))
            {
                // Write some data - exposes bug if mis-calculating where to write data
                using (DiskImageFile diffFile = DiskImageFile.InitializeDifferencing(diffStream, Ownership.None, baseFile, @"C:\TEMP\Base.vhd", @".\Base.vhd", new DateTime(2007, 12, 31)))
                {
                    Disk disk = new Disk(new DiskImageFile[] { diffFile, baseFile }, Ownership.None);
                    disk.Content.Write(new byte[512], 0, 512);
                }
            }

            using (DiskImageFile diffFile = new DiskImageFile(diffStream))
            {
                // Testing the obsolete method - disable warning
            #pragma warning disable 618
                string[] locations = diffFile.GetParentLocations(@"E:\FOO\");
            #pragma warning restore 618
                Assert.AreEqual(2, locations.Length);
                Assert.AreEqual(@"C:\TEMP\Base.vhd", locations[0]);
                Assert.AreEqual(@"E:\FOO\Base.vhd", locations[1]);
            }

            using (DiskImageFile diffFile = new DiskImageFile(diffStream))
            {
                // Testing the new method - note relative path because diff file initialized without a path
                string[] locations = diffFile.GetParentLocations();
                Assert.AreEqual(2, locations.Length);
                Assert.AreEqual(@"C:\TEMP\Base.vhd", locations[0]);
                Assert.AreEqual(@".\Base.vhd", locations[1]);
            }
        }
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are supported.
 /// </summary>
 /// <param name="path">The path to the disk image</param>
 /// <param name="access">The access requested to the disk</param>
 public Disk(string path, FileAccess access)
 {
     DiskImageFile file = new DiskImageFile(path, access);
     _files = new List<DiscUtils.Tuple<DiskImageFile, Ownership>>();
     _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(file, Ownership.Dispose));
     ResolveFileChain();
 }
        /// <summary>
        /// Initializes a stream as a differencing disk VHD file.
        /// </summary>
        /// <param name="stream">The stream to initialize.</param>
        /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the <paramref name="stream"/>.</param>
        /// <param name="parent">The disk this file is a different from.</param>
        /// <param name="ownsParent">Indicates if the new instance controls the lifetime of the <paramref name="parent"/> file.</param>
        /// <param name="parentAbsolutePath">The full path to the parent disk.</param>
        /// <param name="parentRelativePath">The relative path from the new disk to the parent disk.</param>
        /// <param name="parentModificationTime">The time the parent disk's file was last modified (from file system).</param>
        /// <returns>An object that accesses the stream as a VHD file</returns>
        public static Disk InitializeDifferencing(
            Stream stream,
            Ownership ownsStream,
            DiskImageFile parent,
            Ownership ownsParent,
            string parentAbsolutePath,
            string parentRelativePath,
            DateTime parentModificationTime)
        {
            DiskImageFile file = DiskImageFile.InitializeDifferencing(stream, ownsStream, parent, parentAbsolutePath, parentRelativePath, parentModificationTime);

            return(new Disk(file, Ownership.Dispose, parent, ownsParent));
        }
        /// <summary>
        /// Initializes a stream as a differencing disk VHD file.
        /// </summary>
        /// <param name="stream">The stream to initialize.</param>
        /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
        /// <param name="parent">The disk this file is a different from.</param>
        /// <param name="parentAbsolutePath">The full path to the parent disk.</param>
        /// <param name="parentRelativePath">The relative path from the new disk to the parent disk.</param>
        /// <param name="parentModificationTimeUtc">The time the parent disk's file was last modified (from file system).</param>
        /// <returns>An object that accesses the stream as a VHD file.</returns>
        public static DiskImageFile InitializeDifferencing(
            Stream stream,
            Ownership ownsStream,
            DiskImageFile parent,
            string parentAbsolutePath,
            string parentRelativePath,
            DateTime parentModificationTimeUtc)
        {
            InitializeDifferencingInternal(stream, parent, parentAbsolutePath, parentRelativePath,
                                           parentModificationTimeUtc);

            return(new DiskImageFile(stream, ownsStream));
        }
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are supported.
 /// </summary>
 /// <param name="file">The file containing the disk</param>
 /// <param name="ownsFile">Indicates if the new instance should control the lifetime of the file.</param>
 /// <param name="parentLocator">Object used to locate the parent disk</param>
 /// <param name="parentPath">Path to the parent disk (if required)</param>
 private Disk(DiskImageFile file, Ownership ownsFile, FileLocator parentLocator, string parentPath)
 {
     _files = new List <DiscUtils.Tuple <DiskImageFile, Ownership> >();
     _files.Add(new DiscUtils.Tuple <DiskImageFile, Ownership>(file, ownsFile));
     if (file.NeedsParent)
     {
         _files.Add(
             new DiscUtils.Tuple <DiskImageFile, Ownership>(
                 new DiskImageFile(parentLocator, parentPath, FileAccess.Read),
                 Ownership.Dispose));
         ResolveFileChain();
     }
 }
        private static void InitializeDifferencingInternal(Stream stream, DiskImageFile parent, string parentAbsolutePath, string parentRelativePath, DateTime parentModificationTimeUtc)
        {
            Footer footer = new Footer(parent.Geometry, parent._footer.CurrentSize, FileType.Differencing);

            footer.DataOffset   = 512; // Offset of Dynamic Header
            footer.OriginalSize = parent._footer.OriginalSize;
            footer.UpdateChecksum();
            byte[] footerBlock = new byte[512];
            footer.ToBytes(footerBlock, 0);

            long tableOffset = 512 + 1024; // Footer + Header

            uint blockSize = (parent._dynamicHeader == null) ? DynamicHeader.DefaultBlockSize : parent._dynamicHeader.BlockSize;

            DynamicHeader dynamicHeader = new DynamicHeader(-1, tableOffset, blockSize, footer.CurrentSize);
            int           batSize       = (((dynamicHeader.MaxTableEntries * 4) + Utilities.SectorSize - 1) / Utilities.SectorSize) * Utilities.SectorSize;

            dynamicHeader.ParentUniqueId    = parent.UniqueId;
            dynamicHeader.ParentTimestamp   = parentModificationTimeUtc;
            dynamicHeader.ParentUnicodeName = Utilities.GetFileFromPath(parentAbsolutePath);
            dynamicHeader.ParentLocators[7].PlatformCode       = ParentLocator.PlatformCodeWindowsAbsoluteUnicode;
            dynamicHeader.ParentLocators[7].PlatformDataSpace  = 512;
            dynamicHeader.ParentLocators[7].PlatformDataLength = parentAbsolutePath.Length * 2;
            dynamicHeader.ParentLocators[7].PlatformDataOffset = tableOffset + batSize;
            dynamicHeader.ParentLocators[6].PlatformCode       = ParentLocator.PlatformCodeWindowsRelativeUnicode;
            dynamicHeader.ParentLocators[6].PlatformDataSpace  = 512;
            dynamicHeader.ParentLocators[6].PlatformDataLength = parentRelativePath.Length * 2;
            dynamicHeader.ParentLocators[6].PlatformDataOffset = tableOffset + batSize + 512;
            dynamicHeader.UpdateChecksum();
            byte[] dynamicHeaderBlock = new byte[1024];
            dynamicHeader.ToBytes(dynamicHeaderBlock, 0);

            byte[] platformLocator1 = new byte[512];
            Encoding.Unicode.GetBytes(parentAbsolutePath, 0, parentAbsolutePath.Length, platformLocator1, 0);
            byte[] platformLocator2 = new byte[512];
            Encoding.Unicode.GetBytes(parentRelativePath, 0, parentRelativePath.Length, platformLocator2, 0);

            byte[] bat = new byte[batSize];
            for (int i = 0; i < bat.Length; ++i)
            {
                bat[i] = 0xFF;
            }

            stream.Position = 0;
            stream.Write(footerBlock, 0, 512);
            stream.Write(dynamicHeaderBlock, 0, 1024);
            stream.Write(bat, 0, batSize);
            stream.Write(platformLocator1, 0, 512);
            stream.Write(platformLocator2, 0, 512);
            stream.Write(footerBlock, 0, 512);
        }
Exemple #8
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));
        }
        public void ConstructorFromFiles()
        {
            MemoryStream  baseStream = new MemoryStream();
            DiskImageFile baseFile   = DiskImageFile.InitializeDynamic(baseStream, Ownership.Dispose, 16 * 1024L * 1024 * 1024);

            MemoryStream  childStream = new MemoryStream();
            DiskImageFile childFile   = DiskImageFile.InitializeDifferencing(childStream, Ownership.Dispose, baseFile, @"C:\temp\foo.vhd", @".\foo.vhd", DateTime.Now);

            MemoryStream  grandChildStream = new MemoryStream();
            DiskImageFile grandChildFile   = DiskImageFile.InitializeDifferencing(grandChildStream, Ownership.Dispose, childFile, @"C:\temp\child1.vhd", @".\child1.vhd", DateTime.Now);

            using (Disk disk = new Disk(new DiskImageFile[] { grandChildFile, childFile, baseFile }, Ownership.Dispose))
            {
                Assert.IsNotNull(disk.Content);
            }
        }
        public void InitializeDifferencing()
        {
            MemoryStream  baseStream = new MemoryStream();
            MemoryStream  diffStream = new MemoryStream();
            DiskImageFile baseFile   = DiskImageFile.InitializeDynamic(baseStream, Ownership.Dispose, 16 * 1024L * 1024 * 1024);

            using (Disk disk = Disk.InitializeDifferencing(diffStream, Ownership.None, baseFile, Ownership.Dispose, @"C:\TEMP\Base.vhd", @".\Base.vhd", DateTime.UtcNow))
            {
                Assert.IsNotNull(disk);
                Assert.That(disk.Geometry.Capacity > 15.8 * 1024L * 1024 * 1024 && disk.Geometry.Capacity <= 16 * 1024L * 1024 * 1024);
                Assert.That(disk.Geometry.Capacity == baseFile.Geometry.Capacity);
                Assert.AreEqual(2, new List <VirtualDiskLayer>(disk.Layers).Count);
            }
            Assert.Greater(1 * 1024 * 1024, diffStream.Length);
            diffStream.Dispose();
        }
        public void InitializeDifferencing()
        {
            MemoryStream baseStream = new MemoryStream();
            MemoryStream diffStream = new MemoryStream();

            using (DiskImageFile baseFile = DiskImageFile.InitializeDynamic(baseStream, Ownership.Dispose, 16 * 1024L * 1024 * 1024))
            {
                using (DiskImageFile diffFile = DiskImageFile.InitializeDifferencing(diffStream, Ownership.None, baseFile, @"C:\TEMP\Base.vhd", @".\Base.vhd", new DateTime(2007, 12, 31)))
                {
                    Assert.IsNotNull(diffFile);
                    Assert.That(diffFile.Geometry.Capacity > 15.8 * 1024L * 1024 * 1024 && diffFile.Geometry.Capacity < 16 * 1024L * 1024 * 1024);
                    Assert.IsTrue(diffFile.IsSparse);
                    Assert.AreNotEqual(diffFile.CreationTimestamp, new DateTime(2007, 12, 31));
                }
            }
            Assert.Greater(1 * 1024 * 1024, diffStream.Length);
        }
Exemple #12
0
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are supported.
 /// </summary>
 /// <param name="file">The file containing the disk.</param>
 /// <param name="ownsFile">Indicates if the new instance should control the lifetime of the file.</param>
 /// <param name="parentFile">The file containing the disk's parent.</param>
 /// <param name="ownsParent">Indicates if the new instance should control the lifetime of the parentFile.</param>
 private Disk(DiskImageFile file, Ownership ownsFile, DiskImageFile parentFile, Ownership ownsParent)
 {
     _files = new List <Tuple <DiskImageFile, Ownership> >();
     _files.Add(new Tuple <DiskImageFile, Ownership>(file, ownsFile));
     if (file.NeedsParent)
     {
         _files.Add(new Tuple <DiskImageFile, Ownership>(parentFile, ownsParent));
         ResolveFileChain();
     }
     else
     {
         if (parentFile != null && ownsParent == Ownership.Dispose)
         {
             parentFile.Dispose();
         }
     }
 }
        internal static DiskImageFile InitializeDynamic(FileLocator locator, string path, long capacity, Geometry geometry, long blockSize)
        {
            DiskImageFile result = null;
            Stream        stream = locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None);

            try
            {
                InitializeDynamicInternal(stream, capacity, geometry, blockSize);
                result = new DiskImageFile(locator, path, stream, Ownership.Dispose);
                stream = null;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            return(result);
        }
        public void FooterMissing()
        {
            //
            // Simulates a partial failure extending the file, that the file footer is corrupt - should read start of the file instead.
            //

            Geometry geometry;

            MemoryStream stream = new MemoryStream();
            using (DiskImageFile file = DiskImageFile.InitializeDynamic(stream, Ownership.None, 16 * 1024L * 1024 * 1024))
            {
                geometry = file.Geometry;
            }

            stream.SetLength(stream.Length - 512);

            using (DiskImageFile file = new DiskImageFile(stream))
            {
                Assert.AreEqual(geometry, file.Geometry);
            }
        }
        public void FooterMissing()
        {
            //
            // Simulates a partial failure extending the file, that the file footer is corrupt - should read start of the file instead.
            //

            Geometry geometry;

            MemoryStream stream = new MemoryStream();

            using (DiskImageFile file = DiskImageFile.InitializeDynamic(stream, Ownership.None, 16 * 1024L * 1024 * 1024))
            {
                geometry = file.Geometry;
            }

            stream.SetLength(stream.Length - 512);

            using (DiskImageFile file = new DiskImageFile(stream))
            {
                Assert.AreEqual(geometry, file.Geometry);
            }
        }
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are supported.
 /// </summary>
 /// <param name="file">The file containing the disk</param>
 /// <param name="ownsFile">Indicates if the new instance should control the lifetime of the file.</param>
 /// <param name="parentFile">The file containing the disk's parent</param>
 /// <param name="ownsParent">Indicates if the new instance should control the lifetime of the parentFile</param>
 private Disk(DiskImageFile file, Ownership ownsFile, DiskImageFile parentFile, Ownership ownsParent)
 {
     _files = new List<DiscUtils.Tuple<DiskImageFile, Ownership>>();
     _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(file, ownsFile));
     if (file.NeedsParent)
     {
         _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(parentFile, ownsParent));
         ResolveFileChain();
     }
     else
     {
         if (parentFile != null && ownsParent == Ownership.Dispose)
         {
             parentFile.Dispose();
         }
     }
 }
        /// <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);
        }
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are not supported.
 /// </summary>
 /// <param name="file">The file containing the disk</param>
 /// <param name="ownsFile">Indicates if the new instance should control the lifetime of the file.</param>
 private Disk(DiskImageFile file, Ownership ownsFile)
 {
     _files = new List<DiscUtils.Tuple<DiskImageFile, Ownership>>();
     _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(file, ownsFile));
     ResolveFileChain();
 }
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are supported.
 /// </summary>
 /// <param name="file">The file containing the disk</param>
 /// <param name="ownsFile">Indicates if the new instance should control the lifetime of the file.</param>
 /// <param name="parentLocator">Object used to locate the parent disk</param>
 /// <param name="parentPath">Path to the parent disk (if required)</param>
 private Disk(DiskImageFile file, Ownership ownsFile, FileLocator parentLocator, string parentPath)
 {
     _files = new List<DiscUtils.Tuple<DiskImageFile, Ownership>>();
     _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(file, ownsFile));
     if (file.NeedsParent)
     {
         _files.Add(
             new DiscUtils.Tuple<DiskImageFile, Ownership>(
                 new DiskImageFile(parentLocator, parentPath, FileAccess.Read),
                 Ownership.Dispose));
         ResolveFileChain();
     }
 }
Exemple #20
0
        private static void InitializeDifferencingInternal(Stream stream, DiskImageFile parent, string parentAbsolutePath, string parentRelativePath, DateTime parentModificationTimeUtc)
        {
            Footer footer = new Footer(parent.Geometry, parent._footer.CurrentSize, FileType.Differencing);
            footer.DataOffset = 512; // Offset of Dynamic Header
            footer.OriginalSize = parent._footer.OriginalSize;
            footer.UpdateChecksum();
            byte[] footerBlock = new byte[512];
            footer.ToBytes(footerBlock, 0);

            long tableOffset = 512 + 1024; // Footer + Header

            uint blockSize = (parent._dynamicHeader == null) ? DynamicHeader.DefaultBlockSize : parent._dynamicHeader.BlockSize;

            DynamicHeader dynamicHeader = new DynamicHeader(-1, tableOffset, blockSize, footer.CurrentSize);
            int batSize = (((dynamicHeader.MaxTableEntries * 4) + Utilities.SectorSize - 1) / Utilities.SectorSize) * Utilities.SectorSize;
            dynamicHeader.ParentUniqueId = parent.UniqueId;
            dynamicHeader.ParentTimestamp = parentModificationTimeUtc;
            dynamicHeader.ParentUnicodeName = Utilities.GetFileFromPath(parentAbsolutePath);
            dynamicHeader.ParentLocators[7].PlatformCode = ParentLocator.PlatformCodeWindowsAbsoluteUnicode;
            dynamicHeader.ParentLocators[7].PlatformDataSpace = 512;
            dynamicHeader.ParentLocators[7].PlatformDataLength = parentAbsolutePath.Length * 2;
            dynamicHeader.ParentLocators[7].PlatformDataOffset = tableOffset + batSize;
            dynamicHeader.ParentLocators[6].PlatformCode = ParentLocator.PlatformCodeWindowsRelativeUnicode;
            dynamicHeader.ParentLocators[6].PlatformDataSpace = 512;
            dynamicHeader.ParentLocators[6].PlatformDataLength = parentRelativePath.Length * 2;
            dynamicHeader.ParentLocators[6].PlatformDataOffset = tableOffset + batSize + 512;
            dynamicHeader.UpdateChecksum();
            byte[] dynamicHeaderBlock = new byte[1024];
            dynamicHeader.ToBytes(dynamicHeaderBlock, 0);

            byte[] platformLocator1 = new byte[512];
            Encoding.Unicode.GetBytes(parentAbsolutePath, 0, parentAbsolutePath.Length, platformLocator1, 0);
            byte[] platformLocator2 = new byte[512];
            Encoding.Unicode.GetBytes(parentRelativePath, 0, parentRelativePath.Length, platformLocator2, 0);

            byte[] bat = new byte[batSize];
            for (int i = 0; i < bat.Length; ++i)
            {
                bat[i] = 0xFF;
            }

            stream.Position = 0;
            stream.Write(footerBlock, 0, 512);
            stream.Write(dynamicHeaderBlock, 0, 1024);
            stream.Write(bat, 0, batSize);
            stream.Write(platformLocator1, 0, 512);
            stream.Write(platformLocator2, 0, 512);
            stream.Write(footerBlock, 0, 512);
        }
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are supported.
 /// </summary>
 /// <param name="locator">The locator to access relative files</param>
 /// <param name="path">The path to the disk image</param>
 /// <param name="access">The access requested to the disk</param>
 internal Disk(FileLocator locator, string path, FileAccess access)
 {
     DiskImageFile file = new DiskImageFile(locator, path, access);
     _files = new List<DiscUtils.Tuple<DiskImageFile, Ownership>>();
     _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(file, Ownership.Dispose));
     ResolveFileChain();
 }
Exemple #22
0
 /// <summary>
 /// Creates a new instance from a file on a file system.
 /// </summary>
 /// <param name="fileSystem">The file system containing the disk.</param>
 /// <param name="path">The file system relative path to the disk.</param>
 /// <param name="access">The access requested to the disk.</param>
 public Disk(DiscFileSystem fileSystem, string path, FileAccess access)
 {
     FileLocator fileLocator = new DiscFileLocator(fileSystem, Path.GetDirectoryName(path));
     DiskImageFile file = new DiskImageFile(fileLocator, Path.GetFileName(path), access);
     _files = new List<Tuple<DiskImageFile, Ownership>>();
     _files.Add(new Tuple<DiskImageFile, Ownership>(file, Ownership.Dispose));
     ResolveFileChain();
 }
Exemple #23
0
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are not supported.
 /// </summary>
 /// <param name="file">The file containing the disk.</param>
 /// <param name="ownsFile">Indicates if the new instance should control the lifetime of the file.</param>
 private Disk(DiskImageFile file, Ownership ownsFile)
 {
     _files = new List <Tuple <DiskImageFile, Ownership> >();
     _files.Add(new Tuple <DiskImageFile, Ownership>(file, ownsFile));
     ResolveFileChain();
 }
 /// <summary>
 /// Initializes a stream as a differencing disk VHD file.
 /// </summary>
 /// <param name="stream">The stream to initialize.</param>
 /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the <paramref name="stream"/>.</param>
 /// <param name="parent">The disk this file is a different from.</param>
 /// <param name="ownsParent">Indicates if the new instance controls the lifetime of the <paramref name="parent"/> file.</param>
 /// <param name="parentAbsolutePath">The full path to the parent disk.</param>
 /// <param name="parentRelativePath">The relative path from the new disk to the parent disk.</param>
 /// <param name="parentModificationTime">The time the parent disk's file was last modified (from file system).</param>
 /// <returns>An object that accesses the stream as a VHD file</returns>
 public static Disk InitializeDifferencing(
     Stream stream,
     Ownership ownsStream,
     DiskImageFile parent,
     Ownership ownsParent,
     string parentAbsolutePath,
     string parentRelativePath,
     DateTime parentModificationTime)
 {
     DiskImageFile file = DiskImageFile.InitializeDifferencing(stream, ownsStream, parent, parentAbsolutePath, parentRelativePath, parentModificationTime);
     return new Disk(file, Ownership.Dispose, parent, ownsParent);
 }
Exemple #25
0
 internal static Disk InitializeDynamic(FileLocator fileLocator, string path, long capacity, Geometry geometry,
                                        long blockSize)
 {
     return(new Disk(DiskImageFile.InitializeDynamic(fileLocator, path, capacity, geometry, blockSize),
                     Ownership.Dispose));
 }
Exemple #26
0
        /// <summary>
        /// Initializes a stream as a differencing disk VHD file.
        /// </summary>
        /// <param name="stream">The stream to initialize.</param>
        /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
        /// <param name="parent">The disk this file is a different from.</param>
        /// <param name="parentAbsolutePath">The full path to the parent disk.</param>
        /// <param name="parentRelativePath">The relative path from the new disk to the parent disk.</param>
        /// <param name="parentModificationTimeUtc">The time the parent disk's file was last modified (from file system).</param>
        /// <returns>An object that accesses the stream as a VHD file.</returns>
        public static DiskImageFile InitializeDifferencing(
            Stream stream,
            Ownership ownsStream,
            DiskImageFile parent,
            string parentAbsolutePath,
            string parentRelativePath,
            DateTime parentModificationTimeUtc)
        {
            InitializeDifferencingInternal(stream, parent, parentAbsolutePath, parentRelativePath, parentModificationTimeUtc);

            return new DiskImageFile(stream, ownsStream);
        }
Exemple #27
0
 /// <summary>
 /// Initializes a stream as a dynamically-sized VHD file.
 /// </summary>
 /// <param name="stream">The stream to initialize.</param>
 /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
 /// <param name="capacity">The desired capacity of the new disk.</param>
 /// <param name="blockSize">The size of each block (unit of allocation).</param>
 /// <returns>An object that accesses the stream as a VHD file.</returns>
 public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, long blockSize)
 {
     return(new Disk(DiskImageFile.InitializeDynamic(stream, ownsStream, capacity, blockSize), Ownership.Dispose));
 }
Exemple #28
0
 /// <summary>
 /// Initializes a stream as a dynamically-sized VHD file.
 /// </summary>
 /// <param name="stream">The stream to initialize.</param>
 /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
 /// <param name="capacity">The desired capacity of the new disk.</param>
 /// <param name="geometry">The desired geometry of the new disk, or <c>null</c> for default.</param>
 /// <returns>An object that accesses the stream as a VHD file.</returns>
 public static Disk InitializeDynamic(Stream stream, Ownership ownsStream, long capacity, Geometry geometry)
 {
     return(new Disk(DiskImageFile.InitializeDynamic(stream, ownsStream, capacity, geometry), Ownership.Dispose));
 }
        private void ResolveFileChain()
        {
            DiskImageFile file = _files[_files.Count - 1].First;

            while (file.NeedsParent)
            {
                FileLocator fileLocator = file.RelativeFileLocator;
                bool found = false;
                foreach (string testPath in file.GetParentLocations())
                {
                    if (fileLocator.Exists(testPath))
                    {
                        DiskImageFile newFile = new DiskImageFile(fileLocator, testPath, FileAccess.Read);

                        if (newFile.UniqueId != file.ParentUniqueId)
                        {
                            throw new IOException(string.Format(CultureInfo.InstalledUICulture, "Invalid disk chain found looking for parent with id {0}, found {1} with id {2}", file.ParentUniqueId, newFile.FullPath, newFile.UniqueId));
                        }

                        file = newFile;
                        _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(file, Ownership.Dispose));
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    throw new IOException(string.Format(CultureInfo.InvariantCulture, "Failed to find parent for disk '{0}'", file.FullPath));
                }
            }
        }
Exemple #30
0
 public DiskExtent(DiskImageFile file)
 {
     _file = file;
 }
 /// <summary>
 /// Initializes a new instance of the Disk class.  Differencing disks are supported.
 /// </summary>
 /// <param name="fileSystem">The file system containing the disk.</param>
 /// <param name="path">The file system relative path to the disk.</param>
 /// <param name="access">The access requested to the disk.</param>
 public Disk(DiscFileSystem fileSystem, string path, FileAccess access)
 {
     FileLocator fileLocator = new DiscFileLocator(fileSystem, Utilities.GetDirectoryFromPath(path));
     DiskImageFile file = new DiskImageFile(fileLocator, Utilities.GetFileFromPath(path), access);
     _files = new List<DiscUtils.Tuple<DiskImageFile, Ownership>>();
     _files.Add(new DiscUtils.Tuple<DiskImageFile, Ownership>(file, Ownership.Dispose));
     ResolveFileChain();
 }
Exemple #32
0
        protected override void DoRun()
        {
            if (!_dontCheck.IsPresent)
            {
                using (Stream s = new FileStream(_vhdFile.Value, FileMode.Open, FileAccess.Read))
                {
                    FileChecker vhdChecker = new FileChecker(s);
                    if (!vhdChecker.Check(Console.Out, ReportLevels.All))
                    {
                        Console.WriteLine("Aborting: Invalid VHD file");
                        Environment.Exit(1);
                    }
                }
            }

            using (DiskImageFile vhdFile = new DiskImageFile(_vhdFile.Value, FileAccess.Read))
            {
                DiskImageFileInfo info = vhdFile.Information;

                FileInfo fileInfo = new FileInfo(_vhdFile.Value);

                Console.WriteLine("File Info");
                Console.WriteLine("---------");
                Console.WriteLine("           File Name: {0}", fileInfo.FullName);
                Console.WriteLine("           File Size: {0} bytes", fileInfo.Length);
                Console.WriteLine("  File Creation Time: {0} (UTC)", fileInfo.CreationTimeUtc);
                Console.WriteLine("     File Write Time: {0} (UTC)", fileInfo.LastWriteTimeUtc);
                Console.WriteLine();

                Console.WriteLine("Common Disk Info");
                Console.WriteLine("-----------------");
                Console.WriteLine("              Cookie: {0:x8}", info.Cookie);
                Console.WriteLine("            Features: {0:x8}", info.Features);
                Console.WriteLine(" File Format Version: {0}.{1}", ((info.FileFormatVersion >> 16) & 0xFFFF), (info.FileFormatVersion & 0xFFFF));
                Console.WriteLine("       Creation Time: {0} (UTC)", info.CreationTimestamp);
                Console.WriteLine("         Creator App: {0:x8}", info.CreatorApp);
                Console.WriteLine("     Creator Version: {0}.{1}", ((info.CreatorVersion >> 16) & 0xFFFF), (info.CreatorVersion & 0xFFFF));
                Console.WriteLine("     Creator Host OS: {0}", info.CreatorHostOS);
                Console.WriteLine("       Original Size: {0} bytes", info.OriginalSize);
                Console.WriteLine("        Current Size: {0} bytes", info.CurrentSize);
                Console.WriteLine("    Geometry (C/H/S): {0}", info.Geometry);
                Console.WriteLine("           Disk Type: {0}", info.DiskType);
                Console.WriteLine("            Checksum: {0:x8}", info.FooterChecksum);
                Console.WriteLine("           Unique Id: {0}", info.UniqueId);
                Console.WriteLine("         Saved State: {0}", info.SavedState);
                Console.WriteLine();

                if (info.DiskType == FileType.Differencing || info.DiskType == FileType.Dynamic)
                {
                    Console.WriteLine();
                    Console.WriteLine("Dynamic Disk Info");
                    Console.WriteLine("-----------------");
                    Console.WriteLine("              Cookie: {0}", info.DynamicCookie);
                    Console.WriteLine("      Header Version: {0}.{1}", ((info.DynamicHeaderVersion >> 16) & 0xFFFF), (info.DynamicHeaderVersion & 0xFFFF));
                    Console.WriteLine("         Block Count: {0}", info.DynamicBlockCount);
                    Console.WriteLine("          Block Size: {0} bytes", info.DynamicBlockSize);
                    Console.WriteLine("            Checksum: {0:x8}", info.DynamicChecksum);
                    Console.WriteLine("    Parent Unique Id: {0}", info.DynamicParentUniqueId);
                    Console.WriteLine("   Parent Write Time: {0} (UTC)", info.DynamicParentTimestamp);
                    Console.WriteLine("         Parent Name: {0}", info.DynamicParentUnicodeName);
                    Console.Write("    Parent Locations: ");
                    foreach (string parentLocation in info.DynamicParentLocators)
                    {
                        Console.Write("{0}\n                      ", parentLocation);
                    }
                    Console.WriteLine();
                }
            }
        }
Exemple #33
0
 internal static Disk InitializeFixed(FileLocator fileLocator, string path, long capacity, Geometry geometry)
 {
     return(new Disk(DiskImageFile.InitializeFixed(fileLocator, path, capacity, geometry), Ownership.Dispose));
 }
Exemple #34
0
        internal static DiskImageFile InitializeFixed(FileLocator locator, string path, long capacity, Geometry geometry)
        {
            DiskImageFile result = null;
            Stream stream = locator.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
            try
            {
                InitializeFixedInternal(stream, capacity, geometry);
                result = new DiskImageFile(locator, path, stream, Ownership.Dispose);
                stream = null;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            return result;
        }