]>
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 "include/assert.h"
25 #define dout_context g_ceph_context
26 #define dout_subsys ceph_subsys_rgw
28 const string
RGWOp_Metadata_Get::name() {
29 return "get_metadata";
32 static inline void frame_metadata_key(req_state
*s
, string
& out
) {
34 string key
= s
->info
.args
.get("key", &exists
);
37 if (!s
->init_state
.url_bucket
.empty()) {
38 section
= s
->init_state
.url_bucket
;
47 out
+= string(":") + key
;
51 void RGWOp_Metadata_Get::execute() {
54 frame_metadata_key(s
, metadata_key
);
57 http_ret
= store
->meta_mgr
->get(metadata_key
, s
->formatter
);
59 dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret
) << dendl
;
66 const string
RGWOp_Metadata_List::name() {
67 return "list_metadata";
70 void RGWOp_Metadata_List::execute() {
73 frame_metadata_key(s
, metadata_key
);
78 http_ret
= store
->meta_mgr
->list_keys_init(metadata_key
, &handle
);
80 dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret
) << dendl
;
86 s
->formatter
->open_array_section("keys");
90 http_ret
= store
->meta_mgr
->list_keys_next(handle
, max
, keys
, &truncated
);
92 dout(5) << "ERROR: lists_keys_next(): " << cpp_strerror(http_ret
)
97 for (list
<string
>::iterator iter
= keys
.begin(); iter
!= keys
.end();
99 s
->formatter
->dump_string("key", *iter
);
104 s
->formatter
->close_section();
106 store
->meta_mgr
->list_keys_complete(handle
);
111 int RGWOp_Metadata_Put::get_data(bufferlist
& bl
) {
117 cl
= atoll(s
->length
);
119 data
= (char *)malloc(cl
+ 1);
123 read_len
= recv_body(s
, data
, cl
);
124 if (cl
!= (size_t)read_len
) {
125 dout(10) << "recv_body incomplete" << dendl
;
131 bl
.append(data
, read_len
);
133 int chunk_size
= CEPH_PAGE_SIZE
;
134 const char *enc
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
135 if (!enc
|| strcmp(enc
, "chunked")) {
136 return -ERR_LENGTH_REQUIRED
;
138 data
= (char *)malloc(chunk_size
);
143 read_len
= recv_body(s
, data
, chunk_size
);
148 bl
.append(data
, read_len
);
149 } while (read_len
== chunk_size
);
156 void RGWOp_Metadata_Put::execute() {
160 http_ret
= get_data(bl
);
165 http_ret
= do_aws4_auth_completion();
170 frame_metadata_key(s
, metadata_key
);
172 RGWMetadataHandler::sync_type_t sync_type
= RGWMetadataHandler::APPLY_ALWAYS
;
174 bool mode_exists
= false;
175 string mode_string
= s
->info
.args
.get("update-type", &mode_exists
);
177 bool parsed
= RGWMetadataHandler::string_to_sync_type(mode_string
,
185 http_ret
= store
->meta_mgr
->put(metadata_key
, bl
, sync_type
,
188 dout(5) << "ERROR: can't put key: " << cpp_strerror(http_ret
) << dendl
;
191 // translate internal codes into return header
192 if (http_ret
== STATUS_NO_APPLY
)
193 update_status
= "skipped";
194 else if (http_ret
== STATUS_APPLIED
)
195 update_status
= "applied";
198 void RGWOp_Metadata_Put::send_response() {
199 int http_return_code
= http_ret
;
200 if ((http_ret
== STATUS_NO_APPLY
) || (http_ret
== STATUS_APPLIED
))
201 http_return_code
= STATUS_NO_CONTENT
;
202 set_req_state_err(s
, http_return_code
);
204 stringstream ver_stream
;
205 ver_stream
<< "ver:" << ondisk_version
.ver
206 <<",tag:" << ondisk_version
.tag
;
207 dump_header_if_nonempty(s
, "RGWX_UPDATE_STATUS", update_status
);
208 dump_header_if_nonempty(s
, "RGWX_UPDATE_VERSION", ver_stream
.str());
212 void RGWOp_Metadata_Delete::execute() {
215 frame_metadata_key(s
, metadata_key
);
216 http_ret
= store
->meta_mgr
->remove(metadata_key
);
218 dout(5) << "ERROR: can't remove key: " << cpp_strerror(http_ret
) << dendl
;
224 void RGWOp_Metadata_Lock::execute() {
225 string duration_str
, lock_id
;
228 frame_metadata_key(s
, metadata_key
);
232 duration_str
= s
->info
.args
.get("length");
233 lock_id
= s
->info
.args
.get("lock_id");
235 if ((!s
->info
.args
.exists("key")) ||
236 (duration_str
.empty()) ||
238 dout(5) << "Error invalid parameter list" << dendl
;
246 dur
= strict_strtol(duration_str
.c_str(), 10, &err
);
247 if (!err
.empty() || dur
<= 0) {
248 dout(5) << "invalid length param " << duration_str
<< dendl
;
252 http_ret
= store
->meta_mgr
->lock_exclusive(metadata_key
, make_timespan(dur
), lock_id
);
253 if (http_ret
== -EBUSY
)
254 http_ret
= -ERR_LOCKED
;
257 void RGWOp_Metadata_Unlock::execute() {
261 frame_metadata_key(s
, metadata_key
);
265 lock_id
= s
->info
.args
.get("lock_id");
267 if ((!s
->info
.args
.exists("key")) ||
269 dout(5) << "Error invalid parameter list" << dendl
;
274 http_ret
= store
->meta_mgr
->unlock(metadata_key
, lock_id
);
277 RGWOp
*RGWHandler_Metadata::op_get() {
278 if (s
->info
.args
.exists("key"))
279 return new RGWOp_Metadata_Get
;
281 return new RGWOp_Metadata_List
;
284 RGWOp
*RGWHandler_Metadata::op_put() {
285 return new RGWOp_Metadata_Put
;
288 RGWOp
*RGWHandler_Metadata::op_delete() {
289 return new RGWOp_Metadata_Delete
;
292 RGWOp
*RGWHandler_Metadata::op_post() {
293 if (s
->info
.args
.exists("lock"))
294 return new RGWOp_Metadata_Lock
;
295 else if (s
->info
.args
.exists("unlock"))
296 return new RGWOp_Metadata_Unlock
;