]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_rest_metadata.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
14 #include "include/page.h"
18 #include "rgw_rest_s3.h"
19 #include "rgw_rest_metadata.h"
20 #include "rgw_client_io.h"
21 #include "common/errno.h"
22 #include "common/strtol.h"
23 #include "rgw/rgw_b64.h"
24 #include "include/assert.h"
26 #define dout_context g_ceph_context
27 #define dout_subsys ceph_subsys_rgw
29 const string
RGWOp_Metadata_Get::name() {
30 return "get_metadata";
33 static inline void frame_metadata_key(req_state
*s
, string
& out
) {
35 string key
= s
->info
.args
.get("key", &exists
);
38 if (!s
->init_state
.url_bucket
.empty()) {
39 section
= s
->init_state
.url_bucket
;
48 out
+= string(":") + key
;
52 void RGWOp_Metadata_Get::execute() {
55 frame_metadata_key(s
, metadata_key
);
58 http_ret
= store
->meta_mgr
->get(metadata_key
, s
->formatter
);
60 dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret
) << dendl
;
67 const string
RGWOp_Metadata_List::name() {
68 return "list_metadata";
71 void RGWOp_Metadata_List::execute() {
73 ldout(s
->cct
, 16) << __func__
74 << " raw marker " << s
->info
.args
.get("marker")
78 marker
= s
->info
.args
.get("marker");
79 if (!marker
.empty()) {
80 marker
= rgw::from_base64(marker
);
82 ldout(s
->cct
, 16) << __func__
83 << " marker " << marker
<< dendl
;
85 marker
= std::string("");
88 bool max_entries_specified
;
89 string max_entries_str
=
90 s
->info
.args
.get("max-entries", &max_entries_specified
);
92 bool extended_response
= (max_entries_specified
); /* for backward compatibility, if max-entries is not specified
93 we will send the old response format */
94 uint64_t max_entries
= 0;
96 if (max_entries_specified
) {
98 max_entries
= (unsigned)strict_strtol(max_entries_str
.c_str(), 10, &err
);
100 dout(5) << "Error parsing max-entries " << max_entries_str
<< dendl
;
108 frame_metadata_key(s
, metadata_key
);
114 marker = "3:b55a9110:root::bu_9:head";
115 marker = "3:b9a8b2a6:root::sorry_janefonda_890:head";
116 marker = "3:bf885d8f:root::sorry_janefonda_665:head";
119 http_ret
= store
->meta_mgr
->list_keys_init(metadata_key
, marker
, &handle
);
121 dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret
) << dendl
;
128 if (extended_response
) {
129 s
->formatter
->open_object_section("result");
132 s
->formatter
->open_array_section("keys");
137 left
= (max_entries_specified
? max_entries
- count
: max
);
138 http_ret
= store
->meta_mgr
->list_keys_next(handle
, left
, keys
, &truncated
);
140 dout(5) << "ERROR: lists_keys_next(): " << cpp_strerror(http_ret
)
145 for (list
<string
>::iterator iter
= keys
.begin(); iter
!= keys
.end();
147 s
->formatter
->dump_string("key", *iter
);
151 } while (truncated
&& left
> 0);
153 s
->formatter
->close_section();
155 if (extended_response
) {
156 encode_json("truncated", truncated
, s
->formatter
);
157 encode_json("count", count
, s
->formatter
);
160 rgw::to_base64(store
->meta_mgr
->get_marker(handle
));
161 encode_json("marker", esc_marker
, s
->formatter
);
163 s
->formatter
->close_section();
165 store
->meta_mgr
->list_keys_complete(handle
);
170 int RGWOp_Metadata_Put::get_data(bufferlist
& bl
) {
176 cl
= atoll(s
->length
);
178 data
= (char *)malloc(cl
+ 1);
182 read_len
= recv_body(s
, data
, cl
);
183 if (cl
!= (size_t)read_len
) {
184 dout(10) << "recv_body incomplete" << dendl
;
190 bl
.append(data
, read_len
);
192 int chunk_size
= CEPH_PAGE_SIZE
;
193 const char *enc
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
194 if (!enc
|| strcmp(enc
, "chunked")) {
195 return -ERR_LENGTH_REQUIRED
;
197 data
= (char *)malloc(chunk_size
);
202 read_len
= recv_body(s
, data
, chunk_size
);
207 bl
.append(data
, read_len
);
208 } while (read_len
== chunk_size
);
215 void RGWOp_Metadata_Put::execute() {
219 http_ret
= get_data(bl
);
224 http_ret
= do_aws4_auth_completion();
229 frame_metadata_key(s
, metadata_key
);
231 RGWMetadataHandler::sync_type_t sync_type
= RGWMetadataHandler::APPLY_ALWAYS
;
233 bool mode_exists
= false;
234 string mode_string
= s
->info
.args
.get("update-type", &mode_exists
);
236 bool parsed
= RGWMetadataHandler::string_to_sync_type(mode_string
,
244 http_ret
= store
->meta_mgr
->put(metadata_key
, bl
, sync_type
,
247 dout(5) << "ERROR: can't put key: " << cpp_strerror(http_ret
) << dendl
;
250 // translate internal codes into return header
251 if (http_ret
== STATUS_NO_APPLY
)
252 update_status
= "skipped";
253 else if (http_ret
== STATUS_APPLIED
)
254 update_status
= "applied";
257 void RGWOp_Metadata_Put::send_response() {
258 int http_return_code
= http_ret
;
259 if ((http_ret
== STATUS_NO_APPLY
) || (http_ret
== STATUS_APPLIED
))
260 http_return_code
= STATUS_NO_CONTENT
;
261 set_req_state_err(s
, http_return_code
);
263 stringstream ver_stream
;
264 ver_stream
<< "ver:" << ondisk_version
.ver
265 <<",tag:" << ondisk_version
.tag
;
266 dump_header_if_nonempty(s
, "RGWX_UPDATE_STATUS", update_status
);
267 dump_header_if_nonempty(s
, "RGWX_UPDATE_VERSION", ver_stream
.str());
271 void RGWOp_Metadata_Delete::execute() {
274 frame_metadata_key(s
, metadata_key
);
275 http_ret
= store
->meta_mgr
->remove(metadata_key
);
277 dout(5) << "ERROR: can't remove key: " << cpp_strerror(http_ret
) << dendl
;
283 void RGWOp_Metadata_Lock::execute() {
284 string duration_str
, lock_id
;
287 frame_metadata_key(s
, metadata_key
);
291 duration_str
= s
->info
.args
.get("length");
292 lock_id
= s
->info
.args
.get("lock_id");
294 if ((!s
->info
.args
.exists("key")) ||
295 (duration_str
.empty()) ||
297 dout(5) << "Error invalid parameter list" << dendl
;
305 dur
= strict_strtol(duration_str
.c_str(), 10, &err
);
306 if (!err
.empty() || dur
<= 0) {
307 dout(5) << "invalid length param " << duration_str
<< dendl
;
311 http_ret
= store
->meta_mgr
->lock_exclusive(metadata_key
, make_timespan(dur
), lock_id
);
312 if (http_ret
== -EBUSY
)
313 http_ret
= -ERR_LOCKED
;
316 void RGWOp_Metadata_Unlock::execute() {
320 frame_metadata_key(s
, metadata_key
);
324 lock_id
= s
->info
.args
.get("lock_id");
326 if ((!s
->info
.args
.exists("key")) ||
328 dout(5) << "Error invalid parameter list" << dendl
;
333 http_ret
= store
->meta_mgr
->unlock(metadata_key
, lock_id
);
336 RGWOp
*RGWHandler_Metadata::op_get() {
337 if (s
->info
.args
.exists("key"))
338 return new RGWOp_Metadata_Get
;
340 return new RGWOp_Metadata_List
;
343 RGWOp
*RGWHandler_Metadata::op_put() {
344 return new RGWOp_Metadata_Put
;
347 RGWOp
*RGWHandler_Metadata::op_delete() {
348 return new RGWOp_Metadata_Delete
;
351 RGWOp
*RGWHandler_Metadata::op_post() {
352 if (s
->info
.args
.exists("lock"))
353 return new RGWOp_Metadata_Lock
;
354 else if (s
->info
.args
.exists("unlock"))
355 return new RGWOp_Metadata_Unlock
;