1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
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.
15 #include "common/entity_name.h"
21 extern const char *ceph_entity_type_name(int type
);
23 struct str_to_entity_type_t
{
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" },
43 const std::string
& EntityName::
49 const char* EntityName::
52 return type_id
.c_str();
56 from_str(const string
& s
)
58 size_t pos
= s
.find('.');
60 if (pos
== string::npos
)
63 string type_
= s
.substr(0, pos
);
64 string id_
= s
.substr(pos
+ 1);
71 set(uint32_t type_
, const std::string
&id_
)
77 std::ostringstream oss
;
78 oss
<< ceph_entity_type_name(type_
) << "." << id_
;
86 set(const std::string
&type_
, const std::string
&id_
)
88 uint32_t t
= str_to_ceph_entity_type(type_
.c_str());
89 if (t
== CEPH_ENTITY_TYPE_ANY
)
96 set_type(uint32_t type_
)
102 set_type(const char *type_
)
104 return set(type_
, id
);
108 set_id(const std::string
&id_
)
113 void EntityName::set_name(entity_name_t n
)
116 sprintf(s
, "%lld", (long long)n
.num());
120 const char* EntityName::
123 return ceph_entity_type_name(type
);
126 const char *EntityName::
127 get_type_name() const
129 return ceph_entity_type_name(type
);
132 const std::string
&EntityName::
139 has_default_id() const
141 return (id
== "admin");
144 std::string
EntityName::
145 get_valid_types_as_str()
150 for (i
= 0; i
< sizeof(STR_TO_ENTITY_TYPE
)/sizeof(STR_TO_ENTITY_TYPE
[0]); ++i
) {
152 out
+= STR_TO_ENTITY_TYPE
[i
].str
;
158 bool operator<(const EntityName
& a
, const EntityName
& b
)
160 return (a
.type
< b
.type
) || (a
.type
== b
.type
&& a
.id
< b
.id
);
163 std::ostream
& operator<<(std::ostream
& out
, const EntityName
& n
)
165 return out
<< n
.to_str();
168 uint32_t str_to_ceph_entity_type(const char * str
)
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
;
175 return CEPH_ENTITY_TYPE_ANY
;