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 if (rename(fileName.c_str(), fileNameBak.c_str()))
79 printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno));
87 //-------------------------------------------------------------------------
90 if(bakSuccessed && removeBak)
92 if (unlink(fileNameBak.c_str()))
94 printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno));
98 //-------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
107 class FILES_STORE_CREATOR
113 FILES_STORE_CREATOR()
114 : fs(new FILES_STORE())
117 ~FILES_STORE_CREATOR()
122 FILES_STORE * GetStore()
127 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
130 FILES_STORE_CREATOR fsc;
131 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
134 BASE_STORE * GetStore()
136 return fsc.GetStore();
138 //-----------------------------------------------------------------------------
139 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
145 //-----------------------------------------------------------------------------
146 FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS()
149 //-----------------------------------------------------------------------------
150 int FILES_STORE_SETTINGS::ParseOwner(const vector<PARAM_VALUE> & moduleParams, const string & owner, uid_t * uid)
154 vector<PARAM_VALUE>::const_iterator pvi;
155 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
156 if (pvi == moduleParams.end())
158 errorStr = "Parameter \'" + owner + "\' not found.";
159 printfd(__FILE__, "%s\n", errorStr.c_str());
162 if (User2UID(pvi->value[0].c_str(), uid) < 0)
164 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
165 printfd(__FILE__, "%s\n", errorStr.c_str());
170 //-----------------------------------------------------------------------------
171 int FILES_STORE_SETTINGS::ParseGroup(const vector<PARAM_VALUE> & moduleParams, const string & group, gid_t * gid)
175 vector<PARAM_VALUE>::const_iterator pvi;
176 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
177 if (pvi == moduleParams.end())
179 errorStr = "Parameter \'" + group + "\' not found.";
180 printfd(__FILE__, "%s\n", errorStr.c_str());
183 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
185 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
186 printfd(__FILE__, "%s\n", errorStr.c_str());
191 //-----------------------------------------------------------------------------
192 int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val)
194 if (0 == strcasecmp(value.c_str(), "yes"))
199 if (0 == strcasecmp(value.c_str(), "no"))
205 errorStr = "Incorrect value \'" + value + "\'.";
208 //-----------------------------------------------------------------------------
209 int FILES_STORE_SETTINGS::ParseMode(const vector<PARAM_VALUE> & moduleParams, const string & modeStr, mode_t * mode)
213 vector<PARAM_VALUE>::const_iterator pvi;
214 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
215 if (pvi == moduleParams.end())
217 errorStr = "Parameter \'" + modeStr + "\' not found.";
218 printfd(__FILE__, "%s\n", errorStr.c_str());
221 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
223 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
224 printfd(__FILE__, "%s\n", errorStr.c_str());
229 //-----------------------------------------------------------------------------
230 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
232 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
234 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
236 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
239 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
241 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
243 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
246 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
248 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
250 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
253 vector<PARAM_VALUE>::const_iterator pvi;
255 pv.param = "RemoveBak";
256 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
257 if (pvi == s.moduleParams.end())
263 if (ParseYesNo(pvi->value[0], &removeBak))
265 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
270 pv.param = "ReadBak";
271 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
272 if (pvi == s.moduleParams.end())
278 if (ParseYesNo(pvi->value[0], &readBak))
280 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
285 pv.param = "WorkDir";
286 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
287 if (pvi == s.moduleParams.end())
289 errorStr = "Parameter \'WorkDir\' not found.";
290 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
294 workDir = pvi->value[0];
295 if (workDir.size() && workDir[workDir.size() - 1] == '/')
297 workDir.resize(workDir.size() - 1);
299 usersDir = workDir + "/users/";
300 tariffsDir = workDir + "/tariffs/";
301 adminsDir = workDir + "/admins/";
305 //-----------------------------------------------------------------------------
306 const string & FILES_STORE_SETTINGS::GetStrError() const
310 //-----------------------------------------------------------------------------
311 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
317 errorStr = string("User \'") + string(user) + string("\' not found in system.");
318 printfd(__FILE__, "%s\n", errorStr.c_str());
325 //-----------------------------------------------------------------------------
326 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
332 errorStr = string("Group \'") + string(gr) + string("\' not found in system.");
333 printfd(__FILE__, "%s\n", errorStr.c_str());
340 //-----------------------------------------------------------------------------
341 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
348 errorStr = string("Error parsing mode \'") + str + string("\'");
349 printfd(__FILE__, "%s\n", errorStr.c_str());
353 for (int i = 0; i < 3; i++)
354 if (str[i] > '7' || str[i] < '0')
356 errorStr = string("Error parsing mode \'") + str + string("\'");
357 printfd(__FILE__, "%s\n", errorStr.c_str());
365 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
369 //-----------------------------------------------------------------------------
370 string FILES_STORE_SETTINGS::GetWorkDir() const
374 //-----------------------------------------------------------------------------
375 string FILES_STORE_SETTINGS::GetUsersDir() const
379 //-----------------------------------------------------------------------------
380 string FILES_STORE_SETTINGS::GetAdminsDir() const
384 //-----------------------------------------------------------------------------
385 string FILES_STORE_SETTINGS::GetTariffsDir() const
389 //-----------------------------------------------------------------------------
390 mode_t FILES_STORE_SETTINGS::GetStatMode() const
394 //-----------------------------------------------------------------------------
395 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
397 mode_t mode = statMode;
398 if (statMode & S_IRUSR) mode |= S_IXUSR;
399 if (statMode & S_IRGRP) mode |= S_IXGRP;
400 if (statMode & S_IROTH) mode |= S_IXOTH;
403 //-----------------------------------------------------------------------------
404 uid_t FILES_STORE_SETTINGS::GetStatUID() const
408 //-----------------------------------------------------------------------------
409 gid_t FILES_STORE_SETTINGS::GetStatGID() const
413 //-----------------------------------------------------------------------------
414 mode_t FILES_STORE_SETTINGS::GetConfMode() const
418 //-----------------------------------------------------------------------------
419 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
421 mode_t mode = confMode;
422 if (confMode & S_IRUSR) mode |= S_IXUSR;
423 if (confMode & S_IRGRP) mode |= S_IXGRP;
424 if (confMode & S_IROTH) mode |= S_IXOTH;
427 //-----------------------------------------------------------------------------
428 uid_t FILES_STORE_SETTINGS::GetConfUID() const
432 //-----------------------------------------------------------------------------
433 gid_t FILES_STORE_SETTINGS::GetConfGID() const
437 //-----------------------------------------------------------------------------
438 mode_t FILES_STORE_SETTINGS::GetLogMode() const
442 //-----------------------------------------------------------------------------
443 uid_t FILES_STORE_SETTINGS::GetLogUID() const
447 //-----------------------------------------------------------------------------
448 gid_t FILES_STORE_SETTINGS::GetLogGID() const
452 //-----------------------------------------------------------------------------
453 bool FILES_STORE_SETTINGS::GetRemoveBak() const
457 //-----------------------------------------------------------------------------
458 bool FILES_STORE_SETTINGS::GetReadBak() const
462 //-----------------------------------------------------------------------------
463 //-----------------------------------------------------------------------------
464 //-----------------------------------------------------------------------------
465 FILES_STORE::FILES_STORE()
467 version = "file_store v.1.04";
469 pthread_mutexattr_t attr;
470 pthread_mutexattr_init(&attr);
471 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
472 pthread_mutex_init(&mutex, &attr);
474 //-----------------------------------------------------------------------------
475 FILES_STORE::~FILES_STORE()
479 //-----------------------------------------------------------------------------
480 void FILES_STORE::SetSettings(const MODULE_SETTINGS & s)
484 //-----------------------------------------------------------------------------
485 int FILES_STORE::ParseSettings()
487 int ret = storeSettings.ParseSettings(settings);
490 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
491 errorStr = storeSettings.GetStrError();
495 //-----------------------------------------------------------------------------
496 const string & FILES_STORE::GetStrError() const
498 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
501 //-----------------------------------------------------------------------------
502 const string & FILES_STORE::GetVersion() const
506 //-----------------------------------------------------------------------------
507 int FILES_STORE::GetFilesList(vector<string> * filesList, const string & directory, mode_t mode, const string & ext) const
509 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
517 d = opendir(directory.c_str());
521 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
522 errorStr = "Directory \'" + directory + "\' cannot be opened.";
523 //printfd(__FILE__, "%s\n", errorStr.c_str());
528 int extLen = ext.size() ;
529 while ((dir = readdir(d)))
531 if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
533 str = directory + "/" + string(dir->d_name);
534 if (!stat(str.c_str(), &st))
536 if (st.st_mode & mode) // ïÔÓÅ× ÆÁÊÌÏ× or directories
538 d_nameLen = strlen(dir->d_name);
539 if (d_nameLen > extLen)
541 if (strcmp(dir->d_name + (d_nameLen - extLen), ext.c_str()) == 0)
543 dir->d_name[d_nameLen - extLen] = 0;
544 filesList->push_back(dir->d_name);
555 //-----------------------------------------------------------------------------
556 int FILES_STORE::GetUsersList(vector<string> * usersList) const
558 return GetFilesList(usersList, storeSettings.GetUsersDir(), S_IFDIR, "");
560 //-----------------------------------------------------------------------------
561 int FILES_STORE::GetAdminsList(vector<string> * adminsList) const
563 return GetFilesList(adminsList, storeSettings.GetAdminsDir(), S_IFREG, ".adm");
565 //-----------------------------------------------------------------------------
566 int FILES_STORE::GetTariffsList(vector<string> * tariffsList) const
568 return GetFilesList(tariffsList, storeSettings.GetTariffsDir(), S_IFREG, ".tf");
570 //-----------------------------------------------------------------------------
571 int FILES_STORE::RemoveDir(const char * path) const
573 vector<string> filesList;
575 GetFilesList(&filesList, path, S_IFREG, "");
577 for (unsigned i = 0; i < filesList.size(); i++)
579 string file = path + string("/") + filesList[i];
580 if (unlink(file.c_str()))
582 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
583 errorStr = "unlink failed. Message: '";
584 errorStr += strerror(errno);
586 printfd(__FILE__, "FILES_STORE::RemoveDir - unlink failed. Message: '%s'\n", strerror(errno));
591 GetFilesList(&filesList, path, S_IFDIR, "");
593 for (unsigned i = 0; i < filesList.size(); i++)
595 string dir = string(path) + "/" + filesList[i];
596 RemoveDir(dir.c_str());
601 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
602 errorStr = "rmdir failed. Message: '";
603 errorStr += strerror(errno);
605 printfd(__FILE__, "FILES_STORE::RemoveDir - rmdir failed. Message: '%s'\n", strerror(errno));
611 //-----------------------------------------------------------------------------
612 int FILES_STORE::AddUser(const string & login) const
616 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
618 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
620 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
621 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
622 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
626 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
629 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
630 errorStr = "Cannot create file \"" + fileName + "\'";
631 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
634 /*f = fopen(fileName.c_str(), "wt");
637 if (fprintf(f, "\n") < 0)
639 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
640 errorStr = "fprintf failed. Message: '";
641 errorStr += strerror(errno);
643 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
650 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
651 errorStr = "Cannot create file \"" + fileName + "\'";
652 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
656 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
659 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
660 errorStr = "Cannot create file \"" + fileName + "\'";
661 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
664 /*f = fopen(fileName.c_str(), "wt");
667 if (fprintf(f, "\n") < 0)
669 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
670 errorStr = "fprintf failed. Message: '";
671 errorStr += strerror(errno);
673 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
680 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
681 errorStr = "Cannot create file \"" + fileName + "\'";
682 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
687 //-----------------------------------------------------------------------------
688 int FILES_STORE::DelUser(const string & login) const
693 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
694 if (access(dirName.c_str(), F_OK) != 0)
696 if (mkdir(dirName.c_str(), 0700) != 0)
698 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
699 errorStr = "Directory '" + dirName + "' cannot be created.";
700 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
705 if (access(dirName.c_str(), F_OK) == 0)
707 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
708 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
709 if (rename(dirName1.c_str(), dirName.c_str()))
711 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
712 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
713 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
719 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
720 if (RemoveDir(dirName.c_str()))
727 //-----------------------------------------------------------------------------
728 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
731 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
732 if (RestoreUserConf(conf, login, fileName))
734 if (!storeSettings.GetReadBak())
738 return RestoreUserConf(conf, login, fileName + ".bak");
742 //-----------------------------------------------------------------------------
743 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
745 CONFIGFILE cf(fileName);
751 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
752 errorStr = "User \'" + login + "\' data not read.";
753 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
757 if (cf.ReadString("Password", &conf->password, "") < 0)
759 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
760 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
761 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
764 if (conf->password.empty())
766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
767 errorStr = "User \'" + login + "\' password is blank.";
768 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
772 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
774 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
775 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
776 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
779 if (conf->tariffName.empty())
781 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
782 errorStr = "User \'" + login + "\' tariff is blank.";
783 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
788 cf.ReadString("IP", &ipStr, "?");
796 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
797 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
798 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
803 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
805 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
806 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
807 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
811 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
813 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
814 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
815 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
819 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
821 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
822 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
823 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
827 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
828 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
829 cf.ReadString("TariffChange", &conf->nextTariff, "");
830 cf.ReadString("Group", &conf->group, "");
831 cf.ReadString("RealName", &conf->realName, "");
832 cf.ReadString("Address", &conf->address, "");
833 cf.ReadString("Phone", &conf->phone, "");
834 cf.ReadString("Note", &conf->note, "");
835 cf.ReadString("email", &conf->email, "");
837 char userdataName[12];
838 for (int i = 0; i < USERDATA_NUM; i++)
840 snprintf(userdataName, 12, "Userdata%d", i);
841 cf.ReadString(userdataName, &conf->userdata[i], "");
844 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
846 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
847 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
848 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
854 //-----------------------------------------------------------------------------
855 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
858 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
860 if (RestoreUserStat(stat, login, fileName))
862 if (!storeSettings.GetReadBak())
866 return RestoreUserStat(stat, login, fileName + ".bak");
870 //-----------------------------------------------------------------------------
871 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
873 CONFIGFILE cf(fileName);
879 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
880 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
881 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
887 for (int i = 0; i < DIR_NUM; i++)
890 snprintf(s, 22, "D%d", i);
891 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
893 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
894 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
895 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
898 stat->down[i] = traff;
900 snprintf(s, 22, "U%d", i);
901 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
903 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
904 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
905 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
911 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
913 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
914 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
915 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
919 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
921 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
922 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
923 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
927 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
929 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
930 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
931 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
935 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
937 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
938 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
939 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
943 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
945 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
946 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
947 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
951 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
953 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
954 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
955 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
961 //-----------------------------------------------------------------------------
962 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
965 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
967 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
969 Touch(fileName + ".new");
972 CONFIGFILE cfstat(fileName + ".new");
974 int e = cfstat.Error();
978 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
979 errorStr = string("User \'") + login + "\' conf not written\n";
980 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
984 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
985 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
989 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
990 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
993 cfstat.WriteString("Password", conf.password);
994 cfstat.WriteInt ("Passive", conf.passive);
995 cfstat.WriteInt ("Down", conf.disabled);
996 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
997 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
998 cfstat.WriteString("Tariff", conf.tariffName);
999 cfstat.WriteString("Address", conf.address);
1000 cfstat.WriteString("Phone", conf.phone);
1001 cfstat.WriteString("Email", conf.email);
1002 cfstat.WriteString("Note", conf.note);
1003 cfstat.WriteString("RealName", conf.realName);
1004 cfstat.WriteString("Group", conf.group);
1005 cfstat.WriteDouble("Credit", conf.credit);
1006 cfstat.WriteString("TariffChange", conf.nextTariff);
1008 char userdataName[12];
1009 for (int i = 0; i < USERDATA_NUM; i++)
1011 snprintf(userdataName, 12, "Userdata%d", i);
1012 cfstat.WriteString(userdataName, conf.userdata[i]);
1014 cfstat.WriteInt("CreditExpire", conf.creditExpire);
1018 cfstat.WriteString("IP", ipStr.str());
1021 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1023 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1024 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1025 printfd(__FILE__, "FILES_STORE::SaveUserConf - rename failed. Message: '%s'\n", strerror(errno));
1031 //-----------------------------------------------------------------------------
1032 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1036 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
1038 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
1040 Touch(fileName + ".new");
1043 CONFIGFILE cfstat(fileName + ".new");
1044 int e = cfstat.Error();
1048 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1049 errorStr = string("User \'") + login + "\' stat not written\n";
1050 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
1054 for (int i = 0; i < DIR_NUM; i++)
1056 snprintf(s, 22, "D%d", i);
1057 cfstat.WriteInt(s, stat.down[i]);
1058 snprintf(s, 22, "U%d", i);
1059 cfstat.WriteInt(s, stat.up[i]);
1062 cfstat.WriteDouble("Cash", stat.cash);
1063 cfstat.WriteDouble("FreeMb", stat.freeMb);
1064 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
1065 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1066 cfstat.WriteInt("PassiveTime", stat.passiveTime);
1067 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
1069 e = chmod(fileName.c_str(), storeSettings.GetStatMode());
1070 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
1074 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1075 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1079 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1081 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1082 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1083 printfd(__FILE__, "FILES_STORE::SaveUserStat - rename failed. Message: '%s'\n", strerror(errno));
1089 //-----------------------------------------------------------------------------
1090 int FILES_STORE::WriteLogString(const string & str, const string & login) const
1093 time_t tm = time(NULL);
1095 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
1096 f = fopen(fileName.c_str(), "at");
1100 fprintf(f, "%s", LogDate(tm));
1102 fprintf(f, "%s", str.c_str());
1108 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1109 errorStr = "Cannot open \'" + fileName + "\'";
1110 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1114 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1115 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1119 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1120 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1125 //-----------------------------------------------------------------------------
1126 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
1129 time_t tm = time(NULL);
1131 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
1132 f = fopen(fileName.c_str(), "at");
1136 fprintf(f, "%s", LogDate(tm));
1138 fprintf(f, "%s", str.c_str());
1144 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1145 errorStr = "Cannot open \'" + fileName + "\'";
1146 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1150 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1151 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1155 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1156 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1161 //-----------------------------------------------------------------------------
1162 int FILES_STORE::WriteUserChgLog(const string & login,
1163 const string & admLogin,
1165 const string & paramName,
1166 const string & oldValue,
1167 const string & newValue,
1168 const string & message) const
1170 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1171 + paramName + "\' parameter changed from \'" + oldValue +
1172 "\' to \'" + newValue + "\'. " + message;
1174 return WriteLogString(userLogMsg, login);
1176 //-----------------------------------------------------------------------------
1177 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1179 string logStr = "Connect, " + inet_ntostring(ip);
1180 if (WriteLogString(logStr, login))
1182 return WriteLog2String(logStr, login);
1184 //-----------------------------------------------------------------------------
1185 int FILES_STORE::WriteUserDisconnect(const string & login,
1186 const DIR_TRAFF & up,
1187 const DIR_TRAFF & down,
1188 const DIR_TRAFF & sessionUp,
1189 const DIR_TRAFF & sessionDown,
1192 const std::string & reason) const
1194 stringstream logStr;
1195 logStr << "Disconnect, "
1196 << " session upload: \'"
1198 << "\' session download: \'"
1200 << "\' month upload: \'"
1202 << "\' month download: \'"
1208 if (WriteLogString(logStr.str(), login))
1211 logStr << " freeMb: \'"
1218 return WriteLog2String(logStr.str(), login);
1220 //-----------------------------------------------------------------------------
1221 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1226 strprintf(&str,"%s/%s/stat.%d.%02d",
1227 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1236 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1237 errorStr = "Cannot create file " + str;
1238 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1244 for (int i = 0; i < DIR_NUM; i++)
1246 snprintf(dirName, 3, "U%d", i);
1247 s.WriteInt(dirName, stat.up[i]);
1248 snprintf(dirName, 3, "D%d", i);
1249 s.WriteInt(dirName, stat.down[i]);
1252 s.WriteDouble("cash", stat.cash);
1256 //-----------------------------------------------------------------------------*/
1257 int FILES_STORE::AddAdmin(const string & login) const
1260 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1262 if (Touch(fileName))
1264 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1265 errorStr = "Cannot create file " + fileName;
1266 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1272 //-----------------------------------------------------------------------------*/
1273 int FILES_STORE::DelAdmin(const string & login) const
1276 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1277 if (unlink(fileName.c_str()))
1279 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1280 errorStr = "unlink failed. Message: '";
1281 errorStr += strerror(errno);
1283 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1287 //-----------------------------------------------------------------------------*/
1288 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1290 char passwordE[2 * ADM_PASSWD_LEN + 2];
1291 char pass[ADM_PASSWD_LEN + 1];
1292 char adminPass[ADM_PASSWD_LEN + 1];
1296 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1298 Touch(fileName + ".new");
1301 CONFIGFILE cf(fileName + ".new");
1307 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1308 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1309 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1313 memset(pass, 0, sizeof(pass));
1314 memset(adminPass, 0, sizeof(adminPass));
1317 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1319 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1320 adminPass[ADM_PASSWD_LEN - 1] = 0;
1322 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1324 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1327 pass[ADM_PASSWD_LEN - 1] = 0;
1328 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1329 //printfd(__FILE__, "passwordE %s\n", passwordE);
1331 cf.WriteString("password", passwordE);
1332 cf.WriteInt("ChgConf", ac.priv.userConf);
1333 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1334 cf.WriteInt("ChgStat", ac.priv.userStat);
1335 cf.WriteInt("ChgCash", ac.priv.userCash);
1336 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1337 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1338 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1341 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1343 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1344 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1345 printfd(__FILE__, "FILES_STORE::SaveAdmin - rename failed. Message: '%s'\n", strerror(errno));
1351 //-----------------------------------------------------------------------------
1352 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1355 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1356 CONFIGFILE cf(fileName);
1357 char pass[ADM_PASSWD_LEN + 1];
1358 char password[ADM_PASSWD_LEN + 1];
1359 char passwordE[2 * ADM_PASSWD_LEN + 2];
1366 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1367 errorStr = "Cannot open " + fileName;
1368 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1374 if (cf.ReadString("password", &p, "*"))
1376 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1377 errorStr = "Error in parameter password";
1378 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1382 memset(passwordE, 0, sizeof(passwordE));
1383 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1385 //printfd(__FILE__, "passwordE %s\n", passwordE);
1387 memset(pass, 0, sizeof(pass));
1389 if (passwordE[0] != 0)
1391 Decode21(pass, passwordE);
1392 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1394 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1396 DecodeString(password + 8*i, pass + 8*i, &ctx);
1404 ac->password = password;
1406 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1407 ac->priv.userConf = a;
1410 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1411 errorStr = "Error in parameter ChgConf";
1412 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1416 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1417 ac->priv.userPasswd = a;
1420 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1421 errorStr = "Error in parameter ChgPassword";
1422 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1426 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1427 ac->priv.userStat = a;
1430 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1431 errorStr = "Error in parameter ChgStat";
1432 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1436 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1437 ac->priv.userCash = a;
1440 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1441 errorStr = "Error in parameter ChgCash";
1442 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1446 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1447 ac->priv.userAddDel = a;
1450 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1451 errorStr = "Error in parameter UsrAddDel";
1452 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1456 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1457 ac->priv.adminChg = a;
1460 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1461 errorStr = "Error in parameter ChgAdmin";
1462 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1466 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1467 ac->priv.tariffChg = a;
1470 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1471 errorStr = "Error in parameter ChgTariff";
1472 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1478 //-----------------------------------------------------------------------------
1479 int FILES_STORE::AddTariff(const string & name) const
1482 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1483 if (Touch(fileName))
1485 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1486 errorStr = "Cannot create file " + fileName;
1487 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1492 //-----------------------------------------------------------------------------
1493 int FILES_STORE::DelTariff(const string & name) const
1496 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1497 if (unlink(fileName.c_str()))
1499 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1500 errorStr = "unlink failed. Message: '";
1501 errorStr += strerror(errno);
1503 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1507 //-----------------------------------------------------------------------------
1508 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1510 string tariffFileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1511 CONFIGFILE conf(tariffFileName);
1513 td->tariffConf.name = tariffName;
1515 if (conf.Error() != 0)
1517 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1518 errorStr = "Cannot read file " + tariffFileName;
1519 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1524 for (int i = 0; i<DIR_NUM; i++)
1526 strprintf(¶m, "Time%d", i);
1527 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1529 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1530 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1531 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1535 ParseTariffTimeStr(str.c_str(),
1536 td->dirPrice[i].hDay,
1537 td->dirPrice[i].mDay,
1538 td->dirPrice[i].hNight,
1539 td->dirPrice[i].mNight);
1541 strprintf(¶m, "PriceDayA%d", i);
1542 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1544 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1545 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1546 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1549 td->dirPrice[i].priceDayA /= (1024*1024);
1551 strprintf(¶m, "PriceDayB%d", i);
1552 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1554 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1555 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1556 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1559 td->dirPrice[i].priceDayB /= (1024*1024);
1561 strprintf(¶m, "PriceNightA%d", i);
1562 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1564 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1565 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1566 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1569 td->dirPrice[i].priceNightA /= (1024*1024);
1571 strprintf(¶m, "PriceNightB%d", i);
1572 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1574 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1575 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1576 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1579 td->dirPrice[i].priceNightB /= (1024*1024);
1581 strprintf(¶m, "Threshold%d", i);
1582 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1584 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1585 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1586 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1590 strprintf(¶m, "SinglePrice%d", i);
1591 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1593 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1594 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1595 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1599 strprintf(¶m, "NoDiscount%d", i);
1600 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1602 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1603 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1604 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1609 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1611 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1612 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1613 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1617 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1619 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1620 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1621 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1625 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1627 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1628 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1629 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1633 if (conf.ReadString("TraffType", &str, "") < 0)
1635 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1636 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1637 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1641 if (!strcasecmp(str.c_str(), "up"))
1642 td->tariffConf.traffType = TRAFF_UP;
1644 if (!strcasecmp(str.c_str(), "down"))
1645 td->tariffConf.traffType = TRAFF_DOWN;
1647 if (!strcasecmp(str.c_str(), "up+down"))
1648 td->tariffConf.traffType = TRAFF_UP_DOWN;
1650 if (!strcasecmp(str.c_str(), "max"))
1651 td->tariffConf.traffType = TRAFF_MAX;
1654 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1655 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1656 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1661 //-----------------------------------------------------------------------------
1662 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1664 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1666 Touch(fileName + ".new");
1669 CONFIGFILE cf(fileName + ".new");
1675 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1676 errorStr = "Error writing tariff " + tariffName;
1677 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1682 for (int i = 0; i < DIR_NUM; i++)
1684 strprintf(¶m, "PriceDayA%d", i);
1685 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1687 strprintf(¶m, "PriceDayB%d", i);
1688 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1690 strprintf(¶m, "PriceNightA%d", i);
1691 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1693 strprintf(¶m, "PriceNightB%d", i);
1694 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1696 strprintf(¶m, "Threshold%d", i);
1697 cf.WriteInt(param, td.dirPrice[i].threshold);
1700 strprintf(¶m, "Time%d", i);
1702 strprintf(&s, "%0d:%0d-%0d:%0d",
1703 td.dirPrice[i].hDay,
1704 td.dirPrice[i].mDay,
1705 td.dirPrice[i].hNight,
1706 td.dirPrice[i].mNight);
1708 cf.WriteString(param, s);
1710 strprintf(¶m, "NoDiscount%d", i);
1711 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1713 strprintf(¶m, "SinglePrice%d", i);
1714 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1717 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1718 cf.WriteDouble("Fee", td.tariffConf.fee);
1719 cf.WriteDouble("Free", td.tariffConf.free);
1721 switch (td.tariffConf.traffType)
1724 cf.WriteString("TraffType", "up");
1727 cf.WriteString("TraffType", "down");
1730 cf.WriteString("TraffType", "up+down");
1733 cf.WriteString("TraffType", "max");
1738 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1740 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1741 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1742 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1748 //-----------------------------------------------------------------------------
1749 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1751 const string & login) const
1753 char fn[FN_STR_LEN];
1754 char dn[FN_STR_LEN];
1761 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1762 if (access(dn, F_OK) != 0)
1764 if (mkdir(dn, 0700) != 0)
1766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1767 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1768 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1773 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1774 e += chmod(dn, storeSettings.GetStatModeDir());
1778 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1779 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1784 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1790 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1791 storeSettings.GetUsersDir().c_str(),
1795 if (access(dn, F_OK) != 0)
1797 if (mkdir(dn, 0700) != 0)
1799 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1800 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1801 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1806 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1807 e += chmod(dn, storeSettings.GetStatModeDir());
1811 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1812 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1815 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1816 storeSettings.GetUsersDir().c_str(),
1819 lt->tm_mon+1 < 10 ? "0" : "",
1821 if (access(dn, F_OK) != 0)
1823 if (mkdir(dn, 0700) != 0)
1825 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1826 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1827 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1832 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1833 e += chmod(dn, storeSettings.GetStatModeDir());
1837 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1838 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1841 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1843 statFile = fopen (fn, "at");
1847 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1848 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1849 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1856 lt1 = localtime(&lastStat);
1865 lt2 = localtime(&t);
1871 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1872 h1, m1, s1, h2, m2, s2) < 0)
1874 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1875 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1876 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1881 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1882 stIter = statTree.begin();
1884 while (stIter != statTree.end())
1887 x2str(stIter->second.up, u);
1888 x2str(stIter->second.down, d);
1889 #ifdef TRAFF_STAT_WITH_PORTS
1890 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1891 inet_ntostring(stIter->first.ip).c_str(),
1896 stIter->second.cash) < 0)
1898 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1899 errorStr = "fprint failed. Message: '";
1900 errorStr += strerror(errno);
1902 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1907 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1908 inet_ntostring(stIter->first.ip).c_str(),
1912 stIter->second.cash) < 0)
1914 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1915 errorStr = string("fprint failed. Message: '");
1916 errorStr += strerror(errno);
1918 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1929 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1930 e += chmod(fn, storeSettings.GetStatMode());
1934 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1935 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1940 //-----------------------------------------------------------------------------
1941 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1943 //ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1944 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1945 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1951 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1952 if (access(dn.c_str(), F_OK) != 0)
1954 if (mkdir(dn.c_str(), 0700) != 0)
1956 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1957 errorStr = "Directory \'";
1959 errorStr += "\' cannot be created.";
1960 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1965 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1967 gettimeofday(&tv, NULL);
1969 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1970 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1974 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1975 errorStr = "File \'";
1977 errorStr += "\' cannot be writen.";
1978 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1982 return EditMessage(*msg, login);
1984 //-----------------------------------------------------------------------------
1985 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1987 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1988 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1989 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1994 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1996 if (access(fileName.c_str(), F_OK) != 0)
1999 x2str(msg.header.id, idstr);
2000 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2001 errorStr = "Message for user \'";
2002 errorStr += login + "\' with ID \'";
2003 errorStr += idstr + "\' does not exist.";
2004 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
2008 Touch(fileName + ".new");
2010 msgFile = fopen((fileName + ".new").c_str(), "wt");
2013 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2014 errorStr = "File \'" + fileName + "\' cannot be writen.";
2015 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
2020 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
2021 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
2022 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
2023 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
2024 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
2025 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
2026 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
2030 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2031 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
2032 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
2038 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
2040 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
2042 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2043 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
2044 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
2050 //-----------------------------------------------------------------------------
2051 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
2054 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2055 msg->header.id = id;
2056 return ReadMessage(fn, &msg->header, &msg->text);
2058 //-----------------------------------------------------------------------------
2059 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
2062 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2064 return unlink(fn.c_str());
2066 //-----------------------------------------------------------------------------
2067 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2069 vector<string> messages;
2071 dn = storeSettings.GetUsersDir() + "/" + login + "/messages/";
2072 GetFilesList(&messages, dn, S_IFREG, "");
2074 //hdrsList->resize(messages.size());
2076 for (unsigned i = 0; i < messages.size(); i++)
2078 unsigned long long id = 0;
2080 if (str2x(messages[i].c_str(), id))
2082 if (unlink((dn + messages[i]).c_str()))
2084 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2085 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2086 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2093 if (ReadMessage(dn + messages[i], &hdr, NULL))
2100 if (unlink((dn + messages[i]).c_str()))
2102 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2103 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2104 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2111 hdrsList->push_back(hdr);
2115 //-----------------------------------------------------------------------------
2116 int FILES_STORE::ReadMessage(const string & fileName,
2118 string * text) const
2121 msgFile = fopen(fileName.c_str(), "rt");
2124 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2125 errorStr = "File \'";
2126 errorStr += fileName;
2127 errorStr += "\' cannot be openned.";
2128 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2134 d[1] = &hdr->lastSendTime;
2135 d[2] = &hdr->creationTime;
2136 d[3] = &hdr->showTime;
2137 d[4] = (unsigned*)(&hdr->repeat);
2138 d[5] = &hdr->repeatPeriod;
2140 memset(p, 0, sizeof(p));
2142 for (int pos = 0; pos < 6; pos++)
2144 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2145 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2146 errorStr = "Cannot read file \'";
2147 errorStr += fileName;
2148 errorStr += "\'. Missing data.";
2149 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2150 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2156 ep = strrchr(p, '\r');
2158 ep = strrchr(p, '\n');
2163 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2164 errorStr = "Cannot read file \'";
2165 errorStr += fileName;
2166 errorStr += "\'. Missing data.";
2167 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2168 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2173 if (str2x(p, *(d[pos])))
2175 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2176 errorStr = "Cannot read file \'";
2177 errorStr += fileName;
2178 errorStr += "\'. Incorrect value. \'";
2181 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2188 memset(txt, 0, sizeof(txt));
2191 text->erase(text->begin(), text->end());
2192 while (!feof(msgFile))
2195 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2205 //-----------------------------------------------------------------------------
2206 int FILES_STORE::Touch(const std::string & path) const
2208 FILE * f = fopen(path.c_str(), "wb");
2216 //-----------------------------------------------------------------------------