From fae842d2a1bb2b46ff0e82ca4ffe3f71f2d35f59 Mon Sep 17 00:00:00 2001 From: Matt Benjamin Date: Tue, 8 Sep 2015 10:56:30 -0400 Subject: [PATCH] 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 --- src/include/rados/librgw.h | 2 +- src/rgw/librgw.cc | 341 +++++++++++++++++++++++-------------- src/rgw/rgw_lib.h | 1 + src/test/librgw_file.cc | 2 +- 4 files changed, 219 insertions(+), 127 deletions(-) diff --git a/src/include/rados/librgw.h b/src/include/rados/librgw.h index 66c6a7964d8..2772c94d22e 100644 --- a/src/include/rados/librgw.h +++ b/src/include/rados/librgw.h @@ -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, diff --git a/src/rgw/librgw.cc b/src/rgw/librgw.cc index 12b4b019a6e..62083ed460a 100644 --- a/src/rgw/librgw.cc +++ b/src/rgw/librgw.cc @@ -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 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(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(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(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 args; + return init(args); +} + +int RGWLib::init(vector& 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 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) +{ + 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 lg(librgw_mtx); + if (! g_ceph_context) { + vector args; + argv_to_vec(argc, const_cast(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(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(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) { - return rgwlib.stop(); + free(bin); +} + +void librgw_shutdown(librgw_t rgw) +{ + CephContext* cct = static_cast(rgw); +#if 0 + rgwlib.stop(); +#endif + cct->put(); } } /* extern "C" */ diff --git a/src/rgw/rgw_lib.h b/src/rgw/rgw_lib.h index e53a086df24..084875a891b 100644 --- a/src/rgw/rgw_lib.h +++ b/src/rgw/rgw_lib.h @@ -33,6 +33,7 @@ public: ~RGWLib() {} int init(); + int init(vector& args); int stop(); /* generate dynamic handle currently unique per librgw object diff --git a/src/test/librgw_file.cc b/src/test/librgw_file.cc index ebf0eaaa0e3..4fa07ccc6f4 100644 --- a/src/test/librgw_file.cc +++ b/src/test/librgw_file.cc @@ -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); } -- 2.47.3