Example #1
0
        private void CreateRemoteBlob()
        {
            var baseBlob = this.blobObjectFactory.Create(baseVhdBlobUri);

            if (!baseBlob.Exists())
            {
                throw new InvalidOperationException(String.Format("Base image to patch doesn't exist in blob storage: {0}", baseVhdBlobUri.Uri));
            }
            var blobVhdFooter = baseBlob.GetVhdFooter();

            long               blobSize;
            VhdFilePath        localBaseVhdPath;
            IEnumerable <Guid> childrenVhdIds;

            using (var vhdFile = new VhdFileFactory().Create(localVhd.FullName))
            {
                localBaseVhdPath = vhdFile.GetFilePathBy(blobVhdFooter.UniqueId);
                childrenVhdIds   = vhdFile.GetChildrenIds(blobVhdFooter.UniqueId).ToArray();
                blobSize         = vhdFile.Footer.CurrentSize;
            }

            FileMetaData fileMetaData = GetFileMetaData(baseBlob, localBaseVhdPath);

            var md5Hash = baseBlob.GetBlobMd5Hash();

            if (!md5Hash.SequenceEqual(fileMetaData.MD5Hash))
            {
                var message = String.Format("Patching cannot proceed, MD5 hash of base image in blob storage ({0}) and base VHD file ({1}) does not match ",
                                            baseBlob.Uri,
                                            localBaseVhdPath);
                throw new InvalidOperationException(message);
            }

            Program.SyncOutput.MessageCreatingNewPageBlob(blobSize);

            CopyBaseImageToDestination();

            using (var vds = new VirtualDiskStream(localVhd.FullName))
            {
                var streamExtents = vds.Extents.ToArray();
                var enumerable    = streamExtents.Where(e => childrenVhdIds.Contains(e.Owner)).ToArray();
                foreach (var streamExtent in enumerable)
                {
                    var indexRange = streamExtent.Range;
                    destinationBlob.ClearPagesAsync(indexRange.StartIndex, indexRange.Length)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                }
            }

            using (var bmds = new BlobMetaDataScope(destinationBlob))
            {
                bmds.Current.RemoveBlobMd5Hash();
                bmds.Current.SetUploadMetaData(OperationMetaData);
                bmds.Complete();
            }
        }
        private void CreateRemoteBlob()
        {
            var baseBlob = this.blobObjectFactory.Create(baseVhdBlobUri);

            if (!baseBlob.Exists())
            {
                throw new InvalidOperationException(String.Format("Base image to patch doesn't exist in blob storage: {0}", baseVhdBlobUri.Uri));
            }
            var blobVhdFooter = baseBlob.GetVhdFooter();

            long blobSize;
            VhdFilePath localBaseVhdPath;
            IEnumerable<Guid> childrenVhdIds;
            using (var vhdFile = new VhdFileFactory().Create(localVhd.FullName))
            {
                localBaseVhdPath = vhdFile.GetFilePathBy(blobVhdFooter.UniqueId);
                childrenVhdIds = vhdFile.GetChildrenIds(blobVhdFooter.UniqueId).ToArray();
                blobSize = vhdFile.Footer.VirtualSize;
            }

            FileMetaData fileMetaData = GetFileMetaData(baseBlob, localBaseVhdPath);

            var md5Hash = baseBlob.GetBlobMd5Hash();
            if (!md5Hash.SequenceEqual(fileMetaData.MD5Hash))
            {
                var message = String.Format("Patching cannot proceed, MD5 hash of base image in blob storage ({0}) and base VHD file ({1}) does not match ",
                                            baseBlob.Uri,
                                            localBaseVhdPath);
                throw new InvalidOperationException(message);
            }

            Program.SyncOutput.MessageCreatingNewPageBlob(blobSize);

            CopyBaseImageToDestination();

            using (var vds = new VirtualDiskStream(localVhd.FullName))
            {
                var streamExtents = vds.Extents.ToArray();
                var enumerable = streamExtents.Where(e => childrenVhdIds.Contains(e.Owner)).ToArray();
                foreach (var streamExtent in enumerable)
                {
                    var indexRange = streamExtent.Range;
                    destinationBlob.ClearPages(indexRange.StartIndex, indexRange.Length);
                }
            }

            using (var bmds = new BlobMetaDataScope(destinationBlob))
            {
                bmds.Current.RemoveBlobMd5Hash();
                bmds.Current.SetUploadMetaData(OperationMetaData);
                bmds.Complete();
            }
        }