#include <pwd.h>
#include <grp.h>
-#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <sstream>
#include <algorithm>
-#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"
using namespace std;
-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;
- }
+int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext);
- }
- //-------------------------------------------------------------------------
- ~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
-{
-private:
- FILES_STORE * fs;
-
-public:
- FILES_STORE_CREATOR()
- : fs(new FILES_STORE())
- {
- };
- ~FILES_STORE_CREATOR()
- {
- delete fs;
- };
-
- FILES_STORE * GetStore()
- {
- return fs;
- };
-};
+const int pt_mega = 1024 * 1024;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-FILES_STORE_CREATOR fsc;
+PLUGIN_CREATOR<FILES_STORE> fsc;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-BASE_STORE * GetStore()
+STORE * GetStore()
{
-return fsc.GetStore();
+return fsc.GetPlugin();
}
//-----------------------------------------------------------------------------
FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
return version;
}
//-----------------------------------------------------------------------------
-int FILES_STORE::GetFilesList(vector<string> * filesList, const string & directory, mode_t mode, const string & ext) const
+int FILES_STORE::GetUsersList(vector<string> * userList) const
{
-// æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
-DIR * d;
-string str;
-struct stat st;
-dirent * dir;
+vector<string> 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<string> * usersList) const
+int FILES_STORE::GetAdminsList(vector<string> * adminList) const
{
-return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, "");
-}
-//-----------------------------------------------------------------------------
-int FILES_STORE::GetAdminsList(vector<string> * adminsList) const
-{
-return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm");
+vector<string> 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<string> * tariffsList) const
+int FILES_STORE::GetTariffsList(vector<string> * tariffList) const
{
-return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf");
+vector<string> 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<string> 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));
+ 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;
}
{
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;
string fileName;
fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
-//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
-
CONFIGFILE cfstat(fileName, true);
int e = cfstat.Error();
string fileName;
fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
-//BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
-
{
CONFIGFILE cfstat(fileName, true);
int e = cfstat.Error();
//-----------------------------------------------------------------------------
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);
-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
+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;
}
//-----------------------------------------------------------------------------*/
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;
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)
return -1;
}
+if (cf.ReadInt("ChgService", &a, 0) == 0)
+ ac->priv.serviceChg = a;
+else
+ ac->priv.serviceChg = 0;
+
+if (cf.ReadInt("ChgCorp", &a, 0) == 0)
+ ac->priv.corpChg = a;
+else
+ ac->priv.corpChg = 0;
+
return 0;
}
//-----------------------------------------------------------------------------
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;
}
//-----------------------------------------------------------------------------
int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
{
-vector<string> 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<string> messages;
+GetFileList(&messages, dn, S_IFREG, "");
+
for (unsigned i = 0; i < messages.size(); i++)
{
unsigned long long id = 0;
return -1;
}
//-----------------------------------------------------------------------------
+int GetFileList(vector<string> * 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;
+}
+//-----------------------------------------------------------------------------