Skip to content

Commit 2f09f39

Browse files
committed
still pass in the context
1 parent c129f84 commit 2f09f39

File tree

4 files changed

+69
-76
lines changed

4 files changed

+69
-76
lines changed

include/aws/s3/private/s3_checksums.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,6 @@ struct aws_input_stream *aws_checksum_stream_new(
103103
AWS_S3_API
104104
int aws_checksum_stream_finalize_checksum(struct aws_input_stream *checksum_stream, struct aws_byte_buf *checksum_buf);
105105

106-
/**
107-
* Finalize the checksum has read so far to the checksum context.
108-
* Not thread safe.
109-
*/
110-
AWS_S3_API
111-
int aws_checksum_stream_finalize_checksum_context(
112-
struct aws_input_stream *checksum_stream,
113-
struct aws_s3_upload_request_checksum_context *checksum_context);
114-
115106
/**
116107
* TODO: properly support chunked encoding.
117108
* Creates a chunked encoding stream that wraps an existing stream and adds checksum trailers.

source/s3_checksum_context.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ int aws_s3_upload_request_checksum_context_finalize_checksum(
170170
return AWS_OP_ERR;
171171
}
172172
context->synced_data.checksum_calculated = true;
173+
} else {
174+
AWS_LOGF_ERROR(
175+
AWS_LS_S3_GENERAL, "Checksum already finalized. This should not happen. Please file a bug report.");
176+
s_unlock_synced_data(context);
177+
return aws_raise_error(AWS_ERROR_INVALID_STATE);
173178
}
174179
s_unlock_synced_data(context);
175180
return AWS_OP_SUCCESS;

source/s3_checksum_stream.c

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ static int s_aws_input_checksum_stream_read(struct aws_input_stream *stream, str
4848
if (aws_checksum_update(impl->checksum, &to_sum)) {
4949
return AWS_OP_ERR;
5050
}
51+
if (impl->context) {
52+
/* If we're at the end of the stream, compute and store the final checksum */
53+
struct aws_stream_status status;
54+
if (aws_input_stream_get_status(impl->old_stream, &status)) {
55+
return AWS_OP_ERR;
56+
}
57+
if (status.is_end_of_stream) {
58+
if (aws_checksum_finalize(impl->checksum, &impl->checksum_result) != AWS_OP_SUCCESS) {
59+
AWS_LOGF_ERROR(
60+
AWS_LS_S3_CLIENT,
61+
"Failed to calculate checksum with error code %d (%s).",
62+
aws_last_error(),
63+
aws_error_str(aws_last_error()));
64+
aws_byte_buf_reset(&impl->checksum_result, true);
65+
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
66+
}
67+
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&impl->checksum_result);
68+
if (aws_s3_upload_request_checksum_context_finalize_checksum(impl->context, checksum_result_cursor) !=
69+
AWS_OP_SUCCESS) {
70+
AWS_LOGF_ERROR(
71+
AWS_LS_S3_CLIENT,
72+
"Failed to finalize checksum context with error code %d (%s).",
73+
aws_last_error(),
74+
aws_error_str(aws_last_error()));
75+
aws_byte_buf_reset(&impl->checksum_result, true);
76+
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
77+
}
78+
}
79+
}
5180
return AWS_OP_SUCCESS;
5281
}
5382

@@ -82,7 +111,7 @@ static struct aws_input_stream_vtable s_aws_input_checksum_stream_vtable = {
82111
.get_length = s_aws_input_checksum_stream_get_length,
83112
};
84113

85-
struct aws_input_stream *aws_checksum_stream_new(
114+
static struct aws_checksum_stream *s_aws_checksum_input_checksum_stream_new(
86115
struct aws_allocator *allocator,
87116
struct aws_input_stream *existing_stream,
88117
enum aws_s3_checksum_algorithm algorithm) {
@@ -100,55 +129,58 @@ struct aws_input_stream *aws_checksum_stream_new(
100129
impl->old_stream = aws_input_stream_acquire(existing_stream);
101130
aws_ref_count_init(
102131
&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_checksum_stream_destroy);
103-
104-
return &impl->base;
132+
return impl;
105133
on_error:
106134
aws_mem_release(impl->allocator, impl);
107135
return NULL;
108136
}
109137

138+
struct aws_input_stream *aws_checksum_stream_new(
139+
struct aws_allocator *allocator,
140+
struct aws_input_stream *existing_stream,
141+
enum aws_s3_checksum_algorithm algorithm) {
142+
AWS_PRECONDITION(existing_stream);
143+
struct aws_checksum_stream *impl = s_aws_checksum_input_checksum_stream_new(allocator, existing_stream, algorithm);
144+
if (impl) {
145+
return &impl->base;
146+
}
147+
return NULL;
148+
}
149+
110150
struct aws_input_stream *aws_checksum_stream_new_with_context(
111151
struct aws_allocator *allocator,
112152
struct aws_input_stream *existing_stream,
113153
struct aws_s3_upload_request_checksum_context *context) {
114154
AWS_PRECONDITION(existing_stream);
115155
AWS_PRECONDITION(context);
116-
117-
struct aws_checksum_stream *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_checksum_stream));
118-
impl->allocator = allocator;
119-
impl->base.vtable = &s_aws_input_checksum_stream_vtable;
120-
121-
impl->checksum = aws_checksum_new(allocator, context->algorithm);
122-
if (impl->checksum == NULL) {
123-
goto on_error;
156+
struct aws_checksum_stream *impl =
157+
s_aws_checksum_input_checksum_stream_new(allocator, existing_stream, context->algorithm);
158+
if (impl) {
159+
impl->context = context;
160+
return &impl->base;
124161
}
125-
aws_byte_buf_init(&impl->checksum_result, allocator, impl->checksum->digest_size);
126-
impl->old_stream = aws_input_stream_acquire(existing_stream);
127-
aws_ref_count_init(
128-
&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_checksum_stream_destroy);
129-
impl->context = context;
130-
return &impl->base;
131-
on_error:
132-
aws_mem_release(impl->allocator, impl);
133162
return NULL;
134163
}
135164

136165
int aws_checksum_stream_finalize_checksum(struct aws_input_stream *checksum_stream, struct aws_byte_buf *checksum_buf) {
137166
AWS_PRECONDITION(checksum_buf);
138167
AWS_PRECONDITION(checksum_buf->len == 0 && "Checksum output buffer is not empty");
168+
int rt_code = AWS_OP_ERR;
139169

140170
struct aws_checksum_stream *impl = AWS_CONTAINER_OF(checksum_stream, struct aws_checksum_stream, base);
171+
struct aws_byte_buf checksum_result;
172+
aws_byte_buf_init(&checksum_result, impl->allocator, impl->checksum->digest_size);
141173

142-
if (aws_checksum_finalize(impl->checksum, &impl->checksum_result) != AWS_OP_SUCCESS) {
174+
if (aws_checksum_finalize(impl->checksum, &checksum_result) != AWS_OP_SUCCESS) {
143175
AWS_LOGF_ERROR(
144176
AWS_LS_S3_CLIENT,
145177
"Failed to calculate checksum with error code %d (%s).",
146178
aws_last_error(),
147179
aws_error_str(aws_last_error()));
148-
aws_byte_buf_reset(&impl->checksum_result, true);
149-
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
180+
aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
181+
goto done;
150182
}
151-
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&impl->checksum_result);
183+
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&checksum_result);
152184
if (aws_base64_encode(&checksum_result_cursor, checksum_buf) != AWS_OP_SUCCESS) {
153185
AWS_LOGF_ERROR(
154186
AWS_LS_S3_CLIENT,
@@ -157,37 +189,11 @@ int aws_checksum_stream_finalize_checksum(struct aws_input_stream *checksum_stre
157189
aws_error_str(aws_last_error()),
158190
checksum_buf->capacity,
159191
checksum_buf->len);
160-
aws_byte_buf_reset(&impl->checksum_result, true);
161-
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
162-
}
163-
164-
return AWS_OP_SUCCESS;
165-
}
166-
167-
int aws_checksum_stream_finalize_checksum_context(
168-
struct aws_input_stream *checksum_stream,
169-
struct aws_s3_upload_request_checksum_context *checksum_context) {
170-
struct aws_checksum_stream *impl = AWS_CONTAINER_OF(checksum_stream, struct aws_checksum_stream, base);
171-
172-
if (aws_checksum_finalize(impl->checksum, &impl->checksum_result) != AWS_OP_SUCCESS) {
173-
AWS_LOGF_ERROR(
174-
AWS_LS_S3_CLIENT,
175-
"Failed to calculate checksum with error code %d (%s).",
176-
aws_last_error(),
177-
aws_error_str(aws_last_error()));
178-
aws_byte_buf_reset(&impl->checksum_result, true);
179-
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
192+
aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
193+
goto done;
180194
}
181-
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&impl->checksum_result);
182-
if (aws_s3_upload_request_checksum_context_finalize_checksum(checksum_context, checksum_result_cursor) !=
183-
AWS_OP_SUCCESS) {
184-
AWS_LOGF_ERROR(
185-
AWS_LS_S3_CLIENT,
186-
"Failed to finalize checksum context with error code %d (%s).",
187-
aws_last_error(),
188-
aws_error_str(aws_last_error()));
189-
aws_byte_buf_reset(&impl->checksum_result, true);
190-
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
191-
}
192-
return AWS_OP_SUCCESS;
195+
rt_code = AWS_OP_SUCCESS;
196+
done:
197+
aws_byte_buf_clean_up(&checksum_result);
198+
return rt_code;
193199
}

source/s3_chunk_stream.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ struct aws_chunk_stream {
2727
/* Pointing to the stream we read from */
2828
struct aws_input_stream *current_stream;
2929
struct aws_input_stream *chunk_body_stream;
30-
struct aws_input_stream *checksum_stream;
3130

3231
struct aws_s3_upload_request_checksum_context *checksum_context;
3332
struct aws_byte_buf pre_chunk_buffer;
@@ -62,13 +61,6 @@ static int s_set_post_chunk_stream(struct aws_chunk_stream *parent_stream) {
6261
}
6362
struct aws_byte_cursor post_trailer_cursor = aws_byte_cursor_from_string(s_post_trailer);
6463
struct aws_byte_cursor colon_cursor = aws_byte_cursor_from_string(s_colon);
65-
if (parent_stream->checksum_stream) {
66-
/* If we have the checksum stream, finalize the checksum now as we finished reading from it. */
67-
if (aws_checksum_stream_finalize_checksum_context(
68-
parent_stream->checksum_stream, parent_stream->checksum_context)) {
69-
return AWS_OP_ERR;
70-
}
71-
}
7264
struct aws_byte_cursor checksum_result_cursor =
7365
aws_s3_upload_request_checksum_context_get_checksum_cursor(parent_stream->checksum_context);
7466
AWS_ASSERT(parent_stream->checksum_context->encoded_checksum_size == checksum_result_cursor.len);
@@ -167,7 +159,6 @@ static void s_aws_input_chunk_stream_destroy(struct aws_chunk_stream *impl) {
167159
if (impl) {
168160
aws_input_stream_release(impl->current_stream);
169161
aws_input_stream_release(impl->chunk_body_stream);
170-
aws_input_stream_release(impl->checksum_stream);
171162
aws_byte_buf_clean_up(&impl->pre_chunk_buffer);
172163
aws_byte_buf_clean_up(&impl->post_chunk_buffer);
173164
/* Either we calculated the checksum, or we the checksum is empty. Otherwise, something was wrong. */
@@ -227,11 +218,11 @@ struct aws_input_stream *aws_chunk_stream_new(
227218
}
228219
if (should_calculate_checksum) {
229220
/* Wrap the existing stream with checksum stream to calculate the checksum when reading from it. */
230-
impl->checksum_stream = aws_checksum_stream_new(allocator, existing_stream, algorithm);
231-
if (impl->checksum_stream == NULL) {
221+
impl->chunk_body_stream =
222+
aws_checksum_stream_new_with_context(allocator, existing_stream, impl->checksum_context);
223+
if (impl->chunk_body_stream == NULL) {
232224
goto error;
233225
}
234-
impl->chunk_body_stream = aws_input_stream_acquire(impl->checksum_stream);
235226
} else {
236227
/* No need to calculate the checksum during read, use the existing stream directly. */
237228
impl->chunk_body_stream = aws_input_stream_acquire(existing_stream);

0 commit comments

Comments
 (0)