]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/entity_name.cc
update sources to v12.1.0
[ceph.git] / ceph / src / common / entity_name.cc
CommitLineData
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
19using std::string;
20
21extern const char *ceph_entity_type_name(int type);
22
23struct str_to_entity_type_t {
24 uint32_t type;
25 const char *str;
26};
27
28static 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
37EntityName::
38EntityName()
39 : type(0)
40{
41}
42
43const std::string& EntityName::
44to_str() const
45{
46 return type_id;
47}
48
49const char* EntityName::
50to_cstr() const
51{
52 return type_id.c_str();
53}
54
55bool EntityName::
56from_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
70void EntityName::
71set(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
85int EntityName::
86set(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
95void EntityName::
96set_type(uint32_t type_)
97{
98 set(type_, id);
99}
100
101int EntityName::
102set_type(const char *type_)
103{
104 return set(type_, id);
105}
106
107void EntityName::
108set_id(const std::string &id_)
109{
110 set(type, id_);
111}
112
113void 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
120const char* EntityName::
121get_type_str() const
122{
123 return ceph_entity_type_name(type);
124}
125
126const char *EntityName::
127get_type_name() const
128{
129 return ceph_entity_type_name(type);
130}
131
132const std::string &EntityName::
133get_id() const
134{
135 return id;
136}
137
138bool EntityName::
139has_default_id() const
140{
141 return (id == "admin");
142}
143
144std::string EntityName::
145get_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
158bool operator<(const EntityName& a, const EntityName& b)
159{
160 return (a.type < b.type) || (a.type == b.type && a.id < b.id);
161}
162
163std::ostream& operator<<(std::ostream& out, const EntityName& n)
164{
165 return out << n.to_str();
166}
167
168uint32_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}