Ejemplo n.º 1
0
        public static VhdFooter CreateFixedDiskFooter(long virtualSize)
        {
            var helper            = new AttributeHelper <VhdFooter>();
            var footer            = new VhdFooter();
            var reservedAttribute = helper.GetAttribute(() => footer.Reserved);

            footer.Cookie             = VhdCookie.CreateFooterCookie();
            footer.Features           = VhdFeature.Reserved;
            footer.FileFormatVersion  = VhdFileFormatVersion.DefaultFileFormatVersion;
            footer.HeaderOffset       = VhdConstants.VHD_NO_DATA_LONG;
            footer.TimeStamp          = DateTime.UtcNow;
            footer.CreatorApplication = WindowsAzureCreatorApplicationName;
            footer.CreatorVersion     = VhdCreatorVersion.CSUP2011;
            footer.CreatorHostOsType  = HostOsType.Windows;
            footer.PhsyicalSize       = virtualSize;
            footer.VirtualSize        = virtualSize;
            footer.DiskGeometry       = DiskGeometry.CreateFromVirtualSize(virtualSize);
            footer.DiskType           = DiskType.Fixed;
            footer.UniqueId           = Guid.NewGuid();
            footer.SavedState         = false;
            footer.Reserved           = new byte[reservedAttribute.Size];

            var footerSerializer = new VhdFooterSerializer(footer);
            var byteArray        = footerSerializer.ToByteArray();

            var reader        = new VhdDataReader(new BinaryReader(new MemoryStream(byteArray)));
            var footerFactory = new VhdFooterFactory(reader);
            var vhdFooter     = footerFactory.CreateFooter();

            return(vhdFooter);
        }
Ejemplo n.º 2
0
        private VhdFile Create(Stream stream, string vhdDirectory)
        {
            var reader = new BinaryReader(stream, Encoding.Unicode);

            try
            {
                var dataReader = new VhdDataReader(reader);
                var footer     = new VhdFooterFactory(dataReader).CreateFooter();

                VhdHeader            header = null;
                BlockAllocationTable blockAllocationTable = null;
                VhdFile parent = null;
                if (footer.DiskType != DiskType.Fixed)
                {
                    header = new VhdHeaderFactory(dataReader, footer).CreateHeader();
                    blockAllocationTable = new BlockAllocationTableFactory(dataReader, header).Create();
                    if (footer.DiskType == DiskType.Differencing)
                    {
                        var parentPath = vhdDirectory == null ? header.ParentPath : Path.Combine(vhdDirectory, header.GetRelativeParentPath());
                        parent = Create(parentPath);
                    }
                }
                return(new VhdFile(footer, header, blockAllocationTable, parent, stream));
            }
            catch (EndOfStreamException)
            {
                throw new VhdParsingException("unsupported format");
            }
        }
Ejemplo n.º 3
0
        private VhdFile Create(StreamSource streamSource)
        {
            var disposer = new Action(() => { if (streamSource.DisposeOnException)
                                              {
                                                  streamSource.Stream.Dispose();
                                              }
                                      });
            bool throwing = false;

            try
            {
                var reader     = new BinaryReader(streamSource.Stream, Encoding.Unicode);
                var dataReader = new VhdDataReader(reader);
                var footer     = new VhdFooterFactory(dataReader).CreateFooter();

                VhdHeader            header = null;
                BlockAllocationTable blockAllocationTable = null;
                VhdFile parent = null;
                if (footer.DiskType != DiskType.Fixed)
                {
                    header = new VhdHeaderFactory(dataReader, footer).CreateHeader();
                    blockAllocationTable = new BlockAllocationTableFactory(dataReader, header).Create();
                    if (footer.DiskType == DiskType.Differencing)
                    {
                        var parentPath = streamSource.VhdDirectory == null ? header.ParentPath : Path.Combine(streamSource.VhdDirectory, header.GetRelativeParentPath());
                        parent = Create(parentPath);
                    }
                }
                return(new VhdFile(footer, header, blockAllocationTable, parent, streamSource.Stream));
            }
            catch (Exception e)
            {
                throwing = true;
                throw new VhdParsingException("unsupported format", e);
            }
            finally
            {
                if (throwing)
                {
                    disposer();
                }
            }
        }
Ejemplo n.º 4
0
        private IEnumerable <CompletionPort> CreateAsync(AsyncMachine <VhdFile> machine, StreamSource streamSource)
        {
            var reader        = new BinaryReader(streamSource.Stream, Encoding.Unicode);
            var dataReader    = new VhdDataReader(reader);
            var footerFactory = new VhdFooterFactory(dataReader);

            footerFactory.BeginCreateFooter(machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            var footer = TryCatch <VhdFooter>(footerFactory.EndCreateFooter, machine.CompletionResult);

            VhdHeader            header = null;
            BlockAllocationTable blockAllocationTable = null;
            VhdFile parent = null;

            if (footer.DiskType != DiskType.Fixed)
            {
                var headerFactory = new VhdHeaderFactory(dataReader, footer);

                headerFactory.BeginCreateHeader(machine.CompletionCallback, null);
                yield return(CompletionPort.SingleOperation);

                header = TryCatch <VhdHeader>(headerFactory.EndCreateHeader, machine.CompletionResult);

                var tableFactory = new BlockAllocationTableFactory(dataReader, header);
                tableFactory.BeginCreate(machine.CompletionCallback, null);
                yield return(CompletionPort.SingleOperation);

                blockAllocationTable = TryCatch <BlockAllocationTable>(tableFactory.EndCreate, machine.CompletionResult);

                if (footer.DiskType == DiskType.Differencing)
                {
                    var parentPath = streamSource.VhdDirectory == null ? header.ParentPath : Path.Combine(streamSource.VhdDirectory, header.GetRelativeParentPath());

                    BeginCreate(parentPath, machine.CompletionCallback, null);
                    yield return(CompletionPort.SingleOperation);

                    parent = EndCreate(machine.CompletionResult);
                }
            }
            machine.ParameterValue = new VhdFile(footer, header, blockAllocationTable, parent, streamSource.Stream);
        }
 public BlockAllocationTableFactory(VhdDataReader dataReader, VhdHeader header)
 {
     this.dataReader = dataReader;
     this.header = header;
 }
Ejemplo n.º 6
0
 public VhdFooterFactory(VhdDataReader dataReader)
 {
     this.dataReader      = dataReader;
     this.diskTypeFactory = new DiskTypeFactory();
 }
Ejemplo n.º 7
0
 public VhdParentLocatorFactory(VhdDataReader dataReader, long offset)
 {
     this.dataReader = dataReader;
     this.offset     = offset;
     attributeHelper = new AttributeHelper <ParentLocator>();
 }
Ejemplo n.º 8
0
 public BlockAllocationTableFactory(VhdDataReader dataReader, VhdHeader header)
 {
     this.dataReader = dataReader;
     this.header     = header;
 }
 public VhdParentLocatorFactory(VhdDataReader dataReader, long offset)
 {
     this.dataReader = dataReader;
     this.offset = offset;
     attributeHelper = new AttributeHelper<ParentLocator>();
 }
Ejemplo n.º 10
0
 public VhdHeaderFactory(VhdDataReader dataReader, VhdFooter footer)
 {
     this.dataReader = dataReader;
     this.footer     = footer;
     headerOffset    = this.footer.HeaderOffset;
 }