X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/fddd732457ef4b8955fefa105f7e95ba192b705b..b50165cd491723d34aea2d05a41e78a29edd14a9:/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 aea3aedc..1eaef642 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 @@ -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 @@ -131,7 +132,7 @@ FILES_STORE_CREATOR fsc; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -BASE_STORE * GetStore() +STORE * GetStore() { return fsc.GetStore(); } @@ -504,79 +505,69 @@ 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; +vector files; -filesList->clear(); - -d = opendir(directory.c_str()); - -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 +int FILES_STORE::GetAdminsList(vector * adminList) const { -return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, ""); -} -//----------------------------------------------------------------------------- -int FILES_STORE::GetAdminsList(vector * adminsList) 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; +vector fileList; -GetFilesList(&filesList, path, S_IFREG, ""); +GetFileList(&fileList, path, S_IFREG, ""); -for (unsigned i = 0; i < filesList.size(); i++) +for (unsigned i = 0; i < fileList.size(); i++) { - string file = path + string("/") + filesList[i]; + string file = path + string("/") + fileList[i]; if (unlink(file.c_str())) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -588,11 +579,12 @@ for (unsigned i = 0; i < filesList.size(); i++) } } -GetFilesList(&filesList, path, S_IFDIR, ""); +fileList.clear(); +GetFileList(&fileList, path, S_IFDIR, ""); -for (unsigned i = 0; i < filesList.size(); i++) +for (unsigned i = 0; i < fileList.size(); i++) { - string dir = string(path) + "/" + filesList[i]; + string dir = string(path) + "/" + fileList[i]; RemoveDir(dir.c_str()); } @@ -791,7 +783,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; @@ -966,65 +958,53 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/conf"; //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); -Touch(fileName + ".new"); +CONFIGFILE cfstat(fileName, true); - { - CONFIGFILE cfstat(fileName + ".new"); - - int e = cfstat.Error(); - - if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = 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()); +int e = cfstat.Error(); - if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); - } +if (e) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + errorStr = string("User \'") + login + "\' conf not written\n"; + printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str()); + return -1; + } - cfstat.WriteString("Password", conf.password); - cfstat.WriteInt ("Passive", conf.passive); - cfstat.WriteInt ("Down", conf.disabled); - cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat); - cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline); - cfstat.WriteString("Tariff", conf.tariffName); - cfstat.WriteString("Address", conf.address); - cfstat.WriteString("Phone", conf.phone); - cfstat.WriteString("Email", conf.email); - cfstat.WriteString("Note", conf.note); - cfstat.WriteString("RealName", conf.realName); - cfstat.WriteString("Group", conf.group); - cfstat.WriteDouble("Credit", conf.credit); - cfstat.WriteString("TariffChange", conf.nextTariff); - - char userdataName[12]; - for (int i = 0; i < USERDATA_NUM; i++) - { - snprintf(userdataName, 12, "Userdata%d", i); - cfstat.WriteString(userdataName, conf.userdata[i]); - } - cfstat.WriteInt("CreditExpire", conf.creditExpire); +e = chmod(fileName.c_str(), storeSettings.GetConfMode()); +e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID()); - stringstream ipStr; - ipStr << conf.ips; - cfstat.WriteString("IP", ipStr.str()); +if (e) + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } -if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0) +cfstat.WriteString("Password", conf.password); +cfstat.WriteInt ("Passive", conf.passive); +cfstat.WriteInt ("Down", conf.disabled); +cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat); +cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline); +cfstat.WriteString("Tariff", conf.tariffName); +cfstat.WriteString("Address", conf.address); +cfstat.WriteString("Phone", conf.phone); +cfstat.WriteString("Email", conf.email); +cfstat.WriteString("Note", conf.note); +cfstat.WriteString("RealName", conf.realName); +cfstat.WriteString("Group", conf.group); +cfstat.WriteDouble("Credit", conf.credit); +cfstat.WriteString("TariffChange", conf.nextTariff); + +char userdataName[12]; +for (int i = 0; i < USERDATA_NUM; i++) { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error moving dir from " + fileName + ".new to " + fileName; - printfd(__FILE__, "FILES_STORE::SaveUserConf - rename failed. Message: '%s'\n", strerror(errno)); - return -1; + snprintf(userdataName, 12, "Userdata%d", i); + cfstat.WriteString(userdataName, conf.userdata[i]); } +cfstat.WriteInt("CreditExpire", conf.creditExpire); + +stringstream ipStr; +ipStr << conf.ips; +cfstat.WriteString("IP", ipStr.str()); return 0; } @@ -1037,10 +1017,8 @@ fileName = storeSettings.GetUsersDir() + "/" + login + "/stat"; //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak()); -Touch(fileName + ".new"); - { - CONFIGFILE cfstat(fileName + ".new"); + CONFIGFILE cfstat(fileName, true); int e = cfstat.Error(); if (e) @@ -1065,23 +1043,15 @@ Touch(fileName + ".new"); cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime); cfstat.WriteInt("PassiveTime", stat.passiveTime); cfstat.WriteInt("LastActivityTime", stat.lastActivityTime); - - e = chmod(fileName.c_str(), storeSettings.GetStatMode()); - e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID()); - - if (e) - { - STG_LOCKER lock(&mutex, __FILE__, __LINE__); - printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); - } } -if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0) +int e = chmod(fileName.c_str(), storeSettings.GetStatMode()); +e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID()); + +if (e) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); - errorStr = "Error moving dir from " + fileName + ".new to " + fileName; - printfd(__FILE__, "FILES_STORE::SaveUserStat - rename failed. Message: '%s'\n", strerror(errno)); - return -1; + printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } return 0; @@ -1220,37 +1190,58 @@ return WriteLog2String(logStr.str(), login); //----------------------------------------------------------------------------- int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const { -string str; -int e; - -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); -Touch(str); - -CONFIGFILE s(str); -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 +string stat2; +strprintf(&stat2,"%s/%s/stat2.%d.%02d", + storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1); -for (int i = 0; i < DIR_NUM; i++) +CONFIGFILE s2(stat2, true); + +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++) { + 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 } +// 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; } //-----------------------------------------------------------------------------*/ @@ -1295,10 +1286,8 @@ string fileName; strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str()); -Touch(fileName + ".new"); - { - CONFIGFILE cf(fileName); + CONFIGFILE cf(fileName, true); int e = cf.Error(); @@ -1326,7 +1315,6 @@ Touch(fileName + ".new"); pass[ADM_PASSWD_LEN - 1] = 0; Encode12(passwordE, pass, ADM_PASSWD_LEN); - //printfd(__FILE__, "passwordE %s\n", passwordE); cf.WriteString("password", passwordE); cf.WriteInt("ChgConf", ac.priv.userConf); @@ -1338,14 +1326,6 @@ Touch(fileName + ".new"); cf.WriteInt("ChgAdmin", ac.priv.adminChg); } -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::SaveAdmin - rename failed. Message: '%s'\n", strerror(errno)); - return -1; - } - return 0; } //----------------------------------------------------------------------------- @@ -1507,15 +1487,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; } @@ -1663,10 +1643,8 @@ int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) c { string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; -Touch(fileName + ".new"); - { - CONFIGFILE cf(fileName); + CONFIGFILE cf(fileName, true); int e = cf.Error(); @@ -1735,14 +1713,6 @@ Touch(fileName + ".new"); } } -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; } //----------------------------------------------------------------------------- @@ -2030,6 +2000,7 @@ 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; } @@ -2066,13 +2037,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; @@ -2214,3 +2190,54 @@ if (f) 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; +} +//-----------------------------------------------------------------------------