@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
26
26
#include " log.h"
27
27
#include " filesys.h"
28
28
#include " exceptions.h"
29
+ #include " util/serialize.h"
29
30
#include " util/string.h"
30
31
31
32
#include " leveldb/db.h"
@@ -97,5 +98,100 @@ void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
97
98
delete it;
98
99
}
99
100
100
- #endif // USE_LEVELDB
101
+ AuthDatabaseLevelDB::AuthDatabaseLevelDB (const std::string &savedir)
102
+ {
103
+ leveldb::Options options;
104
+ options.create_if_missing = true ;
105
+ leveldb::Status status = leveldb::DB::Open (options,
106
+ savedir + DIR_DELIM + " auth.db" , &m_database);
107
+ ENSURE_STATUS_OK (status);
108
+ }
109
+
110
+ AuthDatabaseLevelDB::~AuthDatabaseLevelDB ()
111
+ {
112
+ delete m_database;
113
+ }
114
+
115
+ bool AuthDatabaseLevelDB::getAuth (const std::string &name, AuthEntry &res)
116
+ {
117
+ std::string raw;
118
+ leveldb::Status s = m_database->Get (leveldb::ReadOptions (), name, &raw);
119
+ if (!s.ok ())
120
+ return false ;
121
+ std::istringstream is (raw);
122
+
123
+ /*
124
+ u8 version = 1
125
+ std::string password
126
+ u16 number of privileges
127
+ for each privilege {
128
+ std::string privilege
129
+ }
130
+ s64 last_login
131
+ */
132
+
133
+ if (readU8 (is) > 1 )
134
+ return false ;
135
+
136
+ res.id = 1 ;
137
+ res.name = name;
138
+ res.password = deSerializeString (is);
139
+
140
+ u16 privilege_count = readU16 (is);
141
+ res.privileges .clear ();
142
+ res.privileges .reserve (privilege_count);
143
+ for (u16 i = 0 ; i < privilege_count; i++) {
144
+ res.privileges .push_back (deSerializeString (is));
145
+ }
146
+
147
+ res.last_login = readS64 (is);
148
+ return true ;
149
+ }
101
150
151
+ bool AuthDatabaseLevelDB::saveAuth (const AuthEntry &authEntry)
152
+ {
153
+ std::ostringstream os;
154
+ writeU8 (os, 1 );
155
+ os << serializeString (authEntry.password );
156
+
157
+ size_t privilege_count = authEntry.privileges .size ();
158
+ FATAL_ERROR_IF (privilege_count > U16_MAX,
159
+ " Unsupported number of privileges" );
160
+ writeU16 (os, privilege_count);
161
+ for (const std::string &privilege : authEntry.privileges ) {
162
+ os << serializeString (privilege);
163
+ }
164
+
165
+ writeS64 (os, authEntry.last_login );
166
+ leveldb::Status s = m_database->Put (leveldb::WriteOptions (),
167
+ authEntry.name , os.str ());
168
+ return s.ok ();
169
+ }
170
+
171
+ bool AuthDatabaseLevelDB::createAuth (AuthEntry &authEntry)
172
+ {
173
+ return saveAuth (authEntry);
174
+ }
175
+
176
+ bool AuthDatabaseLevelDB::deleteAuth (const std::string &name)
177
+ {
178
+ leveldb::Status s = m_database->Delete (leveldb::WriteOptions (), name);
179
+ return s.ok ();
180
+ }
181
+
182
+ void AuthDatabaseLevelDB::listNames (std::vector<std::string> &res)
183
+ {
184
+ leveldb::Iterator* it = m_database->NewIterator (leveldb::ReadOptions ());
185
+ res.clear ();
186
+ for (it->SeekToFirst (); it->Valid (); it->Next ()) {
187
+ res.emplace_back (it->key ().ToString ());
188
+ }
189
+ delete it;
190
+ }
191
+
192
+ void AuthDatabaseLevelDB::reload ()
193
+ {
194
+ // No-op for LevelDB.
195
+ }
196
+
197
+ #endif // USE_LEVELDB
0 commit comments