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 $
46 #include "stg/common.h"
47 #include "stg/user_ips.h"
48 #include "stg/user_conf.h"
49 #include "stg/user_stat.h"
50 #include "stg/const.h"
51 #include "stg/blowfish.h"
52 #include "stg/logger.h"
53 #include "stg/locker.h"
54 #include "file_store.h"
56 #define DELETED_USERS_DIR "deleted_users"
58 #define adm_enc_passwd "cjeifY8m3"
62 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext);
64 const int pt_mega = 1024 * 1024;
65 //-----------------------------------------------------------------------------
70 //-------------------------------------------------------------------------
71 BAK_FILE(const string & fileName, bool removeBak)
76 BAK_FILE::removeBak = removeBak;
77 fileNameBak = fileName + ".bak";
78 if (rename(fileName.c_str(), fileNameBak.c_str()))
80 printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno));
88 //-------------------------------------------------------------------------
91 if(bakSuccessed && removeBak)
93 if (unlink(fileNameBak.c_str()))
95 printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno));
99 //-------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
108 class FILES_STORE_CREATOR
114 FILES_STORE_CREATOR()
115 : fs(new FILES_STORE())
118 ~FILES_STORE_CREATOR()
123 FILES_STORE * GetStore()
128 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
131 FILES_STORE_CREATOR fsc;
132 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
137 return fsc.GetStore();
139 //-----------------------------------------------------------------------------
140 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
146 //-----------------------------------------------------------------------------
147 FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS()
150 //-----------------------------------------------------------------------------
151 int FILES_STORE_SETTINGS::ParseOwner(const vector<PARAM_VALUE> & moduleParams, const string & owner, uid_t * uid)
155 vector<PARAM_VALUE>::const_iterator pvi;
156 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
157 if (pvi == moduleParams.end())
159 errorStr = "Parameter \'" + owner + "\' not found.";
160 printfd(__FILE__, "%s\n", errorStr.c_str());
163 if (User2UID(pvi->value[0].c_str(), uid) < 0)
165 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
166 printfd(__FILE__, "%s\n", errorStr.c_str());
171 //-----------------------------------------------------------------------------
172 int FILES_STORE_SETTINGS::ParseGroup(const vector<PARAM_VALUE> & moduleParams, const string & group, gid_t * gid)
176 vector<PARAM_VALUE>::const_iterator pvi;
177 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
178 if (pvi == moduleParams.end())
180 errorStr = "Parameter \'" + group + "\' not found.";
181 printfd(__FILE__, "%s\n", errorStr.c_str());
184 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
186 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
187 printfd(__FILE__, "%s\n", errorStr.c_str());
192 //-----------------------------------------------------------------------------
193 int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val)
195 if (0 == strcasecmp(value.c_str(), "yes"))
200 if (0 == strcasecmp(value.c_str(), "no"))
206 errorStr = "Incorrect value \'" + value + "\'.";
209 //-----------------------------------------------------------------------------
210 int FILES_STORE_SETTINGS::ParseMode(const vector<PARAM_VALUE> & moduleParams, const string & modeStr, mode_t * mode)
214 vector<PARAM_VALUE>::const_iterator pvi;
215 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
216 if (pvi == moduleParams.end())
218 errorStr = "Parameter \'" + modeStr + "\' not found.";
219 printfd(__FILE__, "%s\n", errorStr.c_str());
222 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
224 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
225 printfd(__FILE__, "%s\n", errorStr.c_str());
230 //-----------------------------------------------------------------------------
231 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
233 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
235 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
237 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
240 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
242 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
244 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
247 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
249 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
251 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
254 vector<PARAM_VALUE>::const_iterator pvi;
256 pv.param = "RemoveBak";
257 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
258 if (pvi == s.moduleParams.end())
264 if (ParseYesNo(pvi->value[0], &removeBak))
266 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
271 pv.param = "ReadBak";
272 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
273 if (pvi == s.moduleParams.end())
279 if (ParseYesNo(pvi->value[0], &readBak))
281 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
286 pv.param = "WorkDir";
287 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
288 if (pvi == s.moduleParams.end())
290 errorStr = "Parameter \'WorkDir\' not found.";
291 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
295 workDir = pvi->value[0];
296 if (workDir.size() && workDir[workDir.size() - 1] == '/')
298 workDir.resize(workDir.size() - 1);
300 usersDir = workDir + "/users/";
301 tariffsDir = workDir + "/tariffs/";
302 adminsDir = workDir + "/admins/";
306 //-----------------------------------------------------------------------------
307 const string & FILES_STORE_SETTINGS::GetStrError() const
311 //-----------------------------------------------------------------------------
312 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
318 errorStr = string("User \'") + string(user) + string("\' not found in system.");
319 printfd(__FILE__, "%s\n", errorStr.c_str());
326 //-----------------------------------------------------------------------------
327 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
333 errorStr = string("Group \'") + string(gr) + string("\' not found in system.");
334 printfd(__FILE__, "%s\n", errorStr.c_str());
341 //-----------------------------------------------------------------------------
342 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
349 errorStr = string("Error parsing mode \'") + str + string("\'");
350 printfd(__FILE__, "%s\n", errorStr.c_str());
354 for (int i = 0; i < 3; i++)
355 if (str[i] > '7' || str[i] < '0')
357 errorStr = string("Error parsing mode \'") + str + string("\'");
358 printfd(__FILE__, "%s\n", errorStr.c_str());
366 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
370 //-----------------------------------------------------------------------------
371 string FILES_STORE_SETTINGS::GetWorkDir() const
375 //-----------------------------------------------------------------------------
376 string FILES_STORE_SETTINGS::GetUsersDir() const
380 //-----------------------------------------------------------------------------
381 string FILES_STORE_SETTINGS::GetAdminsDir() const
385 //-----------------------------------------------------------------------------
386 string FILES_STORE_SETTINGS::GetTariffsDir() const
390 //-----------------------------------------------------------------------------
391 mode_t FILES_STORE_SETTINGS::GetStatMode() const
395 //-----------------------------------------------------------------------------
396 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
398 mode_t mode = statMode;
399 if (statMode & S_IRUSR) mode |= S_IXUSR;
400 if (statMode & S_IRGRP) mode |= S_IXGRP;
401 if (statMode & S_IROTH) mode |= S_IXOTH;
404 //-----------------------------------------------------------------------------
405 uid_t FILES_STORE_SETTINGS::GetStatUID() const
409 //-----------------------------------------------------------------------------
410 gid_t FILES_STORE_SETTINGS::GetStatGID() const
414 //-----------------------------------------------------------------------------
415 mode_t FILES_STORE_SETTINGS::GetConfMode() const
419 //-----------------------------------------------------------------------------
420 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
422 mode_t mode = confMode;
423 if (confMode & S_IRUSR) mode |= S_IXUSR;
424 if (confMode & S_IRGRP) mode |= S_IXGRP;
425 if (confMode & S_IROTH) mode |= S_IXOTH;
428 //-----------------------------------------------------------------------------
429 uid_t FILES_STORE_SETTINGS::GetConfUID() const
433 //-----------------------------------------------------------------------------
434 gid_t FILES_STORE_SETTINGS::GetConfGID() const
438 //-----------------------------------------------------------------------------
439 mode_t FILES_STORE_SETTINGS::GetLogMode() const
443 //-----------------------------------------------------------------------------
444 uid_t FILES_STORE_SETTINGS::GetLogUID() const
448 //-----------------------------------------------------------------------------
449 gid_t FILES_STORE_SETTINGS::GetLogGID() const
453 //-----------------------------------------------------------------------------
454 bool FILES_STORE_SETTINGS::GetRemoveBak() const
458 //-----------------------------------------------------------------------------
459 bool FILES_STORE_SETTINGS::GetReadBak() const
463 //-----------------------------------------------------------------------------
464 //-----------------------------------------------------------------------------
465 //-----------------------------------------------------------------------------
466 FILES_STORE::FILES_STORE()
468 version = "file_store v.1.04";
470 pthread_mutexattr_t attr;
471 pthread_mutexattr_init(&attr);
472 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
473 pthread_mutex_init(&mutex, &attr);
475 //-----------------------------------------------------------------------------
476 FILES_STORE::~FILES_STORE()
480 //-----------------------------------------------------------------------------
481 void FILES_STORE::SetSettings(const MODULE_SETTINGS & s)
485 //-----------------------------------------------------------------------------
486 int FILES_STORE::ParseSettings()
488 int ret = storeSettings.ParseSettings(settings);
491 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
492 errorStr = storeSettings.GetStrError();
496 //-----------------------------------------------------------------------------
497 const string & FILES_STORE::GetStrError() const
499 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
502 //-----------------------------------------------------------------------------
503 const string & FILES_STORE::GetVersion() const
507 //-----------------------------------------------------------------------------
508 int FILES_STORE::GetUsersList(vector<string> * userList) const
510 vector<string> files;
512 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
514 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
515 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + string(strerror(errno));
519 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
521 userList->swap(files);
525 //-----------------------------------------------------------------------------
526 int FILES_STORE::GetAdminsList(vector<string> * adminList) const
528 vector<string> files;
530 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
532 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
533 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + string(strerror(errno));
537 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
539 adminList->swap(files);
543 //-----------------------------------------------------------------------------
544 int FILES_STORE::GetTariffsList(vector<string> * tariffList) const
546 vector<string> files;
548 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
550 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
551 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + string(strerror(errno));
555 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
557 tariffList->swap(files);
561 //-----------------------------------------------------------------------------
562 int FILES_STORE::RemoveDir(const char * path) const
564 vector<string> fileList;
566 GetFileList(&fileList, path, S_IFREG, "");
568 for (unsigned i = 0; i < fileList.size(); i++)
570 string file = path + string("/") + fileList[i];
571 if (unlink(file.c_str()))
573 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
574 errorStr = "unlink failed. Message: '";
575 errorStr += strerror(errno);
577 printfd(__FILE__, "FILES_STORE::RemoveDir - unlink failed. Message: '%s'\n", strerror(errno));
583 GetFileList(&fileList, path, S_IFDIR, "");
585 for (unsigned i = 0; i < fileList.size(); i++)
587 string dir = string(path) + "/" + fileList[i];
588 RemoveDir(dir.c_str());
593 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
594 errorStr = "rmdir failed. Message: '";
595 errorStr += strerror(errno);
597 printfd(__FILE__, "FILES_STORE::RemoveDir - rmdir failed. Message: '%s'\n", strerror(errno));
603 //-----------------------------------------------------------------------------
604 int FILES_STORE::AddUser(const string & login) const
608 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
610 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
612 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
613 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
614 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
618 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
621 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
622 errorStr = "Cannot create file \"" + fileName + "\'";
623 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
626 /*f = fopen(fileName.c_str(), "wt");
629 if (fprintf(f, "\n") < 0)
631 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
632 errorStr = "fprintf failed. Message: '";
633 errorStr += strerror(errno);
635 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
642 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
643 errorStr = "Cannot create file \"" + fileName + "\'";
644 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
648 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
651 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
652 errorStr = "Cannot create file \"" + fileName + "\'";
653 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
656 /*f = fopen(fileName.c_str(), "wt");
659 if (fprintf(f, "\n") < 0)
661 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
662 errorStr = "fprintf failed. Message: '";
663 errorStr += strerror(errno);
665 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
672 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
673 errorStr = "Cannot create file \"" + fileName + "\'";
674 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
679 //-----------------------------------------------------------------------------
680 int FILES_STORE::DelUser(const string & login) const
685 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
686 if (access(dirName.c_str(), F_OK) != 0)
688 if (mkdir(dirName.c_str(), 0700) != 0)
690 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
691 errorStr = "Directory '" + dirName + "' cannot be created.";
692 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
697 if (access(dirName.c_str(), F_OK) == 0)
699 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
700 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
701 if (rename(dirName1.c_str(), dirName.c_str()))
703 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
704 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
705 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
711 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
712 if (RemoveDir(dirName.c_str()))
719 //-----------------------------------------------------------------------------
720 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
723 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
724 if (RestoreUserConf(conf, login, fileName))
726 if (!storeSettings.GetReadBak())
730 return RestoreUserConf(conf, login, fileName + ".bak");
734 //-----------------------------------------------------------------------------
735 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
737 CONFIGFILE cf(fileName);
743 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
744 errorStr = "User \'" + login + "\' data not read.";
745 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
749 if (cf.ReadString("Password", &conf->password, "") < 0)
751 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
752 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
753 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
756 if (conf->password.empty())
758 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
759 errorStr = "User \'" + login + "\' password is blank.";
760 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
764 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
767 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
768 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
771 if (conf->tariffName.empty())
773 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
774 errorStr = "User \'" + login + "\' tariff is blank.";
775 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
780 cf.ReadString("IP", &ipStr, "?");
786 catch (const string & s)
788 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
789 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
790 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
795 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
797 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
798 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
799 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
803 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
805 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
806 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
807 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
811 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
813 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
814 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
815 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
819 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
820 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
821 cf.ReadString("TariffChange", &conf->nextTariff, "");
822 cf.ReadString("Group", &conf->group, "");
823 cf.ReadString("RealName", &conf->realName, "");
824 cf.ReadString("Address", &conf->address, "");
825 cf.ReadString("Phone", &conf->phone, "");
826 cf.ReadString("Note", &conf->note, "");
827 cf.ReadString("email", &conf->email, "");
829 char userdataName[12];
830 for (int i = 0; i < USERDATA_NUM; i++)
832 snprintf(userdataName, 12, "Userdata%d", i);
833 cf.ReadString(userdataName, &conf->userdata[i], "");
836 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
838 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
839 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
840 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
846 //-----------------------------------------------------------------------------
847 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
850 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
852 if (RestoreUserStat(stat, login, fileName))
854 if (!storeSettings.GetReadBak())
858 return RestoreUserStat(stat, login, fileName + ".bak");
862 //-----------------------------------------------------------------------------
863 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
865 CONFIGFILE cf(fileName);
871 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
872 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
873 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
879 for (int i = 0; i < DIR_NUM; i++)
882 snprintf(s, 22, "D%d", i);
883 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
885 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
886 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
887 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
890 stat->down[i] = traff;
892 snprintf(s, 22, "U%d", i);
893 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
895 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
896 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
897 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
903 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
905 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
906 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
907 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
911 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
913 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
914 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
915 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
919 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
921 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
922 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
923 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
927 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
929 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
930 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
931 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
935 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
937 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
938 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
939 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
943 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
945 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
946 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
947 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
953 //-----------------------------------------------------------------------------
954 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
957 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
959 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
961 CONFIGFILE cfstat(fileName, true);
963 int e = cfstat.Error();
967 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
968 errorStr = string("User \'") + login + "\' conf not written\n";
969 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
973 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
974 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
978 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
979 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
982 cfstat.WriteString("Password", conf.password);
983 cfstat.WriteInt ("Passive", conf.passive);
984 cfstat.WriteInt ("Down", conf.disabled);
985 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
986 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
987 cfstat.WriteString("Tariff", conf.tariffName);
988 cfstat.WriteString("Address", conf.address);
989 cfstat.WriteString("Phone", conf.phone);
990 cfstat.WriteString("Email", conf.email);
991 cfstat.WriteString("Note", conf.note);
992 cfstat.WriteString("RealName", conf.realName);
993 cfstat.WriteString("Group", conf.group);
994 cfstat.WriteDouble("Credit", conf.credit);
995 cfstat.WriteString("TariffChange", conf.nextTariff);
997 char userdataName[12];
998 for (int i = 0; i < USERDATA_NUM; i++)
1000 snprintf(userdataName, 12, "Userdata%d", i);
1001 cfstat.WriteString(userdataName, conf.userdata[i]);
1003 cfstat.WriteInt("CreditExpire", conf.creditExpire);
1007 cfstat.WriteString("IP", ipStr.str());
1011 //-----------------------------------------------------------------------------
1012 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1016 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
1018 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
1021 CONFIGFILE cfstat(fileName, true);
1022 int e = cfstat.Error();
1026 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1027 errorStr = string("User \'") + login + "\' stat not written\n";
1028 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
1032 for (int i = 0; i < DIR_NUM; i++)
1034 snprintf(s, 22, "D%d", i);
1035 cfstat.WriteInt(s, stat.down[i]);
1036 snprintf(s, 22, "U%d", i);
1037 cfstat.WriteInt(s, stat.up[i]);
1040 cfstat.WriteDouble("Cash", stat.cash);
1041 cfstat.WriteDouble("FreeMb", stat.freeMb);
1042 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
1043 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1044 cfstat.WriteInt("PassiveTime", stat.passiveTime);
1045 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
1048 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
1049 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
1053 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1054 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1059 //-----------------------------------------------------------------------------
1060 int FILES_STORE::WriteLogString(const string & str, const string & login) const
1063 time_t tm = time(NULL);
1065 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
1066 f = fopen(fileName.c_str(), "at");
1070 fprintf(f, "%s", LogDate(tm));
1072 fprintf(f, "%s", str.c_str());
1078 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1079 errorStr = "Cannot open \'" + fileName + "\'";
1080 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1084 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1085 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1089 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1090 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1095 //-----------------------------------------------------------------------------
1096 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
1099 time_t tm = time(NULL);
1101 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
1102 f = fopen(fileName.c_str(), "at");
1106 fprintf(f, "%s", LogDate(tm));
1108 fprintf(f, "%s", str.c_str());
1114 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1115 errorStr = "Cannot open \'" + fileName + "\'";
1116 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1120 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1121 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1125 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1126 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1131 //-----------------------------------------------------------------------------
1132 int FILES_STORE::WriteUserChgLog(const string & login,
1133 const string & admLogin,
1135 const string & paramName,
1136 const string & oldValue,
1137 const string & newValue,
1138 const string & message) const
1140 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1141 + paramName + "\' parameter changed from \'" + oldValue +
1142 "\' to \'" + newValue + "\'. " + message;
1144 return WriteLogString(userLogMsg, login);
1146 //-----------------------------------------------------------------------------
1147 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1149 string logStr = "Connect, " + inet_ntostring(ip);
1150 if (WriteLogString(logStr, login))
1152 return WriteLog2String(logStr, login);
1154 //-----------------------------------------------------------------------------
1155 int FILES_STORE::WriteUserDisconnect(const string & login,
1156 const DIR_TRAFF & up,
1157 const DIR_TRAFF & down,
1158 const DIR_TRAFF & sessionUp,
1159 const DIR_TRAFF & sessionDown,
1162 const std::string & reason) const
1164 stringstream logStr;
1165 logStr << "Disconnect, "
1166 << " session upload: \'"
1168 << "\' session download: \'"
1170 << "\' month upload: \'"
1172 << "\' month download: \'"
1178 if (WriteLogString(logStr.str(), login))
1181 logStr << " freeMb: \'"
1188 return WriteLog2String(logStr.str(), login);
1190 //-----------------------------------------------------------------------------
1191 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1195 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1196 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1198 CONFIGFILE s(stat1, true);
1202 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1203 errorStr = "Cannot create file '" + stat1 + "'";
1204 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1210 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1211 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1213 CONFIGFILE s2(stat2, true);
1217 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1218 errorStr = "Cannot create file '" + stat2 + "'";
1219 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1223 for (size_t i = 0; i < DIR_NUM; i++)
1226 snprintf(dirName, 3, "U%d", i);
1227 s.WriteInt(dirName, stat.up[i]); // Classic
1228 s2.WriteInt(dirName, stat.up[i]); // New
1229 snprintf(dirName, 3, "D%d", i);
1230 s.WriteInt(dirName, stat.down[i]); // Classic
1231 s2.WriteInt(dirName, stat.down[i]); // New
1235 s.WriteDouble("cash", stat.cash);
1238 s2.WriteDouble("Cash", stat.cash);
1239 s2.WriteDouble("FreeMb", stat.freeMb);
1240 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1241 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1242 s2.WriteInt("PassiveTime", stat.passiveTime);
1243 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1247 //-----------------------------------------------------------------------------*/
1248 int FILES_STORE::AddAdmin(const string & login) const
1251 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1253 if (Touch(fileName))
1255 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1256 errorStr = "Cannot create file " + fileName;
1257 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1263 //-----------------------------------------------------------------------------*/
1264 int FILES_STORE::DelAdmin(const string & login) const
1267 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1268 if (unlink(fileName.c_str()))
1270 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1271 errorStr = "unlink failed. Message: '";
1272 errorStr += strerror(errno);
1274 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1278 //-----------------------------------------------------------------------------*/
1279 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1281 char passwordE[2 * ADM_PASSWD_LEN + 2];
1282 char pass[ADM_PASSWD_LEN + 1];
1283 char adminPass[ADM_PASSWD_LEN + 1];
1287 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1290 CONFIGFILE cf(fileName, true);
1296 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1297 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1298 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1302 memset(pass, 0, sizeof(pass));
1303 memset(adminPass, 0, sizeof(adminPass));
1306 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1308 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1309 adminPass[ADM_PASSWD_LEN - 1] = 0;
1311 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1313 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1316 pass[ADM_PASSWD_LEN - 1] = 0;
1317 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1319 cf.WriteString("password", passwordE);
1320 cf.WriteInt("ChgConf", ac.priv.userConf);
1321 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1322 cf.WriteInt("ChgStat", ac.priv.userStat);
1323 cf.WriteInt("ChgCash", ac.priv.userCash);
1324 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1325 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1326 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1331 //-----------------------------------------------------------------------------
1332 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1335 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1336 CONFIGFILE cf(fileName);
1337 char pass[ADM_PASSWD_LEN + 1];
1338 char password[ADM_PASSWD_LEN + 1];
1339 char passwordE[2 * ADM_PASSWD_LEN + 2];
1346 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1347 errorStr = "Cannot open " + fileName;
1348 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1354 if (cf.ReadString("password", &p, "*"))
1356 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1357 errorStr = "Error in parameter password";
1358 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1362 memset(passwordE, 0, sizeof(passwordE));
1363 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1365 memset(pass, 0, sizeof(pass));
1367 if (passwordE[0] != 0)
1369 Decode21(pass, passwordE);
1370 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1372 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1374 DecodeString(password + 8*i, pass + 8*i, &ctx);
1382 ac->password = password;
1384 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1385 ac->priv.userConf = a;
1388 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1389 errorStr = "Error in parameter ChgConf";
1390 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1394 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1395 ac->priv.userPasswd = a;
1398 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1399 errorStr = "Error in parameter ChgPassword";
1400 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1404 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1405 ac->priv.userStat = a;
1408 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1409 errorStr = "Error in parameter ChgStat";
1410 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1414 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1415 ac->priv.userCash = a;
1418 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1419 errorStr = "Error in parameter ChgCash";
1420 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1424 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1425 ac->priv.userAddDel = a;
1428 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1429 errorStr = "Error in parameter UsrAddDel";
1430 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1434 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1435 ac->priv.adminChg = a;
1438 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1439 errorStr = "Error in parameter ChgAdmin";
1440 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1444 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1445 ac->priv.tariffChg = a;
1448 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1449 errorStr = "Error in parameter ChgTariff";
1450 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1456 //-----------------------------------------------------------------------------
1457 int FILES_STORE::AddTariff(const string & name) const
1460 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1461 if (Touch(fileName))
1463 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1464 errorStr = "Cannot create file " + fileName;
1465 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1470 //-----------------------------------------------------------------------------
1471 int FILES_STORE::DelTariff(const string & name) const
1474 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1475 if (unlink(fileName.c_str()))
1477 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1478 errorStr = "unlink failed. Message: '";
1479 errorStr += strerror(errno);
1481 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1485 //-----------------------------------------------------------------------------
1486 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1488 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1489 CONFIGFILE conf(fileName);
1491 td->tariffConf.name = tariffName;
1493 if (conf.Error() != 0)
1495 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1496 errorStr = "Cannot read file " + fileName;
1497 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1502 for (int i = 0; i<DIR_NUM; i++)
1504 strprintf(¶m, "Time%d", i);
1505 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1507 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1508 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1509 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1513 ParseTariffTimeStr(str.c_str(),
1514 td->dirPrice[i].hDay,
1515 td->dirPrice[i].mDay,
1516 td->dirPrice[i].hNight,
1517 td->dirPrice[i].mNight);
1519 strprintf(¶m, "PriceDayA%d", i);
1520 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1522 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1523 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1524 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1527 td->dirPrice[i].priceDayA /= (1024*1024);
1529 strprintf(¶m, "PriceDayB%d", i);
1530 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1532 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1533 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1534 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1537 td->dirPrice[i].priceDayB /= (1024*1024);
1539 strprintf(¶m, "PriceNightA%d", i);
1540 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1542 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1543 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1544 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1547 td->dirPrice[i].priceNightA /= (1024*1024);
1549 strprintf(¶m, "PriceNightB%d", i);
1550 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1552 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1553 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1554 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1557 td->dirPrice[i].priceNightB /= (1024*1024);
1559 strprintf(¶m, "Threshold%d", i);
1560 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1562 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1563 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1564 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1568 strprintf(¶m, "SinglePrice%d", i);
1569 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1571 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1572 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1573 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1577 strprintf(¶m, "NoDiscount%d", i);
1578 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1580 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1581 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1582 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1587 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1589 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1590 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1591 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1595 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1597 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1598 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1599 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1603 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1605 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1606 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1607 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1611 if (conf.ReadString("TraffType", &str, "") < 0)
1613 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1614 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1615 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1619 if (!strcasecmp(str.c_str(), "up"))
1620 td->tariffConf.traffType = TRAFF_UP;
1622 if (!strcasecmp(str.c_str(), "down"))
1623 td->tariffConf.traffType = TRAFF_DOWN;
1625 if (!strcasecmp(str.c_str(), "up+down"))
1626 td->tariffConf.traffType = TRAFF_UP_DOWN;
1628 if (!strcasecmp(str.c_str(), "max"))
1629 td->tariffConf.traffType = TRAFF_MAX;
1632 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1633 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1634 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1639 //-----------------------------------------------------------------------------
1640 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1642 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1645 CONFIGFILE cf(fileName, true);
1651 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1652 errorStr = "Error writing tariff " + tariffName;
1653 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1658 for (int i = 0; i < DIR_NUM; i++)
1660 strprintf(¶m, "PriceDayA%d", i);
1661 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1663 strprintf(¶m, "PriceDayB%d", i);
1664 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1666 strprintf(¶m, "PriceNightA%d", i);
1667 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1669 strprintf(¶m, "PriceNightB%d", i);
1670 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1672 strprintf(¶m, "Threshold%d", i);
1673 cf.WriteInt(param, td.dirPrice[i].threshold);
1676 strprintf(¶m, "Time%d", i);
1678 strprintf(&s, "%0d:%0d-%0d:%0d",
1679 td.dirPrice[i].hDay,
1680 td.dirPrice[i].mDay,
1681 td.dirPrice[i].hNight,
1682 td.dirPrice[i].mNight);
1684 cf.WriteString(param, s);
1686 strprintf(¶m, "NoDiscount%d", i);
1687 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1689 strprintf(¶m, "SinglePrice%d", i);
1690 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1693 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1694 cf.WriteDouble("Fee", td.tariffConf.fee);
1695 cf.WriteDouble("Free", td.tariffConf.free);
1697 switch (td.tariffConf.traffType)
1700 cf.WriteString("TraffType", "up");
1703 cf.WriteString("TraffType", "down");
1706 cf.WriteString("TraffType", "up+down");
1709 cf.WriteString("TraffType", "max");
1716 //-----------------------------------------------------------------------------
1717 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1719 const string & login) const
1721 char fn[FN_STR_LEN];
1722 char dn[FN_STR_LEN];
1729 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1730 if (access(dn, F_OK) != 0)
1732 if (mkdir(dn, 0700) != 0)
1734 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1735 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1736 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1741 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1742 e += chmod(dn, storeSettings.GetStatModeDir());
1746 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1747 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1752 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1758 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1759 storeSettings.GetUsersDir().c_str(),
1763 if (access(dn, F_OK) != 0)
1765 if (mkdir(dn, 0700) != 0)
1767 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1768 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1769 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1774 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1775 e += chmod(dn, storeSettings.GetStatModeDir());
1779 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1780 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1783 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1784 storeSettings.GetUsersDir().c_str(),
1787 lt->tm_mon+1 < 10 ? "0" : "",
1789 if (access(dn, F_OK) != 0)
1791 if (mkdir(dn, 0700) != 0)
1793 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1794 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1795 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1800 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1801 e += chmod(dn, storeSettings.GetStatModeDir());
1805 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1806 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1809 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1811 statFile = fopen (fn, "at");
1815 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1816 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1817 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1824 lt1 = localtime(&lastStat);
1833 lt2 = localtime(&t);
1839 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1840 h1, m1, s1, h2, m2, s2) < 0)
1842 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1843 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1844 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1849 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1850 stIter = statTree.begin();
1852 while (stIter != statTree.end())
1855 x2str(stIter->second.up, u);
1856 x2str(stIter->second.down, d);
1857 #ifdef TRAFF_STAT_WITH_PORTS
1858 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1859 inet_ntostring(stIter->first.ip).c_str(),
1864 stIter->second.cash) < 0)
1866 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1867 errorStr = "fprint failed. Message: '";
1868 errorStr += strerror(errno);
1870 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1875 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1876 inet_ntostring(stIter->first.ip).c_str(),
1880 stIter->second.cash) < 0)
1882 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1883 errorStr = string("fprint failed. Message: '");
1884 errorStr += strerror(errno);
1886 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1897 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1898 e += chmod(fn, storeSettings.GetStatMode());
1902 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1903 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1908 //-----------------------------------------------------------------------------
1909 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1911 //ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1912 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1913 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1919 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1920 if (access(dn.c_str(), F_OK) != 0)
1922 if (mkdir(dn.c_str(), 0700) != 0)
1924 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1925 errorStr = "Directory \'";
1927 errorStr += "\' cannot be created.";
1928 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1933 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1935 gettimeofday(&tv, NULL);
1937 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1938 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1942 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1943 errorStr = "File \'";
1945 errorStr += "\' cannot be writen.";
1946 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1950 return EditMessage(*msg, login);
1952 //-----------------------------------------------------------------------------
1953 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1955 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1956 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1957 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1962 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1964 if (access(fileName.c_str(), F_OK) != 0)
1967 x2str(msg.header.id, idstr);
1968 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1969 errorStr = "Message for user \'";
1970 errorStr += login + "\' with ID \'";
1971 errorStr += idstr + "\' does not exist.";
1972 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1976 Touch(fileName + ".new");
1978 msgFile = fopen((fileName + ".new").c_str(), "wt");
1981 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1982 errorStr = "File \'" + fileName + "\' cannot be writen.";
1983 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1988 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1989 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1990 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1991 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1992 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1993 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1994 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1998 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1999 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
2000 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
2007 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
2009 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
2011 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2012 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
2013 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
2019 //-----------------------------------------------------------------------------
2020 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
2023 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2024 msg->header.id = id;
2025 return ReadMessage(fn, &msg->header, &msg->text);
2027 //-----------------------------------------------------------------------------
2028 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
2031 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2033 return unlink(fn.c_str());
2035 //-----------------------------------------------------------------------------
2036 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2038 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
2040 //hdrsList->resize(messages.size());
2042 if (access(dn.c_str(), F_OK) != 0)
2047 vector<string> messages;
2048 GetFileList(&messages, dn, S_IFREG, "");
2050 for (unsigned i = 0; i < messages.size(); i++)
2052 unsigned long long id = 0;
2054 if (str2x(messages[i].c_str(), id))
2056 if (unlink((dn + messages[i]).c_str()))
2058 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2059 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2060 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2067 if (ReadMessage(dn + messages[i], &hdr, NULL))
2074 if (unlink((dn + messages[i]).c_str()))
2076 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2077 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2078 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2085 hdrsList->push_back(hdr);
2089 //-----------------------------------------------------------------------------
2090 int FILES_STORE::ReadMessage(const string & fileName,
2092 string * text) const
2095 msgFile = fopen(fileName.c_str(), "rt");
2098 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2099 errorStr = "File \'";
2100 errorStr += fileName;
2101 errorStr += "\' cannot be openned.";
2102 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2108 d[1] = &hdr->lastSendTime;
2109 d[2] = &hdr->creationTime;
2110 d[3] = &hdr->showTime;
2111 d[4] = (unsigned*)(&hdr->repeat);
2112 d[5] = &hdr->repeatPeriod;
2114 memset(p, 0, sizeof(p));
2116 for (int pos = 0; pos < 6; pos++)
2118 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2119 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2120 errorStr = "Cannot read file \'";
2121 errorStr += fileName;
2122 errorStr += "\'. Missing data.";
2123 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2124 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2130 ep = strrchr(p, '\r');
2132 ep = strrchr(p, '\n');
2137 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2138 errorStr = "Cannot read file \'";
2139 errorStr += fileName;
2140 errorStr += "\'. Missing data.";
2141 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2142 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2147 if (str2x(p, *(d[pos])))
2149 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2150 errorStr = "Cannot read file \'";
2151 errorStr += fileName;
2152 errorStr += "\'. Incorrect value. \'";
2155 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2162 memset(txt, 0, sizeof(txt));
2165 text->erase(text->begin(), text->end());
2166 while (!feof(msgFile))
2169 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2179 //-----------------------------------------------------------------------------
2180 int FILES_STORE::Touch(const std::string & path) const
2182 FILE * f = fopen(path.c_str(), "wb");
2190 //-----------------------------------------------------------------------------
2191 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2193 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
2195 DIR * d = opendir(directory.c_str());
2199 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2204 while ((entry = readdir(d)))
2206 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2209 string str = directory + "/" + string(entry->d_name);
2212 if (stat(str.c_str(), &st))
2215 if (!(st.st_mode & mode)) // Filter by mode
2221 size_t d_nameLen = strlen(entry->d_name);
2222 if (d_nameLen <= ext.size())
2225 if (ext == entry->d_name + (d_nameLen - ext.size()))
2227 entry->d_name[d_nameLen - ext.size()] = 0;
2228 fileList->push_back(entry->d_name);
2233 fileList->push_back(entry->d_name);
2241 //-----------------------------------------------------------------------------