Debian Codegraph @

crt/aws-c-s3/samples/s3/s3-ls.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/condition_variable.h>
8 #include <aws/common/mutex.h>
9 #include <aws/common/zero.h>
10 #include <aws/io/uri.h>
11 #include <aws/s3/private/s3_list_objects.h>
12
13 #include "app_ctx.h"
14
15 #include <inttypes.h>
16 #include <stdio.h>
17
18 struct s3_ls_app_data {
19 struct aws_uri uri;
20 struct app_ctx *app_ctx;
21 struct aws_mutex mutex;
22 struct aws_condition_variable cvar;
23 bool execution_completed;
24 bool long_format;
25 };
26
27 static void s_usage(int exit_code) {
28 FILE *output = exit_code == 0 ? stdout : stderr;
29 fprintf(output, "usage: s3 ls [options] s3://{bucket}[/prefix]\n");
30 fprintf(output, " bucket: the S3 bucket to list objects\n");
31 fprintf(output, " prefix: the prefix to filter\n");
32 fprintf(output, " -l, List in long format\n");
33 fprintf(output, " -h, --help\n");
34 fprintf(output, " Display this message and quit.\n");
35 exit(exit_code);
36 }
37
38 static struct aws_cli_option s_long_options[] = {
39 {"long-format", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'l'},
40 /* Per getopt(3) the last element of the array has to be filled with all zeros */
41 {NULL, AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 0},
42 };
43
44 static void s_parse_options(int argc, char **argv, struct s3_ls_app_data *ctx) {
45 int option_index = 0;
46
47 int opt_val = 0;
48 bool uri_found = false;
49 do {
50 opt_val = aws_cli_getopt_long(argc, argv, "l", s_long_options, &option_index);
51 /* START_OF_TEXT means our positional argument */
52 if (opt_val == 'l') {
53 ctx->long_format = true;
54 }
55 if (opt_val == 0x02) {
56 struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str(aws_cli_positional_arg);
57
58 if (aws_uri_init_parse(&ctx->uri, ctx->app_ctx->allocator, &uri_cursor)) {
59 fprintf(
60 stderr,
61 "Failed to parse uri %s with error %s\n",
62 (char *)uri_cursor.ptr,
63 aws_error_debug_str(aws_last_error()));
64 s_usage(1);
65 }
66 uri_found = true;
67 }
68 } while (opt_val != -1);
69
70 if (!uri_found) {
71 fprintf(stderr, "A URI for the request must be supplied.\n");
72 s_usage(1);
73 }
74 }
75
76 /**
77 * Predicate used to decide if the application is ready to exit.
78 * The corresponding condition variable is set when the last
79 * page of ListObjects is received.
80 */
81 static bool s_app_completion_predicate(void *arg) {
82 struct s3_ls_app_data *app_ctx = arg;
83 return app_ctx->execution_completed;
84 }
85
86 /**
87 * Called once for each object returned in the ListObjectsV2 responses.
88 */
89 int s_on_object(const struct aws_s3_object_info *info, void *user_data) {
90 struct s3_ls_app_data *app_ctx = user_data;
91
92 if (app_ctx->long_format) {
93 printf("%-18" PRIu64 " ", info->size);
94 }
95 printf("%.*s\n", (int)info->key.len, info->key.ptr);
96 return AWS_OP_SUCCESS;
97 }
98
99 /**
100 * Called once for each ListObjectsV2 response received.
101 * If the response contains a continuation token indicating there are more results to be fetched,
102 * requests the next page using aws_s3_paginator_continue.
103 */
104 void s_on_list_finished(struct aws_s3_paginator *paginator, int error_code, void *user_data) {
105 struct s3_ls_app_data *app_ctx = user_data;
106
107 if (error_code == 0) {
108 bool has_more_results = aws_s3_paginator_has_more_results(paginator);
109 if (has_more_results) {
110 /* get next page */
111 int result = aws_s3_paginator_continue(paginator, &app_ctx->app_ctx->signing_config);
112 if (result) {
113 fprintf(stderr, "ERROR returned by aws_s3_paginator_continue from s_on_list_finished: %d\n", result);
114 }
115 return;
116 }
117 } else {
118 fprintf(
119 stderr,
120 "Failure while listing objects. Please check if you have valid credentials and s3 path is correct. "
121 "Error: "
122 "%s\n",
123 aws_error_debug_str(error_code));
124 }
125
126 /* all pages received. triggers the condition variable to exit the application. */
127 aws_mutex_lock(&app_ctx->mutex);
128 app_ctx->execution_completed = true;
129 aws_mutex_unlock(&app_ctx->mutex);
130 aws_condition_variable_notify_one(&app_ctx->cvar);
131 }
132
133 int s3_ls_main(int argc, char *argv[], const char *command_name, void *user_data) {
134 (void)command_name;
135 struct app_ctx *app_ctx = user_data;
136
137 if (app_ctx->help_requested) {
138 s_usage(0);
139 }
140
141 if (!app_ctx->region) {
142 fprintf(stderr, "region is a required argument\n");
143 s_usage(1);
144 }
145
146 struct s3_ls_app_data impl_data = {
147 .app_ctx = app_ctx,
148 .mutex = AWS_MUTEX_INIT,
149 .cvar = AWS_CONDITION_VARIABLE_INIT,
150 };
151
152 app_ctx->sub_command_data = &impl_data;
153
154 s_parse_options(argc, argv, &impl_data);
155
156 struct aws_byte_cursor bucket = impl_data.uri.host_name;
157 struct aws_byte_cursor prefix;
158 if (impl_data.uri.path.len == 0 || (impl_data.uri.path.len == 1 && impl_data.uri.path.ptr[0] == '/')) {
159 prefix.len = 0;
160 prefix.ptr = NULL;
161 } else {
162 /* skips the initial / in the path */
163 prefix.len = impl_data.uri.path.len - 1;
164 prefix.ptr = impl_data.uri.path.ptr + 1;
165 }
166
167 /* listObjects */
168 struct aws_s3_list_objects_params params = {.client = app_ctx->client, .bucket_name = bucket, .prefix = prefix};
169
170 char endpoint[1024];
171 snprintf(endpoint, sizeof(endpoint), "s3.%s.amazonaws.com", app_ctx->region);
172 params.endpoint = aws_byte_cursor_from_c_str(endpoint);
173 params.user_data = &impl_data;
174 params.on_object = &s_on_object;
175 params.on_list_finished = &s_on_list_finished;
176
177 struct aws_s3_paginator *paginator = aws_s3_initiate_list_objects(app_ctx->allocator, &params);
178 int paginator_result = aws_s3_paginator_continue(paginator, &app_ctx->signing_config);
179 if (paginator_result) {
180 printf("ERROR returned from initial call to aws_s3_paginator_continue: %d \n", paginator_result);
181 }
182
183 aws_s3_paginator_release(paginator);
184
185 /* wait completion of last page */
186 aws_mutex_lock(&impl_data.mutex);
187 aws_condition_variable_wait_pred(&impl_data.cvar, &impl_data.mutex, s_app_completion_predicate, &impl_data);
188 aws_mutex_unlock(&impl_data.mutex);
189
190 return 0;
191 }