X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/1bf5ae378079e212f6918d09d87dec5420a0d6ec..2dd96d1468a065ebee3e37b1defcf2c438d72e2f:/projects/stargazer/plugins/store/files/file_store.cpp diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index 556bf8cf..5fadbd4d 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,15 +43,15 @@ #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 "file_store.h" -#include "blowfish.h" -#include "stg_logger.h" -#include "stg_locker.h" #define DELETED_USERS_DIR "deleted_users" @@ -60,6 +59,8 @@ using namespace std; +int GetFileList(vector * fileList, const string & directory, mode_t mode, const string & ext); + const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- class BAK_FILE @@ -74,29 +75,6 @@ public: bakSuccessed = false; BAK_FILE::removeBak = removeBak; fileNameBak = fileName + ".bak"; - /*struct stat fileStat; - if (stat(fileName.c_str(), &fileStat) == 0) - { - char * buff = new char[fileStat.st_size]; - f = fopen(fileName.c_str(), "rb"); - if(f) - { - fread(buff, 1, fileStat.st_size, f); - fclose(f); - - fileNameBak = fileName + ".bak"; - f = fopen(fileNameBak.c_str(), "wb"); - if(f) - { - fwrite(buff, 1, fileStat.st_size, f); - fclose(f); - } - } - - delete[] buff; - - bakSuccessed = true; - }*/ if (rename(fileName.c_str(), fileNameBak.c_str())) { printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno)); @@ -154,7 +132,7 @@ FILES_STORE_CREATOR fsc; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -BASE_STORE * GetStore() +STORE * GetStore() { return fsc.GetStore(); } @@ -442,9 +420,9 @@ return confMode; mode_t FILES_STORE_SETTINGS::GetConfModeDir() const { mode_t mode = confMode; -if (statMode & S_IRUSR) mode |= S_IXUSR; -if (statMode & S_IRGRP) mode |= S_IXGRP; -if (statMode & S_IROTH) mode |= S_IXOTH; +if (confMode & S_IRUSR) mode |= S_IXUSR; +if (confMode & S_IRGRP) mode |= S_IXGRP; +if (confMode & S_IROTH) mode |= S_IXOTH; return mode; } //----------------------------------------------------------------------------- @@ -485,13 +463,6 @@ return readBak; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -/*BASE_SETTINGS * FILES_STORE::GetStoreSettings() -{ -return &storeSettings; -}*/ -//----------------------------------------------------------------------------- FILES_STORE::FILES_STORE() { version = "file_store v.1.04"; @@ -534,105 +505,118 @@ const string & FILES_STORE::GetVersion() const return version; } //----------------------------------------------------------------------------- -int FILES_STORE::GetFilesList(vector * filesList, const string & directory, mode_t mode, const string & ext) const +int FILES_STORE::GetUsersList(vector * userList) const { -// æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ -DIR * d; -string str; -struct stat st; -dirent * dir; - -filesList->clear(); - -d = opendir(directory.c_str()); +vector files; -if (!d) +if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, "")) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Directory \'" + directory + "\' cannot be opened."; - //printfd(__FILE__, "%s\n", errorStr.c_str()); + errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + 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__); + +userList->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::GetAdminsList(vector * adminList) const { -return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm"); +vector files; + +if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm")) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&mutex, __FILE__, __LINE__); + +adminList->swap(files); + +return 0; } //----------------------------------------------------------------------------- -int FILES_STORE::GetTariffsList(vector * tariffsList) const +int FILES_STORE::GetTariffsList(vector * tariffList) const { -return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf"); +vector files; + +if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf")) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + 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; + + string str = path; + str += "/" + 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)); + return -1; + } } - } -GetFilesList(&filesList, path, S_IFDIR, ""); + if (!(st.st_mode & S_IFDIR)) + { + if (RemoveDir(str.c_str())) + { + 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; } @@ -641,7 +625,6 @@ return 0; //----------------------------------------------------------------------------- int FILES_STORE::AddUser(const string & login) const { -FILE * f; string fileName; strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str()); @@ -655,7 +638,14 @@ if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == } strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str()); -f = fopen(fileName.c_str(), "wt"); +if (Touch(fileName)) + { + 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; + } +/*f = fopen(fileName.c_str(), "wt"); if (f) { if (fprintf(f, "\n") < 0) @@ -675,10 +665,17 @@ else errorStr = "Cannot create file \"" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; - } + }*/ strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str()); -f = fopen(fileName.c_str(), "wt"); +if (Touch(fileName)) + { + 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; + } +/*f = fopen(fileName.c_str(), "wt"); if (f) { if (fprintf(f, "\n") < 0) @@ -698,7 +695,7 @@ else errorStr = "Cannot create file \"" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; - } + }*/ return 0; } //----------------------------------------------------------------------------- @@ -808,7 +805,7 @@ try { i = StrToIPS(ipStr); } -catch (string s) +catch (const string & s) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s; @@ -981,17 +978,9 @@ int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) cons string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; -BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); +//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); -if (access(fileName.c_str(), W_OK) != 0) - { - FILE * f; - f = fopen(fileName.c_str(), "wb"); - if (f) - fclose(f); - } - -CONFIGFILE cfstat(fileName); +CONFIGFILE cfstat(fileName, true); int e = cfstat.Error(); @@ -1048,43 +1037,37 @@ char s[22]; string fileName; fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; -BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); +//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); -if (access(fileName.c_str(), W_OK) != 0) { - FILE * f; - f = fopen(fileName.c_str(), "wb"); - if (f) - fclose(f); - } + CONFIGFILE cfstat(fileName, true); + int e = cfstat.Error(); -CONFIGFILE cfstat(fileName); -int e = cfstat.Error(); + if (e) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = string("User \'") + login + "\' stat not written\n"; + printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str()); + return -1; + } -if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = string("User \'") + login + "\' stat not written\n"; - printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str()); - return -1; - } + for (int i = 0; i < DIR_NUM; i++) + { + snprintf(s, 22, "D%d", i); + cfstat.WriteInt(s, stat.down[i]); + snprintf(s, 22, "U%d", i); + cfstat.WriteInt(s, stat.up[i]); + } -for (int i = 0; i < DIR_NUM; i++) - { - snprintf(s, 22, "D%d", i); - cfstat.WriteInt(s, stat.down[i]); - snprintf(s, 22, "U%d", i); - cfstat.WriteInt(s, stat.up[i]); + cfstat.WriteDouble("Cash", stat.cash); + cfstat.WriteDouble("FreeMb", stat.freeMb); + cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd); + cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime); + cfstat.WriteInt("PassiveTime", stat.passiveTime); + cfstat.WriteInt("LastActivityTime", stat.lastActivityTime); } -cfstat.WriteDouble("Cash", stat.cash); -cfstat.WriteDouble("FreeMb", stat.freeMb); -cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd); -cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime); -cfstat.WriteInt("PassiveTime", stat.passiveTime); -cfstat.WriteInt("LastActivityTime", stat.lastActivityTime); - -e = chmod(fileName.c_str(), storeSettings.GetStatMode()); +int e = chmod(fileName.c_str(), storeSettings.GetStatMode()); e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID()); if (e) @@ -1201,22 +1184,8 @@ int FILES_STORE::WriteUserDisconnect(const string & login, const std::string & reason) const { stringstream logStr; -logStr << "Disconnect, "; -/*stringstream sssu; -stringstream sssd; -stringstream ssmu; -stringstream ssmd; -stringstream sscash; - -ssmu << up; -ssmd << down; - -sssu << sessionUp; -sssd << sessionDown; - -sscash << cash;*/ - -logStr << " session upload: \'" +logStr << "Disconnect, " + << " session upload: \'" << sessionUp << "\' session download: \'" << sessionDown @@ -1243,44 +1212,57 @@ return WriteLog2String(logStr.str(), login); //----------------------------------------------------------------------------- int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const { -string str; -CONFIGFILE * s; -int e; -FILE *f; - -strprintf(&str,"%s/%s/stat.%d.%02d", +// Classic stats +string stat1; +strprintf(&stat1,"%s/%s/stat.%d.%02d", storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); -if ((f = fopen(str.c_str(), "w"))) +CONFIGFILE s(stat1, true); + +if (s.Error()) { - fprintf(f, "\n"); - fclose(f); + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Cannot create file '" + stat1 + "'"; + printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); + return -1; } -s = new CONFIGFILE(str); -e = s->Error(); +// New stats +string stat2; +strprintf(&stat2,"%s/%s/stat2.%d.%02d", + storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); -if (e) +CONFIGFILE s2(stat2, true); + +if (s2.Error()) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot create file " + str; + errorStr = "Cannot create file '" + stat2 + "'"; printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); return -1; } -char dirName[3]; - -for (int i = 0; i < DIR_NUM; i++) +for (size_t i = 0; i < DIR_NUM; i++) { + char dirName[3]; snprintf(dirName, 3, "U%d", i); - s->WriteInt(dirName, stat.up[i]); + s.WriteInt(dirName, stat.up[i]); // Classic + s2.WriteInt(dirName, stat.up[i]); // New snprintf(dirName, 3, "D%d", i); - s->WriteInt(dirName, stat.down[i]); + s.WriteInt(dirName, stat.down[i]); // Classic + s2.WriteInt(dirName, stat.down[i]); // New } -s->WriteDouble("cash", stat.cash); +// Classic +s.WriteDouble("cash", stat.cash); -delete s; +// New +s2.WriteDouble("Cash", stat.cash); +s2.WriteDouble("FreeMb", stat.freeMb); +s2.WriteDouble("LastCashAdd", stat.lastCashAdd); +s2.WriteInt("LastCashAddTime", stat.lastCashAddTime); +s2.WriteInt("PassiveTime", stat.passiveTime); +s2.WriteInt("LastActivityTime", stat.lastActivityTime); return 0; } @@ -1289,19 +1271,16 @@ int FILES_STORE::AddAdmin(const string & login) const { string fileName; strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str()); -FILE * f; -f = fopen(fileName.c_str(), "wt"); -if (f) + +if (Touch(fileName)) { - fprintf(f, "\n"); - fclose(f); - return 0; + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Cannot create file " + fileName; + printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str()); + return -1; } -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -errorStr = "Cannot create file " + fileName; -printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str()); -return -1; +return 0; } //-----------------------------------------------------------------------------*/ int FILES_STORE::DelAdmin(const string & login) const @@ -1329,44 +1308,45 @@ string fileName; strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str()); -CONFIGFILE cf(fileName); + { + CONFIGFILE cf(fileName, true); -int e = cf.Error(); + int e = cf.Error(); -if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot write admin " + ac.login + ". " + fileName; - printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str()); - return -1; - } + if (e) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Cannot write admin " + ac.login + ". " + fileName; + printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str()); + return -1; + } -memset(pass, 0, sizeof(pass)); -memset(adminPass, 0, sizeof(adminPass)); + memset(pass, 0, sizeof(pass)); + memset(adminPass, 0, sizeof(adminPass)); -BLOWFISH_CTX ctx; -EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx); + BLOWFISH_CTX ctx; + EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx); -strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN); -adminPass[ADM_PASSWD_LEN - 1] = 0; + strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN); + adminPass[ADM_PASSWD_LEN - 1] = 0; -for (int i = 0; i < ADM_PASSWD_LEN/8; i++) - { - EncodeString(pass + 8*i, adminPass + 8*i, &ctx); - } + for (int i = 0; i < ADM_PASSWD_LEN/8; i++) + { + EncodeString(pass + 8*i, adminPass + 8*i, &ctx); + } -pass[ADM_PASSWD_LEN - 1] = 0; -Encode12(passwordE, pass, ADM_PASSWD_LEN); -//printfd(__FILE__, "passwordE %s\n", passwordE); + pass[ADM_PASSWD_LEN - 1] = 0; + Encode12(passwordE, pass, ADM_PASSWD_LEN); -cf.WriteString("password", passwordE); -cf.WriteInt("ChgConf", ac.priv.userConf); -cf.WriteInt("ChgPassword", ac.priv.userPasswd); -cf.WriteInt("ChgStat", ac.priv.userStat); -cf.WriteInt("ChgCash", ac.priv.userCash); -cf.WriteInt("UsrAddDel", ac.priv.userAddDel); -cf.WriteInt("ChgTariff", ac.priv.tariffChg); -cf.WriteInt("ChgAdmin", ac.priv.adminChg); + cf.WriteString("password", passwordE); + cf.WriteInt("ChgConf", ac.priv.userConf); + cf.WriteInt("ChgPassword", ac.priv.userPasswd); + cf.WriteInt("ChgStat", ac.priv.userStat); + cf.WriteInt("ChgCash", ac.priv.userCash); + cf.WriteInt("UsrAddDel", ac.priv.userAddDel); + cf.WriteInt("ChgTariff", ac.priv.tariffChg); + cf.WriteInt("ChgAdmin", ac.priv.adminChg); + } return 0; } @@ -1378,7 +1358,7 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_ CONFIGFILE cf(fileName); char pass[ADM_PASSWD_LEN + 1]; char password[ADM_PASSWD_LEN + 1]; -char passwordE[2*ADM_PASSWD_LEN + 2]; +char passwordE[2 * ADM_PASSWD_LEN + 2]; BLOWFISH_CTX ctx; string p; @@ -1404,8 +1384,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) @@ -1502,19 +1480,14 @@ int FILES_STORE::AddTariff(const string & name) const { string fileName; strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str()); -FILE * f; -f = fopen(fileName.c_str(), "wt"); -if (f) +if (Touch(fileName)) { - fprintf(f, "\n"); - fclose(f); - return 0; + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Cannot create file " + fileName; + printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str()); + return -1; } - -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -errorStr = "Cannot create file " + fileName; -printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str()); -return -1; +return 0; } //----------------------------------------------------------------------------- int FILES_STORE::DelTariff(const string & name) const @@ -1534,15 +1507,15 @@ return 0; //----------------------------------------------------------------------------- int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const { -string tariffFileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; -CONFIGFILE conf(tariffFileName); +string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; +CONFIGFILE conf(fileName); string str; td->tariffConf.name = tariffName; if (conf.Error() != 0) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Cannot read file " + tariffFileName; + errorStr = "Cannot read file " + fileName; printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str()); return -1; } @@ -1688,81 +1661,78 @@ return 0; //----------------------------------------------------------------------------- int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const { -string tariffFileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; -if (access(tariffFileName.c_str(), W_OK) != 0) - { - int fd = open(tariffFileName.c_str(), O_CREAT, 0600); - if (fd) - close(fd); - } +string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; -CONFIGFILE cf(tariffFileName); + { + CONFIGFILE cf(fileName, true); -int e = cf.Error(); + int e = cf.Error(); -if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error writing tariff " + tariffName; - printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str()); - return e; - } + if (e) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = "Error writing tariff " + tariffName; + printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str()); + return e; + } -string param; -for (int i = 0; i < DIR_NUM; i++) - { - strprintf(¶m, "PriceDayA%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega); + string param; + for (int i = 0; i < DIR_NUM; i++) + { + strprintf(¶m, "PriceDayA%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega); - strprintf(¶m, "PriceDayB%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega); + strprintf(¶m, "PriceDayB%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega); - strprintf(¶m, "PriceNightA%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega); + strprintf(¶m, "PriceNightA%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega); - strprintf(¶m, "PriceNightB%d", i); - cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega); + strprintf(¶m, "PriceNightB%d", i); + cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega); - strprintf(¶m, "Threshold%d", i); - cf.WriteInt(param, td.dirPrice[i].threshold); + strprintf(¶m, "Threshold%d", i); + cf.WriteInt(param, td.dirPrice[i].threshold); - string s; - strprintf(¶m, "Time%d", i); + string s; + strprintf(¶m, "Time%d", i); - strprintf(&s, "%0d:%0d-%0d:%0d", - td.dirPrice[i].hDay, - td.dirPrice[i].mDay, - td.dirPrice[i].hNight, - td.dirPrice[i].mNight); + strprintf(&s, "%0d:%0d-%0d:%0d", + td.dirPrice[i].hDay, + td.dirPrice[i].mDay, + td.dirPrice[i].hNight, + td.dirPrice[i].mNight); - cf.WriteString(param, s); + cf.WriteString(param, s); - strprintf(¶m, "NoDiscount%d", i); - cf.WriteInt(param, td.dirPrice[i].noDiscount); + strprintf(¶m, "NoDiscount%d", i); + cf.WriteInt(param, td.dirPrice[i].noDiscount); - strprintf(¶m, "SinglePrice%d", i); - cf.WriteInt(param, td.dirPrice[i].singlePrice); - } + strprintf(¶m, "SinglePrice%d", i); + cf.WriteInt(param, td.dirPrice[i].singlePrice); + } -cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost); -cf.WriteDouble("Fee", td.tariffConf.fee); -cf.WriteDouble("Free", td.tariffConf.free); + cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost); + cf.WriteDouble("Fee", td.tariffConf.fee); + cf.WriteDouble("Free", td.tariffConf.free); -switch (td.tariffConf.traffType) - { - case TRAFF_UP: - cf.WriteString("TraffType", "up"); - break; - case TRAFF_DOWN: - cf.WriteString("TraffType", "down"); - break; - case TRAFF_UP_DOWN: - cf.WriteString("TraffType", "up+down"); - break; - case TRAFF_MAX: - cf.WriteString("TraffType", "max"); - break; + switch (td.tariffConf.traffType) + { + case TRAFF_UP: + cf.WriteString("TraffType", "up"); + break; + case TRAFF_DOWN: + cf.WriteString("TraffType", "down"); + break; + case TRAFF_UP_DOWN: + cf.WriteString("TraffType", "up+down"); + break; + case TRAFF_MAX: + cf.WriteString("TraffType", "max"); + break; + } } + return 0; } //----------------------------------------------------------------------------- @@ -1773,8 +1743,7 @@ int FILES_STORE::WriteDetailedStat(const map & statTree, char fn[FN_STR_LEN]; char dn[FN_STR_LEN]; FILE * statFile; -//char timestr[30]; -time_t t;//, lastTimeReal; +time_t t; tm * lt; t = time(NULL); @@ -1895,6 +1864,7 @@ if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n", STG_LOCKER lock(&mutex, __FILE__, __LINE__); errorStr = string("fprint failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(statFile); return -1; } @@ -1920,6 +1890,7 @@ while (stIter != statTree.end()) errorStr += strerror(errno); errorStr += "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(statFile); return -1; } #else @@ -1935,6 +1906,7 @@ while (stIter != statTree.end()) errorStr += strerror(errno); errorStr += "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(statFile); return -1; } #endif @@ -1958,14 +1930,13 @@ return 0; //----------------------------------------------------------------------------- int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const { -//ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ. +//ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ. //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ. string fn; string dn; struct timeval tv; -FILE * msgFile; strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str()); if (access(dn.c_str(), F_OK) != 0) @@ -1981,16 +1952,14 @@ if (access(dn.c_str(), F_OK) != 0) } } -chmod(dn.c_str(), storeSettings.GetConfMode() | S_IXUSR); +chmod(dn.c_str(), storeSettings.GetConfModeDir()); gettimeofday(&tv, NULL); msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec); strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id); -msgFile = fopen (fn.c_str(), "wt"); - -if (!msgFile) +if (Touch(fn)) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); errorStr = "File \'"; @@ -1999,7 +1968,7 @@ if (!msgFile) printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } -fclose(msgFile); + return EditMessage(*msg, login); } //----------------------------------------------------------------------------- @@ -2009,12 +1978,12 @@ int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ. -string fn; +string fileName; FILE * msgFile; -strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id); +strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id); -if (access(fn.c_str(), F_OK) != 0) +if (access(fileName.c_str(), F_OK) != 0) { string idstr; x2str(msg.header.id, idstr); @@ -2026,11 +1995,13 @@ if (access(fn.c_str(), F_OK) != 0) return -1; } -msgFile = fopen(fn.c_str(), "wt"); +Touch(fileName + ".new"); + +msgFile = fopen((fileName + ".new").c_str(), "wt"); if (!msgFile) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "File \'" + fn + "\' cannot be writen."; + errorStr = "File \'" + fileName + "\' cannot be writen."; printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno)); return -1; } @@ -2049,12 +2020,22 @@ if (!res) STG_LOCKER lock(&mutex, __FILE__, __LINE__); errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno)); + fclose(msgFile); return -1; } fclose(msgFile); -chmod(fn.c_str(), storeSettings.GetConfMode()); +chmod((fileName + ".new").c_str(), storeSettings.GetConfMode()); + +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)); + return -1; + } + return 0; } //----------------------------------------------------------------------------- @@ -2076,13 +2057,18 @@ return unlink(fn.c_str()); //----------------------------------------------------------------------------- int FILES_STORE::GetMessageHdrs(vector * hdrsList, const string & login) const { -vector messages; -string dn; -dn = storeSettings.GetUsersDir() + "/" + login + "/messages/"; -GetFilesList(&messages, dn, S_IFREG, ""); +string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/"); //hdrsList->resize(messages.size()); +if (access(dn.c_str(), F_OK) != 0) + { + return 0; + } + +vector messages; +GetFileList(&messages, dn, S_IFREG, ""); + for (unsigned i = 0; i < messages.size(); i++) { unsigned long long id = 0; @@ -2209,9 +2195,69 @@ if (text) (*text) += txt; } } -//fprintf(msgFile, "%s\n", msg.text.c_str()); fclose(msgFile); return 0; } //----------------------------------------------------------------------------- +int FILES_STORE::Touch(const std::string & path) const +{ +FILE * f = fopen(path.c_str(), "wb"); +if (f) + { + fclose(f); + return 0; + } +return -1; +} +//----------------------------------------------------------------------------- +int GetFileList(vector * fileList, const string & directory, mode_t mode, const 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; + + string str = directory + "/" + 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; +} +//-----------------------------------------------------------------------------