]>
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 if (s
->aws4_auth_needs_complete
) {
166 http_ret
= do_aws4_auth_completion();
172 frame_metadata_key(s
, metadata_key
);
174 RGWMetadataHandler::sync_type_t sync_type
= RGWMetadataHandler::APPLY_ALWAYS
;
176 bool mode_exists
= false;
177 string mode_string
= s
->info
.args
.get("update-type", &mode_exists
);
179 bool parsed
= RGWMetadataHandler::string_to_sync_type(mode_string
,
187 http_ret
= store
->meta_mgr
->put(metadata_key
, bl
, sync_type
,
190 dout(5) << "ERROR: can't put key: " << cpp_strerror(http_ret
) << dendl
;
193 // translate internal codes into return header
194 if (http_ret
== STATUS_NO_APPLY
)
195 update_status
= "skipped";
196 else if (http_ret
== STATUS_APPLIED
)
197 update_status
= "applied";
200 void RGWOp_Metadata_Put::send_response() {
201 int http_return_code
= http_ret
;
202 if ((http_ret
== STATUS_NO_APPLY
) || (http_ret
== STATUS_APPLIED
))
203 http_return_code
= STATUS_NO_CONTENT
;
204 set_req_state_err(s
, http_return_code
);
206 stringstream ver_stream
;
207 ver_stream
<< "ver:" << ondisk_version
.ver
208 <<",tag:" << ondisk_version
.tag
;
209 dump_header_if_nonempty(s
, "RGWX_UPDATE_STATUS", update_status
);
210 dump_header_if_nonempty(s
, "RGWX_UPDATE_VERSION", ver_stream
.str());
214 void RGWOp_Metadata_Delete::execute() {
217 frame_metadata_key(s
, metadata_key
);
218 http_ret
= store
->meta_mgr
->remove(metadata_key
);
220 dout(5) << "ERROR: can't remove key: " << cpp_strerror(http_ret
) << dendl
;
226 void RGWOp_Metadata_Lock::execute() {
227 string duration_str
, lock_id
;
230 frame_metadata_key(s
, metadata_key
);
234 duration_str
= s
->info
.args
.get("length");
235 lock_id
= s
->info
.args
.get("lock_id");
237 if ((!s
->info
.args
.exists("key")) ||
238 (duration_str
.empty()) ||
240 dout(5) << "Error invalid parameter list" << dendl
;
248 dur
= strict_strtol(duration_str
.c_str(), 10, &err
);
249 if (!err
.empty() || dur
<= 0) {
250 dout(5) << "invalid length param " << duration_str
<< dendl
;
254 http_ret
= store
->meta_mgr
->lock_exclusive(metadata_key
, make_timespan(dur
), lock_id
);
255 if (http_ret
== -EBUSY
)
256 http_ret
= -ERR_LOCKED
;
259 void RGWOp_Metadata_Unlock::execute() {
263 frame_metadata_key(s
, metadata_key
);
267 lock_id
= s
->info
.args
.get("lock_id");
269 if ((!s
->info
.args
.exists("key")) ||
271 dout(5) << "Error invalid parameter list" << dendl
;
276 http_ret
= store
->meta_mgr
->unlock(metadata_key
, lock_id
);
279 RGWOp
*RGWHandler_Metadata::op_get() {
280 if (s
->info
.args
.exists("key"))
281 return new RGWOp_Metadata_Get
;
283 return new RGWOp_Metadata_List
;
286 RGWOp
*RGWHandler_Metadata::op_put() {
287 return new RGWOp_Metadata_Put
;
290 RGWOp
*RGWHandler_Metadata::op_delete() {
291 return new RGWOp_Metadata_Delete
;
294 RGWOp
*RGWHandler_Metadata::op_post() {
295 if (s
->info
.args
.exists("lock"))
296 return new RGWOp_Metadata_Lock
;
297 else if (s
->info
.args
.exists("unlock"))
298 return new RGWOp_Metadata_Unlock
;