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