From 7470cc4eb5c399f92e5f00c3e7abe536d47dce47 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 6 Apr 2011 12:32:39 -0700 Subject: [PATCH] config: complain if --name gives an invalid type Signed-off-by: Colin McCabe --- src/common/ceph_argparse.cc | 7 ++-- src/common/entity_name.cc | 58 +++++++++++++++++++++++-------- src/common/entity_name.h | 6 ++-- src/test/cli/cconf/invalid-args.t | 4 +++ 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/common/ceph_argparse.cc b/src/common/ceph_argparse.cc index 0cc7288f87605..9583a5d5af1ed 100644 --- a/src/common/ceph_argparse.cc +++ b/src/common/ceph_argparse.cc @@ -324,7 +324,7 @@ bool ceph_argparse_witharg(std::vector &args, else if (first[strlen_a] == '\0') { // find second part (or not) if (i+1 == args.end()) { - std::cerr << "Option " << *i << " requires an argument." << std::endl; + cerr << "Option " << *i << " requires an argument." << std::endl; _exit(1); } i = args.erase(i); @@ -362,8 +362,9 @@ CephInitParameters ceph_argparse_early_args } else if (ceph_argparse_witharg(args, i, &val, "--name", "-n", (char*)NULL)) { if (!iparams.name.from_str(val)) { - std::cerr << "You must pass a string of the form TYPE.ID to " - "the --name option." << std::endl; + cerr << "You must pass a string of the form TYPE.ID to " + << "the --name option. Valid types are: " + << EntityName::get_valid_types_as_str() << std::endl; _exit(1); } } diff --git a/src/common/entity_name.cc b/src/common/entity_name.cc index 2864a0c6bd121..6287e550dae7c 100644 --- a/src/common/entity_name.cc +++ b/src/common/entity_name.cc @@ -15,6 +15,7 @@ #include "common/entity_name.h" #include "include/msgr.h" +#include #include #include @@ -22,6 +23,19 @@ using std::string; extern const char *ceph_entity_type_name(int type); +struct str_to_entity_type_t { + uint32_t type; + const char *str; +}; + +static const str_to_entity_type_t STR_TO_ENTITY_TYPE[] = { + { CEPH_ENTITY_TYPE_AUTH, "auth" }, + { CEPH_ENTITY_TYPE_MON, "mon" }, + { CEPH_ENTITY_TYPE_OSD, "osd" }, + { CEPH_ENTITY_TYPE_MDS, "mds" }, + { CEPH_ENTITY_TYPE_CLIENT, "client" }, +}; + EntityName:: EntityName() : type(0) @@ -50,7 +64,8 @@ from_str(const string& s) string type_ = s.substr(0, pos); string id_ = s.substr(pos + 1); - set(type_, id_); + if (set(type_, id_)) + return false; return true; } @@ -65,10 +80,14 @@ set(uint32_t type_, const std::string &id_) type_id = oss.str(); } -void EntityName:: +int EntityName:: set(const std::string &type_, const std::string &id_) { - set(str_to_ceph_entity_type(type_.c_str()), id_); + uint32_t t = str_to_ceph_entity_type(type_.c_str()); + if (t == CEPH_ENTITY_TYPE_ANY) + return -EINVAL; + set(t, id_); + return 0; } void EntityName:: @@ -77,10 +96,10 @@ set_type(uint32_t type_) set(type_, id); } -void EntityName:: +int EntityName:: set_type(const char *type_) { - set(type_, id); + return set(type_, id); } void EntityName:: @@ -125,6 +144,20 @@ has_default_id() const return (id == "admin"); } +std::string EntityName:: +get_valid_types_as_str() +{ + std::string out; + size_t i; + std::string sep(""); + for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) { + out += sep; + out += STR_TO_ENTITY_TYPE[i].str; + sep = ", "; + } + return out; +} + bool operator<(const EntityName& a, const EntityName& b) { return (a.type < b.type) || (a.type == b.type && a.id < b.id); @@ -137,15 +170,10 @@ std::ostream& operator<<(std::ostream& out, const EntityName& n) uint32_t str_to_ceph_entity_type(const char * str) { - if (strcmp(str, "auth") == 0) { - return CEPH_ENTITY_TYPE_AUTH; - } else if (strcmp(str, "mon") == 0) { - return CEPH_ENTITY_TYPE_MON; - } else if (strcmp(str, "osd") == 0) { - return CEPH_ENTITY_TYPE_OSD; - } else if (strcmp(str, "mds") == 0) { - return CEPH_ENTITY_TYPE_MDS; - } else { - return CEPH_ENTITY_TYPE_CLIENT; + size_t i; + for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) { + if (strcmp(str, STR_TO_ENTITY_TYPE[i].str) == 0) + return STR_TO_ENTITY_TYPE[i].type; } + return CEPH_ENTITY_TYPE_ANY; } diff --git a/src/common/entity_name.h b/src/common/entity_name.h index cfc7278924ab0..b2d4a67d6a727 100644 --- a/src/common/entity_name.h +++ b/src/common/entity_name.h @@ -47,9 +47,9 @@ struct EntityName const char *to_cstr() const; bool from_str(const std::string& s); void set(uint32_t type_, const std::string &id_); - void set(const std::string &type_, const std::string &id_); + int set(const std::string &type_, const std::string &id_); void set_type(uint32_t type_); - void set_type(const char *type); + int set_type(const char *type); void set_id(const std::string &id_); const char* get_type_str() const; @@ -59,6 +59,8 @@ struct EntityName const std::string &get_id() const; bool has_default_id() const; + static std::string get_valid_types_as_str(); + friend bool operator<(const EntityName& a, const EntityName& b); friend std::ostream& operator<<(std::ostream& out, const EntityName& n); diff --git a/src/test/cli/cconf/invalid-args.t b/src/test/cli/cconf/invalid-args.t index eeff0944077f9..d1247fc9f48c5 100644 --- a/src/test/cli/cconf/invalid-args.t +++ b/src/test/cli/cconf/invalid-args.t @@ -7,6 +7,10 @@ $ cconf -c test.conf broken [1] + $ cconf -c test.conf --name total.garbage + You must pass a string of the form TYPE.ID to the --name option. Valid types are: auth, mon, osd, mds, client + [1] + # TODO output an error (missing key), not the whole usage $ cconf -c test.conf -s bar lookup: expected exactly one argument -- 2.39.5