public VhdFooter CreateFooter()
 {
     try
     {
         ValidateVhdSize();
         var attributeHelper = new AttributeHelper <VhdFooter>();
         var footer          = new VhdFooter();
         footer.Cookie             = ReadVhdCookie(attributeHelper.GetAttribute(() => footer.Cookie));
         footer.Features           = ReadFeatures(attributeHelper.GetAttribute(() => footer.Features));
         footer.FileFormatVersion  = ReadVhdFileFormatVersion(attributeHelper.GetAttribute(() => footer.FileFormatVersion));
         footer.HeaderOffset       = ReadHeaderOffset(attributeHelper.GetAttribute(() => footer.HeaderOffset));
         footer.TimeStamp          = ReadTimeStamp(attributeHelper.GetAttribute(() => footer.TimeStamp));
         footer.CreatorApplication = ReadCreatorApplication(attributeHelper.GetAttribute(() => footer.CreatorApplication));
         footer.CreatorVersion     = ReadCreatorVersion(attributeHelper.GetAttribute(() => footer.CreatorVersion));
         footer.CreatorHostOsType  = ReadCreatorHostOsType(attributeHelper.GetAttribute(() => footer.CreatorHostOsType));
         footer.PhsyicalSize       = ReadPhysicalSize(attributeHelper.GetAttribute(() => footer.PhsyicalSize));
         footer.VirtualSize        = ReadVirtualSize(attributeHelper.GetAttribute(() => footer.VirtualSize));
         footer.DiskGeometry       = ReadDiskGeometry(attributeHelper.GetAttribute(() => footer.DiskGeometry));
         footer.DiskType           = ReadDiskType(attributeHelper.GetAttribute(() => footer.DiskType));
         footer.CheckSum           = ReadCheckSum(attributeHelper.GetAttribute(() => footer.CheckSum));
         footer.UniqueId           = ReadUniqueId(attributeHelper.GetAttribute(() => footer.UniqueId));
         footer.SavedState         = ReadSavedState(attributeHelper.GetAttribute(() => footer.SavedState));
         footer.Reserved           = ReadReserved(attributeHelper.GetAttribute(() => footer.Reserved));
         footer.RawData            = ReadWholeFooter(attributeHelper.GetAttribute(() => footer.RawData));
         return(footer);
     }
     catch (EndOfStreamException e)
     {
         throw new VhdParsingException("unsupported format", e);
     }
 }
        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();

            using (var memoryStream = new MemoryStream(byteArray))
            {
                var binaryReader  = new BinaryReader(memoryStream);
                var vhdDataReader = new VhdDataReader(binaryReader);
                var footerFactory = new VhdFooterFactory(vhdDataReader);
                var vhdFooter     = footerFactory.CreateFooter();
                return(vhdFooter);
            }
        }
 public VhdFooterSerializer(VhdFooter vhdFooter)
 {
     this.vhdFooter = vhdFooter;
     this.attributeHelper = new AttributeHelper<VhdFooter>();
 }
        private IEnumerable <CompletionPort> CreateFooterAsync(AsyncMachine <VhdFooter> machine)
        {
            ValidateVhdSize();
            var attributeHelper = new AttributeHelper <VhdFooter>();
            var footer          = new VhdFooter();

            BeginReadVhdCookie(attributeHelper.GetAttribute(() => footer.Cookie), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.Cookie = TryCatch <VhdCookie>(EndReadVhdCookie, machine.CompletionResult);

            BeginReadFeatures(attributeHelper.GetAttribute(() => footer.Features), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.Features = TryCatch <VhdFeature>(EndReadFeatures, machine.CompletionResult);

            BeginReadVhdFileFormatVersion(attributeHelper.GetAttribute(() => footer.FileFormatVersion), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.FileFormatVersion = TryCatch <VhdFileFormatVersion>(EndReadVhdFileFormatVersion, machine.CompletionResult);

            BeginReadHeaderOffset(attributeHelper.GetAttribute(() => footer.HeaderOffset), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.HeaderOffset = TryCatch <long>(EndReadHeaderOffset, machine.CompletionResult);

            BeginReadTimeStamp(attributeHelper.GetAttribute(() => footer.TimeStamp), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.TimeStamp = TryCatch <DateTime>(EndReadTimeStamp, machine.CompletionResult);

            BeginReadCreatorApplication(attributeHelper.GetAttribute(() => footer.CreatorApplication), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CreatorApplication = TryCatch <string>(EndReadCreatorApplication, machine.CompletionResult);

            BeginReadCreatorVersion(attributeHelper.GetAttribute(() => footer.CreatorVersion), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CreatorVersion = TryCatch <VhdCreatorVersion>(EndReadCreatorVersion, machine.CompletionResult);

            BeginReadCreatorHostOsType(attributeHelper.GetAttribute(() => footer.CreatorHostOsType), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CreatorHostOsType = TryCatch <HostOsType>(EndReadCreatorHostOsType, machine.CompletionResult);

            BeginReadPhysicalSize(attributeHelper.GetAttribute(() => footer.PhsyicalSize), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.PhsyicalSize = TryCatch <long>(EndReadPhysicalSize, machine.CompletionResult);

            BeginReadVirtualSize(attributeHelper.GetAttribute(() => footer.VirtualSize), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.VirtualSize = TryCatch <long>(EndReadVirtualSize, machine.CompletionResult);

            BeginReadDiskGeometry(attributeHelper.GetAttribute(() => footer.DiskGeometry), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.DiskGeometry = TryCatch <DiskGeometry>(EndReadDiskGeometry, machine.CompletionResult);

            BeginReadDiskType(attributeHelper.GetAttribute(() => footer.DiskType), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.DiskType = TryCatch <DiskType>(EndReadDiskType, machine.CompletionResult);

            BeginReadCheckSum(attributeHelper.GetAttribute(() => footer.CheckSum), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CheckSum = TryCatch <uint>(EndReadCheckSum, machine.CompletionResult);

            BeginReadUniqueId(attributeHelper.GetAttribute(() => footer.UniqueId), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.UniqueId = TryCatch <Guid>(EndReadUniqueId, machine.CompletionResult);

            BeginReadSavedState(attributeHelper.GetAttribute(() => footer.SavedState), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.SavedState = TryCatch <bool>(EndReadSavedState, machine.CompletionResult);

            BeginReadReserved(attributeHelper.GetAttribute(() => footer.Reserved), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.Reserved = TryCatch <byte[]>(EndReadReserved, machine.CompletionResult);

            BeginReadWholeFooter(attributeHelper.GetAttribute(() => footer.RawData), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.RawData = TryCatch <byte[]>(EndReadWholeFooter, machine.CompletionResult);

            machine.ParameterValue = footer;
        }
Пример #5
0
 public VhdFooterSerializer(VhdFooter vhdFooter)
 {
     this.vhdFooter       = vhdFooter;
     this.attributeHelper = new AttributeHelper <VhdFooter>();
 }
 public VhdHeaderFactory(VhdDataReader dataReader, VhdFooter footer)
 {
     this.dataReader = dataReader;
     this.footer     = footer;
     headerOffset    = this.footer.HeaderOffset;
 }