From: David Zafman Date: Tue, 30 Aug 2016 17:56:06 +0000 (-0700) Subject: common: Change cleanbin() to use base64 encoding, update ceph-objectstore-tool X-Git-Tag: v11.1.0~256^2~5 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=92e982c71995b863466d83671468f84761cb1793;p=ceph-ci.git common: Change cleanbin() to use base64 encoding, update ceph-objectstore-tool Signed-off-by: David Zafman --- diff --git a/src/common/util.cc b/src/common/util.cc index 5ab1e441a30..73b457ff541 100644 --- a/src/common/util.cc +++ b/src/common/util.cc @@ -268,25 +268,38 @@ void dump_services(Formatter* f, const map >& services, const f->close_section(); } -// Convert non-printable characters to '\###' -void cleanbin(string &str) + +// If non-printable characters found then convert bufferlist to +// base64 encoded string indicating whether it did. +string cleanbin(bufferlist &bl, bool &base64) { - bool cleaned = false; - string clean; - - for (string::iterator it = str.begin(); it != str.end(); ++it) { - if (!isprint(*it)) { - clean.push_back('\\'); - clean.push_back('0' + ((*it >> 6) & 7)); - clean.push_back('0' + ((*it >> 3) & 7)); - clean.push_back('0' + (*it & 7)); - cleaned = true; - } else { - clean.push_back(*it); - } + bufferlist::iterator it; + for (it = bl.begin(); it != bl.end(); ++it) { + if (iscntrl(*it)) + break; + } + if (it == bl.end()) { + base64 = false; + string result(bl.c_str(), bl.length()); + return result; } - if (cleaned) - str = clean; - return; + bufferlist b64; + bl.encode_base64(b64); + string encoded(b64.c_str(), b64.length()); + base64 = true; + return encoded; +} + +// If non-printable characters found then convert to "Base64:" followed by +// base64 encoding +string cleanbin(string &str) +{ + bool base64; + bufferlist bl; + bl.append(str); + string result = cleanbin(bl, base64); + if (base64) + result = "Base64:" + result; + return result; } diff --git a/src/include/util.h b/src/include/util.h index 6a85f1b894a..b152c1130df 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -82,5 +82,6 @@ void collect_sys_info(map *m, CephContext *cct); /// @param type the service type of given @p services, for example @p osd or @p mon. void dump_services(Formatter* f, const map >& services, const char* type); -void cleanbin(string &str); +string cleanbin(bufferlist &bl, bool &b64); +string cleanbin(string &str); #endif /* CEPH_UTIL_H */ diff --git a/src/tools/ceph_objectstore_tool.cc b/src/tools/ceph_objectstore_tool.cc index b120fadc596..e2636d202f4 100644 --- a/src/tools/ceph_objectstore_tool.cc +++ b/src/tools/ceph_objectstore_tool.cc @@ -1522,7 +1522,7 @@ int do_list_attrs(ObjectStore *store, coll_t coll, ghobject_t &ghobj) for (map::iterator i = aset.begin();i != aset.end(); ++i) { string key(i->first); if (outistty) - cleanbin(key); + key = cleanbin(key); cout << key << std::endl; } return 0; @@ -1543,7 +1543,7 @@ int do_list_omap(ObjectStore *store, coll_t coll, ghobject_t &ghobj) for (map::iterator i = oset.begin();i != oset.end(); ++i) { string key(i->first); if (outistty) - cleanbin(key); + key = cleanbin(key); cout << key << std::endl; } } @@ -1649,7 +1649,7 @@ int do_get_attr(ObjectStore *store, coll_t coll, ghobject_t &ghobj, string key) string value(bp.c_str(), bp.length()); if (outistty) { - cleanbin(value); + value = cleanbin(value); value.push_back('\n'); } cout << value; @@ -1725,7 +1725,7 @@ int do_get_omap(ObjectStore *store, coll_t coll, ghobject_t &ghobj, string key) bufferlist bl = out.begin()->second; string value(bl.c_str(), bl.length()); if (outistty) { - cleanbin(value); + value = cleanbin(value); value.push_back('\n'); } cout << value; @@ -1796,7 +1796,7 @@ int do_get_omaphdr(ObjectStore *store, coll_t coll, ghobject_t &ghobj) string header(hdrbl.c_str(), hdrbl.length()); if (outistty) { - cleanbin(header); + header = cleanbin(header); header.push_back('\n'); } cout << header;