Skip to content

Commit ea89228

Browse files
committed
refactor(storage): use merged options for checksum validation and simplify logic
1 parent 67f4d94 commit ea89228

3 files changed

Lines changed: 11 additions & 13 deletions

File tree

google/cloud/storage/examples/storage_async_samples.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,8 @@ void CreateAndWriteAppendableObjectWithChecksum(
750750

751751
// Set the expected CRC32C checksum in the current options scope
752752
// just before calling Finalize().
753-
// Note: 548262564U is the pre-computed CRC32C checksum for the string "Some data\n".
754-
// If the data changes, this checksum must be updated to match.
753+
// Note: 548262564U is the pre-computed CRC32C checksum for the string "Some
754+
// data\n". If the data changes, this checksum must be updated to match.
755755
google::cloud::internal::OptionsSpan span(
756756
google::cloud::Options{}.set<gcs::UseCrc32cValueOption>(548262564U));
757757
co_return (co_await writer.Finalize(std::move(token))).value();

google/cloud/storage/internal/async/writer_connection_impl.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/storage/internal/async/writer_connection_impl.h"
16+
#include "google/cloud/storage/async/options.h"
17+
#include "google/cloud/storage/hashing_options.h"
1618
#include "google/cloud/storage/internal/async/handle_redirect_error.h"
1719
#include "google/cloud/storage/internal/async/partial_upload.h"
1820
#include "google/cloud/storage/internal/async/write_payload_impl.h"
1921
#include "google/cloud/storage/internal/grpc/ctype_cord_workaround.h"
2022
#include "google/cloud/storage/internal/grpc/object_metadata_parser.h"
2123
#include "google/cloud/storage/internal/grpc/object_request_parser.h"
22-
#include "google/cloud/storage/async/options.h"
23-
#include "google/cloud/storage/hashing_options.h"
2424
#include "google/cloud/internal/make_status.h"
2525

2626
namespace google {
@@ -147,19 +147,18 @@ AsyncWriterConnectionImpl::Finalize(storage::WritePayload payload) {
147147
auto merged = google::cloud::internal::MergeOptions(
148148
current_options, options_ ? *options_ : google::cloud::Options{});
149149

150-
// Default to letting the internal hash function compute and send the checksum.
150+
// Default to letting the internal hash function compute and send the
151+
// checksum.
151152
auto action = PartialUpload::kFinalizeWithChecksum;
152153

153-
if (is_append || current_options.has<google::cloud::storage::UseCrc32cValueOption>()) {
154+
if (is_append || merged.has<google::cloud::storage::UseCrc32cValueOption>()) {
155+
// If it's an appendable upload, or the user manually specified an expected
156+
// checksum, we manually inject it and use `kFinalize` so the internal hash
157+
// function doesn't compute and overwrite it with a partial or local hash.
154158
if (merged.has<google::cloud::storage::UseCrc32cValueOption>()) {
155159
write.mutable_object_checksums()->set_crc32c(
156160
merged.get<google::cloud::storage::UseCrc32cValueOption>());
157161
}
158-
// For appendable uploads, the internal hash function only sees the chunks uploaded
159-
// in this stream, not the full object. We use `kFinalize` to avoid sending this
160-
// partial hash, which would otherwise fail validation.
161-
// If the user specified a manual expected checksum in `current_options`, we must
162-
// also use `kFinalize` so the internal hash function doesn't overwrite it.
163162
action = PartialUpload::kFinalize;
164163
}
165164

google/cloud/storage/internal/async/writer_connection_impl_test.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,7 @@ TEST(AsyncWriterConnectionTest, FinalizeAppendableWithExpectedChecksum) {
757757
EXPECT_EQ(request.common_object_request_params().encryption_algorithm(),
758758
"test-only-algo");
759759
EXPECT_TRUE(request.has_object_checksums());
760-
EXPECT_EQ(request.object_checksums().crc32c(),
761-
123456);
760+
EXPECT_EQ(request.object_checksums().crc32c(), 123456);
762761
return sequencer.PushBack("Write");
763762
});
764763
EXPECT_CALL(*mock, Read).WillOnce([&]() {

0 commit comments

Comments
 (0)