librgw: remove classes from extern, argv

Remove class definitions from extern "C" block, which was
illogical.

Block in support for passing a C-style argument list to the
librgw_create library initializer, so code won't be tempted to
use one from librados.

Signed-off-by: Matt Benjamin <mbenjamin@redhat.com>
This commit is contained in:
Matt Benjamin 2015-09-08 10:56:30 -04:00
parent eb2ac51c07
commit fae842d2a1
4 changed files with 219 additions and 127 deletions

View File

@ -19,7 +19,7 @@ extern "C" {
#endif
typedef void* librgw_t;
int librgw_create(librgw_t *rgw, const char * const id);
int librgw_create(librgw_t *rgw, const char * const id, int argc, char **argv);
int librgw_acl_bin2xml(librgw_t rgw, const char *bin, int bin_len, char **xml);
void librgw_free_xml(librgw_t rgw, char *xml);
int librgw_acl_xml2bin(librgw_t rgw, const char *xml, char **bin,

View File

@ -58,125 +58,6 @@ static std::mutex librgw_mtx;
RGWLib librgw; /* XXX initialize? */
extern "C" {
int librgw_create(librgw_t* rgw, const char* const id)
{
CephInitParameters iparams(CEPH_ENTITY_TYPE_CLIENT);
if (id) {
iparams.name.set(CEPH_ENTITY_TYPE_CLIENT, id);
}
CephContext *cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0,
"rgw_data");
cct->_conf->set_val("log_to_stderr", "false"); // quiet by default
cct->_conf->set_val("err_to_stderr", "true"); // quiet by default
cct->_conf->parse_env(); // environment variables override
cct->_conf->apply_changes(NULL);
common_init_finish(cct);
/* assign ref'd cct as g_ceph_context if none exists */
if (! g_ceph_context) {
std::lock_guard<std::mutex> lg(librgw_mtx);
if (! g_ceph_context)
g_ceph_context = cct->get();
}
*rgw = cct;
return 0;
}
int librgw_acl_bin2xml(librgw_t rgw, const char* bin, int bin_len, char** xml)
{
CephContext* cct = static_cast<CephContext*>(rgw);
try {
// convert to bufferlist
bufferlist bl;
bl.append(bin, bin_len);
// convert to RGWAccessControlPolicy
RGWAccessControlPolicy_S3 acl(cct);
bufferlist::iterator bli(bl.begin());
acl.decode(bli);
// convert to XML stringstream
stringstream ss;
acl.to_xml(ss);
// convert to XML C string
*xml = strdup(ss.str().c_str());
if (!*xml)
return -ENOBUFS;
return 0;
}
catch (const std::exception& e) {
lderr(cct) << "librgw_acl_bin2xml: caught exception " << e.what() << dendl;
return -2000;
}
catch (...) {
lderr(cct) << "librgw_acl_bin2xml: caught unknown exception " << dendl;
return -2000;
}
}
void librgw_free_xml(librgw_t rgw, char *xml)
{
free(xml);
}
int librgw_acl_xml2bin(librgw_t rgw, const char* xml, char** bin, int* bin_len)
{
CephContext* cct = static_cast<CephContext*>(rgw);
char *bin_ = NULL;
try {
RGWACLXMLParser_S3 parser(cct);
if (!parser.init()) {
return -1000;
}
if (!parser.parse(xml, strlen(xml), true)) {
return -EINVAL;
}
RGWAccessControlPolicy_S3* policy =
(RGWAccessControlPolicy_S3*)parser.find_first("AccessControlPolicy");
if (!policy) {
return -1001;
}
bufferlist bl;
policy->encode(bl);
bin_ = (char*)malloc(bl.length());
if (!bin_) {
return -ENOBUFS;
}
int bin_len_ = bl.length();
bl.copy(0, bin_len_, bin_);
*bin = bin_;
*bin_len = bin_len_;
return 0;
}
catch (const std::exception& e) {
lderr(cct) << "librgw_acl_bin2xml: caught exception " << e.what() << dendl;
}
catch (...) {
lderr(cct) << "librgw_acl_bin2xml: caught unknown exception " << dendl;
}
if (!bin_)
free(bin_);
bin_ = NULL;
return -2000;
}
void librgw_free_bin(librgw_t rgw, char* bin)
{
free(bin);
}
void librgw_shutdown(librgw_t rgw)
{
CephContext* cct = static_cast<CephContext*>(rgw);
cct->put();
}
class C_InitTimeout : public Context {
public:
C_InitTimeout() {}
@ -322,6 +203,12 @@ void RGWLibFrontend::gen_request(const string& method, const string& resource,
}
int RGWLib::init()
{
vector<const char*> args;
return init(args);
}
int RGWLib::init(vector<const char*>& args)
{
int r = 0;
/* alternative default for module */
@ -330,7 +217,6 @@ int RGWLib::init()
def_args.push_back("--keyring=$rgw_data/keyring");
def_args.push_back("--log-file=/var/log/radosgw/$cluster-$name.log");
vector<const char*> args;
global_init(&def_args, args, CEPH_ENTITY_TYPE_CLIENT,
CODE_ENVIRONMENT_DAEMON,
CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS);
@ -401,8 +287,7 @@ int RGWLib::init()
fe->run();
return 0;
}
} /* RGWLib::init() */
int RGWLib::stop()
{
@ -431,7 +316,7 @@ int RGWLib::stop()
ceph::crypto::shutdown();
return 0;
}
} /* RGWLib::stop() */
int RGWLib::get_uri(const uint64_t handle, string& uri)
{
@ -475,6 +360,91 @@ int RGWLibIO::set_uid(RGWRados *store, const rgw_user& uid)
}
/* TODO: implement */
int RGWLib::get_userinfo_by_uid(const string& uid, RGWUserInfo& info)
{
return 0;
}
int RGWLib::get_user_acl()
{
return 0;
}
int RGWLib::set_user_permissions()
{
return 0;
}
int RGWLib::set_user_quota()
{
return 0;
}
int RGWLib::get_user_quota()
{
return 0;
}
int RGWLib::get_user_buckets_list()
{
return 0;
}
int RGWLib::get_bucket_objects_list()
{
return 0;
}
int RGWLib::create_bucket()
{
return 0;
}
int RGWLib::delete_bucket()
{
return 0;
}
int RGWLib::get_bucket_attributes()
{
return 0;
}
int RGWLib::set_bucket_attributes()
{
return 0;
}
int RGWLib::create_object ()
{
return 0;
}
int RGWLib::delete_object()
{
return 0;
}
int RGWLib::write()
{
return 0;
}
int RGWLib::read()
{
return 0;
}
int RGWLib::get_object_attributes()
{
return 0;
}
int RGWLib::set_object_attributes()
{
return 0;
}
int RGWLibIO::send_status(int status, const char* status_name)
{
return 0;
@ -548,14 +518,135 @@ void RGWLibIO::init_env(CephContext* cct)
/* global RGW library object */
static RGWLib rgwlib;
extern "C" {
int librgw_init()
{
return rgwlib.init();
}
int librgw_stop()
int librgw_create(librgw_t* rgw, const char* const id, int argc, char **argv)
{
return rgwlib.stop();
CephInitParameters iparams(CEPH_ENTITY_TYPE_CLIENT);
if (id) {
iparams.name.set(CEPH_ENTITY_TYPE_CLIENT, id);
}
CephContext* cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0,
"rgw_data");
cct->_conf->set_val("log_to_stderr", "false"); // quiet by default
cct->_conf->set_val("err_to_stderr", "true"); // quiet by default
cct->_conf->parse_env(); // environment variables override
cct->_conf->apply_changes(NULL);
common_init_finish(cct);
/* assign ref'd cct as g_ceph_context if none exists */
if (! g_ceph_context) {
std::lock_guard<std::mutex> lg(librgw_mtx);
if (! g_ceph_context) {
vector<const char*> args;
argv_to_vec(argc, const_cast<const char**>(argv), args);
librgw.init(args);
}
}
*rgw = cct;
return 0;
}
int librgw_acl_bin2xml(librgw_t rgw, const char* bin, int bin_len, char** xml)
{
CephContext* cct = static_cast<CephContext*>(rgw);
try {
// convert to bufferlist
bufferlist bl;
bl.append(bin, bin_len);
// convert to RGWAccessControlPolicy
RGWAccessControlPolicy_S3 acl(cct);
bufferlist::iterator bli(bl.begin());
acl.decode(bli);
// convert to XML stringstream
stringstream ss;
acl.to_xml(ss);
// convert to XML C string
*xml = strdup(ss.str().c_str());
if (!*xml)
return -ENOBUFS;
return 0;
}
catch (const std::exception& e) {
lderr(cct) << "librgw_acl_bin2xml: caught exception " << e.what() << dendl;
return -2000;
}
catch (...) {
lderr(cct) << "librgw_acl_bin2xml: caught unknown exception " << dendl;
return -2000;
}
}
void librgw_free_xml(librgw_t rgw, char *xml)
{
free(xml);
}
int librgw_acl_xml2bin(librgw_t rgw, const char* xml, char** bin, int* bin_len)
{
CephContext* cct = static_cast<CephContext*>(rgw);
char *bin_ = NULL;
try {
RGWACLXMLParser_S3 parser(cct);
if (!parser.init()) {
return -1000;
}
if (!parser.parse(xml, strlen(xml), true)) {
return -EINVAL;
}
RGWAccessControlPolicy_S3* policy =
(RGWAccessControlPolicy_S3*)parser.find_first("AccessControlPolicy");
if (!policy) {
return -1001;
}
bufferlist bl;
policy->encode(bl);
bin_ = (char*)malloc(bl.length());
if (!bin_) {
return -ENOBUFS;
}
int bin_len_ = bl.length();
bl.copy(0, bin_len_, bin_);
*bin = bin_;
*bin_len = bin_len_;
return 0;
}
catch (const std::exception& e) {
lderr(cct) << "librgw_acl_bin2xml: caught exception " << e.what() << dendl;
}
catch (...) {
lderr(cct) << "librgw_acl_bin2xml: caught unknown exception " << dendl;
}
if (!bin_)
free(bin_);
bin_ = NULL;
return -2000;
}
void librgw_free_bin(librgw_t rgw, char* bin)
{
free(bin);
}
void librgw_shutdown(librgw_t rgw)
{
CephContext* cct = static_cast<CephContext*>(rgw);
#if 0
rgwlib.stop();
#endif
cct->put();
}
} /* extern "C" */

View File

@ -33,6 +33,7 @@ public:
~RGWLib() {}
int init();
int init(vector<const char *>& args);
int stop();
/* generate dynamic handle currently unique per librgw object

View File

@ -27,7 +27,7 @@ namespace {
}
TEST(LibRGW, INIT) {
int ret = librgw_create(&rgw, NULL);
int ret = librgw_create(&rgw, nullptr, 0, nullptr);
ASSERT_EQ(ret, 0);
ASSERT_NE(rgw, nullptr);
}