]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 New Dream Network | |
7 | * | |
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. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include "common/entity_name.h" | |
7c673cae | 16 | |
7c673cae | 17 | #include <sstream> |
7c673cae FG |
18 | |
19 | using std::string; | |
20 | ||
21 | extern const char *ceph_entity_type_name(int type); | |
22 | ||
23 | struct str_to_entity_type_t { | |
24 | uint32_t type; | |
25 | const char *str; | |
26 | }; | |
27 | ||
28 | static const str_to_entity_type_t STR_TO_ENTITY_TYPE[] = { | |
29 | { CEPH_ENTITY_TYPE_AUTH, "auth" }, | |
30 | { CEPH_ENTITY_TYPE_MON, "mon" }, | |
31 | { CEPH_ENTITY_TYPE_OSD, "osd" }, | |
32 | { CEPH_ENTITY_TYPE_MDS, "mds" }, | |
33 | { CEPH_ENTITY_TYPE_MGR, "mgr" }, | |
34 | { CEPH_ENTITY_TYPE_CLIENT, "client" }, | |
35 | }; | |
36 | ||
37 | EntityName:: | |
38 | EntityName() | |
39 | : type(0) | |
40 | { | |
41 | } | |
42 | ||
43 | const std::string& EntityName:: | |
44 | to_str() const | |
45 | { | |
46 | return type_id; | |
47 | } | |
48 | ||
49 | const char* EntityName:: | |
50 | to_cstr() const | |
51 | { | |
52 | return type_id.c_str(); | |
53 | } | |
54 | ||
55 | bool EntityName:: | |
56 | from_str(const string& s) | |
57 | { | |
58 | size_t pos = s.find('.'); | |
59 | ||
60 | if (pos == string::npos) | |
61 | return false; | |
62 | ||
63 | string type_ = s.substr(0, pos); | |
64 | string id_ = s.substr(pos + 1); | |
65 | if (set(type_, id_)) | |
66 | return false; | |
67 | return true; | |
68 | } | |
69 | ||
70 | void EntityName:: | |
71 | set(uint32_t type_, const std::string &id_) | |
72 | { | |
73 | type = type_; | |
74 | id = id_; | |
75 | ||
31f18b77 FG |
76 | if (type) { |
77 | std::ostringstream oss; | |
78 | oss << ceph_entity_type_name(type_) << "." << id_; | |
79 | type_id = oss.str(); | |
80 | } else { | |
81 | type_id.clear(); | |
82 | } | |
7c673cae FG |
83 | } |
84 | ||
85 | int EntityName:: | |
86 | set(const std::string &type_, const std::string &id_) | |
87 | { | |
88 | uint32_t t = str_to_ceph_entity_type(type_.c_str()); | |
89 | if (t == CEPH_ENTITY_TYPE_ANY) | |
90 | return -EINVAL; | |
91 | set(t, id_); | |
92 | return 0; | |
93 | } | |
94 | ||
95 | void EntityName:: | |
96 | set_type(uint32_t type_) | |
97 | { | |
98 | set(type_, id); | |
99 | } | |
100 | ||
101 | int EntityName:: | |
102 | set_type(const char *type_) | |
103 | { | |
104 | return set(type_, id); | |
105 | } | |
106 | ||
107 | void EntityName:: | |
108 | set_id(const std::string &id_) | |
109 | { | |
110 | set(type, id_); | |
111 | } | |
112 | ||
113 | void EntityName::set_name(entity_name_t n) | |
114 | { | |
115 | char s[40]; | |
116 | sprintf(s, "%lld", (long long)n.num()); | |
117 | set(n.type(), s); | |
118 | } | |
119 | ||
120 | const char* EntityName:: | |
121 | get_type_str() const | |
122 | { | |
123 | return ceph_entity_type_name(type); | |
124 | } | |
125 | ||
126 | const char *EntityName:: | |
127 | get_type_name() const | |
128 | { | |
129 | return ceph_entity_type_name(type); | |
130 | } | |
131 | ||
132 | const std::string &EntityName:: | |
133 | get_id() const | |
134 | { | |
135 | return id; | |
136 | } | |
137 | ||
138 | bool EntityName:: | |
139 | has_default_id() const | |
140 | { | |
141 | return (id == "admin"); | |
142 | } | |
143 | ||
144 | std::string EntityName:: | |
145 | get_valid_types_as_str() | |
146 | { | |
147 | std::string out; | |
148 | size_t i; | |
149 | std::string sep(""); | |
150 | for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) { | |
151 | out += sep; | |
152 | out += STR_TO_ENTITY_TYPE[i].str; | |
153 | sep = ", "; | |
154 | } | |
155 | return out; | |
156 | } | |
157 | ||
158 | bool operator<(const EntityName& a, const EntityName& b) | |
159 | { | |
160 | return (a.type < b.type) || (a.type == b.type && a.id < b.id); | |
161 | } | |
162 | ||
163 | std::ostream& operator<<(std::ostream& out, const EntityName& n) | |
164 | { | |
165 | return out << n.to_str(); | |
166 | } | |
167 | ||
168 | uint32_t str_to_ceph_entity_type(const char * str) | |
169 | { | |
170 | size_t i; | |
171 | for (i = 0; i < sizeof(STR_TO_ENTITY_TYPE)/sizeof(STR_TO_ENTITY_TYPE[0]); ++i) { | |
172 | if (strcmp(str, STR_TO_ENTITY_TYPE[i].str) == 0) | |
173 | return STR_TO_ENTITY_TYPE[i].type; | |
174 | } | |
175 | return CEPH_ENTITY_TYPE_ANY; | |
176 | } |