From 38bd273d234fa7e77aa7d6d1f9446d9984b5f906 Mon Sep 17 00:00:00 2001 From: JinmingHu Date: Sun, 30 May 2021 10:36:49 +0800 Subject: [PATCH] fix read consistency issue (#2364) --- .../azure-storage-blobs/src/blob_client.cpp | 15 +++------------ .../src/share_file_client.cpp | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/sdk/storage/azure-storage-blobs/src/blob_client.cpp b/sdk/storage/azure-storage-blobs/src/blob_client.cpp index 5e0cb408c..baf0981cc 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_client.cpp @@ -191,10 +191,7 @@ namespace Azure { namespace Storage { namespace Blobs { { newOptions.Range.Value().Length = options.Range.Value().Length.Value() - retryOffset; } - if (!newOptions.AccessConditions.IfMatch.HasValue()) - { - newOptions.AccessConditions.IfMatch = eTag; - } + newOptions.AccessConditions.IfMatch = eTag; return std::move(Download(newOptions, context).Value.BodyStream); }; @@ -291,10 +288,7 @@ namespace Azure { namespace Storage { namespace Blobs { chunkOptions.Range = Core::Http::HttpRange(); chunkOptions.Range.Value().Offset = offset; chunkOptions.Range.Value().Length = length; - if (!chunkOptions.AccessConditions.IfMatch.HasValue()) - { - chunkOptions.AccessConditions.IfMatch = eTag; - } + chunkOptions.AccessConditions.IfMatch = eTag; auto chunk = Download(chunkOptions, context); int64_t bytesRead = chunk.Value.BodyStream->ReadToCount( buffer + (offset - firstChunkOffset), @@ -417,10 +411,7 @@ namespace Azure { namespace Storage { namespace Blobs { chunkOptions.Range = Core::Http::HttpRange(); chunkOptions.Range.Value().Offset = offset; chunkOptions.Range.Value().Length = length; - if (!chunkOptions.AccessConditions.IfMatch.HasValue()) - { - chunkOptions.AccessConditions.IfMatch = eTag; - } + chunkOptions.AccessConditions.IfMatch = eTag; auto chunk = Download(chunkOptions, context); bodyStreamToFile( *(chunk.Value.BodyStream), diff --git a/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp b/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp index 405a83e02..d1a4639fa 100644 --- a/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp +++ b/sdk/storage/azure-storage-files-shares/src/share_file_client.cpp @@ -272,8 +272,7 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { auto newResponse = Download(newOptions, context); if (eTag != newResponse.Value.Details.ETag) { - throw Azure::Core::RequestFailedException( - "File was changed during the download process."); + throw Azure::Core::RequestFailedException("file was modified in the middle of download"); } return std::move(newResponse.Value.BodyStream); }; @@ -669,6 +668,7 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { } auto firstChunk = Download(firstChunkOptions, context); + const Azure::ETag etag = firstChunk.Value.Details.ETag; int64_t fileSize; int64_t fileRangeSize; @@ -728,6 +728,11 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { { throw Azure::Core::RequestFailedException("error when reading body stream"); } + if (chunk.Value.Details.ETag != etag) + { + throw Azure::Core::RequestFailedException( + "file was modified in the middle of download"); + } if (chunkId == numChunks - 1) { @@ -774,6 +779,7 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { _internal::FileWriter fileWriter(fileName); auto firstChunk = Download(firstChunkOptions, context); + const Azure::ETag etag = firstChunk.Value.Details.ETag; int64_t fileSize; int64_t fileRangeSize; @@ -840,6 +846,11 @@ namespace Azure { namespace Storage { namespace Files { namespace Shares { chunkOptions.Range.Value().Offset = offset; chunkOptions.Range.Value().Length = length; auto chunk = Download(chunkOptions, context); + if (chunk.Value.Details.ETag != etag) + { + throw Azure::Core::RequestFailedException( + "file was modified in the middle of download"); + } bodyStreamToFile( *(chunk.Value.BodyStream), fileWriter,