s3_list_parts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/s3/private/s3_list_parts.h>
  6. #include <aws/s3/private/s3_paginator.h>
  7. #include <aws/s3/private/s3_util.h>
  8. #include <aws/common/ref_count.h>
  9. #include <aws/common/xml_parser.h>
  10. #include <aws/io/uri.h>
  11. #include <aws/http/http.h>
  12. #include <aws/http/request_response.h>
  13. struct aws_s3_operation_data {
  14. struct aws_allocator *allocator;
  15. struct aws_string *key;
  16. struct aws_string *upload_id;
  17. struct aws_ref_count ref_count;
  18. aws_s3_on_part_fn *on_part;
  19. void *user_data;
  20. };
  21. static void s_ref_count_zero_callback(void *arg) {
  22. struct aws_s3_operation_data *operation_data = arg;
  23. if (operation_data->key) {
  24. aws_string_destroy(operation_data->key);
  25. }
  26. if (operation_data->upload_id) {
  27. aws_string_destroy(operation_data->upload_id);
  28. }
  29. aws_mem_release(operation_data->allocator, operation_data);
  30. }
  31. static void s_on_paginator_cleanup(void *user_data) {
  32. struct aws_s3_operation_data *operation_data = user_data;
  33. aws_ref_count_release(&operation_data->ref_count);
  34. }
  35. struct result_wrapper {
  36. struct aws_allocator *allocator;
  37. struct aws_s3_part_info part_info;
  38. };
  39. /* invoked when the ListPartResult/Parts node is iterated. */
  40. static bool s_on_parts_node(struct aws_xml_parser *parser, struct aws_xml_node *node, void *user_data) {
  41. struct result_wrapper *result_wrapper = user_data;
  42. struct aws_s3_part_info *part_info = &result_wrapper->part_info;
  43. /* for each Parts node, get the info from it and send it off as an part we've encountered */
  44. struct aws_byte_cursor node_name;
  45. aws_xml_node_get_name(node, &node_name);
  46. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ETag")) {
  47. return aws_xml_node_as_body(parser, node, &part_info->e_tag) == AWS_OP_SUCCESS;
  48. }
  49. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "LastModified")) {
  50. struct aws_byte_cursor date_cur;
  51. if (aws_xml_node_as_body(parser, node, &date_cur) == AWS_OP_SUCCESS) {
  52. aws_date_time_init_from_str_cursor(&part_info->last_modified, &date_cur, AWS_DATE_FORMAT_ISO_8601);
  53. return true;
  54. }
  55. return false;
  56. }
  57. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Size")) {
  58. struct aws_byte_cursor size_cur;
  59. if (aws_xml_node_as_body(parser, node, &size_cur) == AWS_OP_SUCCESS) {
  60. if (aws_byte_cursor_utf8_parse_u64(size_cur, &part_info->size)) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. }
  66. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "PartNumber")) {
  67. struct aws_byte_cursor part_number_cur;
  68. if (aws_xml_node_as_body(parser, node, &part_number_cur) == AWS_OP_SUCCESS) {
  69. uint64_t part_number = 0;
  70. if (aws_byte_cursor_utf8_parse_u64(part_number_cur, &part_number)) {
  71. return false;
  72. }
  73. if (part_number > UINT32_MAX) {
  74. aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
  75. return false;
  76. }
  77. part_info->part_number = (uint32_t)part_number;
  78. return true;
  79. }
  80. }
  81. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumCRC32")) {
  82. return aws_xml_node_as_body(parser, node, &part_info->checksumCRC32) == AWS_OP_SUCCESS;
  83. }
  84. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumCRC32C")) {
  85. return aws_xml_node_as_body(parser, node, &part_info->checksumCRC32C) == AWS_OP_SUCCESS;
  86. }
  87. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumSHA1")) {
  88. return aws_xml_node_as_body(parser, node, &part_info->checksumSHA1) == AWS_OP_SUCCESS;
  89. }
  90. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumSHA256")) {
  91. return aws_xml_node_as_body(parser, node, &part_info->checksumSHA256) == AWS_OP_SUCCESS;
  92. }
  93. return true;
  94. }
  95. static bool s_on_list_bucket_result_node_encountered(
  96. struct aws_xml_parser *parser,
  97. struct aws_xml_node *node,
  98. void *user_data) {
  99. struct aws_s3_operation_data *operation_data = user_data;
  100. struct aws_byte_cursor node_name;
  101. aws_xml_node_get_name(node, &node_name);
  102. struct result_wrapper result_wrapper;
  103. AWS_ZERO_STRUCT(result_wrapper);
  104. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Part")) {
  105. result_wrapper.allocator = operation_data->allocator;
  106. /* this will traverse the current Parts node, get the metadata necessary to construct
  107. * an instance of fs_info so we can invoke the callback on it. This happens once per part. */
  108. bool ret_val = aws_xml_node_traverse(parser, node, s_on_parts_node, &result_wrapper) == AWS_OP_SUCCESS;
  109. struct aws_byte_buf trimmed_etag;
  110. AWS_ZERO_STRUCT(trimmed_etag);
  111. if (result_wrapper.part_info.e_tag.len) {
  112. struct aws_string *quoted_etag_str =
  113. aws_string_new_from_cursor(result_wrapper.allocator, &result_wrapper.part_info.e_tag);
  114. replace_quote_entities(result_wrapper.allocator, quoted_etag_str, &trimmed_etag);
  115. result_wrapper.part_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag);
  116. aws_string_destroy(quoted_etag_str);
  117. }
  118. if (ret_val && operation_data->on_part) {
  119. ret_val |= operation_data->on_part(&result_wrapper.part_info, operation_data->user_data);
  120. }
  121. if (trimmed_etag.len) {
  122. aws_byte_buf_clean_up(&trimmed_etag);
  123. }
  124. return ret_val;
  125. }
  126. return true;
  127. }
  128. static int s_construct_next_request_http_message(
  129. struct aws_byte_cursor *continuation_token,
  130. void *user_data,
  131. struct aws_http_message **out_message) {
  132. AWS_PRECONDITION(user_data);
  133. struct aws_s3_operation_data *operation_data = user_data;
  134. struct aws_byte_buf request_path;
  135. struct aws_byte_cursor key_val = aws_byte_cursor_from_string(operation_data->key);
  136. aws_byte_buf_init_copy_from_cursor(&request_path, operation_data->allocator, key_val);
  137. if (operation_data->upload_id) {
  138. struct aws_byte_cursor upload_id = aws_byte_cursor_from_c_str("?uploadId=");
  139. aws_byte_buf_append_dynamic(&request_path, &upload_id);
  140. struct aws_byte_cursor upload_id_val = aws_byte_cursor_from_string(operation_data->upload_id);
  141. aws_byte_buf_append_dynamic(&request_path, &upload_id_val);
  142. }
  143. if (continuation_token) {
  144. struct aws_byte_cursor continuation = aws_byte_cursor_from_c_str("&part-number-marker=");
  145. aws_byte_buf_append_dynamic(&request_path, &continuation);
  146. aws_byte_buf_append_encoding_uri_param(&request_path, continuation_token);
  147. }
  148. struct aws_http_message *list_parts_request = aws_http_message_new_request(operation_data->allocator);
  149. aws_http_message_set_request_path(list_parts_request, aws_byte_cursor_from_buf(&request_path));
  150. aws_byte_buf_clean_up(&request_path);
  151. struct aws_http_header accept_header = {
  152. .name = aws_byte_cursor_from_c_str("accept"),
  153. .value = aws_byte_cursor_from_c_str("application/xml"),
  154. };
  155. aws_http_message_add_header(list_parts_request, accept_header);
  156. aws_http_message_set_request_method(list_parts_request, aws_http_method_get);
  157. *out_message = list_parts_request;
  158. return AWS_OP_SUCCESS;
  159. }
  160. struct aws_s3_paginator *aws_s3_initiate_list_parts(
  161. struct aws_allocator *allocator,
  162. const struct aws_s3_list_parts_params *params) {
  163. AWS_FATAL_PRECONDITION(params);
  164. AWS_FATAL_PRECONDITION(params->client);
  165. AWS_FATAL_PRECONDITION(params->bucket_name.len);
  166. AWS_FATAL_PRECONDITION(params->key.len);
  167. AWS_FATAL_PRECONDITION(params->upload_id.len);
  168. AWS_FATAL_PRECONDITION(params->endpoint.len);
  169. struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data));
  170. operation_data->allocator = allocator;
  171. operation_data->key = aws_string_new_from_cursor(allocator, &params->key);
  172. operation_data->upload_id = aws_string_new_from_cursor(allocator, &params->upload_id);
  173. operation_data->on_part = params->on_part;
  174. operation_data->user_data = params->user_data;
  175. aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback);
  176. struct aws_byte_cursor xml_result_node_name = aws_byte_cursor_from_c_str("ListPartsResult");
  177. const struct aws_byte_cursor continuation_node_name = aws_byte_cursor_from_c_str("NextPartNumberMarker");
  178. struct aws_s3_paginated_operation_params operation_params = {
  179. .next_message = s_construct_next_request_http_message,
  180. .on_result_node_encountered_fn = s_on_list_bucket_result_node_encountered,
  181. .on_paginated_operation_cleanup = s_on_paginator_cleanup,
  182. .result_xml_node_name = &xml_result_node_name,
  183. .continuation_token_node_name = &continuation_node_name,
  184. .user_data = operation_data,
  185. };
  186. struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params);
  187. struct aws_s3_paginator_params paginator_params = {
  188. .client = params->client,
  189. .bucket_name = params->bucket_name,
  190. .endpoint = params->endpoint,
  191. .operation = operation,
  192. .on_page_finished_fn = params->on_list_finished,
  193. .user_data = params->user_data,
  194. };
  195. struct aws_s3_paginator *paginator = aws_s3_initiate_paginator(allocator, &paginator_params);
  196. // transfer control to paginator
  197. aws_s3_paginated_operation_release(operation);
  198. return paginator;
  199. }
  200. struct aws_s3_paginated_operation *aws_s3_list_parts_operation_new(
  201. struct aws_allocator *allocator,
  202. const struct aws_s3_list_parts_params *params) {
  203. AWS_FATAL_PRECONDITION(params);
  204. AWS_FATAL_PRECONDITION(params->key.len);
  205. AWS_FATAL_PRECONDITION(params->upload_id.len);
  206. struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data));
  207. operation_data->allocator = allocator;
  208. operation_data->key = aws_string_new_from_cursor(allocator, &params->key);
  209. operation_data->upload_id = aws_string_new_from_cursor(allocator, &params->upload_id);
  210. operation_data->on_part = params->on_part;
  211. operation_data->user_data = params->user_data;
  212. aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback);
  213. struct aws_byte_cursor xml_result_node_name = aws_byte_cursor_from_c_str("ListPartsResult");
  214. const struct aws_byte_cursor continuation_node_name = aws_byte_cursor_from_c_str("NextPartNumberMarker");
  215. struct aws_s3_paginated_operation_params operation_params = {
  216. .next_message = s_construct_next_request_http_message,
  217. .on_result_node_encountered_fn = s_on_list_bucket_result_node_encountered,
  218. .on_paginated_operation_cleanup = s_on_paginator_cleanup,
  219. .result_xml_node_name = &xml_result_node_name,
  220. .continuation_token_node_name = &continuation_node_name,
  221. .user_data = operation_data,
  222. };
  223. struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params);
  224. return operation;
  225. }