X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/1bf5ae378079e212f6918d09d87dec5420a0d6ec..1cb912e4f90473f78d2f0f10ab5c9b2c5bd36440:/projects/stargazer/plugins/store/files/file_store.cpp diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index 556bf8cf..eedb60e8 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -28,191 +28,124 @@ #define _GNU_SOURCE #endif +#include "file_store.h" + +#include "stg/common.h" +#include "stg/user_ips.h" +#include "stg/user_conf.h" +#include "stg/user_stat.h" +#include "stg/const.h" +#include "stg/blowfish.h" +#include "stg/logger.h" +#include "stg/locker.h" +#include "stg/admin_conf.h" +#include "stg/tariff.h" +#include "stg/tariff_conf.h" +#include "stg/service_conf.h" + +#include +#include +#include +#include +#include +#include + #include #include -#include #include #include #include #include #include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "user_ips.h" -#include "user_conf.h" -#include "user_stat.h" -#include "stg_const.h" -#include "file_store.h" -#include "blowfish.h" -#include "stg_logger.h" -#include "stg_locker.h" - #define DELETED_USERS_DIR "deleted_users" #define adm_enc_passwd "cjeifY8m3" -using namespace std; +int GetFileList(std::vector * fileList, const std::string & directory, mode_t mode, const std::string & ext); const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- -class BAK_FILE +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +namespace { -public: - //------------------------------------------------------------------------- - BAK_FILE(const string & fileName, bool removeBak) - : f(NULL), - removeBak(false) - { - bakSuccessed = false; - BAK_FILE::removeBak = removeBak; - fileNameBak = fileName + ".bak"; - /*struct stat fileStat; - if (stat(fileName.c_str(), &fileStat) == 0) - { - char * buff = new char[fileStat.st_size]; - f = fopen(fileName.c_str(), "rb"); - if(f) - { - fread(buff, 1, fileStat.st_size, f); - fclose(f); - - fileNameBak = fileName + ".bak"; - f = fopen(fileNameBak.c_str(), "wb"); - if(f) - { - fwrite(buff, 1, fileStat.st_size, f); - fclose(f); - } - } - - delete[] buff; - - bakSuccessed = true; - }*/ - if (rename(fileName.c_str(), fileNameBak.c_str())) - { - printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno)); - } - else - { - bakSuccessed = true; - } - - } - //------------------------------------------------------------------------- - ~BAK_FILE() - { - if(bakSuccessed && removeBak) - { - if (unlink(fileNameBak.c_str())) - { - printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno)); - } - } - } - //------------------------------------------------------------------------- - -private: - FILE * f; - bool bakSuccessed; - string fileNameBak; - bool removeBak; -}; -//----------------------------------------------------------------------------- -class FILES_STORE_CREATOR +bool CheckAndCreate(const std::string & dir, mode_t mode) { -private: - FILES_STORE * fs; +if (access(dir.c_str(), F_OK) == 0) + return true; +if (mkdir(dir.c_str(), mode) == 0) + return true; +return false; +} -public: - FILES_STORE_CREATOR() - : fs(new FILES_STORE()) - { - }; - ~FILES_STORE_CREATOR() - { - delete fs; - }; +} - FILES_STORE * GetStore() - { - return fs; - }; -}; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -FILES_STORE_CREATOR fsc; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -BASE_STORE * GetStore() +extern "C" STG::Store* GetStore() { -return fsc.GetStore(); + static FILES_STORE plugin; + return &plugin; } //----------------------------------------------------------------------------- FILES_STORE_SETTINGS::FILES_STORE_SETTINGS() - : settings(NULL), - removeBak(true), - readBak(true) + : m_statMode(0), + m_statUID(0), + m_statGID(0), + m_confMode(0), + m_confUID(0), + m_confGID(0), + m_userLogMode(0), + m_userLogUID(0), + m_userLogGID(0), + m_removeBak(true), + m_readBak(true) { } //----------------------------------------------------------------------------- -FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS() +int FILES_STORE_SETTINGS::ParseOwner(const std::vector & moduleParams, const std::string & owner, uid_t * uid) { -} -//----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseOwner(const vector & moduleParams, const string & owner, uid_t * uid) -{ -PARAM_VALUE pv; +STG::ParamValue pv; pv.param = owner; -vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { - errorStr = "Parameter \'" + owner + "\' not found."; - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = "Parameter \'" + owner + "\' not found."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } if (User2UID(pvi->value[0].c_str(), uid) < 0) { - errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'"; - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'"; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseGroup(const vector & moduleParams, const string & group, gid_t * gid) +int FILES_STORE_SETTINGS::ParseGroup(const std::vector & moduleParams, const std::string & group, gid_t * gid) { -PARAM_VALUE pv; +STG::ParamValue pv; pv.param = group; -vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { - errorStr = "Parameter \'" + group + "\' not found."; - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = "Parameter \'" + group + "\' not found."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } if (Group2GID(pvi->value[0].c_str(), gid) < 0) { - errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'"; - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'"; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val) +int FILES_STORE_SETTINGS::ParseYesNo(const std::string & value, bool * val) { if (0 == strcasecmp(value.c_str(), "yes")) { @@ -225,65 +158,65 @@ if (0 == strcasecmp(value.c_str(), "no")) return 0; } -errorStr = "Incorrect value \'" + value + "\'."; +m_errorStr = "Incorrect value \'" + value + "\'."; return -1; } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseMode(const vector & moduleParams, const string & modeStr, mode_t * mode) +int FILES_STORE_SETTINGS::ParseMode(const std::vector & moduleParams, const std::string & modeStr, mode_t * mode) { -PARAM_VALUE pv; +STG::ParamValue pv; pv.param = modeStr; -vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { - errorStr = "Parameter \'" + modeStr + "\' not found."; - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = "Parameter \'" + modeStr + "\' not found."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } if (Str2Mode(pvi->value[0].c_str(), mode) < 0) { - errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'"; - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'"; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) +int FILES_STORE_SETTINGS::ParseSettings(const STG::ModuleSettings & s) { -if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0) +if (ParseOwner(s.moduleParams, "StatOwner", &m_statUID) < 0) return -1; -if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0) +if (ParseGroup(s.moduleParams, "StatGroup", &m_statGID) < 0) return -1; -if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0) +if (ParseMode(s.moduleParams, "StatMode", &m_statMode) < 0) return -1; -if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0) +if (ParseOwner(s.moduleParams, "ConfOwner", &m_confUID) < 0) return -1; -if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0) +if (ParseGroup(s.moduleParams, "ConfGroup", &m_confGID) < 0) return -1; -if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0) +if (ParseMode(s.moduleParams, "ConfMode", &m_confMode) < 0) return -1; -if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0) +if (ParseOwner(s.moduleParams, "UserLogOwner", &m_userLogUID) < 0) return -1; -if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0) +if (ParseGroup(s.moduleParams, "UserLogGroup", &m_userLogGID) < 0) return -1; -if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0) +if (ParseMode(s.moduleParams, "UserLogMode", &m_userLogMode) < 0) return -1; -vector::const_iterator pvi; -PARAM_VALUE pv; +std::vector::const_iterator pvi; +STG::ParamValue pv; pv.param = "RemoveBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { - removeBak = true; + m_removeBak = true; } else { - if (ParseYesNo(pvi->value[0], &removeBak)) + if (ParseYesNo(pvi->value[0], &m_removeBak)) { printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n"); return -1; @@ -292,13 +225,13 @@ else pv.param = "ReadBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { - readBak = false; + m_readBak = false; } else { - if (ParseYesNo(pvi->value[0], &readBak)) + if (ParseYesNo(pvi->value[0], &m_readBak)) { printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n"); return -1; @@ -307,28 +240,53 @@ else pv.param = "WorkDir"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { - errorStr = "Parameter \'WorkDir\' not found."; + m_errorStr = "Parameter \'WorkDir\' not found."; printfd(__FILE__, "Parameter 'WorkDir' not found\n"); return -1; } -workDir = pvi->value[0]; -if (workDir.size() && workDir[workDir.size() - 1] == '/') +m_workDir = pvi->value[0]; +if (m_workDir.size() && m_workDir[m_workDir.size() - 1] == '/') + { + m_workDir.resize(m_workDir.size() - 1); + } +m_usersDir = m_workDir + "/users/"; +if (!CheckAndCreate(m_usersDir, GetConfModeDir())) + { + m_errorStr = m_usersDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); + return -1; + } +m_tariffsDir = m_workDir + "/tariffs/"; +if (!CheckAndCreate(m_tariffsDir, GetConfModeDir())) { - workDir.resize(workDir.size() - 1); + m_errorStr = m_tariffsDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); + return -1; + } +m_adminsDir = m_workDir + "/admins/"; +if (!CheckAndCreate(m_adminsDir, GetConfModeDir())) + { + m_errorStr = m_adminsDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); + return -1; + } +m_servicesDir = m_workDir + "/services/"; +if (!CheckAndCreate(m_servicesDir, GetConfModeDir())) + { + m_errorStr = m_servicesDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", m_errorStr.c_str()); + return -1; } -usersDir = workDir + "/users/"; -tariffsDir = workDir + "/tariffs/"; -adminsDir = workDir + "/admins/"; return 0; } //----------------------------------------------------------------------------- -const string & FILES_STORE_SETTINGS::GetStrError() const +const std::string & FILES_STORE_SETTINGS::GetStrError() const { -return errorStr; +return m_errorStr; } //----------------------------------------------------------------------------- int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid) @@ -337,8 +295,8 @@ struct passwd * pw; pw = getpwnam(user); if (!pw) { - errorStr = string("User \'") + string(user) + string("\' not found in system."); - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = std::string("User \'") + std::string(user) + std::string("\' not found in system."); + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } @@ -352,8 +310,8 @@ struct group * grp; grp = getgrnam(gr); if (!grp) { - errorStr = string("Group \'") + string(gr) + string("\' not found in system."); - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = std::string("Group \'") + std::string(gr) + std::string("\' not found in system."); + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } @@ -363,357 +321,255 @@ return 0; //----------------------------------------------------------------------------- int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode) { -char a; -char b; -char c; if (strlen(str) > 3) { - errorStr = string("Error parsing mode \'") + str + string("\'"); - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = std::string("Error parsing mode \'") + str + std::string("\'"); + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } for (int i = 0; i < 3; i++) if (str[i] > '7' || str[i] < '0') { - errorStr = string("Error parsing mode \'") + str + string("\'"); - printfd(__FILE__, "%s\n", errorStr.c_str()); + m_errorStr = std::string("Error parsing mode \'") + str + std::string("\'"); + printfd(__FILE__, "%s\n", m_errorStr.c_str()); return -1; } -a = str[0] - '0'; -b = str[1] - '0'; -c = str[2] - '0'; +mode_t a = str[0] - '0'; +mode_t b = str[1] - '0'; +mode_t c = str[2] - '0'; -*mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6); +*mode = c + (b << 3) + (a << 6); return 0; } //----------------------------------------------------------------------------- -string FILES_STORE_SETTINGS::GetWorkDir() const -{ -return workDir; -} -//----------------------------------------------------------------------------- -string FILES_STORE_SETTINGS::GetUsersDir() const -{ -return usersDir; -} -//----------------------------------------------------------------------------- -string FILES_STORE_SETTINGS::GetAdminsDir() const -{ -return adminsDir; -} -//----------------------------------------------------------------------------- -string FILES_STORE_SETTINGS::GetTariffsDir() const -{ -return tariffsDir; -} -//----------------------------------------------------------------------------- -mode_t FILES_STORE_SETTINGS::GetStatMode() const -{ -return statMode; -} -//----------------------------------------------------------------------------- mode_t FILES_STORE_SETTINGS::GetStatModeDir() const { -mode_t mode = statMode; -if (statMode & S_IRUSR) mode |= S_IXUSR; -if (statMode & S_IRGRP) mode |= S_IXGRP; -if (statMode & S_IROTH) mode |= S_IXOTH; +mode_t mode = m_statMode; +if (m_statMode & S_IRUSR) mode |= S_IXUSR; +if (m_statMode & S_IRGRP) mode |= S_IXGRP; +if (m_statMode & S_IROTH) mode |= S_IXOTH; return mode; } //----------------------------------------------------------------------------- -uid_t FILES_STORE_SETTINGS::GetStatUID() const -{ -return statUID; -} -//----------------------------------------------------------------------------- -gid_t FILES_STORE_SETTINGS::GetStatGID() const -{ -return statGID; -} -//----------------------------------------------------------------------------- -mode_t FILES_STORE_SETTINGS::GetConfMode() const -{ -return confMode; -} -//----------------------------------------------------------------------------- mode_t FILES_STORE_SETTINGS::GetConfModeDir() const { -mode_t mode = confMode; -if (statMode & S_IRUSR) mode |= S_IXUSR; -if (statMode & S_IRGRP) mode |= S_IXGRP; -if (statMode & S_IROTH) mode |= S_IXOTH; +mode_t mode = m_confMode; +if (m_confMode & S_IRUSR) mode |= S_IXUSR; +if (m_confMode & S_IRGRP) mode |= S_IXGRP; +if (m_confMode & S_IROTH) mode |= S_IXOTH; return mode; } //----------------------------------------------------------------------------- -uid_t FILES_STORE_SETTINGS::GetConfUID() const -{ -return confUID; -} //----------------------------------------------------------------------------- -gid_t FILES_STORE_SETTINGS::GetConfGID() const -{ -return confGID; -} -//----------------------------------------------------------------------------- -mode_t FILES_STORE_SETTINGS::GetLogMode() const -{ -return userLogMode; -} -//----------------------------------------------------------------------------- -uid_t FILES_STORE_SETTINGS::GetLogUID() const -{ -return userLogUID; -} -//----------------------------------------------------------------------------- -gid_t FILES_STORE_SETTINGS::GetLogGID() const -{ -return userLogGID; -} -//----------------------------------------------------------------------------- -bool FILES_STORE_SETTINGS::GetRemoveBak() const -{ -return removeBak; -} -//----------------------------------------------------------------------------- -bool FILES_STORE_SETTINGS::GetReadBak() const -{ -return readBak; -} -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -/*BASE_SETTINGS * FILES_STORE::GetStoreSettings() -{ -return &storeSettings; -}*/ //----------------------------------------------------------------------------- FILES_STORE::FILES_STORE() + : m_version("file_store v.1.04"), + m_logger(STG::PluginLogger::get("store_files")) { -version = "file_store v.1.04"; - pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); -pthread_mutex_init(&mutex, &attr); -}; -//----------------------------------------------------------------------------- -FILES_STORE::~FILES_STORE() -{ - -}; -//----------------------------------------------------------------------------- -void FILES_STORE::SetSettings(const MODULE_SETTINGS & s) -{ -settings = s; +pthread_mutex_init(&m_mutex, &attr); } //----------------------------------------------------------------------------- int FILES_STORE::ParseSettings() { -int ret = storeSettings.ParseSettings(settings); +int ret = m_storeSettings.ParseSettings(m_settings); if (ret) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = storeSettings.GetStrError(); + STG_LOCKER lock(&m_mutex); + m_errorStr = m_storeSettings.GetStrError(); } return ret; } //----------------------------------------------------------------------------- -const string & FILES_STORE::GetStrError() const +int FILES_STORE::GetUsersList(std::vector * userList) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -return errorStr; -} -//----------------------------------------------------------------------------- -const string & FILES_STORE::GetVersion() const -{ -return version; -} -//----------------------------------------------------------------------------- -int FILES_STORE::GetFilesList(vector * filesList, const string & directory, mode_t mode, const string & ext) const -{ -// æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ -DIR * d; -string str; -struct stat st; -dirent * dir; - -filesList->clear(); +std::vector files; -d = opendir(directory.c_str()); - -if (!d) +if (GetFileList(&files, m_storeSettings.GetUsersDir(), S_IFDIR, "")) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + directory + "\' cannot be opened."; - //printfd(__FILE__, "%s\n", errorStr.c_str()); + STG_LOCKER lock(&m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetUsersDir() + "': " + std::string(strerror(errno)); return -1; } -int d_nameLen; -int extLen = ext.size() ; -while ((dir = readdir(d))) - { - if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")) - { - str = directory + "/" + string(dir->d_name); - if (!stat(str.c_str(), &st)) - { - if (st.st_mode & mode) // ïÔÓÅ× ÆÁÊÌÏ× or directories - { - d_nameLen = strlen(dir->d_name); - if (d_nameLen > extLen) - { - if (strcmp(dir->d_name + (d_nameLen - extLen), ext.c_str()) == 0) - { - dir->d_name[d_nameLen - extLen] = 0; - filesList->push_back(dir->d_name); - } - } - } - } - } - } +STG_LOCKER lock(&m_mutex); + +userList->swap(files); -closedir(d); return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetUsersList(vector * usersList) const +int FILES_STORE::GetAdminsList(std::vector * adminList) const { -return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, ""); +std::vector files; + +if (GetFileList(&files, m_storeSettings.GetAdminsDir(), S_IFREG, ".adm")) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&m_mutex); + +adminList->swap(files); + +return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetAdminsList(vector * adminsList) const +int FILES_STORE::GetTariffsList(std::vector * tariffList) const { -return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm"); +std::vector files; + +if (GetFileList(&files, m_storeSettings.GetTariffsDir(), S_IFREG, ".tf")) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&m_mutex); + +tariffList->swap(files); + +return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetTariffsList(vector * tariffsList) const +int FILES_STORE::GetServicesList(std::vector * list) const { -return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf"); +std::vector files; + +if (GetFileList(&files, m_storeSettings.GetServicesDir(), S_IFREG, ".serv")) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetServicesDir() + "': " + std::string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&m_mutex); + +list->swap(files); + +return 0; } //----------------------------------------------------------------------------- int FILES_STORE::RemoveDir(const char * path) const { -vector filesList; +DIR * d = opendir(path); -GetFilesList(&filesList, path, S_IFREG, ""); +if (!d) + { + m_errorStr = "failed to open dir. Message: '"; + m_errorStr += strerror(errno); + m_errorStr += "'"; + printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno)); + return -1; + } -for (unsigned i = 0; i < filesList.size(); i++) +dirent * entry; +while ((entry = readdir(d))) { - string file = path + string("/") + filesList[i]; - if (unlink(file.c_str())) + if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))) + continue; + + std::string str = path; + str += "/" + std::string(entry->d_name); + + struct stat st; + if (stat(str.c_str(), &st)) + continue; + + if ((st.st_mode & S_IFREG)) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "unlink failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; - printfd(__FILE__, "FILES_STORE::RemoveDir - unlink failed. Message: '%s'\n", strerror(errno)); - return -1; + if (unlink(str.c_str())) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "unlink failed. Message: '"; + m_errorStr += strerror(errno); + m_errorStr += "'"; + printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno)); + closedir(d); + return -1; + } } - } -GetFilesList(&filesList, path, S_IFDIR, ""); + if (!(st.st_mode & S_IFDIR)) + { + if (RemoveDir(str.c_str())) + { + closedir(d); + return -1; + } -for (unsigned i = 0; i < filesList.size(); i++) - { - string dir = string(path) + "/" + filesList[i]; - RemoveDir(dir.c_str()); + } } +closedir(d); + if (rmdir(path)) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "rmdir failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; - printfd(__FILE__, "FILES_STORE::RemoveDir - rmdir failed. Message: '%s'\n", strerror(errno)); + STG_LOCKER lock(&m_mutex); + m_errorStr = "rmdir failed. Message: '"; + m_errorStr += strerror(errno); + m_errorStr += "'"; + printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno)); return -1; } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::AddUser(const string & login) const +int FILES_STORE::AddUser(const std::string & login) const { -FILE * f; -string fileName; +std::string fileName; -strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str()); +strprintf(&fileName, "%s%s", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } -strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str()); -f = fopen(fileName.c_str(), "wt"); -if (f) +strprintf(&fileName, "%s%s/conf", m_storeSettings.GetUsersDir().c_str(), login.c_str()); +if (Touch(fileName)) { - if (fprintf(f, "\n") < 0) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "fprintf failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; - printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno)); - return -1; - } - fclose(f); - } -else - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot create file \"" + fileName + "\'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot create file \"" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } -strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str()); -f = fopen(fileName.c_str(), "wt"); -if (f) +strprintf(&fileName, "%s%s/stat", m_storeSettings.GetUsersDir().c_str(), login.c_str()); +if (Touch(fileName)) { - if (fprintf(f, "\n") < 0) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "fprintf failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; - printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno)); - return -1; - } - fclose(f); - } -else - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot create file \"" + fileName + "\'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot create file \"" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::DelUser(const string & login) const +int FILES_STORE::DelUser(const std::string & login) const { -string dirName; -string dirName1; +std::string dirName; +std::string dirName1; -strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str()); +strprintf(&dirName, "%s/%s", m_storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR); if (access(dirName.c_str(), F_OK) != 0) { if (mkdir(dirName.c_str(), 0700) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory '" + dirName + "' cannot be created."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Directory '" + dirName + "' cannot be created."; printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -721,19 +577,19 @@ if (access(dirName.c_str(), F_OK) != 0) if (access(dirName.c_str(), F_OK) == 0) { - strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL)); - strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str()); + strprintf(&dirName, "%s/%s/%s.%lu", m_storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR, login.c_str(), time(NULL)); + strprintf(&dirName1, "%s/%s", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (rename(dirName1.c_str(), dirName.c_str())) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error moving dir from " + dirName1 + " to " + dirName; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error moving dir from " + dirName1 + " to " + dirName; printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno)); return -1; } } else { - strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str()); + strprintf(&dirName, "%s/%s", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (RemoveDir(dirName.c_str())) { return -1; @@ -742,13 +598,13 @@ else return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const +int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login) const { -string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; +std::string fileName; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/conf"; if (RestoreUserConf(conf, login, fileName)) { - if (!storeSettings.GetReadBak()) + if (!m_storeSettings.GetReadBak()) { return -1; } @@ -757,86 +613,83 @@ if (RestoreUserConf(conf, login, fileName)) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const +int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login, const std::string & fileName) const { CONFIGFILE cf(fileName); int e = cf.Error(); -string str; if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadString("Password", &conf->password, "") < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read. Parameter Password."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read. Parameter Password."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str()); return -1; } if (conf->password.empty()) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' password is blank."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' password is blank."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str()); return -1; } if (cf.ReadString("tariff", &conf->tariffName, "") < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read. Parameter Tariff."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read. Parameter Tariff."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str()); return -1; } if (conf->tariffName.empty()) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' tariff is blank."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' tariff is blank."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str()); return -1; } -string ipStr; +std::string ipStr; cf.ReadString("IP", &ipStr, "?"); -USER_IPS i; try { - i = StrToIPS(ipStr); + conf->ips = STG::UserIPs::parse(ipStr); } -catch (string s) +catch (const std::string & s) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s; printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str()); return -1; } -conf->ips = i; if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadInt("down", &conf->disabled, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read. Parameter Down."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read. Parameter Down."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadInt("passive", &conf->passive, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read. Parameter Passive."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read. Parameter Passive."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str()); return -1; } @@ -860,8 +713,8 @@ for (int i = 0; i < USERDATA_NUM; i++) if (cf.ReadDouble("Credit", &conf->credit, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' data not read. Parameter Credit."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' data not read. Parameter Credit."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str()); return -1; } @@ -869,14 +722,14 @@ if (cf.ReadDouble("Credit", &conf->credit, 0) != 0) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const +int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login) const { -string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; +std::string fileName; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/stat"; if (RestoreUserStat(stat, login, fileName)) { - if (!storeSettings.GetReadBak()) + if (!m_storeSettings.GetReadBak()) { return -1; } @@ -885,7 +738,7 @@ if (RestoreUserStat(stat, login, fileName)) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const +int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login, const std::string & fileName) const { CONFIGFILE cf(fileName); @@ -893,8 +746,8 @@ int e = cf.Error(); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + "."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + "."; printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str()); return -1; } @@ -907,68 +760,68 @@ for (int i = 0; i < DIR_NUM; i++) snprintf(s, 22, "D%d", i); if (cf.ReadULongLongInt(s, &traff, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s); + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s); printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str()); return -1; } - stat->down[i] = traff; + stat->monthDown[i] = traff; snprintf(s, 22, "U%d", i); if (cf.ReadULongLongInt(s, &traff, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s); + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s); printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str()); return -1; } - stat->up[i] = traff; + stat->monthUp[i] = traff; } if (cf.ReadDouble("Cash", &stat->cash, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter Cash"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter Cash"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str()); return -1; } if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str()); return -1; } @@ -976,39 +829,29 @@ if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const +int FILES_STORE::SaveUserConf(const STG::UserConf & conf, const std::string & login) const { -string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; - -BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); - -if (access(fileName.c_str(), W_OK) != 0) - { - FILE * f; - f = fopen(fileName.c_str(), "wb"); - if (f) - fclose(f); - } +std::string fileName; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/conf"; -CONFIGFILE cfstat(fileName); +CONFIGFILE cfstat(fileName, true); int e = cfstat.Error(); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("User \'") + login + "\' conf not written\n"; + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("User \'") + login + "\' conf not written\n"; printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str()); return -1; } -e = chmod(fileName.c_str(), storeSettings.GetConfMode()); -e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID()); +e = chmod(fileName.c_str(), m_storeSettings.GetConfMode()); +e += chown(fileName.c_str(), m_storeSettings.GetConfUID(), m_storeSettings.GetConfGID()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1035,73 +878,65 @@ for (int i = 0; i < USERDATA_NUM; i++) } cfstat.WriteInt("CreditExpire", conf.creditExpire); -stringstream ipStr; +std::ostringstream ipStr; ipStr << conf.ips; cfstat.WriteString("IP", ipStr.str()); return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const +int FILES_STORE::SaveUserStat(const STG::UserStat & stat, const std::string & login) const { -char s[22]; -string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; +std::string fileName; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/stat"; -BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); - -if (access(fileName.c_str(), W_OK) != 0) { - FILE * f; - f = fopen(fileName.c_str(), "wb"); - if (f) - fclose(f); - } + CONFIGFILE cfstat(fileName, true); + int e = cfstat.Error(); -CONFIGFILE cfstat(fileName); -int e = cfstat.Error(); + if (e) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("User \'") + login + "\' stat not written\n"; + printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str()); + return -1; + } -if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("User \'") + login + "\' stat not written\n"; - printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str()); - return -1; - } + for (int i = 0; i < DIR_NUM; i++) + { + char s[22]; + snprintf(s, 22, "D%d", i); + cfstat.WriteInt(s, stat.monthDown[i]); + snprintf(s, 22, "U%d", i); + cfstat.WriteInt(s, stat.monthUp[i]); + } -for (int i = 0; i < DIR_NUM; i++) - { - snprintf(s, 22, "D%d", i); - cfstat.WriteInt(s, stat.down[i]); - snprintf(s, 22, "U%d", i); - cfstat.WriteInt(s, stat.up[i]); + cfstat.WriteDouble("Cash", stat.cash); + cfstat.WriteDouble("FreeMb", stat.freeMb); + cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd); + cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime); + cfstat.WriteInt("PassiveTime", stat.passiveTime); + cfstat.WriteInt("LastActivityTime", stat.lastActivityTime); } -cfstat.WriteDouble("Cash", stat.cash); -cfstat.WriteDouble("FreeMb", stat.freeMb); -cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd); -cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime); -cfstat.WriteInt("PassiveTime", stat.passiveTime); -cfstat.WriteInt("LastActivityTime", stat.lastActivityTime); - -e = chmod(fileName.c_str(), storeSettings.GetStatMode()); -e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID()); +int e = chmod(fileName.c_str(), m_storeSettings.GetStatMode()); +e += chown(fileName.c_str(), m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::WriteLogString(const string & str, const string & login) const +int FILES_STORE::WriteLogString(const std::string & str, const std::string & login) const { FILE * f; time_t tm = time(NULL); -string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/log"; +std::string fileName; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/log"; f = fopen(fileName.c_str(), "at"); if (f) @@ -1114,30 +949,30 @@ if (f) } else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot open \'" + fileName + "\'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot open \'" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str()); return -1; } -int e = chmod(fileName.c_str(), storeSettings.GetLogMode()); -e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID()); +int e = chmod(fileName.c_str(), m_storeSettings.GetLogMode()); +e += chown(fileName.c_str(), m_storeSettings.GetLogUID(), m_storeSettings.GetLogGID()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::WriteLog2String(const string & str, const string & login) const +int FILES_STORE::WriteLog2String(const std::string & str, const std::string & login) const { FILE * f; time_t tm = time(NULL); -string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/log2"; +std::string fileName; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/log2"; f = fopen(fileName.c_str(), "at"); if (f) @@ -1150,80 +985,66 @@ if (f) } else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot open \'" + fileName + "\'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot open \'" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str()); return -1; } -int e = chmod(fileName.c_str(), storeSettings.GetLogMode()); -e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID()); +int e = chmod(fileName.c_str(), m_storeSettings.GetLogMode()); +e += chown(fileName.c_str(), m_storeSettings.GetLogUID(), m_storeSettings.GetLogGID()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::WriteUserChgLog(const string & login, - const string & admLogin, +int FILES_STORE::WriteUserChgLog(const std::string & login, + const std::string & admLogin, uint32_t admIP, - const string & paramName, - const string & oldValue, - const string & newValue, - const string & message) const + const std::string & paramName, + const std::string & oldValue, + const std::string & newValue, + const std::string & message) const { -string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'" +std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'" + paramName + "\' parameter changed from \'" + oldValue + "\' to \'" + newValue + "\'. " + message; return WriteLogString(userLogMsg, login); } //----------------------------------------------------------------------------- -int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const +int FILES_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const { -string logStr = "Connect, " + inet_ntostring(ip); +std::string logStr = "Connect, " + inet_ntostring(ip); if (WriteLogString(logStr, login)) return -1; return WriteLog2String(logStr, login); } //----------------------------------------------------------------------------- -int FILES_STORE::WriteUserDisconnect(const string & login, - const DIR_TRAFF & up, - const DIR_TRAFF & down, - const DIR_TRAFF & sessionUp, - const DIR_TRAFF & sessionDown, +int FILES_STORE::WriteUserDisconnect(const std::string & login, + const STG::DirTraff & monthUp, + const STG::DirTraff & monthDown, + const STG::DirTraff & sessionUp, + const STG::DirTraff & sessionDown, double cash, double freeMb, const std::string & reason) const { -stringstream logStr; -logStr << "Disconnect, "; -/*stringstream sssu; -stringstream sssd; -stringstream ssmu; -stringstream ssmd; -stringstream sscash; - -ssmu << up; -ssmd << down; - -sssu << sessionUp; -sssd << sessionDown; - -sscash << cash;*/ - -logStr << " session upload: \'" +std::ostringstream logStr; +logStr << "Disconnect, " + << " session upload: \'" << sessionUp << "\' session download: \'" << sessionDown << "\' month upload: \'" - << up + << monthUp << "\' month download: \'" - << down + << monthDown << "\' cash: \'" << cash << "\'"; @@ -1241,162 +1062,173 @@ logStr << " freeMb: \'" return WriteLog2String(logStr.str(), login); } //----------------------------------------------------------------------------- -int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const +int FILES_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const { -string str; -CONFIGFILE * s; -int e; -FILE *f; +// Classic stats +std::string stat1; +strprintf(&stat1,"%s/%s/stat.%d.%02d", + m_storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); -strprintf(&str,"%s/%s/stat.%d.%02d", - storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); +CONFIGFILE s(stat1, true); -if ((f = fopen(str.c_str(), "w"))) +if (s.Error()) { - fprintf(f, "\n"); - fclose(f); + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot create file '" + stat1 + "'"; + printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); + return -1; } -s = new CONFIGFILE(str); -e = s->Error(); +// New stats +std::string stat2; +strprintf(&stat2,"%s/%s/stat2.%d.%02d", + m_storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); -if (e) +CONFIGFILE s2(stat2, true); + +if (s2.Error()) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot create file " + str; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot create file '" + stat2 + "'"; printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); return -1; } -char dirName[3]; - -for (int i = 0; i < DIR_NUM; i++) +for (size_t i = 0; i < DIR_NUM; i++) { - snprintf(dirName, 3, "U%d", i); - s->WriteInt(dirName, stat.up[i]); - snprintf(dirName, 3, "D%d", i); - s->WriteInt(dirName, stat.down[i]); + char dirName[3]; + snprintf(dirName, 3, "U%llu", i); + s.WriteInt(dirName, stat.monthUp[i]); // Classic + s2.WriteInt(dirName, stat.monthUp[i]); // New + snprintf(dirName, 3, "D%llu", i); + s.WriteInt(dirName, stat.monthDown[i]); // Classic + s2.WriteInt(dirName, stat.monthDown[i]); // New } -s->WriteDouble("cash", stat.cash); +// Classic +s.WriteDouble("cash", stat.cash); -delete s; +// New +s2.WriteDouble("Cash", stat.cash); +s2.WriteDouble("FreeMb", stat.freeMb); +s2.WriteDouble("LastCashAdd", stat.lastCashAdd); +s2.WriteInt("LastCashAddTime", stat.lastCashAddTime); +s2.WriteInt("PassiveTime", stat.passiveTime); +s2.WriteInt("LastActivityTime", stat.lastActivityTime); return 0; } //-----------------------------------------------------------------------------*/ -int FILES_STORE::AddAdmin(const string & login) const +int FILES_STORE::AddAdmin(const std::string & login) const { -string fileName; -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); -FILE * f; -f = fopen(fileName.c_str(), "wt"); -if (f) +std::string fileName; +strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str()); + +if (Touch(fileName)) { - fprintf(f, "\n"); - fclose(f); - return 0; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot create file " + fileName; + printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str()); + return -1; } -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -errorStr = "Cannot create file " + fileName; -printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str()); -return -1; +return 0; } //-----------------------------------------------------------------------------*/ -int FILES_STORE::DelAdmin(const string & login) const +int FILES_STORE::DelAdmin(const std::string & login) const { -string fileName; -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); +std::string fileName; +strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str()); if (unlink(fileName.c_str())) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "unlink failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "unlink failed. Message: '"; + m_errorStr += strerror(errno); + m_errorStr += "'"; printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno)); } return 0; } //-----------------------------------------------------------------------------*/ -int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const +int FILES_STORE::SaveAdmin(const STG::AdminConf & ac) const { -char passwordE[2 * ADM_PASSWD_LEN + 2]; -char pass[ADM_PASSWD_LEN + 1]; -char adminPass[ADM_PASSWD_LEN + 1]; +std::string fileName; -string fileName; +strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), ac.login.c_str()); -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str()); + { + CONFIGFILE cf(fileName, true); -CONFIGFILE cf(fileName); + int e = cf.Error(); -int e = cf.Error(); + if (e) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot write admin " + ac.login + ". " + fileName; + printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str()); + return -1; + } -if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot write admin " + ac.login + ". " + fileName; - printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str()); - return -1; - } + char pass[ADM_PASSWD_LEN + 1]; + memset(pass, 0, sizeof(pass)); -memset(pass, 0, sizeof(pass)); -memset(adminPass, 0, sizeof(adminPass)); + char adminPass[ADM_PASSWD_LEN + 1]; + memset(adminPass, 0, sizeof(adminPass)); -BLOWFISH_CTX ctx; -EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx); + BLOWFISH_CTX ctx; + InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx); -strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN); -adminPass[ADM_PASSWD_LEN - 1] = 0; + strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN); + adminPass[ADM_PASSWD_LEN - 1] = 0; -for (int i = 0; i < ADM_PASSWD_LEN/8; i++) - { - EncodeString(pass + 8*i, adminPass + 8*i, &ctx); - } + for (int i = 0; i < ADM_PASSWD_LEN/8; i++) + { + EncryptBlock(pass + 8*i, adminPass + 8*i, &ctx); + } -pass[ADM_PASSWD_LEN - 1] = 0; -Encode12(passwordE, pass, ADM_PASSWD_LEN); -//printfd(__FILE__, "passwordE %s\n", passwordE); + pass[ADM_PASSWD_LEN - 1] = 0; + char passwordE[2 * ADM_PASSWD_LEN + 2]; + Encode12(passwordE, pass, ADM_PASSWD_LEN); -cf.WriteString("password", passwordE); -cf.WriteInt("ChgConf", ac.priv.userConf); -cf.WriteInt("ChgPassword", ac.priv.userPasswd); -cf.WriteInt("ChgStat", ac.priv.userStat); -cf.WriteInt("ChgCash", ac.priv.userCash); -cf.WriteInt("UsrAddDel", ac.priv.userAddDel); -cf.WriteInt("ChgTariff", ac.priv.tariffChg); -cf.WriteInt("ChgAdmin", ac.priv.adminChg); + cf.WriteString("password", passwordE); + cf.WriteInt("ChgConf", ac.priv.userConf); + cf.WriteInt("ChgPassword", ac.priv.userPasswd); + cf.WriteInt("ChgStat", ac.priv.userStat); + cf.WriteInt("ChgCash", ac.priv.userCash); + cf.WriteInt("UsrAddDel", ac.priv.userAddDel); + cf.WriteInt("ChgTariff", ac.priv.tariffChg); + cf.WriteInt("ChgAdmin", ac.priv.adminChg); + cf.WriteInt("ChgService", ac.priv.serviceChg); + cf.WriteInt("ChgCorp", ac.priv.corpChg); + } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const +int FILES_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const { -string fileName; -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); +std::string fileName; +strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str()); CONFIGFILE cf(fileName); char pass[ADM_PASSWD_LEN + 1]; char password[ADM_PASSWD_LEN + 1]; -char passwordE[2*ADM_PASSWD_LEN + 2]; +char passwordE[2 * ADM_PASSWD_LEN + 2]; BLOWFISH_CTX ctx; -string p; +std::string p; if (cf.Error()) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot open " + fileName; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot open " + fileName; printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str()); return -1; } -int a; - if (cf.ReadString("password", &p, "*")) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter password"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter password"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str()); return -1; } @@ -1404,18 +1236,16 @@ if (cf.ReadString("password", &p, "*")) memset(passwordE, 0, sizeof(passwordE)); strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN); -//printfd(__FILE__, "passwordE %s\n", passwordE); - memset(pass, 0, sizeof(pass)); if (passwordE[0] != 0) { Decode21(pass, passwordE); - EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx); + InitContext(adm_enc_passwd, strlen(adm_enc_passwd), &ctx); for (int i = 0; i < ADM_PASSWD_LEN/8; i++) { - DecodeString(password + 8*i, pass + 8*i, &ctx); + DecryptBlock(password + 8*i, pass + 8*i, &ctx); } } else @@ -1425,136 +1255,143 @@ else ac->password = password; -if (cf.ReadInt("ChgConf", &a, 0) == 0) +uint16_t a; + +if (cf.ReadUShortInt("ChgConf", &a, 0) == 0) ac->priv.userConf = a; else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter ChgConf"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter ChgConf"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str()); return -1; } -if (cf.ReadInt("ChgPassword", &a, 0) == 0) +if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0) ac->priv.userPasswd = a; else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter ChgPassword"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter ChgPassword"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str()); return -1; } -if (cf.ReadInt("ChgStat", &a, 0) == 0) +if (cf.ReadUShortInt("ChgStat", &a, 0) == 0) ac->priv.userStat = a; else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter ChgStat"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter ChgStat"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str()); return -1; } -if (cf.ReadInt("ChgCash", &a, 0) == 0) +if (cf.ReadUShortInt("ChgCash", &a, 0) == 0) ac->priv.userCash = a; else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter ChgCash"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter ChgCash"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str()); return -1; } -if (cf.ReadInt("UsrAddDel", &a, 0) == 0) +if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0) ac->priv.userAddDel = a; else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter UsrAddDel"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter UsrAddDel"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str()); return -1; } -if (cf.ReadInt("ChgAdmin", &a, 0) == 0) +if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0) ac->priv.adminChg = a; else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter ChgAdmin"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter ChgAdmin"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str()); return -1; } -if (cf.ReadInt("ChgTariff", &a, 0) == 0) +if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0) ac->priv.tariffChg = a; else { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error in parameter ChgTariff"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter ChgTariff"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str()); return -1; } +if (cf.ReadUShortInt("ChgService", &a, 0) == 0) + ac->priv.serviceChg = a; +else + ac->priv.serviceChg = 0; + +if (cf.ReadUShortInt("ChgCorp", &a, 0) == 0) + ac->priv.corpChg = a; +else + ac->priv.corpChg = 0; + return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::AddTariff(const string & name) const +int FILES_STORE::AddTariff(const std::string & name) const { -string fileName; -strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str()); -FILE * f; -f = fopen(fileName.c_str(), "wt"); -if (f) +std::string fileName; +strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str()); +if (Touch(fileName)) { - fprintf(f, "\n"); - fclose(f); - return 0; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot create file " + fileName; + printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str()); + return -1; } - -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -errorStr = "Cannot create file " + fileName; -printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str()); -return -1; +return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::DelTariff(const string & name) const +int FILES_STORE::DelTariff(const std::string & name) const { -string fileName; -strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str()); +std::string fileName; +strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str()); if (unlink(fileName.c_str())) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "unlink failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "unlink failed. Message: '"; + m_errorStr += strerror(errno); + m_errorStr += "'"; printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno)); } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const +int FILES_STORE::RestoreTariff(STG::TariffData * td, const std::string & tariffName) const { -string tariffFileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; -CONFIGFILE conf(tariffFileName); -string str; +std::string fileName = m_storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; +CONFIGFILE conf(fileName); +std::string str; td->tariffConf.name = tariffName; if (conf.Error() != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read file " + tariffFileName; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read file " + fileName; printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str()); return -1; } -string param; +std::string param; for (int i = 0; idirPrice[i].priceDayA, 0.0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1578,8 +1415,8 @@ for (int i = 0; idirPrice[i].priceDayB, 0.0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1588,8 +1425,8 @@ for (int i = 0; idirPrice[i].priceNightA, 0.0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1598,8 +1435,8 @@ for (int i = 0; idirPrice[i].priceNightB, 0.0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1608,8 +1445,8 @@ for (int i = 0; idirPrice[i].threshold, 0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1617,8 +1454,8 @@ for (int i = 0; idirPrice[i].singlePrice, 0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1626,8 +1463,8 @@ for (int i = 0; idirPrice[i].noDiscount, 0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1635,168 +1472,257 @@ for (int i = 0; itariffConf.fee, 0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee"; printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str()); return -1; } if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter Free"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter Free"; printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str()); return -1; } if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost"; printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str()); return -1; } if (conf.ReadString("TraffType", &str, "") < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType"; printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str()); return -1; } -if (!strcasecmp(str.c_str(), "up")) - td->tariffConf.traffType = TRAFF_UP; +td->tariffConf.traffType = STG::Tariff::parseTraffType(str); + +if (conf.ReadString("Period", &str, "month") < 0) + td->tariffConf.period = STG::Tariff::MONTH; else - if (!strcasecmp(str.c_str(), "down")) - td->tariffConf.traffType = TRAFF_DOWN; - else - if (!strcasecmp(str.c_str(), "up+down")) - td->tariffConf.traffType = TRAFF_UP_DOWN; - else - if (!strcasecmp(str.c_str(), "max")) - td->tariffConf.traffType = TRAFF_MAX; - else - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect"; - printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str()); - return -1; - } + td->tariffConf.period = STG::Tariff::parsePeriod(str); + +if (conf.ReadString("ChangePolicy", &str, "allow") < 0) + td->tariffConf.changePolicy = STG::Tariff::ALLOW; +else + td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(str); + +conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0); return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const +int FILES_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const { -string tariffFileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; -if (access(tariffFileName.c_str(), W_OK) != 0) +std::string fileName = m_storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; + { - int fd = open(tariffFileName.c_str(), O_CREAT, 0600); - if (fd) - close(fd); - } + CONFIGFILE cf(fileName, true); -CONFIGFILE cf(tariffFileName); + int e = cf.Error(); -int e = cf.Error(); + if (e) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error writing tariff " + tariffName; + printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str()); + return e; + } -if (e) + std::string param; + for (int i = 0; i < DIR_NUM; i++) + { + strprintf(¶m, "PriceDayA%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega); + + strprintf(¶m, "PriceDayB%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega); + + strprintf(¶m, "PriceNightA%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega); + + strprintf(¶m, "PriceNightB%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega); + + strprintf(¶m, "Threshold%d", i); + cf.WriteInt(param, td.dirPrice[i].threshold); + + std::string s; + strprintf(¶m, "Time%d", i); + + strprintf(&s, "%0d:%0d-%0d:%0d", + td.dirPrice[i].hDay, + td.dirPrice[i].mDay, + td.dirPrice[i].hNight, + td.dirPrice[i].mNight); + + cf.WriteString(param, s); + + strprintf(¶m, "NoDiscount%d", i); + cf.WriteInt(param, td.dirPrice[i].noDiscount); + + strprintf(¶m, "SinglePrice%d", i); + cf.WriteInt(param, td.dirPrice[i].singlePrice); + } + + cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost); + cf.WriteDouble("Fee", td.tariffConf.fee); + cf.WriteDouble("Free", td.tariffConf.free); + cf.WriteString("TraffType", STG::Tariff::toString(td.tariffConf.traffType)); + cf.WriteString("Period", STG::Tariff::toString(td.tariffConf.period)); + cf.WriteString("ChangePolicy", STG::Tariff::toString(td.tariffConf.changePolicy)); + cf.WriteTime("ChangePolicyTimeout", td.tariffConf.changePolicyTimeout); + } + +return 0; +} +//-----------------------------------------------------------------------------*/ +int FILES_STORE::AddService(const std::string & name) const +{ +std::string fileName; +strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), name.c_str()); + +if (Touch(fileName)) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error writing tariff " + tariffName; - printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str()); - return e; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot create file " + fileName; + printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str()); + return -1; } -string param; -for (int i = 0; i < DIR_NUM; i++) +return 0; +} +//-----------------------------------------------------------------------------*/ +int FILES_STORE::DelService(const std::string & name) const +{ +std::string fileName; +strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), name.c_str()); +if (unlink(fileName.c_str())) { - strprintf(¶m, "PriceDayA%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega); + STG_LOCKER lock(&m_mutex); + m_errorStr = "unlink failed. Message: '"; + m_errorStr += strerror(errno); + m_errorStr += "'"; + printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno)); + } +return 0; +} +//-----------------------------------------------------------------------------*/ +int FILES_STORE::SaveService(const STG::ServiceConf & conf) const +{ +std::string fileName; - strprintf(¶m, "PriceDayB%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega); +strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), conf.name.c_str()); - strprintf(¶m, "PriceNightA%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega); + { + CONFIGFILE cf(fileName, true); - strprintf(¶m, "PriceNightB%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega); + int e = cf.Error(); - strprintf(¶m, "Threshold%d", i); - cf.WriteInt(param, td.dirPrice[i].threshold); + if (e) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot write service " + conf.name + ". " + fileName; + printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str()); + return -1; + } - string s; - strprintf(¶m, "Time%d", i); + cf.WriteString("name", conf.name); + cf.WriteString("comment", conf.comment); + cf.WriteDouble("cost", conf.cost); + cf.WriteInt("pay_day", conf.payDay); + } - strprintf(&s, "%0d:%0d-%0d:%0d", - td.dirPrice[i].hDay, - td.dirPrice[i].mDay, - td.dirPrice[i].hNight, - td.dirPrice[i].mNight); +return 0; +} +//----------------------------------------------------------------------------- +int FILES_STORE::RestoreService(STG::ServiceConf * conf, const std::string & name) const +{ +std::string fileName; +strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), name.c_str()); +CONFIGFILE cf(fileName); - cf.WriteString(param, s); +if (cf.Error()) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot open " + fileName; + printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str()); + return -1; + } - strprintf(¶m, "NoDiscount%d", i); - cf.WriteInt(param, td.dirPrice[i].noDiscount); +if (cf.ReadString("name", &conf->name, name)) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter 'name'"; + printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str()); + return -1; + } - strprintf(¶m, "SinglePrice%d", i); - cf.WriteInt(param, td.dirPrice[i].singlePrice); +if (cf.ReadString("comment", &conf->comment, "")) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter 'comment'"; + printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str()); + return -1; } -cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost); -cf.WriteDouble("Fee", td.tariffConf.fee); -cf.WriteDouble("Free", td.tariffConf.free); +if (cf.ReadDouble("cost", &conf->cost, 0.0)) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter 'cost'"; + printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str()); + return -1; + } -switch (td.tariffConf.traffType) +unsigned short value = 0; +if (cf.ReadUShortInt("pay_day", &value, 0)) { - case TRAFF_UP: - cf.WriteString("TraffType", "up"); - break; - case TRAFF_DOWN: - cf.WriteString("TraffType", "down"); - break; - case TRAFF_UP_DOWN: - cf.WriteString("TraffType", "up+down"); - break; - case TRAFF_MAX: - cf.WriteString("TraffType", "max"); - break; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error in parameter 'pay_day'"; + printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str()); + return -1; } +conf->payDay = value; + return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::WriteDetailedStat(const map & statTree, +int FILES_STORE::WriteDetailedStat(const STG::TraffStat & statTree, time_t lastStat, - const string & login) const + const std::string & login) const { char fn[FN_STR_LEN]; char dn[FN_STR_LEN]; FILE * statFile; -//char timestr[30]; -time_t t;//, lastTimeReal; +time_t t; tm * lt; t = time(NULL); -snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str()); +snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + string(dn) + "\' cannot be created."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } } -int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID()); -e += chmod(dn, storeSettings.GetStatModeDir()); +int e = chown(dn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID()); +e += chmod(dn, m_storeSettings.GetStatModeDir()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1809,7 +1735,7 @@ if (lt->tm_hour == 0 && lt->tm_min <= 5) } snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d", - storeSettings.GetUsersDir().c_str(), + m_storeSettings.GetUsersDir().c_str(), login.c_str(), lt->tm_year+1900); @@ -1817,24 +1743,24 @@ if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + string(dn) + "\' cannot be created."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } } -e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID()); -e += chmod(dn, storeSettings.GetStatModeDir()); +e = chown(dn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID()); +e += chmod(dn, m_storeSettings.GetStatModeDir()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d", - storeSettings.GetUsersDir().c_str(), + m_storeSettings.GetUsersDir().c_str(), login.c_str(), lt->tm_year+1900, lt->tm_mon+1 < 10 ? "0" : "", @@ -1843,19 +1769,19 @@ if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + string(dn) + "\' cannot be created."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } } -e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID()); -e += chmod(dn, storeSettings.GetStatModeDir()); +e = chown(dn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID()); +e += chmod(dn, m_storeSettings.GetStatModeDir()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1865,8 +1791,8 @@ statFile = fopen (fn, "at"); if (!statFile) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "File \'" + string(fn) + "\' cannot be written."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "File \'" + std::string(fn) + "\' cannot be written."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -1892,20 +1818,19 @@ s2 = lt2->tm_sec; if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n", h1, m1, s1, h2, m2, s2) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("fprint failed. Message: '") + strerror(errno) + "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(statFile); return -1; } -map::const_iterator stIter; -stIter = statTree.begin(); +auto stIter = statTree.begin(); while (stIter != statTree.end()) { - string u, d; - x2str(stIter->second.up, u); - x2str(stIter->second.down, d); + const auto u = std::to_string(stIter->second.up); + const auto d = std::to_string(stIter->second.down); #ifdef TRAFF_STAT_WITH_PORTS if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n", inet_ntostring(stIter->first.ip).c_str(), @@ -1915,11 +1840,12 @@ while (stIter != statTree.end()) u.c_str(), stIter->second.cash) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "fprint failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "fprint failed. Message: '"; + m_errorStr += strerror(errno); + m_errorStr += "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(statFile); return -1; } #else @@ -1930,11 +1856,12 @@ while (stIter != statTree.end()) u.c_str(), stIter->second.cash) < 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("fprint failed. Message: '"); - errorStr += strerror(errno); - errorStr += "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("fprint failed. Message: '"); + m_errorStr += strerror(errno); + m_errorStr += "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(statFile); return -1; } #endif @@ -1944,99 +1871,88 @@ while (stIter != statTree.end()) fclose(statFile); -e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID()); -e += chmod(fn, storeSettings.GetStatMode()); +e = chown(fn, m_storeSettings.GetStatUID(), m_storeSettings.GetStatGID()); +e += chmod(fn, m_storeSettings.GetStatMode()); if (e) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); + STG_LOCKER lock(&m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const +int FILES_STORE::AddMessage(STG::Message * msg, const std::string & login) const { -//ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ. -//úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ -//ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ. - -string fn; -string dn; +std::string fn; +std::string dn; struct timeval tv; -FILE * msgFile; -strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str()); +strprintf(&dn, "%s/%s/messages", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (access(dn.c_str(), F_OK) != 0) { if (mkdir(dn.c_str(), 0700) != 0) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'"; - errorStr += dn; - errorStr += "\' cannot be created."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Directory \'"; + m_errorStr += dn; + m_errorStr += "\' cannot be created."; printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } } -chmod(dn.c_str(), storeSettings.GetConfMode() | S_IXUSR); +chmod(dn.c_str(), m_storeSettings.GetConfModeDir()); gettimeofday(&tv, NULL); -msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec); +msg->header.id = tv.tv_sec * 1000000 + tv.tv_usec; strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id); -msgFile = fopen (fn.c_str(), "wt"); - -if (!msgFile) +if (Touch(fn)) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "File \'"; - errorStr += fn; - errorStr += "\' cannot be writen."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "File \'"; + m_errorStr += fn; + m_errorStr += "\' cannot be writen."; printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } -fclose(msgFile); + return EditMessage(*msg, login); } //----------------------------------------------------------------------------- -int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const +int FILES_STORE::EditMessage(const STG::Message & msg, const std::string & login) const { -//ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ. -//úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ -//ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ. - -string fn; +std::string fileName; FILE * msgFile; -strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id); +strprintf(&fileName, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id); -if (access(fn.c_str(), F_OK) != 0) +if (access(fileName.c_str(), F_OK) != 0) { - string idstr; - x2str(msg.header.id, idstr); - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Message for user \'"; - errorStr += login + "\' with ID \'"; - errorStr += idstr + "\' does not exist."; - printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str()); + STG_LOCKER lock(&m_mutex); + m_errorStr = "Message for user \'"; + m_errorStr += login + "\' with ID \'"; + m_errorStr += std::to_string(msg.header.id) + "\' does not exist."; + printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", m_errorStr.c_str()); return -1; } -msgFile = fopen(fn.c_str(), "wt"); +Touch(fileName + ".new"); + +msgFile = fopen((fileName + ".new").c_str(), "wt"); if (!msgFile) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "File \'" + fn + "\' cannot be writen."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "File \'" + fileName + "\' cannot be writen."; printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } bool res = true; -res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0); +res &= (fprintf(msgFile, "%u\n", msg.header.type) >= 0); res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0); res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0); res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0); @@ -2046,42 +1962,55 @@ res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0); if (!res) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(msgFile); return -1; } fclose(msgFile); -chmod(fn.c_str(), storeSettings.GetConfMode()); +chmod((fileName + ".new").c_str(), m_storeSettings.GetConfMode()); + +if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0) + { + STG_LOCKER lock(&m_mutex); + m_errorStr = "Error moving dir from " + fileName + ".new to " + fileName; + printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno)); + return -1; + } + return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const +int FILES_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const { -string fn; -strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id); +std::string fn; +strprintf(&fn, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str(), login.c_str(), id); msg->header.id = id; return ReadMessage(fn, &msg->header, &msg->text); } //----------------------------------------------------------------------------- -int FILES_STORE::DelMessage(uint64_t id, const string & login) const +int FILES_STORE::DelMessage(uint64_t id, const std::string & login) const { -string fn; -strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id); +std::string fn; +strprintf(&fn, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str(), login.c_str(), id); return unlink(fn.c_str()); } //----------------------------------------------------------------------------- -int FILES_STORE::GetMessageHdrs(vector * hdrsList, const string & login) const +int FILES_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const { -vector messages; -string dn; -dn = storeSettings.GetUsersDir() + "/" + login + "/messages/"; -GetFilesList(&messages, dn, S_IFREG, ""); +std::string dn(m_storeSettings.GetUsersDir() + "/" + login + "/messages/"); -//hdrsList->resize(messages.size()); +if (access(dn.c_str(), F_OK) != 0) + { + return 0; + } + +std::vector messages; +GetFileList(&messages, dn, S_IFREG, ""); for (unsigned i = 0; i < messages.size(); i++) { @@ -2091,15 +2020,15 @@ for (unsigned i = 0; i < messages.size(); i++) { if (unlink((dn + messages[i]).c_str())) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("unlink failed. Message: '") + strerror(errno) + "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno)); return -1; } continue; } - STG_MSG_HDR hdr; + STG::Message::Header hdr; if (ReadMessage(dn + messages[i], &hdr, NULL)) { return -1; @@ -2109,8 +2038,8 @@ for (unsigned i = 0; i < messages.size(); i++) { if (unlink((dn + messages[i]).c_str())) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("unlink failed. Message: '") + strerror(errno) + "'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -2123,18 +2052,18 @@ for (unsigned i = 0; i < messages.size(); i++) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::ReadMessage(const string & fileName, - STG_MSG_HDR * hdr, - string * text) const +int FILES_STORE::ReadMessage(const std::string & fileName, + STG::Message::Header * hdr, + std::string * text) const { FILE * msgFile; msgFile = fopen(fileName.c_str(), "rt"); if (!msgFile) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "File \'"; - errorStr += fileName; - errorStr += "\' cannot be openned."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "File \'"; + m_errorStr += fileName; + m_errorStr += "\' cannot be openned."; printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -2144,7 +2073,7 @@ d[0] = &hdr->type; d[1] = &hdr->lastSendTime; d[2] = &hdr->creationTime; d[3] = &hdr->showTime; -d[4] = (unsigned*)(&hdr->repeat); +d[4] = reinterpret_cast(&hdr->repeat); d[5] = &hdr->repeatPeriod; memset(p, 0, sizeof(p)); @@ -2152,10 +2081,10 @@ memset(p, 0, sizeof(p)); for (int pos = 0; pos < 6; pos++) { if (fgets(p, sizeof(p) - 1, msgFile) == NULL) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read file \'"; - errorStr += fileName; - errorStr += "\'. Missing data."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read file \'"; + m_errorStr += fileName; + m_errorStr += "\'. Missing data."; printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n"); printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos); fclose(msgFile); @@ -2170,10 +2099,10 @@ for (int pos = 0; pos < 6; pos++) if (feof(msgFile)) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read file \'"; - errorStr += fileName; - errorStr += "\'. Missing data."; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read file \'"; + m_errorStr += fileName; + m_errorStr += "\'. Missing data."; printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n"); printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos); fclose(msgFile); @@ -2182,12 +2111,12 @@ for (int pos = 0; pos < 6; pos++) if (str2x(p, *(d[pos]))) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read file \'"; - errorStr += fileName; - errorStr += "\'. Incorrect value. \'"; - errorStr += p; - errorStr += "\'"; + STG_LOCKER lock(&m_mutex); + m_errorStr = "Cannot read file \'"; + m_errorStr += fileName; + m_errorStr += "\'. Incorrect value. \'"; + m_errorStr += p; + m_errorStr += "\'"; printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n"); fclose(msgFile); return -1; @@ -2209,9 +2138,67 @@ if (text) (*text) += txt; } } -//fprintf(msgFile, "%s\n", msg.text.c_str()); fclose(msgFile); return 0; } //----------------------------------------------------------------------------- +int FILES_STORE::Touch(const std::string & path) const +{ +FILE * f = fopen(path.c_str(), "wb"); +if (f) + { + fclose(f); + return 0; + } +return -1; +} +//----------------------------------------------------------------------------- +int GetFileList(std::vector * fileList, const std::string & directory, mode_t mode, const std::string & ext) +{ +DIR * d = opendir(directory.c_str()); + +if (!d) + { + printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno)); + return -1; + } +dirent * entry; +while ((entry = readdir(d))) + { + if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))) + continue; + + std::string str = directory + "/" + std::string(entry->d_name); + + struct stat st; + if (stat(str.c_str(), &st)) + continue; + + if (!(st.st_mode & mode)) // Filter by mode + continue; + + if (!ext.empty()) + { + // Check extension + size_t d_nameLen = strlen(entry->d_name); + if (d_nameLen <= ext.size()) + continue; + + if (ext == entry->d_name + (d_nameLen - ext.size())) + { + entry->d_name[d_nameLen - ext.size()] = 0; + fileList->push_back(entry->d_name); + } + } + else + { + fileList->push_back(entry->d_name); + } + } + +closedir(d); + +return 0; +} +//-----------------------------------------------------------------------------