Skip to content

Commit 5f5257c

Browse files
committed
Add an option to skip MD5 computation and checking. Also disable MD5 computation when using HTTPS because it is redundant.
1 parent 50e0e90 commit 5f5257c

File tree

7 files changed

+37
-13
lines changed

7 files changed

+37
-13
lines changed

src/ServiceManagement/Compute/Commands.ServiceManagement/Model/UploadParameters.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Model
2121
{
2222
public class UploadParameters
2323
{
24-
public UploadParameters(BlobUri destinationUri, BlobUri baseImageUri, FileInfo localFilePath, bool overWrite, int numberOfUploaderThreads)
24+
public UploadParameters(BlobUri destinationUri, BlobUri baseImageUri, FileInfo localFilePath, bool overWrite, bool skipMd5, int numberOfUploaderThreads)
2525
{
2626
DestinationUri = destinationUri;
2727
BaseImageUri = baseImageUri;
2828
LocalFilePath = localFilePath;
2929
OverWrite = overWrite;
30+
SkipMd5 = skipMd5;
3031
NumberOfUploaderThreads = numberOfUploaderThreads;
3132
}
3233

@@ -38,6 +39,8 @@ public UploadParameters(BlobUri destinationUri, BlobUri baseImageUri, FileInfo l
3839

3940
public bool OverWrite { get; private set; }
4041

42+
public bool SkipMd5 { get; private set; }
43+
4144
public int NumberOfUploaderThreads { get; private set; }
4245

4346
public AddAzureVhdCommand Cmdlet { get; set; }

src/ServiceManagement/Compute/Commands.ServiceManagement/Model/VhdUploaderModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static VhdUploadContext Upload(UploadParameters uploadParameters)
3030
}
3131
else
3232
{
33-
blobCreator = new BlobCreator(uploadParameters.LocalFilePath, uploadParameters.DestinationUri, uploadParameters.BlobObjectFactory, uploadParameters.OverWrite);
33+
blobCreator = new BlobCreator(uploadParameters.LocalFilePath, uploadParameters.DestinationUri, uploadParameters.BlobObjectFactory, uploadParameters.OverWrite, uploadParameters.SkipMd5);
3434
}
3535

3636
using (var uploadContext = blobCreator.Create())

src/ServiceManagement/Compute/Commands.ServiceManagement/StorageServices/AddAzureVhdCommand.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public SwitchParameter OverWrite
7878
set;
7979
}
8080

81+
[Parameter(Position = 6, Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = "Vhd", HelpMessage = "Skip computation of MD5 checksum")]
82+
[ValidateNotNullOrEmpty]
83+
[Alias("sm")]
84+
public SwitchParameter SkipMd5
85+
{
86+
get;
87+
set;
88+
}
89+
8190
public UploadParameters ValidateParameters()
8291
{
8392
BlobUri destinationUri;
@@ -106,7 +115,7 @@ public UploadParameters ValidateParameters()
106115
PathIntrinsics currentPath = SessionState.Path;
107116
var filePath = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString()));
108117

109-
var parameters = new UploadParameters(destinationUri, baseImageUri, filePath, OverWrite.IsPresent, NumberOfUploaderThreads)
118+
var parameters = new UploadParameters(destinationUri, baseImageUri, filePath, OverWrite.IsPresent, SkipMd5.IsPresent, NumberOfUploaderThreads)
110119
{
111120
Cmdlet = this,
112121
BlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1))

src/ServiceManagement/Compute/Sync/Upload/BlobCreator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ namespace Microsoft.WindowsAzure.Commands.Sync.Upload
1919
{
2020
public class BlobCreator : BlobCreatorBase
2121
{
22-
public BlobCreator(FileInfo localVhd, BlobUri destination, ICloudPageBlobObjectFactory blobObjectFactory, bool overWrite) :
23-
base(localVhd, destination, blobObjectFactory, overWrite)
22+
public BlobCreator(FileInfo localVhd, BlobUri destination, ICloudPageBlobObjectFactory blobObjectFactory, bool overWrite, bool skipMd5 = false) :
23+
base(localVhd, destination, blobObjectFactory, overWrite, skipMd5)
2424
{
2525
}
2626

src/ServiceManagement/Compute/Sync/Upload/BlobCreatorBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ public abstract class BlobCreatorBase
5050
protected CloudPageBlob destinationBlob;
5151
protected BlobRequestOptions requestOptions;
5252
protected bool overWrite;
53+
protected bool skipMd5;
5354
private readonly TimeSpan delayBetweenRetries = TimeSpan.FromSeconds(10);
5455

5556
private const int MegaByte = 1024 * 1024;
5657
private const int PageSizeInBytes = 2 * MegaByte;
5758
private const int MaxBufferSize = 20 * MegaByte;
5859

59-
protected BlobCreatorBase(FileInfo localVhd, BlobUri blobDestination, ICloudPageBlobObjectFactory blobObjectFactory, bool overWrite)
60+
protected BlobCreatorBase(FileInfo localVhd, BlobUri blobDestination, ICloudPageBlobObjectFactory blobObjectFactory, bool overWrite, bool skipMd5 = false)
6061
{
6162
this.localVhd = localVhd;
6263
this.blobObjectFactory = blobObjectFactory;
6364
this.destination = new Uri(blobDestination.BlobPath);
6465
this.blobDestination = blobDestination;
6566
this.overWrite = overWrite;
67+
this.skipMd5 = skipMd5;
6668

6769
this.destinationBlob = blobObjectFactory.Create(blobDestination);
6870
this.requestOptions = this.blobObjectFactory.CreateRequestOptions();
@@ -79,7 +81,7 @@ public LocalMetaData OperationMetaData
7981
{
8082
this.operationMetaData = new LocalMetaData
8183
{
82-
FileMetaData = FileMetaData.Create(localVhd.FullName),
84+
FileMetaData = FileMetaData.Create(localVhd.FullName, skipMd5),
8385
SystemInformation = SystemInformation.Create()
8486
};
8587
}

src/ServiceManagement/Compute/Sync/Upload/BlobSynchronizer.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ public bool Synchronize()
5656
{
5757
using (dwr)
5858
{
59-
var md5HashOfDataChunk = GetBase64EncodedMd5Hash(dwr.Data, (int) dwr.Range.Length);
6059
using (var stream = new MemoryStream(dwr.Data, 0, (int)dwr.Range.Length))
6160
{
62-
b.Properties.ContentMD5 = md5HashOfDataChunk;
61+
// HTTPS provides transport level security that renders
62+
// MD5 checking redundant
63+
if (blob.Uri.Scheme != Uri.UriSchemeHttps)
64+
{
65+
var md5HashOfDataChunk = GetBase64EncodedMd5Hash(dwr.Data, (int)dwr.Range.Length);
66+
b.Properties.ContentMD5 = md5HashOfDataChunk;
67+
}
68+
6369
b.WritePages(stream, dwr.Range.StartIndex);
6470
}
6571
}
@@ -78,7 +84,11 @@ public bool Synchronize()
7884
{
7985
using(var bdms = new BlobMetaDataScope(new CloudPageBlob(blob.Uri, blob.ServiceClient.Credentials)))
8086
{
81-
bdms.Current.SetBlobMd5Hash(md5Hash);
87+
if (this.md5Hash != null && this.md5Hash.Length != 0)
88+
{
89+
bdms.Current.SetBlobMd5Hash(md5Hash);
90+
}
91+
8292
bdms.Current.CleanUpUploadMetaData();
8393
bdms.Complete();
8494
}

src/ServiceManagement/Compute/Sync/Upload/UploadOperationMetaData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public DateTime LastModifiedDateUtc
5858
set { this.InternalLastModifiedDateUtc = value.ToString(DateTimeFormatInfo.InvariantInfo); }
5959
}
6060

61-
public static FileMetaData Create(string filePath)
61+
public static FileMetaData Create(string filePath, bool skipMd5 = false)
6262
{
6363
var fileInfo = new FileInfo(filePath);
6464
if(!fileInfo.Exists)
@@ -75,14 +75,14 @@ public static FileMetaData Create(string filePath)
7575
LastModifiedDateUtc = DateTime.SpecifyKind(fileInfo.LastWriteTimeUtc, DateTimeKind.Utc),
7676
Size = fileInfo.Length,
7777
VhdSize = stream.Length,
78-
MD5Hash = CalculateMd5Hash(stream, filePath)
78+
MD5Hash = skipMd5 ? new byte[0] : CalculateMd5Hash(stream, filePath)
7979
};
8080
}
8181
}
8282

8383
private static byte[] CalculateMd5Hash(Stream stream, string filePath)
8484
{
85-
using(var md5 = MD5.Create())
85+
using (var md5 = MD5.Create())
8686
{
8787
using (var swrp = new StreamWithReadProgress(stream, TimeSpan.FromSeconds(1)))
8888
{

0 commit comments

Comments
 (0)