Loading src/backends/file.cpp 0 → 100644 +200 −0 Changes for src/backends/file.cpp: 200 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <cstring> #include <exception> #include <mutex> #ifndef Windows #include <sys/types.h> #include <sys/stat.h> #include <sys/file.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #endif #include "file.h" namespace authdb { std::mutex _fileMutex; }; authdb::File::File(const char* path,size_t version,const char *domain){ if( (_FileDes=::open(path,O_RDWR)) <0){ _FileDes=::open(path,O_CREAT|O_RDWR,0660); AuthHeader head; memset(&head,0,sizeof(AuthHeader)); head.start=0xFE; head.end=0XFF; head.Version=version; head.Revesion=0; snprintf(head.domain,255,"%s",domain); if(_FileDes < 0 || ::write(_FileDes,&head,sizeof(head)) < 0){ std::string err="could not create file: "; err+=path; err+="!"; throw AuthBackendError(err.c_str()); } } _lockFile = path; _lockFile += ".lock"; if(_FileDes<0){ std::string err="could not open file: "; err+=path; err+="!"; throw AuthBackendError(err.c_str()); } } authdb::File::~File(){ } void authdb::File::read(unsigned char* src, size_t srcsize){ #ifndef Windows int pos=0; do{ int rd=::read(_FileDes,src+pos,srcsize); if(rd<0){ throw AuthBackendError("could not read file!"); } pos+=rd; srcsize-=rd; }while(srcsize!=0); #endif } void authdb::File::write(const unsigned char* dest, size_t destsize){ #ifndef Windows int pos=0; do{ int wr = ::write(_FileDes,dest+pos,destsize); if(wr<0){ throw AuthBackendError("could not write file!"); } pos+=wr; destsize-=wr; }while(destsize!=0); #endif } void authdb::File::lock(){ _fileMutex.lock(); #ifndef Windows for(;;){ mode_t m = umask( 0 ); _LockDes = open( _lockFile.c_str(), O_RDWR|O_CREAT, 0666 ); umask( m ); if(_LockDes<0){ continue; } if( flock( _LockDes, LOCK_EX) != 0 ){ switch(errno){ case ENOLCK | EINVAL:{ std::string err = "couldn't lock the lockfile: "; err+=strerror(errno); throw AuthBackendError(err.c_str()); } default: continue; } }else{ return; } } #endif } void authdb::File::unlock(){ #ifndef Windows flock(_LockDes,LOCK_UN); close(_LockDes); remove(_lockFile.c_str()); #endif _fileMutex.unlock(); } void authdb::File::setPos(size_t pos){ if(lseek(_FileDes,pos,SEEK_SET)<0){ std::string err = "could not set to pos: "; err += pos; err += " on file: "; err+= strerror(errno); throw AuthBackendError(err.c_str()); } } size_t authdb::File::getPos(){ int pos = lseek(_FileDes,0,SEEK_CUR); if(pos<0){ throw AuthBackendError("could not get position!"); } return pos; } size_t authdb::File::end(){ int pos = lseek(_FileDes,0,SEEK_END); if(pos<0){ throw AuthBackendError("could not find end!"); } return pos; } size_t authdb::File::getRevesion(){ size_t cur=getPos(); setPos(0); AuthHeader head; ::read(_FileDes,&head,sizeof(head)); setPos(cur); return head.Revesion; } void authdb::File::newRevesion(){ size_t cur=getPos(); setPos(0); AuthHeader head; ::read(_FileDes,&head,sizeof(head)); ++head.Revesion; setPos(0); ::write(_FileDes,&head,sizeof(head)); setPos(cur); } Loading
src/backends/file.cpp 0 → 100644 +200 −0 Changes for src/backends/file.cpp: 200 added lines, 0 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <cstring> #include <exception> #include <mutex> #ifndef Windows #include <sys/types.h> #include <sys/stat.h> #include <sys/file.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #endif #include "file.h" namespace authdb { std::mutex _fileMutex; }; authdb::File::File(const char* path,size_t version,const char *domain){ if( (_FileDes=::open(path,O_RDWR)) <0){ _FileDes=::open(path,O_CREAT|O_RDWR,0660); AuthHeader head; memset(&head,0,sizeof(AuthHeader)); head.start=0xFE; head.end=0XFF; head.Version=version; head.Revesion=0; snprintf(head.domain,255,"%s",domain); if(_FileDes < 0 || ::write(_FileDes,&head,sizeof(head)) < 0){ std::string err="could not create file: "; err+=path; err+="!"; throw AuthBackendError(err.c_str()); } } _lockFile = path; _lockFile += ".lock"; if(_FileDes<0){ std::string err="could not open file: "; err+=path; err+="!"; throw AuthBackendError(err.c_str()); } } authdb::File::~File(){ } void authdb::File::read(unsigned char* src, size_t srcsize){ #ifndef Windows int pos=0; do{ int rd=::read(_FileDes,src+pos,srcsize); if(rd<0){ throw AuthBackendError("could not read file!"); } pos+=rd; srcsize-=rd; }while(srcsize!=0); #endif } void authdb::File::write(const unsigned char* dest, size_t destsize){ #ifndef Windows int pos=0; do{ int wr = ::write(_FileDes,dest+pos,destsize); if(wr<0){ throw AuthBackendError("could not write file!"); } pos+=wr; destsize-=wr; }while(destsize!=0); #endif } void authdb::File::lock(){ _fileMutex.lock(); #ifndef Windows for(;;){ mode_t m = umask( 0 ); _LockDes = open( _lockFile.c_str(), O_RDWR|O_CREAT, 0666 ); umask( m ); if(_LockDes<0){ continue; } if( flock( _LockDes, LOCK_EX) != 0 ){ switch(errno){ case ENOLCK | EINVAL:{ std::string err = "couldn't lock the lockfile: "; err+=strerror(errno); throw AuthBackendError(err.c_str()); } default: continue; } }else{ return; } } #endif } void authdb::File::unlock(){ #ifndef Windows flock(_LockDes,LOCK_UN); close(_LockDes); remove(_lockFile.c_str()); #endif _fileMutex.unlock(); } void authdb::File::setPos(size_t pos){ if(lseek(_FileDes,pos,SEEK_SET)<0){ std::string err = "could not set to pos: "; err += pos; err += " on file: "; err+= strerror(errno); throw AuthBackendError(err.c_str()); } } size_t authdb::File::getPos(){ int pos = lseek(_FileDes,0,SEEK_CUR); if(pos<0){ throw AuthBackendError("could not get position!"); } return pos; } size_t authdb::File::end(){ int pos = lseek(_FileDes,0,SEEK_END); if(pos<0){ throw AuthBackendError("could not find end!"); } return pos; } size_t authdb::File::getRevesion(){ size_t cur=getPos(); setPos(0); AuthHeader head; ::read(_FileDes,&head,sizeof(head)); setPos(cur); return head.Revesion; } void authdb::File::newRevesion(){ size_t cur=getPos(); setPos(0); AuthHeader head; ::read(_FileDes,&head,sizeof(head)); ++head.Revesion; setPos(0); ::write(_FileDes,&head,sizeof(head)); setPos(cur); }