X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/f593f4ebbb85b779f7424a1f49465672d2076cf4..296c906e2689b9b5dc26d2f2d3e1e2d9e1662d3b:/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 03a672aa..d654a28c 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -28,20 +28,7 @@ #define _GNU_SOURCE #endif -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include +#include "file_store.h" #include "stg/common.h" #include "stg/user_ips.h" @@ -50,9 +37,25 @@ #include "stg/const.h" #include "stg/blowfish.h" #include "stg/logger.h" -#include "stg/locker.h" -#include "stg/plugin_creator.h" -#include "file_store.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 #define DELETED_USERS_DIR "deleted_users" @@ -66,71 +69,76 @@ const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- namespace { -PLUGIN_CREATOR fsc; + +bool CheckAndCreate(const std::string & dir, mode_t mode) +{ +if (access(dir.c_str(), F_OK) == 0) + return true; +if (mkdir(dir.c_str(), mode) == 0) + return true; +return false; } -extern "C" STORE * GetStore(); -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -STORE * GetStore() +} + +extern "C" STG::Store* GetStore() { -return fsc.GetPlugin(); + static FILES_STORE plugin; + return &plugin; } //----------------------------------------------------------------------------- FILES_STORE_SETTINGS::FILES_STORE_SETTINGS() - : settings(NULL), - statMode(0), - statUID(0), - statGID(0), - confMode(0), - confUID(0), - confGID(0), - userLogMode(0), - userLogUID(0), - userLogGID(0), - 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) { } //----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseOwner(const std::vector & moduleParams, const std::string & owner, uid_t * uid) +int FILES_STORE_SETTINGS::ParseOwner(const std::vector & moduleParams, const std::string & owner, uid_t * uid) { -PARAM_VALUE pv; +STG::ParamValue pv; pv.param = owner; -std::vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); 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 std::vector & moduleParams, const std::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; -std::vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); 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; @@ -149,65 +157,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 std::vector & moduleParams, const std::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; -std::vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); 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; -std::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() || 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; @@ -218,11 +226,11 @@ pv.param = "ReadBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); 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; @@ -233,26 +241,51 @@ pv.param = "WorkDir"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); 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())) + { + 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())) { - workDir.resize(workDir.size() - 1); + 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 std::string & FILES_STORE_SETTINGS::GetStrError() const { -return errorStr; +return m_errorStr; } //----------------------------------------------------------------------------- int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid) @@ -261,8 +294,8 @@ struct passwd * pw; pw = getpwnam(user); if (!pw) { - errorStr = std::string("User \'") + std::string(user) + std::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; } @@ -276,8 +309,8 @@ struct group * grp; grp = getgrnam(gr); if (!grp) { - errorStr = std::string("Group \'") + std::string(gr) + std::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; } @@ -287,74 +320,63 @@ return 0; //----------------------------------------------------------------------------- int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode) { -char a; -char b; -char c; if (strlen(str) > 3) { - errorStr = std::string("Error parsing mode \'") + str + std::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 = std::string("Error parsing mode \'") + str + std::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; } //----------------------------------------------------------------------------- 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; } //----------------------------------------------------------------------------- mode_t FILES_STORE_SETTINGS::GetConfModeDir() const { -mode_t mode = confMode; -if (confMode & S_IRUSR) mode |= S_IXUSR; -if (confMode & S_IRGRP) mode |= S_IXGRP; -if (confMode & 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; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- FILES_STORE::FILES_STORE() - : errorStr(), - version("file_store v.1.04"), - storeSettings(), - settings(), - mutex(), - logger(GetPluginLogger(GetStgLogger(), "store_files")) + : m_version("file_store v.1.04"), + m_logger(STG::PluginLogger::get("store_files")) { -pthread_mutexattr_t attr; -pthread_mutexattr_init(&attr); -pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); -pthread_mutex_init(&mutex, &attr); } //----------------------------------------------------------------------------- int FILES_STORE::ParseSettings() { -int ret = storeSettings.ParseSettings(settings); +int ret = m_storeSettings.ParseSettings(m_settings); if (ret) { - STG_LOCKER lock(&mutex); - errorStr = storeSettings.GetStrError(); + std::lock_guard lock(m_mutex); + m_errorStr = m_storeSettings.GetStrError(); } return ret; } @@ -363,14 +385,14 @@ int FILES_STORE::GetUsersList(std::vector * userList) const { std::vector files; -if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, "")) +if (GetFileList(&files, m_storeSettings.GetUsersDir(), S_IFDIR, "")) { - STG_LOCKER lock(&mutex); - errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + std::string(strerror(errno)); + std::lock_guard lock(m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetUsersDir() + "': " + std::string(strerror(errno)); return -1; } -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); userList->swap(files); @@ -381,14 +403,14 @@ int FILES_STORE::GetAdminsList(std::vector * adminList) const { std::vector files; -if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm")) +if (GetFileList(&files, m_storeSettings.GetAdminsDir(), S_IFREG, ".adm")) { - STG_LOCKER lock(&mutex); - errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno)); + std::lock_guard lock(m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno)); return -1; } -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); adminList->swap(files); @@ -399,17 +421,35 @@ int FILES_STORE::GetTariffsList(std::vector * tariffList) const { std::vector files; -if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf")) +if (GetFileList(&files, m_storeSettings.GetTariffsDir(), S_IFREG, ".tf")) { - STG_LOCKER lock(&mutex); - errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno)); + std::lock_guard lock(m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno)); return -1; } -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); tariffList->swap(files); +return 0; +} +//----------------------------------------------------------------------------- +int FILES_STORE::GetServicesList(std::vector * list) const +{ +std::vector files; + +if (GetFileList(&files, m_storeSettings.GetServicesDir(), S_IFREG, ".serv")) + { + std::lock_guard lock(m_mutex); + m_errorStr = "Failed to open '" + m_storeSettings.GetServicesDir() + "': " + std::string(strerror(errno)); + return -1; + } + +std::lock_guard lock(m_mutex); + +list->swap(files); + return 0; } //----------------------------------------------------------------------------- @@ -419,9 +459,9 @@ DIR * d = opendir(path); if (!d) { - errorStr = "failed to open dir. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + 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; } @@ -443,10 +483,10 @@ while ((entry = readdir(d))) { if (unlink(str.c_str())) { - STG_LOCKER lock(&mutex); - errorStr = "unlink failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + std::lock_guard 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; @@ -468,10 +508,10 @@ closedir(d); if (rmdir(path)) { - STG_LOCKER lock(&mutex); - errorStr = "rmdir failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + std::lock_guard 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; } @@ -483,30 +523,30 @@ int FILES_STORE::AddUser(const std::string & login) const { 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); - errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'"; + std::lock_guard 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()); +strprintf(&fileName, "%s%s/conf", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (Touch(fileName)) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot create file \"" + fileName + "\'"; + std::lock_guard 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()); +strprintf(&fileName, "%s%s/stat", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (Touch(fileName)) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot create file \"" + fileName + "\'"; + std::lock_guard lock(m_mutex); + m_errorStr = "Cannot create file \"" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -518,13 +558,13 @@ int FILES_STORE::DelUser(const std::string & login) const std::string dirName; std::string dirName1; -strprintf(&dirName, "%s/%s", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR); +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); - errorStr = "Directory '" + dirName + "' cannot be created."; + std::lock_guard lock(m_mutex); + m_errorStr = "Directory '" + dirName + "' cannot be created."; printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -532,19 +572,19 @@ if (access(dirName.c_str(), F_OK) != 0) if (access(dirName.c_str(), F_OK) == 0) { - strprintf(&dirName, "%s/%s/%s.%lu", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR, 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); - errorStr = "Error moving dir from " + dirName1 + " to " + dirName; + std::lock_guard 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; @@ -553,13 +593,13 @@ else return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const +int FILES_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login) const { std::string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/conf"; if (RestoreUserConf(conf, login, fileName)) { - if (!storeSettings.GetReadBak()) + if (!m_storeSettings.GetReadBak()) { return -1; } @@ -568,85 +608,83 @@ if (RestoreUserConf(conf, login, fileName)) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login, const std::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(); if (e) { - STG_LOCKER lock(&mutex); - errorStr = "User \'" + login + "\' data not read."; + std::lock_guard 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); - errorStr = "User \'" + login + "\' data not read. Parameter Password."; + std::lock_guard 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); - errorStr = "User \'" + login + "\' password is blank."; + std::lock_guard 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); - errorStr = "User \'" + login + "\' data not read. Parameter Tariff."; + std::lock_guard 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); - errorStr = "User \'" + login + "\' tariff is blank."; + std::lock_guard 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; } std::string ipStr; cf.ReadString("IP", &ipStr, "?"); -USER_IPS ips; try { - ips = StrToIPS(ipStr); + conf->ips = STG::UserIPs::parse(ipStr); } catch (const std::string & s) { - STG_LOCKER lock(&mutex); - errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s; + std::lock_guard 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 = ips; if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0) { - STG_LOCKER lock(&mutex); - errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline."; + std::lock_guard 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); - errorStr = "User \'" + login + "\' data not read. Parameter Down."; + std::lock_guard 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); - errorStr = "User \'" + login + "\' data not read. Parameter Passive."; + std::lock_guard 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; } @@ -670,8 +708,8 @@ for (int i = 0; i < USERDATA_NUM; i++) if (cf.ReadDouble("Credit", &conf->credit, 0) != 0) { - STG_LOCKER lock(&mutex); - errorStr = "User \'" + login + "\' data not read. Parameter Credit."; + std::lock_guard 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; } @@ -679,14 +717,14 @@ if (cf.ReadDouble("Credit", &conf->credit, 0) != 0) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const +int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login) const { std::string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/stat"; if (RestoreUserStat(stat, login, fileName)) { - if (!storeSettings.GetReadBak()) + if (!m_storeSettings.GetReadBak()) { return -1; } @@ -695,7 +733,7 @@ if (RestoreUserStat(stat, login, fileName)) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login, const std::string & fileName) const +int FILES_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login, const std::string & fileName) const { CONFIGFILE cf(fileName); @@ -703,8 +741,8 @@ int e = cf.Error(); if (e) { - STG_LOCKER lock(&mutex); - errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + "."; + std::lock_guard 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; } @@ -717,8 +755,8 @@ 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); - errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s); + std::lock_guard 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; } @@ -727,8 +765,8 @@ for (int i = 0; i < DIR_NUM; i++) snprintf(s, 22, "U%d", i); if (cf.ReadULongLongInt(s, &traff, 0) != 0) { - STG_LOCKER lock(&mutex); - errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s); + std::lock_guard 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; } @@ -737,48 +775,48 @@ for (int i = 0; i < DIR_NUM; i++) if (cf.ReadDouble("Cash", &stat->cash, 0) != 0) { - STG_LOCKER lock(&mutex); - errorStr = "User \'" + login + "\' stat not read. Parameter Cash"; + std::lock_guard 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); - errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb"; + std::lock_guard 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); - errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime"; + std::lock_guard 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); - errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime"; + std::lock_guard 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); - errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd"; + std::lock_guard 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); - errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime"; + std::lock_guard 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; } @@ -786,10 +824,10 @@ if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const +int FILES_STORE::SaveUserConf(const STG::UserConf & conf, const std::string & login) const { std::string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/conf"; CONFIGFILE cfstat(fileName, true); @@ -797,18 +835,18 @@ int e = cfstat.Error(); if (e) { - STG_LOCKER lock(&mutex); - errorStr = std::string("User \'") + login + "\' conf not written\n"; + std::lock_guard 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); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -842,10 +880,10 @@ cfstat.WriteString("IP", ipStr.str()); return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const +int FILES_STORE::SaveUserStat(const STG::UserStat & stat, const std::string & login) const { std::string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/stat"; { CONFIGFILE cfstat(fileName, true); @@ -853,8 +891,8 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; if (e) { - STG_LOCKER lock(&mutex); - errorStr = std::string("User \'") + login + "\' stat not written\n"; + std::lock_guard 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; } @@ -876,12 +914,12 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; cfstat.WriteInt("LastActivityTime", stat.lastActivityTime); } -int 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); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -893,7 +931,7 @@ int FILES_STORE::WriteLogString(const std::string & str, const std::string & log FILE * f; time_t tm = time(NULL); std::string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/log"; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/log"; f = fopen(fileName.c_str(), "at"); if (f) @@ -906,18 +944,18 @@ if (f) } else { - STG_LOCKER lock(&mutex); - errorStr = "Cannot open \'" + fileName + "\'"; + std::lock_guard 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); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -929,7 +967,7 @@ int FILES_STORE::WriteLog2String(const std::string & str, const std::string & lo FILE * f; time_t tm = time(NULL); std::string fileName; -fileName = storeSettings.GetUsersDir() + "/" + login + "/log2"; +fileName = m_storeSettings.GetUsersDir() + "/" + login + "/log2"; f = fopen(fileName.c_str(), "at"); if (f) @@ -942,18 +980,18 @@ if (f) } else { - STG_LOCKER lock(&mutex); - errorStr = "Cannot open \'" + fileName + "\'"; + std::lock_guard 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); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -984,10 +1022,10 @@ return WriteLog2String(logStr, login); } //----------------------------------------------------------------------------- int FILES_STORE::WriteUserDisconnect(const std::string & login, - const DIR_TRAFF & monthUp, - const DIR_TRAFF & monthDown, - const DIR_TRAFF & sessionUp, - const DIR_TRAFF & sessionDown, + 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 @@ -1019,19 +1057,19 @@ logStr << " freeMb: \'" return WriteLog2String(logStr.str(), login); } //----------------------------------------------------------------------------- -int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const +int FILES_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const { // Classic stats std::string stat1; strprintf(&stat1,"%s/%s/stat.%d.%02d", - storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); + m_storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); CONFIGFILE s(stat1, true); if (s.Error()) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot create file '" + stat1 + "'"; + std::lock_guard 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; } @@ -1039,14 +1077,14 @@ if (s.Error()) // New stats std::string stat2; strprintf(&stat2,"%s/%s/stat2.%d.%02d", - storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); + m_storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); CONFIGFILE s2(stat2, true); if (s2.Error()) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot create file '" + stat2 + "'"; + std::lock_guard 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; } @@ -1054,10 +1092,10 @@ if (s2.Error()) for (size_t i = 0; i < DIR_NUM; i++) { char dirName[3]; - snprintf(dirName, 3, "U%llu", (unsigned long long)i); + 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", (unsigned long long)i); + snprintf(dirName, 3, "D%llu", i); s.WriteInt(dirName, stat.monthDown[i]); // Classic s2.WriteInt(dirName, stat.monthDown[i]); // New } @@ -1079,12 +1117,12 @@ return 0; int FILES_STORE::AddAdmin(const std::string & login) const { std::string fileName; -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); +strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str()); if (Touch(fileName)) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot create file " + fileName; + std::lock_guard 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; } @@ -1095,23 +1133,23 @@ return 0; int FILES_STORE::DelAdmin(const std::string & login) const { std::string fileName; -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); +strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str()); if (unlink(fileName.c_str())) { - STG_LOCKER lock(&mutex); - errorStr = "unlink failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + std::lock_guard 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 { std::string fileName; -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str()); +strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), ac.login.c_str()); { CONFIGFILE cf(fileName, true); @@ -1120,8 +1158,8 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login if (e) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot write admin " + ac.login + ". " + fileName; + std::lock_guard 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; } @@ -1162,10 +1200,10 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const +int FILES_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const { std::string fileName; -strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); +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]; @@ -1176,16 +1214,16 @@ std::string p; if (cf.Error()) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot open " + fileName; + std::lock_guard 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; } if (cf.ReadString("password", &p, "*")) { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter password"; + std::lock_guard 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; } @@ -1218,8 +1256,8 @@ if (cf.ReadUShortInt("ChgConf", &a, 0) == 0) ac->priv.userConf = a; else { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter ChgConf"; + std::lock_guard 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; } @@ -1228,8 +1266,8 @@ if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0) ac->priv.userPasswd = a; else { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter ChgPassword"; + std::lock_guard 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; } @@ -1238,8 +1276,8 @@ if (cf.ReadUShortInt("ChgStat", &a, 0) == 0) ac->priv.userStat = a; else { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter ChgStat"; + std::lock_guard 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; } @@ -1248,8 +1286,8 @@ if (cf.ReadUShortInt("ChgCash", &a, 0) == 0) ac->priv.userCash = a; else { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter ChgCash"; + std::lock_guard 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; } @@ -1258,8 +1296,8 @@ if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0) ac->priv.userAddDel = a; else { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter UsrAddDel"; + std::lock_guard 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; } @@ -1268,8 +1306,8 @@ if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0) ac->priv.adminChg = a; else { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter ChgAdmin"; + std::lock_guard 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; } @@ -1278,8 +1316,8 @@ if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0) ac->priv.tariffChg = a; else { - STG_LOCKER lock(&mutex); - errorStr = "Error in parameter ChgTariff"; + std::lock_guard 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; } @@ -1300,11 +1338,11 @@ return 0; int FILES_STORE::AddTariff(const std::string & name) const { std::string fileName; -strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str()); +strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str()); if (Touch(fileName)) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot create file " + fileName; + std::lock_guard 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; } @@ -1314,29 +1352,29 @@ return 0; int FILES_STORE::DelTariff(const std::string & name) const { std::string fileName; -strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str()); +strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str()); if (unlink(fileName.c_str())) { - STG_LOCKER lock(&mutex); - errorStr = "unlink failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + std::lock_guard 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 std::string & tariffName) const +int FILES_STORE::RestoreTariff(STG::TariffData * td, const std::string & tariffName) const { -std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; +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); - errorStr = "Cannot read file " + fileName; + std::lock_guard 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; } @@ -1347,8 +1385,8 @@ for (int i = 0; idirPrice[i].priceDayA, 0.0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + std::lock_guard 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; } @@ -1372,8 +1410,8 @@ for (int i = 0; idirPrice[i].priceDayB, 0.0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + std::lock_guard 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; } @@ -1382,8 +1420,8 @@ for (int i = 0; idirPrice[i].priceNightA, 0.0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + std::lock_guard 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; } @@ -1392,8 +1430,8 @@ for (int i = 0; idirPrice[i].priceNightB, 0.0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + std::lock_guard 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; } @@ -1402,8 +1440,8 @@ for (int i = 0; idirPrice[i].threshold, 0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + std::lock_guard 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; } @@ -1411,8 +1449,8 @@ for (int i = 0; idirPrice[i].singlePrice, 0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + std::lock_guard 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; } @@ -1420,8 +1458,8 @@ for (int i = 0; idirPrice[i].noDiscount, 0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + std::lock_guard 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; } @@ -1429,53 +1467,55 @@ for (int i = 0; itariffConf.fee, 0) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee"; + std::lock_guard 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); - errorStr = "Cannot read tariff " + tariffName + ". Parameter Free"; + std::lock_guard 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); - errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost"; + std::lock_guard 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); - errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType"; + std::lock_guard 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; } -td->tariffConf.traffType = TARIFF::StringToTraffType(str); +td->tariffConf.traffType = STG::Tariff::parseTraffType(str); if (conf.ReadString("Period", &str, "month") < 0) - td->tariffConf.period = TARIFF::MONTH; + td->tariffConf.period = STG::Tariff::MONTH; else - td->tariffConf.period = TARIFF::StringToPeriod(str); + td->tariffConf.period = STG::Tariff::parsePeriod(str); if (conf.ReadString("ChangePolicy", &str, "allow") < 0) - td->tariffConf.changePolicy = TARIFF::ALLOW; + td->tariffConf.changePolicy = STG::Tariff::ALLOW; else - td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str); + 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 std::string & tariffName) const +int FILES_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const { -std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; +std::string fileName = m_storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; { CONFIGFILE cf(fileName, true); @@ -1484,8 +1524,8 @@ std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; if (e) { - STG_LOCKER lock(&mutex); - errorStr = "Error writing tariff " + tariffName; + std::lock_guard 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; } @@ -1529,14 +1569,126 @@ std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost); cf.WriteDouble("Fee", td.tariffConf.fee); cf.WriteDouble("Free", td.tariffConf.free); - cf.WriteString("TraffType", TARIFF::TraffTypeToString(td.tariffConf.traffType)); - cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period)); + 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)) + { + std::lock_guard 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; + } + +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())) + { + std::lock_guard 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(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), conf.name.c_str()); + + { + CONFIGFILE cf(fileName, true); + + int e = cf.Error(); + + if (e) + { + std::lock_guard 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; + } + + cf.WriteString("name", conf.name); + cf.WriteString("comment", conf.comment); + cf.WriteDouble("cost", conf.cost); + cf.WriteInt("pay_day", conf.payDay); + } + +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); + +if (cf.Error()) + { + std::lock_guard lock(m_mutex); + m_errorStr = "Cannot open " + fileName; + printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str()); + return -1; + } + +if (cf.ReadString("name", &conf->name, name)) + { + std::lock_guard 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; + } + +if (cf.ReadString("comment", &conf->comment, "")) + { + std::lock_guard 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; + } + +if (cf.ReadDouble("cost", &conf->cost, 0.0)) + { + std::lock_guard 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; + } + +unsigned short value = 0; +if (cf.ReadUShortInt("pay_day", &value, 0)) + { + std::lock_guard 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 std::map & statTree, +int FILES_STORE::WriteDetailedStat(const STG::TraffStat & statTree, time_t lastStat, const std::string & login) const { @@ -1548,24 +1700,24 @@ 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); - errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; + std::lock_guard 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); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1578,7 +1730,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); @@ -1586,24 +1738,24 @@ if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&mutex); - errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; + std::lock_guard 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); + std::lock_guard 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" : "", @@ -1612,19 +1764,19 @@ if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&mutex); - errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; + std::lock_guard 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); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1634,8 +1786,8 @@ statFile = fopen (fn, "at"); if (!statFile) { - STG_LOCKER lock(&mutex); - errorStr = "File \'" + std::string(fn) + "\' cannot be written."; + std::lock_guard 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; } @@ -1661,21 +1813,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); - errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'"; + std::lock_guard 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; } -std::map::const_iterator stIter; -stIter = statTree.begin(); +auto stIter = statTree.begin(); while (stIter != statTree.end()) { - std::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(), @@ -1685,10 +1835,10 @@ while (stIter != statTree.end()) u.c_str(), stIter->second.cash) < 0) { - STG_LOCKER lock(&mutex); - errorStr = "fprint failed. Message: '"; - errorStr += strerror(errno); - errorStr += "'"; + std::lock_guard 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; @@ -1701,10 +1851,10 @@ while (stIter != statTree.end()) u.c_str(), stIter->second.cash) < 0) { - STG_LOCKER lock(&mutex); - errorStr = std::string("fprint failed. Message: '"); - errorStr += strerror(errno); - errorStr += "'"; + std::lock_guard 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; @@ -1716,51 +1866,51 @@ 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); + std::lock_guard 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 std::string & login) const +int FILES_STORE::AddMessage(STG::Message * msg, const std::string & login) const { std::string fn; std::string dn; struct timeval tv; -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); - errorStr = "Directory \'"; - errorStr += dn; - errorStr += "\' cannot be created."; + std::lock_guard 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.GetConfModeDir()); +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); if (Touch(fn)) { - STG_LOCKER lock(&mutex); - errorStr = "File \'"; - errorStr += fn; - errorStr += "\' cannot be writen."; + std::lock_guard 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; } @@ -1768,22 +1918,20 @@ if (Touch(fn)) return EditMessage(*msg, login); } //----------------------------------------------------------------------------- -int FILES_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const +int FILES_STORE::EditMessage(const STG::Message & msg, const std::string & login) const { std::string fileName; FILE * msgFile; -strprintf(&fileName, "%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(fileName.c_str(), F_OK) != 0) { - std::string idstr; - x2str(msg.header.id, idstr); - STG_LOCKER lock(&mutex); - errorStr = "Message for user \'"; - errorStr += login + "\' with ID \'"; - errorStr += idstr + "\' does not exist."; - printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str()); + std::lock_guard 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; } @@ -1792,8 +1940,8 @@ Touch(fileName + ".new"); msgFile = fopen((fileName + ".new").c_str(), "wt"); if (!msgFile) { - STG_LOCKER lock(&mutex); - errorStr = "File \'" + fileName + "\' cannot be writen."; + std::lock_guard lock(m_mutex); + m_errorStr = "File \'" + fileName + "\' cannot be writen."; printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -1809,8 +1957,8 @@ res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0); if (!res) { - STG_LOCKER lock(&mutex); - errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'"; + std::lock_guard 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; @@ -1818,12 +1966,12 @@ if (!res) fclose(msgFile); -chmod((fileName + ".new").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(&mutex); - errorStr = "Error moving dir from " + fileName + ".new to " + fileName; + std::lock_guard 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; } @@ -1831,10 +1979,10 @@ if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const +int FILES_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const { std::string fn; -strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id); +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); } @@ -1842,14 +1990,14 @@ return ReadMessage(fn, &msg->header, &msg->text); int FILES_STORE::DelMessage(uint64_t id, const std::string & login) const { std::string fn; -strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id); +strprintf(&fn, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str(), login.c_str(), id); return unlink(fn.c_str()); } //----------------------------------------------------------------------------- -int FILES_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const +int FILES_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const { -std::string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/"); +std::string dn(m_storeSettings.GetUsersDir() + "/" + login + "/messages/"); if (access(dn.c_str(), F_OK) != 0) { @@ -1867,15 +2015,15 @@ for (unsigned i = 0; i < messages.size(); i++) { if (unlink((dn + messages[i]).c_str())) { - STG_LOCKER lock(&mutex); - errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; + std::lock_guard 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; @@ -1885,8 +2033,8 @@ for (unsigned i = 0; i < messages.size(); i++) { if (unlink((dn + messages[i]).c_str())) { - STG_LOCKER lock(&mutex); - errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; + std::lock_guard 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; } @@ -1900,17 +2048,17 @@ return 0; } //----------------------------------------------------------------------------- int FILES_STORE::ReadMessage(const std::string & fileName, - STG_MSG_HDR * hdr, + STG::Message::Header * hdr, std::string * text) const { FILE * msgFile; msgFile = fopen(fileName.c_str(), "rt"); if (!msgFile) { - STG_LOCKER lock(&mutex); - errorStr = "File \'"; - errorStr += fileName; - errorStr += "\' cannot be openned."; + std::lock_guard 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; } @@ -1920,7 +2068,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)); @@ -1928,10 +2076,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); - errorStr = "Cannot read file \'"; - errorStr += fileName; - errorStr += "\'. Missing data."; + std::lock_guard 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); @@ -1946,10 +2094,10 @@ for (int pos = 0; pos < 6; pos++) if (feof(msgFile)) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read file \'"; - errorStr += fileName; - errorStr += "\'. Missing data."; + std::lock_guard 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); @@ -1958,12 +2106,12 @@ for (int pos = 0; pos < 6; pos++) if (str2x(p, *(d[pos]))) { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read file \'"; - errorStr += fileName; - errorStr += "\'. Incorrect value. \'"; - errorStr += p; - errorStr += "\'"; + std::lock_guard 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;