2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/10/07 19:53:11 $
33 #include <sys/types.h>
49 #include "user_conf.h"
50 #include "user_stat.h"
51 #include "stg_const.h"
52 #include "file_store.h"
54 #include "stg_logger.h"
55 #include "stg_locker.h"
57 #define DELETED_USERS_DIR "deleted_users"
59 #define adm_enc_passwd "cjeifY8m3"
63 const int pt_mega = 1024 * 1024;
64 //-----------------------------------------------------------------------------
69 //-------------------------------------------------------------------------
70 BAK_FILE(const string & fileName, bool removeBak)
75 BAK_FILE::removeBak = removeBak;
76 fileNameBak = fileName + ".bak";
77 /*struct stat fileStat;
78 if (stat(fileName.c_str(), &fileStat) == 0)
80 char * buff = new char[fileStat.st_size];
81 f = fopen(fileName.c_str(), "rb");
84 fread(buff, 1, fileStat.st_size, f);
87 fileNameBak = fileName + ".bak";
88 f = fopen(fileNameBak.c_str(), "wb");
91 fwrite(buff, 1, fileStat.st_size, f);
100 if (rename(fileName.c_str(), fileNameBak.c_str()))
102 printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno));
110 //-------------------------------------------------------------------------
113 if(bakSuccessed && removeBak)
115 if (unlink(fileNameBak.c_str()))
117 printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno));
121 //-------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
130 class FILES_STORE_CREATOR
136 FILES_STORE_CREATOR()
137 : fs(new FILES_STORE())
140 ~FILES_STORE_CREATOR()
145 FILES_STORE * GetStore()
150 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
153 FILES_STORE_CREATOR fsc;
154 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
157 BASE_STORE * GetStore()
159 return fsc.GetStore();
161 //-----------------------------------------------------------------------------
162 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
168 //-----------------------------------------------------------------------------
169 FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS()
172 //-----------------------------------------------------------------------------
173 int FILES_STORE_SETTINGS::ParseOwner(const vector<PARAM_VALUE> & moduleParams, const string & owner, uid_t * uid)
177 vector<PARAM_VALUE>::const_iterator pvi;
178 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
179 if (pvi == moduleParams.end())
181 errorStr = "Parameter \'" + owner + "\' not found.";
182 printfd(__FILE__, "%s\n", errorStr.c_str());
185 if (User2UID(pvi->value[0].c_str(), uid) < 0)
187 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
188 printfd(__FILE__, "%s\n", errorStr.c_str());
193 //-----------------------------------------------------------------------------
194 int FILES_STORE_SETTINGS::ParseGroup(const vector<PARAM_VALUE> & moduleParams, const string & group, gid_t * gid)
198 vector<PARAM_VALUE>::const_iterator pvi;
199 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
200 if (pvi == moduleParams.end())
202 errorStr = "Parameter \'" + group + "\' not found.";
203 printfd(__FILE__, "%s\n", errorStr.c_str());
206 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
208 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
209 printfd(__FILE__, "%s\n", errorStr.c_str());
214 //-----------------------------------------------------------------------------
215 int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val)
217 if (0 == strcasecmp(value.c_str(), "yes"))
222 if (0 == strcasecmp(value.c_str(), "no"))
228 errorStr = "Incorrect value \'" + value + "\'.";
231 //-----------------------------------------------------------------------------
232 int FILES_STORE_SETTINGS::ParseMode(const vector<PARAM_VALUE> & moduleParams, const string & modeStr, mode_t * mode)
236 vector<PARAM_VALUE>::const_iterator pvi;
237 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
238 if (pvi == moduleParams.end())
240 errorStr = "Parameter \'" + modeStr + "\' not found.";
241 printfd(__FILE__, "%s\n", errorStr.c_str());
244 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
246 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
247 printfd(__FILE__, "%s\n", errorStr.c_str());
252 //-----------------------------------------------------------------------------
253 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
255 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
257 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
259 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
262 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
264 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
266 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
269 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
271 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
273 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
276 vector<PARAM_VALUE>::const_iterator pvi;
278 pv.param = "RemoveBak";
279 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
280 if (pvi == s.moduleParams.end())
286 if (ParseYesNo(pvi->value[0], &removeBak))
288 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
293 pv.param = "ReadBak";
294 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
295 if (pvi == s.moduleParams.end())
301 if (ParseYesNo(pvi->value[0], &readBak))
303 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
308 pv.param = "WorkDir";
309 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
310 if (pvi == s.moduleParams.end())
312 errorStr = "Parameter \'WorkDir\' not found.";
313 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
317 workDir = pvi->value[0];
318 if (workDir.size() && workDir[workDir.size() - 1] == '/')
320 workDir.resize(workDir.size() - 1);
322 usersDir = workDir + "/users/";
323 tariffsDir = workDir + "/tariffs/";
324 adminsDir = workDir + "/admins/";
328 //-----------------------------------------------------------------------------
329 const string & FILES_STORE_SETTINGS::GetStrError() const
333 //-----------------------------------------------------------------------------
334 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
340 errorStr = string("User \'") + string(user) + string("\' not found in system.");
341 printfd(__FILE__, "%s\n", errorStr.c_str());
348 //-----------------------------------------------------------------------------
349 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
355 errorStr = string("Group \'") + string(gr) + string("\' not found in system.");
356 printfd(__FILE__, "%s\n", errorStr.c_str());
363 //-----------------------------------------------------------------------------
364 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
371 errorStr = string("Error parsing mode \'") + str + string("\'");
372 printfd(__FILE__, "%s\n", errorStr.c_str());
376 for (int i = 0; i < 3; i++)
377 if (str[i] > '7' || str[i] < '0')
379 errorStr = string("Error parsing mode \'") + str + string("\'");
380 printfd(__FILE__, "%s\n", errorStr.c_str());
388 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
392 //-----------------------------------------------------------------------------
393 string FILES_STORE_SETTINGS::GetWorkDir() const
397 //-----------------------------------------------------------------------------
398 string FILES_STORE_SETTINGS::GetUsersDir() const
402 //-----------------------------------------------------------------------------
403 string FILES_STORE_SETTINGS::GetAdminsDir() const
407 //-----------------------------------------------------------------------------
408 string FILES_STORE_SETTINGS::GetTariffsDir() const
412 //-----------------------------------------------------------------------------
413 mode_t FILES_STORE_SETTINGS::GetStatMode() const
417 //-----------------------------------------------------------------------------
418 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
420 mode_t mode = statMode;
421 if (statMode & S_IRUSR) mode |= S_IXUSR;
422 if (statMode & S_IRGRP) mode |= S_IXGRP;
423 if (statMode & S_IROTH) mode |= S_IXOTH;
426 //-----------------------------------------------------------------------------
427 uid_t FILES_STORE_SETTINGS::GetStatUID() const
431 //-----------------------------------------------------------------------------
432 gid_t FILES_STORE_SETTINGS::GetStatGID() const
436 //-----------------------------------------------------------------------------
437 mode_t FILES_STORE_SETTINGS::GetConfMode() const
441 //-----------------------------------------------------------------------------
442 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
444 mode_t mode = confMode;
445 if (statMode & S_IRUSR) mode |= S_IXUSR;
446 if (statMode & S_IRGRP) mode |= S_IXGRP;
447 if (statMode & S_IROTH) mode |= S_IXOTH;
450 //-----------------------------------------------------------------------------
451 uid_t FILES_STORE_SETTINGS::GetConfUID() const
455 //-----------------------------------------------------------------------------
456 gid_t FILES_STORE_SETTINGS::GetConfGID() const
460 //-----------------------------------------------------------------------------
461 mode_t FILES_STORE_SETTINGS::GetLogMode() const
465 //-----------------------------------------------------------------------------
466 uid_t FILES_STORE_SETTINGS::GetLogUID() const
470 //-----------------------------------------------------------------------------
471 gid_t FILES_STORE_SETTINGS::GetLogGID() const
475 //-----------------------------------------------------------------------------
476 bool FILES_STORE_SETTINGS::GetRemoveBak() const
480 //-----------------------------------------------------------------------------
481 bool FILES_STORE_SETTINGS::GetReadBak() const
485 //-----------------------------------------------------------------------------
486 //-----------------------------------------------------------------------------
487 //-----------------------------------------------------------------------------
488 //-----------------------------------------------------------------------------
489 //-----------------------------------------------------------------------------
490 /*BASE_SETTINGS * FILES_STORE::GetStoreSettings()
492 return &storeSettings;
494 //-----------------------------------------------------------------------------
495 FILES_STORE::FILES_STORE()
497 version = "file_store v.1.04";
499 pthread_mutexattr_t attr;
500 pthread_mutexattr_init(&attr);
501 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
502 pthread_mutex_init(&mutex, &attr);
504 //-----------------------------------------------------------------------------
505 FILES_STORE::~FILES_STORE()
509 //-----------------------------------------------------------------------------
510 void FILES_STORE::SetSettings(const MODULE_SETTINGS & s)
514 //-----------------------------------------------------------------------------
515 int FILES_STORE::ParseSettings()
517 int ret = storeSettings.ParseSettings(settings);
520 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
521 errorStr = storeSettings.GetStrError();
525 //-----------------------------------------------------------------------------
526 const string & FILES_STORE::GetStrError() const
528 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
531 //-----------------------------------------------------------------------------
532 const string & FILES_STORE::GetVersion() const
536 //-----------------------------------------------------------------------------
537 int FILES_STORE::GetFilesList(vector<string> * filesList, const string & directory, mode_t mode, const string & ext) const
539 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
547 d = opendir(directory.c_str());
551 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
552 errorStr = "Directory \'" + directory + "\' cannot be opened.";
553 //printfd(__FILE__, "%s\n", errorStr.c_str());
558 int extLen = ext.size() ;
559 while ((dir = readdir(d)))
561 if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
563 str = directory + "/" + string(dir->d_name);
564 if (!stat(str.c_str(), &st))
566 if (st.st_mode & mode) // ïÔÓÅ× ÆÁÊÌÏ× or directories
568 d_nameLen = strlen(dir->d_name);
569 if (d_nameLen > extLen)
571 if (strcmp(dir->d_name + (d_nameLen - extLen), ext.c_str()) == 0)
573 dir->d_name[d_nameLen - extLen] = 0;
574 filesList->push_back(dir->d_name);
585 //-----------------------------------------------------------------------------
586 int FILES_STORE::GetUsersList(vector<string> * usersList) const
588 return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, "");
590 //-----------------------------------------------------------------------------
591 int FILES_STORE::GetAdminsList(vector<string> * adminsList) const
593 return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm");
595 //-----------------------------------------------------------------------------
596 int FILES_STORE::GetTariffsList(vector<string> * tariffsList) const
598 return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf");
600 //-----------------------------------------------------------------------------
601 int FILES_STORE::RemoveDir(const char * path) const
603 vector<string> filesList;
605 GetFilesList(&filesList, path, S_IFREG, "");
607 for (unsigned i = 0; i < filesList.size(); i++)
609 string file = path + string("/") + filesList[i];
610 if (unlink(file.c_str()))
612 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
613 errorStr = "unlink failed. Message: '";
614 errorStr += strerror(errno);
616 printfd(__FILE__, "FILES_STORE::RemoveDir - unlink failed. Message: '%s'\n", strerror(errno));
621 GetFilesList(&filesList, path, S_IFDIR, "");
623 for (unsigned i = 0; i < filesList.size(); i++)
625 string dir = string(path) + "/" + filesList[i];
626 RemoveDir(dir.c_str());
631 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
632 errorStr = "rmdir failed. Message: '";
633 errorStr += strerror(errno);
635 printfd(__FILE__, "FILES_STORE::RemoveDir - rmdir failed. Message: '%s'\n", strerror(errno));
641 //-----------------------------------------------------------------------------
642 int FILES_STORE::AddUser(const string & login) const
647 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
649 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
651 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
652 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
653 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
657 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
658 f = fopen(fileName.c_str(), "wt");
661 if (fprintf(f, "\n") < 0)
663 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
664 errorStr = "fprintf failed. Message: '";
665 errorStr += strerror(errno);
667 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
674 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
675 errorStr = "Cannot create file \"" + fileName + "\'";
676 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
680 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
681 f = fopen(fileName.c_str(), "wt");
684 if (fprintf(f, "\n") < 0)
686 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
687 errorStr = "fprintf failed. Message: '";
688 errorStr += strerror(errno);
690 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
697 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
698 errorStr = "Cannot create file \"" + fileName + "\'";
699 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
704 //-----------------------------------------------------------------------------
705 int FILES_STORE::DelUser(const string & login) const
710 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
711 if (access(dirName.c_str(), F_OK) != 0)
713 if (mkdir(dirName.c_str(), 0700) != 0)
715 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
716 errorStr = "Directory '" + dirName + "' cannot be created.";
717 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
722 if (access(dirName.c_str(), F_OK) == 0)
724 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
725 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
726 if (rename(dirName1.c_str(), dirName.c_str()))
728 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
729 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
730 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
736 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
737 if (RemoveDir(dirName.c_str()))
744 //-----------------------------------------------------------------------------
745 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
748 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
749 if (RestoreUserConf(conf, login, fileName))
751 if (!storeSettings.GetReadBak())
755 return RestoreUserConf(conf, login, fileName + ".bak");
759 //-----------------------------------------------------------------------------
760 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
762 CONFIGFILE cf(fileName);
768 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
769 errorStr = "User \'" + login + "\' data not read.";
770 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
774 if (cf.ReadString("Password", &conf->password, "") < 0)
776 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
777 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
778 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
781 if (conf->password.empty())
783 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
784 errorStr = "User \'" + login + "\' password is blank.";
785 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
789 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
791 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
792 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
793 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
796 if (conf->tariffName.empty())
798 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
799 errorStr = "User \'" + login + "\' tariff is blank.";
800 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
805 cf.ReadString("IP", &ipStr, "?");
813 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
814 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
815 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
820 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
822 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
823 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
824 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
828 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
830 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
831 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
832 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
836 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
838 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
839 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
840 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
844 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
845 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
846 cf.ReadString("TariffChange", &conf->nextTariff, "");
847 cf.ReadString("Group", &conf->group, "");
848 cf.ReadString("RealName", &conf->realName, "");
849 cf.ReadString("Address", &conf->address, "");
850 cf.ReadString("Phone", &conf->phone, "");
851 cf.ReadString("Note", &conf->note, "");
852 cf.ReadString("email", &conf->email, "");
854 char userdataName[12];
855 for (int i = 0; i < USERDATA_NUM; i++)
857 snprintf(userdataName, 12, "Userdata%d", i);
858 cf.ReadString(userdataName, &conf->userdata[i], "");
861 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
863 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
864 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
865 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
871 //-----------------------------------------------------------------------------
872 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
875 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
877 if (RestoreUserStat(stat, login, fileName))
879 if (!storeSettings.GetReadBak())
883 return RestoreUserStat(stat, login, fileName + ".bak");
887 //-----------------------------------------------------------------------------
888 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
890 CONFIGFILE cf(fileName);
896 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
897 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
898 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
904 for (int i = 0; i < DIR_NUM; i++)
907 snprintf(s, 22, "D%d", i);
908 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
910 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
911 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
912 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
915 stat->down[i] = traff;
917 snprintf(s, 22, "U%d", i);
918 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
920 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
921 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
922 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
928 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
930 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
931 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
932 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
936 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
938 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
939 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
940 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
944 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
946 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
947 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
948 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
952 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
954 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
955 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
956 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
960 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
962 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
963 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
964 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
968 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
970 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
971 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
972 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
978 //-----------------------------------------------------------------------------
979 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
982 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
984 BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
986 if (access(fileName.c_str(), W_OK) != 0)
989 f = fopen(fileName.c_str(), "wb");
994 CONFIGFILE cfstat(fileName);
996 int e = cfstat.Error();
1000 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1001 errorStr = string("User \'") + login + "\' conf not written\n";
1002 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
1006 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
1007 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
1011 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1012 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1015 cfstat.WriteString("Password", conf.password);
1016 cfstat.WriteInt ("Passive", conf.passive);
1017 cfstat.WriteInt ("Down", conf.disabled);
1018 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
1019 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
1020 cfstat.WriteString("Tariff", conf.tariffName);
1021 cfstat.WriteString("Address", conf.address);
1022 cfstat.WriteString("Phone", conf.phone);
1023 cfstat.WriteString("Email", conf.email);
1024 cfstat.WriteString("Note", conf.note);
1025 cfstat.WriteString("RealName", conf.realName);
1026 cfstat.WriteString("Group", conf.group);
1027 cfstat.WriteDouble("Credit", conf.credit);
1028 cfstat.WriteString("TariffChange", conf.nextTariff);
1030 char userdataName[12];
1031 for (int i = 0; i < USERDATA_NUM; i++)
1033 snprintf(userdataName, 12, "Userdata%d", i);
1034 cfstat.WriteString(userdataName, conf.userdata[i]);
1036 cfstat.WriteInt("CreditExpire", conf.creditExpire);
1040 cfstat.WriteString("IP", ipStr.str());
1044 //-----------------------------------------------------------------------------
1045 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1049 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
1051 BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
1053 if (access(fileName.c_str(), W_OK) != 0)
1056 f = fopen(fileName.c_str(), "wb");
1061 CONFIGFILE cfstat(fileName);
1062 int e = cfstat.Error();
1066 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1067 errorStr = string("User \'") + login + "\' stat not written\n";
1068 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
1072 for (int i = 0; i < DIR_NUM; i++)
1074 snprintf(s, 22, "D%d", i);
1075 cfstat.WriteInt(s, stat.down[i]);
1076 snprintf(s, 22, "U%d", i);
1077 cfstat.WriteInt(s, stat.up[i]);
1080 cfstat.WriteDouble("Cash", stat.cash);
1081 cfstat.WriteDouble("FreeMb", stat.freeMb);
1082 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
1083 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1084 cfstat.WriteInt("PassiveTime", stat.passiveTime);
1085 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
1087 e = chmod(fileName.c_str(), storeSettings.GetStatMode());
1088 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
1092 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1093 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1098 //-----------------------------------------------------------------------------
1099 int FILES_STORE::WriteLogString(const string & str, const string & login) const
1102 time_t tm = time(NULL);
1104 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
1105 f = fopen(fileName.c_str(), "at");
1109 fprintf(f, "%s", LogDate(tm));
1111 fprintf(f, "%s", str.c_str());
1117 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1118 errorStr = "Cannot open \'" + fileName + "\'";
1119 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1123 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1124 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1128 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1129 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1134 //-----------------------------------------------------------------------------
1135 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
1138 time_t tm = time(NULL);
1140 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
1141 f = fopen(fileName.c_str(), "at");
1145 fprintf(f, "%s", LogDate(tm));
1147 fprintf(f, "%s", str.c_str());
1153 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1154 errorStr = "Cannot open \'" + fileName + "\'";
1155 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1159 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1160 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1164 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1165 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1170 //-----------------------------------------------------------------------------
1171 int FILES_STORE::WriteUserChgLog(const string & login,
1172 const string & admLogin,
1174 const string & paramName,
1175 const string & oldValue,
1176 const string & newValue,
1177 const string & message) const
1179 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1180 + paramName + "\' parameter changed from \'" + oldValue +
1181 "\' to \'" + newValue + "\'. " + message;
1183 return WriteLogString(userLogMsg, login);
1185 //-----------------------------------------------------------------------------
1186 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1188 string logStr = "Connect, " + inet_ntostring(ip);
1189 if (WriteLogString(logStr, login))
1191 return WriteLog2String(logStr, login);
1193 //-----------------------------------------------------------------------------
1194 int FILES_STORE::WriteUserDisconnect(const string & login,
1195 const DIR_TRAFF & up,
1196 const DIR_TRAFF & down,
1197 const DIR_TRAFF & sessionUp,
1198 const DIR_TRAFF & sessionDown,
1201 const std::string & reason) const
1203 stringstream logStr;
1204 logStr << "Disconnect, ";
1205 /*stringstream sssu;
1209 stringstream sscash;
1215 sssd << sessionDown;
1219 logStr << " session upload: \'"
1221 << "\' session download: \'"
1223 << "\' month upload: \'"
1225 << "\' month download: \'"
1231 if (WriteLogString(logStr.str(), login))
1234 logStr << " freeMb: \'"
1241 return WriteLog2String(logStr.str(), login);
1243 //-----------------------------------------------------------------------------
1244 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1251 strprintf(&str,"%s/%s/stat.%d.%02d",
1252 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1254 if ((f = fopen(str.c_str(), "w")))
1260 s = new CONFIGFILE(str);
1265 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1266 errorStr = "Cannot create file " + str;
1267 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1273 for (int i = 0; i < DIR_NUM; i++)
1275 snprintf(dirName, 3, "U%d", i);
1276 s->WriteInt(dirName, stat.up[i]);
1277 snprintf(dirName, 3, "D%d", i);
1278 s->WriteInt(dirName, stat.down[i]);
1281 s->WriteDouble("cash", stat.cash);
1287 //-----------------------------------------------------------------------------*/
1288 int FILES_STORE::AddAdmin(const string & login) const
1291 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1293 f = fopen(fileName.c_str(), "wt");
1301 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1302 errorStr = "Cannot create file " + fileName;
1303 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1306 //-----------------------------------------------------------------------------*/
1307 int FILES_STORE::DelAdmin(const string & login) const
1310 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1311 if (unlink(fileName.c_str()))
1313 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1314 errorStr = "unlink failed. Message: '";
1315 errorStr += strerror(errno);
1317 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1321 //-----------------------------------------------------------------------------*/
1322 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1324 char passwordE[2 * ADM_PASSWD_LEN + 2];
1325 char pass[ADM_PASSWD_LEN + 1];
1326 char adminPass[ADM_PASSWD_LEN + 1];
1330 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1332 CONFIGFILE cf(fileName);
1338 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1339 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1340 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1344 memset(pass, 0, sizeof(pass));
1345 memset(adminPass, 0, sizeof(adminPass));
1348 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1350 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1351 adminPass[ADM_PASSWD_LEN - 1] = 0;
1353 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1355 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1358 pass[ADM_PASSWD_LEN - 1] = 0;
1359 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1360 //printfd(__FILE__, "passwordE %s\n", passwordE);
1362 cf.WriteString("password", passwordE);
1363 cf.WriteInt("ChgConf", ac.priv.userConf);
1364 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1365 cf.WriteInt("ChgStat", ac.priv.userStat);
1366 cf.WriteInt("ChgCash", ac.priv.userCash);
1367 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1368 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1369 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1373 //-----------------------------------------------------------------------------
1374 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1377 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1378 CONFIGFILE cf(fileName);
1379 char pass[ADM_PASSWD_LEN + 1];
1380 char password[ADM_PASSWD_LEN + 1];
1381 char passwordE[2*ADM_PASSWD_LEN + 2];
1388 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1389 errorStr = "Cannot open " + fileName;
1390 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1396 if (cf.ReadString("password", &p, "*"))
1398 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1399 errorStr = "Error in parameter password";
1400 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1404 memset(passwordE, 0, sizeof(passwordE));
1405 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1407 //printfd(__FILE__, "passwordE %s\n", passwordE);
1409 memset(pass, 0, sizeof(pass));
1411 if (passwordE[0] != 0)
1413 Decode21(pass, passwordE);
1414 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1416 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1418 DecodeString(password + 8*i, pass + 8*i, &ctx);
1426 ac->password = password;
1428 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1429 ac->priv.userConf = a;
1432 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1433 errorStr = "Error in parameter ChgConf";
1434 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1438 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1439 ac->priv.userPasswd = a;
1442 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1443 errorStr = "Error in parameter ChgPassword";
1444 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1448 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1449 ac->priv.userStat = a;
1452 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1453 errorStr = "Error in parameter ChgStat";
1454 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1458 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1459 ac->priv.userCash = a;
1462 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1463 errorStr = "Error in parameter ChgCash";
1464 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1468 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1469 ac->priv.userAddDel = a;
1472 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1473 errorStr = "Error in parameter UsrAddDel";
1474 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1478 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1479 ac->priv.adminChg = a;
1482 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1483 errorStr = "Error in parameter ChgAdmin";
1484 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1488 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1489 ac->priv.tariffChg = a;
1492 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1493 errorStr = "Error in parameter ChgTariff";
1494 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1500 //-----------------------------------------------------------------------------
1501 int FILES_STORE::AddTariff(const string & name) const
1504 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1506 f = fopen(fileName.c_str(), "wt");
1514 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1515 errorStr = "Cannot create file " + fileName;
1516 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1519 //-----------------------------------------------------------------------------
1520 int FILES_STORE::DelTariff(const string & name) const
1523 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1524 if (unlink(fileName.c_str()))
1526 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1527 errorStr = "unlink failed. Message: '";
1528 errorStr += strerror(errno);
1530 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1534 //-----------------------------------------------------------------------------
1535 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1537 string tariffFileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1538 CONFIGFILE conf(tariffFileName);
1540 td->tariffConf.name = tariffName;
1542 if (conf.Error() != 0)
1544 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1545 errorStr = "Cannot read file " + tariffFileName;
1546 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1551 for (int i = 0; i<DIR_NUM; i++)
1553 strprintf(¶m, "Time%d", i);
1554 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1556 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1557 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1558 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1562 ParseTariffTimeStr(str.c_str(),
1563 td->dirPrice[i].hDay,
1564 td->dirPrice[i].mDay,
1565 td->dirPrice[i].hNight,
1566 td->dirPrice[i].mNight);
1568 strprintf(¶m, "PriceDayA%d", i);
1569 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1571 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1572 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1573 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1576 td->dirPrice[i].priceDayA /= (1024*1024);
1578 strprintf(¶m, "PriceDayB%d", i);
1579 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1581 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1582 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1583 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1586 td->dirPrice[i].priceDayB /= (1024*1024);
1588 strprintf(¶m, "PriceNightA%d", i);
1589 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1591 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1592 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1593 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1596 td->dirPrice[i].priceNightA /= (1024*1024);
1598 strprintf(¶m, "PriceNightB%d", i);
1599 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1601 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1602 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1603 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1606 td->dirPrice[i].priceNightB /= (1024*1024);
1608 strprintf(¶m, "Threshold%d", i);
1609 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1611 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1612 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1613 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1617 strprintf(¶m, "SinglePrice%d", i);
1618 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1620 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1621 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1622 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1626 strprintf(¶m, "NoDiscount%d", i);
1627 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1629 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1630 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1631 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1636 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1638 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1639 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1640 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1644 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1646 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1647 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1648 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1652 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1654 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1655 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1656 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1660 if (conf.ReadString("TraffType", &str, "") < 0)
1662 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1663 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1664 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1668 if (!strcasecmp(str.c_str(), "up"))
1669 td->tariffConf.traffType = TRAFF_UP;
1671 if (!strcasecmp(str.c_str(), "down"))
1672 td->tariffConf.traffType = TRAFF_DOWN;
1674 if (!strcasecmp(str.c_str(), "up+down"))
1675 td->tariffConf.traffType = TRAFF_UP_DOWN;
1677 if (!strcasecmp(str.c_str(), "max"))
1678 td->tariffConf.traffType = TRAFF_MAX;
1681 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1682 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1683 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1688 //-----------------------------------------------------------------------------
1689 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1691 string tariffFileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1692 if (access(tariffFileName.c_str(), W_OK) != 0)
1694 int fd = open(tariffFileName.c_str(), O_CREAT, 0600);
1699 CONFIGFILE cf(tariffFileName);
1705 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1706 errorStr = "Error writing tariff " + tariffName;
1707 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1712 for (int i = 0; i < DIR_NUM; i++)
1714 strprintf(¶m, "PriceDayA%d", i);
1715 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1717 strprintf(¶m, "PriceDayB%d", i);
1718 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1720 strprintf(¶m, "PriceNightA%d", i);
1721 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1723 strprintf(¶m, "PriceNightB%d", i);
1724 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1726 strprintf(¶m, "Threshold%d", i);
1727 cf.WriteInt(param, td.dirPrice[i].threshold);
1730 strprintf(¶m, "Time%d", i);
1732 strprintf(&s, "%0d:%0d-%0d:%0d",
1733 td.dirPrice[i].hDay,
1734 td.dirPrice[i].mDay,
1735 td.dirPrice[i].hNight,
1736 td.dirPrice[i].mNight);
1738 cf.WriteString(param, s);
1740 strprintf(¶m, "NoDiscount%d", i);
1741 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1743 strprintf(¶m, "SinglePrice%d", i);
1744 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1747 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1748 cf.WriteDouble("Fee", td.tariffConf.fee);
1749 cf.WriteDouble("Free", td.tariffConf.free);
1751 switch (td.tariffConf.traffType)
1754 cf.WriteString("TraffType", "up");
1757 cf.WriteString("TraffType", "down");
1760 cf.WriteString("TraffType", "up+down");
1763 cf.WriteString("TraffType", "max");
1768 //-----------------------------------------------------------------------------
1769 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1771 const string & login) const
1773 char fn[FN_STR_LEN];
1774 char dn[FN_STR_LEN];
1777 time_t t;//, lastTimeReal;
1782 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1783 if (access(dn, F_OK) != 0)
1785 if (mkdir(dn, 0700) != 0)
1787 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1788 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1789 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1794 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1795 e += chmod(dn, storeSettings.GetStatModeDir());
1799 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1800 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1805 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1811 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1812 storeSettings.GetUsersDir().c_str(),
1816 if (access(dn, F_OK) != 0)
1818 if (mkdir(dn, 0700) != 0)
1820 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1821 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1822 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1827 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1828 e += chmod(dn, storeSettings.GetStatModeDir());
1832 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1833 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1836 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1837 storeSettings.GetUsersDir().c_str(),
1840 lt->tm_mon+1 < 10 ? "0" : "",
1842 if (access(dn, F_OK) != 0)
1844 if (mkdir(dn, 0700) != 0)
1846 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1847 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1848 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1853 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1854 e += chmod(dn, storeSettings.GetStatModeDir());
1858 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1859 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1862 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1864 statFile = fopen (fn, "at");
1868 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1869 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1870 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1877 lt1 = localtime(&lastStat);
1886 lt2 = localtime(&t);
1892 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1893 h1, m1, s1, h2, m2, s2) < 0)
1895 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1896 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1897 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1902 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1903 stIter = statTree.begin();
1905 while (stIter != statTree.end())
1908 x2str(stIter->second.up, u);
1909 x2str(stIter->second.down, d);
1910 #ifdef TRAFF_STAT_WITH_PORTS
1911 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1912 inet_ntostring(stIter->first.ip).c_str(),
1917 stIter->second.cash) < 0)
1919 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1920 errorStr = "fprint failed. Message: '";
1921 errorStr += strerror(errno);
1923 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1927 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1928 inet_ntostring(stIter->first.ip).c_str(),
1932 stIter->second.cash) < 0)
1934 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1935 errorStr = string("fprint failed. Message: '");
1936 errorStr += strerror(errno);
1938 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1948 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1949 e += chmod(fn, storeSettings.GetStatMode());
1953 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1954 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1959 //-----------------------------------------------------------------------------
1960 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1962 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1963 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1964 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1971 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1972 if (access(dn.c_str(), F_OK) != 0)
1974 if (mkdir(dn.c_str(), 0700) != 0)
1976 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1977 errorStr = "Directory \'";
1979 errorStr += "\' cannot be created.";
1980 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1985 chmod(dn.c_str(), storeSettings.GetConfMode() | S_IXUSR);
1987 gettimeofday(&tv, NULL);
1989 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1990 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1992 msgFile = fopen (fn.c_str(), "wt");
1996 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1997 errorStr = "File \'";
1999 errorStr += "\' cannot be writen.";
2000 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
2004 return EditMessage(*msg, login);
2006 //-----------------------------------------------------------------------------
2007 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
2009 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
2010 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
2011 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
2016 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
2018 if (access(fn.c_str(), F_OK) != 0)
2021 x2str(msg.header.id, idstr);
2022 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2023 errorStr = "Message for user \'";
2024 errorStr += login + "\' with ID \'";
2025 errorStr += idstr + "\' does not exist.";
2026 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
2030 msgFile = fopen(fn.c_str(), "wt");
2033 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2034 errorStr = "File \'" + fn + "\' cannot be writen.";
2035 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
2040 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
2041 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
2042 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
2043 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
2044 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
2045 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
2046 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
2050 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2051 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
2052 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
2058 chmod(fn.c_str(), storeSettings.GetConfMode());
2061 //-----------------------------------------------------------------------------
2062 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
2065 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2066 msg->header.id = id;
2067 return ReadMessage(fn, &msg->header, &msg->text);
2069 //-----------------------------------------------------------------------------
2070 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
2073 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2075 return unlink(fn.c_str());
2077 //-----------------------------------------------------------------------------
2078 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2080 vector<string> messages;
2082 dn = storeSettings.GetUsersDir() + "/" + login + "/messages/";
2083 GetFilesList(&messages, dn, S_IFREG, "");
2085 //hdrsList->resize(messages.size());
2087 for (unsigned i = 0; i < messages.size(); i++)
2089 unsigned long long id = 0;
2091 if (str2x(messages[i].c_str(), id))
2093 if (unlink((dn + messages[i]).c_str()))
2095 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2096 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2097 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2104 if (ReadMessage(dn + messages[i], &hdr, NULL))
2111 if (unlink((dn + messages[i]).c_str()))
2113 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2114 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2115 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2122 hdrsList->push_back(hdr);
2126 //-----------------------------------------------------------------------------
2127 int FILES_STORE::ReadMessage(const string & fileName,
2129 string * text) const
2132 msgFile = fopen(fileName.c_str(), "rt");
2135 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2136 errorStr = "File \'";
2137 errorStr += fileName;
2138 errorStr += "\' cannot be openned.";
2139 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2145 d[1] = &hdr->lastSendTime;
2146 d[2] = &hdr->creationTime;
2147 d[3] = &hdr->showTime;
2148 d[4] = (unsigned*)(&hdr->repeat);
2149 d[5] = &hdr->repeatPeriod;
2151 memset(p, 0, sizeof(p));
2153 for (int pos = 0; pos < 6; pos++)
2155 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2156 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2157 errorStr = "Cannot read file \'";
2158 errorStr += fileName;
2159 errorStr += "\'. Missing data.";
2160 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2161 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2167 ep = strrchr(p, '\r');
2169 ep = strrchr(p, '\n');
2174 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2175 errorStr = "Cannot read file \'";
2176 errorStr += fileName;
2177 errorStr += "\'. Missing data.";
2178 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2179 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2184 if (str2x(p, *(d[pos])))
2186 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2187 errorStr = "Cannot read file \'";
2188 errorStr += fileName;
2189 errorStr += "\'. Incorrect value. \'";
2192 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2199 memset(txt, 0, sizeof(txt));
2202 text->erase(text->begin(), text->end());
2203 while (!feof(msgFile))
2206 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2213 //fprintf(msgFile, "%s\n", msg.text.c_str());
2217 //-----------------------------------------------------------------------------