44#if defined(__linux__)
55
66#include < core/helper/utils.hpp>
7- #include < spdlog/spdlog.h>
8-
9-
7+ #include < fmt/format.h>
108namespace {
119
1210 namespace constants {
@@ -33,7 +31,7 @@ namespace {
3331
3432secret::SecretStorage::SecretStorage (KeyringType type) : m_type{ type } {
3533
36- key_serial_t key_type;
34+ key_serial_t key_type{} ;
3735 switch (m_type) {
3836 case secret::KeyringType::User:
3937 key_type = KEY_SPEC_USER_KEYRING;
@@ -43,8 +41,8 @@ secret::SecretStorage::SecretStorage(KeyringType type) : m_type{ type } {
4341 // -1 stands for current uid, 0 stands for: do not create a link to another keyring
4442 this ->m_ring_id = keyctl_get_persistent (-1 , 0 );
4543 if (this ->m_ring_id < 0 ) {
46- spdlog::error ( " Error while getting the persistent keyring: {}" , strerror (errno));
47- return ;
44+ throw std::runtime_error ( fmt::format ( " Error while getting the persistent keyring: {}" , strerror (errno))
45+ ) ;
4846 }
4947
5048 return ;
@@ -57,99 +55,93 @@ secret::SecretStorage::SecretStorage(KeyringType type) : m_type{ type } {
5755 this ->m_ring_id = keyctl_get_keyring_ID (key_type, 1 );
5856
5957 if (m_ring_id < 0 ) {
60- spdlog::error (" Error while getting the requested keyring: {}" , strerror (errno));
61- return ;
58+ throw std::runtime_error (fmt::format (" Error while getting the requested keyring: {}" , strerror (errno)));
6259 }
6360}
6461
6562
66- [[nodiscard]] std::optional< std::string> secret::SecretStorage::load (const std::string& key) const {
63+ [[nodiscard]] helper::expected<std::string, std::string> secret::SecretStorage::load (const std::string& key) const {
6764
6865 if (m_ring_id < 0 ) {
69- spdlog::error (" Error while loading a key, ring_id is invalid" );
70- return std::nullopt ;
66+ return helper::unexpected<std::string>{ " Error while loading a key, ring_id is invalid" };
7167 }
7268
73- auto id = get_id_from_name (m_ring_id, key);
69+ auto key_id = get_id_from_name (m_ring_id, key);
7470
75- if (id < 0 ) {
76- spdlog::error (" Error while loading a key, can't find key by name: {}" , strerror (errno));
77- return std::nullopt ;
71+ if (key_id < 0 ) {
72+ return helper::unexpected<std::string>{
73+ fmt::format (" Error while loading a key, can't find key by name: {}" , strerror (errno))
74+ };
7875 }
7976
8077
8178 void * buffer = nullptr ;
8279
83- auto result = keyctl_read_alloc (id , &buffer);
80+ auto result = keyctl_read_alloc (key_id , &buffer);
8481
8582 if (result < 0 ) {
86- spdlog::error (" Error while loading a key, can't read the value: {}" , strerror (errno));
87- return std::nullopt ;
83+ return helper::unexpected<std::string>{
84+ fmt::format (" Error while loading a key, can't read the value: {}" , strerror (errno))
85+ };
8886 }
8987
9088 auto result_string = std::string{ static_cast <char *>(buffer) };
9189 free (buffer);
9290 return result_string;
9391}
9492
95- void secret::SecretStorage::store (const std::string& key, const std::string& value, bool update) const {
93+ [[nodiscard]] std::optional<std::string>
94+ secret::SecretStorage::store (const std::string& key, const std::string& value, bool update) const {
9695
9796 if (m_ring_id < 0 ) {
98- spdlog::error (" Error while storing a key, ring_id is invalid" );
99- return ;
97+ return " Error while storing a key, ring_id is invalid" ;
10098 }
10199
102- auto id = get_id_from_name (m_ring_id, key);
100+ auto key_id = get_id_from_name (m_ring_id, key);
103101
104102
105- if (id > 0 && not update) {
106- spdlog::error (" Error while storing a key, it already exists and can't update it" );
107- return ;
103+ if (key_id > 0 && not update) {
104+ return " Error while storing a key, it already exists and can't update it" ;
108105 }
109106
110107 auto full_key = get_key_name (key);
111108
112109 auto new_id = add_key (constants::key_type_user, full_key.c_str (), value.c_str (), value.size (), m_ring_id);
113110
114111 if (new_id < 0 ) {
115- spdlog::error (" Error while storing a key, can't add the key: {}" , strerror (errno));
116- return ;
112+ return fmt::format (" Error while storing a key, can't add the key: {}" , strerror (errno));
117113 }
118114
119115
120116 auto result = keyctl_link (new_id, m_ring_id);
121117
122118 if (result < 0 ) {
123- spdlog::error (" Error while storing a key, can't link the key to the keyring: {}" , strerror (errno));
124- return ;
119+ return fmt::format (" Error while storing a key, can't link the key to the keyring: {}" , strerror (errno));
125120 }
121+
122+ return std::nullopt ;
126123}
127124
128125
129- void secret::SecretStorage::remove (const std::string& key) const {
126+ [[nodiscard]] std::optional<std::string> secret::SecretStorage::remove (const std::string& key) const {
130127
131128 if (m_ring_id < 0 ) {
132- spdlog::error (" Error while remove a key, ring_id is invalid" );
133- return ;
129+ return fmt::format (" Error while remove a key, ring_id is invalid" );
134130 }
135131
136- auto id = get_id_from_name (m_ring_id, key);
132+ auto key_id = get_id_from_name (m_ring_id, key);
137133
138- if (id < 0 ) {
139- spdlog::error (" Error while removing a key, can't find key by name: {}" , strerror (errno));
140- return ;
134+ if (key_id < 0 ) {
135+ return fmt::format (" Error while removing a key, can't find key by name: {}" , strerror (errno));
141136 }
142137
143- auto result = keyctl_unlink (id , m_ring_id);
138+ auto result = keyctl_unlink (key_id , m_ring_id);
144139
145140 if (result < 0 ) {
146- spdlog::error (" Error while removing a key, can't unlink key: {}" , strerror (errno));
147- return ;
141+ return fmt::format (" Error while removing a key, can't unlink key: {}" , strerror (errno));
148142 }
149143
150-
151-
152-
144+ return std::nullopt ;
153145}
154146
155147
0 commit comments