Debian Codegraph @

crt/aws-c-s3/samples/s3/s3-cp.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/command_line_parser.h>
7 #include <aws/common/file.h>
8 #include <aws/io/stream.h>
9 #include <aws/io/uri.h>
10 #include <aws/s3/private/s3_auto_ranged_put.h>
11 #include <aws/s3/private/s3_copy_object.h>
12 #include <aws/s3/private/s3_list_objects.h>
13
14 #include "app_ctx.h"
15 #include "cli_progress_bar.h"
16
17 #include <inttypes.h>
18 #include <stdio.h>
19
20 #ifdef _MSC_VER
21 # pragma warning(disable : 4706) /* assignment in conditional */
22 #endif /* _MSC_VER */
23
24 struct cp_app_ctx {
25 struct app_ctx *app_ctx;
26 struct aws_uri source_uri;
27 struct aws_uri destination_uri;
28 struct progress_listener_group *listener_group;
29 struct aws_mutex mutex;
30 struct aws_condition_variable c_var;
31 const char *source_endpoint;
32 const char *dest_endpoint;
33 size_t expected_transfers;
34 size_t completed_transfers;
35 bool list_objects_completed;
36 bool source_s3;
37 bool source_file_system;
38 bool dest_s3;
39 bool dest_file_system;
40 bool source_is_directory_or_prefix;
41 };
42
43 struct single_transfer_ctx {
44 struct cp_app_ctx *cp_app_ctx;
45 struct progress_listener *listener;
46 struct aws_s3_meta_request *meta_request;
47 FILE *output_sink;
48 };
49
50 static void s_usage(int exit_code) {
51 FILE *sink = exit_code == 0 ? stdout : stderr;
52
53 fprintf(
54 sink,
55 "usage: s3-cp [options] source_path "
56 "destination_path\n");
57 fprintf(
58 sink,
59 " source_path: an S3 bucket containing the object prefix to copy, or a local filesystem path to upload.\n");
60 fprintf(sink, " In the case of an S3 bucket, use URI format S3://{bucket_name}/{prefix_and_or_key}.\n");
61
62 fprintf(
63 sink,
64 " destination_path: an S3 bucket containing the object prefix to copy, or a local filesystem path to download "
65 "to.\n");
66 fprintf(sink, " In the case of an S3 bucket, use URI format S3://{bucket_name}/{prefix_and_or_key}.\n");
67 fprintf(sink, " If both the source_path and destination_path are S3 URIs it will attempt an object copy. \n");
68 fprintf(sink, " Note: both buckets must be in the same region for the copy to work\n");
69 exit(exit_code);
70 }
71
72 static struct aws_cli_option s_long_options[] = {
73 /* Per getopt(3) the last element of the array has to be filled with all zeros */
74 {NULL, AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 0},
75 };
76
77 static void s_parse_options(int argc, char **argv, struct cp_app_ctx *ctx) {
78 bool src_uri_found = false;
79 bool dest_uri_found = false;
80 int option_index = 0;
81 int opt_val = -1;
82
83 do {
84 opt_val = aws_cli_getopt_long(argc, argv, "", s_long_options, &option_index);
85 /* START_OF_TEXT means our positional argument */
86 if (opt_val == 0x02) {
87 struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str(aws_cli_positional_arg);
88
89 struct aws_uri *uri_to_parse = !src_uri_found ? &ctx->source_uri : &ctx->destination_uri;
90
91 if (aws_uri_init_parse(uri_to_parse, ctx->app_ctx->allocator, &uri_cursor)) {
92 fprintf(
93 stderr,
94 "Failed to parse uri %s with error %s\n",
95 (char *)uri_cursor.ptr,
96 aws_error_debug_str(aws_last_error()));
97 s_usage(1);
98 }
99
100 if (uri_to_parse == &ctx->source_uri) {
101 src_uri_found = true;
102 } else {
103 dest_uri_found = true;
104 }
105 }
106 } while (opt_val != -1);
107
108 if (!(src_uri_found && dest_uri_found)) {
109 fprintf(stderr, "An URI for the source and destination must be provided.\n");
110 s_usage(1);
111 }
112 }
113
114 static void s_dispatch_and_run_transfers(struct cp_app_ctx *cp_app_ctx);
115
116 int s3_cp_main(int argc, char *argv[], const char *command_name, void *user_data) {
117 (void)command_name;
118
119 struct app_ctx *app_ctx = user_data;
120
121 if (app_ctx->help_requested) {
122 s_usage(0);
123 }
124
125 if (!app_ctx->region) {
126 fprintf(stderr, "region is a required argument\n");
127 s_usage(1);
128 }
129
130 struct cp_app_ctx cp_app_ctx = {
131 .app_ctx = app_ctx,
132 .mutex = AWS_MUTEX_INIT,
133 .c_var = AWS_CONDITION_VARIABLE_INIT,
134 };
135 app_ctx->sub_command_data = &cp_app_ctx;
136
137 s_parse_options(argc, argv, &cp_app_ctx);
138
139 cp_app_ctx.listener_group = progress_listener_group_new(app_ctx->allocator);
140 progress_listener_group_run_background_render_thread(cp_app_ctx.listener_group);
141
142 char source_endpoint[1024];
143 AWS_ZERO_ARRAY(source_endpoint);
144 char dest_endpoint[1024];
145 AWS_ZERO_ARRAY(dest_endpoint);
146
147 struct aws_byte_cursor s3_scheme = aws_byte_cursor_from_c_str("s3");
148 struct aws_byte_cursor file_scheme = aws_byte_cursor_from_c_str("file");
149
150 if (aws_byte_cursor_eq_ignore_case(&cp_app_ctx.source_uri.scheme, &s3_scheme)) {
151 cp_app_ctx.source_s3 = true;
152 cp_app_ctx.source_is_directory_or_prefix = true;
153
154 struct aws_byte_cursor source_bucket = cp_app_ctx.source_uri.host_name;
155
156 snprintf(
157 source_endpoint,
158 sizeof(source_endpoint),
159 "%.*s.s3.%s.amazonaws.com",
160 (int)source_bucket.len,
161 source_bucket.ptr,
162 app_ctx->region);
163
164 cp_app_ctx.source_endpoint = source_endpoint;
165 } else if (
166 aws_byte_cursor_eq_ignore_case(&cp_app_ctx.source_uri.scheme, &file_scheme) ||
167 cp_app_ctx.source_uri.scheme.len == 0) {
168 cp_app_ctx.source_file_system = true;
169
170 struct aws_string *path_str = aws_string_new_from_buf(app_ctx->allocator, &cp_app_ctx.source_uri.uri_str);
171 struct aws_string *path_open_mode = aws_string_new_from_c_str(app_ctx->allocator, "r");
172
173 FILE *file_open_check = NULL;
174 if (aws_directory_exists(path_str)) {
175 cp_app_ctx.source_is_directory_or_prefix = true;
176 } else if ((file_open_check = aws_fopen_safe(path_str, path_open_mode))) {
177 cp_app_ctx.source_is_directory_or_prefix = false;
178 fclose(file_open_check);
179 } else {
180 fprintf(stderr, "Source path does not exist\n");
181 s_usage(1);
182 }
183
184 aws_string_destroy(path_open_mode);
185 aws_string_destroy(path_str);
186 } else {
187 fprintf(stderr, "Source URI type is unsupported. s3://, file://, or / are currently supported\n");
188 s_usage(1);
189 }
190
191 if (aws_byte_cursor_eq_ignore_case(&cp_app_ctx.destination_uri.scheme, &s3_scheme)) {
192 cp_app_ctx.dest_s3 = true;
193
194 struct aws_byte_cursor destination_bucket = cp_app_ctx.destination_uri.host_name;
195
196 snprintf(
197 dest_endpoint,
198 sizeof(dest_endpoint),
199 "%.*s.s3.%s.amazonaws.com",
200 (int)destination_bucket.len,
201 destination_bucket.ptr,
202 app_ctx->region);
203
204 cp_app_ctx.dest_endpoint = dest_endpoint;
205
206 } else if (
207 aws_byte_cursor_eq_ignore_case(&cp_app_ctx.destination_uri.scheme, &file_scheme) ||
208 cp_app_ctx.destination_uri.scheme.len == 0) {
209 cp_app_ctx.dest_file_system = true;
210 } else {
211 fprintf(stderr, "Destination URI type is unsupported. s3://, file://, or / are currently supported\n");
212 s_usage(1);
213 }
214
215 s_dispatch_and_run_transfers(&cp_app_ctx);
216
217 aws_condition_variable_clean_up(&cp_app_ctx.c_var);
218 aws_mutex_clean_up(&cp_app_ctx.mutex);
219
220 return 0;
221 }
222
223 /* this stream wrapper is purely for updating progress bars on upload */
224 struct progress_update_stream {
225 struct aws_input_stream base;
226 struct single_transfer_ctx *transfer;
227 struct aws_input_stream *wrapped_stream;
228 struct aws_allocator *allocator;
229 };
230
231 int s_input_seek(struct aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis) {
232 struct progress_update_stream *update_stream = AWS_CONTAINER_OF(stream, struct progress_update_stream, base);
233
234 if (basis == AWS_SSB_BEGIN && offset == 0) {
235 progress_listener_reset_progress(update_stream->transfer->listener);
236 struct aws_string *state =
237 aws_string_new_from_c_str(update_stream->transfer->cp_app_ctx->app_ctx->allocator, "In Progress");
238 progress_listener_update_state(update_stream->transfer->listener, state);
239 aws_string_destroy(state);
240 }
241
242 return aws_input_stream_seek(update_stream->wrapped_stream, offset, basis);
243 }
244
245 int s_input_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) {
246 struct progress_update_stream *update_stream = AWS_CONTAINER_OF(stream, struct progress_update_stream, base);
247
248 size_t current_len = dest->len;
249 int val = aws_input_stream_read(update_stream->wrapped_stream, dest);
250 size_t progress = dest->len - current_len;
251 progress_listener_update_progress(update_stream->transfer->listener, progress);
252 return val;
253 }
254
255 static int s_input_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) {
256 struct progress_update_stream *update_stream = AWS_CONTAINER_OF(stream, struct progress_update_stream, base);
257
258 return aws_input_stream_get_status(update_stream->wrapped_stream, status);
259 }
260
261 static int s_input_get_length(struct aws_input_stream *stream, int64_t *out_length) {
262 struct progress_update_stream *update_stream = AWS_CONTAINER_OF(stream, struct progress_update_stream, base);
263 return aws_input_stream_get_length(update_stream->wrapped_stream, out_length);
264 }
265
266 static void s_input_destroy(void *data) {
267 struct progress_update_stream *update_stream = (struct progress_update_stream *)data;
268 aws_input_stream_release(update_stream->wrapped_stream);
269 aws_mem_release(update_stream->allocator, update_stream);
270 }
271
272 static struct aws_input_stream_vtable s_update_input_stream_vtable = {
273 .get_length = s_input_get_length,
274 .seek = s_input_seek,
275 .read = s_input_read,
276 .get_status = s_input_get_status,
277 };
278 /* end of stream for the progress bar */
279
280 static const struct aws_byte_cursor g_host_header_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host");
281 static const struct aws_byte_cursor g_x_amz_copy_source_name =
282 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source");
283
284 /* helper function for getting an http request to make the copy object request. */
285 static struct aws_http_message *s_copy_object_request_new(
286 struct cp_app_ctx *cp_app_ctx,
287 const struct aws_byte_cursor *source_bucket,
288 const struct aws_byte_cursor *source_key,
289 const struct aws_byte_cursor *destination_key) {
290 struct aws_http_message *message = aws_http_message_new_request(cp_app_ctx->app_ctx->allocator);
291
292 if (aws_http_message_set_request_path(message, *destination_key)) {
293 goto error_clean_up_message;
294 }
295
296 struct aws_byte_cursor endpoint = aws_byte_cursor_from_c_str(cp_app_ctx->dest_endpoint);
297 struct aws_http_header host_header = {.name = g_host_header_name, .value = endpoint};
298 if (aws_http_message_add_header(message, host_header)) {
299 goto error_clean_up_message;
300 }
301
302 char copy_source_value[1024];
303 snprintf(
304 copy_source_value,
305 sizeof(copy_source_value),
306 "%.*s/%.*s",
307 (int)source_bucket->len,
308 source_bucket->ptr,
309 (int)source_key->len,
310 source_key->ptr);
311
312 struct aws_byte_cursor copy_source_cursor = aws_byte_cursor_from_c_str(copy_source_value);
313 struct aws_byte_buf copy_source_value_encoded;
314 aws_byte_buf_init(&copy_source_value_encoded, cp_app_ctx->app_ctx->allocator, 1024);
315 aws_byte_buf_append_encoding_uri_param(&copy_source_value_encoded, &copy_source_cursor);
316
317 struct aws_http_header copy_source_header = {
318 .name = g_x_amz_copy_source_name,
319 .value = aws_byte_cursor_from_buf(&copy_source_value_encoded),
320 };
321 if (aws_http_message_add_header(message, copy_source_header)) {
322 goto error_clean_up_message;
323 }
324
325 if (aws_http_message_set_request_method(message, aws_http_method_put)) {
326 goto error_clean_up_message;
327 }
328
329 aws_byte_buf_clean_up(&copy_source_value_encoded);
330 return message;
331
332 error_clean_up_message:
333
334 aws_byte_buf_clean_up(&copy_source_value_encoded);
335 if (message != NULL) {
336 aws_http_message_release(message);
337 message = NULL;
338 }
339
340 return NULL;
341 }
342
343 /* upon copy object progress, update the progress bar. */
344 static void s_copy_object_progress(
345 struct aws_s3_meta_request *meta_request,
346 const struct aws_s3_meta_request_progress *progress,
347 void *user_data) {
348 (void)meta_request;
349
350 struct single_transfer_ctx *transfer_ctx = user_data;
351
352 progress_listener_update_max_value(transfer_ctx->listener, progress->content_length);
353 progress_listener_update_progress(transfer_ctx->listener, progress->bytes_transferred);
354 }
355
356 /* invoked upon the completion of a copy object request. */
357 static void s_copy_object_request_finish(
358 struct aws_s3_meta_request *meta_request,
359 const struct aws_s3_meta_request_result *meta_request_result,
360 void *user_data) {
361 (void)meta_request;
362 struct single_transfer_ctx *transfer_ctx = user_data;
363
364 struct aws_string *new_state = NULL;
365 if (meta_request_result->error_code == AWS_ERROR_SUCCESS) {
366 new_state = aws_string_new_from_c_str(transfer_ctx->cp_app_ctx->app_ctx->allocator, "Completed");
367 progress_listener_update_progress(transfer_ctx->listener, 100);
368 } else {
369 new_state = aws_string_new_from_c_str(transfer_ctx->cp_app_ctx->app_ctx->allocator, "Error");
370 }
371
372 progress_listener_update_state(transfer_ctx->listener, new_state);
373 aws_s3_meta_request_release(transfer_ctx->meta_request);
374
375 aws_mutex_lock(&transfer_ctx->cp_app_ctx->mutex);
376 transfer_ctx->cp_app_ctx->completed_transfers++;
377 aws_mutex_unlock(&transfer_ctx->cp_app_ctx->mutex);
378 aws_condition_variable_notify_one(&transfer_ctx->cp_app_ctx->c_var);
379
380 aws_mem_release(transfer_ctx->cp_app_ctx->app_ctx->allocator, transfer_ctx);
381 }
382
383 /* create a copy object request and send it based on the source bucket/key and destination bucket/key. */
384 static int s_kick_off_copy_object_request(
385 struct cp_app_ctx *cp_app_ctx,
386 const struct aws_byte_cursor *source_bucket,
387 const struct aws_byte_cursor *source_key,
388 const struct aws_byte_cursor *destination_key) {
389 struct aws_http_message *message =
390 s_copy_object_request_new(cp_app_ctx, source_bucket, source_key, destination_key);
391
392 struct single_transfer_ctx *transfer_ctx =
393 aws_mem_calloc(cp_app_ctx->app_ctx->allocator, 1, sizeof(struct single_transfer_ctx));
394 transfer_ctx->cp_app_ctx = cp_app_ctx;
395
396 struct aws_byte_buf label_buf;
397 struct aws_byte_cursor label_start_cur = aws_byte_cursor_from_c_str("copy: s3://");
398 aws_byte_buf_init_copy_from_cursor(&label_buf, cp_app_ctx->app_ctx->allocator, label_start_cur);
399 aws_byte_buf_append_dynamic(&label_buf, source_bucket);
400 aws_byte_buf_append_byte_dynamic(&label_buf, '/');
401 aws_byte_buf_append_dynamic(&label_buf, source_key);
402 struct aws_byte_cursor to_cur = aws_byte_cursor_from_c_str(" to s3://");
403 aws_byte_buf_append_dynamic(&label_buf, &to_cur);
404 aws_byte_buf_append_dynamic(&label_buf, &cp_app_ctx->destination_uri.host_name);
405 aws_byte_buf_append_dynamic(&label_buf, destination_key);
406
407 struct aws_string *label = aws_string_new_from_buf(cp_app_ctx->app_ctx->allocator, &label_buf);
408 aws_byte_buf_clean_up(&label_buf);
409 struct aws_string *state = aws_string_new_from_c_str(cp_app_ctx->app_ctx->allocator, "In Progress");
410
411 transfer_ctx->listener = progress_listener_new(cp_app_ctx->listener_group, label, state, 100);
412
413 struct aws_s3_meta_request_options meta_request_options = {
414 .user_data = transfer_ctx,
415 .body_callback = NULL,
416 .signing_config = &cp_app_ctx->app_ctx->signing_config,
417 .finish_callback = s_copy_object_request_finish,
418 .headers_callback = NULL,
419 .message = message,
420 .shutdown_callback = NULL,
421 .progress_callback = s_copy_object_progress,
422 .type = AWS_S3_META_REQUEST_TYPE_COPY_OBJECT,
423 };
424
425 transfer_ctx->meta_request = aws_s3_client_make_meta_request(cp_app_ctx->app_ctx->client, &meta_request_options);
426 if (transfer_ctx->meta_request == NULL) {
427 fprintf(
428 stderr,
429 "Failure when initiating copy object request with error %s\n",
430 aws_error_debug_str(aws_last_error()));
431 exit(1);
432 }
433
434 aws_mutex_lock(&transfer_ctx->cp_app_ctx->mutex);
435 transfer_ctx->cp_app_ctx->expected_transfers++;
436 aws_mutex_unlock(&transfer_ctx->cp_app_ctx->mutex);
437
438 return AWS_OP_SUCCESS;
439 }
440
441 /* invoked upon an upload completion. */
442 void s_put_request_finished(
443 struct aws_s3_meta_request *meta_request,
444 const struct aws_s3_meta_request_result *meta_request_result,
445 void *user_data) {
446 (void)meta_request;
447
448 struct single_transfer_ctx *transfer_ctx = user_data;
449
450 struct aws_string *state = NULL;
451 if (meta_request_result->error_code == AWS_ERROR_SUCCESS) {
452 state = aws_string_new_from_c_str(transfer_ctx->cp_app_ctx->app_ctx->allocator, "Completed");
453 } else {
454 state = aws_string_new_from_c_str(transfer_ctx->cp_app_ctx->app_ctx->allocator, "Failed");
455 }
456
457 progress_listener_update_state(transfer_ctx->listener, state);
458 aws_string_destroy(state);
459
460 aws_mutex_lock(&transfer_ctx->cp_app_ctx->mutex);
461 transfer_ctx->cp_app_ctx->completed_transfers++;
462 aws_mutex_unlock(&transfer_ctx->cp_app_ctx->mutex);
463 aws_condition_variable_notify_one(&transfer_ctx->cp_app_ctx->c_var);
464 aws_s3_meta_request_release(transfer_ctx->meta_request);
465 aws_mem_release(transfer_ctx->cp_app_ctx->app_ctx->allocator, transfer_ctx);
466 }
467
468 static bool s_are_all_transfers_done(void *arg) {
469 struct cp_app_ctx *cp_app_ctx = arg;
470 return cp_app_ctx->expected_transfers == cp_app_ctx->completed_transfers;
471 }
472
473 /* kick off a PUT request for an object from a file on disk */
474 static int s_kickoff_put_object(
475 struct cp_app_ctx *cp_app_ctx,
476 const struct aws_byte_cursor *src_path,
477 const struct aws_byte_cursor *dest_path,
478 uint64_t file_size) {
479 struct single_transfer_ctx *transfer_ctx =
480 aws_mem_calloc(cp_app_ctx->app_ctx->allocator, 1, sizeof(struct single_transfer_ctx));
481 transfer_ctx->cp_app_ctx = cp_app_ctx;
482
483 struct aws_byte_buf uri_path;
484 struct aws_byte_cursor destination_path = *dest_path;
485
486 aws_byte_buf_init_copy_from_cursor(&uri_path, cp_app_ctx->app_ctx->allocator, cp_app_ctx->destination_uri.path);
487 aws_byte_buf_append_dynamic(&uri_path, &destination_path);
488
489 for (size_t i = 0; i < uri_path.len; ++i) {
490 if (uri_path.buffer[i] == '\\') {
491 uri_path.buffer[i] = '/';
492 }
493 }
494
495 struct aws_byte_cursor full_path = aws_byte_cursor_from_buf(&uri_path);
496
497 if (uri_path.buffer[0] == '.') {
498 aws_byte_cursor_advance(&full_path, 1);
499 }
500
501 struct aws_byte_buf label_buf;
502 struct aws_byte_cursor operation_name_cur = aws_byte_cursor_from_c_str("upload: ");
503 aws_byte_buf_init_copy_from_cursor(&label_buf, cp_app_ctx->app_ctx->allocator, operation_name_cur);
504 aws_byte_buf_append_dynamic(&label_buf, dest_path);
505 struct aws_byte_cursor to_cur = aws_byte_cursor_from_c_str(" to s3://");
506 aws_byte_buf_append_dynamic(&label_buf, &to_cur);
507 aws_byte_buf_append_dynamic(&label_buf, &cp_app_ctx->destination_uri.authority);
508 aws_byte_buf_append_dynamic(&label_buf, &full_path);
509
510 struct aws_string *label = aws_string_new_from_buf(cp_app_ctx->app_ctx->allocator, &label_buf);
511 aws_byte_buf_clean_up(&label_buf);
512
513 struct aws_string *state = aws_string_new_from_c_str(cp_app_ctx->app_ctx->allocator, "In Progress");
514
515 transfer_ctx->listener = progress_listener_new(cp_app_ctx->listener_group, label, state, file_size);
516 aws_string_destroy(state);
517 aws_string_destroy(label);
518 aws_byte_buf_clean_up(&label_buf);
519
520 struct aws_s3_meta_request_options request_options = {
521 .user_data = transfer_ctx,
522 .signing_config = &cp_app_ctx->app_ctx->signing_config,
523 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
524 .finish_callback = s_put_request_finished,
525 };
526
527 struct aws_http_header host_header = {
528 .name = g_host_header_name,
529 .value = aws_byte_cursor_from_c_str(cp_app_ctx->dest_endpoint),
530 };
531
532 char content_length[256];
533 AWS_ZERO_ARRAY(content_length);
534 snprintf(content_length, AWS_ARRAY_SIZE(content_length), "%" PRIu64, file_size);
535
536 struct aws_http_header content_length_header = {
537 .name = aws_byte_cursor_from_c_str("content-length"),
538 .value = aws_byte_cursor_from_c_str(content_length),
539 };
540
541 request_options.message = aws_http_message_new_request(cp_app_ctx->app_ctx->allocator);
542 aws_http_message_add_header(request_options.message, host_header);
543 aws_http_message_add_header(request_options.message, content_length_header);
544 aws_http_message_set_request_method(request_options.message, aws_http_method_put);
545 aws_http_message_set_request_path(request_options.message, full_path);
546
547 struct aws_input_stream *body_input =
548 aws_input_stream_new_from_file(cp_app_ctx->app_ctx->allocator, (const char *)src_path->ptr);
549
550 if (!body_input) {
551 aws_mem_release(cp_app_ctx->app_ctx->allocator, transfer_ctx);
552 return AWS_OP_ERR;
553 }
554
555 struct progress_update_stream *update_stream =
556 aws_mem_calloc(cp_app_ctx->app_ctx->allocator, 1, sizeof(struct progress_update_stream));
557 update_stream->transfer = transfer_ctx;
558 update_stream->wrapped_stream = body_input;
559 update_stream->allocator = cp_app_ctx->app_ctx->allocator;
560 update_stream->base.vtable = &s_update_input_stream_vtable;
561 aws_ref_count_init(&update_stream->base.ref_count, update_stream, s_input_destroy);
562
563 struct aws_input_stream *wrap_stream = &update_stream->base;
564 aws_http_message_set_body_stream(request_options.message, wrap_stream);
565
566 transfer_ctx->meta_request = aws_s3_client_make_meta_request(cp_app_ctx->app_ctx->client, &request_options);
567 /* message owns the stream */
568 aws_input_stream_release(wrap_stream);
569 aws_http_message_release(request_options.message);
570
571 if (!transfer_ctx->meta_request) {
572 aws_mem_release(cp_app_ctx->app_ctx->allocator, transfer_ctx);
573 return AWS_OP_ERR;
574 }
575
576 aws_mutex_lock(&transfer_ctx->cp_app_ctx->mutex);
577 transfer_ctx->cp_app_ctx->expected_transfers++;
578 aws_mutex_unlock(&transfer_ctx->cp_app_ctx->mutex);
579
580 return AWS_OP_SUCCESS;
581 }
582
583 /* invoked upon walking a directory. it's invoked for each entry found in the directory. */
584 static bool s_on_directory_entry(const struct aws_directory_entry *entry, void *user_data) {
585 struct cp_app_ctx *cp_app_ctx = user_data;
586
587 if (entry->file_type & AWS_FILE_TYPE_FILE) {
588 struct aws_byte_cursor escaped_dest_path = entry->relative_path;
589 aws_byte_cursor_advance(&escaped_dest_path, cp_app_ctx->source_uri.uri_str.len);
590 int ret_val = s_kickoff_put_object(cp_app_ctx, &entry->relative_path, &escaped_dest_path, entry->file_size);
591 return ret_val == AWS_OP_SUCCESS;
592 }
593
594 return true;
595 }
596
597 /* upon each byte downloaded, update the progress bar, and write to disk. */
598 int s_get_body_callback(
599 struct aws_s3_meta_request *meta_request,
600 const struct aws_byte_cursor *body,
601 uint64_t range_start,
602 void *user_data) {
603 (void)meta_request;
604 (void)range_start;
605
606 struct single_transfer_ctx *transfer_ctx = user_data;
607
608 fwrite(body->ptr, sizeof(uint8_t), body->len, transfer_ctx->output_sink);
609 progress_listener_update_progress(transfer_ctx->listener, body->len);
610
611 return AWS_OP_SUCCESS;
612 }
613
614 /* Invoked when the get object request completes. */
615 void s_get_request_finished(
616 struct aws_s3_meta_request *meta_request,
617 const struct aws_s3_meta_request_result *meta_request_result,
618 void *user_data) {
619 (void)meta_request;
620
621 struct single_transfer_ctx *transfer_ctx = user_data;
622
623 struct aws_string *state = NULL;
624 if (meta_request_result->error_code == AWS_ERROR_SUCCESS) {
625 state = aws_string_new_from_c_str(transfer_ctx->cp_app_ctx->app_ctx->allocator, "Completed");
626 } else {
627 state = aws_string_new_from_c_str(transfer_ctx->cp_app_ctx->app_ctx->allocator, "Failed");
628 }
629
630 progress_listener_update_state(transfer_ctx->listener, state);
631 aws_string_destroy(state);
632
633 fclose(transfer_ctx->output_sink);
634 aws_mutex_lock(&transfer_ctx->cp_app_ctx->mutex);
635 transfer_ctx->cp_app_ctx->completed_transfers++;
636 aws_mutex_unlock(&transfer_ctx->cp_app_ctx->mutex);
637 aws_condition_variable_notify_one(&transfer_ctx->cp_app_ctx->c_var);
638 aws_s3_meta_request_release(transfer_ctx->meta_request);
639 aws_mem_release(transfer_ctx->cp_app_ctx->app_ctx->allocator, transfer_ctx);
640 }
641
642 /* Setup a get object request and write the file to disk as it's downloaded. */
643 static int s_kickoff_get_object(
644 struct cp_app_ctx *cp_app_ctx,
645 const struct aws_byte_cursor *key,
646 const struct aws_byte_cursor *destination,
647 uint64_t size) {
648 struct single_transfer_ctx *transfer_ctx =
649 aws_mem_calloc(cp_app_ctx->app_ctx->allocator, 1, sizeof(struct single_transfer_ctx));
650 transfer_ctx->cp_app_ctx = cp_app_ctx;
651
652 struct aws_byte_buf label_buf;
653 struct aws_byte_cursor operation_name_cur = aws_byte_cursor_from_c_str("download: s3://");
654 aws_byte_buf_init_copy_from_cursor(&label_buf, cp_app_ctx->app_ctx->allocator, operation_name_cur);
655 aws_byte_buf_append_dynamic(&label_buf, &cp_app_ctx->source_uri.host_name);
656 struct aws_byte_cursor slash_cur = aws_byte_cursor_from_c_str("/");
657 aws_byte_buf_append_dynamic(&label_buf, &slash_cur);
658 aws_byte_buf_append_dynamic(&label_buf, key);
659 struct aws_byte_cursor to_cur = aws_byte_cursor_from_c_str(" to ");
660 aws_byte_buf_append_dynamic(&label_buf, &to_cur);
661 aws_byte_buf_append_dynamic(&label_buf, destination);
662
663 struct aws_string *label = aws_string_new_from_buf(cp_app_ctx->app_ctx->allocator, &label_buf);
664 aws_byte_buf_clean_up(&label_buf);
665
666 struct aws_string *state = aws_string_new_from_c_str(cp_app_ctx->app_ctx->allocator, "In Progress");
667
668 transfer_ctx->listener = progress_listener_new(cp_app_ctx->listener_group, label, state, size);
669 aws_string_destroy(state);
670 aws_string_destroy(label);
671 aws_byte_buf_clean_up(&label_buf);
672
673 struct aws_string *file_path = aws_string_new_from_cursor(cp_app_ctx->app_ctx->allocator, destination);
674 struct aws_string *mode = aws_string_new_from_c_str(cp_app_ctx->app_ctx->allocator, "wb");
675 transfer_ctx->output_sink = aws_fopen_safe(file_path, mode);
676 aws_string_destroy(mode);
677 aws_string_destroy(file_path);
678
679 if (!transfer_ctx->output_sink) {
680 return AWS_OP_ERR;
681 }
682
683 struct aws_s3_meta_request_options request_options = {
684 .user_data = transfer_ctx,
685 .signing_config = &cp_app_ctx->app_ctx->signing_config,
686 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
687 .finish_callback = s_get_request_finished,
688 .body_callback = s_get_body_callback,
689 };
690
691 struct aws_http_header host_header = {
692 .name = g_host_header_name,
693 .value = aws_byte_cursor_from_c_str(cp_app_ctx->source_endpoint),
694 };
695
696 struct aws_http_header accept_header = {
697 .name = aws_byte_cursor_from_c_str("accept"),
698 .value = aws_byte_cursor_from_c_str("*/*"),
699 };
700
701 struct aws_http_header user_agent_header = {
702 .name = aws_byte_cursor_from_c_str("user-agent"),
703 .value = aws_byte_cursor_from_c_str("AWS common runtime command-line client"),
704 };
705
706 request_options.message = aws_http_message_new_request(cp_app_ctx->app_ctx->allocator);
707 aws_http_message_add_header(request_options.message, host_header);
708 aws_http_message_add_header(request_options.message, accept_header);
709 aws_http_message_add_header(request_options.message, user_agent_header);
710 aws_http_message_set_request_method(request_options.message, aws_http_method_get);
711
712 struct aws_byte_buf path_buf;
713 aws_byte_buf_init(&path_buf, cp_app_ctx->app_ctx->allocator, key->len + 1);
714 aws_byte_buf_append_dynamic(&path_buf, &slash_cur);
715 aws_byte_buf_append_dynamic(&path_buf, key);
716 struct aws_byte_cursor path_cur = aws_byte_cursor_from_buf(&path_buf);
717 aws_http_message_set_request_path(request_options.message, path_cur);
718 aws_byte_buf_clean_up(&path_buf);
719
720 transfer_ctx->meta_request = aws_s3_client_make_meta_request(cp_app_ctx->app_ctx->client, &request_options);
721
722 if (!transfer_ctx->meta_request) {
723 return AWS_OP_ERR;
724 }
725
726 aws_mutex_lock(&transfer_ctx->cp_app_ctx->mutex);
727 transfer_ctx->cp_app_ctx->expected_transfers++;
728 aws_mutex_unlock(&transfer_ctx->cp_app_ctx->mutex);
729
730 return AWS_OP_SUCCESS;
731 }
732
733 /* upon listing the objects in a bucket, this is invoked for each object encountered. */
734 static int s_on_list_object(const struct aws_s3_object_info *info, void *user_data) {
735 struct cp_app_ctx *cp_app_ctx = user_data;
736
737 /* size greater than zero means it's an actual object. */
738 if (info->key.len > 0) {
739 struct aws_byte_cursor trimmed_key = info->key;
740
741 /* in this first case, if prefix and key are the same, we just download to the dest uri at key name. */
742 if (aws_byte_cursor_eq_ignore_case(&info->key, &info->prefix)) {
743 for (size_t i = trimmed_key.len - 1; i > 0; --i) {
744 if (trimmed_key.ptr[i] == '/') {
745 aws_byte_cursor_advance(&trimmed_key, i + 1);
746 break;
747 }
748 }
749 } else if (info->prefix.len) {
750 aws_byte_cursor_advance(&trimmed_key, info->prefix.len);
751 }
752
753 /* if we're going to be downloading to disk, set up the get object requests here. */
754 if (cp_app_ctx->dest_file_system) {
755 struct aws_byte_buf dest_directory;
756 aws_byte_buf_init_copy(
757 &dest_directory, cp_app_ctx->app_ctx->allocator, &cp_app_ctx->destination_uri.uri_str);
758
759 struct aws_string *dir_path = aws_string_new_from_buf(cp_app_ctx->app_ctx->allocator, &dest_directory);
760 if (!aws_directory_exists(dir_path)) {
761 aws_directory_create(dir_path);
762 }
763 aws_string_destroy(dir_path);
764
765 struct aws_array_list splits;
766 aws_array_list_init_dynamic(&splits, cp_app_ctx->app_ctx->allocator, 8, sizeof(struct aws_byte_cursor));
767 aws_byte_cursor_split_on_char(&trimmed_key, '/', &splits);
768
769 for (size_t i = 0; i < aws_array_list_length(&splits); ++i) {
770 struct aws_byte_cursor path_component;
771 aws_array_list_get_at(&splits, &path_component, i);
772
773 if (path_component.len > 0) {
774 if (dest_directory.buffer[dest_directory.len - 1] != AWS_PATH_DELIM) {
775 struct aws_byte_cursor slash_cur = aws_byte_cursor_from_c_str(AWS_PATH_DELIM_STR);
776 aws_byte_buf_append_dynamic(&dest_directory, &slash_cur);
777 }
778 aws_byte_buf_append_dynamic(&dest_directory, &path_component);
779
780 dir_path = aws_string_new_from_buf(cp_app_ctx->app_ctx->allocator, &dest_directory);
781 if (i < aws_array_list_length(&splits) - 1 && !aws_directory_exists(dir_path)) {
782 aws_directory_create(dir_path);
783 }
784 aws_string_destroy(dir_path);
785 }
786 }
787
788 struct aws_byte_cursor destination_cur = aws_byte_cursor_from_buf(&dest_directory);
789 int ret_val = s_kickoff_get_object(cp_app_ctx, &info->key, &destination_cur, info->size);
790 aws_byte_buf_clean_up(&dest_directory);
791
792 return ret_val;
793 }
794
795 /* otherwise, we're copying between buckets. Set up the copy here. */
796 struct aws_byte_buf destination_key;
797 aws_byte_buf_init_copy_from_cursor(
798 &destination_key, cp_app_ctx->app_ctx->allocator, cp_app_ctx->destination_uri.path);
799 aws_byte_buf_append_dynamic(&destination_key, &trimmed_key);
800 struct aws_byte_cursor destination_key_cur = aws_byte_cursor_from_buf(&destination_key);
801
802 int return_code = s_kick_off_copy_object_request(
803 cp_app_ctx, &cp_app_ctx->source_uri.host_name, &info->key, &destination_key_cur);
804
805 aws_byte_buf_clean_up(&destination_key);
806 return return_code;
807 }
808
809 return AWS_OP_SUCCESS;
810 }
811
812 static bool s_are_all_transfers_and_listings_done(void *arg) {
813 struct cp_app_ctx *cp_app_ctx = arg;
814 return cp_app_ctx->expected_transfers == cp_app_ctx->completed_transfers && cp_app_ctx->list_objects_completed;
815 }
816
817 void s_on_object_list_finished(struct aws_s3_paginator *paginator, int error_code, void *user_data) {
818
819 struct cp_app_ctx *cp_app_ctx = user_data;
820 if (error_code != AWS_OP_SUCCESS) {
821 fprintf(
822 stderr,
823 "Failure while listing objects. Please check if you have valid credentials and s3 path is correct. Error: "
824 "%s\n",
825 aws_error_debug_str(error_code));
826 exit(1);
827 }
828 if (aws_s3_paginator_has_more_results(paginator)) {
829 aws_s3_paginator_continue(paginator, &cp_app_ctx->app_ctx->signing_config);
830 } else {
831 aws_mutex_lock(&cp_app_ctx->mutex);
832 cp_app_ctx->list_objects_completed = true;
833 aws_mutex_unlock(&cp_app_ctx->mutex);
834 }
835 }
836
837 void s_dispatch_and_run_transfers(struct cp_app_ctx *cp_app_ctx) {
838
839 struct aws_s3_paginator *paginator = NULL;
840
841 /* the source argument is either a directory on disk or s3 (it's not actually possible to tell a prefix from an
842 * object) */
843 if (cp_app_ctx->source_is_directory_or_prefix) {
844 /* uploading a directory from disk to S3 */
845 if (cp_app_ctx->source_file_system) {
846 struct aws_string *path =
847 aws_string_new_from_buf(cp_app_ctx->app_ctx->allocator, &cp_app_ctx->source_uri.uri_str);
848
849 if (aws_directory_traverse(cp_app_ctx->app_ctx->allocator, path, true, s_on_directory_entry, cp_app_ctx)) {
850 fprintf(
851 stderr, "Failure while traversing directory. Error %s\n", aws_error_debug_str(aws_last_error()));
852 exit(1);
853 }
854
855 aws_string_destroy(path);
856 aws_mutex_lock(&cp_app_ctx->mutex);
857 aws_condition_variable_wait_pred(
858 &cp_app_ctx->c_var, &cp_app_ctx->mutex, s_are_all_transfers_done, cp_app_ctx);
859 aws_mutex_unlock(&cp_app_ctx->mutex);
860
861 /* due to trickery in list_objects, this handles the source is s3 key instead of just a prefix, so we
862 * don't need to handle that later.*/
863 } else {
864 /* this is an s3 bucket, and we'll be downloading it to disk. Incidentally, this also works for plain ole
865 * single objects. */
866 char main_endpoint[1024];
867 AWS_ZERO_ARRAY(main_endpoint);
868 snprintf(main_endpoint, sizeof(main_endpoint), "s3.%s.amazonaws.com", cp_app_ctx->app_ctx->region);
869 struct aws_byte_cursor prefix_cur = cp_app_ctx->source_uri.path;
870 aws_byte_cursor_advance(&prefix_cur, 1);
871 struct aws_s3_list_objects_params list_objects_params = {
872 .user_data = cp_app_ctx,
873 .endpoint = aws_byte_cursor_from_c_str(main_endpoint),
874 .client = cp_app_ctx->app_ctx->client,
875 .prefix = prefix_cur,
876 .bucket_name = cp_app_ctx->source_uri.host_name,
877 .on_object = s_on_list_object,
878 .on_list_finished = s_on_object_list_finished,
879 };
880
881 paginator = aws_s3_initiate_list_objects(cp_app_ctx->app_ctx->allocator, &list_objects_params);
882
883 if (!paginator) {
884 fprintf(stderr, "List objects failed with error %s\n", aws_error_debug_str(aws_last_error()));
885 s_usage(1);
886 }
887 aws_s3_paginator_continue(paginator, &cp_app_ctx->app_ctx->signing_config);
888
889 aws_mutex_lock(&cp_app_ctx->mutex);
890 aws_condition_variable_wait_pred(
891 &cp_app_ctx->c_var, &cp_app_ctx->mutex, s_are_all_transfers_and_listings_done, cp_app_ctx);
892 aws_mutex_unlock(&cp_app_ctx->mutex);
893
894 aws_s3_paginator_release(paginator);
895 }
896 } else {
897 /* only handles a single file from disk being uploaded to s3. */
898 if (cp_app_ctx->source_file_system) {
899 struct aws_byte_cursor source_path = aws_byte_cursor_from_buf(&cp_app_ctx->source_uri.uri_str);
900 int64_t source_file_length = 0;
901 struct aws_string *source_path_str =
902 aws_string_new_from_cursor(cp_app_ctx->app_ctx->allocator, &source_path);
903 struct aws_string *open_mode = aws_string_new_from_c_str(cp_app_ctx->app_ctx->allocator, "rb");
904 FILE *src_file = aws_fopen_safe(source_path_str, open_mode);
905
906 if (!src_file) {
907 fprintf(stderr, "File open failed with error %s\n", aws_error_debug_str(aws_last_error()));
908 s_usage(1);
909 }
910
911 aws_file_get_length(src_file, &source_file_length);
912 fclose(src_file);
913 aws_string_destroy(open_mode);
914 aws_string_destroy(source_path_str);
915
916 struct aws_byte_cursor file_name_portion = cp_app_ctx->source_uri.path;
917
918 /* just the final segment of the key */
919 for (size_t i = file_name_portion.len - 1; i > 0; --i) {
920 if (file_name_portion.ptr[i] == AWS_PATH_DELIM) {
921 aws_byte_cursor_advance(&file_name_portion, i + 1);
922 break;
923 }
924 }
925
926 if (s_kickoff_put_object(cp_app_ctx, &source_path, &file_name_portion, source_file_length)) {
927 fprintf(stderr, "File transfer failed with error %s\n", aws_error_debug_str(aws_last_error()));
928 s_usage(1);
929 }
930
931 aws_mutex_lock(&cp_app_ctx->mutex);
932 aws_condition_variable_wait_pred(
933 &cp_app_ctx->c_var, &cp_app_ctx->mutex, s_are_all_transfers_done, cp_app_ctx);
934 aws_mutex_unlock(&cp_app_ctx->mutex);
935 }
936 }
937 }