Loading CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ message(STATUS "System: ${CMAKE_HOST_SYSTEM_NAME}") if( NOT ${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows" ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra --warn-no-unused-parameter -Wstack-usage=4096 --std=c++20") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -DNDEBUG") endif() set(version 0.1) Loading src/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ list(APPEND authdbSrcs backends/file.cpp backends/cluster.cpp backend.cpp index.cpp data.cpp user.cpp domain.cpp Loading src/backend.cpp +18 −3 Original line number Diff line number Diff line Loading @@ -84,6 +84,15 @@ void authdb::AuthBackend::unlock(){ _Lock.unlock(); } void authdb::AuthBackend::lock_shared(){ if (!_Lock.try_lock_shared_for(std::chrono::seconds(5))) throw AuthBackendError("backend shared lock timeout"); } void authdb::AuthBackend::unlock_shared(){ _Lock.unlock_shared(); } void authdb::AuthBackend::setPos(size_t pos){ _Api->setPos(pos); } Loading Loading @@ -117,11 +126,17 @@ bool authdb::AuthBackend::clusterDataExists() const { return _Api->clusterDataExists(); } authdb::AuthBackend::Guard::Guard(AuthBackend &backend) : _Backend(backend){ authdb::AuthBackend::Guard::Guard(AuthBackend &backend, LockMode mode) : _Backend(backend), _Mode(mode){ if(_Mode == Exclusive) _Backend.lock(); else _Backend.lock_shared(); } authdb::AuthBackend::Guard::~Guard(){ if(_Mode == Exclusive) _Backend.unlock(); else _Backend.unlock_shared(); } src/backend.h +9 −2 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ #include <stdexcept> #include <mutex> #include <shared_mutex> #pragma once Loading Loading @@ -69,14 +70,17 @@ namespace authdb{ AuthBackend(const AuthBackend &backend); ~AuthBackend(); enum LockMode { Exclusive, Shared }; class Guard { public: Guard(AuthBackend &backend); Guard(AuthBackend &backend, LockMode mode = Exclusive); ~Guard(); Guard(const Guard &) = delete; Guard &operator=(const Guard &) = delete; private: AuthBackend &_Backend; LockMode _Mode; }; void setPos(size_t pos); Loading @@ -95,6 +99,7 @@ namespace authdb{ void read(char unsigned *src,size_t srcsize); const std::string &getDomain() const { return _Domain; } const std::string &getOptions() const { return _Options; } int getType() const { return _Type; } bool clusterDataExists() const; Loading @@ -102,11 +107,13 @@ namespace authdb{ private: void lock(); void unlock(); void lock_shared(); void unlock_shared(); int _Type; AuthBackendApi *_Api; const std::string _Options; const std::string _Domain; std::timed_mutex _Lock; std::shared_timed_mutex _Lock; }; }; src/client.cpp +18 −23 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ #include "client.h" #include "exception.h" #include "backend.h" #include "index.h" namespace authdb{ class ConnectionData { Loading Loading @@ -111,30 +112,24 @@ void authdb::Client::create(AuthBackend& backend, class ClientData* data){ } bool authdb::Client::exits(AuthBackend& backend,const uuid::uuid &clid, const char* name, int& ret){ authdb::AuthData::Record cur; size_t rd=sizeof(authdb::AuthHeader),end=backend.end(); while(rd<end){ backend.setPos(rd); backend.read((unsigned char*)&cur,sizeof(AuthData::Record)); rd=backend.getPos()+cur.datasize; if(strcmp(cur.fieldname,"clientname")==0 ){ cur.data=new char[cur.datasize]; backend.read((unsigned char*)cur.data,cur.datasize); if(name && strcmp(cur.data,name) == 0){ delete[] cur.data; RecordIndex &idx = getIndex(backend.getOptions()); if (!idx.valid()) { idx.build(backend); } if (name && name[0] != '\0') { uuid::uuid found; if (idx.findByName(DataType::ClientData, "clientname", name, found)) { ret = 1; return true; } } }else if(!clid.empty() && clid==cur.ruid){ delete[] cur.data; if (!clid.empty() && idx.findByUuid(DataType::ClientData, clid)) { ret = 2; return true; } delete[] cur.data; } } ret = -1; return false; } Loading Loading
CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ message(STATUS "System: ${CMAKE_HOST_SYSTEM_NAME}") if( NOT ${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows" ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra --warn-no-unused-parameter -Wstack-usage=4096 --std=c++20") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -DNDEBUG") endif() set(version 0.1) Loading
src/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ list(APPEND authdbSrcs backends/file.cpp backends/cluster.cpp backend.cpp index.cpp data.cpp user.cpp domain.cpp Loading
src/backend.cpp +18 −3 Original line number Diff line number Diff line Loading @@ -84,6 +84,15 @@ void authdb::AuthBackend::unlock(){ _Lock.unlock(); } void authdb::AuthBackend::lock_shared(){ if (!_Lock.try_lock_shared_for(std::chrono::seconds(5))) throw AuthBackendError("backend shared lock timeout"); } void authdb::AuthBackend::unlock_shared(){ _Lock.unlock_shared(); } void authdb::AuthBackend::setPos(size_t pos){ _Api->setPos(pos); } Loading Loading @@ -117,11 +126,17 @@ bool authdb::AuthBackend::clusterDataExists() const { return _Api->clusterDataExists(); } authdb::AuthBackend::Guard::Guard(AuthBackend &backend) : _Backend(backend){ authdb::AuthBackend::Guard::Guard(AuthBackend &backend, LockMode mode) : _Backend(backend), _Mode(mode){ if(_Mode == Exclusive) _Backend.lock(); else _Backend.lock_shared(); } authdb::AuthBackend::Guard::~Guard(){ if(_Mode == Exclusive) _Backend.unlock(); else _Backend.unlock_shared(); }
src/backend.h +9 −2 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ #include <stdexcept> #include <mutex> #include <shared_mutex> #pragma once Loading Loading @@ -69,14 +70,17 @@ namespace authdb{ AuthBackend(const AuthBackend &backend); ~AuthBackend(); enum LockMode { Exclusive, Shared }; class Guard { public: Guard(AuthBackend &backend); Guard(AuthBackend &backend, LockMode mode = Exclusive); ~Guard(); Guard(const Guard &) = delete; Guard &operator=(const Guard &) = delete; private: AuthBackend &_Backend; LockMode _Mode; }; void setPos(size_t pos); Loading @@ -95,6 +99,7 @@ namespace authdb{ void read(char unsigned *src,size_t srcsize); const std::string &getDomain() const { return _Domain; } const std::string &getOptions() const { return _Options; } int getType() const { return _Type; } bool clusterDataExists() const; Loading @@ -102,11 +107,13 @@ namespace authdb{ private: void lock(); void unlock(); void lock_shared(); void unlock_shared(); int _Type; AuthBackendApi *_Api; const std::string _Options; const std::string _Domain; std::timed_mutex _Lock; std::shared_timed_mutex _Lock; }; };
src/client.cpp +18 −23 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ #include "client.h" #include "exception.h" #include "backend.h" #include "index.h" namespace authdb{ class ConnectionData { Loading Loading @@ -111,30 +112,24 @@ void authdb::Client::create(AuthBackend& backend, class ClientData* data){ } bool authdb::Client::exits(AuthBackend& backend,const uuid::uuid &clid, const char* name, int& ret){ authdb::AuthData::Record cur; size_t rd=sizeof(authdb::AuthHeader),end=backend.end(); while(rd<end){ backend.setPos(rd); backend.read((unsigned char*)&cur,sizeof(AuthData::Record)); rd=backend.getPos()+cur.datasize; if(strcmp(cur.fieldname,"clientname")==0 ){ cur.data=new char[cur.datasize]; backend.read((unsigned char*)cur.data,cur.datasize); if(name && strcmp(cur.data,name) == 0){ delete[] cur.data; RecordIndex &idx = getIndex(backend.getOptions()); if (!idx.valid()) { idx.build(backend); } if (name && name[0] != '\0') { uuid::uuid found; if (idx.findByName(DataType::ClientData, "clientname", name, found)) { ret = 1; return true; } } }else if(!clid.empty() && clid==cur.ruid){ delete[] cur.data; if (!clid.empty() && idx.findByUuid(DataType::ClientData, clid)) { ret = 2; return true; } delete[] cur.data; } } ret = -1; return false; } Loading