-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlistobjects.cpp
268 lines (242 loc) · 9.88 KB
/
listobjects.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "irods/private/s3_api/s3_api.hpp"
#include "irods/private/s3_api/authentication.hpp"
#include "irods/private/s3_api/bucket.hpp"
#include "irods/private/s3_api/common_routines.hpp"
#include "irods/private/s3_api/connection.hpp"
#include "irods/private/s3_api/log.hpp"
#include "irods/private/s3_api/common.hpp"
#include "irods/private/s3_api/session.hpp"
#include <boost/asio/awaitable.hpp>
#include <boost/asio/this_coro.hpp>
#include <experimental/coroutine>
#include <boost/beast.hpp>
#include <boost/asio.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/url.hpp>
#include <boost/lexical_cast.hpp>
#include <irods/filesystem.hpp>
#include <irods/query_builder.hpp>
#include <iostream>
#include <unordered_set>
#include <chrono>
#include <fmt/format.h>
namespace asio = boost::asio;
namespace beast = boost::beast;
namespace logging = irods::http::logging;
const static std::string_view date_format{"{:%Y-%m-%dT%H:%M:%S.000Z}"};
void irods::s3::actions::handle_listobjects_v2(
irods::http::session_pointer_type session_ptr,
boost::beast::http::request_parser<boost::beast::http::empty_body>& parser,
const boost::urls::url_view& url)
{
using namespace boost::property_tree;
beast::http::response<beast::http::empty_body> response;
auto irods_username = irods::s3::authentication::authenticates(parser, url);
if (!irods_username) {
response.result(beast::http::status::forbidden);
logging::debug("{}: returned {}", __FUNCTION__, response.reason());
session_ptr->send(std::move(response));
return;
}
auto conn = irods::get_connection(*irods_username);
auto rcComm_t_ptr = static_cast<RcComm*>(conn);
irods::experimental::filesystem::path bucket_base;
if (auto bucket = irods::s3::resolve_bucket(url.segments()); bucket.has_value()) {
bucket_base = bucket.value();
}
else {
response.result(beast::http::status::not_found);
logging::debug("{}: returned {}", __FUNCTION__, response.reason());
session_ptr->send(std::move(response));
return;
}
auto base_length = bucket_base.string().size();
auto resolved_path = irods::s3::finish_path(bucket_base, url.segments());
boost::property_tree::ptree document;
irods::experimental::filesystem::path the_prefix;
if (const auto prefix = url.params().find("prefix"); prefix != url.params().end()) {
the_prefix = (*prefix).value;
}
// For recursive searches, no delimiter is passed in. In that case only return all data objects
// which have the prefix.
// TODO: We might not be able to support delimiters that are not "/".
bool delimiter_in_request = false;
if (const auto prefix = url.params().find("delimiter"); prefix != url.params().end()) {
delimiter_in_request = true;
}
auto full_path = resolved_path / the_prefix;
std::string query;
document.add("ListBucketResult", "");
document.add("ListBucketResult.Name", bucket_base.c_str());
document.add("ListBucketResult.Prefix", the_prefix.c_str());
document.add("ListBucketResult.Marker", "");
document.add("ListBucketResult.IsTruncated", "false");
if (delimiter_in_request) {
if (full_path.object_name().empty()) {
// Path ends in a slash, this is an exact collection match
// and all objects in that collection
// Get exact collections underneath this collection
query = fmt::format(
"select COLL_NAME where COLL_NAME like '{}/%' and COLL_NAME not like '{}/%/%'",
full_path.parent_path().c_str(),
full_path.parent_path().c_str());
logging::debug("{}: query={}", __FUNCTION__, query);
for (auto&& row : irods::query<RcComm>(rcComm_t_ptr, query)) {
ptree object;
std::string key = (row[0].size() > base_length ? row[0].substr(base_length) : "");
if (key.starts_with("/")) {
key = key.substr(1);
}
key += "/";
object.put("Prefix", key);
document.add_child("ListBucketResult.CommonPrefixes", object);
}
// Get the data objects within the collection
query = fmt::format(
"select COLL_NAME, DATA_NAME, DATA_OWNER_NAME, DATA_SIZE, DATA_MODIFY_TIME where COLL_NAME = '{}'",
full_path.parent_path().c_str());
logging::debug("{}: query={}", __FUNCTION__, query);
for (auto&& row : irods::query<RcComm>(rcComm_t_ptr, query)) {
ptree object;
std::string key = (row[0].size() > base_length ? row[0].substr(base_length) : "") + "/" + row[1];
if (key.starts_with("/")) {
key = key.substr(1);
}
object.put("Key", key);
object.put("Etag", row[0] + row[1]);
object.put("Owner", row[2]);
object.put("Size", atoi(row[3].c_str()));
try {
std::time_t modified_epoch_time = boost::lexical_cast<std::time_t>(row[4]);
std::string modified_time_str =
irods::s3::api::common_routines::convert_time_t_to_str(modified_epoch_time, date_format);
object.put("LastModified", modified_time_str);
}
catch (const boost::bad_lexical_cast&) {
// do nothing - don't add LastModified tag
}
document.add_child("ListBucketResult.Contents", object);
}
}
else {
// Path does not end in a slash. This is a query for collections and data objects
// with a trailing wildcard.
// First get collections
query = fmt::format(
"select COLL_NAME where COLL_NAME like '{}%' and COLL_NAME not like '{}%/%'",
full_path.c_str(),
full_path.c_str());
logging::debug("{}: query={}", __FUNCTION__, query);
for (auto&& row : irods::query<RcComm>(rcComm_t_ptr, query)) {
ptree object;
std::string key = (row[0].size() > base_length ? row[0].substr(base_length) : "");
if (key.starts_with("/")) {
key = key.substr(1);
}
key += "/";
object.put("Prefix", key);
document.add_child("ListBucketResult.CommonPrefixes", object);
}
// Now get data objects
query = fmt::format(
"select COLL_NAME, DATA_NAME, DATA_OWNER_NAME, DATA_SIZE, DATA_MODIFY_TIME where COLL_NAME = '{}'"
" and DATA_NAME like '{}%'",
full_path.parent_path().c_str(),
full_path.object_name().c_str());
logging::debug("{}: query={}", __FUNCTION__, query);
for (auto&& row : irods::query<RcComm>(rcComm_t_ptr, query)) {
ptree object;
std::string key = (row[0].size() > base_length ? row[0].substr(base_length) : "") + "/" + row[1];
if (key.starts_with("/")) {
key = key.substr(1);
}
object.put("Key", key);
object.put("Etag", row[0] + row[1]);
object.put("Owner", row[2]);
object.put("Size", atoi(row[3].c_str()));
try {
std::time_t modified_epoch_time = boost::lexical_cast<std::time_t>(row[4]);
std::string modified_time_str =
irods::s3::api::common_routines::convert_time_t_to_str(modified_epoch_time, date_format);
object.put("LastModified", modified_time_str);
}
catch (const boost::bad_lexical_cast&) {
// do nothing - don't add LastModified tag
}
document.add_child("ListBucketResult.Contents", object);
}
}
}
else {
// No delimiter in request. When there is no delimiter provided, for listing purposes AWS simply searches for
// all objects with the given prefix. To make this behave similarly in iRODS, we need to perform two searches:
//
// 1. Look for objects with COLL_NAME like <prefix>%
// 2. Look for objects with COLL_NAME = <parent> and DATA_NAME like <object>%
// look for objects with COLL_NAME like <prefix>%
query = fmt::format(
"select COLL_NAME, DATA_NAME, DATA_OWNER_NAME, DATA_SIZE, DATA_MODIFY_TIME where COLL_NAME like '{}%'",
full_path.c_str());
logging::debug("{}: query={}", __FUNCTION__, query);
for (auto&& row : irods::query<RcComm>(rcComm_t_ptr, query)) {
ptree object;
std::string key = (row[0].size() > base_length ? row[0].substr(base_length) : "") + "/" + row[1];
if (key.starts_with("/")) {
key = key.substr(1);
}
object.put("Key", key);
object.put("Etag", row[0] + row[1]);
object.put("Owner", row[2]);
object.put("Size", atoi(row[3].c_str()));
try {
std::time_t modified_epoch_time = boost::lexical_cast<std::time_t>(row[4]);
std::string modified_time_str =
irods::s3::api::common_routines::convert_time_t_to_str(modified_epoch_time, date_format);
object.put("LastModified", modified_time_str);
}
catch (const boost::bad_lexical_cast&) {
// do nothing - don't add LastModified tag
}
document.add_child("ListBucketResult.Contents", object);
}
// look for objects with COLL_NAME = <parent> and DATA_NAME like <object>%
query = fmt::format(
"select COLL_NAME, DATA_NAME, DATA_OWNER_NAME, DATA_SIZE, DATA_MODIFY_TIME where COLL_NAME = '{}'"
" and DATA_NAME like '{}%'",
full_path.parent_path().c_str(),
full_path.object_name().c_str());
logging::debug("{}: query={}", __FUNCTION__, query);
for (auto&& row : irods::query<RcComm>(rcComm_t_ptr, query)) {
ptree object;
std::string key = (row[0].size() > base_length ? row[0].substr(base_length) : "") + "/" + row[1];
if (key.starts_with("/")) {
key = key.substr(1);
}
object.put("Key", key);
object.put("Etag", row[0] + row[1]);
object.put("Owner", row[2]);
object.put("Size", atoi(row[3].c_str()));
try {
std::time_t modified_epoch_time = boost::lexical_cast<std::time_t>(row[4]);
std::string modified_time_str =
irods::s3::api::common_routines::convert_time_t_to_str(modified_epoch_time, date_format);
object.put("LastModified", modified_time_str);
}
catch (const boost::bad_lexical_cast&) {
// do nothing - don't add LastModified tag
}
document.add_child("ListBucketResult.Contents", object);
}
}
beast::http::response<beast::http::string_body> string_body_response(std::move(response));
std::stringstream s;
boost::property_tree::xml_parser::xml_writer_settings<std::string> settings;
settings.indent_char = ' ';
settings.indent_count = 4;
boost::property_tree::write_xml(s, document, settings);
string_body_response.body() = s.str();
logging::debug("{}: response body {}", __FUNCTION__, s.str());
logging::debug("{}: returned {}", __FUNCTION__, string_body_response.reason());
session_ptr->send(std::move(string_body_response));
}