X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/fa619edb462a17b813e462637dd65d4d7c98471b..d1d9452a687184ac6b992fadc77e89c8a308e388:/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 c94a6036..bf5d389a 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -44,114 +43,67 @@ #include #include -#include "common.h" -#include "user_ips.h" -#include "user_conf.h" -#include "user_stat.h" -#include "stg_const.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/plugin_creator.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 -{ -public: - - //------------------------------------------------------------------------- - BAK_FILE(const string & fileName, bool removeBak) - : f(NULL), - removeBak(false) - { - bakSuccessed = false; - BAK_FILE::removeBak = removeBak; - fileNameBak = fileName + ".bak"; - 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 +//----------------------------------------------------------------------------- +namespace { -private: - FILES_STORE * fs; - -public: - FILES_STORE_CREATOR() - : fs(new FILES_STORE()) - { - }; - ~FILES_STORE_CREATOR() - { - delete fs; - }; +PLUGIN_CREATOR fsc; +} - FILES_STORE * GetStore() - { - return fs; - }; -}; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- +extern "C" STORE * GetStore(); //----------------------------------------------------------------------------- -FILES_STORE_CREATOR fsc; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -BASE_STORE * GetStore() +STORE * GetStore() { -return fsc.GetStore(); +return fsc.GetPlugin(); } //----------------------------------------------------------------------------- FILES_STORE_SETTINGS::FILES_STORE_SETTINGS() : settings(NULL), + errorStr(), + workDir(), + usersDir(), + adminsDir(), + tariffsDir(), + statMode(0), + statUID(0), + statGID(0), + confMode(0), + confUID(0), + confGID(0), + userLogMode(0), + userLogUID(0), + userLogGID(0), removeBak(true), readBak(true) { } //----------------------------------------------------------------------------- -FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS() -{ -} -//----------------------------------------------------------------------------- -int FILES_STORE_SETTINGS::ParseOwner(const vector & moduleParams, const string & owner, uid_t * uid) +int FILES_STORE_SETTINGS::ParseOwner(const std::vector & moduleParams, const std::string & owner, uid_t * uid) { PARAM_VALUE pv; pv.param = owner; -vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); if (pvi == moduleParams.end()) { @@ -168,11 +120,11 @@ if (User2UID(pvi->value[0].c_str(), uid) < 0) 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; pv.param = group; -vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); if (pvi == moduleParams.end()) { @@ -189,7 +141,7 @@ if (Group2GID(pvi->value[0].c_str(), gid) < 0) 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")) { @@ -206,11 +158,11 @@ 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; pv.param = modeStr; -vector::const_iterator pvi; +std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); if (pvi == moduleParams.end()) { @@ -250,7 +202,7 @@ if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0) if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0) return -1; -vector::const_iterator pvi; +std::vector::const_iterator pvi; PARAM_VALUE pv; pv.param = "RemoveBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); @@ -303,7 +255,7 @@ adminsDir = workDir + "/admins/"; return 0; } //----------------------------------------------------------------------------- -const string & FILES_STORE_SETTINGS::GetStrError() const +const std::string & FILES_STORE_SETTINGS::GetStrError() const { return errorStr; } @@ -314,7 +266,7 @@ struct passwd * pw; pw = getpwnam(user); if (!pw) { - errorStr = string("User \'") + string(user) + string("\' not found in system."); + errorStr = std::string("User \'") + std::string(user) + std::string("\' not found in system."); printfd(__FILE__, "%s\n", errorStr.c_str()); return -1; } @@ -329,7 +281,7 @@ struct group * grp; grp = getgrnam(gr); if (!grp) { - errorStr = string("Group \'") + string(gr) + string("\' not found in system."); + errorStr = std::string("Group \'") + std::string(gr) + std::string("\' not found in system."); printfd(__FILE__, "%s\n", errorStr.c_str()); return -1; } @@ -345,7 +297,7 @@ char b; char c; if (strlen(str) > 3) { - errorStr = string("Error parsing mode \'") + str + string("\'"); + errorStr = std::string("Error parsing mode \'") + str + std::string("\'"); printfd(__FILE__, "%s\n", errorStr.c_str()); return -1; } @@ -353,7 +305,7 @@ if (strlen(str) > 3) for (int i = 0; i < 3; i++) if (str[i] > '7' || str[i] < '0') { - errorStr = string("Error parsing mode \'") + str + string("\'"); + errorStr = std::string("Error parsing mode \'") + str + std::string("\'"); printfd(__FILE__, "%s\n", errorStr.c_str()); return -1; } @@ -367,31 +319,6 @@ c = str[2] - '0'; 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; @@ -401,21 +328,6 @@ if (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; @@ -425,61 +337,20 @@ if (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; -} -//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- FILES_STORE::FILES_STORE() + : errorStr(), + version("file_store v.1.04"), + storeSettings(), + settings(), + mutex(), + logger(GetPluginLogger(GetStgLogger(), "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; } //----------------------------------------------------------------------------- int FILES_STORE::ParseSettings() @@ -493,132 +364,136 @@ if (ret) return ret; } //----------------------------------------------------------------------------- -const string & FILES_STORE::GetStrError() const +int FILES_STORE::GetUsersList(std::vector * userList) const { +std::vector files; + +if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, "")) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + std::string(strerror(errno)); + return -1; + } + STG_LOCKER lock(&mutex, __FILE__, __LINE__); -return errorStr; -} -//----------------------------------------------------------------------------- -const string & FILES_STORE::GetVersion() const -{ -return version; + +userList->swap(files); + +return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetFilesList(vector * filesList, const string & directory, mode_t mode, const string & ext) const +int FILES_STORE::GetAdminsList(std::vector * adminList) 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, storeSettings.GetAdminsDir(), S_IFREG, ".adm")) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + directory + "\' cannot be opened."; - //printfd(__FILE__, "%s\n", errorStr.c_str()); + errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + 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(&mutex, __FILE__, __LINE__); + +adminList->swap(files); -closedir(d); return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetUsersList(vector * usersList) const -{ -return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, ""); -} -//----------------------------------------------------------------------------- -int FILES_STORE::GetAdminsList(vector * adminsList) const +int FILES_STORE::GetTariffsList(std::vector * tariffList) const { -return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm"); -} -//----------------------------------------------------------------------------- -int FILES_STORE::GetTariffsList(vector * tariffsList) const -{ -return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf"); +std::vector files; + +if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf")) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&mutex, __FILE__, __LINE__); + +tariffList->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) + { + errorStr = "failed to open dir. Message: '"; + errorStr += strerror(errno); + 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(&mutex, __FILE__, __LINE__); + errorStr = "unlink failed. Message: '"; + errorStr += strerror(errno); + 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)); + 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 { -string fileName; +std::string fileName; strprintf(&fileName, "%s%s", 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) + "'"; + errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -631,27 +506,6 @@ if (Touch(fileName)) printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } -/*f = fopen(fileName.c_str(), "wt"); -if (f) - { - 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 + "\'"; - 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()); if (Touch(fileName)) @@ -661,36 +515,15 @@ if (Touch(fileName)) printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } -/*f = fopen(fileName.c_str(), "wt"); -if (f) - { - 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 + "\'"; - 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", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR); if (access(dirName.c_str(), F_OK) != 0) { if (mkdir(dirName.c_str(), 0700) != 0) @@ -704,7 +537,7 @@ 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(&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()); if (rename(dirName1.c_str(), dirName.c_str())) { @@ -725,9 +558,9 @@ else return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const +int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const { -string fileName; +std::string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; if (RestoreUserConf(conf, login, fileName)) { @@ -740,11 +573,10 @@ 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(USER_CONF * conf, const std::string & login, const std::string & fileName) const { CONFIGFILE cf(fileName); int e = cf.Error(); -string str; if (e) { @@ -784,21 +616,21 @@ if (conf->tariffName.empty()) return -1; } -string ipStr; +std::string ipStr; cf.ReadString("IP", &ipStr, "?"); -USER_IPS i; +USER_IPS ips; try { - i = StrToIPS(ipStr); + ips = StrToIPS(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; printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str()); return -1; } -conf->ips = i; +conf->ips = ips; if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0) { @@ -852,9 +684,9 @@ 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(USER_STAT * stat, const std::string & login) const { -string fileName; +std::string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; if (RestoreUserStat(stat, login, fileName)) @@ -868,7 +700,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(USER_STAT * stat, const std::string & login, const std::string & fileName) const { CONFIGFILE cf(fileName); @@ -891,21 +723,21 @@ for (int i = 0; i < DIR_NUM; i++) if (cf.ReadULongLongInt(s, &traff, 0) != 0) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s); + 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); + 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) @@ -959,13 +791,11 @@ 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 USER_CONF & conf, const std::string & login) const { -string fileName; +std::string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; -//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); - CONFIGFILE cfstat(fileName, true); int e = cfstat.Error(); @@ -973,7 +803,7 @@ int e = cfstat.Error(); if (e) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("User \'") + login + "\' conf not written\n"; + 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; } @@ -1010,21 +840,18 @@ 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 USER_STAT & stat, const std::string & login) const { -char s[22]; -string fileName; +std::string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; -//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); - { CONFIGFILE cfstat(fileName, true); int e = cfstat.Error(); @@ -1032,17 +859,18 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; if (e) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("User \'") + login + "\' stat not written\n"; + 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; } for (int i = 0; i < DIR_NUM; i++) { + char s[22]; snprintf(s, 22, "D%d", i); - cfstat.WriteInt(s, stat.down[i]); + cfstat.WriteInt(s, stat.monthDown[i]); snprintf(s, 22, "U%d", i); - cfstat.WriteInt(s, stat.up[i]); + cfstat.WriteInt(s, stat.monthUp[i]); } cfstat.WriteDouble("Cash", stat.cash); @@ -1053,7 +881,7 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; cfstat.WriteInt("LastActivityTime", stat.lastActivityTime); } -e = chmod(fileName.c_str(), storeSettings.GetStatMode()); +int e = chmod(fileName.c_str(), storeSettings.GetStatMode()); e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID()); if (e) @@ -1065,11 +893,11 @@ if (e) 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; +std::string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/log"; f = fopen(fileName.c_str(), "at"); @@ -1101,11 +929,11 @@ if (e) 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; +std::string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/log2"; f = fopen(fileName.c_str(), "at"); @@ -1137,48 +965,48 @@ if (e) 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, +int FILES_STORE::WriteUserDisconnect(const std::string & login, + const DIR_TRAFF & monthUp, + const DIR_TRAFF & monthDown, const DIR_TRAFF & sessionUp, const DIR_TRAFF & sessionDown, double cash, double freeMb, const std::string & reason) const { -stringstream logStr; +std::ostringstream logStr; logStr << "Disconnect, " << " session upload: \'" << sessionUp << "\' session download: \'" << sessionDown << "\' month upload: \'" - << up + << monthUp << "\' month download: \'" - << down + << monthDown << "\' cash: \'" << cash << "\'"; @@ -1196,43 +1024,66 @@ 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 USER_STAT & stat, int month, int year, const std::string & login) const { -string str; -int e; - -strprintf(&str,"%s/%s/stat.%d.%02d", +// Classic stats +std::string stat1; +strprintf(&stat1,"%s/%s/stat.%d.%02d", storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); -CONFIGFILE s(str, true); -e = s.Error(); +CONFIGFILE s(stat1, true); -if (e) +if (s.Error()) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot create file " + str; + errorStr = "Cannot create file '" + stat1 + "'"; printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); return -1; } -char dirName[3]; +// New stats +std::string stat2; +strprintf(&stat2,"%s/%s/stat2.%d.%02d", + storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); + +CONFIGFILE s2(stat2, true); -for (int i = 0; i < DIR_NUM; i++) +if (s2.Error()) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Cannot create file '" + stat2 + "'"; + printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); + return -1; + } + +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", (unsigned long long)i); + s.WriteInt(dirName, stat.monthUp[i]); // Classic + s2.WriteInt(dirName, stat.monthUp[i]); // New + snprintf(dirName, 3, "D%llu", (unsigned long long)i); + s.WriteInt(dirName, stat.monthDown[i]); // Classic + s2.WriteInt(dirName, stat.monthDown[i]); // New } +// Classic s.WriteDouble("cash", stat.cash); +// 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; +std::string fileName; strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); if (Touch(fileName)) @@ -1246,9 +1097,9 @@ if (Touch(fileName)) return 0; } //-----------------------------------------------------------------------------*/ -int FILES_STORE::DelAdmin(const string & login) const +int FILES_STORE::DelAdmin(const std::string & login) const { -string fileName; +std::string fileName; strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); if (unlink(fileName.c_str())) { @@ -1263,11 +1114,7 @@ return 0; //-----------------------------------------------------------------------------*/ int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const { -char passwordE[2 * ADM_PASSWD_LEN + 2]; -char pass[ADM_PASSWD_LEN + 1]; -char adminPass[ADM_PASSWD_LEN + 1]; - -string fileName; +std::string fileName; strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str()); @@ -1284,7 +1131,10 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login return -1; } + char pass[ADM_PASSWD_LEN + 1]; memset(pass, 0, sizeof(pass)); + + char adminPass[ADM_PASSWD_LEN + 1]; memset(adminPass, 0, sizeof(adminPass)); BLOWFISH_CTX ctx; @@ -1299,6 +1149,7 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login } pass[ADM_PASSWD_LEN - 1] = 0; + char passwordE[2 * ADM_PASSWD_LEN + 2]; Encode12(passwordE, pass, ADM_PASSWD_LEN); cf.WriteString("password", passwordE); @@ -1309,14 +1160,16 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login 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(ADMIN_CONF * ac, const std::string & login) const { -string fileName; +std::string fileName; strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); CONFIGFILE cf(fileName); char pass[ADM_PASSWD_LEN + 1]; @@ -1324,7 +1177,7 @@ char password[ADM_PASSWD_LEN + 1]; char passwordE[2 * ADM_PASSWD_LEN + 2]; BLOWFISH_CTX ctx; -string p; +std::string p; if (cf.Error()) { @@ -1334,8 +1187,6 @@ if (cf.Error()) return -1; } -int a; - if (cf.ReadString("password", &p, "*")) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1347,8 +1198,6 @@ 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) @@ -1368,7 +1217,9 @@ 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 { @@ -1378,7 +1229,7 @@ else return -1; } -if (cf.ReadInt("ChgPassword", &a, 0) == 0) +if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0) ac->priv.userPasswd = a; else { @@ -1388,7 +1239,7 @@ else return -1; } -if (cf.ReadInt("ChgStat", &a, 0) == 0) +if (cf.ReadUShortInt("ChgStat", &a, 0) == 0) ac->priv.userStat = a; else { @@ -1398,7 +1249,7 @@ else return -1; } -if (cf.ReadInt("ChgCash", &a, 0) == 0) +if (cf.ReadUShortInt("ChgCash", &a, 0) == 0) ac->priv.userCash = a; else { @@ -1408,7 +1259,7 @@ else return -1; } -if (cf.ReadInt("UsrAddDel", &a, 0) == 0) +if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0) ac->priv.userAddDel = a; else { @@ -1418,7 +1269,7 @@ else return -1; } -if (cf.ReadInt("ChgAdmin", &a, 0) == 0) +if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0) ac->priv.adminChg = a; else { @@ -1428,7 +1279,7 @@ else return -1; } -if (cf.ReadInt("ChgTariff", &a, 0) == 0) +if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0) ac->priv.tariffChg = a; else { @@ -1438,12 +1289,22 @@ else 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; +std::string fileName; strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str()); if (Touch(fileName)) { @@ -1455,9 +1316,9 @@ if (Touch(fileName)) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::DelTariff(const string & name) const +int FILES_STORE::DelTariff(const std::string & name) const { -string fileName; +std::string fileName; strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str()); if (unlink(fileName.c_str())) { @@ -1470,11 +1331,11 @@ if (unlink(fileName.c_str())) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const +int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const { -string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; +std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; CONFIGFILE conf(fileName); -string str; +std::string str; td->tariffConf.name = tariffName; if (conf.Error() != 0) @@ -1485,7 +1346,7 @@ if (conf.Error() != 0) return -1; } -string param; +std::string param; for (int i = 0; itariffConf.period = TARIFF::MONTH; +else + td->tariffConf.period = TARIFF::StringToPeriod(str); return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const +int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const { -string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; +std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; { CONFIGFILE cf(fileName, true); @@ -1641,7 +1507,7 @@ string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; return e; } - string param; + std::string param; for (int i = 0; i < DIR_NUM; i++) { strprintf(¶m, "PriceDayA%d", i); @@ -1659,7 +1525,7 @@ string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; strprintf(¶m, "Threshold%d", i); cf.WriteInt(param, td.dirPrice[i].threshold); - string s; + std::string s; strprintf(¶m, "Time%d", i); strprintf(&s, "%0d:%0d-%0d:%0d", @@ -1696,14 +1562,16 @@ string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; cf.WriteString("TraffType", "max"); break; } + + cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period)); } return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::WriteDetailedStat(const map & statTree, +int FILES_STORE::WriteDetailedStat(const std::map & statTree, time_t lastStat, - const string & login) const + const std::string & login) const { char fn[FN_STR_LEN]; char dn[FN_STR_LEN]; @@ -1719,7 +1587,7 @@ if (access(dn, F_OK) != 0) if (mkdir(dn, 0700) != 0) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + string(dn) + "\' cannot be created."; + errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -1752,7 +1620,7 @@ if (access(dn, F_OK) != 0) if (mkdir(dn, 0700) != 0) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + string(dn) + "\' cannot be created."; + errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -1778,7 +1646,7 @@ if (access(dn, F_OK) != 0) if (mkdir(dn, 0700) != 0) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + string(dn) + "\' cannot be created."; + errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -1800,7 +1668,7 @@ statFile = fopen (fn, "at"); if (!statFile) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "File \'" + string(fn) + "\' cannot be written."; + errorStr = "File \'" + std::string(fn) + "\' cannot be written."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -1827,18 +1695,18 @@ 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) + "'"; + 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; +std::map::const_iterator stIter; stIter = statTree.begin(); while (stIter != statTree.end()) { - string u, d; + std::string u, d; x2str(stIter->second.up, u); x2str(stIter->second.down, d); #ifdef TRAFF_STAT_WITH_PORTS @@ -1867,7 +1735,7 @@ while (stIter != statTree.end()) stIter->second.cash) < 0) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("fprint failed. Message: '"); + errorStr = std::string("fprint failed. Message: '"); errorStr += strerror(errno); errorStr += "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); @@ -1893,14 +1761,10 @@ if (e) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const +int FILES_STORE::AddMessage(STG_MSG * msg, const std::string & login) const { -//ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ. -//úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ -//ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ. - -string fn; -string dn; +std::string fn; +std::string dn; struct timeval tv; strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str()); @@ -1937,20 +1801,16 @@ if (Touch(fn)) return EditMessage(*msg, login); } //----------------------------------------------------------------------------- -int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const +int FILES_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const { -//ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ. -//úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ -//ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ. - -string fileName; +std::string fileName; FILE * msgFile; strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id); if (access(fileName.c_str(), F_OK) != 0) { - string idstr; + std::string idstr; x2str(msg.header.id, idstr); STG_LOCKER lock(&mutex, __FILE__, __LINE__); errorStr = "Message for user \'"; @@ -1972,7 +1832,7 @@ if (!msgFile) } 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); @@ -1983,8 +1843,9 @@ res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0); if (!res) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'"; + errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(msgFile); return -1; } @@ -1996,37 +1857,40 @@ if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); errorStr = "Error moving dir from " + fileName + ".new to " + fileName; - printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno)); + 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_MSG * msg, const std::string & login) const { -string fn; +std::string fn; strprintf(&fn, "%s/%s/messages/%lld", 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; +std::string fn; strprintf(&fn, "%s/%s/messages/%lld", 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(storeSettings.GetUsersDir() + "/" + login + "/messages/"); + +if (access(dn.c_str(), F_OK) != 0) + { + return 0; + } -//hdrsList->resize(messages.size()); +std::vector messages; +GetFileList(&messages, dn, S_IFREG, ""); for (unsigned i = 0; i < messages.size(); i++) { @@ -2037,7 +1901,7 @@ 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) + "'"; + errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -2055,7 +1919,7 @@ 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) + "'"; + errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -2068,9 +1932,9 @@ for (unsigned i = 0; i < messages.size(); i++) return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::ReadMessage(const string & fileName, +int FILES_STORE::ReadMessage(const std::string & fileName, STG_MSG_HDR * hdr, - string * text) const + std::string * text) const { FILE * msgFile; msgFile = fopen(fileName.c_str(), "rt"); @@ -2169,3 +2033,52 @@ if (f) 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; +} +//-----------------------------------------------------------------------------